viced-rewrite-breaklatercallbacks-20030218
[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("$Header$");
20
21 #include <stdio.h>
22 #include <errno.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 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #else
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #endif
37 #include <afs/nfs.h>
38 #include <afs/assert.h>
39 #include <lwp.h>
40 #include <lock.h>
41 #include <time.h>
42 #include <afs/afsint.h>
43 #include <afs/ihandle.h>
44 #include <afs/vnode.h>
45 #include <afs/volume.h>
46 #include "viced_prototypes.h"
47 #include "viced.h"
48 #ifdef PAGESIZE
49 #undef PAGESIZE
50 #endif
51 #define PAGESIZE (afs_size_t)2048
52
53 extern int LogLevel;
54
55 afs_int32 lpErrno, lpCount;
56
57 /* returns 0 on success, errno on failure */
58 int ReallyRead (file, block, data)
59 DirHandle     * file;
60 afs_size_t      block;
61 char          * data;
62 {
63     int code;
64     FdHandle_t *fdP;
65
66     fdP = IH_OPEN(file->dirh_handle);
67     if (fdP == NULL) {
68         code = errno;
69         ViceLog (0,
70                  ("ReallyRead(): open failed device %X inode %s errno %d\n",
71                   file->dirh_handle->ih_dev,
72                   PrintInode(NULL, file->dirh_handle->ih_ino), code));
73         return code;
74     }
75     if (FDH_SEEK(fdP, (afs_size_t)block * PAGESIZE, SEEK_SET) < 0) {
76         code = errno;
77         ViceLog (0,
78                  ("ReallyRead(): lseek failed device %X inode %s errno %d\n",
79                   file->dirh_handle->ih_dev,
80                   PrintInode(NULL, file->dirh_handle->ih_ino), code));
81         FDH_REALLYCLOSE(fdP);
82         return code;
83     }
84     code = FDH_READ(fdP, data, PAGESIZE);
85     if (code != PAGESIZE) {
86         if (code < 0)
87             code = errno;
88         else
89             code = EIO;
90         ViceLog (0,
91                  ("ReallyRead(): read failed device %X inode %s errno %d\n",
92                   file->dirh_handle->ih_dev,
93                   PrintInode(NULL, file->dirh_handle->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 ReallyWrite (file, block, data)
104 DirHandle     * file;
105 afs_size_t      block;
106 char          * data;
107 {
108     afs_int32 count;
109     FdHandle_t *fdP;
110
111     fdP = IH_OPEN(file->dirh_handle);
112     if (fdP == NULL) {
113         ViceLog (0,
114                  ("ReallyWrite(): open failed device %X inode %s errno %d\n",
115                   file->dirh_handle->ih_dev,
116                   PrintInode(NULL, file->dirh_handle->ih_ino), errno));
117         lpErrno = errno;
118         return 0;
119     }
120     if (FDH_SEEK(fdP, (afs_size_t) block * PAGESIZE, SEEK_SET) < 0) {
121         ViceLog (0,
122                  ("ReallyWrite(): lseek failed device %X inode %s errno %d\n",
123                   file->dirh_handle->ih_dev,
124                   PrintInode(NULL, file->dirh_handle->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,
133                   PrintInode(NULL, file->dirh_handle->ih_ino), errno));
134         lpCount = count;
135         lpErrno = errno;
136         FDH_REALLYCLOSE(fdP);
137         return 0;
138     }
139     FDH_CLOSE(fdP);
140     return 0;
141 }
142
143
144 SetDirHandle(dir, vnode)
145 register DirHandle *dir;
146 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 FidZap (file)
161 DirHandle     * file;
162
163 {
164     IH_RELEASE(file->dirh_handle);
165     memset((char *)file, 0, sizeof(DirHandle));
166 }
167
168 FidZero (file)
169 DirHandle     * file;
170
171 {
172     memset((char *)file, 0, sizeof(DirHandle));
173 }
174
175 FidEq (afile, bfile)
176 DirHandle      * afile;
177 DirHandle      * bfile;
178
179 {
180     if (afile->dirh_ino != bfile->dirh_ino) return 0;
181     if (afile->dirh_dev != bfile->dirh_dev) return 0;
182     if (afile->dirh_vid != bfile->dirh_vid) return 0;
183     if (afile->dirh_cacheCheck != bfile->dirh_cacheCheck) return 0;
184     if (afile->dirh_unique != bfile->dirh_unique) return 0;
185     if (afile->dirh_vnode != bfile->dirh_vnode) return 0;
186
187     return 1;
188 }
189
190 FidVolEq (afile, vid)
191 DirHandle      * afile;
192 afs_int32            vid;
193
194 {
195     if (afile->dirh_vid != vid) return 0;
196     return 1;
197 }
198
199 FidCpy (tofile, fromfile)
200 DirHandle      * tofile;
201 DirHandle      * fromfile;
202
203 {
204     *tofile = *fromfile;
205     IH_COPY(tofile->dirh_handle, fromfile->dirh_handle);
206 }