Deleted Added
full compact
pass1.c (256281) pass1.c (260178)
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
33#endif /* not lint */
34#endif
35#include <sys/cdefs.h>
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
33#endif /* not lint */
34#endif
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: stable/10/sbin/fsck_ffs/pass1.c 248658 2013-03-23 20:00:02Z mckusick $");
36__FBSDID("$FreeBSD: stable/10/sbin/fsck_ffs/pass1.c 260178 2014-01-02 01:44:14Z scottl $");
37
38#include <sys/param.h>
39#include <sys/stat.h>
40#include <sys/sysctl.h>
41
42#include <ufs/ufs/dinode.h>
43#include <ufs/ufs/dir.h>
44#include <ufs/ffs/fs.h>
45
46#include <err.h>
47#include <limits.h>
48#include <stdint.h>
49#include <string.h>
50
51#include "fsck.h"
52
53static ufs2_daddr_t badblk;
54static ufs2_daddr_t dupblk;
55static ino_t lastino; /* last inode in use */
56
57static int checkinode(ino_t inumber, struct inodesc *, int rebuildcg);
58
59void
60pass1(void)
61{
62 struct inostat *info;
63 struct inodesc idesc;
64 struct bufarea *cgbp;
65 struct cg *cgp;
66 ino_t inumber, inosused, mininos;
67 ufs2_daddr_t i, cgd;
68 u_int8_t *cp;
69 int c, rebuildcg;
70
37
38#include <sys/param.h>
39#include <sys/stat.h>
40#include <sys/sysctl.h>
41
42#include <ufs/ufs/dinode.h>
43#include <ufs/ufs/dir.h>
44#include <ufs/ffs/fs.h>
45
46#include <err.h>
47#include <limits.h>
48#include <stdint.h>
49#include <string.h>
50
51#include "fsck.h"
52
53static ufs2_daddr_t badblk;
54static ufs2_daddr_t dupblk;
55static ino_t lastino; /* last inode in use */
56
57static int checkinode(ino_t inumber, struct inodesc *, int rebuildcg);
58
59void
60pass1(void)
61{
62 struct inostat *info;
63 struct inodesc idesc;
64 struct bufarea *cgbp;
65 struct cg *cgp;
66 ino_t inumber, inosused, mininos;
67 ufs2_daddr_t i, cgd;
68 u_int8_t *cp;
69 int c, rebuildcg;
70
71 badblk = dupblk = lastino = 0;
72
71 /*
72 * Set file system reserved blocks in used block map.
73 */
74 for (c = 0; c < sblock.fs_ncg; c++) {
75 cgd = cgdmin(&sblock, c);
76 if (c == 0) {
77 i = cgbase(&sblock, c);
78 } else
79 i = cgsblock(&sblock, c);
80 for (; i < cgd; i++)
81 setbmap(i);
82 }
83 i = sblock.fs_csaddr;
84 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
85 for (; i < cgd; i++)
86 setbmap(i);
87
88 /*
89 * Find all allocated blocks.
90 */
91 memset(&idesc, 0, sizeof(struct inodesc));
92 idesc.id_func = pass1check;
93 n_files = n_blks = 0;
94 for (c = 0; c < sblock.fs_ncg; c++) {
95 inumber = c * sblock.fs_ipg;
96 setinodebuf(inumber);
97 cgbp = cgget(c);
98 cgp = cgbp->b_un.b_cg;
99 rebuildcg = 0;
100 if (!check_cgmagic(c, cgbp))
101 rebuildcg = 1;
102 if (!rebuildcg && sblock.fs_magic == FS_UFS2_MAGIC) {
103 inosused = cgp->cg_initediblk;
104 if (inosused > sblock.fs_ipg) {
105 pfatal(
106"Too many initialized inodes (%ju > %d) in cylinder group %d\nReset to %d\n",
107 (uintmax_t)inosused,
108 sblock.fs_ipg, c, sblock.fs_ipg);
109 inosused = sblock.fs_ipg;
110 }
111 } else {
112 inosused = sblock.fs_ipg;
113 }
114 if (got_siginfo) {
115 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
116 cdevname, c, sblock.fs_ncg,
117 c * 100 / sblock.fs_ncg);
118 got_siginfo = 0;
119 }
120 if (got_sigalarm) {
121 setproctitle("%s p1 %d%%", cdevname,
122 c * 100 / sblock.fs_ncg);
123 got_sigalarm = 0;
124 }
125 /*
126 * If we are using soft updates, then we can trust the
127 * cylinder group inode allocation maps to tell us which
128 * inodes are allocated. We will scan the used inode map
129 * to find the inodes that are really in use, and then
130 * read only those inodes in from disk.
131 */
132 if ((preen || inoopt) && usedsoftdep && !rebuildcg) {
133 cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
134 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
135 if (*cp == 0)
136 continue;
137 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
138 if (*cp & i)
139 break;
140 inosused--;
141 }
142 break;
143 }
144 if (inosused < 0)
145 inosused = 0;
146 }
147 /*
148 * Allocate inoinfo structures for the allocated inodes.
149 */
150 inostathead[c].il_numalloced = inosused;
151 if (inosused == 0) {
152 inostathead[c].il_stat = 0;
153 continue;
154 }
155 info = Calloc((unsigned)inosused, sizeof(struct inostat));
156 if (info == NULL)
157 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
158 (unsigned)(sizeof(struct inostat) * inosused));
159 inostathead[c].il_stat = info;
160 /*
161 * Scan the allocated inodes.
162 */
163 for (i = 0; i < inosused; i++, inumber++) {
164 if (inumber < ROOTINO) {
165 (void)getnextinode(inumber, rebuildcg);
166 continue;
167 }
168 /*
169 * NULL return indicates probable end of allocated
170 * inodes during cylinder group rebuild attempt.
171 * We always keep trying until we get to the minimum
172 * valid number for this cylinder group.
173 */
174 if (checkinode(inumber, &idesc, rebuildcg) == 0 &&
175 i > cgp->cg_initediblk)
176 break;
177 }
178 /*
179 * This optimization speeds up future runs of fsck
180 * by trimming down the number of inodes in cylinder
181 * groups that formerly had many inodes but now have
182 * fewer in use.
183 */
184 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
185 if (inoopt && !preen && !rebuildcg &&
186 sblock.fs_magic == FS_UFS2_MAGIC &&
187 cgp->cg_initediblk > 2 * INOPB(&sblock) &&
188 mininos < cgp->cg_initediblk) {
189 i = cgp->cg_initediblk;
190 if (mininos < 2 * INOPB(&sblock))
191 cgp->cg_initediblk = 2 * INOPB(&sblock);
192 else
193 cgp->cg_initediblk = mininos;
194 pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
195 c, i, cgp->cg_initediblk, "VALID INODES");
196 dirty(cgbp);
197 }
198 if (inosused < sblock.fs_ipg)
199 continue;
200 lastino += 1;
201 if (lastino < (c * sblock.fs_ipg))
202 inosused = 0;
203 else
204 inosused = lastino - (c * sblock.fs_ipg);
205 if (rebuildcg && inosused > cgp->cg_initediblk &&
206 sblock.fs_magic == FS_UFS2_MAGIC) {
207 cgp->cg_initediblk = roundup(inosused, INOPB(&sblock));
208 pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
209 cgp->cg_initediblk);
210 }
211 /*
212 * If we were not able to determine in advance which inodes
213 * were in use, then reduce the size of the inoinfo structure
214 * to the size necessary to describe the inodes that we
215 * really found.
216 */
217 if (inumber == lastino)
218 continue;
219 inostathead[c].il_numalloced = inosused;
220 if (inosused == 0) {
221 free(inostathead[c].il_stat);
222 inostathead[c].il_stat = 0;
223 continue;
224 }
225 info = Calloc((unsigned)inosused, sizeof(struct inostat));
226 if (info == NULL)
227 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
228 (unsigned)(sizeof(struct inostat) * inosused));
229 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
230 free(inostathead[c].il_stat);
231 inostathead[c].il_stat = info;
232 }
233 freeinodebuf();
234}
235
236static int
237checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg)
238{
239 union dinode *dp;
240 off_t kernmaxfilesize;
241 ufs2_daddr_t ndb;
242 mode_t mode;
243 int j, ret, offset;
244
245 if ((dp = getnextinode(inumber, rebuildcg)) == NULL)
246 return (0);
247 mode = DIP(dp, di_mode) & IFMT;
248 if (mode == 0) {
249 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
250 (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
251 NDADDR * sizeof(ufs1_daddr_t)) ||
252 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
253 NIADDR * sizeof(ufs1_daddr_t)) ||
254 dp->dp1.di_mode || dp->dp1.di_size)) ||
255 (sblock.fs_magic == FS_UFS2_MAGIC &&
256 (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
257 NDADDR * sizeof(ufs2_daddr_t)) ||
258 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
259 NIADDR * sizeof(ufs2_daddr_t)) ||
260 dp->dp2.di_mode || dp->dp2.di_size))) {
261 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
262 (u_long)inumber);
263 if (reply("CLEAR") == 1) {
264 dp = ginode(inumber);
265 clearinode(dp);
266 inodirty();
267 }
268 }
269 inoinfo(inumber)->ino_state = USTATE;
270 return (1);
271 }
272 lastino = inumber;
273 /* This should match the file size limit in ffs_mountfs(). */
274 if (sblock.fs_magic == FS_UFS1_MAGIC)
275 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
276 else
277 kernmaxfilesize = sblock.fs_maxfilesize;
278 if (DIP(dp, di_size) > kernmaxfilesize ||
279 DIP(dp, di_size) > sblock.fs_maxfilesize ||
280 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
281 if (debug)
282 printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
283 goto unknown;
284 }
285 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
286 dp = ginode(inumber);
287 DIP_SET(dp, di_size, sblock.fs_fsize);
288 DIP_SET(dp, di_mode, IFREG|0600);
289 inodirty();
290 }
291 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
292 mode == IFSOCK) && DIP(dp, di_size) != 0) {
293 if (debug)
294 printf("bad special-file size %ju:",
295 (uintmax_t)DIP(dp, di_size));
296 goto unknown;
297 }
298 if ((mode == IFBLK || mode == IFCHR) &&
299 (dev_t)DIP(dp, di_rdev) == NODEV) {
300 if (debug)
301 printf("bad special-file rdev NODEV:");
302 goto unknown;
303 }
304 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
305 if (ndb < 0) {
306 if (debug)
307 printf("bad size %ju ndb %ju:",
308 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
309 goto unknown;
310 }
311 if (mode == IFBLK || mode == IFCHR)
312 ndb++;
313 if (mode == IFLNK) {
314 /*
315 * Fake ndb value so direct/indirect block checks below
316 * will detect any garbage after symlink string.
317 */
318 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
319 if (sblock.fs_magic == FS_UFS1_MAGIC)
320 ndb = howmany(DIP(dp, di_size),
321 sizeof(ufs1_daddr_t));
322 else
323 ndb = howmany(DIP(dp, di_size),
324 sizeof(ufs2_daddr_t));
325 if (ndb > NDADDR) {
326 j = ndb - NDADDR;
327 for (ndb = 1; j > 1; j--)
328 ndb *= NINDIR(&sblock);
329 ndb += NDADDR;
330 }
331 }
332 }
333 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
334 if (DIP(dp, di_db[j]) != 0) {
335 if (debug)
336 printf("bad direct addr[%d]: %ju\n", j,
337 (uintmax_t)DIP(dp, di_db[j]));
338 goto unknown;
339 }
340 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
341 ndb /= NINDIR(&sblock);
342 for (; j < NIADDR; j++)
343 if (DIP(dp, di_ib[j]) != 0) {
344 if (debug)
345 printf("bad indirect addr: %ju\n",
346 (uintmax_t)DIP(dp, di_ib[j]));
347 goto unknown;
348 }
349 if (ftypeok(dp) == 0)
350 goto unknown;
351 n_files++;
352 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
353 if (mode == IFDIR) {
354 if (DIP(dp, di_size) == 0)
355 inoinfo(inumber)->ino_state = DCLEAR;
356 else if (DIP(dp, di_nlink) <= 0)
357 inoinfo(inumber)->ino_state = DZLINK;
358 else
359 inoinfo(inumber)->ino_state = DSTATE;
360 cacheino(dp, inumber);
361 countdirs++;
362 } else if (DIP(dp, di_nlink) <= 0)
363 inoinfo(inumber)->ino_state = FZLINK;
364 else
365 inoinfo(inumber)->ino_state = FSTATE;
366 inoinfo(inumber)->ino_type = IFTODT(mode);
367 badblk = dupblk = 0;
368 idesc->id_number = inumber;
369 if (DIP(dp, di_flags) & SF_SNAPSHOT)
370 idesc->id_type = SNAP;
371 else
372 idesc->id_type = ADDR;
373 (void)ckinode(dp, idesc);
374 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
375 idesc->id_type = ADDR;
376 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
377 for (j = 0; j < NXADDR; j++) {
378 if (--ndb == 0 &&
379 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
380 idesc->id_numfrags = numfrags(&sblock,
381 fragroundup(&sblock, offset));
382 else
383 idesc->id_numfrags = sblock.fs_frag;
384 if (dp->dp2.di_extb[j] == 0)
385 continue;
386 idesc->id_blkno = dp->dp2.di_extb[j];
387 ret = (*idesc->id_func)(idesc);
388 if (ret & STOP)
389 break;
390 }
391 }
392 if (sblock.fs_magic == FS_UFS2_MAGIC)
393 eascan(idesc, &dp->dp2);
394 idesc->id_entryno *= btodb(sblock.fs_fsize);
395 if (DIP(dp, di_blocks) != idesc->id_entryno) {
396 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
397 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
398 (uintmax_t)idesc->id_entryno);
399 if (preen)
400 printf(" (CORRECTED)\n");
401 else if (reply("CORRECT") == 0)
402 return (1);
403 if (bkgrdflag == 0) {
404 dp = ginode(inumber);
405 DIP_SET(dp, di_blocks, idesc->id_entryno);
406 inodirty();
407 } else {
408 cmd.value = idesc->id_number;
409 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
410 if (debug)
411 printf("adjblkcnt ino %ju amount %lld\n",
412 (uintmax_t)cmd.value, (long long)cmd.size);
413 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
414 &cmd, sizeof cmd) == -1)
415 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
416 }
417 }
418 return (1);
419unknown:
420 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
421 inoinfo(inumber)->ino_state = FCLEAR;
422 if (reply("CLEAR") == 1) {
423 inoinfo(inumber)->ino_state = USTATE;
424 dp = ginode(inumber);
425 clearinode(dp);
426 inodirty();
427 }
428 return (1);
429}
430
431int
432pass1check(struct inodesc *idesc)
433{
434 int res = KEEPON;
435 int anyout, nfrags;
436 ufs2_daddr_t blkno = idesc->id_blkno;
437 struct dups *dlp;
438 struct dups *new;
439
440 if (idesc->id_type == SNAP) {
441 if (blkno == BLK_NOCOPY)
442 return (KEEPON);
443 if (idesc->id_number == cursnapshot) {
444 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
445 return (KEEPON);
446 if (blkno == BLK_SNAP) {
447 blkno = blkstofrags(&sblock, idesc->id_lbn);
448 idesc->id_entryno -= idesc->id_numfrags;
449 }
450 } else {
451 if (blkno == BLK_SNAP)
452 return (KEEPON);
453 }
454 }
455 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
456 blkerror(idesc->id_number, "BAD", blkno);
457 if (badblk++ >= MAXBAD) {
458 pwarn("EXCESSIVE BAD BLKS I=%lu",
459 (u_long)idesc->id_number);
460 if (preen)
461 printf(" (SKIPPING)\n");
462 else if (reply("CONTINUE") == 0) {
463 ckfini(0);
464 exit(EEXIT);
465 }
73 /*
74 * Set file system reserved blocks in used block map.
75 */
76 for (c = 0; c < sblock.fs_ncg; c++) {
77 cgd = cgdmin(&sblock, c);
78 if (c == 0) {
79 i = cgbase(&sblock, c);
80 } else
81 i = cgsblock(&sblock, c);
82 for (; i < cgd; i++)
83 setbmap(i);
84 }
85 i = sblock.fs_csaddr;
86 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
87 for (; i < cgd; i++)
88 setbmap(i);
89
90 /*
91 * Find all allocated blocks.
92 */
93 memset(&idesc, 0, sizeof(struct inodesc));
94 idesc.id_func = pass1check;
95 n_files = n_blks = 0;
96 for (c = 0; c < sblock.fs_ncg; c++) {
97 inumber = c * sblock.fs_ipg;
98 setinodebuf(inumber);
99 cgbp = cgget(c);
100 cgp = cgbp->b_un.b_cg;
101 rebuildcg = 0;
102 if (!check_cgmagic(c, cgbp))
103 rebuildcg = 1;
104 if (!rebuildcg && sblock.fs_magic == FS_UFS2_MAGIC) {
105 inosused = cgp->cg_initediblk;
106 if (inosused > sblock.fs_ipg) {
107 pfatal(
108"Too many initialized inodes (%ju > %d) in cylinder group %d\nReset to %d\n",
109 (uintmax_t)inosused,
110 sblock.fs_ipg, c, sblock.fs_ipg);
111 inosused = sblock.fs_ipg;
112 }
113 } else {
114 inosused = sblock.fs_ipg;
115 }
116 if (got_siginfo) {
117 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
118 cdevname, c, sblock.fs_ncg,
119 c * 100 / sblock.fs_ncg);
120 got_siginfo = 0;
121 }
122 if (got_sigalarm) {
123 setproctitle("%s p1 %d%%", cdevname,
124 c * 100 / sblock.fs_ncg);
125 got_sigalarm = 0;
126 }
127 /*
128 * If we are using soft updates, then we can trust the
129 * cylinder group inode allocation maps to tell us which
130 * inodes are allocated. We will scan the used inode map
131 * to find the inodes that are really in use, and then
132 * read only those inodes in from disk.
133 */
134 if ((preen || inoopt) && usedsoftdep && !rebuildcg) {
135 cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
136 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
137 if (*cp == 0)
138 continue;
139 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
140 if (*cp & i)
141 break;
142 inosused--;
143 }
144 break;
145 }
146 if (inosused < 0)
147 inosused = 0;
148 }
149 /*
150 * Allocate inoinfo structures for the allocated inodes.
151 */
152 inostathead[c].il_numalloced = inosused;
153 if (inosused == 0) {
154 inostathead[c].il_stat = 0;
155 continue;
156 }
157 info = Calloc((unsigned)inosused, sizeof(struct inostat));
158 if (info == NULL)
159 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
160 (unsigned)(sizeof(struct inostat) * inosused));
161 inostathead[c].il_stat = info;
162 /*
163 * Scan the allocated inodes.
164 */
165 for (i = 0; i < inosused; i++, inumber++) {
166 if (inumber < ROOTINO) {
167 (void)getnextinode(inumber, rebuildcg);
168 continue;
169 }
170 /*
171 * NULL return indicates probable end of allocated
172 * inodes during cylinder group rebuild attempt.
173 * We always keep trying until we get to the minimum
174 * valid number for this cylinder group.
175 */
176 if (checkinode(inumber, &idesc, rebuildcg) == 0 &&
177 i > cgp->cg_initediblk)
178 break;
179 }
180 /*
181 * This optimization speeds up future runs of fsck
182 * by trimming down the number of inodes in cylinder
183 * groups that formerly had many inodes but now have
184 * fewer in use.
185 */
186 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
187 if (inoopt && !preen && !rebuildcg &&
188 sblock.fs_magic == FS_UFS2_MAGIC &&
189 cgp->cg_initediblk > 2 * INOPB(&sblock) &&
190 mininos < cgp->cg_initediblk) {
191 i = cgp->cg_initediblk;
192 if (mininos < 2 * INOPB(&sblock))
193 cgp->cg_initediblk = 2 * INOPB(&sblock);
194 else
195 cgp->cg_initediblk = mininos;
196 pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
197 c, i, cgp->cg_initediblk, "VALID INODES");
198 dirty(cgbp);
199 }
200 if (inosused < sblock.fs_ipg)
201 continue;
202 lastino += 1;
203 if (lastino < (c * sblock.fs_ipg))
204 inosused = 0;
205 else
206 inosused = lastino - (c * sblock.fs_ipg);
207 if (rebuildcg && inosused > cgp->cg_initediblk &&
208 sblock.fs_magic == FS_UFS2_MAGIC) {
209 cgp->cg_initediblk = roundup(inosused, INOPB(&sblock));
210 pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
211 cgp->cg_initediblk);
212 }
213 /*
214 * If we were not able to determine in advance which inodes
215 * were in use, then reduce the size of the inoinfo structure
216 * to the size necessary to describe the inodes that we
217 * really found.
218 */
219 if (inumber == lastino)
220 continue;
221 inostathead[c].il_numalloced = inosused;
222 if (inosused == 0) {
223 free(inostathead[c].il_stat);
224 inostathead[c].il_stat = 0;
225 continue;
226 }
227 info = Calloc((unsigned)inosused, sizeof(struct inostat));
228 if (info == NULL)
229 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
230 (unsigned)(sizeof(struct inostat) * inosused));
231 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
232 free(inostathead[c].il_stat);
233 inostathead[c].il_stat = info;
234 }
235 freeinodebuf();
236}
237
238static int
239checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg)
240{
241 union dinode *dp;
242 off_t kernmaxfilesize;
243 ufs2_daddr_t ndb;
244 mode_t mode;
245 int j, ret, offset;
246
247 if ((dp = getnextinode(inumber, rebuildcg)) == NULL)
248 return (0);
249 mode = DIP(dp, di_mode) & IFMT;
250 if (mode == 0) {
251 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
252 (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
253 NDADDR * sizeof(ufs1_daddr_t)) ||
254 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
255 NIADDR * sizeof(ufs1_daddr_t)) ||
256 dp->dp1.di_mode || dp->dp1.di_size)) ||
257 (sblock.fs_magic == FS_UFS2_MAGIC &&
258 (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
259 NDADDR * sizeof(ufs2_daddr_t)) ||
260 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
261 NIADDR * sizeof(ufs2_daddr_t)) ||
262 dp->dp2.di_mode || dp->dp2.di_size))) {
263 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
264 (u_long)inumber);
265 if (reply("CLEAR") == 1) {
266 dp = ginode(inumber);
267 clearinode(dp);
268 inodirty();
269 }
270 }
271 inoinfo(inumber)->ino_state = USTATE;
272 return (1);
273 }
274 lastino = inumber;
275 /* This should match the file size limit in ffs_mountfs(). */
276 if (sblock.fs_magic == FS_UFS1_MAGIC)
277 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
278 else
279 kernmaxfilesize = sblock.fs_maxfilesize;
280 if (DIP(dp, di_size) > kernmaxfilesize ||
281 DIP(dp, di_size) > sblock.fs_maxfilesize ||
282 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
283 if (debug)
284 printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
285 goto unknown;
286 }
287 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
288 dp = ginode(inumber);
289 DIP_SET(dp, di_size, sblock.fs_fsize);
290 DIP_SET(dp, di_mode, IFREG|0600);
291 inodirty();
292 }
293 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
294 mode == IFSOCK) && DIP(dp, di_size) != 0) {
295 if (debug)
296 printf("bad special-file size %ju:",
297 (uintmax_t)DIP(dp, di_size));
298 goto unknown;
299 }
300 if ((mode == IFBLK || mode == IFCHR) &&
301 (dev_t)DIP(dp, di_rdev) == NODEV) {
302 if (debug)
303 printf("bad special-file rdev NODEV:");
304 goto unknown;
305 }
306 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
307 if (ndb < 0) {
308 if (debug)
309 printf("bad size %ju ndb %ju:",
310 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
311 goto unknown;
312 }
313 if (mode == IFBLK || mode == IFCHR)
314 ndb++;
315 if (mode == IFLNK) {
316 /*
317 * Fake ndb value so direct/indirect block checks below
318 * will detect any garbage after symlink string.
319 */
320 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
321 if (sblock.fs_magic == FS_UFS1_MAGIC)
322 ndb = howmany(DIP(dp, di_size),
323 sizeof(ufs1_daddr_t));
324 else
325 ndb = howmany(DIP(dp, di_size),
326 sizeof(ufs2_daddr_t));
327 if (ndb > NDADDR) {
328 j = ndb - NDADDR;
329 for (ndb = 1; j > 1; j--)
330 ndb *= NINDIR(&sblock);
331 ndb += NDADDR;
332 }
333 }
334 }
335 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
336 if (DIP(dp, di_db[j]) != 0) {
337 if (debug)
338 printf("bad direct addr[%d]: %ju\n", j,
339 (uintmax_t)DIP(dp, di_db[j]));
340 goto unknown;
341 }
342 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
343 ndb /= NINDIR(&sblock);
344 for (; j < NIADDR; j++)
345 if (DIP(dp, di_ib[j]) != 0) {
346 if (debug)
347 printf("bad indirect addr: %ju\n",
348 (uintmax_t)DIP(dp, di_ib[j]));
349 goto unknown;
350 }
351 if (ftypeok(dp) == 0)
352 goto unknown;
353 n_files++;
354 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
355 if (mode == IFDIR) {
356 if (DIP(dp, di_size) == 0)
357 inoinfo(inumber)->ino_state = DCLEAR;
358 else if (DIP(dp, di_nlink) <= 0)
359 inoinfo(inumber)->ino_state = DZLINK;
360 else
361 inoinfo(inumber)->ino_state = DSTATE;
362 cacheino(dp, inumber);
363 countdirs++;
364 } else if (DIP(dp, di_nlink) <= 0)
365 inoinfo(inumber)->ino_state = FZLINK;
366 else
367 inoinfo(inumber)->ino_state = FSTATE;
368 inoinfo(inumber)->ino_type = IFTODT(mode);
369 badblk = dupblk = 0;
370 idesc->id_number = inumber;
371 if (DIP(dp, di_flags) & SF_SNAPSHOT)
372 idesc->id_type = SNAP;
373 else
374 idesc->id_type = ADDR;
375 (void)ckinode(dp, idesc);
376 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
377 idesc->id_type = ADDR;
378 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
379 for (j = 0; j < NXADDR; j++) {
380 if (--ndb == 0 &&
381 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
382 idesc->id_numfrags = numfrags(&sblock,
383 fragroundup(&sblock, offset));
384 else
385 idesc->id_numfrags = sblock.fs_frag;
386 if (dp->dp2.di_extb[j] == 0)
387 continue;
388 idesc->id_blkno = dp->dp2.di_extb[j];
389 ret = (*idesc->id_func)(idesc);
390 if (ret & STOP)
391 break;
392 }
393 }
394 if (sblock.fs_magic == FS_UFS2_MAGIC)
395 eascan(idesc, &dp->dp2);
396 idesc->id_entryno *= btodb(sblock.fs_fsize);
397 if (DIP(dp, di_blocks) != idesc->id_entryno) {
398 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
399 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
400 (uintmax_t)idesc->id_entryno);
401 if (preen)
402 printf(" (CORRECTED)\n");
403 else if (reply("CORRECT") == 0)
404 return (1);
405 if (bkgrdflag == 0) {
406 dp = ginode(inumber);
407 DIP_SET(dp, di_blocks, idesc->id_entryno);
408 inodirty();
409 } else {
410 cmd.value = idesc->id_number;
411 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
412 if (debug)
413 printf("adjblkcnt ino %ju amount %lld\n",
414 (uintmax_t)cmd.value, (long long)cmd.size);
415 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
416 &cmd, sizeof cmd) == -1)
417 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
418 }
419 }
420 return (1);
421unknown:
422 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
423 inoinfo(inumber)->ino_state = FCLEAR;
424 if (reply("CLEAR") == 1) {
425 inoinfo(inumber)->ino_state = USTATE;
426 dp = ginode(inumber);
427 clearinode(dp);
428 inodirty();
429 }
430 return (1);
431}
432
433int
434pass1check(struct inodesc *idesc)
435{
436 int res = KEEPON;
437 int anyout, nfrags;
438 ufs2_daddr_t blkno = idesc->id_blkno;
439 struct dups *dlp;
440 struct dups *new;
441
442 if (idesc->id_type == SNAP) {
443 if (blkno == BLK_NOCOPY)
444 return (KEEPON);
445 if (idesc->id_number == cursnapshot) {
446 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
447 return (KEEPON);
448 if (blkno == BLK_SNAP) {
449 blkno = blkstofrags(&sblock, idesc->id_lbn);
450 idesc->id_entryno -= idesc->id_numfrags;
451 }
452 } else {
453 if (blkno == BLK_SNAP)
454 return (KEEPON);
455 }
456 }
457 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
458 blkerror(idesc->id_number, "BAD", blkno);
459 if (badblk++ >= MAXBAD) {
460 pwarn("EXCESSIVE BAD BLKS I=%lu",
461 (u_long)idesc->id_number);
462 if (preen)
463 printf(" (SKIPPING)\n");
464 else if (reply("CONTINUE") == 0) {
465 ckfini(0);
466 exit(EEXIT);
467 }
468 rerun = 1;
466 return (STOP);
467 }
468 }
469 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
470 if (anyout && chkrange(blkno, 1)) {
471 res = SKIP;
472 } else if (!testbmap(blkno)) {
473 n_blks++;
474 setbmap(blkno);
475 } else {
476 blkerror(idesc->id_number, "DUP", blkno);
477 if (dupblk++ >= MAXDUP) {
478 pwarn("EXCESSIVE DUP BLKS I=%lu",
479 (u_long)idesc->id_number);
480 if (preen)
481 printf(" (SKIPPING)\n");
482 else if (reply("CONTINUE") == 0) {
483 ckfini(0);
484 exit(EEXIT);
485 }
469 return (STOP);
470 }
471 }
472 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
473 if (anyout && chkrange(blkno, 1)) {
474 res = SKIP;
475 } else if (!testbmap(blkno)) {
476 n_blks++;
477 setbmap(blkno);
478 } else {
479 blkerror(idesc->id_number, "DUP", blkno);
480 if (dupblk++ >= MAXDUP) {
481 pwarn("EXCESSIVE DUP BLKS I=%lu",
482 (u_long)idesc->id_number);
483 if (preen)
484 printf(" (SKIPPING)\n");
485 else if (reply("CONTINUE") == 0) {
486 ckfini(0);
487 exit(EEXIT);
488 }
489 rerun = 1;
486 return (STOP);
487 }
488 new = (struct dups *)Malloc(sizeof(struct dups));
489 if (new == NULL) {
490 pfatal("DUP TABLE OVERFLOW.");
491 if (reply("CONTINUE") == 0) {
492 ckfini(0);
493 exit(EEXIT);
494 }
490 return (STOP);
491 }
492 new = (struct dups *)Malloc(sizeof(struct dups));
493 if (new == NULL) {
494 pfatal("DUP TABLE OVERFLOW.");
495 if (reply("CONTINUE") == 0) {
496 ckfini(0);
497 exit(EEXIT);
498 }
499 rerun = 1;
495 return (STOP);
496 }
497 new->dup = blkno;
498 if (muldup == 0) {
499 duplist = muldup = new;
500 new->next = 0;
501 } else {
502 new->next = muldup->next;
503 muldup->next = new;
504 }
505 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
506 if (dlp->dup == blkno)
507 break;
508 if (dlp == muldup && dlp->dup != blkno)
509 muldup = new;
510 }
511 /*
512 * count the number of blocks found in id_entryno
513 */
514 idesc->id_entryno++;
515 }
516 return (res);
517}
500 return (STOP);
501 }
502 new->dup = blkno;
503 if (muldup == 0) {
504 duplist = muldup = new;
505 new->next = 0;
506 } else {
507 new->next = muldup->next;
508 muldup->next = new;
509 }
510 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
511 if (dlp->dup == blkno)
512 break;
513 if (dlp == muldup && dlp->dup != blkno)
514 muldup = new;
515 }
516 /*
517 * count the number of blocks found in id_entryno
518 */
519 idesc->id_entryno++;
520 }
521 return (res);
522}