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
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <sys/types.h>
23 #include <sys/param.h>
26 #include <netinet/in.h>
37 #include <afs/assert.h>
40 #include <afs/afsint.h>
42 #include <afs/errors.h>
45 #include <afs/ihandle.h>
46 #include <afs/vnode.h>
47 #include <afs/volume.h>
48 #include <afs/partition.h>
50 #include <afs/fssync.h>
57 #define afs_stat stat64
58 #define afs_fstat fstat64
59 #else /* !O_LARGEFILE */
61 #define afs_fstat fstat
62 #endif /* !O_LARGEFILE */
63 #endif /* !AFS_NT40_ENV */
65 /*@printflike@*/ extern void Log(const char *format, ...);
69 /* This iod stuff is a silly little package to emulate the old qi_in stuff, which
70 emulated the stdio stuff. There is a big assumption here, that the
71 rx_Read will never be called directly, by a routine like readFile, when
72 there is an old character that was pushed back with iod_ungetc. This
73 is really bletchy, and is here for compatibility only. Eventually,
74 we should define a volume format that doesn't require
75 the pushing back of characters (i.e. characters should not double both
76 as an end marker and a begin marker) */
78 struct rx_call *call; /* call to which to write, might be an array */
79 int device; /* dump device ID for volume */
80 int parentId; /* dump parent ID for volume */
81 struct DiskPartition *dumpPartition; /* Dump partition. */
82 struct rx_call **calls; /* array of pointers to calls */
83 int ncalls; /* how many calls/codes in array */
84 int *codes; /* one return code for each call */
85 char haveOldChar; /* state for pushing back a character */
90 /* Forward Declarations */
91 static int DumpDumpHeader(register struct iod *iodp, register Volume * vp,
93 static int DumpPartial(register struct iod *iodp, register Volume * vp,
94 afs_int32 fromtime, int dumpAllDirs);
95 static int DumpVnodeIndex(register struct iod *iodp, Volume * vp,
96 VnodeClass class, afs_int32 fromtime,
98 static int DumpVnode(register struct iod *iodp, struct VnodeDiskObject *v,
99 int volid, int vnodeNumber, int dumpEverything);
100 static int ReadDumpHeader(register struct iod *iodp, struct DumpHeader *hp);
101 static int ReadVnodes(register struct iod *iodp, Volume * vp, int incremental,
102 afs_int32 * Lbuf, afs_int32 s1, afs_int32 * Sbuf,
103 afs_int32 s2, afs_int32 delo);
104 static afs_fsize_t volser_WriteFile(int vn, struct iod *iodp,
105 FdHandle_t * handleP, int tag,
108 static int SizeDumpDumpHeader(register struct iod *iodp, register Volume * vp,
110 register struct volintSize *size);
111 static int SizeDumpPartial(register struct iod *iodp, register Volume * vp,
112 afs_int32 fromtime, int dumpAllDirs,
113 register struct volintSize *size);
114 static int SizeDumpVnodeIndex(register struct iod *iodp, Volume * vp,
115 VnodeClass class, afs_int32 fromtime,
117 register struct volintSize *size);
118 static int SizeDumpVnode(register struct iod *iodp, struct VnodeDiskObject *v,
119 int volid, int vnodeNumber, int dumpEverything,
120 register struct volintSize *size);
123 iod_Init(register struct iod *iodp, register struct rx_call *call)
126 iodp->haveOldChar = 0;
128 iodp->calls = (struct rx_call **)0;
132 iod_InitMulti(struct iod *iodp, struct rx_call **calls, int ncalls,
137 iodp->haveOldChar = 0;
138 iodp->ncalls = ncalls;
140 iodp->call = (struct rx_call *)0;
143 /* N.B. iod_Read doesn't check for oldchar (see previous comment) */
144 #define iod_Read(iodp, buf, nbytes) rx_Read((iodp)->call, buf, nbytes)
146 /* For the single dump case, it's ok to just return the "bytes written"
147 * that rx_Write returns, since all the callers of iod_Write abort when
148 * the returned value is less than they expect. For the multi dump case,
149 * I don't think we want half the replicas to go bad just because one
150 * connection timed out, but if they all time out, then we should give up.
153 iod_Write(struct iod *iodp, char *buf, int nbytes)
158 assert((iodp->call && iodp->ncalls == 1 && !iodp->calls)
159 || (!iodp->call && iodp->ncalls >= 1 && iodp->calls));
162 code = rx_Write(iodp->call, buf, nbytes);
166 for (i = 0; i < iodp->ncalls; i++) {
167 if (iodp->calls[i] && !iodp->codes[i]) {
168 code = rx_Write(iodp->calls[i], buf, nbytes);
169 if (code != nbytes) { /* everything gets merged into a single error */
170 iodp->codes[i] = VOLSERDUMPERROR; /* but that's exactly what the */
171 } /* standard dump does, anyways */
176 } /* for all calls */
185 iod_ungetc(struct iod *iodp, int achar)
187 iodp->oldChar = achar;
188 iodp->haveOldChar = 1;
192 iod_getc(register struct iod *iodp)
196 if (iodp->haveOldChar) {
197 iodp->haveOldChar = 0;
198 return iodp->oldChar;
200 if (iod_Read(iodp, &t, 1) == 1)
206 ReadShort(register struct iod *iodp, register unsigned short *sp)
211 *sp = (b1 << 8) | b0;
216 ReadInt32(register struct iod *iodp, afs_uint32 * lp)
218 afs_uint32 register b3, b2, b1, b0;
223 *lp = (((((b3 << 8) | b2) << 8) | b1) << 8) | b0;
228 ReadString(register struct iod *iodp, register char *to, register int maxa)
232 if ((*to++ = iod_getc(iodp)) == 0)
236 while ((c = iod_getc(iodp)) && c != EOF);
242 ReadByteString(register struct iod *iodp, register byte * to,
246 *to++ = iod_getc(iodp);
250 ReadVolumeHeader(register struct iod *iodp, VolumeDiskData * vol)
254 memset(vol, 0, sizeof(*vol));
255 while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
258 if (!ReadInt32(iodp, &vol->id))
259 return VOLSERREAD_DUMPERROR;
262 if (!ReadInt32(iodp, &trash))
263 return VOLSERREAD_DUMPERROR;
266 ReadString(iodp, vol->name, sizeof(vol->name));
267 /*this means the name of the retsored volume could be possibly different. In conjunction with SAFSVolSignalRestore */
270 vol->inService = iod_getc(iodp);
273 vol->blessed = iod_getc(iodp);
276 if (!ReadInt32(iodp, &vol->uniquifier))
277 return VOLSERREAD_DUMPERROR;
280 vol->type = iod_getc(iodp);
283 if (!ReadInt32(iodp, &vol->parentId))
284 return VOLSERREAD_DUMPERROR;
287 if (!ReadInt32(iodp, &vol->cloneId))
288 return VOLSERREAD_DUMPERROR;
291 if (!ReadInt32(iodp, (afs_uint32 *) & vol->maxquota))
292 return VOLSERREAD_DUMPERROR;
295 if (!ReadInt32(iodp, (afs_uint32 *) & vol->minquota))
296 return VOLSERREAD_DUMPERROR;
299 if (!ReadInt32(iodp, (afs_uint32 *) & vol->diskused))
300 return VOLSERREAD_DUMPERROR; /* Bogus: should calculate this */
303 if (!ReadInt32(iodp, (afs_uint32 *) & vol->filecount))
304 return VOLSERREAD_DUMPERROR;
307 if (!ReadInt32(iodp, &vol->accountNumber))
308 return VOLSERREAD_DUMPERROR;
311 if (!ReadInt32(iodp, &vol->owner))
312 return VOLSERREAD_DUMPERROR;
315 if (!ReadInt32(iodp, &vol->creationDate))
316 return VOLSERREAD_DUMPERROR;
319 if (!ReadInt32(iodp, &vol->accessDate))
320 return VOLSERREAD_DUMPERROR;
323 if (!ReadInt32(iodp, &vol->updateDate))
324 return VOLSERREAD_DUMPERROR;
327 if (!ReadInt32(iodp, &vol->expirationDate))
328 return VOLSERREAD_DUMPERROR;
331 if (!ReadInt32(iodp, &vol->backupDate))
332 return VOLSERREAD_DUMPERROR;
335 ReadString(iodp, vol->offlineMessage,
336 sizeof(vol->offlineMessage));
340 * Detailed volume statistics are never stored in dumps,
341 * so we just restore either the null string if this volume
342 * had already been set to store statistics, or the old motd
343 * contents otherwise. It doesn't matter, since this field
344 * will soon get initialized anyway.
346 ReadString(iodp, (char *)(vol->stat_reads), VMSGSIZE);
349 unsigned short length;
352 if (!ReadShort(iodp, &length))
353 return VOLSERREAD_DUMPERROR;
354 for (i = 0; i < length; i++) {
355 if (!ReadInt32(iodp, &data))
356 return VOLSERREAD_DUMPERROR;
357 if (i < sizeof(vol->weekUse) / sizeof(vol->weekUse[0]))
358 vol->weekUse[i] = data;
363 if (!ReadInt32(iodp, &vol->dayUseDate))
364 return VOLSERREAD_DUMPERROR;
367 if (!ReadInt32(iodp, (afs_uint32 *) & vol->dayUse))
368 return VOLSERREAD_DUMPERROR;
372 iod_ungetc(iodp, tag);
377 DumpTag(register struct iod *iodp, register int tag)
382 return ((iod_Write(iodp, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
387 DumpByte(register struct iod *iodp, char tag, byte value)
390 register byte *p = (unsigned char *)tbuffer;
393 return ((iod_Write(iodp, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
396 #define putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
397 #define putshort(p, v) *p++ = v>>8, *p++ = v
400 DumpDouble(register struct iod *iodp, char tag, register afs_uint32 value1,
401 register afs_uint32 value2)
404 register byte *p = (unsigned char *)tbuffer;
408 return ((iod_Write(iodp, tbuffer, 9) == 9) ? 0 : VOLSERDUMPERROR);
412 DumpInt32(register struct iod *iodp, char tag, register afs_uint32 value)
415 register byte *p = (unsigned char *)tbuffer;
418 return ((iod_Write(iodp, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
422 DumpArrayInt32(register struct iod *iodp, char tag,
423 register afs_uint32 * array, register int nelem)
426 register afs_uint32 v;
428 register byte *p = (unsigned char *)tbuffer;
431 code = iod_Write(iodp, tbuffer, 3);
433 return VOLSERDUMPERROR;
435 p = (unsigned char *)tbuffer;
436 v = *array++; /*this was register */
439 code = iod_Write(iodp, tbuffer, 4);
441 return VOLSERDUMPERROR;
447 DumpShort(register struct iod *iodp, char tag, unsigned int value)
450 register byte *p = (unsigned char *)tbuffer;
454 return ((iod_Write(iodp, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
458 DumpBool(register struct iod *iodp, char tag, unsigned int value)
461 register byte *p = (unsigned char *)tbuffer;
464 return ((iod_Write(iodp, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
468 DumpString(register struct iod *iodp, char tag, register char *s)
472 code = iod_Write(iodp, &tag, 1);
474 return VOLSERDUMPERROR;
476 code = iod_Write(iodp, s, n);
478 return VOLSERDUMPERROR;
483 DumpByteString(register struct iod *iodp, char tag, register byte * bs,
488 code = iod_Write(iodp, &tag, 1);
490 return VOLSERDUMPERROR;
491 code = iod_Write(iodp, (char *)bs, nbytes);
493 return VOLSERDUMPERROR;
498 DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
500 int code = 0, lcode = 0, error = 0;
501 afs_int32 pad = 0, offset;
502 afs_sfsize_t n, nbytes, howMany, howBig;
505 struct afs_stat status;
509 #include <sys/statfs.h>
510 struct statfs tstatfs;
514 howBig = _filelength(handleP->fd_fd);
518 afs_fstat(handleP->fd_fd, &status);
519 howBig = status.st_size;
522 /* Unfortunately in AIX valuable fields such as st_blksize are
523 * gone from the stat structure.
525 fstatfs(handleP->fd_fd, &tstatfs);
526 howMany = tstatfs.f_bsize;
528 howMany = status.st_blksize;
529 #endif /* AFS_AIX_ENV */
530 #endif /* AFS_NT40_ENV */
533 size = FDH_SIZE(handleP);
534 #ifdef AFS_LARGEFILE_ENV
537 SplitInt64(size, hi, lo);
539 code = DumpInt32(iodp, 'f', lo);
541 code = DumpDouble(iodp, 'h', hi, lo);
544 #else /* !AFS_LARGEFILE_ENV */
545 code = DumpInt32(iodp, 'f', size);
546 #endif /* !AFS_LARGEFILE_ENV */
548 return VOLSERDUMPERROR;
551 p = (unsigned char *)malloc(howMany);
553 Log("1 Volser: DumpFile: no memory");
554 return VOLSERDUMPERROR;
557 for (nbytes = size; (nbytes && !error); nbytes -= howMany) {
558 if (nbytes < howMany)
561 /* Read the data - unless we know we can't */
562 n = (lcode ? 0 : FDH_READ(handleP, p, howMany));
564 /* If read any good data and we null padded previously, log the
565 * amount that we had null padded.
567 if ((n > 0) && pad) {
568 Log("1 Volser: DumpFile: Null padding file %d bytes at offset %u\n", pad, offset);
572 /* If didn't read enough data, null padd the rest of the buffer. This
573 * can happen if, for instance, the media has some bad spots. We don't
574 * want to quit the dump, so we start null padding.
577 /* Record the read error */
580 Log("1 Volser: DumpFile: Error %d reading inode %s for vnode %d\n", errno, PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
582 Log("1 Volser: DumpFile: Error reading inode %s for vnode %d\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
585 /* Pad the rest of the buffer with zeros. Remember offset we started
586 * padding. Keep total tally of padding.
588 memset(p + n, 0, howMany - n);
590 offset = (howBig - nbytes) + n;
591 pad += (howMany - n);
593 /* Now seek over the data we could not get. An error here means we
594 * can't do the next read.
596 lcode = FDH_SEEK(handleP, ((size - nbytes) + howMany), SEEK_SET);
597 if (lcode != ((size - nbytes) + howMany)) {
599 Log("1 Volser: DumpFile: Error %d seeking in inode %s for vnode %d\n", errno, PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
601 Log("1 Volser: DumpFile: Error seeking in inode %s for vnode %d\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
609 /* Now write the data out */
610 if (iod_Write(iodp, (char *)p, howMany) != howMany)
611 error = VOLSERDUMPERROR;
615 if (pad) { /* Any padding we hadn't reported yet */
616 Log("1 Volser: DumpFile: Null padding file: %d bytes at offset %u\n",
625 DumpVolumeHeader(register struct iod *iodp, register Volume * vp)
628 static char nullString[1] = ""; /*The ``contents'' of motd */
631 code = DumpTag(iodp, D_VOLUMEHEADER);
633 code = DumpInt32(iodp, 'i', V_id(vp));
636 code = DumpInt32(iodp, 'v', V_stamp(vp).version);
638 code = DumpString(iodp, 'n', V_name(vp));
640 code = DumpBool(iodp, 's', V_inService(vp));
642 code = DumpBool(iodp, 'b', V_blessed(vp));
644 code = DumpInt32(iodp, 'u', V_uniquifier(vp));
646 code = DumpByte(iodp, 't', (byte) V_type(vp));
648 code = DumpInt32(iodp, 'p', V_parentId(vp));
651 code = DumpInt32(iodp, 'c', V_cloneId(vp));
653 code = DumpInt32(iodp, 'q', V_maxquota(vp));
655 code = DumpInt32(iodp, 'm', V_minquota(vp));
657 code = DumpInt32(iodp, 'd', V_diskused(vp));
659 code = DumpInt32(iodp, 'f', V_filecount(vp));
661 code = DumpInt32(iodp, 'a', V_accountNumber(vp));
663 code = DumpInt32(iodp, 'o', V_owner(vp));
665 code = DumpInt32(iodp, 'C', V_creationDate(vp)); /* Rw volume creation date */
667 code = DumpInt32(iodp, 'A', V_accessDate(vp));
669 code = DumpInt32(iodp, 'U', V_updateDate(vp));
671 code = DumpInt32(iodp, 'E', V_expirationDate(vp));
673 code = DumpInt32(iodp, 'B', V_backupDate(vp)); /* Rw volume backup clone date */
675 code = DumpString(iodp, 'O', V_offlineMessage(vp));
677 * We do NOT dump the detailed volume statistics residing in the old
678 * motd field, since we cannot tell from the info in a dump whether
679 * statistics data has been put there. Instead, we dump a null string,
680 * just as if that was what the motd contained.
683 code = DumpString(iodp, 'M', nullString);
686 DumpArrayInt32(iodp, 'W', (afs_uint32 *) V_weekUse(vp),
687 sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
689 code = DumpInt32(iodp, 'D', V_dayUseDate(vp));
691 code = DumpInt32(iodp, 'Z', V_dayUse(vp));
696 DumpEnd(register struct iod *iodp)
698 return (DumpInt32(iodp, D_DUMPEND, DUMPENDMAGIC));
701 /* Guts of the dump code */
703 /* Dump a whole volume */
705 DumpVolume(register struct rx_call *call, register Volume * vp,
706 afs_int32 fromtime, int dumpAllDirs)
710 register struct iod *iodp = &iod;
711 iod_Init(iodp, call);
714 code = DumpDumpHeader(iodp, vp, fromtime);
717 code = DumpPartial(iodp, vp, fromtime, dumpAllDirs);
719 /* hack follows. Errors should be handled quite differently in this version of dump than they used to be.*/
720 if (rx_Error(iodp->call)) {
721 Log("1 Volser: DumpVolume: Rx call failed during dump, error %d\n",
722 rx_Error(iodp->call));
723 return VOLSERDUMPERROR;
726 code = DumpEnd(iodp);
731 /* Dump a volume to multiple places*/
733 DumpVolMulti(struct rx_call **calls, int ncalls, Volume * vp,
734 afs_int32 fromtime, int dumpAllDirs, int *codes)
738 iod_InitMulti(&iod, calls, ncalls, codes);
741 code = DumpDumpHeader(&iod, vp, fromtime);
743 code = DumpPartial(&iod, vp, fromtime, dumpAllDirs);
745 code = DumpEnd(&iod);
749 /* A partial dump (no dump header) */
751 DumpPartial(register struct iod *iodp, register Volume * vp,
752 afs_int32 fromtime, int dumpAllDirs)
756 code = DumpVolumeHeader(iodp, vp);
758 code = DumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs);
760 code = DumpVnodeIndex(iodp, vp, vSmall, fromtime, 0);
765 DumpVnodeIndex(register struct iod *iodp, Volume * vp, VnodeClass class,
766 afs_int32 fromtime, int forcedump)
768 register int code = 0;
769 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
770 char buf[SIZEOF_LARGEDISKVNODE];
771 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
772 StreamHandle_t *file;
776 register int vnodeIndex, nVnodes;
778 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
780 file = FDH_FDOPEN(fdP, "r+");
781 assert(file != NULL);
782 size = OS_SIZE(fdP->fd_fd);
784 nVnodes = (size / vcp->diskSize) - 1;
786 assert((nVnodes + 1) * vcp->diskSize == size);
787 assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
791 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
792 nVnodes--, vnodeIndex++) {
793 flag = forcedump || (vnode->serverModifyTime >= fromtime);
794 /* Note: the >= test is very important since some old volumes may not have
795 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
796 * does dump the file! */
799 DumpVnode(iodp, vnode, V_id(vp),
800 bitNumberToVnodeNumber(vnodeIndex, class), flag);
802 IOMGR_Poll(); /* if we dont' xfr data, but scan instead, could lose conn */
810 DumpDumpHeader(register struct iod *iodp, register Volume * vp,
814 int UseLatestReadOnlyClone = 1;
815 afs_int32 dumpTimes[2];
816 iodp->device = vp->device;
817 iodp->parentId = V_parentId(vp);
818 iodp->dumpPartition = vp->partition;
820 code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
824 UseLatestReadOnlyClone ? V_id(vp) : V_parentId(vp));
826 code = DumpString(iodp, 'n', V_name(vp));
827 dumpTimes[0] = fromtime;
828 dumpTimes[1] = V_backupDate(vp); /* Until the time the clone was made */
830 code = DumpArrayInt32(iodp, 't', (afs_uint32 *) dumpTimes, 2);
835 DumpVnode(register struct iod *iodp, struct VnodeDiskObject *v, int volid,
836 int vnodeNumber, int dumpEverything)
842 if (!v || v->type == vNull)
845 code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier);
849 code = DumpByte(iodp, 't', (byte) v->type);
851 code = DumpShort(iodp, 'l', v->linkCount); /* May not need this */
853 code = DumpInt32(iodp, 'v', v->dataVersion);
855 code = DumpInt32(iodp, 'm', v->unixModifyTime);
857 code = DumpInt32(iodp, 'a', v->author);
859 code = DumpInt32(iodp, 'o', v->owner);
860 if (!code && v->group)
861 code = DumpInt32(iodp, 'g', v->group); /* default group is 0 */
863 code = DumpShort(iodp, 'b', v->modeBits);
865 code = DumpInt32(iodp, 'p', v->parent);
867 code = DumpInt32(iodp, 's', v->serverModifyTime);
868 if (v->type == vDirectory) {
869 acl_HtonACL(VVnodeDiskACL(v));
872 DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v),
875 if (VNDISK_GET_INO(v)) {
876 IH_INIT(ihP, iodp->device, iodp->parentId, VNDISK_GET_INO(v));
879 Log("1 Volser: DumpVnode: dump: Unable to open inode %llu for vnode %u (volume %i); not dumped, error %d\n", (afs_uintmax_t) VNDISK_GET_INO(v), vnodeNumber, volid, errno);
881 return VOLSERREAD_DUMPERROR;
883 code = DumpFile(iodp, vnodeNumber, fdP);
892 ProcessIndex(Volume * vp, VnodeClass class, afs_int32 ** Bufp, int *sizep,
895 int i, nVnodes, offset, code, index = 0;
899 StreamHandle_t *afile;
901 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
902 char buf[SIZEOF_LARGEDISKVNODE], zero[SIZEOF_LARGEDISKVNODE];
903 register struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
905 memset(zero, 0, sizeof(zero)); /* zero out our proto-vnode */
906 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
909 afile = FDH_FDOPEN(fdP, "r+");
913 for (i = 0; i < *sizep; i++) {
916 STREAM_SEEK(afile, Buf[i], 0);
917 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
919 if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
922 afs_fsize_t vnodeLength;
923 Log("RestoreVolume %u Cleanup: Removing old vnode=%u inode=%llu size=%llu\n", V_id(vp), bitNumberToVnodeNumber(i, class), (afs_uintmax_t) VNDISK_GET_INO(vnode), (afs_uintmax_t) vnodeLength);
925 IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(vnode),
929 STREAM_SEEK(afile, Buf[i], 0);
930 (void)STREAM_WRITE(zero, vcp->diskSize, 1, afile); /* Zero it out */
936 Log("RestoreVolume Cleanup: Removed %d inodes for volume %d\n",
939 STREAM_FLUSH(afile); /* ensure 0s are on the disk */
940 OS_SYNC(afile->str_fd);
942 size = OS_SIZE(fdP->fd_fd);
946 vcp->diskSize ? 0 : size - vcp->diskSize) >> vcp->logSize;
948 Buf = (afs_int32 *) malloc(nVnodes * sizeof(afs_int32));
951 memset((char *)Buf, 0, nVnodes * sizeof(afs_int32));
952 STREAM_SEEK(afile, offset = vcp->diskSize, 0);
954 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
958 if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
959 Buf[(offset >> vcp->logSize) - 1] = offset;
962 offset += vcp->diskSize;
975 RestoreVolume(register struct rx_call *call, Volume * avp, int incremental,
976 struct restoreCookie *cookie)
979 struct DumpHeader header;
981 Error error = 0, vupdate;
984 register struct iod *iodp = &iod;
985 afs_int32 *b1 = 0, *b2 = 0;
986 int s1 = 0, s2 = 0, delo = 0, tdelo;
989 iod_Init(iodp, call);
992 if (!ReadDumpHeader(iodp, &header)) {
993 Log("1 Volser: RestoreVolume: Error reading header file for dump; aborted\n");
994 return VOLSERREAD_DUMPERROR;
996 if (iod_getc(iodp) != D_VOLUMEHEADER) {
997 Log("1 Volser: RestoreVolume: Volume header missing from dump; not restored\n");
998 return VOLSERREAD_DUMPERROR;
1000 if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR)
1001 return VOLSERREAD_DUMPERROR;
1003 delo = ProcessIndex(vp, vLarge, &b1, &s1, 0);
1005 delo = ProcessIndex(vp, vSmall, &b2, &s2, 0);
1014 strncpy(vol.name, cookie->name, VOLSER_OLDMAXVOLNAME);
1015 vol.type = cookie->type;
1016 vol.cloneId = cookie->clone;
1017 vol.parentId = cookie->parent;
1022 if (ReadVnodes(iodp, vp, 0, b1, s1, b2, s2, tdelo)) {
1023 error = VOLSERREAD_DUMPERROR;
1026 tag = iod_getc(iodp);
1027 if (tag != D_VOLUMEHEADER)
1029 if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR) {
1030 error = VOLSERREAD_DUMPERROR;
1035 if (tag != D_DUMPEND || !ReadInt32(iodp, &endMagic)
1036 || endMagic != DUMPENDMAGIC) {
1037 Log("1 Volser: RestoreVolume: End of dump not found; restore aborted\n");
1038 error = VOLSERREAD_DUMPERROR;
1043 if (iod_getc(iodp) != EOF) {
1044 Log("1 Volser: RestoreVolume: Unrecognized postamble in dump; restore aborted\n");
1045 error = VOLSERREAD_DUMPERROR;
1050 ProcessIndex(vp, vLarge, &b1, &s1, 1);
1051 ProcessIndex(vp, vSmall, &b2, &s2, 1);
1055 ClearVolumeStats(&vol);
1056 CopyVolumeHeader(&vol, &V_disk(vp));
1057 V_destroyMe(vp) = 0;
1058 VUpdateVolume(&vupdate, vp);
1060 Log("1 Volser: RestoreVolume: Unable to rewrite volume header; restore aborted\n");
1061 error = VOLSERREAD_DUMPERROR;
1065 /* Free the malloced space above */
1074 ReadVnodes(register struct iod *iodp, Volume * vp, int incremental,
1075 afs_int32 * Lbuf, afs_int32 s1, afs_int32 * Sbuf, afs_int32 s2,
1078 afs_int32 vnodeNumber;
1079 char buf[SIZEOF_LARGEDISKVNODE];
1081 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1082 struct VnodeDiskObject oldvnode;
1085 struct VnodeClassInfo *vcp;
1090 tag = iod_getc(iodp);
1091 V_pref(vp, nearInode);
1092 while (tag == D_VNODE) {
1094 memset(buf, 0, sizeof(buf));
1095 if (!ReadInt32(iodp, (afs_uint32 *) & vnodeNumber))
1098 ReadInt32(iodp, &vnode->uniquifier);
1099 while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
1103 vnode->type = (VnodeType) iod_getc(iodp);
1108 ReadShort(iodp, &tlc);
1109 vnode->linkCount = (signed int)tlc;
1113 ReadInt32(iodp, &vnode->dataVersion);
1116 ReadInt32(iodp, &vnode->unixModifyTime);
1119 ReadInt32(iodp, &vnode->serverModifyTime);
1122 ReadInt32(iodp, &vnode->author);
1125 ReadInt32(iodp, &vnode->owner);
1128 ReadInt32(iodp, (afs_uint32 *) & vnode->group);
1131 unsigned short modeBits;
1132 ReadShort(iodp, &modeBits);
1133 vnode->modeBits = (unsigned int)modeBits;
1137 ReadInt32(iodp, &vnode->parent);
1140 ReadByteString(iodp, (byte *) VVnodeDiskACL(vnode),
1141 VAclDiskSize(vnode));
1142 acl_NtohACL(VVnodeDiskACL(vnode));
1144 #ifdef AFS_LARGEFILE_ENV
1150 afs_fsize_t vnodeLength;
1153 IH_CREATE(V_linkHandle(vp), V_device(vp),
1154 VPartitionPath(V_partition(vp)), nearInode,
1155 V_parentId(vp), vnodeNumber,
1156 vnode->uniquifier, vnode->dataVersion);
1157 if (!VALID_INO(ino)) {
1158 perror("unable to allocate inode");
1159 Log("1 Volser: ReadVnodes: Restore aborted\n");
1160 return VOLSERREAD_DUMPERROR;
1163 VNDISK_SET_INO(vnode, ino);
1164 IH_INIT(tmpH, vp->device, V_parentId(vp), ino);
1165 fdP = IH_OPEN(tmpH);
1168 return VOLSERREAD_DUMPERROR;
1171 volser_WriteFile(vnodeNumber, iodp, fdP, tag, &error);
1172 VNDISK_SET_LEN(vnode, vnodeLength);
1173 FDH_REALLYCLOSE(fdP);
1176 Log("1 Volser: ReadVnodes: IDEC inode %llu\n",
1177 (afs_uintmax_t) ino);
1178 IH_DEC(V_linkHandle(vp), ino, V_parentId(vp));
1179 return VOLSERREAD_DUMPERROR;
1186 class = vnodeIdToClass(vnodeNumber);
1187 vcp = &VnodeClassInfo[class];
1189 /* Mark this vnode as in this dump - so we don't delete it later */
1191 idx = (vnodeIndexOffset(vcp, vnodeNumber) >> vcp->logSize) - 1;
1192 if (class == vLarge) {
1193 if (Lbuf && (idx < s1))
1196 if (Sbuf && (idx < s2))
1202 FdHandle_t *fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1204 Log("1 Volser: ReadVnodes: Error opening vnode index; restore aborted\n");
1205 return VOLSERREAD_DUMPERROR;
1207 if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
1209 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1210 FDH_REALLYCLOSE(fdP);
1211 return VOLSERREAD_DUMPERROR;
1213 if (FDH_READ(fdP, &oldvnode, sizeof(oldvnode)) ==
1215 if (oldvnode.type != vNull && VNDISK_GET_INO(&oldvnode)) {
1216 IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(&oldvnode),
1220 vnode->vnodeMagic = vcp->magic;
1221 if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
1223 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1224 FDH_REALLYCLOSE(fdP);
1225 return VOLSERREAD_DUMPERROR;
1227 if (FDH_WRITE(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
1228 Log("1 Volser: ReadVnodes: Error writing vnode index; restore aborted\n");
1229 FDH_REALLYCLOSE(fdP);
1230 return VOLSERREAD_DUMPERROR;
1235 iod_ungetc(iodp, tag);
1242 /* called with disk file only. Note that we don't have to worry about rx_Read
1243 * needing to read an ungetc'd character, since the ReadInt32 will have read
1247 volser_WriteFile(int vn, struct iod *iodp, FdHandle_t * handleP, int tag,
1251 afs_fsize_t filesize;
1252 afs_fsize_t written = 0;
1253 register afs_uint32 size = 8192;
1254 register afs_fsize_t nbytes;
1259 #ifdef AFS_64BIT_ENV
1261 afs_uint32 filesize_high = 0L, filesize_low = 0L;
1262 #ifdef AFS_LARGEFILE_ENV
1264 if (!ReadInt32(iodp, &filesize_high)) {
1269 #endif /* !AFS_LARGEFILE_ENV */
1270 if (!ReadInt32(iodp, &filesize_low)) {
1274 FillInt64(filesize, filesize_high, filesize_low);
1276 #else /* !AFS_64BIT_ENV */
1277 if (!ReadInt32(iodp, &filesize)) {
1281 #endif /* !AFS_64BIT_ENV */
1282 p = (unsigned char *)malloc(size);
1287 for (nbytes = filesize; nbytes; nbytes -= size) {
1291 if ((code = iod_Read(iodp, p, size)) != size) {
1292 Log("1 Volser: WriteFile: Error reading dump file %d size=%llu nbytes=%u (%d of %u); restore aborted\n", vn, (afs_uintmax_t) filesize, nbytes, code, size);
1296 code = FDH_WRITE(handleP, p, size);
1300 Log("1 Volser: WriteFile: Error creating file in volume; restore aborted\n");
1310 ReadDumpHeader(register struct iod *iodp, struct DumpHeader *hp)
1313 afs_uint32 beginMagic;
1314 if (iod_getc(iodp) != D_DUMPHEADER || !ReadInt32(iodp, &beginMagic)
1315 || !ReadInt32(iodp, (afs_uint32 *) & hp->version)
1316 || beginMagic != DUMPBEGINMAGIC)
1320 while ((tag = iod_getc(iodp)) > D_MAX) {
1321 unsigned short arrayLength;
1325 if (!ReadInt32(iodp, &hp->volumeId))
1329 ReadString(iodp, hp->volumeName, sizeof(hp->volumeName));
1332 if (!ReadShort(iodp, &arrayLength))
1334 hp->nDumpTimes = (arrayLength >> 1);
1335 for (i = 0; i < hp->nDumpTimes; i++)
1336 if (!ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].from)
1337 || !ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].to))
1342 if (!hp->volumeId || !hp->nDumpTimes) {
1345 iod_ungetc(iodp, tag);
1350 /* ----- Below are the calls that calculate dump size ----- */
1353 SizeDumpVolumeHeader(register struct iod *iodp, register Volume * vp,
1354 register struct volintSize *v_size)
1357 static char nullString[1] = ""; /*The ``contents'' of motd */
1359 /* if (!code) code = DumpTag(iodp, D_VOLUMEHEADER); */
1360 v_size->dump_size += 1;
1361 /* if (!code) {code = DumpInt32(iodp, 'i',V_id(vp));} */
1362 v_size->dump_size += 5;
1363 /* if (!code) code = DumpInt32(iodp, 'v',V_stamp(vp).version); */
1364 v_size->dump_size += 5;
1365 /* if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1366 v_size->dump_size += (2 + strlen(V_name(vp)));
1367 /* if (!code) code = DumpBool(iodp, 's',V_inService(vp)); */
1368 v_size->dump_size += 2;
1369 /* if (!code) code = DumpBool(iodp, 'b',V_blessed(vp)); */
1370 v_size->dump_size += 2;
1371 /* if (!code) code = DumpInt32(iodp, 'u',V_uniquifier(vp)); */
1372 v_size->dump_size += 5;
1373 /* if (!code) code = DumpByte(iodp, 't',(byte)V_type(vp)); */
1374 v_size->dump_size += 2;
1375 /* if (!code){ code = DumpInt32(iodp, 'p',V_parentId(vp));} */
1376 v_size->dump_size += 5;
1377 /* if (!code) code = DumpInt32(iodp, 'c',V_cloneId(vp)); */
1378 v_size->dump_size += 5;
1379 /* if (!code) code = DumpInt32(iodp, 'q',V_maxquota(vp)); */
1380 v_size->dump_size += 5;
1381 /* if (!code) code = DumpInt32(iodp, 'm',V_minquota(vp)); */
1382 v_size->dump_size += 5;
1383 /* if (!code) code = DumpInt32(iodp, 'd',V_diskused(vp)); */
1384 v_size->dump_size += 5;
1385 /* if (!code) code = DumpInt32(iodp, 'f',V_filecount(vp)); */
1386 v_size->dump_size += 5;
1387 /* if (!code) code = DumpInt32(iodp, 'a', V_accountNumber(vp)); */
1388 v_size->dump_size += 5;
1389 /* if (!code) code = DumpInt32(iodp, 'o', V_owner(vp)); */
1390 v_size->dump_size += 5;
1391 /* if (!code) code = DumpInt32(iodp, 'C',V_creationDate(vp)); /\* Rw volume creation date *\/ */
1392 v_size->dump_size += 5;
1393 /* if (!code) code = DumpInt32(iodp, 'A',V_accessDate(vp)); */
1394 v_size->dump_size += 5;
1395 /* if (!code) code = DumpInt32(iodp, 'U',V_updateDate(vp)); */
1396 v_size->dump_size += 5;
1397 /* if (!code) code = DumpInt32(iodp, 'E',V_expirationDate(vp)); */
1398 v_size->dump_size += 5;
1399 /* if (!code) code = DumpInt32(iodp, 'B',V_backupDate(vp)); /\* Rw volume backup clone date *\/ */
1400 v_size->dump_size += 5;
1401 /* if (!code) code = DumpString(iodp, 'O',V_offlineMessage(vp)); */
1402 v_size->dump_size += (2 + strlen(V_offlineMessage(vp)));
1404 /* * We do NOT dump the detailed volume statistics residing in the old */
1405 /* * motd field, since we cannot tell from the info in a dump whether */
1406 /* * statistics data has been put there. Instead, we dump a null string, */
1407 /* * just as if that was what the motd contained. */
1409 /* if (!code) code = DumpString(iodp, 'M', nullString); */
1410 v_size->dump_size += (2 + strlen(nullString));
1411 /* if (!code) code = DumpArrayInt32(iodp, 'W', (afs_uint32 *)V_weekUse(vp), sizeof(V_weekUse(vp))/sizeof(V_weekUse(vp)[0])); */
1412 v_size->dump_size +=
1413 (3 + 4 * (sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0])));
1414 /* if (!code) code = DumpInt32(iodp, 'D', V_dayUseDate(vp)); */
1415 v_size->dump_size += 5;
1416 /* if (!code) code = DumpInt32(iodp, 'Z', V_dayUse(vp)); */
1417 v_size->dump_size += 5;
1422 SizeDumpEnd(register struct iod *iodp, register struct volintSize *v_size)
1425 v_size->dump_size += 5;
1430 SizeDumpVolume(register struct rx_call *call, register Volume * vp,
1431 afs_int32 fromtime, int dumpAllDirs,
1432 register struct volintSize *v_size)
1436 register struct iod *iodp = (struct iod *)0;
1437 /* iod_Init(iodp, call); */
1440 code = SizeDumpDumpHeader(iodp, vp, fromtime, v_size);
1442 code = SizeDumpPartial(iodp, vp, fromtime, dumpAllDirs, v_size);
1444 code = SizeDumpEnd(iodp, v_size);
1450 SizeDumpDumpHeader(register struct iod *iodp, register Volume * vp,
1451 afs_int32 fromtime, register struct volintSize *v_size)
1454 int UseLatestReadOnlyClone = 1;
1455 afs_int32 dumpTimes[2];
1456 /* iodp->device = vp->device; */
1457 /* iodp->parentId = V_parentId(vp); */
1458 /* iodp->dumpPartition = vp->partition; */
1460 v_size->dump_size = 0; /* initialize the size */
1461 /* if (!code) code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION); */
1462 v_size->dump_size += 9;
1463 /* if (!code) code = DumpInt32(iodp, 'v', UseLatestReadOnlyClone? V_id(vp): V_parentId(vp)); */
1464 v_size->dump_size += 5;
1465 /* if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1466 v_size->dump_size += (2 + strlen(V_name(vp)));
1467 /* dumpTimes[0] = fromtime; */
1468 /* dumpTimes[1] = V_backupDate(vp); /\* Until the time the clone was made *\/ */
1469 /* if (!code) code = DumpArrayInt32(iodp, 't', (afs_uint32 *)dumpTimes, 2); */
1470 v_size->dump_size += (3 + 4 * 2);
1475 SizeDumpVnode(register struct iod *iodp, struct VnodeDiskObject *v, int volid,
1476 int vnodeNumber, int dumpEverything,
1477 register struct volintSize *v_size)
1481 if (!v || v->type == vNull)
1483 /* if (!code) code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier); */
1484 v_size->dump_size += 9;
1485 if (!dumpEverything)
1487 /* if (!code) code = DumpByte(iodp, 't',(byte)v->type); */
1488 v_size->dump_size += 2;
1489 /* if (!code) code = DumpShort(iodp, 'l', v->linkCount); /\* May not need this *\/ */
1490 v_size->dump_size += 3;
1491 /* if (!code) code = DumpInt32(iodp, 'v', v->dataVersion); */
1492 v_size->dump_size += 5;
1493 /* if (!code) code = DumpInt32(iodp, 'm', v->unixModifyTime); */
1494 v_size->dump_size += 5;
1495 /* if (!code) code = DumpInt32(iodp, 'a', v->author); */
1496 v_size->dump_size += 5;
1497 /* if (!code) code = DumpInt32(iodp, 'o', v->owner); */
1498 v_size->dump_size += 5;
1499 /* if (!code && v->group) code = DumpInt32(iodp, 'g', v->group); /\* default group is 0 *\/ */
1501 v_size->dump_size += 5;
1502 /* if (!code) code = DumpShort(iodp, 'b', v->modeBits); */
1503 v_size->dump_size += 3;
1504 /* if (!code) code = DumpInt32(iodp, 'p', v->parent); */
1505 v_size->dump_size += 5;
1506 /* if (!code) code = DumpInt32(iodp, 's', v->serverModifyTime); */
1507 v_size->dump_size += 5;
1508 if (v->type == vDirectory) {
1509 /* acl_HtonACL(VVnodeDiskACL(v)); */
1510 /* if (!code) code = DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v), VAclDiskSize(v)); */
1511 v_size->dump_size += 1 + VAclDiskSize(v);
1514 if (VNDISK_GET_INO(v)) {
1515 v_size->dump_size += (v->length + 5);
1520 /* A partial dump (no dump header) */
1522 SizeDumpPartial(register struct iod *iodp, register Volume * vp,
1523 afs_int32 fromtime, int dumpAllDirs,
1524 register struct volintSize *v_size)
1528 code = SizeDumpVolumeHeader(iodp, vp, v_size);
1531 SizeDumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs,
1534 code = SizeDumpVnodeIndex(iodp, vp, vSmall, fromtime, 0, v_size);
1539 SizeDumpVnodeIndex(register struct iod *iodp, Volume * vp, VnodeClass class,
1540 afs_int32 fromtime, int forcedump,
1541 register struct volintSize *v_size)
1543 register int code = 0;
1544 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1545 char buf[SIZEOF_LARGEDISKVNODE];
1546 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1547 StreamHandle_t *file;
1551 register int vnodeIndex, nVnodes;
1553 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1554 assert(fdP != NULL);
1555 file = FDH_FDOPEN(fdP, "r+");
1556 assert(file != NULL);
1557 size = OS_SIZE(fdP->fd_fd);
1559 nVnodes = (size / vcp->diskSize) - 1;
1561 assert((nVnodes + 1) * vcp->diskSize == size);
1562 assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
1565 for (vnodeIndex = 0;
1566 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
1567 nVnodes--, vnodeIndex++) {
1568 flag = forcedump || (vnode->serverModifyTime >= fromtime);
1569 /* Note: the >= test is very important since some old volumes may not have
1570 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
1571 * does dump the file! */
1574 SizeDumpVnode(iodp, vnode, V_id(vp),
1575 bitNumberToVnodeNumber(vnodeIndex, class), flag,