2 * Copyright 2000, International Business Machines Corporation and others.
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
16 /* Clone a volume. Assumes the new volume is already created */
18 #include <afsconfig.h>
19 #include <afs/param.h>
29 #include <afs/afsint.h>
30 #include <afs/afssyscalls.h>
38 #include "partition.h"
39 #include "viceinode.h"
40 #include "vol_prototypes.h"
43 int (*vol_PollProc) (void) = 0; /* someone must init this */
45 #define ERROR_EXIT(code) do { \
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.
58 #define CLONE_MAXITEMS 100
60 struct clone_items *next;
62 Inode data[CLONE_MAXITEMS];
66 struct clone_items *first;
67 struct clone_items *last;
70 void CloneVolume(Error *, Volume *, Volume *, Volume *);
73 ci_AddItem(struct clone_head *ah, Inode aino)
75 struct clone_items *ti;
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));
81 Log("ci_AddItem: malloc failed\n");
82 osi_Panic("ci_AddItem: malloc failed\n");
85 ti->next = (struct clone_items *)0;
90 /* first dude in the list */
91 ah->first = ah->last = ti;
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.
99 ti->data[ti->nitems++] = aino;
103 /* initialize a clone header */
105 ci_InitHead(struct clone_head *ah)
107 memset(ah, 0, sizeof(*ah));
111 /* apply a function to all dudes in the set */
113 ci_Apply(struct clone_head *ah, int (*aproc) (Inode, void *), void *arock)
115 struct clone_items *ti;
118 for (ti = ah->first; ti; ti = ti->next) {
119 for (i = 0; i < ti->nitems; i++) {
120 (*aproc) (ti->data[i], arock);
126 /* free all dudes in the list */
128 ci_Destroy(struct clone_head *ah)
130 struct clone_items *ti, *ni;
132 for (ti = ah->first; ti; ti = ni) {
133 ni = ti->next; /* guard against freeing */
140 IDecProc(Inode adata, void *arock)
142 struct clone_rock *aparm = (struct clone_rock *)arock;
143 IH_DEC(aparm->h, adata, aparm->vol);
149 DoCloneIndex(Volume * rwvp, Volume * clvp, VnodeClass class, int reclone)
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;
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;
167 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
168 int ReadWriteOriginal = VolumeWriteable(rwvp);
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);
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
180 ci_InitHead(&decHead);
181 decRock.h = V_linkHandle(rwvp);
182 decRock.vol = V_parentId(rwvp);
184 /* Open the RW volume's index file and seek to beginning */
185 IH_COPY(rwH, rwvp->vnodeIndex[class].handle);
189 rwfile = FDH_FDOPEN(rwFd, ReadWriteOriginal ? "r+" : "r");
192 STREAM_ASEEK(rwfile, vcp->diskSize); /* Will fail if no vnodes */
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);
199 clfileout = FDH_FDOPEN(clFdOut, "a");
202 code = STREAM_ASEEK(clfileout, vcp->diskSize);
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.
211 IH_COPY(clHin, clvp->vnodeIndex[class].handle);
212 clFdIn = IH_OPEN(clHin);
215 clfilein = FDH_FDOPEN(clFdIn, "r");
218 STREAM_ASEEK(clfilein, vcp->diskSize); /* Will fail if no vnodes */
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;
227 /* If we are recloning the volume, read the corresponding vnode
228 * from the clone and determine its inode number.
230 if (reclone && !STREAM_EOF(clfilein)
231 && (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1)) {
232 clinode = VNDISK_GET_INO(clvnode);
237 if (rwvnode->type != vNull) {
240 if (rwvnode->vnodeMagic != vcp->magic)
242 rwinode = VNDISK_GET_INO(rwvnode);
244 VNDISK_GET_LEN(ll, rwvnode);
245 diskused += nBlocks(ll);
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)) ==
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);
262 /* If a directory, mark vnode in old volume as cloned */
263 if ((rwvnode->type == vDirectory) && ReadWriteOriginal) {
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
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
277 rwvnode->dataVersion++;
280 code = STREAM_ASEEK(rwfile, offset);
283 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
287 code = STREAM_ASEEK(rwfile, offset + vcp->diskSize);
291 rwvnode->dataVersion--; /* Really needs to be set to the value in the inode,
292 * for the read-only volume */
297 /* Overwrite the vnode entry in the clone volume */
299 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, clfileout);
302 /* Couldn't clone, go back and decrement the inode's link count */
304 if (IH_DEC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
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);
313 /* And if the directory was marked clone, unmark it */
316 if (STREAM_ASEEK(rwfile, offset) != -1)
317 (void)STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
322 /* Removal of the old cloned inode */
324 ci_AddItem(&decHead, clinode); /* just queue it */
329 if (STREAM_ERROR(clfileout))
332 /* Clean out any junk at end of clone file */
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));
343 /* come here to finish up. If code is non-zero, we've already run into problems,
344 * and shouldn't do the idecs.
348 STREAM_CLOSE(rwfile);
350 STREAM_CLOSE(clfilein);
352 STREAM_CLOSE(clfileout);
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.
372 rwFd = IH_OPEN(clvp->vnodeIndex[class].handle);
378 /* If doing a reclone, we're keeping the clone. We need to
379 * truncate the file to offset bytes.
381 if (reclone && !error) {
382 error = FDH_TRUNC(rwFd, offset);
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.
394 code = ci_Apply(&decHead, IDecProc, (char *)&decRock);
397 ci_Destroy(&decHead);
399 if (ReadWriteOriginal && filecount > 0)
400 V_filecount(rwvp) = filecount;
401 if (ReadWriteOriginal && diskused > 0)
402 V_diskused(rwvp) = diskused;
407 CloneVolume(Error * rerror, Volume * original, Volume * new, Volume * old)
409 afs_int32 code, error = 0;
411 afs_int32 filecount = V_filecount(original), diskused = V_diskused(original);
414 reclone = ((new == old) ? 1 : 0);
416 code = DoCloneIndex(original, new, vLarge, reclone);
419 code = DoCloneIndex(original, new, vSmall, reclone);
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));
426 code = CopyVolumeHeader(&V_disk(original), &V_disk(new));