xref: /titanic_41/usr/src/cmd/fs.d/ufs/fsck/pass5.c (revision 8d4e547db823a866b8f73efc0acdc423e2963caf)
1 /*
2  * Copyright 2006 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/mntent.h>
36 #include <sys/fs/ufs_fs.h>
37 #include <sys/vnode.h>
38 #include <sys/fs/ufs_inode.h>
39 #include "fsck.h"
40 
41 static int check_maps(uchar_t *, uchar_t *, int, int, char *, int, int);
42 
43 void
44 pass5(void)
45 {
46 	caddr_t err;
47 	int32_t c, blk, frags;
48 	size_t	basesize, sumsize, mapsize;
49 	int excessdirs;
50 	int inomapsize, blkmapsize;
51 	int update_csums, bad_csum_sb, bad_csum_cg, update_bitmaps;
52 	struct fs *fs = &sblock;
53 	struct cg *cg = &cgrp;
54 	diskaddr_t dbase, dmax;
55 	diskaddr_t d;
56 	uint64_t i, j;
57 	struct csum *cs;
58 	struct csum backup_cs;
59 	time_t now;
60 	struct csum cstotal;
61 	struct inodesc idesc;
62 	union {				/* keep lint happy about alignment */
63 		struct cg cg;		/* the rest of buf has the bitmaps */
64 		char buf[MAXBSIZE];
65 	} u;
66 	caddr_t buf = u.buf;
67 	struct cg *newcg = &u.cg;
68 
69 	(void) memset((void *)buf, 0, sizeof (u.buf));
70 	newcg->cg_niblk = fs->fs_ipg;
71 
72 	if (fs->fs_postblformat != FS_DYNAMICPOSTBLFMT) {
73 		pfatal("UNSUPPORTED ROTATIONAL TABLE FORMAT %d\n",
74 			fs->fs_postblformat);
75 		errexit("Program terminated.");
76 		/* NOTREACHED */
77 	}
78 
79 	/* LINTED this subtraction can't overflow and is int32-aligned */
80 	basesize = &newcg->cg_space[0] - (uchar_t *)newcg;
81 
82 	/*
83 	 * We reserve the space for the old rotation summary
84 	 * tables for the benefit of old kernels, but do not
85 	 * maintain them in modern kernels. In time, they could
86 	 * theoretically go away, if we wanted to deal with
87 	 * changing the on-disk format.
88 	 */
89 
90 	/*
91 	 * Note that we don't use any of the cg_*() macros until
92 	 * after cg_sanity() has approved of what we've got.
93 	 */
94 	newcg->cg_btotoff = basesize;
95 	newcg->cg_boff = newcg->cg_btotoff + fs->fs_cpg * sizeof (daddr32_t);
96 	newcg->cg_iusedoff = newcg->cg_boff +
97 		fs->fs_cpg * fs->fs_nrpos * sizeof (uint16_t);
98 	(void) memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
99 
100 	inomapsize = howmany(fs->fs_ipg, NBBY);
101 	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
102 	blkmapsize = howmany(fs->fs_fpg, NBBY);
103 	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
104 	newcg->cg_magic = CG_MAGIC;
105 
106 	sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
107 	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
108 
109 	init_inodesc(&idesc);
110 	idesc.id_type = ADDR;
111 	(void) memset((void *)&cstotal, 0, sizeof (struct csum));
112 	now = time(NULL);
113 
114 	/*
115 	 * If the last fragments in the file system don't make up a
116 	 * full file system block, mark the bits in the blockmap
117 	 * that correspond to those missing fragments as "allocated",
118 	 * so that the last block doesn't get counted as a free block
119 	 * and those missing fragments don't get counted as free frags.
120 	 */
121 	j = blknum(fs, (uint64_t)fs->fs_size + fs->fs_frag - 1);
122 	for (i = fs->fs_size; i < j; i++)
123 		setbmap(i);
124 
125 	/*
126 	 * The cg summaries are not always updated when using
127 	 * logging.  Since we're really concerned with getting a
128 	 * sane filesystem, rather than in trying to debug UFS
129 	 * corner cases, logically we would just always recompute
130 	 * them.  However, it is disconcerting to users to be asked
131 	 * about updating the summaries when, from their point of
132 	 * view, there's been no indication of a problem up to this
133 	 * point.  So, only do it if we find a discrepancy.
134 	 */
135 	update_csums = -1;
136 	update_bitmaps = 0;
137 	for (c = 0; c < fs->fs_ncg; c++) {
138 		backup_cs = cstotal;
139 
140 		/*
141 		 * cg_sanity() will catch i/o errors for us.
142 		 */
143 		(void) getblk(&cgblk, (diskaddr_t)cgtod(fs, c),
144 		    (size_t)fs->fs_cgsize);
145 		err = cg_sanity(cg, c);
146 		if (err != NULL) {
147 			pfatal("CG %d: %s\n", c, err);
148 			free((void *)err);
149 			if (reply("REPAIR") == 0)
150 				errexit("Program terminated.");
151 			fix_cg(cg, c);
152 		}
153 		/*
154 		 * If the on-disk timestamp is in the future, then it
155 		 * by definition is wrong.  Otherwise, if it's in
156 		 * the past, then use that value so that we don't
157 		 * declare a spurious mismatch.
158 		 */
159 		if (now > cg->cg_time)
160 			newcg->cg_time = cg->cg_time;
161 		else
162 			newcg->cg_time = now;
163 		newcg->cg_cgx = c;
164 		dbase = cgbase(fs, c);
165 		dmax = dbase + fs->fs_fpg;
166 		if (dmax > fs->fs_size)
167 			dmax = fs->fs_size;
168 		newcg->cg_ndblk = dmax - dbase;
169 		if (c == fs->fs_ncg - 1)
170 			newcg->cg_ncyl = fs->fs_ncyl - (fs->fs_cpg * c);
171 		else
172 			newcg->cg_ncyl = fs->fs_cpg;
173 		newcg->cg_niblk = sblock.fs_ipg;
174 		newcg->cg_cs.cs_ndir = 0;
175 		newcg->cg_cs.cs_nffree = 0;
176 		newcg->cg_cs.cs_nbfree = 0;
177 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
178 		if ((cg->cg_rotor >= 0) && (cg->cg_rotor < newcg->cg_ndblk))
179 			newcg->cg_rotor = cg->cg_rotor;
180 		else
181 			newcg->cg_rotor = 0;
182 		if ((cg->cg_frotor >= 0) && (cg->cg_frotor < newcg->cg_ndblk))
183 			newcg->cg_frotor = cg->cg_frotor;
184 		else
185 			newcg->cg_frotor = 0;
186 		if ((cg->cg_irotor >= 0) && (cg->cg_irotor < newcg->cg_niblk))
187 			newcg->cg_irotor = cg->cg_irotor;
188 		else
189 			newcg->cg_irotor = 0;
190 		(void) memset((void *)&newcg->cg_frsum[0], 0,
191 		    sizeof (newcg->cg_frsum));
192 		(void) memset((void *)cg_inosused(newcg), 0, (size_t)mapsize);
193 		/* LINTED macro is int32-aligned per newcg->cg_btotoff above */
194 		(void) memset((void *)&cg_blktot(newcg)[0], 0,
195 		    sumsize + mapsize);
196 		j = fs->fs_ipg * c;
197 		for (i = 0; i < fs->fs_ipg; j++, i++) {
198 			switch (statemap[j] & ~(INORPHAN | INDELAYD)) {
199 
200 			case USTATE:
201 				break;
202 
203 			case DSTATE:
204 			case DCLEAR:
205 			case DFOUND:
206 			case DZLINK:
207 				newcg->cg_cs.cs_ndir++;
208 				/* FALLTHROUGH */
209 
210 			case FSTATE:
211 			case FCLEAR:
212 			case FZLINK:
213 			case SSTATE:
214 			case SCLEAR:
215 				newcg->cg_cs.cs_nifree--;
216 				setbit(cg_inosused(newcg), i);
217 				break;
218 
219 			default:
220 				if (j < UFSROOTINO)
221 					break;
222 				errexit("BAD STATE 0x%x FOR INODE I=%d",
223 				    statemap[j], (int)j);
224 			}
225 		}
226 		if (c == 0) {
227 			for (i = 0; i < UFSROOTINO; i++) {
228 				setbit(cg_inosused(newcg), i);
229 				newcg->cg_cs.cs_nifree--;
230 			}
231 		}
232 		/*
233 		 * Count up what fragments and blocks are free, and
234 		 * reflect the relevant section of blockmap[] into
235 		 * newcg's map.
236 		 */
237 		for (i = 0, d = dbase;
238 		    d < dmax;
239 		    d += fs->fs_frag, i += fs->fs_frag) {
240 			frags = 0;
241 			for (j = 0; j < fs->fs_frag; j++) {
242 				if (testbmap(d + j))
243 					continue;
244 				setbit(cg_blksfree(newcg), i + j);
245 				frags++;
246 			}
247 			if (frags == fs->fs_frag) {
248 				newcg->cg_cs.cs_nbfree++;
249 				j = cbtocylno(fs, i);
250 				/* LINTED macro is int32-aligned per above */
251 				cg_blktot(newcg)[j]++;
252 				/* LINTED cg_blks(newcg) is aligned */
253 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
254 			} else if (frags > 0) {
255 				newcg->cg_cs.cs_nffree += frags;
256 				blk = blkmap(fs, cg_blksfree(newcg), i);
257 				fragacct(fs, blk, newcg->cg_frsum, 1);
258 			}
259 		}
260 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
261 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
262 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
263 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
264 
265 		/*
266 		 * Note that, just like the kernel, we dynamically
267 		 * allocated an array to hold the csums and stuffed
268 		 * the pointer into the in-core superblock's fs_u.fs_csp
269 		 * field.  This means that the fs_u field contains a
270 		 * random value when the disk version is examined, but
271 		 * fs_cs() gives us a valid pointer nonetheless.
272 		 * We need to compare the recalculated summaries to
273 		 * both the superblock version and the on disk version.
274 		 * If either is bad, copy the calculated version over
275 		 * the corrupt values.
276 		 */
277 
278 		cs = &fs->fs_cs(fs, c);
279 		bad_csum_sb = (memcmp((void *)cs, (void *)&newcg->cg_cs,
280 		    sizeof (*cs)) != 0);
281 
282 		bad_csum_cg = (memcmp((void *)&cg->cg_cs, (void *)&newcg->cg_cs,
283 		    sizeof (struct csum)) != 0);
284 
285 		/*
286 		 * Has the user told us what to do yet?  If not, find out.
287 		 */
288 		if ((bad_csum_sb || bad_csum_cg) && (update_csums == -1)) {
289 			if (preen) {
290 				update_csums = 1;
291 				(void) printf("CORRECTING BAD CG SUMMARIES"
292 					" FOR CG %d\n", c);
293 			} else if (update_csums == -1) {
294 				update_csums = (reply(
295 				    "CORRECT BAD CG SUMMARIES FOR CG %d",
296 				    c) == 1);
297 			}
298 		}
299 
300 		if (bad_csum_sb && (update_csums == 1)) {
301 			(void) memmove((void *)cs, (void *)&newcg->cg_cs,
302 			    sizeof (*cs));
303 			sbdirty();
304 			(void) printf("CORRECTED SUPERBLOCK SUMMARIES FOR"
305 				    " CG %d\n", c);
306 		}
307 
308 		if (bad_csum_cg && (update_csums == 1)) {
309 			(void) memmove((void *)&cg->cg_cs,
310 				    (void *)&newcg->cg_cs,
311 				    sizeof (struct csum));
312 			/* LINTED per cg_sanity() */
313 			(void) memmove((void *)&cg_blktot(cg)[0],
314 			    /* LINTED macro aligned as above */
315 			    (void *)&cg_blktot(newcg)[0], sumsize);
316 			cgdirty();
317 			(void) printf("CORRECTED SUMMARIES FOR CG %d\n", c);
318 		}
319 
320 		excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
321 		if (excessdirs < 0) {
322 			pfatal("LOST %d DIRECTORIES IN CG %d\n",
323 			    -excessdirs, c);
324 			excessdirs = 0;
325 		}
326 		if (excessdirs > 0) {
327 			if (check_maps((uchar_t *)cg_inosused(newcg),
328 			    (uchar_t *)cg_inosused(cg), inomapsize,
329 			    cg->cg_cgx * fs->fs_ipg, "DIR", 0, excessdirs)) {
330 				if (!verbose)
331 					(void) printf("DIR BITMAP WRONG ");
332 				if (preen || update_bitmaps ||
333 				    reply("FIX") == 1) {
334 					(void) memmove((void *)cg_inosused(cg),
335 					    (void *)cg_inosused(newcg),
336 					    inomapsize);
337 					cgdirty();
338 					if (preen ||
339 					    (!verbose && update_bitmaps))
340 						(void) printf("(CORRECTED)\n");
341 					update_bitmaps = 1;
342 				}
343 			}
344 		}
345 
346 		if (check_maps((uchar_t *)cg_inosused(newcg),
347 		    (uchar_t *)cg_inosused(cg), inomapsize,
348 		    cg->cg_cgx * fs->fs_ipg, "FILE", excessdirs, fs->fs_ipg)) {
349 			if (!verbose)
350 				(void) printf("FILE BITMAP WRONG ");
351 			if (preen || update_bitmaps || reply("FIX") == 1) {
352 				(void) memmove((void *)cg_inosused(cg),
353 				    (void *)cg_inosused(newcg), inomapsize);
354 				cgdirty();
355 				if (preen ||
356 				    (!verbose && update_bitmaps))
357 					(void) printf("(CORRECTED)\n");
358 				update_bitmaps = 1;
359 			}
360 		}
361 
362 		if (check_maps((uchar_t *)cg_blksfree(cg),
363 		    (uchar_t *)cg_blksfree(newcg), blkmapsize,
364 		    cg->cg_cgx * fs->fs_fpg, "FRAG", 0, fs->fs_fpg)) {
365 			if (!verbose)
366 				(void) printf("FRAG BITMAP WRONG ");
367 			if (preen || update_bitmaps || reply("FIX") == 1) {
368 				(void) memmove((void *)cg_blksfree(cg),
369 				    (void *)cg_blksfree(newcg), blkmapsize);
370 				cgdirty();
371 				if (preen ||
372 				    (!verbose && update_bitmaps))
373 					(void) printf("(CORRECTED)\n");
374 				update_bitmaps = 1;
375 			}
376 		}
377 
378 		/*
379 		 * Fixing one set of problems often shows up more in the
380 		 * same cg.  Just to make sure, go back and check it
381 		 * again if we found something this time through.
382 		 */
383 		if (cgisdirty()) {
384 			cgflush();
385 			cstotal = backup_cs;
386 			c--;
387 		}
388 	}
389 
390 	if ((fflag || !(islog && islogok)) &&
391 	    (memcmp((void *)&cstotal, (void *)&fs->fs_cstotal,
392 	    sizeof (struct csum)) != 0)) {
393 		if (dofix(&idesc, "CORRECT GLOBAL SUMMARY")) {
394 			(void) memmove((void *)&fs->fs_cstotal,
395 			    (void *)&cstotal, sizeof (struct csum));
396 			fs->fs_ronly = 0;
397 			fs->fs_fmod = 0;
398 			sbdirty();
399 		} else {
400 			iscorrupt = 1;
401 		}
402 	}
403 }
404 
405 /*
406  * Compare two allocation bitmaps, reporting any discrepancies.
407  *
408  * If a mismatch is found, if the bit is set in map1, it's considered
409  * to be an indication that the corresponding resource is supposed
410  * to be free, but isn't.  Otherwise, it's considered marked as allocated
411  * but not found to be so.  In other words, if the two maps being compared
412  * use a set bit to indicate something is free, pass the on-disk map
413  * first.  Otherwise, pass the calculated map first.
414  */
415 static int
416 check_maps(
417 	uchar_t *map1,	/* map of claimed allocations */
418 	uchar_t *map2,	/* map of determined allocations */
419 	int mapsize,	/* size of above two maps */
420 	int startvalue,	/* resource value for first element in map */
421 	char *name,	/* name of resource found in maps */
422 	int skip,	/* number of entries to skip before starting to free */
423 	int limit)	/* limit on number of entries to free */
424 {
425 	long i, j, k, l, m, n, size;
426 	int astart, aend, ustart, uend;
427 	int mismatch;
428 
429 	mismatch = 0;
430 	astart = ustart = aend = uend = -1;
431 	for (i = 0; i < mapsize; i++) {
432 		j = *map1++;
433 		k = *map2++;
434 		if (j == k)
435 			continue;
436 		for (m = 0, l = 1; m < NBBY; m++, l <<= 1) {
437 			if ((j & l) == (k & l))
438 				continue;
439 			n = startvalue + i * NBBY + m;
440 			if ((j & l) != 0) {
441 				if (astart == -1) {
442 					astart = aend = n;
443 					continue;
444 				}
445 				if (aend + 1 == n) {
446 					aend = n;
447 					continue;
448 				}
449 				if (verbose) {
450 					if (astart == aend)
451 						pwarn(
452 			    "ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
453 						    name, astart);
454 					else
455 						pwarn(
456 			    "ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
457 						    name, astart, aend);
458 				}
459 				mismatch = 1;
460 				astart = aend = n;
461 			} else {
462 				if (ustart == -1) {
463 					ustart = uend = n;
464 					continue;
465 				}
466 				if (uend + 1 == n) {
467 					uend = n;
468 					continue;
469 				}
470 				size = uend - ustart + 1;
471 				if (size <= skip) {
472 					skip -= size;
473 					ustart = uend = n;
474 					continue;
475 				}
476 				if (skip > 0) {
477 					ustart += skip;
478 					size -= skip;
479 					skip = 0;
480 				}
481 				if (size > limit)
482 					size = limit;
483 				if (verbose) {
484 					if (size == 1)
485 						pwarn(
486 			    "UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
487 						    name, ustart);
488 					else
489 						pwarn(
490 			    "UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
491 						    name, ustart,
492 						    ustart + size - 1);
493 				}
494 				mismatch = 1;
495 				limit -= size;
496 				if (limit <= 0)
497 					return (mismatch);
498 				ustart = uend = n;
499 			}
500 		}
501 	}
502 	if (astart != -1) {
503 		if (verbose) {
504 			if (astart == aend)
505 				pwarn(
506 			    "ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
507 				    name, astart);
508 			else
509 				pwarn(
510 			    "ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
511 				    name, astart, aend);
512 		}
513 		mismatch = 1;
514 	}
515 	if (ustart != -1) {
516 		size = uend - ustart + 1;
517 		if (size <= skip)
518 			return (mismatch);
519 		if (skip > 0) {
520 			ustart += skip;
521 			size -= skip;
522 		}
523 		if (size > limit)
524 			size = limit;
525 		if (verbose) {
526 			if (size == 1)
527 				pwarn(
528 			    "UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
529 				    name, ustart);
530 			else
531 				pwarn(
532 		    "UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
533 				    name, ustart, ustart + size - 1);
534 		}
535 		mismatch = 1;
536 	}
537 	return (mismatch);
538 }
539