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: src/sbin/fsck_ffs/pass1.c,v 1.43.10.1.2.1 2010/02/10 00:26:20 kensmith Exp $");
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 void checkinode(ino_t inumber, struct inodesc *);
64
ino_t inumber, inosused;
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);
96
if (sblock.fs_magic == FS_UFS2_MAGIC) {
97
inosused = cgrp.cg_initediblk;
98
if (inosused > sblock.fs_ipg)
99
inosused = sblock.fs_ipg;
101
inosused = sblock.fs_ipg;
103
printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
104
cdevname, c, sblock.fs_ncg,
105
c * 100 / sblock.fs_ncg);
109
setproctitle("%s p1 %d%%", cdevname,
110
c * 100 / sblock.fs_ncg);
114
* If we are using soft updates, then we can trust the
115
* cylinder group inode allocation maps to tell us which
116
* inodes are allocated. We will scan the used inode map
117
* to find the inodes that are really in use, and then
118
* read only those inodes in from disk.
120
if (preen && usedsoftdep) {
121
if (!cg_chkmagic(&cgrp))
122
pfatal("CG %d: BAD MAGIC NUMBER\n", c);
123
cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
124
for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
127
for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
138
* Allocate inoinfo structures for the allocated inodes.
140
inostathead[c].il_numalloced = inosused;
142
inostathead[c].il_stat = 0;
145
info = calloc((unsigned)inosused, sizeof(struct inostat));
147
errx(EEXIT, "cannot alloc %u bytes for inoinfo",
148
(unsigned)(sizeof(struct inostat) * inosused));
149
inostathead[c].il_stat = info;
151
* Scan the allocated inodes.
153
for (i = 0; i < inosused; i++, inumber++) {
154
if (inumber < ROOTINO) {
155
(void)getnextinode(inumber);
158
checkinode(inumber, &idesc);
161
if (inosused < sblock.fs_ipg || inumber == lastino)
164
* If we were not able to determine in advance which inodes
165
* were in use, then reduce the size of the inoinfo structure
166
* to the size necessary to describe the inodes that we
169
if (lastino < (c * sblock.fs_ipg))
172
inosused = lastino - (c * sblock.fs_ipg);
173
inostathead[c].il_numalloced = inosused;
175
free(inostathead[c].il_stat);
176
inostathead[c].il_stat = 0;
179
info = calloc((unsigned)inosused, sizeof(struct inostat));
181
errx(EEXIT, "cannot alloc %u bytes for inoinfo",
182
(unsigned)(sizeof(struct inostat) * inosused));
183
memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
184
free(inostathead[c].il_stat);
185
inostathead[c].il_stat = info;
191
checkinode(ino_t inumber, struct inodesc *idesc)
194
off_t kernmaxfilesize;
199
dp = getnextinode(inumber);
200
mode = DIP(dp, di_mode) & IFMT;
202
if ((sblock.fs_magic == FS_UFS1_MAGIC &&
203
(memcmp(dp->dp1.di_db, ufs1_zino.di_db,
204
NDADDR * sizeof(ufs1_daddr_t)) ||
205
memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
206
NIADDR * sizeof(ufs1_daddr_t)) ||
207
dp->dp1.di_mode || dp->dp1.di_size)) ||
208
(sblock.fs_magic == FS_UFS2_MAGIC &&
209
(memcmp(dp->dp2.di_db, ufs2_zino.di_db,
210
NDADDR * sizeof(ufs2_daddr_t)) ||
211
memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
212
NIADDR * sizeof(ufs2_daddr_t)) ||
213
dp->dp2.di_mode || dp->dp2.di_size))) {
214
pfatal("PARTIALLY ALLOCATED INODE I=%lu",
216
if (reply("CLEAR") == 1) {
217
dp = ginode(inumber);
222
inoinfo(inumber)->ino_state = USTATE;
226
/* This should match the file size limit in ffs_mountfs(). */
227
if (sblock.fs_magic == FS_UFS1_MAGIC)
228
kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
230
kernmaxfilesize = sblock.fs_maxfilesize;
231
if (DIP(dp, di_size) > kernmaxfilesize ||
232
DIP(dp, di_size) > sblock.fs_maxfilesize ||
233
(mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
235
printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
238
if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
239
dp = ginode(inumber);
240
DIP_SET(dp, di_size, sblock.fs_fsize);
241
DIP_SET(dp, di_mode, IFREG|0600);
244
if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
245
mode == IFSOCK) && DIP(dp, di_size) != 0) {
247
printf("bad special-file size %ju:",
248
(uintmax_t)DIP(dp, di_size));
251
if ((mode == IFBLK || mode == IFCHR) &&
252
(dev_t)DIP(dp, di_rdev) == NODEV) {
254
printf("bad special-file rdev NODEV:");
257
ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
260
printf("bad size %ju ndb %ju:",
261
(uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
264
if (mode == IFBLK || mode == IFCHR)
268
* Fake ndb value so direct/indirect block checks below
269
* will detect any garbage after symlink string.
271
if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
272
if (sblock.fs_magic == FS_UFS1_MAGIC)
273
ndb = howmany(DIP(dp, di_size),
274
sizeof(ufs1_daddr_t));
276
ndb = howmany(DIP(dp, di_size),
277
sizeof(ufs2_daddr_t));
280
for (ndb = 1; j > 1; j--)
281
ndb *= NINDIR(&sblock);
286
for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
287
if (DIP(dp, di_db[j]) != 0) {
289
printf("bad direct addr[%d]: %ju\n", j,
290
(uintmax_t)DIP(dp, di_db[j]));
293
for (j = 0, ndb -= NDADDR; ndb > 0; j++)
294
ndb /= NINDIR(&sblock);
295
for (; j < NIADDR; j++)
296
if (DIP(dp, di_ib[j]) != 0) {
298
printf("bad indirect addr: %ju\n",
299
(uintmax_t)DIP(dp, di_ib[j]));
302
if (ftypeok(dp) == 0)
305
inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
307
if (DIP(dp, di_size) == 0)
308
inoinfo(inumber)->ino_state = DCLEAR;
309
else if (DIP(dp, di_nlink) <= 0)
310
inoinfo(inumber)->ino_state = DZLINK;
312
inoinfo(inumber)->ino_state = DSTATE;
313
cacheino(dp, inumber);
315
} else if (DIP(dp, di_nlink) <= 0)
316
inoinfo(inumber)->ino_state = FZLINK;
318
inoinfo(inumber)->ino_state = FSTATE;
319
inoinfo(inumber)->ino_type = IFTODT(mode);
321
idesc->id_number = inumber;
322
if (DIP(dp, di_flags) & SF_SNAPSHOT)
323
idesc->id_type = SNAP;
325
idesc->id_type = ADDR;
326
(void)ckinode(dp, idesc);
327
if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
328
idesc->id_type = ADDR;
329
ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
330
for (j = 0; j < NXADDR; j++) {
332
(offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
333
idesc->id_numfrags = numfrags(&sblock,
334
fragroundup(&sblock, offset));
336
idesc->id_numfrags = sblock.fs_frag;
337
if (dp->dp2.di_extb[j] == 0)
339
idesc->id_blkno = dp->dp2.di_extb[j];
340
ret = (*idesc->id_func)(idesc);
345
if (sblock.fs_magic == FS_UFS2_MAGIC)
346
eascan(idesc, &dp->dp2);
347
idesc->id_entryno *= btodb(sblock.fs_fsize);
348
if (DIP(dp, di_blocks) != idesc->id_entryno) {
349
pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
350
(u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
351
(uintmax_t)idesc->id_entryno);
353
printf(" (CORRECTED)\n");
354
else if (reply("CORRECT") == 0)
356
if (bkgrdflag == 0) {
357
dp = ginode(inumber);
358
DIP_SET(dp, di_blocks, idesc->id_entryno);
361
cmd.value = idesc->id_number;
362
cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
364
printf("adjblkcnt ino %ju amount %lld\n",
365
(uintmax_t)cmd.value, (long long)cmd.size);
366
if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
367
&cmd, sizeof cmd) == -1)
368
rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
373
pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
374
inoinfo(inumber)->ino_state = FCLEAR;
375
if (reply("CLEAR") == 1) {
376
inoinfo(inumber)->ino_state = USTATE;
377
dp = ginode(inumber);
384
pass1check(struct inodesc *idesc)
388
ufs2_daddr_t blkno = idesc->id_blkno;
392
if (idesc->id_type == SNAP) {
393
if (blkno == BLK_NOCOPY)
395
if (idesc->id_number == cursnapshot) {
396
if (blkno == blkstofrags(&sblock, idesc->id_lbn))
398
if (blkno == BLK_SNAP) {
399
blkno = blkstofrags(&sblock, idesc->id_lbn);
400
idesc->id_entryno -= idesc->id_numfrags;
403
if (blkno == BLK_SNAP)
407
if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
408
blkerror(idesc->id_number, "BAD", blkno);
409
if (badblk++ >= MAXBAD) {
410
pwarn("EXCESSIVE BAD BLKS I=%lu",
411
(u_long)idesc->id_number);
413
printf(" (SKIPPING)\n");
414
else if (reply("CONTINUE") == 0) {
421
for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
422
if (anyout && chkrange(blkno, 1)) {
424
} else if (!testbmap(blkno)) {
428
blkerror(idesc->id_number, "DUP", blkno);
429
if (dupblk++ >= MAXDUP) {
430
pwarn("EXCESSIVE DUP BLKS I=%lu",
431
(u_long)idesc->id_number);
433
printf(" (SKIPPING)\n");
434
else if (reply("CONTINUE") == 0) {
440
new = (struct dups *)malloc(sizeof(struct dups));
442
pfatal("DUP TABLE OVERFLOW.");
443
if (reply("CONTINUE") == 0) {
451
duplist = muldup = new;
454
new->next = muldup->next;
457
for (dlp = duplist; dlp != muldup; dlp = dlp->next)
458
if (dlp->dup == blkno)
460
if (dlp == muldup && dlp->dup != blkno)
464
* count the number of blocks found in id_entryno