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