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