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 static void HandleVolume(struct DiskPartition64 *partP, char *name,
58 char *filename, int fromtime);
59 static Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
60 struct VolumeHeader *header);
61 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
62 char *dumpfile, int fromtime);
65 #include "AFS_component_version_number.c"
68 char name[VMAXPATHLEN];
72 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
76 code = IH_IREAD(ih, 0, to, size);
85 AttachVolume(struct DiskPartition64 * dp, char *volname,
86 struct VolumeHeader * header)
91 vp = (Volume *) calloc(1, sizeof(Volume));
92 vp->specialStatus = 0;
93 vp->device = dp->device;
95 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
96 header->largeVnodeIndex);
97 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
98 header->smallVnodeIndex);
99 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
101 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
102 vp->cacheCheck = 0; /* XXXX */
103 vp->shuttingDown = 0;
104 vp->goingOffline = 0;
106 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
107 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
108 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
110 struct IndexFileHeader iHead;
111 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
112 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
115 struct IndexFileHeader iHead;
116 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
117 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
121 struct versionStamp stamp;
122 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
123 LINKTABLEMAGIC, LINKTABLEVERSION);
133 handleit(struct cmd_syndesc *as, void *arock)
137 afs_uint32 volumeId = 0;
139 char *fileName = NULL;
140 struct DiskPartition64 *partP = NULL;
142 char tmpPartName[20];
149 if (geteuid() != 0) {
150 fprintf(stderr, "voldump must be run as root; sorry\n");
156 if ((ti = as->parms[0].items))
158 if ((ti = as->parms[1].items))
159 volumeId = (afs_uint32)atoi(ti->data);
160 if ((ti = as->parms[2].items))
162 if ((ti = as->parms[3].items))
164 if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
165 code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
167 fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
168 as->parms[4].items->data, code);
175 err = VAttachPartitions();
177 fprintf(stderr, "%d partitions had errors during attach.\n", err);
181 if (strlen(partName) == 1) {
182 if (partName[0] >= 'a' && partName[0] <= 'z') {
183 strcpy(tmpPartName, "/vicepa");
184 tmpPartName[6] = partName[0];
185 partP = VGetPartition(tmpPartName, 0);
188 partP = VGetPartition(partName, 0);
192 "%s is not an AFS partition name on this server.\n",
199 fprintf(stderr, "Must specify volume id!\n");
204 fprintf(stderr, "must specify vice partition.\n");
208 snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
209 HandleVolume(partP, name1, fileName, fromtime);
214 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
216 struct VolumeHeader header;
217 struct VolumeDiskHeader diskHeader;
218 struct afs_stat status;
221 char headerName[1024];
225 snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
226 VPartitionPath(dp), name);
227 if ((fd = afs_open(headerName, O_RDONLY)) == -1
228 || afs_fstat(fd, &status) == -1) {
229 fprintf(stderr, "Cannot read volume header %s\n", name);
233 n = read(fd, &diskHeader, sizeof(diskHeader));
235 if (n != sizeof(diskHeader)
236 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
237 fprintf(stderr, "Error reading volume header %s\n", name);
240 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
242 "Volume %s, version number is incorrect; volume needs salvage\n",
246 DiskToVolumeHeader(&header, &diskHeader);
249 vp = AttachVolume(dp, name, &header);
251 fprintf(stderr, "Error attaching volume header %s\n", name);
255 DoMyVolDump(vp, dp, filename, fromtime);
260 main(int argc, char **argv)
262 struct cmd_syndesc *ts;
264 VolumePackageOptions opts;
266 VOptDefaults(volumeUtility, &opts);
267 if (VInitVolumePackage2(volumeUtility, &opts)) {
268 fprintf(stderr, "errors encountered initializing volume package, but "
269 "trying to continue anyway\n");
272 ts = cmd_CreateSyntax(NULL, handleit, NULL,
273 "Dump a volume to a 'vos dump' format file without using volserver");
274 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL, "AFS partition name");
275 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
276 cmd_AddParm(ts, "-file", CMD_LIST, CMD_OPTIONAL, "Dump filename");
277 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
278 "Trace dump progress (very verbose)");
279 cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
280 code = cmd_Dispatch(argc, argv);
288 DumpDouble(int dumpfd, char tag, afs_uint32 value1,
293 byte *p = (unsigned char *)tbuffer;
295 afs_putint32(p, value1);
296 afs_putint32(p, value2);
298 res = write(dumpfd, tbuffer, 9);
299 return ((res == 9) ? 0 : VOLSERDUMPERROR);
303 DumpInt32(int dumpfd, char tag, afs_uint32 value)
306 byte *p = (unsigned char *)tbuffer;
308 afs_putint32(p, value);
309 return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
313 DumpString(int dumpfd, char tag, char *s)
317 code = write(dumpfd, &tag, 1);
319 return VOLSERDUMPERROR;
321 code = write(dumpfd, s, n);
323 return VOLSERDUMPERROR;
329 DumpArrayInt32(int dumpfd, char tag, afs_uint32 * array,
335 byte *p = (unsigned char *)tbuffer;
337 afs_putshort(p, nelem);
338 code = write(dumpfd, tbuffer, 3);
340 return VOLSERDUMPERROR;
342 p = (unsigned char *)tbuffer;
343 v = *array++; /*this was register */
346 code = write(dumpfd, tbuffer, 4);
348 return VOLSERDUMPERROR;
357 DumpDumpHeader(int dumpfd, Volume * vp, afs_int32 fromtime)
360 afs_int32 dumpTimes[2];
363 fprintf(stderr, "dumping dump header\n");
366 code = DumpDouble(dumpfd, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
369 code = DumpInt32(dumpfd, 'v', V_id(vp));
372 code = DumpString(dumpfd, 'n', V_name(vp));
374 dumpTimes[0] = fromtime;
375 dumpTimes[1] = V_backupDate(vp); /* Until the time the clone was made */
377 code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2);
386 return (DumpInt32(dumpfd, D_DUMPEND, DUMPENDMAGIC));
390 DumpByte(int dumpfd, char tag, byte value)
393 byte *p = (unsigned char *)tbuffer;
396 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
400 DumpTag(int dumpfd, int tag)
405 return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
410 DumpBool(int dumpfd, char tag, unsigned int value)
413 byte *p = (unsigned char *)tbuffer;
416 return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
422 DumpVolumeHeader(int dumpfd, Volume * vp)
427 fprintf(stderr, "dumping volume header\n");
430 code = DumpTag(dumpfd, D_VOLUMEHEADER);
432 code = DumpInt32(dumpfd, 'i', V_id(vp));
434 code = DumpInt32(dumpfd, 'v', V_stamp(vp).version);
436 code = DumpString(dumpfd, 'n', V_name(vp));
438 code = DumpBool(dumpfd, 's', V_inService(vp));
440 code = DumpBool(dumpfd, 'b', V_blessed(vp));
442 code = DumpInt32(dumpfd, 'u', V_uniquifier(vp));
444 code = DumpByte(dumpfd, 't', (byte) V_type(vp));
446 code = DumpInt32(dumpfd, 'p', V_parentId(vp));
448 code = DumpInt32(dumpfd, 'c', V_cloneId(vp));
450 code = DumpInt32(dumpfd, 'q', V_maxquota(vp));
452 code = DumpInt32(dumpfd, 'm', V_minquota(vp));
454 code = DumpInt32(dumpfd, 'd', V_diskused(vp));
456 code = DumpInt32(dumpfd, 'f', V_filecount(vp));
458 code = DumpInt32(dumpfd, 'a', V_accountNumber(vp));
460 code = DumpInt32(dumpfd, 'o', V_owner(vp));
462 code = DumpInt32(dumpfd, 'C', V_creationDate(vp)); /* Rw volume creation date */
464 code = DumpInt32(dumpfd, 'A', V_accessDate(vp));
466 code = DumpInt32(dumpfd, 'U', V_updateDate(vp));
468 code = DumpInt32(dumpfd, 'E', V_expirationDate(vp));
470 code = DumpInt32(dumpfd, 'B', V_backupDate(vp)); /* Rw volume backup clone date */
472 code = DumpString(dumpfd, 'O', V_offlineMessage(vp));
475 * We do NOT dump the detailed volume statistics residing in the old
476 * motd field, since we cannot tell from the info in a dump whether
477 * statistics data has been put there. Instead, we dump a null string,
478 * just as if that was what the motd contained.
481 code = DumpString(dumpfd, 'M', "");
484 DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp),
485 sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
487 code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp));
489 code = DumpInt32(dumpfd, 'Z', V_dayUse(vp));
494 DumpShort(int dumpfd, char tag, unsigned int value)
497 byte *p = (unsigned char *)tbuffer;
501 return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
505 DumpByteString(int dumpfd, char tag, byte * bs, int nbytes)
509 code = write(dumpfd, &tag, 1);
511 return VOLSERDUMPERROR;
512 code = write(dumpfd, (char *)bs, nbytes);
514 return VOLSERDUMPERROR;
520 DumpFile(int dumpfd, int vnode, FdHandle_t * handleP, struct VnodeDiskObject *v)
522 int code = 0, failed_seek = 0, failed_write = 0;
524 afs_foff_t offset = 0;
525 afs_sfsize_t nbytes, howBig;
528 afs_foff_t howFar = 0;
533 struct afs_stat status;
535 LARGE_INTEGER fileSize;
539 #include <sys/statfs.h>
540 struct statfs tstatfs;
544 fprintf(stderr, "dumping file for vnode %d\n", vnode);
547 if (!GetFileSizeEx(handleP->fd_fd, &fileSize)) {
548 Log("DumpFile: GetFileSizeEx returned error code %d on descriptor %d\n", GetLastError(), handleP->fd_fd);
549 return VOLSERDUMPERROR;
551 howBig = fileSize.QuadPart;
555 afs_fstat(handleP->fd_fd, &status);
556 howBig = status.st_size;
559 /* Unfortunately in AIX valuable fields such as st_blksize are
560 * gone from the stat structure.
562 fstatfs(handleP->fd_fd, &tstatfs);
563 howMany = tstatfs.f_bsize;
565 howMany = status.st_blksize;
566 #endif /* AFS_AIX_ENV */
567 #endif /* AFS_NT40_ENV */
570 size = FDH_SIZE(handleP);
573 fprintf(stderr, " howBig = %u, howMany = %u, fdh size = %u\n",
574 (unsigned int) howBig, (unsigned int) howMany,
575 (unsigned int) size);
577 SplitInt64(size, hi, lo);
579 code = DumpInt32(dumpfd, 'f', lo);
581 code = DumpDouble(dumpfd, 'h', hi, lo);
585 return VOLSERDUMPERROR;
588 p = (unsigned char *)malloc(howMany);
590 fprintf(stderr, "out of memory!\n");
591 return VOLSERDUMPERROR;
594 /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */
595 for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) {
596 if (nbytes < howMany)
599 /* Read the data - unless we know we can't */
600 n = (failed_seek ? 0 : FDH_PREAD(handleP, p, howMany, howFar));
603 /* If read any good data and we null padded previously, log the
604 * amount that we had null padded.
606 if ((n > 0) && pad) {
607 fprintf(stderr, "Null padding file %d bytes at offset %lld\n", pad,
612 /* If didn't read enough data, null padd the rest of the buffer. This
613 * can happen if, for instance, the media has some bad spots. We don't
614 * want to quit the dump, so we start null padding.
618 if (verbose) fprintf(stderr, " read %u instead of %u bytes.\n", (unsigned)n, (unsigned)howMany);
620 /* Record the read error */
623 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
624 errno, PrintInode(stmp, handleP->fd_ih->ih_ino),
627 fprintf(stderr, "Error reading inode %s for vnode %d\n",
628 PrintInode(stmp, handleP->fd_ih->ih_ino), vnode);
631 /* Pad the rest of the buffer with zeros. Remember offset we started
632 * padding. Keep total tally of padding.
634 memset(p + n, 0, howMany - n);
636 offset = (howBig - nbytes) + n;
637 pad += (howMany - n);
639 /* Now seek over the data we could not get. An error here means we
640 * can't do the next read.
642 howFar = ((size - nbytes) + howMany);
645 /* Now write the data out */
646 if (write(dumpfd, (char *)p, howMany) != howMany)
647 failed_write = VOLSERDUMPERROR;
650 if (pad) { /* Any padding we hadn't reported yet */
651 fprintf(stderr, "Null padding file: %d bytes at offset %lld\n", pad,
661 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
662 int dumpEverything, struct Volume *vp)
670 fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
672 if (!v || v->type == vNull)
675 code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
679 code = DumpByte(dumpfd, 't', (byte) v->type);
681 code = DumpShort(dumpfd, 'l', v->linkCount); /* May not need this */
683 code = DumpInt32(dumpfd, 'v', v->dataVersion);
685 code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
687 code = DumpInt32(dumpfd, 'a', v->author);
689 code = DumpInt32(dumpfd, 'o', v->owner);
690 if (!code && v->group)
691 code = DumpInt32(dumpfd, 'g', v->group); /* default group is 0 */
693 code = DumpShort(dumpfd, 'b', v->modeBits);
695 code = DumpInt32(dumpfd, 'p', v->parent);
697 code = DumpInt32(dumpfd, 's', v->serverModifyTime);
698 if (v->type == vDirectory) {
699 acl_HtonACL(VVnodeDiskACL(v));
702 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
706 if (VNDISK_GET_INO(v)) {
707 IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
711 "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
712 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber, volid,
718 fprintf(stderr, "about to dump inode %s for vnode %u\n",
719 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber);
720 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
727 fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
733 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
737 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
738 char buf[SIZEOF_LARGEDISKVNODE];
739 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
740 StreamHandle_t *file;
744 afs_foff_t offset = 0;
745 int vnodeIndex, nVnodes = 0;
747 fdP = IH_OPEN(vp->vnodeIndex[class].handle);
748 file = FDH_FDOPEN(fdP, "r+");
749 size = OS_SIZE(fdP->fd_fd);
750 nVnodes = (size / vcp->diskSize) - 1;
753 STREAM_ASEEK(file, vcp->diskSize);
757 nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
758 nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
759 flag = forcedump || (vnode->serverModifyTime >= fromtime);
760 /* Note: the >= test is very important since some old volumes may not have
761 * a serverModifyTime. For an epoch dump, this results in 0>=0 test, which
762 * does dump the file! */
764 fprintf(stderr, "about to dump %s vnode %u (vnode offset = %lld)\n",
765 class == vSmall ? "vSmall" : "vLarge",
766 bitNumberToVnodeNumber(vnodeIndex, class), (long long)offset);
769 DumpVnode(dumpfd, vnode, V_id(vp),
770 bitNumberToVnodeNumber(vnodeIndex, class), flag,
780 /* A partial dump (no dump header) */
782 DumpPartial(int dumpfd, Volume * vp, afs_int32 fromtime,
788 fprintf(stderr, "about to dump the volume header\n");
790 code = DumpVolumeHeader(dumpfd, vp);
793 fprintf(stderr, "about to dump the large vnode index\n");
795 code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
798 fprintf(stderr, "about to dump the small vnode index\n");
800 code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
807 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
816 afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
818 fprintf(stderr, "Failed to open dump file: %s. Exiting.\n",
819 afs_error_message(errno));
823 dumpfd = 1; /* stdout */
827 fprintf(stderr, "about to dump the dump header\n");
829 code = DumpDumpHeader(dumpfd, vp, fromtime);
832 fprintf(stderr, "about to dump volume contents\n");
834 code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
837 fprintf(stderr, "about to dump the dump postamble\n");
839 code = DumpEnd(dumpfd);
842 fprintf(stderr, "finished dump\n");
843 close(dumpfd); /* might be closing stdout, no harm */