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