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>
22 #include <sys/types.h>
24 #ifdef AFS_PTHREAD_ENV
26 #else /* AFS_PTHREAD_ENV */
27 #include <afs/assert.h>
28 #endif /* AFS_PTHREAD_ENV */
45 #include <afs/afsint.h>
49 #include <afs/afssyscalls.h>
53 #include "partition.h"
54 #include "viceinode.h"
55 #include "vol_prototypes.h"
58 int (*vol_PollProc) (void) = 0; /* someone must init this */
60 #define ERROR_EXIT(code) {error = code; goto error_exit;}
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.
70 #define CLONE_MAXITEMS 100
72 struct clone_items *next;
74 Inode data[CLONE_MAXITEMS];
78 struct clone_items *first;
79 struct clone_items *last;
82 void CloneVolume(Error *, Volume *, Volume *, Volume *);
85 ci_AddItem(struct clone_head *ah, Inode aino)
87 register struct clone_items *ti;
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));
93 Log("ci_AddItem: malloc failed\n");
97 ti->next = (struct clone_items *)0;
102 /* first dude in the list */
103 ah->first = ah->last = ti;
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.
111 ti->data[ti->nitems++] = aino;
115 /* initialize a clone header */
117 ci_InitHead(struct clone_head *ah)
119 memset(ah, 0, sizeof(*ah));
123 /* apply a function to all dudes in the set */
125 ci_Apply(struct clone_head *ah, int (*aproc) (Inode, void *), void *arock)
127 register struct clone_items *ti;
130 for (ti = ah->first; ti; ti = ti->next) {
131 for (i = 0; i < ti->nitems; i++) {
132 (*aproc) (ti->data[i], arock);
138 /* free all dudes in the list */
140 ci_Destroy(struct clone_head *ah)
142 register struct clone_items *ti, *ni;
144 for (ti = ah->first; ti; ti = ni) {
145 ni = ti->next; /* guard against freeing */
152 IDecProc(Inode adata, void *arock)
154 struct clone_rock *aparm = (struct clone_rock *)arock;
155 IH_DEC(aparm->h, adata, aparm->vol);
161 DoCloneIndex(Volume * rwvp, Volume * clvp, VnodeClass class, int reclone)
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;
172 struct clone_head decHead;
173 struct clone_rock decRock;
174 afs_int32 offset = 0;
175 afs_int32 dircloned, inodeinced;
176 afs_int32 filecount = 0, diskused = 0;
178 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
179 int ReadWriteOriginal = VolumeWriteable(rwvp);
181 /* Correct number of files in volume: this assumes indexes are always
182 cloned starting with vLarge */
183 if (ReadWriteOriginal && class != vLarge) {
184 filecount = V_filecount(rwvp);
185 diskused = V_diskused(rwvp);
188 /* Open the RW volume's index file and seek to beginning */
189 IH_COPY(rwH, rwvp->vnodeIndex[class].handle);
193 rwfile = FDH_FDOPEN(rwFd, ReadWriteOriginal ? "r+" : "r");
196 STREAM_SEEK(rwfile, vcp->diskSize, 0); /* Will fail if no vnodes */
198 /* Open the clone volume's index file and seek to beginning */
199 IH_COPY(clHout, clvp->vnodeIndex[class].handle);
200 clFdOut = IH_OPEN(clHout);
203 clfileout = FDH_FDOPEN(clFdOut, "a");
206 code = STREAM_SEEK(clfileout, vcp->diskSize, 0);
210 /* If recloning, open the new volume's index; this time for
211 * reading. We never read anything that we're simultaneously
212 * writing, so this all works.
215 IH_COPY(clHin, clvp->vnodeIndex[class].handle);
216 clFdIn = IH_OPEN(clHin);
219 clfilein = FDH_FDOPEN(clFdIn, "r");
222 STREAM_SEEK(clfilein, vcp->diskSize, 0); /* Will fail if no vnodes */
225 /* Initialize list of inodes to nuke */
226 ci_InitHead(&decHead);
227 decRock.h = V_linkHandle(rwvp);
228 decRock.vol = V_parentId(rwvp);
230 /* Read each vnode in the old volume's index file */
231 for (offset = vcp->diskSize;
232 STREAM_READ(rwvnode, vcp->diskSize, 1, rwfile) == 1;
233 offset += vcp->diskSize) {
234 dircloned = inodeinced = 0;
236 /* If we are recloning the volume, read the corresponding vnode
237 * from the clone and determine its inode number.
239 if (reclone && !STREAM_EOF(clfilein)
240 && (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1)) {
241 clinode = VNDISK_GET_INO(clvnode);
246 if (rwvnode->type != vNull) {
249 if (rwvnode->vnodeMagic != vcp->magic)
251 rwinode = VNDISK_GET_INO(rwvnode);
253 VNDISK_GET_LEN(ll, rwvnode);
254 diskused += nBlocks(ll);
256 /* Increment the inode if not already */
257 if (clinode && (clinode == rwinode)) {
258 clinode = 0; /* already cloned - don't delete later */
259 } else if (rwinode) {
260 if (IH_INC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
262 Log("IH_INC failed: %"AFS_PTR_FMT", %s, %u errno %d\n",
263 V_linkHandle(rwvp), PrintInode(NULL, rwinode),
264 V_parentId(rwvp), errno);
271 /* If a directory, mark vnode in old volume as cloned */
272 if ((rwvnode->type == vDirectory) && ReadWriteOriginal) {
275 * It is my firmly held belief that immediately after
276 * copy-on-write, the two directories can be identical.
277 * If the new copy is changed (presumably, that is the very
278 * next thing that will happen) then the dataVersion will
281 /* NOTE: the dataVersion++ is incredibly important!!!.
282 * This will cause the inode created by the file server
283 * on copy-on-write to be stamped with a dataVersion bigger
284 * than the current one. The salvager will then do the
286 rwvnode->dataVersion++;
289 code = STREAM_SEEK(rwfile, offset, 0);
292 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
296 code = STREAM_SEEK(rwfile, offset + vcp->diskSize, 0);
300 rwvnode->dataVersion--; /* Really needs to be set to the value in the inode,
301 * for the read-only volume */
306 /* Overwrite the vnode entry in the clone volume */
308 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, clfileout);
311 /* Couldn't clone, go back and decrement the inode's link count */
313 if (IH_DEC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
315 Log("IH_DEC failed: %"AFS_PTR_FMT", %s, %u errno %d\n",
316 V_linkHandle(rwvp), PrintInode(NULL, rwinode),
317 V_parentId(rwvp), errno);
322 /* And if the directory was marked clone, unmark it */
325 if (STREAM_SEEK(rwfile, offset, 0) != -1)
326 (void)STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
331 /* Removal of the old cloned inode */
333 ci_AddItem(&decHead, clinode); /* just queue it */
338 if (STREAM_ERROR(clfileout))
341 /* Clean out any junk at end of clone file */
343 STREAM_SEEK(clfilein, offset, 0);
344 while (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1) {
345 if (clvnode->type != vNull && VNDISK_GET_INO(clvnode) != 0) {
346 ci_AddItem(&decHead, VNDISK_GET_INO(clvnode));
352 /* come here to finish up. If code is non-zero, we've already run into problems,
353 * and shouldn't do the idecs.
357 STREAM_CLOSE(rwfile);
359 STREAM_CLOSE(clfilein);
361 STREAM_CLOSE(clfileout);
377 /* Next, we sync the disk. We have to reopen in case we're truncating,
378 * since we were using stdio above, and don't know when the buffers
379 * would otherwise be flushed. There's no stdio fftruncate call.
381 rwFd = IH_OPEN(clvp->vnodeIndex[class].handle);
387 /* If doing a reclone, we're keeping the clone. We need to
388 * truncate the file to offset bytes.
390 if (reclone && !error) {
391 error = FDH_TRUNC(rwFd, offset);
398 /* Now finally do the idec's. At this point, all potential
399 * references have been cleaned up and sent to the disk
400 * (see above fclose and fsync). No matter what happens, we
401 * no longer need to keep these references around.
403 code = ci_Apply(&decHead, IDecProc, (char *)&decRock);
406 ci_Destroy(&decHead);
408 if (ReadWriteOriginal && filecount > 0)
409 V_filecount(rwvp) = filecount;
410 if (ReadWriteOriginal && diskused > 0)
411 V_diskused(rwvp) = diskused;
416 CloneVolume(Error * rerror, Volume * original, Volume * new, Volume * old)
418 afs_int32 code, error = 0;
420 afs_int32 filecount = V_filecount(original), diskused = V_diskused(original);
423 reclone = ((new == old) ? 1 : 0);
425 code = DoCloneIndex(original, new, vLarge, reclone);
428 code = DoCloneIndex(original, new, vSmall, reclone);
431 if (filecount != V_filecount(original) || diskused != V_diskused(original))
432 Log("Clone %u: filecount %d -> %d diskused %d -> %d\n",
433 V_id(original), filecount, V_filecount(original), diskused, V_diskused(original));
435 code = CopyVolumeHeader(&V_disk(original), &V_disk(new));