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