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>
23 #include <sys/types.h>
25 #include <afs/afs_assert.h>
42 #include <afs/afsint.h>
46 #include <afs/afssyscalls.h>
50 #include "partition.h"
51 #include "viceinode.h"
52 #include "vol_prototypes.h"
55 int (*vol_PollProc) (void) = 0; /* someone must init this */
57 #define ERROR_EXIT(code) {error = code; goto error_exit;}
59 /* parameters for idec call - this could just be an IHandle_t, but leaving
60 * open the possibility of decrementing the special files as well.
67 #define CLONE_MAXITEMS 100
69 struct clone_items *next;
71 Inode data[CLONE_MAXITEMS];
75 struct clone_items *first;
76 struct clone_items *last;
79 void CloneVolume(Error *, Volume *, Volume *, Volume *);
82 ci_AddItem(struct clone_head *ah, Inode aino)
84 struct clone_items *ti;
86 /* if no last elt (first call) or last item full, get a new one */
87 if ((!ah->last) || ah->last->nitems >= CLONE_MAXITEMS) {
88 ti = (struct clone_items *)malloc(sizeof(struct clone_items));
90 Log("ci_AddItem: malloc failed\n");
91 osi_Panic("ci_AddItem: malloc failed\n");
94 ti->next = (struct clone_items *)0;
99 /* first dude in the list */
100 ah->first = ah->last = ti;
105 /* now ti points to the end of the list, to a clone_item with room
106 * for at least one more element. Add it.
108 ti->data[ti->nitems++] = aino;
112 /* initialize a clone header */
114 ci_InitHead(struct clone_head *ah)
116 memset(ah, 0, sizeof(*ah));
120 /* apply a function to all dudes in the set */
122 ci_Apply(struct clone_head *ah, int (*aproc) (Inode, void *), void *arock)
124 struct clone_items *ti;
127 for (ti = ah->first; ti; ti = ti->next) {
128 for (i = 0; i < ti->nitems; i++) {
129 (*aproc) (ti->data[i], arock);
135 /* free all dudes in the list */
137 ci_Destroy(struct clone_head *ah)
139 struct clone_items *ti, *ni;
141 for (ti = ah->first; ti; ti = ni) {
142 ni = ti->next; /* guard against freeing */
149 IDecProc(Inode adata, void *arock)
151 struct clone_rock *aparm = (struct clone_rock *)arock;
152 IH_DEC(aparm->h, adata, aparm->vol);
158 DoCloneIndex(Volume * rwvp, Volume * clvp, VnodeClass class, int reclone)
160 afs_int32 code, error = 0;
161 FdHandle_t *rwFd = 0, *clFdIn = 0, *clFdOut = 0;
162 StreamHandle_t *rwfile = 0, *clfilein = 0, *clfileout = 0;
163 IHandle_t *rwH = 0, *clHin = 0, *clHout = 0;
164 char buf[SIZEOF_LARGEDISKVNODE], dbuf[SIZEOF_LARGEDISKVNODE];
165 struct VnodeDiskObject *rwvnode = (struct VnodeDiskObject *)buf;
166 struct VnodeDiskObject *clvnode = (struct VnodeDiskObject *)dbuf;
169 struct clone_head decHead;
170 struct clone_rock decRock;
171 afs_foff_t offset = 0;
172 afs_int32 dircloned, inodeinced;
173 afs_int32 filecount = 0, diskused = 0;
176 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
177 int ReadWriteOriginal = VolumeWriteable(rwvp);
179 /* Correct number of files in volume: this assumes indexes are always
180 cloned starting with vLarge */
181 if (ReadWriteOriginal && class != vLarge) {
182 filecount = V_filecount(rwvp);
183 diskused = V_diskused(rwvp);
186 /* Open the RW volume's index file and seek to beginning */
187 IH_COPY(rwH, rwvp->vnodeIndex[class].handle);
191 rwfile = FDH_FDOPEN(rwFd, ReadWriteOriginal ? "r+" : "r");
194 STREAM_ASEEK(rwfile, vcp->diskSize); /* Will fail if no vnodes */
196 /* Open the clone volume's index file and seek to beginning */
197 IH_COPY(clHout, clvp->vnodeIndex[class].handle);
198 clFdOut = IH_OPEN(clHout);
201 clfileout = FDH_FDOPEN(clFdOut, "a");
204 code = STREAM_ASEEK(clfileout, vcp->diskSize);
208 /* If recloning, open the new volume's index; this time for
209 * reading. We never read anything that we're simultaneously
210 * writing, so this all works.
213 IH_COPY(clHin, clvp->vnodeIndex[class].handle);
214 clFdIn = IH_OPEN(clHin);
217 clfilein = FDH_FDOPEN(clFdIn, "r");
220 STREAM_ASEEK(clfilein, vcp->diskSize); /* Will fail if no vnodes */
223 /* Initialize list of inodes to nuke */
224 ci_InitHead(&decHead);
225 decRock.h = V_linkHandle(rwvp);
226 decRock.vol = V_parentId(rwvp);
228 /* Read each vnode in the old volume's index file */
229 for (offset = vcp->diskSize;
230 STREAM_READ(rwvnode, vcp->diskSize, 1, rwfile) == 1;
231 offset += vcp->diskSize) {
232 dircloned = inodeinced = 0;
234 /* If we are recloning the volume, read the corresponding vnode
235 * from the clone and determine its inode number.
237 if (reclone && !STREAM_EOF(clfilein)
238 && (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1)) {
239 clinode = VNDISK_GET_INO(clvnode);
244 if (rwvnode->type != vNull) {
247 if (rwvnode->vnodeMagic != vcp->magic)
249 rwinode = VNDISK_GET_INO(rwvnode);
251 VNDISK_GET_LEN(ll, rwvnode);
252 diskused += nBlocks(ll);
254 /* Increment the inode if not already */
255 if (clinode && (clinode == rwinode)) {
256 clinode = 0; /* already cloned - don't delete later */
257 } else if (rwinode) {
258 if (IH_INC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
260 Log("IH_INC failed: %"AFS_PTR_FMT", %s, %u errno %d\n",
261 V_linkHandle(rwvp), PrintInode(stmp, rwinode),
262 V_parentId(rwvp), errno);
269 /* If a directory, mark vnode in old volume as cloned */
270 if ((rwvnode->type == vDirectory) && ReadWriteOriginal) {
273 * It is my firmly held belief that immediately after
274 * copy-on-write, the two directories can be identical.
275 * If the new copy is changed (presumably, that is the very
276 * next thing that will happen) then the dataVersion will
279 /* NOTE: the dataVersion++ is incredibly important!!!.
280 * This will cause the inode created by the file server
281 * on copy-on-write to be stamped with a dataVersion bigger
282 * than the current one. The salvager will then do the
284 rwvnode->dataVersion++;
287 code = STREAM_ASEEK(rwfile, offset);
290 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
294 code = STREAM_ASEEK(rwfile, offset + vcp->diskSize);
298 rwvnode->dataVersion--; /* Really needs to be set to the value in the inode,
299 * for the read-only volume */
304 /* Overwrite the vnode entry in the clone volume */
306 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, clfileout);
309 /* Couldn't clone, go back and decrement the inode's link count */
311 if (IH_DEC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
313 Log("IH_DEC failed: %"AFS_PTR_FMT", %s, %u errno %d\n",
314 V_linkHandle(rwvp), PrintInode(stmp, rwinode),
315 V_parentId(rwvp), errno);
320 /* And if the directory was marked clone, unmark it */
323 if (STREAM_ASEEK(rwfile, offset) != -1)
324 (void)STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
329 /* Removal of the old cloned inode */
331 ci_AddItem(&decHead, clinode); /* just queue it */
336 if (STREAM_ERROR(clfileout))
339 /* Clean out any junk at end of clone file */
341 STREAM_ASEEK(clfilein, offset);
342 while (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1) {
343 if (clvnode->type != vNull && VNDISK_GET_INO(clvnode) != 0) {
344 ci_AddItem(&decHead, VNDISK_GET_INO(clvnode));
350 /* come here to finish up. If code is non-zero, we've already run into problems,
351 * and shouldn't do the idecs.
355 STREAM_CLOSE(rwfile);
357 STREAM_CLOSE(clfilein);
359 STREAM_CLOSE(clfileout);
375 /* Next, we sync the disk. We have to reopen in case we're truncating,
376 * since we were using stdio above, and don't know when the buffers
377 * would otherwise be flushed. There's no stdio fftruncate call.
379 rwFd = IH_OPEN(clvp->vnodeIndex[class].handle);
385 /* If doing a reclone, we're keeping the clone. We need to
386 * truncate the file to offset bytes.
388 if (reclone && !error) {
389 error = FDH_TRUNC(rwFd, offset);
396 /* Now finally do the idec's. At this point, all potential
397 * references have been cleaned up and sent to the disk
398 * (see above fclose and fsync). No matter what happens, we
399 * no longer need to keep these references around.
401 code = ci_Apply(&decHead, IDecProc, (char *)&decRock);
404 ci_Destroy(&decHead);
406 if (ReadWriteOriginal && filecount > 0)
407 V_filecount(rwvp) = filecount;
408 if (ReadWriteOriginal && diskused > 0)
409 V_diskused(rwvp) = diskused;
414 CloneVolume(Error * rerror, Volume * original, Volume * new, Volume * old)
416 afs_int32 code, error = 0;
418 afs_int32 filecount = V_filecount(original), diskused = V_diskused(original);
421 reclone = ((new == old) ? 1 : 0);
423 code = DoCloneIndex(original, new, vLarge, reclone);
426 code = DoCloneIndex(original, new, vSmall, reclone);
429 if (filecount != V_filecount(original) || diskused != V_diskused(original))
430 Log("Clone %u: filecount %d -> %d diskused %d -> %d\n",
431 V_id(original), filecount, V_filecount(original), diskused, V_diskused(original));
433 code = CopyVolumeHeader(&V_disk(original), &V_disk(new));