xref: /titanic_41/usr/src/cmd/fs.d/ufs/fsck/pass1.c (revision 60405de4d8688d96dd05157c28db3ade5c9bc234)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms are permitted
14  * provided that: (1) source distributions retain this entire copyright
15  * notice and comment, and (2) distributions including binaries display
16  * the following acknowledgement:  ``This product includes software
17  * developed by the University of California, Berkeley and its contributors''
18  * in the documentation or other materials provided with the distribution
19  * and in all advertising materials mentioning features or use of this
20  * software. Neither the name of the University nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/mntent.h>
37 #include <sys/fs/ufs_fs.h>
38 #include <sys/vnode.h>
39 #define	_KERNEL
40 #include <sys/fs/ufs_fsdir.h>
41 #undef _KERNEL
42 #include <sys/fs/ufs_inode.h>
43 #include "fsck.h"
44 
45 /*
46  * for each large file (size > MAXOFF_T), the global largefile_count
47  * gets incremented during this pass.
48  */
49 
50 static uint32_t badblk;		/* number seen for the current inode */
51 static uint32_t dupblk;		/* number seen for the current inode */
52 
53 static void clear_attr_acl(fsck_ino_t, fsck_ino_t, char *);
54 static void verify_inode(fsck_ino_t, struct inodesc *, fsck_ino_t);
55 static void check_dirholes(fsck_ino_t, struct inodesc *);
56 static void collapse_dirhole(fsck_ino_t, struct inodesc *);
57 static void note_used(daddr32_t);
58 
59 void
60 pass1(void)
61 {
62 	uint_t c, i;
63 	daddr32_t cgd;
64 	struct inodesc idesc;
65 	fsck_ino_t inumber;
66 	fsck_ino_t maxinumber;
67 
68 	/*
69 	 * Set file system reserved blocks in used block map.
70 	 */
71 	for (c = 0; c < sblock.fs_ncg; c++) {
72 		cgd = cgdmin(&sblock, c);
73 		if (c == 0) {
74 			/*
75 			 * Doing the first cylinder group, account for
76 			 * the cg summaries as well.
77 			 */
78 			i = cgbase(&sblock, c);
79 			cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
80 		} else {
81 			i = cgsblock(&sblock, c);
82 		}
83 		for (; i < cgd; i++) {
84 			note_used(i);
85 		}
86 	}
87 	/*
88 	 * Note blocks being used by the log, so we don't declare
89 	 * them as available and some time in the future we get a
90 	 * freeing free block panic.
91 	 */
92 	if (islog && islogok && sblock.fs_logbno)
93 		examinelog(sblock.fs_logbno, &note_used);
94 
95 	/*
96 	 * Find all allocated blocks.  This must be completed before
97 	 * we read the contents of any directories, as dirscan() et al
98 	 * don't want to know about block allocation holes.  So, part
99 	 * of this pass is to truncate any directories with holes to
100 	 * just before those holes, so dirscan() can remain blissfully
101 	 * ignorant.
102 	 */
103 	inumber = 0;
104 	n_files = n_blks = 0;
105 	resetinodebuf();
106 	maxinumber = sblock.fs_ncg * sblock.fs_ipg;
107 	for (c = 0; c < sblock.fs_ncg; c++) {
108 		for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
109 			if (inumber < UFSROOTINO)
110 				continue;
111 			init_inodesc(&idesc);
112 			idesc.id_type = ADDR;
113 			idesc.id_func = pass1check;
114 			verify_inode(inumber, &idesc, maxinumber);
115 		}
116 	}
117 	freeinodebuf();
118 }
119 
120 /*
121  * Perform checks on an inode and setup/track the state of the inode
122  * in maps (statemap[], lncntp[]) for future reference and validation.
123  * Initiate the calls to ckinode and in turn pass1check() to handle
124  * further validation.
125  */
126 static void
127 verify_inode(fsck_ino_t inumber, struct inodesc *idesc, fsck_ino_t maxinumber)
128 {
129 	int j, clear, flags;
130 	int isdir;
131 	char *err;
132 	fsck_ino_t shadow, attrinode;
133 	daddr32_t ndb;
134 	struct dinode *dp;
135 	struct inoinfo *iip;
136 
137 	dp = getnextinode(inumber);
138 	if ((dp->di_mode & IFMT) == 0) {
139 		/* mode and type of file is not set */
140 		if ((memcmp((void *)dp->di_db, (void *)zino.di_db,
141 			    NDADDR * sizeof (daddr32_t)) != 0) ||
142 		    (memcmp((void *)dp->di_ib, (void *)zino.di_ib,
143 			    NIADDR * sizeof (daddr32_t)) != 0) ||
144 		    (dp->di_mode != 0) || (dp->di_size != 0)) {
145 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
146 			if (reply("CLEAR") == 1) {
147 				dp = ginode(inumber);
148 				clearinode(dp);
149 				inodirty();
150 			} else {
151 				iscorrupt = 1;
152 			}
153 		}
154 		statemap[inumber] = USTATE;
155 		return;
156 	}
157 
158 	isdir = ((dp->di_mode & IFMT) == IFDIR) ||
159 		((dp->di_mode & IFMT) == IFATTRDIR);
160 
161 	lastino = inumber;
162 	if (dp->di_size > (u_offset_t)UFS_MAXOFFSET_T) {
163 		pfatal("NEGATIVE SIZE %lld I=%d",
164 		    (longlong_t)dp->di_size, inumber);
165 		goto bogus;
166 	}
167 
168 	/*
169 	 * A more precise test of the type is done later on.  Just get
170 	 * rid of the blatantly-wrong ones before we do any
171 	 * significant work.
172 	 */
173 	if ((dp->di_mode & IFMT) == IFMT) {
174 		pfatal("BAD MODE 0%o I=%d",
175 		    dp->di_mode & IFMT, inumber);
176 		if (reply("BAD MODE: MAKE IT A FILE") == 1) {
177 			statemap[inumber] = FSTATE;
178 			dp = ginode(inumber);
179 			dp->di_mode = IFREG | 0600;
180 			inodirty();
181 			truncino(inumber, sblock.fs_fsize, TI_NOPARENT);
182 			dp = getnextrefresh();
183 		} else {
184 			iscorrupt = 1;
185 		}
186 	}
187 
188 	ndb = howmany(dp->di_size, (u_offset_t)sblock.fs_bsize);
189 	if (ndb < 0) {
190 		/* extra space to distinguish from previous pfatal() */
191 		pfatal("NEGATIVE SIZE %lld  I=%d",
192 		    (longlong_t)dp->di_size, inumber);
193 		goto bogus;
194 	}
195 
196 	if ((dp->di_mode & IFMT) == IFBLK ||
197 	    (dp->di_mode & IFMT) == IFCHR) {
198 		if (dp->di_size != 0) {
199 			pfatal("SPECIAL FILE WITH NON-ZERO LENGTH %lld I=%d",
200 			    (longlong_t)dp->di_size, inumber);
201 			goto bogus;
202 		}
203 
204 		for (j = 0; j < NDADDR; j++) {
205 			/*
206 			 * It's a device, so all the block pointers
207 			 * should be zero except for di_ordev.
208 			 * di_ordev is overlayed on the block array,
209 			 * but where varies between big and little
210 			 * endian, so make sure that the only non-zero
211 			 * element is the correct one.  There can be
212 			 * a device whose ordev is zero, so we can't
213 			 * check for the reverse.
214 			 */
215 			if (dp->di_db[j] != 0 &&
216 			    &dp->di_db[j] != &dp->di_ordev) {
217 				if (debug) {
218 					(void) printf(
219 					    "spec file di_db[%d] has %d\n",
220 					    j, dp->di_db[j]);
221 				}
222 				pfatal(
223 			    "SPECIAL FILE WITH NON-ZERO FRAGMENT LIST  I=%d",
224 				    inumber);
225 				goto bogus;
226 			}
227 		}
228 
229 		for (j = 0; j < NIADDR; j++) {
230 			if (dp->di_ib[j] != 0) {
231 				if (debug)
232 					(void) printf(
233 					    "special has %d at ib[%d]\n",
234 					    dp->di_ib[j], j);
235 				pfatal(
236 			    "SPECIAL FILE WITH NON-ZERO FRAGMENT LIST  I=%d",
237 				    inumber);
238 				goto bogus;
239 			}
240 		}
241 	} else {
242 		/*
243 		 * This assignment is mostly here to appease lint, but
244 		 * doesn't hurt.
245 		 */
246 		err = "Internal error: unexpected variant of having "
247 		    "blocks past end of file  I=%d";
248 
249 		clear = 0;
250 
251 		/*
252 		 * If it's not a device, it has to follow the
253 		 * rules for files.  In particular, no blocks after
254 		 * the last one that di_size says is in use.
255 		 */
256 		for (j = ndb; j < NDADDR; j++) {
257 			if (dp->di_db[j] != 0) {
258 				if (debug) {
259 					(void) printf("bad file direct "
260 					    "addr[%d]: block 0x%x "
261 					    "format: 0%o\n",
262 					    j, dp->di_db[j],
263 					    dp->di_mode & IFMT);
264 				}
265 				err = "FILE WITH FRAGMENTS PAST END  I=%d";
266 				clear = 1;
267 				break;
268 			}
269 		}
270 
271 		/*
272 		 * Find last indirect pointer that should be in use,
273 		 * and make sure any after it are clear.
274 		 */
275 		if (!clear) {
276 			for (j = 0, ndb -= NDADDR; ndb > 0; j++) {
277 				ndb /= NINDIR(&sblock);
278 			}
279 			for (; j < NIADDR; j++) {
280 				if (dp->di_ib[j] != 0) {
281 					if (debug) {
282 						(void) printf("bad file "
283 						    "indirect addr: block %d\n",
284 						    dp->di_ib[j]);
285 					}
286 					err =
287 					    "FILE WITH FRAGMENTS PAST END I=%d";
288 					clear = 2;
289 					break;
290 				}
291 			}
292 		}
293 
294 		if (clear) {
295 			/*
296 			 * The discarded blocks will be garbage-
297 			 * collected in pass5.  If we're told not to
298 			 * discard them, it's just lost blocks, which
299 			 * isn't worth setting iscorrupt for.
300 			 */
301 			pwarn(err, inumber);
302 			if (preen || reply("DISCARD EXCESS FRAGMENTS") == 1) {
303 				dp = ginode(inumber);
304 				if (clear == 1) {
305 					for (; j < NDADDR; j++)
306 						dp->di_db[j] = 0;
307 					j = 0;
308 				}
309 				for (; j < NIADDR; j++)
310 					dp->di_ib[j] = 0;
311 				inodirty();
312 				dp = getnextrefresh();
313 				if (preen)
314 					(void) printf(" (TRUNCATED)");
315 			}
316 		}
317 	}
318 
319 	if (ftypeok(dp) == 0) {
320 		pfatal("UNKNOWN FILE TYPE 0%o  I=%d", dp->di_mode, inumber);
321 		goto bogus;
322 	}
323 	n_files++;
324 	TRACK_LNCNTP(inumber, lncntp[inumber] = dp->di_nlink);
325 
326 	/*
327 	 * We can't do anything about it right now, so note that its
328 	 * processing is being delayed.  Otherwise, we'd be changing
329 	 * the block allocations out from under ourselves, which causes
330 	 * no end of confusion.
331 	 */
332 	flags = statemap[inumber] & INDELAYD;
333 
334 	/*
335 	 * if errorlocked or logging, then open deleted files will
336 	 * manifest as di_nlink <= 0 and di_mode != 0
337 	 * so skip them; they're ok.
338 	 */
339 	if (dp->di_nlink <= 0 &&
340 	    !((errorlocked || islog) && dp->di_mode == 0)) {
341 		flags |= INZLINK;
342 		if (debug)
343 			(void) printf(
344 		    "marking i=%d INZLINK; nlink %d, mode 0%o, islog %d\n",
345 			    inumber, dp->di_nlink, dp->di_mode, islog);
346 	}
347 
348 	switch (dp->di_mode & IFMT) {
349 	case IFDIR:
350 	case IFATTRDIR:
351 		if (dp->di_size == 0) {
352 			/*
353 			 * INCLEAR means it will be ignored by passes 2 & 3.
354 			 */
355 			if ((dp->di_mode & IFMT) == IFDIR)
356 				(void) printf("ZERO-LENGTH DIR  I=%d\n",
357 				    inumber);
358 			else
359 				(void) printf("ZERO-LENGTH ATTRDIR  I=%d\n",
360 				    inumber);
361 			add_orphan_dir(inumber);
362 			flags |= INCLEAR;
363 		}
364 		statemap[inumber] = DSTATE | flags;
365 		cacheino(dp, inumber);
366 		countdirs++;
367 		break;
368 
369 	case IFSHAD:
370 		if (dp->di_size == 0) {
371 			(void) printf("ZERO-LENGTH SHADOW  I=%d\n", inumber);
372 			flags |= INCLEAR;
373 		}
374 		statemap[inumber] = SSTATE | flags;
375 		cacheacl(dp, inumber);
376 		break;
377 
378 	default:
379 		statemap[inumber] = FSTATE | flags;
380 	}
381 
382 	badblk = 0;
383 	dupblk = 0;
384 	idesc->id_number = inumber;
385 	idesc->id_fix = DONTKNOW;
386 	if (dp->di_size > (u_offset_t)MAXOFF_T) {
387 		largefile_count++;
388 	}
389 
390 	(void) ckinode(dp, idesc, CKI_TRAVERSE);
391 	if (isdir && (idesc->id_firsthole >= 0))
392 		check_dirholes(inumber, idesc);
393 
394 	if (dp->di_blocks != idesc->id_entryno) {
395 		/*
396 		 * The kernel releases any blocks it finds in the lists,
397 		 * ignoring the block count itself.  So, a bad count is
398 		 * not grounds for setting iscorrupt.
399 		 */
400 		pwarn("INCORRECT DISK BLOCK COUNT I=%u (%d should be %d)",
401 		    inumber, (uint32_t)dp->di_blocks, idesc->id_entryno);
402 		if (!preen && (reply("CORRECT") == 0))
403 			return;
404 		dp = ginode(inumber);
405 		dp->di_blocks = idesc->id_entryno;
406 		iip = getinoinfo(inumber);
407 		if (iip != NULL)
408 			iip->i_isize = dp->di_size;
409 		inodirty();
410 		if (preen)
411 			(void) printf(" (CORRECTED)\n");
412 	}
413 	if (isdir && (dp->di_blocks == 0)) {
414 		/*
415 		 * INCLEAR will cause passes 2 and 3 to skip it.
416 		 */
417 		(void) printf("DIR WITH ZERO BLOCKS  I=%d\n", inumber);
418 		statemap[inumber] = DCLEAR;
419 		add_orphan_dir(inumber);
420 	}
421 
422 	/*
423 	 * Check that the ACL is on a valid file type
424 	 */
425 	shadow = dp->di_shadow;
426 	if (shadow != 0) {
427 		if (acltypeok(dp) == 0) {
428 			clear_attr_acl(inumber, -1,
429 			    "NON-ZERO ACL REFERENCE, I=%d\n");
430 		} else if ((shadow <= UFSROOTINO) ||
431 		    (shadow > maxinumber)) {
432 			clear_attr_acl(inumber, -1,
433 			    "BAD ACL REFERENCE I=%d\n");
434 		} else {
435 			registershadowclient(shadow,
436 			    inumber, &shadowclientinfo);
437 		}
438 	}
439 
440 	attrinode = dp->di_oeftflag;
441 	if (attrinode != 0) {
442 		if ((attrinode <= UFSROOTINO) ||
443 		    (attrinode > maxinumber)) {
444 			clear_attr_acl(attrinode, inumber,
445 			    "BAD ATTRIBUTE REFERENCE TO I=%d FROM I=%d\n");
446 		} else {
447 			dp = ginode(attrinode);
448 			if ((dp->di_mode & IFMT) != IFATTRDIR) {
449 				clear_attr_acl(attrinode, inumber,
450 			    "BAD ATTRIBUTE DIR REF TO I=%d FROM I=%d\n");
451 			} else if (dp->di_size == 0) {
452 				clear_attr_acl(attrinode, inumber,
453 		    "REFERENCE TO ZERO-LENGTH ATTRIBUTE DIR I=%d from I=%d\n");
454 			} else {
455 				registershadowclient(attrinode, inumber,
456 				    &attrclientinfo);
457 			}
458 		}
459 	}
460 	return;
461 
462 	/*
463 	 * If we got here, we've not had the chance to see if a
464 	 * directory has holes, but we know the directory's bad,
465 	 * so it's safe to always return false (no holes found).
466 	 *
467 	 * Also, a pfatal() is always done before jumping here, so
468 	 * we know we're not in preen mode.
469 	 */
470 bogus:
471 	if (isdir) {
472 		/*
473 		 * INCLEAR makes passes 2 & 3 skip it.
474 		 */
475 		statemap[inumber] = DCLEAR;
476 		add_orphan_dir(inumber);
477 		cacheino(dp, inumber);
478 	} else {
479 		statemap[inumber] = FCLEAR;
480 	}
481 	if (reply("CLEAR") == 1) {
482 		(void) tdelete((void *)inumber, &limbo_dirs, ino_t_cmp);
483 		freeino(inumber, TI_PARENT);
484 		inodirty();
485 	} else {
486 		iscorrupt = 1;
487 	}
488 }
489 
490 /*
491  * Do fixup for bad acl/attr references.  If PARENT is -1, then
492  * we assume we're working on a shadow, otherwise an extended attribute.
493  * FMT must be a printf format string, with one %d directive for
494  * the inode number.
495  */
496 static void
497 clear_attr_acl(fsck_ino_t inumber, fsck_ino_t parent, char *fmt)
498 {
499 	fsck_ino_t victim = inumber;
500 	struct dinode *dp;
501 
502 	if (parent != -1)
503 		victim = parent;
504 
505 	if (fmt != NULL) {
506 		if (parent == -1)
507 			pwarn(fmt, (int)inumber);
508 		else
509 			pwarn(fmt, (int)inumber, (int)parent);
510 	}
511 
512 	if (debug)
513 		(void) printf("parent file/dir I=%d\nvictim I=%d",
514 		    (int)parent, (int)victim);
515 
516 	if (!preen && (reply("REMOVE REFERENCE") == 0)) {
517 		iscorrupt = 1;
518 		return;
519 	}
520 
521 	dp = ginode(victim);
522 	if (parent == -1) {
523 		/*
524 		 * The file had a bad shadow/acl, so lock it down
525 		 * until someone can protect it the way they need it
526 		 * to be (i.e., be conservatively paranoid).
527 		 */
528 		dp->di_shadow = 0;
529 		dp->di_mode &= IFMT;
530 	} else {
531 		dp->di_oeftflag = 0;
532 	}
533 
534 	inodirty();
535 	if (preen)
536 		(void) printf(" (CORRECTED)\n");
537 }
538 
539 /*
540  * Check if we have holes in the directory's indirect
541  * blocks.  If there are, get rid of everything after
542  * the first hole.
543  */
544 static void
545 check_dirholes(fsck_ino_t inumber, struct inodesc *idesc)
546 {
547 	char pathbuf[MAXPATHLEN + 1];
548 
549 	getpathname(pathbuf, idesc->id_number, idesc->id_number);
550 	pfatal("I=%d  DIRECTORY %s: CONTAINS EMPTY BLOCKS",
551 	    idesc->id_number, pathbuf);
552 	if (reply("TRUNCATE AT FIRST EMPTY BLOCK") == 1) {
553 		/*
554 		 * We found a hole, so get rid of it.
555 		 */
556 		collapse_dirhole(inumber, idesc);
557 
558 		if (preen)
559 			(void) printf(" (TRUNCATED)\n");
560 	} else {
561 		iscorrupt = 1;
562 	}
563 }
564 
565 /*
566  * Truncate a directory to its first hole.  If there are non-holes
567  * in the direct blocks after the problem block, move them down so
568  * that there's somewhat less lossage.  Doing this for indirect blocks
569  * is left as an exercise for the reader.
570  */
571 static void
572 collapse_dirhole(fsck_ino_t inumber, struct inodesc *idesc)
573 {
574 	offset_t new_size;
575 	int blocks;
576 
577 	if (idesc->id_firsthole < 0) {
578 		return;
579 	}
580 
581 	/*
582 	 * Since truncino() adjusts the size, we don't need to do that here,
583 	 * but we have to tell it what final size we want.
584 	 *
585 	 * We need to count from block zero up through the last block
586 	 * before the hole.  If the hole is in the indirect blocks, chop at
587 	 * the start of the nearest level of indirection.  Orphans will
588 	 * get reconnected, so we're not actually losing anything by doing
589 	 * it this way, and we're simplifying truncation significantly.
590 	 */
591 	new_size = idesc->id_firsthole * (offset_t)sblock.fs_bsize;
592 	blocks = howmany(new_size, sblock.fs_bsize);
593 	if (blocks > NDADDR) {
594 		if (blocks < (NDADDR + NINDIR(&sblock)))
595 			blocks = NDADDR;
596 		else if (blocks < (NDADDR + NINDIR(&sblock) +
597 		    (NINDIR(&sblock) * NINDIR(&sblock))))
598 			blocks = NDADDR + NINDIR(&sblock);
599 		else
600 			blocks = NDADDR + NINDIR(&sblock) +
601 				(NINDIR(&sblock) * NINDIR(&sblock));
602 		new_size = blocks * sblock.fs_bsize;
603 		if (debug)
604 			(void) printf("to %lld (blocks %d)\n",
605 			    (longlong_t)new_size, blocks);
606 	}
607 	truncino(inumber, new_size, TI_NOPARENT);
608 
609 	/*
610 	 * Technically, there are still the original number of fragments
611 	 * associated with the object.  However, that number is not used
612 	 * to control anything, so we can do the in-memory truncation of
613 	 * it without bad things happening.
614 	 */
615 	idesc->id_entryno = btodb(new_size);
616 }
617 
618 int
619 pass1check(struct inodesc *idesc)
620 {
621 	int res = KEEPON;
622 	int anyout;
623 	int nfrags;
624 	daddr32_t lbn;
625 	daddr32_t fragno = idesc->id_blkno;
626 	struct dinode *dp;
627 
628 	/*
629 	 * If this is a fallocate'd file, block numbers may be stored
630 	 * as negative. In that case negate the negative numbers.
631 	 */
632 	dp = ginode(idesc->id_number);
633 	if (dp->di_cflags & IFALLOCATE && fragno < 0)
634 		fragno = -fragno;
635 
636 	if ((anyout = chkrange(fragno, idesc->id_numfrags)) != 0) {
637 		/*
638 		 * Note that blkerror() exits when preening.
639 		 */
640 		blkerror(idesc->id_number, "OUT OF RANGE",
641 		    fragno, idesc->id_lbn * sblock.fs_frag);
642 
643 		dp = ginode(idesc->id_number);
644 		if ((((dp->di_mode & IFMT) == IFDIR) ||
645 		    ((dp->di_mode & IFMT) == IFATTRDIR)) &&
646 		    (idesc->id_firsthole < 0)) {
647 			idesc->id_firsthole = idesc->id_lbn;
648 		}
649 
650 		if (++badblk >= MAXBAD) {
651 			pwarn("EXCESSIVE BAD FRAGMENTS I=%u",
652 			    idesc->id_number);
653 			if (reply("CONTINUE") == 0)
654 				errexit("Program terminated.");
655 			/*
656 			 * See discussion below as to why we don't
657 			 * want to short-circuit the processing of
658 			 * this inode.  However, we know that this
659 			 * particular block is bad, so we don't need
660 			 * to go through the dup check loop.
661 			 */
662 			return (SKIP | STOP);
663 		}
664 	}
665 
666 	/*
667 	 * For each fragment, verify that it is a legal one (either
668 	 * by having already found the entire run to be legal, or by
669 	 * individual inspection), and if it is legal, see if we've
670 	 * seen it before or not.  If we haven't, note that we've seen
671 	 * it and continue on.  If we have (our in-core bitmap shows
672 	 * it as already being busy), then this must be a duplicate
673 	 * allocation.  Whine and moan accordingly.
674 	 *
675 	 * Note that for full-block allocations, this will produce
676 	 * a complaint for each fragment making up the block (i.e.,
677 	 * fs_frags' worth).  Among other things, this could be
678 	 * considered artificially inflating the dup-block count.
679 	 * However, since it is possible that one file has a full
680 	 * fs block allocated, but another is only claiming a frag
681 	 * or two out of the middle, we'll just live it.
682 	 */
683 	for (nfrags = 0; nfrags < idesc->id_numfrags; fragno++, nfrags++) {
684 		if (anyout && chkrange(fragno, 1)) {
685 			/* bad fragment number */
686 			res = SKIP;
687 		} else if (!testbmap(fragno)) {
688 			/* no other claims seen as yet */
689 			note_used(fragno);
690 		} else {
691 			/*
692 			 * We have a duplicate claim for the same fragment.
693 			 *
694 			 * blkerror() exits when preening.
695 			 *
696 			 * We want to report all the dups up until
697 			 * hitting MAXDUP.  Fortunately, blkerror()'s
698 			 * side-effects on statemap[] are idempotent,
699 			 * so the ``extra'' calls are harmless.
700 			 */
701 			lbn = idesc->id_lbn * sblock.fs_frag + nfrags;
702 			if (dupblk < MAXDUP)
703 				blkerror(idesc->id_number, "DUP", fragno, lbn);
704 
705 			/*
706 			 * Use ==, so we only complain once, no matter
707 			 * how far over the limit we end up going.
708 			 */
709 			if (++dupblk == MAXDUP) {
710 				pwarn("EXCESSIVE DUPLICATE FRAGMENTS I=%u",
711 					idesc->id_number);
712 				if (reply("CONTINUE") == 0)
713 					errexit("Program terminated.");
714 
715 				/*
716 				 * If we stop the traversal here, then
717 				 * there may be more dups in the
718 				 * inode's block list that don't get
719 				 * flagged.  Later, if we're told to
720 				 * clear one of the files claiming
721 				 * these blocks, but not the other, we
722 				 * will release blocks that are
723 				 * actually still in use.  An additional
724 				 * fsck run would be necessary to undo
725 				 * the damage.  So, instead of the
726 				 * traditional return (STOP) when told
727 				 * to continue, we really do just continue.
728 				 */
729 			}
730 			(void) find_dup_ref(fragno, idesc->id_number, lbn,
731 					    DB_CREATE | DB_INCR);
732 		}
733 		/*
734 		 * id_entryno counts the number of disk blocks found.
735 		 */
736 		idesc->id_entryno += btodb(sblock.fs_fsize);
737 	}
738 	return (res);
739 }
740 
741 static void
742 note_used(daddr32_t frag)
743 {
744 	n_blks++;
745 	setbmap(frag);
746 }
747