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