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