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