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>
21 #ifdef HAVE_SYS_FILE_H
25 #include <afs/afsutil.h>
28 #include "afs/afsint.h"
32 #include <afs/afssyscalls.h>
39 #include "viceinode.h"
40 #include "partition.h"
41 #include "daemon_com.h"
45 /* forward declarations */
46 static int ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
47 afs_foff_t * aoffset);
49 static void PurgeIndex(Volume * vp, VnodeClass class);
50 static void PurgeHeader(Volume * vp);
53 static void PurgeIndex_r(Volume * vp, VnodeClass class);
54 static void PurgeHeader_r(Volume * vp);
56 /* No lock needed. Only the volserver will call this, and only one transaction
57 * can have a given volume (volid/partition pair) in use at a time
60 VPurgeVolume(Error * ec, Volume * vp)
62 struct DiskPartition64 *tpartp = vp->partition;
63 VolumeId volid, parent;
67 parent = V_parentId(vp);
69 /* so VCheckDetach doesn't try to update the volume header and
70 * dump spurious errors into the logs */
73 /* N.B. it's important here to use the partition pointed to by the
74 * volume header. This routine can, under some circumstances, be called
75 * when two volumes with the same id exist on different partitions.
77 PurgeIndex_r(vp, vLarge);
78 PurgeIndex_r(vp, vSmall);
81 code = VDestroyVolumeDiskHeader(tpartp, volid, parent);
83 Log("VPurgeVolume: Error %ld when destroying volume %lu header\n",
84 afs_printable_int32_ld(code),
85 afs_printable_uint32_lu(volid));
89 * Call the fileserver to break all call backs for that volume
91 FSYNC_VolOp(V_id(vp), tpartp->name, FSYNC_VOL_BREAKCBKS, 0, NULL);
94 #define MAXOBLITATONCE 200
95 /* delete a portion of an index, adjusting offset appropriately. Returns 0 if
96 things work and we should be called again, 1 if success full and done, and -1
97 if an error occurred. It adjusts offset appropriately on 0 or 1 return codes,
98 and otherwise doesn't touch it */
100 ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
101 afs_foff_t * aoffset)
103 struct VnodeClassInfo *vcp;
104 Inode inodes[MAXOBLITATONCE];
105 afs_int32 iindex, nscanned;
107 char buf[SIZEOF_LARGEDISKVNODE];
111 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
114 vcp = &VnodeClassInfo[aclass];
115 offset = *aoffset; /* original offset */
118 /* advance over up to MAXOBLITATONCE inodes. nscanned tells us how many we examined.
119 * We remember the inodes in an array, and idec them after zeroing them in the index.
120 * The reason for these contortions is to make volume deletion idempotent, even
121 * if we crash in the middle of a delete operation. */
122 STREAM_ASEEK(afile, offset);
124 if (iindex >= MAXOBLITATONCE) {
127 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
129 offset += vcp->diskSize;
134 if (vnode->type != vNull) {
135 if (vnode->vnodeMagic != vcp->magic)
136 goto fail; /* something really wrong; let salvager take care of it */
137 if (VNDISK_GET_INO(vnode))
138 inodes[iindex++] = VNDISK_GET_INO(vnode);
142 /* next, obliterate the index and fflush (and fsync) it */
143 STREAM_ASEEK(afile, *aoffset); /* seek back to start of vnode index region */
144 memset(buf, 0, sizeof(buf)); /* zero out our proto-vnode */
145 for (i = 0; i < nscanned; i++) {
146 if (STREAM_WRITE(buf, vcp->diskSize, 1, afile) != 1)
149 STREAM_FLUSH(afile); /* ensure 0s are on the disk */
150 OS_SYNC(afile->str_fd);
152 /* finally, do the idec's */
153 for (i = 0; i < iindex; i++) {
154 IH_DEC(V_linkHandle(avp), inodes[i], V_parentId(avp));
158 /* return the new offset */
160 return hitEOF; /* return 1 if hit EOF (don't call again), otherwise 0 */
168 PurgeIndex(Volume * vp, VnodeClass class)
171 PurgeIndex_r(vp, class);
177 PurgeIndex_r(Volume * vp, VnodeClass class)
179 StreamHandle_t *ifile;
180 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
186 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
190 ifile = FDH_FDOPEN(fdP, "r+");
192 FDH_REALLYCLOSE(fdP);
196 offset = vcp->diskSize;
198 code = ObliterateRegion(vp, class, ifile, &offset);
200 break; /* if error or hit EOF */
208 PurgeHeader(Volume * vp)
217 PurgeHeader_r(Volume * vp)
219 IH_REALLYCLOSE(V_diskDataHandle(vp));
220 IH_DEC(V_linkHandle(vp), vp->vnodeIndex[vLarge].handle->ih_ino, V_id(vp));
221 IH_DEC(V_linkHandle(vp), vp->vnodeIndex[vSmall].handle->ih_ino, V_id(vp));
222 IH_DEC(V_linkHandle(vp), vp->diskDataHandle->ih_ino, V_id(vp));
224 /* And last, but not least, the link count table itself. */
225 IH_REALLYCLOSE(V_linkHandle(vp));
226 IH_DEC(V_linkHandle(vp), vp->linkHandle->ih_ino, V_parentId(vp));