]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/pass1.c
MFC of 346185
[FreeBSD/FreeBSD.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         struct bufarea *cgbp;
65         struct cg *cgp;
66         ino_t inumber, inosused, mininos;
67         ufs2_daddr_t i, cgd;
68         u_int8_t *cp;
69         int c, rebuildcg;
70
71         badblk = dupblk = lastino = 0;
72
73         /*
74          * Set file system reserved blocks in used block map.
75          */
76         for (c = 0; c < sblock.fs_ncg; c++) {
77                 cgd = cgdmin(&sblock, c);
78                 if (c == 0) {
79                         i = cgbase(&sblock, c);
80                 } else
81                         i = cgsblock(&sblock, c);
82                 for (; i < cgd; i++)
83                         setbmap(i);
84         }
85         i = sblock.fs_csaddr;
86         cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
87         for (; i < cgd; i++)
88                 setbmap(i);
89
90         /*
91          * Find all allocated blocks.
92          */
93         memset(&idesc, 0, sizeof(struct inodesc));
94         idesc.id_func = pass1check;
95         n_files = n_blks = 0;
96         for (c = 0; c < sblock.fs_ncg; c++) {
97                 inumber = c * sblock.fs_ipg;
98                 setinodebuf(inumber);
99                 cgbp = cgget(c);
100                 cgp = cgbp->b_un.b_cg;
101                 rebuildcg = 0;
102                 if (!check_cgmagic(c, cgbp))
103                         rebuildcg = 1;
104                 if (!rebuildcg && sblock.fs_magic == FS_UFS2_MAGIC) {
105                         inosused = cgp->cg_initediblk;
106                         if (inosused > sblock.fs_ipg) {
107                                 pfatal(
108 "Too many initialized inodes (%ju > %d) in cylinder group %d\nReset to %d\n",
109                                     (uintmax_t)inosused,
110                                     sblock.fs_ipg, c, sblock.fs_ipg);
111                                 inosused = sblock.fs_ipg;
112                         }
113                 } else {
114                         inosused = sblock.fs_ipg;
115                 }
116                 if (got_siginfo) {
117                         printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
118                             cdevname, c, sblock.fs_ncg,
119                             c * 100 / sblock.fs_ncg);
120                         got_siginfo = 0;
121                 }
122                 if (got_sigalarm) {
123                         setproctitle("%s p1 %d%%", cdevname,
124                              c * 100 / sblock.fs_ncg);
125                         got_sigalarm = 0;
126                 }
127                 /*
128                  * If we are using soft updates, then we can trust the
129                  * cylinder group inode allocation maps to tell us which
130                  * inodes are allocated. We will scan the used inode map
131                  * to find the inodes that are really in use, and then
132                  * read only those inodes in from disk.
133                  */
134                 if ((preen || inoopt) && usedsoftdep && !rebuildcg) {
135                         cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
136                         for ( ; inosused != 0; cp--) {
137                                 if (*cp == 0) {
138                                         if (inosused > CHAR_BIT)
139                                                 inosused -= CHAR_BIT;
140                                         else
141                                                 inosused = 0;
142                                         continue;
143                                 }
144                                 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
145                                         if (*cp & i)
146                                                 break;
147                                         inosused--;
148                                 }
149                                 break;
150                         }
151                 }
152                 /*
153                  * Allocate inoinfo structures for the allocated inodes.
154                  */
155                 inostathead[c].il_numalloced = inosused;
156                 if (inosused == 0) {
157                         inostathead[c].il_stat = NULL;
158                         continue;
159                 }
160                 info = Calloc((unsigned)inosused, sizeof(struct inostat));
161                 if (info == NULL)
162                         errx(EEXIT, "cannot alloc %u bytes for inoinfo",
163                             (unsigned)(sizeof(struct inostat) * inosused));
164                 inostathead[c].il_stat = info;
165                 /*
166                  * Scan the allocated inodes.
167                  */
168                 for (i = 0; i < inosused; i++, inumber++) {
169                         if (inumber < ROOTINO) {
170                                 (void)getnextinode(inumber, rebuildcg);
171                                 continue;
172                         }
173                         /*
174                          * NULL return indicates probable end of allocated
175                          * inodes during cylinder group rebuild attempt.
176                          * We always keep trying until we get to the minimum
177                          * valid number for this cylinder group.
178                          */
179                         if (checkinode(inumber, &idesc, rebuildcg) == 0 &&
180                             i > cgp->cg_initediblk)
181                                 break;
182                 }
183                 /*
184                  * This optimization speeds up future runs of fsck
185                  * by trimming down the number of inodes in cylinder
186                  * groups that formerly had many inodes but now have
187                  * fewer in use.
188                  */
189                 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock));
190                 if (inoopt && !preen && !rebuildcg &&
191                     sblock.fs_magic == FS_UFS2_MAGIC &&
192                     cgp->cg_initediblk > 2 * INOPB(&sblock) &&
193                     mininos < cgp->cg_initediblk) {
194                         i = cgp->cg_initediblk;
195                         if (mininos < 2 * INOPB(&sblock))
196                                 cgp->cg_initediblk = 2 * INOPB(&sblock);
197                         else
198                                 cgp->cg_initediblk = mininos;
199                         pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n",
200                             c, i, cgp->cg_initediblk, "VALID INODES");
201                         dirty(cgbp);
202                 }
203                 if (inosused < sblock.fs_ipg)
204                         continue;
205                 lastino += 1;
206                 if (lastino < (c * sblock.fs_ipg))
207                         inosused = 0;
208                 else
209                         inosused = lastino - (c * sblock.fs_ipg);
210                 if (rebuildcg && inosused > cgp->cg_initediblk &&
211                     sblock.fs_magic == FS_UFS2_MAGIC) {
212                         cgp->cg_initediblk = roundup(inosused, INOPB(&sblock));
213                         pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c,
214                             cgp->cg_initediblk);
215                 }
216                 /*
217                  * If we were not able to determine in advance which inodes
218                  * were in use, then reduce the size of the inoinfo structure
219                  * to the size necessary to describe the inodes that we
220                  * really found.
221                  */
222                 if (inumber == lastino)
223                         continue;
224                 inostathead[c].il_numalloced = inosused;
225                 if (inosused == 0) {
226                         free(inostathead[c].il_stat);
227                         inostathead[c].il_stat = NULL;
228                         continue;
229                 }
230                 info = Calloc((unsigned)inosused, sizeof(struct inostat));
231                 if (info == NULL)
232                         errx(EEXIT, "cannot alloc %u bytes for inoinfo",
233                             (unsigned)(sizeof(struct inostat) * inosused));
234                 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
235                 free(inostathead[c].il_stat);
236                 inostathead[c].il_stat = info;
237         }
238         freeinodebuf();
239 }
240
241 static int
242 checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg)
243 {
244         union dinode *dp;
245         off_t kernmaxfilesize;
246         ufs2_daddr_t ndb;
247         mode_t mode;
248         uintmax_t fixsize;
249         int j, ret, offset;
250
251         if ((dp = getnextinode(inumber, rebuildcg)) == NULL)
252                 return (0);
253         mode = DIP(dp, di_mode) & IFMT;
254         if (mode == 0) {
255                 if ((sblock.fs_magic == FS_UFS1_MAGIC &&
256                      (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
257                         NDADDR * sizeof(ufs1_daddr_t)) ||
258                       memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
259                         NIADDR * sizeof(ufs1_daddr_t)) ||
260                       dp->dp1.di_mode || dp->dp1.di_size)) ||
261                     (sblock.fs_magic == FS_UFS2_MAGIC &&
262                      (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
263                         NDADDR * sizeof(ufs2_daddr_t)) ||
264                       memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
265                         NIADDR * sizeof(ufs2_daddr_t)) ||
266                       dp->dp2.di_mode || dp->dp2.di_size))) {
267                         pfatal("PARTIALLY ALLOCATED INODE I=%lu",
268                             (u_long)inumber);
269                         if (reply("CLEAR") == 1) {
270                                 dp = ginode(inumber);
271                                 clearinode(dp);
272                                 inodirty(dp);
273                         }
274                 }
275                 inoinfo(inumber)->ino_state = USTATE;
276                 return (1);
277         }
278         lastino = inumber;
279         /* This should match the file size limit in ffs_mountfs(). */
280         if (sblock.fs_magic == FS_UFS1_MAGIC)
281                 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
282         else
283                 kernmaxfilesize = sblock.fs_maxfilesize;
284         if (DIP(dp, di_size) > kernmaxfilesize ||
285             DIP(dp, di_size) > sblock.fs_maxfilesize ||
286             (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) {
287                 if (debug)
288                         printf("bad size %ju:", (uintmax_t)DIP(dp, di_size));
289                 goto unknown;
290         }
291         if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
292                 dp = ginode(inumber);
293                 DIP_SET(dp, di_size, sblock.fs_fsize);
294                 DIP_SET(dp, di_mode, IFREG|0600);
295                 inodirty(dp);
296         }
297         if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
298              mode == IFSOCK) && DIP(dp, di_size) != 0) {
299                 if (debug)
300                         printf("bad special-file size %ju:",
301                             (uintmax_t)DIP(dp, di_size));
302                 goto unknown;
303         }
304         if ((mode == IFBLK || mode == IFCHR) &&
305             (dev_t)DIP(dp, di_rdev) == NODEV) {
306                 if (debug)
307                         printf("bad special-file rdev NODEV:");
308                 goto unknown;
309         }
310         ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
311         if (ndb < 0) {
312                 if (debug)
313                         printf("bad size %ju ndb %ju:",
314                                 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb);
315                 goto unknown;
316         }
317         if (mode == IFBLK || mode == IFCHR)
318                 ndb++;
319         if (mode == IFLNK) {
320                 /*
321                  * Fake ndb value so direct/indirect block checks below
322                  * will detect any garbage after symlink string.
323                  */
324                 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
325                         if (sblock.fs_magic == FS_UFS1_MAGIC)
326                                 ndb = howmany(DIP(dp, di_size),
327                                     sizeof(ufs1_daddr_t));
328                         else
329                                 ndb = howmany(DIP(dp, di_size),
330                                     sizeof(ufs2_daddr_t));
331                         if (ndb > NDADDR) {
332                                 j = ndb - NDADDR;
333                                 for (ndb = 1; j > 1; j--)
334                                         ndb *= NINDIR(&sblock);
335                                 ndb += NDADDR;
336                         }
337                 }
338         }
339         for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
340                 if (DIP(dp, di_db[j]) != 0) {
341                         if (debug)
342                                 printf("bad direct addr[%d]: %ju\n", j,
343                                     (uintmax_t)DIP(dp, di_db[j]));
344                         goto unknown;
345                 }
346         for (j = 0, ndb -= NDADDR; ndb > 0; j++)
347                 ndb /= NINDIR(&sblock);
348         for (; j < NIADDR; j++)
349                 if (DIP(dp, di_ib[j]) != 0) {
350                         if (debug)
351                                 printf("bad indirect addr: %ju\n",
352                                     (uintmax_t)DIP(dp, di_ib[j]));
353                         goto unknown;
354                 }
355         if (ftypeok(dp) == 0)
356                 goto unknown;
357         n_files++;
358         inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink);
359         if (mode == IFDIR) {
360                 if (DIP(dp, di_size) == 0)
361                         inoinfo(inumber)->ino_state = DCLEAR;
362                 else if (DIP(dp, di_nlink) <= 0)
363                         inoinfo(inumber)->ino_state = DZLINK;
364                 else
365                         inoinfo(inumber)->ino_state = DSTATE;
366                 cacheino(dp, inumber);
367                 countdirs++;
368         } else if (DIP(dp, di_nlink) <= 0)
369                 inoinfo(inumber)->ino_state = FZLINK;
370         else
371                 inoinfo(inumber)->ino_state = FSTATE;
372         inoinfo(inumber)->ino_type = IFTODT(mode);
373         badblk = dupblk = 0;
374         idesc->id_number = inumber;
375         if (DIP(dp, di_flags) & SF_SNAPSHOT)
376                 idesc->id_type = SNAP;
377         else
378                 idesc->id_type = ADDR;
379         (void)ckinode(dp, idesc);
380         if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
381                 idesc->id_type = ADDR;
382                 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
383                 for (j = 0; j < NXADDR; j++) {
384                         if (--ndb == 0 &&
385                             (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
386                                 idesc->id_numfrags = numfrags(&sblock,
387                                     fragroundup(&sblock, offset));
388                         else
389                                 idesc->id_numfrags = sblock.fs_frag;
390                         if (dp->dp2.di_extb[j] == 0)
391                                 continue;
392                         idesc->id_blkno = dp->dp2.di_extb[j];
393                         ret = (*idesc->id_func)(idesc);
394                         if (ret & STOP)
395                                 break;
396                 }
397         }
398         if (sblock.fs_magic == FS_UFS2_MAGIC)
399                 eascan(idesc, &dp->dp2);
400         idesc->id_entryno *= btodb(sblock.fs_fsize);
401         if (DIP(dp, di_blocks) != idesc->id_entryno) {
402                 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
403                     (u_long)inumber, (uintmax_t)DIP(dp, di_blocks),
404                     (uintmax_t)idesc->id_entryno);
405                 if (preen)
406                         printf(" (CORRECTED)\n");
407                 else if (reply("CORRECT") == 0)
408                         return (1);
409                 if (bkgrdflag == 0) {
410                         dp = ginode(inumber);
411                         DIP_SET(dp, di_blocks, idesc->id_entryno);
412                         inodirty(dp);
413                 } else {
414                         cmd.value = idesc->id_number;
415                         cmd.size = idesc->id_entryno - DIP(dp, di_blocks);
416                         if (debug)
417                                 printf("adjblkcnt ino %ju amount %lld\n",
418                                     (uintmax_t)cmd.value, (long long)cmd.size);
419                         if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
420                             &cmd, sizeof cmd) == -1)
421                                 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
422                 }
423         }
424         /*
425          * Soft updates will always ensure that the file size is correct
426          * for files that contain only direct block pointers. However
427          * soft updates does not roll back sizes for files with indirect
428          * blocks that it has set to unallocated because their contents
429          * have not yet been written to disk. Hence, the file can appear
430          * to have a hole at its end because the block pointer has been
431          * rolled back to zero. Thus, id_lballoc tracks the last allocated
432          * block in the file. Here, for files that extend into indirect
433          * blocks, we check for a size past the last allocated block of
434          * the file and if that is found, shorten the file to reference
435          * the last allocated block to avoid having it reference a hole
436          * at its end.
437          */
438         if (DIP(dp, di_size) > NDADDR * sblock.fs_bsize &&
439             idesc->id_lballoc < lblkno(&sblock, DIP(dp, di_size) - 1)) {
440                 fixsize = lblktosize(&sblock, idesc->id_lballoc + 1);
441                 pwarn("INODE %lu: FILE SIZE %ju BEYOND END OF ALLOCATED FILE, "
442                       "SIZE SHOULD BE %ju", (u_long)inumber,
443                       (uintmax_t)DIP(dp, di_size), fixsize);
444                 if (preen)
445                         printf(" (ADJUSTED)\n");
446                 else if (reply("ADJUST") == 0)
447                         return (1);
448                 if (bkgrdflag == 0) {
449                         dp = ginode(inumber);
450                         DIP_SET(dp, di_size, fixsize);
451                         inodirty(dp);
452                 } else {
453                         cmd.value = idesc->id_number;
454                         cmd.size = fixsize;
455                         if (debug)
456                                 printf("setsize ino %ju size set to %ju\n",
457                                     (uintmax_t)cmd.value, (uintmax_t)cmd.size);
458                         if (sysctl(setsize, MIBSIZE, 0, 0,
459                             &cmd, sizeof cmd) == -1)
460                                 rwerror("SET INODE SIZE", cmd.value);
461                 }
462
463         }
464         return (1);
465 unknown:
466         pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber);
467         inoinfo(inumber)->ino_state = FCLEAR;
468         if (reply("CLEAR") == 1) {
469                 inoinfo(inumber)->ino_state = USTATE;
470                 dp = ginode(inumber);
471                 clearinode(dp);
472                 inodirty(dp);
473         }
474         return (1);
475 }
476
477 int
478 pass1check(struct inodesc *idesc)
479 {
480         int res = KEEPON;
481         int anyout, nfrags;
482         ufs2_daddr_t blkno = idesc->id_blkno;
483         struct dups *dlp;
484         struct dups *new;
485
486         if (idesc->id_type == SNAP) {
487                 if (blkno == BLK_NOCOPY)
488                         return (KEEPON);
489                 if (idesc->id_number == cursnapshot) {
490                         if (blkno == blkstofrags(&sblock, idesc->id_lbn))
491                                 return (KEEPON);
492                         if (blkno == BLK_SNAP) {
493                                 blkno = blkstofrags(&sblock, idesc->id_lbn);
494                                 idesc->id_entryno -= idesc->id_numfrags;
495                         }
496                 } else {
497                         if (blkno == BLK_SNAP)
498                                 return (KEEPON);
499                 }
500         }
501         if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
502                 blkerror(idesc->id_number, "BAD", blkno);
503                 if (badblk++ >= MAXBAD) {
504                         pwarn("EXCESSIVE BAD BLKS I=%lu",
505                             (u_long)idesc->id_number);
506                         if (preen)
507                                 printf(" (SKIPPING)\n");
508                         else if (reply("CONTINUE") == 0) {
509                                 ckfini(0);
510                                 exit(EEXIT);
511                         }
512                         rerun = 1;
513                         return (STOP);
514                 }
515         }
516         for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
517                 if (anyout && chkrange(blkno, 1)) {
518                         res = SKIP;
519                 } else if (!testbmap(blkno)) {
520                         n_blks++;
521                         setbmap(blkno);
522                 } else {
523                         blkerror(idesc->id_number, "DUP", blkno);
524                         if (dupblk++ >= MAXDUP) {
525                                 pwarn("EXCESSIVE DUP BLKS I=%lu",
526                                         (u_long)idesc->id_number);
527                                 if (preen)
528                                         printf(" (SKIPPING)\n");
529                                 else if (reply("CONTINUE") == 0) {
530                                         ckfini(0);
531                                         exit(EEXIT);
532                                 }
533                                 rerun = 1;
534                                 return (STOP);
535                         }
536                         new = (struct dups *)Malloc(sizeof(struct dups));
537                         if (new == NULL) {
538                                 pfatal("DUP TABLE OVERFLOW.");
539                                 if (reply("CONTINUE") == 0) {
540                                         ckfini(0);
541                                         exit(EEXIT);
542                                 }
543                                 rerun = 1;
544                                 return (STOP);
545                         }
546                         new->dup = blkno;
547                         if (muldup == NULL) {
548                                 duplist = muldup = new;
549                                 new->next = NULL;
550                         } else {
551                                 new->next = muldup->next;
552                                 muldup->next = new;
553                         }
554                         for (dlp = duplist; dlp != muldup; dlp = dlp->next)
555                                 if (dlp->dup == blkno)
556                                         break;
557                         if (dlp == muldup && dlp->dup != blkno)
558                                 muldup = new;
559                 }
560                 /*
561                  * count the number of blocks found in id_entryno
562                  */
563                 idesc->id_entryno++;
564         }
565         if (idesc->id_level == 0 && idesc->id_lballoc < idesc->id_lbn)
566                 idesc->id_lballoc = idesc->id_lbn;
567         return (res);
568 }