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 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];
400 /* memset(&vn, 0, sizeof(vn)); */
406 vn.vnode = ntohl(readvalue(4));
407 vn.uniquifier = ntohl(readvalue(4));
414 vn.type = ntohl(readvalue(1));
418 vn.linkCount = ntohl(readvalue(2));
422 vn.dataVersion = ntohl(readvalue(4));
426 vn.unixModTime = ntohl(readvalue(4));
430 vn.servModTime = ntohl(readvalue(4));
434 vn.author = ntohl(readvalue(4));
438 vn.owner = ntohl(readvalue(4));
442 vn.group = ntohl(readvalue(4));
446 vn.modebits = ntohl(readvalue(2));
450 vn.parent = ntohl(readvalue(4));
454 readdata(vn.acl, 192); /* Skip ACL data */
457 #ifdef AFS_LARGEFILE_ENV
461 hi = ntohl(readvalue(4));
462 lo = ntohl(readvalue(4));
463 FillInt64(vn.dataSize, hi, lo);
466 #endif /* !AFS_LARGEFILE_ENV */
469 vn.dataSize = ntohl(readvalue(4));
472 /* parentdir is the name of this dir's vnode-file-link
473 * or this file's parent vnode-file-link.
474 * "./AFSDir-<#>". It's a symbolic link to its real dir.
475 * The parent dir and symbolic link to it must exist.
477 vnode = ((vn.type == 2) ? vn.vnode : vn.parent);
479 strncpy(parentdir, rootdir, sizeof parentdir);
481 afs_snprintf(parentdir, sizeof parentdir, "%s/%s%d", rootdir,
484 len = readlink(parentdir, linkname, MAXNAMELEN);
486 /* parentdir does not exist. So create an orphan dir.
487 * and then link the parentdir to the orphaned dir.
489 afs_snprintf(linkname, sizeof linkname, "%s/%s%d",
490 rootdir, ODIR, vnode);
491 code = mkdir(linkname, 0777);
492 if ((code < 0) && (errno != EEXIST)) {
494 "Error creating directory %s code=%d;%d\n",
495 linkname, code, errno);
498 /* Link the parentdir to it - now parentdir exists */
499 afs_snprintf(linkname, sizeof linkname, "%s%d/", ODIR,
501 code = symlink(linkname, parentdir);
504 "Error creating symlink %s -> %s code=%d;%d\n",
505 parentdir, linkname, code, errno);
512 /* We read the directory entries. If the entry is a
513 * directory, the subdir is created and the root dir
514 * will contain a link to it. If its a file, we only
515 * create a symlink in the dir to the file name.
534 unsigned short pgcount;
542 struct Pageheader header;
544 unsigned short hashTable[128];
548 struct DirHeader header;
549 struct DirEntry entry[1];
554 buffer = (char *)malloc(vn.dataSize);
556 readdata(buffer, vn.dataSize);
557 page0 = (struct Page0 *)buffer;
559 /* Step through each bucket in the hash table, i,
560 * and follow each element in the hash chain, j.
561 * This gives us each entry of the dir.
563 for (i = 0; i < 128; i++) {
564 for (j = ntohs(page0->header.hashTable[i]); j;
565 j = ntohs(page0->entry[j].next)) {
567 this_vn = ntohl(page0->entry[j].fid.vnode);
568 this_name = page0->entry[j].name;
570 if ((strcmp(this_name, ".") == 0)
571 || (strcmp(this_name, "..") == 0))
572 continue; /* Skip these */
574 /* For a directory entry, create it. Then create the
575 * link (from the rootdir) to this directory.
579 /* dirname is the directory to create.
580 * vflink is what will link to it.
582 afs_snprintf(dirname, sizeof dirname, "%s/%s",
583 parentdir, this_name);
584 afs_snprintf(vflink, sizeof vflink, "%s/%s%d",
585 rootdir, ADIR, this_vn);
587 /* The link and directory may already exist */
588 len = readlink(vflink, linkname, MAXNAMELEN);
590 /* Doesn't already exist - so create the directory.
591 * umask will pare the mode bits down.
593 code = mkdir(dirname, 0777);
594 if ((code < 0) && (errno != EEXIST)) {
596 "Error creating directory %s code=%d;%d\n",
597 dirname, code, errno);
600 /* Does already exist - so move the directory.
601 * It was created originally as orphaned.
603 linkname[len - 1] = '\0'; /* remove '/' at end */
604 afs_snprintf(lname, sizeof lname, "%s/%s",
606 code = rename(lname, dirname);
609 "Error renaming %s to %s code=%d;%d\n",
610 lname, dirname, code, errno);
614 /* Now create/update the link to the new/moved directory */
616 afs_snprintf(dirname, sizeof dirname, "%s/",
619 afs_snprintf(dirname, sizeof dirname,
620 "%s%d/%s/", ADIR, vn.vnode,
623 code = symlink(dirname, vflink);
626 "Error creating symlink %s -> %s code=%d;%d\n",
627 vflink, dirname, code, errno);
631 /* For a file entry, we remember the name of the file
632 * by creating a link within the directory. Restoring
633 * the file will later remove the link.
636 /*AFILEENTRY*/ afs_snprintf(vflink,
638 "%s/%s%d", parentdir,
641 code = symlink(this_name, vflink);
642 if ((code < 0) && (errno != EEXIST)) {
644 "Error creating symlink %s -> %s code=%d;%d\n",
645 vflink, page0->entry[j].name, code,
654 else if (vn.type == 1) {
656 /* A file vnode. So create it into the desired directory. A
657 * link should exist in the directory naming the file.
661 afs_sfsize_t size, s;
663 /* Check if its vnode-file-link exists. If not,
664 * then the file will be an orphaned file.
667 afs_snprintf(filename, sizeof filename, "%s/%s%d", parentdir,
669 len = readlink(filename, fname, MAXNAMELEN);
671 afs_snprintf(filename, sizeof filename, "%s/%s%d",
672 rootdir, OFILE, vn.vnode);
673 lfile = 0; /* no longer a linked file; a direct path */
676 /* Create a mode for the file. Use the owner bits and
677 * duplicate them across group and other. The umask
678 * will remove what we don't want.
680 mode = (vn.modebits >> 6) & 0x7;
681 mode |= (mode << 6) | (mode << 3);
683 /* Write the file out */
684 fid = open(filename, (O_CREAT | O_WRONLY | O_TRUNC), mode);
687 s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
688 code = fread(buf, 1, s, dumpfile);
690 (void)write(fid, buf, code);
695 fprintf(stderr, "Code = %d; Errno = %d\n", code,
699 (void)afs_snprintf(tmp, sizeof tmp,
700 "Read %llu bytes out of %llu",
701 (afs_uintmax_t) (vn.dataSize -
703 (afs_uintmax_t) vn.dataSize);
704 fprintf(stderr, "%s\n", tmp);
711 fprintf(stderr, " File %s (%s) is incomplete\n",
715 /* Remove the link to the file */
721 else if (vn.type == 3) {
723 /* A symlink vnode. So read it into the desired directory. This could
724 * also be a mount point. If the volume is being restored to AFS, this
725 * will become a mountpoint. If not, it becomes a symlink to no-where.
729 /* Check if its vnode-file-link exists and create pathname
730 * of the symbolic link. If it doesn't exist,
731 * then the link will be an orphaned link.
733 afs_snprintf(linkname, sizeof linkname, "%s/%s%d", parentdir,
735 len = readlink(linkname, fname, MAXNAMELEN);
737 afs_snprintf(filename, sizeof filename, "%s/%s%d",
738 rootdir, OFILE, vn.vnode);
741 afs_snprintf(filename, sizeof filename, "%s/%s",
745 /* Read the link in, delete it, and then create it */
746 readdata(buf, vn.dataSize);
748 /* If a mountpoint, change its link path to mountroot */
750 if (((buf[0] == '%') || (buf[0] == '#'))
751 && (buf[s - 1] == '.')) {
752 /* This is a symbolic link */
753 buf[s - 1] = 0; /* Remove prefix '.' */
754 strcpy(lname, &buf[1]); /* Remove postfix '#' or '%' */
755 strcpy(buf, mntroot);
760 code = symlink(buf, filename);
763 "Error creating symlink %s -> %s code=%d;%d\n",
764 filename, buf, code, errno);
767 /* Remove the symbolic link */
772 fprintf(stderr, "Unknown Vnode block\n");
784 return ((afs_int32) tag);
788 WorkerBee(struct cmd_syndesc *as, void *arock)
791 afs_int32 type, count, vcount;
793 struct dirent *dirE, *dirF;
794 char name[MAXNAMELEN];
795 char thisdir[MAXPATHLEN], *t;
796 struct DumpHeader dh; /* Defined in dump.h */
797 #if 0/*ndef HAVE_GETCWD*/ /* XXX enable when autoconf happens */
798 extern char *getwd();
799 #define getcwd(x,y) getwd(x)
802 if (as->parms[0].items) { /* -file <dumpfile> */
803 dumpfile = fopen(as->parms[0].items->data, "r");
805 fprintf(stderr, "Cannot open '%s'. Code = %d\n",
806 as->parms[0].items->data, errno);
810 dumpfile = (FILE *) stdin; /* use stdin */
813 /* Read the dump header. From it we get the volume name */
814 type = ntohl(readvalue(1));
816 fprintf(stderr, "Expected DumpHeader\n");
820 type = ReadDumpHeader(&dh);
822 /* Get the root directory we restore to */
823 if (as->parms[1].items) { /* -dir <rootdir> */
824 strcpy(rootdir, as->parms[1].items->data);
826 strcpy(rootdir, ".");
828 strcat(rootdir, "/");
830 /* Append the RW volume name to the root directory */
831 strcat(rootdir, dh.volumeName);
832 len = strlen(rootdir);
833 if (strcmp(".backup", rootdir + len - 7) == 0) {
834 rootdir[len - 7] = 0;
835 } else if (strcmp(".readonly", rootdir + len - 9) == 0) {
836 rootdir[len - 9] = 0;
839 /* Append the extension we asked for */
840 if (as->parms[2].items) {
841 strcat(rootdir, as->parms[2].items->data); /* -extension <ext> */
844 /* The mountpoint root is either specifid in -mountpoint
845 * or -dir or the current working dir.
847 if ((as->parms[3].items) || (as->parms[1].items)) { /* -mountpoint or -dir */
848 t = (char *)getcwd(thisdir, MAXPATHLEN); /* remember current dir */
851 "Cannot get pathname of current working directory: %s\n",
856 /* Change to the mount point dir */
858 chdir((as->parms[3].items ? as->parms[3].items->data : as->
859 parms[1].items->data));
861 fprintf(stderr, "Mount point directory not found: Error = %d\n",
865 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get its full pathname */
868 "Cannot determine pathname of mount point root directory: %s\n",
873 strcat(mntroot, "/"); /* append '/' to end of it */
874 code = chdir(thisdir); /* return to original working dir */
876 fprintf(stderr, "Cannot find working directory: Error = %d\n",
880 } else { /* use current directory */
881 t = (char *)getcwd(mntroot, MAXPATHLEN); /* get full pathname of current dir */
884 "Cannot determine pathname of current working directory: %s\n",
890 strcat(mntroot, "/"); /* append '/' to end of it */
892 /* Set the umask for the restore */
893 if (as->parms[4].items) { /* -umask */
895 mask = strtol(as->parms[4].items->data, 0, 8);
896 fprintf(stderr, "Umask set to 0%03o\n", mask);
900 fprintf(stderr, "Restoring volume dump of '%s' to directory '%s'.\n",
901 dh.volumeName, rootdir);
902 code = mkdir(rootdir, 0777);
903 if ((code < 0) && (errno != EEXIST)) {
904 fprintf(stderr, "Error creating directory %s code=%d;%d\n", rootdir,
908 for (count = 1; type == 2; count++) {
909 type = ReadVolumeHeader(count);
910 for (vcount = 1; type == 3; vcount++)
911 type = ReadVNode(vcount);
915 fprintf(stderr, "Expected End-of-Dump\n");
921 /* For incremental restores, Follow each directory link and
922 * remove an "AFSFile" links.
925 fprintf(stderr, "An incremental dump.\n");
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,
931 dirQ = opendir(name);
932 while (dirQ && (dirF = readdir(dirQ))) {
933 if (strncmp(dirF->d_name, AFILE, strlen(AFILE)) == 0) {
934 afs_snprintf(name, sizeof name, "%s/%s/%s", rootdir,
935 dirE->d_name, dirF->d_name);
940 } else if (strncmp(dirE->d_name, AFILE, strlen(AFILE)) == 0) {
941 afs_snprintf(name, sizeof name, "%s/%s", rootdir,
949 /* Now go through and remove all the directory links */
950 dirP = opendir(rootdir);
951 while (dirP && (dirE = readdir(dirP))) {
952 if (strncmp(dirE->d_name, ADIR, strlen(ADIR)) == 0) {
953 afs_snprintf(name, sizeof name, "%s/%s", rootdir, dirE->d_name);
963 main(int argc, char **argv)
965 struct cmd_syndesc *ts;
969 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
970 cmd_AddParm(ts, "-file", CMD_SINGLE, CMD_OPTIONAL, "dump file");
971 cmd_AddParm(ts, "-dir", CMD_SINGLE, CMD_OPTIONAL, "restore dir");
972 cmd_AddParm(ts, "-extension", CMD_SINGLE, CMD_OPTIONAL, "name extension");
973 cmd_AddParm(ts, "-mountpoint", CMD_SINGLE, CMD_OPTIONAL,
975 cmd_AddParm(ts, "-umask", CMD_SINGLE, CMD_OPTIONAL, "mode mask");
977 return cmd_Dispatch(argc, argv);