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>
47 #include <afs/afsint.h>
50 #include <afs/ihandle.h>
51 #include <afs/vnode.h>
52 #include <afs/volume.h>
57 #include <sys/param.h>
58 #include <sys/types.h>
62 #include <netinet/in.h>
76 char rootdir[MAXPATHLEN];
77 char mntroot[MAXPATHLEN];
78 #define ADIR "AFSDir-"
79 #define AFILE "AFSFile-"
80 #define ODIR "__ORPHANEDIR__."
81 #define OFILE "__ORPHANFILE__."
86 afs_int32 readvalue(size)
93 ptr = (char *) &value;
95 s = sizeof(value) - size;
97 fprintf (stderr, "Too much data in afs_int32\n");
101 code = fread (&ptr[s], 1, size, dumpfile);
102 if (code != size) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
114 code = fread (&value, 1, 1, dumpfile);
115 if (code != 1) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
120 #define BUFSIZE 16384
123 char readdata(buffer, size)
132 s = ((size > BUFSIZE) ? BUFSIZE : size);
133 code = fread(buf, 1, s, dumpfile);
134 if (code != s) fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
139 code = fread (buffer, 1, size, dumpfile);
142 fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
144 fprintf (stderr, "Read %d bytes out of %d\n", code, size);
146 if ((code >= 0) && (code < BUFSIZE))
147 buffer[size] = 0; /* Add null char at end */
151 afs_int32 ReadDumpHeader(dh)
152 struct DumpHeader *dh; /* Defined in dump.h */
158 /* memset(&dh, 0, sizeof(dh)); */
160 magic = ntohl(readvalue(4));
161 dh->version = ntohl(readvalue(4));
168 dh->volumeId = ntohl(readvalue(4));
172 for (i=0, c='a'; c!='\0'; i++) {
173 dh->volumeName[i] = c = readchar();
175 dh->volumeName[i] = c;
179 dh->nDumpTimes = ntohl(readvalue(2)) >> 1;
180 for (i=0; i<dh->nDumpTimes; i++) {
181 dh->dumpTimes[i].from = ntohl(readvalue(4));
182 dh->dumpTimes[i].to = ntohl(readvalue(4));
192 return((afs_int32)tag);
198 char volumeName[100];
200 afs_int32 uniquifier;
207 afs_int32 accountNumber;
209 afs_int32 creationDate;
210 afs_int32 accessDate;
211 afs_int32 updateDate;
212 afs_int32 expirationDate;
213 afs_int32 backupDate;
214 afs_int32 dayUseDate;
217 afs_int32 weekUse[100]; /* weekCount of these */
224 afs_int32 ReadVolumeHeader(count)
227 struct volumeHeader vh;
228 int code, i, done, entries;
231 /* memset(&vh, 0, sizeof(vh)); */
238 vh.volumeId = ntohl(readvalue(4));
242 ntohl(readvalue(4)); /* version stamp - ignore */
246 for (i=0, c='a'; c!='\0'; i++) {
247 vh.volumeName[i] = c = readchar();
249 vh.volumeName[i] = c;
253 vh.inService = ntohl(readvalue(1));
257 vh.blessed = ntohl(readvalue(1));
261 vh.uniquifier = ntohl(readvalue(4));
265 vh.volType = ntohl(readvalue(1));
269 vh.parentVol = ntohl(readvalue(4));
273 vh.cloneId = ntohl(readvalue(4));
277 vh.maxQuota = ntohl(readvalue(4));
281 vh.minQuota = ntohl(readvalue(4));
285 vh.diskUsed = ntohl(readvalue(4));
289 vh.fileCount = ntohl(readvalue(4));
293 vh.accountNumber = ntohl(readvalue(4));
297 vh.owner = ntohl(readvalue(4));
301 vh.creationDate = ntohl(readvalue(4));
305 vh.accessDate = ntohl(readvalue(4));
309 vh.updateDate = ntohl(readvalue(4));
313 vh.expirationDate = ntohl(readvalue(4));
317 vh.backupDate = ntohl(readvalue(4));
321 for (i=0, c='a'; c!='\0'; i++) {
322 vh.message[i] = c = readchar();
324 vh.volumeName[i] = c;
328 vh.weekCount = ntohl(readvalue(2));
329 for (i=0; i<vh.weekCount; i++) {
330 vh.weekUse[i] = ntohl(readvalue(4));
335 for (i=0, c='a'; c!='\0'; i++) {
336 vh.motd[i] = c = readchar();
341 vh.dayUseDate = ntohl(readvalue(4));
345 vh.dayUse = ntohl(readvalue(4));
354 return((afs_int32)tag);
360 afs_int32 uniquifier;
363 afs_int32 dataVersion;
364 afs_int32 unixModTime;
365 afs_int32 servModTime;
373 struct acl_accessList
375 int size; /*size of this access list in bytes, including MySize itself*/
376 int version; /*to deal with upward compatibility ; <= ACL_ACLVERSION*/
378 int positive; /* number of positive entries */
379 int negative; /* number of minus entries */
380 struct acl_accessEntry
382 int id; /*internally-used ID of user or group*/
390 #define MAXNAMELEN 256
392 afs_int32 ReadVNode(count)
396 int code, i, done, entries;
398 char dirname[MAXNAMELEN], linkname[MAXNAMELEN], lname[MAXNAMELEN];
399 char parentdir[MAXNAMELEN], vflink[MAXNAMELEN];
400 char filename[MAXNAMELEN], fname[MAXNAMELEN];
405 /* memset(&vn, 0, sizeof(vn)); */
411 vn.vnode = ntohl(readvalue(4));
412 vn.uniquifier = ntohl(readvalue(4));
419 vn.type = ntohl(readvalue(1));
423 vn.linkCount = ntohl(readvalue(2));
427 vn.dataVersion = ntohl(readvalue(4));
431 vn.unixModTime = ntohl(readvalue(4));
435 vn.servModTime = ntohl(readvalue(4));
439 vn.author = ntohl(readvalue(4));
443 vn.owner = ntohl(readvalue(4));
447 vn.group = ntohl(readvalue(4));
451 vn.modebits = ntohl(readvalue(2));
455 vn.parent = ntohl(readvalue(4));
459 readdata(vn.acl, 192); /* Skip ACL data */
463 vn.dataSize = ntohl(readvalue(4));
465 /* parentdir is the name of this dir's vnode-file-link
466 * or this file's parent vnode-file-link.
467 * "./AFSDir-<#>". It's a symbolic link to its real dir.
468 * The parent dir and symbolic link to it must exist.
470 vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
472 strncpy(parentdir, rootdir, sizeof parentdir);
474 afs_snprintf(parentdir, sizeof parentdir,
475 "%s/%s%d", rootdir, ADIR, vnode);
477 len = readlink(parentdir, linkname, MAXNAMELEN);
479 /* parentdir does not exist. So create an orphan dir.
480 * and then link the parentdir to the orphaned dir.
482 afs_snprintf(linkname, sizeof linkname,
483 "%s/%s%d", rootdir, ODIR, vnode);
484 code = mkdir(linkname, 0777);
485 if ((code < 0) && (errno != EEXIST)) {
486 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
487 linkname, code, errno);
490 /* Link the parentdir to it - now parentdir exists */
491 afs_snprintf(linkname, sizeof linkname,
492 "%s%d/", ODIR, vnode);
493 code = symlink(linkname, parentdir);
495 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
496 parentdir, linkname, code, errno);
503 /* We read the directory entries. If the entry is a
504 * directory, the subdir is created and the root dir
505 * will contain a link to it. If its a file, we only
506 * create a symlink in the dir to the file name.
528 unsigned short pgcount;
537 struct Pageheader header;
539 unsigned short hashTable[128];
544 struct DirHeader header;
545 struct DirEntry entry[1];
550 buffer = (char *)malloc(vn.dataSize);
552 readdata(buffer, vn.dataSize);
553 page0 = (struct Page0 *)buffer;
555 /* Step through each bucket in the hash table, i,
556 * and follow each element in the hash chain, j.
557 * This gives us each entry of the dir.
559 for (i=0; i<128; i++) {
560 for (j=ntohs(page0->header.hashTable[i]); j;
561 j=ntohs(page0->entry[j].next)) {
563 this_vn = ntohl(page0->entry[j].fid.vnode);
564 this_name = page0->entry[j].name;
566 if ((strcmp(this_name,"." ) == 0) ||
567 (strcmp(this_name,"..") == 0))
568 continue; /* Skip these */
570 /* For a directory entry, create it. Then create the
571 * link (from the rootdir) to this directory.
573 if (this_vn & 1) { /*ADIRENTRY*/
574 /* dirname is the directory to create.
575 * vflink is what will link to it.
577 afs_snprintf(dirname, sizeof dirname,
578 "%s/%s", parentdir, this_name);
579 afs_snprintf(vflink, sizeof vflink,
580 "%s/%s%d", rootdir, ADIR, this_vn);
582 /* The link and directory may already exist */
583 len = readlink(vflink, linkname, MAXNAMELEN);
585 /* Doesn't already exist - so create the directory.
586 * umask will pare the mode bits down.
588 code = mkdir(dirname, 0777);
589 if ((code < 0) && (errno != EEXIST)) {
590 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
591 dirname, code, errno);
594 /* Does already exist - so move the directory.
595 * It was created originally as orphaned.
597 linkname[len-1] = '\0'; /* remove '/' at end */
598 afs_snprintf(lname, sizeof lname,
599 "%s/%s", rootdir, linkname);
600 code = rename(lname, dirname);
602 fprintf(stderr, "Error renaming %s to %s code=%d;%d\n",
603 lname, dirname, code, errno);
607 /* Now create/update the link to the new/moved directory */
609 afs_snprintf(dirname, sizeof dirname,
612 afs_snprintf(dirname, sizeof dirname,
613 "%s%d/%s/", ADIR, vn.vnode, this_name);
615 code = symlink(dirname, vflink);
617 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
618 vflink, dirname, code, errno);
622 /* For a file entry, we remember the name of the file
623 * by creating a link within the directory. Restoring
624 * the file will later remove the link.
628 afs_snprintf(vflink, sizeof vflink,
629 "%s/%s%d", parentdir, AFILE, this_vn);
631 code = symlink(this_name, vflink);
632 if ((code < 0) && (errno != EEXIST)) {
633 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
634 vflink, page0->entry[j].name, code, errno);
642 else if (vn.type == 1) {
644 /* A file vnode. So create it into the desired directory. A
645 * link should exist in the directory naming the file.
651 /* Check if its vnode-file-link exists. If not,
652 * then the file will be an orphaned file.
655 afs_snprintf(filename, sizeof filename,
656 "%s/%s%d", parentdir, AFILE, vn.vnode);
657 len = readlink(filename, fname, MAXNAMELEN);
659 afs_snprintf(filename, sizeof filename,
660 "%s/%s%d", rootdir, OFILE, vn.vnode);
661 lfile = 0; /* no longer a linked file; a direct path */
664 /* Create a mode for the file. Use the owner bits and
665 * duplicate them across group and other. The umask
666 * will remove what we don't want.
668 mode = (vn.modebits >> 6) & 0x7;
669 mode |= (mode << 6) | (mode << 3);
671 /* Write the file out */
672 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
675 s = ((size > BUFSIZE) ? BUFSIZE : size);
676 code = fread(buf, 1, s, dumpfile);
678 (void) write(fid, buf, code);
683 fprintf (stderr, "Code = %d; Errno = %d\n", code, errno);
686 (void) afs_snprintf(tmp, sizeof tmp,
687 "Read %llu bytes out of %llu",
688 (afs_uintmax_t)(vn.dataSize - size),
689 (afs_uintmax_t)vn.dataSize);
690 fprintf (stderr, "%s\n", tmp);
697 fprintf(stderr, " File %s (%s) is incomplete\n", filename, fname);
700 /* Remove the link to the file */
706 else if (vn.type == 3) {
708 /* A symlink vnode. So read it into the desired directory. This could
709 * also be a mount point. If the volume is being restored to AFS, this
710 * will become a mountpoint. If not, it becomes a symlink to no-where.
715 /* Check if its vnode-file-link exists and create pathname
716 * of the symbolic link. If it doesn't exist,
717 * then the link will be an orphaned link.
719 afs_snprintf(linkname, sizeof linkname,
720 "%s/%s%d", parentdir, AFILE, vn.vnode);
721 len = readlink(linkname, fname, MAXNAMELEN);
723 afs_snprintf(filename, sizeof filename,
724 "%s/%s%d", rootdir, OFILE, vn.vnode);
727 afs_snprintf(filename, sizeof filename,
728 "%s/%s", parentdir, fname);
731 /* Read the link in, delete it, and then create it */
732 readdata(buf, vn.dataSize);
734 /* If a mountpoint, change its link path to mountroot */
736 if ( ((buf[0] == '%') || (buf[0] == '#')) && (buf[s-1] == '.') ) {
737 /* This is a symbolic link */
738 buf[s-1] = 0; /* Remove prefix '.' */
739 strcpy(lname, &buf[1]); /* Remove postfix '#' or '%' */
740 strcpy(buf, mntroot);
745 code = symlink(buf, filename);
747 fprintf(stderr, "Error creating symlink %s -> %s code=%d;%d\n",
748 filename, buf, code, errno);
751 /* Remove the symbolic link */
755 fprintf (stderr, "Unknown Vnode block\n");
767 return((afs_int32)tag);
771 struct cmd_syndesc *as;
775 afs_int32 type, count, vcount;
777 struct dirent *dirE, *dirF;
778 char fname[MAXNAMELEN], name[MAXNAMELEN], lname[MAXNAMELEN], mname[MAXNAMELEN];
779 char thisdir[MAXPATHLEN], *t;
780 struct DumpHeader dh; /* Defined in dump.h */
781 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
782 extern char *getwd();
783 #define getcwd(x,y) getwd(x)
786 if (as->parms[0].items) { /* -file <dumpfile> */
787 dumpfile = fopen(as->parms[0].items->data, "r");
789 fprintf(stderr, "Cannot open '%s'. Code = %d\n",
790 as->parms[0].items->data, errno);
794 dumpfile = (FILE *)stdin; /* use stdin */
797 /* Read the dump header. From it we get the volume name */
798 type = ntohl(readvalue(1));
800 fprintf(stderr, "Expected DumpHeader\n");
804 type = ReadDumpHeader(&dh);
806 /* Get the root directory we restore to */
807 if (as->parms[1].items) { /* -dir <rootdir> */
808 strcpy(rootdir, as->parms[1].items->data);
814 /* Append the RW volume name to the root directory */
815 strcat(rootdir, dh.volumeName);
816 len = strlen(rootdir);
817 if (strcmp(".backup",rootdir+len-7) == 0) {
819 } else if (strcmp(".readonly",rootdir+len-9) == 0) {
823 /* Append the extension we asked for */
824 if (as->parms[2].items) {
825 strcat(rootdir, as->parms[2].items->data); /* -extension <ext> */
828 /* The mountpoint root is either specifid in -mountpoint
829 * or -dir or the current working dir.
831 if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint or -dir*/
832 t = (char *)getcwd(thisdir, MAXPATHLEN); /* remember current dir */
834 fprintf(stderr, "Cannot get pathname of current working directory: %s\n",
839 /* Change to the mount point dir */
840 code = chdir((as->parms[3].items ? as->parms[3].items->data :
841 as->parms[1].items->data));
843 fprintf(stderr, "Mount point directory not found: Error = %d\n", errno);
846 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get its full pathname */
848 fprintf(stderr, "Cannot determine pathname of mount point root directory: %s\n",
853 strcat(mntroot, "/"); /* append '/' to end of it */
854 code = chdir(thisdir); /* return to original working dir */
856 fprintf(stderr, "Cannot find working directory: Error = %d\n", errno);
859 } else { /* use current directory */
860 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get full pathname of current dir */
862 fprintf(stderr, "Cannot determine pathname of current working directory: %s\n",
868 strcat(mntroot, "/"); /* append '/' to end of it */
870 /* Set the umask for the restore */
871 if (as->parms[4].items) { /* -umask */
873 mask = strtol(as->parms[4].items->data, 0, 8);
874 fprintf(stderr, "Umask set to 0%03o\n", mask);
878 fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
879 dh.volumeName, rootdir);
880 code = mkdir(rootdir, 0777);
881 if ((code < 0) && (errno != EEXIST)) {
882 fprintf(stderr, "Error creating directory %s code=%d;%d\n",
883 rootdir, code, errno);
886 for (count=1; type==2; count++) {
887 type = ReadVolumeHeader(count);
888 for (vcount=1; type==3; vcount++)
889 type = ReadVNode(vcount);
893 fprintf(stderr, "Expected End-of-Dump\n");
899 /* For incremental restores, Follow each directory link and
900 * remove an "AFSFile" links.
903 fprintf(stderr, "An incremental dump.\n");
904 dirP = opendir(rootdir);
905 while (dirP && (dirE = readdir(dirP))) {
906 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
907 afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
908 dirQ = opendir(name);
909 while (dirQ && (dirF = readdir(dirQ))) {
910 if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
911 afs_snprintf(name, sizeof name,
912 "%s/%s/%s", rootdir, dirE->d_name, dirF->d_name);
917 } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
918 afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
925 /* Now go through and remove all the directory links */
926 dirP = opendir(rootdir);
927 while (dirP && (dirE = readdir(dirP))) {
928 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
929 afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
942 struct cmd_syndesc *ts;
947 ts=cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
948 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
949 cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
950 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
951 cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL, "mount point root");
952 cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
954 return cmd_Dispatch(argc, argv);