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 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;
99 inosused = sblock.fs_ipg;
101 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
102 cdevname, c, sblock.fs_ncg,
103 c * 100 / sblock.fs_ncg);
107 setproctitle("%s p1 %d%%", cdevname,
108 c * 100 / sblock.fs_ncg);
112 * If we are using soft updates, then we can trust the
113 * cylinder group inode allocation maps to tell us which
114 * inodes are allocated. We will scan the used inode map
115 * to find the inodes that are really in use, and then
116 * read only those inodes in from disk.
118 if (preen && usedsoftdep) {
119 if (!cg_chkmagic(&cgrp))
120 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
121 cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
122 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
125 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
136 * Allocate inoinfo structures for the allocated inodes.
138 inostathead[c].il_numalloced = inosused;
140 inostathead[c].il_stat = 0;
143 info = calloc((unsigned)inosused, sizeof(struct inostat));
145 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
146 (unsigned)(sizeof(struct inostat) * inosused));
147 inostathead[c].il_stat = info;
149 * Scan the allocated inodes.
151 for (i = 0; i < inosused; i++, inumber++) {
152 if (inumber < ROOTINO) {
153 (void)getnextinode(inumber);
156 checkinode(inumber, &idesc);
159 if (inosused < sblock.fs_ipg || inumber == lastino)
162 * If we were not able to determine in advance which inodes
163 * were in use, then reduce the size of the inoinfo structure
164 * to the size necessary to describe the inodes that we
167 if (lastino < (c * sblock.fs_ipg))
170 inosused = lastino - (c * sblock.fs_ipg);
171 inostathead[c].il_numalloced = inosused;
173 free(inostathead[c].il_stat);
174 inostathead[c].il_stat = 0;
177 info = calloc((unsigned)inosused, sizeof(struct inostat));
179 errx(EEXIT, "cannot alloc %u bytes for inoinfo",
180 (unsigned)(sizeof(struct inostat) * inosused));
181 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
182 free(inostathead[c].il_stat);
183 inostathead[c].il_stat = info;
189 checkinode(ino_t inumber, struct inodesc *idesc)
192 off_t kernmaxfilesize;
197 dp = getnextinode(inumber);
198 mode = DIP(dp, di_mode) & IFMT;
200 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
201 (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
202 NDADDR * sizeof(ufs1_daddr_t)) ||
203 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
204 NIADDR * sizeof(ufs1_daddr_t)) ||
205 dp->dp1.di_mode || dp->dp1.di_size)) ||
206 (sblock.fs_magic == FS_UFS2_MAGIC &&
207 (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
208 NDADDR * sizeof(ufs2_daddr_t)) ||
209 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
210 NIADDR * sizeof(ufs2_daddr_t)) ||
211 dp->dp2.di_mode || dp->dp2.di_size))) {
212 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
214 if (reply("CLEAR") == 1) {
215 dp = ginode(inumber);
220 inoinfo(inumber)->ino_state = USTATE;
224 /* This should match the file size limit in ffs_mountfs(). */
225 if (sblock.fs_magic == FS_UFS1_MAGIC)
226 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
228 kernmaxfilesize = sblock.fs_maxfilesize;
229 if (DIP(dp, di_size) > kernmaxfilesize ||
230 DIP(dp, di_size) > sblock.fs_maxfilesize ||
231 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
233 printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
236 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
237 dp = ginode(inumber);
238 DIP_SET(dp, di_size, sblock.fs_fsize);
239 DIP_SET(dp, di_mode, IFREG|0600);
242 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
243 mode == IFSOCK) && DIP(dp, di_size) != 0) {
245 printf("bad special-file size %ju:",
246 (uintmax_t)DIP(dp, di_size));
249 if ((mode == IFBLK || mode == IFCHR) &&
250 (dev_t)DIP(dp, di_rdev) == NODEV) {
252 printf("bad special-file rdev NODEV:");
255 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
258 printf("bad size %ju ndb %ju:",
259 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
262 if (mode == IFBLK || mode == IFCHR)
266 * Fake ndb value so direct/indirect block checks below
267 * will detect any garbage after symlink string.
269 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
270 if (sblock.fs_magic == FS_UFS1_MAGIC)
271 ndb = howmany(DIP(dp, di_size),
272 sizeof(ufs1_daddr_t));
274 ndb = howmany(DIP(dp, di_size),
275 sizeof(ufs2_daddr_t));
278 for (ndb = 1; j > 1; j--)
279 ndb *= NINDIR(&sblock);
284 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
285 if (DIP(dp, di_db[j]) != 0) {
287 printf("bad direct addr[%d]: %ju\n", j,
288 (uintmax_t)DIP(dp, di_db[j]));
291 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
292 ndb /= NINDIR(&sblock);
293 for (; j < NIADDR; j++)
294 if (DIP(dp, di_ib[j]) != 0) {
296 printf("bad indirect addr: %ju\n",
297 (uintmax_t)DIP(dp, di_ib[j]));
300 if (ftypeok(dp) == 0)
303 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
305 if (DIP(dp, di_size) == 0)
306 inoinfo(inumber)->ino_state = DCLEAR;
307 else if (DIP(dp, di_nlink) <= 0)
308 inoinfo(inumber)->ino_state = DZLINK;
310 inoinfo(inumber)->ino_state = DSTATE;
311 cacheino(dp, inumber);
313 } else if (DIP(dp, di_nlink) <= 0)
314 inoinfo(inumber)->ino_state = FZLINK;
316 inoinfo(inumber)->ino_state = FSTATE;
317 inoinfo(inumber)->ino_type = IFTODT(mode);
319 idesc->id_number = inumber;
320 if (DIP(dp, di_flags) & SF_SNAPSHOT)
321 idesc->id_type = SNAP;
323 idesc->id_type = ADDR;
324 (void)ckinode(dp, idesc);
325 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
326 idesc->id_type = ADDR;
327 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
328 for (j = 0; j < NXADDR; j++) {
330 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
331 idesc->id_numfrags = numfrags(&sblock,
332 fragroundup(&sblock, offset));
334 idesc->id_numfrags = sblock.fs_frag;
335 if (dp->dp2.di_extb[j] == 0)
337 idesc->id_blkno = dp->dp2.di_extb[j];
338 ret = (*idesc->id_func)(idesc);
343 if (sblock.fs_magic == FS_UFS2_MAGIC)
344 eascan(idesc, &dp->dp2);
345 idesc->id_entryno *= btodb(sblock.fs_fsize);
346 if (DIP(dp, di_blocks) != idesc->id_entryno) {
347 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
348 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
349 (uintmax_t)idesc->id_entryno);
351 printf(" (CORRECTED)\n");
352 else if (reply("CORRECT") == 0)
354 if (bkgrdflag == 0) {
355 dp = ginode(inumber);
356 DIP_SET(dp, di_blocks, idesc->id_entryno);
359 cmd.value = idesc->id_number;
360 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
362 printf("adjblkcnt ino %ju amount %lld\n",
363 (uintmax_t)cmd.value, (long long)cmd.size);
364 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
365 &cmd, sizeof cmd) == -1)
366 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
371 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
372 inoinfo(inumber)->ino_state = FCLEAR;
373 if (reply("CLEAR") == 1) {
374 inoinfo(inumber)->ino_state = USTATE;
375 dp = ginode(inumber);
382 pass1check(struct inodesc *idesc)
386 ufs2_daddr_t blkno = idesc->id_blkno;
390 if (idesc->id_type == SNAP) {
391 if (blkno == BLK_NOCOPY)
393 if (idesc->id_number == cursnapshot) {
394 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
396 if (blkno == BLK_SNAP) {
397 blkno = blkstofrags(&sblock, idesc->id_lbn);
398 idesc->id_entryno -= idesc->id_numfrags;
401 if (blkno == BLK_SNAP)
405 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
406 blkerror(idesc->id_number, "BAD", blkno);
407 if (badblk++ >= MAXBAD) {
408 pwarn("EXCESSIVE BAD BLKS I=%lu",
409 (u_long)idesc->id_number);
411 printf(" (SKIPPING)\n");
412 else if (reply("CONTINUE") == 0) {
419 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
420 if (anyout && chkrange(blkno, 1)) {
422 } else if (!testbmap(blkno)) {
426 blkerror(idesc->id_number, "DUP", blkno);
427 if (dupblk++ >= MAXDUP) {
428 pwarn("EXCESSIVE DUP BLKS I=%lu",
429 (u_long)idesc->id_number);
431 printf(" (SKIPPING)\n");
432 else if (reply("CONTINUE") == 0) {
438 new = (struct dups *)malloc(sizeof(struct dups));
440 pfatal("DUP TABLE OVERFLOW.");
441 if (reply("CONTINUE") == 0) {
449 duplist = muldup = new;
452 new->next = muldup->next;
455 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
456 if (dlp->dup == blkno)
458 if (dlp == muldup && dlp->dup != blkno)
462 * count the number of blocks found in id_entryno