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