pass5.c revision 107214
1/*
2 * Copyright (c) 1980, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static const char sccsid[] = "@(#)pass5.c	8.9 (Berkeley) 4/28/95";
37#endif /* not lint */
38#endif
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/sbin/fsck_ffs/pass5.c 107213 2002-11-24 21:41:42Z julian $");
42
43#include <sys/param.h>
44#include <sys/sysctl.h>
45
46#include <ufs/ufs/dinode.h>
47#include <ufs/ffs/fs.h>
48
49#include <err.h>
50#include <limits.h>
51#include <string.h>
52
53#include "fsck.h"
54
55static void check_maps(u_char *, u_char *, int, int, const char *, int *, int, int);
56
57void
58pass5(void)
59{
60	int c, i, j, blk, frags, basesize, mapsize;
61	int inomapsize, blkmapsize;
62	struct fs *fs = &sblock;
63	struct cg *cg = &cgrp;
64	ufs2_daddr_t d, dbase, dmax;
65	int excessdirs, rewritecg = 0;
66	struct csum *cs;
67	struct csum_total cstotal;
68	struct inodesc idesc[3];
69	char buf[MAXBSIZE];
70	struct cg *newcg = (struct cg *)buf;
71
72	inoinfo(WINO)->ino_state = USTATE;
73	memset(newcg, 0, (size_t)fs->fs_cgsize);
74	newcg->cg_niblk = fs->fs_ipg;
75	if (cvtlevel >= 3) {
76		if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
77			if (preen)
78				pwarn("DELETING CLUSTERING MAPS\n");
79			if (preen || reply("DELETE CLUSTERING MAPS")) {
80				fs->fs_contigsumsize = 0;
81				rewritecg = 1;
82				sbdirty();
83			}
84		}
85		if (fs->fs_maxcontig > 1) {
86			const char *doit = 0;
87
88			if (fs->fs_contigsumsize < 1) {
89				doit = "CREAT";
90			} else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
91				   fs->fs_contigsumsize < FS_MAXCONTIG) {
92				doit = "EXPAND";
93			}
94			if (doit) {
95				i = fs->fs_contigsumsize;
96				fs->fs_contigsumsize =
97				    MIN(fs->fs_maxcontig, FS_MAXCONTIG);
98				if (CGSIZE(fs) > (u_int)fs->fs_bsize) {
99					pwarn("CANNOT %s CLUSTER MAPS\n", doit);
100					fs->fs_contigsumsize = i;
101				} else if (preen ||
102				    reply("CREATE CLUSTER MAPS")) {
103					if (preen)
104						pwarn("%sING CLUSTER MAPS\n",
105						    doit);
106					fs->fs_cgsize =
107					    fragroundup(fs, CGSIZE(fs));
108					rewritecg = 1;
109					sbdirty();
110				}
111			}
112		}
113	}
114	basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
115	if (sblock.fs_magic == FS_UFS2_MAGIC) {
116		newcg->cg_iusedoff = basesize;
117	} else {
118		/*
119		 * We reserve the space for the old rotation summary
120		 * tables for the benefit of old kernels, but do not
121		 * maintain them in modern kernels. In time, they can
122		 * go away.
123		 */
124		newcg->cg_old_btotoff = basesize;
125		newcg->cg_old_boff = newcg->cg_old_btotoff +
126		    fs->fs_old_cpg * sizeof(int32_t);
127		newcg->cg_iusedoff = newcg->cg_old_boff +
128		    fs->fs_old_cpg * fs->fs_old_nrpos * sizeof(u_int16_t);
129		memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
130	}
131	inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
132	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
133	blkmapsize = howmany(fs->fs_fpg, CHAR_BIT);
134	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
135	if (fs->fs_contigsumsize > 0) {
136		newcg->cg_clustersumoff = newcg->cg_nextfreeoff -
137		    sizeof(u_int32_t);
138		newcg->cg_clustersumoff =
139		    roundup(newcg->cg_clustersumoff, sizeof(u_int32_t));
140		newcg->cg_clusteroff = newcg->cg_clustersumoff +
141		    (fs->fs_contigsumsize + 1) * sizeof(u_int32_t);
142		newcg->cg_nextfreeoff = newcg->cg_clusteroff +
143		    howmany(fragstoblks(fs, fs->fs_fpg), CHAR_BIT);
144	}
145	newcg->cg_magic = CG_MAGIC;
146	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
147	memset(&idesc[0], 0, sizeof idesc);
148	for (i = 0; i < 3; i++)
149		idesc[i].id_type = ADDR;
150	memset(&cstotal, 0, sizeof(struct csum_total));
151	dmax = blknum(fs, fs->fs_size + fs->fs_frag - 1);
152	for (d = fs->fs_size; d < dmax; d++)
153		setbmap(d);
154	for (c = 0; c < fs->fs_ncg; c++) {
155		if (got_siginfo) {
156			printf("%s: phase 5: cyl group %d of %d (%d%%)\n",
157			    cdevname, c, sblock.fs_ncg,
158			    c * 100 / sblock.fs_ncg);
159			got_siginfo = 0;
160		}
161		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
162		if (!cg_chkmagic(cg))
163			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
164		newcg->cg_time = cg->cg_time;
165		newcg->cg_old_time = cg->cg_old_time;
166		newcg->cg_cgx = c;
167		dbase = cgbase(fs, c);
168		dmax = dbase + fs->fs_fpg;
169		if (dmax > fs->fs_size)
170			dmax = fs->fs_size;
171		newcg->cg_ndblk = dmax - dbase;
172		if (fs->fs_magic == FS_UFS1_MAGIC) {
173			if (c == fs->fs_ncg - 1)
174				newcg->cg_old_ncyl = howmany(newcg->cg_ndblk,
175				    fs->fs_fpg / fs->fs_old_cpg);
176			else
177				newcg->cg_old_ncyl = fs->fs_old_cpg;
178			newcg->cg_old_niblk = fs->fs_ipg;
179			newcg->cg_niblk = 0;
180		}
181		if (fs->fs_contigsumsize > 0)
182			newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
183		newcg->cg_cs.cs_ndir = 0;
184		newcg->cg_cs.cs_nffree = 0;
185		newcg->cg_cs.cs_nbfree = 0;
186		newcg->cg_cs.cs_nifree = fs->fs_ipg;
187		if ((cg->cg_rotor >= 0) && (cg->cg_rotor < newcg->cg_ndblk))
188			newcg->cg_rotor = cg->cg_rotor;
189		else
190			newcg->cg_rotor = 0;
191		if ((cg->cg_frotor >= 0) && (cg->cg_frotor < newcg->cg_ndblk))
192			newcg->cg_frotor = cg->cg_frotor;
193		else
194			newcg->cg_frotor = 0;
195		if ((cg->cg_irotor >= 0) && (cg->cg_irotor < fs->fs_ipg))
196			newcg->cg_irotor = cg->cg_irotor;
197		else
198			newcg->cg_irotor = 0;
199		if (fs->fs_magic == FS_UFS1_MAGIC) {
200			newcg->cg_initediblk = 0;
201		} else {
202			if ((unsigned)cg->cg_initediblk > fs->fs_ipg)
203				newcg->cg_initediblk = fs->fs_ipg;
204			else
205				newcg->cg_initediblk = cg->cg_initediblk;
206		}
207		memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
208		memset(cg_inosused(newcg), 0, (size_t)(mapsize));
209		j = fs->fs_ipg * c;
210		for (i = 0; i < inostathead[c].il_numalloced; j++, i++) {
211			switch (inoinfo(j)->ino_state) {
212
213			case USTATE:
214				break;
215
216			case DSTATE:
217			case DCLEAR:
218			case DFOUND:
219				newcg->cg_cs.cs_ndir++;
220				/* FALLTHROUGH */
221
222			case FSTATE:
223			case FCLEAR:
224				newcg->cg_cs.cs_nifree--;
225				setbit(cg_inosused(newcg), i);
226				break;
227
228			default:
229				if (j < (int)ROOTINO)
230					break;
231				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
232				    inoinfo(j)->ino_state, j);
233			}
234		}
235		if (c == 0)
236			for (i = 0; i < (int)ROOTINO; i++) {
237				setbit(cg_inosused(newcg), i);
238				newcg->cg_cs.cs_nifree--;
239			}
240		for (i = 0, d = dbase;
241		     d < dmax;
242		     d += fs->fs_frag, i += fs->fs_frag) {
243			frags = 0;
244			for (j = 0; j < fs->fs_frag; j++) {
245				if (testbmap(d + j))
246					continue;
247				setbit(cg_blksfree(newcg), i + j);
248				frags++;
249			}
250			if (frags == fs->fs_frag) {
251				newcg->cg_cs.cs_nbfree++;
252				if (fs->fs_contigsumsize > 0)
253					setbit(cg_clustersfree(newcg),
254					    i / fs->fs_frag);
255			} else if (frags > 0) {
256				newcg->cg_cs.cs_nffree += frags;
257				blk = blkmap(fs, cg_blksfree(newcg), i);
258				ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
259			}
260		}
261		if (fs->fs_contigsumsize > 0) {
262			int32_t *sump = cg_clustersum(newcg);
263			u_char *mapp = cg_clustersfree(newcg);
264			int map = *mapp++;
265			int bit = 1;
266			int run = 0;
267
268			for (i = 0; i < newcg->cg_nclusterblks; i++) {
269				if ((map & bit) != 0) {
270					run++;
271				} else if (run != 0) {
272					if (run > fs->fs_contigsumsize)
273						run = fs->fs_contigsumsize;
274					sump[run]++;
275					run = 0;
276				}
277				if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
278					bit <<= 1;
279				} else {
280					map = *mapp++;
281					bit = 1;
282				}
283			}
284			if (run != 0) {
285				if (run > fs->fs_contigsumsize)
286					run = fs->fs_contigsumsize;
287				sump[run]++;
288			}
289		}
290		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
291		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
292		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
293		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
294		cs = &fs->fs_cs(fs, c);
295		if (cursnapshot == 0 &&
296		    memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
297		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
298			memmove(cs, &newcg->cg_cs, sizeof *cs);
299			sbdirty();
300		}
301		if (rewritecg) {
302			memmove(cg, newcg, (size_t)fs->fs_cgsize);
303			cgdirty();
304			continue;
305		}
306		if (cursnapshot == 0 &&
307		    memcmp(newcg, cg, basesize) != 0 &&
308		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
309			memmove(cg, newcg, (size_t)basesize);
310			cgdirty();
311		}
312		if (bkgrdflag != 0 || usedsoftdep || debug) {
313			excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
314			if (excessdirs < 0) {
315				pfatal("LOST %d DIRECTORIES\n", -excessdirs);
316				excessdirs = 0;
317			}
318			if (excessdirs > 0)
319				check_maps(cg_inosused(newcg), cg_inosused(cg),
320				    inomapsize, cg->cg_cgx * fs->fs_ipg, "DIR",
321				    freedirs, 0, excessdirs);
322			check_maps(cg_inosused(newcg), cg_inosused(cg),
323			    inomapsize, cg->cg_cgx * fs->fs_ipg, "FILE",
324			    freefiles, excessdirs, fs->fs_ipg);
325			check_maps(cg_blksfree(cg), cg_blksfree(newcg),
326			    blkmapsize, cg->cg_cgx * fs->fs_fpg, "FRAG",
327			    freeblks, 0, fs->fs_fpg);
328		}
329		if (cursnapshot == 0 &&
330		    memcmp(cg_inosused(newcg), cg_inosused(cg), mapsize) != 0 &&
331		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
332			memmove(cg_inosused(cg), cg_inosused(newcg),
333			      (size_t)mapsize);
334			cgdirty();
335		}
336	}
337	if (cursnapshot == 0 &&
338	    memcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal) != 0
339	    && dofix(&idesc[0], "SUMMARY BLK COUNT(S) WRONG IN SUPERBLK")) {
340		memmove(&fs->fs_cstotal, &cstotal, sizeof cstotal);
341		fs->fs_ronly = 0;
342		fs->fs_fmod = 0;
343		sbdirty();
344	}
345}
346
347static void
348check_maps(
349	u_char *map1,	/* map of claimed allocations */
350	u_char *map2,	/* map of determined allocations */
351	int mapsize,	/* size of above two maps */
352	int startvalue,	/* resource value for first element in map */
353	const char *name,	/* name of resource found in maps */
354	int *opcode,	/* sysctl opcode to free resource */
355	int skip,	/* number of entries to skip before starting to free */
356	int limit)	/* limit on number of entries to free */
357{
358#	define BUFSIZE 16
359	char buf[BUFSIZE];
360	long i, j, k, l, m, n, size;
361	int astart, aend, ustart, uend;
362	void (*msg)(const char *fmt, ...);
363
364	if (bkgrdflag)
365		msg = pfatal;
366	else
367		msg = pwarn;
368	astart = ustart = aend = uend = -1;
369	for (i = 0; i < mapsize; i++) {
370		j = *map1++;
371		k = *map2++;
372		if (j == k)
373			continue;
374		for (m = 0, l = 1; m < CHAR_BIT; m++, l <<= 1) {
375			if ((j & l) == (k & l))
376				continue;
377			n = startvalue + i * CHAR_BIT + m;
378			if ((j & l) != 0) {
379				if (astart == -1) {
380					astart = aend = n;
381					continue;
382				}
383				if (aend + 1 == n) {
384					aend = n;
385					continue;
386				}
387				if (astart == aend)
388					(*msg)("ALLOCATED %s %d MARKED FREE\n",
389					    name, astart);
390				else
391					(*msg)("%s %sS %d-%d MARKED FREE\n",
392					    "ALLOCATED", name, astart, aend);
393				astart = aend = n;
394			} else {
395				if (ustart == -1) {
396					ustart = uend = n;
397					continue;
398				}
399				if (uend + 1 == n) {
400					uend = n;
401					continue;
402				}
403				size = uend - ustart + 1;
404				if (size <= skip) {
405					skip -= size;
406					ustart = uend = n;
407					continue;
408				}
409				if (skip > 0) {
410					ustart += skip;
411					size -= skip;
412					skip = 0;
413				}
414				if (size > limit)
415					size = limit;
416				if (debug && size == 1)
417					pwarn("%s %s %d MARKED USED\n",
418					    "UNALLOCATED", name, ustart);
419				else if (debug)
420					pwarn("%s %sS %d-%ld MARKED USED\n",
421					    "UNALLOCATED", name, ustart,
422					    ustart + size - 1);
423				if (bkgrdflag != 0) {
424					cmd.value = ustart;
425					cmd.size = size;
426					if (sysctl(opcode, MIBSIZE, 0, 0,
427					    &cmd, sizeof cmd) == -1) {
428						snprintf(buf, BUFSIZE,
429						    "FREE %s", name);
430						rwerror(buf, cmd.value);
431					}
432				}
433				limit -= size;
434				if (limit <= 0)
435					return;
436				ustart = uend = n;
437			}
438		}
439	}
440	if (astart != -1) {
441		if (astart == aend)
442			(*msg)("ALLOCATED %s %d MARKED FREE\n", name, astart);
443		else
444			(*msg)("ALLOCATED %sS %d-%d MARKED FREE\n",
445			    name, astart, aend);
446	}
447	if (ustart != -1) {
448		size = uend - ustart + 1;
449		if (size <= skip)
450			return;
451		if (skip > 0) {
452			ustart += skip;
453			size -= skip;
454		}
455		if (size > limit)
456			size = limit;
457		if (debug) {
458			if (size == 1)
459				pwarn("UNALLOCATED %s %d MARKED USED\n",
460				    name, ustart);
461			else
462				pwarn("UNALLOCATED %sS %d-%ld MARKED USED\n",
463				    name, ustart, ustart + size - 1);
464		}
465		if (bkgrdflag != 0) {
466			cmd.value = ustart;
467			cmd.size = size;
468			if (sysctl(opcode, MIBSIZE, 0, 0, &cmd,
469			    sizeof cmd) == -1) {
470				snprintf(buf, BUFSIZE, "FREE %s", name);
471				rwerror(buf, cmd.value);
472			}
473		}
474	}
475}
476