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>
15 #include <sys/types.h>
22 #include <sys/param.h>
25 #include <netinet/in.h>
36 #include <afs/assert.h>
39 #include <afs/afsint.h>
41 #include <afs/errors.h>
44 #include <afs/ihandle.h>
45 #include <afs/vnode.h>
46 #include <afs/volume.h>
47 #include <afs/partition.h>
49 #include <afs/fssync.h>
56 /* This iod stuff is a silly little package to emulate the old qi_in stuff, which
57 emulated the stdio stuff. There is a big assumption here, that the
58 rx_Read will never be called directly, by a routine like readFile, when
59 there is an old character that was pushed back with iod_ungetc. This
60 is really bletchy, and is here for compatibility only. Eventually,
61 we should define a volume format that doesn't require
62 the pushing back of characters (i.e. characters should not double both
63 as an end marker and a begin marker) */
65 struct rx_call *call; /* call to which to write, might be an array */
66 int device; /* dump device ID for volume */
67 int parentId; /* dump parent ID for volume */
68 struct DiskPartition *dumpPartition; /* Dump partition. */
69 struct rx_call **calls; /* array of pointers to calls */
70 int ncalls; /* how many calls/codes in array */
71 int *codes; /* one return code for each call */
72 char haveOldChar; /* state for pushing back a character */
77 /* Forward Declarations */
78 static int DumpDumpHeader(register struct iod *iodp, register Volume *vp,
80 static int DumpPartial(register struct iod *iodp, register Volume *vp,
81 afs_int32 fromtime, int dumpAllDirs);
82 static int DumpVnodeIndex(register struct iod *iodp, Volume *vp,
83 VnodeClass class, afs_int32 fromtime, int forcedump);
84 static int DumpVnode(register struct iod *iodp, struct VnodeDiskObject *v,
85 int volid, int vnodeNumber, int dumpEverything);
86 static int ReadDumpHeader(register struct iod *iodp, struct DumpHeader *hp);
87 static int ReadVnodes(register struct iod *iodp, Volume *vp,
88 int incremental, afs_int32 *Lbuf, afs_int32 s1,
89 afs_int32 *Sbuf, afs_int32 s2, afs_int32 delo);
90 static afs_offs_t volser_WriteFile(int vn, struct iod *iodp,
92 int tag, Error *status);
95 static void iod_Init(register struct iod *iodp, register struct rx_call *call)
98 iodp->haveOldChar = 0;
100 iodp->calls = (struct rx_call **) 0;
103 static void iod_InitMulti(struct iod *iodp, struct rx_call **calls, int ncalls,
108 iodp->haveOldChar = 0;
109 iodp->ncalls = ncalls;
111 iodp->call = (struct rx_call *) 0;
114 /* N.B. iod_Read doesn't check for oldchar (see previous comment) */
115 #define iod_Read(iodp, buf, nbytes) rx_Read((iodp)->call, buf, nbytes)
117 /* For the single dump case, it's ok to just return the "bytes written"
118 * that rx_Write returns, since all the callers of iod_Write abort when
119 * the returned value is less than they expect. For the multi dump case,
120 * I don't think we want half the replicas to go bad just because one
121 * connection timed out, but if they all time out, then we should give up.
123 static int iod_Write(struct iod *iodp, char *buf, int nbytes)
128 assert ((iodp->call && iodp->ncalls == 1 && !iodp->calls) ||
129 (!iodp->call && iodp->ncalls >= 1 && iodp->calls));
132 code = rx_Write(iodp->call, buf, nbytes);
136 for (i=0; i < iodp->ncalls; i++) {
137 if (iodp->calls[i] && !iodp->codes[i]) {
138 code = rx_Write(iodp->calls[i], buf, nbytes);
139 if (code != nbytes) { /* everything gets merged into a single error */
140 iodp->codes[i] = VOLSERDUMPERROR; /* but that's exactly what the */
141 } /* standard dump does, anyways */
146 } /* for all calls */
153 static void iod_ungetc(struct iod *iodp, int achar)
155 iodp->oldChar = achar;
156 iodp->haveOldChar = 1;
159 static int iod_getc(register struct iod *iodp)
163 if (iodp->haveOldChar) {
164 iodp->haveOldChar = 0;
165 return iodp->oldChar;
167 if (iod_Read(iodp, &t, 1) == 1) return t;
171 static int ReadShort(register struct iod *iodp, register unsigned short *sp)
180 static int ReadInt32(register struct iod *iodp, afs_uint32 *lp)
182 afs_uint32 register b3,b2,b1,b0;
187 *lp = (((((b3<<8)|b2)<<8)|b1)<<8)|b0;
191 static void ReadString(register struct iod *iodp, register char *to,
196 if ((*to++ = iod_getc(iodp)) == 0)
200 while ((c = iod_getc(iodp)) && c != EOF);
205 static void ReadByteString(register struct iod *iodp, register byte *to,
209 *to++ = iod_getc(iodp);
212 static int ReadVolumeHeader(register struct iod *iodp, VolumeDiskData *vol)
216 memset(vol, 0, sizeof(*vol));
217 while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
220 if(!ReadInt32(iodp, &vol->id)) return VOLSERREAD_DUMPERROR;
223 if(!ReadInt32(iodp, &trash)) return VOLSERREAD_DUMPERROR;
226 ReadString(iodp, vol->name, sizeof(vol->name));
227 /*this means the name of the retsored volume could be possibly different. In conjunction with SAFSVolSignalRestore */
230 vol->inService = iod_getc(iodp );
233 vol->blessed = iod_getc(iodp );
236 if(!ReadInt32(iodp, &vol->uniquifier)) return VOLSERREAD_DUMPERROR;
239 vol->type = iod_getc(iodp );
242 if(!ReadInt32(iodp, &vol->parentId)) return VOLSERREAD_DUMPERROR;
245 if(!ReadInt32(iodp, &vol->cloneId)) return VOLSERREAD_DUMPERROR;
248 if(!ReadInt32(iodp, (afs_uint32 *)&vol->maxquota)) return VOLSERREAD_DUMPERROR;
251 if(!ReadInt32(iodp, (afs_uint32 *)&vol->minquota)) return VOLSERREAD_DUMPERROR;
254 if(!ReadInt32(iodp, (afs_uint32 *)&vol->diskused)) return VOLSERREAD_DUMPERROR; /* Bogus: should calculate this */
257 if(!ReadInt32(iodp, (afs_uint32 *)&vol->filecount)) return VOLSERREAD_DUMPERROR;
260 if(!ReadInt32(iodp, &vol->accountNumber)) return VOLSERREAD_DUMPERROR;
263 if(!ReadInt32(iodp, &vol->owner)) return VOLSERREAD_DUMPERROR;
266 if(!ReadInt32(iodp, &vol->creationDate)) return VOLSERREAD_DUMPERROR;
269 if(!ReadInt32(iodp, &vol->accessDate)) return VOLSERREAD_DUMPERROR;
272 if(!ReadInt32(iodp, &vol->updateDate)) return VOLSERREAD_DUMPERROR;
275 if(!ReadInt32(iodp, &vol->expirationDate)) return VOLSERREAD_DUMPERROR;
278 if(!ReadInt32(iodp, &vol->backupDate)) return VOLSERREAD_DUMPERROR;
281 ReadString(iodp, vol->offlineMessage, sizeof(vol->offlineMessage));
285 * Detailed volume statistics are never stored in dumps,
286 * so we just restore either the null string if this volume
287 * had already been set to store statistics, or the old motd
288 * contents otherwise. It doesn't matter, since this field
289 * will soon get initialized anyway.
291 ReadString(iodp, (char *)(vol->stat_reads), VMSGSIZE);
294 unsigned short length;
297 if(!ReadShort(iodp, &length)) return VOLSERREAD_DUMPERROR;
298 for (i = 0; i<length; i++) {
299 if(!ReadInt32(iodp, &data)) return VOLSERREAD_DUMPERROR;
300 if (i < sizeof(vol->weekUse)/sizeof(vol->weekUse[0]))
301 vol->weekUse[i] = data;
306 if(!ReadInt32(iodp, &vol->dayUseDate)) return VOLSERREAD_DUMPERROR;
309 if(!ReadInt32(iodp, (afs_uint32 *)&vol->dayUse)) return VOLSERREAD_DUMPERROR;
313 iod_ungetc(iodp, tag);
317 static int DumpTag(register struct iod *iodp, register int tag)
322 return((iod_Write(iodp, &p, 1) == 1)? 0 : VOLSERDUMPERROR);
326 static int DumpByte(register struct iod *iodp, char tag, byte value)
329 register byte *p = (unsigned char *) tbuffer;
332 return((iod_Write(iodp, tbuffer, 2) == 2)? 0: VOLSERDUMPERROR);
335 #define putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
336 #define putshort(p, v) *p++ = v>>8, *p++ = v
338 static int DumpDouble(register struct iod *iodp, char tag,
339 register afs_uint32 value1, register afs_uint32 value2)
342 register byte *p = (unsigned char *) tbuffer;
346 return((iod_Write(iodp, tbuffer, 9) == 9)? 0: VOLSERDUMPERROR);
349 static int DumpInt32(register struct iod *iodp, char tag,
350 register afs_uint32 value)
353 register byte *p = (unsigned char *) tbuffer;
356 return((iod_Write(iodp, tbuffer, 5) == 5)? 0: VOLSERDUMPERROR);
359 static int DumpArrayInt32(register struct iod *iodp, char tag,
360 register afs_uint32 *array, register int nelem)
363 register afs_uint32 v ;
365 register byte *p = (unsigned char *) tbuffer;
368 code = iod_Write(iodp, tbuffer, 3);
369 if (code != 3) return VOLSERDUMPERROR;
371 p = (unsigned char *) tbuffer;
372 v = *array++;/*this was register */
375 code = iod_Write(iodp, tbuffer, 4);
376 if(code != 4) return VOLSERDUMPERROR;
381 static int DumpShort(register struct iod *iodp, char tag, unsigned int value)
384 register byte *p = (unsigned char *) tbuffer;
388 return((iod_Write(iodp, tbuffer, 3) == 3)? 0: VOLSERDUMPERROR);
391 static int DumpBool(register struct iod *iodp, char tag, unsigned int value)
394 register byte *p = (unsigned char *) tbuffer;
397 return((iod_Write(iodp, tbuffer, 2) == 2)? 0: VOLSERDUMPERROR);
400 static int DumpString(register struct iod *iodp, char tag, register char *s)
404 code = iod_Write(iodp, &tag, 1);
405 if(code != 1) return VOLSERDUMPERROR;
407 code = iod_Write(iodp, s, n);
408 if(code != n) return VOLSERDUMPERROR;
412 static int DumpByteString(register struct iod *iodp, char tag,
413 register byte *bs, register int nbytes)
417 code = iod_Write(iodp, &tag, 1);
418 if(code != 1) return VOLSERDUMPERROR;
419 code = iod_Write(iodp, (char *)bs, nbytes);
420 if(code != nbytes) return VOLSERDUMPERROR;
424 static int DumpFile(struct iod *iodp, int vnode, FdHandle_t *handleP)
426 int code = 0, lcode = 0, error = 0;
427 afs_int32 pad = 0, offset;
428 afs_size_t n, nbytes, howMany, howBig;
430 #ifdef AFS_LARGEFILE_ENV
431 struct stat64 status;
437 #include <sys/statfs.h>
438 struct statfs tstatfs;
442 howBig = _filelength(handleP->fd_fd);
446 #ifdef AFS_LARGEFILE_ENV
447 fstat64(handleP->fd_fd, &status);
448 #else /* !AFS_LARGEFILE_ENV */
449 fstat(handleP->fd_fd, &status);
450 #endif /* !AFS_LARGEFILE_ENV */
451 howBig = status.st_size;
454 /* Unfortunately in AIX valuable fields such as st_blksize are
455 * gone from the stat structure.
457 fstatfs(handleP->fd_fd, &tstatfs);
458 howMany = tstatfs.f_bsize;
460 howMany = status.st_blksize;
461 #endif /* AFS_AIX_ENV */
462 #endif /* AFS_NT40_ENV */
465 size = FDH_SIZE(handleP);
466 #ifdef AFS_LARGEFILE_ENV
469 SplitInt64(size, hi, lo);
471 code = DumpInt32(iodp, 'f', lo);
473 code = DumpDouble(iodp, 'h', hi, lo);
476 #else /* !AFS_LARGEFILE_ENV */
477 code = DumpInt32(iodp, 'f', size);
478 #endif /* !AFS_LARGEFILE_ENV */
480 return VOLSERDUMPERROR;
483 p = (unsigned char *) malloc(howMany);
485 Log("1 Volser: DumpFile: no memory");
486 return VOLSERDUMPERROR;
489 for (nbytes = size; (nbytes && !error); nbytes -= howMany) {
490 if (nbytes < howMany)
493 /* Read the data - unless we know we can't */
494 n = (lcode ? 0 : FDH_READ(handleP, p, howMany));
496 /* If read any good data and we null padded previously, log the
497 * amount that we had null padded.
499 if ((n > 0) && pad) {
500 Log("1 Volser: DumpFile: Null padding file %d bytes at offset %u\n",
505 /* If didn't read enough data, null padd the rest of the buffer. This
506 * can happen if, for instance, the media has some bad spots. We don't
507 * want to quit the dump, so we start null padding.
510 /* Record the read error */
513 Log("1 Volser: DumpFile: Error %d reading inode %s for vnode %d\n",
514 errno, PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
517 Log("1 Volser: DumpFile: Error reading inode %s for vnode %d\n",
518 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
521 /* Pad the rest of the buffer with zeros. Remember offset we started
522 * padding. Keep total tally of padding.
524 memset(p+n, 0, howMany-n);
526 offset = (howBig - nbytes) + n;
529 /* Now seek over the data we could not get. An error here means we
530 * can't do the next read.
532 lcode = FDH_SEEK(handleP, ((size - nbytes) + howMany), SEEK_SET);
533 if (lcode != ((size - nbytes) + howMany)) {
535 Log("1 Volser: DumpFile: Error %d seeking in inode %s for vnode %d\n",
536 errno, PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
539 Log("1 Volser: DumpFile: Error seeking in inode %s for vnode %d\n",
540 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
549 /* Now write the data out */
550 if (iod_Write(iodp, (char *)p, howMany) != howMany)
551 error = VOLSERDUMPERROR;
555 if (pad) { /* Any padding we hadn't reported yet */
556 Log("1 Volser: DumpFile: Null padding file: %d bytes at offset %u\n",
564 static int DumpVolumeHeader(register struct iod *iodp, register Volume *vp)
567 static char nullString[1] = ""; /*The ``contents'' of motd*/
569 if (!code) code = DumpTag(iodp, D_VOLUMEHEADER);
570 if (!code) {code = DumpInt32(iodp, 'i',V_id(vp));}
571 if (!code) code = DumpInt32(iodp, 'v',V_stamp(vp).version);
572 if (!code) code = DumpString(iodp, 'n',V_name(vp));
573 if (!code) code = DumpBool(iodp, 's',V_inService(vp));
574 if (!code) code = DumpBool(iodp, 'b',V_blessed(vp));
575 if (!code) code = DumpInt32(iodp, 'u',V_uniquifier(vp));
576 if (!code) code = DumpByte(iodp, 't',(byte)V_type(vp));
577 if (!code){ code = DumpInt32(iodp, 'p',V_parentId(vp));}
578 if (!code) code = DumpInt32(iodp, 'c',V_cloneId(vp));
579 if (!code) code = DumpInt32(iodp, 'q',V_maxquota(vp));
580 if (!code) code = DumpInt32(iodp, 'm',V_minquota(vp));
581 if (!code) code = DumpInt32(iodp, 'd',V_diskused(vp));
582 if (!code) code = DumpInt32(iodp, 'f',V_filecount(vp));
583 if (!code) code = DumpInt32(iodp, 'a', V_accountNumber(vp));
584 if (!code) code = DumpInt32(iodp, 'o', V_owner(vp));
585 if (!code) code = DumpInt32(iodp, 'C',V_creationDate(vp)); /* Rw volume creation date */
586 if (!code) code = DumpInt32(iodp, 'A',V_accessDate(vp));
587 if (!code) code = DumpInt32(iodp, 'U',V_updateDate(vp));
588 if (!code) code = DumpInt32(iodp, 'E',V_expirationDate(vp));
589 if (!code) code = DumpInt32(iodp, 'B',V_backupDate(vp)); /* Rw volume backup clone date */
590 if (!code) code = DumpString(iodp, 'O',V_offlineMessage(vp));
592 * We do NOT dump the detailed volume statistics residing in the old
593 * motd field, since we cannot tell from the info in a dump whether
594 * statistics data has been put there. Instead, we dump a null string,
595 * just as if that was what the motd contained.
597 if (!code) code = DumpString(iodp, 'M', nullString);
598 if (!code) code = DumpArrayInt32(iodp, 'W', (afs_uint32 *)V_weekUse(vp), sizeof(V_weekUse(vp))/sizeof(V_weekUse(vp)[0]));
599 if (!code) code = DumpInt32(iodp, 'D', V_dayUseDate(vp));
600 if (!code) code = DumpInt32(iodp, 'Z', V_dayUse(vp));
604 static int DumpEnd(register struct iod *iodp)
606 return(DumpInt32(iodp, D_DUMPEND, DUMPENDMAGIC));
609 /* Guts of the dump code */
611 /* Dump a whole volume */
612 int DumpVolume(register struct rx_call *call, register Volume *vp,
613 afs_int32 fromtime, int dumpAllDirs)
617 register struct iod *iodp = &iod;
618 iod_Init(iodp, call);
620 if (!code) code = DumpDumpHeader(iodp, vp, fromtime);
622 if (!code) code = DumpPartial(iodp, vp, fromtime, dumpAllDirs);
624 /* hack follows. Errors should be handled quite differently in this version of dump than they used to be.*/
625 if (rx_Error(iodp->call)) {
626 Log("1 Volser: DumpVolume: Rx call failed during dump, error %d\n", rx_Error(iodp->call));
627 return VOLSERDUMPERROR;
629 if (!code) code = DumpEnd(iodp);
634 /* Dump a volume to multiple places*/
635 int DumpVolMulti(struct rx_call **calls, int ncalls, Volume *vp,
636 afs_int32 fromtime, int dumpAllDirs, int *codes)
640 iod_InitMulti(&iod, calls, ncalls, codes);
642 if (!code) code = DumpDumpHeader(&iod, vp, fromtime);
643 if (!code) code = DumpPartial(&iod, vp, fromtime, dumpAllDirs);
644 if (!code) code = DumpEnd(&iod);
648 /* A partial dump (no dump header) */
649 static int DumpPartial(register struct iod *iodp, register Volume *vp,
650 afs_int32 fromtime, int dumpAllDirs)
653 if (!code) code = DumpVolumeHeader(iodp, vp);
654 if (!code) code = DumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs);
655 if (!code) code = DumpVnodeIndex(iodp, vp, vSmall, fromtime, 0);
659 static int DumpVnodeIndex(register struct iod *iodp, Volume *vp,
660 VnodeClass class, afs_int32 fromtime, int forcedump)
662 register int code = 0;
663 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
664 char buf[SIZEOF_LARGEDISKVNODE];
665 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *) buf;
666 StreamHandle_t *file;
670 register int vnodeIndex, nVnodes;
672 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
674 file = FDH_FDOPEN(fdP, "r+");
675 assert(file != NULL);
676 size = OS_SIZE(fdP->fd_fd);
678 nVnodes = (size / vcp->diskSize) - 1;
680 assert((nVnodes+1)*vcp->diskSize == size);
681 assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
684 for (vnodeIndex = 0; nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
685 nVnodes--, vnodeIndex++) {
686 flag = forcedump || (vnode->serverModifyTime >= fromtime);
687 /* Note: the >= test is very important since some old volumes may not have
688 a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
689 does dump the file! */
690 if (!code) code = DumpVnode(iodp, vnode, V_id(vp), bitNumberToVnodeNumber(vnodeIndex, class), flag);
691 if (!flag) IOMGR_Poll(); /* if we dont' xfr data, but scan instead, could lose conn */
698 static int DumpDumpHeader(register struct iod *iodp, register Volume *vp,
702 int UseLatestReadOnlyClone = 1;
703 afs_int32 dumpTimes[2];
704 iodp->device = vp->device;
705 iodp->parentId = V_parentId(vp);
706 iodp->dumpPartition = vp->partition;
707 if (!code) code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
708 if (!code) code = DumpInt32(iodp, 'v', UseLatestReadOnlyClone? V_id(vp): V_parentId(vp));
709 if (!code) code = DumpString(iodp, 'n',V_name(vp));
710 dumpTimes[0] = fromtime;
711 dumpTimes[1] = V_backupDate(vp); /* Until the time the clone was made */
712 if (!code) code = DumpArrayInt32(iodp, 't', (afs_uint32 *)dumpTimes, 2);
716 static int DumpVnode(register struct iod *iodp, struct VnodeDiskObject *v,
717 int volid, int vnodeNumber, int dumpEverything)
723 if (!v || v->type == vNull)
725 if (!code) code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier);
728 if (!code) code = DumpByte(iodp, 't',(byte)v->type);
729 if (!code) code = DumpShort(iodp, 'l', v->linkCount); /* May not need this */
730 if (!code) code = DumpInt32(iodp, 'v', v->dataVersion);
731 if (!code) code = DumpInt32(iodp, 'm', v->unixModifyTime);
732 if (!code) code = DumpInt32(iodp, 'a', v->author);
733 if (!code) code = DumpInt32(iodp, 'o', v->owner);
734 if (!code && v->group) code = DumpInt32(iodp, 'g', v->group); /* default group is 0 */
735 if (!code) code = DumpShort(iodp, 'b', v->modeBits);
736 if (!code) code = DumpInt32(iodp, 'p', v->parent);
737 if (!code) code = DumpInt32(iodp, 's', v->serverModifyTime);
738 if (v->type == vDirectory) {
739 acl_HtonACL(VVnodeDiskACL(v));
740 if (!code) code = DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v), VAclDiskSize(v));
742 if (VNDISK_GET_INO(v)) {
743 IH_INIT(ihP, iodp->device, iodp->parentId, VNDISK_GET_INO(v));
746 Log("1 Volser: DumpVnode: dump: Unable to open inode %d for vnode %d (volume %d); not dumped, error %d\n",
747 VNDISK_GET_INO(v), vnodeNumber, volid, errno);
749 return VOLSERREAD_DUMPERROR;
751 code = DumpFile(iodp, vnodeNumber, fdP);
759 int ProcessIndex(Volume *vp, VnodeClass class, afs_int32 **Bufp, int *sizep,
762 int i, nVnodes, offset, code, index=0;
766 StreamHandle_t *afile;
768 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
769 char buf[SIZEOF_LARGEDISKVNODE], zero[SIZEOF_LARGEDISKVNODE];
770 register struct VnodeDiskObject *vnode = (struct VnodeDiskObject *) buf;
772 memset(zero, 0, sizeof(zero)); /* zero out our proto-vnode */
773 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
776 afile = FDH_FDOPEN(fdP, "r+");
780 for (i = 0; i<*sizep; i++) {
783 STREAM_SEEK(afile, Buf[i], 0);
784 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
786 if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
789 #ifdef AFS_LARGEFILE_ENV
791 VNDISK_GET_LEN(fileLen, vnode);
792 Log("RestoreVolume %d Cleanup: Removing old vnode=%d inode=%d size=(0X%x,0X%x)\n",
793 V_id(vp), bitNumberToVnodeNumber(i,class),
794 VNDISK_GET_INO(vnode),
795 (unsigned) (fileLen >> 32),
796 (unsigned) (fileLen & 0xffffffff));
797 #else /* !AFS_LARGEFILE_ENV */
798 Log("RestoreVolume %d Cleanup: Removing old vnode=%d inode=%d size=%d\n",
799 V_id(vp), bitNumberToVnodeNumber(i,class),
800 VNDISK_GET_INO(vnode), vnode->length);
801 #endif /* !AFS_LARGEFILE_ENV */
803 IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(vnode),
807 STREAM_SEEK(afile, Buf[i], 0);
808 (void ) STREAM_WRITE(zero, vcp->diskSize, 1, afile); /* Zero it out */
814 Log("RestoreVolume Cleanup: Removed %d inodes for volume %d\n",
817 STREAM_FLUSH(afile); /* ensure 0s are on the disk */
818 OS_SYNC(afile->str_fd);
820 size = OS_SIZE(fdP->fd_fd);
822 nVnodes = (size <= vcp->diskSize ? 0 :
823 size-vcp->diskSize) >> vcp->logSize;
825 Buf = (afs_int32 *) malloc(nVnodes * sizeof(afs_int32));
826 if (Buf == NULL) return 1;
827 memset((char *)Buf, 0, nVnodes * sizeof(afs_int32));
828 STREAM_SEEK(afile, offset = vcp->diskSize, 0);
830 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
834 if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
835 Buf[(offset >> vcp->logSize)-1] = offset;
838 offset += vcp->diskSize;
850 int RestoreVolume(register struct rx_call *call, Volume *avp,
851 int incremental, struct restoreCookie *cookie)
854 struct DumpHeader header;
856 Error error = 0, vupdate;
859 register struct iod *iodp = &iod;
860 afs_int32 *b1=0, *b2=0;
861 int s1=0, s2=0, delo=0, tdelo;
864 iod_Init(iodp, call);
867 if (!ReadDumpHeader(iodp, &header)){
868 Log("1 Volser: RestoreVolume: Error reading header file for dump; aborted\n");
869 return VOLSERREAD_DUMPERROR;
871 if (iod_getc(iodp) != D_VOLUMEHEADER){
872 Log("1 Volser: RestoreVolume: Volume header missing from dump; not restored\n");
873 return VOLSERREAD_DUMPERROR;
875 if(ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR) return VOLSERREAD_DUMPERROR;
877 delo = ProcessIndex(vp, vLarge, &b1, &s1, 0);
878 if (!delo) delo = ProcessIndex(vp, vSmall, &b2, &s2, 0);
880 if (b1) free((char *)b1);
881 if (b2) free((char *)b2);
885 strncpy(vol.name,cookie->name,VOLSER_OLDMAXVOLNAME);
886 vol.type = cookie->type;
887 vol.cloneId = cookie->clone;
888 vol.parentId = cookie->parent;
893 if (ReadVnodes(iodp, vp, 0, b1, s1, b2, s2, tdelo)) {
894 error = VOLSERREAD_DUMPERROR;
897 tag = iod_getc(iodp);
898 if (tag != D_VOLUMEHEADER)
900 if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR) {
901 error = VOLSERREAD_DUMPERROR;
906 if (tag != D_DUMPEND || !ReadInt32(iodp, &endMagic) || endMagic != DUMPENDMAGIC){
907 Log("1 Volser: RestoreVolume: End of dump not found; restore aborted\n");
908 error = VOLSERREAD_DUMPERROR;
913 if (iod_getc(iodp) != EOF) {
914 Log("1 Volser: RestoreVolume: Unrecognized postamble in dump; restore aborted\n");
915 error = VOLSERREAD_DUMPERROR;
920 ProcessIndex(vp, vLarge, &b1, &s1, 1);
921 ProcessIndex(vp, vSmall, &b2, &s2, 1);
925 ClearVolumeStats(&vol);
926 CopyVolumeHeader(&vol, &V_disk(vp));
928 VUpdateVolume(&vupdate, vp);
930 Log("1 Volser: RestoreVolume: Unable to rewrite volume header; restore aborted\n");
931 error = VOLSERREAD_DUMPERROR;
935 /* Free the malloced space above */
936 if (b1) free((char *)b1);
937 if (b2) free((char *)b2);
941 static int ReadVnodes(register struct iod *iodp, Volume *vp,
942 int incremental, afs_int32 *Lbuf, afs_int32 s1,
943 afs_int32 *Sbuf, afs_int32 s2, afs_int32 delo)
945 afs_int32 vnodeNumber;
946 char buf[SIZEOF_LARGEDISKVNODE];
948 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *) buf;
949 struct VnodeDiskObject oldvnode;
952 struct VnodeClassInfo *vcp;
957 tag = iod_getc(iodp);
958 V_pref(vp, nearInode);
959 while (tag == D_VNODE) {
961 memset(buf, 0, sizeof (buf));
962 if (!ReadInt32(iodp, (afs_uint32 *)&vnodeNumber))
965 ReadInt32(iodp, &vnode->uniquifier);
966 while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
970 vnode->type = (VnodeType) iod_getc(iodp);
975 ReadShort(iodp, &tlc);
976 vnode->linkCount = (signed int)tlc;
980 ReadInt32(iodp, &vnode->dataVersion);
983 ReadInt32(iodp, &vnode->unixModifyTime);
986 ReadInt32(iodp, &vnode->serverModifyTime);
989 ReadInt32(iodp, &vnode->author);
992 ReadInt32(iodp, &vnode->owner);
995 ReadInt32(iodp, (afs_uint32 *)&vnode->group);
998 unsigned short modeBits;
999 ReadShort(iodp, &modeBits);
1000 vnode->modeBits = (unsigned int)modeBits;
1004 ReadInt32(iodp, &vnode->parent);
1007 ReadByteString(iodp, (byte *)VVnodeDiskACL(vnode),
1008 VAclDiskSize(vnode));
1009 acl_NtohACL(VVnodeDiskACL(vnode));
1011 #ifdef AFS_LARGEFILE_ENV
1019 ino = IH_CREATE(V_linkHandle(vp),
1021 VPartitionPath(V_partition(vp)),
1022 nearInode, V_parentId(vp), vnodeNumber,
1024 vnode->dataVersion);
1025 if (!VALID_INO(ino)) {
1026 perror("unable to allocate inode");
1027 Log("1 Volser: ReadVnodes: Restore aborted\n");
1028 return VOLSERREAD_DUMPERROR;
1031 VNDISK_SET_INO(vnode, ino);
1032 IH_INIT(tmpH, vp->device, V_parentId(vp), ino);
1033 fdP = IH_OPEN(tmpH);
1036 return VOLSERREAD_DUMPERROR;
1039 fileLen = volser_WriteFile(vnodeNumber, iodp, fdP,
1041 VNDISK_SET_LEN(vnode, fileLen);
1042 FDH_REALLYCLOSE(fdP);
1045 Log("1 Volser: ReadVnodes: IDEC inode %d\n", ino);
1046 IH_DEC(V_linkHandle(vp), ino, V_parentId(vp));
1047 return VOLSERREAD_DUMPERROR;
1054 class = vnodeIdToClass(vnodeNumber);
1055 vcp = &VnodeClassInfo[class];
1057 /* Mark this vnode as in this dump - so we don't delete it later */
1059 idx = (vnodeIndexOffset(vcp,vnodeNumber) >> vcp->logSize) - 1;
1060 if (class == vLarge) {
1061 if (Lbuf && (idx < s1)) Lbuf[idx] = 0;
1063 if (Sbuf && (idx < s2)) Sbuf[idx] = 0;
1068 FdHandle_t *fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1070 Log("1 Volser: ReadVnodes: Error opening vnode index; restore aborted\n");
1071 return VOLSERREAD_DUMPERROR;
1073 if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) < 0) {
1074 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1075 FDH_REALLYCLOSE(fdP);
1076 return VOLSERREAD_DUMPERROR;
1078 if (FDH_READ(fdP, &oldvnode, sizeof(oldvnode)) == sizeof(oldvnode)) {
1079 if (oldvnode.type != vNull && VNDISK_GET_INO(&oldvnode)) {
1080 IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(&oldvnode),
1084 vnode->vnodeMagic = vcp->magic;
1085 if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) < 0) {
1086 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1087 FDH_REALLYCLOSE(fdP);
1088 return VOLSERREAD_DUMPERROR;
1090 if (FDH_WRITE(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
1091 Log("1 Volser: ReadVnodes: Error writing vnode index; restore aborted\n");
1092 FDH_REALLYCLOSE(fdP);
1093 return VOLSERREAD_DUMPERROR;
1098 iod_ungetc(iodp, tag);
1105 /* called with disk file only. Note that we don't have to worry about rx_Read
1106 * needing to read an ungetc'd character, since the ReadInt32 will have read
1109 static afs_offs_t volser_WriteFile(int vn, struct iod *iodp, FdHandle_t *handleP,
1110 int tag, Error *status)
1113 afs_offs_t filesize;
1114 afs_offs_t written=0;
1115 register afs_uint32 size = 8192;
1116 register afs_offs_t nbytes;
1121 #ifdef AFS_64BIT_ENV
1123 afs_uint32 filesize_high = 0L, filesize_low = 0L;
1124 #ifdef AFS_LARGEFILE_ENV
1126 if (!ReadInt32(iodp, &filesize_high) ) {
1132 if (!ReadInt32(iodp, &filesize_low)) {
1136 FillInt64(filesize, filesize_high, filesize_low);
1138 #else /* !AFS_64BIT_ENV */
1139 if (!ReadInt32(iodp, &filesize)) {
1143 #endif /* !AFS_64BIT_ENV */
1144 p = (unsigned char *) malloc(size);
1149 for (nbytes = filesize; nbytes; nbytes -= size) {
1153 if ((code = iod_Read(iodp, p, size)) != size) {
1154 #ifdef AFS_64BIT_ENV
1155 Log("1 Volser: WriteFile: Error reading dump file %d size=(0X%x,0X%x) nbytes=%d (%d of %d); restore aborted\n", vn,
1156 (unsigned) (filesize >> 32),
1157 (unsigned) (filesize & 0xffffffff),
1158 nbytes, code, size);
1159 #else /* !AFS_LARGEFILE_ENV */
1160 Log("1 Volser: WriteFile: Error reading dump file %d size=%d nbytes=%d (%d of %d); restore aborted\n", vn, filesize, nbytes, code, size);
1161 #endif /* !AFS_LARGEFILE_ENV */
1165 code = FDH_WRITE(handleP, p, size);
1166 if (code > 0) written += code;
1168 Log("1 Volser: WriteFile: Error creating file in volume; restore aborted\n");
1177 static int ReadDumpHeader(register struct iod *iodp, struct DumpHeader *hp)
1180 afs_uint32 beginMagic;
1181 if (iod_getc(iodp) != D_DUMPHEADER || !ReadInt32(iodp, &beginMagic)
1182 || !ReadInt32(iodp, (afs_uint32 *)&hp->version)
1183 || beginMagic != DUMPBEGINMAGIC
1187 while ((tag = iod_getc(iodp)) > D_MAX) {
1188 unsigned short arrayLength;
1192 if (!ReadInt32(iodp, &hp->volumeId))
1196 ReadString(iodp, hp->volumeName, sizeof(hp->volumeName));
1199 if (!ReadShort(iodp, &arrayLength))
1201 hp->nDumpTimes = (arrayLength >> 1);
1202 for (i = 0; i<hp->nDumpTimes; i++)
1203 if (!ReadInt32(iodp, (afs_uint32 *)&hp->dumpTimes[i].from)
1204 || !ReadInt32(iodp, (afs_uint32 *)&hp->dumpTimes[i].to))
1209 if (!hp->volumeId || !hp->nDumpTimes) {
1212 iod_ungetc(iodp, tag);