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