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