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>
55 static ufs2_daddr_t badblk;
56 static ufs2_daddr_t dupblk;
57 static ino_t lastino; /* last inode in use */
59 static void checkinode(ino_t inumber, struct inodesc *);
66 ino_t inumber, inosused;
72 * Set filesystem reserved blocks in used block map.
74 for (c = 0; c < sblock.fs_ncg; c++) {
75 cgd = cgdmin(&sblock, c);
77 i = cgbase(&sblock, c);
79 i = cgsblock(&sblock, c);
84 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
89 * Find all allocated blocks.
91 memset(&idesc, 0, sizeof(struct inodesc));
92 idesc.id_func = pass1check;
94 for (c = 0; c < sblock.fs_ncg; c++) {
95 inumber = c * sblock.fs_ipg;
97 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
98 if (sblock.fs_magic == FS_UFS2_MAGIC)
99 inosused = cgrp.cg_initediblk;
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 * If we are using soft updates, then we can trust the
110 * cylinder group inode allocation maps to tell us which
111 * inodes are allocated. We will scan the used inode map
112 * to find the inodes that are really in use, and then
113 * read only those inodes in from disk.
115 if (preen && usedsoftdep) {
116 if (!cg_chkmagic(&cgrp))
117 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
118 cp = &cg_inosused(&cgrp)[(inosused - 1) / NBBY];
119 for ( ; inosused > 0; inosused -= NBBY, cp--) {
122 for (i = 1 << (NBBY - 1); i > 0; i >>= 1) {
133 * Allocate inoinfo structures for the allocated inodes.
135 inostathead[c].il_numalloced = inosused;
137 inostathead[c].il_stat = 0;
140 info = calloc((unsigned)inosused, sizeof(struct inostat));
142 pfatal("cannot alloc %u bytes for inoinfo\n",
143 (unsigned)(sizeof(struct inostat) * inosused));
144 inostathead[c].il_stat = info;
146 * Scan the allocated inodes.
148 for (i = 0; i < inosused; i++, inumber++) {
149 if (inumber < ROOTINO) {
150 (void)getnextinode(inumber);
153 checkinode(inumber, &idesc);
156 if (inosused < sblock.fs_ipg || inumber == lastino)
159 * If we were not able to determine in advance which inodes
160 * were in use, then reduce the size of the inoinfo structure
161 * to the size necessary to describe the inodes that we
164 if (lastino < (c * sblock.fs_ipg))
167 inosused = lastino - (c * sblock.fs_ipg);
168 inostathead[c].il_numalloced = inosused;
170 free(inostathead[c].il_stat);
171 inostathead[c].il_stat = 0;
174 info = calloc((unsigned)inosused, sizeof(struct inostat));
176 pfatal("cannot alloc %u bytes for inoinfo\n",
177 (unsigned)(sizeof(struct inostat) * inosused));
178 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
179 free(inostathead[c].il_stat);
180 inostathead[c].il_stat = info;
186 checkinode(ino_t inumber, struct inodesc *idesc)
190 off_t kernmaxfilesize;
196 dp = getnextinode(inumber);
197 mode = DIP(dp, di_mode) & IFMT;
199 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
200 (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
201 NDADDR * sizeof(ufs1_daddr_t)) ||
202 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
203 NIADDR * sizeof(ufs1_daddr_t)) ||
204 dp->dp1.di_mode || dp->dp1.di_size)) ||
205 (sblock.fs_magic == FS_UFS2_MAGIC &&
206 (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
207 NDADDR * sizeof(ufs2_daddr_t)) ||
208 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
209 NIADDR * sizeof(ufs2_daddr_t)) ||
210 dp->dp2.di_mode || dp->dp2.di_size))) {
211 pfatal("PARTIALLY ALLOCATED INODE I=%lu",
213 if (reply("CLEAR") == 1) {
214 dp = ginode(inumber);
219 inoinfo(inumber)->ino_state = USTATE;
223 /* This should match the file size limit in ffs_mountfs(). */
224 if (sblock.fs_magic == FS_UFS1_MAGIC)
225 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
227 kernmaxfilesize = sblock.fs_maxfilesize;
228 if (DIP(dp, di_size) > kernmaxfilesize ||
229 DIP(dp, di_size) > sblock.fs_maxfilesize ||
230 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
232 printf("bad size %qu:", DIP(dp, di_size));
235 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
236 dp = ginode(inumber);
237 DIP(dp, di_size) = sblock.fs_fsize;
238 DIP(dp, di_mode) = IFREG|0600;
241 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
242 mode == IFSOCK) && DIP(dp, di_size) != 0) {
244 printf("bad special-file size %qu:", DIP(dp, di_size));
247 if ((mode == IFBLK || mode == IFCHR) &&
248 (dev_t)DIP(dp, di_rdev) == NODEV) {
250 printf("bad special-file rdev NODEV:");
253 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
256 printf("bad size %qu ndb %qu:",
257 DIP(dp, di_size), ndb);
260 if (mode == IFBLK || mode == IFCHR)
264 * Fake ndb value so direct/indirect block checks below
265 * will detect any garbage after symlink string.
267 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
268 if (sblock.fs_magic == FS_UFS1_MAGIC)
269 ndb = howmany(DIP(dp, di_size),
270 sizeof(ufs1_daddr_t));
272 ndb = howmany(DIP(dp, di_size),
273 sizeof(ufs2_daddr_t));
276 for (ndb = 1; j > 1; j--)
277 ndb *= NINDIR(&sblock);
282 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
283 if (DIP(dp, di_db[j]) != 0) {
285 printf("bad direct addr[%d]: %qu\n", j,
286 (ufs2_daddr_t)DIP(dp, di_db[j]));
289 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
290 ndb /= NINDIR(&sblock);
291 for (; j < NIADDR; j++)
292 if (DIP(dp, di_ib[j]) != 0) {
294 printf("bad indirect addr: %qu\n",
298 if (ftypeok(dp) == 0)
301 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
302 if (DIP(dp, di_nlink) <= 0) {
303 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
305 pfatal("LINK COUNT TABLE OVERFLOW");
306 if (reply("CONTINUE") == 0) {
311 zlnp->zlncnt = inumber;
312 zlnp->next = zlnhead;
317 if (DIP(dp, di_size) == 0)
318 inoinfo(inumber)->ino_state = DCLEAR;
320 inoinfo(inumber)->ino_state = DSTATE;
321 cacheino(dp, inumber);
324 inoinfo(inumber)->ino_state = FSTATE;
325 inoinfo(inumber)->ino_type = IFTODT(mode);
327 idesc->id_number = inumber;
328 if (DIP(dp, di_flags) & SF_SNAPSHOT)
329 idesc->id_type = SNAP;
331 idesc->id_type = ADDR;
332 (void)ckinode(dp, idesc);
333 idesc->id_entryno *= btodb(sblock.fs_fsize);
334 if (DIP(dp, di_blocks) != idesc->id_entryno) {
335 pwarn("INCORRECT BLOCK COUNT I=%lu (%qu should be %qu)",
336 (u_long)inumber, DIP(dp, di_blocks),
339 printf(" (CORRECTED)\n");
340 else if (reply("CORRECT") == 0)
342 if (bkgrdflag == 0) {
343 dp = ginode(inumber);
344 DIP(dp, di_blocks) = idesc->id_entryno;
347 cmd.value = idesc->id_number;
348 cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
350 printf("adjblkcnt ino %qu amount %ld\n",
351 cmd.value, cmd.size);
352 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
353 &cmd, sizeof cmd) == -1)
354 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
359 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
360 inoinfo(inumber)->ino_state = FCLEAR;
361 if (reply("CLEAR") == 1) {
362 inoinfo(inumber)->ino_state = USTATE;
363 dp = ginode(inumber);
370 pass1check(struct inodesc *idesc)
374 ufs2_daddr_t blkno = idesc->id_blkno;
378 if (idesc->id_type == SNAP) {
379 if (blkno == BLK_NOCOPY)
381 if (idesc->id_number == cursnapshot) {
382 if (blkno == blkstofrags(&sblock, idesc->id_lbn))
384 if (blkno == BLK_SNAP) {
385 blkno = blkstofrags(&sblock, idesc->id_lbn);
386 idesc->id_entryno -= idesc->id_numfrags;
389 if (blkno == BLK_SNAP)
393 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
394 blkerror(idesc->id_number, "BAD", blkno);
395 if (badblk++ >= MAXBAD) {
396 pwarn("EXCESSIVE BAD BLKS I=%lu",
397 (u_long)idesc->id_number);
399 printf(" (SKIPPING)\n");
400 else if (reply("CONTINUE") == 0) {
407 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
408 if (anyout && chkrange(blkno, 1)) {
410 } else if (!testbmap(blkno)) {
414 blkerror(idesc->id_number, "DUP", blkno);
415 if (dupblk++ >= MAXDUP) {
416 pwarn("EXCESSIVE DUP BLKS I=%lu",
417 (u_long)idesc->id_number);
419 printf(" (SKIPPING)\n");
420 else if (reply("CONTINUE") == 0) {
426 new = (struct dups *)malloc(sizeof(struct dups));
428 pfatal("DUP TABLE OVERFLOW.");
429 if (reply("CONTINUE") == 0) {
437 duplist = muldup = new;
440 new->next = muldup->next;
443 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
444 if (dlp->dup == blkno)
446 if (dlp == muldup && dlp->dup != blkno)
450 * count the number of blocks found in id_entryno