]>
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
44 static char sccsid
[] = "@(#)restore.c 8.3 (Berkeley) 9/13/94";
46 static const char rcsid
[] =
47 "$Id: restore.c,v 1.4 1999/10/11 13:08:09 stelian Exp $";
50 #include <sys/types.h>
53 #include <sys/param.h>
55 #include <linux/ext2_fs.h>
56 #include <bsdcompat.h>
58 #include <ufs/ufs/dinode.h>
59 #endif /* __linux__ */
65 #include <ext2fs/ext2fs.h>
71 static char *keyval
__P((int));
74 * This implements the 't' option.
75 * List entries on the tape.
78 listfile(char *name
, ino_t ino
, int type
)
80 long descend
= hflag
? GOOD
: FAIL
;
82 if (TSTINO(ino
, dumpmap
) == 0)
84 Vprintf(stdout
, "%s", type
== LEAF
? "leaf" : "dir ");
85 fprintf(stdout
, "%10lu\t%s\n", (unsigned long)ino
, name
);
90 * This implements the 'x' option.
91 * Request that new entries be extracted.
94 addfile(char *name
, ino_t ino
, int type
)
96 register struct entry
*ep
;
97 long descend
= hflag
? GOOD
: FAIL
;
100 if (TSTINO(ino
, dumpmap
) == 0) {
101 Dprintf(stdout
, "%s: not on the tape\n", name
);
104 if (ino
== WINO
&& command
== 'i' && !vflag
)
107 (void) snprintf(buf
, sizeof(buf
), "./%lu", (unsigned long)ino
);
110 (void) genliteraldir(name
, ino
);
116 if (strcmp(name
, myname(ep
)) == 0) {
122 ep
= addentry(name
, ino
, type
);
130 * This is used by the 'i' option to undo previous requests made by addfile.
131 * Delete entries from the request queue.
135 deletefile(char *name
, ino_t ino
, int type
)
137 long descend
= hflag
? GOOD
: FAIL
;
140 if (TSTINO(ino
, dumpmap
) == 0)
142 ep
= lookupname(name
);
145 ep
->e_flags
|= REMOVED
;
146 if (ep
->e_type
!= NODE
)
153 * The following four routines implement the incremental
154 * restore algorithm. The first removes old entries, the second
155 * does renames and calculates the extraction list, the third
156 * cleans up link names missed by the first two, and the final
157 * one deletes old directories.
159 * Directories cannot be immediately deleted, as they may have
160 * other files in them which need to be moved out first. As
161 * directories to be deleted are found, they are put on the
162 * following deletion list. After all deletions and renames
163 * are done, this list is actually deleted.
165 static struct entry
*removelist
;
168 * Remove invalid whiteouts from the old tree.
169 * Remove unneeded leaves from the old tree.
170 * Remove directories from the lookup chains.
173 removeoldleaves(void)
175 register struct entry
*ep
, *nextep
;
176 register ino_t i
, mydirino
;
178 Vprintf(stdout
, "Mark entries to be removed.\n");
179 if ((ep
= lookupino(WINO
))) {
180 Vprintf(stdout
, "Delete whiteouts\n");
181 for ( ; ep
!= NULL
; ep
= nextep
) {
182 nextep
= ep
->e_links
;
183 mydirino
= ep
->e_parent
->e_ino
;
185 * We remove all whiteouts that are in directories
186 * that have been removed or that have been dumped.
188 if (TSTINO(mydirino
, usedinomap
) &&
189 !TSTINO(mydirino
, dumpmap
))
192 (void)fprintf(stderr
, "BUG! Should call delwhiteout\n");
199 for (i
= ROOTINO
+ 1; i
< maxino
; i
++) {
203 if (TSTINO(i
, usedinomap
))
205 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
206 Dprintf(stdout
, "%s: REMOVE\n", myname(ep
));
207 if (ep
->e_type
== LEAF
) {
212 deleteino(ep
->e_ino
);
213 ep
->e_next
= removelist
;
221 * For each directory entry on the incremental tape, determine which
222 * category it falls into as follows:
223 * KEEP - entries that are to be left alone.
224 * NEW - new entries to be added.
225 * EXTRACT - files that must be updated with new contents.
226 * LINK - new links to be added.
227 * Renames are done at the same time.
230 nodeupdates(char *name
, ino_t ino
, int type
)
232 register struct entry
*ep
, *np
, *ip
;
237 # define ONTAPE 0x1 /* inode is on the tape */
238 # define INOFND 0x2 /* inode already exists */
239 # define NAMEFND 0x4 /* name already exists */
240 # define MODECHG 0x8 /* mode of inode changed */
243 * This routine is called once for each element in the
244 * directory hierarchy, with a full path name.
245 * The "type" value is incorrectly specified as LEAF for
246 * directories that are not on the dump tape.
248 * Check to see if the file is on the tape.
250 if (TSTINO(ino
, dumpmap
))
253 * Check to see if the name exists, and if the name is a link.
255 np
= lookupname(name
);
258 ip
= lookupino(np
->e_ino
);
260 panic("corrupted symbol table\n");
265 * Check to see if the inode exists, and if one of its links
266 * corresponds to the name (if one was found).
271 for (ep
= ip
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
279 * If both a name and an inode are found, but they do not
280 * correspond to the same file, then both the inode that has
281 * been found and the inode corresponding to the name that
282 * has been found need to be renamed. The current pathname
283 * is the new name for the inode that has been found. Since
284 * all files to be deleted have already been removed, the
285 * named file is either a now unneeded link, or it must live
286 * under a new name in this dump level. If it is a link, it
287 * can be removed. If it is not a link, it is given a
288 * temporary name in anticipation that it will be renamed
289 * when it is later found by inode number.
291 if (((key
& (INOFND
|NAMEFND
)) == (INOFND
|NAMEFND
)) && ip
!= np
) {
292 if (lookuptype
== LINK
) {
296 Dprintf(stdout
, "name/inode conflict, mktempname %s\n",
303 if ((key
& ONTAPE
) &&
304 (((key
& INOFND
) && ip
->e_type
!= type
) ||
305 ((key
& NAMEFND
) && np
->e_type
!= type
)))
309 * Decide on the disposition of the file based on its flags.
310 * Note that we have already handled the case in which
311 * a name and inode are found that correspond to different files.
312 * Thus if both NAMEFND and INOFND are set then ip == np.
317 * A previously existing file has been found.
318 * Mark it as KEEP so that other links to the inode can be
319 * detected, and so that it will not be reclaimed by the search
320 * for unreferenced names.
324 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
329 * A file on the tape has a name which is the same as a name
330 * corresponding to a different file in the previous dump.
331 * Since all files to be deleted have already been removed,
332 * this file is either a now unneeded link, or it must live
333 * under a new name in this dump level. If it is a link, it
334 * can simply be removed. If it is not a link, it is given a
335 * temporary name in anticipation that it will be renamed
336 * when it is later found by inode number (see INOFND case
337 * below). The entry is then treated as a new file.
340 case ONTAPE
|NAMEFND
|MODECHG
:
341 if (lookuptype
== LINK
) {
350 * A previously non-existent file.
351 * Add it to the file system, and request its extraction.
352 * If it is a directory, create it immediately.
353 * (Since the name is unused there can be no conflict)
356 ep
= addentry(name
, ino
, type
);
359 ep
->e_flags
|= NEW
|KEEP
;
360 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
365 * A file with the same inode number, but a different
366 * name has been found. If the other name has not already
367 * been found (indicated by the KEEP flag, see above) then
368 * this must be a new name for the file, and it is renamed.
369 * If the other name has been found then this must be a
370 * link to the file. Hard links to directories are not
371 * permitted, and are either deleted or converted to
372 * symbolic links. Finally, if the file is on the tape,
373 * a request is made to extract it.
376 if (type
== LEAF
&& (ip
->e_flags
& KEEP
) == 0)
377 ip
->e_flags
|= EXTRACT
;
380 if ((ip
->e_flags
& KEEP
) == 0) {
381 renameit(myname(ip
), name
);
384 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
388 if (ip
->e_type
== NODE
) {
391 "deleted hard link %s to directory %s\n",
395 ep
= addentry(name
, ino
, type
|LINK
);
397 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
402 * A previously known file which is to be updated. If it is a link,
403 * then all names referring to the previous file must be removed
404 * so that the subset of them that remain can be recreated.
406 case ONTAPE
|INOFND
|NAMEFND
:
407 if (lookuptype
== LINK
) {
410 ep
= addentry(name
, ino
, type
|LINK
);
413 ep
->e_flags
|= NEW
|KEEP
;
414 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
418 if (type
== LEAF
&& lookuptype
!= LINK
)
419 np
->e_flags
|= EXTRACT
;
421 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
426 * An inode is being reused in a completely different way.
427 * Normally an extract can simply do an "unlink" followed
428 * by a "creat". Here we must do effectively the same
429 * thing. The complications arise because we cannot really
430 * delete a directory since it may still contain files
431 * that we need to rename, so we delete it from the symbol
432 * table, and put it on the list to be deleted eventually.
433 * Conversely if a directory is to be created, it must be
434 * done immediately, rather than waiting until the
437 case ONTAPE
|INOFND
|MODECHG
:
438 case ONTAPE
|INOFND
|NAMEFND
|MODECHG
:
439 if (ip
->e_flags
& KEEP
) {
440 badentry(ip
, "cannot KEEP and change modes");
443 if (ip
->e_type
== LEAF
) {
444 /* changing from leaf to node */
445 for ( ; ip
!= NULL
; ip
= ip
->e_links
) {
446 if (ip
->e_type
!= LEAF
)
447 badentry(ip
, "NODE and LEAF links to same inode");
451 ip
= addentry(name
, ino
, type
);
454 /* changing from node to leaf */
455 if ((ip
->e_flags
& TMPNAME
) == 0)
457 deleteino(ip
->e_ino
);
458 ip
->e_next
= removelist
;
460 ip
= addentry(name
, ino
, type
);
462 ip
->e_flags
|= NEW
|KEEP
;
463 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
468 * A hard link to a directory that has been removed.
472 Dprintf(stdout
, "[%s] %s: Extraneous name\n", keyval(key
),
478 * If we find a directory entry for a file that is not on
479 * the tape, then we must have found a file that was created
480 * while the dump was in progress. Since we have no contents
481 * for it, we discard the name knowing that it will be on the
482 * next incremental tape.
485 if (compare_ignore_not_found
) break;
486 fprintf(stderr
, "%s: (inode %lu) not found on tape\n",
487 name
, (unsigned long)ino
);
491 * If any of these arise, something is grievously wrong with
492 * the current state of the symbol table.
494 case INOFND
|NAMEFND
|MODECHG
:
495 case NAMEFND
|MODECHG
:
497 fprintf(stderr
, "[%s] %s: inconsistent state\n", keyval(key
),
502 * These states "cannot" arise for any state of the symbol table.
507 panic("[%s] %s: impossible state\n", keyval(key
), name
);
514 * Calculate the active flags in a key.
519 static char keybuf
[32];
521 (void) strcpy(keybuf
, "|NIL");
524 (void) strcat(keybuf
, "|ONTAPE");
526 (void) strcat(keybuf
, "|INOFND");
528 (void) strcat(keybuf
, "|NAMEFND");
530 (void) strcat(keybuf
, "|MODECHG");
535 * Find unreferenced link names.
540 register struct entry
*ep
, *np
;
543 Vprintf(stdout
, "Find unreferenced names.\n");
544 for (i
= ROOTINO
; i
< maxino
; i
++) {
546 if (ep
== NULL
|| ep
->e_type
== LEAF
|| TSTINO(i
, dumpmap
) == 0)
548 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
549 if (np
->e_flags
== 0) {
551 "%s: remove unreferenced name\n",
559 * Any leaves remaining in removed directories is unreferenced.
561 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
) {
562 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
563 if (np
->e_type
== LEAF
) {
564 if (np
->e_flags
!= 0)
565 badentry(np
, "unreferenced with flags");
567 "%s: remove unreferenced name\n",
577 * Remove old nodes (directories).
578 * Note that this routine runs in O(N*D) where:
579 * N is the number of directory entries to be removed.
580 * D is the maximum depth of the tree.
581 * If N == D this can be quite slow. If the list were
582 * topologically sorted, the deletion could be done in
588 register struct entry
*ep
, **prev
;
591 Vprintf(stdout
, "Remove old nodes (directories).\n");
595 for (ep
= removelist
; ep
!= NULL
; ep
= *prev
) {
596 if (ep
->e_entries
!= NULL
) {
606 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
)
607 badentry(ep
, "cannot remove, non-empty");
610 /* Compare the file specified in `ep' (which is on tape) to the */
611 /* current copy of this file on disk. If do_compare is 0, then just */
612 /* make our caller think we did it--this is used to handle hard links */
613 /* to files and devices. */
615 compare_entry(struct entry
*ep
, int do_compare
)
617 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
618 badentry(ep
, "unexpected file on tape");
619 if (do_compare
) (void) comparefile(myname(ep
));
620 ep
->e_flags
&= ~(NEW
|EXTRACT
);
624 * This is the routine used to compare files for the 'C' command.
629 register struct entry
*ep
;
633 first
= lowerbnd(ROOTINO
);
635 while (curfile
.ino
< maxino
) {
636 first
= lowerbnd(first
);
638 * If the next available file is not the one which we
639 * expect then we have missed one or more files. Since
640 * we do not request files that were not on the tape,
641 * the lost files must have been due to a tape read error,
642 * or a file that was removed while the dump was in progress.
644 while (first
< curfile
.ino
) {
645 ep
= lookupino(first
);
647 panic("%d: bad first\n", first
);
648 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
649 ep
->e_flags
&= ~(NEW
|EXTRACT
);
650 first
= lowerbnd(first
);
653 * If we find files on the tape that have no corresponding
654 * directory entries, then we must have found a file that
655 * was created while the dump was in progress. Since we have
656 * no name for it, we discard it knowing that it will be
657 * on the next incremental tape.
659 if (first
!= curfile
.ino
) {
660 fprintf(stderr
, "expected next file %ld, got %lu\n",
661 (long)first
, (unsigned long)curfile
.ino
);
665 ep
= lookupino(curfile
.ino
);
667 panic("unknown file on tape\n");
668 compare_entry(ep
, 1);
669 for (ep
= ep
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
670 compare_entry(ep
, 0);
674 * We checkpoint the restore after every tape reel, so
675 * as to simplify the amount of work re quired by the
679 if (curvol
!= volno
) {
687 * This is the routine used to extract files for the 'r' command.
688 * Extract new leaves.
691 createleaves(char *symtabfile
)
693 register struct entry
*ep
;
697 if (command
== 'R') {
698 Vprintf(stdout
, "Continue extraction of new leaves\n");
700 Vprintf(stdout
, "Extract new leaves.\n");
701 dumpsymtable(symtabfile
, volno
);
703 first
= lowerbnd(ROOTINO
);
705 while (curfile
.ino
< maxino
) {
706 first
= lowerbnd(first
);
708 * If the next available file is not the one which we
709 * expect then we have missed one or more files. Since
710 * we do not request files that were not on the tape,
711 * the lost files must have been due to a tape read error,
712 * or a file that was removed while the dump was in progress.
714 while (first
< curfile
.ino
) {
715 ep
= lookupino(first
);
717 panic("%d: bad first\n", first
);
718 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
719 ep
->e_flags
&= ~(NEW
|EXTRACT
);
720 first
= lowerbnd(first
);
723 * If we find files on the tape that have no corresponding
724 * directory entries, then we must have found a file that
725 * was created while the dump was in progress. Since we have
726 * no name for it, we discard it knowing that it will be
727 * on the next incremental tape.
729 if (first
!= curfile
.ino
) {
730 fprintf(stderr
, "expected next file %ld, got %lu\n",
731 (long)first
, (unsigned long)curfile
.ino
);
735 ep
= lookupino(curfile
.ino
);
737 panic("unknown file on tape\n");
738 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
739 badentry(ep
, "unexpected file on tape");
741 * If the file is to be extracted, then the old file must
742 * be removed since its type may change from one leaf type
743 * to another (e.g. "file" to "character special").
745 if ((ep
->e_flags
& EXTRACT
) != 0) {
747 ep
->e_flags
&= ~REMOVED
;
749 (void) extractfile(myname(ep
));
750 ep
->e_flags
&= ~(NEW
|EXTRACT
);
752 * We checkpoint the restore after every tape reel, so
753 * as to simplify the amount of work required by the
757 if (curvol
!= volno
) {
758 dumpsymtable(symtabfile
, volno
);
766 * This is the routine used to extract files for the 'x' and 'i' commands.
767 * Efficiently extract a subset of the files on a tape.
772 register ino_t first
, next
, last
;
773 register struct entry
*ep
;
776 Vprintf(stdout
, "Extract requested files\n");
777 curfile
.action
= SKIP
;
781 first
= lowerbnd(ROOTINO
);
782 last
= upperbnd(maxino
- 1);
784 first
= lowerbnd(first
);
785 last
= upperbnd(last
);
787 * Check to see if any files remain to be extracted
792 * Reject any volumes with inodes greater
793 * than the last one needed
795 while (curfile
.ino
> last
) {
796 curfile
.action
= SKIP
;
802 * Decide on the next inode needed.
803 * Skip across the inodes until it is found
804 * or an out of order volume change is encountered
806 next
= lowerbnd(curfile
.ino
);
809 while (next
> curfile
.ino
&& volno
== curvol
)
813 } while (volno
== curvol
+ 1);
815 * If volume change out of order occurred the
816 * current state must be recalculated
821 * If the current inode is greater than the one we were
822 * looking for then we missed the one we were looking for.
823 * Since we only attempt to extract files listed in the
824 * dump map, the lost files must have been due to a tape
825 * read error, or a file that was removed while the dump
826 * was in progress. Thus we report all requested files
827 * between the one we were looking for, and the one we
828 * found as missing, and delete their request flags.
830 while (next
< curfile
.ino
) {
831 ep
= lookupino(next
);
833 panic("corrupted symbol table\n");
834 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
836 next
= lowerbnd(next
);
839 * The current inode is the one that we are looking for,
840 * so extract it per its requested name.
842 if (next
== curfile
.ino
&& next
<= last
) {
843 ep
= lookupino(next
);
845 panic("corrupted symbol table\n");
846 (void) extractfile(myname(ep
));
860 register struct entry
*np
, *ep
;
864 if ((ep
= lookupino(WINO
))) {
865 Vprintf(stdout
, "Add whiteouts\n");
866 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
867 if ((ep
->e_flags
& NEW
) == 0)
870 (void)fprintf(stderr
, "BUG! Should call addwhiteout\n");
872 (void) addwhiteout(myname(ep
));
877 Vprintf(stdout
, "Add links\n");
878 for (i
= ROOTINO
; i
< maxino
; i
++) {
882 for (np
= ep
->e_links
; np
!= NULL
; np
= np
->e_links
) {
883 if ((np
->e_flags
& NEW
) == 0)
885 (void) strcpy(name
, myname(ep
));
886 if (ep
->e_type
== NODE
) {
887 (void) linkit(name
, myname(np
), SYMLINK
);
889 (void) linkit(name
, myname(np
), HARDLINK
);
897 * Check the symbol table.
898 * We do this to insure that all the requested work was done, and
899 * that no temporary names remain.
904 register struct entry
*ep
;
907 Vprintf(stdout
, "Check the symbol table.\n");
908 for (i
= WINO
; i
< maxino
; i
++) {
909 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
910 ep
->e_flags
&= ~KEEP
;
911 if (ep
->e_type
== NODE
)
912 ep
->e_flags
&= ~(NEW
|EXISTED
);
913 if (ep
->e_flags
/* != NULL */)
914 badentry(ep
, "incomplete operations");
920 * Compare with the directory structure on the tape
921 * A paranoid check that things are as they should be.
924 verifyfile(char *name
, ino_t ino
, int type
)
926 struct entry
*np
, *ep
;
929 ep
= lookupname(name
);
931 fprintf(stderr
, "Warning: missing name %s\n", name
);
937 for ( ; np
!= NULL
; np
= np
->e_links
)
941 panic("missing inumber %d\n", ino
);
942 if (ep
->e_type
== LEAF
&& type
!= LEAF
)
943 badentry(ep
, "type should be LEAF");