]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sbin/fsck_ffs/pass1.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sbin / fsck_ffs / 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  * 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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static const char sccsid[] = "@(#)pass1.c       8.6 (Berkeley) 4/28/95";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/sysctl.h>
41
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ufs/dir.h>
44 #include <ufs/ffs/fs.h>
45
46 #include <err.h>
47 #include <limits.h>
48 #include <stdint.h>
49 #include <string.h>
50
51 #include "fsck.h"
52
53 static ufs2_daddr_t badblk;
54 static ufs2_daddr_t dupblk;
55 static ino_t lastino;           /* last inode in use */
56
57 static int checkinode(ino_t inumber, struct inodesc *, int rebuildcg);
58
59 void
60 pass1(void)
61 {
62         struct inostat *info;
63         struct inodesc idesc;
64         ino_t inumber, inosused, mininos;
65         ufs2_daddr_t i, cgd;
66         u_int8_t *cp;
67         int c, rebuildcg;
68
69         /*
70          * Set file system reserved blocks in used block map.
71          */
72         for (c = 0; c < sblock.fs_ncg; c++) {
73                 cgd = cgdmin(&sblock, c);
74                 if (c == 0) {
75                         i = cgbase(&sblock, c);
76                 } else
77                         i = cgsblock(&sblock, c);
78                 for (; i < cgd; i++)
79                         setbmap(i);
80         }
81         i = sblock.fs_csaddr;
82         cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
83         for (; i < cgd; i++)
84                 setbmap(i);
85
86         /*
87          * Find all allocated blocks.
88          */
89         memset(&idesc, 0, sizeof(struct inodesc));
90         idesc.id_func = pass1check;
91         n_files = n_blks = 0;
92         for (c = 0; c < sblock.fs_ncg; c++) {
93                 inumber = c * sblock.fs_ipg;
94                 setinodebuf(inumber);
95                 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
96                 rebuildcg = 0;
97                 if (!check_cgmagic(c, &cgrp))
98                         rebuildcg = 1;
99                 if (!rebuildcg && sblock.fs_magic == FS_UFS2_MAGIC) {
100                         inosused = cgrp.cg_initediblk;
101                         if (inosused > sblock.fs_ipg)
102                                 inosused = sblock.fs_ipg;
103                 } else
104                         inosused = sblock.fs_ipg;
105                 if (got_siginfo) {
106                         printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
107                             cdevname, c, sblock.fs_ncg,
108                             c * 100 / sblock.fs_ncg);
109                         got_siginfo = 0;
110                 }
111                 if (got_sigalarm) {
112                         setproctitle("%s p1 %d%%", cdevname,
113                              c * 100 / sblock.fs_ncg);
114                         got_sigalarm = 0;
115                 }
116                 /*
117                  * If we are using soft updates, then we can trust the
118                  * cylinder group inode allocation maps to tell us which
119                  * inodes are allocated. We will scan the used inode map
120                  * to find the inodes that are really in use, and then
121                  * read only those inodes in from disk.
122                  */
123                 if ((preen || inoopt) && usedsoftdep && !rebuildcg) {
124                         cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT];
125                         for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
126                                 if (*cp == 0)
127                                         continue;
128                                 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
129                                         if (*cp & i)
130                                                 break;
131                                         inosused--;
132                                 }
133                                 break;
134                         }
135                         if (inosused < 0)
136                                 inosused = 0;
137                 }
138                 /*
139                  * Allocate inoinfo structures for the allocated inodes.
140                  */
141                 inostathead[c].il_numalloced = inosused;
142                 if (inosused == 0) {
143                         inostathead[c].il_stat = 0;
144                         continue;
145                 }
146                 info = calloc((unsigned)inosused, sizeof(struct inostat));
147                 if (info == NULL)
148                         errx(EEXIT, "cannot alloc %u bytes for inoinfo",
149                             (unsigned)(sizeof(struct inostat) * inosused));
150                 inostathead[c].il_stat = info;
151                 /*
152                  * Scan the allocated inodes.
153                  */
154                 for (i = 0; i < inosused; i++, inumber++) {
155                         if (inumber < ROOTINO) {
156                                 (void)getnextinode(inumber, rebuildcg);
157                                 continue;
158                         }
159                         /*
160                          * NULL return indicates probable end of allocated
161                          * inodes during cylinder group rebuild attempt.
162                          * We always keep trying until we get to the minimum
163                          * valid number for this cylinder group.
164                          */
165                         if (checkinode(inumber, &idesc, rebuildcg) == 0 &&
166                             i > cgrp.cg_initediblk)
167                                 break;
168                 }
169                 /*
170                  * This optimization speeds up future runs of fsck
171                  * by trimming down the number of inodes in cylinder
172                  * groups that formerly had many inodes but now have
173                  * fewer in use.
174                  */
175                 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
176                 if (inoopt && !preen && !rebuildcg &&
177                     sblock.fs_magic == FS_UFS2_MAGIC &&
178                     cgrp.cg_initediblk > 2 * INOPB(&sblock) &&
179                     mininos < cgrp.cg_initediblk) {
180                         i = cgrp.cg_initediblk;
181                         if (mininos < 2 * INOPB(&sblock))
182                                 cgrp.cg_initediblk = 2 * INOPB(&sblock);
183                         else
184                                 cgrp.cg_initediblk = mininos;
185                         pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
186                             c, i, cgrp.cg_initediblk, "VALID INODES");
187                         cgdirty();
188                 }
189                 if (inosused < sblock.fs_ipg)
190                         continue;
191                 lastino += 1;
192                 if (lastino < (c * sblock.fs_ipg))
193                         inosused = 0;
194                 else
195                         inosused = lastino - (c * sblock.fs_ipg);
196                 if (rebuildcg && inosused > cgrp.cg_initediblk &&
197                     sblock.fs_magic == FS_UFS2_MAGIC) {
198                         cgrp.cg_initediblk = roundup(inosused, INOPB(&sblock));
199                         pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
200                             cgrp.cg_initediblk);
201                 }
202                 /*
203                  * If we were not able to determine in advance which inodes
204                  * were in use, then reduce the size of the inoinfo structure
205                  * to the size necessary to describe the inodes that we
206                  * really found.
207                  */
208                 if (inumber == lastino)
209                         continue;
210                 inostathead[c].il_numalloced = inosused;
211                 if (inosused == 0) {
212                         free(inostathead[c].il_stat);
213                         inostathead[c].il_stat = 0;
214                         continue;
215                 }
216                 info = calloc((unsigned)inosused, sizeof(struct inostat));
217                 if (info == NULL)
218                         errx(EEXIT, "cannot alloc %u bytes for inoinfo",
219                             (unsigned)(sizeof(struct inostat) * inosused));
220                 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
221                 free(inostathead[c].il_stat);
222                 inostathead[c].il_stat = info;
223         }
224         freeinodebuf();
225 }
226
227 static int
228 checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg)
229 {
230         union dinode *dp;
231         off_t kernmaxfilesize;
232         ufs2_daddr_t ndb;
233         mode_t mode;
234         int j, ret, offset;
235
236         if ((dp = getnextinode(inumber, rebuildcg)) == NULL)
237                 return (0);
238         mode = DIP(dp, di_mode) & IFMT;
239         if (mode == 0) {
240                 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
241                      (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
242                         NDADDR * sizeof(ufs1_daddr_t)) ||
243                       memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
244                         NIADDR * sizeof(ufs1_daddr_t)) ||
245                       dp->dp1.di_mode || dp->dp1.di_size)) ||
246                     (sblock.fs_magic == FS_UFS2_MAGIC &&
247                      (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
248                         NDADDR * sizeof(ufs2_daddr_t)) ||
249                       memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
250                         NIADDR * sizeof(ufs2_daddr_t)) ||
251                       dp->dp2.di_mode || dp->dp2.di_size))) {
252                         pfatal("PARTIALLY ALLOCATED INODE I=%lu",
253                             (u_long)inumber);
254                         if (reply("CLEAR") == 1) {
255                                 dp = ginode(inumber);
256                                 clearinode(dp);
257                                 inodirty();
258                         }
259                 }
260                 inoinfo(inumber)->ino_state = USTATE;
261                 return (1);
262         }
263         lastino = inumber;
264         /* This should match the file size limit in ffs_mountfs(). */
265         if (sblock.fs_magic == FS_UFS1_MAGIC)
266                 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
267         else
268                 kernmaxfilesize = sblock.fs_maxfilesize;
269         if (DIP(dp, di_size) > kernmaxfilesize ||
270             DIP(dp, di_size) > sblock.fs_maxfilesize ||
271             (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
272                 if (debug)
273                         printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
274                 goto unknown;
275         }
276         if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
277                 dp = ginode(inumber);
278                 DIP_SET(dp, di_size, sblock.fs_fsize);
279                 DIP_SET(dp, di_mode, IFREG|0600);
280                 inodirty();
281         }
282         if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
283              mode == IFSOCK) && DIP(dp, di_size) != 0) {
284                 if (debug)
285                         printf("bad special-file size %ju:",
286                             (uintmax_t)DIP(dp, di_size));
287                 goto unknown;
288         }
289         if ((mode == IFBLK || mode == IFCHR) &&
290             (dev_t)DIP(dp, di_rdev) == NODEV) {
291                 if (debug)
292                         printf("bad special-file rdev NODEV:");
293                 goto unknown;
294         }
295         ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
296         if (ndb < 0) {
297                 if (debug)
298                         printf("bad size %ju ndb %ju:",
299                                 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
300                 goto unknown;
301         }
302         if (mode == IFBLK || mode == IFCHR)
303                 ndb++;
304         if (mode == IFLNK) {
305                 /*
306                  * Fake ndb value so direct/indirect block checks below
307                  * will detect any garbage after symlink string.
308                  */
309                 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
310                         if (sblock.fs_magic == FS_UFS1_MAGIC)
311                                 ndb = howmany(DIP(dp, di_size),
312                                     sizeof(ufs1_daddr_t));
313                         else
314                                 ndb = howmany(DIP(dp, di_size),
315                                     sizeof(ufs2_daddr_t));
316                         if (ndb > NDADDR) {
317                                 j = ndb - NDADDR;
318                                 for (ndb = 1; j > 1; j--)
319                                         ndb *= NINDIR(&sblock);
320                                 ndb += NDADDR;
321                         }
322                 }
323         }
324         for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
325                 if (DIP(dp, di_db[j]) != 0) {
326                         if (debug)
327                                 printf("bad direct addr[%d]: %ju\n", j,
328                                     (uintmax_t)DIP(dp, di_db[j]));
329                         goto unknown;
330                 }
331         for (j = 0, ndb -= NDADDR; ndb > 0; j++)
332                 ndb /= NINDIR(&sblock);
333         for (; j < NIADDR; j++)
334                 if (DIP(dp, di_ib[j]) != 0) {
335                         if (debug)
336                                 printf("bad indirect addr: %ju\n",
337                                     (uintmax_t)DIP(dp, di_ib[j]));
338                         goto unknown;
339                 }
340         if (ftypeok(dp) == 0)
341                 goto unknown;
342         n_files++;
343         inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
344         if (mode == IFDIR) {
345                 if (DIP(dp, di_size) == 0)
346                         inoinfo(inumber)->ino_state = DCLEAR;
347                 else if (DIP(dp, di_nlink) <= 0)
348                         inoinfo(inumber)->ino_state = DZLINK;
349                 else
350                         inoinfo(inumber)->ino_state = DSTATE;
351                 cacheino(dp, inumber);
352                 countdirs++;
353         } else if (DIP(dp, di_nlink) <= 0)
354                 inoinfo(inumber)->ino_state = FZLINK;
355         else
356                 inoinfo(inumber)->ino_state = FSTATE;
357         inoinfo(inumber)->ino_type = IFTODT(mode);
358         badblk = dupblk = 0;
359         idesc->id_number = inumber;
360         if (DIP(dp, di_flags) & SF_SNAPSHOT)
361                 idesc->id_type = SNAP;
362         else
363                 idesc->id_type = ADDR;
364         (void)ckinode(dp, idesc);
365         if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
366                 idesc->id_type = ADDR;
367                 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
368                 for (j = 0; j < NXADDR; j++) {
369                         if (--ndb == 0 &&
370                             (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
371                                 idesc->id_numfrags = numfrags(&sblock,
372                                     fragroundup(&sblock, offset));
373                         else
374                                 idesc->id_numfrags = sblock.fs_frag;
375                         if (dp->dp2.di_extb[j] == 0)
376                                 continue;
377                         idesc->id_blkno = dp->dp2.di_extb[j];
378                         ret = (*idesc->id_func)(idesc);
379                         if (ret & STOP)
380                                 break;
381                 }
382         }
383         if (sblock.fs_magic == FS_UFS2_MAGIC)
384                 eascan(idesc, &dp->dp2);
385         idesc->id_entryno *= btodb(sblock.fs_fsize);
386         if (DIP(dp, di_blocks) != idesc->id_entryno) {
387                 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
388                     (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
389                     (uintmax_t)idesc->id_entryno);
390                 if (preen)
391                         printf(" (CORRECTED)\n");
392                 else if (reply("CORRECT") == 0)
393                         return (1);
394                 if (bkgrdflag == 0) {
395                         dp = ginode(inumber);
396                         DIP_SET(dp, di_blocks, idesc->id_entryno);
397                         inodirty();
398                 } else {
399                         cmd.value = idesc->id_number;
400                         cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
401                         if (debug)
402                                 printf("adjblkcnt ino %ju amount %lld\n",
403                                     (uintmax_t)cmd.value, (long long)cmd.size);
404                         if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
405                             &cmd, sizeof cmd) == -1)
406                                 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
407                 }
408         }
409         return (1);
410 unknown:
411         pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
412         inoinfo(inumber)->ino_state = FCLEAR;
413         if (reply("CLEAR") == 1) {
414                 inoinfo(inumber)->ino_state = USTATE;
415                 dp = ginode(inumber);
416                 clearinode(dp);
417                 inodirty();
418         }
419         return (1);
420 }
421
422 int
423 pass1check(struct inodesc *idesc)
424 {
425         int res = KEEPON;
426         int anyout, nfrags;
427         ufs2_daddr_t blkno = idesc->id_blkno;
428         struct dups *dlp;
429         struct dups *new;
430
431         if (idesc->id_type == SNAP) {
432                 if (blkno == BLK_NOCOPY)
433                         return (KEEPON);
434                 if (idesc->id_number == cursnapshot) {
435                         if (blkno == blkstofrags(&sblock, idesc->id_lbn))
436                                 return (KEEPON);
437                         if (blkno == BLK_SNAP) {
438                                 blkno = blkstofrags(&sblock, idesc->id_lbn);
439                                 idesc->id_entryno -= idesc->id_numfrags;
440                         }
441                 } else {
442                         if (blkno == BLK_SNAP)
443                                 return (KEEPON);
444                 }
445         }
446         if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
447                 blkerror(idesc->id_number, "BAD", blkno);
448                 if (badblk++ >= MAXBAD) {
449                         pwarn("EXCESSIVE BAD BLKS I=%lu",
450                             (u_long)idesc->id_number);
451                         if (preen)
452                                 printf(" (SKIPPING)\n");
453                         else if (reply("CONTINUE") == 0) {
454                                 ckfini(0);
455                                 exit(EEXIT);
456                         }
457                         return (STOP);
458                 }
459         }
460         for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
461                 if (anyout && chkrange(blkno, 1)) {
462                         res = SKIP;
463                 } else if (!testbmap(blkno)) {
464                         n_blks++;
465                         setbmap(blkno);
466                 } else {
467                         blkerror(idesc->id_number, "DUP", blkno);
468                         if (dupblk++ >= MAXDUP) {
469                                 pwarn("EXCESSIVE DUP BLKS I=%lu",
470                                         (u_long)idesc->id_number);
471                                 if (preen)
472                                         printf(" (SKIPPING)\n");
473                                 else if (reply("CONTINUE") == 0) {
474                                         ckfini(0);
475                                         exit(EEXIT);
476                                 }
477                                 return (STOP);
478                         }
479                         new = (struct dups *)malloc(sizeof(struct dups));
480                         if (new == NULL) {
481                                 pfatal("DUP TABLE OVERFLOW.");
482                                 if (reply("CONTINUE") == 0) {
483                                         ckfini(0);
484                                         exit(EEXIT);
485                                 }
486                                 return (STOP);
487                         }
488                         new->dup = blkno;
489                         if (muldup == 0) {
490                                 duplist = muldup = new;
491                                 new->next = 0;
492                         } else {
493                                 new->next = muldup->next;
494                                 muldup->next = new;
495                         }
496                         for (dlp = duplist; dlp != muldup; dlp = dlp->next)
497                                 if (dlp->dup == blkno)
498                                         break;
499                         if (dlp == muldup && dlp->dup != blkno)
500                                 muldup = new;
501                 }
502                 /*
503                  * count the number of blocks found in id_entryno
504                  */
505                 idesc->id_entryno++;
506         }
507         return (res);
508 }