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