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 $
37 #include <sys/param.h>
39 #include <vfs/ufs/dinode.h>
40 #include <vfs/ufs/dir.h>
41 #include <vfs/ufs/fs.h>
48 long diskreads, totalreads; /* Disk cache statistics */
50 static void rwerror(char *mesg, ufs_daddr_t blk);
53 ftypeok(struct ufs1_dinode *dp)
55 switch (dp->di_mode & IFMT) {
68 printf("bad file type 0%o\n", dp->di_mode);
80 pfatal("INTERNAL ERROR: GOT TO reply()");
81 persevere = !strcmp(question, "CONTINUE");
83 if (!persevere && (nflag || fswritefd < 0)) {
84 printf("%s? no\n\n", question);
88 if (yflag || (persevere && nflag)) {
89 printf("%s? yes\n\n", question);
93 printf("%s? [yn] ", question);
96 while (c != '\n' && getc(stdin) != '\n') {
102 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
104 if (c == 'y' || c == 'Y')
111 * Look up state information for an inode.
114 inoinfo(ufs1_ino_t inum)
116 static struct inostat unallocated = { USTATE, 0, 0 };
117 struct inostatlist *ilp;
121 errx(EEXIT, "inoinfo: inumber %d out of range", inum);
122 ilp = &inostathead[inum / sblock.fs_ipg];
123 iloff = inum % sblock.fs_ipg;
124 if (iloff >= ilp->il_numalloced)
125 return (&unallocated);
126 return (&ilp->il_stat[iloff]);
130 * Malloc buffers and set up cache.
140 bufp = malloc((unsigned int)sblock.fs_bsize);
142 errx(EEXIT, "cannot allocate buffer pool");
143 cgblk.b_un.b_buf = bufp;
145 bufhead.b_next = bufhead.b_prev = &bufhead;
146 bufcnt = MAXBUFSPACE / sblock.fs_bsize;
147 if (bufcnt < MINBUFS)
149 for (i = 0; i < bufcnt; i++) {
150 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
151 bufp = malloc((unsigned int)sblock.fs_bsize);
152 if (bp == NULL || bufp == NULL) {
155 errx(EEXIT, "cannot allocate buffer pool");
157 bp->b_un.b_buf = bufp;
158 bp->b_prev = &bufhead;
159 bp->b_next = bufhead.b_next;
160 bufhead.b_next->b_prev = bp;
164 bufhead.b_size = i; /* save number of buffers */
168 * Manage a cache of directory blocks.
171 getdatablk(ufs_daddr_t blkno, long size)
175 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
176 if (bp->b_bno == fsbtodb(&sblock, blkno))
178 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
179 if ((bp->b_flags & B_INUSE) == 0)
182 errx(EEXIT, "deadlocked buffer pool");
183 getblk(bp, blkno, size);
187 bp->b_prev->b_next = bp->b_next;
188 bp->b_next->b_prev = bp->b_prev;
189 bp->b_prev = &bufhead;
190 bp->b_next = bufhead.b_next;
191 bufhead.b_next->b_prev = bp;
193 bp->b_flags |= B_INUSE;
198 getblk(struct bufarea *bp, ufs_daddr_t blk, long size)
202 dblk = fsbtodb(&sblock, blk);
203 if (bp->b_bno != dblk) {
204 flush(fswritefd, bp);
206 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
213 flush(int fd, struct bufarea *bp)
220 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
221 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
225 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
228 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
229 bwrite(fswritefd, (char *)sblock.fs_csp + i,
230 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
231 sblock.fs_cssize - i < sblock.fs_bsize ?
232 sblock.fs_cssize - i : sblock.fs_bsize);
237 rwerror(char *mesg, ufs_daddr_t blk)
242 pfatal("CANNOT %s: BLK %ld", mesg, blk);
243 if (reply("CONTINUE") == 0)
248 ckfini(int markclean)
250 struct bufarea *bp, *nbp;
251 int ofsmodified, cnt = 0;
257 flush(fswritefd, &sblk);
258 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
259 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
260 sblk.b_bno = SBOFF / dev_bsize;
262 flush(fswritefd, &sblk);
264 flush(fswritefd, &cgblk);
265 free(cgblk.b_un.b_buf);
266 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
268 flush(fswritefd, bp);
270 free(bp->b_un.b_buf);
273 if (bufhead.b_size != cnt)
274 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
276 if (sblock.fs_clean != markclean) {
277 sblock.fs_clean = markclean;
279 ofsmodified = fsmodified;
280 flush(fswritefd, &sblk);
281 fsmodified = ofsmodified;
283 printf("\n***** FILE SYSTEM MARKED %s *****\n",
284 markclean ? "CLEAN" : "DIRTY");
288 } else if (!preen && !markclean) {
289 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
293 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
294 totalreads, (int)(diskreads * 100 / totalreads));
300 bread(int fd, char *buf, ufs_daddr_t blk, long size)
308 if (lseek(fd, offset, 0) < 0)
309 rwerror("SEEK", blk);
310 else if (read(fd, buf, (int)size) == size)
312 rwerror("READ", blk);
313 if (lseek(fd, offset, 0) < 0)
314 rwerror("SEEK", blk);
316 memset(buf, 0, (size_t)size);
317 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
318 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
319 if (read(fd, cp, (int)secsize) != secsize) {
320 lseek(fd, offset + i + secsize, 0);
321 if (secsize != dev_bsize && dev_bsize != 1)
322 printf(" %ld (%ld),",
323 (blk * dev_bsize + i) / secsize,
324 blk + i / dev_bsize);
326 printf(" %ld,", blk + i / dev_bsize);
337 bwrite(int fd, char *buf, ufs_daddr_t blk, long size)
347 if (lseek(fd, offset, 0) < 0)
348 rwerror("SEEK", blk);
349 else if (write(fd, buf, (int)size) == size) {
354 rwerror("WRITE", blk);
355 if (lseek(fd, offset, 0) < 0)
356 rwerror("SEEK", blk);
357 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
358 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
359 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
360 lseek(fd, offset + i + dev_bsize, 0);
361 printf(" %ld,", blk + i / dev_bsize);
368 * allocate a data block with the specified number of fragments
373 int i, j, k, cg, baseblk;
374 struct cg *cgp = &cgrp;
376 if (frags <= 0 || frags > sblock.fs_frag)
378 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
379 for (j = 0; j <= sblock.fs_frag - frags; j++) {
382 for (k = 1; k < frags; k++)
383 if (testbmap(i + j + k))
389 cg = dtog(&sblock, i + j);
390 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
391 if (!cg_chkmagic(cgp))
392 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
393 baseblk = dtogd(&sblock, i + j);
394 for (k = 0; k < frags; k++) {
396 clrbit(cg_blksfree(cgp), baseblk + k);
399 if (frags == sblock.fs_frag)
400 cgp->cg_cs.cs_nbfree--;
402 cgp->cg_cs.cs_nffree -= frags;
411 * Free a previously allocated block
414 freeblk(ufs_daddr_t blkno, long frags)
416 struct inodesc idesc;
418 idesc.id_blkno = blkno;
419 idesc.id_numfrags = frags;
427 getpathname(char *namebuf, ufs1_ino_t curdir, ufs1_ino_t ino)
431 struct inodesc idesc;
434 if (curdir == ino && ino == ROOTINO) {
435 strcpy(namebuf, "/");
439 (inoinfo(curdir)->ino_state != DSTATE &&
440 inoinfo(curdir)->ino_state != DFOUND)) {
441 strcpy(namebuf, "?");
445 memset(&idesc, 0, sizeof(struct inodesc));
446 idesc.id_type = DATA;
447 idesc.id_fix = IGNORE;
448 cp = &namebuf[MAXPATHLEN - 1];
451 idesc.id_parent = curdir;
454 while (ino != ROOTINO) {
455 idesc.id_number = ino;
456 idesc.id_func = findino;
457 idesc.id_name = "..";
458 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
461 idesc.id_number = idesc.id_parent;
462 idesc.id_parent = ino;
463 idesc.id_func = findname;
464 idesc.id_name = namebuf;
465 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
467 len = strlen(namebuf);
469 memmove(cp, namebuf, (size_t)len);
471 if (cp < &namebuf[MAXNAMLEN])
473 ino = idesc.id_number;
478 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
491 * When preening, allow a single quit to signal
492 * a special exit after filesystem checks complete
493 * so that reboot sequence may be interrupted.
499 printf("returning to single-user after filesystem check\n");
501 signal(SIGQUIT, SIG_DFL);
505 * Ignore a single quit signal; wait and flush just in case.
506 * Used by child processes in preen.
514 signal(SIGQUIT, SIG_IGN);
515 signal(SIGQUIT, SIG_DFL);
526 * determine whether an inode should be fixed.
529 dofix(struct inodesc *idesc, char *msg)
532 switch (idesc->id_fix) {
535 if (idesc->id_type == DATA)
536 direrror(idesc->id_number, msg);
540 printf(" (SALVAGED)\n");
544 if (reply("SALVAGE") == 0) {
545 idesc->id_fix = NOFIX;
559 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
568 * An unexpected inconsistency occured.
569 * Die if preening or filesystem is running with soft dependency protocol,
570 * otherwise just print message and continue.
573 pfatal(const char *fmt, ...)
578 vfprintf(stderr, fmt, ap);
582 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
585 if (cdevname == NULL)
587 fprintf(stderr, "%s: ", cdevname);
588 vfprintf(stderr, fmt, ap);
590 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
591 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
597 * Pwarn just prints a message when not preening or running soft dependency
598 * protocol, or a warning (preceded by filename) when preening.
601 pwarn(const char *fmt, ...)
606 fprintf(stderr, "%s: ", cdevname);
607 vfprintf(stderr, fmt, ap);
612 * Stub for routines from kernel.
615 panic(const char *fmt, ...)
620 pfatal("INTERNAL INCONSISTENCY:");
621 vfprintf(stderr, fmt, ap);