2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 static const char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/sysctl.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ufs/dir.h>
44 #include <ufs/ffs/fs.h>
53 static ufs2_daddr_t badblk;
54 static ufs2_daddr_t dupblk;
55 static ino_t lastino; /* last inode in use */
57 static int checkinode(ino_t inumber, struct inodesc *, int rebuildcg);
64 ino_t inumber, inosused, mininos;
70 * Set file system reserved blocks in used block map.
72 for (c = 0; c < sblock.fs_ncg; c++) {
73 cgd = cgdmin(&sblock, c);
75 i = cgbase(&sblock, c);
77 i = cgsblock(&sblock, c);
82 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
87 * Find all allocated blocks.
89 memset(&idesc, 0, sizeof(struct inodesc));
90 idesc.id_func = pass1check;
92 for (c = 0; c < sblock.fs_ncg; c++) {
93 inumber = c * sblock.fs_ipg;
95 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
97 if (!check_cgmagic(c, &cgrp))
99 if (!rebuildcg && sblock.fs_magic == FS_UFS2_MAGIC) {
100 inosused = cgrp.cg_initediblk;
101 if (inosused > sblock.fs_ipg) {
102 pfatal("%s (%d > %d) %s %d\nReset to %d\n",
103 "Too many initialized inodes", inosused,
104 sblock.fs_ipg, "in cylinder group", c,
106 inosused = sblock.fs_ipg;
109 inosused = sblock.fs_ipg;
112 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
113 cdevname, c, sblock.fs_ncg,
114 c * 100 / sblock.fs_ncg);
118 setproctitle("%s p1 %d%%", cdevname,
119 c * 100 / sblock.fs_ncg);
123 * If we are using soft updates, then we can trust the
124 * cylinder group inode allocation maps to tell us which
125 * inodes are allocated. We will scan the used inode map
126 * to find the inodes that are really in use, and then
127 * read only those inodes in from disk.
129 if ((preen || inoopt) && usedsoftdep && !rebuildcg) {
130 cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
131 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
134 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
145 * Allocate inoinfo structures for the allocated inodes.
147 inostathead[c].il_numalloced = inosused;
149 inostathead[c].il_stat = 0;
152 info = calloc((unsigned)inosused, sizeof(struct inostat));
154 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
155 (unsigned)(sizeof(struct inostat) * inosused));
156 inostathead[c].il_stat = info;
158 * Scan the allocated inodes.
160 for (i = 0; i < inosused; i++, inumber++) {
161 if (inumber < ROOTINO) {
162 (void)getnextinode(inumber, rebuildcg);
166 * NULL return indicates probable end of allocated
167 * inodes during cylinder group rebuild attempt.
168 * We always keep trying until we get to the minimum
169 * valid number for this cylinder group.
171 if (checkinode(inumber, &idesc, rebuildcg) == 0 &&
172 i > cgrp.cg_initediblk)
176 * This optimization speeds up future runs of fsck
177 * by trimming down the number of inodes in cylinder
178 * groups that formerly had many inodes but now have
181 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
182 if (inoopt && !preen && !rebuildcg &&
183 sblock.fs_magic == FS_UFS2_MAGIC &&
184 cgrp.cg_initediblk > 2 * INOPB(&sblock) &&
185 mininos < cgrp.cg_initediblk) {
186 i = cgrp.cg_initediblk;
187 if (mininos < 2 * INOPB(&sblock))
188 cgrp.cg_initediblk = 2 * INOPB(&sblock);
190 cgrp.cg_initediblk = mininos;
191 pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
192 c, i, cgrp.cg_initediblk, "VALID INODES");
195 if (inosused < sblock.fs_ipg)
198 if (lastino < (c * sblock.fs_ipg))
201 inosused = lastino - (c * sblock.fs_ipg);
202 if (rebuildcg && inosused > cgrp.cg_initediblk &&
203 sblock.fs_magic == FS_UFS2_MAGIC) {
204 cgrp.cg_initediblk = roundup(inosused, INOPB(&sblock));
205 pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
209 * If we were not able to determine in advance which inodes
210 * were in use, then reduce the size of the inoinfo structure
211 * to the size necessary to describe the inodes that we
214 if (inumber == lastino)
216 inostathead[c].il_numalloced = inosused;
218 free(inostathead[c].il_stat);
219 inostathead[c].il_stat = 0;
222 info = calloc((unsigned)inosused, sizeof(struct inostat));
224 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
225 (unsigned)(sizeof(struct inostat) * inosused));
226 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
227 free(inostathead[c].il_stat);
228 inostathead[c].il_stat = info;
234 checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg)
237 off_t kernmaxfilesize;
242 if ((dp = getnextinode(inumber, rebuildcg)) == NULL)
244 mode = DIP(dp, di_mode) & IFMT;
246 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
247 (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
248 NDADDR * sizeof(ufs1_daddr_t)) ||
249 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
250 NIADDR * sizeof(ufs1_daddr_t)) ||
251 dp->dp1.di_mode || dp->dp1.di_size)) ||
252 (sblock.fs_magic == FS_UFS2_MAGIC &&
253 (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
254 NDADDR * sizeof(ufs2_daddr_t)) ||
255 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
256 NIADDR * sizeof(ufs2_daddr_t)) ||
257 dp->dp2.di_mode || dp->dp2.di_size))) {
258 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
260 if (reply("CLEAR") == 1) {
261 dp = ginode(inumber);
266 inoinfo(inumber)->ino_state = USTATE;
270 /* This should match the file size limit in ffs_mountfs(). */
271 if (sblock.fs_magic == FS_UFS1_MAGIC)
272 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
274 kernmaxfilesize = sblock.fs_maxfilesize;
275 if (DIP(dp, di_size) > kernmaxfilesize ||
276 DIP(dp, di_size) > sblock.fs_maxfilesize ||
277 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
279 printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
282 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
283 dp = ginode(inumber);
284 DIP_SET(dp, di_size, sblock.fs_fsize);
285 DIP_SET(dp, di_mode, IFREG|0600);
288 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
289 mode == IFSOCK) && DIP(dp, di_size) != 0) {
291 printf("bad special-file size %ju:",
292 (uintmax_t)DIP(dp, di_size));
295 if ((mode == IFBLK || mode == IFCHR) &&
296 (dev_t)DIP(dp, di_rdev) == NODEV) {
298 printf("bad special-file rdev NODEV:");
301 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
304 printf("bad size %ju ndb %ju:",
305 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
308 if (mode == IFBLK || mode == IFCHR)
312 * Fake ndb value so direct/indirect block checks below
313 * will detect any garbage after symlink string.
315 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
316 if (sblock.fs_magic == FS_UFS1_MAGIC)
317 ndb = howmany(DIP(dp, di_size),
318 sizeof(ufs1_daddr_t));
320 ndb = howmany(DIP(dp, di_size),
321 sizeof(ufs2_daddr_t));
324 for (ndb = 1; j > 1; j--)
325 ndb *= NINDIR(&sblock);
330 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
331 if (DIP(dp, di_db[j]) != 0) {
333 printf("bad direct addr[%d]: %ju\n", j,
334 (uintmax_t)DIP(dp, di_db[j]));
337 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
338 ndb /= NINDIR(&sblock);
339 for (; j < NIADDR; j++)
340 if (DIP(dp, di_ib[j]) != 0) {
342 printf("bad indirect addr: %ju\n",
343 (uintmax_t)DIP(dp, di_ib[j]));
346 if (ftypeok(dp) == 0)
349 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
351 if (DIP(dp, di_size) == 0)
352 inoinfo(inumber)->ino_state = DCLEAR;
353 else if (DIP(dp, di_nlink) <= 0)
354 inoinfo(inumber)->ino_state = DZLINK;
356 inoinfo(inumber)->ino_state = DSTATE;
357 cacheino(dp, inumber);
359 } else if (DIP(dp, di_nlink) <= 0)
360 inoinfo(inumber)->ino_state = FZLINK;
362 inoinfo(inumber)->ino_state = FSTATE;
363 inoinfo(inumber)->ino_type = IFTODT(mode);
365 idesc->id_number = inumber;
366 if (DIP(dp, di_flags) & SF_SNAPSHOT)
367 idesc->id_type = SNAP;
369 idesc->id_type = ADDR;
370 (void)ckinode(dp, idesc);
371 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
372 idesc->id_type = ADDR;
373 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
374 for (j = 0; j < NXADDR; j++) {
376 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
377 idesc->id_numfrags = numfrags(&sblock,
378 fragroundup(&sblock, offset));
380 idesc->id_numfrags = sblock.fs_frag;
381 if (dp->dp2.di_extb[j] == 0)
383 idesc->id_blkno = dp->dp2.di_extb[j];
384 ret = (*idesc->id_func)(idesc);
389 if (sblock.fs_magic == FS_UFS2_MAGIC)
390 eascan(idesc, &dp->dp2);
391 idesc->id_entryno *= btodb(sblock.fs_fsize);
392 if (DIP(dp, di_blocks) != idesc->id_entryno) {
393 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
394 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
395 (uintmax_t)idesc->id_entryno);
397 printf(" (CORRECTED)\n");
398 else if (reply("CORRECT") == 0)
400 if (bkgrdflag == 0) {
401 dp = ginode(inumber);
402 DIP_SET(dp, di_blocks, idesc->id_entryno);
405 cmd.value = idesc->id_number;
406 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
408 printf("adjblkcnt ino %ju amount %lld\n",
409 (uintmax_t)cmd.value, (long long)cmd.size);
410 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
411 &cmd, sizeof cmd) == -1)
412 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
417 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
418 inoinfo(inumber)->ino_state = FCLEAR;
419 if (reply("CLEAR") == 1) {
420 inoinfo(inumber)->ino_state = USTATE;
421 dp = ginode(inumber);
429 pass1check(struct inodesc *idesc)
433 ufs2_daddr_t blkno = idesc->id_blkno;
437 if (idesc->id_type == SNAP) {
438 if (blkno == BLK_NOCOPY)
440 if (idesc->id_number == cursnapshot) {
441 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
443 if (blkno == BLK_SNAP) {
444 blkno = blkstofrags(&sblock, idesc->id_lbn);
445 idesc->id_entryno -= idesc->id_numfrags;
448 if (blkno == BLK_SNAP)
452 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
453 blkerror(idesc->id_number, "BAD", blkno);
454 if (badblk++ >= MAXBAD) {
455 pwarn("EXCESSIVE BAD BLKS I=%lu",
456 (u_long)idesc->id_number);
458 printf(" (SKIPPING)\n");
459 else if (reply("CONTINUE") == 0) {
466 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
467 if (anyout && chkrange(blkno, 1)) {
469 } else if (!testbmap(blkno)) {
473 blkerror(idesc->id_number, "DUP", blkno);
474 if (dupblk++ >= MAXDUP) {
475 pwarn("EXCESSIVE DUP BLKS I=%lu",
476 (u_long)idesc->id_number);
478 printf(" (SKIPPING)\n");
479 else if (reply("CONTINUE") == 0) {
485 new = (struct dups *)malloc(sizeof(struct dups));
487 pfatal("DUP TABLE OVERFLOW.");
488 if (reply("CONTINUE") == 0) {
496 duplist = muldup = new;
499 new->next = muldup->next;
502 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
503 if (dlp->dup == blkno)
505 if (dlp == muldup && dlp->dup != blkno)
509 * count the number of blocks found in id_entryno