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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)restore.c 8.3 (Berkeley) 9/13/94
30 * $FreeBSD: src/sbin/restore/restore.c,v 1.7.2.1 2002/03/01 21:32:28 iedowse Exp $
31 * $DragonFly: src/sbin/restore/restore.c,v 1.9 2008/06/05 18:06:31 swildner Exp $
34 #include <sys/types.h>
36 #include <vfs/ufs/dinode.h>
44 static char *keyval(int);
47 * This implements the 't' option.
48 * List entries on the tape.
51 listfile(char *name, ufs1_ino_t ino, int type)
53 long descend = hflag ? GOOD : FAIL;
55 if (TSTINO(ino, dumpmap) == 0)
57 vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
58 fprintf(stdout, "%10d\t%s\n", ino, name);
63 * This implements the 'x' option.
64 * Request that new entries be extracted.
67 addfile(char *name, ufs1_ino_t ino, int type)
70 long descend = hflag ? GOOD : FAIL;
73 if (TSTINO(ino, dumpmap) == 0) {
74 dprintf(stdout, "%s: not on the tape\n", name);
77 if (ino == WINO && command == 'i' && !vflag)
80 sprintf(buf, "./%u", ino);
83 genliteraldir(name, ino);
89 if (strcmp(name, myname(ep)) == 0) {
95 ep = addentry(name, ino, type);
103 * This is used by the 'i' option to undo previous requests made by addfile.
104 * Delete entries from the request queue.
108 deletefile(char *name, ufs1_ino_t ino, int type)
110 long descend = hflag ? GOOD : FAIL;
113 if (TSTINO(ino, dumpmap) == 0)
115 ep = lookupname(name);
118 ep->e_flags |= REMOVED;
119 if (ep->e_type != NODE)
126 * The following four routines implement the incremental
127 * restore algorithm. The first removes old entries, the second
128 * does renames and calculates the extraction list, the third
129 * cleans up link names missed by the first two, and the final
130 * one deletes old directories.
132 * Directories cannot be immediately deleted, as they may have
133 * other files in them which need to be moved out first. As
134 * directories to be deleted are found, they are put on the
135 * following deletion list. After all deletions and renames
136 * are done, this list is actually deleted.
138 static struct entry *removelist;
141 * Remove invalid whiteouts from the old tree.
142 * Remove unneeded leaves from the old tree.
143 * Remove directories from the lookup chains.
146 removeoldleaves(void)
148 struct entry *ep, *nextep;
149 ufs1_ino_t i, mydirino;
151 vprintf(stdout, "Mark entries to be removed.\n");
152 if ((ep = lookupino(WINO))) {
153 vprintf(stdout, "Delete whiteouts\n");
154 for ( ; ep != NULL; ep = nextep) {
155 nextep = ep->e_links;
156 mydirino = ep->e_parent->e_ino;
158 * We remove all whiteouts that are in directories
159 * that have been removed or that have been dumped.
161 if (TSTINO(mydirino, usedinomap) &&
162 !TSTINO(mydirino, dumpmap))
168 for (i = ROOTINO + 1; i < maxino; i++) {
172 if (TSTINO(i, usedinomap))
174 for ( ; ep != NULL; ep = ep->e_links) {
175 dprintf(stdout, "%s: REMOVE\n", myname(ep));
176 if (ep->e_type == LEAF) {
181 deleteino(ep->e_ino);
182 ep->e_next = removelist;
190 * For each directory entry on the incremental tape, determine which
191 * category it falls into as follows:
192 * KEEP - entries that are to be left alone.
193 * NEW - new entries to be added.
194 * EXTRACT - files that must be updated with new contents.
195 * LINK - new links to be added.
196 * Renames are done at the same time.
199 nodeupdates(char *name, ufs1_ino_t ino, int type)
201 struct entry *ep, *np, *ip;
206 # define ONTAPE 0x1 /* inode is on the tape */
207 # define INOFND 0x2 /* inode already exists */
208 # define NAMEFND 0x4 /* name already exists */
209 # define MODECHG 0x8 /* mode of inode changed */
212 * This routine is called once for each element in the
213 * directory hierarchy, with a full path name.
214 * The "type" value is incorrectly specified as LEAF for
215 * directories that are not on the dump tape.
217 * Check to see if the file is on the tape.
219 if (TSTINO(ino, dumpmap))
222 * Check to see if the name exists, and if the name is a link.
224 np = lookupname(name);
227 ip = lookupino(np->e_ino);
229 panic("corrupted symbol table\n");
234 * Check to see if the inode exists, and if one of its links
235 * corresponds to the name (if one was found).
240 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
248 * If both a name and an inode are found, but they do not
249 * correspond to the same file, then both the inode that has
250 * been found and the inode corresponding to the name that
251 * has been found need to be renamed. The current pathname
252 * is the new name for the inode that has been found. Since
253 * all files to be deleted have already been removed, the
254 * named file is either a now unneeded link, or it must live
255 * under a new name in this dump level. If it is a link, it
256 * can be removed. If it is not a link, it is given a
257 * temporary name in anticipation that it will be renamed
258 * when it is later found by inode number.
260 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
261 if (lookuptype == LINK) {
265 dprintf(stdout, "name/inode conflict, mktempname %s\n",
272 if ((key & ONTAPE) &&
273 (((key & INOFND) && ip->e_type != type) ||
274 ((key & NAMEFND) && np->e_type != type)))
278 * Decide on the disposition of the file based on its flags.
279 * Note that we have already handled the case in which
280 * a name and inode are found that correspond to different files.
281 * Thus if both NAMEFND and INOFND are set then ip == np.
286 * A previously existing file has been found.
287 * Mark it as KEEP so that other links to the inode can be
288 * detected, and so that it will not be reclaimed by the search
289 * for unreferenced names.
293 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
298 * A file on the tape has a name which is the same as a name
299 * corresponding to a different file in the previous dump.
300 * Since all files to be deleted have already been removed,
301 * this file is either a now unneeded link, or it must live
302 * under a new name in this dump level. If it is a link, it
303 * can simply be removed. If it is not a link, it is given a
304 * temporary name in anticipation that it will be renamed
305 * when it is later found by inode number (see INOFND case
306 * below). The entry is then treated as a new file.
309 case ONTAPE|NAMEFND|MODECHG:
310 if (lookuptype == LINK) {
319 * A previously non-existent file.
320 * Add it to the file system, and request its extraction.
321 * If it is a directory, create it immediately.
322 * (Since the name is unused there can be no conflict)
325 ep = addentry(name, ino, type);
328 ep->e_flags |= NEW|KEEP;
329 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
334 * A file with the same inode number, but a different
335 * name has been found. If the other name has not already
336 * been found (indicated by the KEEP flag, see above) then
337 * this must be a new name for the file, and it is renamed.
338 * If the other name has been found then this must be a
339 * link to the file. Hard links to directories are not
340 * permitted, and are either deleted or converted to
341 * symbolic links. Finally, if the file is on the tape,
342 * a request is made to extract it.
345 if (type == LEAF && (ip->e_flags & KEEP) == 0)
346 ip->e_flags |= EXTRACT;
349 if ((ip->e_flags & KEEP) == 0) {
350 renameit(myname(ip), name);
353 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
357 if (ip->e_type == NODE) {
360 "deleted hard link %s to directory %s\n",
364 ep = addentry(name, ino, type|LINK);
366 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
371 * A previously known file which is to be updated. If it is a link,
372 * then all names referring to the previous file must be removed
373 * so that the subset of them that remain can be recreated.
375 case ONTAPE|INOFND|NAMEFND:
376 if (lookuptype == LINK) {
379 ep = addentry(name, ino, type|LINK);
382 ep->e_flags |= NEW|KEEP;
383 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
387 if (type == LEAF && lookuptype != LINK)
388 np->e_flags |= EXTRACT;
390 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
395 * An inode is being reused in a completely different way.
396 * Normally an extract can simply do an "unlink" followed
397 * by a "creat". Here we must do effectively the same
398 * thing. The complications arise because we cannot really
399 * delete a directory since it may still contain files
400 * that we need to rename, so we delete it from the symbol
401 * table, and put it on the list to be deleted eventually.
402 * Conversely if a directory is to be created, it must be
403 * done immediately, rather than waiting until the
406 case ONTAPE|INOFND|MODECHG:
407 case ONTAPE|INOFND|NAMEFND|MODECHG:
408 if (ip->e_flags & KEEP) {
409 badentry(ip, "cannot KEEP and change modes");
412 if (ip->e_type == LEAF) {
413 /* changing from leaf to node */
414 for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
415 if (ip->e_type != LEAF)
416 badentry(ip, "NODE and LEAF links to same inode");
420 ip = addentry(name, ino, type);
423 /* changing from node to leaf */
424 if ((ip->e_flags & TMPNAME) == 0)
426 deleteino(ip->e_ino);
427 ip->e_next = removelist;
429 ip = addentry(name, ino, type);
431 ip->e_flags |= NEW|KEEP;
432 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
437 * A hard link to a directory that has been removed.
441 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
447 * If we find a directory entry for a file that is not on
448 * the tape, then we must have found a file that was created
449 * while the dump was in progress. Since we have no contents
450 * for it, we discard the name knowing that it will be on the
451 * next incremental tape.
454 fprintf(stderr, "%s: (inode %d) not found on tape\n",
459 * If any of these arise, something is grievously wrong with
460 * the current state of the symbol table.
462 case INOFND|NAMEFND|MODECHG:
463 case NAMEFND|MODECHG:
465 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
470 * These states "cannot" arise for any state of the symbol table.
475 panic("[%s] %s: impossible state\n", keyval(key), name);
482 * Calculate the active flags in a key.
487 static char keybuf[32];
489 strcpy(keybuf, "|NIL");
492 strcat(keybuf, "|ONTAPE");
494 strcat(keybuf, "|INOFND");
496 strcat(keybuf, "|NAMEFND");
498 strcat(keybuf, "|MODECHG");
503 * Find unreferenced link names.
508 struct entry *ep, *np;
511 vprintf(stdout, "Find unreferenced names.\n");
512 for (i = ROOTINO; i < maxino; i++) {
514 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
516 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
517 if (np->e_flags == 0) {
519 "%s: remove unreferenced name\n",
527 * Any leaves remaining in removed directories is unreferenced.
529 for (ep = removelist; ep != NULL; ep = ep->e_next) {
530 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
531 if (np->e_type == LEAF) {
532 if (np->e_flags != 0)
533 badentry(np, "unreferenced with flags");
535 "%s: remove unreferenced name\n",
545 * Remove old nodes (directories).
546 * Note that this routine runs in O(N*D) where:
547 * N is the number of directory entries to be removed.
548 * D is the maximum depth of the tree.
549 * If N == D this can be quite slow. If the list were
550 * topologically sorted, the deletion could be done in
556 struct entry *ep, **prev;
559 vprintf(stdout, "Remove old nodes (directories).\n");
563 for (ep = removelist; ep != NULL; ep = *prev) {
564 if (ep->e_entries != NULL) {
574 for (ep = removelist; ep != NULL; ep = ep->e_next)
575 badentry(ep, "cannot remove, non-empty");
579 * This is the routine used to extract files for the 'r' command.
580 * Extract new leaves.
583 createleaves(char *symtabfile)
589 if (command == 'R') {
590 vprintf(stdout, "Continue extraction of new leaves\n");
592 vprintf(stdout, "Extract new leaves.\n");
593 dumpsymtable(symtabfile, volno);
595 first = lowerbnd(ROOTINO);
597 while (curfile.ino < maxino) {
598 first = lowerbnd(first);
600 * If the next available file is not the one which we
601 * expect then we have missed one or more files. Since
602 * we do not request files that were not on the tape,
603 * the lost files must have been due to a tape read error,
604 * or a file that was removed while the dump was in progress.
606 while (first < curfile.ino) {
607 ep = lookupino(first);
609 panic("%d: bad first\n", first);
610 fprintf(stderr, "%s: not found on tape\n", myname(ep));
611 ep->e_flags &= ~(NEW|EXTRACT);
612 first = lowerbnd(first);
615 * If we find files on the tape that have no corresponding
616 * directory entries, then we must have found a file that
617 * was created while the dump was in progress. Since we have
618 * no name for it, we discard it knowing that it will be
619 * on the next incremental tape.
621 if (first != curfile.ino) {
622 fprintf(stderr, "expected next file %d, got %d\n",
627 ep = lookupino(curfile.ino);
629 panic("unknown file on tape\n");
630 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
631 badentry(ep, "unexpected file on tape");
633 * If the file is to be extracted, then the old file must
634 * be removed since its type may change from one leaf type
635 * to another (e.g. "file" to "character special").
637 if ((ep->e_flags & EXTRACT) != 0) {
639 ep->e_flags &= ~REMOVED;
641 extractfile(myname(ep));
642 ep->e_flags &= ~(NEW|EXTRACT);
644 * We checkpoint the restore after every tape reel, so
645 * as to simplify the amount of work required by the
649 if (curvol != volno) {
650 dumpsymtable(symtabfile, volno);
658 * This is the routine used to extract files for the 'x' and 'i' commands.
659 * Efficiently extract a subset of the files on a tape.
664 ufs1_ino_t first, next, last;
668 vprintf(stdout, "Extract requested files\n");
669 curfile.action = SKIP;
673 first = lowerbnd(ROOTINO);
674 last = upperbnd(maxino - 1);
677 first = lowerbnd(first);
678 last = upperbnd(last);
680 * Check to see if any files remain to be extracted
685 * Reject any volumes with inodes greater than the last
686 * one needed, so that we can quickly skip backwards to
687 * a volume containing useful inodes. We can't do this
688 * if there are no further volumes available (curfile.ino
689 * >= maxino) or if we are already at the first tape.
691 if (curfile.ino > last && curfile.ino < maxino && volno > 1) {
692 curfile.action = SKIP;
699 * Decide on the next inode needed.
700 * Skip across the inodes until it is found
701 * or a volume change is encountered
703 if (curfile.ino < maxino) {
704 next = lowerbnd(curfile.ino);
705 while (next > curfile.ino && volno == curvol)
707 if (volno != curvol) {
714 * No further volumes or inodes available. Set
715 * `next' to the first inode, so that a warning
716 * is emitted below for each missing file.
721 * If the current inode is greater than the one we were
722 * looking for then we missed the one we were looking for.
723 * Since we only attempt to extract files listed in the
724 * dump map, the lost files must have been due to a tape
725 * read error, or a file that was removed while the dump
726 * was in progress. Thus we report all requested files
727 * between the one we were looking for, and the one we
728 * found as missing, and delete their request flags.
730 while (next < curfile.ino) {
731 ep = lookupino(next);
733 panic("corrupted symbol table\n");
734 fprintf(stderr, "%s: not found on tape\n", myname(ep));
736 next = lowerbnd(next);
739 * The current inode is the one that we are looking for,
740 * so extract it per its requested name.
742 if (next == curfile.ino && next <= last) {
743 ep = lookupino(next);
745 panic("corrupted symbol table\n");
746 extractfile(myname(ep));
760 struct entry *np, *ep;
764 if ((ep = lookupino(WINO))) {
765 vprintf(stdout, "Add whiteouts\n");
766 for ( ; ep != NULL; ep = ep->e_links) {
767 if ((ep->e_flags & NEW) == 0)
769 addwhiteout(myname(ep));
773 vprintf(stdout, "Add links\n");
774 for (i = ROOTINO; i < maxino; i++) {
778 for (np = ep->e_links; np != NULL; np = np->e_links) {
779 if ((np->e_flags & NEW) == 0)
781 strcpy(name, myname(ep));
782 if (ep->e_type == NODE) {
783 linkit(name, myname(np), SYMLINK);
785 linkit(name, myname(np), HARDLINK);
793 * Check the symbol table.
794 * We do this to insure that all the requested work was done, and
795 * that no temporary names remain.
803 vprintf(stdout, "Check the symbol table.\n");
804 for (i = WINO; i < maxino; i++) {
805 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
806 ep->e_flags &= ~KEEP;
807 if (ep->e_type == NODE)
808 ep->e_flags &= ~(NEW|EXISTED);
809 if (ep->e_flags != 0)
810 badentry(ep, "incomplete operations");
816 * Compare with the directory structure on the tape
817 * A paranoid check that things are as they should be.
820 verifyfile(char *name, ufs1_ino_t ino, int type)
822 struct entry *np, *ep;
825 ep = lookupname(name);
827 fprintf(stderr, "Warning: missing name %s\n", name);
833 for ( ; np != NULL; np = np->e_links)
837 panic("missing inumber %d\n", ino);
838 if (ep->e_type == LEAF && type != LEAF)
839 badentry(ep, "type should be LEAF");