47d9a8c1bafb9956897b082ad9354732d7f8f969
[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     FdHandle_t *fdP;
54
55     fdP = IH_OPEN(file->dirh_handle);
56     if (fdP == NULL) {
57         code = errno;
58         ViceLog(0,
59                 ("ReallyRead(): open failed device %X inode %s errno %d\n",
60                  file->dirh_handle->ih_dev, PrintInode(NULL,
61                                                        file->dirh_handle->
62                                                        ih_ino), code));
63         return code;
64     }
65     if (FDH_SEEK(fdP, block * PAGESIZE, SEEK_SET) < 0) {
66         code = errno;
67         ViceLog(0,
68                 ("ReallyRead(): lseek failed device %X inode %s errno %d\n",
69                  file->dirh_handle->ih_dev, PrintInode(NULL,
70                                                        file->dirh_handle->
71                                                        ih_ino), code));
72         FDH_REALLYCLOSE(fdP);
73         return code;
74     }
75     code = FDH_READ(fdP, data, PAGESIZE);
76     if (code != PAGESIZE) {
77         if (code < 0)
78             code = errno;
79         else
80             code = EIO;
81         ViceLog(0,
82                 ("ReallyRead(): read failed device %X inode %s errno %d\n",
83                  file->dirh_handle->ih_dev, PrintInode(NULL,
84                                                        file->dirh_handle->
85                                                        ih_ino), code));
86         FDH_REALLYCLOSE(fdP);
87         return code;
88     }
89     FDH_CLOSE(fdP);
90     return 0;
91
92 }
93
94 /* returns 0 on success, errno on failure */
95 int
96 ReallyWrite(DirHandle * file, int block, char *data)
97 {
98     afs_int32 count;
99     FdHandle_t *fdP;
100
101     fdP = IH_OPEN(file->dirh_handle);
102     if (fdP == NULL) {
103         ViceLog(0,
104                 ("ReallyWrite(): open failed device %X inode %s errno %d\n",
105                  file->dirh_handle->ih_dev, PrintInode(NULL,
106                                                        file->dirh_handle->
107                                                        ih_ino), errno));
108         lpErrno = errno;
109         return 0;
110     }
111     if (FDH_SEEK(fdP, block * PAGESIZE, SEEK_SET) < 0) {
112         ViceLog(0,
113                 ("ReallyWrite(): lseek failed device %X inode %s errno %d\n",
114                  file->dirh_handle->ih_dev, PrintInode(NULL,
115                                                        file->dirh_handle->
116                                                        ih_ino), errno));
117         lpErrno = errno;
118         FDH_REALLYCLOSE(fdP);
119         return 0;
120     }
121     if ((count = FDH_WRITE(fdP, data, PAGESIZE)) != PAGESIZE) {
122         ViceLog(0,
123                 ("ReallyWrite(): write failed device %X inode %s errno %d\n",
124                  file->dirh_handle->ih_dev, PrintInode(NULL,
125                                                        file->dirh_handle->
126                                                        ih_ino), errno));
127         lpCount = count;
128         lpErrno = errno;
129         FDH_REALLYCLOSE(fdP);
130         return 0;
131     }
132     FDH_CLOSE(fdP);
133     return 0;
134 }
135
136
137 void
138 SetDirHandle(register DirHandle * dir, register Vnode * vnode)
139 {
140     register Volume *vp = vnode->volumePtr;
141     register IHandle_t *h;
142     IH_COPY(h, vnode->handle);
143     dir->dirh_ino = h->ih_ino;
144     dir->dirh_dev = h->ih_dev;
145     dir->dirh_vid = h->ih_vid;
146     dir->dirh_cacheCheck = vp->cacheCheck;
147     dir->dirh_unique = vnode->disk.uniquifier;
148     dir->dirh_vnode = vnode->vnodeNumber;
149     dir->dirh_handle = h;
150 }
151
152 void
153 FidZap(DirHandle * file)
154 {
155     IH_RELEASE(file->dirh_handle);
156     memset(file, 0, sizeof(DirHandle));
157 }
158
159 void
160 FidZero(DirHandle * file)
161 {
162     memset(file, 0, sizeof(DirHandle));
163 }
164
165 int
166 FidEq(DirHandle * afile, DirHandle * bfile)
167 {
168     if (afile->dirh_ino != bfile->dirh_ino)
169         return 0;
170     if (afile->dirh_dev != bfile->dirh_dev)
171         return 0;
172     if (afile->dirh_vid != bfile->dirh_vid)
173         return 0;
174     if (afile->dirh_cacheCheck != bfile->dirh_cacheCheck)
175         return 0;
176     if (afile->dirh_unique != bfile->dirh_unique)
177         return 0;
178     if (afile->dirh_vnode != bfile->dirh_vnode)
179         return 0;
180
181     return 1;
182 }
183
184 int
185 FidVolEq(DirHandle * afile, afs_int32 vid)
186 {
187     if (afile->dirh_vid != vid)
188         return 0;
189     return 1;
190 }
191
192 int
193 FidCpy(DirHandle * tofile, DirHandle * fromfile)
194 {
195     *tofile = *fromfile;
196     IH_COPY(tofile->dirh_handle, fromfile->dirh_handle);
197     return 0;
198 }