]>
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
6 * Stelian Pop <pop@cybercable.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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
43 static const char rcsid
[] =
44 "$Id: restore.c,v 1.11 2000/11/10 14:42:25 stelian Exp $";
47 #include <sys/types.h>
50 #include <sys/param.h>
52 #include <linux/ext2_fs.h>
53 #include <bsdcompat.h>
55 #include <ufs/ufs/dinode.h>
56 #endif /* __linux__ */
62 #include <ext2fs/ext2fs.h>
68 static char *keyval
__P((int));
71 * This implements the 't' option.
72 * List entries on the tape.
75 listfile(char *name
, ino_t ino
, int type
)
77 long descend
= hflag
? GOOD
: FAIL
;
79 if (TSTINO(ino
, dumpmap
) == 0)
81 Vprintf(stdout
, "%s", type
== LEAF
? "leaf" : "dir ");
82 fprintf(stdout
, "%10lu\t%s\n", (unsigned long)ino
, name
);
87 * This implements the 'x' option.
88 * Request that new entries be extracted.
91 addfile(char *name
, ino_t ino
, int type
)
93 register struct entry
*ep
, *np
;
94 long descend
= hflag
? GOOD
: FAIL
;
97 if (TSTINO(ino
, dumpmap
) == 0) {
98 Dprintf(stdout
, "%s: not on the tape\n", name
);
101 if (ino
== WINO
&& command
== 'i' && !vflag
)
104 (void) snprintf(buf
, sizeof(buf
), "./%lu", (unsigned long)ino
);
107 (void) genliteraldir(name
, ino
);
113 if (strcmp(name
, myname(ep
)) == 0) {
118 for (np
= ep
->e_links
; np
; np
= np
->e_links
)
119 if (strcmp(name
, myname(np
)) == 0) {
124 ep
= addentry(name
, ino
, type
);
132 * This is used by the 'i' option to undo previous requests made by addfile.
133 * Delete entries from the request queue.
137 deletefile(char *name
, ino_t ino
, int type
)
139 long descend
= hflag
? GOOD
: FAIL
;
142 if (TSTINO(ino
, dumpmap
) == 0)
144 ep
= lookupname(name
);
147 ep
->e_flags
|= REMOVED
;
148 if (ep
->e_type
!= NODE
)
155 * The following four routines implement the incremental
156 * restore algorithm. The first removes old entries, the second
157 * does renames and calculates the extraction list, the third
158 * cleans up link names missed by the first two, and the final
159 * one deletes old directories.
161 * Directories cannot be immediately deleted, as they may have
162 * other files in them which need to be moved out first. As
163 * directories to be deleted are found, they are put on the
164 * following deletion list. After all deletions and renames
165 * are done, this list is actually deleted.
167 static struct entry
*removelist
;
170 * Remove invalid whiteouts from the old tree.
171 * Remove unneeded leaves from the old tree.
172 * Remove directories from the lookup chains.
175 removeoldleaves(void)
177 register struct entry
*ep
, *nextep
;
178 register ino_t i
, mydirino
;
180 Vprintf(stdout
, "Mark entries to be removed.\n");
181 if ((ep
= lookupino(WINO
))) {
182 Vprintf(stdout
, "Delete whiteouts\n");
183 for ( ; ep
!= NULL
; ep
= nextep
) {
184 nextep
= ep
->e_links
;
185 mydirino
= ep
->e_parent
->e_ino
;
187 * We remove all whiteouts that are in directories
188 * that have been removed or that have been dumped.
190 if (TSTINO(mydirino
, usedinomap
) &&
191 !TSTINO(mydirino
, dumpmap
))
194 (void)fprintf(stderr
, "BUG! Should call delwhiteout\n");
201 for (i
= ROOTINO
+ 1; i
< maxino
; i
++) {
205 if (TSTINO(i
, usedinomap
))
207 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
208 Dprintf(stdout
, "%s: REMOVE\n", myname(ep
));
209 if (ep
->e_type
== LEAF
) {
214 deleteino(ep
->e_ino
);
215 ep
->e_next
= removelist
;
223 * For each directory entry on the incremental tape, determine which
224 * category it falls into as follows:
225 * KEEP - entries that are to be left alone.
226 * NEW - new entries to be added.
227 * EXTRACT - files that must be updated with new contents.
228 * LINK - new links to be added.
229 * Renames are done at the same time.
232 nodeupdates(char *name
, ino_t ino
, int type
)
234 register struct entry
*ep
, *np
, *ip
;
239 # define ONTAPE 0x1 /* inode is on the tape */
240 # define INOFND 0x2 /* inode already exists */
241 # define NAMEFND 0x4 /* name already exists */
242 # define MODECHG 0x8 /* mode of inode changed */
245 * This routine is called once for each element in the
246 * directory hierarchy, with a full path name.
247 * The "type" value is incorrectly specified as LEAF for
248 * directories that are not on the dump tape.
250 * Check to see if the file is on the tape.
252 if (TSTINO(ino
, dumpmap
))
255 * Check to see if the name exists, and if the name is a link.
257 np
= lookupname(name
);
260 ip
= lookupino(np
->e_ino
);
262 panic("corrupted symbol table\n");
267 * Check to see if the inode exists, and if one of its links
268 * corresponds to the name (if one was found).
273 for (ep
= ip
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
281 * If both a name and an inode are found, but they do not
282 * correspond to the same file, then both the inode that has
283 * been found and the inode corresponding to the name that
284 * has been found need to be renamed. The current pathname
285 * is the new name for the inode that has been found. Since
286 * all files to be deleted have already been removed, the
287 * named file is either a now unneeded link, or it must live
288 * under a new name in this dump level. If it is a link, it
289 * can be removed. If it is not a link, it is given a
290 * temporary name in anticipation that it will be renamed
291 * when it is later found by inode number.
293 if (((key
& (INOFND
|NAMEFND
)) == (INOFND
|NAMEFND
)) && ip
!= np
) {
294 if (lookuptype
== LINK
) {
298 Dprintf(stdout
, "name/inode conflict, mktempname %s\n",
305 if ((key
& ONTAPE
) &&
306 (((key
& INOFND
) && ip
->e_type
!= type
) ||
307 ((key
& NAMEFND
) && np
->e_type
!= type
)))
311 * Decide on the disposition of the file based on its flags.
312 * Note that we have already handled the case in which
313 * a name and inode are found that correspond to different files.
314 * Thus if both NAMEFND and INOFND are set then ip == np.
319 * A previously existing file has been found.
320 * Mark it as KEEP so that other links to the inode can be
321 * detected, and so that it will not be reclaimed by the search
322 * for unreferenced names.
326 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
331 * A file on the tape has a name which is the same as a name
332 * corresponding to a different file in the previous dump.
333 * Since all files to be deleted have already been removed,
334 * this file is either a now unneeded link, or it must live
335 * under a new name in this dump level. If it is a link, it
336 * can simply be removed. If it is not a link, it is given a
337 * temporary name in anticipation that it will be renamed
338 * when it is later found by inode number (see INOFND case
339 * below). The entry is then treated as a new file.
342 case ONTAPE
|NAMEFND
|MODECHG
:
343 if (lookuptype
== LINK
) {
352 * A previously non-existent file.
353 * Add it to the file system, and request its extraction.
354 * If it is a directory, create it immediately.
355 * (Since the name is unused there can be no conflict)
358 ep
= addentry(name
, ino
, type
);
361 ep
->e_flags
|= NEW
|KEEP
;
362 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
367 * A file with the same inode number, but a different
368 * name has been found. If the other name has not already
369 * been found (indicated by the KEEP flag, see above) then
370 * this must be a new name for the file, and it is renamed.
371 * If the other name has been found then this must be a
372 * link to the file. Hard links to directories are not
373 * permitted, and are either deleted or converted to
374 * symbolic links. Finally, if the file is on the tape,
375 * a request is made to extract it.
378 if (type
== LEAF
&& (ip
->e_flags
& KEEP
) == 0)
379 ip
->e_flags
|= EXTRACT
;
382 if ((ip
->e_flags
& KEEP
) == 0) {
383 renameit(myname(ip
), name
);
386 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
390 if (ip
->e_type
== NODE
) {
393 "deleted hard link %s to directory %s\n",
397 ep
= addentry(name
, ino
, type
|LINK
);
399 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
404 * A previously known file which is to be updated. If it is a link,
405 * then all names referring to the previous file must be removed
406 * so that the subset of them that remain can be recreated.
408 case ONTAPE
|INOFND
|NAMEFND
:
409 if (lookuptype
== LINK
) {
412 ep
= addentry(name
, ino
, type
|LINK
);
415 ep
->e_flags
|= NEW
|KEEP
;
416 Dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
420 if (type
== LEAF
&& lookuptype
!= LINK
)
421 np
->e_flags
|= EXTRACT
;
423 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
428 * An inode is being reused in a completely different way.
429 * Normally an extract can simply do an "unlink" followed
430 * by a "creat". Here we must do effectively the same
431 * thing. The complications arise because we cannot really
432 * delete a directory since it may still contain files
433 * that we need to rename, so we delete it from the symbol
434 * table, and put it on the list to be deleted eventually.
435 * Conversely if a directory is to be created, it must be
436 * done immediately, rather than waiting until the
439 case ONTAPE
|INOFND
|MODECHG
:
440 case ONTAPE
|INOFND
|NAMEFND
|MODECHG
:
441 if (ip
->e_flags
& KEEP
) {
442 badentry(ip
, "cannot KEEP and change modes");
445 if (ip
->e_type
== LEAF
) {
446 /* changing from leaf to node */
447 for ( ; ip
!= NULL
; ip
= ip
->e_links
) {
448 if (ip
->e_type
!= LEAF
)
449 badentry(ip
, "NODE and LEAF links to same inode");
453 ip
= addentry(name
, ino
, type
);
456 /* changing from node to leaf */
457 if ((ip
->e_flags
& TMPNAME
) == 0)
459 deleteino(ip
->e_ino
);
460 ip
->e_next
= removelist
;
462 ip
= addentry(name
, ino
, type
);
464 ip
->e_flags
|= NEW
|KEEP
;
465 Dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
470 * A hard link to a directory that has been removed.
474 Dprintf(stdout
, "[%s] %s: Extraneous name\n", keyval(key
),
480 * If we find a directory entry for a file that is not on
481 * the tape, then we must have found a file that was created
482 * while the dump was in progress. Since we have no contents
483 * for it, we discard the name knowing that it will be on the
484 * next incremental tape.
487 if (compare_ignore_not_found
) break;
488 fprintf(stderr
, "%s: (inode %lu) not found on tape\n",
489 name
, (unsigned long)ino
);
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.
522 static char keybuf
[32];
524 (void) strcpy(keybuf
, "|NIL");
527 (void) strcat(keybuf
, "|ONTAPE");
529 (void) strcat(keybuf
, "|INOFND");
531 (void) strcat(keybuf
, "|NAMEFND");
533 (void) strcat(keybuf
, "|MODECHG");
538 * Find unreferenced link names.
543 register struct entry
*ep
, *np
;
546 Vprintf(stdout
, "Find unreferenced names.\n");
547 for (i
= ROOTINO
; i
< maxino
; i
++) {
549 if (ep
== NULL
|| ep
->e_type
== LEAF
|| TSTINO(i
, dumpmap
) == 0)
551 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
552 if (np
->e_flags
== 0) {
554 "%s: remove unreferenced name\n",
562 * Any leaves remaining in removed directories is unreferenced.
564 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
) {
565 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
566 if (np
->e_type
== LEAF
) {
567 if (np
->e_flags
!= 0)
568 badentry(np
, "unreferenced with flags");
570 "%s: remove unreferenced name\n",
580 * Remove old nodes (directories).
581 * Note that this routine runs in O(N*D) where:
582 * N is the number of directory entries to be removed.
583 * D is the maximum depth of the tree.
584 * If N == D this can be quite slow. If the list were
585 * topologically sorted, the deletion could be done in
591 register struct entry
*ep
, **prev
;
594 Vprintf(stdout
, "Remove old nodes (directories).\n");
598 for (ep
= removelist
; ep
!= NULL
; ep
= *prev
) {
599 if (ep
->e_entries
!= NULL
) {
609 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
)
610 badentry(ep
, "cannot remove, non-empty");
613 /* Compare the file specified in `ep' (which is on tape) to the */
614 /* current copy of this file on disk. If do_compare is 0, then just */
615 /* make our caller think we did it--this is used to handle hard links */
616 /* 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");
624 if (do_compare
) (void) comparefile(myname(ep
));
625 ep
->e_flags
&= ~(NEW
|EXTRACT
);
629 * This is the routine used to compare files for the 'C' command.
634 register struct entry
*ep
;
638 first
= lowerbnd(ROOTINO
);
640 while (curfile
.ino
< maxino
) {
641 first
= lowerbnd(first
);
643 * If the next available file is not the one which we
644 * expect then we have missed one or more files. Since
645 * we do not request files that were not on the tape,
646 * the lost files must have been due to a tape read error,
647 * or a file that was removed while the dump was in progress.
649 while (first
< curfile
.ino
) {
650 ep
= lookupino(first
);
652 panic("%d: bad first\n", first
);
653 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
655 ep
->e_flags
&= ~(NEW
|EXTRACT
);
656 first
= lowerbnd(first
);
659 * If we find files on the tape that have no corresponding
660 * directory entries, then we must have found a file that
661 * was created while the dump was in progress. Since we have
662 * no name for it, we discard it knowing that it will be
663 * on the next incremental tape.
665 if (first
!= curfile
.ino
) {
666 fprintf(stderr
, "expected next file %ld, got %lu\n",
667 (long)first
, (unsigned long)curfile
.ino
);
672 ep
= lookupino(curfile
.ino
);
674 panic("unknown file on tape\n");
677 compare_entry(ep
, 1);
678 for (ep
= ep
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
679 compare_entry(ep
, 0);
683 * We checkpoint the restore after every tape reel, so
684 * as to simplify the amount of work re quired by the
688 if (curvol
!= volno
) {
696 * This is the routine used to extract files for the 'r' command.
697 * Extract new leaves.
700 createleaves(char *symtabfile
)
702 register struct entry
*ep
;
706 if (command
== 'R') {
707 Vprintf(stdout
, "Continue extraction of new leaves\n");
709 Vprintf(stdout
, "Extract new leaves.\n");
710 dumpsymtable(symtabfile
, volno
);
712 first
= lowerbnd(ROOTINO
);
714 while (curfile
.ino
< maxino
) {
715 first
= lowerbnd(first
);
717 * If the next available file is not the one which we
718 * expect then we have missed one or more files. Since
719 * we do not request files that were not on the tape,
720 * the lost files must have been due to a tape read error,
721 * or a file that was removed while the dump was in progress.
723 while (first
< curfile
.ino
) {
724 ep
= lookupino(first
);
726 panic("%d: bad first\n", first
);
727 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
728 ep
->e_flags
&= ~(NEW
|EXTRACT
);
729 first
= lowerbnd(first
);
732 * If we find files on the tape that have no corresponding
733 * directory entries, then we must have found a file that
734 * was created while the dump was in progress. Since we have
735 * no name for it, we discard it knowing that it will be
736 * on the next incremental tape.
738 if (first
!= curfile
.ino
) {
739 fprintf(stderr
, "expected next file %ld, got %lu\n",
740 (long)first
, (unsigned long)curfile
.ino
);
744 ep
= lookupino(curfile
.ino
);
746 panic("unknown file on tape\n");
747 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
748 badentry(ep
, "unexpected file on tape");
750 * If the file is to be extracted, then the old file must
751 * be removed since its type may change from one leaf type
752 * to another (e.g. "file" to "character special").
754 if ((ep
->e_flags
& EXTRACT
) != 0) {
756 ep
->e_flags
&= ~REMOVED
;
758 (void) extractfile(myname(ep
));
759 ep
->e_flags
&= ~(NEW
|EXTRACT
);
761 * We checkpoint the restore after every tape reel, so
762 * as to simplify the amount of work required by the
766 if (curvol
!= volno
) {
767 dumpsymtable(symtabfile
, volno
);
775 * This is the routine used to extract files for the 'x' and 'i' commands.
776 * Efficiently extract a subset of the files on a tape.
781 register ino_t first
, next
, last
;
782 register struct entry
*ep
;
785 Vprintf(stdout
, "Extract requested files\n");
786 curfile
.action
= SKIP
;
790 first
= lowerbnd(ROOTINO
);
791 last
= upperbnd(maxino
- 1);
793 first
= lowerbnd(first
);
794 last
= upperbnd(last
);
796 * Check to see if any files remain to be extracted
801 * Reject any volumes with inodes greater
802 * than the last one needed
804 while (curfile
.ino
> last
) {
805 curfile
.action
= SKIP
;
811 * Decide on the next inode needed.
812 * Skip across the inodes until it is found
813 * or an out of order volume change is encountered
815 next
= lowerbnd(curfile
.ino
);
818 while (next
> curfile
.ino
&& volno
== curvol
)
822 } while (volno
== curvol
+ 1);
824 * If volume change out of order occurred the
825 * current state must be recalculated
830 * If the current inode is greater than the one we were
831 * looking for then we missed the one we were looking for.
832 * Since we only attempt to extract files listed in the
833 * dump map, the lost files must have been due to a tape
834 * read error, or a file that was removed while the dump
835 * was in progress. Thus we report all requested files
836 * between the one we were looking for, and the one we
837 * found as missing, and delete their request flags.
839 while (next
< curfile
.ino
) {
840 ep
= lookupino(next
);
842 panic("corrupted symbol table\n");
843 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
845 next
= lowerbnd(next
);
848 * The current inode is the one that we are looking for,
849 * so extract it per its requested name.
851 if (next
== curfile
.ino
&& next
<= last
) {
852 ep
= lookupino(next
);
854 panic("corrupted symbol table\n");
855 (void) extractfile(myname(ep
));
869 register struct entry
*np
, *ep
;
873 if ((ep
= lookupino(WINO
))) {
874 Vprintf(stdout
, "Add whiteouts\n");
875 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
876 if ((ep
->e_flags
& NEW
) == 0)
879 (void)fprintf(stderr
, "BUG! Should call addwhiteout\n");
881 (void) addwhiteout(myname(ep
));
886 Vprintf(stdout
, "Add links\n");
887 for (i
= ROOTINO
; i
< maxino
; i
++) {
891 for (np
= ep
->e_links
; np
!= NULL
; np
= np
->e_links
) {
892 if ((np
->e_flags
& NEW
) == 0)
894 (void) strcpy(name
, myname(ep
));
895 if (ep
->e_type
== NODE
) {
896 (void) linkit(name
, myname(np
), SYMLINK
);
898 (void) linkit(name
, myname(np
), HARDLINK
);
906 * Check the symbol table.
907 * We do this to insure that all the requested work was done, and
908 * that no temporary names remain.
913 register struct entry
*ep
;
916 Vprintf(stdout
, "Check the symbol table.\n");
917 for (i
= WINO
; i
< maxino
; i
++) {
918 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
919 ep
->e_flags
&= ~KEEP
;
920 if (ep
->e_type
== NODE
)
921 ep
->e_flags
&= ~(NEW
|EXISTED
);
922 if (ep
->e_flags
/* != NULL */)
923 badentry(ep
, "incomplete operations");
929 * Compare with the directory structure on the tape
930 * A paranoid check that things are as they should be.
933 verifyfile(char *name
, ino_t ino
, int type
)
935 struct entry
*np
, *ep
;
938 ep
= lookupname(name
);
940 fprintf(stderr
, "Warning: missing name %s\n", name
);
946 for ( ; np
!= NULL
; np
= np
->e_links
)
950 panic("missing inumber %d\n", ino
);
951 if (ep
->e_type
== LEAF
&& type
!= LEAF
)
952 badentry(ep
, "type should be LEAF");