pam test should return an int in main
[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 <afs/nfs.h>
44 #include <lwp.h>
45 #include <lock.h>
46 #include <afs/afssyscalls.h>
47 #include <afs/ihandle.h>
48 #include <afs/vnode.h>
49 #include <afs/volume.h>
50 #include <afs/partition.h>
51 #include <afs/viceinode.h>
52 #include "vol.h"
53 #include "volint.h"
54 #include "volser.h"
55 #include "physio.h"
56 #include "volser_internal.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_sfsize_t 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_ASEEK(stream, vcp->diskSize);
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_ASEEK(stream, offset);
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_sfsize_t size;
259     afs_foff_t offset;
260
261     infdP = IH_OPEN(inh);
262     if (!infdP) {
263         sprintf(m->line, "Couldn't open input directory %u.%u.%u\n",
264                     infdP->fd_ih->ih_vid,
265                     (afs_uint32)(infdP->fd_ih->ih_ino & NAMEI_VNODEMASK),
266                     (afs_uint32)(infdP->fd_ih->ih_ino >> NAMEI_UNIQSHIFT));
267         rx_Write(m->call, m->line, strlen(m->line));
268         return EIO;
269     }
270     outfdP = IH_OPEN(outh);
271     if (!outfdP) {
272         sprintf(m->line, "Couldn't open output directory %u.%u.%u\n",
273                     outfdP->fd_ih->ih_vid,
274                     (afs_uint32)(outfdP->fd_ih->ih_ino & NAMEI_VNODEMASK),
275                     (afs_uint32)(outfdP->fd_ih->ih_ino >> NAMEI_UNIQSHIFT));
276         rx_Write(m->call, m->line, strlen(m->line));
277         FDH_REALLYCLOSE(infdP);
278         return EIO;
279     }
280     tbuf = malloc(2048);
281     offset = 0;
282     size = FDH_SIZE(infdP);
283     while (size) {
284         size_t tlen;
285         tlen = size > 2048 ? 2048 : size;
286         if (FDH_PREAD(infdP, tbuf, tlen, offset) != 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_PWRITE(outfdP, tbuf, tlen, offset) != 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         offset += tlen;
310     }
311     free(tbuf);
312     FDH_CLOSE(outfdP);
313     FDH_REALLYCLOSE(infdP);
314     return 0;
315 }
316
317 afs_int32 copyVnodes(struct Msg *m, Volume *vol, Volume *newvol,
318                         afs_int32 class,
319                         struct VnodeExtract *list, afs_int32 length,
320                         afs_int32 where, afs_uint64 *blocks,
321                         struct VnodeDiskObject *parVnode)
322 {
323     afs_int32 i, code = 0;
324     char buf[SIZEOF_LARGEDISKVNODE];
325     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)&buf;
326     FdHandle_t *fdP = 0;
327     FdHandle_t *newfdP = 0;
328     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
329     struct VnodeExtract *e;
330     afs_sfsize_t size;
331     afs_uint64 offset;
332     Inode ino, newino;
333
334     fdP = IH_OPEN(vol->vnodeIndex[class].handle);
335     if (!fdP) {
336         Log("Couldn't open %s Index of volume %u\n",
337                 class ? "small":"large", V_id(vol));
338         code = EIO;
339         goto Bad_Copy;
340     }
341     newfdP = IH_OPEN(newvol->vnodeIndex[class].handle);
342     if (!newfdP) {
343         Log("Couldn't open %s Index of volume %u\n",
344                 class ? "small":"large", V_id(newvol));
345         code = EIO;
346         goto Bad_Copy;
347     }
348     size = FDH_SIZE(fdP);
349
350     for (i=0; i<length; i++) {
351         e = &list[i];
352         if (e->flag) {
353             afs_uint64 size;
354             offset = (e->vN + 1 - class) << (vcp->logSize -1);
355             if (FDH_PREAD(fdP, vnode, vcp->diskSize, offset) != vcp->diskSize) {
356                 Log("Couldn't read in %s Index of volume %u at offset %"
357                     AFS_UINT64_FMT "\n", class ? "small":"large",
358                     V_id(vol), offset);
359                 code = EIO;
360                 goto Bad_Copy;
361             }
362             if (e->flag & PARENT) {
363                 /*
364                  *   do a preventive copy on write for later update
365                  */
366                 IHandle_t *newh = 0;
367                 IHandle_t *h = 0;
368 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
369                 Inode nearInode;
370                 V_pref(vol,nearInode)
371 #endif
372
373                 newino = IH_CREATE(V_linkHandle(vol), V_device(vol),
374                                 VPartitionPath(V_partition(vol)),
375                                 nearInode, V_parentId(vol),
376                                 e->vN, vnode->uniquifier,
377                                 vnode->dataVersion);
378                 IH_INIT(newh, V_device(vol), V_parentId(vol), newino);
379                 ino = VNDISK_GET_INO(vnode);
380                 IH_INIT(h, V_device(vol), V_parentId(vol), ino);
381                 code = copyDir(m, h, newh);
382                 if (code)
383                     goto Bad_Copy;
384                 /* Now update the vnode and write it back to disk */
385                 VNDISK_SET_INO(vnode, newino);
386                 vnode->cloned = 0;
387                 if (FDH_PWRITE(fdP, vnode, vcp->diskSize, offset) != vcp->diskSize) {
388                     Log("Couldn't write in %s Index of volume %u at offset %"
389                         AFS_UINT64_FMT "\n", class ? "small":"large",
390                         V_id(vol), offset);
391                     code = EIO;
392                     goto Bad_Copy;
393                 }
394                 memcpy(parVnode, vnode, sizeof(struct VnodeDiskObject));
395             }
396             if (e->flag & NEEDED && e->vN != where) {
397                 VNDISK_GET_LEN(size, vnode);
398                 *blocks += (size + 0x3ff) >> 10;
399                 ino = VNDISK_GET_INO(vnode);
400                 if (ino) {
401                     IHandle_t *h, *newh;
402                     Inode nearInode;
403 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
404                     V_pref(vol,nearInode)
405 #endif
406                     IH_INIT(h, vol->device, V_parentId(vol), ino);
407                     if (e->parent == where)
408                         vnode->parent = 1;
409                     newino = IH_CREATE(V_linkHandle(newvol), V_device(newvol),
410                                 VPartitionPath(V_partition(newvol)),
411                                 nearInode, V_parentId(newvol),
412                                 e->vN, vnode->uniquifier,
413                                 vnode->dataVersion);
414                     if (!VALID_INO(newino)) {
415                         Log("IH_CREATE failed for %u.%u.%u\n",
416                             V_id(newvol), e->vN, vnode->uniquifier);
417                         code = EIO;
418                         goto Bad_Copy;
419                     }
420                     nearInode = newino;
421                     IH_INIT(newh, newvol->device, V_parentId(newvol), newino);
422                     code = namei_replace_file_by_hardlink(newh, h);
423                     VNDISK_SET_INO(vnode, newino);
424 #ifdef AFS_RXOSD_SUPPORT
425                 } else {
426                     code = osd_split_objects(vol, newvol, vnode, e->vN);
427 #endif /*  AFS_RXOSD_SUPPORT */
428                 }
429                 if (code)
430                     goto Bad_Copy;
431                 if (e->flag & CHANGEPARENT)
432                     vnode->parent = 1; /* in new root-directory */
433                 vnode->cloned = 0;
434                 if (FDH_PWRITE(newfdP, vnode, vcp->diskSize, offset) != vcp->diskSize) {
435                     Log("Couldn't write in %s Index of volume %u to offset %"
436                         AFS_UINT64_FMT "\n", class ? "small":"large",
437                         V_id(newvol), 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_PREAD(newfdP, vnode2, vcp->diskSize, newoffset) != vcp->diskSize) {
455             Log("splitvolume: couldn't read in large Index of new volume %u at offset %u\n",
456                     V_id(newvol), vcp->diskSize);
457             code = EIO;
458             goto Bad_Copy;
459         }
460         offset = (where + 1 - class) << (vcp->logSize -1);
461         if (FDH_PREAD(fdP, vnode, vcp->diskSize, offset) != vcp->diskSize) {
462             Log("Couldn't read in large Index of old volume %u at offset %"
463                 AFS_UINT64_FMT "\n", V_id(vol), offset);
464             code = EIO;
465             goto Bad_Copy;
466         }
467         VNDISK_GET_LEN(size, vnode);
468         *blocks += (size + 0x3ff) >> 10;
469         ino = VNDISK_GET_INO(vnode);
470         IH_INIT(h, vol->device, V_parentId(vol), ino);
471         newino = VNDISK_GET_INO(vnode2);
472         IH_INIT(newh, newvol->device, V_parentId(newvol), newino);
473         code = copyDir(m, h, newh);
474         if (code) {
475             Log("splitvolume: copyDir failed for new root from "
476                 "%u.%u.%u to %u.1.1\n",
477                 V_id(vol), where, vnode->uniquifier, V_id(newvol));
478             code = EIO;
479             goto Bad_Copy;
480         }
481         VNDISK_SET_INO(vnode, newino);
482         vnode->uniquifier = 1;
483         vnode->cloned = 0;
484         vnode->parent = vnode2->parent;
485         vnode->serverModifyTime = vnode2->serverModifyTime;
486         if (FDH_PWRITE(newfdP, vnode, vcp->diskSize, newoffset) != vcp->diskSize) {
487             Log("splitvolume: couldn't write in large Index of %u at offset %u\n",
488                     V_id(newvol), vcp->diskSize);
489             code = EIO;
490         }
491     }
492 Bad_Copy:
493     if (fdP)
494         FDH_CLOSE(fdP);
495     if (newfdP)
496         FDH_CLOSE(newfdP);
497     return code;
498 }
499
500 static afs_int32
501 findName(Volume *vol, struct VnodeDiskObject *vd, afs_uint32 vN,
502          afs_uint32 un, char *name,afs_int32 length)
503 {
504     afs_int32 code;
505     Inode ino;
506     DirHandle dir;
507
508     ino = VNDISK_GET_INO(vd);
509     SetSalvageDirHandle(&dir, V_id(vol), V_device(vol), ino);
510
511     code = InverseLookup(&dir, vN, un, name, length);
512     FidZap(&dir);
513     return code;
514 }
515
516 static afs_int32
517 createMountpoint(Volume *vol, Volume *newvol, struct VnodeDiskObject *parent,
518                 afs_uint32 vN,  struct VnodeDiskObject *vd, char *name)
519 {
520     afs_int32 code;
521     Inode ino, newino;
522     DirHandle dir;
523     IHandle_t *h;
524     struct VnodeDiskObject vnode;
525     FdHandle_t *fdP, *fdP2;
526     afs_uint64 size;
527     afs_foff_t offset;
528     afs_int32 class = vSmall;
529     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
530 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
531     Inode nearInode = 0;
532 #endif
533     AFSFid fid;
534     struct timeval now;
535     afs_uint32 newvN;
536     char symlink[32];
537     ssize_t rc;
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     while (1) {
547         rc = FDH_PREAD(fdP, &vnode, vcp->diskSize, offset);
548         if (rc != vcp->diskSize) {
549             if (rc < 0) {
550                 Log("split volume: error reading small vnode index of %u\n", V_id(vol));
551                 return EIO;
552              }
553              if (rc == 0)
554                  break;
555              if (rc < vcp->diskSize)
556                  break;
557         }
558         if (vnode.type == vNull)
559             break;
560         offset += vcp->diskSize;
561     }
562     memset(&vnode, 0, sizeof(vnode));
563     vnode.type = vSymlink;
564     V_nextVnodeUnique(vol)++;
565     vnode.uniquifier = V_nextVnodeUnique(vol);
566     vnode.author = vd->author;
567     vnode.owner = vd->owner;
568     vnode.group = vd->group;
569     vnode.modeBits = 0644;
570     vnode.unixModifyTime = now.tv_sec;
571     vnode.serverModifyTime = now.tv_sec;
572     vnode.dataVersion = 1;
573     vnode.linkCount = 1;
574     vnode.parent = vN;
575
576     newvN = (offset >> (VnodeClassInfo[class].logSize - 1)) - 1 + class;
577 #if defined(NEARINODE_HINT) && !defined(AFS_NAMEI_ENV)
578     V_pref(vol,nearInode)
579 #endif
580     newino = IH_CREATE(V_linkHandle(vol), V_device(vol),
581                 VPartitionPath(V_partition(vol)), nearInode,
582                 V_parentId(vol), newvN, vnode.uniquifier, 1);
583
584     IH_INIT(h, V_device(vol), V_parentId(vol), newino);
585     fdP2 = IH_OPEN(h);
586     if (!fdP2) {
587         Log("split volume: couldn't open inode for mountpoint %u.%u.%u\n",
588                 V_id(vol), newvN, vnode.uniquifier);
589         return EIO;
590     }
591     sprintf(symlink, "#%s", V_name(newvol));
592     size = strlen(symlink) + 1;
593     if (FDH_PWRITE(fdP2, symlink, size, 0) != size) {
594         Log("split volume: couldn't write mountpoint %u.%u.%u\n",
595                 V_id(vol), newvN, vnode.uniquifier);
596         return EIO;
597     }
598     FDH_REALLYCLOSE(fdP2);
599     IH_RELEASE(h);
600     VNDISK_SET_INO(&vnode, newino);
601     VNDISK_SET_LEN(&vnode, size);
602 #ifndef AFS_RXOSD_SUPPORT
603     vnode.vnodeMagic = SMALLVNODEMAGIC;
604 #endif
605     if (FDH_PWRITE(fdP, &vnode, vcp->diskSize, offset) != vcp->diskSize) {
606         Log("split volume: couldn't write vnode for mountpoint %u.%u.%u\n",
607                 V_id(vol), newvN, vnode.uniquifier);
608         return EIO;
609     }
610     FDH_REALLYCLOSE(fdP);
611
612     fid.Volume = V_id(vol);
613     fid.Vnode = newvN;
614     fid.Unique = vnode.uniquifier;
615
616     /*
617      * Now  update the parent directory.
618      */
619
620     ino = VNDISK_GET_INO(parent);
621     SetSalvageDirHandle(&dir, V_id(vol), V_device(vol), ino);
622
623     code = Delete(&dir, name);
624     if (code) {
625         Log("splitvolume: couldn't delete directory entry for %s in %u.%u.%u, code = %d\n",
626                         name, V_id(vol), vN, parent->uniquifier, code);
627         return code;
628     }
629     code = Create(&dir, name, &fid);
630     FidZap(&dir);
631
632     class = vLarge;
633     vcp = &VnodeClassInfo[class];
634     fdP = IH_OPEN(vol->vnodeIndex[class].handle);
635     offset = (vN + 1 - class) << (vcp->logSize -1);
636     parent->dataVersion++;
637     if (FDH_PWRITE(fdP, parent, vcp->diskSize, offset) != vcp->diskSize) {
638         Log("split volume: couldn't write vnode for parent directory %u.%u.%u\n",
639                 V_id(vol), vN, parent->uniquifier);
640         return EIO;
641     }
642     FDH_REALLYCLOSE(fdP);
643     return code;
644 }
645
646 static afs_int32
647 deleteVnodes(Volume *vol, afs_int32 class,
648              struct VnodeExtract *list, afs_int32 length,
649              afs_uint64 *blocks)
650 {
651     afs_int32 i, code = 0;
652     char buf[SIZEOF_LARGEDISKVNODE];
653     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)&buf;
654     FdHandle_t *fdP = 0;
655     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
656     struct VnodeExtract *e;
657     afs_sfsize_t size;
658     afs_uint64 offset;
659     Inode ino;
660
661     fdP = IH_OPEN(vol->vnodeIndex[class].handle);
662     if (!fdP) {
663         Log("Couldn't open %s Index of volume %u\n",
664                 class ? "small":"large", V_id(vol));
665         code = EIO;
666         goto Bad_Delete;
667     }
668     size = FDH_SIZE(fdP);
669
670     for (i=0; i<length; i++) {
671         e = &list[i];
672         if (e->flag & NEEDED) {
673             afs_uint64 size;
674             offset = (e->vN + 1 - class) << (vcp->logSize -1);
675             if (FDH_PREAD(fdP, vnode, vcp->diskSize, offset) != vcp->diskSize) {
676                 Log("Couldn't read in %s Index of volume %u at offset %"
677                     AFS_UINT64_FMT "\n", class ? "small":"large", V_id(vol),
678                     offset);
679                 code = EIO;
680                 goto Bad_Delete;
681             }
682             VNDISK_GET_LEN(size, vnode);
683             *blocks += (size + 0x3ff) >> 10;
684             ino = VNDISK_GET_INO(vnode);
685             if (ino) {
686                 IHandle_t *h;
687                 IH_INIT(h, vol->device, V_parentId(vol), ino);
688                     IH_DEC(h, ino, V_parentId(vol));
689 #ifdef AFS_RXOSD_SUPPORT
690             } else {
691                 code = osdRemove(vol, vnode, e->vN);
692 #endif /*  AFS_RXOSD_SUPPORT */
693             }
694             memset(vnode, 0, vcp->diskSize);
695             vnode->type = vNull;
696             if (FDH_PWRITE(fdP, vnode, vcp->diskSize, offset) != vcp->diskSize) {
697                    Log("Couldn't write in %s Index of volume %u to offset %"
698                        AFS_UINT64_FMT "\n", class ? "small":"large",
699                        V_id(vol), offset);
700             }
701         }
702     }
703 Bad_Delete:
704     if (fdP)
705         FDH_CLOSE(fdP);
706     return code;
707 }
708
709 afs_int32
710 split_volume(struct rx_call *call, Volume *vol, Volume *newvol,
711              afs_uint32 where, afs_int32 verbose)
712 {
713     Error code = 0;
714     struct VnodeExtract *dirList = 0;
715     struct VnodeExtract *fileList = 0;
716     afs_uint64 blocks = 0;
717     afs_uint32 filesNeeded, dirsNeeded;
718     afs_uint32 dl, fl;
719     char buf[SIZEOF_LARGEDISKVNODE];
720     char buf2[SIZEOF_LARGEDISKVNODE];
721     struct VnodeDiskObject *rootVnode = (struct VnodeDiskObject *)&buf;
722     struct VnodeDiskObject *parVnode = (struct VnodeDiskObject *)&buf2;
723     char name[256];
724     afs_uint32 parent;
725     struct Msg *m;
726
727     m = (struct Msg *) malloc(sizeof(struct Msg));
728     memset(m, 0, sizeof(struct Msg));
729     m->call = call;
730     m->verbose = verbose;
731
732     /*
733      *  First step: planning
734      *
735      *  Find out which directories will belong to the new volume
736      *
737      */
738     if (verbose) {
739         sprintf(m->line,
740                 "1st step: extract vnode essence from large vnode file\n");
741         rx_Write(m->call, m->line, strlen(m->line));
742     }
743
744     code = ExtractVnodes(m, vol, vLarge, &dirList, &dl, where, rootVnode,
745                         &parent, parVnode);
746     if (code) {
747         sprintf(m->line,
748                 "ExtractVnodes failed for %u for directories with code %d\n",
749                 V_id(vol), code);
750         rx_Write(m->call, m->line, strlen(m->line));
751         return code;
752     }
753
754     if (verbose) {
755         sprintf(m->line, "2nd step: look for name of vnode %u in directory %u.%u.%u\n",
756                 where, V_id(vol), parent, parVnode->uniquifier);
757         rx_Write(m->call, m->line, strlen(m->line));
758     }
759     code = findName(vol, parVnode, where, rootVnode->uniquifier,
760                     name,  sizeof(name));
761     if (code) {
762         sprintf(m->line,
763                 "splitvolume: could'nt find name of %u in directory %u.%u.%u.\n",
764                 where, V_id(vol), parent, parVnode->uniquifier);
765         rx_Write(m->call, m->line, strlen(m->line));
766         return code;
767     }
768     if (verbose) {
769         sprintf(m->line, "name of %u is %s\n", where, name);
770         rx_Write(m->call, m->line, strlen(m->line));
771     }
772
773     if (verbose) {
774         sprintf(m->line, "3rd step: find all directory vnodes belonging to the subtree under %u \"%s\"\n",
775                         where, name);
776         rx_Write(m->call, m->line, strlen(m->line));
777     }
778     code = FindVnodes(m, where, dirList, dl, dirList, dl, &dirsNeeded, 1);
779     if (code) {
780         sprintf(m->line,
781                 "FindVnodes for directories failed with code %d\n", code);
782         rx_Write(m->call, m->line, strlen(m->line));
783         return code;
784     }
785
786     if (verbose) {
787         sprintf(m->line, "4th step extract vnode essence from small vnode file\n");
788         rx_Write(m->call, m->line, strlen(m->line));
789     }
790     code = ExtractVnodes(m, vol, vSmall, &fileList, &fl, where, 0, 0, 0);
791     if (code) {
792         sprintf(m->line,
793                 "ExtractVnodes failed for %u for files with code %d\n",
794                 V_id(vol), code);
795         rx_Write(m->call, m->line, strlen(m->line));
796         return code;
797     }
798     if (verbose) {
799         sprintf(m->line, "5th step: find all small vnodes belonging to the subtree under %u \"%s\"\n",
800                         where, name);
801         rx_Write(m->call, m->line, strlen(m->line));
802     }
803     FindVnodes(m, where, fileList, fl, dirList, dl, &filesNeeded, 0);
804
805     /*
806      *  Third step: create hard links for all files needed
807      *
808      */
809
810     V_destroyMe(newvol) = DESTROY_ME;
811     V_inService(newvol) = 0;
812     if (verbose) {
813         sprintf(m->line, "6th step: create hard links in the AFSIDat tree between files of the old and new volume\n");
814         rx_Write(m->call, m->line, strlen(m->line));
815     }
816     code = copyVnodes(m, vol, newvol, 1, fileList, fl, where, &blocks, 0);
817     if (code) {
818         sprintf(m->line, "copyVnodes for files failed with code %d\n", code);
819         rx_Write(m->call, m->line, strlen(m->line));
820         return code;
821     }
822
823     /*
824      *  Forth step: create hard links for all directories and copy
825      *  split directory to new root directory
826      */
827
828     if (verbose) {
829         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",
830                 where);
831         rx_Write(m->call, m->line, strlen(m->line));
832     }
833     code = copyVnodes(m, vol, newvol, 0, dirList, dl, where, &blocks, parVnode);
834     if (code) {
835         sprintf(m->line, "copyVnodes for directories failed with code %d\n", code);
836         rx_Write(m->call, m->line, strlen(m->line));
837         return code;
838     }
839
840     /*
841      *  Finalize new volume
842      *
843      */
844     if (verbose) {
845         sprintf(m->line, "8th step: write new volume's metadata to disk\n");
846         rx_Write(m->call, m->line, strlen(m->line));
847     }
848
849     V_diskused(newvol) = blocks;
850 #ifdef AFS_RXOSD_SUPPORT
851     V_osdFlag(newvol) = V_osdFlag(vol);
852 #endif
853     V_filecount(newvol) = filesNeeded + dirsNeeded;
854     V_destroyMe(newvol) = 0;
855     V_maxquota(newvol) = V_maxquota(vol);
856     V_uniquifier(newvol) = V_uniquifier(vol);
857     V_inService(newvol) = 1;
858     VUpdateVolume(&code, newvol);
859
860     /*
861      *  Sixth step: change directory entry in old volume:
862      *  rename old tree and create mount point for new volume.
863      */
864     if (verbose) {
865         sprintf(m->line, "9th step: create mountpoint \"%s\" for new volume in old volume's directory %u.\n", name, parent);
866         rx_Write(m->call, m->line, strlen(m->line));
867     }
868
869     code = createMountpoint(vol, newvol, parVnode, parent, rootVnode, name);
870     if (code) {
871         sprintf(m->line, "createMountpoint failed with code %d\n", code);
872         rx_Write(m->call, m->line, strlen(m->line));
873         return code;
874     }
875     /*
876      * Now both volumes should be ready and consistent, but the old volume
877      * contains still the vnodes and data we transferred into the new one.
878      * Delete orphaned vnodes and data.
879      */
880
881     blocks = 0;
882     if (verbose) {
883         sprintf(m->line, "10th step: delete large vnodes belonging to subtree in the old volume.\n");
884         rx_Write(m->call, m->line, strlen(m->line));
885     }
886     deleteVnodes(vol, vLarge, dirList, dl, &blocks);
887     if (verbose) {
888         sprintf(m->line, "11th step: delete small vnodes belonging to subtree in the old volume.\n");
889         rx_Write(m->call, m->line, strlen(m->line));
890     }
891     deleteVnodes(vol, vSmall, fileList, fl, &blocks);
892     V_diskused(vol) -= blocks;
893     V_filecount(vol) -= (filesNeeded + dirsNeeded + 1);
894     VUpdateVolume(&code, vol);
895
896     sprintf(m->line, "Finished!\n");
897     rx_Write(m->call, m->line, strlen(m->line));
898     m->line[0] = 0;
899     m->line[1] = 0;
900     m->line[2] = 0;
901     m->line[3] = 0;
902     rx_Write(m->call, m->line, 4);
903     free(m);
904     return code;
905 }
906 #endif