]>
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-2000
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 const char rcsid
[] =
43 "$Id: restore.c,v 1.8 2000/05/28 16:52:21 stelian Exp $";
46 #include <sys/types.h>
49 #include <sys/param.h>
51 #include <linux/ext2_fs.h>
52 #include <bsdcompat.h>
54 #include <ufs/ufs/dinode.h>
55 #endif /* __linux__ */
61 #include <ext2fs/ext2fs.h>
67 static char *keyval
__P((int));
70 * This implements the 't' option.
71 * List entries on the tape.
74 listfile(char *name
, ino_t ino
, int type
)
76 long descend
= hflag
? GOOD
: FAIL
;
78 if (TSTINO(ino
, dumpmap
) == 0)
80 Vprintf(stdout
, "%s", type
== LEAF
? "leaf" : "dir ");
81 fprintf(stdout
, "%10lu\t%s\n", (unsigned long)ino
, name
);
86 * This implements the 'x' option.
87 * Request that new entries be extracted.
90 addfile(char *name
, ino_t ino
, int type
)
92 register struct entry
*ep
;
93 long descend
= hflag
? GOOD
: FAIL
;
96 if (TSTINO(ino
, dumpmap
) == 0) {
97 Dprintf(stdout
, "%s: not on the tape\n", name
);
100 if (ino
== WINO
&& command
== 'i' && !vflag
)
103 (void) snprintf(buf
, sizeof(buf
), "./%lu", (unsigned long)ino
);
106 (void) genliteraldir(name
, ino
);
112 if (strcmp(name
, myname(ep
)) == 0) {
118 ep
= addentry(name
, ino
, type
);
126 * This is used by the 'i' option to undo previous requests made by addfile.
127 * Delete entries from the request queue.
131 deletefile(char *name
, ino_t ino
, int type
)
133 long descend
= hflag
? GOOD
: FAIL
;
136 if (TSTINO(ino
, dumpmap
) == 0)
138 ep
= lookupname(name
);
141 ep
->e_flags
|= REMOVED
;
142 if (ep
->e_type
!= NODE
)
149 * The following four routines implement the incremental
150 * restore algorithm. The first removes old entries, the second
151 * does renames and calculates the extraction list, the third
152 * cleans up link names missed by the first two, and the final
153 * one deletes old directories.
155 * Directories cannot be immediately deleted, as they may have
156 * other files in them which need to be moved out first. As
157 * directories to be deleted are found, they are put on the
158 * following deletion list. After all deletions and renames
159 * are done, this list is actually deleted.
161 static struct entry
*removelist
;
164 * Remove invalid whiteouts from the old tree.
165 * Remove unneeded leaves from the old tree.
166 * Remove directories from the lookup chains.
169 removeoldleaves(void)
171 register struct entry
*ep
, *nextep
;
172 register ino_t i
, mydirino
;
174 Vprintf(stdout
, "Mark entries to be removed.\n");
175 if ((ep
= lookupino(WINO
))) {
176 Vprintf(stdout
, "Delete whiteouts\n");
177 for ( ; ep
!= NULL
; ep
= nextep
) {
178 nextep
= ep
->e_links
;
179 mydirino
= ep
->e_parent
->e_ino
;
181 * We remove all whiteouts that are in directories
182 * that have been removed or that have been dumped.
184 if (TSTINO(mydirino
, usedinomap
) &&
185 !TSTINO(mydirino
, dumpmap
))
188 (void)fprintf(stderr
, "BUG! Should call delwhiteout\n");
195 for (i
= ROOTINO
+ 1; i
< maxino
; i
++) {
199 if (TSTINO(i
, usedinomap
))
201 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
202 Dprintf(stdout
, "%s: REMOVE\n", myname(ep
));
203 if (ep
->e_type
== LEAF
) {
208 deleteino(ep
->e_ino
);
209 ep
->e_next
= removelist
;
217 * For each directory entry on the incremental tape, determine which
218 * category it falls into as follows:
219 * KEEP - entries that are to be left alone.
220 * NEW - new entries to be added.
221 * EXTRACT - files that must be updated with new contents.
222 * LINK - new links to be added.
223 * Renames are done at the same time.
226 nodeupdates(char *name
, ino_t ino
, int type
)
228 register struct entry
*ep
, *np
, *ip
;
233 # define ONTAPE 0x1 /* inode is on the tape */
234 # define INOFND 0x2 /* inode already exists */
235 # define NAMEFND 0x4 /* name already exists */
236 # define MODECHG 0x8 /* mode of inode changed */
239 * This routine is called once for each element in the
240 * directory hierarchy, with a full path name.
241 * The "type" value is incorrectly specified as LEAF for
242 * directories that are not on the dump tape.
244 * Check to see if the file is on the tape.
246 if (TSTINO(ino
, dumpmap
))
249 * Check to see if the name exists, and if the name is a link.
251 np
= lookupname(name
);
254 ip
= lookupino(np
->e_ino
);
256 panic("corrupted symbol table\n");
261 * Check to see if the inode exists, and if one of its links
262 * corresponds to the name (if one was found).
267 for (ep
= ip
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
275 * If both a name and an inode are found, but they do not
276 * correspond to the same file, then both the inode that has
277 * been found and the inode corresponding to the name that
278 * has been found need to be renamed. The current pathname
279 * is the new name for the inode that has been found. Since
280 * all files to be deleted have already been removed, the
281 * named file is either a now unneeded link, or it must live
282 * under a new name in this dump level. If it is a link, it
283 * can be removed. If it is not a link, it is given a
284 * temporary name in anticipation that it will be renamed
285 * when it is later found by inode number.
287 if (((key
& (INOFND
|NAMEFND
)) == (INOFND
|NAMEFND
)) && ip
!= np
) {
288 if (lookuptype
== LINK
) {
292 Dprintf(stdout
, "name/inode conflict, mktempname %s\n",
299 if ((key
& ONTAPE
) &&
300 (((key
& INOFND
) && ip
->e_type
!= type
) ||
301 ((key
& NAMEFND
) && np
->e_type
!= type
)))
305 * Decide on the disposition of the file based on its flags.
306 * Note that we have already handled the case in which
307 * a name and inode are found that correspond to different files.
308 * Thus if both NAMEFND and INOFND are set then ip == np.
313 * A previously existing file has been found.
314 * Mark it as KEEP so that other links to the inode can be
315 * detected, and so that it will not be reclaimed by the search
316 * for unreferenced names.
320 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
325 * A file on the tape has a name which is the same as a name
326 * corresponding to a different file in the previous dump.
327 * Since all files to be deleted have already been removed,
328 * this file is either a now unneeded link, or it must live
329 * under a new name in this dump level. If it is a link, it
330 * can simply be removed. If it is not a link, it is given a
331 * temporary name in anticipation that it will be renamed
332 * when it is later found by inode number (see INOFND case
333 * below). The entry is then treated as a new file.
336 case ONTAPE
|NAMEFND
|MODECHG
:
337 if (lookuptype
== LINK
) {
346 * A previously non-existent file.
347 * Add it to the file system, and request its extraction.
348 * If it is a directory, create it immediately.
349 * (Since the name is unused there can be no conflict)
352 ep
= addentry(name
, ino
, type
);
355 ep
->e_flags
|= NEW
|KEEP
;
356 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
361 * A file with the same inode number, but a different
362 * name has been found. If the other name has not already
363 * been found (indicated by the KEEP flag, see above) then
364 * this must be a new name for the file, and it is renamed.
365 * If the other name has been found then this must be a
366 * link to the file. Hard links to directories are not
367 * permitted, and are either deleted or converted to
368 * symbolic links. Finally, if the file is on the tape,
369 * a request is made to extract it.
372 if (type
== LEAF
&& (ip
->e_flags
& KEEP
) == 0)
373 ip
->e_flags
|= EXTRACT
;
376 if ((ip
->e_flags
& KEEP
) == 0) {
377 renameit(myname(ip
), name
);
380 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
384 if (ip
->e_type
== NODE
) {
387 "deleted hard link %s to directory %s\n",
391 ep
= addentry(name
, ino
, type
|LINK
);
393 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
398 * A previously known file which is to be updated. If it is a link,
399 * then all names referring to the previous file must be removed
400 * so that the subset of them that remain can be recreated.
402 case ONTAPE
|INOFND
|NAMEFND
:
403 if (lookuptype
== LINK
) {
406 ep
= addentry(name
, ino
, type
|LINK
);
409 ep
->e_flags
|= NEW
|KEEP
;
410 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
414 if (type
== LEAF
&& lookuptype
!= LINK
)
415 np
->e_flags
|= EXTRACT
;
417 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
422 * An inode is being reused in a completely different way.
423 * Normally an extract can simply do an "unlink" followed
424 * by a "creat". Here we must do effectively the same
425 * thing. The complications arise because we cannot really
426 * delete a directory since it may still contain files
427 * that we need to rename, so we delete it from the symbol
428 * table, and put it on the list to be deleted eventually.
429 * Conversely if a directory is to be created, it must be
430 * done immediately, rather than waiting until the
433 case ONTAPE
|INOFND
|MODECHG
:
434 case ONTAPE
|INOFND
|NAMEFND
|MODECHG
:
435 if (ip
->e_flags
& KEEP
) {
436 badentry(ip
, "cannot KEEP and change modes");
439 if (ip
->e_type
== LEAF
) {
440 /* changing from leaf to node */
441 for ( ; ip
!= NULL
; ip
= ip
->e_links
) {
442 if (ip
->e_type
!= LEAF
)
443 badentry(ip
, "NODE and LEAF links to same inode");
447 ip
= addentry(name
, ino
, type
);
450 /* changing from node to leaf */
451 if ((ip
->e_flags
& TMPNAME
) == 0)
453 deleteino(ip
->e_ino
);
454 ip
->e_next
= removelist
;
456 ip
= addentry(name
, ino
, type
);
458 ip
->e_flags
|= NEW
|KEEP
;
459 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
464 * A hard link to a directory that has been removed.
468 Dprintf(stdout
, "[%s] %s: Extraneous name\n", keyval(key
),
474 * If we find a directory entry for a file that is not on
475 * the tape, then we must have found a file that was created
476 * while the dump was in progress. Since we have no contents
477 * for it, we discard the name knowing that it will be on the
478 * next incremental tape.
481 if (compare_ignore_not_found
) break;
482 fprintf(stderr
, "%s: (inode %lu) not found on tape\n",
483 name
, (unsigned long)ino
);
487 * If any of these arise, something is grievously wrong with
488 * the current state of the symbol table.
490 case INOFND
|NAMEFND
|MODECHG
:
491 case NAMEFND
|MODECHG
:
493 fprintf(stderr
, "[%s] %s: inconsistent state\n", keyval(key
),
498 * These states "cannot" arise for any state of the symbol table.
503 panic("[%s] %s: impossible state\n", keyval(key
), name
);
510 * Calculate the active flags in a key.
515 static char keybuf
[32];
517 (void) strcpy(keybuf
, "|NIL");
520 (void) strcat(keybuf
, "|ONTAPE");
522 (void) strcat(keybuf
, "|INOFND");
524 (void) strcat(keybuf
, "|NAMEFND");
526 (void) strcat(keybuf
, "|MODECHG");
531 * Find unreferenced link names.
536 register struct entry
*ep
, *np
;
539 Vprintf(stdout
, "Find unreferenced names.\n");
540 for (i
= ROOTINO
; i
< maxino
; i
++) {
542 if (ep
== NULL
|| ep
->e_type
== LEAF
|| TSTINO(i
, dumpmap
) == 0)
544 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
545 if (np
->e_flags
== 0) {
547 "%s: remove unreferenced name\n",
555 * Any leaves remaining in removed directories is unreferenced.
557 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
) {
558 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
559 if (np
->e_type
== LEAF
) {
560 if (np
->e_flags
!= 0)
561 badentry(np
, "unreferenced with flags");
563 "%s: remove unreferenced name\n",
573 * Remove old nodes (directories).
574 * Note that this routine runs in O(N*D) where:
575 * N is the number of directory entries to be removed.
576 * D is the maximum depth of the tree.
577 * If N == D this can be quite slow. If the list were
578 * topologically sorted, the deletion could be done in
584 register struct entry
*ep
, **prev
;
587 Vprintf(stdout
, "Remove old nodes (directories).\n");
591 for (ep
= removelist
; ep
!= NULL
; ep
= *prev
) {
592 if (ep
->e_entries
!= NULL
) {
602 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
)
603 badentry(ep
, "cannot remove, non-empty");
606 /* Compare the file specified in `ep' (which is on tape) to the */
607 /* current copy of this file on disk. If do_compare is 0, then just */
608 /* make our caller think we did it--this is used to handle hard links */
609 /* to files and devices. */
611 compare_entry(struct entry
*ep
, int do_compare
)
613 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0) {
614 badentry(ep
, "unexpected file on tape");
617 if (do_compare
) (void) comparefile(myname(ep
));
618 ep
->e_flags
&= ~(NEW
|EXTRACT
);
622 * This is the routine used to compare files for the 'C' command.
627 register struct entry
*ep
;
631 first
= lowerbnd(ROOTINO
);
633 while (curfile
.ino
< maxino
) {
634 first
= lowerbnd(first
);
636 * If the next available file is not the one which we
637 * expect then we have missed one or more files. Since
638 * we do not request files that were not on the tape,
639 * the lost files must have been due to a tape read error,
640 * or a file that was removed while the dump was in progress.
642 while (first
< curfile
.ino
) {
643 ep
= lookupino(first
);
645 panic("%d: bad first\n", first
);
646 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
648 ep
->e_flags
&= ~(NEW
|EXTRACT
);
649 first
= lowerbnd(first
);
652 * If we find files on the tape that have no corresponding
653 * directory entries, then we must have found a file that
654 * was created while the dump was in progress. Since we have
655 * no name for it, we discard it knowing that it will be
656 * on the next incremental tape.
658 if (first
!= curfile
.ino
) {
659 fprintf(stderr
, "expected next file %ld, got %lu\n",
660 (long)first
, (unsigned long)curfile
.ino
);
665 ep
= lookupino(curfile
.ino
);
667 panic("unknown file on tape\n");
670 compare_entry(ep
, 1);
671 for (ep
= ep
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
672 compare_entry(ep
, 0);
676 * We checkpoint the restore after every tape reel, so
677 * as to simplify the amount of work re quired by the
681 if (curvol
!= volno
) {
689 * This is the routine used to extract files for the 'r' command.
690 * Extract new leaves.
693 createleaves(char *symtabfile
)
695 register struct entry
*ep
;
699 if (command
== 'R') {
700 Vprintf(stdout
, "Continue extraction of new leaves\n");
702 Vprintf(stdout
, "Extract new leaves.\n");
703 dumpsymtable(symtabfile
, volno
);
705 first
= lowerbnd(ROOTINO
);
707 while (curfile
.ino
< maxino
) {
708 first
= lowerbnd(first
);
710 * If the next available file is not the one which we
711 * expect then we have missed one or more files. Since
712 * we do not request files that were not on the tape,
713 * the lost files must have been due to a tape read error,
714 * or a file that was removed while the dump was in progress.
716 while (first
< curfile
.ino
) {
717 ep
= lookupino(first
);
719 panic("%d: bad first\n", first
);
720 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
721 ep
->e_flags
&= ~(NEW
|EXTRACT
);
722 first
= lowerbnd(first
);
725 * If we find files on the tape that have no corresponding
726 * directory entries, then we must have found a file that
727 * was created while the dump was in progress. Since we have
728 * no name for it, we discard it knowing that it will be
729 * on the next incremental tape.
731 if (first
!= curfile
.ino
) {
732 fprintf(stderr
, "expected next file %ld, got %lu\n",
733 (long)first
, (unsigned long)curfile
.ino
);
737 ep
= lookupino(curfile
.ino
);
739 panic("unknown file on tape\n");
740 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
741 badentry(ep
, "unexpected file on tape");
743 * If the file is to be extracted, then the old file must
744 * be removed since its type may change from one leaf type
745 * to another (e.g. "file" to "character special").
747 if ((ep
->e_flags
& EXTRACT
) != 0) {
749 ep
->e_flags
&= ~REMOVED
;
751 (void) extractfile(myname(ep
));
752 ep
->e_flags
&= ~(NEW
|EXTRACT
);
754 * We checkpoint the restore after every tape reel, so
755 * as to simplify the amount of work required by the
759 if (curvol
!= volno
) {
760 dumpsymtable(symtabfile
, volno
);
768 * This is the routine used to extract files for the 'x' and 'i' commands.
769 * Efficiently extract a subset of the files on a tape.
774 register ino_t first
, next
, last
;
775 register struct entry
*ep
;
778 Vprintf(stdout
, "Extract requested files\n");
779 curfile
.action
= SKIP
;
783 first
= lowerbnd(ROOTINO
);
784 last
= upperbnd(maxino
- 1);
786 first
= lowerbnd(first
);
787 last
= upperbnd(last
);
789 * Check to see if any files remain to be extracted
794 * Reject any volumes with inodes greater
795 * than the last one needed
797 while (curfile
.ino
> last
) {
798 curfile
.action
= SKIP
;
804 * Decide on the next inode needed.
805 * Skip across the inodes until it is found
806 * or an out of order volume change is encountered
808 next
= lowerbnd(curfile
.ino
);
811 while (next
> curfile
.ino
&& volno
== curvol
)
815 } while (volno
== curvol
+ 1);
817 * If volume change out of order occurred the
818 * current state must be recalculated
823 * If the current inode is greater than the one we were
824 * looking for then we missed the one we were looking for.
825 * Since we only attempt to extract files listed in the
826 * dump map, the lost files must have been due to a tape
827 * read error, or a file that was removed while the dump
828 * was in progress. Thus we report all requested files
829 * between the one we were looking for, and the one we
830 * found as missing, and delete their request flags.
832 while (next
< curfile
.ino
) {
833 ep
= lookupino(next
);
835 panic("corrupted symbol table\n");
836 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
838 next
= lowerbnd(next
);
841 * The current inode is the one that we are looking for,
842 * so extract it per its requested name.
844 if (next
== curfile
.ino
&& next
<= last
) {
845 ep
= lookupino(next
);
847 panic("corrupted symbol table\n");
848 (void) extractfile(myname(ep
));
862 register struct entry
*np
, *ep
;
866 if ((ep
= lookupino(WINO
))) {
867 Vprintf(stdout
, "Add whiteouts\n");
868 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
869 if ((ep
->e_flags
& NEW
) == 0)
872 (void)fprintf(stderr
, "BUG! Should call addwhiteout\n");
874 (void) addwhiteout(myname(ep
));
879 Vprintf(stdout
, "Add links\n");
880 for (i
= ROOTINO
; i
< maxino
; i
++) {
884 for (np
= ep
->e_links
; np
!= NULL
; np
= np
->e_links
) {
885 if ((np
->e_flags
& NEW
) == 0)
887 (void) strcpy(name
, myname(ep
));
888 if (ep
->e_type
== NODE
) {
889 (void) linkit(name
, myname(np
), SYMLINK
);
891 (void) linkit(name
, myname(np
), HARDLINK
);
899 * Check the symbol table.
900 * We do this to insure that all the requested work was done, and
901 * that no temporary names remain.
906 register struct entry
*ep
;
909 Vprintf(stdout
, "Check the symbol table.\n");
910 for (i
= WINO
; i
< maxino
; i
++) {
911 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
912 ep
->e_flags
&= ~KEEP
;
913 if (ep
->e_type
== NODE
)
914 ep
->e_flags
&= ~(NEW
|EXISTED
);
915 if (ep
->e_flags
/* != NULL */)
916 badentry(ep
, "incomplete operations");
922 * Compare with the directory structure on the tape
923 * A paranoid check that things are as they should be.
926 verifyfile(char *name
, ino_t ino
, int type
)
928 struct entry
*np
, *ep
;
931 ep
= lookupname(name
);
933 fprintf(stderr
, "Warning: missing name %s\n", name
);
939 for ( ; np
!= NULL
; np
= np
->e_links
)
943 panic("missing inumber %d\n", ino
);
944 if (ep
->e_type
== LEAF
&& type
!= LEAF
)
945 badentry(ep
, "type should be LEAF");