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"
30 #include <rx/rx_queue.h>
31 #include <afs/afsint.h>
33 #include <afs/errors.h>
36 #include <afs/afssyscalls.h>
37 #include <afs/ihandle.h>
38 #include <afs/vnode.h>
39 #include <afs/volume.h>
40 #include <afs/partition.h>
41 #include <afs/viceinode.h>
42 #include <afs/afssyscalls.h>
45 #include <afs/com_err.h>
51 #define afs_putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
52 #define afs_putshort(p, v) *p++ = v>>8, *p++ = v
54 int VolumeChanged; /* needed by physio - leave alone */
57 /* Forward Declarations */
58 static void HandleVolume(struct DiskPartition64 *partP, char *name,
59 char *filename, int fromtime);
60 static Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
61 struct VolumeHeader *header);
62 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
63 char *dumpfile, int fromtime);
66 #include "AFS_component_version_number.c"
69 char name[VMAXPATHLEN];
73 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
77 code = IH_IREAD(ih, 0, to, size);
86 AttachVolume(struct DiskPartition64 * dp, char *volname,
87 struct VolumeHeader * header)
92 vp = (Volume *) calloc(1, sizeof(Volume));
93 vp->specialStatus = 0;
94 vp->device = dp->device;
96 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
97 header->largeVnodeIndex);
98 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
99 header->smallVnodeIndex);
100 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
102 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
103 vp->cacheCheck = 0; /* XXXX */
104 vp->shuttingDown = 0;
105 vp->goingOffline = 0;
107 vp->header = calloc(1, sizeof(*vp->header));
108 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
109 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
111 struct IndexFileHeader iHead;
112 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
113 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
116 struct IndexFileHeader iHead;
117 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
118 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
122 struct versionStamp stamp;
123 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
124 LINKTABLEMAGIC, LINKTABLEVERSION);
134 handleit(struct cmd_syndesc *as, void *arock)
138 afs_uint32 volumeId = 0;
140 char *fileName = NULL;
141 struct DiskPartition64 *partP = NULL;
143 char tmpPartName[20];
150 if (geteuid() != 0) {
151 fprintf(stderr, "voldump must be run as root; sorry\n");
157 if ((ti = as->parms[0].items))
159 if ((ti = as->parms[1].items))
160 volumeId = (afs_uint32)atoi(ti->data);
161 if ((ti = as->parms[2].items))
163 if ((ti = as->parms[3].items))
165 if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
166 code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
168 fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
169 as->parms[4].items->data, code);
176 err = VAttachPartitions();
178 fprintf(stderr, "%d partitions had errors during attach.\n", err);
182 if (strlen(partName) == 1) {
183 if (partName[0] >= 'a' && partName[0] <= 'z') {
184 strcpy(tmpPartName, "/vicepa");
185 tmpPartName[6] = partName[0];
186 partP = VGetPartition(tmpPartName, 0);
189 partP = VGetPartition(partName, 0);
193 "%s is not an AFS partition name on this server.\n",
200 fprintf(stderr, "Must specify volume id!\n");
205 fprintf(stderr, "must specify vice partition.\n");
209 snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
210 HandleVolume(partP, name1, fileName, fromtime);
215 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
217 struct VolumeHeader header;
218 struct VolumeDiskHeader diskHeader;
219 struct afs_stat status;
222 char headerName[1024];
226 snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
227 VPartitionPath(dp), name);
228 if ((fd = afs_open(headerName, O_RDONLY)) == -1
229 || afs_fstat(fd, &status) == -1) {
230 fprintf(stderr, "Cannot read volume header %s\n", name);
234 n = read(fd, &diskHeader, sizeof(diskHeader));
236 if (n != sizeof(diskHeader)
237 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
238 fprintf(stderr, "Error reading volume header %s\n", name);
241 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
243 "Volume %s, version number is incorrect; volume needs salvage\n",
247 DiskToVolumeHeader(&header, &diskHeader);
250 vp = AttachVolume(dp, name, &header);
252 fprintf(stderr, "Error attaching volume header %s\n", name);
256 DoMyVolDump(vp, dp, filename, fromtime);
263 main(int argc, char **argv)
265 struct cmd_syndesc *ts;
267 VolumePackageOptions opts;
269 VOptDefaults(volumeUtility, &opts);
270 if (VInitVolumePackage2(volumeUtility, &opts)) {
271 fprintf(stderr, "errors encountered initializing volume package, but "
272 "trying to continue anyway\n");
275 ts = cmd_CreateSyntax(NULL, handleit, NULL, 0,
276 "Dump a volume to a 'vos dump' format file without using volserver");
277 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL, "AFS partition name");
278 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
279 cmd_AddParm(ts, "-file", CMD_LIST, CMD_OPTIONAL, "Dump filename");
280 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
281 "Trace dump progress (very verbose)");
282 cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
283 code = cmd_Dispatch(argc, argv);
291 DumpDouble(int dumpfd, char tag, afs_uint32 value1,
296 byte *p = (unsigned char *)tbuffer;
298 afs_putint32(p, value1);
299 afs_putint32(p, value2);
301 res = write(dumpfd, tbuffer, 9);
302 return ((res == 9) ? 0 : VOLSERDUMPERROR);
306 DumpInt32(int dumpfd, char tag, afs_uint32 value)
309 byte *p = (unsigned char *)tbuffer;
311 afs_putint32(p, value);
312 return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
316 DumpString(int dumpfd, char tag, char *s)
320 code = write(dumpfd, &tag, 1);
322 return VOLSERDUMPERROR;
324 code = write(dumpfd, s, n);
326 return VOLSERDUMPERROR;
332 DumpArrayInt32(int dumpfd, char tag, afs_uint32 * array,
338 byte *p = (unsigned char *)tbuffer;
340 afs_putshort(p, nelem);
341 code = write(dumpfd, tbuffer, 3);
343 return VOLSERDUMPERROR;
345 p = (unsigned char *)tbuffer;
346 v = *array++; /*this was register */
349 code = write(dumpfd, tbuffer, 4);
351 return VOLSERDUMPERROR;
360 DumpDumpHeader(int dumpfd, Volume * vp, afs_int32 fromtime)
363 afs_int32 dumpTimes[2];
366 fprintf(stderr, "dumping dump header\n");
369 code = DumpDouble(dumpfd, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
372 code = DumpInt32(dumpfd, 'v', V_id(vp));
375 code = DumpString(dumpfd, 'n', V_name(vp));
377 dumpTimes[0] = fromtime;
378 switch (V_type(vp)) {
379 case readwriteVolume:
380 dumpTimes[1] = V_updateDate(vp); /* until last update */
383 dumpTimes[1] = V_copyDate(vp); /* until clone was made */
386 dumpTimes[1] = V_backupDate(vp); /* until backup was made */
392 code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2);
401 return (DumpInt32(dumpfd, D_DUMPEND, DUMPENDMAGIC));
405 DumpByte(int dumpfd, char tag, byte value)
408 byte *p = (unsigned char *)tbuffer;
411 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
415 DumpTag(int dumpfd, int tag)
420 return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
425 DumpBool(int dumpfd, char tag, unsigned int value)
428 byte *p = (unsigned char *)tbuffer;
431 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
437 DumpVolumeHeader(int dumpfd, Volume * vp)
442 fprintf(stderr, "dumping volume header\n");
445 code = DumpTag(dumpfd, D_VOLUMEHEADER);
447 code = DumpInt32(dumpfd, 'i', V_id(vp));
449 code = DumpInt32(dumpfd, 'v', V_stamp(vp).version);
451 code = DumpString(dumpfd, 'n', V_name(vp));
453 code = DumpBool(dumpfd, 's', V_inService(vp));
455 code = DumpBool(dumpfd, 'b', V_blessed(vp));
457 code = DumpInt32(dumpfd, 'u', V_uniquifier(vp));
459 code = DumpByte(dumpfd, 't', (byte) V_type(vp));
461 code = DumpInt32(dumpfd, 'p', V_parentId(vp));
463 code = DumpInt32(dumpfd, 'c', V_cloneId(vp));
465 code = DumpInt32(dumpfd, 'q', V_maxquota(vp));
467 code = DumpInt32(dumpfd, 'm', V_minquota(vp));
469 code = DumpInt32(dumpfd, 'd', V_diskused(vp));
471 code = DumpInt32(dumpfd, 'f', V_filecount(vp));
473 code = DumpInt32(dumpfd, 'a', V_accountNumber(vp));
475 code = DumpInt32(dumpfd, 'o', V_owner(vp));
477 code = DumpInt32(dumpfd, 'C', V_creationDate(vp)); /* Rw volume creation date */
479 code = DumpInt32(dumpfd, 'A', V_accessDate(vp));
481 code = DumpInt32(dumpfd, 'U', V_updateDate(vp));
483 code = DumpInt32(dumpfd, 'E', V_expirationDate(vp));
485 code = DumpInt32(dumpfd, 'B', V_backupDate(vp)); /* Rw volume backup clone date */
487 code = DumpString(dumpfd, 'O', V_offlineMessage(vp));
490 * We do NOT dump the detailed volume statistics residing in the old
491 * motd field, since we cannot tell from the info in a dump whether
492 * statistics data has been put there. Instead, we dump a null string,
493 * just as if that was what the motd contained.
496 code = DumpString(dumpfd, 'M', "");
499 DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp),
500 sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
502 code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp));
504 code = DumpInt32(dumpfd, 'Z', V_dayUse(vp));
509 DumpShort(int dumpfd, char tag, unsigned int value)
512 byte *p = (unsigned char *)tbuffer;
516 return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
520 DumpByteString(int dumpfd, char tag, byte * bs, int nbytes)
524 code = write(dumpfd, &tag, 1);
526 return VOLSERDUMPERROR;
527 code = write(dumpfd, (char *)bs, nbytes);
529 return VOLSERDUMPERROR;
535 DumpFile(int dumpfd, int vnode, FdHandle_t * handleP, struct VnodeDiskObject *v)
537 int code = 0, failed_seek = 0, failed_write = 0;
539 afs_foff_t offset = 0;
540 afs_sfsize_t nbytes, howBig;
543 afs_foff_t howFar = 0;
548 struct afs_stat status;
550 LARGE_INTEGER fileSize;
554 #include <sys/statfs.h>
555 struct statfs tstatfs;
559 fprintf(stderr, "dumping file for vnode %d\n", vnode);
562 if (!GetFileSizeEx(handleP->fd_fd, &fileSize)) {
563 Log("DumpFile: GetFileSizeEx returned error code %d on descriptor %d\n", GetLastError(), handleP->fd_fd);
564 return VOLSERDUMPERROR;
566 howBig = fileSize.QuadPart;
570 afs_fstat(handleP->fd_fd, &status);
571 howBig = status.st_size;
574 /* Unfortunately in AIX valuable fields such as st_blksize are
575 * gone from the stat structure.
577 fstatfs(handleP->fd_fd, &tstatfs);
578 howMany = tstatfs.f_bsize;
580 howMany = status.st_blksize;
581 #endif /* AFS_AIX_ENV */
582 #endif /* AFS_NT40_ENV */
585 size = FDH_SIZE(handleP);
588 fprintf(stderr, " howBig = %u, howMany = %u, fdh size = %u\n",
589 (unsigned int) howBig, (unsigned int) howMany,
590 (unsigned int) size);
592 SplitInt64(size, hi, lo);
594 code = DumpInt32(dumpfd, 'f', lo);
596 code = DumpDouble(dumpfd, 'h', hi, lo);
600 return VOLSERDUMPERROR;
605 fprintf(stderr, "out of memory!\n");
606 return VOLSERDUMPERROR;
609 /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */
610 for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) {
611 if (nbytes < howMany)
614 /* Read the data - unless we know we can't */
615 n = (failed_seek ? 0 : FDH_PREAD(handleP, p, howMany, howFar));
618 /* If read any good data and we null padded previously, log the
619 * amount that we had null padded.
621 if ((n > 0) && pad) {
622 fprintf(stderr, "Null padding file %d bytes at offset %lld\n", pad,
627 /* If didn't read enough data, null padd the rest of the buffer. This
628 * can happen if, for instance, the media has some bad spots. We don't
629 * want to quit the dump, so we start null padding.
633 if (verbose) fprintf(stderr, " read %u instead of %u bytes.\n", (unsigned)n, (unsigned)howMany);
635 /* Record the read error */
638 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
639 errno, PrintInode(stmp, handleP->fd_ih->ih_ino),
642 fprintf(stderr, "Error reading inode %s for vnode %d\n",
643 PrintInode(stmp, handleP->fd_ih->ih_ino), vnode);
646 /* Pad the rest of the buffer with zeros. Remember offset we started
647 * padding. Keep total tally of padding.
649 memset(p + n, 0, howMany - n);
651 offset = (howBig - nbytes) + n;
652 pad += (howMany - n);
654 /* Now seek over the data we could not get. An error here means we
655 * can't do the next read.
657 howFar = ((size - nbytes) + howMany);
660 /* Now write the data out */
661 if (write(dumpfd, (char *)p, howMany) != howMany)
662 failed_write = VOLSERDUMPERROR;
665 if (pad) { /* Any padding we hadn't reported yet */
666 fprintf(stderr, "Null padding file: %d bytes at offset %lld\n", pad,
676 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
677 int dumpEverything, struct Volume *vp)
685 fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
687 if (!v || v->type == vNull)
690 code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
694 code = DumpByte(dumpfd, 't', (byte) v->type);
696 code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */
698 code = DumpInt32(dumpfd, 'v', v->dataVersion);
700 code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
702 code = DumpInt32(dumpfd, 'a', v->author);
704 code = DumpInt32(dumpfd, 'o', v->owner);
705 if (!code && v->group)
706 code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */
708 code = DumpShort(dumpfd, 'b', v->modeBits);
710 code = DumpInt32(dumpfd, 'p', v->parent);
712 code = DumpInt32(dumpfd, 's', v->serverModifyTime);
713 if (v->type == vDirectory) {
714 acl_HtonACL(VVnodeDiskACL(v));
717 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
721 if (VNDISK_GET_INO(v)) {
722 IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
726 "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
727 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber, volid,
733 fprintf(stderr, "about to dump inode %s for vnode %u\n",
734 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber);
735 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
742 fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
748 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
752 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
753 char buf[SIZEOF_LARGEDISKVNODE];
754 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
755 StreamHandle_t *file;
759 afs_foff_t offset = 0;
760 int vnodeIndex, nVnodes = 0;
762 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
763 file = FDH_FDOPEN(fdP, "r+");
764 size = OS_SIZE(fdP->fd_fd);
765 nVnodes = (size / vcp->diskSize) - 1;
768 STREAM_ASEEK(file, vcp->diskSize);
772 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
773 nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
774 flag = forcedump || (vnode->serverModifyTime >= fromtime);
775 /* Note: the >= test is very important since some old volumes may not have
776 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
777 * does dump the file! */
779 fprintf(stderr, "about to dump %s vnode %u (vnode offset = %lld)\n",
780 class == vSmall ? "vSmall" : "vLarge",
781 bitNumberToVnodeNumber(vnodeIndex, class), (long long)offset);
784 DumpVnode(dumpfd, vnode, V_id(vp),
785 bitNumberToVnodeNumber(vnodeIndex, class), flag,
795 /* A partial dump (no dump header) */
797 DumpPartial(int dumpfd, Volume * vp, afs_int32 fromtime,
803 fprintf(stderr, "about to dump the volume header\n");
805 code = DumpVolumeHeader(dumpfd, vp);
808 fprintf(stderr, "about to dump the large vnode index\n");
810 code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
813 fprintf(stderr, "about to dump the small vnode index\n");
815 code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
822 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
831 afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
833 fprintf(stderr, "Failed to open dump file: %s. Exiting.\n",
834 afs_error_message(errno));
838 dumpfd = 1; /* stdout */
842 fprintf(stderr, "about to dump the dump header\n");
844 code = DumpDumpHeader(dumpfd, vp, fromtime);
847 fprintf(stderr, "about to dump volume contents\n");
849 code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
852 fprintf(stderr, "about to dump the dump postamble\n");
854 code = DumpEnd(dumpfd);
857 fprintf(stderr, "finished dump\n");
858 close(dumpfd); /* might be closing stdout, no harm */