]>
git.wh0rd.org - dump.git/blob - restore/restore.c
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
41 * $Id: restore.c,v 1.5 1999/10/11 13:31:13 stelian Exp $
44 #include <sys/types.h>
47 #include <sys/param.h>
49 #include <linux/ext2_fs.h>
50 #include <bsdcompat.h>
52 #include <ufs/ufs/dinode.h>
53 #endif /* __linux__ */
59 #include <ext2fs/ext2fs.h>
65 static char *keyval
__P((int));
68 * This implements the 't' option.
69 * List entries on the tape.
72 listfile(char *name
, ino_t ino
, int type
)
74 long descend
= hflag
? GOOD
: FAIL
;
76 if (TSTINO(ino
, dumpmap
) == 0)
78 Vprintf(stdout
, "%s", type
== LEAF
? "leaf" : "dir ");
79 fprintf(stdout
, "%10lu\t%s\n", (unsigned long)ino
, name
);
84 * This implements the 'x' option.
85 * Request that new entries be extracted.
88 addfile(char *name
, ino_t ino
, int type
)
90 register struct entry
*ep
;
91 long descend
= hflag
? GOOD
: FAIL
;
94 if (TSTINO(ino
, dumpmap
) == 0) {
95 Dprintf(stdout
, "%s: not on the tape\n", name
);
98 if (ino
== WINO
&& command
== 'i' && !vflag
)
101 (void) snprintf(buf
, sizeof(buf
), "./%lu", (unsigned long)ino
);
104 (void) genliteraldir(name
, ino
);
110 if (strcmp(name
, myname(ep
)) == 0) {
116 ep
= addentry(name
, ino
, type
);
124 * This is used by the 'i' option to undo previous requests made by addfile.
125 * Delete entries from the request queue.
129 deletefile(char *name
, ino_t ino
, int type
)
131 long descend
= hflag
? GOOD
: FAIL
;
134 if (TSTINO(ino
, dumpmap
) == 0)
136 ep
= lookupname(name
);
139 ep
->e_flags
|= REMOVED
;
140 if (ep
->e_type
!= NODE
)
147 * The following four routines implement the incremental
148 * restore algorithm. The first removes old entries, the second
149 * does renames and calculates the extraction list, the third
150 * cleans up link names missed by the first two, and the final
151 * one deletes old directories.
153 * Directories cannot be immediately deleted, as they may have
154 * other files in them which need to be moved out first. As
155 * directories to be deleted are found, they are put on the
156 * following deletion list. After all deletions and renames
157 * are done, this list is actually deleted.
159 static struct entry
*removelist
;
162 * Remove invalid whiteouts from the old tree.
163 * Remove unneeded leaves from the old tree.
164 * Remove directories from the lookup chains.
167 removeoldleaves(void)
169 register struct entry
*ep
, *nextep
;
170 register ino_t i
, mydirino
;
172 Vprintf(stdout
, "Mark entries to be removed.\n");
173 if ((ep
= lookupino(WINO
))) {
174 Vprintf(stdout
, "Delete whiteouts\n");
175 for ( ; ep
!= NULL
; ep
= nextep
) {
176 nextep
= ep
->e_links
;
177 mydirino
= ep
->e_parent
->e_ino
;
179 * We remove all whiteouts that are in directories
180 * that have been removed or that have been dumped.
182 if (TSTINO(mydirino
, usedinomap
) &&
183 !TSTINO(mydirino
, dumpmap
))
186 (void)fprintf(stderr
, "BUG! Should call delwhiteout\n");
193 for (i
= ROOTINO
+ 1; i
< maxino
; i
++) {
197 if (TSTINO(i
, usedinomap
))
199 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
200 Dprintf(stdout
, "%s: REMOVE\n", myname(ep
));
201 if (ep
->e_type
== LEAF
) {
206 deleteino(ep
->e_ino
);
207 ep
->e_next
= removelist
;
215 * For each directory entry on the incremental tape, determine which
216 * category it falls into as follows:
217 * KEEP - entries that are to be left alone.
218 * NEW - new entries to be added.
219 * EXTRACT - files that must be updated with new contents.
220 * LINK - new links to be added.
221 * Renames are done at the same time.
224 nodeupdates(char *name
, ino_t ino
, int type
)
226 register struct entry
*ep
, *np
, *ip
;
231 # define ONTAPE 0x1 /* inode is on the tape */
232 # define INOFND 0x2 /* inode already exists */
233 # define NAMEFND 0x4 /* name already exists */
234 # define MODECHG 0x8 /* mode of inode changed */
237 * This routine is called once for each element in the
238 * directory hierarchy, with a full path name.
239 * The "type" value is incorrectly specified as LEAF for
240 * directories that are not on the dump tape.
242 * Check to see if the file is on the tape.
244 if (TSTINO(ino
, dumpmap
))
247 * Check to see if the name exists, and if the name is a link.
249 np
= lookupname(name
);
252 ip
= lookupino(np
->e_ino
);
254 panic("corrupted symbol table\n");
259 * Check to see if the inode exists, and if one of its links
260 * corresponds to the name (if one was found).
265 for (ep
= ip
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
273 * If both a name and an inode are found, but they do not
274 * correspond to the same file, then both the inode that has
275 * been found and the inode corresponding to the name that
276 * has been found need to be renamed. The current pathname
277 * is the new name for the inode that has been found. Since
278 * all files to be deleted have already been removed, the
279 * named file is either a now unneeded link, or it must live
280 * under a new name in this dump level. If it is a link, it
281 * can be removed. If it is not a link, it is given a
282 * temporary name in anticipation that it will be renamed
283 * when it is later found by inode number.
285 if (((key
& (INOFND
|NAMEFND
)) == (INOFND
|NAMEFND
)) && ip
!= np
) {
286 if (lookuptype
== LINK
) {
290 Dprintf(stdout
, "name/inode conflict, mktempname %s\n",
297 if ((key
& ONTAPE
) &&
298 (((key
& INOFND
) && ip
->e_type
!= type
) ||
299 ((key
& NAMEFND
) && np
->e_type
!= type
)))
303 * Decide on the disposition of the file based on its flags.
304 * Note that we have already handled the case in which
305 * a name and inode are found that correspond to different files.
306 * Thus if both NAMEFND and INOFND are set then ip == np.
311 * A previously existing file has been found.
312 * Mark it as KEEP so that other links to the inode can be
313 * detected, and so that it will not be reclaimed by the search
314 * for unreferenced names.
318 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
323 * A file on the tape has a name which is the same as a name
324 * corresponding to a different file in the previous dump.
325 * Since all files to be deleted have already been removed,
326 * this file is either a now unneeded link, or it must live
327 * under a new name in this dump level. If it is a link, it
328 * can simply be removed. If it is not a link, it is given a
329 * temporary name in anticipation that it will be renamed
330 * when it is later found by inode number (see INOFND case
331 * below). The entry is then treated as a new file.
334 case ONTAPE
|NAMEFND
|MODECHG
:
335 if (lookuptype
== LINK
) {
344 * A previously non-existent file.
345 * Add it to the file system, and request its extraction.
346 * If it is a directory, create it immediately.
347 * (Since the name is unused there can be no conflict)
350 ep
= addentry(name
, ino
, type
);
353 ep
->e_flags
|= NEW
|KEEP
;
354 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
359 * A file with the same inode number, but a different
360 * name has been found. If the other name has not already
361 * been found (indicated by the KEEP flag, see above) then
362 * this must be a new name for the file, and it is renamed.
363 * If the other name has been found then this must be a
364 * link to the file. Hard links to directories are not
365 * permitted, and are either deleted or converted to
366 * symbolic links. Finally, if the file is on the tape,
367 * a request is made to extract it.
370 if (type
== LEAF
&& (ip
->e_flags
& KEEP
) == 0)
371 ip
->e_flags
|= EXTRACT
;
374 if ((ip
->e_flags
& KEEP
) == 0) {
375 renameit(myname(ip
), name
);
378 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
382 if (ip
->e_type
== NODE
) {
385 "deleted hard link %s to directory %s\n",
389 ep
= addentry(name
, ino
, type
|LINK
);
391 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
396 * A previously known file which is to be updated. If it is a link,
397 * then all names referring to the previous file must be removed
398 * so that the subset of them that remain can be recreated.
400 case ONTAPE
|INOFND
|NAMEFND
:
401 if (lookuptype
== LINK
) {
404 ep
= addentry(name
, ino
, type
|LINK
);
407 ep
->e_flags
|= NEW
|KEEP
;
408 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
412 if (type
== LEAF
&& lookuptype
!= LINK
)
413 np
->e_flags
|= EXTRACT
;
415 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
420 * An inode is being reused in a completely different way.
421 * Normally an extract can simply do an "unlink" followed
422 * by a "creat". Here we must do effectively the same
423 * thing. The complications arise because we cannot really
424 * delete a directory since it may still contain files
425 * that we need to rename, so we delete it from the symbol
426 * table, and put it on the list to be deleted eventually.
427 * Conversely if a directory is to be created, it must be
428 * done immediately, rather than waiting until the
431 case ONTAPE
|INOFND
|MODECHG
:
432 case ONTAPE
|INOFND
|NAMEFND
|MODECHG
:
433 if (ip
->e_flags
& KEEP
) {
434 badentry(ip
, "cannot KEEP and change modes");
437 if (ip
->e_type
== LEAF
) {
438 /* changing from leaf to node */
439 for ( ; ip
!= NULL
; ip
= ip
->e_links
) {
440 if (ip
->e_type
!= LEAF
)
441 badentry(ip
, "NODE and LEAF links to same inode");
445 ip
= addentry(name
, ino
, type
);
448 /* changing from node to leaf */
449 if ((ip
->e_flags
& TMPNAME
) == 0)
451 deleteino(ip
->e_ino
);
452 ip
->e_next
= removelist
;
454 ip
= addentry(name
, ino
, type
);
456 ip
->e_flags
|= NEW
|KEEP
;
457 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
462 * A hard link to a directory that has been removed.
466 Dprintf(stdout
, "[%s] %s: Extraneous name\n", keyval(key
),
472 * If we find a directory entry for a file that is not on
473 * the tape, then we must have found a file that was created
474 * while the dump was in progress. Since we have no contents
475 * for it, we discard the name knowing that it will be on the
476 * next incremental tape.
479 if (compare_ignore_not_found
) break;
480 fprintf(stderr
, "%s: (inode %lu) not found on tape\n",
481 name
, (unsigned long)ino
);
485 * If any of these arise, something is grievously wrong with
486 * the current state of the symbol table.
488 case INOFND
|NAMEFND
|MODECHG
:
489 case NAMEFND
|MODECHG
:
491 fprintf(stderr
, "[%s] %s: inconsistent state\n", keyval(key
),
496 * These states "cannot" arise for any state of the symbol table.
501 panic("[%s] %s: impossible state\n", keyval(key
), name
);
508 * Calculate the active flags in a key.
513 static char keybuf
[32];
515 (void) strcpy(keybuf
, "|NIL");
518 (void) strcat(keybuf
, "|ONTAPE");
520 (void) strcat(keybuf
, "|INOFND");
522 (void) strcat(keybuf
, "|NAMEFND");
524 (void) strcat(keybuf
, "|MODECHG");
529 * Find unreferenced link names.
534 register struct entry
*ep
, *np
;
537 Vprintf(stdout
, "Find unreferenced names.\n");
538 for (i
= ROOTINO
; i
< maxino
; i
++) {
540 if (ep
== NULL
|| ep
->e_type
== LEAF
|| TSTINO(i
, dumpmap
) == 0)
542 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
543 if (np
->e_flags
== 0) {
545 "%s: remove unreferenced name\n",
553 * Any leaves remaining in removed directories is unreferenced.
555 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
) {
556 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
557 if (np
->e_type
== LEAF
) {
558 if (np
->e_flags
!= 0)
559 badentry(np
, "unreferenced with flags");
561 "%s: remove unreferenced name\n",
571 * Remove old nodes (directories).
572 * Note that this routine runs in O(N*D) where:
573 * N is the number of directory entries to be removed.
574 * D is the maximum depth of the tree.
575 * If N == D this can be quite slow. If the list were
576 * topologically sorted, the deletion could be done in
582 register struct entry
*ep
, **prev
;
585 Vprintf(stdout
, "Remove old nodes (directories).\n");
589 for (ep
= removelist
; ep
!= NULL
; ep
= *prev
) {
590 if (ep
->e_entries
!= NULL
) {
600 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
)
601 badentry(ep
, "cannot remove, non-empty");
604 /* Compare the file specified in `ep' (which is on tape) to the */
605 /* current copy of this file on disk. If do_compare is 0, then just */
606 /* make our caller think we did it--this is used to handle hard links */
607 /* to files and devices. */
609 compare_entry(struct entry
*ep
, int do_compare
)
611 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
612 badentry(ep
, "unexpected file on tape");
613 if (do_compare
) (void) comparefile(myname(ep
));
614 ep
->e_flags
&= ~(NEW
|EXTRACT
);
618 * This is the routine used to compare files for the 'C' command.
623 register struct entry
*ep
;
627 first
= lowerbnd(ROOTINO
);
629 while (curfile
.ino
< maxino
) {
630 first
= lowerbnd(first
);
632 * If the next available file is not the one which we
633 * expect then we have missed one or more files. Since
634 * we do not request files that were not on the tape,
635 * the lost files must have been due to a tape read error,
636 * or a file that was removed while the dump was in progress.
638 while (first
< curfile
.ino
) {
639 ep
= lookupino(first
);
641 panic("%d: bad first\n", first
);
642 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
643 ep
->e_flags
&= ~(NEW
|EXTRACT
);
644 first
= lowerbnd(first
);
647 * If we find files on the tape that have no corresponding
648 * directory entries, then we must have found a file that
649 * was created while the dump was in progress. Since we have
650 * no name for it, we discard it knowing that it will be
651 * on the next incremental tape.
653 if (first
!= curfile
.ino
) {
654 fprintf(stderr
, "expected next file %ld, got %lu\n",
655 (long)first
, (unsigned long)curfile
.ino
);
659 ep
= lookupino(curfile
.ino
);
661 panic("unknown file on tape\n");
662 compare_entry(ep
, 1);
663 for (ep
= ep
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
664 compare_entry(ep
, 0);
668 * We checkpoint the restore after every tape reel, so
669 * as to simplify the amount of work re quired by the
673 if (curvol
!= volno
) {
681 * This is the routine used to extract files for the 'r' command.
682 * Extract new leaves.
685 createleaves(char *symtabfile
)
687 register struct entry
*ep
;
691 if (command
== 'R') {
692 Vprintf(stdout
, "Continue extraction of new leaves\n");
694 Vprintf(stdout
, "Extract new leaves.\n");
695 dumpsymtable(symtabfile
, volno
);
697 first
= lowerbnd(ROOTINO
);
699 while (curfile
.ino
< maxino
) {
700 first
= lowerbnd(first
);
702 * If the next available file is not the one which we
703 * expect then we have missed one or more files. Since
704 * we do not request files that were not on the tape,
705 * the lost files must have been due to a tape read error,
706 * or a file that was removed while the dump was in progress.
708 while (first
< curfile
.ino
) {
709 ep
= lookupino(first
);
711 panic("%d: bad first\n", first
);
712 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
713 ep
->e_flags
&= ~(NEW
|EXTRACT
);
714 first
= lowerbnd(first
);
717 * If we find files on the tape that have no corresponding
718 * directory entries, then we must have found a file that
719 * was created while the dump was in progress. Since we have
720 * no name for it, we discard it knowing that it will be
721 * on the next incremental tape.
723 if (first
!= curfile
.ino
) {
724 fprintf(stderr
, "expected next file %ld, got %lu\n",
725 (long)first
, (unsigned long)curfile
.ino
);
729 ep
= lookupino(curfile
.ino
);
731 panic("unknown file on tape\n");
732 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
733 badentry(ep
, "unexpected file on tape");
735 * If the file is to be extracted, then the old file must
736 * be removed since its type may change from one leaf type
737 * to another (e.g. "file" to "character special").
739 if ((ep
->e_flags
& EXTRACT
) != 0) {
741 ep
->e_flags
&= ~REMOVED
;
743 (void) extractfile(myname(ep
));
744 ep
->e_flags
&= ~(NEW
|EXTRACT
);
746 * We checkpoint the restore after every tape reel, so
747 * as to simplify the amount of work required by the
751 if (curvol
!= volno
) {
752 dumpsymtable(symtabfile
, volno
);
760 * This is the routine used to extract files for the 'x' and 'i' commands.
761 * Efficiently extract a subset of the files on a tape.
766 register ino_t first
, next
, last
;
767 register struct entry
*ep
;
770 Vprintf(stdout
, "Extract requested files\n");
771 curfile
.action
= SKIP
;
775 first
= lowerbnd(ROOTINO
);
776 last
= upperbnd(maxino
- 1);
778 first
= lowerbnd(first
);
779 last
= upperbnd(last
);
781 * Check to see if any files remain to be extracted
786 * Reject any volumes with inodes greater
787 * than the last one needed
789 while (curfile
.ino
> last
) {
790 curfile
.action
= SKIP
;
796 * Decide on the next inode needed.
797 * Skip across the inodes until it is found
798 * or an out of order volume change is encountered
800 next
= lowerbnd(curfile
.ino
);
803 while (next
> curfile
.ino
&& volno
== curvol
)
807 } while (volno
== curvol
+ 1);
809 * If volume change out of order occurred the
810 * current state must be recalculated
815 * If the current inode is greater than the one we were
816 * looking for then we missed the one we were looking for.
817 * Since we only attempt to extract files listed in the
818 * dump map, the lost files must have been due to a tape
819 * read error, or a file that was removed while the dump
820 * was in progress. Thus we report all requested files
821 * between the one we were looking for, and the one we
822 * found as missing, and delete their request flags.
824 while (next
< curfile
.ino
) {
825 ep
= lookupino(next
);
827 panic("corrupted symbol table\n");
828 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
830 next
= lowerbnd(next
);
833 * The current inode is the one that we are looking for,
834 * so extract it per its requested name.
836 if (next
== curfile
.ino
&& next
<= last
) {
837 ep
= lookupino(next
);
839 panic("corrupted symbol table\n");
840 (void) extractfile(myname(ep
));
854 register struct entry
*np
, *ep
;
858 if ((ep
= lookupino(WINO
))) {
859 Vprintf(stdout
, "Add whiteouts\n");
860 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
861 if ((ep
->e_flags
& NEW
) == 0)
864 (void)fprintf(stderr
, "BUG! Should call addwhiteout\n");
866 (void) addwhiteout(myname(ep
));
871 Vprintf(stdout
, "Add links\n");
872 for (i
= ROOTINO
; i
< maxino
; i
++) {
876 for (np
= ep
->e_links
; np
!= NULL
; np
= np
->e_links
) {
877 if ((np
->e_flags
& NEW
) == 0)
879 (void) strcpy(name
, myname(ep
));
880 if (ep
->e_type
== NODE
) {
881 (void) linkit(name
, myname(np
), SYMLINK
);
883 (void) linkit(name
, myname(np
), HARDLINK
);
891 * Check the symbol table.
892 * We do this to insure that all the requested work was done, and
893 * that no temporary names remain.
898 register struct entry
*ep
;
901 Vprintf(stdout
, "Check the symbol table.\n");
902 for (i
= WINO
; i
< maxino
; i
++) {
903 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
904 ep
->e_flags
&= ~KEEP
;
905 if (ep
->e_type
== NODE
)
906 ep
->e_flags
&= ~(NEW
|EXISTED
);
907 if (ep
->e_flags
/* != NULL */)
908 badentry(ep
, "incomplete operations");
914 * Compare with the directory structure on the tape
915 * A paranoid check that things are as they should be.
918 verifyfile(char *name
, ino_t ino
, int type
)
920 struct entry
*np
, *ep
;
923 ep
= lookupname(name
);
925 fprintf(stderr
, "Warning: missing name %s\n", name
);
931 for ( ; np
!= NULL
; np
= np
->e_links
)
935 panic("missing inumber %d\n", ino
);
936 if (ep
->e_type
== LEAF
&& type
!= LEAF
)
937 badentry(ep
, "type should be LEAF");