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
inosused = sblock.fs_ipg;
104
inosused = sblock.fs_ipg;
106
printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
107
cdevname, c, sblock.fs_ncg,
108
c * 100 / sblock.fs_ncg);
112
setproctitle("%s p1 %d%%", cdevname,
113
c * 100 / sblock.fs_ncg);
117
* If we are using soft updates, then we can trust the
118
* cylinder group inode allocation maps to tell us which
119
* inodes are allocated. We will scan the used inode map
120
* to find the inodes that are really in use, and then
121
* read only those inodes in from disk.
123
if ((preen || inoopt) && usedsoftdep && !rebuildcg) {
124
cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
125
for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
128
for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
139
* Allocate inoinfo structures for the allocated inodes.
141
inostathead[c].il_numalloced = inosused;
143
inostathead[c].il_stat = 0;
146
info = calloc((unsigned)inosused, sizeof(struct inostat));
148
errx(EEXIT, "cannot alloc %u bytes for inoinfo",
149
(unsigned)(sizeof(struct inostat) * inosused));
150
inostathead[c].il_stat = info;
152
* Scan the allocated inodes.
154
for (i = 0; i < inosused; i++, inumber++) {
155
if (inumber < ROOTINO) {
156
(void)getnextinode(inumber, rebuildcg);
160
* NULL return indicates probable end of allocated
161
* inodes during cylinder group rebuild attempt.
162
* We always keep trying until we get to the minimum
163
* valid number for this cylinder group.
165
if (checkinode(inumber, &idesc, rebuildcg) == 0 &&
166
i > cgrp.cg_initediblk)
170
* This optimization speeds up future runs of fsck
171
* by trimming down the number of inodes in cylinder
172
* groups that formerly had many inodes but now have
175
mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
176
if (inoopt && !preen && !rebuildcg &&
177
sblock.fs_magic == FS_UFS2_MAGIC &&
178
cgrp.cg_initediblk > 2 * INOPB(&sblock) &&
179
mininos < cgrp.cg_initediblk) {
180
i = cgrp.cg_initediblk;
181
if (mininos < 2 * INOPB(&sblock))
182
cgrp.cg_initediblk = 2 * INOPB(&sblock);
184
cgrp.cg_initediblk = mininos;
185
pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
186
c, i, cgrp.cg_initediblk, "VALID INODES");
189
if (inosused < sblock.fs_ipg)
192
if (lastino < (c * sblock.fs_ipg))
195
inosused = lastino - (c * sblock.fs_ipg);
196
if (rebuildcg && inosused > cgrp.cg_initediblk &&
197
sblock.fs_magic == FS_UFS2_MAGIC) {
198
cgrp.cg_initediblk = roundup(inosused, INOPB(&sblock));
199
pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
203
* If we were not able to determine in advance which inodes
204
* were in use, then reduce the size of the inoinfo structure
205
* to the size necessary to describe the inodes that we
208
if (inumber == lastino)
210
inostathead[c].il_numalloced = inosused;
212
free(inostathead[c].il_stat);
213
inostathead[c].il_stat = 0;
216
info = calloc((unsigned)inosused, sizeof(struct inostat));
218
errx(EEXIT, "cannot alloc %u bytes for inoinfo",
219
(unsigned)(sizeof(struct inostat) * inosused));
220
memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
221
free(inostathead[c].il_stat);
222
inostathead[c].il_stat = info;
228
checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg)
231
off_t kernmaxfilesize;
236
if ((dp = getnextinode(inumber, rebuildcg)) == NULL)
238
mode = DIP(dp, di_mode) & IFMT;
240
if ((sblock.fs_magic == FS_UFS1_MAGIC &&
241
(memcmp(dp->dp1.di_db, ufs1_zino.di_db,
242
NDADDR * sizeof(ufs1_daddr_t)) ||
243
memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
244
NIADDR * sizeof(ufs1_daddr_t)) ||
245
dp->dp1.di_mode || dp->dp1.di_size)) ||
246
(sblock.fs_magic == FS_UFS2_MAGIC &&
247
(memcmp(dp->dp2.di_db, ufs2_zino.di_db,
248
NDADDR * sizeof(ufs2_daddr_t)) ||
249
memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
250
NIADDR * sizeof(ufs2_daddr_t)) ||
251
dp->dp2.di_mode || dp->dp2.di_size))) {
252
pfatal("PARTIALLY ALLOCATED INODE I=%lu",
254
if (reply("CLEAR") == 1) {
255
dp = ginode(inumber);
260
inoinfo(inumber)->ino_state = USTATE;
264
/* This should match the file size limit in ffs_mountfs(). */
265
if (sblock.fs_magic == FS_UFS1_MAGIC)
266
kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
268
kernmaxfilesize = sblock.fs_maxfilesize;
269
if (DIP(dp, di_size) > kernmaxfilesize ||
270
DIP(dp, di_size) > sblock.fs_maxfilesize ||
271
(mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
273
printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
276
if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
277
dp = ginode(inumber);
278
DIP_SET(dp, di_size, sblock.fs_fsize);
279
DIP_SET(dp, di_mode, IFREG|0600);
282
if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
283
mode == IFSOCK) && DIP(dp, di_size) != 0) {
285
printf("bad special-file size %ju:",
286
(uintmax_t)DIP(dp, di_size));
289
if ((mode == IFBLK || mode == IFCHR) &&
290
(dev_t)DIP(dp, di_rdev) == NODEV) {
292
printf("bad special-file rdev NODEV:");
295
ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
298
printf("bad size %ju ndb %ju:",
299
(uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
302
if (mode == IFBLK || mode == IFCHR)
306
* Fake ndb value so direct/indirect block checks below
307
* will detect any garbage after symlink string.
309
if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
310
if (sblock.fs_magic == FS_UFS1_MAGIC)
311
ndb = howmany(DIP(dp, di_size),
312
sizeof(ufs1_daddr_t));
314
ndb = howmany(DIP(dp, di_size),
315
sizeof(ufs2_daddr_t));
318
for (ndb = 1; j > 1; j--)
319
ndb *= NINDIR(&sblock);
324
for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
325
if (DIP(dp, di_db[j]) != 0) {
327
printf("bad direct addr[%d]: %ju\n", j,
328
(uintmax_t)DIP(dp, di_db[j]));
331
for (j = 0, ndb -= NDADDR; ndb > 0; j++)
332
ndb /= NINDIR(&sblock);
333
for (; j < NIADDR; j++)
334
if (DIP(dp, di_ib[j]) != 0) {
336
printf("bad indirect addr: %ju\n",
337
(uintmax_t)DIP(dp, di_ib[j]));
340
if (ftypeok(dp) == 0)
343
inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
345
if (DIP(dp, di_size) == 0)
346
inoinfo(inumber)->ino_state = DCLEAR;
347
else if (DIP(dp, di_nlink) <= 0)
348
inoinfo(inumber)->ino_state = DZLINK;
350
inoinfo(inumber)->ino_state = DSTATE;
351
cacheino(dp, inumber);
353
} else if (DIP(dp, di_nlink) <= 0)
354
inoinfo(inumber)->ino_state = FZLINK;
356
inoinfo(inumber)->ino_state = FSTATE;
357
inoinfo(inumber)->ino_type = IFTODT(mode);
359
idesc->id_number = inumber;
360
if (DIP(dp, di_flags) & SF_SNAPSHOT)
361
idesc->id_type = SNAP;
363
idesc->id_type = ADDR;
364
(void)ckinode(dp, idesc);
365
if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
366
idesc->id_type = ADDR;
367
ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
368
for (j = 0; j < NXADDR; j++) {
370
(offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
371
idesc->id_numfrags = numfrags(&sblock,
372
fragroundup(&sblock, offset));
374
idesc->id_numfrags = sblock.fs_frag;
375
if (dp->dp2.di_extb[j] == 0)
377
idesc->id_blkno = dp->dp2.di_extb[j];
378
ret = (*idesc->id_func)(idesc);
383
if (sblock.fs_magic == FS_UFS2_MAGIC)
384
eascan(idesc, &dp->dp2);
385
idesc->id_entryno *= btodb(sblock.fs_fsize);
386
if (DIP(dp, di_blocks) != idesc->id_entryno) {
387
pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
388
(u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
389
(uintmax_t)idesc->id_entryno);
391
printf(" (CORRECTED)\n");
392
else if (reply("CORRECT") == 0)
394
if (bkgrdflag == 0) {
395
dp = ginode(inumber);
396
DIP_SET(dp, di_blocks, idesc->id_entryno);
399
cmd.value = idesc->id_number;
400
cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
402
printf("adjblkcnt ino %ju amount %lld\n",
403
(uintmax_t)cmd.value, (long long)cmd.size);
404
if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
405
&cmd, sizeof cmd) == -1)
406
rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
411
pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
412
inoinfo(inumber)->ino_state = FCLEAR;
413
if (reply("CLEAR") == 1) {
414
inoinfo(inumber)->ino_state = USTATE;
415
dp = ginode(inumber);
423
pass1check(struct inodesc *idesc)
427
ufs2_daddr_t blkno = idesc->id_blkno;
431
if (idesc->id_type == SNAP) {
432
if (blkno == BLK_NOCOPY)
434
if (idesc->id_number == cursnapshot) {
435
if (blkno == blkstofrags(&sblock, idesc->id_lbn))
437
if (blkno == BLK_SNAP) {
438
blkno = blkstofrags(&sblock, idesc->id_lbn);
439
idesc->id_entryno -= idesc->id_numfrags;
442
if (blkno == BLK_SNAP)
446
if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
447
blkerror(idesc->id_number, "BAD", blkno);
448
if (badblk++ >= MAXBAD) {
449
pwarn("EXCESSIVE BAD BLKS I=%lu",
450
(u_long)idesc->id_number);
452
printf(" (SKIPPING)\n");
453
else if (reply("CONTINUE") == 0) {
460
for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
461
if (anyout && chkrange(blkno, 1)) {
463
} else if (!testbmap(blkno)) {
467
blkerror(idesc->id_number, "DUP", blkno);
468
if (dupblk++ >= MAXDUP) {
469
pwarn("EXCESSIVE DUP BLKS I=%lu",
470
(u_long)idesc->id_number);
472
printf(" (SKIPPING)\n");
473
else if (reply("CONTINUE") == 0) {
479
new = (struct dups *)malloc(sizeof(struct dups));
481
pfatal("DUP TABLE OVERFLOW.");
482
if (reply("CONTINUE") == 0) {
490
duplist = muldup = new;
493
new->next = muldup->next;
496
for (dlp = duplist; dlp != muldup; dlp = dlp->next)
497
if (dlp->dup == blkno)
499
if (dlp == muldup && dlp->dup != blkno)
503
* count the number of blocks found in id_entryno