2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)utilities.c 8.6 (Berkeley) 5/19/95
34 * $FreeBSD: src/sbin/fsck/utilities.c,v 1.11.2.3 2001/01/23 23:11:07 iedowse Exp $
35 * $DragonFly: src/sbin/fsck/utilities.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
38 #include <sys/param.h>
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ufs/dir.h>
42 #include <ufs/ffs/fs.h>
49 long diskreads, totalreads; /* Disk cache statistics */
51 static void rwerror __P((char *mesg, ufs_daddr_t blk));
57 switch (dp->di_mode & IFMT) {
70 printf("bad file type 0%o\n", dp->di_mode);
83 pfatal("INTERNAL ERROR: GOT TO reply()");
84 persevere = !strcmp(question, "CONTINUE");
86 if (!persevere && (nflag || fswritefd < 0)) {
87 printf("%s? no\n\n", question);
91 if (yflag || (persevere && nflag)) {
92 printf("%s? yes\n\n", question);
96 printf("%s? [yn] ", question);
97 (void) fflush(stdout);
99 while (c != '\n' && getc(stdin) != '\n') {
105 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
107 if (c == 'y' || c == 'Y')
114 * Look up state information for an inode.
120 static struct inostat unallocated = { USTATE, 0, 0 };
121 struct inostatlist *ilp;
125 errx(EEXIT, "inoinfo: inumber %d out of range", inum);
126 ilp = &inostathead[inum / sblock.fs_ipg];
127 iloff = inum % sblock.fs_ipg;
128 if (iloff >= ilp->il_numalloced)
129 return (&unallocated);
130 return (&ilp->il_stat[iloff]);
134 * Malloc buffers and set up cache.
139 register struct bufarea *bp;
143 pbp = pdirbp = (struct bufarea *)0;
144 bufp = malloc((unsigned int)sblock.fs_bsize);
146 errx(EEXIT, "cannot allocate buffer pool");
147 cgblk.b_un.b_buf = bufp;
149 bufhead.b_next = bufhead.b_prev = &bufhead;
150 bufcnt = MAXBUFSPACE / sblock.fs_bsize;
151 if (bufcnt < MINBUFS)
153 for (i = 0; i < bufcnt; i++) {
154 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
155 bufp = malloc((unsigned int)sblock.fs_bsize);
156 if (bp == NULL || bufp == NULL) {
159 errx(EEXIT, "cannot allocate buffer pool");
161 bp->b_un.b_buf = bufp;
162 bp->b_prev = &bufhead;
163 bp->b_next = bufhead.b_next;
164 bufhead.b_next->b_prev = bp;
168 bufhead.b_size = i; /* save number of buffers */
172 * Manage a cache of directory blocks.
175 getdatablk(blkno, size)
179 register struct bufarea *bp;
181 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
182 if (bp->b_bno == fsbtodb(&sblock, blkno))
184 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
185 if ((bp->b_flags & B_INUSE) == 0)
188 errx(EEXIT, "deadlocked buffer pool");
189 getblk(bp, blkno, size);
193 bp->b_prev->b_next = bp->b_next;
194 bp->b_next->b_prev = bp->b_prev;
195 bp->b_prev = &bufhead;
196 bp->b_next = bufhead.b_next;
197 bufhead.b_next->b_prev = bp;
199 bp->b_flags |= B_INUSE;
204 getblk(bp, blk, size)
205 register struct bufarea *bp;
211 dblk = fsbtodb(&sblock, blk);
212 if (bp->b_bno != dblk) {
213 flush(fswritefd, bp);
215 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
224 register struct bufarea *bp;
231 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
232 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
236 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
239 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
240 bwrite(fswritefd, (char *)sblock.fs_csp + i,
241 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
242 sblock.fs_cssize - i < sblock.fs_bsize ?
243 sblock.fs_cssize - i : sblock.fs_bsize);
255 pfatal("CANNOT %s: BLK %ld", mesg, blk);
256 if (reply("CONTINUE") == 0)
264 register struct bufarea *bp, *nbp;
265 int ofsmodified, cnt = 0;
268 (void)close(fsreadfd);
271 flush(fswritefd, &sblk);
272 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
273 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
274 sblk.b_bno = SBOFF / dev_bsize;
276 flush(fswritefd, &sblk);
278 flush(fswritefd, &cgblk);
279 free(cgblk.b_un.b_buf);
280 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
282 flush(fswritefd, bp);
284 free(bp->b_un.b_buf);
287 if (bufhead.b_size != cnt)
288 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
289 pbp = pdirbp = (struct bufarea *)0;
290 if (sblock.fs_clean != markclean) {
291 sblock.fs_clean = markclean;
293 ofsmodified = fsmodified;
294 flush(fswritefd, &sblk);
295 fsmodified = ofsmodified;
297 printf("\n***** FILE SYSTEM MARKED %s *****\n",
298 markclean ? "CLEAN" : "DIRTY");
302 } else if (!preen && !markclean) {
303 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
307 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
308 totalreads, (int)(diskreads * 100 / totalreads));
309 (void)close(fsreadfd);
310 (void)close(fswritefd);
314 bread(fd, buf, blk, size)
326 if (lseek(fd, offset, 0) < 0)
327 rwerror("SEEK", blk);
328 else if (read(fd, buf, (int)size) == size)
330 rwerror("READ", blk);
331 if (lseek(fd, offset, 0) < 0)
332 rwerror("SEEK", blk);
334 memset(buf, 0, (size_t)size);
335 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
336 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
337 if (read(fd, cp, (int)secsize) != secsize) {
338 (void)lseek(fd, offset + i + secsize, 0);
339 if (secsize != dev_bsize && dev_bsize != 1)
340 printf(" %ld (%ld),",
341 (blk * dev_bsize + i) / secsize,
342 blk + i / dev_bsize);
344 printf(" %ld,", blk + i / dev_bsize);
355 bwrite(fd, buf, blk, size)
369 if (lseek(fd, offset, 0) < 0)
370 rwerror("SEEK", blk);
371 else if (write(fd, buf, (int)size) == size) {
376 rwerror("WRITE", blk);
377 if (lseek(fd, offset, 0) < 0)
378 rwerror("SEEK", blk);
379 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
380 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
381 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
382 (void)lseek(fd, offset + i + dev_bsize, 0);
383 printf(" %ld,", blk + i / dev_bsize);
390 * allocate a data block with the specified number of fragments
396 int i, j, k, cg, baseblk;
397 struct cg *cgp = &cgrp;
399 if (frags <= 0 || frags > sblock.fs_frag)
401 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
402 for (j = 0; j <= sblock.fs_frag - frags; j++) {
405 for (k = 1; k < frags; k++)
406 if (testbmap(i + j + k))
412 cg = dtog(&sblock, i + j);
413 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
414 if (!cg_chkmagic(cgp))
415 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
416 baseblk = dtogd(&sblock, i + j);
417 for (k = 0; k < frags; k++) {
419 clrbit(cg_blksfree(cgp), baseblk + k);
422 if (frags == sblock.fs_frag)
423 cgp->cg_cs.cs_nbfree--;
425 cgp->cg_cs.cs_nffree -= frags;
434 * Free a previously allocated block
437 freeblk(blkno, frags)
441 struct inodesc idesc;
443 idesc.id_blkno = blkno;
444 idesc.id_numfrags = frags;
445 (void)pass4check(&idesc);
452 getpathname(namebuf, curdir, ino)
458 struct inodesc idesc;
461 if (curdir == ino && ino == ROOTINO) {
462 (void)strcpy(namebuf, "/");
466 (inoinfo(curdir)->ino_state != DSTATE &&
467 inoinfo(curdir)->ino_state != DFOUND)) {
468 (void)strcpy(namebuf, "?");
472 memset(&idesc, 0, sizeof(struct inodesc));
473 idesc.id_type = DATA;
474 idesc.id_fix = IGNORE;
475 cp = &namebuf[MAXPATHLEN - 1];
478 idesc.id_parent = curdir;
481 while (ino != ROOTINO) {
482 idesc.id_number = ino;
483 idesc.id_func = findino;
484 idesc.id_name = "..";
485 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
488 idesc.id_number = idesc.id_parent;
489 idesc.id_parent = ino;
490 idesc.id_func = findname;
491 idesc.id_name = namebuf;
492 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
494 len = strlen(namebuf);
496 memmove(cp, namebuf, (size_t)len);
498 if (cp < &namebuf[MAXNAMLEN])
500 ino = idesc.id_number;
505 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
518 * When preening, allow a single quit to signal
519 * a special exit after filesystem checks complete
520 * so that reboot sequence may be interrupted.
526 printf("returning to single-user after filesystem check\n");
528 (void)signal(SIGQUIT, SIG_DFL);
532 * Ignore a single quit signal; wait and flush just in case.
533 * Used by child processes in preen.
541 (void)signal(SIGQUIT, SIG_IGN);
542 (void)signal(SIGQUIT, SIG_DFL);
553 * determine whether an inode should be fixed.
557 register struct inodesc *idesc;
561 switch (idesc->id_fix) {
564 if (idesc->id_type == DATA)
565 direrror(idesc->id_number, msg);
569 printf(" (SALVAGED)\n");
573 if (reply("SALVAGE") == 0) {
574 idesc->id_fix = NOFIX;
588 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
601 * An unexpected inconsistency occured.
602 * Die if preening or filesystem is running with soft dependency protocol,
603 * otherwise just print message and continue.
607 pfatal(const char *fmt, ...)
609 pfatal(fmt, va_alist)
621 (void)vfprintf(stderr, fmt, ap);
624 (void)fprintf(stderr,
625 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
628 if (cdevname == NULL)
630 (void)fprintf(stderr, "%s: ", cdevname);
631 (void)vfprintf(stderr, fmt, ap);
632 (void)fprintf(stderr,
633 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
634 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
640 * Pwarn just prints a message when not preening or running soft dependency
641 * protocol, or a warning (preceded by filename) when preening.
645 pwarn(const char *fmt, ...)
659 (void)fprintf(stderr, "%s: ", cdevname);
660 (void)vfprintf(stderr, fmt, ap);
665 * Stub for routines from kernel.
669 panic(const char *fmt, ...)
682 pfatal("INTERNAL INCONSISTENCY:");
683 (void)vfprintf(stderr, fmt, ap);