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