]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/fsutil.c
This commit was generated by cvs2svn to compensate for changes in r104990,
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / fsutil.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[] = "@(#)utilities.c   8.6 (Berkeley) 5/19/95";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
45 #include <sys/disklabel.h>
46 #include <sys/stat.h>
47 #include <sys/disklabel.h>
48
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ffs/fs.h>
52
53 #include <err.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <fstab.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62
63 #include "fsck.h"
64
65 long    diskreads, totalreads;  /* Disk cache statistics */
66
67 int
68 ftypeok(union dinode *dp)
69 {
70         switch (DIP(dp, di_mode) & IFMT) {
71
72         case IFDIR:
73         case IFREG:
74         case IFBLK:
75         case IFCHR:
76         case IFLNK:
77         case IFSOCK:
78         case IFIFO:
79                 return (1);
80
81         default:
82                 if (debug)
83                         printf("bad file type 0%o\n", DIP(dp, di_mode));
84                 return (0);
85         }
86 }
87
88 int
89 reply(const char *question)
90 {
91         int persevere;
92         char c;
93
94         if (preen)
95                 pfatal("INTERNAL ERROR: GOT TO reply()");
96         persevere = !strcmp(question, "CONTINUE");
97         printf("\n");
98         if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
99                 printf("%s? no\n\n", question);
100                 resolved = 0;
101                 return (0);
102         }
103         if (yflag || (persevere && nflag)) {
104                 printf("%s? yes\n\n", question);
105                 return (1);
106         }
107         do      {
108                 printf("%s? [yn] ", question);
109                 (void) fflush(stdout);
110                 c = getc(stdin);
111                 while (c != '\n' && getc(stdin) != '\n') {
112                         if (feof(stdin)) {
113                                 resolved = 0;
114                                 return (0);
115                         }
116                 }
117         } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
118         printf("\n");
119         if (c == 'y' || c == 'Y')
120                 return (1);
121         resolved = 0;
122         return (0);
123 }
124
125 /*
126  * Look up state information for an inode.
127  */
128 struct inostat *
129 inoinfo(ino_t inum)
130 {
131         static struct inostat unallocated = { USTATE, 0, 0 };
132         struct inostatlist *ilp;
133         int iloff;
134
135         if (inum > maxino)
136                 errx(EEXIT, "inoinfo: inumber %d out of range", inum);
137         ilp = &inostathead[inum / sblock.fs_ipg];
138         iloff = inum % sblock.fs_ipg;
139         if (iloff >= ilp->il_numalloced)
140                 return (&unallocated);
141         return (&ilp->il_stat[iloff]);
142 }
143
144 /*
145  * Malloc buffers and set up cache.
146  */
147 void
148 bufinit(void)
149 {
150         struct bufarea *bp;
151         long bufcnt, i;
152         char *bufp;
153
154         pbp = pdirbp = (struct bufarea *)0;
155         bufp = malloc((unsigned int)sblock.fs_bsize);
156         if (bufp == 0)
157                 errx(EEXIT, "cannot allocate buffer pool");
158         cgblk.b_un.b_buf = bufp;
159         initbarea(&cgblk);
160         bufhead.b_next = bufhead.b_prev = &bufhead;
161         bufcnt = MAXBUFSPACE / sblock.fs_bsize;
162         if (bufcnt < MINBUFS)
163                 bufcnt = MINBUFS;
164         for (i = 0; i < bufcnt; i++) {
165                 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
166                 bufp = malloc((unsigned int)sblock.fs_bsize);
167                 if (bp == NULL || bufp == NULL) {
168                         if (i >= MINBUFS)
169                                 break;
170                         errx(EEXIT, "cannot allocate buffer pool");
171                 }
172                 bp->b_un.b_buf = bufp;
173                 bp->b_prev = &bufhead;
174                 bp->b_next = bufhead.b_next;
175                 bufhead.b_next->b_prev = bp;
176                 bufhead.b_next = bp;
177                 initbarea(bp);
178         }
179         bufhead.b_size = i;     /* save number of buffers */
180 }
181
182 /*
183  * Manage a cache of directory blocks.
184  */
185 struct bufarea *
186 getdatablk(ufs2_daddr_t blkno, long size)
187 {
188         struct bufarea *bp;
189
190         for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
191                 if (bp->b_bno == fsbtodb(&sblock, blkno))
192                         goto foundit;
193         for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
194                 if ((bp->b_flags & B_INUSE) == 0)
195                         break;
196         if (bp == &bufhead)
197                 errx(EEXIT, "deadlocked buffer pool");
198         getblk(bp, blkno, size);
199         /* fall through */
200 foundit:
201         bp->b_prev->b_next = bp->b_next;
202         bp->b_next->b_prev = bp->b_prev;
203         bp->b_prev = &bufhead;
204         bp->b_next = bufhead.b_next;
205         bufhead.b_next->b_prev = bp;
206         bufhead.b_next = bp;
207         bp->b_flags |= B_INUSE;
208         return (bp);
209 }
210
211 void
212 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
213 {
214         ufs2_daddr_t dblk;
215
216         totalreads++;
217         dblk = fsbtodb(&sblock, blk);
218         if (bp->b_bno != dblk) {
219                 flush(fswritefd, bp);
220                 diskreads++;
221                 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
222                 bp->b_bno = dblk;
223                 bp->b_size = size;
224         }
225 }
226
227 void
228 flush(int fd, struct bufarea *bp)
229 {
230         int i, j;
231
232         if (!bp->b_dirty)
233                 return;
234         bp->b_dirty = 0;
235         if (fswritefd < 0) {
236                 pfatal("WRITING IN READ_ONLY MODE.\n");
237                 return;
238         }
239         if (bp->b_errs != 0)
240                 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
241                     (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
242                     (long long)bp->b_bno);
243         bp->b_errs = 0;
244         bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
245         if (bp != &sblk)
246                 return;
247         for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
248                 bwrite(fswritefd, (char *)sblock.fs_csp + i,
249                     fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
250                     sblock.fs_cssize - i < sblock.fs_bsize ?
251                     sblock.fs_cssize - i : sblock.fs_bsize);
252         }
253 }
254
255 void
256 rwerror(const char *mesg, ufs2_daddr_t blk)
257 {
258
259         if (bkgrdcheck)
260                 exit(EEXIT);
261         if (preen == 0)
262                 printf("\n");
263         pfatal("CANNOT %s: %ld", mesg, (long)blk);
264         if (reply("CONTINUE") == 0)
265                 exit(EEXIT);
266 }
267
268 void
269 ckfini(int markclean)
270 {
271         struct bufarea *bp, *nbp;
272         int ofsmodified, cnt = 0;
273
274         if (bkgrdflag) {
275                 unlink(snapname);
276                 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
277                         cmd.value = FS_UNCLEAN;
278                         cmd.size = markclean ? -1 : 1;
279                         if (sysctlbyname("vfs.ffs.setflags", 0, 0,
280                             &cmd, sizeof cmd) == -1)
281                                 rwerror("SET FILESYSTEM FLAGS", FS_UNCLEAN);
282                         if (!preen) {
283                                 printf("\n***** FILE SYSTEM MARKED %s *****\n",
284                                     markclean ? "CLEAN" : "DIRTY");
285                                 if (!markclean)
286                                         rerun = 1;
287                         }
288                 } else if (!preen && !markclean) {
289                         printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
290                         rerun = 1;
291                 }
292         }
293         if (fswritefd < 0) {
294                 (void)close(fsreadfd);
295                 return;
296         }
297         flush(fswritefd, &sblk);
298         if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
299             sblk.b_bno != fsbtodb(&sblock, sblock.fs_sblockloc) &&
300             !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
301                 sblk.b_bno = fsbtodb(&sblock, sblock.fs_sblockloc);
302                 sbdirty();
303                 flush(fswritefd, &sblk);
304         }
305         flush(fswritefd, &cgblk);
306         free(cgblk.b_un.b_buf);
307         for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
308                 cnt++;
309                 flush(fswritefd, bp);
310                 nbp = bp->b_prev;
311                 free(bp->b_un.b_buf);
312                 free((char *)bp);
313         }
314         if (bufhead.b_size != cnt)
315                 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
316         pbp = pdirbp = (struct bufarea *)0;
317         if (cursnapshot == 0 && sblock.fs_clean != markclean) {
318                 if ((sblock.fs_clean = markclean) != 0)
319                         sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
320                 sbdirty();
321                 ofsmodified = fsmodified;
322                 flush(fswritefd, &sblk);
323                 fsmodified = ofsmodified;
324                 if (!preen) {
325                         printf("\n***** FILE SYSTEM MARKED %s *****\n",
326                             markclean ? "CLEAN" : "DIRTY");
327                         if (!markclean)
328                                 rerun = 1;
329                 }
330         } else if (!preen && !markclean) {
331                 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
332                 rerun = 1;
333         }
334         if (debug && totalreads > 0)
335                 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
336                     totalreads, (int)(diskreads * 100 / totalreads));
337         (void)close(fsreadfd);
338         (void)close(fswritefd);
339 }
340
341 int
342 bread(int fd, char *buf, ufs2_daddr_t blk, long size)
343 {
344         char *cp;
345         int i, errs;
346         off_t offset;
347
348         offset = blk;
349         offset *= dev_bsize;
350         if (lseek(fd, offset, 0) < 0)
351                 rwerror("SEEK BLK", blk);
352         else if (read(fd, buf, (int)size) == size)
353                 return (0);
354         rwerror("READ BLK", blk);
355         if (lseek(fd, offset, 0) < 0)
356                 rwerror("SEEK BLK", blk);
357         errs = 0;
358         memset(buf, 0, (size_t)size);
359         printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
360         for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
361                 if (read(fd, cp, (int)secsize) != secsize) {
362                         (void)lseek(fd, offset + i + secsize, 0);
363                         if (secsize != dev_bsize && dev_bsize != 1)
364                                 printf(" %jd (%jd),",
365                                     (intmax_t)(blk * dev_bsize + i) / secsize,
366                                     (intmax_t)blk + i / dev_bsize);
367                         else
368                                 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
369                         errs++;
370                 }
371         }
372         printf("\n");
373         if (errs)
374                 resolved = 0;
375         return (errs);
376 }
377
378 void
379 bwrite(int fd, char *buf, ufs2_daddr_t blk, long size)
380 {
381         int i;
382         char *cp;
383         off_t offset;
384
385         if (fd < 0)
386                 return;
387         offset = blk;
388         offset *= dev_bsize;
389         if (lseek(fd, offset, 0) < 0)
390                 rwerror("SEEK BLK", blk);
391         else if (write(fd, buf, (int)size) == size) {
392                 fsmodified = 1;
393                 return;
394         }
395         resolved = 0;
396         rwerror("WRITE BLK", blk);
397         if (lseek(fd, offset, 0) < 0)
398                 rwerror("SEEK BLK", blk);
399         printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
400         for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
401                 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
402                         (void)lseek(fd, offset + i + dev_bsize, 0);
403                         printf(" %jd,", (intmax_t)blk + i / dev_bsize);
404                 }
405         printf("\n");
406         return;
407 }
408
409 /*
410  * allocate a data block with the specified number of fragments
411  */
412 ufs2_daddr_t
413 allocblk(long frags)
414 {
415         int i, j, k, cg, baseblk;
416         struct cg *cgp = &cgrp;
417
418         if (frags <= 0 || frags > sblock.fs_frag)
419                 return (0);
420         for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
421                 for (j = 0; j <= sblock.fs_frag - frags; j++) {
422                         if (testbmap(i + j))
423                                 continue;
424                         for (k = 1; k < frags; k++)
425                                 if (testbmap(i + j + k))
426                                         break;
427                         if (k < frags) {
428                                 j += k;
429                                 continue;
430                         }
431                         cg = dtog(&sblock, i + j);
432                         getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
433                         if (!cg_chkmagic(cgp))
434                                 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
435                         baseblk = dtogd(&sblock, i + j);
436                         for (k = 0; k < frags; k++) {
437                                 setbmap(i + j + k);
438                                 clrbit(cg_blksfree(cgp), baseblk + k);
439                         }
440                         n_blks += frags;
441                         if (frags == sblock.fs_frag)
442                                 cgp->cg_cs.cs_nbfree--;
443                         else
444                                 cgp->cg_cs.cs_nffree -= frags;
445                         cgdirty();
446                         return (i + j);
447                 }
448         }
449         return (0);
450 }
451
452 /*
453  * Free a previously allocated block
454  */
455 void
456 freeblk(ufs2_daddr_t blkno, long frags)
457 {
458         struct inodesc idesc;
459
460         idesc.id_blkno = blkno;
461         idesc.id_numfrags = frags;
462         (void)pass4check(&idesc);
463 }
464
465 /*
466  * Find a pathname
467  */
468 void
469 getpathname(char *namebuf, ino_t curdir, ino_t ino)
470 {
471         int len;
472         char *cp;
473         struct inodesc idesc;
474         static int busy = 0;
475
476         if (curdir == ino && ino == ROOTINO) {
477                 (void)strcpy(namebuf, "/");
478                 return;
479         }
480         if (busy ||
481             (inoinfo(curdir)->ino_state != DSTATE &&
482              inoinfo(curdir)->ino_state != DFOUND)) {
483                 (void)strcpy(namebuf, "?");
484                 return;
485         }
486         busy = 1;
487         memset(&idesc, 0, sizeof(struct inodesc));
488         idesc.id_type = DATA;
489         idesc.id_fix = IGNORE;
490         cp = &namebuf[MAXPATHLEN - 1];
491         *cp = '\0';
492         if (curdir != ino) {
493                 idesc.id_parent = curdir;
494                 goto namelookup;
495         }
496         while (ino != ROOTINO) {
497                 idesc.id_number = ino;
498                 idesc.id_func = findino;
499                 idesc.id_name = strdup("..");
500                 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
501                         break;
502         namelookup:
503                 idesc.id_number = idesc.id_parent;
504                 idesc.id_parent = ino;
505                 idesc.id_func = findname;
506                 idesc.id_name = namebuf;
507                 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
508                         break;
509                 len = strlen(namebuf);
510                 cp -= len;
511                 memmove(cp, namebuf, (size_t)len);
512                 *--cp = '/';
513                 if (cp < &namebuf[MAXNAMLEN])
514                         break;
515                 ino = idesc.id_number;
516         }
517         busy = 0;
518         if (ino != ROOTINO)
519                 *--cp = '?';
520         memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
521 }
522
523 void
524 catch(int sig __unused)
525 {
526
527         ckfini(0);
528         exit(12);
529 }
530
531 /*
532  * When preening, allow a single quit to signal
533  * a special exit after file system checks complete
534  * so that reboot sequence may be interrupted.
535  */
536 void
537 catchquit(int sig __unused)
538 {
539         printf("returning to single-user after file system check\n");
540         returntosingle = 1;
541         (void)signal(SIGQUIT, SIG_DFL);
542 }
543
544 /*
545  * determine whether an inode should be fixed.
546  */
547 int
548 dofix(struct inodesc *idesc, const char *msg)
549 {
550
551         switch (idesc->id_fix) {
552
553         case DONTKNOW:
554                 if (idesc->id_type == DATA)
555                         direrror(idesc->id_number, msg);
556                 else
557                         pwarn("%s", msg);
558                 if (preen) {
559                         printf(" (SALVAGED)\n");
560                         idesc->id_fix = FIX;
561                         return (ALTERED);
562                 }
563                 if (reply("SALVAGE") == 0) {
564                         idesc->id_fix = NOFIX;
565                         return (0);
566                 }
567                 idesc->id_fix = FIX;
568                 return (ALTERED);
569
570         case FIX:
571                 return (ALTERED);
572
573         case NOFIX:
574         case IGNORE:
575                 return (0);
576
577         default:
578                 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
579         }
580         /* NOTREACHED */
581         return (0);
582 }
583
584 #include <stdarg.h>
585
586 /*
587  * An unexpected inconsistency occured.
588  * Die if preening or file system is running with soft dependency protocol,
589  * otherwise just print message and continue.
590  */
591 void
592 pfatal(const char *fmt, ...)
593 {
594         va_list ap;
595         va_start(ap, fmt);
596         if (!preen) {
597                 (void)vfprintf(stdout, fmt, ap);
598                 va_end(ap);
599                 if (usedsoftdep)
600                         (void)fprintf(stdout,
601                             "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
602                 /*
603                  * Force foreground fsck to clean up inconsistency.
604                  */
605                 if (bkgrdflag) {
606                         cmd.value = FS_NEEDSFSCK;
607                         cmd.size = 1;
608                         if (sysctlbyname("vfs.ffs.setflags", 0, 0,
609                             &cmd, sizeof cmd) == -1)
610                                 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
611                         fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
612                         ckfini(0);
613                         exit(EEXIT);
614                 }
615                 return;
616         }
617         if (cdevname == NULL)
618                 cdevname = strdup("fsck");
619         (void)fprintf(stdout, "%s: ", cdevname);
620         (void)vfprintf(stdout, fmt, ap);
621         (void)fprintf(stdout,
622             "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
623             cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
624         /*
625          * Force foreground fsck to clean up inconsistency.
626          */
627         if (bkgrdflag) {
628                 cmd.value = FS_NEEDSFSCK;
629                 cmd.size = 1;
630                 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
631                     &cmd, sizeof cmd) == -1)
632                         pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
633         }
634         ckfini(0);
635         exit(EEXIT);
636 }
637
638 /*
639  * Pwarn just prints a message when not preening or running soft dependency
640  * protocol, or a warning (preceded by filename) when preening.
641  */
642 void
643 pwarn(const char *fmt, ...)
644 {
645         va_list ap;
646         va_start(ap, fmt);
647         if (preen)
648                 (void)fprintf(stdout, "%s: ", cdevname);
649         (void)vfprintf(stdout, fmt, ap);
650         va_end(ap);
651 }
652
653 /*
654  * Stub for routines from kernel.
655  */
656 void
657 panic(const char *fmt, ...)
658 {
659         va_list ap;
660         va_start(ap, fmt);
661         pfatal("INTERNAL INCONSISTENCY:");
662         (void)vfprintf(stdout, fmt, ap);
663         va_end(ap);
664         exit(EEXIT);
665 }