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 <afs/afsint.h>
32 #include <afs/errors.h>
35 #include <afs/afssyscalls.h>
36 #include <afs/ihandle.h>
37 #include <afs/vnode.h>
38 #include <afs/volume.h>
39 #include <afs/partition.h>
40 #include <afs/viceinode.h>
41 #include <afs/afssyscalls.h>
44 #include <afs/com_err.h>
50 #define afs_putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
51 #define afs_putshort(p, v) *p++ = v>>8, *p++ = v
53 int VolumeChanged; /* needed by physio - leave alone */
56 /* Forward Declarations */
57 void HandleVolume(struct DiskPartition64 *partP, char *name, char *filename, int fromtime);
58 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
59 struct VolumeHeader *header);
60 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
61 char *dumpfile, int fromtime);
64 #include "AFS_component_version_number.c"
67 char name[VMAXPATHLEN];
71 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
75 code = IH_IREAD(ih, 0, to, size);
84 AttachVolume(struct DiskPartition64 * dp, char *volname,
85 struct VolumeHeader * header)
90 vp = (Volume *) calloc(1, sizeof(Volume));
91 vp->specialStatus = 0;
92 vp->device = dp->device;
94 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
95 header->largeVnodeIndex);
96 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
97 header->smallVnodeIndex);
98 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
100 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
101 vp->cacheCheck = 0; /* XXXX */
102 vp->shuttingDown = 0;
103 vp->goingOffline = 0;
105 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
106 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
107 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
109 struct IndexFileHeader iHead;
110 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
111 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
114 struct IndexFileHeader iHead;
115 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
116 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
120 struct versionStamp stamp;
121 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
122 LINKTABLEMAGIC, LINKTABLEVERSION);
132 handleit(struct cmd_syndesc *as, void *arock)
136 afs_uint32 volumeId = 0;
138 char *fileName = NULL;
139 struct DiskPartition64 *partP = NULL;
141 char tmpPartName[20];
148 if (geteuid() != 0) {
149 fprintf(stderr, "voldump must be run as root; sorry\n");
155 if ((ti = as->parms[0].items))
157 if ((ti = as->parms[1].items))
158 volumeId = (afs_uint32)atoi(ti->data);
159 if ((ti = as->parms[2].items))
161 if ((ti = as->parms[3].items))
163 if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
164 code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
166 fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
167 as->parms[4].items->data, code);
174 err = VAttachPartitions();
176 fprintf(stderr, "%d partitions had errors during attach.\n", err);
180 if (strlen(partName) == 1) {
181 if (partName[0] >= 'a' && partName[0] <= 'z') {
182 strcpy(tmpPartName, "/vicepa");
183 tmpPartName[6] = partName[0];
184 partP = VGetPartition(tmpPartName, 0);
187 partP = VGetPartition(partName, 0);
191 "%s is not an AFS partition name on this server.\n",
198 fprintf(stderr, "Must specify volume id!\n");
203 fprintf(stderr, "must specify vice partition.\n");
207 snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
208 HandleVolume(partP, name1, fileName, fromtime);
213 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
215 struct VolumeHeader header;
216 struct VolumeDiskHeader diskHeader;
217 struct afs_stat status;
220 char headerName[1024];
224 snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
225 VPartitionPath(dp), name);
226 if ((fd = afs_open(headerName, O_RDONLY)) == -1
227 || afs_fstat(fd, &status) == -1) {
228 fprintf(stderr, "Cannot read volume header %s\n", name);
232 n = read(fd, &diskHeader, sizeof(diskHeader));
234 if (n != sizeof(diskHeader)
235 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
236 fprintf(stderr, "Error reading volume header %s\n", name);
239 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
241 "Volume %s, version number is incorrect; volume needs salvage\n",
245 DiskToVolumeHeader(&header, &diskHeader);
248 vp = AttachVolume(dp, name, &header);
250 fprintf(stderr, "Error attaching volume header %s\n", name);
254 DoMyVolDump(vp, dp, filename, fromtime);
259 main(int argc, char **argv)
261 struct cmd_syndesc *ts;
263 VolumePackageOptions opts;
265 VOptDefaults(volumeUtility, &opts);
266 if (VInitVolumePackage2(volumeUtility, &opts)) {
267 fprintf(stderr, "errors encountered initializing volume package, but "
268 "trying to continue anyway\n");
271 ts = cmd_CreateSyntax(NULL, handleit, NULL,
272 "Dump a volume to a 'vos dump' format file without using volserver");
273 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL, "AFS partition name");
274 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
275 cmd_AddParm(ts, "-file", CMD_LIST, CMD_OPTIONAL, "Dump filename");
276 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
277 "Trace dump progress (very verbose)");
278 cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
279 code = cmd_Dispatch(argc, argv);
287 DumpDouble(int dumpfd, char tag, afs_uint32 value1,
292 byte *p = (unsigned char *)tbuffer;
294 afs_putint32(p, value1);
295 afs_putint32(p, value2);
297 res = write(dumpfd, tbuffer, 9);
298 return ((res == 9) ? 0 : VOLSERDUMPERROR);
302 DumpInt32(int dumpfd, char tag, afs_uint32 value)
305 byte *p = (unsigned char *)tbuffer;
307 afs_putint32(p, value);
308 return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
312 DumpString(int dumpfd, char tag, char *s)
316 code = write(dumpfd, &tag, 1);
318 return VOLSERDUMPERROR;
320 code = write(dumpfd, s, n);
322 return VOLSERDUMPERROR;
328 DumpArrayInt32(int dumpfd, char tag, afs_uint32 * array,
334 byte *p = (unsigned char *)tbuffer;
336 afs_putshort(p, nelem);
337 code = write(dumpfd, tbuffer, 3);
339 return VOLSERDUMPERROR;
341 p = (unsigned char *)tbuffer;
342 v = *array++; /*this was register */
345 code = write(dumpfd, tbuffer, 4);
347 return VOLSERDUMPERROR;
356 DumpDumpHeader(int dumpfd, Volume * vp, afs_int32 fromtime)
359 afs_int32 dumpTimes[2];
362 fprintf(stderr, "dumping dump header\n");
365 code = DumpDouble(dumpfd, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
368 code = DumpInt32(dumpfd, 'v', V_id(vp));
371 code = DumpString(dumpfd, 'n', V_name(vp));
373 dumpTimes[0] = fromtime;
374 dumpTimes[1] = V_backupDate(vp); /* Until the time the clone was made */
376 code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2);
385 return (DumpInt32(dumpfd, D_DUMPEND, DUMPENDMAGIC));
389 DumpByte(int dumpfd, char tag, byte value)
392 byte *p = (unsigned char *)tbuffer;
395 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
399 DumpTag(int dumpfd, int tag)
404 return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
409 DumpBool(int dumpfd, char tag, unsigned int value)
412 byte *p = (unsigned char *)tbuffer;
415 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
421 DumpVolumeHeader(int dumpfd, Volume * vp)
426 fprintf(stderr, "dumping volume header\n");
429 code = DumpTag(dumpfd, D_VOLUMEHEADER);
431 code = DumpInt32(dumpfd, 'i', V_id(vp));
433 code = DumpInt32(dumpfd, 'v', V_stamp(vp).version);
435 code = DumpString(dumpfd, 'n', V_name(vp));
437 code = DumpBool(dumpfd, 's', V_inService(vp));
439 code = DumpBool(dumpfd, 'b', V_blessed(vp));
441 code = DumpInt32(dumpfd, 'u', V_uniquifier(vp));
443 code = DumpByte(dumpfd, 't', (byte) V_type(vp));
445 code = DumpInt32(dumpfd, 'p', V_parentId(vp));
447 code = DumpInt32(dumpfd, 'c', V_cloneId(vp));
449 code = DumpInt32(dumpfd, 'q', V_maxquota(vp));
451 code = DumpInt32(dumpfd, 'm', V_minquota(vp));
453 code = DumpInt32(dumpfd, 'd', V_diskused(vp));
455 code = DumpInt32(dumpfd, 'f', V_filecount(vp));
457 code = DumpInt32(dumpfd, 'a', V_accountNumber(vp));
459 code = DumpInt32(dumpfd, 'o', V_owner(vp));
461 code = DumpInt32(dumpfd, 'C', V_creationDate(vp)); /* Rw volume creation date */
463 code = DumpInt32(dumpfd, 'A', V_accessDate(vp));
465 code = DumpInt32(dumpfd, 'U', V_updateDate(vp));
467 code = DumpInt32(dumpfd, 'E', V_expirationDate(vp));
469 code = DumpInt32(dumpfd, 'B', V_backupDate(vp)); /* Rw volume backup clone date */
471 code = DumpString(dumpfd, 'O', V_offlineMessage(vp));
474 * We do NOT dump the detailed volume statistics residing in the old
475 * motd field, since we cannot tell from the info in a dump whether
476 * statistics data has been put there. Instead, we dump a null string,
477 * just as if that was what the motd contained.
480 code = DumpString(dumpfd, 'M', "");
483 DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp),
484 sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
486 code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp));
488 code = DumpInt32(dumpfd, 'Z', V_dayUse(vp));
493 DumpShort(int dumpfd, char tag, unsigned int value)
496 byte *p = (unsigned char *)tbuffer;
500 return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
504 DumpByteString(int dumpfd, char tag, byte * bs, int nbytes)
508 code = write(dumpfd, &tag, 1);
510 return VOLSERDUMPERROR;
511 code = write(dumpfd, (char *)bs, nbytes);
513 return VOLSERDUMPERROR;
519 DumpFile(int dumpfd, int vnode, FdHandle_t * handleP, struct VnodeDiskObject *v)
521 int code = 0, failed_seek = 0, failed_write = 0;
523 afs_foff_t offset = 0;
524 afs_sfsize_t nbytes, howBig;
527 afs_foff_t howFar = 0;
532 struct afs_stat status;
534 LARGE_INTEGER fileSize;
538 #include <sys/statfs.h>
539 struct statfs tstatfs;
543 fprintf(stderr, "dumping file for vnode %d\n", vnode);
546 if (!GetFileSizeEx(handleP->fd_fd, &fileSize)) {
547 Log("DumpFile: GetFileSizeEx returned error code %d on descriptor %d\n", GetLastError(), handleP->fd_fd);
548 return VOLSERDUMPERROR;
550 howBig = fileSize.QuadPart;
554 afs_fstat(handleP->fd_fd, &status);
555 howBig = status.st_size;
558 /* Unfortunately in AIX valuable fields such as st_blksize are
559 * gone from the stat structure.
561 fstatfs(handleP->fd_fd, &tstatfs);
562 howMany = tstatfs.f_bsize;
564 howMany = status.st_blksize;
565 #endif /* AFS_AIX_ENV */
566 #endif /* AFS_NT40_ENV */
569 size = FDH_SIZE(handleP);
572 fprintf(stderr, " howBig = %u, howMany = %u, fdh size = %u\n",
573 (unsigned int) howBig, (unsigned int) howMany,
574 (unsigned int) size);
576 SplitInt64(size, hi, lo);
578 code = DumpInt32(dumpfd, 'f', lo);
580 code = DumpDouble(dumpfd, 'h', hi, lo);
584 return VOLSERDUMPERROR;
587 p = (unsigned char *)malloc(howMany);
589 fprintf(stderr, "out of memory!\n");
590 return VOLSERDUMPERROR;
593 /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */
594 for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) {
595 if (nbytes < howMany)
598 /* Read the data - unless we know we can't */
599 n = (failed_seek ? 0 : FDH_PREAD(handleP, p, howMany, howFar));
602 /* If read any good data and we null padded previously, log the
603 * amount that we had null padded.
605 if ((n > 0) && pad) {
606 fprintf(stderr, "Null padding file %d bytes at offset %lld\n", pad,
611 /* If didn't read enough data, null padd the rest of the buffer. This
612 * can happen if, for instance, the media has some bad spots. We don't
613 * want to quit the dump, so we start null padding.
617 if (verbose) fprintf(stderr, " read %u instead of %u bytes.\n", (unsigned)n, (unsigned)howMany);
619 /* Record the read error */
622 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
623 errno, PrintInode(stmp, handleP->fd_ih->ih_ino),
626 fprintf(stderr, "Error reading inode %s for vnode %d\n",
627 PrintInode(stmp, handleP->fd_ih->ih_ino), vnode);
630 /* Pad the rest of the buffer with zeros. Remember offset we started
631 * padding. Keep total tally of padding.
633 memset(p + n, 0, howMany - n);
635 offset = (howBig - nbytes) + n;
636 pad += (howMany - n);
638 /* Now seek over the data we could not get. An error here means we
639 * can't do the next read.
641 howFar = ((size - nbytes) + howMany);
644 /* Now write the data out */
645 if (write(dumpfd, (char *)p, howMany) != howMany)
646 failed_write = VOLSERDUMPERROR;
649 if (pad) { /* Any padding we hadn't reported yet */
650 fprintf(stderr, "Null padding file: %d bytes at offset %lld\n", pad,
660 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
661 int dumpEverything, struct Volume *vp)
669 fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
671 if (!v || v->type == vNull)
674 code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
678 code = DumpByte(dumpfd, 't', (byte) v->type);
680 code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */
682 code = DumpInt32(dumpfd, 'v', v->dataVersion);
684 code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
686 code = DumpInt32(dumpfd, 'a', v->author);
688 code = DumpInt32(dumpfd, 'o', v->owner);
689 if (!code && v->group)
690 code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */
692 code = DumpShort(dumpfd, 'b', v->modeBits);
694 code = DumpInt32(dumpfd, 'p', v->parent);
696 code = DumpInt32(dumpfd, 's', v->serverModifyTime);
697 if (v->type == vDirectory) {
698 acl_HtonACL(VVnodeDiskACL(v));
701 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
705 if (VNDISK_GET_INO(v)) {
706 IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
710 "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
711 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber, volid,
717 fprintf(stderr, "about to dump inode %s for vnode %u\n",
718 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber);
719 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
726 fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
732 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
736 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
737 char buf[SIZEOF_LARGEDISKVNODE];
738 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
739 StreamHandle_t *file;
743 afs_foff_t offset = 0;
744 int vnodeIndex, nVnodes = 0;
746 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
747 file = FDH_FDOPEN(fdP, "r+");
748 size = OS_SIZE(fdP->fd_fd);
749 nVnodes = (size / vcp->diskSize) - 1;
752 STREAM_ASEEK(file, vcp->diskSize);
756 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
757 nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
758 flag = forcedump || (vnode->serverModifyTime >= fromtime);
759 /* Note: the >= test is very important since some old volumes may not have
760 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
761 * does dump the file! */
763 fprintf(stderr, "about to dump %s vnode %u (vnode offset = %lld)\n",
764 class == vSmall ? "vSmall" : "vLarge",
765 bitNumberToVnodeNumber(vnodeIndex, class), (long long)offset);
768 DumpVnode(dumpfd, vnode, V_id(vp),
769 bitNumberToVnodeNumber(vnodeIndex, class), flag,
779 /* A partial dump (no dump header) */
781 DumpPartial(int dumpfd, Volume * vp, afs_int32 fromtime,
787 fprintf(stderr, "about to dump the volume header\n");
789 code = DumpVolumeHeader(dumpfd, vp);
792 fprintf(stderr, "about to dump the large vnode index\n");
794 code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
797 fprintf(stderr, "about to dump the small vnode index\n");
799 code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
806 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
815 afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
817 fprintf(stderr, "Failed to open dump file: %s. Exiting.\n",
818 afs_error_message(errno));
822 dumpfd = 1; /* stdout */
826 fprintf(stderr, "about to dump the dump header\n");
828 code = DumpDumpHeader(dumpfd, vp, fromtime);
831 fprintf(stderr, "about to dump volume contents\n");
833 code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
836 fprintf(stderr, "about to dump the dump postamble\n");
838 code = DumpEnd(dumpfd);
841 fprintf(stderr, "finished dump\n");
842 close(dumpfd); /* might be closing stdout, no harm */