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