openafs-string-header-cleanup-20071030
[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
21     ("$Header$");
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #ifdef AFS_NT40_ENV
27 #include <fcntl.h>
28 #else
29 #include <sys/file.h>
30 #include <unistd.h>
31 #include <string.h>
32 #ifdef  AFS_SUN5_ENV
33 #include <sys/fcntl.h>
34 #endif
35 #endif
36 #include <rx/xdr.h>
37 #include <afs/afsint.h>
38 #include <errno.h>
39 #include <afs/afssyscalls.h>
40 #include "nfs.h"
41 #include "ihandle.h"
42 #include "salvage.h"
43 #include "afs/assert.h"
44 #include "afs/dir.h"
45
46 /* returns 0 on success, errno on failure */
47 int
48 ReallyRead(DirHandle * file, int block, char *data)
49 {
50     FdHandle_t *fdP;
51     int code;
52     errno = 0;
53     fdP = IH_OPEN(file->dirh_handle);
54     if (fdP == NULL) {
55         code = errno;
56         return code;
57     }
58     if (FDH_SEEK(fdP, block * AFS_PAGESIZE, SEEK_SET) < 0) {
59         code = errno;
60         FDH_REALLYCLOSE(fdP);
61         return code;
62     }
63     code = FDH_READ(fdP, data, (afs_fsize_t) AFS_PAGESIZE);
64     if (code != AFS_PAGESIZE) {
65         if (code < 0)
66             code = errno;
67         else
68             code = EIO;
69         FDH_REALLYCLOSE(fdP);
70         return code;
71     }
72     FDH_CLOSE(fdP);
73     return 0;
74 }
75
76 /* returns 0 on success, errno on failure */
77 int
78 ReallyWrite(DirHandle * file, int block, char *data)
79 {
80     FdHandle_t *fdP;
81     extern int VolumeChanged;
82     int code;
83
84     errno = 0;
85
86     fdP = IH_OPEN(file->dirh_handle);
87     if (fdP == NULL) {
88         code = errno;
89         return code;
90     }
91     if (FDH_SEEK(fdP, block * AFS_PAGESIZE, SEEK_SET) < 0) {
92         code = errno;
93         FDH_REALLYCLOSE(fdP);
94         return code;
95     }
96     code = FDH_WRITE(fdP, data, (afs_fsize_t) AFS_PAGESIZE);
97     if (code != AFS_PAGESIZE) {
98         if (code < 0)
99             code = errno;
100         else
101             code = EIO;
102         FDH_REALLYCLOSE(fdP);
103         return code;
104     }
105     FDH_CLOSE(fdP);
106     VolumeChanged = 1;
107     return 0;
108 }
109
110 /* SetSalvageDirHandle:
111  * Create a handle to a directory entry and reference it (IH_INIT).
112  * The handle needs to be dereferenced with the FidZap() routine.
113  */
114 void
115 SetSalvageDirHandle(DirHandle * dir, afs_int32 volume, Device device,
116                     Inode inode)
117 {
118     static SalvageCacheCheck = 1;
119     memset(dir, 0, sizeof(DirHandle));
120
121     dir->dirh_device = device;
122     dir->dirh_volume = volume;
123     dir->dirh_inode = inode;
124     IH_INIT(dir->dirh_handle, device, volume, inode);
125     /* Always re-read for a new dirhandle */
126     dir->dirh_cacheCheck = SalvageCacheCheck++;
127 }
128
129 void
130 FidZap(DirHandle * file)
131 {
132     IH_RELEASE(file->dirh_handle);
133     memset(file, 0, sizeof(DirHandle));
134 }
135
136 void
137 FidZero(DirHandle * file)
138 {
139     memset(file, 0, sizeof(DirHandle));
140 }
141
142 int
143 FidEq(DirHandle * afile, DirHandle * bfile)
144 {
145     if (afile->dirh_volume != bfile->dirh_volume)
146         return 0;
147     if (afile->dirh_device != bfile->dirh_device)
148         return 0;
149     if (afile->dirh_cacheCheck != bfile->dirh_cacheCheck)
150         return 0;
151     if (afile->dirh_inode != bfile->dirh_inode)
152         return 0;
153     return 1;
154 }
155
156 int
157 FidVolEq(DirHandle * afile, afs_int32 vid)
158 {
159     if (afile->dirh_volume != vid)
160         return 0;
161     return 1;
162 }
163
164 void
165 FidCpy(DirHandle * tofile, DirHandle * fromfile)
166 {
167     *tofile = *fromfile;
168     IH_COPY(tofile->dirh_handle, fromfile->dirh_handle);
169 }
170
171 void
172 Die(char *msg)
173 {
174     printf("%s\n", msg);
175     assert(1 == 2);
176 }