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>
24 #include <sys/types.h>
26 #ifdef AFS_PTHREAD_ENV
28 #else /* AFS_PTHREAD_ENV */
29 #include <afs/assert.h>
30 #endif /* AFS_PTHREAD_ENV */
46 #include <afs/afsint.h>
50 #include <afs/afssyscalls.h>
54 #include "partition.h"
55 #include "viceinode.h"
57 /*@printflike@*/ extern void Log(const char *format, ...);
59 int (*vol_PollProc) (void) = 0; /* someone must init this */
61 #define ERROR_EXIT(code) {error = code; goto error_exit;}
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.
71 #define CLONE_MAXITEMS 100
73 struct clone_items *next;
75 Inode data[CLONE_MAXITEMS];
79 struct clone_items *first;
80 struct clone_items *last;
83 void CloneVolume(Error *, Volume *, Volume *, Volume *);
86 ci_AddItem(struct clone_head *ah, Inode aino)
88 register struct clone_items *ti;
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));
94 Log("ci_AddItem: malloc failed\n");
98 ti->next = (struct clone_items *)0;
103 /* first dude in the list */
104 ah->first = ah->last = ti;
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.
112 ti->data[ti->nitems++] = aino;
116 /* initialize a clone header */
118 ci_InitHead(struct clone_head *ah)
120 memset(ah, 0, sizeof(*ah));
124 /* apply a function to all dudes in the set */
126 ci_Apply(struct clone_head *ah, int (*aproc) (Inode, void *), void *arock)
128 register struct clone_items *ti;
131 for (ti = ah->first; ti; ti = ti->next) {
132 for (i = 0; i < ti->nitems; i++) {
133 (*aproc) (ti->data[i], arock);
139 /* free all dudes in the list */
141 ci_Destroy(struct clone_head *ah)
143 register struct clone_items *ti, *ni;
145 for (ti = ah->first; ti; ti = ni) {
146 ni = ti->next; /* guard against freeing */
153 IDecProc(Inode adata, struct clone_rock *aparm)
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;
177 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
178 int ReadWriteOriginal = VolumeWriteable(rwvp);
180 /* Open the RW volume's index file and seek to beginning */
181 IH_COPY(rwH, rwvp->vnodeIndex[class].handle);
185 rwfile = FDH_FDOPEN(rwFd, ReadWriteOriginal ? "r+" : "r");
188 STREAM_SEEK(rwfile, vcp->diskSize, 0); /* Will fail if no vnodes */
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);
195 clfileout = FDH_FDOPEN(clFdOut, "a");
198 code = STREAM_SEEK(clfileout, vcp->diskSize, 0);
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.
207 IH_COPY(clHin, clvp->vnodeIndex[class].handle);
208 clFdIn = IH_OPEN(clHin);
211 clfilein = FDH_FDOPEN(clFdIn, "r");
214 STREAM_SEEK(clfilein, vcp->diskSize, 0); /* Will fail if no vnodes */
217 /* Initialize list of inodes to nuke */
218 ci_InitHead(&decHead);
219 decRock.h = V_linkHandle(rwvp);
220 decRock.vol = V_parentId(rwvp);
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;
228 /* If we are recloning the volume, read the corresponding vnode
229 * from the clone and determine its inode number.
231 if (reclone && !STREAM_EOF(clfilein)
232 && (STREAM_READ(clvnode, vcp->diskSize, 1, clfilein) == 1)) {
233 clinode = VNDISK_GET_INO(clvnode);
238 if (rwvnode->type != vNull) {
239 if (rwvnode->vnodeMagic != vcp->magic)
241 rwinode = VNDISK_GET_INO(rwvnode);
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)) ==
249 Log("IH_INC failed: %x, %s, %u errno %d\n",
250 V_linkHandle(rwvp), PrintInode(NULL, rwinode),
251 V_parentId(rwvp), errno);
258 /* If a directory, mark vnode in old volume as cloned */
259 if ((rwvnode->type == vDirectory) && ReadWriteOriginal) {
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
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
273 rwvnode->dataVersion++;
276 code = STREAM_SEEK(rwfile, offset, 0);
279 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
283 code = STREAM_SEEK(rwfile, offset + vcp->diskSize, 0);
287 rwvnode->dataVersion--; /* Really needs to be set to the value in the inode,
288 * for the read-only volume */
293 /* Overwrite the vnode entry in the clone volume */
295 code = STREAM_WRITE(rwvnode, vcp->diskSize, 1, clfileout);
298 /* Couldn't clone, go back and decrement the inode's link count */
300 if (IH_DEC(V_linkHandle(rwvp), rwinode, V_parentId(rwvp)) ==
302 Log("IH_DEC failed: %x, %s, %u errno %d\n",
303 V_linkHandle(rwvp), PrintInode(NULL, rwinode),
304 V_parentId(rwvp), errno);
309 /* And if the directory was marked clone, unmark it */
312 if (STREAM_SEEK(rwfile, offset, 0) != -1)
313 (void)STREAM_WRITE(rwvnode, vcp->diskSize, 1, rwfile);
318 /* Removal of the old cloned inode */
320 ci_AddItem(&decHead, clinode); /* just queue it */
325 if (STREAM_ERROR(clfileout))
328 /* Clean out any junk at end of clone file */
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));
339 /* come here to finish up. If code is non-zero, we've already run into problems,
340 * and shouldn't do the idecs.
344 STREAM_CLOSE(rwfile);
346 STREAM_CLOSE(clfilein);
348 STREAM_CLOSE(clfileout);
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.
368 rwFd = IH_OPEN(clvp->vnodeIndex[class].handle);
374 /* If doing a reclone, we're keeping the clone. We need to
375 * truncate the file to offset bytes.
377 if (reclone && !error) {
378 error = FDH_TRUNC(rwFd, offset);
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.
390 code = ci_Apply(&decHead, IDecProc, (char *)&decRock);
393 ci_Destroy(&decHead);
399 CloneVolume(Error * rerror, Volume * original, Volume * new, Volume * old)
401 afs_int32 code, error = 0;
405 reclone = ((new == old) ? 1 : 0);
407 code = DoCloneIndex(original, new, vLarge, reclone);
410 code = DoCloneIndex(original, new, vSmall, reclone);
414 code = CopyVolumeHeader(&V_disk(original), &V_disk(new));