037f96f47fa3d3de722d5407e2fac997f500da79
[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 #include <stdio.h>
22 #ifdef AFS_NT40_ENV
23 #include <fcntl.h>
24 #include <io.h>
25 #else
26 #include <sys/param.h>
27 #include <sys/file.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30 #endif
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <afs/afs_assert.h>
34 #include <afs/afsutil.h>
35
36 #include <rx/xdr.h>
37 #include "afs/afsint.h"
38 #include "nfs.h"
39 #include "lwp.h"
40 #include "lock.h"
41 #include <afs/afssyscalls.h>
42 #include "ihandle.h"
43 #ifdef AFS_NT40_ENV
44 #include "ntops.h"
45 #endif
46 #include "vnode.h"
47 #include "volume.h"
48 #include "viceinode.h"
49 #include "partition.h"
50 #include "daemon_com.h"
51 #include "fssync.h"
52 #include "common.h"
53
54 /* forward declarations */
55 static int ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
56                             afs_foff_t * aoffset);
57 #if 0
58 static void PurgeIndex(Volume * vp, VnodeClass class);
59 static void PurgeHeader(Volume * vp);
60 #endif
61
62 static void PurgeIndex_r(Volume * vp, VnodeClass class);
63 static void PurgeHeader_r(Volume * vp);
64
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
67  */
68 void
69 VPurgeVolume(Error * ec, Volume * vp)
70 {
71     struct DiskPartition64 *tpartp = vp->partition;
72     VolumeId volid, parent;
73     afs_int32 code;
74
75     volid = V_id(vp);
76     parent = V_parentId(vp);
77
78     /* so VCheckDetach doesn't try to update the volume header and
79      * dump spurious errors into the logs */
80     V_inUse(vp) = 0;
81
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.
85      */
86     PurgeIndex_r(vp, vLarge);
87     PurgeIndex_r(vp, vSmall);
88     PurgeHeader_r(vp);
89
90     code = VDestroyVolumeDiskHeader(tpartp, volid, parent);
91     if (code) {
92         Log("VPurgeVolume: Error %ld when destroying volume %lu header\n",
93             afs_printable_int32_ld(code),
94             afs_printable_uint32_lu(volid));
95     }
96
97     /*
98      * Call the fileserver to break all call backs for that volume
99      */
100     FSYNC_VolOp(V_id(vp), tpartp->name, FSYNC_VOL_BREAKCBKS, 0, NULL);
101 }
102
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 */
108 static int
109 ObliterateRegion(Volume * avp, VnodeClass aclass, StreamHandle_t * afile,
110                  afs_foff_t * aoffset)
111 {
112     struct VnodeClassInfo *vcp;
113     Inode inodes[MAXOBLITATONCE];
114     afs_int32 iindex, nscanned;
115     afs_foff_t offset;
116     char buf[SIZEOF_LARGEDISKVNODE];
117     int hitEOF;
118     int i;
119     afs_int32 code;
120     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
121
122     hitEOF = 0;
123     vcp = &VnodeClassInfo[aclass];
124     offset = *aoffset;          /* original offset */
125     iindex = 0;
126     nscanned = 0;
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_ASEEK(afile, offset);
132     while (1) {
133         if (iindex >= MAXOBLITATONCE) {
134             break;
135         }
136         code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
137         nscanned++;
138         offset += vcp->diskSize;
139         if (code != 1) {
140             hitEOF = 1;
141             break;
142         }
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);
148         }
149     }
150
151     /* next, obliterate the index and fflush (and fsync) it */
152     STREAM_ASEEK(afile, *aoffset);      /* 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)
156             goto fail;
157     }
158     STREAM_FLUSH(afile);        /* ensure 0s are on the disk */
159     OS_SYNC(afile->str_fd);
160
161     /* finally, do the idec's */
162     for (i = 0; i < iindex; i++) {
163         IH_DEC(V_linkHandle(avp), inodes[i], V_parentId(avp));
164         DOPOLL;
165     }
166
167     /* return the new offset */
168     *aoffset = offset;
169     return hitEOF;              /* return 1 if hit EOF (don't call again), otherwise 0 */
170
171   fail:
172     return -1;
173 }
174
175 #if 0
176 static void
177 PurgeIndex(Volume * vp, VnodeClass class)
178 {
179     VOL_LOCK;
180     PurgeIndex_r(vp, class);
181     VOL_UNLOCK;
182 }
183 #endif
184
185 static void
186 PurgeIndex_r(Volume * vp, VnodeClass class)
187 {
188     StreamHandle_t *ifile;
189     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
190     afs_foff_t offset;
191     afs_int32 code;
192     FdHandle_t *fdP;
193
194
195     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
196     if (fdP == NULL)
197         return;
198
199     ifile = FDH_FDOPEN(fdP, "r+");
200     if (!ifile) {
201         FDH_REALLYCLOSE(fdP);
202         return;
203     }
204
205     offset = vcp->diskSize;
206     while (1) {
207         code = ObliterateRegion(vp, class, ifile, &offset);
208         if (code)
209             break;              /* if error or hit EOF */
210     }
211     STREAM_CLOSE(ifile);
212     FDH_CLOSE(fdP);
213 }
214
215 #if 0
216 static void
217 PurgeHeader(Volume * vp)
218 {
219     VOL_LOCK;
220     PurgeHeader_r(vp);
221     VOL_UNLOCK;
222 }
223 #endif
224
225 static void
226 PurgeHeader_r(Volume * vp)
227 {
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));
232 #ifdef AFS_NAMEI_ENV
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));
236 #endif
237 }