Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sbin / fsck / pass1.c
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static const char sccsid[] = "@(#)pass1.c       8.6 (Berkeley) 4/28/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/sbin/fsck/pass1.c,v 1.16.2.5 2002/06/23 22:34:58 iedowse Exp $";
40 #endif /* not lint */
41
42 #include <sys/param.h>
43
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
47
48 #include <err.h>
49 #include <string.h>
50
51 #include "fsck.h"
52
53 static ufs_daddr_t badblk;
54 static ufs_daddr_t dupblk;
55 static ino_t lastino;           /* last inode in use */
56
57 static void checkinode __P((ino_t inumber, struct inodesc *));
58
59 void
60 pass1()
61 {
62         u_int8_t *cp;
63         ino_t inumber;
64         int c, i, cgd, inosused;
65         struct inostat *info;
66         struct inodesc idesc;
67
68         /*
69          * Set file system reserved blocks in used block map.
70          */
71         for (c = 0; c < sblock.fs_ncg; c++) {
72                 cgd = cgdmin(&sblock, c);
73                 if (c == 0) {
74                         i = cgbase(&sblock, c);
75                 } else
76                         i = cgsblock(&sblock, c);
77                 for (; i < cgd; i++)
78                         setbmap(i);
79         }
80         i = sblock.fs_csaddr;
81         cgd = i+ howmany(sblock.fs_cssize, sblock.fs_fsize);
82         for (; i < cgd; i++)
83                 setbmap(i);
84         /*
85          * Find all allocated blocks.
86          */
87         memset(&idesc, 0, sizeof(struct inodesc));
88         idesc.id_type = ADDR;
89         idesc.id_func = pass1check;
90         n_files = n_blks = 0;
91         for (c = 0; c < sblock.fs_ncg; c++) {
92                 inumber = c * sblock.fs_ipg;
93                 setinodebuf(inumber);
94                 inosused = sblock.fs_ipg;
95                 if (got_siginfo) {
96                         printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
97                             cdevname, c, sblock.fs_ncg,
98                             c * 100 / sblock.fs_ncg);
99                         got_siginfo = 0;
100                 }
101                 /*
102                  * If we are using soft updates, then we can trust the
103                  * cylinder group inode allocation maps to tell us which
104                  * inodes are allocated. We will scan the used inode map
105                  * to find the inodes that are really in use, and then
106                  * read only those inodes in from disk.
107                  */
108                 if (preen && usedsoftdep) {
109                         getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
110                         if (!cg_chkmagic(&cgrp))
111                                 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
112                         cp = &cg_inosused(&cgrp)[(sblock.fs_ipg - 1) / NBBY];
113                         for ( ; inosused > 0; inosused -= NBBY, cp--) {
114                                 if (*cp == 0)
115                                         continue;
116                                 for (i = 1 << (NBBY - 1); i > 0; i >>= 1) {
117                                         if (*cp & i)
118                                                 break;
119                                         inosused--;
120                                 }
121                                 break;
122                         }
123                         if (inosused < 0)
124                                 inosused = 0;
125                 }
126                 /*
127                  * Allocate inoinfo structures for the allocated inodes.
128                  */
129                 inostathead[c].il_numalloced = inosused;
130                 if (inosused == 0) {
131                         inostathead[c].il_stat = 0;
132                         continue;
133                 }
134                 info = calloc((unsigned)inosused, sizeof(struct inostat));
135                 if (info == NULL)
136                         pfatal("cannot alloc %u bytes for inoinfo\n",
137                             (unsigned)(sizeof(struct inostat) * inosused));
138                 inostathead[c].il_stat = info;
139                 /*
140                  * Scan the allocated inodes.
141                  */
142                 for (i = 0; i < inosused; i++, inumber++) {
143                         if (inumber < ROOTINO) {
144                                 (void)getnextinode(inumber);
145                                 continue;
146                         }
147                         checkinode(inumber, &idesc);
148                 }
149                 lastino += 1;
150                 if (inosused < sblock.fs_ipg || inumber == lastino)
151                         continue;
152                 /*
153                  * If we were not able to determine in advance which inodes
154                  * were in use, then reduce the size of the inoinfo structure
155                  * to the size necessary to describe the inodes that we
156                  * really found.
157                  */
158                 inosused = lastino - (c * sblock.fs_ipg);
159                 if (inosused < 0)
160                         inosused = 0;
161                 inostathead[c].il_numalloced = inosused;
162                 if (inosused == 0) {
163                         free(inostathead[c].il_stat);
164                         inostathead[c].il_stat = 0;
165                         continue;
166                 }
167                 info = calloc((unsigned)inosused, sizeof(struct inostat));
168                 if (info == NULL)
169                         pfatal("cannot alloc %u bytes for inoinfo\n",
170                             (unsigned)(sizeof(struct inostat) * inosused));
171                 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
172                 free(inostathead[c].il_stat);
173                 inostathead[c].il_stat = info;
174         }
175         freeinodebuf();
176 }
177
178 static void
179 checkinode(inumber, idesc)
180         ino_t inumber;
181         register struct inodesc *idesc;
182 {
183         register struct dinode *dp;
184         struct zlncnt *zlnp;
185         u_int64_t kernmaxfilesize;
186         ufs_daddr_t ndb, j;
187         mode_t mode;
188         char *symbuf;
189
190         dp = getnextinode(inumber);
191         mode = dp->di_mode & IFMT;
192         if (mode == 0) {
193                 if (memcmp(dp->di_db, zino.di_db,
194                         NDADDR * sizeof(ufs_daddr_t)) ||
195                     memcmp(dp->di_ib, zino.di_ib,
196                         NIADDR * sizeof(ufs_daddr_t)) ||
197                     dp->di_mode || dp->di_size) {
198                         pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
199                         if (reply("CLEAR") == 1) {
200                                 dp = ginode(inumber);
201                                 clearinode(dp);
202                                 inodirty();
203                         }
204                 }
205                 inoinfo(inumber)->ino_state = USTATE;
206                 return;
207         }
208         lastino = inumber;
209         /* This should match the file size limit in ffs_mountfs(). */
210         kernmaxfilesize = (u_int64_t)0x40000000 * sblock.fs_bsize - 1;
211         if (kernmaxfilesize > (u_int64_t)0x80000000u * PAGE_SIZE - 1)
212                 kernmaxfilesize = (u_int64_t)0x80000000u * PAGE_SIZE - 1;
213         if (dp->di_size > kernmaxfilesize ||
214             dp->di_size > sblock.fs_maxfilesize ||
215             (mode == IFDIR && dp->di_size > MAXDIRSIZE)) {
216                 if (debug)
217                         printf("bad size %qu:", dp->di_size);
218                 goto unknown;
219         }
220         if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
221                 dp = ginode(inumber);
222                 dp->di_size = sblock.fs_fsize;
223                 dp->di_mode = IFREG|0600;
224                 inodirty();
225         }
226         if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
227              mode == IFSOCK) && dp->di_size != 0) {
228                 if (debug)
229                         printf("bad special-file size %qu:", dp->di_size);
230                 goto unknown;
231         }
232         ndb = howmany(dp->di_size, sblock.fs_bsize);
233         if (ndb < 0) {
234                 if (debug)
235                         printf("bad size %qu ndb %d:",
236                                 dp->di_size, ndb);
237                 goto unknown;
238         }
239         if (mode == IFBLK || mode == IFCHR)
240                 ndb++;
241         if (mode == IFLNK) {
242                 if (doinglevel2 &&
243                     dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
244                     dp->di_blocks != 0) {
245                         symbuf = alloca(secsize);
246                         if (bread(fsreadfd, symbuf,
247                             fsbtodb(&sblock, dp->di_db[0]),
248                             (long)secsize) != 0)
249                                 errx(EEXIT, "cannot read symlink");
250                         if (debug) {
251                                 symbuf[dp->di_size] = 0;
252                                 printf("convert symlink %lu(%s) of size %ld\n",
253                                     (u_long)inumber, symbuf, (long)dp->di_size);
254                         }
255                         dp = ginode(inumber);
256                         memmove(dp->di_shortlink, symbuf, (long)dp->di_size);
257                         dp->di_blocks = 0;
258                         inodirty();
259                 }
260                 /*
261                  * Fake ndb value so direct/indirect block checks below
262                  * will detect any garbage after symlink string.
263                  */
264                 if (dp->di_size < sblock.fs_maxsymlinklen) {
265                         ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
266                         if (ndb > NDADDR) {
267                                 j = ndb - NDADDR;
268                                 for (ndb = 1; j > 1; j--)
269                                         ndb *= NINDIR(&sblock);
270                                 ndb += NDADDR;
271                         }
272                 }
273         }
274         for (j = ndb; j < NDADDR; j++)
275                 if (dp->di_db[j] != 0) {
276                         if (debug)
277                                 printf("bad direct addr: %ld\n",
278                                     (long)dp->di_db[j]);
279                         goto unknown;
280                 }
281         for (j = 0, ndb -= NDADDR; ndb > 0; j++)
282                 ndb /= NINDIR(&sblock);
283         for (; j < NIADDR; j++)
284                 if (dp->di_ib[j] != 0) {
285                         if (debug)
286                                 printf("bad indirect addr: %ld\n",
287                                     (long)dp->di_ib[j]);
288                         goto unknown;
289                 }
290         if (ftypeok(dp) == 0)
291                 goto unknown;
292         n_files++;
293         inoinfo(inumber)->ino_linkcnt = dp->di_nlink;
294         if (dp->di_nlink <= 0) {
295                 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
296                 if (zlnp == NULL) {
297                         pfatal("LINK COUNT TABLE OVERFLOW");
298                         if (reply("CONTINUE") == 0) {
299                                 ckfini(0);
300                                 exit(EEXIT);
301                         }
302                 } else {
303                         zlnp->zlncnt = inumber;
304                         zlnp->next = zlnhead;
305                         zlnhead = zlnp;
306                 }
307         }
308         if (mode == IFDIR) {
309                 if (dp->di_size == 0)
310                         inoinfo(inumber)->ino_state = DCLEAR;
311                 else
312                         inoinfo(inumber)->ino_state = DSTATE;
313                 cacheino(dp, inumber);
314                 countdirs++;
315         } else
316                 inoinfo(inumber)->ino_state = FSTATE;
317         inoinfo(inumber)->ino_type = IFTODT(mode);
318         if (doinglevel2 &&
319             (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
320                 dp = ginode(inumber);
321                 dp->di_uid = dp->di_ouid;
322                 dp->di_ouid = -1;
323                 dp->di_gid = dp->di_ogid;
324                 dp->di_ogid = -1;
325                 inodirty();
326         }
327         badblk = dupblk = 0;
328         idesc->id_number = inumber;
329         (void)ckinode(dp, idesc);
330         idesc->id_entryno *= btodb(sblock.fs_fsize);
331         if (dp->di_blocks != idesc->id_entryno) {
332                 pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
333                     inumber, dp->di_blocks, idesc->id_entryno);
334                 if (preen)
335                         printf(" (CORRECTED)\n");
336                 else if (reply("CORRECT") == 0)
337                         return;
338                 dp = ginode(inumber);
339                 dp->di_blocks = idesc->id_entryno;
340                 inodirty();
341         }
342         return;
343 unknown:
344         pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
345         inoinfo(inumber)->ino_state = FCLEAR;
346         if (reply("CLEAR") == 1) {
347                 inoinfo(inumber)->ino_state = USTATE;
348                 dp = ginode(inumber);
349                 clearinode(dp);
350                 inodirty();
351         }
352 }
353
354 int
355 pass1check(idesc)
356         register struct inodesc *idesc;
357 {
358         int res = KEEPON;
359         int anyout, nfrags;
360         ufs_daddr_t blkno = idesc->id_blkno;
361         register struct dups *dlp;
362         struct dups *new;
363
364         if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
365                 blkerror(idesc->id_number, "BAD", blkno);
366                 if (badblk++ >= MAXBAD) {
367                         pwarn("EXCESSIVE BAD BLKS I=%lu",
368                                 idesc->id_number);
369                         if (preen)
370                                 printf(" (SKIPPING)\n");
371                         else if (reply("CONTINUE") == 0) {
372                                 ckfini(0);
373                                 exit(EEXIT);
374                         }
375                         return (STOP);
376                 }
377         }
378         for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
379                 if (anyout && chkrange(blkno, 1)) {
380                         res = SKIP;
381                 } else if (!testbmap(blkno)) {
382                         n_blks++;
383                         setbmap(blkno);
384                 } else {
385                         blkerror(idesc->id_number, "DUP", blkno);
386                         if (dupblk++ >= MAXDUP) {
387                                 pwarn("EXCESSIVE DUP BLKS I=%lu",
388                                         idesc->id_number);
389                                 if (preen)
390                                         printf(" (SKIPPING)\n");
391                                 else if (reply("CONTINUE") == 0) {
392                                         ckfini(0);
393                                         exit(EEXIT);
394                                 }
395                                 return (STOP);
396                         }
397                         new = (struct dups *)malloc(sizeof(struct dups));
398                         if (new == NULL) {
399                                 pfatal("DUP TABLE OVERFLOW.");
400                                 if (reply("CONTINUE") == 0) {
401                                         ckfini(0);
402                                         exit(EEXIT);
403                                 }
404                                 return (STOP);
405                         }
406                         new->dup = blkno;
407                         if (muldup == 0) {
408                                 duplist = muldup = new;
409                                 new->next = 0;
410                         } else {
411                                 new->next = muldup->next;
412                                 muldup->next = new;
413                         }
414                         for (dlp = duplist; dlp != muldup; dlp = dlp->next)
415                                 if (dlp->dup == blkno)
416                                         break;
417                         if (dlp == muldup && dlp->dup != blkno)
418                                 muldup = new;
419                 }
420                 /*
421                  * count the number of blocks found in id_entryno
422                  */
423                 idesc->id_entryno++;
424         }
425         return (res);
426 }