dc1e219562a8c0588d7671a61d0470a439eb5e32
[openafs.git] / src / vol / nuke.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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <rx/xdr.h>
17 #include <afs/afsint.h>
18 #include <stdio.h>
19 #ifdef AFS_PTHREAD_ENV
20 #include <assert.h>
21 #else /* AFS_PTHREAD_ENV */
22 #include <afs/assert.h>
23 #endif /* AFS_PTHREAD_ENV */
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
28 #include <string.h>
29 #else
30 #include <strings.h>
31 #endif
32
33 #include <afs/assert.h>
34 #include "nfs.h"
35 #include "lwp.h"
36 #include "lock.h"
37 #include <afs/afssyscalls.h>
38 #include "ihandle.h"
39 #include "vnode.h"
40 #include "volume.h"
41 #include "partition.h"
42 #include "viceinode.h"
43 #include "salvage.h"
44 #include "fssync.h"
45
46 #ifdef O_LARGEFILE
47 #define afs_stat        stat64
48 #else /* !O_LARGEFILE */
49 #define afs_stat        stat
50 #endif /* !O_LARGEFILE */
51
52 /*@printflike@*/ extern void Log(const char *format, ...);
53
54
55 struct Lock localLock;
56 char *vol_DevName();
57
58 #define MAXATONCE       100
59 /* structure containing neatly packed set of inodes and the # of times we'll have
60  * to idec them in order to reclaim their storage.  NukeProc, called by ListViceInodes,
61  * builds this list for us.
62  */
63 struct ilist {
64     struct ilist *next;
65     afs_int32 freePtr;          /* first free index in this table */
66     Inode inode[MAXATONCE];     /* inode # */
67     afs_int32 count[MAXATONCE]; /* link count */
68 } *allInodes = 0;
69
70 /* called with a structure specifying info about the inode, and our rock (which
71  * is the volume ID.  Returns true if we should keep this inode, otherwise false.
72  * Note that ainfo->u.param[0] is always the volume ID, for any vice inode.
73  */
74 static int
75 NukeProc(struct ViceInodeInfo *ainfo, afs_int32 avolid)
76 {
77     struct ilist *ti;
78     register afs_int32 i;
79
80 #ifndef AFS_PTHREAD_ENV
81     IOMGR_Poll();               /* poll so we don't kill the RPC connection */
82 #endif /* !AFS_PTHREAD_ENV */
83
84     /* check if this is the volume we're looking for */
85     if (ainfo->u.param[0] != avolid)
86         return 0;               /* don't want this one */
87     /* record the info */
88     if (!allInodes || allInodes->freePtr >= MAXATONCE) {
89         ti = (struct ilist *)malloc(sizeof(struct ilist));
90         memset(ti, 0, sizeof(*ti));
91         ti->next = allInodes;
92         allInodes = ti;
93     } else
94         ti = allInodes;         /* use the one with space */
95     i = ti->freePtr++;          /* find our slot in this mess */
96     ti->inode[i] = ainfo->inodeNumber;
97     ti->count[i] = ainfo->linkCount;
98     return 0;                   /* don't care if anything's written out, actually */
99 }
100
101 /* function called with partition name and volid ID, and which removes all
102  * inodes marked with the specified volume ID.  If the volume is a read-only
103  * clone, we'll only remove the header inodes, since they're the only inodes
104  * marked with that volume ID.  If you want to reclaim all the data, you should
105  * nuke the read-write volume ID.
106  *
107  * Note also that nuking a read-write volume effectively nukes all RO volumes
108  * cloned from that RW volume ID, too, since everything except for their
109  * indices will be gone.
110  */
111 int
112 nuke(char *aname, afs_int32 avolid)
113 {
114     /* first process the partition containing this junk */
115     struct afs_stat tstat;
116     struct ilist *ti, *ni;
117     register afs_int32 code;
118     char *tfile;
119     int i, j, forceSal;
120     char devName[64], wpath[100];
121     char *lastDevComp;
122 #ifdef AFS_NAMEI_ENV
123 #ifdef AFS_NT40_ENV
124     char path[MAX_PATH];
125 #else
126     char *path;
127     namei_t ufs_name;
128 #endif
129 #endif /* AFS_NAMEI_ENV */
130     IHandle_t *fileH;
131
132     if (avolid == 0)
133         return EINVAL;
134     code = afs_stat(aname, &tstat);
135     if (code) {
136         printf("volnuke: partition %s does not exist.\n", aname);
137         return code;
138     }
139     /* get the device name for the partition */
140 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
141     lastDevComp = aname;
142 #else
143 #ifdef AFS_NT40_ENV
144     lastDevComp = &aname[strlen(aname) - 1];
145     *lastDevComp = toupper(*lastDevComp);
146 #else
147     tfile = vol_DevName(tstat.st_dev, wpath);
148     if (!tfile) {
149         printf("volnuke: can't find %s's device.\n", aname);
150         return 1;
151     }
152     strcpy(devName, tfile);     /* save this from the static buffer */
153     /* aim lastDevComp at the 'foo' of '/dev/foo' */
154     lastDevComp = strrchr(devName, '/');
155     /* either points at slash, or there is no slash; adjust appropriately */
156     if (lastDevComp)
157         lastDevComp++;
158     else
159         lastDevComp = devName;
160 #endif /* AFS_NT40_ENV */
161 #endif /* AFS_NAMEI_ENV && !AFS_NT40_ENV */
162
163     ObtainWriteLock(&localLock);
164     /* OK, we have the mounted on place, aname, the device name (in devName).
165      * all we need to do to call ListViceInodes is find the inodes for the
166      * volume we're nuking.
167      */
168 #ifdef AFS_NAMEI_ENV
169     code =
170         ListViceInodes(lastDevComp, aname, NULL, NukeProc, avolid, &forceSal,
171                        0, wpath);
172 #else
173     code =
174         ListViceInodes(lastDevComp, aname, "/tmp/vNukeXX", NukeProc, avolid,
175                        &forceSal, 0, wpath);
176     unlink("/tmp/vNukeXX");     /* clean it up now */
177 #endif
178     if (code == 0) {
179         /* actually do the idecs now */
180         for (ti = allInodes; ti; ti = ti->next) {
181             for (i = 0; i < ti->freePtr; i++) {
182 #ifndef AFS_PTHREAD_ENV
183                 IOMGR_Poll();   /* keep RPC running */
184 #endif /* !AFS_PTHREAD_ENV */
185                 /* idec this inode into oblivion */
186 #ifdef AFS_NAMEI_ENV
187 #ifdef AFS_NT40_ENV
188                 IH_INIT(fileH, (int)(*lastDevComp - 'A'), avolid,
189                         ti->inode[i]);
190                 nt_HandleToName(path, fileH);
191 #else
192                 IH_INIT(fileH, (int)volutil_GetPartitionID(aname), avolid,
193                         ti->inode[i]);
194                 namei_HandleToName(&ufs_name, fileH);
195                 path = ufs_name.n_path;
196 #endif /* AFS_NT40_ENV */
197                 IH_RELEASE(fileH);
198                 if (unlink(path) < 0) {
199                     Log("Nuke: Failed to remove %s\n", path);
200                 }
201 #else /* AFS_NAMEI_ENV */
202                 IH_INIT(fileH, (int)tstat.st_dev, avolid, ti->inode[i]);
203                 for (j = 0; j < ti->count[i]; j++) {
204                     code = IH_DEC(fileH, ti->inode[i], avolid);
205                 }
206                 IH_RELEASE(fileH);
207 #endif /* AFS_NAMEI_ENV */
208             }
209             ni = ti->next;
210             free(ti);
211         }
212         code = 0;               /* we really don't care about it except for debugging */
213         allInodes = NULL;
214
215         /* at this point, we should try to remove the volume header file itself.
216          * the volume header file is the file named VNNNNN.vol in the UFS file
217          * system, and is a normal file.  As such, it is not stamped with the
218          * volume's ID in its inode, and has to be removed explicitly.
219          */
220         /* reuse devName buffer now */
221 #ifdef AFS_NT40_ENV
222         afs_snprintf(devName, sizeof devName, "%c:\\%s", *lastDevComp,
223                      VolumeExternalName(avolid));
224 #else
225         afs_snprintf(devName, sizeof devName, "%s/%s", aname,
226                      VolumeExternalName(avolid));
227 #endif /* AFS_NT40_ENV */
228         code = unlink(devName);
229         if (code)
230             code = errno;
231     } else {
232         /* just free things */
233         for (ti = allInodes; ti; ti = ni) {
234             ni = ti->next;
235             free(ti);
236         }
237         allInodes = NULL;
238     }
239     ReleaseWriteLock(&localLock);
240     return code;
241 }