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. 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.
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
29 * @(#)utilities.c 8.6 (Berkeley) 5/19/95
30 * $FreeBSD: src/sbin/fsck/utilities.c,v 1.11.2.3 2001/01/23 23:11:07 iedowse Exp $
33 #include <sys/param.h>
35 #include <vfs/ufs/dinode.h>
36 #include <vfs/ufs/dir.h>
37 #include <vfs/ufs/fs.h>
44 long diskreads, totalreads; /* Disk cache statistics */
45 struct bufarea bufhead; /* head of list of other blks in filesys */
46 char *cdevname; /* name of device being checked */
47 char yflag; /* assume a yes response */
48 char nflag; /* assume a no response */
49 int debug; /* output debugging info */
50 char resolved; /* cleared if unresolved changes => not clean */
51 int fsmodified; /* 1 => write done to file system */
52 int got_siginfo; /* received a SIGINFO */
54 static void rwerror(char *mesg, ufs_daddr_t blk);
57 ftypeok(struct ufs1_dinode *dp)
59 switch (dp->di_mode & IFMT) {
72 printf("bad file type 0%o\n", dp->di_mode);
84 pfatal("INTERNAL ERROR: GOT TO reply()");
85 persevere = !strcmp(question, "CONTINUE");
87 if (!persevere && (nflag || fswritefd < 0)) {
88 printf("%s? no\n\n", question);
92 if (yflag || (persevere && nflag)) {
93 printf("%s? yes\n\n", question);
97 printf("%s? [yn] ", question);
100 while (c != '\n' && getc(stdin) != '\n') {
106 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
108 if (c == 'y' || c == 'Y')
115 * Look up state information for an inode.
118 inoinfo(ufs1_ino_t inum)
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.
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(ufs_daddr_t blkno, long size)
179 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
180 if (bp->b_bno == fsbtodb(&sblock, blkno))
182 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
183 if ((bp->b_flags & B_INUSE) == 0)
186 errx(EEXIT, "deadlocked buffer pool");
187 getblk(bp, blkno, size);
191 bp->b_prev->b_next = bp->b_next;
192 bp->b_next->b_prev = bp->b_prev;
193 bp->b_prev = &bufhead;
194 bp->b_next = bufhead.b_next;
195 bufhead.b_next->b_prev = bp;
197 bp->b_flags |= B_INUSE;
202 getblk(struct bufarea *bp, ufs_daddr_t blk, long size)
206 dblk = fsbtodb(&sblock, blk);
207 if (bp->b_bno != dblk) {
208 flush(fswritefd, bp);
210 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
217 flush(int fd, struct bufarea *bp)
224 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
225 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
229 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
232 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
233 bwrite(fswritefd, (char *)sblock.fs_csp + i,
234 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
235 sblock.fs_cssize - i < sblock.fs_bsize ?
236 sblock.fs_cssize - i : sblock.fs_bsize);
241 rwerror(char *mesg, ufs_daddr_t blk)
246 pfatal("CANNOT %s: BLK %d", mesg, blk);
247 if (reply("CONTINUE") == 0)
252 ckfini(int markclean)
254 struct bufarea *bp, *nbp;
255 int ofsmodified, cnt = 0;
261 flush(fswritefd, &sblk);
262 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
263 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
264 sblk.b_bno = SBOFF / dev_bsize;
266 flush(fswritefd, &sblk);
268 flush(fswritefd, &cgblk);
269 free(cgblk.b_un.b_buf);
270 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
272 flush(fswritefd, bp);
274 free(bp->b_un.b_buf);
277 if (bufhead.b_size != cnt)
278 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
280 if (sblock.fs_clean != markclean) {
281 sblock.fs_clean = markclean;
283 ofsmodified = fsmodified;
284 flush(fswritefd, &sblk);
285 fsmodified = ofsmodified;
287 printf("\n***** FILE SYSTEM MARKED %s *****\n",
288 markclean ? "CLEAN" : "DIRTY");
292 } else if (!preen && !markclean) {
293 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
297 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
298 totalreads, (int)(diskreads * 100 / totalreads));
304 bread(int fd, char *buf, ufs_daddr_t blk, long size)
312 if (lseek(fd, offset, 0) < 0)
313 rwerror("SEEK", blk);
314 else if (read(fd, buf, (int)size) == size)
316 rwerror("READ", blk);
317 if (lseek(fd, offset, 0) < 0)
318 rwerror("SEEK", blk);
320 memset(buf, 0, (size_t)size);
321 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
322 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
323 if (read(fd, cp, (int)secsize) != secsize) {
324 lseek(fd, offset + i + secsize, 0);
325 if (secsize != dev_bsize && dev_bsize != 1)
326 printf(" %ld (%ld),",
327 (blk * dev_bsize + i) / secsize,
328 blk + i / dev_bsize);
330 printf(" %ld,", blk + i / dev_bsize);
341 bwrite(int fd, char *buf, ufs_daddr_t blk, long size)
351 if (lseek(fd, offset, 0) < 0)
352 rwerror("SEEK", blk);
353 else if (write(fd, buf, (int)size) == size) {
358 rwerror("WRITE", blk);
359 if (lseek(fd, offset, 0) < 0)
360 rwerror("SEEK", blk);
361 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
362 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
363 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
364 lseek(fd, offset + i + dev_bsize, 0);
365 printf(" %ld,", blk + i / dev_bsize);
372 * allocate a data block with the specified number of fragments
377 int i, j, k, cg, baseblk;
378 struct cg *cgp = &cgrp;
380 if (frags <= 0 || frags > sblock.fs_frag)
382 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
383 for (j = 0; j <= sblock.fs_frag - frags; j++) {
386 for (k = 1; k < frags; k++)
387 if (testbmap(i + j + k))
393 cg = dtog(&sblock, i + j);
394 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
395 if (!cg_chkmagic(cgp))
396 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
397 baseblk = dtogd(&sblock, i + j);
398 for (k = 0; k < frags; k++) {
400 clrbit(cg_blksfree(cgp), baseblk + k);
403 if (frags == sblock.fs_frag)
404 cgp->cg_cs.cs_nbfree--;
406 cgp->cg_cs.cs_nffree -= frags;
415 * Free a previously allocated block
418 freeblk(ufs_daddr_t blkno, long frags)
420 struct inodesc idesc;
422 idesc.id_blkno = blkno;
423 idesc.id_numfrags = frags;
431 getpathname(char *namebuf, ufs1_ino_t curdir, ufs1_ino_t ino)
435 struct inodesc idesc;
438 if (curdir == ino && ino == UFS_ROOTINO) {
439 strcpy(namebuf, "/");
443 (inoinfo(curdir)->ino_state != DSTATE &&
444 inoinfo(curdir)->ino_state != DFOUND)) {
445 strcpy(namebuf, "?");
449 memset(&idesc, 0, sizeof(struct inodesc));
450 idesc.id_type = DATA;
451 idesc.id_fix = IGNORE;
452 cp = &namebuf[MAXPATHLEN - 1];
455 idesc.id_parent = curdir;
458 while (ino != UFS_ROOTINO) {
459 idesc.id_number = ino;
460 idesc.id_func = findino;
461 idesc.id_name = "..";
462 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
465 idesc.id_number = idesc.id_parent;
466 idesc.id_parent = ino;
467 idesc.id_func = findname;
468 idesc.id_name = namebuf;
469 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
471 len = strlen(namebuf);
473 memmove(cp, namebuf, (size_t)len);
475 if (cp < &namebuf[MAXNAMLEN])
477 ino = idesc.id_number;
480 if (ino != UFS_ROOTINO)
482 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
495 * When preening, allow a single quit to signal
496 * a special exit after filesystem checks complete
497 * so that reboot sequence may be interrupted.
503 printf("returning to single-user after filesystem check\n");
505 signal(SIGQUIT, SIG_DFL);
509 * Ignore a single quit signal; wait and flush just in case.
510 * Used by child processes in preen.
518 signal(SIGQUIT, SIG_IGN);
519 signal(SIGQUIT, SIG_DFL);
530 * determine whether an inode should be fixed.
533 dofix(struct inodesc *idesc, char *msg)
536 switch (idesc->id_fix) {
539 if (idesc->id_type == DATA)
540 direrror(idesc->id_number, msg);
544 printf(" (SALVAGED)\n");
548 if (reply("SALVAGE") == 0) {
549 idesc->id_fix = NOFIX;
563 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
572 * An unexpected inconsistency occured.
573 * Die if preening or filesystem is running with soft dependency protocol,
574 * otherwise just print message and continue.
577 pfatal(const char *fmt, ...)
582 vfprintf(stderr, fmt, ap);
586 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
589 if (cdevname == NULL)
591 fprintf(stderr, "%s: ", cdevname);
592 vfprintf(stderr, fmt, ap);
594 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
595 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
601 * Pwarn just prints a message when not preening or running soft dependency
602 * protocol, or a warning (preceded by filename) when preening.
605 pwarn(const char *fmt, ...)
610 fprintf(stderr, "%s: ", cdevname);
611 vfprintf(stderr, fmt, ap);
616 * Stub for routines from kernel.
619 panic(const char *fmt, ...)
624 pfatal("INTERNAL INCONSISTENCY:");
625 vfprintf(stderr, fmt, ap);