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>
20 #ifdef IGNORE_SOME_GCC_WARNINGS
21 # pragma GCC diagnostic warning "-Wformat"
34 #include <sys/param.h>
41 #include <afs/afsint.h>
43 #include <afs/errors.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 <afs/afssyscalls.h>
55 #include <afs/com_err.h>
79 #define putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
80 #define putshort(p, v) *p++ = v>>8, *p++ = v
83 #define afs_stat stat64
84 #define afs_fstat fstat64
85 #define afs_open open64
86 #else /* !O_LARGEFILE */
88 #define afs_fstat fstat
90 #endif /* !O_LARGEFILE */
92 int VolumeChanged; /* needed by physio - leave alone */
95 /* Forward Declarations */
96 void HandleVolume(struct DiskPartition64 *partP, char *name, char *filename, int fromtime);
97 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
98 register struct VolumeHeader *header);
99 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
100 char *dumpfile, int fromtime);
103 #include "AFS_component_version_number.c"
106 char name[VMAXPATHLEN];
110 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
114 code = IH_IREAD(ih, 0, to, size);
123 AttachVolume(struct DiskPartition64 * dp, char *volname,
124 register struct VolumeHeader * header)
129 vp = (Volume *) calloc(1, sizeof(Volume));
130 vp->specialStatus = 0;
131 vp->device = dp->device;
133 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
134 header->largeVnodeIndex);
135 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
136 header->smallVnodeIndex);
137 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
139 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
140 vp->cacheCheck = 0; /* XXXX */
141 vp->shuttingDown = 0;
142 vp->goingOffline = 0;
144 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
145 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
146 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
148 struct IndexFileHeader iHead;
149 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
150 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
153 struct IndexFileHeader iHead;
154 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
155 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
159 struct versionStamp stamp;
160 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
161 LINKTABLEMAGIC, LINKTABLEVERSION);
171 handleit(struct cmd_syndesc *as, void *arock)
173 register struct cmd_item *ti;
175 afs_uint32 volumeId = 0;
177 char *fileName = NULL;
178 struct DiskPartition64 *partP = NULL;
180 char tmpPartName[20];
187 if (geteuid() != 0) {
188 fprintf(stderr, "voldump must be run as root; sorry\n");
194 if ((ti = as->parms[0].items))
196 if ((ti = as->parms[1].items))
197 volumeId = (afs_uint32)atoi(ti->data);
198 if ((ti = as->parms[2].items))
200 if ((ti = as->parms[3].items))
202 if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
203 code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
205 fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
206 as->parms[4].items->data, code);
213 err = VAttachPartitions();
215 fprintf(stderr, "%d partitions had errors during attach.\n", err);
219 if (strlen(partName) == 1) {
220 if (partName[0] >= 'a' && partName[0] <= 'z') {
221 strcpy(tmpPartName, "/vicepa");
222 tmpPartName[6] = partName[0];
223 partP = VGetPartition(tmpPartName, 0);
226 partP = VGetPartition(partName, 0);
230 "%s is not an AFS partition name on this server.\n",
237 fprintf(stderr, "Must specify volume id!\n");
242 fprintf(stderr, "must specify vice partition.\n");
246 (void)afs_snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
247 HandleVolume(partP, name1, fileName, fromtime);
252 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
254 struct VolumeHeader header;
255 struct VolumeDiskHeader diskHeader;
256 struct afs_stat status;
259 char headerName[1024];
263 (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
264 VPartitionPath(dp), name);
265 if ((fd = afs_open(headerName, O_RDONLY)) == -1
266 || afs_fstat(fd, &status) == -1) {
267 fprintf(stderr, "Cannot read volume header %s\n", name);
271 n = read(fd, &diskHeader, sizeof(diskHeader));
273 if (n != sizeof(diskHeader)
274 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
275 fprintf(stderr, "Error reading volume header %s\n", name);
278 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
280 "Volume %s, version number is incorrect; volume needs salvage\n",
284 DiskToVolumeHeader(&header, &diskHeader);
287 vp = AttachVolume(dp, name, &header);
289 fprintf(stderr, "Error attaching volume header %s\n", name);
293 DoMyVolDump(vp, dp, filename, fromtime);
298 main(int argc, char **argv)
300 register struct cmd_syndesc *ts;
302 VolumePackageOptions opts;
304 VOptDefaults(volumeUtility, &opts);
305 VInitVolumePackage2(volumeUtility, &opts);
307 ts = cmd_CreateSyntax(NULL, handleit, NULL,
308 "Dump a volume to a 'vos dump' format file without using volserver");
309 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL, "AFS partition name");
310 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
311 cmd_AddParm(ts, "-file", CMD_LIST, CMD_OPTIONAL, "Dump filename");
312 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
313 "Trace dump progress (very verbose)");
314 cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
315 code = cmd_Dispatch(argc, argv);
323 DumpDouble(int dumpfd, char tag, register afs_uint32 value1,
324 register afs_uint32 value2)
328 register byte *p = (unsigned char *)tbuffer;
333 res = write(dumpfd, tbuffer, 9);
334 return ((res == 9) ? 0 : VOLSERDUMPERROR);
338 DumpInt32(int dumpfd, char tag, register afs_uint32 value)
341 register byte *p = (unsigned char *)tbuffer;
344 return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
348 DumpString(int dumpfd, char tag, register char *s)
352 code = write(dumpfd, &tag, 1);
354 return VOLSERDUMPERROR;
356 code = write(dumpfd, s, n);
358 return VOLSERDUMPERROR;
364 DumpArrayInt32(int dumpfd, char tag, register afs_uint32 * array,
368 register afs_uint32 v;
370 register byte *p = (unsigned char *)tbuffer;
373 code = write(dumpfd, tbuffer, 3);
375 return VOLSERDUMPERROR;
377 p = (unsigned char *)tbuffer;
378 v = *array++; /*this was register */
381 code = write(dumpfd, tbuffer, 4);
383 return VOLSERDUMPERROR;
392 DumpDumpHeader(int dumpfd, register Volume * vp, afs_int32 fromtime)
395 afs_int32 dumpTimes[2];
398 fprintf(stderr, "dumping dump header\n");
401 code = DumpDouble(dumpfd, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
404 code = DumpInt32(dumpfd, 'v', V_id(vp));
407 code = DumpString(dumpfd, 'n', V_name(vp));
409 dumpTimes[0] = fromtime;
410 dumpTimes[1] = V_backupDate(vp); /* Until the time the clone was made */
412 code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2);
421 return (DumpInt32(dumpfd, D_DUMPEND, DUMPENDMAGIC));
425 DumpByte(int dumpfd, char tag, byte value)
428 register byte *p = (unsigned char *)tbuffer;
431 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
435 DumpTag(int dumpfd, register int tag)
440 return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
445 DumpBool(int dumpfd, char tag, unsigned int value)
448 register byte *p = (unsigned char *)tbuffer;
451 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
457 DumpVolumeHeader(int dumpfd, register Volume * vp)
462 fprintf(stderr, "dumping volume header\n");
465 code = DumpTag(dumpfd, D_VOLUMEHEADER);
467 code = DumpInt32(dumpfd, 'i', V_id(vp));
469 code = DumpInt32(dumpfd, 'v', V_stamp(vp).version);
471 code = DumpString(dumpfd, 'n', V_name(vp));
473 code = DumpBool(dumpfd, 's', V_inService(vp));
475 code = DumpBool(dumpfd, 'b', V_blessed(vp));
477 code = DumpInt32(dumpfd, 'u', V_uniquifier(vp));
479 code = DumpByte(dumpfd, 't', (byte) V_type(vp));
481 code = DumpInt32(dumpfd, 'p', V_parentId(vp));
483 code = DumpInt32(dumpfd, 'c', V_cloneId(vp));
485 code = DumpInt32(dumpfd, 'q', V_maxquota(vp));
487 code = DumpInt32(dumpfd, 'm', V_minquota(vp));
489 code = DumpInt32(dumpfd, 'd', V_diskused(vp));
491 code = DumpInt32(dumpfd, 'f', V_filecount(vp));
493 code = DumpInt32(dumpfd, 'a', V_accountNumber(vp));
495 code = DumpInt32(dumpfd, 'o', V_owner(vp));
497 code = DumpInt32(dumpfd, 'C', V_creationDate(vp)); /* Rw volume creation date */
499 code = DumpInt32(dumpfd, 'A', V_accessDate(vp));
501 code = DumpInt32(dumpfd, 'U', V_updateDate(vp));
503 code = DumpInt32(dumpfd, 'E', V_expirationDate(vp));
505 code = DumpInt32(dumpfd, 'B', V_backupDate(vp)); /* Rw volume backup clone date */
507 code = DumpString(dumpfd, 'O', V_offlineMessage(vp));
510 * We do NOT dump the detailed volume statistics residing in the old
511 * motd field, since we cannot tell from the info in a dump whether
512 * statistics data has been put there. Instead, we dump a null string,
513 * just as if that was what the motd contained.
516 code = DumpString(dumpfd, 'M', "");
519 DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp),
520 sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
522 code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp));
524 code = DumpInt32(dumpfd, 'Z', V_dayUse(vp));
529 DumpShort(int dumpfd, char tag, unsigned int value)
532 register byte *p = (unsigned char *)tbuffer;
536 return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
540 DumpByteString(int dumpfd, char tag, register byte * bs, register int nbytes)
544 code = write(dumpfd, &tag, 1);
546 return VOLSERDUMPERROR;
547 code = write(dumpfd, (char *)bs, nbytes);
549 return VOLSERDUMPERROR;
555 DumpFile(int dumpfd, int vnode, FdHandle_t * handleP, struct VnodeDiskObject *v)
557 int code = 0, failed_seek = 0, failed_write = 0;
559 afs_int32 offset = 0;
560 afs_sfsize_t n, nbytes, howMany, howBig;
564 struct afs_stat status;
568 #include <sys/statfs.h>
569 struct statfs tstatfs;
573 fprintf(stderr, "dumping file for vnode %d\n", vnode);
576 howBig = _filelength(handleP->fd_fd);
580 afs_fstat(handleP->fd_fd, &status);
581 howBig = status.st_size;
584 /* Unfortunately in AIX valuable fields such as st_blksize are
585 * gone from the stat structure.
587 fstatfs(handleP->fd_fd, &tstatfs);
588 howMany = tstatfs.f_bsize;
590 howMany = status.st_blksize;
591 #endif /* AFS_AIX_ENV */
592 #endif /* AFS_NT40_ENV */
595 size = FDH_SIZE(handleP);
598 fprintf(stderr, " howBig = %u, howMany = %u, fdh size = %u\n",
599 (unsigned int) howBig, (unsigned int) howMany,
600 (unsigned int) size);
602 SplitInt64(size, hi, lo);
604 code = DumpInt32(dumpfd, 'f', lo);
606 code = DumpDouble(dumpfd, 'h', hi, lo);
610 return VOLSERDUMPERROR;
613 p = (unsigned char *)malloc(howMany);
615 fprintf(stderr, "out of memory!\n");
616 return VOLSERDUMPERROR;
619 /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */
620 for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) {
621 if (nbytes < howMany)
624 /* Read the data - unless we know we can't */
625 n = (failed_seek ? 0 : FDH_READ(handleP, p, howMany));
627 /* If read any good data and we null padded previously, log the
628 * amount that we had null padded.
630 if ((n > 0) && pad) {
631 fprintf(stderr, "Null padding file %d bytes at offset %u\n", pad,
636 /* If didn't read enough data, null padd the rest of the buffer. This
637 * can happen if, for instance, the media has some bad spots. We don't
638 * want to quit the dump, so we start null padding.
642 if (verbose) fprintf(stderr, " read %u instead of %u bytes.\n", n, howMany);
644 /* Record the read error */
647 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
648 errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
651 fprintf(stderr, "Error reading inode %s for vnode %d\n",
652 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
655 /* Pad the rest of the buffer with zeros. Remember offset we started
656 * padding. Keep total tally of padding.
658 memset(p + n, 0, howMany - n);
660 offset = (howBig - nbytes) + n;
661 pad += (howMany - n);
663 /* Now seek over the data we could not get. An error here means we
664 * can't do the next read.
666 failed_seek = FDH_SEEK(handleP, ((size - nbytes) + howMany), SEEK_SET);
667 if (failed_seek != ((size - nbytes) + howMany)) {
668 if (failed_seek < 0) {
670 "Error %d seeking in inode %s for vnode %d\n",
671 errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
675 "Error seeking in inode %s for vnode %d\n",
676 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
684 /* Now write the data out */
685 if (write(dumpfd, (char *)p, howMany) != howMany)
686 failed_write = VOLSERDUMPERROR;
689 if (pad) { /* Any padding we hadn't reported yet */
690 fprintf(stderr, "Null padding file: %d bytes at offset %u\n", pad,
700 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
701 int dumpEverything, struct Volume *vp)
708 fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
710 if (!v || v->type == vNull)
713 code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
717 code = DumpByte(dumpfd, 't', (byte) v->type);
719 code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */
721 code = DumpInt32(dumpfd, 'v', v->dataVersion);
723 code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
725 code = DumpInt32(dumpfd, 'a', v->author);
727 code = DumpInt32(dumpfd, 'o', v->owner);
728 if (!code && v->group)
729 code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */
731 code = DumpShort(dumpfd, 'b', v->modeBits);
733 code = DumpInt32(dumpfd, 'p', v->parent);
735 code = DumpInt32(dumpfd, 's', v->serverModifyTime);
736 if (v->type == vDirectory) {
737 acl_HtonACL(VVnodeDiskACL(v));
740 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
744 if (VNDISK_GET_INO(v)) {
745 IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
749 "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
750 PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber, volid,
756 fprintf(stderr, "about to dump inode %s for vnode %u\n",
757 PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber);
758 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
765 fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
771 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
774 register int code = 0;
775 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
776 char buf[SIZEOF_LARGEDISKVNODE];
777 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
778 StreamHandle_t *file;
783 register int vnodeIndex, nVnodes = 0;
785 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
786 file = FDH_FDOPEN(fdP, "r+");
787 size = OS_SIZE(fdP->fd_fd);
788 nVnodes = (size / vcp->diskSize) - 1;
791 STREAM_SEEK(file, vcp->diskSize, 0);
795 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
796 nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
797 flag = forcedump || (vnode->serverModifyTime >= fromtime);
798 /* Note: the >= test is very important since some old volumes may not have
799 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
800 * does dump the file! */
802 fprintf(stderr, "about to dump %s vnode %u (vnode offset = %u)\n",
803 class == vSmall ? "vSmall" : "vLarge",
804 bitNumberToVnodeNumber(vnodeIndex, class), offset);
807 DumpVnode(dumpfd, vnode, V_id(vp),
808 bitNumberToVnodeNumber(vnodeIndex, class), flag,
818 /* A partial dump (no dump header) */
820 DumpPartial(int dumpfd, register Volume * vp, afs_int32 fromtime,
826 fprintf(stderr, "about to dump the volume header\n");
828 code = DumpVolumeHeader(dumpfd, vp);
831 fprintf(stderr, "about to dump the large vnode index\n");
833 code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
836 fprintf(stderr, "about to dump the small vnode index\n");
838 code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
845 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
854 afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
856 fprintf(stderr, "Failed to open dump file: %s. Exiting.\n",
857 afs_error_message(errno));
861 dumpfd = 1; /* stdout */
865 fprintf(stderr, "about to dump the dump header\n");
867 code = DumpDumpHeader(dumpfd, vp, fromtime);
870 fprintf(stderr, "about to dump volume contents\n");
872 code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
875 fprintf(stderr, "about to dump the dump postamble\n");
877 code = DumpEnd(dumpfd);
880 fprintf(stderr, "finished dump\n");
881 close(dumpfd); /* might be closing stdout, no harm */