2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)restore.c 8.3 (Berkeley) 9/13/94
34 * $FreeBSD: src/sbin/restore/restore.c,v 1.7.2.1 2002/03/01 21:32:28 iedowse Exp $
35 * $DragonFly: src/sbin/restore/restore.c,v 1.9 2008/06/05 18:06:31 swildner Exp $
38 #include <sys/types.h>
40 #include <vfs/ufs/dinode.h>
48 static char *keyval(int);
51 * This implements the 't' option.
52 * List entries on the tape.
55 listfile(char *name
, ufs1_ino_t ino
, int type
)
57 long descend
= hflag
? GOOD
: FAIL
;
59 if (TSTINO(ino
, dumpmap
) == 0)
61 vprintf(stdout
, "%s", type
== LEAF
? "leaf" : "dir ");
62 fprintf(stdout
, "%10d\t%s\n", ino
, name
);
67 * This implements the 'x' option.
68 * Request that new entries be extracted.
71 addfile(char *name
, ufs1_ino_t ino
, int type
)
74 long descend
= hflag
? GOOD
: FAIL
;
77 if (TSTINO(ino
, dumpmap
) == 0) {
78 dprintf(stdout
, "%s: not on the tape\n", name
);
81 if (ino
== WINO
&& command
== 'i' && !vflag
)
84 sprintf(buf
, "./%u", ino
);
87 genliteraldir(name
, ino
);
93 if (strcmp(name
, myname(ep
)) == 0) {
99 ep
= addentry(name
, ino
, type
);
107 * This is used by the 'i' option to undo previous requests made by addfile.
108 * Delete entries from the request queue.
112 deletefile(char *name
, ufs1_ino_t ino
, int type
)
114 long descend
= hflag
? GOOD
: FAIL
;
117 if (TSTINO(ino
, dumpmap
) == 0)
119 ep
= lookupname(name
);
122 ep
->e_flags
|= REMOVED
;
123 if (ep
->e_type
!= NODE
)
130 * The following four routines implement the incremental
131 * restore algorithm. The first removes old entries, the second
132 * does renames and calculates the extraction list, the third
133 * cleans up link names missed by the first two, and the final
134 * one deletes old directories.
136 * Directories cannot be immediately deleted, as they may have
137 * other files in them which need to be moved out first. As
138 * directories to be deleted are found, they are put on the
139 * following deletion list. After all deletions and renames
140 * are done, this list is actually deleted.
142 static struct entry
*removelist
;
145 * Remove invalid whiteouts from the old tree.
146 * Remove unneeded leaves from the old tree.
147 * Remove directories from the lookup chains.
150 removeoldleaves(void)
152 struct entry
*ep
, *nextep
;
153 ufs1_ino_t i
, mydirino
;
155 vprintf(stdout
, "Mark entries to be removed.\n");
156 if ((ep
= lookupino(WINO
))) {
157 vprintf(stdout
, "Delete whiteouts\n");
158 for ( ; ep
!= NULL
; ep
= nextep
) {
159 nextep
= ep
->e_links
;
160 mydirino
= ep
->e_parent
->e_ino
;
162 * We remove all whiteouts that are in directories
163 * that have been removed or that have been dumped.
165 if (TSTINO(mydirino
, usedinomap
) &&
166 !TSTINO(mydirino
, dumpmap
))
172 for (i
= ROOTINO
+ 1; i
< maxino
; i
++) {
176 if (TSTINO(i
, usedinomap
))
178 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
179 dprintf(stdout
, "%s: REMOVE\n", myname(ep
));
180 if (ep
->e_type
== LEAF
) {
185 deleteino(ep
->e_ino
);
186 ep
->e_next
= removelist
;
194 * For each directory entry on the incremental tape, determine which
195 * category it falls into as follows:
196 * KEEP - entries that are to be left alone.
197 * NEW - new entries to be added.
198 * EXTRACT - files that must be updated with new contents.
199 * LINK - new links to be added.
200 * Renames are done at the same time.
203 nodeupdates(char *name
, ufs1_ino_t ino
, int type
)
205 struct entry
*ep
, *np
, *ip
;
210 # define ONTAPE 0x1 /* inode is on the tape */
211 # define INOFND 0x2 /* inode already exists */
212 # define NAMEFND 0x4 /* name already exists */
213 # define MODECHG 0x8 /* mode of inode changed */
216 * This routine is called once for each element in the
217 * directory hierarchy, with a full path name.
218 * The "type" value is incorrectly specified as LEAF for
219 * directories that are not on the dump tape.
221 * Check to see if the file is on the tape.
223 if (TSTINO(ino
, dumpmap
))
226 * Check to see if the name exists, and if the name is a link.
228 np
= lookupname(name
);
231 ip
= lookupino(np
->e_ino
);
233 panic("corrupted symbol table\n");
238 * Check to see if the inode exists, and if one of its links
239 * corresponds to the name (if one was found).
244 for (ep
= ip
->e_links
; ep
!= NULL
; ep
= ep
->e_links
) {
252 * If both a name and an inode are found, but they do not
253 * correspond to the same file, then both the inode that has
254 * been found and the inode corresponding to the name that
255 * has been found need to be renamed. The current pathname
256 * is the new name for the inode that has been found. Since
257 * all files to be deleted have already been removed, the
258 * named file is either a now unneeded link, or it must live
259 * under a new name in this dump level. If it is a link, it
260 * can be removed. If it is not a link, it is given a
261 * temporary name in anticipation that it will be renamed
262 * when it is later found by inode number.
264 if (((key
& (INOFND
|NAMEFND
)) == (INOFND
|NAMEFND
)) && ip
!= np
) {
265 if (lookuptype
== LINK
) {
269 dprintf(stdout
, "name/inode conflict, mktempname %s\n",
276 if ((key
& ONTAPE
) &&
277 (((key
& INOFND
) && ip
->e_type
!= type
) ||
278 ((key
& NAMEFND
) && np
->e_type
!= type
)))
282 * Decide on the disposition of the file based on its flags.
283 * Note that we have already handled the case in which
284 * a name and inode are found that correspond to different files.
285 * Thus if both NAMEFND and INOFND are set then ip == np.
290 * A previously existing file has been found.
291 * Mark it as KEEP so that other links to the inode can be
292 * detected, and so that it will not be reclaimed by the search
293 * for unreferenced names.
297 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
302 * A file on the tape has a name which is the same as a name
303 * corresponding to a different file in the previous dump.
304 * Since all files to be deleted have already been removed,
305 * this file is either a now unneeded link, or it must live
306 * under a new name in this dump level. If it is a link, it
307 * can simply be removed. If it is not a link, it is given a
308 * temporary name in anticipation that it will be renamed
309 * when it is later found by inode number (see INOFND case
310 * below). The entry is then treated as a new file.
313 case ONTAPE
|NAMEFND
|MODECHG
:
314 if (lookuptype
== LINK
) {
323 * A previously non-existent file.
324 * Add it to the file system, and request its extraction.
325 * If it is a directory, create it immediately.
326 * (Since the name is unused there can be no conflict)
329 ep
= addentry(name
, ino
, type
);
332 ep
->e_flags
|= NEW
|KEEP
;
333 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
338 * A file with the same inode number, but a different
339 * name has been found. If the other name has not already
340 * been found (indicated by the KEEP flag, see above) then
341 * this must be a new name for the file, and it is renamed.
342 * If the other name has been found then this must be a
343 * link to the file. Hard links to directories are not
344 * permitted, and are either deleted or converted to
345 * symbolic links. Finally, if the file is on the tape,
346 * a request is made to extract it.
349 if (type
== LEAF
&& (ip
->e_flags
& KEEP
) == 0)
350 ip
->e_flags
|= EXTRACT
;
353 if ((ip
->e_flags
& KEEP
) == 0) {
354 renameit(myname(ip
), name
);
357 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
361 if (ip
->e_type
== NODE
) {
364 "deleted hard link %s to directory %s\n",
368 ep
= addentry(name
, ino
, type
|LINK
);
370 dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
375 * A previously known file which is to be updated. If it is a link,
376 * then all names referring to the previous file must be removed
377 * so that the subset of them that remain can be recreated.
379 case ONTAPE
|INOFND
|NAMEFND
:
380 if (lookuptype
== LINK
) {
383 ep
= addentry(name
, ino
, type
|LINK
);
386 ep
->e_flags
|= NEW
|KEEP
;
387 dprintf(stdout
, "[%s] %s: %s|LINK\n", keyval(key
), name
,
391 if (type
== LEAF
&& lookuptype
!= LINK
)
392 np
->e_flags
|= EXTRACT
;
394 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
399 * An inode is being reused in a completely different way.
400 * Normally an extract can simply do an "unlink" followed
401 * by a "creat". Here we must do effectively the same
402 * thing. The complications arise because we cannot really
403 * delete a directory since it may still contain files
404 * that we need to rename, so we delete it from the symbol
405 * table, and put it on the list to be deleted eventually.
406 * Conversely if a directory is to be created, it must be
407 * done immediately, rather than waiting until the
410 case ONTAPE
|INOFND
|MODECHG
:
411 case ONTAPE
|INOFND
|NAMEFND
|MODECHG
:
412 if (ip
->e_flags
& KEEP
) {
413 badentry(ip
, "cannot KEEP and change modes");
416 if (ip
->e_type
== LEAF
) {
417 /* changing from leaf to node */
418 for (ip
= lookupino(ino
); ip
!= NULL
; ip
= ip
->e_links
) {
419 if (ip
->e_type
!= LEAF
)
420 badentry(ip
, "NODE and LEAF links to same inode");
424 ip
= addentry(name
, ino
, type
);
427 /* changing from node to leaf */
428 if ((ip
->e_flags
& TMPNAME
) == 0)
430 deleteino(ip
->e_ino
);
431 ip
->e_next
= removelist
;
433 ip
= addentry(name
, ino
, type
);
435 ip
->e_flags
|= NEW
|KEEP
;
436 dprintf(stdout
, "[%s] %s: %s\n", keyval(key
), name
,
441 * A hard link to a directory that has been removed.
445 dprintf(stdout
, "[%s] %s: Extraneous name\n", keyval(key
),
451 * If we find a directory entry for a file that is not on
452 * the tape, then we must have found a file that was created
453 * while the dump was in progress. Since we have no contents
454 * for it, we discard the name knowing that it will be on the
455 * next incremental tape.
458 fprintf(stderr
, "%s: (inode %d) not found on tape\n",
463 * If any of these arise, something is grievously wrong with
464 * the current state of the symbol table.
466 case INOFND
|NAMEFND
|MODECHG
:
467 case NAMEFND
|MODECHG
:
469 fprintf(stderr
, "[%s] %s: inconsistent state\n", keyval(key
),
474 * These states "cannot" arise for any state of the symbol table.
479 panic("[%s] %s: impossible state\n", keyval(key
), name
);
486 * Calculate the active flags in a key.
491 static char keybuf
[32];
493 strcpy(keybuf
, "|NIL");
496 strcat(keybuf
, "|ONTAPE");
498 strcat(keybuf
, "|INOFND");
500 strcat(keybuf
, "|NAMEFND");
502 strcat(keybuf
, "|MODECHG");
507 * Find unreferenced link names.
512 struct entry
*ep
, *np
;
515 vprintf(stdout
, "Find unreferenced names.\n");
516 for (i
= ROOTINO
; i
< maxino
; i
++) {
518 if (ep
== NULL
|| ep
->e_type
== LEAF
|| TSTINO(i
, dumpmap
) == 0)
520 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
521 if (np
->e_flags
== 0) {
523 "%s: remove unreferenced name\n",
531 * Any leaves remaining in removed directories is unreferenced.
533 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
) {
534 for (np
= ep
->e_entries
; np
!= NULL
; np
= np
->e_sibling
) {
535 if (np
->e_type
== LEAF
) {
536 if (np
->e_flags
!= 0)
537 badentry(np
, "unreferenced with flags");
539 "%s: remove unreferenced name\n",
549 * Remove old nodes (directories).
550 * Note that this routine runs in O(N*D) where:
551 * N is the number of directory entries to be removed.
552 * D is the maximum depth of the tree.
553 * If N == D this can be quite slow. If the list were
554 * topologically sorted, the deletion could be done in
560 struct entry
*ep
, **prev
;
563 vprintf(stdout
, "Remove old nodes (directories).\n");
567 for (ep
= removelist
; ep
!= NULL
; ep
= *prev
) {
568 if (ep
->e_entries
!= NULL
) {
578 for (ep
= removelist
; ep
!= NULL
; ep
= ep
->e_next
)
579 badentry(ep
, "cannot remove, non-empty");
583 * This is the routine used to extract files for the 'r' command.
584 * Extract new leaves.
587 createleaves(char *symtabfile
)
593 if (command
== 'R') {
594 vprintf(stdout
, "Continue extraction of new leaves\n");
596 vprintf(stdout
, "Extract new leaves.\n");
597 dumpsymtable(symtabfile
, volno
);
599 first
= lowerbnd(ROOTINO
);
601 while (curfile
.ino
< maxino
) {
602 first
= lowerbnd(first
);
604 * If the next available file is not the one which we
605 * expect then we have missed one or more files. Since
606 * we do not request files that were not on the tape,
607 * the lost files must have been due to a tape read error,
608 * or a file that was removed while the dump was in progress.
610 while (first
< curfile
.ino
) {
611 ep
= lookupino(first
);
613 panic("%d: bad first\n", first
);
614 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
615 ep
->e_flags
&= ~(NEW
|EXTRACT
);
616 first
= lowerbnd(first
);
619 * If we find files on the tape that have no corresponding
620 * directory entries, then we must have found a file that
621 * was created while the dump was in progress. Since we have
622 * no name for it, we discard it knowing that it will be
623 * on the next incremental tape.
625 if (first
!= curfile
.ino
) {
626 fprintf(stderr
, "expected next file %d, got %d\n",
631 ep
= lookupino(curfile
.ino
);
633 panic("unknown file on tape\n");
634 if ((ep
->e_flags
& (NEW
|EXTRACT
)) == 0)
635 badentry(ep
, "unexpected file on tape");
637 * If the file is to be extracted, then the old file must
638 * be removed since its type may change from one leaf type
639 * to another (e.g. "file" to "character special").
641 if ((ep
->e_flags
& EXTRACT
) != 0) {
643 ep
->e_flags
&= ~REMOVED
;
645 extractfile(myname(ep
));
646 ep
->e_flags
&= ~(NEW
|EXTRACT
);
648 * We checkpoint the restore after every tape reel, so
649 * as to simplify the amount of work required by the
653 if (curvol
!= volno
) {
654 dumpsymtable(symtabfile
, volno
);
662 * This is the routine used to extract files for the 'x' and 'i' commands.
663 * Efficiently extract a subset of the files on a tape.
668 ufs1_ino_t first
, next
, last
;
672 vprintf(stdout
, "Extract requested files\n");
673 curfile
.action
= SKIP
;
677 first
= lowerbnd(ROOTINO
);
678 last
= upperbnd(maxino
- 1);
681 first
= lowerbnd(first
);
682 last
= upperbnd(last
);
684 * Check to see if any files remain to be extracted
689 * Reject any volumes with inodes greater than the last
690 * one needed, so that we can quickly skip backwards to
691 * a volume containing useful inodes. We can't do this
692 * if there are no further volumes available (curfile.ino
693 * >= maxino) or if we are already at the first tape.
695 if (curfile
.ino
> last
&& curfile
.ino
< maxino
&& volno
> 1) {
696 curfile
.action
= SKIP
;
703 * Decide on the next inode needed.
704 * Skip across the inodes until it is found
705 * or a volume change is encountered
707 if (curfile
.ino
< maxino
) {
708 next
= lowerbnd(curfile
.ino
);
709 while (next
> curfile
.ino
&& volno
== curvol
)
711 if (volno
!= curvol
) {
718 * No further volumes or inodes available. Set
719 * `next' to the first inode, so that a warning
720 * is emitted below for each missing file.
725 * If the current inode is greater than the one we were
726 * looking for then we missed the one we were looking for.
727 * Since we only attempt to extract files listed in the
728 * dump map, the lost files must have been due to a tape
729 * read error, or a file that was removed while the dump
730 * was in progress. Thus we report all requested files
731 * between the one we were looking for, and the one we
732 * found as missing, and delete their request flags.
734 while (next
< curfile
.ino
) {
735 ep
= lookupino(next
);
737 panic("corrupted symbol table\n");
738 fprintf(stderr
, "%s: not found on tape\n", myname(ep
));
740 next
= lowerbnd(next
);
743 * The current inode is the one that we are looking for,
744 * so extract it per its requested name.
746 if (next
== curfile
.ino
&& next
<= last
) {
747 ep
= lookupino(next
);
749 panic("corrupted symbol table\n");
750 extractfile(myname(ep
));
764 struct entry
*np
, *ep
;
768 if ((ep
= lookupino(WINO
))) {
769 vprintf(stdout
, "Add whiteouts\n");
770 for ( ; ep
!= NULL
; ep
= ep
->e_links
) {
771 if ((ep
->e_flags
& NEW
) == 0)
773 addwhiteout(myname(ep
));
777 vprintf(stdout
, "Add links\n");
778 for (i
= ROOTINO
; i
< maxino
; i
++) {
782 for (np
= ep
->e_links
; np
!= NULL
; np
= np
->e_links
) {
783 if ((np
->e_flags
& NEW
) == 0)
785 strcpy(name
, myname(ep
));
786 if (ep
->e_type
== NODE
) {
787 linkit(name
, myname(np
), SYMLINK
);
789 linkit(name
, myname(np
), HARDLINK
);
797 * Check the symbol table.
798 * We do this to insure that all the requested work was done, and
799 * that no temporary names remain.
807 vprintf(stdout
, "Check the symbol table.\n");
808 for (i
= WINO
; i
< maxino
; i
++) {
809 for (ep
= lookupino(i
); ep
!= NULL
; ep
= ep
->e_links
) {
810 ep
->e_flags
&= ~KEEP
;
811 if (ep
->e_type
== NODE
)
812 ep
->e_flags
&= ~(NEW
|EXISTED
);
813 if (ep
->e_flags
!= 0)
814 badentry(ep
, "incomplete operations");
820 * Compare with the directory structure on the tape
821 * A paranoid check that things are as they should be.
824 verifyfile(char *name
, ufs1_ino_t ino
, int type
)
826 struct entry
*np
, *ep
;
829 ep
= lookupname(name
);
831 fprintf(stderr
, "Warning: missing name %s\n", name
);
837 for ( ; np
!= NULL
; np
= np
->e_links
)
841 panic("missing inumber %d\n", ino
);
842 if (ep
->e_type
== LEAF
&& type
!= LEAF
)
843 badentry(ep
, "type should be LEAF");