death to register
[openafs.git] / src / viced / 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 /*  physio.c    - Physical I/O routines for the buffer package          */
11 /*                                                                      */
12 /*  Date: 5/1/85                                                        */
13 /*                                                                      */
14 /************************************************************************/
15
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #ifdef AFS_NT40_ENV
24 #include <fcntl.h>
25 #else
26 #include <sys/file.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #endif
30 #include <afs/nfs.h>
31 #include <afs/assert.h>
32 #include <lwp.h>
33 #include <lock.h>
34 #include <time.h>
35 #include <afs/afsint.h>
36 #include <afs/ihandle.h>
37 #include <afs/vnode.h>
38 #include <afs/volume.h>
39 #include "viced_prototypes.h"
40 #include "viced.h"
41 #ifdef PAGESIZE
42 #undef PAGESIZE
43 #endif
44 #define PAGESIZE 2048
45
46 afs_int32 lpErrno, lpCount;
47
48 /* returns 0 on success, errno on failure */
49 int
50 ReallyRead(DirHandle * file, int block, char *data)
51 {
52     int code;
53     ssize_t rdlen;
54     FdHandle_t *fdP;
55
56     fdP = IH_OPEN(file->dirh_handle);
57     if (fdP == NULL) {
58         code = errno;
59         ViceLog(0,
60                 ("ReallyRead(): open failed device %X inode %s errno %d\n",
61                  file->dirh_handle->ih_dev, PrintInode(NULL,
62                                                        file->dirh_handle->
63                                                        ih_ino), code));
64         return code;
65     }
66     if (FDH_SEEK(fdP, block * PAGESIZE, SEEK_SET) < 0) {
67         code = errno;
68         ViceLog(0,
69                 ("ReallyRead(): lseek failed device %X inode %s errno %d\n",
70                  file->dirh_handle->ih_dev, PrintInode(NULL,
71                                                        file->dirh_handle->
72                                                        ih_ino), code));
73         FDH_REALLYCLOSE(fdP);
74         return code;
75     }
76     rdlen = FDH_READ(fdP, data, PAGESIZE);
77     if (rdlen != PAGESIZE) {
78         if (rdlen < 0)
79             code = errno;
80         else
81             code = EIO;
82         ViceLog(0,
83                 ("ReallyRead(): read failed device %X inode %s errno %d\n",
84                  file->dirh_handle->ih_dev, PrintInode(NULL,
85                                                        file->dirh_handle->
86                                                        ih_ino), code));
87         FDH_REALLYCLOSE(fdP);
88         return code;
89     }
90     FDH_CLOSE(fdP);
91     return 0;
92
93 }
94
95 /* returns 0 on success, errno on failure */
96 int
97 ReallyWrite(DirHandle * file, int block, char *data)
98 {
99     ssize_t count;
100     FdHandle_t *fdP;
101
102     fdP = IH_OPEN(file->dirh_handle);
103     if (fdP == NULL) {
104         ViceLog(0,
105                 ("ReallyWrite(): open failed device %X inode %s errno %d\n",
106                  file->dirh_handle->ih_dev, PrintInode(NULL,
107                                                        file->dirh_handle->
108                                                        ih_ino), errno));
109         lpErrno = errno;
110         return 0;
111     }
112     if (FDH_SEEK(fdP, block * PAGESIZE, SEEK_SET) < 0) {
113         ViceLog(0,
114                 ("ReallyWrite(): lseek failed device %X inode %s errno %d\n",
115                  file->dirh_handle->ih_dev, PrintInode(NULL,
116                                                        file->dirh_handle->
117                                                        ih_ino), errno));
118         lpErrno = errno;
119         FDH_REALLYCLOSE(fdP);
120         return 0;
121     }
122     if ((count = FDH_WRITE(fdP, data, PAGESIZE)) != PAGESIZE) {
123         ViceLog(0,
124                 ("ReallyWrite(): write failed device %X inode %s errno %d\n",
125                  file->dirh_handle->ih_dev, PrintInode(NULL,
126                                                        file->dirh_handle->
127                                                        ih_ino), errno));
128         lpCount = count;
129         lpErrno = errno;
130         FDH_REALLYCLOSE(fdP);
131         return 0;
132     }
133     FDH_CLOSE(fdP);
134     return 0;
135 }
136
137
138 void
139 SetDirHandle(DirHandle * dir, Vnode * vnode)
140 {
141     Volume *vp = vnode->volumePtr;
142     IHandle_t *h;
143     IH_COPY(h, vnode->handle);
144     dir->dirh_ino = h->ih_ino;
145     dir->dirh_dev = h->ih_dev;
146     dir->dirh_vid = h->ih_vid;
147     dir->dirh_cacheCheck = vp->cacheCheck;
148     dir->dirh_unique = vnode->disk.uniquifier;
149     dir->dirh_vnode = vnode->vnodeNumber;
150     dir->dirh_handle = h;
151 }
152
153 void
154 FidZap(DirHandle * file)
155 {
156     IH_RELEASE(file->dirh_handle);
157     memset(file, 0, sizeof(DirHandle));
158 }
159
160 void
161 FidZero(DirHandle * file)
162 {
163     memset(file, 0, sizeof(DirHandle));
164 }
165
166 int
167 FidEq(DirHandle * afile, DirHandle * bfile)
168 {
169     if (afile->dirh_ino != bfile->dirh_ino)
170         return 0;
171     if (afile->dirh_dev != bfile->dirh_dev)
172         return 0;
173     if (afile->dirh_vid != bfile->dirh_vid)
174         return 0;
175     if (afile->dirh_cacheCheck != bfile->dirh_cacheCheck)
176         return 0;
177     if (afile->dirh_unique != bfile->dirh_unique)
178         return 0;
179     if (afile->dirh_vnode != bfile->dirh_vnode)
180         return 0;
181
182     return 1;
183 }
184
185 int
186 FidVolEq(DirHandle * afile, afs_int32 vid)
187 {
188     if (afile->dirh_vid != vid)
189         return 0;
190     return 1;
191 }
192
193 int
194 FidCpy(DirHandle * tofile, DirHandle * fromfile)
195 {
196     *tofile = *fromfile;
197     IH_COPY(tofile->dirh_handle, fromfile->dirh_handle);
198     return 0;
199 }