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