]>
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
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.6 1999/10/13 09:57: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");
615 if (do_compare
) (void) comparefile(myname(ep
));
616 ep
->e_flags
&= ~(NEW
|EXTRACT
);
620 * This is the routine used to compare files for the 'C' command.
625 register struct entry
*ep
;
629 first
= lowerbnd(ROOTINO
);
631 while (curfile
.ino
< maxino
) {
632 first
= lowerbnd(first
);
634 * If the next available file is not the one which we
635 * expect then we have missed one or more files. Since
636 * we do not request files that were not on the tape,
637 * the lost files must have been due to a tape read error,
638 * or a file that was removed while the dump was in progress.
640 while (first
< curfile
.ino
) {
641 ep
= lookupino(first
);
643 panic("%d: bad first\n", first
);
644 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
645 ep
->e_flags
&= ~(NEW
|EXTRACT
);
646 first
= lowerbnd(first
);
649 * If we find files on the tape that have no corresponding
650 * directory entries, then we must have found a file that
651 * was created while the dump was in progress. Since we have
652 * no name for it, we discard it knowing that it will be
653 * on the next incremental tape.
655 if (first
!= curfile
.ino
) {
656 fprintf(stderr
, "expected next file %ld, got %lu\n",
657 (long)first
, (unsigned long)curfile
.ino
);
661 ep
= lookupino(curfile
.ino
);
663 panic("unknown file on tape\n");
664 compare_entry(ep
, 1);
665 for (ep
= ep
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
666 compare_entry(ep
, 0);
670 * We checkpoint the restore after every tape reel, so
671 * as to simplify the amount of work re quired by the
675 if (curvol
!= volno
) {
683 * This is the routine used to extract files for the 'r' command.
684 * Extract new leaves.
687 createleaves(char *symtabfile
)
689 register struct entry
*ep
;
693 if (command
== 'R') {
694 Vprintf(stdout
, "Continue extraction of new leaves\n");
696 Vprintf(stdout
, "Extract new leaves.\n");
697 dumpsymtable(symtabfile
, volno
);
699 first
= lowerbnd(ROOTINO
);
701 while (curfile
.ino
< maxino
) {
702 first
= lowerbnd(first
);
704 * If the next available file is not the one which we
705 * expect then we have missed one or more files. Since
706 * we do not request files that were not on the tape,
707 * the lost files must have been due to a tape read error,
708 * or a file that was removed while the dump was in progress.
710 while (first
< curfile
.ino
) {
711 ep
= lookupino(first
);
713 panic("%d: bad first\n", first
);
714 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
715 ep
->e_flags
&= ~(NEW
|EXTRACT
);
716 first
= lowerbnd(first
);
719 * If we find files on the tape that have no corresponding
720 * directory entries, then we must have found a file that
721 * was created while the dump was in progress. Since we have
722 * no name for it, we discard it knowing that it will be
723 * on the next incremental tape.
725 if (first
!= curfile
.ino
) {
726 fprintf(stderr
, "expected next file %ld, got %lu\n",
727 (long)first
, (unsigned long)curfile
.ino
);
731 ep
= lookupino(curfile
.ino
);
733 panic("unknown file on tape\n");
734 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
735 badentry(ep
, "unexpected file on tape");
737 * If the file is to be extracted, then the old file must
738 * be removed since its type may change from one leaf type
739 * to another (e.g. "file" to "character special").
741 if ((ep
->e_flags
& EXTRACT
) != 0) {
743 ep
->e_flags
&= ~REMOVED
;
745 (void) extractfile(myname(ep
));
746 ep
->e_flags
&= ~(NEW
|EXTRACT
);
748 * We checkpoint the restore after every tape reel, so
749 * as to simplify the amount of work required by the
753 if (curvol
!= volno
) {
754 dumpsymtable(symtabfile
, volno
);
762 * This is the routine used to extract files for the 'x' and 'i' commands.
763 * Efficiently extract a subset of the files on a tape.
768 register ino_t first
, next
, last
;
769 register struct entry
*ep
;
772 Vprintf(stdout
, "Extract requested files\n");
773 curfile
.action
= SKIP
;
777 first
= lowerbnd(ROOTINO
);
778 last
= upperbnd(maxino
- 1);
780 first
= lowerbnd(first
);
781 last
= upperbnd(last
);
783 * Check to see if any files remain to be extracted
788 * Reject any volumes with inodes greater
789 * than the last one needed
791 while (curfile
.ino
> last
) {
792 curfile
.action
= SKIP
;
798 * Decide on the next inode needed.
799 * Skip across the inodes until it is found
800 * or an out of order volume change is encountered
802 next
= lowerbnd(curfile
.ino
);
805 while (next
> curfile
.ino
&& volno
== curvol
)
809 } while (volno
== curvol
+ 1);
811 * If volume change out of order occurred the
812 * current state must be recalculated
817 * If the current inode is greater than the one we were
818 * looking for then we missed the one we were looking for.
819 * Since we only attempt to extract files listed in the
820 * dump map, the lost files must have been due to a tape
821 * read error, or a file that was removed while the dump
822 * was in progress. Thus we report all requested files
823 * between the one we were looking for, and the one we
824 * found as missing, and delete their request flags.
826 while (next
< curfile
.ino
) {
827 ep
= lookupino(next
);
829 panic("corrupted symbol table\n");
830 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
832 next
= lowerbnd(next
);
835 * The current inode is the one that we are looking for,
836 * so extract it per its requested name.
838 if (next
== curfile
.ino
&& next
<= last
) {
839 ep
= lookupino(next
);
841 panic("corrupted symbol table\n");
842 (void) extractfile(myname(ep
));
856 register struct entry
*np
, *ep
;
860 if ((ep
= lookupino(WINO
))) {
861 Vprintf(stdout
, "Add whiteouts\n");
862 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
863 if ((ep
->e_flags
& NEW
) == 0)
866 (void)fprintf(stderr
, "BUG! Should call addwhiteout\n");
868 (void) addwhiteout(myname(ep
));
873 Vprintf(stdout
, "Add links\n");
874 for (i
= ROOTINO
; i
< maxino
; i
++) {
878 for (np
= ep
->e_links
; np
!= NULL
; np
= np
->e_links
) {
879 if ((np
->e_flags
& NEW
) == 0)
881 (void) strcpy(name
, myname(ep
));
882 if (ep
->e_type
== NODE
) {
883 (void) linkit(name
, myname(np
), SYMLINK
);
885 (void) linkit(name
, myname(np
), HARDLINK
);
893 * Check the symbol table.
894 * We do this to insure that all the requested work was done, and
895 * that no temporary names remain.
900 register struct entry
*ep
;
903 Vprintf(stdout
, "Check the symbol table.\n");
904 for (i
= WINO
; i
< maxino
; i
++) {
905 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
906 ep
->e_flags
&= ~KEEP
;
907 if (ep
->e_type
== NODE
)
908 ep
->e_flags
&= ~(NEW
|EXISTED
);
909 if (ep
->e_flags
/* != NULL */)
910 badentry(ep
, "incomplete operations");
916 * Compare with the directory structure on the tape
917 * A paranoid check that things are as they should be.
920 verifyfile(char *name
, ino_t ino
, int type
)
922 struct entry
*np
, *ep
;
925 ep
= lookupname(name
);
927 fprintf(stderr
, "Warning: missing name %s\n", name
);
933 for ( ; np
!= NULL
; np
= np
->e_links
)
937 panic("missing inumber %d\n", ino
);
938 if (ep
->e_type
== LEAF
&& type
!= LEAF
)
939 badentry(ep
, "type should be LEAF");