1127664Sbms/*-
2172677Smlaier * SPDX-License-Identifier: BSD-3-Clause
3172677Smlaier *
4127664Sbms * Copyright (c) 1983, 1993
5127664Sbms *	The Regents of the University of California.  All rights reserved.
6127664Sbms *
7127664Sbms * Redistribution and use in source and binary forms, with or without
8127664Sbms * modification, are permitted provided that the following conditions
9127664Sbms * are met:
10127664Sbms * 1. Redistributions of source code must retain the above copyright
11127664Sbms *    notice, this list of conditions and the following disclaimer.
12127664Sbms * 2. Redistributions in binary form must reproduce the above copyright
13127664Sbms *    notice, this list of conditions and the following disclaimer in the
14127664Sbms *    documentation and/or other materials provided with the distribution.
15172677Smlaier * 3. Neither the name of the University nor the names of its contributors
16172677Smlaier *    may be used to endorse or promote products derived from this software
17172677Smlaier *    without specific prior written permission.
18172677Smlaier *
19127664Sbms * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20127664Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21127664Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22127664Sbms * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23127664Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24127664Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25127664Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26127664Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27127664Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28127664Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29127664Sbms * SUCH DAMAGE.
30127664Sbms */
31127664Sbms
32127664Sbms#include <sys/types.h>
33127664Sbms
34127664Sbms#include <limits.h>
35127664Sbms#include <stdint.h>
36214518Srpaulo#include <stdio.h>
37127664Sbms#include <string.h>
38127664Sbms
39127664Sbms#include <ufs/ufs/dinode.h>
40127664Sbms
41127664Sbms#include "restore.h"
42127664Sbms#include "extern.h"
43127664Sbms
44127664Sbmsstatic char *keyval(int);
45172677Smlaier
46127664Sbms/*
47127664Sbms * This implements the 't' option.
48127664Sbms * List entries on the tape.
49127664Sbms */
50127664Sbmslong
51127664Sbmslistfile(char *name, ino_t ino, int type)
52127664Sbms{
53127664Sbms	long descend = hflag ? GOOD : FAIL;
54127664Sbms
55127664Sbms	if (TSTINO(ino, dumpmap) == 0)
56127664Sbms		return (descend);
57127664Sbms	vprintf(stdout, "%s", type == LEAF ? "leaf" : "dir ");
58127664Sbms	fprintf(stdout, "%10ju\t%s\n", (uintmax_t)ino, name);
59127664Sbms	return (descend);
60127664Sbms}
61127664Sbms
62127664Sbms/*
63127664Sbms * This implements the 'x' option.
64127664Sbms * Request that new entries be extracted.
65127664Sbms */
66127664Sbmslong
67127664Sbmsaddfile(char *name, ino_t ino, int type)
68127664Sbms{
69127664Sbms	struct entry *ep;
70127664Sbms	long descend = hflag ? GOOD : FAIL;
71127664Sbms	char buf[100];
72127664Sbms
73127664Sbms	if (TSTINO(ino, dumpmap) == 0) {
74127664Sbms		dprintf(stdout, "%s: not on the tape\n", name);
75127664Sbms		return (descend);
76127664Sbms	}
77127664Sbms	if (ino == UFS_WINO && command == 'i' && !vflag)
78127664Sbms		return (descend);
79127664Sbms	if (!mflag) {
80127664Sbms		(void) sprintf(buf, "./%ju", (uintmax_t)ino);
81127664Sbms		name = buf;
82127664Sbms		if (type == NODE) {
83127664Sbms			(void) genliteraldir(name, ino);
84127664Sbms			return (descend);
85127664Sbms		}
86127664Sbms	}
87127664Sbms	ep = lookupino(ino);
88127664Sbms	if (ep != NULL) {
89127664Sbms		if (strcmp(name, myname(ep)) == 0) {
90127664Sbms			ep->e_flags |= NEW;
91127664Sbms			return (descend);
92127664Sbms		}
93127664Sbms		type |= LINK;
94127664Sbms	}
95127664Sbms	ep = addentry(name, ino, type);
96127664Sbms	if (type == NODE)
97127664Sbms		newnode(ep);
98127664Sbms	ep->e_flags |= NEW;
99127664Sbms	return (descend);
100127664Sbms}
101127664Sbms
102127664Sbms/*
103127664Sbms * This is used by the 'i' option to undo previous requests made by addfile.
104127664Sbms * Delete entries from the request queue.
105127664Sbms */
106127664Sbms/* ARGSUSED */
107127664Sbmslong
108127664Sbmsdeletefile(char *name, ino_t ino, int type)
109127664Sbms{
110127664Sbms	long descend = hflag ? GOOD : FAIL;
111127664Sbms	struct entry *ep;
112127664Sbms
113127664Sbms	if (TSTINO(ino, dumpmap) == 0)
114127664Sbms		return (descend);
115127664Sbms	ep = lookupname(name);
116127664Sbms	if (ep != NULL) {
117127664Sbms		ep->e_flags &= ~NEW;
118127664Sbms		ep->e_flags |= REMOVED;
119127664Sbms		if (ep->e_type != NODE)
120127664Sbms			freeentry(ep);
121127664Sbms	}
122127664Sbms	return (descend);
123127664Sbms}
124127664Sbms
125127664Sbms/*
126127664Sbms * The following four routines implement the incremental
127127664Sbms * restore algorithm. The first removes old entries, the second
128127664Sbms * does renames and calculates the extraction list, the third
129127664Sbms * cleans up link names missed by the first two, and the final
130127664Sbms * one deletes old directories.
131127664Sbms *
132127664Sbms * Directories cannot be immediately deleted, as they may have
133127664Sbms * other files in them which need to be moved out first. As
134127664Sbms * directories to be deleted are found, they are put on the
135127664Sbms * following deletion list. After all deletions and renames
136127664Sbms * are done, this list is actually deleted.
137127664Sbms */
138127664Sbmsstatic struct entry *removelist;
139127664Sbms
140127664Sbms/*
141127664Sbms *	Remove invalid whiteouts from the old tree.
142127664Sbms *	Remove unneeded leaves from the old tree.
143127664Sbms *	Remove directories from the lookup chains.
144127664Sbms */
145127664Sbmsvoid
146127664Sbmsremoveoldleaves(void)
147127664Sbms{
148127664Sbms	struct entry *ep, *nextep;
149127664Sbms	ino_t i, mydirino;
150127664Sbms
151127664Sbms	vprintf(stdout, "Mark entries to be removed.\n");
152127664Sbms	if ((ep = lookupino(UFS_WINO))) {
153127664Sbms		vprintf(stdout, "Delete whiteouts\n");
154127664Sbms		for ( ; ep != NULL; ep = nextep) {
155127664Sbms			nextep = ep->e_links;
156127664Sbms			mydirino = ep->e_parent->e_ino;
157127664Sbms			/*
158127664Sbms			 * We remove all whiteouts that are in directories
159146768Ssam			 * that have been removed or that have been dumped.
160127664Sbms			 */
161127664Sbms			if (TSTINO(mydirino, usedinomap) &&
162127664Sbms			    !TSTINO(mydirino, dumpmap))
163127664Sbms				continue;
164127664Sbms			delwhiteout(ep);
165127664Sbms			freeentry(ep);
166127664Sbms		}
167127664Sbms	}
168127664Sbms	for (i = UFS_ROOTINO + 1; i < maxino; i++) {
169127664Sbms		ep = lookupino(i);
170127664Sbms		if (ep == NULL)
171127664Sbms			continue;
172127664Sbms		if (TSTINO(i, usedinomap))
173127664Sbms			continue;
174127664Sbms		for ( ; ep != NULL; ep = ep->e_links) {
175127664Sbms			dprintf(stdout, "%s: REMOVE\n", myname(ep));
176127664Sbms			if (ep->e_type == LEAF) {
177127664Sbms				removeleaf(ep);
178127664Sbms				freeentry(ep);
179127664Sbms			} else {
180127664Sbms				mktempname(ep);
181127664Sbms				deleteino(ep->e_ino);
182127664Sbms				ep->e_next = removelist;
183127664Sbms				removelist = ep;
184127664Sbms			}
185127664Sbms		}
186127664Sbms	}
187127664Sbms}
188127664Sbms
189127664Sbms/*
190127664Sbms *	For each directory entry on the incremental tape, determine which
191127664Sbms *	category it falls into as follows:
192127664Sbms *	KEEP - entries that are to be left alone.
193127664Sbms *	NEW - new entries to be added.
194127664Sbms *	EXTRACT - files that must be updated with new contents.
195127664Sbms *	LINK - new links to be added.
196127664Sbms *	Renames are done at the same time.
197127664Sbms */
198127664Sbmslong
199127664Sbmsnodeupdates(char *name, ino_t ino, int type)
200127664Sbms{
201127664Sbms	struct entry *ep, *np, *ip;
202127664Sbms	long descend = GOOD;
203127664Sbms	int lookuptype = 0;
204127664Sbms	int key = 0;
205127664Sbms		/* key values */
206127664Sbms#		define ONTAPE	0x1	/* inode is on the tape */
207127664Sbms#		define INOFND	0x2	/* inode already exists */
208127664Sbms#		define NAMEFND	0x4	/* name already exists */
209127664Sbms#		define MODECHG	0x8	/* mode of inode changed */
210127664Sbms
211127664Sbms	/*
212127664Sbms	 * This routine is called once for each element in the
213127664Sbms	 * directory hierarchy, with a full path name.
214127664Sbms	 * The "type" value is incorrectly specified as LEAF for
215127664Sbms	 * directories that are not on the dump tape.
216127664Sbms	 *
217127664Sbms	 * Check to see if the file is on the tape.
218127664Sbms	 */
219127664Sbms	if (TSTINO(ino, dumpmap))
220127664Sbms		key |= ONTAPE;
221127664Sbms	/*
222127664Sbms	 * Check to see if the name exists, and if the name is a link.
223127664Sbms	 */
224146768Ssam	np = lookupname(name);
225146768Ssam	if (np != NULL) {
226127664Sbms		key |= NAMEFND;
227127664Sbms		ip = lookupino(np->e_ino);
228172677Smlaier		if (ip == NULL)
229162012Ssam			panic("corrupted symbol table\n");
230172677Smlaier		if (ip != np)
231172677Smlaier			lookuptype = LINK;
232172677Smlaier	}
233172677Smlaier	/*
234172677Smlaier	 * Check to see if the inode exists, and if one of its links
235172677Smlaier	 * corresponds to the name (if one was found).
236172677Smlaier	 */
237172677Smlaier	ip = lookupino(ino);
238172677Smlaier	if (ip != NULL) {
239162012Ssam		key |= INOFND;
240146768Ssam		for (ep = ip->e_links; ep != NULL; ep = ep->e_links) {
241146768Ssam			if (ep == np) {
242146768Ssam				ip = ep;
243146768Ssam				break;
244146768Ssam			}
245146768Ssam		}
246146768Ssam	}
247146768Ssam	/*
248146768Ssam	 * If both a name and an inode are found, but they do not
249146768Ssam	 * correspond to the same file, then both the inode that has
250146768Ssam	 * been found and the inode corresponding to the name that
251146768Ssam	 * has been found need to be renamed. The current pathname
252146768Ssam	 * is the new name for the inode that has been found. Since
253146768Ssam	 * all files to be deleted have already been removed, the
254127664Sbms	 * named file is either a now unneeded link, or it must live
255127664Sbms	 * under a new name in this dump level. If it is a link, it
256127664Sbms	 * can be removed. If it is not a link, it is given a
257127664Sbms	 * temporary name in anticipation that it will be renamed
258146768Ssam	 * when it is later found by inode number.
259127664Sbms	 */
260127664Sbms	if (((key & (INOFND|NAMEFND)) == (INOFND|NAMEFND)) && ip != np) {
261127664Sbms		if (lookuptype == LINK) {
262127664Sbms			removeleaf(np);
263127664Sbms			freeentry(np);
264127664Sbms		} else {
265127664Sbms			dprintf(stdout, "name/inode conflict, mktempname %s\n",
266127664Sbms				myname(np));
267127664Sbms			mktempname(np);
268127664Sbms		}
269127664Sbms		np = NULL;
270127664Sbms		key &= ~NAMEFND;
271127664Sbms	}
272127664Sbms	if ((key & ONTAPE) &&
273127664Sbms	  (((key & INOFND) && ip->e_type != type) ||
274127664Sbms	   ((key & NAMEFND) && np->e_type != type)))
275127664Sbms		key |= MODECHG;
276127664Sbms
277127664Sbms	/*
278127664Sbms	 * Decide on the disposition of the file based on its flags.
279127664Sbms	 * Note that we have already handled the case in which
280127664Sbms	 * a name and inode are found that correspond to different files.
281127664Sbms	 * Thus if both NAMEFND and INOFND are set then ip == np.
282127664Sbms	 */
283127664Sbms	switch (key) {
284127664Sbms
285127664Sbms	/*
286127664Sbms	 * A previously existing file has been found.
287127664Sbms	 * Mark it as KEEP so that other links to the inode can be
288127664Sbms	 * detected, and so that it will not be reclaimed by the search
289127664Sbms	 * for unreferenced names.
290127664Sbms	 */
291146768Ssam	case INOFND|NAMEFND:
292146768Ssam		ip->e_flags |= KEEP;
293146768Ssam		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
294146768Ssam			flagvalues(ip));
295127664Sbms		break;
296146768Ssam
297146768Ssam	/*
298127664Sbms	 * A file on the tape has a name which is the same as a name
299127664Sbms	 * corresponding to a different file in the previous dump.
300127664Sbms	 * Since all files to be deleted have already been removed,
301127664Sbms	 * this file is either a now unneeded link, or it must live
302127664Sbms	 * under a new name in this dump level. If it is a link, it
303127664Sbms	 * can simply be removed. If it is not a link, it is given a
304190225Srpaulo	 * temporary name in anticipation that it will be renamed
305190225Srpaulo	 * when it is later found by inode number (see INOFND case
306190225Srpaulo	 * below). The entry is then treated as a new file.
307190225Srpaulo	 */
308190225Srpaulo	case ONTAPE|NAMEFND:
309190225Srpaulo	case ONTAPE|NAMEFND|MODECHG:
310190225Srpaulo		if (lookuptype == LINK) {
311190225Srpaulo			removeleaf(np);
312190225Srpaulo			freeentry(np);
313127664Sbms		} else {
314127664Sbms			mktempname(np);
315146768Ssam		}
316146768Ssam		/* FALLTHROUGH */
317146768Ssam
318146768Ssam	/*
319127664Sbms	 * A previously non-existent file.
320127664Sbms	 * Add it to the file system, and request its extraction.
321127664Sbms	 * If it is a directory, create it immediately.
322127664Sbms	 * (Since the name is unused there can be no conflict)
323127664Sbms	 */
324127664Sbms	case ONTAPE:
325146768Ssam		ep = addentry(name, ino, type);
326127664Sbms		if (type == NODE)
327127664Sbms			newnode(ep);
328		ep->e_flags |= NEW|KEEP;
329		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
330			flagvalues(ep));
331		break;
332
333	/*
334	 * A file with the same inode number, but a different
335	 * name has been found. If the other name has not already
336	 * been found (indicated by the KEEP flag, see above) then
337	 * this must be a new name for the file, and it is renamed.
338	 * If the other name has been found then this must be a
339	 * link to the file. Hard links to directories are not
340	 * permitted, and are either deleted or converted to
341	 * symbolic links. Finally, if the file is on the tape,
342	 * a request is made to extract it.
343	 */
344	case ONTAPE|INOFND:
345		if (type == LEAF && (ip->e_flags & KEEP) == 0)
346			ip->e_flags |= EXTRACT;
347		/* FALLTHROUGH */
348	case INOFND:
349		if ((ip->e_flags & KEEP) == 0) {
350			renameit(myname(ip), name);
351			moveentry(ip, name);
352			ip->e_flags |= KEEP;
353			dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
354				flagvalues(ip));
355			break;
356		}
357		if (ip->e_type == NODE) {
358			descend = FAIL;
359			fprintf(stderr,
360				"deleted hard link %s to directory %s\n",
361				name, myname(ip));
362			break;
363		}
364		ep = addentry(name, ino, type|LINK);
365		ep->e_flags |= NEW;
366		dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
367			flagvalues(ep));
368		break;
369
370	/*
371	 * A previously known file which is to be updated. If it is a link,
372	 * then all names referring to the previous file must be removed
373	 * so that the subset of them that remain can be recreated.
374	 */
375	case ONTAPE|INOFND|NAMEFND:
376		if (lookuptype == LINK) {
377			removeleaf(np);
378			freeentry(np);
379			ep = addentry(name, ino, type|LINK);
380			if (type == NODE)
381			        newnode(ep);
382			ep->e_flags |= NEW|KEEP;
383			dprintf(stdout, "[%s] %s: %s|LINK\n", keyval(key), name,
384				flagvalues(ep));
385			break;
386		}
387		if (type == LEAF && lookuptype != LINK)
388			np->e_flags |= EXTRACT;
389		np->e_flags |= KEEP;
390		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
391			flagvalues(np));
392		break;
393
394	/*
395	 * An inode is being reused in a completely different way.
396	 * Normally an extract can simply do an "unlink" followed
397	 * by a "creat". Here we must do effectively the same
398	 * thing. The complications arise because we cannot really
399	 * delete a directory since it may still contain files
400	 * that we need to rename, so we delete it from the symbol
401	 * table, and put it on the list to be deleted eventually.
402	 * Conversely if a directory is to be created, it must be
403	 * done immediately, rather than waiting until the
404	 * extraction phase.
405	 */
406	case ONTAPE|INOFND|MODECHG:
407	case ONTAPE|INOFND|NAMEFND|MODECHG:
408		if (ip->e_flags & KEEP) {
409			badentry(ip, "cannot KEEP and change modes");
410			break;
411		}
412		if (ip->e_type == LEAF) {
413			/* changing from leaf to node */
414			for (ip = lookupino(ino); ip != NULL; ip = ip->e_links) {
415				if (ip->e_type != LEAF)
416					badentry(ip, "NODE and LEAF links to same inode");
417				removeleaf(ip);
418				freeentry(ip);
419			}
420			ip = addentry(name, ino, type);
421			newnode(ip);
422		} else {
423			/* changing from node to leaf */
424			if ((ip->e_flags & TMPNAME) == 0)
425				mktempname(ip);
426			deleteino(ip->e_ino);
427			ip->e_next = removelist;
428			removelist = ip;
429			ip = addentry(name, ino, type);
430		}
431		ip->e_flags |= NEW|KEEP;
432		dprintf(stdout, "[%s] %s: %s\n", keyval(key), name,
433			flagvalues(ip));
434		break;
435
436	/*
437	 * A hard link to a directory that has been removed.
438	 * Ignore it.
439	 */
440	case NAMEFND:
441		dprintf(stdout, "[%s] %s: Extraneous name\n", keyval(key),
442			name);
443		descend = FAIL;
444		break;
445
446	/*
447	 * If we find a directory entry for a file that is not on
448	 * the tape, then we must have found a file that was created
449	 * while the dump was in progress. Since we have no contents
450	 * for it, we discard the name knowing that it will be on the
451	 * next incremental tape.
452	 */
453	case 0:
454		fprintf(stderr, "%s: (inode %ju) not found on tape\n",
455		    name, (uintmax_t)ino);
456		break;
457
458	/*
459	 * If any of these arise, something is grievously wrong with
460	 * the current state of the symbol table.
461	 */
462	case INOFND|NAMEFND|MODECHG:
463	case NAMEFND|MODECHG:
464	case INOFND|MODECHG:
465		fprintf(stderr, "[%s] %s: inconsistent state\n", keyval(key),
466			name);
467		break;
468
469	/*
470	 * These states "cannot" arise for any state of the symbol table.
471	 */
472	case ONTAPE|MODECHG:
473	case MODECHG:
474	default:
475		panic("[%s] %s: impossible state\n", keyval(key), name);
476		break;
477	}
478	return (descend);
479}
480
481/*
482 * Calculate the active flags in a key.
483 */
484static char *
485keyval(int key)
486{
487	static char keybuf[32];
488
489	(void) strcpy(keybuf, "|NIL");
490	keybuf[0] = '\0';
491	if (key & ONTAPE)
492		(void) strcat(keybuf, "|ONTAPE");
493	if (key & INOFND)
494		(void) strcat(keybuf, "|INOFND");
495	if (key & NAMEFND)
496		(void) strcat(keybuf, "|NAMEFND");
497	if (key & MODECHG)
498		(void) strcat(keybuf, "|MODECHG");
499	return (&keybuf[1]);
500}
501
502/*
503 * Find unreferenced link names.
504 */
505void
506findunreflinks(void)
507{
508	struct entry *ep, *np;
509	ino_t i;
510
511	vprintf(stdout, "Find unreferenced names.\n");
512	for (i = UFS_ROOTINO; i < maxino; i++) {
513		ep = lookupino(i);
514		if (ep == NULL || ep->e_type == LEAF || TSTINO(i, dumpmap) == 0)
515			continue;
516		for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
517			if (np->e_flags == 0) {
518				dprintf(stdout,
519				    "%s: remove unreferenced name\n",
520				    myname(np));
521				removeleaf(np);
522				freeentry(np);
523			}
524		}
525	}
526	/*
527	 * Any leaves remaining in removed directories is unreferenced.
528	 */
529	for (ep = removelist; ep != NULL; ep = ep->e_next) {
530		for (np = ep->e_entries; np != NULL; np = np->e_sibling) {
531			if (np->e_type == LEAF) {
532				if (np->e_flags != 0)
533					badentry(np, "unreferenced with flags");
534				dprintf(stdout,
535				    "%s: remove unreferenced name\n",
536				    myname(np));
537				removeleaf(np);
538				freeentry(np);
539			}
540		}
541	}
542}
543
544/*
545 * Remove old nodes (directories).
546 * Note that this routine runs in O(N*D) where:
547 *	N is the number of directory entries to be removed.
548 *	D is the maximum depth of the tree.
549 * If N == D this can be quite slow. If the list were
550 * topologically sorted, the deletion could be done in
551 * time O(N).
552 */
553void
554removeoldnodes(void)
555{
556	struct entry *ep, **prev;
557	long change;
558
559	vprintf(stdout, "Remove old nodes (directories).\n");
560	do	{
561		change = 0;
562		prev = &removelist;
563		for (ep = removelist; ep != NULL; ep = *prev) {
564			if (ep->e_entries != NULL) {
565				prev = &ep->e_next;
566				continue;
567			}
568			*prev = ep->e_next;
569			removenode(ep);
570			freeentry(ep);
571			change++;
572		}
573	} while (change);
574	for (ep = removelist; ep != NULL; ep = ep->e_next)
575		badentry(ep, "cannot remove, non-empty");
576}
577
578/*
579 * This is the routine used to extract files for the 'r' command.
580 * Extract new leaves.
581 */
582void
583createleaves(char *symtabfile)
584{
585	struct entry *ep;
586	ino_t first;
587	long curvol;
588
589	if (command == 'R') {
590		vprintf(stdout, "Continue extraction of new leaves\n");
591	} else {
592		vprintf(stdout, "Extract new leaves.\n");
593		dumpsymtable(symtabfile, volno);
594	}
595	first = lowerbnd(UFS_ROOTINO);
596	curvol = volno;
597	while (curfile.ino < maxino) {
598		first = lowerbnd(first);
599		/*
600		 * If the next available file is not the one which we
601		 * expect then we have missed one or more files. Since
602		 * we do not request files that were not on the tape,
603		 * the lost files must have been due to a tape read error,
604		 * or a file that was removed while the dump was in progress.
605		 */
606		while (first < curfile.ino) {
607			ep = lookupino(first);
608			if (ep == NULL)
609				panic("%ju: bad first\n", (uintmax_t)first);
610			fprintf(stderr, "%s: not found on tape\n", myname(ep));
611			ep->e_flags &= ~(NEW|EXTRACT);
612			first = lowerbnd(first);
613		}
614		/*
615		 * If we find files on the tape that have no corresponding
616		 * directory entries, then we must have found a file that
617		 * was created while the dump was in progress. Since we have
618		 * no name for it, we discard it knowing that it will be
619		 * on the next incremental tape.
620		 */
621		if (first != curfile.ino) {
622			fprintf(stderr, "expected next file %ju, got %ju\n",
623			    (uintmax_t)first, (uintmax_t)curfile.ino);
624			skipfile();
625			goto next;
626		}
627		ep = lookupino(curfile.ino);
628		if (ep == NULL)
629			panic("unknown file on tape\n");
630		if ((ep->e_flags & (NEW|EXTRACT)) == 0)
631			badentry(ep, "unexpected file on tape");
632		/*
633		 * If the file is to be extracted, then the old file must
634		 * be removed since its type may change from one leaf type
635		 * to another (e.g. "file" to "character special").
636		 */
637		if ((ep->e_flags & EXTRACT) != 0) {
638			removeleaf(ep);
639			ep->e_flags &= ~REMOVED;
640		}
641		(void) extractfile(myname(ep));
642		ep->e_flags &= ~(NEW|EXTRACT);
643		/*
644		 * We checkpoint the restore after every tape reel, so
645		 * as to simplify the amount of work required by the
646		 * 'R' command.
647		 */
648	next:
649		if (curvol != volno) {
650			dumpsymtable(symtabfile, volno);
651			skipmaps();
652			curvol = volno;
653		}
654	}
655}
656
657/*
658 * This is the routine used to extract files for the 'x' and 'i' commands.
659 * Efficiently extract a subset of the files on a tape.
660 */
661void
662createfiles(void)
663{
664	ino_t first, next, last;
665	struct entry *ep;
666	long curvol;
667
668	vprintf(stdout, "Extract requested files\n");
669	curfile.action = SKIP;
670	getvol((long)1);
671	skipmaps();
672	skipdirs();
673	first = lowerbnd(UFS_ROOTINO);
674	last = upperbnd(maxino - 1);
675	for (;;) {
676		curvol = volno;
677		first = lowerbnd(first);
678		last = upperbnd(last);
679		/*
680		 * Check to see if any files remain to be extracted
681		 */
682		if (first > last)
683			return;
684		if (Dflag) {
685			if (curfile.ino == maxino)
686				return;
687			if((ep = lookupino(curfile.ino)) != NULL &&
688			    (ep->e_flags & (NEW|EXTRACT))) {
689				goto justgetit;
690			} else {
691				skipfile();
692				continue;
693			}
694		}
695		/*
696		 * Reject any volumes with inodes greater than the last
697		 * one needed, so that we can quickly skip backwards to
698		 * a volume containing useful inodes. We can't do this
699		 * if there are no further volumes available (curfile.ino
700		 * >= maxino) or if we are already at the first tape.
701		 */
702		if (curfile.ino > last && curfile.ino < maxino && volno > 1) {
703			curfile.action = SKIP;
704			getvol((long)0);
705			skipmaps();
706			skipdirs();
707			continue;
708		}
709		/*
710		 * Decide on the next inode needed.
711		 * Skip across the inodes until it is found
712		 * or a volume change is encountered
713		 */
714		if (curfile.ino < maxino) {
715			next = lowerbnd(curfile.ino);
716			while (next > curfile.ino && volno == curvol)
717				skipfile();
718			if (volno != curvol) {
719				skipmaps();
720				skipdirs();
721				continue;
722			}
723		} else {
724			/*
725			 * No further volumes or inodes available. Set
726			 * `next' to the first inode, so that a warning
727			 * is emitted below for each missing file.
728			 */
729			next = first;
730		}
731		/*
732		 * If the current inode is greater than the one we were
733		 * looking for then we missed the one we were looking for.
734		 * Since we only attempt to extract files listed in the
735		 * dump map, the lost files must have been due to a tape
736		 * read error, or a file that was removed while the dump
737		 * was in progress. Thus we report all requested files
738		 * between the one we were looking for, and the one we
739		 * found as missing, and delete their request flags.
740		 */
741		while (next < curfile.ino) {
742			ep = lookupino(next);
743			if (ep == NULL)
744				panic("corrupted symbol table\n");
745			fprintf(stderr, "%s: not found on tape\n", myname(ep));
746			ep->e_flags &= ~NEW;
747			next = lowerbnd(next);
748		}
749		/*
750		 * The current inode is the one that we are looking for,
751		 * so extract it per its requested name.
752		 */
753		if (next == curfile.ino && next <= last) {
754			ep = lookupino(next);
755			if (ep == NULL)
756				panic("corrupted symbol table\n");
757justgetit:
758			(void) extractfile(myname(ep));
759			ep->e_flags &= ~NEW;
760			if (volno != curvol)
761				skipmaps();
762		}
763	}
764}
765
766/*
767 * Add links.
768 */
769void
770createlinks(void)
771{
772	struct entry *np, *ep;
773	ino_t i;
774	char name[BUFSIZ];
775
776	if ((ep = lookupino(UFS_WINO))) {
777		vprintf(stdout, "Add whiteouts\n");
778		for ( ; ep != NULL; ep = ep->e_links) {
779			if ((ep->e_flags & NEW) == 0)
780				continue;
781			(void) addwhiteout(myname(ep));
782			ep->e_flags &= ~NEW;
783		}
784	}
785	vprintf(stdout, "Add links\n");
786	for (i = UFS_ROOTINO; i < maxino; i++) {
787		ep = lookupino(i);
788		if (ep == NULL)
789			continue;
790		for (np = ep->e_links; np != NULL; np = np->e_links) {
791			if ((np->e_flags & NEW) == 0)
792				continue;
793			(void) strcpy(name, myname(ep));
794			if (ep->e_type == NODE) {
795				(void) linkit(name, myname(np), SYMLINK);
796			} else {
797				(void) linkit(name, myname(np), HARDLINK);
798			}
799			np->e_flags &= ~NEW;
800		}
801	}
802}
803
804/*
805 * Check the symbol table.
806 * We do this to insure that all the requested work was done, and
807 * that no temporary names remain.
808 */
809void
810checkrestore(void)
811{
812	struct entry *ep;
813	ino_t i;
814
815	vprintf(stdout, "Check the symbol table.\n");
816	for (i = UFS_WINO; i < maxino; i++) {
817		for (ep = lookupino(i); ep != NULL; ep = ep->e_links) {
818			ep->e_flags &= ~KEEP;
819			if (ep->e_type == NODE)
820				ep->e_flags &= ~(NEW|EXISTED);
821			if (ep->e_flags != 0)
822				badentry(ep, "incomplete operations");
823		}
824	}
825}
826
827/*
828 * Compare with the directory structure on the tape
829 * A paranoid check that things are as they should be.
830 */
831long
832verifyfile(char *name, ino_t ino, int type)
833{
834	struct entry *np, *ep;
835	long descend = GOOD;
836
837	ep = lookupname(name);
838	if (ep == NULL) {
839		fprintf(stderr, "Warning: missing name %s\n", name);
840		return (FAIL);
841	}
842	np = lookupino(ino);
843	if (np != ep)
844		descend = FAIL;
845	for ( ; np != NULL; np = np->e_links)
846		if (np == ep)
847			break;
848	if (np == NULL)
849		panic("missing inumber %ju\n", (uintmax_t)ino);
850	if (ep->e_type == LEAF && type != LEAF)
851		badentry(ep, "type should be LEAF");
852	return (descend);
853}
854