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"
36 #include <sys/param.h>
43 #include <afs/afsint.h>
45 #include <afs/errors.h>
48 #include <afs/afssyscalls.h>
49 #include <afs/ihandle.h>
50 #include <afs/vnode.h>
51 #include <afs/volume.h>
52 #include <afs/partition.h>
53 #include <afs/viceinode.h>
54 #include <afs/afssyscalls.h>
57 #include <afs/com_err.h>
81 #define afs_putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
82 #define afs_putshort(p, v) *p++ = v>>8, *p++ = v
84 int VolumeChanged; /* needed by physio - leave alone */
87 /* Forward Declarations */
88 void HandleVolume(struct DiskPartition64 *partP, char *name, char *filename, int fromtime);
89 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
90 struct VolumeHeader *header);
91 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
92 char *dumpfile, int fromtime);
95 #include "AFS_component_version_number.c"
98 char name[VMAXPATHLEN];
102 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
106 code = IH_IREAD(ih, 0, to, size);
115 AttachVolume(struct DiskPartition64 * dp, char *volname,
116 struct VolumeHeader * header)
121 vp = (Volume *) calloc(1, sizeof(Volume));
122 vp->specialStatus = 0;
123 vp->device = dp->device;
125 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
126 header->largeVnodeIndex);
127 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
128 header->smallVnodeIndex);
129 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
131 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
132 vp->cacheCheck = 0; /* XXXX */
133 vp->shuttingDown = 0;
134 vp->goingOffline = 0;
136 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
137 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
138 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
140 struct IndexFileHeader iHead;
141 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
142 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
145 struct IndexFileHeader iHead;
146 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
147 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
151 struct versionStamp stamp;
152 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
153 LINKTABLEMAGIC, LINKTABLEVERSION);
163 handleit(struct cmd_syndesc *as, void *arock)
167 afs_uint32 volumeId = 0;
169 char *fileName = NULL;
170 struct DiskPartition64 *partP = NULL;
172 char tmpPartName[20];
179 if (geteuid() != 0) {
180 fprintf(stderr, "voldump must be run as root; sorry\n");
186 if ((ti = as->parms[0].items))
188 if ((ti = as->parms[1].items))
189 volumeId = (afs_uint32)atoi(ti->data);
190 if ((ti = as->parms[2].items))
192 if ((ti = as->parms[3].items))
194 if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
195 code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
197 fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
198 as->parms[4].items->data, code);
205 err = VAttachPartitions();
207 fprintf(stderr, "%d partitions had errors during attach.\n", err);
211 if (strlen(partName) == 1) {
212 if (partName[0] >= 'a' && partName[0] <= 'z') {
213 strcpy(tmpPartName, "/vicepa");
214 tmpPartName[6] = partName[0];
215 partP = VGetPartition(tmpPartName, 0);
218 partP = VGetPartition(partName, 0);
222 "%s is not an AFS partition name on this server.\n",
229 fprintf(stderr, "Must specify volume id!\n");
234 fprintf(stderr, "must specify vice partition.\n");
238 snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
239 HandleVolume(partP, name1, fileName, fromtime);
244 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
246 struct VolumeHeader header;
247 struct VolumeDiskHeader diskHeader;
248 struct afs_stat status;
251 char headerName[1024];
255 snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
256 VPartitionPath(dp), name);
257 if ((fd = afs_open(headerName, O_RDONLY)) == -1
258 || afs_fstat(fd, &status) == -1) {
259 fprintf(stderr, "Cannot read volume header %s\n", name);
263 n = read(fd, &diskHeader, sizeof(diskHeader));
265 if (n != sizeof(diskHeader)
266 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
267 fprintf(stderr, "Error reading volume header %s\n", name);
270 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
272 "Volume %s, version number is incorrect; volume needs salvage\n",
276 DiskToVolumeHeader(&header, &diskHeader);
279 vp = AttachVolume(dp, name, &header);
281 fprintf(stderr, "Error attaching volume header %s\n", name);
285 DoMyVolDump(vp, dp, filename, fromtime);
290 main(int argc, char **argv)
292 struct cmd_syndesc *ts;
294 VolumePackageOptions opts;
296 VOptDefaults(volumeUtility, &opts);
297 if (VInitVolumePackage2(volumeUtility, &opts)) {
298 fprintf(stderr, "errors encountered initializing volume package, but "
299 "trying to continue anyway\n");
302 ts = cmd_CreateSyntax(NULL, handleit, NULL,
303 "Dump a volume to a 'vos dump' format file without using volserver");
304 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL, "AFS partition name");
305 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
306 cmd_AddParm(ts, "-file", CMD_LIST, CMD_OPTIONAL, "Dump filename");
307 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
308 "Trace dump progress (very verbose)");
309 cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
310 code = cmd_Dispatch(argc, argv);
318 DumpDouble(int dumpfd, char tag, afs_uint32 value1,
323 byte *p = (unsigned char *)tbuffer;
325 afs_putint32(p, value1);
326 afs_putint32(p, value2);
328 res = write(dumpfd, tbuffer, 9);
329 return ((res == 9) ? 0 : VOLSERDUMPERROR);
333 DumpInt32(int dumpfd, char tag, afs_uint32 value)
336 byte *p = (unsigned char *)tbuffer;
338 afs_putint32(p, value);
339 return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
343 DumpString(int dumpfd, char tag, char *s)
347 code = write(dumpfd, &tag, 1);
349 return VOLSERDUMPERROR;
351 code = write(dumpfd, s, n);
353 return VOLSERDUMPERROR;
359 DumpArrayInt32(int dumpfd, char tag, afs_uint32 * array,
365 byte *p = (unsigned char *)tbuffer;
367 afs_putshort(p, nelem);
368 code = write(dumpfd, tbuffer, 3);
370 return VOLSERDUMPERROR;
372 p = (unsigned char *)tbuffer;
373 v = *array++; /*this was register */
376 code = write(dumpfd, tbuffer, 4);
378 return VOLSERDUMPERROR;
387 DumpDumpHeader(int dumpfd, Volume * vp, afs_int32 fromtime)
390 afs_int32 dumpTimes[2];
393 fprintf(stderr, "dumping dump header\n");
396 code = DumpDouble(dumpfd, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
399 code = DumpInt32(dumpfd, 'v', V_id(vp));
402 code = DumpString(dumpfd, 'n', V_name(vp));
404 dumpTimes[0] = fromtime;
405 dumpTimes[1] = V_backupDate(vp); /* Until the time the clone was made */
407 code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2);
416 return (DumpInt32(dumpfd, D_DUMPEND, DUMPENDMAGIC));
420 DumpByte(int dumpfd, char tag, byte value)
423 byte *p = (unsigned char *)tbuffer;
426 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
430 DumpTag(int dumpfd, int tag)
435 return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
440 DumpBool(int dumpfd, char tag, unsigned int value)
443 byte *p = (unsigned char *)tbuffer;
446 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
452 DumpVolumeHeader(int dumpfd, Volume * vp)
457 fprintf(stderr, "dumping volume header\n");
460 code = DumpTag(dumpfd, D_VOLUMEHEADER);
462 code = DumpInt32(dumpfd, 'i', V_id(vp));
464 code = DumpInt32(dumpfd, 'v', V_stamp(vp).version);
466 code = DumpString(dumpfd, 'n', V_name(vp));
468 code = DumpBool(dumpfd, 's', V_inService(vp));
470 code = DumpBool(dumpfd, 'b', V_blessed(vp));
472 code = DumpInt32(dumpfd, 'u', V_uniquifier(vp));
474 code = DumpByte(dumpfd, 't', (byte) V_type(vp));
476 code = DumpInt32(dumpfd, 'p', V_parentId(vp));
478 code = DumpInt32(dumpfd, 'c', V_cloneId(vp));
480 code = DumpInt32(dumpfd, 'q', V_maxquota(vp));
482 code = DumpInt32(dumpfd, 'm', V_minquota(vp));
484 code = DumpInt32(dumpfd, 'd', V_diskused(vp));
486 code = DumpInt32(dumpfd, 'f', V_filecount(vp));
488 code = DumpInt32(dumpfd, 'a', V_accountNumber(vp));
490 code = DumpInt32(dumpfd, 'o', V_owner(vp));
492 code = DumpInt32(dumpfd, 'C', V_creationDate(vp)); /* Rw volume creation date */
494 code = DumpInt32(dumpfd, 'A', V_accessDate(vp));
496 code = DumpInt32(dumpfd, 'U', V_updateDate(vp));
498 code = DumpInt32(dumpfd, 'E', V_expirationDate(vp));
500 code = DumpInt32(dumpfd, 'B', V_backupDate(vp)); /* Rw volume backup clone date */
502 code = DumpString(dumpfd, 'O', V_offlineMessage(vp));
505 * We do NOT dump the detailed volume statistics residing in the old
506 * motd field, since we cannot tell from the info in a dump whether
507 * statistics data has been put there. Instead, we dump a null string,
508 * just as if that was what the motd contained.
511 code = DumpString(dumpfd, 'M', "");
514 DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp),
515 sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
517 code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp));
519 code = DumpInt32(dumpfd, 'Z', V_dayUse(vp));
524 DumpShort(int dumpfd, char tag, unsigned int value)
527 byte *p = (unsigned char *)tbuffer;
531 return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
535 DumpByteString(int dumpfd, char tag, byte * bs, int nbytes)
539 code = write(dumpfd, &tag, 1);
541 return VOLSERDUMPERROR;
542 code = write(dumpfd, (char *)bs, nbytes);
544 return VOLSERDUMPERROR;
550 DumpFile(int dumpfd, int vnode, FdHandle_t * handleP, struct VnodeDiskObject *v)
552 int code = 0, failed_seek = 0, failed_write = 0;
554 afs_foff_t offset = 0;
555 afs_sfsize_t nbytes, howBig;
558 afs_foff_t howFar = 0;
563 struct afs_stat status;
567 #include <sys/statfs.h>
568 struct statfs tstatfs;
572 fprintf(stderr, "dumping file for vnode %d\n", vnode);
575 howBig = _filelength(handleP->fd_fd);
579 afs_fstat(handleP->fd_fd, &status);
580 howBig = status.st_size;
583 /* Unfortunately in AIX valuable fields such as st_blksize are
584 * gone from the stat structure.
586 fstatfs(handleP->fd_fd, &tstatfs);
587 howMany = tstatfs.f_bsize;
589 howMany = status.st_blksize;
590 #endif /* AFS_AIX_ENV */
591 #endif /* AFS_NT40_ENV */
594 size = FDH_SIZE(handleP);
597 fprintf(stderr, " howBig = %u, howMany = %u, fdh size = %u\n",
598 (unsigned int) howBig, (unsigned int) howMany,
599 (unsigned int) size);
601 SplitInt64(size, hi, lo);
603 code = DumpInt32(dumpfd, 'f', lo);
605 code = DumpDouble(dumpfd, 'h', hi, lo);
609 return VOLSERDUMPERROR;
612 p = (unsigned char *)malloc(howMany);
614 fprintf(stderr, "out of memory!\n");
615 return VOLSERDUMPERROR;
618 /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */
619 for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) {
620 if (nbytes < howMany)
623 /* Read the data - unless we know we can't */
624 n = (failed_seek ? 0 : FDH_PREAD(handleP, p, howMany, howFar));
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 %lld\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", (unsigned)n, (unsigned)howMany);
644 /* Record the read error */
647 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
648 errno, PrintInode(stmp, handleP->fd_ih->ih_ino),
651 fprintf(stderr, "Error reading inode %s for vnode %d\n",
652 PrintInode(stmp, 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 howFar = ((size - nbytes) + howMany);
669 /* Now write the data out */
670 if (write(dumpfd, (char *)p, howMany) != howMany)
671 failed_write = VOLSERDUMPERROR;
674 if (pad) { /* Any padding we hadn't reported yet */
675 fprintf(stderr, "Null padding file: %d bytes at offset %lld\n", pad,
685 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
686 int dumpEverything, struct Volume *vp)
694 fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
696 if (!v || v->type == vNull)
699 code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
703 code = DumpByte(dumpfd, 't', (byte) v->type);
705 code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */
707 code = DumpInt32(dumpfd, 'v', v->dataVersion);
709 code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
711 code = DumpInt32(dumpfd, 'a', v->author);
713 code = DumpInt32(dumpfd, 'o', v->owner);
714 if (!code && v->group)
715 code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */
717 code = DumpShort(dumpfd, 'b', v->modeBits);
719 code = DumpInt32(dumpfd, 'p', v->parent);
721 code = DumpInt32(dumpfd, 's', v->serverModifyTime);
722 if (v->type == vDirectory) {
723 acl_HtonACL(VVnodeDiskACL(v));
726 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
730 if (VNDISK_GET_INO(v)) {
731 IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
735 "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
736 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber, volid,
742 fprintf(stderr, "about to dump inode %s for vnode %u\n",
743 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber);
744 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
751 fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
757 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
761 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
762 char buf[SIZEOF_LARGEDISKVNODE];
763 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
764 StreamHandle_t *file;
768 afs_foff_t offset = 0;
769 int vnodeIndex, nVnodes = 0;
771 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
772 file = FDH_FDOPEN(fdP, "r+");
773 size = OS_SIZE(fdP->fd_fd);
774 nVnodes = (size / vcp->diskSize) - 1;
777 STREAM_ASEEK(file, vcp->diskSize);
781 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
782 nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
783 flag = forcedump || (vnode->serverModifyTime >= fromtime);
784 /* Note: the >= test is very important since some old volumes may not have
785 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
786 * does dump the file! */
788 fprintf(stderr, "about to dump %s vnode %u (vnode offset = %lld)\n",
789 class == vSmall ? "vSmall" : "vLarge",
790 bitNumberToVnodeNumber(vnodeIndex, class), (long long)offset);
793 DumpVnode(dumpfd, vnode, V_id(vp),
794 bitNumberToVnodeNumber(vnodeIndex, class), flag,
804 /* A partial dump (no dump header) */
806 DumpPartial(int dumpfd, Volume * vp, afs_int32 fromtime,
812 fprintf(stderr, "about to dump the volume header\n");
814 code = DumpVolumeHeader(dumpfd, vp);
817 fprintf(stderr, "about to dump the large vnode index\n");
819 code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
822 fprintf(stderr, "about to dump the small vnode index\n");
824 code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
831 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
840 afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
842 fprintf(stderr, "Failed to open dump file: %s. Exiting.\n",
843 afs_error_message(errno));
847 dumpfd = 1; /* stdout */
851 fprintf(stderr, "about to dump the dump header\n");
853 code = DumpDumpHeader(dumpfd, vp, fromtime);
856 fprintf(stderr, "about to dump volume contents\n");
858 code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
861 fprintf(stderr, "about to dump the dump postamble\n");
863 code = DumpEnd(dumpfd);
866 fprintf(stderr, "finished dump\n");
867 close(dumpfd); /* might be closing stdout, no harm */