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
36 static char sccsid[] = "@(#)restore.c 8.3 (Berkeley) 9/13/94";
38 static const char rcsid[] =
39 "$FreeBSD: src/sbin/restore/restore.c,v 1.7.2.1 2002/03/01 21:32:28 iedowse Exp $";
42 #include <sys/types.h>
44 #include <ufs/ufs/dinode.h>
52 static char *keyval __P((int));
55 * This implements the 't' option.
56 * List entries on the tape.
59 listfile(name, ino, type)
64 long descend = hflag ? GOOD : FAIL;
66 if (TSTINO(ino, dumpmap) == 0)
68 vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
69 fprintf(stdout, "%10d\t%s\n", ino, name);
74 * This implements the 'x' option.
75 * Request that new entries be extracted.
78 addfile(name, ino, type)
83 register struct entry *ep;
84 long descend = hflag ? GOOD : FAIL;
87 if (TSTINO(ino, dumpmap) == 0) {
88 dprintf(stdout, "%s: not on the tape\n", name);
91 if (ino == WINO && command == 'i' && !vflag)
94 (void) sprintf(buf, "./%u", ino);
97 (void) genliteraldir(name, ino);
103 if (strcmp(name, myname(ep)) == 0) {
109 ep = addentry(name, ino, type);
117 * This is used by the 'i' option to undo previous requests made by addfile.
118 * Delete entries from the request queue.
122 deletefile(name, ino, type)
127 long descend = hflag ? GOOD : FAIL;
130 if (TSTINO(ino, dumpmap) == 0)
132 ep = lookupname(name);
135 ep->e_flags |= REMOVED;
136 if (ep->e_type != NODE)
143 * The following four routines implement the incremental
144 * restore algorithm. The first removes old entries, the second
145 * does renames and calculates the extraction list, the third
146 * cleans up link names missed by the first two, and the final
147 * one deletes old directories.
149 * Directories cannot be immediately deleted, as they may have
150 * other files in them which need to be moved out first. As
151 * directories to be deleted are found, they are put on the
152 * following deletion list. After all deletions and renames
153 * are done, this list is actually deleted.
155 static struct entry *removelist;
158 * Remove invalid whiteouts from the old tree.
159 * Remove unneeded leaves from the old tree.
160 * Remove directories from the lookup chains.
165 register struct entry *ep, *nextep;
166 register ino_t i, mydirino;
168 vprintf(stdout, "Mark entries to be removed.\n");
169 if ((ep = lookupino(WINO))) {
170 vprintf(stdout, "Delete whiteouts\n");
171 for ( ; ep != NULL; ep = nextep) {
172 nextep = ep->e_links;
173 mydirino = ep->e_parent->e_ino;
175 * We remove all whiteouts that are in directories
176 * that have been removed or that have been dumped.
178 if (TSTINO(mydirino, usedinomap) &&
179 !TSTINO(mydirino, dumpmap))
185 for (i = ROOTINO + 1; i < maxino; i++) {
189 if (TSTINO(i, usedinomap))
191 for ( ; ep != NULL; ep = ep->e_links) {
192 dprintf(stdout, "%s: REMOVE\n", myname(ep));
193 if (ep->e_type == LEAF) {
198 deleteino(ep->e_ino);
199 ep->e_next = removelist;
207 * For each directory entry on the incremental tape, determine which
208 * category it falls into as follows:
209 * KEEP - entries that are to be left alone.
210 * NEW - new entries to be added.
211 * EXTRACT - files that must be updated with new contents.
212 * LINK - new links to be added.
213 * Renames are done at the same time.
216 nodeupdates(name, ino, type)
221 register struct entry *ep, *np, *ip;
226 # define ONTAPE 0x1 /* inode is on the tape */
227 # define INOFND 0x2 /* inode already exists */
228 # define NAMEFND 0x4 /* name already exists */
229 # define MODECHG 0x8 /* mode of inode changed */
232 * This routine is called once for each element in the
233 * directory hierarchy, with a full path name.
234 * The "type" value is incorrectly specified as LEAF for
235 * directories that are not on the dump tape.
237 * Check to see if the file is on the tape.
239 if (TSTINO(ino, dumpmap))
242 * Check to see if the name exists, and if the name is a link.
244 np = lookupname(name);
247 ip = lookupino(np->e_ino);
249 panic("corrupted symbol table\n");
254 * Check to see if the inode exists, and if one of its links
255 * corresponds to the name (if one was found).
260 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
268 * If both a name and an inode are found, but they do not
269 * correspond to the same file, then both the inode that has
270 * been found and the inode corresponding to the name that
271 * has been found need to be renamed. The current pathname
272 * is the new name for the inode that has been found. Since
273 * all files to be deleted have already been removed, the
274 * named file is either a now unneeded link, or it must live
275 * under a new name in this dump level. If it is a link, it
276 * can be removed. If it is not a link, it is given a
277 * temporary name in anticipation that it will be renamed
278 * when it is later found by inode number.
280 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
281 if (lookuptype == LINK) {
285 dprintf(stdout, "name/inode conflict, mktempname %s\n",
292 if ((key & ONTAPE) &&
293 (((key & INOFND) && ip->e_type != type) ||
294 ((key & NAMEFND) && np->e_type != type)))
298 * Decide on the disposition of the file based on its flags.
299 * Note that we have already handled the case in which
300 * a name and inode are found that correspond to different files.
301 * Thus if both NAMEFND and INOFND are set then ip == np.
306 * A previously existing file has been found.
307 * Mark it as KEEP so that other links to the inode can be
308 * detected, and so that it will not be reclaimed by the search
309 * for unreferenced names.
313 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
318 * A file on the tape has a name which is the same as a name
319 * corresponding to a different file in the previous dump.
320 * Since all files to be deleted have already been removed,
321 * this file is either a now unneeded link, or it must live
322 * under a new name in this dump level. If it is a link, it
323 * can simply be removed. If it is not a link, it is given a
324 * temporary name in anticipation that it will be renamed
325 * when it is later found by inode number (see INOFND case
326 * below). The entry is then treated as a new file.
329 case ONTAPE|NAMEFND|MODECHG:
330 if (lookuptype == LINK) {
339 * A previously non-existent file.
340 * Add it to the file system, and request its extraction.
341 * If it is a directory, create it immediately.
342 * (Since the name is unused there can be no conflict)
345 ep = addentry(name, ino, type);
348 ep->e_flags |= NEW|KEEP;
349 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
354 * A file with the same inode number, but a different
355 * name has been found. If the other name has not already
356 * been found (indicated by the KEEP flag, see above) then
357 * this must be a new name for the file, and it is renamed.
358 * If the other name has been found then this must be a
359 * link to the file. Hard links to directories are not
360 * permitted, and are either deleted or converted to
361 * symbolic links. Finally, if the file is on the tape,
362 * a request is made to extract it.
365 if (type == LEAF && (ip->e_flags & KEEP) == 0)
366 ip->e_flags |= EXTRACT;
369 if ((ip->e_flags & KEEP) == 0) {
370 renameit(myname(ip), name);
373 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
377 if (ip->e_type == NODE) {
380 "deleted hard link %s to directory %s\n",
384 ep = addentry(name, ino, type|LINK);
386 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
391 * A previously known file which is to be updated. If it is a link,
392 * then all names referring to the previous file must be removed
393 * so that the subset of them that remain can be recreated.
395 case ONTAPE|INOFND|NAMEFND:
396 if (lookuptype == LINK) {
399 ep = addentry(name, ino, type|LINK);
402 ep->e_flags |= NEW|KEEP;
403 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
407 if (type == LEAF && lookuptype != LINK)
408 np->e_flags |= EXTRACT;
410 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
415 * An inode is being reused in a completely different way.
416 * Normally an extract can simply do an "unlink" followed
417 * by a "creat". Here we must do effectively the same
418 * thing. The complications arise because we cannot really
419 * delete a directory since it may still contain files
420 * that we need to rename, so we delete it from the symbol
421 * table, and put it on the list to be deleted eventually.
422 * Conversely if a directory is to be created, it must be
423 * done immediately, rather than waiting until the
426 case ONTAPE|INOFND|MODECHG:
427 case ONTAPE|INOFND|NAMEFND|MODECHG:
428 if (ip->e_flags & KEEP) {
429 badentry(ip, "cannot KEEP and change modes");
432 if (ip->e_type == LEAF) {
433 /* changing from leaf to node */
434 for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
435 if (ip->e_type != LEAF)
436 badentry(ip, "NODE and LEAF links to same inode");
440 ip = addentry(name, ino, type);
443 /* changing from node to leaf */
444 if ((ip->e_flags & TMPNAME) == 0)
446 deleteino(ip->e_ino);
447 ip->e_next = removelist;
449 ip = addentry(name, ino, type);
451 ip->e_flags |= NEW|KEEP;
452 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
457 * A hard link to a directory that has been removed.
461 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
467 * If we find a directory entry for a file that is not on
468 * the tape, then we must have found a file that was created
469 * while the dump was in progress. Since we have no contents
470 * for it, we discard the name knowing that it will be on the
471 * next incremental tape.
474 fprintf(stderr, "%s: (inode %d) not found on tape\n",
479 * If any of these arise, something is grievously wrong with
480 * the current state of the symbol table.
482 case INOFND|NAMEFND|MODECHG:
483 case NAMEFND|MODECHG:
485 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
490 * These states "cannot" arise for any state of the symbol table.
495 panic("[%s] %s: impossible state\n", keyval(key), name);
502 * Calculate the active flags in a key.
508 static char keybuf[32];
510 (void) strcpy(keybuf, "|NIL");
513 (void) strcat(keybuf, "|ONTAPE");
515 (void) strcat(keybuf, "|INOFND");
517 (void) strcat(keybuf, "|NAMEFND");
519 (void) strcat(keybuf, "|MODECHG");
524 * Find unreferenced link names.
529 register struct entry *ep, *np;
532 vprintf(stdout, "Find unreferenced names.\n");
533 for (i = ROOTINO; i < maxino; i++) {
535 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
537 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
538 if (np->e_flags == 0) {
540 "%s: remove unreferenced name\n",
548 * Any leaves remaining in removed directories is unreferenced.
550 for (ep = removelist; ep != NULL; ep = ep->e_next) {
551 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
552 if (np->e_type == LEAF) {
553 if (np->e_flags != 0)
554 badentry(np, "unreferenced with flags");
556 "%s: remove unreferenced name\n",
566 * Remove old nodes (directories).
567 * Note that this routine runs in O(N*D) where:
568 * N is the number of directory entries to be removed.
569 * D is the maximum depth of the tree.
570 * If N == D this can be quite slow. If the list were
571 * topologically sorted, the deletion could be done in
577 register struct entry *ep, **prev;
580 vprintf(stdout, "Remove old nodes (directories).\n");
584 for (ep = removelist; ep != NULL; ep = *prev) {
585 if (ep->e_entries != NULL) {
595 for (ep = removelist; ep != NULL; ep = ep->e_next)
596 badentry(ep, "cannot remove, non-empty");
600 * This is the routine used to extract files for the 'r' command.
601 * Extract new leaves.
604 createleaves(symtabfile)
607 register struct entry *ep;
611 if (command == 'R') {
612 vprintf(stdout, "Continue extraction of new leaves\n");
614 vprintf(stdout, "Extract new leaves.\n");
615 dumpsymtable(symtabfile, volno);
617 first = lowerbnd(ROOTINO);
619 while (curfile.ino < maxino) {
620 first = lowerbnd(first);
622 * If the next available file is not the one which we
623 * expect then we have missed one or more files. Since
624 * we do not request files that were not on the tape,
625 * the lost files must have been due to a tape read error,
626 * or a file that was removed while the dump was in progress.
628 while (first < curfile.ino) {
629 ep = lookupino(first);
631 panic("%d: bad first\n", first);
632 fprintf(stderr, "%s: not found on tape\n", myname(ep));
633 ep->e_flags &= ~(NEW|EXTRACT);
634 first = lowerbnd(first);
637 * If we find files on the tape that have no corresponding
638 * directory entries, then we must have found a file that
639 * was created while the dump was in progress. Since we have
640 * no name for it, we discard it knowing that it will be
641 * on the next incremental tape.
643 if (first != curfile.ino) {
644 fprintf(stderr, "expected next file %d, got %d\n",
649 ep = lookupino(curfile.ino);
651 panic("unknown file on tape\n");
652 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
653 badentry(ep, "unexpected file on tape");
655 * If the file is to be extracted, then the old file must
656 * be removed since its type may change from one leaf type
657 * to another (e.g. "file" to "character special").
659 if ((ep->e_flags & EXTRACT) != 0) {
661 ep->e_flags &= ~REMOVED;
663 (void) extractfile(myname(ep));
664 ep->e_flags &= ~(NEW|EXTRACT);
666 * We checkpoint the restore after every tape reel, so
667 * as to simplify the amount of work required by the
671 if (curvol != volno) {
672 dumpsymtable(symtabfile, volno);
680 * This is the routine used to extract files for the 'x' and 'i' commands.
681 * Efficiently extract a subset of the files on a tape.
686 register ino_t first, next, last;
687 register struct entry *ep;
690 vprintf(stdout, "Extract requested files\n");
691 curfile.action = SKIP;
695 first = lowerbnd(ROOTINO);
696 last = upperbnd(maxino - 1);
699 first = lowerbnd(first);
700 last = upperbnd(last);
702 * Check to see if any files remain to be extracted
707 * Reject any volumes with inodes greater than the last
708 * one needed, so that we can quickly skip backwards to
709 * a volume containing useful inodes. We can't do this
710 * if there are no further volumes available (curfile.ino
711 * >= maxino) or if we are already at the first tape.
713 if (curfile.ino > last && curfile.ino < maxino && volno > 1) {
714 curfile.action = SKIP;
721 * Decide on the next inode needed.
722 * Skip across the inodes until it is found
723 * or a volume change is encountered
725 if (curfile.ino < maxino) {
726 next = lowerbnd(curfile.ino);
727 while (next > curfile.ino && volno == curvol)
729 if (volno != curvol) {
736 * No further volumes or inodes available. Set
737 * `next' to the first inode, so that a warning
738 * is emitted below for each missing file.
743 * If the current inode is greater than the one we were
744 * looking for then we missed the one we were looking for.
745 * Since we only attempt to extract files listed in the
746 * dump map, the lost files must have been due to a tape
747 * read error, or a file that was removed while the dump
748 * was in progress. Thus we report all requested files
749 * between the one we were looking for, and the one we
750 * found as missing, and delete their request flags.
752 while (next < curfile.ino) {
753 ep = lookupino(next);
755 panic("corrupted symbol table\n");
756 fprintf(stderr, "%s: not found on tape\n", myname(ep));
758 next = lowerbnd(next);
761 * The current inode is the one that we are looking for,
762 * so extract it per its requested name.
764 if (next == curfile.ino && next <= last) {
765 ep = lookupino(next);
767 panic("corrupted symbol table\n");
768 (void) extractfile(myname(ep));
782 register struct entry *np, *ep;
786 if ((ep = lookupino(WINO))) {
787 vprintf(stdout, "Add whiteouts\n");
788 for ( ; ep != NULL; ep = ep->e_links) {
789 if ((ep->e_flags & NEW) == 0)
791 (void) addwhiteout(myname(ep));
795 vprintf(stdout, "Add links\n");
796 for (i = ROOTINO; i < maxino; i++) {
800 for (np = ep->e_links; np != NULL; np = np->e_links) {
801 if ((np->e_flags & NEW) == 0)
803 (void) strcpy(name, myname(ep));
804 if (ep->e_type == NODE) {
805 (void) linkit(name, myname(np), SYMLINK);
807 (void) linkit(name, myname(np), HARDLINK);
815 * Check the symbol table.
816 * We do this to insure that all the requested work was done, and
817 * that no temporary names remain.
822 register struct entry *ep;
825 vprintf(stdout, "Check the symbol table.\n");
826 for (i = WINO; i < maxino; i++) {
827 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
828 ep->e_flags &= ~KEEP;
829 if (ep->e_type == NODE)
830 ep->e_flags &= ~(NEW|EXISTED);
831 if (ep->e_flags != 0)
832 badentry(ep, "incomplete operations");
838 * Compare with the directory structure on the tape
839 * A paranoid check that things are as they should be.
842 verifyfile(name, ino, type)
847 struct entry *np, *ep;
850 ep = lookupname(name);
852 fprintf(stderr, "Warning: missing name %s\n", name);
858 for ( ; np != NULL; np = np->e_links)
862 panic("missing inumber %d\n", ino);
863 if (ep->e_type == LEAF && type != LEAF)
864 badentry(ep, "type should be LEAF");