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