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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
38 static const char rcsid[] =
42 #include <sys/param.h>
44 #include <sys/sysctl.h>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ufs/dir.h>
48 #include <ufs/ffs/fs.h>
57 static ufs2_daddr_t badblk;
58 static ufs2_daddr_t dupblk;
59 static ino_t lastino; /* last inode in use */
61 static void checkinode(ino_t inumber, struct inodesc *);
68 ino_t inumber, inosused;
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;
99 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
100 if (sblock.fs_magic == FS_UFS2_MAGIC)
101 inosused = cgrp.cg_initediblk;
103 inosused = sblock.fs_ipg;
105 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
106 cdevname, c, sblock.fs_ncg,
107 c * 100 / sblock.fs_ncg);
111 * If we are using soft updates, then we can trust the
112 * cylinder group inode allocation maps to tell us which
113 * inodes are allocated. We will scan the used inode map
114 * to find the inodes that are really in use, and then
115 * read only those inodes in from disk.
117 if (preen && usedsoftdep) {
118 if (!cg_chkmagic(&cgrp))
119 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
120 cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
121 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
124 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
135 * Allocate inoinfo structures for the allocated inodes.
137 inostathead[c].il_numalloced = inosused;
139 inostathead[c].il_stat = 0;
142 info = calloc((unsigned)inosused, sizeof(struct inostat));
144 pfatal("cannot alloc %u bytes for inoinfo\n",
145 (unsigned)(sizeof(struct inostat) * inosused));
146 inostathead[c].il_stat = info;
148 * Scan the allocated inodes.
150 for (i = 0; i < inosused; i++, inumber++) {
151 if (inumber < ROOTINO) {
152 (void)getnextinode(inumber);
155 checkinode(inumber, &idesc);
158 if (inosused < sblock.fs_ipg || inumber == lastino)
161 * If we were not able to determine in advance which inodes
162 * were in use, then reduce the size of the inoinfo structure
163 * to the size necessary to describe the inodes that we
166 if (lastino < (c * sblock.fs_ipg))
169 inosused = lastino - (c * sblock.fs_ipg);
170 inostathead[c].il_numalloced = inosused;
172 free(inostathead[c].il_stat);
173 inostathead[c].il_stat = 0;
176 info = calloc((unsigned)inosused, sizeof(struct inostat));
178 pfatal("cannot alloc %u bytes for inoinfo\n",
179 (unsigned)(sizeof(struct inostat) * inosused));
180 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
181 free(inostathead[c].il_stat);
182 inostathead[c].il_stat = info;
188 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(dp, di_size) = sblock.fs_fsize;
239 DIP(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);
304 if (DIP(dp, di_nlink) <= 0) {
305 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
307 pfatal("LINK COUNT TABLE OVERFLOW");
308 if (reply("CONTINUE") == 0) {
313 zlnp->zlncnt = inumber;
314 zlnp->next = zlnhead;
319 if (DIP(dp, di_size) == 0)
320 inoinfo(inumber)->ino_state = DCLEAR;
322 inoinfo(inumber)->ino_state = DSTATE;
323 cacheino(dp, inumber);
326 inoinfo(inumber)->ino_state = FSTATE;
327 inoinfo(inumber)->ino_type = IFTODT(mode);
329 idesc->id_number = inumber;
330 if (DIP(dp, di_flags) & SF_SNAPSHOT)
331 idesc->id_type = SNAP;
333 idesc->id_type = ADDR;
334 (void)ckinode(dp, idesc);
335 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
336 idesc->id_type = ADDR;
337 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
338 for (j = 0; j < NXADDR; j++) {
340 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
341 idesc->id_numfrags = numfrags(&sblock,
342 fragroundup(&sblock, offset));
344 idesc->id_numfrags = sblock.fs_frag;
345 if (dp->dp2.di_extb[j] == 0)
347 idesc->id_blkno = dp->dp2.di_extb[j];
348 ret = (*idesc->id_func)(idesc);
353 if (sblock.fs_magic == FS_UFS2_MAGIC)
354 eascan(idesc, &dp->dp2);
355 idesc->id_entryno *= btodb(sblock.fs_fsize);
356 if (DIP(dp, di_blocks) != idesc->id_entryno) {
357 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
358 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
359 (uintmax_t)idesc->id_entryno);
361 printf(" (CORRECTED)\n");
362 else if (reply("CORRECT") == 0)
364 if (bkgrdflag == 0) {
365 dp = ginode(inumber);
366 DIP(dp, di_blocks) = idesc->id_entryno;
369 cmd.value = idesc->id_number;
370 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
372 printf("adjblkcnt ino %ju amount %lld\n",
373 (uintmax_t)cmd.value, (long long)cmd.size);
374 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
375 &cmd, sizeof cmd) == -1)
376 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
381 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
382 inoinfo(inumber)->ino_state = FCLEAR;
383 if (reply("CLEAR") == 1) {
384 inoinfo(inumber)->ino_state = USTATE;
385 dp = ginode(inumber);
392 pass1check(struct inodesc *idesc)
396 ufs2_daddr_t blkno = idesc->id_blkno;
400 if (idesc->id_type == SNAP) {
401 if (blkno == BLK_NOCOPY)
403 if (idesc->id_number == cursnapshot) {
404 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
406 if (blkno == BLK_SNAP) {
407 blkno = blkstofrags(&sblock, idesc->id_lbn);
408 idesc->id_entryno -= idesc->id_numfrags;
411 if (blkno == BLK_SNAP)
415 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
416 blkerror(idesc->id_number, "BAD", blkno);
417 if (badblk++ >= MAXBAD) {
418 pwarn("EXCESSIVE BAD BLKS I=%lu",
419 (u_long)idesc->id_number);
421 printf(" (SKIPPING)\n");
422 else if (reply("CONTINUE") == 0) {
429 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
430 if (anyout && chkrange(blkno, 1)) {
432 } else if (!testbmap(blkno)) {
436 blkerror(idesc->id_number, "DUP", blkno);
437 if (dupblk++ >= MAXDUP) {
438 pwarn("EXCESSIVE DUP BLKS I=%lu",
439 (u_long)idesc->id_number);
441 printf(" (SKIPPING)\n");
442 else if (reply("CONTINUE") == 0) {
448 new = (struct dups *)malloc(sizeof(struct dups));
450 pfatal("DUP TABLE OVERFLOW.");
451 if (reply("CONTINUE") == 0) {
459 duplist = muldup = new;
462 new->next = muldup->next;
465 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
466 if (dlp->dup == blkno)
468 if (dlp == muldup && dlp->dup != blkno)
472 * count the number of blocks found in id_entryno