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 */
46 static void rwerror(char *mesg, ufs_daddr_t blk);
49 ftypeok(struct ufs1_dinode *dp)
51 switch (dp->di_mode & IFMT) {
64 printf("bad file type 0%o\n", dp->di_mode);
76 pfatal("INTERNAL ERROR: GOT TO reply()");
77 persevere = !strcmp(question, "CONTINUE");
79 if (!persevere && (nflag || fswritefd < 0)) {
80 printf("%s? no\n\n", question);
84 if (yflag || (persevere && nflag)) {
85 printf("%s? yes\n\n", question);
89 printf("%s? [yn] ", question);
92 while (c != '\n' && getc(stdin) != '\n') {
98 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
100 if (c == 'y' || c == 'Y')
107 * Look up state information for an inode.
110 inoinfo(ufs1_ino_t inum)
112 static struct inostat unallocated = { USTATE, 0, 0 };
113 struct inostatlist *ilp;
117 errx(EEXIT, "inoinfo: inumber %d out of range", inum);
118 ilp = &inostathead[inum / sblock.fs_ipg];
119 iloff = inum % sblock.fs_ipg;
120 if (iloff >= ilp->il_numalloced)
121 return (&unallocated);
122 return (&ilp->il_stat[iloff]);
126 * Malloc buffers and set up cache.
136 bufp = malloc((unsigned int)sblock.fs_bsize);
138 errx(EEXIT, "cannot allocate buffer pool");
139 cgblk.b_un.b_buf = bufp;
141 bufhead.b_next = bufhead.b_prev = &bufhead;
142 bufcnt = MAXBUFSPACE / sblock.fs_bsize;
143 if (bufcnt < MINBUFS)
145 for (i = 0; i < bufcnt; i++) {
146 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
147 bufp = malloc((unsigned int)sblock.fs_bsize);
148 if (bp == NULL || bufp == NULL) {
151 errx(EEXIT, "cannot allocate buffer pool");
153 bp->b_un.b_buf = bufp;
154 bp->b_prev = &bufhead;
155 bp->b_next = bufhead.b_next;
156 bufhead.b_next->b_prev = bp;
160 bufhead.b_size = i; /* save number of buffers */
164 * Manage a cache of directory blocks.
167 getdatablk(ufs_daddr_t blkno, long size)
171 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
172 if (bp->b_bno == fsbtodb(&sblock, blkno))
174 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
175 if ((bp->b_flags & B_INUSE) == 0)
178 errx(EEXIT, "deadlocked buffer pool");
179 getblk(bp, blkno, size);
183 bp->b_prev->b_next = bp->b_next;
184 bp->b_next->b_prev = bp->b_prev;
185 bp->b_prev = &bufhead;
186 bp->b_next = bufhead.b_next;
187 bufhead.b_next->b_prev = bp;
189 bp->b_flags |= B_INUSE;
194 getblk(struct bufarea *bp, ufs_daddr_t blk, long size)
198 dblk = fsbtodb(&sblock, blk);
199 if (bp->b_bno != dblk) {
200 flush(fswritefd, bp);
202 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
209 flush(int fd, struct bufarea *bp)
216 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
217 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
221 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
224 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
225 bwrite(fswritefd, (char *)sblock.fs_csp + i,
226 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
227 sblock.fs_cssize - i < sblock.fs_bsize ?
228 sblock.fs_cssize - i : sblock.fs_bsize);
233 rwerror(char *mesg, ufs_daddr_t blk)
238 pfatal("CANNOT %s: BLK %d", mesg, blk);
239 if (reply("CONTINUE") == 0)
244 ckfini(int markclean)
246 struct bufarea *bp, *nbp;
247 int ofsmodified, cnt = 0;
253 flush(fswritefd, &sblk);
254 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
255 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
256 sblk.b_bno = SBOFF / dev_bsize;
258 flush(fswritefd, &sblk);
260 flush(fswritefd, &cgblk);
261 free(cgblk.b_un.b_buf);
262 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
264 flush(fswritefd, bp);
266 free(bp->b_un.b_buf);
269 if (bufhead.b_size != cnt)
270 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
272 if (sblock.fs_clean != markclean) {
273 sblock.fs_clean = markclean;
275 ofsmodified = fsmodified;
276 flush(fswritefd, &sblk);
277 fsmodified = ofsmodified;
279 printf("\n***** FILE SYSTEM MARKED %s *****\n",
280 markclean ? "CLEAN" : "DIRTY");
284 } else if (!preen && !markclean) {
285 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
289 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
290 totalreads, (int)(diskreads * 100 / totalreads));
296 bread(int fd, char *buf, ufs_daddr_t blk, long size)
304 if (lseek(fd, offset, 0) < 0)
305 rwerror("SEEK", blk);
306 else if (read(fd, buf, (int)size) == size)
308 rwerror("READ", blk);
309 if (lseek(fd, offset, 0) < 0)
310 rwerror("SEEK", blk);
312 memset(buf, 0, (size_t)size);
313 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
314 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
315 if (read(fd, cp, (int)secsize) != secsize) {
316 lseek(fd, offset + i + secsize, 0);
317 if (secsize != dev_bsize && dev_bsize != 1)
318 printf(" %ld (%ld),",
319 (blk * dev_bsize + i) / secsize,
320 blk + i / dev_bsize);
322 printf(" %ld,", blk + i / dev_bsize);
333 bwrite(int fd, char *buf, ufs_daddr_t blk, long size)
343 if (lseek(fd, offset, 0) < 0)
344 rwerror("SEEK", blk);
345 else if (write(fd, buf, (int)size) == size) {
350 rwerror("WRITE", blk);
351 if (lseek(fd, offset, 0) < 0)
352 rwerror("SEEK", blk);
353 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
354 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
355 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
356 lseek(fd, offset + i + dev_bsize, 0);
357 printf(" %ld,", blk + i / dev_bsize);
364 * allocate a data block with the specified number of fragments
369 int i, j, k, cg, baseblk;
370 struct cg *cgp = &cgrp;
372 if (frags <= 0 || frags > sblock.fs_frag)
374 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
375 for (j = 0; j <= sblock.fs_frag - frags; j++) {
378 for (k = 1; k < frags; k++)
379 if (testbmap(i + j + k))
385 cg = dtog(&sblock, i + j);
386 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
387 if (!cg_chkmagic(cgp))
388 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
389 baseblk = dtogd(&sblock, i + j);
390 for (k = 0; k < frags; k++) {
392 clrbit(cg_blksfree(cgp), baseblk + k);
395 if (frags == sblock.fs_frag)
396 cgp->cg_cs.cs_nbfree--;
398 cgp->cg_cs.cs_nffree -= frags;
407 * Free a previously allocated block
410 freeblk(ufs_daddr_t blkno, long frags)
412 struct inodesc idesc;
414 idesc.id_blkno = blkno;
415 idesc.id_numfrags = frags;
423 getpathname(char *namebuf, ufs1_ino_t curdir, ufs1_ino_t ino)
427 struct inodesc idesc;
430 if (curdir == ino && ino == ROOTINO) {
431 strcpy(namebuf, "/");
435 (inoinfo(curdir)->ino_state != DSTATE &&
436 inoinfo(curdir)->ino_state != DFOUND)) {
437 strcpy(namebuf, "?");
441 memset(&idesc, 0, sizeof(struct inodesc));
442 idesc.id_type = DATA;
443 idesc.id_fix = IGNORE;
444 cp = &namebuf[MAXPATHLEN - 1];
447 idesc.id_parent = curdir;
450 while (ino != ROOTINO) {
451 idesc.id_number = ino;
452 idesc.id_func = findino;
453 idesc.id_name = "..";
454 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
457 idesc.id_number = idesc.id_parent;
458 idesc.id_parent = ino;
459 idesc.id_func = findname;
460 idesc.id_name = namebuf;
461 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
463 len = strlen(namebuf);
465 memmove(cp, namebuf, (size_t)len);
467 if (cp < &namebuf[MAXNAMLEN])
469 ino = idesc.id_number;
474 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
487 * When preening, allow a single quit to signal
488 * a special exit after filesystem checks complete
489 * so that reboot sequence may be interrupted.
495 printf("returning to single-user after filesystem check\n");
497 signal(SIGQUIT, SIG_DFL);
501 * Ignore a single quit signal; wait and flush just in case.
502 * Used by child processes in preen.
510 signal(SIGQUIT, SIG_IGN);
511 signal(SIGQUIT, SIG_DFL);
522 * determine whether an inode should be fixed.
525 dofix(struct inodesc *idesc, char *msg)
528 switch (idesc->id_fix) {
531 if (idesc->id_type == DATA)
532 direrror(idesc->id_number, msg);
536 printf(" (SALVAGED)\n");
540 if (reply("SALVAGE") == 0) {
541 idesc->id_fix = NOFIX;
555 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
564 * An unexpected inconsistency occured.
565 * Die if preening or filesystem is running with soft dependency protocol,
566 * otherwise just print message and continue.
569 pfatal(const char *fmt, ...)
574 vfprintf(stderr, fmt, ap);
578 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
581 if (cdevname == NULL)
583 fprintf(stderr, "%s: ", cdevname);
584 vfprintf(stderr, fmt, ap);
586 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
587 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
593 * Pwarn just prints a message when not preening or running soft dependency
594 * protocol, or a warning (preceded by filename) when preening.
597 pwarn(const char *fmt, ...)
602 fprintf(stderr, "%s: ", cdevname);
603 vfprintf(stderr, fmt, ap);
608 * Stub for routines from kernel.
611 panic(const char *fmt, ...)
616 pfatal("INTERNAL INCONSISTENCY:");
617 vfprintf(stderr, fmt, ap);