libroken: Build on windows
[openafs.git] / src / vol / 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 /*
11         System:         VICE-TWO
12         Module:         physio.c
13         Institution:    The Information Technology Center, Carnegie-Mellon University
14
15  */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 #include <roken.h>
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <errno.h>
25 #ifdef AFS_NT40_ENV
26 #include <fcntl.h>
27 #else
28 #include <sys/file.h>
29 #include <unistd.h>
30 #include <string.h>
31 #ifdef  AFS_SUN5_ENV
32 #include <sys/fcntl.h>
33 #endif
34 #endif
35 #include <rx/xdr.h>
36 #include <afs/afsint.h>
37 #include <errno.h>
38 #include <afs/afssyscalls.h>
39 #include "nfs.h"
40 #include "ihandle.h"
41 #include "salvage.h"
42 #include <afs/dir.h>
43 #include <assert.h>
44 #include "vol_internal.h"
45
46 /* returns 0 on success, errno on failure */
47 int
48 ReallyRead(DirHandle * file, int block, char *data)
49 {
50     FdHandle_t *fdP;
51     int code;
52     ssize_t nBytes;
53     errno = 0;
54     fdP = IH_OPEN(file->dirh_handle);
55     if (fdP == NULL) {
56         code = errno;
57         return code;
58     }
59     nBytes = FDH_PREAD(fdP, data, (afs_fsize_t) AFS_PAGESIZE,
60                        ((afs_foff_t)block) * AFS_PAGESIZE);
61     if (nBytes != AFS_PAGESIZE) {
62         if (nBytes < 0)
63             code = errno;
64         else
65             code = EIO;
66         FDH_REALLYCLOSE(fdP);
67         return code;
68     }
69     FDH_CLOSE(fdP);
70     return 0;
71 }
72
73 /* returns 0 on success, errno on failure */
74 int
75 ReallyWrite(DirHandle * file, int block, char *data)
76 {
77     FdHandle_t *fdP;
78     int code;
79     ssize_t nBytes;
80
81     errno = 0;
82
83     fdP = IH_OPEN(file->dirh_handle);
84     if (fdP == NULL) {
85         code = errno;
86         return code;
87     }
88     nBytes = FDH_PWRITE(fdP, data, (afs_fsize_t) AFS_PAGESIZE,
89                         ((afs_foff_t)block) * AFS_PAGESIZE);
90     if (nBytes != AFS_PAGESIZE) {
91         if (nBytes < 0)
92             code = errno;
93         else
94             code = EIO;
95         FDH_REALLYCLOSE(fdP);
96         return code;
97     }
98     FDH_CLOSE(fdP);
99     *(file->volumeChanged) = 1;
100     return 0;
101 }
102
103 /* SetSalvageDirHandle:
104  * Create a handle to a directory entry and reference it (IH_INIT).
105  * The handle needs to be dereferenced with the FidZap() routine.
106  */
107 void
108 SetSalvageDirHandle(DirHandle * dir, afs_int32 volume, Device device,
109                     Inode inode, int *volumeChanged)
110 {
111     static int SalvageCacheCheck = 1;
112     memset(dir, 0, sizeof(DirHandle));
113
114     dir->dirh_device = device;
115     dir->dirh_volume = volume;
116     dir->dirh_inode = inode;
117     IH_INIT(dir->dirh_handle, device, volume, inode);
118     /* Always re-read for a new dirhandle */
119     dir->dirh_cacheCheck = SalvageCacheCheck++;
120     dir->volumeChanged = volumeChanged;
121 }
122
123 void
124 FidZap(DirHandle * file)
125 {
126     IH_RELEASE(file->dirh_handle);
127     memset(file, 0, sizeof(DirHandle));
128 }
129
130 void
131 FidZero(DirHandle * file)
132 {
133     memset(file, 0, sizeof(DirHandle));
134 }
135
136 int
137 FidEq(DirHandle * afile, DirHandle * bfile)
138 {
139     if (afile->dirh_volume != bfile->dirh_volume)
140         return 0;
141     if (afile->dirh_device != bfile->dirh_device)
142         return 0;
143     if (afile->dirh_cacheCheck != bfile->dirh_cacheCheck)
144         return 0;
145     if (afile->dirh_inode != bfile->dirh_inode)
146         return 0;
147     return 1;
148 }
149
150 int
151 FidVolEq(DirHandle * afile, afs_int32 vid)
152 {
153     if (afile->dirh_volume != vid)
154         return 0;
155     return 1;
156 }
157
158 void
159 FidCpy(DirHandle * tofile, DirHandle * fromfile)
160 {
161     *tofile = *fromfile;
162     IH_COPY(tofile->dirh_handle, fromfile->dirh_handle);
163 }
164
165 void
166 Die(char *msg)
167 {
168     printf("%s\n", msg);
169     osi_Panic("%s\n", msg);
170 }