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