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);
66 ino_t inumber, inosused, mininos;
71 badblk = dupblk = lastino = 0;
74 * Set file system reserved blocks in used block map.
76 for (c = 0; c < sblock.fs_ncg; c++) {
77 cgd = cgdmin(&sblock, c);
79 i = cgbase(&sblock, c);
81 i = cgsblock(&sblock, c);
86 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
91 * Find all allocated blocks.
93 memset(&idesc, 0, sizeof(struct inodesc));
94 idesc.id_func = pass1check;
96 for (c = 0; c < sblock.fs_ncg; c++) {
97 inumber = c * sblock.fs_ipg;
100 cgp = cgbp->b_un.b_cg;
102 if (!check_cgmagic(c, cgbp))
104 if (!rebuildcg && sblock.fs_magic == FS_UFS2_MAGIC) {
105 inosused = cgp->cg_initediblk;
106 if (inosused > sblock.fs_ipg) {
108 "Too many initialized inodes (%ju > %d) in cylinder group %d\nReset to %d\n",
110 sblock.fs_ipg, c, sblock.fs_ipg);
111 inosused = sblock.fs_ipg;
114 inosused = sblock.fs_ipg;
117 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
118 cdevname, c, sblock.fs_ncg,
119 c * 100 / sblock.fs_ncg);
123 setproctitle("%s p1 %d%%", cdevname,
124 c * 100 / sblock.fs_ncg);
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.
134 if ((preen || inoopt) && usedsoftdep && !rebuildcg) {
135 cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
136 for ( ; inosused != 0; cp--) {
138 if (inosused > CHAR_BIT)
139 inosused -= CHAR_BIT;
144 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
153 * Allocate inoinfo structures for the allocated inodes.
155 inostathead[c].il_numalloced = inosused;
157 inostathead[c].il_stat = 0;
160 info = Calloc((unsigned)inosused, sizeof(struct inostat));
162 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
163 (unsigned)(sizeof(struct inostat) * inosused));
164 inostathead[c].il_stat = info;
166 * Scan the allocated inodes.
168 for (i = 0; i < inosused; i++, inumber++) {
169 if (inumber < ROOTINO) {
170 (void)getnextinode(inumber, rebuildcg);
174 * NULL return indicates probable end of allocated
175 * inodes during cylinder group rebuild attempt.
176 * We always keep trying until we get to the minimum
177 * valid number for this cylinder group.
179 if (checkinode(inumber, &idesc, rebuildcg) == 0 &&
180 i > cgp->cg_initediblk)
184 * This optimization speeds up future runs of fsck
185 * by trimming down the number of inodes in cylinder
186 * groups that formerly had many inodes but now have
189 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
190 if (inoopt && !preen && !rebuildcg &&
191 sblock.fs_magic == FS_UFS2_MAGIC &&
192 cgp->cg_initediblk > 2 * INOPB(&sblock) &&
193 mininos < cgp->cg_initediblk) {
194 i = cgp->cg_initediblk;
195 if (mininos < 2 * INOPB(&sblock))
196 cgp->cg_initediblk = 2 * INOPB(&sblock);
198 cgp->cg_initediblk = mininos;
199 pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
200 c, i, cgp->cg_initediblk, "VALID INODES");
203 if (inosused < sblock.fs_ipg)
206 if (lastino < (c * sblock.fs_ipg))
209 inosused = lastino - (c * sblock.fs_ipg);
210 if (rebuildcg && inosused > cgp->cg_initediblk &&
211 sblock.fs_magic == FS_UFS2_MAGIC) {
212 cgp->cg_initediblk = roundup(inosused, INOPB(&sblock));
213 pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
217 * If we were not able to determine in advance which inodes
218 * were in use, then reduce the size of the inoinfo structure
219 * to the size necessary to describe the inodes that we
222 if (inumber == lastino)
224 inostathead[c].il_numalloced = inosused;
226 free(inostathead[c].il_stat);
227 inostathead[c].il_stat = 0;
230 info = Calloc((unsigned)inosused, sizeof(struct inostat));
232 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
233 (unsigned)(sizeof(struct inostat) * inosused));
234 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
235 free(inostathead[c].il_stat);
236 inostathead[c].il_stat = info;
242 checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg)
245 off_t kernmaxfilesize;
250 if ((dp = getnextinode(inumber, rebuildcg)) == NULL)
252 mode = DIP(dp, di_mode) & IFMT;
254 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
255 (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
256 NDADDR * sizeof(ufs1_daddr_t)) ||
257 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
258 NIADDR * sizeof(ufs1_daddr_t)) ||
259 dp->dp1.di_mode || dp->dp1.di_size)) ||
260 (sblock.fs_magic == FS_UFS2_MAGIC &&
261 (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
262 NDADDR * sizeof(ufs2_daddr_t)) ||
263 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
264 NIADDR * sizeof(ufs2_daddr_t)) ||
265 dp->dp2.di_mode || dp->dp2.di_size))) {
266 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
268 if (reply("CLEAR") == 1) {
269 dp = ginode(inumber);
274 inoinfo(inumber)->ino_state = USTATE;
278 /* This should match the file size limit in ffs_mountfs(). */
279 if (sblock.fs_magic == FS_UFS1_MAGIC)
280 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
282 kernmaxfilesize = sblock.fs_maxfilesize;
283 if (DIP(dp, di_size) > kernmaxfilesize ||
284 DIP(dp, di_size) > sblock.fs_maxfilesize ||
285 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
287 printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
290 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
291 dp = ginode(inumber);
292 DIP_SET(dp, di_size, sblock.fs_fsize);
293 DIP_SET(dp, di_mode, IFREG|0600);
296 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
297 mode == IFSOCK) && DIP(dp, di_size) != 0) {
299 printf("bad special-file size %ju:",
300 (uintmax_t)DIP(dp, di_size));
303 if ((mode == IFBLK || mode == IFCHR) &&
304 (dev_t)DIP(dp, di_rdev) == NODEV) {
306 printf("bad special-file rdev NODEV:");
309 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
312 printf("bad size %ju ndb %ju:",
313 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
316 if (mode == IFBLK || mode == IFCHR)
320 * Fake ndb value so direct/indirect block checks below
321 * will detect any garbage after symlink string.
323 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
324 if (sblock.fs_magic == FS_UFS1_MAGIC)
325 ndb = howmany(DIP(dp, di_size),
326 sizeof(ufs1_daddr_t));
328 ndb = howmany(DIP(dp, di_size),
329 sizeof(ufs2_daddr_t));
332 for (ndb = 1; j > 1; j--)
333 ndb *= NINDIR(&sblock);
338 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
339 if (DIP(dp, di_db[j]) != 0) {
341 printf("bad direct addr[%d]: %ju\n", j,
342 (uintmax_t)DIP(dp, di_db[j]));
345 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
346 ndb /= NINDIR(&sblock);
347 for (; j < NIADDR; j++)
348 if (DIP(dp, di_ib[j]) != 0) {
350 printf("bad indirect addr: %ju\n",
351 (uintmax_t)DIP(dp, di_ib[j]));
354 if (ftypeok(dp) == 0)
357 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
359 if (DIP(dp, di_size) == 0)
360 inoinfo(inumber)->ino_state = DCLEAR;
361 else if (DIP(dp, di_nlink) <= 0)
362 inoinfo(inumber)->ino_state = DZLINK;
364 inoinfo(inumber)->ino_state = DSTATE;
365 cacheino(dp, inumber);
367 } else if (DIP(dp, di_nlink) <= 0)
368 inoinfo(inumber)->ino_state = FZLINK;
370 inoinfo(inumber)->ino_state = FSTATE;
371 inoinfo(inumber)->ino_type = IFTODT(mode);
373 idesc->id_number = inumber;
374 if (DIP(dp, di_flags) & SF_SNAPSHOT)
375 idesc->id_type = SNAP;
377 idesc->id_type = ADDR;
378 (void)ckinode(dp, idesc);
379 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
380 idesc->id_type = ADDR;
381 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
382 for (j = 0; j < NXADDR; j++) {
384 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
385 idesc->id_numfrags = numfrags(&sblock,
386 fragroundup(&sblock, offset));
388 idesc->id_numfrags = sblock.fs_frag;
389 if (dp->dp2.di_extb[j] == 0)
391 idesc->id_blkno = dp->dp2.di_extb[j];
392 ret = (*idesc->id_func)(idesc);
397 if (sblock.fs_magic == FS_UFS2_MAGIC)
398 eascan(idesc, &dp->dp2);
399 idesc->id_entryno *= btodb(sblock.fs_fsize);
400 if (DIP(dp, di_blocks) != idesc->id_entryno) {
401 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
402 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
403 (uintmax_t)idesc->id_entryno);
405 printf(" (CORRECTED)\n");
406 else if (reply("CORRECT") == 0)
408 if (bkgrdflag == 0) {
409 dp = ginode(inumber);
410 DIP_SET(dp, di_blocks, idesc->id_entryno);
413 cmd.value = idesc->id_number;
414 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
416 printf("adjblkcnt ino %ju amount %lld\n",
417 (uintmax_t)cmd.value, (long long)cmd.size);
418 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
419 &cmd, sizeof cmd) == -1)
420 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
425 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
426 inoinfo(inumber)->ino_state = FCLEAR;
427 if (reply("CLEAR") == 1) {
428 inoinfo(inumber)->ino_state = USTATE;
429 dp = ginode(inumber);
437 pass1check(struct inodesc *idesc)
441 ufs2_daddr_t blkno = idesc->id_blkno;
445 if (idesc->id_type == SNAP) {
446 if (blkno == BLK_NOCOPY)
448 if (idesc->id_number == cursnapshot) {
449 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
451 if (blkno == BLK_SNAP) {
452 blkno = blkstofrags(&sblock, idesc->id_lbn);
453 idesc->id_entryno -= idesc->id_numfrags;
456 if (blkno == BLK_SNAP)
460 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
461 blkerror(idesc->id_number, "BAD", blkno);
462 if (badblk++ >= MAXBAD) {
463 pwarn("EXCESSIVE BAD BLKS I=%lu",
464 (u_long)idesc->id_number);
466 printf(" (SKIPPING)\n");
467 else if (reply("CONTINUE") == 0) {
475 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
476 if (anyout && chkrange(blkno, 1)) {
478 } else if (!testbmap(blkno)) {
482 blkerror(idesc->id_number, "DUP", blkno);
483 if (dupblk++ >= MAXDUP) {
484 pwarn("EXCESSIVE DUP BLKS I=%lu",
485 (u_long)idesc->id_number);
487 printf(" (SKIPPING)\n");
488 else if (reply("CONTINUE") == 0) {
495 new = (struct dups *)Malloc(sizeof(struct dups));
497 pfatal("DUP TABLE OVERFLOW.");
498 if (reply("CONTINUE") == 0) {
507 duplist = muldup = new;
510 new->next = muldup->next;
513 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
514 if (dlp->dup == blkno)
516 if (dlp == muldup && dlp->dup != blkno)
520 * count the number of blocks found in id_entryno