pull-prototypes-to-head-20020821
[openafs.git] / src / vol / physio.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11         System:         VICE-TWO
12         Module:         physio.c
13         Institution:    The Information Technology Center, Carnegie-Mellon University
14
15  */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 RCSID("$Header$");
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <errno.h>
25 #ifdef AFS_NT40_ENV
26 #include <fcntl.h>
27 #else
28 #include <sys/file.h>
29 #include <unistd.h>
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #else
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #endif
37 #ifdef  AFS_SUN5_ENV
38 #include <sys/fcntl.h>
39 #endif
40 #endif
41 #include <rx/xdr.h>
42 #include <afs/afsint.h>
43 #include <errno.h>
44 #include <afs/afssyscalls.h>
45 #include "ihandle.h"
46 #include "nfs.h"
47 #include "salvage.h"
48 #include "afs/assert.h"
49 #include "afs/dir.h"
50
51 /* returns 0 on success, errno on failure */
52 int ReallyRead (file, block, data)
53 DirHandle     * file;
54 int             block;
55 char          * data;
56 {
57     FdHandle_t *fdP;
58     int code;
59     errno = 0;
60     fdP = IH_OPEN(file->dirh_handle);
61     if (fdP == NULL) {
62         code = errno;
63         return code;
64     }
65     if (FDH_SEEK(fdP, block*AFS_PAGESIZE, SEEK_SET) < 0) {
66         code = errno;
67         FDH_REALLYCLOSE(fdP);
68         return code;
69     }
70     code = FDH_READ(fdP, data, AFS_PAGESIZE);
71     if (code != AFS_PAGESIZE) {
72         if (code < 0)
73             code = errno;
74         else 
75             code = EIO;
76         FDH_REALLYCLOSE(fdP);
77         return code;
78     }
79     FDH_CLOSE(fdP);
80     return 0;
81 }
82
83 /* returns 0 on success, errno on failure */
84 int ReallyWrite (file, block, data)
85 DirHandle     * file;
86 int             block;
87 char          * data;
88 {
89     FdHandle_t *fdP;
90     extern int VolumeChanged;
91     int code;
92
93     errno = 0;
94
95     fdP = IH_OPEN(file->dirh_handle);
96     if (fdP == NULL) {
97         code = errno;
98         return code;
99     }
100     if (FDH_SEEK(fdP, block*AFS_PAGESIZE, SEEK_SET) < 0) {
101         code = errno;
102         FDH_REALLYCLOSE(fdP);
103         return code;
104     }
105     code = FDH_WRITE(fdP, data, AFS_PAGESIZE);
106     if (code != AFS_PAGESIZE) {
107         if (code < 0)
108             code = errno;
109         else 
110             code = EIO;
111         FDH_REALLYCLOSE(fdP);
112         return code;
113     }
114     FDH_CLOSE(fdP);
115     VolumeChanged = 1;
116     return 0;
117 }
118
119 /* SetSalvageDirHandle:
120  * Create a handle to a directory entry and reference it (IH_INIT).
121  * The handle needs to be dereferenced with the FidZap() routine.
122  */
123 SetSalvageDirHandle(dir, volume, device, inode)
124 DirHandle *dir;
125 afs_int32 volume;
126 Inode inode;
127 Device device;
128 {
129     static SalvageCacheCheck = 1;
130     memset(dir, 0, sizeof(DirHandle));
131
132     dir->dirh_device = device;
133     dir->dirh_volume = volume;
134     dir->dirh_inode = inode;
135     IH_INIT(dir->dirh_handle, device, volume,  inode);
136     /* Always re-read for a new dirhandle */
137     dir->dirh_cacheCheck = SalvageCacheCheck++;
138 }
139
140 FidZap (file)
141 DirHandle     * file;
142
143 {
144     IH_RELEASE(file->dirh_handle);
145     memset(file, 0, sizeof(DirHandle));
146 }
147
148 FidZero (file)
149 DirHandle     * file;
150
151 {
152     memset(file, 0, sizeof(DirHandle));
153 }
154
155 FidEq (afile, bfile)
156 DirHandle      * afile;
157 DirHandle      * bfile;
158
159 {
160     if (afile->dirh_volume != bfile->dirh_volume) return 0;
161     if (afile->dirh_device != bfile->dirh_device) return 0;
162     if (afile->dirh_cacheCheck != bfile->dirh_cacheCheck) return 0;
163     if (afile->dirh_inode != bfile->dirh_inode) return 0;
164     return 1;
165 }
166
167 FidVolEq (afile, vid)
168 DirHandle      * afile;
169 afs_int32            vid;
170
171 {
172     if (afile->dirh_volume != vid) return 0;
173     return 1;
174 }
175
176 FidCpy (tofile, fromfile)
177 DirHandle      * tofile;
178 DirHandle      * fromfile;
179
180 {
181     *tofile = *fromfile;
182     IH_COPY(tofile->dirh_handle, fromfile->dirh_handle);
183 }
184
185 Die (msg)
186 char  * msg;
187
188 {
189     printf("%s\n",msg);
190     assert(1==2);
191 }
192