2 * Copyright 2000, International Business Machines Corporation and others.
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
13 Institution: The Information Technology Center, Carnegie-Mellon University
16 #include <afsconfig.h>
17 #include <afs/param.h>
25 #include <sys/param.h>
32 #include <afs/assert.h>
33 #include <afs/afsutil.h>
36 #include "afs/afsint.h"
40 #include <afs/afssyscalls.h>
47 #include "viceinode.h"
48 #include "partition.h"
49 #include "daemon_com.h"
52 /* forward declarations */
53 static int ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
56 static void PurgeIndex(Volume * vp, VnodeClass class);
57 static void PurgeHeader(Volume * vp);
60 static void PurgeIndex_r(Volume * vp, VnodeClass class);
61 static void PurgeHeader_r(Volume * vp);
63 /*@printflike@*/ extern void Log(const char *format, ...);
65 /* No lock needed. Only the volserver will call this, and only one transaction
66 * can have a given volume (volid/partition pair) in use at a time
69 VPurgeVolume(Error * ec, Volume * vp)
71 struct DiskPartition64 *tpartp = vp->partition;
72 VolumeId volid, parent;
76 parent = V_parentId(vp);
78 /* so VCheckDetach doesn't try to update the volume header and
79 * dump spurious errors into the logs */
82 /* N.B. it's important here to use the partition pointed to by the
83 * volume header. This routine can, under some circumstances, be called
84 * when two volumes with the same id exist on different partitions.
86 PurgeIndex_r(vp, vLarge);
87 PurgeIndex_r(vp, vSmall);
90 code = VDestroyVolumeDiskHeader(tpartp, volid, parent);
92 Log("VPurgeVolume: Error %ld when destroying volume %lu header\n",
93 afs_printable_int32_ld(code),
94 afs_printable_uint32_lu(volid));
98 * Call the fileserver to break all call backs for that volume
100 FSYNC_VolOp(V_id(vp), tpartp->name, FSYNC_VOL_BREAKCBKS, 0, NULL);
103 #define MAXOBLITATONCE 200
104 /* delete a portion of an index, adjusting offset appropriately. Returns 0 if
105 things work and we should be called again, 1 if success full and done, and -1
106 if an error occurred. It adjusts offset appropriately on 0 or 1 return codes,
107 and otherwise doesn't touch it */
109 ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
112 register struct VnodeClassInfo *vcp;
113 Inode inodes[MAXOBLITATONCE];
114 register afs_int32 iindex, nscanned;
116 char buf[SIZEOF_LARGEDISKVNODE];
119 register afs_int32 code;
120 register struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
123 vcp = &VnodeClassInfo[aclass];
124 offset = *aoffset; /* original offset */
127 /* advance over up to MAXOBLITATONCE inodes. nscanned tells us how many we examined.
128 * We remember the inodes in an array, and idec them after zeroing them in the index.
129 * The reason for these contortions is to make volume deletion idempotent, even
130 * if we crash in the middle of a delete operation. */
131 STREAM_SEEK(afile, offset, 0);
133 if (iindex >= MAXOBLITATONCE) {
136 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
138 offset += vcp->diskSize;
143 if (vnode->type != vNull) {
144 if (vnode->vnodeMagic != vcp->magic)
145 goto fail; /* something really wrong; let salvager take care of it */
146 if (VNDISK_GET_INO(vnode))
147 inodes[iindex++] = VNDISK_GET_INO(vnode);
151 /* next, obliterate the index and fflush (and fsync) it */
152 STREAM_SEEK(afile, *aoffset, 0); /* seek back to start of vnode index region */
153 memset(buf, 0, sizeof(buf)); /* zero out our proto-vnode */
154 for (i = 0; i < nscanned; i++) {
155 if (STREAM_WRITE(buf, vcp->diskSize, 1, afile) != 1)
158 STREAM_FLUSH(afile); /* ensure 0s are on the disk */
159 OS_SYNC(afile->str_fd);
161 /* finally, do the idec's */
162 for (i = 0; i < iindex; i++) {
163 IH_DEC(V_linkHandle(avp), inodes[i], V_parentId(avp));
167 /* return the new offset */
169 return hitEOF; /* return 1 if hit EOF (don't call again), otherwise 0 */
177 PurgeIndex(Volume * vp, VnodeClass class)
180 PurgeIndex_r(vp, class);
186 PurgeIndex_r(Volume * vp, VnodeClass class)
188 StreamHandle_t *ifile;
189 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
191 register afs_int32 code;
195 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
199 ifile = FDH_FDOPEN(fdP, "r+");
201 FDH_REALLYCLOSE(fdP);
205 offset = vcp->diskSize;
207 code = ObliterateRegion(vp, class, ifile, &offset);
209 break; /* if error or hit EOF */
217 PurgeHeader(Volume * vp)
226 PurgeHeader_r(Volume * vp)
228 IH_REALLYCLOSE(V_diskDataHandle(vp));
229 IH_DEC(V_linkHandle(vp), vp->vnodeIndex[vLarge].handle->ih_ino, V_id(vp));
230 IH_DEC(V_linkHandle(vp), vp->vnodeIndex[vSmall].handle->ih_ino, V_id(vp));
231 IH_DEC(V_linkHandle(vp), vp->diskDataHandle->ih_ino, V_id(vp));
233 /* And last, but not least, the link count table itself. */
234 IH_REALLYCLOSE(V_linkHandle(vp));
235 IH_DEC(V_linkHandle(vp), vp->linkHandle->ih_ino, V_parentId(vp));