b4c84e344b9d9159eabf8723c28a773317c4119a
[openafs.git] / src / volser / vol_split.c
1 /*
2  * Copyright (c) 2007, Hartmut Reuter,
3  * RZG, Max-Planck-Institut f. Plasmaphysik.
4  * All Rights Reserved.
5  *
6  */
7
8 #include <afsconfig.h>
9 #include <afs/param.h>
10
11 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
12 #include <sys/types.h>
13 #include <stdio.h>
14 #ifdef AFS_PTHREAD_ENV
15 #include <assert.h>
16 #else /* AFS_PTHREAD_ENV */
17 #include <afs/assert.h>
18 #endif /* AFS_PTHREAD_ENV */
19 #ifdef AFS_NT40_ENV
20 #include <fcntl.h>
21 #include <windows.h>
22 #include <winbase.h>
23 #include <io.h>
24 #include <time.h>
25 #else
26 #include <sys/file.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #endif
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #else
33 #ifdef HAVE_STRINGS_H
34 #include <strings.h>
35 #endif
36 #endif
37 #include <errno.h>
38 #include <sys/stat.h>
39
40 #include <afs/dir.h>
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.h"
53 #include "volint.h"
54 #include "volser.h"
55 #include "physio.h"
56 #include "volser_prototypes.h"
57 #ifdef AFS_RXOSD_SUPPORT
58 #include "rxosd.h"
59 #include "vol_osd.h"
60 #include "../vol/vol_osd_prototypes.h"
61 #endif
62
63 #define NEEDED  1
64 #define PARENT  2
65 #define CHANGEPARENT 4
66
67 #define NAMEI_VNODEMASK    0x03ffffff
68 #define NAMEI_TAGMASK      0x7
69 #define NAMEI_TAGSHIFT     26
70 #define NAMEI_UNIQMASK     0xffffffff
71 #define NAMEI_UNIQSHIFT    32
72
73 struct VnodeExtract {
74     afs_uint32 vN;
75     afs_uint32 parent;
76     afs_uint32 flag;
77 };
78
79 struct Msg {
80     struct rx_call * call;
81     int verbose;
82     char line[1024];
83 };
84
85 static afs_int32 
86 ExtractVnodes(struct Msg *m, Volume *vol, afs_int32 class, 
87               struct VnodeExtract **list,
88               afs_uint32 *length, afs_uint32 where,
89               struct VnodeDiskObject *vd,
90               afs_uint32 *parent, struct VnodeDiskObject *parentvd)
91 {
92     afs_int32 code = 0;
93     char buf[SIZEOF_LARGEDISKVNODE];
94     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)&buf;
95     FdHandle_t *fdP = 0;
96     StreamHandle_t *stream = 0;
97     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
98     struct VnodeExtract *e;
99     afs_uint32 size;
100     afs_uint32 offset;
101
102     *length = 0;
103     if (parent)
104         *parent = 0;
105
106     fdP = IH_OPEN(vol->vnodeIndex[class].handle);
107     if (!fdP) {
108         sprintf(m->line, "Couldn't open %s Index of volume %u\n", 
109                 class ? "small":"large", V_id(vol));
110         rx_Write(m->call, m->line, strlen(m->line));
111         code = EIO;
112         goto Bad_Extract;
113     }
114     size = FDH_SIZE(fdP);
115     *list = (struct VnodeExtract *) malloc(size / vcp->diskSize
116                                         * sizeof(struct VnodeExtract));
117     if (!(*list)) {
118         code = ENOMEM;
119         goto Bad_Extract;
120     }
121     memset(*list, 0, size / vcp->diskSize * sizeof(struct VnodeExtract));
122     stream = FDH_FDOPEN(fdP, "r");
123     if (!stream) {
124         sprintf(m->line, "Couldn't stream open %s Index of volume %u\n", 
125                 class ? "small":"large", V_id(vol));
126         rx_Write(m->call, m->line, strlen(m->line));
127         return EIO;
128         goto Bad_Extract;
129     }
130     code = STREAM_SEEK(stream, vcp->diskSize, 0);
131     if (code)
132         goto Bad_Extract;
133
134     offset = vcp->diskSize;
135     e = *list;
136     while (!STREAM_EOF(stream)) {
137         afs_int32 vN = (offset >> (vcp->logSize -1)) - 1 + class;
138         if (STREAM_READ(vnode, vcp->diskSize, 1, stream) == 1) {
139             if (vnode->type != vNull) {
140                 e->vN = vN;
141                 e->parent = vnode->parent;
142                 if (vN == where && class == vLarge) {
143                     memcpy(vd, vnode, vcp->diskSize);
144                     *parent = vnode->parent;
145                 }
146                 e++;
147             }
148             offset += vcp->diskSize;
149         }
150     }
151     *length = (e - *list);
152     if (class == vLarge) {
153         if (*parent) {
154             offset = (*parent + 1 - class) << (vcp->logSize -1);
155             code = STREAM_SEEK(stream, offset, 0);
156             if (STREAM_READ(vnode, vcp->diskSize, 1, stream) == 1) 
157                 memcpy(parentvd, vnode, vcp->diskSize);
158             else
159                 code = EIO;
160         } else {
161             sprintf(m->line, "SplitVolume: extract didn't see directory %u\n", where);
162             rx_Write(m->call, m->line, strlen(m->line));
163             code = ENOENT;
164         }
165     }
166     if (m->verbose) {
167         sprintf(m->line, "Volume %u has %u %s vnodes in volume %u\n",
168                         V_parentId(vol), *length, class? "small":"large",
169                         V_id(vol));
170         rx_Write(m->call, m->line, strlen(m->line));
171     }
172     
173 Bad_Extract:
174     if (stream)
175         STREAM_CLOSE(stream);
176     if (fdP)
177         FDH_CLOSE(fdP);
178     if (code) {
179         free(*list);
180         *list = 0;
181     }
182     return code;
183 }
184
185 static afs_int32 
186 FindVnodes(struct Msg *m, afs_uint32 where, 
187            struct VnodeExtract *list, afs_int32 length, 
188            struct VnodeExtract *dlist, afs_int32 dlength, 
189            afs_uint32 *needed, afs_int32 class)
190 {
191     afs_int32 i, j, found = 0;
192     afs_int32 parent = 0;
193
194     *needed = 0;
195     for (i=0; i<length; i++) {
196         if (list[i].vN == where) {        /* dir to be replaced by mount point */
197             list[i].flag |= NEEDED;
198             parent = list[i].parent;
199             found = 1;
200             (*needed)++;
201         }
202         if (list[i].parent == where) {          /* all 1st generation children */
203             list[i].flag |= (NEEDED + CHANGEPARENT);
204             (*needed)++;
205         }
206     } 
207     if (list[0].vN & 1) {               /* only for directories */
208         if (!found) {
209             sprintf(m->line, 
210                 "SplitVolume: directory %u where to start new volume not found\n",
211                  where);
212             rx_Write(m->call, m->line, strlen(m->line));
213             return ENOENT;
214         }
215         found = 0;
216         for (i=0; i<length; i++) { 
217             if (list[i].vN == parent) { /* dir where to create mount point */
218                 list[i].flag |= PARENT;
219                 found = 1;
220                 break;
221             }
222         }
223         if (!found) {
224             sprintf(m->line, "SplitVolume: parent directory %u not found\n", 
225                         parent);
226             rx_Write(m->call, m->line, strlen(m->line));
227             return ENOENT;
228         }
229     }
230     found = 1;
231     while (found) {
232         found = 0;
233         for (i=0; i<dlength; i++) {
234             if (!(dlist[i].flag & NEEDED)) /* dirs to remain in old volume */
235                 continue;
236             for (j=0; j<length; j++) {
237                 if (list[j].parent == dlist[i].vN && !(list[j].flag & NEEDED)) {
238                     list[j].flag |= NEEDED;
239                     (*needed)++;
240                     found = 1;
241                 }
242             }
243         }
244     }
245     if (m->verbose) {
246         sprintf(m->line, "%u %s vnodes will go into the new volume\n", 
247                         *needed, class ? "small" : "large");
248         rx_Write(m->call, m->line, strlen(m->line));
249     }
250     return 0;
251 }
252     
253 static afs_int32 
254 copyDir(struct Msg *m, IHandle_t *inh, IHandle_t *outh)
255 {
256     FdHandle_t *infdP, *outfdP;
257     char *tbuf;
258     afs_size_t size;
259
260     infdP = IH_OPEN(inh);
261     if (!infdP) {
262         sprintf(m->line, "Couldn't open input directory %u.%u.%u\n", 
263                     infdP->fd_ih->ih_vid,
264                     (afs_uint32)(infdP->fd_ih->ih_ino & NAMEI_VNODEMASK),
265                     (afs_uint32)(infdP->fd_ih->ih_ino >> NAMEI_UNIQSHIFT));
266         rx_Write(m->call, m->line, strlen(m->line));
267         return EIO;
268     }
269     outfdP = IH_OPEN(outh);
270     if (!outfdP) {
271         sprintf(m->line, "Couldn't open output directory %u.%u.%u\n", 
272                     outfdP->fd_ih->ih_vid,
273                     (afs_uint32)(outfdP->fd_ih->ih_ino & NAMEI_VNODEMASK),
274                     (afs_uint32)(outfdP->fd_ih->ih_ino >> NAMEI_UNIQSHIFT));
275         rx_Write(m->call, m->line, strlen(m->line));
276         FDH_REALLYCLOSE(infdP);
277         return EIO;
278     }
279     tbuf = malloc(2048);
280     FDH_SEEK(infdP, 0, 0);
281     FDH_SEEK(outfdP, 0, 0);
282     size = FDH_SIZE(infdP);
283     while (size) {
284         afs_int32 tlen;
285         tlen = size > 2048 ? 2048 : size;
286         if (FDH_READ(infdP, tbuf, tlen) != tlen) {
287             sprintf(m->line, "Couldn't read directory %u.%u.%u\n", 
288                     infdP->fd_ih->ih_vid,
289                     (afs_uint32)(infdP->fd_ih->ih_ino & NAMEI_VNODEMASK),
290                     (afs_uint32)(infdP->fd_ih->ih_ino >> NAMEI_UNIQSHIFT));
291             rx_Write(m->call, m->line, strlen(m->line));
292             FDH_REALLYCLOSE(infdP);
293             FDH_REALLYCLOSE(outfdP);
294             free(tbuf);
295             return EIO;
296         }
297         if (FDH_WRITE(outfdP, tbuf, tlen) != tlen) {
298             sprintf(m->line, "Couldn't write directory %u.%u.%u\n", 
299                     outfdP->fd_ih->ih_vid,
300                     (afs_uint32)(outfdP->fd_ih->ih_ino & NAMEI_VNODEMASK),
301                     (afs_uint32)(outfdP->fd_ih->ih_ino >> NAMEI_UNIQSHIFT));
302             rx_Write(m->call, m->line, strlen(m->line));
303             FDH_REALLYCLOSE(infdP);
304             FDH_REALLYCLOSE(outfdP);
305             free(tbuf);
306             return EIO;
307         }
308         size -= tlen;
309     }
310     free(tbuf);
311     FDH_CLOSE(outfdP);
312     FDH_REALLYCLOSE(infdP);
313     return 0;
314 }
315
316 afs_int32 copyVnodes(struct Msg *m, Volume *vol, Volume *newvol, 
317                         afs_int32 class, 
318                         struct VnodeExtract *list, afs_int32 length,
319                         afs_int32 where, afs_uint64 *blocks,
320                         struct VnodeDiskObject *parVnode) 
321 {
322     afs_int32 i, code = 0;
323     char buf[SIZEOF_LARGEDISKVNODE];
324     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)&buf;
325     FdHandle_t *fdP = 0;
326     FdHandle_t *newfdP = 0;
327     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
328     struct VnodeExtract *e;
329     afs_uint64 size;
330     afs_uint64 offset;
331     Inode ino, newino;
332
333     fdP = IH_OPEN(vol->vnodeIndex[class].handle);
334     if (!fdP) {
335         Log("Couldn't open %s Index of volume %u\n", 
336                 class ? "small":"large", V_id(vol));
337         code = EIO;
338         goto Bad_Copy;
339     }
340     newfdP = IH_OPEN(newvol->vnodeIndex[class].handle);
341     if (!newfdP) {
342         Log("Couldn't open %s Index of volume %u\n", 
343                 class ? "small":"large", V_id(newvol));
344         code = EIO;
345         goto Bad_Copy;
346     }
347     size = FDH_SIZE(fdP);
348
349     for (i=0; i<length; i++) {
350         e = &list[i];
351         if (e->flag) {
352             afs_uint64 size;
353             offset = (e->vN + 1 - class) << (vcp->logSize -1);
354             if (FDH_SEEK(fdP, offset, 0) != offset
355              || FDH_READ(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
356                 Log("Couldn't read in %s Index of volume %u at offset\n", 
357                         class ? "small":"large", V_id(vol), offset);
358                 code = EIO;
359                 goto Bad_Copy;
360             }
361             if (e->flag & PARENT) {
362                 /* 
363                  *   do a preventive copy on write for later update 
364                  */ 
365                 IHandle_t *newh = 0;
366                 IHandle_t *h = 0;
367 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
368                 Inode nearInode;
369                 V_pref(vol,nearInode)
370 #endif
371
372                 newino = IH_CREATE(V_linkHandle(vol), V_device(vol),
373                                 VPartitionPath(V_partition(vol)),
374                                 nearInode, V_parentId(vol), 
375                                 e->vN, vnode->uniquifier,
376                                 vnode->dataVersion);
377                 IH_INIT(newh, V_device(vol), V_parentId(vol), newino);
378                 ino = VNDISK_GET_INO(vnode);
379                 IH_INIT(h, V_device(vol), V_parentId(vol), ino);
380                 code = copyDir(m, h, newh);
381                 if (code)
382                     goto Bad_Copy;
383                 /* Now update the vnode and write it back to disk */
384                 VNDISK_SET_INO(vnode, newino);
385                 vnode->cloned = 0;
386                 if (FDH_SEEK(fdP, offset, 0) != offset
387                  || FDH_WRITE(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
388                     Log("Couldn't write in %s Index of volume %u at offset\n", 
389                             class ? "small":"large", V_id(vol), offset);
390                     code = EIO;
391                     goto Bad_Copy;
392                 }
393                 memcpy(parVnode, vnode, sizeof(struct VnodeDiskObject));
394             }
395             if (e->flag & NEEDED && e->vN != where) {
396                 VNDISK_GET_LEN(size, vnode);
397                 *blocks += (size + 0x3ff) >> 10;
398                 ino = VNDISK_GET_INO(vnode);
399                 if (ino) {
400                     IHandle_t *h, *newh;
401                     Inode nearInode;
402 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
403                     V_pref(vol,nearInode)
404 #endif
405                     IH_INIT(h, vol->device, V_parentId(vol), ino);
406                     if (e->parent == where) 
407                         vnode->parent = 1;
408                     newino = IH_CREATE(V_linkHandle(newvol), V_device(newvol),
409                                 VPartitionPath(V_partition(newvol)),
410                                 nearInode, V_parentId(newvol), 
411                                 e->vN, vnode->uniquifier,
412                                 vnode->dataVersion);
413                     if (!VALID_INO(newino)) {
414                         Log("IH_CREATE failed for %u.%u.%u\n", 
415                             V_id(newvol), e->vN, vnode->uniquifier);
416                         code = EIO;
417                         goto Bad_Copy;
418                     }
419                     nearInode = newino;
420                     IH_INIT(newh, newvol->device, V_parentId(newvol), newino);
421                     code = namei_replace_file_by_hardlink(newh, h);
422                     VNDISK_SET_INO(vnode, newino);
423 #ifdef AFS_RXOSD_SUPPORT
424                 } else {
425                     code = osd_split_objects(vol, newvol, vnode, e->vN);
426 #endif /*  AFS_RXOSD_SUPPORT */
427                 }
428                 if (code)
429                     goto Bad_Copy;
430                 if (e->flag & CHANGEPARENT)
431                     vnode->parent = 1; /* in new root-directory */
432                 vnode->cloned = 0;
433                 if (FDH_SEEK(newfdP, offset, 0) != offset
434                  || FDH_WRITE(newfdP, vnode, vcp->diskSize) != vcp->diskSize) {
435                     Log("Couldn't write in %s Index of volume %u to offset\n", 
436                             class ? "small":"large", V_id(newvol), 
437                             offset);
438                     code = EIO;
439                     goto Bad_Copy;
440                 }
441             }
442         }
443     }
444     /*
445      *  Now copy the root directory from old to new volume 
446      */
447     if (class == vLarge) {
448         IHandle_t *h, *newh;
449         char buf2[SIZEOF_LARGEDISKVNODE];
450         struct VnodeDiskObject *vnode2 = (struct VnodeDiskObject *)&buf2;
451         afs_uint64 newoffset;
452         
453         newoffset = vcp->diskSize;
454         if (FDH_SEEK(newfdP, newoffset, 0) != newoffset
455          || FDH_READ(newfdP, vnode2, vcp->diskSize) != vcp->diskSize) {
456             Log("splitvolume: couldn't read in large Index of new volume %u at offset %u\n", 
457                     V_id(newvol), vcp->diskSize);
458             code = EIO;
459             goto Bad_Copy;
460         }
461         offset = (where + 1 - class) << (vcp->logSize -1);
462         if (FDH_SEEK(fdP, offset, 0) != offset
463          || FDH_READ(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
464             Log("Couldn't read in large Index of old volume %u at offset\n", 
465                         V_id(vol), offset);
466             code = EIO;
467             goto Bad_Copy;
468         }
469         VNDISK_GET_LEN(size, vnode);
470         *blocks += (size + 0x3ff) >> 10;
471         ino = VNDISK_GET_INO(vnode);
472         IH_INIT(h, vol->device, V_parentId(vol), ino);
473         newino = VNDISK_GET_INO(vnode2);
474         IH_INIT(newh, newvol->device, V_parentId(newvol), newino);
475         code = copyDir(m, h, newh);
476         if (code) {
477             Log("splitvolume: copyDir failed for new root from %u.u.u to %u.1.1\n", 
478                         V_id(vol), where, vnode->uniquifier, V_id(newvol));
479             code = EIO;
480             goto Bad_Copy;
481         }
482         VNDISK_SET_INO(vnode, newino);
483         vnode->uniquifier = 1;
484         vnode->cloned = 0;
485         vnode->parent = vnode2->parent;
486         vnode->serverModifyTime = vnode2->serverModifyTime;
487         if (FDH_SEEK(newfdP, newoffset, 0) != newoffset
488           || FDH_WRITE(newfdP, vnode, vcp->diskSize) != vcp->diskSize) {
489             Log("splitvolume: couldn't write in large Index of %u at offset %u\n", 
490                     V_id(newvol), vcp->diskSize);
491             code = EIO;
492         } 
493     }
494 Bad_Copy:
495     if (fdP)
496         FDH_CLOSE(fdP);
497     if (newfdP)
498         FDH_CLOSE(newfdP);
499     return code;
500 }
501
502 static afs_int32
503 findName(Volume *vol, struct VnodeDiskObject *vd, afs_uint32 vN, 
504          afs_uint32 un, char *name,afs_int32 length)
505 {
506     afs_int32 code;
507     Inode ino;
508     DirHandle dir;
509
510     ino = VNDISK_GET_INO(vd);
511     SetSalvageDirHandle(&dir, V_id(vol), V_device(vol), ino);
512
513     code = InverseLookup(&dir, vN, un, name, length);
514     FidZap(&dir);   
515     return code;
516 }
517
518 static afs_int32
519 createMountpoint(Volume *vol, Volume *newvol, struct VnodeDiskObject *parent, 
520                 afs_uint32 vN,  struct VnodeDiskObject *vd, char *name)
521 {
522     afs_int32 code;
523     Inode ino, newino;
524     DirHandle dir;
525     IHandle_t *h;
526     struct VnodeDiskObject vnode;
527     FdHandle_t *fdP, *fdP2;
528     afs_uint64 offset, size;
529     afs_int32 class = vSmall;
530     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
531 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
532     Inode nearInode = 0;
533 #endif
534     AFSFid fid;
535     struct timeval now;
536     afs_uint32 newvN;
537     char symlink[32];
538
539     FT_GetTimeOfDay(&now, 0);
540     fdP = IH_OPEN(vol->vnodeIndex[vSmall].handle);
541     if (!fdP) {
542         Log("split volume: error opening small vnode index of %u\n", V_id(vol));
543         return EIO;
544     }
545     offset = vcp->diskSize;
546     if (FDH_SEEK(fdP, offset, 0) != offset) {
547         Log("split volume: error seeking in small vnode index of %u\n", V_id(vol));
548         return EIO;
549     }
550     while (1) {
551         if (FDH_READ(fdP, &vnode, vcp->diskSize) != vcp->diskSize)
552             break;
553         if (vnode.type == vNull)
554             break;
555         offset += vcp->diskSize;
556     }
557     memset(&vnode, 0, sizeof(vnode));
558     vnode.type = vSymlink;
559     V_nextVnodeUnique(vol)++;
560     vnode.uniquifier = V_nextVnodeUnique(vol);
561     vnode.author = vd->author;
562     vnode.owner = vd->owner;
563     vnode.group = vd->group;
564     vnode.modeBits = 0644;
565     vnode.unixModifyTime = now.tv_sec;
566     vnode.serverModifyTime = now.tv_sec;
567     vnode.dataVersion = 1;
568     vnode.linkCount = 1;
569     vnode.parent = vN;
570
571     newvN = (offset >> (VnodeClassInfo[class].logSize - 1)) - 1 + class;
572 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
573     V_pref(vol,nearInode)
574 #endif
575     newino = IH_CREATE(V_linkHandle(vol), V_device(vol),
576                 VPartitionPath(V_partition(vol)), nearInode,
577                 V_parentId(vol), newvN, vnode.uniquifier, 1);
578     
579     IH_INIT(h, V_device(vol), V_parentId(vol), newino);
580     fdP2 = IH_OPEN(h);
581     if (!fdP2) {
582         Log("split volume: couldn't open inode for mountpoint %u.%u.%u\n", 
583                 V_id(vol), newvN, vnode.uniquifier);
584         return EIO;
585     }
586     FDH_SEEK(fdP2, 0, 0);
587     sprintf(symlink, "#%s", V_name(newvol));
588     size = strlen(symlink) + 1;
589     if (FDH_WRITE(fdP2, symlink, size) != size) {
590         Log("split volume: couldn't write mountpoint %u.%u.%u\n", 
591                 V_id(vol), newvN, vnode.uniquifier);
592         return EIO;
593     }
594     FDH_REALLYCLOSE(fdP2);
595     IH_RELEASE(h);
596     VNDISK_SET_INO(&vnode, newino);
597     VNDISK_SET_LEN(&vnode, size);
598 #ifndef AFS_RXOSD_SUPPORT
599     vnode.vnodeMagic = SMALLVNODEMAGIC;
600 #endif    
601     if (FDH_SEEK(fdP, offset, 0) != offset
602       || FDH_WRITE(fdP, &vnode, vcp->diskSize) != vcp->diskSize) {
603         Log("split volume: couldn't write vnode for mountpoint %u.%u.%u\n", 
604                 V_id(vol), newvN, vnode.uniquifier);
605         return EIO;
606     }
607     FDH_REALLYCLOSE(fdP);
608
609     fid.Volume = V_id(vol);
610     fid.Vnode = newvN;
611     fid.Unique = vnode.uniquifier;
612
613     /*
614      * Now  update the parent directory.
615      */
616
617     ino = VNDISK_GET_INO(parent);
618     SetSalvageDirHandle(&dir, V_id(vol), V_device(vol), ino);
619
620     code = Delete(&dir, name);
621     if (code) {
622         Log("splitvolume: couldn't delete directory entry for %s in %u.%u.%u, code = %d\n",
623                         name, V_id(vol), vN, parent->uniquifier, code);
624         return code;
625     }
626     code = Create(&dir, name, &fid);
627     FidZap(&dir);   
628  
629     class = vLarge;
630     vcp = &VnodeClassInfo[class];
631     fdP = IH_OPEN(vol->vnodeIndex[class].handle);
632     offset = (vN + 1 - class) << (vcp->logSize -1);
633     parent->dataVersion++;
634     if (FDH_SEEK(fdP, offset, 0) != offset
635       || FDH_WRITE(fdP, parent, vcp->diskSize) != vcp->diskSize) {
636         Log("split volume: couldn't write vnode for parent directory %u.%u.%u\n", 
637                 V_id(vol), vN, parent->uniquifier);
638         return EIO;
639     }
640     FDH_REALLYCLOSE(fdP);
641     return code;
642 }
643
644 static afs_int32 
645 deleteVnodes(Volume *vol, afs_int32 class, 
646              struct VnodeExtract *list, afs_int32 length,
647              afs_uint64 *blocks)
648 {
649     afs_int32 i, code = 0;
650     char buf[SIZEOF_LARGEDISKVNODE];
651     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)&buf;
652     FdHandle_t *fdP = 0;
653     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
654     struct VnodeExtract *e;
655     afs_uint64 size;
656     afs_uint64 offset;
657     Inode ino;
658
659     fdP = IH_OPEN(vol->vnodeIndex[class].handle);
660     if (!fdP) {
661         Log("Couldn't open %s Index of volume %u\n", 
662                 class ? "small":"large", V_id(vol));
663         code = EIO;
664         goto Bad_Delete;
665     }
666     size = FDH_SIZE(fdP);
667
668     for (i=0; i<length; i++) {
669         e = &list[i];
670         if (e->flag & NEEDED) {
671             afs_uint64 size;
672             offset = (e->vN + 1 - class) << (vcp->logSize -1);
673             if (FDH_SEEK(fdP, offset, 0) != offset) {
674                 Log("Couldn't seek in %s Index of volume %u to offset\n", 
675                         class ? "small":"large", V_id(vol), offset);
676                 code = EIO;
677                 goto Bad_Delete;
678             }
679             if (FDH_READ(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
680                 Log("Couldn't read in %s Index of volume %u at offset\n", 
681                         class ? "small":"large", V_id(vol), offset);
682                 code = EIO;
683                 goto Bad_Delete;
684             }
685             VNDISK_GET_LEN(size, vnode);
686             *blocks += (size + 0x3ff) >> 10;
687             ino = VNDISK_GET_INO(vnode);
688             if (ino) {
689                 IHandle_t *h;
690                 IH_INIT(h, vol->device, V_parentId(vol), ino);
691                     IH_DEC(h, ino, V_parentId(vol));
692 #ifdef AFS_RXOSD_SUPPORT
693             } else {
694                 code = osdRemove(vol, vnode, e->vN);
695 #endif /*  AFS_RXOSD_SUPPORT */
696             }
697             memset(vnode, 0, vcp->diskSize);
698             vnode->type = vNull;
699             if (FDH_SEEK(fdP, offset, 0) != offset 
700               || FDH_WRITE(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
701                    Log("Couldn't write in %s Index of volume %u to offset\n", 
702                             class ? "small":"large", V_id(vol), 
703                             offset);
704             }
705         }
706     }
707 Bad_Delete:
708     if (fdP)
709         FDH_CLOSE(fdP);
710     return code;
711 }
712
713 afs_int32 
714 split_volume(struct rx_call *call, Volume *vol, Volume *newvol, 
715              afs_uint32 where, afs_int32 verbose)
716 {
717     Error code = 0;
718     struct VnodeExtract *dirList = 0;
719     struct VnodeExtract *fileList = 0;
720     afs_uint64 blocks = 0;
721     afs_uint32 filesNeeded, dirsNeeded;
722     afs_uint32 dl, fl;
723     char buf[SIZEOF_LARGEDISKVNODE];
724     char buf2[SIZEOF_LARGEDISKVNODE];
725     struct VnodeDiskObject *rootVnode = (struct VnodeDiskObject *)&buf;
726     struct VnodeDiskObject *parVnode = (struct VnodeDiskObject *)&buf2;
727     char name[256];
728     afs_uint32 parent;
729     struct Msg *m;
730
731     m = (struct Msg *) malloc(sizeof(struct Msg));
732     memset(m, 0, sizeof(struct Msg));
733     m->call = call;
734     m->verbose = verbose;
735
736     /* 
737      *  First step: planning
738      *
739      *  Find out which directories will belong to the new volume
740      *
741      */
742     if (verbose) {
743         sprintf(m->line, 
744                 "1st step: extract vnode essence from large vnode file\n");
745         rx_Write(m->call, m->line, strlen(m->line));
746     }
747
748     code = ExtractVnodes(m, vol, vLarge, &dirList, &dl, where, rootVnode, 
749                         &parent, parVnode);
750     if (code) {
751         sprintf(m->line, 
752                 "ExtractVnodes failed for %u for directories with code %d\n",
753                 V_id(vol), code);
754         rx_Write(m->call, m->line, strlen(m->line));
755         return code;
756     }
757
758     if (verbose) {
759         sprintf(m->line, "2nd step: look for name of vnode %u in directory %u.%u.%u\n",
760                 where, V_id(vol), parent, parVnode->uniquifier); 
761         rx_Write(m->call, m->line, strlen(m->line));
762     }
763     code = findName(vol, parVnode, where, rootVnode->uniquifier, 
764                     name,  sizeof(name));
765     if (code) {
766         sprintf(m->line, 
767                 "splitvolume: could'nt find name of %u in directory %u.%u.%u.\n",
768                 where, V_id(vol), parent, parVnode->uniquifier); 
769         rx_Write(m->call, m->line, strlen(m->line));
770         return code;
771     }
772     if (verbose) {
773         sprintf(m->line, "name of %u is %s\n", where, name); 
774         rx_Write(m->call, m->line, strlen(m->line));
775     }
776
777     if (verbose) {
778         sprintf(m->line, "3rd step: find all directory vnodes belonging to the subtree under %u \"%s\"\n", 
779                         where, name);
780         rx_Write(m->call, m->line, strlen(m->line));
781     }
782     code = FindVnodes(m, where, dirList, dl, dirList, dl, &dirsNeeded, 1);
783     if (code) {
784         sprintf(m->line, 
785                 "FindVnodes for directories failed with code %d\n", code);
786         rx_Write(m->call, m->line, strlen(m->line));
787         return code;
788     }
789
790     if (verbose) {
791         sprintf(m->line, "4th step extract vnode essence from small vnode file\n");
792         rx_Write(m->call, m->line, strlen(m->line));
793     }
794     code = ExtractVnodes(m, vol, vSmall, &fileList, &fl, where, 0, 0, 0);
795     if (code) {
796         sprintf(m->line, 
797                 "ExtractVnodes failed for %u for files with code %d\n",
798                 V_id(vol), code);
799         rx_Write(m->call, m->line, strlen(m->line));
800         return code;
801     }
802     if (verbose) {
803         sprintf(m->line, "5th step: find all small vnodes belonging to the subtree under %u \"%s\"\n", 
804                         where, name);
805         rx_Write(m->call, m->line, strlen(m->line));
806     }
807     FindVnodes(m, where, fileList, fl, dirList, dl, &filesNeeded, 0);
808
809     /* 
810      *  Third step: create hard links for all files needed
811      *
812      */
813
814     V_destroyMe(newvol) = DESTROY_ME;
815     V_inService(newvol) = 0;
816     if (verbose) {
817         sprintf(m->line, "6th step: create hard links in the AFSIDat tree between files of the old and new volume\n");
818         rx_Write(m->call, m->line, strlen(m->line));
819     }
820     code = copyVnodes(m, vol, newvol, 1, fileList, fl, where, &blocks, 0); 
821     if (code) {
822         sprintf(m->line, "copyVnodes for files failed with code %d\n", code);
823         rx_Write(m->call, m->line, strlen(m->line));
824         return code;
825     }
826         
827     /* 
828      *  Forth step: create hard links for all directories and copy 
829      *  split directory to new root directory
830      */
831
832     if (verbose) {
833         sprintf(m->line, "7th step: create hard links in the AFSIDat tree between directories of the old and new volume and make dir %u to new volume's root directory.\n",
834                 where);
835         rx_Write(m->call, m->line, strlen(m->line));
836     }
837     code = copyVnodes(m, vol, newvol, 0, dirList, dl, where, &blocks, parVnode); 
838     if (code) {
839         sprintf(m->line, "copyVnodes for directories failed with code %d\n", code);
840         rx_Write(m->call, m->line, strlen(m->line));
841         return code;
842     }
843
844     /*
845      *  Finalize new volume
846      *
847      */
848     if (verbose) {
849         sprintf(m->line, "8th step: write new volume's metadata to disk\n");
850         rx_Write(m->call, m->line, strlen(m->line));
851     }
852
853     V_diskused(newvol) = blocks;
854 #ifdef AFS_RXOSD_SUPPORT
855     V_osdFlag(newvol) = V_osdFlag(vol);
856 #endif
857     V_filecount(newvol) = filesNeeded + dirsNeeded;
858     V_destroyMe(newvol) = 0;
859     V_maxquota(newvol) = V_maxquota(vol);
860     V_uniquifier(newvol) = V_uniquifier(vol);
861     V_inService(newvol) = 1;
862     VUpdateVolume(&code, newvol);
863
864     /*
865      *  Sixth step: change directory entry in old volume:
866      *  rename old tree and create mount point for new volume.
867      */
868     if (verbose) {
869         sprintf(m->line, "9th step: create mountpoint \"%s\" for new volume in old volume's directory %u.\n", name, parent);
870         rx_Write(m->call, m->line, strlen(m->line));
871     }
872     
873     code = createMountpoint(vol, newvol, parVnode, parent, rootVnode, name);
874     if (code) {
875         sprintf(m->line, "createMountpoint failed with code %d\n", code);
876         rx_Write(m->call, m->line, strlen(m->line));
877         return code;
878     }
879     /*
880      * Now both volumes should be ready and consistent, but the old volume
881      * contains still the vnodes and data we transferred into the new one.
882      * Delete orphaned vnodes and data.
883      */ 
884
885     blocks = 0;
886     if (verbose) {
887         sprintf(m->line, "10th step: delete large vnodes belonging to subtree in the old volume.\n");
888         rx_Write(m->call, m->line, strlen(m->line));
889     }
890     deleteVnodes(vol, vLarge, dirList, dl, &blocks);
891     if (verbose) {
892         sprintf(m->line, "11th step: delete small vnodes belonging to subtree in the old volume.\n");
893         rx_Write(m->call, m->line, strlen(m->line));
894     }
895     deleteVnodes(vol, vSmall, fileList, fl, &blocks);
896     V_diskused(vol) -= blocks;
897     V_filecount(vol) -= (filesNeeded + dirsNeeded + 1);
898     VUpdateVolume(&code, vol);
899
900     sprintf(m->line, "Finished!\n");
901     rx_Write(m->call, m->line, strlen(m->line));
902     m->line[0] = 0;
903     m->line[1] = 0;
904     m->line[2] = 0;
905     m->line[3] = 0;
906     rx_Write(m->call, m->line, 4);
907     free(m);
908     return code;
909 }
910 #endif