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