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
11 * Read a vos dump and recreate the tree.
13 * restorevol [-file <dump file>]
14 * [-dir <restore dir>]
15 * [-extension <name extension>]
16 * [-mountpoint <mount point root>]
17 * [-umask <mode mask>]
19 * 1. The dump file will be restored within the current or that specified with -dir.
20 * 2. Within this dir, a subdir is created. It's name is the RW volume name
21 * that was dumped. An extension can be appended to this directory name
23 * 3. All mountpoints will appear as symbolic links to the volume. The
24 * pathname to the volume will be either that in -mountpoint, or -dir.
25 * Symbolic links remain untouched.
26 * 4. You can change your umask during the restore with -umask. Otherwise, it
27 * uses your current umask. Mode bits for directories are 0777 (then
28 * AND'ed with the umask). Mode bits for files are the owner mode bits
29 * duplicated accross group and user (then AND'ed with the umask).
30 * 5. For restores of full dumps, if a directory says it has a file and
31 * the file is not found, then a symbolic link "AFSFile-<#>" will
32 * appear in that restored tree. Restores of incremental dumps remove
33 * all these files at the end (expensive because it is a tree search).
34 * 6. If a file or directory was found in the dump but found not to be
35 * connected to the hierarchical tree, then the file or directory
36 * will be connected at the root of the tree as "__ORPHANEDIR__.<#>"
37 * or "__ORPHANFILE__.<#>".
38 * 7. ACLs are not restored.
42 #include <afsconfig.h>
43 #include <afs/param.h>
48 #include <afs/afsint.h>
51 #include <afs/ihandle.h>
52 #include <afs/vnode.h>
53 #include <afs/volume.h>
58 #include <sys/param.h>
59 #include <sys/types.h>
63 #include <netinet/in.h>
77 char rootdir[MAXPATHLEN];
78 char mntroot[MAXPATHLEN];
79 #define ADIR "AFSDir-"
80 #define AFILE "AFSFile-"
81 #define ODIR "__ORPHANEDIR__."
82 #define OFILE "__ORPHANFILE__."
97 s = sizeof(value) - size;
99 fprintf(stderr, "Too much data in afs_int32\n");
103 code = fread(&ptr[s], 1, size, dumpfile);
105 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
118 code = fread(&value, 1, 1, dumpfile);
120 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
125 #define BUFSIZE 16384
129 readdata(buffer, size)
138 s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
139 code = fread(buf, 1, s, dumpfile);
141 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
145 code = fread(buffer, 1, size, dumpfile);
148 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
150 fprintf(stderr, "Read %d bytes out of %d\n", code, size);
152 if ((code >= 0) && (code < BUFSIZE))
153 buffer[size] = 0; /* Add null char at end */
159 struct DumpHeader *dh; /* Defined in dump.h */
165 /* memset(&dh, 0, sizeof(dh)); */
167 magic = ntohl(readvalue(4));
168 dh->version = ntohl(readvalue(4));
175 dh->volumeId = ntohl(readvalue(4));
179 for (i = 0, c = 'a'; c != '\0'; i++) {
180 dh->volumeName[i] = c = readchar();
182 dh->volumeName[i] = c;
186 dh->nDumpTimes = ntohl(readvalue(2)) >> 1;
187 for (i = 0; i < dh->nDumpTimes; i++) {
188 dh->dumpTimes[i].from = ntohl(readvalue(4));
189 dh->dumpTimes[i].to = ntohl(readvalue(4));
199 return ((afs_int32) tag);
202 struct volumeHeader {
204 char volumeName[100];
206 afs_int32 uniquifier;
213 afs_int32 accountNumber;
215 afs_int32 creationDate;
216 afs_int32 accessDate;
217 afs_int32 updateDate;
218 afs_int32 expirationDate;
219 afs_int32 backupDate;
220 afs_int32 dayUseDate;
223 afs_int32 weekUse[100]; /* weekCount of these */
231 ReadVolumeHeader(count)
234 struct volumeHeader vh;
235 int code, i, done, entries;
238 /* memset(&vh, 0, sizeof(vh)); */
245 vh.volumeId = ntohl(readvalue(4));
249 ntohl(readvalue(4)); /* version stamp - ignore */
253 for (i = 0, c = 'a'; c != '\0'; i++) {
254 vh.volumeName[i] = c = readchar();
256 vh.volumeName[i] = c;
260 vh.inService = ntohl(readvalue(1));
264 vh.blessed = ntohl(readvalue(1));
268 vh.uniquifier = ntohl(readvalue(4));
272 vh.volType = ntohl(readvalue(1));
276 vh.parentVol = ntohl(readvalue(4));
280 vh.cloneId = ntohl(readvalue(4));
284 vh.maxQuota = ntohl(readvalue(4));
288 vh.minQuota = ntohl(readvalue(4));
292 vh.diskUsed = ntohl(readvalue(4));
296 vh.fileCount = ntohl(readvalue(4));
300 vh.accountNumber = ntohl(readvalue(4));
304 vh.owner = ntohl(readvalue(4));
308 vh.creationDate = ntohl(readvalue(4));
312 vh.accessDate = ntohl(readvalue(4));
316 vh.updateDate = ntohl(readvalue(4));
320 vh.expirationDate = ntohl(readvalue(4));
324 vh.backupDate = ntohl(readvalue(4));
328 for (i = 0, c = 'a'; c != '\0'; i++) {
329 vh.message[i] = c = readchar();
331 vh.volumeName[i] = c;
335 vh.weekCount = ntohl(readvalue(2));
336 for (i = 0; i < vh.weekCount; i++) {
337 vh.weekUse[i] = ntohl(readvalue(4));
342 for (i = 0, c = 'a'; c != '\0'; i++) {
343 vh.motd[i] = c = readchar();
348 vh.dayUseDate = ntohl(readvalue(4));
352 vh.dayUse = ntohl(readvalue(4));
361 return ((afs_int32) tag);
366 afs_int32 uniquifier;
369 afs_int32 dataVersion;
370 afs_int32 unixModTime;
371 afs_int32 servModTime;
379 struct acl_accessList {
380 int size; /*size of this access list in bytes, including MySize itself */
381 int version; /*to deal with upward compatibility ; <= ACL_ACLVERSION */
383 int positive; /* number of positive entries */
384 int negative; /* number of minus entries */
385 struct acl_accessEntry {
386 int id; /*internally-used ID of user or group */
387 int rights; /*mask */
391 afs_sfsize_t dataSize;
394 #define MAXNAMELEN 256
401 int code, i, done, entries;
403 char dirname[MAXNAMELEN], linkname[MAXNAMELEN], lname[MAXNAMELEN];
404 char parentdir[MAXNAMELEN], vflink[MAXNAMELEN];
405 char filename[MAXNAMELEN], fname[MAXNAMELEN];
410 /* memset(&vn, 0, sizeof(vn)); */
416 vn.vnode = ntohl(readvalue(4));
417 vn.uniquifier = ntohl(readvalue(4));
424 vn.type = ntohl(readvalue(1));
428 vn.linkCount = ntohl(readvalue(2));
432 vn.dataVersion = ntohl(readvalue(4));
436 vn.unixModTime = ntohl(readvalue(4));
440 vn.servModTime = ntohl(readvalue(4));
444 vn.author = ntohl(readvalue(4));
448 vn.owner = ntohl(readvalue(4));
452 vn.group = ntohl(readvalue(4));
456 vn.modebits = ntohl(readvalue(2));
460 vn.parent = ntohl(readvalue(4));
464 readdata(vn.acl, 192); /* Skip ACL data */
467 #ifdef AFS_LARGEFILE_ENV
471 hi = ntohl(readvalue(4));
472 lo = ntohl(readvalue(4));
473 FillInt64(vn.dataSize, hi, lo);
476 #endif /* !AFS_LARGEFILE_ENV */
479 vn.dataSize = ntohl(readvalue(4));
482 /* parentdir is the name of this dir's vnode-file-link
483 * or this file's parent vnode-file-link.
484 * "./AFSDir-<#>". It's a symbolic link to its real dir.
485 * The parent dir and symbolic link to it must exist.
487 vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
489 strncpy(parentdir, rootdir, sizeof parentdir);
491 afs_snprintf(parentdir, sizeof parentdir, "%s/%s%d", rootdir,
494 len = readlink(parentdir, linkname, MAXNAMELEN);
496 /* parentdir does not exist. So create an orphan dir.
497 * and then link the parentdir to the orphaned dir.
499 afs_snprintf(linkname, sizeof linkname, "%s/%s%d",
500 rootdir, ODIR, vnode);
501 code = mkdir(linkname, 0777);
502 if ((code < 0) && (errno != EEXIST)) {
504 "Error creating directory %s code=%d;%d\n",
505 linkname, code, errno);
508 /* Link the parentdir to it - now parentdir exists */
509 afs_snprintf(linkname, sizeof linkname, "%s%d/", ODIR,
511 code = symlink(linkname, parentdir);
514 "Error creating symlink %s -> %s code=%d;%d\n",
515 parentdir, linkname, code, errno);
522 /* We read the directory entries. If the entry is a
523 * directory, the subdir is created and the root dir
524 * will contain a link to it. If its a file, we only
525 * create a symlink in the dir to the file name.
544 unsigned short pgcount;
552 struct Pageheader header;
554 unsigned short hashTable[128];
558 struct DirHeader header;
559 struct DirEntry entry[1];
564 buffer = (char *)malloc(vn.dataSize);
566 readdata(buffer, vn.dataSize);
567 page0 = (struct Page0 *)buffer;
569 /* Step through each bucket in the hash table, i,
570 * and follow each element in the hash chain, j.
571 * This gives us each entry of the dir.
573 for (i = 0; i < 128; i++) {
574 for (j = ntohs(page0->header.hashTable[i]); j;
575 j = ntohs(page0->entry[j].next)) {
577 this_vn = ntohl(page0->entry[j].fid.vnode);
578 this_name = page0->entry[j].name;
580 if ((strcmp(this_name, ".") == 0)
581 || (strcmp(this_name, "..") == 0))
582 continue; /* Skip these */
584 /* For a directory entry, create it. Then create the
585 * link (from the rootdir) to this directory.
589 /* dirname is the directory to create.
590 * vflink is what will link to it.
592 afs_snprintf(dirname, sizeof dirname, "%s/%s",
593 parentdir, this_name);
594 afs_snprintf(vflink, sizeof vflink, "%s/%s%d",
595 rootdir, ADIR, this_vn);
597 /* The link and directory may already exist */
598 len = readlink(vflink, linkname, MAXNAMELEN);
600 /* Doesn't already exist - so create the directory.
601 * umask will pare the mode bits down.
603 code = mkdir(dirname, 0777);
604 if ((code < 0) && (errno != EEXIST)) {
606 "Error creating directory %s code=%d;%d\n",
607 dirname, code, errno);
610 /* Does already exist - so move the directory.
611 * It was created originally as orphaned.
613 linkname[len - 1] = '\0'; /* remove '/' at end */
614 afs_snprintf(lname, sizeof lname, "%s/%s",
616 code = rename(lname, dirname);
619 "Error renaming %s to %s code=%d;%d\n",
620 lname, dirname, code, errno);
624 /* Now create/update the link to the new/moved directory */
626 afs_snprintf(dirname, sizeof dirname, "%s/",
629 afs_snprintf(dirname, sizeof dirname,
630 "%s%d/%s/", ADIR, vn.vnode,
633 code = symlink(dirname, vflink);
636 "Error creating symlink %s -> %s code=%d;%d\n",
637 vflink, dirname, code, errno);
641 /* For a file entry, we remember the name of the file
642 * by creating a link within the directory. Restoring
643 * the file will later remove the link.
646 /*AFILEENTRY*/ afs_snprintf(vflink,
648 "%s/%s%d", parentdir,
651 code = symlink(this_name, vflink);
652 if ((code < 0) && (errno != EEXIST)) {
654 "Error creating symlink %s -> %s code=%d;%d\n",
655 vflink, page0->entry[j].name, code,
664 else if (vn.type == 1) {
666 /* A file vnode. So create it into the desired directory. A
667 * link should exist in the directory naming the file.
671 afs_sfsize_t size, s;
673 /* Check if its vnode-file-link exists. If not,
674 * then the file will be an orphaned file.
677 afs_snprintf(filename, sizeof filename, "%s/%s%d", parentdir,
679 len = readlink(filename, fname, MAXNAMELEN);
681 afs_snprintf(filename, sizeof filename, "%s/%s%d",
682 rootdir, OFILE, vn.vnode);
683 lfile = 0; /* no longer a linked file; a direct path */
686 /* Create a mode for the file. Use the owner bits and
687 * duplicate them across group and other. The umask
688 * will remove what we don't want.
690 mode = (vn.modebits >> 6) & 0x7;
691 mode |= (mode << 6) | (mode << 3);
693 /* Write the file out */
694 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
697 s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
698 code = fread(buf, 1, s, dumpfile);
700 (void)write(fid, buf, code);
705 fprintf(stderr, "Code = %d; Errno = %d\n", code,
709 (void)afs_snprintf(tmp, sizeof tmp,
710 "Read %llu bytes out of %llu",
711 (afs_uintmax_t) (vn.dataSize -
713 (afs_uintmax_t) vn.dataSize);
714 fprintf(stderr, "%s\n", tmp);
721 fprintf(stderr, " File %s (%s) is incomplete\n",
725 /* Remove the link to the file */
731 else if (vn.type == 3) {
733 /* A symlink vnode. So read it into the desired directory. This could
734 * also be a mount point. If the volume is being restored to AFS, this
735 * will become a mountpoint. If not, it becomes a symlink to no-where.
740 /* Check if its vnode-file-link exists and create pathname
741 * of the symbolic link. If it doesn't exist,
742 * then the link will be an orphaned link.
744 afs_snprintf(linkname, sizeof linkname, "%s/%s%d", parentdir,
746 len = readlink(linkname, fname, MAXNAMELEN);
748 afs_snprintf(filename, sizeof filename, "%s/%s%d",
749 rootdir, OFILE, vn.vnode);
752 afs_snprintf(filename, sizeof filename, "%s/%s",
756 /* Read the link in, delete it, and then create it */
757 readdata(buf, vn.dataSize);
759 /* If a mountpoint, change its link path to mountroot */
761 if (((buf[0] == '%') || (buf[0] == '#'))
762 && (buf[s - 1] == '.')) {
763 /* This is a symbolic link */
764 buf[s - 1] = 0; /* Remove prefix '.' */
765 strcpy(lname, &buf[1]); /* Remove postfix '#' or '%' */
766 strcpy(buf, mntroot);
771 code = symlink(buf, filename);
774 "Error creating symlink %s -> %s code=%d;%d\n",
775 filename, buf, code, errno);
778 /* Remove the symbolic link */
783 fprintf(stderr, "Unknown Vnode block\n");
795 return ((afs_int32) tag);
799 struct cmd_syndesc *as;
802 int code = 0, c, len;
803 afs_int32 type, count, vcount;
805 struct dirent *dirE, *dirF;
806 char fname[MAXNAMELEN], name[MAXNAMELEN], lname[MAXNAMELEN],
808 char thisdir[MAXPATHLEN], *t;
809 struct DumpHeader dh; /* Defined in dump.h */
810 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
811 extern char *getwd();
812 #define getcwd(x,y) getwd(x)
815 if (as->parms[0].items) { /* -file <dumpfile> */
816 dumpfile = fopen(as->parms[0].items->data, "r");
818 fprintf(stderr, "Cannot open '%s'. Code = %d\n",
819 as->parms[0].items->data, errno);
823 dumpfile = (FILE *) stdin; /* use stdin */
826 /* Read the dump header. From it we get the volume name */
827 type = ntohl(readvalue(1));
829 fprintf(stderr, "Expected DumpHeader\n");
833 type = ReadDumpHeader(&dh);
835 /* Get the root directory we restore to */
836 if (as->parms[1].items) { /* -dir <rootdir> */
837 strcpy(rootdir, as->parms[1].items->data);
839 strcpy(rootdir, ".");
841 strcat(rootdir, "/");
843 /* Append the RW volume name to the root directory */
844 strcat(rootdir, dh.volumeName);
845 len = strlen(rootdir);
846 if (strcmp(".backup", rootdir + len - 7) == 0) {
847 rootdir[len - 7] = 0;
848 } else if (strcmp(".readonly", rootdir + len - 9) == 0) {
849 rootdir[len - 9] = 0;
852 /* Append the extension we asked for */
853 if (as->parms[2].items) {
854 strcat(rootdir, as->parms[2].items->data); /* -extension <ext> */
857 /* The mountpoint root is either specifid in -mountpoint
858 * or -dir or the current working dir.
860 if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint or -dir */
861 t = (char *)getcwd(thisdir, MAXPATHLEN); /* remember current dir */
864 "Cannot get pathname of current working directory: %s\n",
869 /* Change to the mount point dir */
871 chdir((as->parms[3].items ? as->parms[3].items->data : as->
872 parms[1].items->data));
874 fprintf(stderr, "Mount point directory not found: Error = %d\n",
878 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get its full pathname */
881 "Cannot determine pathname of mount point root directory: %s\n",
886 strcat(mntroot, "/"); /* append '/' to end of it */
887 code = chdir(thisdir); /* return to original working dir */
889 fprintf(stderr, "Cannot find working directory: Error = %d\n",
893 } else { /* use current directory */
894 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get full pathname of current dir */
897 "Cannot determine pathname of current working directory: %s\n",
903 strcat(mntroot, "/"); /* append '/' to end of it */
905 /* Set the umask for the restore */
906 if (as->parms[4].items) { /* -umask */
908 mask = strtol(as->parms[4].items->data, 0, 8);
909 fprintf(stderr, "Umask set to 0%03o\n", mask);
913 fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
914 dh.volumeName, rootdir);
915 code = mkdir(rootdir, 0777);
916 if ((code < 0) && (errno != EEXIST)) {
917 fprintf(stderr, "Error creating directory %s code=%d;%d\n", rootdir,
921 for (count = 1; type == 2; count++) {
922 type = ReadVolumeHeader(count);
923 for (vcount = 1; type == 3; vcount++)
924 type = ReadVNode(vcount);
928 fprintf(stderr, "Expected End-of-Dump\n");
934 /* For incremental restores, Follow each directory link and
935 * remove an "AFSFile" links.
938 fprintf(stderr, "An incremental dump.\n");
939 dirP = opendir(rootdir);
940 while (dirP && (dirE = readdir(dirP))) {
941 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
942 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
944 dirQ = opendir(name);
945 while (dirQ && (dirF = readdir(dirQ))) {
946 if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
947 afs_snprintf(name, sizeof name, "%s/%s/%s", rootdir,
948 dirE->d_name, dirF->d_name);
953 } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
954 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
962 /* Now go through and remove all the directory links */
963 dirP = opendir(rootdir);
964 while (dirP && (dirE = readdir(dirP))) {
965 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
966 afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
979 struct cmd_syndesc *ts;
984 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
985 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
986 cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
987 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
988 cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL,
990 cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
992 return cmd_Dispatch(argc, argv);