volser: allow clonevol purge id to be new id
[openafs.git] / src / vol / clone.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:         clone.c
13
14  */
15
16 /* Clone a volume.  Assumes the new volume is already created */
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21 #include <roken.h>
22
23 #ifdef AFS_NT40_ENV
24 #include <windows.h>
25 #include <winbase.h>
26 #endif
27
28 #include <rx/xdr.h>
29 #include <afs/afsint.h>
30 #include <afs/afssyscalls.h>
31
32 #include "nfs.h"
33 #include "lwp.h"
34 #include "lock.h"
35 #include "ihandle.h"
36 #include "vnode.h"
37 #include "volume.h"
38 #include "partition.h"
39 #include "viceinode.h"
40 #include "vol_prototypes.h"
41 #include "common.h"
42
43 int (*vol_PollProc) (void) = 0; /* someone must init this */
44
45 #define ERROR_EXIT(code) do { \
46     error = code; \
47     goto error_exit; \
48 } while (0)
49
50 /* parameters for idec call - this could just be an IHandle_t, but leaving
51  * open the possibility of decrementing the special files as well.
52  */
53 struct clone_rock {
54     IHandle_t *h;
55     VolId vol;
56 };
57
58 #define CLONE_MAXITEMS  100
59 struct clone_items {
60     struct clone_items *next;
61     afs_int32 nitems;
62     Inode data[CLONE_MAXITEMS];
63 };
64
65 struct clone_head {
66     struct clone_items *first;
67     struct clone_items *last;
68 };
69
70 void CloneVolume(Error *, Volume *, Volume *, Volume *);
71
72 static int
73 ci_AddItem(struct clone_head *ah, Inode aino)
74 {
75     struct clone_items *ti;
76
77     /* if no last elt (first call) or last item full, get a new one */
78     if ((!ah->last) || ah->last->nitems >= CLONE_MAXITEMS) {
79         ti = (struct clone_items *)malloc(sizeof(struct clone_items));
80         if (!ti) {
81             Log("ci_AddItem: malloc failed\n");
82             osi_Panic("ci_AddItem: malloc failed\n");
83         }
84         ti->nitems = 0;
85         ti->next = (struct clone_items *)0;
86         if (ah->last) {
87             ah->last->next = ti;
88             ah->last = ti;
89         } else {
90             /* first dude in the list */
91             ah->first = ah->last = ti;
92         }
93     } else
94         ti = ah->last;
95
96     /* now ti points to the end of the list, to a clone_item with room
97      * for at least one more element.  Add it.
98      */
99     ti->data[ti->nitems++] = aino;
100     return 0;
101 }
102
103 /* initialize a clone header */
104 int
105 ci_InitHead(struct clone_head *ah)
106 {
107     memset(ah, 0, sizeof(*ah));
108     return 0;
109 }
110
111 /* apply a function to all dudes in the set */
112 int
113 ci_Apply(struct clone_head *ah, int (*aproc) (Inode,  void *), void *arock)
114 {
115     struct clone_items *ti;
116     int i;
117
118     for (ti = ah->first; ti; ti = ti->next) {
119         for (i = 0; i < ti->nitems; i++) {
120             (*aproc) (ti->data[i], arock);
121         }
122     }
123     return 0;
124 }
125
126 /* free all dudes in the list */
127 int
128 ci_Destroy(struct clone_head *ah)
129 {
130     struct clone_items *ti, *ni;
131
132     for (ti = ah->first; ti; ti = ni) {
133         ni = ti->next;          /* guard against freeing */
134         free(ti);
135     }
136     return 0;
137 }
138
139 static int
140 IDecProc(Inode adata, void *arock)
141 {
142     struct clone_rock *aparm = (struct clone_rock *)arock;
143     IH_DEC(aparm->h, adata, aparm->vol);
144     DOPOLL;
145     return 0;
146 }
147
148 afs_int32
149 DoCloneIndex(Volume * rwvp, Volume * clvp, VnodeClass class, int reclone)
150 {
151     afs_int32 code, error = 0;
152     FdHandle_t *rwFd = 0, *clFdIn = 0, *clFdOut = 0;
153     StreamHandle_t *rwfile = 0, *clfilein = 0, *clfileout = 0;
154     IHandle_t *rwH = 0, *clHin = 0, *clHout = 0;
155     char buf[SIZEOF_LARGEDISKVNODE], dbuf[SIZEOF_LARGEDISKVNODE];
156     struct VnodeDiskObject *rwvnode = (struct VnodeDiskObject *)buf;
157     struct VnodeDiskObject *clvnode = (struct VnodeDiskObject *)dbuf;
158     Inode rwinode = 0;
159     Inode clinode;
160     struct clone_head decHead;
161     struct clone_rock decRock;
162     afs_foff_t offset = 0;
163     afs_int32 dircloned, inodeinced;
164     afs_int32 filecount = 0, diskused = 0;
165     afs_ino_str_t stmp;
166
167     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
168     int ReadWriteOriginal = VolumeWriteable(rwvp);
169
170     /* Correct number of files in volume: this assumes indexes are always
171        cloned starting with vLarge */
172     if (ReadWriteOriginal && class != vLarge) {
173         filecount = V_filecount(rwvp);
174         diskused = V_diskused(rwvp);
175     }
176
177     /* Initialize list of inodes to nuke - must do this before any calls
178      * to ERROR_EXIT, as the error handler requires an initialised list
179      */
180     ci_InitHead(&decHead);
181     decRock.h = V_linkHandle(rwvp);
182     decRock.vol = V_parentId(rwvp);
183
184     /* Open the RW volume's index file and seek to beginning */
185     IH_COPY(rwH, rwvp->vnodeIndex[class].handle);
186     rwFd = IH_OPEN(rwH);
187     if (!rwFd)
188         ERROR_EXIT(EIO);
189     rwfile = FDH_FDOPEN(rwFd, ReadWriteOriginal ? "r+" : "r");
190     if (!rwfile)
191         ERROR_EXIT(EIO);
192     STREAM_ASEEK(rwfile, vcp->diskSize);        /* Will fail if no vnodes */
193
194     /* Open the clone volume's index file and seek to beginning */
195     IH_COPY(clHout, clvp->vnodeIndex[class].handle);
196     clFdOut = IH_OPEN(clHout);
197     if (!clFdOut)
198         ERROR_EXIT(EIO);
199     clfileout = FDH_FDOPEN(clFdOut, "a");
200     if (!clfileout)
201         ERROR_EXIT(EIO);
202     code = STREAM_ASEEK(clfileout, vcp->diskSize);
203     if (code)
204         ERROR_EXIT(EIO);
205
206     /* If recloning, open the new volume's index; this time for
207      * reading. We never read anything that we're simultaneously
208      * writing, so this all works.
209      */
210     if (reclone) {
211         IH_COPY(clHin, clvp->vnodeIndex[class].handle);
212         clFdIn = IH_OPEN(clHin);
213         if (!clFdIn)
214             ERROR_EXIT(EIO);
215         clfilein = FDH_FDOPEN(clFdIn, "r");
216         if (!clfilein)
217             ERROR_EXIT(EIO);
218         STREAM_ASEEK(clfilein, vcp->diskSize);  /* Will fail if no vnodes */
219     }
220
221     /* Read each vnode in the old volume's index file */
222     for (offset = vcp->diskSize;
223          STREAM_READ(rwvnode, vcp->diskSize, 1, rwfile) == 1;
224          offset += vcp->diskSize) {
225         dircloned = inodeinced = 0;
226
227         /* If we are recloning the volume, read the corresponding vnode
228          * from the clone and determine its inode number.
229          */
230         if (reclone && !STREAM_EOF(clfilein)
231             && (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1)) {
232             clinode = VNDISK_GET_INO(clvnode);
233         } else {
234             clinode = 0;
235         }
236
237         if (rwvnode->type != vNull) {
238             afs_fsize_t ll;
239
240             if (rwvnode->vnodeMagic != vcp->magic)
241                 ERROR_EXIT(-1);
242             rwinode = VNDISK_GET_INO(rwvnode);
243             filecount++;
244             VNDISK_GET_LEN(ll, rwvnode);
245             diskused += nBlocks(ll);
246
247             /* Increment the inode if not already */
248             if (clinode && (clinode == rwinode)) {
249                 clinode = 0;    /* already cloned - don't delete later */
250             } else if (rwinode) {
251                 if (IH_INC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
252                     -1) {
253                     Log("IH_INC failed: %"AFS_PTR_FMT", %s, %u errno %d\n",
254                         V_linkHandle(rwvp), PrintInode(stmp, rwinode),
255                         V_parentId(rwvp), errno);
256                     VForceOffline(rwvp);
257                     ERROR_EXIT(EIO);
258                 }
259                 inodeinced = 1;
260             }
261
262             /* If a directory, mark vnode in old volume as cloned */
263             if ((rwvnode->type == vDirectory) && ReadWriteOriginal) {
264 #ifdef DVINC
265                 /*
266                  * It is my firmly held belief that immediately after
267                  * copy-on-write, the two directories can be identical.
268                  * If the new copy is changed (presumably, that is the very
269                  * next thing that will happen) then the dataVersion will
270                  * get bumped.
271                  */
272                 /* NOTE:  the dataVersion++ is incredibly important!!!.
273                  * This will cause the inode created by the file server
274                  * on copy-on-write to be stamped with a dataVersion bigger
275                  * than the current one.  The salvager will then do the
276                  * right thing */
277                 rwvnode->dataVersion++;
278 #endif /* DVINC */
279                 rwvnode->cloned = 1;
280                 code = STREAM_ASEEK(rwfile, offset);
281                 if (code == -1)
282                     goto clonefailed;
283                 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
284                 if (code != 1)
285                     goto clonefailed;
286                 dircloned = 1;
287                 code = STREAM_ASEEK(rwfile, offset + vcp->diskSize);
288                 if (code == -1)
289                     goto clonefailed;
290 #ifdef DVINC
291                 rwvnode->dataVersion--; /* Really needs to be set to the value in the inode,
292                                          * for the read-only volume */
293 #endif /* DVINC */
294             }
295         }
296
297         /* Overwrite the vnode entry in the clone volume */
298         rwvnode->cloned = 0;
299         code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, clfileout);
300         if (code != 1) {
301           clonefailed:
302             /* Couldn't clone, go back and decrement the inode's link count */
303             if (inodeinced) {
304                 if (IH_DEC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
305                     -1) {
306                     Log("IH_DEC failed: %"AFS_PTR_FMT", %s, %u errno %d\n",
307                         V_linkHandle(rwvp), PrintInode(stmp, rwinode),
308                         V_parentId(rwvp), errno);
309                     VForceOffline(rwvp);
310                     ERROR_EXIT(EIO);
311                 }
312             }
313             /* And if the directory was marked clone, unmark it */
314             if (dircloned) {
315                 rwvnode->cloned = 0;
316                 if (STREAM_ASEEK(rwfile, offset) != -1)
317                     (void)STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
318             }
319             ERROR_EXIT(EIO);
320         }
321
322         /* Removal of the old cloned inode */
323         if (clinode) {
324             ci_AddItem(&decHead, clinode);      /* just queue it */
325         }
326
327         DOPOLL;
328     }
329     if (STREAM_ERROR(clfileout))
330         ERROR_EXIT(EIO);
331
332     /* Clean out any junk at end of clone file */
333     if (reclone) {
334         STREAM_ASEEK(clfilein, offset);
335         while (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1) {
336             if (clvnode->type != vNull && VNDISK_GET_INO(clvnode) != 0) {
337                 ci_AddItem(&decHead, VNDISK_GET_INO(clvnode));
338             }
339             DOPOLL;
340         }
341     }
342
343     /* come here to finish up.  If code is non-zero, we've already run into problems,
344      * and shouldn't do the idecs.
345      */
346   error_exit:
347     if (rwfile)
348         STREAM_CLOSE(rwfile);
349     if (clfilein)
350         STREAM_CLOSE(clfilein);
351     if (clfileout)
352         STREAM_CLOSE(clfileout);
353
354     if (rwFd)
355         FDH_CLOSE(rwFd);
356     if (clFdIn)
357         FDH_CLOSE(clFdIn);
358     if (clFdOut)
359         FDH_CLOSE(clFdOut);
360
361     if (rwH)
362         IH_RELEASE(rwH);
363     if (clHout)
364         IH_RELEASE(clHout);
365     if (clHin)
366         IH_RELEASE(clHin);
367
368     /* Next, we sync the disk. We have to reopen in case we're truncating,
369      * since we were using stdio above, and don't know when the buffers
370      * would otherwise be flushed.  There's no stdio fftruncate call.
371      */
372     rwFd = IH_OPEN(clvp->vnodeIndex[class].handle);
373     if (rwFd == NULL) {
374         if (!error)
375             error = EIO;
376     } else {
377         if (reclone) {
378             /* If doing a reclone, we're keeping the clone. We need to
379              * truncate the file to offset bytes.
380              */
381             if (reclone && !error) {
382                 error = FDH_TRUNC(rwFd, offset);
383             }
384         }
385         FDH_SYNC(rwFd);
386         FDH_CLOSE(rwFd);
387     }
388
389     /* Now finally do the idec's.  At this point, all potential
390      * references have been cleaned up and sent to the disk
391      * (see above fclose and fsync). No matter what happens, we
392      * no longer need to keep these references around.
393      */
394     code = ci_Apply(&decHead, IDecProc, (char *)&decRock);
395     if (!error)
396         error = code;
397     ci_Destroy(&decHead);
398
399     if (ReadWriteOriginal && filecount > 0)
400         V_filecount(rwvp) = filecount;
401     if (ReadWriteOriginal && diskused > 0)
402         V_diskused(rwvp) = diskused;
403     return error;
404 }
405
406 void
407 CloneVolume(Error * rerror, Volume * original, Volume * new, Volume * old)
408 {
409     afs_int32 code, error = 0;
410     afs_int32 reclone;
411     afs_int32 filecount = V_filecount(original), diskused = V_diskused(original);
412
413     *rerror = 0;
414     reclone = ((new == old) ? 1 : 0);
415
416     code = DoCloneIndex(original, new, vLarge, reclone);
417     if (code)
418         ERROR_EXIT(code);
419     code = DoCloneIndex(original, new, vSmall, reclone);
420     if (code)
421         ERROR_EXIT(code);
422     if (filecount != V_filecount(original) || diskused != V_diskused(original))
423        Log("Clone %u: filecount %d -> %d diskused %d -> %d\n",
424             V_id(original), filecount, V_filecount(original), diskused, V_diskused(original));
425
426     code = CopyVolumeHeader(&V_disk(original), &V_disk(new));
427     if (code)
428         ERROR_EXIT(code);
429
430   error_exit:
431     *rerror = error;
432 }