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 <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
53 static ufs_daddr_t badblk;
54 static ufs_daddr_t dupblk;
55 static ino_t lastino; /* last inode in use */
57 static void checkinode __P((ino_t inumber, struct inodesc *));
64 int c, i, cgd, inosused;
69 * Set file system reserved blocks in used block map.
71 for (c = 0; c < sblock.fs_ncg; c++) {
72 cgd = cgdmin(&sblock, c);
74 i = cgbase(&sblock, c);
75 cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
77 i = cgsblock(&sblock, c);
82 * Find all allocated blocks.
84 memset(&idesc, 0, sizeof(struct inodesc));
86 idesc.id_func = pass1check;
88 for (c = 0; c < sblock.fs_ncg; c++) {
89 inumber = c * sblock.fs_ipg;
91 inosused = sblock.fs_ipg;
93 * If we are using soft updates, then we can trust the
94 * cylinder group inode allocation maps to tell us which
95 * inodes are allocated. We will scan the used inode map
96 * to find the inodes that are really in use, and then
97 * read only those inodes in from disk.
99 if (preen && usedsoftdep) {
100 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
101 if (!cg_chkmagic(&cgrp))
102 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
103 cp = &cg_inosused(&cgrp)[(sblock.fs_ipg - 1) / NBBY];
104 for ( ; inosused > 0; inosused -= NBBY, cp--) {
107 for (i = 1 << (NBBY - 1); i > 0; i >>= 1) {
118 * Allocate inoinfo structures for the allocated inodes.
120 inostathead[c].il_numalloced = inosused;
122 inostathead[c].il_stat = 0;
125 info = calloc((unsigned)inosused, sizeof(struct inostat));
127 pfatal("cannot alloc %u bytes for inoinfo\n",
128 (unsigned)(sizeof(struct inostat) * inosused));
129 inostathead[c].il_stat = info;
131 * Scan the allocated inodes.
133 for (i = 0; i < inosused; i++, inumber++) {
134 if (inumber < ROOTINO) {
135 (void)getnextinode(inumber);
138 checkinode(inumber, &idesc);
141 if (inosused < sblock.fs_ipg || inumber == lastino)
144 * If we were not able to determine in advance which inodes
145 * were in use, then reduce the size of the inoinfo structure
146 * to the size necessary to describe the inodes that we
149 inosused = lastino - (c * sblock.fs_ipg);
152 inostathead[c].il_numalloced = inosused;
154 free(inostathead[c].il_stat);
155 inostathead[c].il_stat = 0;
158 info = calloc((unsigned)inosused, sizeof(struct inostat));
160 pfatal("cannot alloc %u bytes for inoinfo\n",
161 (unsigned)(sizeof(struct inostat) * inosused));
162 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
163 free(inostathead[c].il_stat);
164 inostathead[c].il_stat = info;
170 checkinode(inumber, idesc)
172 register struct inodesc *idesc;
174 register struct dinode *dp;
180 dp = getnextinode(inumber);
181 mode = dp->di_mode & IFMT;
183 if (memcmp(dp->di_db, zino.di_db,
184 NDADDR * sizeof(ufs_daddr_t)) ||
185 memcmp(dp->di_ib, zino.di_ib,
186 NIADDR * sizeof(ufs_daddr_t)) ||
187 dp->di_mode || dp->di_size) {
188 pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
189 if (reply("CLEAR") == 1) {
190 dp = ginode(inumber);
195 inoinfo(inumber)->ino_state = USTATE;
199 if (/* dp->di_size < 0 || */
200 dp->di_size + sblock.fs_bsize - 1 < dp->di_size ||
201 (mode == IFDIR && dp->di_size > MAXDIRSIZE)) {
203 printf("bad size %qu:", dp->di_size);
206 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
207 dp = ginode(inumber);
208 dp->di_size = sblock.fs_fsize;
209 dp->di_mode = IFREG|0600;
212 ndb = howmany(dp->di_size, sblock.fs_bsize);
215 printf("bad size %qu ndb %d:",
219 if (mode == IFBLK || mode == IFCHR)
223 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
224 dp->di_blocks != 0) {
225 symbuf = alloca(secsize);
226 if (bread(fsreadfd, symbuf,
227 fsbtodb(&sblock, dp->di_db[0]),
229 errx(EEXIT, "cannot read symlink");
231 symbuf[dp->di_size] = 0;
232 printf("convert symlink %lu(%s) of size %ld\n",
233 (u_long)inumber, symbuf, (long)dp->di_size);
235 dp = ginode(inumber);
236 memmove(dp->di_shortlink, symbuf, (long)dp->di_size);
241 * Fake ndb value so direct/indirect block checks below
242 * will detect any garbage after symlink string.
244 if (dp->di_size < sblock.fs_maxsymlinklen) {
245 ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
248 for (ndb = 1; j > 1; j--)
249 ndb *= NINDIR(&sblock);
254 for (j = ndb; j < NDADDR; j++)
255 if (dp->di_db[j] != 0) {
257 printf("bad direct addr: %ld\n",
261 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
262 ndb /= NINDIR(&sblock);
263 for (; j < NIADDR; j++)
264 if (dp->di_ib[j] != 0) {
266 printf("bad indirect addr: %ld\n",
270 if (ftypeok(dp) == 0)
273 inoinfo(inumber)->ino_linkcnt = dp->di_nlink;
274 if (dp->di_nlink <= 0) {
275 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
277 pfatal("LINK COUNT TABLE OVERFLOW");
278 if (reply("CONTINUE") == 0) {
283 zlnp->zlncnt = inumber;
284 zlnp->next = zlnhead;
289 if (dp->di_size == 0)
290 inoinfo(inumber)->ino_state = DCLEAR;
292 inoinfo(inumber)->ino_state = DSTATE;
293 cacheino(dp, inumber);
296 inoinfo(inumber)->ino_state = FSTATE;
297 inoinfo(inumber)->ino_type = IFTODT(mode);
299 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
300 dp = ginode(inumber);
301 dp->di_uid = dp->di_ouid;
303 dp->di_gid = dp->di_ogid;
308 idesc->id_number = inumber;
309 (void)ckinode(dp, idesc);
310 idesc->id_entryno *= btodb(sblock.fs_fsize);
311 if (dp->di_blocks != idesc->id_entryno) {
312 pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
313 inumber, dp->di_blocks, idesc->id_entryno);
315 printf(" (CORRECTED)\n");
316 else if (reply("CORRECT") == 0)
318 dp = ginode(inumber);
319 dp->di_blocks = idesc->id_entryno;
324 pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
325 inoinfo(inumber)->ino_state = FCLEAR;
326 if (reply("CLEAR") == 1) {
327 inoinfo(inumber)->ino_state = USTATE;
328 dp = ginode(inumber);
336 register struct inodesc *idesc;
340 ufs_daddr_t blkno = idesc->id_blkno;
341 register struct dups *dlp;
344 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
345 blkerror(idesc->id_number, "BAD", blkno);
346 if (badblk++ >= MAXBAD) {
347 pwarn("EXCESSIVE BAD BLKS I=%lu",
350 printf(" (SKIPPING)\n");
351 else if (reply("CONTINUE") == 0) {
358 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
359 if (anyout && chkrange(blkno, 1)) {
361 } else if (!testbmap(blkno)) {
365 blkerror(idesc->id_number, "DUP", blkno);
366 if (dupblk++ >= MAXDUP) {
367 pwarn("EXCESSIVE DUP BLKS I=%lu",
370 printf(" (SKIPPING)\n");
371 else if (reply("CONTINUE") == 0) {
377 new = (struct dups *)malloc(sizeof(struct dups));
379 pfatal("DUP TABLE OVERFLOW.");
380 if (reply("CONTINUE") == 0) {
388 duplist = muldup = new;
391 new->next = muldup->next;
394 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
395 if (dlp->dup == blkno)
397 if (dlp == muldup && dlp->dup != blkno)
401 * count the number of blocks found in id_entryno