2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
34 #include <sys/sysctl.h>
36 #include <ufs/ufs/dinode.h>
37 #include <ufs/ufs/dir.h>
38 #include <ufs/ffs/fs.h>
47 static ufs2_daddr_t badblk;
48 static ufs2_daddr_t dupblk;
49 static ino_t lastino; /* last inode in use */
51 static int checkinode(ino_t inumber, struct inodesc *, int rebuiltcg);
60 ino_t inumber, inosused, mininos;
65 badblk = dupblk = lastino = 0;
68 * Set file system reserved blocks in used block map.
70 for (c = 0; c < sblock.fs_ncg; c++) {
71 cgd = cgdmin(&sblock, c);
73 i = cgbase(&sblock, c);
75 i = cgsblock(&sblock, c);
80 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
85 * Find all allocated blocks.
87 memset(&idesc, 0, sizeof(struct inodesc));
88 idesc.id_func = pass1check;
90 for (c = 0; c < sblock.fs_ncg; c++) {
91 inumber = c * sblock.fs_ipg;
93 cgp = cgbp->b_un.b_cg;
95 if (!check_cgmagic(c, cgbp)) {
96 if (!reply("REBUILD CYLINDER GROUP")) {
99 pwarn("YOU WILL NEED TO RERUN FSCK.\n");
107 if (!rebuiltcg && sblock.fs_magic == FS_UFS2_MAGIC) {
108 inosused = cgp->cg_initediblk;
109 if (inosused > sblock.fs_ipg) {
110 pfatal("Too many initialized inodes (%ju > %d) "
111 "in cylinder group %d\nReset to %d\n",
112 (uintmax_t)inosused, sblock.fs_ipg, c,
114 inosused = sblock.fs_ipg;
117 inosused = sblock.fs_ipg;
120 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
121 cdevname, c, sblock.fs_ncg,
122 c * 100 / sblock.fs_ncg);
126 setproctitle("%s p1 %d%%", cdevname,
127 c * 100 / sblock.fs_ncg);
131 * If we are using soft updates, then we can trust the
132 * cylinder group inode allocation maps to tell us which
133 * inodes are allocated. We will scan the used inode map
134 * to find the inodes that are really in use, and then
135 * read only those inodes in from disk.
137 if ((preen || inoopt) && usedsoftdep && !rebuiltcg) {
138 cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
139 for ( ; inosused != 0; cp--) {
141 if (inosused > CHAR_BIT)
142 inosused -= CHAR_BIT;
147 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
156 * Allocate inoinfo structures for the allocated inodes.
158 inostathead[c].il_numalloced = inosused;
160 inostathead[c].il_stat = NULL;
163 info = Calloc((unsigned)inosused, sizeof(struct inostat));
165 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
166 (unsigned)(sizeof(struct inostat) * inosused));
167 inostathead[c].il_stat = info;
169 * Scan the allocated inodes.
171 setinodebuf(c, inosused);
172 for (i = 0; i < inosused; i++, inumber++) {
173 if (inumber < UFS_ROOTINO) {
174 (void)getnextinode(inumber, rebuiltcg);
178 * NULL return indicates probable end of allocated
179 * inodes during cylinder group rebuild attempt.
180 * We always keep trying until we get to the minimum
181 * valid number for this cylinder group.
183 if (checkinode(inumber, &idesc, rebuiltcg) == 0 &&
184 i > cgp->cg_initediblk)
188 * This optimization speeds up future runs of fsck
189 * by trimming down the number of inodes in cylinder
190 * groups that formerly had many inodes but now have
193 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
194 if (inoopt && !preen && !rebuiltcg &&
195 sblock.fs_magic == FS_UFS2_MAGIC &&
196 cgp->cg_initediblk > 2 * INOPB(&sblock) &&
197 mininos < cgp->cg_initediblk) {
198 i = cgp->cg_initediblk;
199 if (mininos < 2 * INOPB(&sblock))
200 cgp->cg_initediblk = 2 * INOPB(&sblock);
202 cgp->cg_initediblk = mininos;
203 pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
204 c, i, cgp->cg_initediblk, "VALID INODES");
207 if (inosused < sblock.fs_ipg)
210 if (lastino < (c * sblock.fs_ipg))
213 inosused = lastino - (c * sblock.fs_ipg);
214 if (rebuiltcg && inosused > cgp->cg_initediblk &&
215 sblock.fs_magic == FS_UFS2_MAGIC) {
216 cgp->cg_initediblk = roundup(inosused, INOPB(&sblock));
217 pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
221 * If we were not able to determine in advance which inodes
222 * were in use, then reduce the size of the inoinfo structure
223 * to the size necessary to describe the inodes that we
224 * really found. Always leave map space in the first cylinder
225 * group in case we need to a root or lost+found directory.
227 if (inumber == lastino || c == 0)
229 inostathead[c].il_numalloced = inosused;
231 free(inostathead[c].il_stat);
232 inostathead[c].il_stat = NULL;
235 info = Calloc((unsigned)inosused, sizeof(struct inostat));
237 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
238 (unsigned)(sizeof(struct inostat) * inosused));
239 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
240 free(inostathead[c].il_stat);
241 inostathead[c].il_stat = info;
247 checkinode(ino_t inumber, struct inodesc *idesc, int rebuiltcg)
253 intmax_t size, fixsize;
256 if ((dp = getnextinode(inumber, rebuiltcg)) == NULL) {
257 pfatal("INVALID INODE");
260 mode = DIP(dp, di_mode) & IFMT;
262 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
263 (memcmp(dp->dp1.di_db, zino.dp1.di_db,
264 UFS_NDADDR * sizeof(ufs1_daddr_t)) ||
265 memcmp(dp->dp1.di_ib, zino.dp1.di_ib,
266 UFS_NIADDR * sizeof(ufs1_daddr_t)) ||
267 dp->dp1.di_mode || dp->dp1.di_size)) ||
268 (sblock.fs_magic == FS_UFS2_MAGIC &&
269 (memcmp(dp->dp2.di_db, zino.dp2.di_db,
270 UFS_NDADDR * sizeof(ufs2_daddr_t)) ||
271 memcmp(dp->dp2.di_ib, zino.dp2.di_ib,
272 UFS_NIADDR * sizeof(ufs2_daddr_t)) ||
273 dp->dp2.di_mode || dp->dp2.di_size))) {
274 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
276 if (reply("CLEAR") == 1) {
277 ginode(inumber, &ip);
283 inoinfo(inumber)->ino_state = USTATE;
287 if (chkfilesize(mode, DIP(dp, di_size)) == 0) {
288 pfatal("BAD FILE SIZE");
291 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
292 ginode(inumber, &ip);
294 DIP_SET(dp, di_size, sblock.fs_fsize);
295 DIP_SET(dp, di_mode, IFREG|0600);
299 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
300 mode == IFSOCK) && DIP(dp, di_size) != 0) {
302 printf("bad special-file size %ju:",
303 (uintmax_t)DIP(dp, di_size));
304 pfatal("BAD SPECIAL-FILE SIZE");
307 if ((mode == IFBLK || mode == IFCHR) &&
308 (dev_t)DIP(dp, di_rdev) == NODEV) {
310 printf("bad special-file rdev NODEV:");
311 pfatal("BAD SPECIAL-FILE RDEV");
314 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
317 printf("negative size %ju ndb %ju:",
318 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
319 pfatal("NEGATIVE FILE SIZE");
322 if (mode == IFBLK || mode == IFCHR)
326 * Fake ndb value so direct/indirect block checks below
327 * will detect any garbage after symlink string.
329 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
330 if (sblock.fs_magic == FS_UFS1_MAGIC)
331 ndb = howmany(DIP(dp, di_size),
332 sizeof(ufs1_daddr_t));
334 ndb = howmany(DIP(dp, di_size),
335 sizeof(ufs2_daddr_t));
336 if (ndb > UFS_NDADDR) {
337 j = ndb - UFS_NDADDR;
338 for (ndb = 1; j > 1; j--)
339 ndb *= NINDIR(&sblock);
344 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++) {
345 if (DIP(dp, di_db[j]) == 0)
348 printf("invalid direct addr[%d]: %ju\n", j,
349 (uintmax_t)DIP(dp, di_db[j]));
350 pfatal("INVALID DIRECT BLOCK");
351 ginode(inumber, &ip);
353 if (reply("CLEAR") == 1) {
354 DIP_SET(ip.i_dp, di_db[j], 0);
359 for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++)
360 ndb /= NINDIR(&sblock);
361 for (; j < UFS_NIADDR; j++) {
362 if (DIP(dp, di_ib[j]) == 0)
365 printf("invalid indirect addr: %ju\n",
366 (uintmax_t)DIP(dp, di_ib[j]));
367 pfatal("INVALID INDIRECT BLOCK");
368 ginode(inumber, &ip);
370 if (reply("CLEAR") == 1) {
371 DIP_SET(ip.i_dp, di_ib[j], 0);
376 if (ftypeok(dp) == 0) {
377 pfatal("UNKNOWN FILE TYPE");
381 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
383 if (DIP(dp, di_size) == 0) {
384 inoinfo(inumber)->ino_state = DCLEAR;
385 } else if (DIP(dp, di_nlink) == 0) {
386 inoinfo(inumber)->ino_state = DZLINK;
388 inoinfo(inumber)->ino_state = DSTATE;
390 cacheino(dp, inumber);
392 } else if (DIP(dp, di_nlink) <= 0)
393 inoinfo(inumber)->ino_state = FZLINK;
395 inoinfo(inumber)->ino_state = FSTATE;
396 inoinfo(inumber)->ino_type = IFTODT(mode);
398 idesc->id_number = inumber;
399 if (DIP(dp, di_flags) & SF_SNAPSHOT)
400 inoinfo(inumber)->ino_idtype = SNAP;
402 inoinfo(inumber)->ino_idtype = ADDR;
403 idesc->id_type = inoinfo(inumber)->ino_idtype;
404 (void)ckinode(dp, idesc);
405 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
406 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
407 for (j = 0; j < UFS_NXADDR; j++) {
409 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
410 idesc->id_numfrags = numfrags(&sblock,
411 fragroundup(&sblock, offset));
413 idesc->id_numfrags = sblock.fs_frag;
414 if (dp->dp2.di_extb[j] == 0)
416 idesc->id_blkno = dp->dp2.di_extb[j];
417 ret = (*idesc->id_func)(idesc);
422 if (sblock.fs_magic == FS_UFS2_MAGIC)
423 eascan(idesc, &dp->dp2);
424 idesc->id_entryno *= btodb(sblock.fs_fsize);
425 if (DIP(dp, di_blocks) != idesc->id_entryno) {
426 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
427 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
428 (uintmax_t)idesc->id_entryno);
430 printf(" (CORRECTED)\n");
431 else if (reply("CORRECT") == 0)
433 if (bkgrdflag == 0) {
434 ginode(inumber, &ip);
435 DIP_SET(ip.i_dp, di_blocks, idesc->id_entryno);
439 cmd.value = idesc->id_number;
440 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
442 printf("adjblkcnt ino %ju amount %lld\n",
443 (uintmax_t)cmd.value, (long long)cmd.size);
444 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
445 &cmd, sizeof cmd) == -1)
446 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
450 * UFS does not allow files to end with a hole; it requires that
451 * the last block of a file be allocated. The last allocated block
452 * in a file is tracked in id_lballoc. Here, we check for a size
453 * past the last allocated block of the file and if that is found,
454 * shorten the file to reference the last allocated block to avoid
455 * having it reference a hole at its end.
457 * Soft updates will always ensure that the file size is correct
458 * for files that contain only direct block pointers. However
459 * soft updates does not roll back sizes for files with indirect
460 * blocks that it has set to unallocated because their contents
461 * have not yet been written to disk. Hence, the file can appear
462 * to have a hole at its end because the block pointer has been
463 * rolled back to zero. Thus finding a hole at the end of a file
464 * that is located in an indirect block receives only a warning
465 * while finding a hole at the end of a file in a direct block
466 * receives a fatal error message.
468 size = DIP(dp, di_size);
469 if (idesc->id_lballoc < lblkno(&sblock, size - 1) &&
470 /* exclude embedded symbolic links */
471 ((mode != IFLNK) || size >= sblock.fs_maxsymlinklen)) {
472 fixsize = lblktosize(&sblock, idesc->id_lballoc + 1);
473 if (size > UFS_NDADDR * sblock.fs_bsize)
474 pwarn("INODE %lu: FILE SIZE %ju BEYOND END OF "
475 "ALLOCATED FILE, SIZE SHOULD BE %ju",
476 (u_long)inumber, size, fixsize);
478 pfatal("INODE %lu: FILE SIZE %ju BEYOND END OF "
479 "ALLOCATED FILE, SIZE SHOULD BE %ju",
480 (u_long)inumber, size, fixsize);
482 printf(" (ADJUSTED)\n");
483 else if (reply("ADJUST") == 0)
485 if (bkgrdflag == 0) {
486 ginode(inumber, &ip);
487 DIP_SET(ip.i_dp, di_size, fixsize);
491 cmd.value = idesc->id_number;
494 printf("setsize ino %ju size set to %ju\n",
495 (uintmax_t)cmd.value, (uintmax_t)cmd.size);
496 if (sysctl(setsize, MIBSIZE, 0, 0,
497 &cmd, sizeof cmd) == -1)
498 rwerror("SET INODE SIZE", cmd.value);
504 ginode(inumber, &ip);
506 inoinfo(inumber)->ino_state = USTATE;
507 if (reply("CLEAR") == 1) {
516 pass1check(struct inodesc *idesc)
520 ufs2_daddr_t blkno = idesc->id_blkno;
524 if (idesc->id_type == SNAP) {
525 if (blkno == BLK_NOCOPY)
527 if (idesc->id_number == cursnapshot) {
528 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
530 if (blkno == BLK_SNAP) {
531 blkno = blkstofrags(&sblock, idesc->id_lbn);
532 idesc->id_entryno -= idesc->id_numfrags;
535 if (blkno == BLK_SNAP)
539 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
540 blkerror(idesc->id_number, "BAD", blkno);
541 if (badblk++ >= MAXBAD) {
542 pwarn("EXCESSIVE BAD BLKS I=%lu",
543 (u_long)idesc->id_number);
545 printf(" (SKIPPING)\n");
546 else if (reply("CONTINUE") == 0) {
554 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
555 if (anyout && chkrange(blkno, 1)) {
557 } else if (!testbmap(blkno)) {
561 blkerror(idesc->id_number, "DUP", blkno);
562 if (dupblk++ >= MAXDUP) {
563 pwarn("EXCESSIVE DUP BLKS I=%lu",
564 (u_long)idesc->id_number);
566 printf(" (SKIPPING)\n");
567 else if (reply("CONTINUE") == 0) {
574 new = (struct dups *)Malloc(sizeof(struct dups));
576 pfatal("DUP TABLE OVERFLOW.");
577 if (reply("CONTINUE") == 0) {
585 if (muldup == NULL) {
586 duplist = muldup = new;
589 new->next = muldup->next;
592 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
593 if (dlp->dup == blkno)
595 if (dlp == muldup && dlp->dup != blkno)
599 * count the number of blocks found in id_entryno
603 if (idesc->id_level == 0 && idesc->id_lballoc < idesc->id_lbn)
604 idesc->id_lballoc = idesc->id_lbn;