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