2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)restore.c 8.3 (Berkeley) 9/13/94";
38 #include <sys/types.h>
41 #include <ufs/ufs/dinode.h>
49 static char *keyval __P((int));
52 * This implements the 't' option.
53 * List entries on the tape.
56 listfile(name, ino, type)
61 long descend = hflag ? GOOD : FAIL;
63 if (TSTINO(ino, dumpmap) == 0)
65 vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
66 fprintf(stdout, "%10d\t%s\n", ino, name);
71 * This implements the 'x' option.
72 * Request that new entries be extracted.
75 addfile(name, ino, type)
80 register struct entry *ep;
81 long descend = hflag ? GOOD : FAIL;
84 if (TSTINO(ino, dumpmap) == 0) {
85 dprintf(stdout, "%s: not on the tape\n", name);
88 if (ino == WINO && command == 'i' && !vflag)
91 (void) sprintf(buf, "./%u", ino);
94 (void) genliteraldir(name, ino);
100 if (strcmp(name, myname(ep)) == 0) {
106 ep = addentry(name, ino, type);
114 * This is used by the 'i' option to undo previous requests made by addfile.
115 * Delete entries from the request queue.
119 deletefile(name, ino, type)
124 long descend = hflag ? GOOD : FAIL;
127 if (TSTINO(ino, dumpmap) == 0)
129 ep = lookupname(name);
132 ep->e_flags |= REMOVED;
133 if (ep->e_type != NODE)
140 * The following four routines implement the incremental
141 * restore algorithm. The first removes old entries, the second
142 * does renames and calculates the extraction list, the third
143 * cleans up link names missed by the first two, and the final
144 * one deletes old directories.
146 * Directories cannot be immediately deleted, as they may have
147 * other files in them which need to be moved out first. As
148 * directories to be deleted are found, they are put on the
149 * following deletion list. After all deletions and renames
150 * are done, this list is actually deleted.
152 static struct entry *removelist;
155 * Remove invalid whiteouts from the old tree.
156 * Remove unneeded leaves from the old tree.
157 * Remove directories from the lookup chains.
162 register struct entry *ep, *nextep;
163 register ino_t i, mydirino;
165 vprintf(stdout, "Mark entries to be removed.\n");
166 if (ep = lookupino(WINO)) {
167 vprintf(stdout, "Delete whiteouts\n");
168 for ( ; ep != NULL; ep = nextep) {
169 nextep = ep->e_links;
170 mydirino = ep->e_parent->e_ino;
172 * We remove all whiteouts that are in directories
173 * that have been removed or that have been dumped.
175 if (TSTINO(mydirino, usedinomap) &&
176 !TSTINO(mydirino, dumpmap))
182 for (i = ROOTINO + 1; i < maxino; i++) {
186 if (TSTINO(i, usedinomap))
188 for ( ; ep != NULL; ep = ep->e_links) {
189 dprintf(stdout, "%s: REMOVE\n", myname(ep));
190 if (ep->e_type == LEAF) {
195 deleteino(ep->e_ino);
196 ep->e_next = removelist;
204 * For each directory entry on the incremental tape, determine which
205 * category it falls into as follows:
206 * KEEP - entries that are to be left alone.
207 * NEW - new entries to be added.
208 * EXTRACT - files that must be updated with new contents.
209 * LINK - new links to be added.
210 * Renames are done at the same time.
213 nodeupdates(name, ino, type)
218 register struct entry *ep, *np, *ip;
223 # define ONTAPE 0x1 /* inode is on the tape */
224 # define INOFND 0x2 /* inode already exists */
225 # define NAMEFND 0x4 /* name already exists */
226 # define MODECHG 0x8 /* mode of inode changed */
229 * This routine is called once for each element in the
230 * directory hierarchy, with a full path name.
231 * The "type" value is incorrectly specified as LEAF for
232 * directories that are not on the dump tape.
234 * Check to see if the file is on the tape.
236 if (TSTINO(ino, dumpmap))
239 * Check to see if the name exists, and if the name is a link.
241 np = lookupname(name);
244 ip = lookupino(np->e_ino);
246 panic("corrupted symbol table\n");
251 * Check to see if the inode exists, and if one of its links
252 * corresponds to the name (if one was found).
257 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
265 * If both a name and an inode are found, but they do not
266 * correspond to the same file, then both the inode that has
267 * been found and the inode corresponding to the name that
268 * has been found need to be renamed. The current pathname
269 * is the new name for the inode that has been found. Since
270 * all files to be deleted have already been removed, the
271 * named file is either a now unneeded link, or it must live
272 * under a new name in this dump level. If it is a link, it
273 * can be removed. If it is not a link, it is given a
274 * temporary name in anticipation that it will be renamed
275 * when it is later found by inode number.
277 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
278 if (lookuptype == LINK) {
282 dprintf(stdout, "name/inode conflict, mktempname %s\n",
289 if ((key & ONTAPE) &&
290 (((key & INOFND) && ip->e_type != type) ||
291 ((key & NAMEFND) && np->e_type != type)))
295 * Decide on the disposition of the file based on its flags.
296 * Note that we have already handled the case in which
297 * a name and inode are found that correspond to different files.
298 * Thus if both NAMEFND and INOFND are set then ip == np.
303 * A previously existing file has been found.
304 * Mark it as KEEP so that other links to the inode can be
305 * detected, and so that it will not be reclaimed by the search
306 * for unreferenced names.
310 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
315 * A file on the tape has a name which is the same as a name
316 * corresponding to a different file in the previous dump.
317 * Since all files to be deleted have already been removed,
318 * this file is either a now unneeded link, or it must live
319 * under a new name in this dump level. If it is a link, it
320 * can simply be removed. If it is not a link, it is given a
321 * temporary name in anticipation that it will be renamed
322 * when it is later found by inode number (see INOFND case
323 * below). The entry is then treated as a new file.
326 case ONTAPE|NAMEFND|MODECHG:
327 if (lookuptype == LINK) {
336 * A previously non-existent file.
337 * Add it to the file system, and request its extraction.
338 * If it is a directory, create it immediately.
339 * (Since the name is unused there can be no conflict)
342 ep = addentry(name, ino, type);
345 ep->e_flags |= NEW|KEEP;
346 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
351 * A file with the same inode number, but a different
352 * name has been found. If the other name has not already
353 * been found (indicated by the KEEP flag, see above) then
354 * this must be a new name for the file, and it is renamed.
355 * If the other name has been found then this must be a
356 * link to the file. Hard links to directories are not
357 * permitted, and are either deleted or converted to
358 * symbolic links. Finally, if the file is on the tape,
359 * a request is made to extract it.
362 if (type == LEAF && (ip->e_flags & KEEP) == 0)
363 ip->e_flags |= EXTRACT;
366 if ((ip->e_flags & KEEP) == 0) {
367 renameit(myname(ip), name);
370 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
374 if (ip->e_type == NODE) {
377 "deleted hard link %s to directory %s\n",
381 ep = addentry(name, ino, type|LINK);
383 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
388 * A previously known file which is to be updated. If it is a link,
389 * then all names referring to the previous file must be removed
390 * so that the subset of them that remain can be recreated.
392 case ONTAPE|INOFND|NAMEFND:
393 if (lookuptype == LINK) {
396 ep = addentry(name, ino, type|LINK);
399 ep->e_flags |= NEW|KEEP;
400 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
404 if (type == LEAF && lookuptype != LINK)
405 np->e_flags |= EXTRACT;
407 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
412 * An inode is being reused in a completely different way.
413 * Normally an extract can simply do an "unlink" followed
414 * by a "creat". Here we must do effectively the same
415 * thing. The complications arise because we cannot really
416 * delete a directory since it may still contain files
417 * that we need to rename, so we delete it from the symbol
418 * table, and put it on the list to be deleted eventually.
419 * Conversely if a directory is to be created, it must be
420 * done immediately, rather than waiting until the
423 case ONTAPE|INOFND|MODECHG:
424 case ONTAPE|INOFND|NAMEFND|MODECHG:
425 if (ip->e_flags & KEEP) {
426 badentry(ip, "cannot KEEP and change modes");
429 if (ip->e_type == LEAF) {
430 /* changing from leaf to node */
431 for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
432 if (ip->e_type != LEAF)
433 badentry(ip, "NODE and LEAF links to same inode");
437 ip = addentry(name, ino, type);
440 /* changing from node to leaf */
441 if ((ip->e_flags & TMPNAME) == 0)
443 deleteino(ip->e_ino);
444 ip->e_next = removelist;
446 ip = addentry(name, ino, type);
448 ip->e_flags |= NEW|KEEP;
449 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
454 * A hard link to a diirectory that has been removed.
458 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
464 * If we find a directory entry for a file that is not on
465 * the tape, then we must have found a file that was created
466 * while the dump was in progress. Since we have no contents
467 * for it, we discard the name knowing that it will be on the
468 * next incremental tape.
471 fprintf(stderr, "%s: (inode %d) not found on tape\n",
476 * If any of these arise, something is grievously wrong with
477 * the current state of the symbol table.
479 case INOFND|NAMEFND|MODECHG:
480 case NAMEFND|MODECHG:
482 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
487 * These states "cannot" arise for any state of the symbol table.
492 panic("[%s] %s: impossible state\n", keyval(key), name);
499 * Calculate the active flags in a key.
505 static char keybuf[32];
507 (void) strcpy(keybuf, "|NIL");
510 (void) strcat(keybuf, "|ONTAPE");
512 (void) strcat(keybuf, "|INOFND");
514 (void) strcat(keybuf, "|NAMEFND");
516 (void) strcat(keybuf, "|MODECHG");
521 * Find unreferenced link names.
526 register struct entry *ep, *np;
529 vprintf(stdout, "Find unreferenced names.\n");
530 for (i = ROOTINO; i < maxino; i++) {
532 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
534 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
535 if (np->e_flags == 0) {
537 "%s: remove unreferenced name\n",
545 * Any leaves remaining in removed directories is unreferenced.
547 for (ep = removelist; ep != NULL; ep = ep->e_next) {
548 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
549 if (np->e_type == LEAF) {
550 if (np->e_flags != 0)
551 badentry(np, "unreferenced with flags");
553 "%s: remove unreferenced name\n",
563 * Remove old nodes (directories).
564 * Note that this routine runs in O(N*D) where:
565 * N is the number of directory entries to be removed.
566 * D is the maximum depth of the tree.
567 * If N == D this can be quite slow. If the list were
568 * topologically sorted, the deletion could be done in
574 register struct entry *ep, **prev;
577 vprintf(stdout, "Remove old nodes (directories).\n");
581 for (ep = removelist; ep != NULL; ep = *prev) {
582 if (ep->e_entries != NULL) {
592 for (ep = removelist; ep != NULL; ep = ep->e_next)
593 badentry(ep, "cannot remove, non-empty");
597 * This is the routine used to extract files for the 'r' command.
598 * Extract new leaves.
601 createleaves(symtabfile)
604 register struct entry *ep;
608 if (command == 'R') {
609 vprintf(stdout, "Continue extraction of new leaves\n");
611 vprintf(stdout, "Extract new leaves.\n");
612 dumpsymtable(symtabfile, volno);
614 first = lowerbnd(ROOTINO);
616 while (curfile.ino < maxino) {
617 first = lowerbnd(first);
619 * If the next available file is not the one which we
620 * expect then we have missed one or more files. Since
621 * we do not request files that were not on the tape,
622 * the lost files must have been due to a tape read error,
623 * or a file that was removed while the dump was in progress.
625 while (first < curfile.ino) {
626 ep = lookupino(first);
628 panic("%d: bad first\n", first);
629 fprintf(stderr, "%s: not found on tape\n", myname(ep));
630 ep->e_flags &= ~(NEW|EXTRACT);
631 first = lowerbnd(first);
634 * If we find files on the tape that have no corresponding
635 * directory entries, then we must have found a file that
636 * was created while the dump was in progress. Since we have
637 * no name for it, we discard it knowing that it will be
638 * on the next incremental tape.
640 if (first != curfile.ino) {
641 fprintf(stderr, "expected next file %d, got %d\n",
646 ep = lookupino(curfile.ino);
648 panic("unknown file on tape\n");
649 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
650 badentry(ep, "unexpected file on tape");
652 * If the file is to be extracted, then the old file must
653 * be removed since its type may change from one leaf type
654 * to another (eg "file" to "character special").
656 if ((ep->e_flags & EXTRACT) != 0) {
658 ep->e_flags &= ~REMOVED;
660 (void) extractfile(myname(ep));
661 ep->e_flags &= ~(NEW|EXTRACT);
663 * We checkpoint the restore after every tape reel, so
664 * as to simplify the amount of work re quired by the
668 if (curvol != volno) {
669 dumpsymtable(symtabfile, volno);
677 * This is the routine used to extract files for the 'x' and 'i' commands.
678 * Efficiently extract a subset of the files on a tape.
683 register ino_t first, next, last;
684 register struct entry *ep;
687 vprintf(stdout, "Extract requested files\n");
688 curfile.action = SKIP;
692 first = lowerbnd(ROOTINO);
693 last = upperbnd(maxino - 1);
695 first = lowerbnd(first);
696 last = upperbnd(last);
698 * Check to see if any files remain to be extracted
703 * Reject any volumes with inodes greater
704 * than the last one needed
706 while (curfile.ino > last) {
707 curfile.action = SKIP;
713 * Decide on the next inode needed.
714 * Skip across the inodes until it is found
715 * or an out of order volume change is encountered
717 next = lowerbnd(curfile.ino);
720 while (next > curfile.ino && volno == curvol)
724 } while (volno == curvol + 1);
726 * If volume change out of order occurred the
727 * current state must be recalculated
732 * If the current inode is greater than the one we were
733 * looking for then we missed the one we were looking for.
734 * Since we only attempt to extract files listed in the
735 * dump map, the lost files must have been due to a tape
736 * read error, or a file that was removed while the dump
737 * was in progress. Thus we report all requested files
738 * between the one we were looking for, and the one we
739 * found as missing, and delete their request flags.
741 while (next < curfile.ino) {
742 ep = lookupino(next);
744 panic("corrupted symbol table\n");
745 fprintf(stderr, "%s: not found on tape\n", myname(ep));
747 next = lowerbnd(next);
750 * The current inode is the one that we are looking for,
751 * so extract it per its requested name.
753 if (next == curfile.ino && next <= last) {
754 ep = lookupino(next);
756 panic("corrupted symbol table\n");
757 (void) extractfile(myname(ep));
771 register struct entry *np, *ep;
775 if (ep = lookupino(WINO)) {
776 vprintf(stdout, "Add whiteouts\n");
777 for ( ; ep != NULL; ep = ep->e_links) {
778 if ((ep->e_flags & NEW) == 0)
780 (void) addwhiteout(myname(ep));
784 vprintf(stdout, "Add links\n");
785 for (i = ROOTINO; i < maxino; i++) {
789 for (np = ep->e_links; np != NULL; np = np->e_links) {
790 if ((np->e_flags & NEW) == 0)
792 (void) strcpy(name, myname(ep));
793 if (ep->e_type == NODE) {
794 (void) linkit(name, myname(np), SYMLINK);
796 (void) linkit(name, myname(np), HARDLINK);
804 * Check the symbol table.
805 * We do this to insure that all the requested work was done, and
806 * that no temporary names remain.
811 register struct entry *ep;
814 vprintf(stdout, "Check the symbol table.\n");
815 for (i = WINO; i < maxino; i++) {
816 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
817 ep->e_flags &= ~KEEP;
818 if (ep->e_type == NODE)
819 ep->e_flags &= ~(NEW|EXISTED);
820 if (ep->e_flags != 0)
821 badentry(ep, "incomplete operations");
827 * Compare with the directory structure on the tape
828 * A paranoid check that things are as they should be.
831 verifyfile(name, ino, type)
836 struct entry *np, *ep;
839 ep = lookupname(name);
841 fprintf(stderr, "Warning: missing name %s\n", name);
847 for ( ; np != NULL; np = np->e_links)
851 panic("missing inumber %d\n", ino);
852 if (ep->e_type == LEAF && type != LEAF)
853 badentry(ep, "type should be LEAF");