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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.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.15 2001/04/10 12:46:53 stelian Exp $";
48 #include <sys/types.h>
51 #include <sys/param.h>
53 #include <linux/ext2_fs.h>
54 #include <bsdcompat.h>
56 #include <ufs/ufs/dinode.h>
57 #endif /* __linux__ */
63 #include <ext2fs/ext2fs.h>
69 static char *keyval __P((int));
72 * This implements the 't' option.
73 * List entries on the tape.
76 listfile(char *name, dump_ino_t ino, int type)
78 long descend = hflag ? GOOD : FAIL;
84 if (TSTINO(ino, dumpmap) == 0)
86 Vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
88 if (tapeposflag) { /* add QFA positions to output */
89 (void)Inode2Tapepos(ino, &tnum, &tpos, 1);
90 fprintf(stdout, "%10lu\t%ld\t%ld\t%s\n", (unsigned long)ino,
95 fprintf(stdout, "%10lu\t%s\n", (unsigned long)ino, name);
100 * This implements the 'x' option.
101 * Request that new entries be extracted.
104 addfile(char *name, dump_ino_t ino, int type)
106 register struct entry *ep, *np;
107 long descend = hflag ? GOOD : FAIL;
110 if (TSTINO(ino, dumpmap) == 0) {
111 Dprintf(stdout, "%s: not on the tape\n", name);
114 if (ino == WINO && command == 'i' && !vflag)
117 (void) snprintf(buf, sizeof(buf), "./%lu", (unsigned long)ino);
120 (void) genliteraldir(name, ino);
126 if (strcmp(name, myname(ep)) == 0) {
131 for (np = ep->e_links; np; np = np->e_links)
132 if (strcmp(name, myname(np)) == 0) {
137 ep = addentry(name, ino, type);
145 * This is used by the 'i' option to undo previous requests made by addfile.
146 * Delete entries from the request queue.
150 deletefile(char *name, dump_ino_t ino, int type)
152 long descend = hflag ? GOOD : FAIL;
155 if (TSTINO(ino, dumpmap) == 0)
157 ep = lookupname(name);
160 ep->e_flags |= REMOVED;
161 if (ep->e_type != NODE)
168 * The following four routines implement the incremental
169 * restore algorithm. The first removes old entries, the second
170 * does renames and calculates the extraction list, the third
171 * cleans up link names missed by the first two, and the final
172 * one deletes old directories.
174 * Directories cannot be immediately deleted, as they may have
175 * other files in them which need to be moved out first. As
176 * directories to be deleted are found, they are put on the
177 * following deletion list. After all deletions and renames
178 * are done, this list is actually deleted.
180 static struct entry *removelist;
183 * Remove invalid whiteouts from the old tree.
184 * Remove unneeded leaves from the old tree.
185 * Remove directories from the lookup chains.
188 removeoldleaves(void)
190 register struct entry *ep, *nextep;
191 register dump_ino_t i, mydirino;
193 Vprintf(stdout, "Mark entries to be removed.\n");
194 if ((ep = lookupino(WINO))) {
195 Vprintf(stdout, "Delete whiteouts\n");
196 for ( ; ep != NULL; ep = nextep) {
197 nextep = ep->e_links;
198 mydirino = ep->e_parent->e_ino;
200 * We remove all whiteouts that are in directories
201 * that have been removed or that have been dumped.
203 if (TSTINO(mydirino, usedinomap) &&
204 !TSTINO(mydirino, dumpmap))
207 (void)fprintf(stderr, "BUG! Should call delwhiteout\n");
214 for (i = ROOTINO + 1; i < maxino; i++) {
218 if (TSTINO(i, usedinomap))
220 for ( ; ep != NULL; ep = ep->e_links) {
221 Dprintf(stdout, "%s: REMOVE\n", myname(ep));
222 if (ep->e_type == LEAF) {
227 deleteino(ep->e_ino);
228 ep->e_next = removelist;
236 * For each directory entry on the incremental tape, determine which
237 * category it falls into as follows:
238 * KEEP - entries that are to be left alone.
239 * NEW - new entries to be added.
240 * EXTRACT - files that must be updated with new contents.
241 * LINK - new links to be added.
242 * Renames are done at the same time.
245 nodeupdates(char *name, dump_ino_t ino, int type)
247 register struct entry *ep, *np, *ip;
252 # define ONTAPE 0x1 /* inode is on the tape */
253 # define INOFND 0x2 /* inode already exists */
254 # define NAMEFND 0x4 /* name already exists */
255 # define MODECHG 0x8 /* mode of inode changed */
258 * This routine is called once for each element in the
259 * directory hierarchy, with a full path name.
260 * The "type" value is incorrectly specified as LEAF for
261 * directories that are not on the dump tape.
263 * Check to see if the file is on the tape.
265 if (TSTINO(ino, dumpmap))
268 * Check to see if the name exists, and if the name is a link.
270 np = lookupname(name);
273 ip = lookupino(np->e_ino);
275 panic("corrupted symbol table\n");
280 * Check to see if the inode exists, and if one of its links
281 * corresponds to the name (if one was found).
286 for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
294 * If both a name and an inode are found, but they do not
295 * correspond to the same file, then both the inode that has
296 * been found and the inode corresponding to the name that
297 * has been found need to be renamed. The current pathname
298 * is the new name for the inode that has been found. Since
299 * all files to be deleted have already been removed, the
300 * named file is either a now unneeded link, or it must live
301 * under a new name in this dump level. If it is a link, it
302 * can be removed. If it is not a link, it is given a
303 * temporary name in anticipation that it will be renamed
304 * when it is later found by inode number.
306 if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
307 if (lookuptype == LINK) {
311 Dprintf(stdout, "name/inode conflict, mktempname %s\n",
318 if ((key & ONTAPE) &&
319 (((key & INOFND) && ip->e_type != type) ||
320 ((key & NAMEFND) && np->e_type != type)))
324 * Decide on the disposition of the file based on its flags.
325 * Note that we have already handled the case in which
326 * a name and inode are found that correspond to different files.
327 * Thus if both NAMEFND and INOFND are set then ip == np.
332 * A previously existing file has been found.
333 * Mark it as KEEP so that other links to the inode can be
334 * detected, and so that it will not be reclaimed by the search
335 * for unreferenced names.
339 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
344 * A file on the tape has a name which is the same as a name
345 * corresponding to a different file in the previous dump.
346 * Since all files to be deleted have already been removed,
347 * this file is either a now unneeded link, or it must live
348 * under a new name in this dump level. If it is a link, it
349 * can simply be removed. If it is not a link, it is given a
350 * temporary name in anticipation that it will be renamed
351 * when it is later found by inode number (see INOFND case
352 * below). The entry is then treated as a new file.
355 case ONTAPE|NAMEFND|MODECHG:
356 if (lookuptype == LINK) {
365 * A previously non-existent file.
366 * Add it to the file system, and request its extraction.
367 * If it is a directory, create it immediately.
368 * (Since the name is unused there can be no conflict)
371 ep = addentry(name, ino, type);
374 ep->e_flags |= NEW|KEEP;
375 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
380 * A file with the same inode number, but a different
381 * name has been found. If the other name has not already
382 * been found (indicated by the KEEP flag, see above) then
383 * this must be a new name for the file, and it is renamed.
384 * If the other name has been found then this must be a
385 * link to the file. Hard links to directories are not
386 * permitted, and are either deleted or converted to
387 * symbolic links. Finally, if the file is on the tape,
388 * a request is made to extract it.
391 if (type == LEAF && (ip->e_flags & KEEP) == 0)
392 ip->e_flags |= EXTRACT;
395 if ((ip->e_flags & KEEP) == 0) {
396 renameit(myname(ip), name);
399 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
403 if (ip->e_type == NODE) {
406 "deleted hard link %s to directory %s\n",
410 ep = addentry(name, ino, type|LINK);
412 Dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
417 * A previously known file which is to be updated. If it is a link,
418 * then all names referring to the previous file must be removed
419 * so that the subset of them that remain can be recreated.
421 case ONTAPE|INOFND|NAMEFND:
422 if (lookuptype == LINK) {
425 ep = addentry(name, ino, type|LINK);
428 ep->e_flags |= NEW|KEEP;
429 Dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
433 if (type == LEAF && lookuptype != LINK)
434 np->e_flags |= EXTRACT;
436 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
441 * An inode is being reused in a completely different way.
442 * Normally an extract can simply do an "unlink" followed
443 * by a "creat". Here we must do effectively the same
444 * thing. The complications arise because we cannot really
445 * delete a directory since it may still contain files
446 * that we need to rename, so we delete it from the symbol
447 * table, and put it on the list to be deleted eventually.
448 * Conversely if a directory is to be created, it must be
449 * done immediately, rather than waiting until the
452 case ONTAPE|INOFND|MODECHG:
453 case ONTAPE|INOFND|NAMEFND|MODECHG:
454 if (ip->e_flags & KEEP) {
455 badentry(ip, "cannot KEEP and change modes");
458 if (ip->e_type == LEAF) {
459 /* changing from leaf to node */
460 for ( ; ip != NULL; ip = ip->e_links) {
461 if (ip->e_type != LEAF)
462 badentry(ip, "NODE and LEAF links to same inode");
466 ip = addentry(name, ino, type);
469 /* changing from node to leaf */
470 if ((ip->e_flags & TMPNAME) == 0)
472 deleteino(ip->e_ino);
473 ip->e_next = removelist;
475 ip = addentry(name, ino, type);
477 ip->e_flags |= NEW|KEEP;
478 Dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
483 * A hard link to a directory that has been removed.
487 Dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
493 * If we find a directory entry for a file that is not on
494 * the tape, then we must have found a file that was created
495 * while the dump was in progress. Since we have no contents
496 * for it, we discard the name knowing that it will be on the
497 * next incremental tape.
500 if (compare_ignore_not_found) break;
501 fprintf(stderr, "%s: (inode %lu) not found on tape\n",
502 name, (unsigned long)ino);
507 * If any of these arise, something is grievously wrong with
508 * the current state of the symbol table.
510 case INOFND|NAMEFND|MODECHG:
511 case NAMEFND|MODECHG:
513 fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
518 * These states "cannot" arise for any state of the symbol table.
523 panic("[%s] %s: impossible state\n", keyval(key), name);
530 * Calculate the active flags in a key.
535 static char keybuf[32];
537 (void) strcpy(keybuf, "|NIL");
540 (void) strcat(keybuf, "|ONTAPE");
542 (void) strcat(keybuf, "|INOFND");
544 (void) strcat(keybuf, "|NAMEFND");
546 (void) strcat(keybuf, "|MODECHG");
551 * Find unreferenced link names.
556 register struct entry *ep, *np;
557 register dump_ino_t i;
559 Vprintf(stdout, "Find unreferenced names.\n");
560 for (i = ROOTINO; i < maxino; i++) {
562 if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
564 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
565 if (np->e_flags == 0) {
567 "%s: remove unreferenced name\n",
575 * Any leaves remaining in removed directories is unreferenced.
577 for (ep = removelist; ep != NULL; ep = ep->e_next) {
578 for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
579 if (np->e_type == LEAF) {
580 if (np->e_flags != 0)
581 badentry(np, "unreferenced with flags");
583 "%s: remove unreferenced name\n",
593 * Remove old nodes (directories).
594 * Note that this routine runs in O(N*D) where:
595 * N is the number of directory entries to be removed.
596 * D is the maximum depth of the tree.
597 * If N == D this can be quite slow. If the list were
598 * topologically sorted, the deletion could be done in
604 register struct entry *ep, **prev;
607 Vprintf(stdout, "Remove old nodes (directories).\n");
611 for (ep = removelist; ep != NULL; ep = *prev) {
612 if (ep->e_entries != NULL) {
622 for (ep = removelist; ep != NULL; ep = ep->e_next)
623 badentry(ep, "cannot remove, non-empty");
626 /* Compare the file specified in `ep' (which is on tape) to the */
627 /* current copy of this file on disk. If do_compare is 0, then just */
628 /* make our caller think we did it--this is used to handle hard links */
629 /* to files and devices. */
631 compare_entry(struct entry *ep, int do_compare)
633 if ((ep->e_flags & (NEW|EXTRACT)) == 0) {
634 badentry(ep, "unexpected file on tape");
637 if (do_compare) (void) comparefile(myname(ep));
638 ep->e_flags &= ~(NEW|EXTRACT);
642 * This is the routine used to compare files for the 'C' command.
647 register struct entry *ep;
651 first = lowerbnd(ROOTINO);
653 while (curfile.ino < maxino) {
654 first = lowerbnd(first);
656 * If the next available file is not the one which we
657 * expect then we have missed one or more files. Since
658 * we do not request files that were not on the tape,
659 * the lost files must have been due to a tape read error,
660 * or a file that was removed while the dump was in progress.
662 while (first < curfile.ino) {
663 ep = lookupino(first);
665 panic("%d: bad first\n", first);
666 fprintf(stderr, "%s: not found on tape\n", myname(ep));
668 ep->e_flags &= ~(NEW|EXTRACT);
669 first = lowerbnd(first);
672 * If we find files on the tape that have no corresponding
673 * directory entries, then we must have found a file that
674 * was created while the dump was in progress. Since we have
675 * no name for it, we discard it knowing that it will be
676 * on the next incremental tape.
678 if (first != curfile.ino) {
679 fprintf(stderr, "expected next file %ld, got %lu\n",
680 (long)first, (unsigned long)curfile.ino);
685 ep = lookupino(curfile.ino);
687 panic("unknown file on tape\n");
690 compare_entry(ep, 1);
691 for (ep = ep->e_links; ep != NULL; ep = ep->e_links) {
692 compare_entry(ep, 0);
696 * We checkpoint the restore after every tape reel, so
697 * as to simplify the amount of work re quired by the
701 if (curvol != volno) {
709 * This is the routine used to extract files for the 'r' command.
710 * Extract new leaves.
713 createleaves(char *symtabfile)
715 register struct entry *ep;
719 if (command == 'R') {
720 Vprintf(stdout, "Continue extraction of new leaves\n");
722 Vprintf(stdout, "Extract new leaves.\n");
723 dumpsymtable(symtabfile, volno);
725 first = lowerbnd(ROOTINO);
727 while (curfile.ino < maxino) {
728 first = lowerbnd(first);
730 * If the next available file is not the one which we
731 * expect then we have missed one or more files. Since
732 * we do not request files that were not on the tape,
733 * the lost files must have been due to a tape read error,
734 * or a file that was removed while the dump was in progress.
736 while (first < curfile.ino) {
737 ep = lookupino(first);
739 panic("%d: bad first\n", first);
740 fprintf(stderr, "%s: not found on tape\n", myname(ep));
741 ep->e_flags &= ~(NEW|EXTRACT);
742 first = lowerbnd(first);
745 * If we find files on the tape that have no corresponding
746 * directory entries, then we must have found a file that
747 * was created while the dump was in progress. Since we have
748 * no name for it, we discard it knowing that it will be
749 * on the next incremental tape.
751 if (first != curfile.ino) {
752 fprintf(stderr, "expected next file %ld, got %lu\n",
753 (long)first, (unsigned long)curfile.ino);
757 ep = lookupino(curfile.ino);
759 panic("unknown file on tape\n");
760 if ((ep->e_flags & (NEW|EXTRACT)) == 0)
761 badentry(ep, "unexpected file on tape");
763 * If the file is to be extracted, then the old file must
764 * be removed since its type may change from one leaf type
765 * to another (e.g. "file" to "character special").
767 if ((ep->e_flags & EXTRACT) != 0) {
769 ep->e_flags &= ~REMOVED;
771 (void) extractfile(myname(ep));
772 ep->e_flags &= ~(NEW|EXTRACT);
774 * We checkpoint the restore after every tape reel, so
775 * as to simplify the amount of work required by the
779 if (curvol != volno) {
780 dumpsymtable(symtabfile, volno);
788 * This is the routine used to extract files for the 'x' and 'i' commands.
789 * Efficiently extract a subset of the files on a tape.
794 register dump_ino_t first, next, last;
795 register struct entry *ep;
798 long tnum, tpos, curtpos, tmpcnt;
799 time_t tistart, tiend, titaken;
802 Vprintf(stdout, "Extract requested files\n");
803 curfile.action = SKIP;
807 first = lowerbnd(ROOTINO);
808 last = upperbnd(maxino - 1);
813 first = lowerbnd(first);
814 last = upperbnd(last);
816 * Check to see if any files remain to be extracted
821 * Reject any volumes with inodes greater
822 * than the last one needed
824 while (curfile.ino > last) {
825 curfile.action = SKIP;
831 * Decide on the next inode needed.
832 * Skip across the inodes until it is found
833 * or an out of order volume change is encountered
835 next = lowerbnd(curfile.ino);
837 tistart = time(NULL);
839 /* get tape position for inode (position directly) */
840 (void)Inode2Tapepos(next, &tnum, &tpos, 1);
842 /* get tape position for last available inode
843 * (position before) */
844 (void)Inode2Tapepos(next, &tnum, &tpos, 0);
847 (void)RequestVol(tnum);
848 if (GetTapePos(&curtpos) == 0) {
849 /* curtpos +1000 ???, some drives
850 * might be too slow */
851 if (tpos > curtpos) {
853 msg("positioning tape %ld from %ld to %ld for inode %10lu ...\n", volno, curtpos, tpos, (unsigned long)next);
855 if (GotoTapePos(tpos) == 0) {
857 if (GetTapePos(&curtpos) == 0)
858 msg("before resnyc at tape position %ld\n", curtpos);
860 (void)ReReadFromTape();
862 if (GetTapePos(&curtpos) == 0)
863 msg("after resync at tape position %ld\n", curtpos);
874 while (next > curfile.ino && volno == curvol) {
882 } while (volno == curvol + 1);
885 titaken = tiend - tistart;
887 if (titaken / 60 > 0)
888 msg("%ld reads took %d:%02d:%02d\n",
889 tmpcnt, titaken / 3600,
890 (titaken % 3600) / 60, titaken % 60);
895 * If volume change out of order occurred the
896 * current state must be recalculated
901 * If the current inode is greater than the one we were
902 * looking for then we missed the one we were looking for.
903 * Since we only attempt to extract files listed in the
904 * dump map, the lost files must have been due to a tape
905 * read error, or a file that was removed while the dump
906 * was in progress. Thus we report all requested files
907 * between the one we were looking for, and the one we
908 * found as missing, and delete their request flags.
910 while (next < curfile.ino) {
911 ep = lookupino(next);
913 panic("corrupted symbol table\n");
914 fprintf(stderr, "%s: not found on tape\n", myname(ep));
916 next = lowerbnd(next);
919 * The current inode is the one that we are looking for,
920 * so extract it per its requested name.
922 if (next == curfile.ino && next <= last) {
923 ep = lookupino(next);
925 panic("corrupted symbol table\n");
926 (void) extractfile(myname(ep));
940 register struct entry *np, *ep;
941 register dump_ino_t i;
944 if ((ep = lookupino(WINO))) {
945 Vprintf(stdout, "Add whiteouts\n");
946 for ( ; ep != NULL; ep = ep->e_links) {
947 if ((ep->e_flags & NEW) == 0)
950 (void)fprintf(stderr, "BUG! Should call addwhiteout\n");
952 (void) addwhiteout(myname(ep));
957 Vprintf(stdout, "Add links\n");
958 for (i = ROOTINO; i < maxino; i++) {
962 for (np = ep->e_links; np != NULL; np = np->e_links) {
963 if ((np->e_flags & NEW) == 0)
965 (void) strcpy(name, myname(ep));
966 if (ep->e_type == NODE) {
967 (void) linkit(name, myname(np), SYMLINK);
969 (void) linkit(name, myname(np), HARDLINK);
977 * Check the symbol table.
978 * We do this to insure that all the requested work was done, and
979 * that no temporary names remain.
984 register struct entry *ep;
985 register dump_ino_t i;
987 Vprintf(stdout, "Check the symbol table.\n");
988 for (i = WINO; i < maxino; i++) {
989 for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
990 ep->e_flags &= ~KEEP;
991 if (ep->e_type == NODE)
992 ep->e_flags &= ~(NEW|EXISTED);
993 if (ep->e_flags /* != NULL */)
994 badentry(ep, "incomplete operations");
1000 * Compare with the directory structure on the tape
1001 * A paranoid check that things are as they should be.
1004 verifyfile(char *name, dump_ino_t ino, int type)
1006 struct entry *np, *ep;
1007 long descend = GOOD;
1009 ep = lookupname(name);
1011 fprintf(stderr, "Warning: missing name %s\n", name);
1014 np = lookupino(ino);
1017 for ( ; np != NULL; np = np->e_links)
1021 panic("missing inumber %d\n", ino);
1022 if (ep->e_type == LEAF && type != LEAF)
1023 badentry(ep, "type should be LEAF");