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 * 4. 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
32 static char sccsid[] = "@(#)restore.c 8.3 (Berkeley) 9/13/94";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
46 #include <ufs/ufs/dinode.h>
51 static char *keyval(int);
54 * This implements the 't' option.
55 * List entries on the tape.
58 listfile(char *name, ino_t ino, int type)
60 long descend = hflag ? GOOD : FAIL;
62 if (TSTINO(ino, dumpmap) == 0)
64 vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
65 fprintf(stdout, "%10ju\t%s\n", (uintmax_t)ino, name);
70 * This implements the 'x' option.
71 * Request that new entries be extracted.
74 addfile(char *name, ino_t ino, int type)
77 long descend = hflag ? GOOD : FAIL;
80 if (TSTINO(ino, dumpmap) == 0) {
81 dprintf(stdout, "%s: not on the tape\n", name);
84 if (ino == WINO && command == 'i' && !vflag)
87 (void) sprintf(buf, "./%ju", (uintmax_t)ino);
90 (void) genliteraldir(name, ino);
96 if (strcmp(name, myname(ep)) == 0) {
102 ep = addentry(name, ino, type);
110 * This is used by the 'i' option to undo previous requests made by addfile.
111 * Delete entries from the request queue.
115 deletefile(char *name, ino_t ino, int type)
117 long descend = hflag ? GOOD : FAIL;
120 if (TSTINO(ino, dumpmap) == 0)
122 ep = lookupname(name);
125 ep->e_flags |= REMOVED;
126 if (ep->e_type != NODE)
133 * The following four routines implement the incremental
134 * restore algorithm. The first removes old entries, the second
135 * does renames and calculates the extraction list, the third
136 * cleans up link names missed by the first two, and the final
137 * one deletes old directories.
139 * Directories cannot be immediately deleted, as they may have
140 * other files in them which need to be moved out first. As
141 * directories to be deleted are found, they are put on the
142 * following deletion list. After all deletions and renames
143 * are done, this list is actually deleted.
145 static struct entry *removelist;
148 * Remove invalid whiteouts from the old tree.
149 * Remove unneeded leaves from the old tree.
150 * Remove directories from the lookup chains.
153 removeoldleaves(void)
155 struct entry *ep, *nextep;
158 vprintf(stdout, "Mark entries to be removed.\n");
159 if ((ep = lookupino(WINO))) {
160 vprintf(stdout, "Delete whiteouts\n");
161 for ( ; ep != NULL; ep = nextep) {
162 nextep = ep->e_links;
163 mydirino = ep->e_parent->e_ino;
165 * We remove all whiteouts that are in directories
166 * that have been removed or that have been dumped.
168 if (TSTINO(mydirino, usedinomap) &&
169 !TSTINO(mydirino, dumpmap))
175 for (i = ROOTINO + 1; i < maxino; i++) {
179 if (TSTINO(i, usedinomap))
181 for ( ; ep != NULL; ep = ep->e_links) {
182 dprintf(stdout, "%s: REMOVE\n", myname(ep));
183 if (ep->e_type == LEAF) {
188 deleteino(ep->e_ino);
189 ep->e_next = removelist;
197 * For each directory entry on the incremental tape, determine which
198 * category it falls into as follows:
199 * KEEP - entries that are to be left alone.
200 * NEW - new entries to be added.
201 * EXTRACT - files that must be updated with new contents.
202 * LINK - new links to be added.
203 * Renames are done at the same time.
206 nodeupdates(char *name, ino_t ino, int type)
208 struct entry *ep, *np, *ip;
213 # define ONTAPE 0x1 /* inode is on the tape */
214 # define INOFND 0x2 /* inode already exists */
215 # define NAMEFND 0x4 /* name already exists */
216 # define MODECHG 0x8 /* mode of inode changed */
219 * This routine is called once for each element in the
220 * directory hierarchy, with a full path name.
221 * The "type" value is incorrectly specified as LEAF for
222 * directories that are not on the dump tape.
224 * Check to see if the file is on the tape.
226 if (TSTINO(ino, dumpmap))
229 * Check to see if the name exists, and if the name is a link.
231 np = lookupname(name);
234 ip = lookupino(np->e_ino);
236 panic("corrupted symbol table\n");
241 * Check to see if the inode exists, and if one of its links
242 * corresponds to the name (if one was found).
247 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
255 * If both a name and an inode are found, but they do not
256 * correspond to the same file, then both the inode that has
257 * been found and the inode corresponding to the name that
258 * has been found need to be renamed. The current pathname
259 * is the new name for the inode that has been found. Since
260 * all files to be deleted have already been removed, the
261 * named file is either a now unneeded link, or it must live
262 * under a new name in this dump level. If it is a link, it
263 * can be removed. If it is not a link, it is given a
264 * temporary name in anticipation that it will be renamed
265 * when it is later found by inode number.
267 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
268 if (lookuptype == LINK) {
272 dprintf(stdout, "name/inode conflict, mktempname %s\n",
279 if ((key & ONTAPE) &&
280 (((key & INOFND) && ip->e_type != type) ||
281 ((key & NAMEFND) && np->e_type != type)))
285 * Decide on the disposition of the file based on its flags.
286 * Note that we have already handled the case in which
287 * a name and inode are found that correspond to different files.
288 * Thus if both NAMEFND and INOFND are set then ip == np.
293 * A previously existing file has been found.
294 * Mark it as KEEP so that other links to the inode can be
295 * detected, and so that it will not be reclaimed by the search
296 * for unreferenced names.
300 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
305 * A file on the tape has a name which is the same as a name
306 * corresponding to a different file in the previous dump.
307 * Since all files to be deleted have already been removed,
308 * this file is either a now unneeded link, or it must live
309 * under a new name in this dump level. If it is a link, it
310 * can simply be removed. If it is not a link, it is given a
311 * temporary name in anticipation that it will be renamed
312 * when it is later found by inode number (see INOFND case
313 * below). The entry is then treated as a new file.
316 case ONTAPE|NAMEFND|MODECHG:
317 if (lookuptype == LINK) {
326 * A previously non-existent file.
327 * Add it to the file system, and request its extraction.
328 * If it is a directory, create it immediately.
329 * (Since the name is unused there can be no conflict)
332 ep = addentry(name, ino, type);
335 ep->e_flags |= NEW|KEEP;
336 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
341 * A file with the same inode number, but a different
342 * name has been found. If the other name has not already
343 * been found (indicated by the KEEP flag, see above) then
344 * this must be a new name for the file, and it is renamed.
345 * If the other name has been found then this must be a
346 * link to the file. Hard links to directories are not
347 * permitted, and are either deleted or converted to
348 * symbolic links. Finally, if the file is on the tape,
349 * a request is made to extract it.
352 if (type == LEAF && (ip->e_flags & KEEP) == 0)
353 ip->e_flags |= EXTRACT;
356 if ((ip->e_flags & KEEP) == 0) {
357 renameit(myname(ip), name);
360 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
364 if (ip->e_type == NODE) {
367 "deleted hard link %s to directory %s\n",
371 ep = addentry(name, ino, type|LINK);
373 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
378 * A previously known file which is to be updated. If it is a link,
379 * then all names referring to the previous file must be removed
380 * so that the subset of them that remain can be recreated.
382 case ONTAPE|INOFND|NAMEFND:
383 if (lookuptype == LINK) {
386 ep = addentry(name, ino, type|LINK);
389 ep->e_flags |= NEW|KEEP;
390 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
394 if (type == LEAF && lookuptype != LINK)
395 np->e_flags |= EXTRACT;
397 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
402 * An inode is being reused in a completely different way.
403 * Normally an extract can simply do an "unlink" followed
404 * by a "creat". Here we must do effectively the same
405 * thing. The complications arise because we cannot really
406 * delete a directory since it may still contain files
407 * that we need to rename, so we delete it from the symbol
408 * table, and put it on the list to be deleted eventually.
409 * Conversely if a directory is to be created, it must be
410 * done immediately, rather than waiting until the
413 case ONTAPE|INOFND|MODECHG:
414 case ONTAPE|INOFND|NAMEFND|MODECHG:
415 if (ip->e_flags & KEEP) {
416 badentry(ip, "cannot KEEP and change modes");
419 if (ip->e_type == LEAF) {
420 /* changing from leaf to node */
421 for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
422 if (ip->e_type != LEAF)
423 badentry(ip, "NODE and LEAF links to same inode");
427 ip = addentry(name, ino, type);
430 /* changing from node to leaf */
431 if ((ip->e_flags & TMPNAME) == 0)
433 deleteino(ip->e_ino);
434 ip->e_next = removelist;
436 ip = addentry(name, ino, type);
438 ip->e_flags |= NEW|KEEP;
439 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
444 * A hard link to a directory that has been removed.
448 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
454 * If we find a directory entry for a file that is not on
455 * the tape, then we must have found a file that was created
456 * while the dump was in progress. Since we have no contents
457 * for it, we discard the name knowing that it will be on the
458 * next incremental tape.
461 fprintf(stderr, "%s: (inode %ju) not found on tape\n",
462 name, (uintmax_t)ino);
466 * If any of these arise, something is grievously wrong with
467 * the current state of the symbol table.
469 case INOFND|NAMEFND|MODECHG:
470 case NAMEFND|MODECHG:
472 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
477 * These states "cannot" arise for any state of the symbol table.
482 panic("[%s] %s: impossible state\n", keyval(key), name);
489 * Calculate the active flags in a key.
494 static char keybuf[32];
496 (void) strcpy(keybuf, "|NIL");
499 (void) strcat(keybuf, "|ONTAPE");
501 (void) strcat(keybuf, "|INOFND");
503 (void) strcat(keybuf, "|NAMEFND");
505 (void) strcat(keybuf, "|MODECHG");
510 * Find unreferenced link names.
515 struct entry *ep, *np;
518 vprintf(stdout, "Find unreferenced names.\n");
519 for (i = ROOTINO; i < maxino; i++) {
521 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
523 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
524 if (np->e_flags == 0) {
526 "%s: remove unreferenced name\n",
534 * Any leaves remaining in removed directories is unreferenced.
536 for (ep = removelist; ep != NULL; ep = ep->e_next) {
537 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
538 if (np->e_type == LEAF) {
539 if (np->e_flags != 0)
540 badentry(np, "unreferenced with flags");
542 "%s: remove unreferenced name\n",
552 * Remove old nodes (directories).
553 * Note that this routine runs in O(N*D) where:
554 * N is the number of directory entries to be removed.
555 * D is the maximum depth of the tree.
556 * If N == D this can be quite slow. If the list were
557 * topologically sorted, the deletion could be done in
563 struct entry *ep, **prev;
566 vprintf(stdout, "Remove old nodes (directories).\n");
570 for (ep = removelist; ep != NULL; ep = *prev) {
571 if (ep->e_entries != NULL) {
581 for (ep = removelist; ep != NULL; ep = ep->e_next)
582 badentry(ep, "cannot remove, non-empty");
586 * This is the routine used to extract files for the 'r' command.
587 * Extract new leaves.
590 createleaves(char *symtabfile)
596 if (command == 'R') {
597 vprintf(stdout, "Continue extraction of new leaves\n");
599 vprintf(stdout, "Extract new leaves.\n");
600 dumpsymtable(symtabfile, volno);
602 first = lowerbnd(ROOTINO);
604 while (curfile.ino < maxino) {
605 first = lowerbnd(first);
607 * If the next available file is not the one which we
608 * expect then we have missed one or more files. Since
609 * we do not request files that were not on the tape,
610 * the lost files must have been due to a tape read error,
611 * or a file that was removed while the dump was in progress.
613 while (first < curfile.ino) {
614 ep = lookupino(first);
616 panic("%ju: bad first\n", (uintmax_t)first);
617 fprintf(stderr, "%s: not found on tape\n", myname(ep));
618 ep->e_flags &= ~(NEW|EXTRACT);
619 first = lowerbnd(first);
622 * If we find files on the tape that have no corresponding
623 * directory entries, then we must have found a file that
624 * was created while the dump was in progress. Since we have
625 * no name for it, we discard it knowing that it will be
626 * on the next incremental tape.
628 if (first != curfile.ino) {
629 fprintf(stderr, "expected next file %ju, got %ju\n",
630 (uintmax_t)first, (uintmax_t)curfile.ino);
634 ep = lookupino(curfile.ino);
636 panic("unknown file on tape\n");
637 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
638 badentry(ep, "unexpected file on tape");
640 * If the file is to be extracted, then the old file must
641 * be removed since its type may change from one leaf type
642 * to another (e.g. "file" to "character special").
644 if ((ep->e_flags & EXTRACT) != 0) {
646 ep->e_flags &= ~REMOVED;
648 (void) extractfile(myname(ep));
649 ep->e_flags &= ~(NEW|EXTRACT);
651 * We checkpoint the restore after every tape reel, so
652 * as to simplify the amount of work required by the
656 if (curvol != volno) {
657 dumpsymtable(symtabfile, volno);
665 * This is the routine used to extract files for the 'x' and 'i' commands.
666 * Efficiently extract a subset of the files on a tape.
671 ino_t first, next, last;
675 vprintf(stdout, "Extract requested files\n");
676 curfile.action = SKIP;
680 first = lowerbnd(ROOTINO);
681 last = upperbnd(maxino - 1);
684 first = lowerbnd(first);
685 last = upperbnd(last);
687 * Check to see if any files remain to be extracted
692 if (curfile.ino == maxino)
694 if((ep = lookupino(curfile.ino)) != NULL &&
695 (ep->e_flags & (NEW|EXTRACT))) {
703 * Reject any volumes with inodes greater than the last
704 * one needed, so that we can quickly skip backwards to
705 * a volume containing useful inodes. We can't do this
706 * if there are no further volumes available (curfile.ino
707 * >= maxino) or if we are already at the first tape.
709 if (curfile.ino > last && curfile.ino < maxino && volno > 1) {
710 curfile.action = SKIP;
717 * Decide on the next inode needed.
718 * Skip across the inodes until it is found
719 * or a volume change is encountered
721 if (curfile.ino < maxino) {
722 next = lowerbnd(curfile.ino);
723 while (next > curfile.ino && volno == curvol)
725 if (volno != curvol) {
732 * No further volumes or inodes available. Set
733 * `next' to the first inode, so that a warning
734 * is emitted below for each missing file.
739 * If the current inode is greater than the one we were
740 * looking for then we missed the one we were looking for.
741 * Since we only attempt to extract files listed in the
742 * dump map, the lost files must have been due to a tape
743 * read error, or a file that was removed while the dump
744 * was in progress. Thus we report all requested files
745 * between the one we were looking for, and the one we
746 * found as missing, and delete their request flags.
748 while (next < curfile.ino) {
749 ep = lookupino(next);
751 panic("corrupted symbol table\n");
752 fprintf(stderr, "%s: not found on tape\n", myname(ep));
754 next = lowerbnd(next);
757 * The current inode is the one that we are looking for,
758 * so extract it per its requested name.
760 if (next == curfile.ino && next <= last) {
761 ep = lookupino(next);
763 panic("corrupted symbol table\n");
765 (void) extractfile(myname(ep));
779 struct entry *np, *ep;
783 if ((ep = lookupino(WINO))) {
784 vprintf(stdout, "Add whiteouts\n");
785 for ( ; ep != NULL; ep = ep->e_links) {
786 if ((ep->e_flags & NEW) == 0)
788 (void) addwhiteout(myname(ep));
792 vprintf(stdout, "Add links\n");
793 for (i = ROOTINO; i < maxino; i++) {
797 for (np = ep->e_links; np != NULL; np = np->e_links) {
798 if ((np->e_flags & NEW) == 0)
800 (void) strcpy(name, myname(ep));
801 if (ep->e_type == NODE) {
802 (void) linkit(name, myname(np), SYMLINK);
804 (void) linkit(name, myname(np), HARDLINK);
812 * Check the symbol table.
813 * We do this to insure that all the requested work was done, and
814 * that no temporary names remain.
822 vprintf(stdout, "Check the symbol table.\n");
823 for (i = WINO; i < maxino; i++) {
824 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
825 ep->e_flags &= ~KEEP;
826 if (ep->e_type == NODE)
827 ep->e_flags &= ~(NEW|EXISTED);
828 if (ep->e_flags != 0)
829 badentry(ep, "incomplete operations");
835 * Compare with the directory structure on the tape
836 * A paranoid check that things are as they should be.
839 verifyfile(char *name, ino_t ino, int type)
841 struct entry *np, *ep;
844 ep = lookupname(name);
846 fprintf(stderr, "Warning: missing name %s\n", name);
852 for ( ; np != NULL; np = np->e_links)
856 panic("missing inumber %ju\n", (uintmax_t)ino);
857 if (ep->e_type == LEAF && type != LEAF)
858 badentry(ep, "type should be LEAF");