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
13 Institution: The Information Technology Center, Carnegie-Mellon University
17 #include <afsconfig.h>
18 #include <afs/param.h>
33 #include <sys/param.h>
40 #include <afs/afsint.h>
42 #include <afs/errors.h>
45 #include <afs/afssyscalls.h>
49 #include "partition.h"
50 #include "viceinode.h"
51 #include <afs/afssyscalls.h>
72 #include "volser/volser.h"
73 #include "volser/volint.h"
74 #include "volser/dump.h"
76 #define putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
77 #define putshort(p, v) *p++ = v>>8, *p++ = v
80 #define afs_stat stat64
81 #define afs_fstat fstat64
82 #define afs_open open64
83 #else /* !O_LARGEFILE */
85 #define afs_fstat fstat
87 #endif /* !O_LARGEFILE */
89 int VolumeChanged; /* needed by physio - leave alone */
92 /* Forward Declarations */
93 void HandleVolume(struct DiskPartition64 *partP, char *name, char *filename, int fromtime);
94 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
95 register struct VolumeHeader *header);
96 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
97 char *dumpfile, int fromtime);
100 #include "AFS_component_version_number.c"
103 char name[VMAXPATHLEN];
107 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
112 code = IH_IREAD(ih, 0, to, size);
121 AttachVolume(struct DiskPartition64 * dp, char *volname,
122 register struct VolumeHeader * header)
127 vp = (Volume *) calloc(1, sizeof(Volume));
128 vp->specialStatus = 0;
129 vp->device = dp->device;
131 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
132 header->largeVnodeIndex);
133 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
134 header->smallVnodeIndex);
135 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
137 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
138 vp->cacheCheck = 0; /* XXXX */
139 vp->shuttingDown = 0;
140 vp->goingOffline = 0;
142 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
143 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
144 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
146 struct IndexFileHeader iHead;
147 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
148 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
151 struct IndexFileHeader iHead;
152 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
153 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
157 struct versionStamp stamp;
158 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
159 LINKTABLEMAGIC, LINKTABLEVERSION);
169 handleit(struct cmd_syndesc *as, void *arock)
171 register struct cmd_item *ti;
175 char *fileName = NULL;
176 struct DiskPartition64 *partP = NULL;
178 char tmpPartName[20];
185 if (geteuid() != 0) {
186 fprintf(stderr, "voldump must be run as root; sorry\n");
192 if ((ti = as->parms[0].items))
194 if ((ti = as->parms[1].items))
195 volumeId = atoi(ti->data);
196 if ((ti = as->parms[2].items))
198 if ((ti = as->parms[3].items))
200 if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
201 code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
203 fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
204 as->parms[4].items->data, code);
211 err = VAttachPartitions();
213 fprintf(stderr, "%d partitions had errors during attach.\n", err);
217 if (strlen(partName) == 1) {
218 if (partName[0] >= 'a' && partName[0] <= 'z') {
219 strcpy(tmpPartName, "/vicepa");
220 tmpPartName[6] = partName[0];
221 partP = VGetPartition(tmpPartName, 0);
224 partP = VGetPartition(partName, 0);
228 "%s is not an AFS partition name on this server.\n",
235 fprintf(stderr, "Must specify volume id!\n");
240 fprintf(stderr, "must specify vice partition.\n");
244 (void)afs_snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
245 HandleVolume(partP, name1, fileName, fromtime);
250 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
252 struct VolumeHeader header;
253 struct VolumeDiskHeader diskHeader;
254 struct afs_stat status, stat;
258 char headerName[1024];
262 (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
263 VPartitionPath(dp), name);
264 if ((fd = afs_open(headerName, O_RDONLY)) == -1
265 || afs_fstat(fd, &status) == -1) {
266 fprintf(stderr, "Cannot read volume header %s\n", name);
270 n = read(fd, &diskHeader, sizeof(diskHeader));
272 if (n != sizeof(diskHeader)
273 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
274 fprintf(stderr, "Error reading volume header %s\n", name);
277 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
279 "Volume %s, version number is incorrect; volume needs salvage\n",
283 DiskToVolumeHeader(&header, &diskHeader);
286 vp = AttachVolume(dp, name, &header);
288 fprintf(stderr, "Error attaching volume header %s\n", name);
292 DoMyVolDump(vp, dp, filename, fromtime);
297 main(int argc, char **argv)
299 register struct cmd_syndesc *ts;
302 VInitVolumePackage(volumeUtility, 5, 5, DONT_CONNECT_FS, 0);
304 ts = cmd_CreateSyntax(NULL, handleit, NULL,
305 "Dump a volume to a 'vos dump' format file without using volserver");
306 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL, "AFS partition name");
307 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
308 cmd_AddParm(ts, "-file", CMD_LIST, CMD_OPTIONAL, "Dump filename");
309 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
310 "Trace dump progress (very verbose)");
311 cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
312 code = cmd_Dispatch(argc, argv);
320 DumpDouble(int dumpfd, char tag, register afs_uint32 value1,
321 register afs_uint32 value2)
325 register byte *p = (unsigned char *)tbuffer;
330 res = write(dumpfd, tbuffer, 9);
331 return ((res == 9) ? 0 : VOLSERDUMPERROR);
335 DumpInt32(int dumpfd, char tag, register afs_uint32 value)
338 register byte *p = (unsigned char *)tbuffer;
341 return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
345 DumpString(int dumpfd, char tag, register char *s)
349 code = write(dumpfd, &tag, 1);
351 return VOLSERDUMPERROR;
353 code = write(dumpfd, s, n);
355 return VOLSERDUMPERROR;
361 DumpArrayInt32(int dumpfd, char tag, register afs_uint32 * array,
365 register afs_uint32 v;
367 register byte *p = (unsigned char *)tbuffer;
370 code = write(dumpfd, tbuffer, 3);
372 return VOLSERDUMPERROR;
374 p = (unsigned char *)tbuffer;
375 v = *array++; /*this was register */
378 code = write(dumpfd, tbuffer, 4);
380 return VOLSERDUMPERROR;
389 DumpDumpHeader(int dumpfd, register Volume * vp, afs_int32 fromtime)
392 afs_int32 dumpTimes[2];
395 fprintf(stderr, "dumping dump header\n");
398 code = DumpDouble(dumpfd, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
401 code = DumpInt32(dumpfd, 'v', V_id(vp));
404 code = DumpString(dumpfd, 'n', V_name(vp));
406 dumpTimes[0] = fromtime;
407 dumpTimes[1] = V_backupDate(vp); /* Until the time the clone was made */
409 code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2);
418 return (DumpInt32(dumpfd, D_DUMPEND, DUMPENDMAGIC));
422 DumpByte(int dumpfd, char tag, byte value)
425 register byte *p = (unsigned char *)tbuffer;
428 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
432 DumpTag(int dumpfd, register int tag)
437 return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
442 DumpBool(int dumpfd, char tag, unsigned int value)
445 register byte *p = (unsigned char *)tbuffer;
448 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
454 DumpVolumeHeader(int dumpfd, register Volume * vp)
459 fprintf(stderr, "dumping volume header\n");
462 code = DumpTag(dumpfd, D_VOLUMEHEADER);
464 code = DumpInt32(dumpfd, 'i', V_id(vp));
466 code = DumpInt32(dumpfd, 'v', V_stamp(vp).version);
468 code = DumpString(dumpfd, 'n', V_name(vp));
470 code = DumpBool(dumpfd, 's', V_inService(vp));
472 code = DumpBool(dumpfd, 'b', V_blessed(vp));
474 code = DumpInt32(dumpfd, 'u', V_uniquifier(vp));
476 code = DumpByte(dumpfd, 't', (byte) V_type(vp));
478 code = DumpInt32(dumpfd, 'p', V_parentId(vp));
480 code = DumpInt32(dumpfd, 'c', V_cloneId(vp));
482 code = DumpInt32(dumpfd, 'q', V_maxquota(vp));
484 code = DumpInt32(dumpfd, 'm', V_minquota(vp));
486 code = DumpInt32(dumpfd, 'd', V_diskused(vp));
488 code = DumpInt32(dumpfd, 'f', V_filecount(vp));
490 code = DumpInt32(dumpfd, 'a', V_accountNumber(vp));
492 code = DumpInt32(dumpfd, 'o', V_owner(vp));
494 code = DumpInt32(dumpfd, 'C', V_creationDate(vp)); /* Rw volume creation date */
496 code = DumpInt32(dumpfd, 'A', V_accessDate(vp));
498 code = DumpInt32(dumpfd, 'U', V_updateDate(vp));
500 code = DumpInt32(dumpfd, 'E', V_expirationDate(vp));
502 code = DumpInt32(dumpfd, 'B', V_backupDate(vp)); /* Rw volume backup clone date */
504 code = DumpString(dumpfd, 'O', V_offlineMessage(vp));
507 * We do NOT dump the detailed volume statistics residing in the old
508 * motd field, since we cannot tell from the info in a dump whether
509 * statistics data has been put there. Instead, we dump a null string,
510 * just as if that was what the motd contained.
513 code = DumpString(dumpfd, 'M', "");
516 DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp),
517 sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
519 code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp));
521 code = DumpInt32(dumpfd, 'Z', V_dayUse(vp));
523 code = DumpInt32(dumpfd, 'V', V_volUpCounter(vp));
528 DumpShort(int dumpfd, char tag, unsigned int value)
531 register byte *p = (unsigned char *)tbuffer;
535 return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
539 DumpByteString(int dumpfd, char tag, register byte * bs, register int nbytes)
543 code = write(dumpfd, &tag, 1);
545 return VOLSERDUMPERROR;
546 code = write(dumpfd, (char *)bs, nbytes);
548 return VOLSERDUMPERROR;
554 DumpFile(int dumpfd, int vnode, FdHandle_t * handleP, struct VnodeDiskObject *v)
556 int code = 0, failed_seek = 0, failed_write = 0;
558 afs_int32 offset = 0;
559 afs_sfsize_t n, nbytes, howMany, howBig;
562 struct afs_stat status;
564 afs_sfsize_t size, tmpsize;
566 #include <sys/statfs.h>
567 struct statfs tstatfs;
571 fprintf(stderr, "dumping file for vnode %d\n", vnode);
574 howBig = _filelength(handleP->fd_fd);
578 afs_fstat(handleP->fd_fd, &status);
579 howBig = status.st_size;
582 /* Unfortunately in AIX valuable fields such as st_blksize are
583 * gone from the stat structure.
585 fstatfs(handleP->fd_fd, &tstatfs);
586 howMany = tstatfs.f_bsize;
588 howMany = status.st_blksize;
589 #endif /* AFS_AIX_ENV */
590 #endif /* AFS_NT40_ENV */
593 size = FDH_SIZE(handleP);
596 fprintf(stderr, " howBig = %u, howMany = %u, fdh size = %u\n",
597 howBig, howMany, size);
599 #ifdef AFS_LARGEFILE_ENV
602 SplitInt64(size, hi, lo);
604 code = DumpInt32(dumpfd, 'f', lo);
606 code = DumpDouble(dumpfd, 'h', hi, lo);
609 #else /* !AFS_LARGEFILE_ENV */
610 code = DumpInt32(dumpfd, 'f', size);
611 #endif /* !AFS_LARGEFILE_ENV */
613 return VOLSERDUMPERROR;
616 p = (unsigned char *)malloc(howMany);
618 fprintf(stderr, "out of memory!\n");
619 return VOLSERDUMPERROR;
622 /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */
623 for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) {
624 if (nbytes < howMany)
627 /* Read the data - unless we know we can't */
628 n = (failed_seek ? 0 : FDH_READ(handleP, p, howMany));
630 /* If read any good data and we null padded previously, log the
631 * amount that we had null padded.
633 if ((n > 0) && pad) {
634 fprintf(stderr, "Null padding file %d bytes at offset %u\n", pad,
639 /* If didn't read enough data, null padd the rest of the buffer. This
640 * can happen if, for instance, the media has some bad spots. We don't
641 * want to quit the dump, so we start null padding.
645 if (verbose) fprintf(stderr, " read %u instead of %u bytes.\n", n, howMany);
647 /* Record the read error */
650 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
651 errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
654 fprintf(stderr, "Error reading inode %s for vnode %d\n",
655 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
658 /* Pad the rest of the buffer with zeros. Remember offset we started
659 * padding. Keep total tally of padding.
661 memset(p + n, 0, howMany - n);
663 offset = (howBig - nbytes) + n;
664 pad += (howMany - n);
666 /* Now seek over the data we could not get. An error here means we
667 * can't do the next read.
669 failed_seek = FDH_SEEK(handleP, ((size - nbytes) + howMany), SEEK_SET);
670 if (failed_seek != ((size - nbytes) + howMany)) {
671 if (failed_seek < 0) {
673 "Error %d seeking in inode %s for vnode %d\n",
674 errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
678 "Error seeking in inode %s for vnode %d\n",
679 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
687 /* Now write the data out */
688 if (write(dumpfd, (char *)p, howMany) != howMany)
689 failed_write = VOLSERDUMPERROR;
692 if (pad) { /* Any padding we hadn't reported yet */
693 fprintf(stderr, "Null padding file: %d bytes at offset %u\n", pad,
703 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
704 int dumpEverything, struct Volume *vp)
711 fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
713 if (!v || v->type == vNull)
716 code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
720 code = DumpByte(dumpfd, 't', (byte) v->type);
722 code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */
724 code = DumpInt32(dumpfd, 'v', v->dataVersion);
726 code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
728 code = DumpInt32(dumpfd, 'a', v->author);
730 code = DumpInt32(dumpfd, 'o', v->owner);
731 if (!code && v->group)
732 code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */
734 code = DumpShort(dumpfd, 'b', v->modeBits);
736 code = DumpInt32(dumpfd, 'p', v->parent);
738 code = DumpInt32(dumpfd, 's', v->serverModifyTime);
739 if (v->type == vDirectory) {
740 acl_HtonACL(VVnodeDiskACL(v));
743 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
747 if (VNDISK_GET_INO(v)) {
748 IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
752 "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
753 PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber, volid,
759 fprintf(stderr, "about to dump inode %s for vnode %u\n",
760 PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber);
761 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
768 fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
774 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
777 register int code = 0;
778 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
779 char buf[SIZEOF_LARGEDISKVNODE];
780 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
781 StreamHandle_t *file;
786 register int vnodeIndex, nVnodes = 0;
788 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
789 file = FDH_FDOPEN(fdP, "r+");
790 size = OS_SIZE(fdP->fd_fd);
791 nVnodes = (size / vcp->diskSize) - 1;
794 STREAM_SEEK(file, vcp->diskSize, 0);
798 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
799 nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
800 flag = forcedump || (vnode->serverModifyTime >= fromtime);
801 /* Note: the >= test is very important since some old volumes may not have
802 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
803 * does dump the file! */
805 fprintf(stderr, "about to dump %s vnode %u (vnode offset = %u)\n",
806 class == vSmall ? "vSmall" : "vLarge",
807 bitNumberToVnodeNumber(vnodeIndex, class), offset);
810 DumpVnode(dumpfd, vnode, V_id(vp),
811 bitNumberToVnodeNumber(vnodeIndex, class), flag,
821 /* A partial dump (no dump header) */
823 DumpPartial(int dumpfd, register Volume * vp, afs_int32 fromtime,
829 fprintf(stderr, "about to dump the volume header\n");
831 code = DumpVolumeHeader(dumpfd, vp);
834 fprintf(stderr, "about to dump the large vnode index\n");
836 code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
839 fprintf(stderr, "about to dump the small vnode index\n");
841 code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
848 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
857 afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
859 fprintf(stderr, "Failed to open dump file! Exiting.\n");
863 dumpfd = 1; /* stdout */
867 fprintf(stderr, "about to dump the dump header\n");
869 code = DumpDumpHeader(dumpfd, vp, fromtime);
872 fprintf(stderr, "about to dump volume contents\n");
874 code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
877 fprintf(stderr, "about to dump the dump postamble\n");
879 code = DumpEnd(dumpfd);
882 fprintf(stderr, "finished dump\n");
883 close(dumpfd); /* might be closing stdout, no harm */