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>
31 #include <sys/param.h>
38 #include <afs/afsint.h>
40 #include <afs/errors.h>
43 #include <afs/afssyscalls.h>
47 #include "partition.h"
48 #include "viceinode.h"
49 #include <afs/afssyscalls.h>
70 #include "volser/volser.h"
71 #include "volser/volint.h"
72 #include "volser/dump.h"
74 #define putint32(p, v) *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
75 #define putshort(p, v) *p++ = v>>8, *p++ = v
78 #define afs_stat stat64
79 #define afs_fstat fstat64
80 #define afs_open open64
81 #else /* !O_LARGEFILE */
83 #define afs_fstat fstat
85 #endif /* !O_LARGEFILE */
87 int VolumeChanged; /* needed by physio - leave alone */
90 /* Forward Declarations */
91 void HandleVolume(struct DiskPartition64 *partP, char *name, char *filename, int fromtime);
92 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
93 register struct VolumeHeader *header);
94 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
95 char *dumpfile, int fromtime);
98 #include "AFS_component_version_number.c"
101 char name[VMAXPATHLEN];
105 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
110 code = IH_IREAD(ih, 0, to, size);
119 AttachVolume(struct DiskPartition64 * dp, char *volname,
120 register struct VolumeHeader * header)
125 vp = (Volume *) calloc(1, sizeof(Volume));
126 vp->specialStatus = 0;
127 vp->device = dp->device;
129 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
130 header->largeVnodeIndex);
131 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
132 header->smallVnodeIndex);
133 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
135 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
136 vp->cacheCheck = 0; /* XXXX */
137 vp->shuttingDown = 0;
138 vp->goingOffline = 0;
140 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
141 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
142 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
144 struct IndexFileHeader iHead;
145 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
146 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
149 struct IndexFileHeader iHead;
150 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
151 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
155 struct versionStamp stamp;
156 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
157 LINKTABLEMAGIC, LINKTABLEVERSION);
167 handleit(struct cmd_syndesc *as, void *arock)
169 register struct cmd_item *ti;
171 afs_uint32 volumeId = 0;
173 char *fileName = NULL;
174 struct DiskPartition64 *partP = NULL;
176 char tmpPartName[20];
183 if (geteuid() != 0) {
184 fprintf(stderr, "voldump must be run as root; sorry\n");
190 if ((ti = as->parms[0].items))
192 if ((ti = as->parms[1].items))
193 volumeId = (afs_uint32)atoi(ti->data);
194 if ((ti = as->parms[2].items))
196 if ((ti = as->parms[3].items))
198 if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
199 code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
201 fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
202 as->parms[4].items->data, code);
209 err = VAttachPartitions();
211 fprintf(stderr, "%d partitions had errors during attach.\n", err);
215 if (strlen(partName) == 1) {
216 if (partName[0] >= 'a' && partName[0] <= 'z') {
217 strcpy(tmpPartName, "/vicepa");
218 tmpPartName[6] = partName[0];
219 partP = VGetPartition(tmpPartName, 0);
222 partP = VGetPartition(partName, 0);
226 "%s is not an AFS partition name on this server.\n",
233 fprintf(stderr, "Must specify volume id!\n");
238 fprintf(stderr, "must specify vice partition.\n");
242 (void)afs_snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
243 HandleVolume(partP, name1, fileName, fromtime);
248 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
250 struct VolumeHeader header;
251 struct VolumeDiskHeader diskHeader;
252 struct afs_stat status, stat;
256 char headerName[1024];
260 (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
261 VPartitionPath(dp), name);
262 if ((fd = afs_open(headerName, O_RDONLY)) == -1
263 || afs_fstat(fd, &status) == -1) {
264 fprintf(stderr, "Cannot read volume header %s\n", name);
268 n = read(fd, &diskHeader, sizeof(diskHeader));
270 if (n != sizeof(diskHeader)
271 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
272 fprintf(stderr, "Error reading volume header %s\n", name);
275 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
277 "Volume %s, version number is incorrect; volume needs salvage\n",
281 DiskToVolumeHeader(&header, &diskHeader);
284 vp = AttachVolume(dp, name, &header);
286 fprintf(stderr, "Error attaching volume header %s\n", name);
290 DoMyVolDump(vp, dp, filename, fromtime);
295 main(int argc, char **argv)
297 register struct cmd_syndesc *ts;
300 VInitVolumePackage(volumeUtility, 5, 5, DONT_CONNECT_FS, 0);
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, register afs_uint32 value1,
319 register afs_uint32 value2)
323 register byte *p = (unsigned char *)tbuffer;
328 res = write(dumpfd, tbuffer, 9);
329 return ((res == 9) ? 0 : VOLSERDUMPERROR);
333 DumpInt32(int dumpfd, char tag, register afs_uint32 value)
336 register byte *p = (unsigned char *)tbuffer;
339 return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
343 DumpString(int dumpfd, char tag, register 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, register afs_uint32 * array,
363 register afs_uint32 v;
365 register byte *p = (unsigned char *)tbuffer;
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, register 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 register byte *p = (unsigned char *)tbuffer;
426 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
430 DumpTag(int dumpfd, register int tag)
435 return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
440 DumpBool(int dumpfd, char tag, unsigned int value)
443 register byte *p = (unsigned char *)tbuffer;
446 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
452 DumpVolumeHeader(int dumpfd, register 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 register byte *p = (unsigned char *)tbuffer;
531 return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
535 DumpByteString(int dumpfd, char tag, register byte * bs, register 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_int32 offset = 0;
555 afs_sfsize_t n, nbytes, howMany, howBig;
558 struct afs_stat status;
560 afs_sfsize_t size, tmpsize;
562 #include <sys/statfs.h>
563 struct statfs tstatfs;
567 fprintf(stderr, "dumping file for vnode %d\n", vnode);
570 howBig = _filelength(handleP->fd_fd);
574 afs_fstat(handleP->fd_fd, &status);
575 howBig = status.st_size;
578 /* Unfortunately in AIX valuable fields such as st_blksize are
579 * gone from the stat structure.
581 fstatfs(handleP->fd_fd, &tstatfs);
582 howMany = tstatfs.f_bsize;
584 howMany = status.st_blksize;
585 #endif /* AFS_AIX_ENV */
586 #endif /* AFS_NT40_ENV */
589 size = FDH_SIZE(handleP);
592 fprintf(stderr, " howBig = %u, howMany = %u, fdh size = %u\n",
593 howBig, howMany, size);
595 #ifdef AFS_LARGEFILE_ENV
598 SplitInt64(size, hi, lo);
600 code = DumpInt32(dumpfd, 'f', lo);
602 code = DumpDouble(dumpfd, 'h', hi, lo);
605 #else /* !AFS_LARGEFILE_ENV */
606 code = DumpInt32(dumpfd, 'f', size);
607 #endif /* !AFS_LARGEFILE_ENV */
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_READ(handleP, p, howMany));
626 /* If read any good data and we null padded previously, log the
627 * amount that we had null padded.
629 if ((n > 0) && pad) {
630 fprintf(stderr, "Null padding file %d bytes at offset %u\n", pad,
635 /* If didn't read enough data, null padd the rest of the buffer. This
636 * can happen if, for instance, the media has some bad spots. We don't
637 * want to quit the dump, so we start null padding.
641 if (verbose) fprintf(stderr, " read %u instead of %u bytes.\n", n, howMany);
643 /* Record the read error */
646 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
647 errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
650 fprintf(stderr, "Error reading inode %s for vnode %d\n",
651 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
654 /* Pad the rest of the buffer with zeros. Remember offset we started
655 * padding. Keep total tally of padding.
657 memset(p + n, 0, howMany - n);
659 offset = (howBig - nbytes) + n;
660 pad += (howMany - n);
662 /* Now seek over the data we could not get. An error here means we
663 * can't do the next read.
665 failed_seek = FDH_SEEK(handleP, ((size - nbytes) + howMany), SEEK_SET);
666 if (failed_seek != ((size - nbytes) + howMany)) {
667 if (failed_seek < 0) {
669 "Error %d seeking in inode %s for vnode %d\n",
670 errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
674 "Error seeking in inode %s for vnode %d\n",
675 PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
683 /* Now write the data out */
684 if (write(dumpfd, (char *)p, howMany) != howMany)
685 failed_write = VOLSERDUMPERROR;
688 if (pad) { /* Any padding we hadn't reported yet */
689 fprintf(stderr, "Null padding file: %d bytes at offset %u\n", pad,
699 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
700 int dumpEverything, struct Volume *vp)
707 fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
709 if (!v || v->type == vNull)
712 code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
716 code = DumpByte(dumpfd, 't', (byte) v->type);
718 code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */
720 code = DumpInt32(dumpfd, 'v', v->dataVersion);
722 code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
724 code = DumpInt32(dumpfd, 'a', v->author);
726 code = DumpInt32(dumpfd, 'o', v->owner);
727 if (!code && v->group)
728 code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */
730 code = DumpShort(dumpfd, 'b', v->modeBits);
732 code = DumpInt32(dumpfd, 'p', v->parent);
734 code = DumpInt32(dumpfd, 's', v->serverModifyTime);
735 if (v->type == vDirectory) {
736 acl_HtonACL(VVnodeDiskACL(v));
739 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
743 if (VNDISK_GET_INO(v)) {
744 IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
748 "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
749 PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber, volid,
755 fprintf(stderr, "about to dump inode %s for vnode %u\n",
756 PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber);
757 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
764 fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
770 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
773 register int code = 0;
774 register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
775 char buf[SIZEOF_LARGEDISKVNODE];
776 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
777 StreamHandle_t *file;
782 register int vnodeIndex, nVnodes = 0;
784 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
785 file = FDH_FDOPEN(fdP, "r+");
786 size = OS_SIZE(fdP->fd_fd);
787 nVnodes = (size / vcp->diskSize) - 1;
790 STREAM_SEEK(file, vcp->diskSize, 0);
794 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
795 nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
796 flag = forcedump || (vnode->serverModifyTime >= fromtime);
797 /* Note: the >= test is very important since some old volumes may not have
798 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
799 * does dump the file! */
801 fprintf(stderr, "about to dump %s vnode %u (vnode offset = %u)\n",
802 class == vSmall ? "vSmall" : "vLarge",
803 bitNumberToVnodeNumber(vnodeIndex, class), offset);
806 DumpVnode(dumpfd, vnode, V_id(vp),
807 bitNumberToVnodeNumber(vnodeIndex, class), flag,
817 /* A partial dump (no dump header) */
819 DumpPartial(int dumpfd, register Volume * vp, afs_int32 fromtime,
825 fprintf(stderr, "about to dump the volume header\n");
827 code = DumpVolumeHeader(dumpfd, vp);
830 fprintf(stderr, "about to dump the large vnode index\n");
832 code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
835 fprintf(stderr, "about to dump the small vnode index\n");
837 code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
844 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
853 afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
855 fprintf(stderr, "Failed to open dump file! Exiting.\n");
859 dumpfd = 1; /* stdout */
863 fprintf(stderr, "about to dump the dump header\n");
865 code = DumpDumpHeader(dumpfd, vp, fromtime);
868 fprintf(stderr, "about to dump volume contents\n");
870 code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
873 fprintf(stderr, "about to dump the dump postamble\n");
875 code = DumpEnd(dumpfd);
878 fprintf(stderr, "finished dump\n");
879 close(dumpfd); /* might be closing stdout, no harm */