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>
46 #include <afs/afsint.h>
49 #include <afs/ihandle.h>
50 #include <afs/vnode.h>
51 #include <afs/volume.h>
56 #include <sys/param.h>
57 #include <sys/types.h>
61 #include <netinet/in.h>
68 char rootdir[MAXPATHLEN];
69 char mntroot[MAXPATHLEN];
70 #define ADIR "AFSDir-"
71 #define AFILE "AFSFile-"
72 #define ODIR "__ORPHANEDIR__."
73 #define OFILE "__ORPHANFILE__."
88 s = sizeof(value) - size;
90 fprintf(stderr, "Too much data in afs_int32\n");
94 code = fread(&ptr[s], 1, size, dumpfile);
96 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
108 code = fread(&value, 1, 1, dumpfile);
110 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
115 #define BUFSIZE 16384
119 readdata(char *buffer, afs_sfsize_t size)
126 s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
127 code = fread(buf, 1, s, dumpfile);
129 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
133 code = fread(buffer, 1, size, dumpfile);
136 fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
138 fprintf(stderr, "Read %d bytes out of %" AFS_INT64_FMT "\n", code, (afs_uintmax_t)size);
140 if ((code >= 0) && (code < BUFSIZE))
141 buffer[size] = 0; /* Add null char at end */
146 ReadDumpHeader(struct DumpHeader *dh)
152 /* memset(&dh, 0, sizeof(dh)); */
154 magic = ntohl(readvalue(4));
155 dh->version = ntohl(readvalue(4));
162 dh->volumeId = ntohl(readvalue(4));
166 for (i = 0, c = 'a'; c != '\0'; i++) {
167 dh->volumeName[i] = c = readchar();
169 dh->volumeName[i] = c;
173 dh->nDumpTimes = ntohl(readvalue(2)) >> 1;
174 for (i = 0; i < dh->nDumpTimes; i++) {
175 dh->dumpTimes[i].from = ntohl(readvalue(4));
176 dh->dumpTimes[i].to = ntohl(readvalue(4));
186 return ((afs_int32) tag);
189 struct volumeHeader {
191 char volumeName[100];
193 afs_int32 uniquifier;
200 afs_int32 accountNumber;
202 afs_int32 creationDate;
203 afs_int32 accessDate;
204 afs_int32 updateDate;
205 afs_int32 expirationDate;
206 afs_int32 backupDate;
207 afs_int32 dayUseDate;
210 afs_int32 weekUse[100]; /* weekCount of these */
215 afs_int32 volUpdateCounter;
219 ReadVolumeHeader(afs_int32 count)
221 struct volumeHeader vh;
225 /* memset(&vh, 0, sizeof(vh)); */
232 vh.volumeId = ntohl(readvalue(4));
236 (void)ntohl(readvalue(4)); /* version stamp - ignore */
240 for (i = 0, c = 'a'; c != '\0'; i++) {
241 vh.volumeName[i] = c = readchar();
243 vh.volumeName[i] = c;
247 vh.inService = ntohl(readvalue(1));
251 vh.blessed = ntohl(readvalue(1));
255 vh.uniquifier = ntohl(readvalue(4));
259 vh.volType = ntohl(readvalue(1));
263 vh.parentVol = ntohl(readvalue(4));
267 vh.cloneId = ntohl(readvalue(4));
271 vh.maxQuota = ntohl(readvalue(4));
275 vh.minQuota = ntohl(readvalue(4));
279 vh.diskUsed = ntohl(readvalue(4));
283 vh.fileCount = ntohl(readvalue(4));
287 vh.accountNumber = ntohl(readvalue(4));
291 vh.owner = ntohl(readvalue(4));
295 vh.creationDate = ntohl(readvalue(4));
299 vh.accessDate = ntohl(readvalue(4));
303 vh.updateDate = ntohl(readvalue(4));
307 vh.expirationDate = ntohl(readvalue(4));
311 vh.backupDate = ntohl(readvalue(4));
315 for (i = 0, c = 'a'; c != '\0'; i++) {
316 vh.message[i] = c = readchar();
318 vh.volumeName[i] = c;
322 vh.weekCount = ntohl(readvalue(2));
323 for (i = 0; i < vh.weekCount; i++) {
324 vh.weekUse[i] = ntohl(readvalue(4));
329 for (i = 0, c = 'a'; c != '\0'; i++) {
330 vh.motd[i] = c = readchar();
335 vh.dayUseDate = ntohl(readvalue(4));
339 vh.dayUse = ntohl(readvalue(4));
343 readvalue(4); /*volUpCounter*/
352 return ((afs_int32) tag);
357 afs_int32 uniquifier;
360 afs_int32 dataVersion;
361 afs_int32 unixModTime;
362 afs_int32 servModTime;
370 struct acl_accessList {
371 int size; /*size of this access list in bytes, including MySize itself */
372 int version; /*to deal with upward compatibility ; <= ACL_ACLVERSION */
374 int positive; /* number of positive entries */
375 int negative; /* number of minus entries */
376 struct acl_accessEntry {
377 int id; /*internally-used ID of user or group */
378 int rights; /*mask */
382 afs_sfsize_t dataSize;
385 #define MAXNAMELEN 256
388 ReadVNode(afs_int32 count)
393 char dirname[MAXNAMELEN], linkname[MAXNAMELEN], lname[MAXNAMELEN];
394 char parentdir[MAXNAMELEN], vflink[MAXNAMELEN];
395 char filename[MAXNAMELEN], fname[MAXNAMELEN];
401 /* memset(&vn, 0, sizeof(vn)); */
407 vn.vnode = ntohl(readvalue(4));
408 vn.uniquifier = ntohl(readvalue(4));
415 vn.type = ntohl(readvalue(1));
419 vn.linkCount = ntohl(readvalue(2));
423 vn.dataVersion = ntohl(readvalue(4));
427 vn.unixModTime = ntohl(readvalue(4));
431 vn.servModTime = ntohl(readvalue(4));
435 vn.author = ntohl(readvalue(4));
439 vn.owner = ntohl(readvalue(4));
443 vn.group = ntohl(readvalue(4));
447 vn.modebits = ntohl(readvalue(2));
451 vn.parent = ntohl(readvalue(4));
455 readdata(vn.acl, 192); /* Skip ACL data */
459 hi = ntohl(readvalue(4));
460 lo = ntohl(readvalue(4));
461 FillInt64(vn.dataSize, hi, lo);
465 vn.dataSize = ntohl(readvalue(4));
468 /* parentdir is the name of this dir's vnode-file-link
469 * or this file's parent vnode-file-link.
470 * "./AFSDir-<#>". It's a symbolic link to its real dir.
471 * The parent dir and symbolic link to it must exist.
473 vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
475 strncpy(parentdir, rootdir, sizeof parentdir);
477 afs_snprintf(parentdir, sizeof parentdir, "%s/%s%d", rootdir,
480 len = readlink(parentdir, linkname, MAXNAMELEN);
482 /* parentdir does not exist. So create an orphan dir.
483 * and then link the parentdir to the orphaned dir.
485 afs_snprintf(linkname, sizeof linkname, "%s/%s%d",
486 rootdir, ODIR, vnode);
487 code = mkdir(linkname, 0777);
488 if ((code < 0) && (errno != EEXIST)) {
490 "Error creating directory %s code=%d;%d\n",
491 linkname, code, errno);
494 /* Link the parentdir to it - now parentdir exists */
495 afs_snprintf(linkname, sizeof linkname, "%s%d/", ODIR,
497 code = symlink(linkname, parentdir);
500 "Error creating symlink %s -> %s code=%d;%d\n",
501 parentdir, linkname, code, errno);
508 /* We read the directory entries. If the entry is a
509 * directory, the subdir is created and the root dir
510 * will contain a link to it. If its a file, we only
511 * create a symlink in the dir to the file name.
530 unsigned short pgcount;
538 struct Pageheader header;
540 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.
575 /* dirname is the directory to create.
576 * vflink is what will link to it.
578 afs_snprintf(dirname, sizeof dirname, "%s/%s",
579 parentdir, this_name);
580 afs_snprintf(vflink, sizeof vflink, "%s/%s%d",
581 rootdir, ADIR, this_vn);
583 /* The link and directory may already exist */
584 len = readlink(vflink, linkname, MAXNAMELEN);
586 /* Doesn't already exist - so create the directory.
587 * umask will pare the mode bits down.
589 code = mkdir(dirname, 0777);
590 if ((code < 0) && (errno != EEXIST)) {
592 "Error creating directory %s code=%d;%d\n",
593 dirname, code, errno);
596 /* Does already exist - so move the directory.
597 * It was created originally as orphaned.
599 linkname[len - 1] = '\0'; /* remove '/' at end */
600 afs_snprintf(lname, sizeof lname, "%s/%s",
602 code = rename(lname, dirname);
605 "Error renaming %s to %s code=%d;%d\n",
606 lname, dirname, code, errno);
610 /* Now create/update the link to the new/moved directory */
612 afs_snprintf(dirname, sizeof dirname, "%s/",
615 afs_snprintf(dirname, sizeof dirname,
616 "%s%d/%s/", ADIR, vn.vnode,
619 code = symlink(dirname, vflink);
622 "Error creating symlink %s -> %s code=%d;%d\n",
623 vflink, dirname, code, errno);
627 /* For a file entry, we remember the name of the file
628 * by creating a link within the directory. Restoring
629 * the file will later remove the link.
632 /*AFILEENTRY*/ afs_snprintf(vflink,
634 "%s/%s%d", parentdir,
637 code = symlink(this_name, vflink);
638 if ((code < 0) && (errno != EEXIST)) {
640 "Error creating symlink %s -> %s code=%d;%d\n",
641 vflink, page0->entry[j].name, code,
650 else if (vn.type == 1) {
652 /* A file vnode. So create it into the desired directory. A
653 * link should exist in the directory naming the file.
657 afs_sfsize_t size, s;
659 /* Check if its vnode-file-link exists. If not,
660 * then the file will be an orphaned file.
663 afs_snprintf(filename, sizeof filename, "%s/%s%d", parentdir,
665 len = readlink(filename, fname, MAXNAMELEN);
667 afs_snprintf(filename, sizeof filename, "%s/%s%d",
668 rootdir, OFILE, vn.vnode);
669 lfile = 0; /* no longer a linked file; a direct path */
672 /* Create a mode for the file. Use the owner bits and
673 * duplicate them across group and other. The umask
674 * will remove what we don't want.
676 mode = (vn.modebits >> 6) & 0x7;
677 mode |= (mode << 6) | (mode << 3);
679 /* Write the file out */
680 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
683 s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
684 code = fread(buf, 1, s, dumpfile);
686 (void)write(fid, buf, code);
691 fprintf(stderr, "Code = %d; Errno = %d\n", code,
695 (void)afs_snprintf(tmp, sizeof tmp,
696 "Read %llu bytes out of %llu",
697 (afs_uintmax_t) (vn.dataSize -
699 (afs_uintmax_t) vn.dataSize);
700 fprintf(stderr, "%s\n", tmp);
707 fprintf(stderr, " File %s (%s) is incomplete\n",
711 /* Remove the link to the file */
717 else if (vn.type == 3) {
719 /* A symlink vnode. So read it into the desired directory. This could
720 * also be a mount point. If the volume is being restored to AFS, this
721 * will become a mountpoint. If not, it becomes a symlink to no-where.
725 /* Check if its vnode-file-link exists and create pathname
726 * of the symbolic link. If it doesn't exist,
727 * then the link will be an orphaned link.
729 afs_snprintf(linkname, sizeof linkname, "%s/%s%d", parentdir,
731 len = readlink(linkname, fname, MAXNAMELEN);
733 afs_snprintf(filename, sizeof filename, "%s/%s%d",
734 rootdir, OFILE, vn.vnode);
737 afs_snprintf(filename, sizeof filename, "%s/%s",
741 /* Read the link in, delete it, and then create it */
742 readdata(buf, vn.dataSize);
744 /* If a mountpoint, change its link path to mountroot */
746 if (((buf[0] == '%') || (buf[0] == '#'))
747 && (buf[s - 1] == '.')) {
748 /* This is a symbolic link */
749 buf[s - 1] = 0; /* Remove prefix '.' */
750 strcpy(lname, &buf[1]); /* Remove postfix '#' or '%' */
751 strcpy(buf, mntroot);
756 code = symlink(buf, filename);
759 "Error creating symlink %s -> %s code=%d;%d\n",
760 filename, buf, code, errno);
763 /* Remove the symbolic link */
768 fprintf(stderr, "Unknown Vnode block\n");
780 return ((afs_int32) tag);
784 WorkerBee(struct cmd_syndesc *as, void *arock)
787 afs_int32 type, count, vcount;
789 struct dirent *dirE, *dirF;
790 char name[MAXNAMELEN];
791 char thisdir[MAXPATHLEN], *t;
792 struct DumpHeader dh; /* Defined in dump.h */
793 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
794 extern char *getwd();
795 #define getcwd(x,y) getwd(x)
798 if (as->parms[0].items) { /* -file <dumpfile> */
799 dumpfile = fopen(as->parms[0].items->data, "r");
801 fprintf(stderr, "Cannot open '%s'. Code = %d\n",
802 as->parms[0].items->data, errno);
806 dumpfile = (FILE *) stdin; /* use stdin */
809 /* Read the dump header. From it we get the volume name */
810 type = ntohl(readvalue(1));
812 fprintf(stderr, "Expected DumpHeader\n");
816 type = ReadDumpHeader(&dh);
818 /* Get the root directory we restore to */
819 if (as->parms[1].items) { /* -dir <rootdir> */
820 strcpy(rootdir, as->parms[1].items->data);
822 strcpy(rootdir, ".");
824 strcat(rootdir, "/");
826 /* Append the RW volume name to the root directory */
827 strcat(rootdir, dh.volumeName);
828 len = strlen(rootdir);
829 if (strcmp(".backup", rootdir + len - 7) == 0) {
830 rootdir[len - 7] = 0;
831 } else if (strcmp(".readonly", rootdir + len - 9) == 0) {
832 rootdir[len - 9] = 0;
835 /* Append the extension we asked for */
836 if (as->parms[2].items) {
837 strcat(rootdir, as->parms[2].items->data); /* -extension <ext> */
840 /* The mountpoint root is either specifid in -mountpoint
841 * or -dir or the current working dir.
843 if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint or -dir */
844 t = (char *)getcwd(thisdir, MAXPATHLEN); /* remember current dir */
847 "Cannot get pathname of current working directory: %s\n",
852 /* Change to the mount point dir */
854 chdir((as->parms[3].items ? as->parms[3].items->data : as->
855 parms[1].items->data));
857 fprintf(stderr, "Mount point directory not found: Error = %d\n",
861 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get its full pathname */
864 "Cannot determine pathname of mount point root directory: %s\n",
869 strcat(mntroot, "/"); /* append '/' to end of it */
870 code = chdir(thisdir); /* return to original working dir */
872 fprintf(stderr, "Cannot find working directory: Error = %d\n",
876 } else { /* use current directory */
877 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get full pathname of current dir */
880 "Cannot determine pathname of current working directory: %s\n",
886 strcat(mntroot, "/"); /* append '/' to end of it */
888 /* Set the umask for the restore */
889 if (as->parms[4].items) { /* -umask */
891 mask = strtol(as->parms[4].items->data, 0, 8);
892 fprintf(stderr, "Umask set to 0%03o\n", mask);
896 fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
897 dh.volumeName, rootdir);
898 code = mkdir(rootdir, 0777);
899 if ((code < 0) && (errno != EEXIST)) {
900 fprintf(stderr, "Error creating directory %s code=%d;%d\n", rootdir,
904 for (count = 1; type == 2; count++) {
905 type = ReadVolumeHeader(count);
906 for (vcount = 1; type == 3; vcount++)
907 type = ReadVNode(vcount);
911 fprintf(stderr, "Expected End-of-Dump\n");
917 /* For incremental restores, Follow each directory link and
918 * remove an "AFSFile" links.
921 fprintf(stderr, "An incremental dump.\n");
922 dirP = opendir(rootdir);
923 while (dirP && (dirE = readdir(dirP))) {
924 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
925 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
927 dirQ = opendir(name);
928 while (dirQ && (dirF = readdir(dirQ))) {
929 if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
930 afs_snprintf(name, sizeof name, "%s/%s/%s", rootdir,
931 dirE->d_name, dirF->d_name);
936 } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
937 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
945 /* Now go through and remove all the directory links */
946 dirP = opendir(rootdir);
947 while (dirP && (dirE = readdir(dirP))) {
948 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
949 afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
959 main(int argc, char **argv)
961 struct cmd_syndesc *ts;
965 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
966 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
967 cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
968 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
969 cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL,
971 cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
973 return cmd_Dispatch(argc, argv);