]>
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, 1995, 1996
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static char sccsid
[] = "@(#)restore.c 8.3 (Berkeley) 9/13/94";
45 #include <sys/types.h>
50 #include <linux/ext2_fs.h>
51 #include <bsdcompat.h>
53 #include <ufs/ufs/dinode.h>
54 #endif /* __linux__ */
60 #include <ext2fs/ext2fs.h>
66 static char *keyval
__P((int));
69 * This implements the 't' option.
70 * List entries on the tape.
73 listfile(name
, ino
, type
)
78 long descend
= hflag
? GOOD
: FAIL
;
80 if (TSTINO(ino
, dumpmap
) == 0)
82 vprintf(stdout
, "%s", type
== LEAF
? "leaf" : "dir ");
83 fprintf(stdout
, "%10d\t%s\n", ino
, name
);
88 * This implements the 'x' option.
89 * Request that new entries be extracted.
92 addfile(name
, ino
, type
)
97 register struct entry
*ep
;
98 long descend
= hflag
? GOOD
: FAIL
;
101 if (TSTINO(ino
, dumpmap
) == 0) {
102 dprintf(stdout
, "%s: not on the tape\n", name
);
105 if (ino
== WINO
&& command
== 'i' && !vflag
)
108 (void) sprintf(buf
, "./%u", ino
);
111 (void) genliteraldir(name
, ino
);
117 if (strcmp(name
, myname(ep
)) == 0) {
123 ep
= addentry(name
, ino
, type
);
131 * This is used by the 'i' option to undo previous requests made by addfile.
132 * Delete entries from the request queue.
136 deletefile(name
, ino
, type
)
141 long descend
= hflag
? GOOD
: FAIL
;
144 if (TSTINO(ino
, dumpmap
) == 0)
146 ep
= lookupname(name
);
149 ep
->e_flags
|= REMOVED
;
150 if (ep
->e_type
!= NODE
)
157 * The following four routines implement the incremental
158 * restore algorithm. The first removes old entries, the second
159 * does renames and calculates the extraction list, the third
160 * cleans up link names missed by the first two, and the final
161 * one deletes old directories.
163 * Directories cannot be immediately deleted, as they may have
164 * other files in them which need to be moved out first. As
165 * directories to be deleted are found, they are put on the
166 * following deletion list. After all deletions and renames
167 * are done, this list is actually deleted.
169 static struct entry
*removelist
;
172 * Remove invalid whiteouts from the old tree.
173 * Remove unneeded leaves from the old tree.
174 * Remove directories from the lookup chains.
179 register struct entry
*ep
, *nextep
;
180 register ino_t i
, mydirino
;
182 vprintf(stdout
, "Mark entries to be removed.\n");
183 if (ep
= lookupino(WINO
)) {
184 vprintf(stdout
, "Delete whiteouts\n");
185 for ( ; ep
!= NULL
; ep
= nextep
) {
186 nextep
= ep
->e_links
;
187 mydirino
= ep
->e_parent
->e_ino
;
189 * We remove all whiteouts that are in directories
190 * that have been removed or that have been dumped.
192 if (TSTINO(mydirino
, usedinomap
) &&
193 !TSTINO(mydirino
, dumpmap
))
196 (void)fprintf(stderr
, "BUG! Should call delwhiteout\n");
203 for (i
= ROOTINO
+ 1; i
< maxino
; i
++) {
207 if (TSTINO(i
, usedinomap
))
209 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
210 dprintf(stdout
, "%s: REMOVE\n", myname(ep
));
211 if (ep
->e_type
== LEAF
) {
216 deleteino(ep
->e_ino
);
217 ep
->e_next
= removelist
;
225 * For each directory entry on the incremental tape, determine which
226 * category it falls into as follows:
227 * KEEP - entries that are to be left alone.
228 * NEW - new entries to be added.
229 * EXTRACT - files that must be updated with new contents.
230 * LINK - new links to be added.
231 * Renames are done at the same time.
234 nodeupdates(name
, ino
, type
)
239 register struct entry
*ep
, *np
, *ip
;
244 # define ONTAPE 0x1 /* inode is on the tape */
245 # define INOFND 0x2 /* inode already exists */
246 # define NAMEFND 0x4 /* name already exists */
247 # define MODECHG 0x8 /* mode of inode changed */
250 * This routine is called once for each element in the
251 * directory hierarchy, with a full path name.
252 * The "type" value is incorrectly specified as LEAF for
253 * directories that are not on the dump tape.
255 * Check to see if the file is on the tape.
257 if (TSTINO(ino
, dumpmap
))
260 * Check to see if the name exists, and if the name is a link.
262 np
= lookupname(name
);
265 ip
= lookupino(np
->e_ino
);
267 panic("corrupted symbol table\n");
272 * Check to see if the inode exists, and if one of its links
273 * corresponds to the name (if one was found).
278 for (ep
= ip
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
286 * If both a name and an inode are found, but they do not
287 * correspond to the same file, then both the inode that has
288 * been found and the inode corresponding to the name that
289 * has been found need to be renamed. The current pathname
290 * is the new name for the inode that has been found. Since
291 * all files to be deleted have already been removed, the
292 * named file is either a now unneeded link, or it must live
293 * under a new name in this dump level. If it is a link, it
294 * can be removed. If it is not a link, it is given a
295 * temporary name in anticipation that it will be renamed
296 * when it is later found by inode number.
298 if (((key
& (INOFND
|NAMEFND
)) == (INOFND
|NAMEFND
)) && ip
!= np
) {
299 if (lookuptype
== LINK
) {
303 dprintf(stdout
, "name/inode conflict, mktempname %s\n",
310 if ((key
& ONTAPE
) &&
311 (((key
& INOFND
) && ip
->e_type
!= type
) ||
312 ((key
& NAMEFND
) && np
->e_type
!= type
)))
316 * Decide on the disposition of the file based on its flags.
317 * Note that we have already handled the case in which
318 * a name and inode are found that correspond to different files.
319 * Thus if both NAMEFND and INOFND are set then ip == np.
324 * A previously existing file has been found.
325 * Mark it as KEEP so that other links to the inode can be
326 * detected, and so that it will not be reclaimed by the search
327 * for unreferenced names.
331 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
336 * A file on the tape has a name which is the same as a name
337 * corresponding to a different file in the previous dump.
338 * Since all files to be deleted have already been removed,
339 * this file is either a now unneeded link, or it must live
340 * under a new name in this dump level. If it is a link, it
341 * can simply be removed. If it is not a link, it is given a
342 * temporary name in anticipation that it will be renamed
343 * when it is later found by inode number (see INOFND case
344 * below). The entry is then treated as a new file.
347 case ONTAPE
|NAMEFND
|MODECHG
:
348 if (lookuptype
== LINK
) {
357 * A previously non-existent file.
358 * Add it to the file system, and request its extraction.
359 * If it is a directory, create it immediately.
360 * (Since the name is unused there can be no conflict)
363 ep
= addentry(name
, ino
, type
);
366 ep
->e_flags
|= NEW
|KEEP
;
367 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
372 * A file with the same inode number, but a different
373 * name has been found. If the other name has not already
374 * been found (indicated by the KEEP flag, see above) then
375 * this must be a new name for the file, and it is renamed.
376 * If the other name has been found then this must be a
377 * link to the file. Hard links to directories are not
378 * permitted, and are either deleted or converted to
379 * symbolic links. Finally, if the file is on the tape,
380 * a request is made to extract it.
383 if (type
== LEAF
&& (ip
->e_flags
& KEEP
) == 0)
384 ip
->e_flags
|= EXTRACT
;
387 if ((ip
->e_flags
& KEEP
) == 0) {
388 renameit(myname(ip
), name
);
391 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
395 if (ip
->e_type
== NODE
) {
398 "deleted hard link %s to directory %s\n",
402 ep
= addentry(name
, ino
, type
|LINK
);
404 dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
409 * A previously known file which is to be updated. If it is a link,
410 * then all names referring to the previous file must be removed
411 * so that the subset of them that remain can be recreated.
413 case ONTAPE
|INOFND
|NAMEFND
:
414 if (lookuptype
== LINK
) {
417 ep
= addentry(name
, ino
, type
|LINK
);
420 ep
->e_flags
|= NEW
|KEEP
;
421 dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
425 if (type
== LEAF
&& lookuptype
!= LINK
)
426 np
->e_flags
|= EXTRACT
;
428 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
433 * An inode is being reused in a completely different way.
434 * Normally an extract can simply do an "unlink" followed
435 * by a "creat". Here we must do effectively the same
436 * thing. The complications arise because we cannot really
437 * delete a directory since it may still contain files
438 * that we need to rename, so we delete it from the symbol
439 * table, and put it on the list to be deleted eventually.
440 * Conversely if a directory is to be created, it must be
441 * done immediately, rather than waiting until the
444 case ONTAPE
|INOFND
|MODECHG
:
445 case ONTAPE
|INOFND
|NAMEFND
|MODECHG
:
446 if (ip
->e_flags
& KEEP
) {
447 badentry(ip
, "cannot KEEP and change modes");
450 if (ip
->e_type
== LEAF
) {
451 /* changing from leaf to node */
454 ip
= addentry(name
, ino
, type
);
457 /* changing from node to leaf */
458 if ((ip
->e_flags
& TMPNAME
) == 0)
460 deleteino(ip
->e_ino
);
461 ip
->e_next
= removelist
;
463 ip
= addentry(name
, ino
, type
);
465 ip
->e_flags
|= NEW
|KEEP
;
466 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
471 * A hard link to a diirectory that has been removed.
475 dprintf(stdout
, "[%s] %s: Extraneous name\n", keyval(key
),
481 * If we find a directory entry for a file that is not on
482 * the tape, then we must have found a file that was created
483 * while the dump was in progress. Since we have no contents
484 * for it, we discard the name knowing that it will be on the
485 * next incremental tape.
488 if (compare_ignore_not_found
) break;
489 fprintf(stderr
, "%s: (inode %d) not found on tape\n",
494 * If any of these arise, something is grievously wrong with
495 * the current state of the symbol table.
497 case INOFND
|NAMEFND
|MODECHG
:
498 case NAMEFND
|MODECHG
:
500 fprintf(stderr
, "[%s] %s: inconsistent state\n", keyval(key
),
505 * These states "cannot" arise for any state of the symbol table.
510 panic("[%s] %s: impossible state\n", keyval(key
), name
);
517 * Calculate the active flags in a key.
523 static char keybuf
[32];
525 (void) strcpy(keybuf
, "|NIL");
528 (void) strcat(keybuf
, "|ONTAPE");
530 (void) strcat(keybuf
, "|INOFND");
532 (void) strcat(keybuf
, "|NAMEFND");
534 (void) strcat(keybuf
, "|MODECHG");
539 * Find unreferenced link names.
544 register struct entry
*ep
, *np
;
547 vprintf(stdout
, "Find unreferenced names.\n");
548 for (i
= ROOTINO
; i
< maxino
; i
++) {
550 if (ep
== NULL
|| ep
->e_type
== LEAF
|| TSTINO(i
, dumpmap
) == 0)
552 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
553 if (np
->e_flags
== 0) {
555 "%s: remove unreferenced name\n",
563 * Any leaves remaining in removed directories is unreferenced.
565 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
) {
566 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
567 if (np
->e_type
== LEAF
) {
568 if (np
->e_flags
!= 0)
569 badentry(np
, "unreferenced with flags");
571 "%s: remove unreferenced name\n",
581 * Remove old nodes (directories).
582 * Note that this routine runs in O(N*D) where:
583 * N is the number of directory entries to be removed.
584 * D is the maximum depth of the tree.
585 * If N == D this can be quite slow. If the list were
586 * topologically sorted, the deletion could be done in
592 register struct entry
*ep
, **prev
;
595 vprintf(stdout
, "Remove old nodes (directories).\n");
599 for (ep
= removelist
; ep
!= NULL
; ep
= *prev
) {
600 if (ep
->e_entries
!= NULL
) {
610 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
)
611 badentry(ep
, "cannot remove, non-empty");
614 /* Compare the file specified in `ep' (which is on tape) to the */
615 /* current copy of this file on disk. If do_compare is 0, then just */
616 /* make our caller think we did it--this is used to handle hard links */
617 /* to files and devices. */
618 compare_entry(struct entry
*ep
, int do_compare
)
620 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
621 badentry(ep
, "unexpected file on tape");
622 if (do_compare
) (void) comparefile(myname(ep
));
623 ep
->e_flags
&= ~(NEW
|EXTRACT
);
627 * This is the routine used to compare files for the 'C' command.
632 register struct entry
*ep
;
636 first
= lowerbnd(ROOTINO
);
638 while (curfile
.ino
< maxino
) {
639 first
= lowerbnd(first
);
641 * If the next available file is not the one which we
642 * expect then we have missed one or more files. Since
643 * we do not request files that were not on the tape,
644 * the lost files must have been due to a tape read error,
645 * or a file that was removed while the dump was in progress.
647 while (first
< curfile
.ino
) {
648 ep
= lookupino(first
);
650 panic("%d: bad first\n", first
);
651 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
652 ep
->e_flags
&= ~(NEW
|EXTRACT
);
653 first
= lowerbnd(first
);
656 * If we find files on the tape that have no corresponding
657 * directory entries, then we must have found a file that
658 * was created while the dump was in progress. Since we have
659 * no name for it, we discard it knowing that it will be
660 * on the next incremental tape.
662 if (first
!= curfile
.ino
) {
663 fprintf(stderr
, "expected next file %d, got %d\n",
668 ep
= lookupino(curfile
.ino
);
670 panic("unknown file on tape\n");
671 compare_entry(ep
, 1);
672 for (ep
= ep
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
673 compare_entry(ep
, 0);
677 * We checkpoint the restore after every tape reel, so
678 * as to simplify the amount of work re quired by the
682 if (curvol
!= volno
) {
690 * This is the routine used to extract files for the 'r' command.
691 * Extract new leaves.
694 createleaves(symtabfile
)
697 register struct entry
*ep
;
701 if (command
== 'R') {
702 vprintf(stdout
, "Continue extraction of new leaves\n");
704 vprintf(stdout
, "Extract new leaves.\n");
705 dumpsymtable(symtabfile
, volno
);
707 first
= lowerbnd(ROOTINO
);
709 while (curfile
.ino
< maxino
) {
710 first
= lowerbnd(first
);
712 * If the next available file is not the one which we
713 * expect then we have missed one or more files. Since
714 * we do not request files that were not on the tape,
715 * the lost files must have been due to a tape read error,
716 * or a file that was removed while the dump was in progress.
718 while (first
< curfile
.ino
) {
719 ep
= lookupino(first
);
721 panic("%d: bad first\n", first
);
722 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
723 ep
->e_flags
&= ~(NEW
|EXTRACT
);
724 first
= lowerbnd(first
);
727 * If we find files on the tape that have no corresponding
728 * directory entries, then we must have found a file that
729 * was created while the dump was in progress. Since we have
730 * no name for it, we discard it knowing that it will be
731 * on the next incremental tape.
733 if (first
!= curfile
.ino
) {
734 fprintf(stderr
, "expected next file %d, got %d\n",
739 ep
= lookupino(curfile
.ino
);
741 panic("unknown file on tape\n");
742 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
743 badentry(ep
, "unexpected file on tape");
745 * If the file is to be extracted, then the old file must
746 * be removed since its type may change from one leaf type
747 * to another (eg "file" to "character special").
749 if ((ep
->e_flags
& EXTRACT
) != 0) {
751 ep
->e_flags
&= ~REMOVED
;
753 (void) extractfile(myname(ep
));
754 ep
->e_flags
&= ~(NEW
|EXTRACT
);
756 * We checkpoint the restore after every tape reel, so
757 * as to simplify the amount of work re quired by the
761 if (curvol
!= volno
) {
762 dumpsymtable(symtabfile
, volno
);
770 * This is the routine used to extract files for the 'x' and 'i' commands.
771 * Efficiently extract a subset of the files on a tape.
776 register ino_t first
, next
, last
;
777 register struct entry
*ep
;
780 vprintf(stdout
, "Extract requested files\n");
781 curfile
.action
= SKIP
;
785 first
= lowerbnd(ROOTINO
);
786 last
= upperbnd(maxino
- 1);
788 first
= lowerbnd(first
);
789 last
= upperbnd(last
);
791 * Check to see if any files remain to be extracted
796 * Reject any volumes with inodes greater
797 * than the last one needed
799 while (curfile
.ino
> last
) {
800 curfile
.action
= SKIP
;
806 * Decide on the next inode needed.
807 * Skip across the inodes until it is found
808 * or an out of order volume change is encountered
810 next
= lowerbnd(curfile
.ino
);
813 while (next
> curfile
.ino
&& volno
== curvol
)
817 } while (volno
== curvol
+ 1);
819 * If volume change out of order occurred the
820 * current state must be recalculated
825 * If the current inode is greater than the one we were
826 * looking for then we missed the one we were looking for.
827 * Since we only attempt to extract files listed in the
828 * dump map, the lost files must have been due to a tape
829 * read error, or a file that was removed while the dump
830 * was in progress. Thus we report all requested files
831 * between the one we were looking for, and the one we
832 * found as missing, and delete their request flags.
834 while (next
< curfile
.ino
) {
835 ep
= lookupino(next
);
837 panic("corrupted symbol table\n");
838 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
840 next
= lowerbnd(next
);
843 * The current inode is the one that we are looking for,
844 * so extract it per its requested name.
846 if (next
== curfile
.ino
&& next
<= last
) {
847 ep
= lookupino(next
);
849 panic("corrupted symbol table\n");
850 (void) extractfile(myname(ep
));
864 register struct entry
*np
, *ep
;
868 if (ep
= lookupino(WINO
)) {
869 vprintf(stdout
, "Add whiteouts\n");
870 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
871 if ((ep
->e_flags
& NEW
) == 0)
874 (void)fprintf(stderr
, "BUG! Should call addwhiteout\n");
876 (void) addwhiteout(myname(ep
));
881 vprintf(stdout
, "Add links\n");
882 for (i
= ROOTINO
; i
< maxino
; i
++) {
886 for (np
= ep
->e_links
; np
!= NULL
; np
= np
->e_links
) {
887 if ((np
->e_flags
& NEW
) == 0)
889 (void) strcpy(name
, myname(ep
));
890 if (ep
->e_type
== NODE
) {
891 (void) linkit(name
, myname(np
), SYMLINK
);
893 (void) linkit(name
, myname(np
), HARDLINK
);
901 * Check the symbol table.
902 * We do this to insure that all the requested work was done, and
903 * that no temporary names remain.
908 register struct entry
*ep
;
911 vprintf(stdout
, "Check the symbol table.\n");
912 for (i
= WINO
; i
< maxino
; i
++) {
913 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
914 ep
->e_flags
&= ~KEEP
;
915 if (ep
->e_type
== NODE
)
916 ep
->e_flags
&= ~(NEW
|EXISTED
);
917 if (ep
->e_flags
/* != NULL */)
918 badentry(ep
, "incomplete operations");
924 * Compare with the directory structure on the tape
925 * A paranoid check that things are as they should be.
928 verifyfile(name
, ino
, type
)
933 struct entry
*np
, *ep
;
936 ep
= lookupname(name
);
938 fprintf(stderr
, "Warning: missing name %s\n", name
);
944 for ( ; np
!= NULL
; np
= np
->e_links
)
948 panic("missing inumber %d\n", ino
);
949 if (ep
->e_type
== LEAF
&& type
!= LEAF
)
950 badentry(ep
, "type should be LEAF");