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>
45 #include <ufs/ufs/dinode.h>
50 static char *keyval(int);
53 * This implements the 't' option.
54 * List entries on the tape.
57 listfile(char *name, ino_t ino, int type)
59 long descend = hflag ? GOOD : FAIL;
61 if (TSTINO(ino, dumpmap) == 0)
63 vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
64 fprintf(stdout, "%10d\t%s\n", ino, name);
69 * This implements the 'x' option.
70 * Request that new entries be extracted.
73 addfile(char *name, ino_t ino, int type)
76 long descend = hflag ? GOOD : FAIL;
79 if (TSTINO(ino, dumpmap) == 0) {
80 dprintf(stdout, "%s: not on the tape\n", name);
83 if (ino == WINO && command == 'i' && !vflag)
86 (void) sprintf(buf, "./%u", ino);
89 (void) genliteraldir(name, ino);
95 if (strcmp(name, myname(ep)) == 0) {
101 ep = addentry(name, ino, type);
109 * This is used by the 'i' option to undo previous requests made by addfile.
110 * Delete entries from the request queue.
114 deletefile(char *name, ino_t ino, int type)
116 long descend = hflag ? GOOD : FAIL;
119 if (TSTINO(ino, dumpmap) == 0)
121 ep = lookupname(name);
124 ep->e_flags |= REMOVED;
125 if (ep->e_type != NODE)
132 * The following four routines implement the incremental
133 * restore algorithm. The first removes old entries, the second
134 * does renames and calculates the extraction list, the third
135 * cleans up link names missed by the first two, and the final
136 * one deletes old directories.
138 * Directories cannot be immediately deleted, as they may have
139 * other files in them which need to be moved out first. As
140 * directories to be deleted are found, they are put on the
141 * following deletion list. After all deletions and renames
142 * are done, this list is actually deleted.
144 static struct entry *removelist;
147 * Remove invalid whiteouts from the old tree.
148 * Remove unneeded leaves from the old tree.
149 * Remove directories from the lookup chains.
152 removeoldleaves(void)
154 struct entry *ep, *nextep;
157 vprintf(stdout, "Mark entries to be removed.\n");
158 if ((ep = lookupino(WINO))) {
159 vprintf(stdout, "Delete whiteouts\n");
160 for ( ; ep != NULL; ep = nextep) {
161 nextep = ep->e_links;
162 mydirino = ep->e_parent->e_ino;
164 * We remove all whiteouts that are in directories
165 * that have been removed or that have been dumped.
167 if (TSTINO(mydirino, usedinomap) &&
168 !TSTINO(mydirino, dumpmap))
174 for (i = ROOTINO + 1; i < maxino; i++) {
178 if (TSTINO(i, usedinomap))
180 for ( ; ep != NULL; ep = ep->e_links) {
181 dprintf(stdout, "%s: REMOVE\n", myname(ep));
182 if (ep->e_type == LEAF) {
187 deleteino(ep->e_ino);
188 ep->e_next = removelist;
196 * For each directory entry on the incremental tape, determine which
197 * category it falls into as follows:
198 * KEEP - entries that are to be left alone.
199 * NEW - new entries to be added.
200 * EXTRACT - files that must be updated with new contents.
201 * LINK - new links to be added.
202 * Renames are done at the same time.
205 nodeupdates(char *name, ino_t ino, int type)
207 struct entry *ep, *np, *ip;
212 # define ONTAPE 0x1 /* inode is on the tape */
213 # define INOFND 0x2 /* inode already exists */
214 # define NAMEFND 0x4 /* name already exists */
215 # define MODECHG 0x8 /* mode of inode changed */
218 * This routine is called once for each element in the
219 * directory hierarchy, with a full path name.
220 * The "type" value is incorrectly specified as LEAF for
221 * directories that are not on the dump tape.
223 * Check to see if the file is on the tape.
225 if (TSTINO(ino, dumpmap))
228 * Check to see if the name exists, and if the name is a link.
230 np = lookupname(name);
233 ip = lookupino(np->e_ino);
235 panic("corrupted symbol table\n");
240 * Check to see if the inode exists, and if one of its links
241 * corresponds to the name (if one was found).
246 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
254 * If both a name and an inode are found, but they do not
255 * correspond to the same file, then both the inode that has
256 * been found and the inode corresponding to the name that
257 * has been found need to be renamed. The current pathname
258 * is the new name for the inode that has been found. Since
259 * all files to be deleted have already been removed, the
260 * named file is either a now unneeded link, or it must live
261 * under a new name in this dump level. If it is a link, it
262 * can be removed. If it is not a link, it is given a
263 * temporary name in anticipation that it will be renamed
264 * when it is later found by inode number.
266 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
267 if (lookuptype == LINK) {
271 dprintf(stdout, "name/inode conflict, mktempname %s\n",
278 if ((key & ONTAPE) &&
279 (((key & INOFND) && ip->e_type != type) ||
280 ((key & NAMEFND) && np->e_type != type)))
284 * Decide on the disposition of the file based on its flags.
285 * Note that we have already handled the case in which
286 * a name and inode are found that correspond to different files.
287 * Thus if both NAMEFND and INOFND are set then ip == np.
292 * A previously existing file has been found.
293 * Mark it as KEEP so that other links to the inode can be
294 * detected, and so that it will not be reclaimed by the search
295 * for unreferenced names.
299 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
304 * A file on the tape has a name which is the same as a name
305 * corresponding to a different file in the previous dump.
306 * Since all files to be deleted have already been removed,
307 * this file is either a now unneeded link, or it must live
308 * under a new name in this dump level. If it is a link, it
309 * can simply be removed. If it is not a link, it is given a
310 * temporary name in anticipation that it will be renamed
311 * when it is later found by inode number (see INOFND case
312 * below). The entry is then treated as a new file.
315 case ONTAPE|NAMEFND|MODECHG:
316 if (lookuptype == LINK) {
325 * A previously non-existent file.
326 * Add it to the file system, and request its extraction.
327 * If it is a directory, create it immediately.
328 * (Since the name is unused there can be no conflict)
331 ep = addentry(name, ino, type);
334 ep->e_flags |= NEW|KEEP;
335 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
340 * A file with the same inode number, but a different
341 * name has been found. If the other name has not already
342 * been found (indicated by the KEEP flag, see above) then
343 * this must be a new name for the file, and it is renamed.
344 * If the other name has been found then this must be a
345 * link to the file. Hard links to directories are not
346 * permitted, and are either deleted or converted to
347 * symbolic links. Finally, if the file is on the tape,
348 * a request is made to extract it.
351 if (type == LEAF && (ip->e_flags & KEEP) == 0)
352 ip->e_flags |= EXTRACT;
355 if ((ip->e_flags & KEEP) == 0) {
356 renameit(myname(ip), name);
359 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
363 if (ip->e_type == NODE) {
366 "deleted hard link %s to directory %s\n",
370 ep = addentry(name, ino, type|LINK);
372 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
377 * A previously known file which is to be updated. If it is a link,
378 * then all names referring to the previous file must be removed
379 * so that the subset of them that remain can be recreated.
381 case ONTAPE|INOFND|NAMEFND:
382 if (lookuptype == LINK) {
385 ep = addentry(name, ino, type|LINK);
388 ep->e_flags |= NEW|KEEP;
389 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
393 if (type == LEAF && lookuptype != LINK)
394 np->e_flags |= EXTRACT;
396 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
401 * An inode is being reused in a completely different way.
402 * Normally an extract can simply do an "unlink" followed
403 * by a "creat". Here we must do effectively the same
404 * thing. The complications arise because we cannot really
405 * delete a directory since it may still contain files
406 * that we need to rename, so we delete it from the symbol
407 * table, and put it on the list to be deleted eventually.
408 * Conversely if a directory is to be created, it must be
409 * done immediately, rather than waiting until the
412 case ONTAPE|INOFND|MODECHG:
413 case ONTAPE|INOFND|NAMEFND|MODECHG:
414 if (ip->e_flags & KEEP) {
415 badentry(ip, "cannot KEEP and change modes");
418 if (ip->e_type == LEAF) {
419 /* changing from leaf to node */
420 for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
421 if (ip->e_type != LEAF)
422 badentry(ip, "NODE and LEAF links to same inode");
426 ip = addentry(name, ino, type);
429 /* changing from node to leaf */
430 if ((ip->e_flags & TMPNAME) == 0)
432 deleteino(ip->e_ino);
433 ip->e_next = removelist;
435 ip = addentry(name, ino, type);
437 ip->e_flags |= NEW|KEEP;
438 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
443 * A hard link to a directory that has been removed.
447 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
453 * If we find a directory entry for a file that is not on
454 * the tape, then we must have found a file that was created
455 * while the dump was in progress. Since we have no contents
456 * for it, we discard the name knowing that it will be on the
457 * next incremental tape.
460 fprintf(stderr, "%s: (inode %d) not found on tape\n",
465 * If any of these arise, something is grievously wrong with
466 * the current state of the symbol table.
468 case INOFND|NAMEFND|MODECHG:
469 case NAMEFND|MODECHG:
471 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
476 * These states "cannot" arise for any state of the symbol table.
481 panic("[%s] %s: impossible state\n", keyval(key), name);
488 * Calculate the active flags in a key.
493 static char keybuf[32];
495 (void) strcpy(keybuf, "|NIL");
498 (void) strcat(keybuf, "|ONTAPE");
500 (void) strcat(keybuf, "|INOFND");
502 (void) strcat(keybuf, "|NAMEFND");
504 (void) strcat(keybuf, "|MODECHG");
509 * Find unreferenced link names.
514 struct entry *ep, *np;
517 vprintf(stdout, "Find unreferenced names.\n");
518 for (i = ROOTINO; i < maxino; i++) {
520 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
522 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
523 if (np->e_flags == 0) {
525 "%s: remove unreferenced name\n",
533 * Any leaves remaining in removed directories is unreferenced.
535 for (ep = removelist; ep != NULL; ep = ep->e_next) {
536 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
537 if (np->e_type == LEAF) {
538 if (np->e_flags != 0)
539 badentry(np, "unreferenced with flags");
541 "%s: remove unreferenced name\n",
551 * Remove old nodes (directories).
552 * Note that this routine runs in O(N*D) where:
553 * N is the number of directory entries to be removed.
554 * D is the maximum depth of the tree.
555 * If N == D this can be quite slow. If the list were
556 * topologically sorted, the deletion could be done in
562 struct entry *ep, **prev;
565 vprintf(stdout, "Remove old nodes (directories).\n");
569 for (ep = removelist; ep != NULL; ep = *prev) {
570 if (ep->e_entries != NULL) {
580 for (ep = removelist; ep != NULL; ep = ep->e_next)
581 badentry(ep, "cannot remove, non-empty");
585 * This is the routine used to extract files for the 'r' command.
586 * Extract new leaves.
589 createleaves(char *symtabfile)
595 if (command == 'R') {
596 vprintf(stdout, "Continue extraction of new leaves\n");
598 vprintf(stdout, "Extract new leaves.\n");
599 dumpsymtable(symtabfile, volno);
601 first = lowerbnd(ROOTINO);
603 while (curfile.ino < maxino) {
604 first = lowerbnd(first);
606 * If the next available file is not the one which we
607 * expect then we have missed one or more files. Since
608 * we do not request files that were not on the tape,
609 * the lost files must have been due to a tape read error,
610 * or a file that was removed while the dump was in progress.
612 while (first < curfile.ino) {
613 ep = lookupino(first);
615 panic("%d: bad first\n", first);
616 fprintf(stderr, "%s: not found on tape\n", myname(ep));
617 ep->e_flags &= ~(NEW|EXTRACT);
618 first = lowerbnd(first);
621 * If we find files on the tape that have no corresponding
622 * directory entries, then we must have found a file that
623 * was created while the dump was in progress. Since we have
624 * no name for it, we discard it knowing that it will be
625 * on the next incremental tape.
627 if (first != curfile.ino) {
628 fprintf(stderr, "expected next file %d, got %d\n",
633 ep = lookupino(curfile.ino);
635 panic("unknown file on tape\n");
636 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
637 badentry(ep, "unexpected file on tape");
639 * If the file is to be extracted, then the old file must
640 * be removed since its type may change from one leaf type
641 * to another (e.g. "file" to "character special").
643 if ((ep->e_flags & EXTRACT) != 0) {
645 ep->e_flags &= ~REMOVED;
647 (void) extractfile(myname(ep));
648 ep->e_flags &= ~(NEW|EXTRACT);
650 * We checkpoint the restore after every tape reel, so
651 * as to simplify the amount of work required by the
655 if (curvol != volno) {
656 dumpsymtable(symtabfile, volno);
664 * This is the routine used to extract files for the 'x' and 'i' commands.
665 * Efficiently extract a subset of the files on a tape.
670 ino_t first, next, last;
674 vprintf(stdout, "Extract requested files\n");
675 curfile.action = SKIP;
679 first = lowerbnd(ROOTINO);
680 last = upperbnd(maxino - 1);
683 first = lowerbnd(first);
684 last = upperbnd(last);
686 * Check to see if any files remain to be extracted
691 if (curfile.ino == maxino)
693 if((ep = lookupino(curfile.ino)) != NULL &&
694 (ep->e_flags & (NEW|EXTRACT))) {
702 * Reject any volumes with inodes greater than the last
703 * one needed, so that we can quickly skip backwards to
704 * a volume containing useful inodes. We can't do this
705 * if there are no further volumes available (curfile.ino
706 * >= maxino) or if we are already at the first tape.
708 if (curfile.ino > last && curfile.ino < maxino && volno > 1) {
709 curfile.action = SKIP;
716 * Decide on the next inode needed.
717 * Skip across the inodes until it is found
718 * or a volume change is encountered
720 if (curfile.ino < maxino) {
721 next = lowerbnd(curfile.ino);
722 while (next > curfile.ino && volno == curvol)
724 if (volno != curvol) {
731 * No further volumes or inodes available. Set
732 * `next' to the first inode, so that a warning
733 * is emitted below for each missing file.
738 * If the current inode is greater than the one we were
739 * looking for then we missed the one we were looking for.
740 * Since we only attempt to extract files listed in the
741 * dump map, the lost files must have been due to a tape
742 * read error, or a file that was removed while the dump
743 * was in progress. Thus we report all requested files
744 * between the one we were looking for, and the one we
745 * found as missing, and delete their request flags.
747 while (next < curfile.ino) {
748 ep = lookupino(next);
750 panic("corrupted symbol table\n");
751 fprintf(stderr, "%s: not found on tape\n", myname(ep));
753 next = lowerbnd(next);
756 * The current inode is the one that we are looking for,
757 * so extract it per its requested name.
759 if (next == curfile.ino && next <= last) {
760 ep = lookupino(next);
762 panic("corrupted symbol table\n");
764 (void) extractfile(myname(ep));
778 struct entry *np, *ep;
782 if ((ep = lookupino(WINO))) {
783 vprintf(stdout, "Add whiteouts\n");
784 for ( ; ep != NULL; ep = ep->e_links) {
785 if ((ep->e_flags & NEW) == 0)
787 (void) addwhiteout(myname(ep));
791 vprintf(stdout, "Add links\n");
792 for (i = ROOTINO; i < maxino; i++) {
796 for (np = ep->e_links; np != NULL; np = np->e_links) {
797 if ((np->e_flags & NEW) == 0)
799 (void) strcpy(name, myname(ep));
800 if (ep->e_type == NODE) {
801 (void) linkit(name, myname(np), SYMLINK);
803 (void) linkit(name, myname(np), HARDLINK);
811 * Check the symbol table.
812 * We do this to insure that all the requested work was done, and
813 * that no temporary names remain.
821 vprintf(stdout, "Check the symbol table.\n");
822 for (i = WINO; i < maxino; i++) {
823 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
824 ep->e_flags &= ~KEEP;
825 if (ep->e_type == NODE)
826 ep->e_flags &= ~(NEW|EXISTED);
827 if (ep->e_flags != 0)
828 badentry(ep, "incomplete operations");
834 * Compare with the directory structure on the tape
835 * A paranoid check that things are as they should be.
838 verifyfile(char *name, ino_t ino, int type)
840 struct entry *np, *ep;
843 ep = lookupname(name);
845 fprintf(stderr, "Warning: missing name %s\n", name);
851 for ( ; np != NULL; np = np->e_links)
855 panic("missing inumber %d\n", ino);
856 if (ep->e_type == LEAF && type != LEAF)
857 badentry(ep, "type should be LEAF");