xref: /freebsd/sbin/fsck_ffs/pass1.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char sccsid[] = "@(#)pass1.c	8.6 (Berkeley) 4/28/95";
35 #endif /* not lint */
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/sysctl.h>
43 
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
47 
48 #include <err.h>
49 #include <limits.h>
50 #include <stdint.h>
51 #include <string.h>
52 
53 #include "fsck.h"
54 
55 static ufs2_daddr_t badblk;
56 static ufs2_daddr_t dupblk;
57 static ino_t lastino;		/* last inode in use */
58 
59 static int checkinode(ino_t inumber, struct inodesc *, int rebuiltcg);
60 
61 void
62 pass1(void)
63 {
64 	struct inostat *info;
65 	struct inodesc idesc;
66 	struct bufarea *cgbp;
67 	struct cg *cgp;
68 	ino_t inumber, inosused, mininos;
69 	ufs2_daddr_t i, cgd;
70 	u_int8_t *cp;
71 	int c, rebuiltcg;
72 
73 	badblk = dupblk = lastino = 0;
74 
75 	/*
76 	 * Set file system reserved blocks in used block map.
77 	 */
78 	for (c = 0; c < sblock.fs_ncg; c++) {
79 		cgd = cgdmin(&sblock, c);
80 		if (c == 0) {
81 			i = cgbase(&sblock, c);
82 		} else
83 			i = cgsblock(&sblock, c);
84 		for (; i < cgd; i++)
85 			setbmap(i);
86 	}
87 	i = sblock.fs_csaddr;
88 	cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
89 	for (; i < cgd; i++)
90 		setbmap(i);
91 
92 	/*
93 	 * Find all allocated blocks.
94 	 */
95 	memset(&idesc, 0, sizeof(struct inodesc));
96 	idesc.id_func = pass1check;
97 	n_files = n_blks = 0;
98 	for (c = 0; c < sblock.fs_ncg; c++) {
99 		inumber = c * sblock.fs_ipg;
100 		cgbp = cglookup(c);
101 		cgp = cgbp->b_un.b_cg;
102 		rebuiltcg = 0;
103 		if (!check_cgmagic(c, cgbp)) {
104 			if (!reply("REBUILD CYLINDER GROUP")) {
105 				cgheader_corrupt = 1;
106 				if (!nflag) {
107 					printf("YOU WILL NEED TO RERUN FSCK.\n");
108 					rerun = 1;
109 				}
110 			} else {
111 				rebuild_cg(c, cgbp);
112 				rebuiltcg = 1;
113 			}
114 		}
115 		if (!rebuiltcg && sblock.fs_magic == FS_UFS2_MAGIC) {
116 			inosused = cgp->cg_initediblk;
117 			if (inosused > sblock.fs_ipg) {
118 				pfatal("Too many initialized inodes (%ju > %d) "
119 				    "in cylinder group %d\nReset to %d\n",
120 				    (uintmax_t)inosused, sblock.fs_ipg, c,
121 				    sblock.fs_ipg);
122 				inosused = sblock.fs_ipg;
123 			}
124 		} else {
125 			inosused = sblock.fs_ipg;
126 		}
127 		if (got_siginfo) {
128 			printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
129 			    cdevname, c, sblock.fs_ncg,
130 			    c * 100 / sblock.fs_ncg);
131 			got_siginfo = 0;
132 		}
133 		if (got_sigalarm) {
134 			setproctitle("%s p1 %d%%", cdevname,
135 			     c * 100 / sblock.fs_ncg);
136 			got_sigalarm = 0;
137 		}
138 		/*
139 		 * If we are using soft updates, then we can trust the
140 		 * cylinder group inode allocation maps to tell us which
141 		 * inodes are allocated. We will scan the used inode map
142 		 * to find the inodes that are really in use, and then
143 		 * read only those inodes in from disk.
144 		 */
145 		if ((preen || inoopt) && usedsoftdep && !rebuiltcg) {
146 			cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
147 			for ( ; inosused != 0; cp--) {
148 				if (*cp == 0) {
149 					if (inosused > CHAR_BIT)
150 						inosused -= CHAR_BIT;
151 					else
152 						inosused = 0;
153 					continue;
154 				}
155 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
156 					if (*cp & i)
157 						break;
158 					inosused--;
159 				}
160 				break;
161 			}
162 		}
163 		/*
164 		 * Allocate inoinfo structures for the allocated inodes.
165 		 */
166 		inostathead[c].il_numalloced = inosused;
167 		if (inosused == 0) {
168 			inostathead[c].il_stat = NULL;
169 			continue;
170 		}
171 		info = Calloc((unsigned)inosused, sizeof(struct inostat));
172 		if (info == NULL)
173 			errx(EEXIT, "cannot alloc %u bytes for inoinfo",
174 			    (unsigned)(sizeof(struct inostat) * inosused));
175 		inostathead[c].il_stat = info;
176 		/*
177 		 * Scan the allocated inodes.
178 		 */
179 		setinodebuf(c, inosused);
180 		for (i = 0; i < inosused; i++, inumber++) {
181 			if (inumber < UFS_ROOTINO) {
182 				(void)getnextinode(inumber, rebuiltcg);
183 				continue;
184 			}
185 			/*
186 			 * NULL return indicates probable end of allocated
187 			 * inodes during cylinder group rebuild attempt.
188 			 * We always keep trying until we get to the minimum
189 			 * valid number for this cylinder group.
190 			 */
191 			if (checkinode(inumber, &idesc, rebuiltcg) == 0 &&
192 			    i > cgp->cg_initediblk)
193 				break;
194 		}
195 		/*
196 		 * This optimization speeds up future runs of fsck
197 		 * by trimming down the number of inodes in cylinder
198 		 * groups that formerly had many inodes but now have
199 		 * fewer in use.
200 		 */
201 		mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
202 		if (inoopt && !preen && !rebuiltcg &&
203 		    sblock.fs_magic == FS_UFS2_MAGIC &&
204 		    cgp->cg_initediblk > 2 * INOPB(&sblock) &&
205 		    mininos < cgp->cg_initediblk) {
206 			i = cgp->cg_initediblk;
207 			if (mininos < 2 * INOPB(&sblock))
208 				cgp->cg_initediblk = 2 * INOPB(&sblock);
209 			else
210 				cgp->cg_initediblk = mininos;
211 			pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
212 			    c, i, cgp->cg_initediblk, "VALID INODES");
213 			cgdirty(cgbp);
214 		}
215 		if (inosused < sblock.fs_ipg)
216 			continue;
217 		lastino += 1;
218 		if (lastino < (c * sblock.fs_ipg))
219 			inosused = 0;
220 		else
221 			inosused = lastino - (c * sblock.fs_ipg);
222 		if (rebuiltcg && inosused > cgp->cg_initediblk &&
223 		    sblock.fs_magic == FS_UFS2_MAGIC) {
224 			cgp->cg_initediblk = roundup(inosused, INOPB(&sblock));
225 			pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
226 			    cgp->cg_initediblk);
227 		}
228 		/*
229 		 * If we were not able to determine in advance which inodes
230 		 * were in use, then reduce the size of the inoinfo structure
231 		 * to the size necessary to describe the inodes that we
232 		 * really found. Always leave map space in the first cylinder
233 		 * group in case we need to a root or lost+found directory.
234 		 */
235 		if (inumber == lastino || c == 0)
236 			continue;
237 		inostathead[c].il_numalloced = inosused;
238 		if (inosused == 0) {
239 			free(inostathead[c].il_stat);
240 			inostathead[c].il_stat = NULL;
241 			continue;
242 		}
243 		info = Calloc((unsigned)inosused, sizeof(struct inostat));
244 		if (info == NULL)
245 			errx(EEXIT, "cannot alloc %u bytes for inoinfo",
246 			    (unsigned)(sizeof(struct inostat) * inosused));
247 		memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
248 		free(inostathead[c].il_stat);
249 		inostathead[c].il_stat = info;
250 	}
251 	freeinodebuf();
252 }
253 
254 static int
255 checkinode(ino_t inumber, struct inodesc *idesc, int rebuiltcg)
256 {
257 	struct inode ip;
258 	union dinode *dp;
259 	off_t kernmaxfilesize;
260 	ufs2_daddr_t ndb;
261 	mode_t mode;
262 	intmax_t size, fixsize;
263 	int j, ret, offset;
264 
265 	if ((dp = getnextinode(inumber, rebuiltcg)) == NULL) {
266 		pfatal("INVALID INODE");
267 		goto unknown;
268 	}
269 	mode = DIP(dp, di_mode) & IFMT;
270 	if (mode == 0) {
271 		if ((sblock.fs_magic == FS_UFS1_MAGIC &&
272 		     (memcmp(dp->dp1.di_db, zino.dp1.di_db,
273 			UFS_NDADDR * sizeof(ufs1_daddr_t)) ||
274 		      memcmp(dp->dp1.di_ib, zino.dp1.di_ib,
275 			UFS_NIADDR * sizeof(ufs1_daddr_t)) ||
276 		      dp->dp1.di_mode || dp->dp1.di_size)) ||
277 		    (sblock.fs_magic == FS_UFS2_MAGIC &&
278 		     (memcmp(dp->dp2.di_db, zino.dp2.di_db,
279 			UFS_NDADDR * sizeof(ufs2_daddr_t)) ||
280 		      memcmp(dp->dp2.di_ib, zino.dp2.di_ib,
281 			UFS_NIADDR * sizeof(ufs2_daddr_t)) ||
282 		      dp->dp2.di_mode || dp->dp2.di_size))) {
283 			pfatal("PARTIALLY ALLOCATED INODE I=%lu",
284 			    (u_long)inumber);
285 			if (reply("CLEAR") == 1) {
286 				ginode(inumber, &ip);
287 				clearinode(ip.i_dp);
288 				inodirty(&ip);
289 				irelse(&ip);
290 			}
291 		}
292 		inoinfo(inumber)->ino_state = USTATE;
293 		return (1);
294 	}
295 	lastino = inumber;
296 	/* This should match the file size limit in ffs_mountfs(). */
297 	if (sblock.fs_magic == FS_UFS1_MAGIC)
298 		kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
299 	else
300 		kernmaxfilesize = sblock.fs_maxfilesize;
301 	if (DIP(dp, di_size) > kernmaxfilesize ||
302 	    DIP(dp, di_size) > sblock.fs_maxfilesize ||
303 	    (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
304 		if (debug)
305 			printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
306 		pfatal("BAD FILE SIZE");
307 		goto unknown;
308 	}
309 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
310 		ginode(inumber, &ip);
311 		dp = ip.i_dp;
312 		DIP_SET(dp, di_size, sblock.fs_fsize);
313 		DIP_SET(dp, di_mode, IFREG|0600);
314 		inodirty(&ip);
315 		irelse(&ip);
316 	}
317 	if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
318 	     mode == IFSOCK) && DIP(dp, di_size) != 0) {
319 		if (debug)
320 			printf("bad special-file size %ju:",
321 			    (uintmax_t)DIP(dp, di_size));
322 		pfatal("BAD SPECIAL-FILE SIZE");
323 		goto unknown;
324 	}
325 	if ((mode == IFBLK || mode == IFCHR) &&
326 	    (dev_t)DIP(dp, di_rdev) == NODEV) {
327 		if (debug)
328 			printf("bad special-file rdev NODEV:");
329 		pfatal("BAD SPECIAL-FILE RDEV");
330 		goto unknown;
331 	}
332 	ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
333 	if (ndb < 0) {
334 		if (debug)
335 			printf("negative size %ju ndb %ju:",
336 				(uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
337 		pfatal("NEGATIVE FILE SIZE");
338 		goto unknown;
339 	}
340 	if (mode == IFBLK || mode == IFCHR)
341 		ndb++;
342 	if (mode == IFLNK) {
343 		/*
344 		 * Fake ndb value so direct/indirect block checks below
345 		 * will detect any garbage after symlink string.
346 		 */
347 		if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
348 			if (sblock.fs_magic == FS_UFS1_MAGIC)
349 				ndb = howmany(DIP(dp, di_size),
350 				    sizeof(ufs1_daddr_t));
351 			else
352 				ndb = howmany(DIP(dp, di_size),
353 				    sizeof(ufs2_daddr_t));
354 			if (ndb > UFS_NDADDR) {
355 				j = ndb - UFS_NDADDR;
356 				for (ndb = 1; j > 1; j--)
357 					ndb *= NINDIR(&sblock);
358 				ndb += UFS_NDADDR;
359 			}
360 		}
361 	}
362 	for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++) {
363 		if (DIP(dp, di_db[j]) == 0)
364 			continue;
365 		if (debug)
366 			printf("invalid direct addr[%d]: %ju\n", j,
367 			    (uintmax_t)DIP(dp, di_db[j]));
368 		pfatal("INVALID DIRECT BLOCK");
369 		ginode(inumber, &ip);
370 		prtinode(&ip);
371 		if (reply("CLEAR") == 1) {
372 			DIP_SET(ip.i_dp, di_db[j], 0);
373 			inodirty(&ip);
374 		}
375 		irelse(&ip);
376 	}
377 	for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++)
378 		ndb /= NINDIR(&sblock);
379 	for (; j < UFS_NIADDR; j++) {
380 		if (DIP(dp, di_ib[j]) == 0)
381 			continue;
382 		if (debug)
383 			printf("invalid indirect addr: %ju\n",
384 			    (uintmax_t)DIP(dp, di_ib[j]));
385 		pfatal("INVALID INDIRECT BLOCK");
386 		ginode(inumber, &ip);
387 		prtinode(&ip);
388 		if (reply("CLEAR") == 1) {
389 			DIP_SET(ip.i_dp, di_ib[j], 0);
390 			inodirty(&ip);
391 		}
392 		irelse(&ip);
393 	}
394 	if (ftypeok(dp) == 0) {
395 		pfatal("UNKNOWN FILE TYPE");
396 		goto unknown;
397 	}
398 	n_files++;
399 	inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
400 	if (mode == IFDIR) {
401 		if (DIP(dp, di_size) == 0) {
402 			inoinfo(inumber)->ino_state = DCLEAR;
403 		} else if (DIP(dp, di_nlink) <= 0) {
404 			inoinfo(inumber)->ino_state = DZLINK;
405 		} else {
406 			inoinfo(inumber)->ino_state = DSTATE;
407 			cacheino(dp, inumber);
408 			countdirs++;
409 		}
410 	} else if (DIP(dp, di_nlink) <= 0)
411 		inoinfo(inumber)->ino_state = FZLINK;
412 	else
413 		inoinfo(inumber)->ino_state = FSTATE;
414 	inoinfo(inumber)->ino_type = IFTODT(mode);
415 	badblk = dupblk = 0;
416 	idesc->id_number = inumber;
417 	if (DIP(dp, di_flags) & SF_SNAPSHOT)
418 		inoinfo(inumber)->ino_idtype = SNAP;
419 	else
420 		inoinfo(inumber)->ino_idtype = ADDR;
421 	idesc->id_type = inoinfo(inumber)->ino_idtype;
422 	(void)ckinode(dp, idesc);
423 	if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
424 		ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
425 		for (j = 0; j < UFS_NXADDR; j++) {
426 			if (--ndb == 0 &&
427 			    (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
428 				idesc->id_numfrags = numfrags(&sblock,
429 				    fragroundup(&sblock, offset));
430 			else
431 				idesc->id_numfrags = sblock.fs_frag;
432 			if (dp->dp2.di_extb[j] == 0)
433 				continue;
434 			idesc->id_blkno = dp->dp2.di_extb[j];
435 			ret = (*idesc->id_func)(idesc);
436 			if (ret & STOP)
437 				break;
438 		}
439 	}
440 	if (sblock.fs_magic == FS_UFS2_MAGIC)
441 		eascan(idesc, &dp->dp2);
442 	idesc->id_entryno *= btodb(sblock.fs_fsize);
443 	if (DIP(dp, di_blocks) != idesc->id_entryno) {
444 		pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
445 		    (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
446 		    (uintmax_t)idesc->id_entryno);
447 		if (preen)
448 			printf(" (CORRECTED)\n");
449 		else if (reply("CORRECT") == 0)
450 			return (1);
451 		if (bkgrdflag == 0) {
452 			ginode(inumber, &ip);
453 			DIP_SET(ip.i_dp, di_blocks, idesc->id_entryno);
454 			inodirty(&ip);
455 			irelse(&ip);
456 		} else {
457 			cmd.value = idesc->id_number;
458 			cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
459 			if (debug)
460 				printf("adjblkcnt ino %ju amount %lld\n",
461 				    (uintmax_t)cmd.value, (long long)cmd.size);
462 			if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
463 			    &cmd, sizeof cmd) == -1)
464 				rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
465 		}
466 	}
467 	/*
468 	 * UFS does not allow files to end with a hole; it requires that
469 	 * the last block of a file be allocated. The last allocated block
470 	 * in a file is tracked in id_lballoc. Here, we check for a size
471 	 * past the last allocated block of the file and if that is found,
472 	 * shorten the file to reference the last allocated block to avoid
473 	 * having it reference a hole at its end.
474 	 *
475 	 * Soft updates will always ensure that the file size is correct
476 	 * for files that contain only direct block pointers. However
477 	 * soft updates does not roll back sizes for files with indirect
478 	 * blocks that it has set to unallocated because their contents
479 	 * have not yet been written to disk. Hence, the file can appear
480 	 * to have a hole at its end because the block pointer has been
481 	 * rolled back to zero. Thus finding a hole at the end of a file
482 	 * that is located in an indirect block receives only a warning
483 	 * while finding a hole at the end of a file in a direct block
484 	 * receives a fatal error message.
485 	 */
486 	size = DIP(dp, di_size);
487 	if (idesc->id_lballoc < lblkno(&sblock, size - 1) &&
488 	    /* exclude embedded symbolic links */
489 	    ((mode != IFLNK) || size >= sblock.fs_maxsymlinklen)) {
490  		fixsize = lblktosize(&sblock, idesc->id_lballoc + 1);
491 		if (size > UFS_NDADDR * sblock.fs_bsize)
492 			pwarn("INODE %lu: FILE SIZE %ju BEYOND END OF "
493 			      "ALLOCATED FILE, SIZE SHOULD BE %ju",
494 			      (u_long)inumber, size, fixsize);
495 		else
496 			pfatal("INODE %lu: FILE SIZE %ju BEYOND END OF "
497 			      "ALLOCATED FILE, SIZE SHOULD BE %ju",
498 			      (u_long)inumber, size, fixsize);
499 		if (preen)
500 			printf(" (ADJUSTED)\n");
501 		else if (reply("ADJUST") == 0)
502 			return (1);
503 		if (bkgrdflag == 0) {
504 			ginode(inumber, &ip);
505 			DIP_SET(ip.i_dp, di_size, fixsize);
506 			inodirty(&ip);
507 			irelse(&ip);
508 		} else {
509 			cmd.value = idesc->id_number;
510 			cmd.size = fixsize;
511 			if (debug)
512 				printf("setsize ino %ju size set to %ju\n",
513 				    (uintmax_t)cmd.value, (uintmax_t)cmd.size);
514 			if (sysctl(setsize, MIBSIZE, 0, 0,
515 			    &cmd, sizeof cmd) == -1)
516 				rwerror("SET INODE SIZE", cmd.value);
517 		}
518 
519 	}
520 	return (1);
521 unknown:
522 	ginode(inumber, &ip);
523 	prtinode(&ip);
524 	inoinfo(inumber)->ino_state = USTATE;
525 	if (reply("CLEAR") == 1) {
526 		clearinode(ip.i_dp);
527 		inodirty(&ip);
528 	}
529 	irelse(&ip);
530 	return (1);
531 }
532 
533 int
534 pass1check(struct inodesc *idesc)
535 {
536 	int res = KEEPON;
537 	int anyout, nfrags;
538 	ufs2_daddr_t blkno = idesc->id_blkno;
539 	struct dups *dlp;
540 	struct dups *new;
541 
542 	if (idesc->id_type == SNAP) {
543 		if (blkno == BLK_NOCOPY)
544 			return (KEEPON);
545 		if (idesc->id_number == cursnapshot) {
546 			if (blkno == blkstofrags(&sblock, idesc->id_lbn))
547 				return (KEEPON);
548 			if (blkno == BLK_SNAP) {
549 				blkno = blkstofrags(&sblock, idesc->id_lbn);
550 				idesc->id_entryno -= idesc->id_numfrags;
551 			}
552 		} else {
553 			if (blkno == BLK_SNAP)
554 				return (KEEPON);
555 		}
556 	}
557 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
558 		blkerror(idesc->id_number, "BAD", blkno);
559 		if (badblk++ >= MAXBAD) {
560 			pwarn("EXCESSIVE BAD BLKS I=%lu",
561 			    (u_long)idesc->id_number);
562 			if (preen)
563 				printf(" (SKIPPING)\n");
564 			else if (reply("CONTINUE") == 0) {
565 				ckfini(0);
566 				exit(EEXIT);
567 			}
568 			rerun = 1;
569 			return (STOP);
570 		}
571 	}
572 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
573 		if (anyout && chkrange(blkno, 1)) {
574 			res = SKIP;
575 		} else if (!testbmap(blkno)) {
576 			n_blks++;
577 			setbmap(blkno);
578 		} else {
579 			blkerror(idesc->id_number, "DUP", blkno);
580 			if (dupblk++ >= MAXDUP) {
581 				pwarn("EXCESSIVE DUP BLKS I=%lu",
582 					(u_long)idesc->id_number);
583 				if (preen)
584 					printf(" (SKIPPING)\n");
585 				else if (reply("CONTINUE") == 0) {
586 					ckfini(0);
587 					exit(EEXIT);
588 				}
589 				rerun = 1;
590 				return (STOP);
591 			}
592 			new = (struct dups *)Malloc(sizeof(struct dups));
593 			if (new == NULL) {
594 				pfatal("DUP TABLE OVERFLOW.");
595 				if (reply("CONTINUE") == 0) {
596 					ckfini(0);
597 					exit(EEXIT);
598 				}
599 				rerun = 1;
600 				return (STOP);
601 			}
602 			new->dup = blkno;
603 			if (muldup == NULL) {
604 				duplist = muldup = new;
605 				new->next = NULL;
606 			} else {
607 				new->next = muldup->next;
608 				muldup->next = new;
609 			}
610 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
611 				if (dlp->dup == blkno)
612 					break;
613 			if (dlp == muldup && dlp->dup != blkno)
614 				muldup = new;
615 		}
616 		/*
617 		 * count the number of blocks found in id_entryno
618 		 */
619 		idesc->id_entryno++;
620 	}
621 	if (idesc->id_level == 0 && idesc->id_lballoc < idesc->id_lbn)
622 		idesc->id_lballoc = idesc->id_lbn;
623 	return (res);
624 }
625