Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / vol / purge.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: purge.c
13 Institution: The Information Technology Center, Carnegie-Mellon University
14
15 */
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19 #include <roken.h>
20
21 #ifdef HAVE_SYS_FILE_H
22 #include <sys/file.h>
23 #endif
24
25 #include <afs/afsutil.h>
26 #include <rx/rx_queue.h>
27
28 #include <rx/xdr.h>
29 #include "afs/afsint.h"
30 #include "nfs.h"
31 #include "lwp.h"
32 #include "lock.h"
33 #include <afs/afssyscalls.h>
34 #include "ihandle.h"
35 #ifdef AFS_NT40_ENV
36 #include "ntops.h"
37 #endif
38 #include "vnode.h"
39 #include "volume.h"
40 #include "viceinode.h"
41 #include "partition.h"
42 #include "daemon_com.h"
43 #include "fssync.h"
44 #include "common.h"
45
46 /* forward declarations */
47 static int ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
48 afs_foff_t * aoffset);
49 #if 0
50 static void PurgeIndex(Volume * vp, VnodeClass class);
51 static void PurgeHeader(Volume * vp);
52 #endif
53
54 static void PurgeIndex_r(Volume * vp, VnodeClass class);
55 static void PurgeHeader_r(Volume * vp);
56
57 /* No lock needed. Only the volserver will call this, and only one transaction
58 * can have a given volume (volid/partition pair) in use at a time
59 */
60 void
61 VPurgeVolume(Error * ec, Volume * vp)
62 {
63 struct DiskPartition64 *tpartp = vp->partition;
64 VolumeId volid, parent;
65 afs_int32 code;
66
67 volid = V_id(vp);
68 parent = V_parentId(vp);
69
70 /* so VCheckDetach doesn't try to update the volume header and
71 * dump spurious errors into the logs */
72 V_inUse(vp) = 0;
73
74 /* N.B. it's important here to use the partition pointed to by the
75 * volume header. This routine can, under some circumstances, be called
76 * when two volumes with the same id exist on different partitions.
77 */
78 PurgeIndex_r(vp, vLarge);
79 PurgeIndex_r(vp, vSmall);
80 PurgeHeader_r(vp);
81
82 code = VDestroyVolumeDiskHeader(tpartp, volid, parent);
83 if (code) {
84 Log("VPurgeVolume: Error %ld when destroying volume %lu header\n",
85 afs_printable_int32_ld(code),
86 afs_printable_uint32_lu(volid));
87 }
88
89 /*
90 * Call the fileserver to break all call backs for that volume
91 */
92 FSYNC_VolOp(V_id(vp), tpartp->name, FSYNC_VOL_BREAKCBKS, 0, NULL);
93 }
94
95 #define MAXOBLITATONCE 200
96 /* delete a portion of an index, adjusting offset appropriately. Returns 0 if
97 things work and we should be called again, 1 if success full and done, and -1
98 if an error occurred. It adjusts offset appropriately on 0 or 1 return codes,
99 and otherwise doesn't touch it */
100 static int
101 ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
102 afs_foff_t * aoffset)
103 {
104 struct VnodeClassInfo *vcp;
105 Inode inodes[MAXOBLITATONCE];
106 afs_int32 iindex, nscanned;
107 afs_foff_t offset;
108 char buf[SIZEOF_LARGEDISKVNODE];
109 int hitEOF;
110 int i;
111 afs_int32 code;
112 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
113
114 hitEOF = 0;
115 vcp = &VnodeClassInfo[aclass];
116 offset = *aoffset; /* original offset */
117 iindex = 0;
118 nscanned = 0;
119 /* advance over up to MAXOBLITATONCE inodes. nscanned tells us how many we examined.
120 * We remember the inodes in an array, and idec them after zeroing them in the index.
121 * The reason for these contortions is to make volume deletion idempotent, even
122 * if we crash in the middle of a delete operation. */
123 STREAM_ASEEK(afile, offset);
124 while (1) {
125 if (iindex >= MAXOBLITATONCE) {
126 break;
127 }
128 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
129 nscanned++;
130 offset += vcp->diskSize;
131 if (code != 1) {
132 hitEOF = 1;
133 break;
134 }
135 if (vnode->type != vNull) {
136 if (vnode->vnodeMagic != vcp->magic)
137 goto fail; /* something really wrong; let salvager take care of it */
138 if (VNDISK_GET_INO(vnode))
139 inodes[iindex++] = VNDISK_GET_INO(vnode);
140 }
141 }
142
143 /* next, obliterate the index and fflush (and fsync) it */
144 STREAM_ASEEK(afile, *aoffset); /* seek back to start of vnode index region */
145 memset(buf, 0, sizeof(buf)); /* zero out our proto-vnode */
146 for (i = 0; i < nscanned; i++) {
147 if (STREAM_WRITE(buf, vcp->diskSize, 1, afile) != 1)
148 goto fail;
149 }
150 STREAM_FLUSH(afile); /* ensure 0s are on the disk */
151 OS_SYNC(afile->str_fd);
152
153 /* finally, do the idec's */
154 for (i = 0; i < iindex; i++) {
155 IH_DEC(V_linkHandle(avp), inodes[i], V_parentId(avp));
156 DOPOLL;
157 }
158
159 /* return the new offset */
160 *aoffset = offset;
161 return hitEOF; /* return 1 if hit EOF (don't call again), otherwise 0 */
162
163 fail:
164 return -1;
165 }
166
167 #if 0
168 static void
169 PurgeIndex(Volume * vp, VnodeClass class)
170 {
171 VOL_LOCK;
172 PurgeIndex_r(vp, class);
173 VOL_UNLOCK;
174 }
175 #endif
176
177 static void
178 PurgeIndex_r(Volume * vp, VnodeClass class)
179 {
180 StreamHandle_t *ifile;
181 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
182 afs_foff_t offset;
183 afs_int32 code;
184 FdHandle_t *fdP;
185
186
187 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
188 if (fdP == NULL)
189 return;
190
191 ifile = FDH_FDOPEN(fdP, "r+");
192 if (!ifile) {
193 FDH_REALLYCLOSE(fdP);
194 return;
195 }
196
197 offset = vcp->diskSize;
198 while (1) {
199 code = ObliterateRegion(vp, class, ifile, &offset);
200 if (code)
201 break; /* if error or hit EOF */
202 }
203 STREAM_CLOSE(ifile);
204 FDH_CLOSE(fdP);
205 }
206
207 #if 0
208 static void
209 PurgeHeader(Volume * vp)
210 {
211 VOL_LOCK;
212 PurgeHeader_r(vp);
213 VOL_UNLOCK;
214 }
215 #endif
216
217 static void
218 PurgeHeader_r(Volume * vp)
219 {
220 #ifndef AFS_NAMEI_ENV
221 /* namei opens and closes the given ihandle during IH_DEC, so don't try to
222 * also close it here */
223 IH_REALLYCLOSE(V_diskDataHandle(vp));
224 #endif
225 IH_DEC(V_linkHandle(vp), vp->vnodeIndex[vLarge].handle->ih_ino, V_id(vp));
226 IH_DEC(V_linkHandle(vp), vp->vnodeIndex[vSmall].handle->ih_ino, V_id(vp));
227 IH_DEC(V_linkHandle(vp), vp->diskDataHandle->ih_ino, V_id(vp));
228 #ifdef AFS_NAMEI_ENV
229 /* And last, but not least, the link count table itself. */
230 IH_DEC(V_linkHandle(vp), vp->linkHandle->ih_ino, V_parentId(vp));
231 #endif
232 }