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