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 * 4. 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
32 static const char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
42 #include <sys/disklabel.h>
44 #include <sys/disklabel.h>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ufs/dir.h>
48 #include <ufs/ffs/fs.h>
62 static void slowio_start(void);
63 static void slowio_end(void);
65 long diskreads, totalreads; /* Disk cache statistics */
66 struct timeval slowio_starttime;
67 int slowio_delay_usec = 10000; /* Initial IO delay for background fsck */
71 ftypeok(union dinode *dp)
73 switch (DIP(dp, di_mode) & IFMT) {
86 printf("bad file type 0%o\n", DIP(dp, di_mode));
92 reply(const char *question)
98 pfatal("INTERNAL ERROR: GOT TO reply()");
99 persevere = !strcmp(question, "CONTINUE");
101 if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
102 printf("%s? no\n\n", question);
106 if (yflag || (persevere && nflag)) {
107 printf("%s? yes\n\n", question);
111 printf("%s? [yn] ", question);
112 (void) fflush(stdout);
114 while (c != '\n' && getc(stdin) != '\n') {
120 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
122 if (c == 'y' || c == 'Y')
129 * Look up state information for an inode.
134 static struct inostat unallocated = { USTATE, 0, 0 };
135 struct inostatlist *ilp;
139 errx(EEXIT, "inoinfo: inumber %d out of range", inum);
140 ilp = &inostathead[inum / sblock.fs_ipg];
141 iloff = inum % sblock.fs_ipg;
142 if (iloff >= ilp->il_numalloced)
143 return (&unallocated);
144 return (&ilp->il_stat[iloff]);
148 * Malloc buffers and set up cache.
157 pbp = pdirbp = (struct bufarea *)0;
158 bufp = malloc((unsigned int)sblock.fs_bsize);
160 errx(EEXIT, "cannot allocate buffer pool");
161 cgblk.b_un.b_buf = bufp;
163 bufhead.b_next = bufhead.b_prev = &bufhead;
164 bufcnt = MAXBUFSPACE / sblock.fs_bsize;
165 if (bufcnt < MINBUFS)
167 for (i = 0; i < bufcnt; i++) {
168 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
169 bufp = malloc((unsigned int)sblock.fs_bsize);
170 if (bp == NULL || bufp == NULL) {
173 errx(EEXIT, "cannot allocate buffer pool");
175 bp->b_un.b_buf = bufp;
176 bp->b_prev = &bufhead;
177 bp->b_next = bufhead.b_next;
178 bufhead.b_next->b_prev = bp;
182 bufhead.b_size = i; /* save number of buffers */
186 * Manage a cache of directory blocks.
189 getdatablk(ufs2_daddr_t blkno, long size)
193 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
194 if (bp->b_bno == fsbtodb(&sblock, blkno))
196 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
197 if ((bp->b_flags & B_INUSE) == 0)
200 errx(EEXIT, "deadlocked buffer pool");
201 getblk(bp, blkno, size);
204 bp->b_prev->b_next = bp->b_next;
205 bp->b_next->b_prev = bp->b_prev;
206 bp->b_prev = &bufhead;
207 bp->b_next = bufhead.b_next;
208 bufhead.b_next->b_prev = bp;
210 bp->b_flags |= B_INUSE;
215 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
220 dblk = fsbtodb(&sblock, blk);
221 if (bp->b_bno != dblk) {
222 flush(fswritefd, bp);
224 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
231 flush(int fd, struct bufarea *bp)
239 pfatal("WRITING IN READ_ONLY MODE.\n");
243 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
244 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
245 (long long)bp->b_bno);
247 blwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
250 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
251 blwrite(fswritefd, (char *)sblock.fs_csp + i,
252 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
253 sblock.fs_cssize - i < sblock.fs_bsize ?
254 sblock.fs_cssize - i : sblock.fs_bsize);
259 rwerror(const char *mesg, ufs2_daddr_t blk)
266 pfatal("CANNOT %s: %ld", mesg, (long)blk);
267 if (reply("CONTINUE") == 0)
272 ckfini(int markclean)
274 struct bufarea *bp, *nbp;
275 int ofsmodified, cnt = 0;
279 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
280 cmd.value = FS_UNCLEAN;
281 cmd.size = markclean ? -1 : 1;
282 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
283 &cmd, sizeof cmd) == -1)
284 rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
286 printf("\n***** FILE SYSTEM MARKED %s *****\n",
287 markclean ? "CLEAN" : "DIRTY");
291 } else if (!preen && !markclean) {
292 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
297 (void)close(fsreadfd);
300 flush(fswritefd, &sblk);
301 if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
302 sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
303 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
304 sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
306 flush(fswritefd, &sblk);
308 flush(fswritefd, &cgblk);
309 free(cgblk.b_un.b_buf);
310 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
312 flush(fswritefd, bp);
314 free(bp->b_un.b_buf);
317 if (bufhead.b_size != cnt)
318 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
319 pbp = pdirbp = (struct bufarea *)0;
320 if (cursnapshot == 0 && sblock.fs_clean != markclean) {
321 if ((sblock.fs_clean = markclean) != 0) {
322 sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
323 sblock.fs_pendingblocks = 0;
324 sblock.fs_pendinginodes = 0;
327 ofsmodified = fsmodified;
328 flush(fswritefd, &sblk);
329 fsmodified = ofsmodified;
331 printf("\n***** FILE SYSTEM MARKED %s *****\n",
332 markclean ? "CLEAN" : "DIRTY");
336 } else if (!preen && !markclean) {
337 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
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);
348 blread(int fd, char *buf, ufs2_daddr_t blk, long size)
358 if (lseek(fd, offset, 0) < 0)
359 rwerror("SEEK BLK", blk);
360 else if (read(fd, buf, (int)size) == size) {
365 rwerror("READ BLK", blk);
366 if (lseek(fd, offset, 0) < 0)
367 rwerror("SEEK BLK", blk);
369 memset(buf, 0, (size_t)size);
370 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
371 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
372 if (read(fd, cp, (int)secsize) != secsize) {
373 (void)lseek(fd, offset + i + secsize, 0);
374 if (secsize != dev_bsize && dev_bsize != 1)
375 printf(" %jd (%jd),",
376 (intmax_t)(blk * dev_bsize + i) / secsize,
377 (intmax_t)blk + i / dev_bsize);
379 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
390 blwrite(int fd, char *buf, ufs2_daddr_t blk, long size)
400 if (lseek(fd, offset, 0) < 0)
401 rwerror("SEEK BLK", blk);
402 else if (write(fd, buf, (int)size) == size) {
407 rwerror("WRITE BLK", blk);
408 if (lseek(fd, offset, 0) < 0)
409 rwerror("SEEK BLK", blk);
410 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
411 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
412 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
413 (void)lseek(fd, offset + i + dev_bsize, 0);
414 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
421 * Check cg's magic number. If catastrophic mode is enabled and the cg's
422 * magic number is bad, offer an option to clear the whole cg.
425 check_cgmagic(int cg, struct cg *cgp)
428 if (!cg_chkmagic(cgp)) {
429 pwarn("CG %d: BAD MAGIC NUMBER\n", cg);
430 if (catastrophicflag) {
431 if (reply("CLEAR CG")) {
432 memset(cgp, 0, (size_t)sblock.fs_cgsize);
433 cgp->cg_initediblk = sblock.fs_ipg;
434 cgp->cg_old_niblk = sblock.fs_ipg;
435 cgp->cg_old_ncyl = sblock.fs_old_cpg;
437 cgp->cg_niblk = sblock.fs_ipg;
438 cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
439 cgp->cg_magic = CG_MAGIC;
441 printf("PLEASE RERUN FSCK.\n");
445 printf("YOU MAY NEED TO RERUN FSCK WITH -C IF IT CRASHED.\n");
450 * allocate a data block with the specified number of fragments
455 int i, j, k, cg, baseblk;
456 struct cg *cgp = &cgrp;
458 if (frags <= 0 || frags > sblock.fs_frag)
460 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
461 for (j = 0; j <= sblock.fs_frag - frags; j++) {
464 for (k = 1; k < frags; k++)
465 if (testbmap(i + j + k))
471 cg = dtog(&sblock, i + j);
472 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
473 check_cgmagic(cg, cgp);
474 baseblk = dtogd(&sblock, i + j);
475 for (k = 0; k < frags; k++) {
477 clrbit(cg_blksfree(cgp), baseblk + k);
480 if (frags == sblock.fs_frag)
481 cgp->cg_cs.cs_nbfree--;
483 cgp->cg_cs.cs_nffree -= frags;
492 * Free a previously allocated block
495 freeblk(ufs2_daddr_t blkno, long frags)
497 struct inodesc idesc;
499 idesc.id_blkno = blkno;
500 idesc.id_numfrags = frags;
501 (void)pass4check(&idesc);
504 /* Slow down IO so as to leave some disk bandwidth for other processes */
509 /* Delay one in every 8 operations */
510 slowio_pollcnt = (slowio_pollcnt + 1) & 7;
511 if (slowio_pollcnt == 0) {
512 gettimeofday(&slowio_starttime, NULL);
522 if (slowio_pollcnt != 0)
525 /* Update the slowdown interval. */
526 gettimeofday(&tv, NULL);
527 delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
528 (tv.tv_usec - slowio_starttime.tv_usec);
531 if (delay_usec > 2500000)
532 delay_usec = 2500000;
533 slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
534 /* delay by 8 times the average IO delay */
535 if (slowio_delay_usec > 64)
536 usleep(slowio_delay_usec * 8);
543 getpathname(char *namebuf, ino_t curdir, ino_t ino)
547 struct inodesc idesc;
550 if (curdir == ino && ino == ROOTINO) {
551 (void)strcpy(namebuf, "/");
554 if (busy || !INO_IS_DVALID(curdir)) {
555 (void)strcpy(namebuf, "?");
559 memset(&idesc, 0, sizeof(struct inodesc));
560 idesc.id_type = DATA;
561 idesc.id_fix = IGNORE;
562 cp = &namebuf[MAXPATHLEN - 1];
565 idesc.id_parent = curdir;
568 while (ino != ROOTINO) {
569 idesc.id_number = ino;
570 idesc.id_func = findino;
571 idesc.id_name = strdup("..");
572 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
575 idesc.id_number = idesc.id_parent;
576 idesc.id_parent = ino;
577 idesc.id_func = findname;
578 idesc.id_name = namebuf;
579 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
581 len = strlen(namebuf);
583 memmove(cp, namebuf, (size_t)len);
585 if (cp < &namebuf[MAXNAMLEN])
587 ino = idesc.id_number;
592 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
596 catch(int sig __unused)
604 * When preening, allow a single quit to signal
605 * a special exit after file system checks complete
606 * so that reboot sequence may be interrupted.
609 catchquit(int sig __unused)
611 printf("returning to single-user after file system check\n");
613 (void)signal(SIGQUIT, SIG_DFL);
617 * determine whether an inode should be fixed.
620 dofix(struct inodesc *idesc, const char *msg)
623 switch (idesc->id_fix) {
626 if (idesc->id_type == DATA)
627 direrror(idesc->id_number, msg);
631 printf(" (SALVAGED)\n");
635 if (reply("SALVAGE") == 0) {
636 idesc->id_fix = NOFIX;
650 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
659 * An unexpected inconsistency occured.
660 * Die if preening or file system is running with soft dependency protocol,
661 * otherwise just print message and continue.
664 pfatal(const char *fmt, ...)
669 (void)vfprintf(stdout, fmt, ap);
672 (void)fprintf(stdout,
673 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
675 * Force foreground fsck to clean up inconsistency.
678 cmd.value = FS_NEEDSFSCK;
680 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
681 &cmd, sizeof cmd) == -1)
682 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
683 fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
689 if (cdevname == NULL)
690 cdevname = strdup("fsck");
691 (void)fprintf(stdout, "%s: ", cdevname);
692 (void)vfprintf(stdout, fmt, ap);
693 (void)fprintf(stdout,
694 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
695 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
697 * Force foreground fsck to clean up inconsistency.
700 cmd.value = FS_NEEDSFSCK;
702 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
703 &cmd, sizeof cmd) == -1)
704 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
711 * Pwarn just prints a message when not preening or running soft dependency
712 * protocol, or a warning (preceded by filename) when preening.
715 pwarn(const char *fmt, ...)
720 (void)fprintf(stdout, "%s: ", cdevname);
721 (void)vfprintf(stdout, fmt, ap);
726 * Stub for routines from kernel.
729 panic(const char *fmt, ...)
733 pfatal("INTERNAL INCONSISTENCY:");
734 (void)vfprintf(stdout, fmt, ap);