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