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