2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static char sccsid[] = "@(#)restore.c 8.3 (Berkeley) 9/13/94";
46 static const char rcsid[] =
47 "$Id: restore.c,v 1.2 1999/10/11 12:53:24 stelian Exp $";
50 #include <sys/types.h>
54 #include <linux/ext2_fs.h>
55 #include <bsdcompat.h>
57 #include <ufs/ufs/dinode.h>
58 #endif /* __linux__ */
64 #include <ext2fs/ext2fs.h>
70 static char *keyval __P((int));
73 * This implements the 't' option.
74 * List entries on the tape.
77 listfile(name, ino, type)
82 long descend = hflag ? GOOD : FAIL;
84 if (TSTINO(ino, dumpmap) == 0)
86 vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
87 fprintf(stdout, "%10ld\t%s\n", ino, name);
92 * This implements the 'x' option.
93 * Request that new entries be extracted.
96 addfile(name, ino, type)
101 register struct entry *ep;
102 long descend = hflag ? GOOD : FAIL;
105 if (TSTINO(ino, dumpmap) == 0) {
106 dprintf(stdout, "%s: not on the tape\n", name);
109 if (ino == WINO && command == 'i' && !vflag)
112 (void) sprintf(buf, "./%lu", ino);
115 (void) genliteraldir(name, ino);
121 if (strcmp(name, myname(ep)) == 0) {
127 ep = addentry(name, ino, type);
135 * This is used by the 'i' option to undo previous requests made by addfile.
136 * Delete entries from the request queue.
140 deletefile(name, ino, type)
145 long descend = hflag ? GOOD : FAIL;
148 if (TSTINO(ino, dumpmap) == 0)
150 ep = lookupname(name);
153 ep->e_flags |= REMOVED;
154 if (ep->e_type != NODE)
161 * The following four routines implement the incremental
162 * restore algorithm. The first removes old entries, the second
163 * does renames and calculates the extraction list, the third
164 * cleans up link names missed by the first two, and the final
165 * one deletes old directories.
167 * Directories cannot be immediately deleted, as they may have
168 * other files in them which need to be moved out first. As
169 * directories to be deleted are found, they are put on the
170 * following deletion list. After all deletions and renames
171 * are done, this list is actually deleted.
173 static struct entry *removelist;
176 * Remove invalid whiteouts from the old tree.
177 * Remove unneeded leaves from the old tree.
178 * Remove directories from the lookup chains.
183 register struct entry *ep, *nextep;
184 register ino_t i, mydirino;
186 vprintf(stdout, "Mark entries to be removed.\n");
187 if ((ep = lookupino(WINO))) {
188 vprintf(stdout, "Delete whiteouts\n");
189 for ( ; ep != NULL; ep = nextep) {
190 nextep = ep->e_links;
191 mydirino = ep->e_parent->e_ino;
193 * We remove all whiteouts that are in directories
194 * that have been removed or that have been dumped.
196 if (TSTINO(mydirino, usedinomap) &&
197 !TSTINO(mydirino, dumpmap))
200 (void)fprintf(stderr, "BUG! Should call delwhiteout\n");
207 for (i = ROOTINO + 1; i < maxino; i++) {
211 if (TSTINO(i, usedinomap))
213 for ( ; ep != NULL; ep = ep->e_links) {
214 dprintf(stdout, "%s: REMOVE\n", myname(ep));
215 if (ep->e_type == LEAF) {
220 deleteino(ep->e_ino);
221 ep->e_next = removelist;
229 * For each directory entry on the incremental tape, determine which
230 * category it falls into as follows:
231 * KEEP - entries that are to be left alone.
232 * NEW - new entries to be added.
233 * EXTRACT - files that must be updated with new contents.
234 * LINK - new links to be added.
235 * Renames are done at the same time.
238 nodeupdates(name, ino, type)
243 register struct entry *ep, *np, *ip;
248 # define ONTAPE 0x1 /* inode is on the tape */
249 # define INOFND 0x2 /* inode already exists */
250 # define NAMEFND 0x4 /* name already exists */
251 # define MODECHG 0x8 /* mode of inode changed */
254 * This routine is called once for each element in the
255 * directory hierarchy, with a full path name.
256 * The "type" value is incorrectly specified as LEAF for
257 * directories that are not on the dump tape.
259 * Check to see if the file is on the tape.
261 if (TSTINO(ino, dumpmap))
264 * Check to see if the name exists, and if the name is a link.
266 np = lookupname(name);
269 ip = lookupino(np->e_ino);
271 panic("corrupted symbol table\n");
276 * Check to see if the inode exists, and if one of its links
277 * corresponds to the name (if one was found).
282 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
290 * If both a name and an inode are found, but they do not
291 * correspond to the same file, then both the inode that has
292 * been found and the inode corresponding to the name that
293 * has been found need to be renamed. The current pathname
294 * is the new name for the inode that has been found. Since
295 * all files to be deleted have already been removed, the
296 * named file is either a now unneeded link, or it must live
297 * under a new name in this dump level. If it is a link, it
298 * can be removed. If it is not a link, it is given a
299 * temporary name in anticipation that it will be renamed
300 * when it is later found by inode number.
302 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
303 if (lookuptype == LINK) {
307 dprintf(stdout, "name/inode conflict, mktempname %s\n",
314 if ((key & ONTAPE) &&
315 (((key & INOFND) && ip->e_type != type) ||
316 ((key & NAMEFND) && np->e_type != type)))
320 * Decide on the disposition of the file based on its flags.
321 * Note that we have already handled the case in which
322 * a name and inode are found that correspond to different files.
323 * Thus if both NAMEFND and INOFND are set then ip == np.
328 * A previously existing file has been found.
329 * Mark it as KEEP so that other links to the inode can be
330 * detected, and so that it will not be reclaimed by the search
331 * for unreferenced names.
335 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
340 * A file on the tape has a name which is the same as a name
341 * corresponding to a different file in the previous dump.
342 * Since all files to be deleted have already been removed,
343 * this file is either a now unneeded link, or it must live
344 * under a new name in this dump level. If it is a link, it
345 * can simply be removed. If it is not a link, it is given a
346 * temporary name in anticipation that it will be renamed
347 * when it is later found by inode number (see INOFND case
348 * below). The entry is then treated as a new file.
351 case ONTAPE|NAMEFND|MODECHG:
352 if (lookuptype == LINK) {
361 * A previously non-existent file.
362 * Add it to the file system, and request its extraction.
363 * If it is a directory, create it immediately.
364 * (Since the name is unused there can be no conflict)
367 ep = addentry(name, ino, type);
370 ep->e_flags |= NEW|KEEP;
371 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
376 * A file with the same inode number, but a different
377 * name has been found. If the other name has not already
378 * been found (indicated by the KEEP flag, see above) then
379 * this must be a new name for the file, and it is renamed.
380 * If the other name has been found then this must be a
381 * link to the file. Hard links to directories are not
382 * permitted, and are either deleted or converted to
383 * symbolic links. Finally, if the file is on the tape,
384 * a request is made to extract it.
387 if (type == LEAF && (ip->e_flags & KEEP) == 0)
388 ip->e_flags |= EXTRACT;
391 if ((ip->e_flags & KEEP) == 0) {
392 renameit(myname(ip), name);
395 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
399 if (ip->e_type == NODE) {
402 "deleted hard link %s to directory %s\n",
406 ep = addentry(name, ino, type|LINK);
408 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
413 * A previously known file which is to be updated. If it is a link,
414 * then all names referring to the previous file must be removed
415 * so that the subset of them that remain can be recreated.
417 case ONTAPE|INOFND|NAMEFND:
418 if (lookuptype == LINK) {
421 ep = addentry(name, ino, type|LINK);
424 ep->e_flags |= NEW|KEEP;
425 dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
429 if (type == LEAF && lookuptype != LINK)
430 np->e_flags |= EXTRACT;
432 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
437 * An inode is being reused in a completely different way.
438 * Normally an extract can simply do an "unlink" followed
439 * by a "creat". Here we must do effectively the same
440 * thing. The complications arise because we cannot really
441 * delete a directory since it may still contain files
442 * that we need to rename, so we delete it from the symbol
443 * table, and put it on the list to be deleted eventually.
444 * Conversely if a directory is to be created, it must be
445 * done immediately, rather than waiting until the
448 case ONTAPE|INOFND|MODECHG:
449 case ONTAPE|INOFND|NAMEFND|MODECHG:
450 if (ip->e_flags & KEEP) {
451 badentry(ip, "cannot KEEP and change modes");
454 if (ip->e_type == LEAF) {
455 /* changing from leaf to node */
456 for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
457 if (ip->e_type != LEAF)
458 badentry(ip, "NODE and LEAF links to same inode");
462 ip = addentry(name, ino, type);
465 /* changing from node to leaf */
466 if ((ip->e_flags & TMPNAME) == 0)
468 deleteino(ip->e_ino);
469 ip->e_next = removelist;
471 ip = addentry(name, ino, type);
473 ip->e_flags |= NEW|KEEP;
474 dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
479 * A hard link to a directory that has been removed.
483 dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
489 * If we find a directory entry for a file that is not on
490 * the tape, then we must have found a file that was created
491 * while the dump was in progress. Since we have no contents
492 * for it, we discard the name knowing that it will be on the
493 * next incremental tape.
496 if (compare_ignore_not_found) break;
497 fprintf(stderr, "%s: (inode %ld) not found on tape\n",
502 * If any of these arise, something is grievously wrong with
503 * the current state of the symbol table.
505 case INOFND|NAMEFND|MODECHG:
506 case NAMEFND|MODECHG:
508 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
513 * These states "cannot" arise for any state of the symbol table.
518 panic("[%s] %s: impossible state\n", keyval(key), name);
525 * Calculate the active flags in a key.
531 static char keybuf[32];
533 (void) strcpy(keybuf, "|NIL");
536 (void) strcat(keybuf, "|ONTAPE");
538 (void) strcat(keybuf, "|INOFND");
540 (void) strcat(keybuf, "|NAMEFND");
542 (void) strcat(keybuf, "|MODECHG");
547 * Find unreferenced link names.
552 register struct entry *ep, *np;
555 vprintf(stdout, "Find unreferenced names.\n");
556 for (i = ROOTINO; i < maxino; i++) {
558 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
560 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
561 if (np->e_flags == 0) {
563 "%s: remove unreferenced name\n",
571 * Any leaves remaining in removed directories is unreferenced.
573 for (ep = removelist; ep != NULL; ep = ep->e_next) {
574 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
575 if (np->e_type == LEAF) {
576 if (np->e_flags != 0)
577 badentry(np, "unreferenced with flags");
579 "%s: remove unreferenced name\n",
589 * Remove old nodes (directories).
590 * Note that this routine runs in O(N*D) where:
591 * N is the number of directory entries to be removed.
592 * D is the maximum depth of the tree.
593 * If N == D this can be quite slow. If the list were
594 * topologically sorted, the deletion could be done in
600 register struct entry *ep, **prev;
603 vprintf(stdout, "Remove old nodes (directories).\n");
607 for (ep = removelist; ep != NULL; ep = *prev) {
608 if (ep->e_entries != NULL) {
618 for (ep = removelist; ep != NULL; ep = ep->e_next)
619 badentry(ep, "cannot remove, non-empty");
622 /* Compare the file specified in `ep' (which is on tape) to the */
623 /* current copy of this file on disk. If do_compare is 0, then just */
624 /* make our caller think we did it--this is used to handle hard links */
625 /* to files and devices. */
627 compare_entry(struct entry *ep, int do_compare)
629 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
630 badentry(ep, "unexpected file on tape");
631 if (do_compare) (void) comparefile(myname(ep));
632 ep->e_flags &= ~(NEW|EXTRACT);
636 * This is the routine used to compare files for the 'C' command.
641 register struct entry *ep;
645 first = lowerbnd(ROOTINO);
647 while (curfile.ino < maxino) {
648 first = lowerbnd(first);
650 * If the next available file is not the one which we
651 * expect then we have missed one or more files. Since
652 * we do not request files that were not on the tape,
653 * the lost files must have been due to a tape read error,
654 * or a file that was removed while the dump was in progress.
656 while (first < curfile.ino) {
657 ep = lookupino(first);
659 panic("%d: bad first\n", first);
660 fprintf(stderr, "%s: not found on tape\n", myname(ep));
661 ep->e_flags &= ~(NEW|EXTRACT);
662 first = lowerbnd(first);
665 * If we find files on the tape that have no corresponding
666 * directory entries, then we must have found a file that
667 * was created while the dump was in progress. Since we have
668 * no name for it, we discard it knowing that it will be
669 * on the next incremental tape.
671 if (first != curfile.ino) {
672 fprintf(stderr, "expected next file %ld, got %ld\n",
677 ep = lookupino(curfile.ino);
679 panic("unknown file on tape\n");
680 compare_entry(ep, 1);
681 for (ep = ep->e_links; ep != NULL; ep = ep->e_links) {
682 compare_entry(ep, 0);
686 * We checkpoint the restore after every tape reel, so
687 * as to simplify the amount of work re quired by the
691 if (curvol != volno) {
699 * This is the routine used to extract files for the 'r' command.
700 * Extract new leaves.
703 createleaves(symtabfile)
706 register struct entry *ep;
710 if (command == 'R') {
711 vprintf(stdout, "Continue extraction of new leaves\n");
713 vprintf(stdout, "Extract new leaves.\n");
714 dumpsymtable(symtabfile, volno);
716 first = lowerbnd(ROOTINO);
718 while (curfile.ino < maxino) {
719 first = lowerbnd(first);
721 * If the next available file is not the one which we
722 * expect then we have missed one or more files. Since
723 * we do not request files that were not on the tape,
724 * the lost files must have been due to a tape read error,
725 * or a file that was removed while the dump was in progress.
727 while (first < curfile.ino) {
728 ep = lookupino(first);
730 panic("%d: bad first\n", first);
731 fprintf(stderr, "%s: not found on tape\n", myname(ep));
732 ep->e_flags &= ~(NEW|EXTRACT);
733 first = lowerbnd(first);
736 * If we find files on the tape that have no corresponding
737 * directory entries, then we must have found a file that
738 * was created while the dump was in progress. Since we have
739 * no name for it, we discard it knowing that it will be
740 * on the next incremental tape.
742 if (first != curfile.ino) {
743 fprintf(stderr, "expected next file %ld, got %ld\n",
748 ep = lookupino(curfile.ino);
750 panic("unknown file on tape\n");
751 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
752 badentry(ep, "unexpected file on tape");
754 * If the file is to be extracted, then the old file must
755 * be removed since its type may change from one leaf type
756 * to another (e.g. "file" to "character special").
758 if ((ep->e_flags & EXTRACT) != 0) {
760 ep->e_flags &= ~REMOVED;
762 (void) extractfile(myname(ep));
763 ep->e_flags &= ~(NEW|EXTRACT);
765 * We checkpoint the restore after every tape reel, so
766 * as to simplify the amount of work required by the
770 if (curvol != volno) {
771 dumpsymtable(symtabfile, volno);
779 * This is the routine used to extract files for the 'x' and 'i' commands.
780 * Efficiently extract a subset of the files on a tape.
785 register ino_t first, next, last;
786 register struct entry *ep;
789 vprintf(stdout, "Extract requested files\n");
790 curfile.action = SKIP;
794 first = lowerbnd(ROOTINO);
795 last = upperbnd(maxino - 1);
797 first = lowerbnd(first);
798 last = upperbnd(last);
800 * Check to see if any files remain to be extracted
805 * Reject any volumes with inodes greater
806 * than the last one needed
808 while (curfile.ino > last) {
809 curfile.action = SKIP;
815 * Decide on the next inode needed.
816 * Skip across the inodes until it is found
817 * or an out of order volume change is encountered
819 next = lowerbnd(curfile.ino);
822 while (next > curfile.ino && volno == curvol)
826 } while (volno == curvol + 1);
828 * If volume change out of order occurred the
829 * current state must be recalculated
834 * If the current inode is greater than the one we were
835 * looking for then we missed the one we were looking for.
836 * Since we only attempt to extract files listed in the
837 * dump map, the lost files must have been due to a tape
838 * read error, or a file that was removed while the dump
839 * was in progress. Thus we report all requested files
840 * between the one we were looking for, and the one we
841 * found as missing, and delete their request flags.
843 while (next < curfile.ino) {
844 ep = lookupino(next);
846 panic("corrupted symbol table\n");
847 fprintf(stderr, "%s: not found on tape\n", myname(ep));
849 next = lowerbnd(next);
852 * The current inode is the one that we are looking for,
853 * so extract it per its requested name.
855 if (next == curfile.ino && next <= last) {
856 ep = lookupino(next);
858 panic("corrupted symbol table\n");
859 (void) extractfile(myname(ep));
873 register struct entry *np, *ep;
877 if ((ep = lookupino(WINO))) {
878 vprintf(stdout, "Add whiteouts\n");
879 for ( ; ep != NULL; ep = ep->e_links) {
880 if ((ep->e_flags & NEW) == 0)
883 (void)fprintf(stderr, "BUG! Should call addwhiteout\n");
885 (void) addwhiteout(myname(ep));
890 vprintf(stdout, "Add links\n");
891 for (i = ROOTINO; i < maxino; i++) {
895 for (np = ep->e_links; np != NULL; np = np->e_links) {
896 if ((np->e_flags & NEW) == 0)
898 (void) strcpy(name, myname(ep));
899 if (ep->e_type == NODE) {
900 (void) linkit(name, myname(np), SYMLINK);
902 (void) linkit(name, myname(np), HARDLINK);
910 * Check the symbol table.
911 * We do this to insure that all the requested work was done, and
912 * that no temporary names remain.
917 register struct entry *ep;
920 vprintf(stdout, "Check the symbol table.\n");
921 for (i = WINO; i < maxino; i++) {
922 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
923 ep->e_flags &= ~KEEP;
924 if (ep->e_type == NODE)
925 ep->e_flags &= ~(NEW|EXISTED);
926 if (ep->e_flags != 0)
927 badentry(ep, "incomplete operations");
933 * Compare with the directory structure on the tape
934 * A paranoid check that things are as they should be.
937 verifyfile(name, ino, type)
942 struct entry *np, *ep;
945 ep = lookupname(name);
947 fprintf(stderr, "Warning: missing name %s\n", name);
953 for ( ; np != NULL; np = np->e_links)
957 panic("missing inumber %d\n", ino);
958 if (ep->e_type == LEAF && type != LEAF)
959 badentry(ep, "type should be LEAF");