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