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>
43 #include <sys/disklabel.h>
44 #include <sys/ioctl.h>
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ufs/dir.h>
49 #include <ufs/ffs/fs.h>
63 static void slowio_start(void);
64 static void slowio_end(void);
66 long diskreads, totalreads; /* Disk cache statistics */
67 struct timeval slowio_starttime;
68 int slowio_delay_usec = 10000; /* Initial IO delay for background fsck */
70 static TAILQ_HEAD(buflist, bufarea) bufhead; /* head of buffer cache list */
71 static int numbufs; /* size of buffer cache */
74 ftypeok(union dinode *dp)
76 switch (DIP(dp, di_mode) & IFMT) {
89 printf("bad file type 0%o\n", DIP(dp, di_mode));
95 reply(const char *question)
101 pfatal("INTERNAL ERROR: GOT TO reply()");
102 persevere = !strcmp(question, "CONTINUE");
104 if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
105 printf("%s? no\n\n", question);
109 if (yflag || (persevere && nflag)) {
110 printf("%s? yes\n\n", question);
114 printf("%s? [yn] ", question);
115 (void) fflush(stdout);
117 while (c != '\n' && getc(stdin) != '\n') {
123 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
125 if (c == 'y' || c == 'Y')
132 * Look up state information for an inode.
137 static struct inostat unallocated = { USTATE, 0, 0 };
138 struct inostatlist *ilp;
142 errx(EEXIT, "inoinfo: inumber %ju out of range",
144 ilp = &inostathead[inum / sblock.fs_ipg];
145 iloff = inum % sblock.fs_ipg;
146 if (iloff >= ilp->il_numalloced)
147 return (&unallocated);
148 return (&ilp->il_stat[iloff]);
152 * Malloc buffers and set up cache.
161 pbp = pdirbp = (struct bufarea *)0;
162 bufp = malloc((unsigned int)sblock.fs_bsize);
164 errx(EEXIT, "cannot allocate buffer pool");
165 cgblk.b_un.b_buf = bufp;
167 TAILQ_INIT(&bufhead);
169 if (bufcnt < MINBUFS)
171 for (i = 0; i < bufcnt; i++) {
172 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
173 bufp = malloc((unsigned int)sblock.fs_bsize);
174 if (bp == NULL || bufp == NULL) {
177 errx(EEXIT, "cannot allocate buffer pool");
179 bp->b_un.b_buf = bufp;
180 TAILQ_INSERT_HEAD(&bufhead, bp, b_list);
183 numbufs = i; /* save number of buffers */
187 * Manage a cache of directory blocks.
190 getdatablk(ufs2_daddr_t blkno, long size)
194 TAILQ_FOREACH(bp, &bufhead, b_list)
195 if (bp->b_bno == fsbtodb(&sblock, blkno))
197 TAILQ_FOREACH_REVERSE(bp, &bufhead, buflist, b_list)
198 if ((bp->b_flags & B_INUSE) == 0)
201 errx(EEXIT, "deadlocked buffer pool");
202 getblk(bp, blkno, size);
205 TAILQ_REMOVE(&bufhead, bp, b_list);
206 TAILQ_INSERT_HEAD(&bufhead, bp, b_list);
207 bp->b_flags |= B_INUSE;
212 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
217 dblk = fsbtodb(&sblock, blk);
218 if (bp->b_bno != dblk) {
219 flush(fswritefd, bp);
221 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
228 flush(int fd, struct bufarea *bp)
236 pfatal("WRITING IN READ_ONLY MODE.\n");
240 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
241 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
242 (long long)bp->b_bno);
244 blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
247 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
248 blwrite(fswritefd, (char *)sblock.fs_csp + i,
249 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
250 sblock.fs_cssize - i < sblock.fs_bsize ?
251 sblock.fs_cssize - i : sblock.fs_bsize);
256 rwerror(const char *mesg, ufs2_daddr_t blk)
263 pfatal("CANNOT %s: %ld", mesg, (long)blk);
264 if (reply("CONTINUE") == 0)
269 ckfini(int markclean)
271 struct bufarea *bp, *nbp;
272 int ofsmodified, cnt;
276 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
277 cmd.value = FS_UNCLEAN;
278 cmd.size = markclean ? -1 : 1;
279 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
280 &cmd, sizeof cmd) == -1)
281 rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
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 if (debug && totalreads > 0)
294 printf("cache with %d buffers missed %ld of %ld (%d%%)\n",
295 numbufs, diskreads, totalreads,
296 (int)(diskreads * 100 / totalreads));
298 (void)close(fsreadfd);
301 flush(fswritefd, &sblk);
302 if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
303 sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
304 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
305 sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
307 flush(fswritefd, &sblk);
309 flush(fswritefd, &cgblk);
310 free(cgblk.b_un.b_buf);
312 TAILQ_FOREACH_REVERSE_SAFE(bp, &bufhead, buflist, b_list, nbp) {
313 TAILQ_REMOVE(&bufhead, bp, b_list);
315 flush(fswritefd, bp);
316 free(bp->b_un.b_buf);
320 errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
321 pbp = pdirbp = (struct bufarea *)0;
322 if (cursnapshot == 0 && sblock.fs_clean != markclean) {
323 if ((sblock.fs_clean = markclean) != 0) {
324 sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
325 sblock.fs_pendingblocks = 0;
326 sblock.fs_pendinginodes = 0;
329 ofsmodified = fsmodified;
330 flush(fswritefd, &sblk);
331 fsmodified = ofsmodified;
333 printf("\n***** FILE SYSTEM MARKED %s *****\n",
334 markclean ? "CLEAN" : "DIRTY");
340 printf("\n***** FILE SYSTEM IS CLEAN *****\n");
342 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
346 (void)close(fsreadfd);
347 (void)close(fswritefd);
351 blread(int fd, char *buf, ufs2_daddr_t blk, long size)
361 if (lseek(fd, offset, 0) < 0)
362 rwerror("SEEK BLK", blk);
363 else if (read(fd, buf, (int)size) == size) {
368 rwerror("READ BLK", blk);
369 if (lseek(fd, offset, 0) < 0)
370 rwerror("SEEK BLK", blk);
372 memset(buf, 0, (size_t)size);
373 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
374 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
375 if (read(fd, cp, (int)secsize) != secsize) {
376 (void)lseek(fd, offset + i + secsize, 0);
377 if (secsize != dev_bsize && dev_bsize != 1)
378 printf(" %jd (%jd),",
379 (intmax_t)(blk * dev_bsize + i) / secsize,
380 (intmax_t)blk + i / dev_bsize);
382 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
393 blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
403 if (lseek(fd, offset, 0) < 0)
404 rwerror("SEEK BLK", blk);
405 else if (write(fd, buf, size) == size) {
410 rwerror("WRITE BLK", blk);
411 if (lseek(fd, offset, 0) < 0)
412 rwerror("SEEK BLK", blk);
413 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
414 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
415 if (write(fd, cp, dev_bsize) != dev_bsize) {
416 (void)lseek(fd, offset + i + dev_bsize, 0);
417 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
424 blerase(int fd, ufs2_daddr_t blk, long size)
430 ioarg[0] = blk * dev_bsize;
432 ioctl(fd, DIOCGDELETE, ioarg);
433 /* we don't really care if we succeed or not */
438 * Verify cylinder group's magic number and other parameters. If the
439 * test fails, offer an option to rebuild the whole cylinder group.
442 check_cgmagic(int cg, struct cg *cgp)
446 * Extended cylinder group checks.
448 if (cg_chkmagic(cgp) &&
449 ((sblock.fs_magic == FS_UFS1_MAGIC &&
450 cgp->cg_old_niblk == sblock.fs_ipg &&
451 cgp->cg_ndblk <= sblock.fs_fpg &&
452 cgp->cg_old_ncyl <= sblock.fs_old_cpg) ||
453 (sblock.fs_magic == FS_UFS2_MAGIC &&
454 cgp->cg_niblk == sblock.fs_ipg &&
455 cgp->cg_ndblk <= sblock.fs_fpg &&
456 cgp->cg_initediblk <= sblock.fs_ipg))) {
459 pfatal("CYLINDER GROUP %d: BAD MAGIC NUMBER", cg);
460 if (!reply("REBUILD CYLINDER GROUP")) {
461 printf("YOU WILL NEED TO RERUN FSCK.\n");
466 * Zero out the cylinder group and then initialize critical fields.
467 * Bit maps and summaries will be recalculated by later passes.
469 memset(cgp, 0, (size_t)sblock.fs_cgsize);
470 cgp->cg_magic = CG_MAGIC;
472 cgp->cg_niblk = sblock.fs_ipg;
473 cgp->cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
474 sblock.fs_ipg : 2 * INOPB(&sblock);
475 if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
476 cgp->cg_ndblk = sblock.fs_fpg;
478 cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
479 cgp->cg_iusedoff = &cgp->cg_space[0] - (u_char *)(&cgp->cg_firstfield);
480 if (sblock.fs_magic == FS_UFS1_MAGIC) {
482 cgp->cg_initediblk = 0;
483 cgp->cg_old_ncyl = sblock.fs_old_cpg;
484 cgp->cg_old_niblk = sblock.fs_ipg;
485 cgp->cg_old_btotoff = cgp->cg_iusedoff;
486 cgp->cg_old_boff = cgp->cg_old_btotoff +
487 sblock.fs_old_cpg * sizeof(int32_t);
488 cgp->cg_iusedoff = cgp->cg_old_boff +
489 sblock.fs_old_cpg * sizeof(u_int16_t);
491 cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
492 cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
493 if (sblock.fs_contigsumsize > 0) {
494 cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
495 cgp->cg_clustersumoff =
496 roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
497 cgp->cg_clustersumoff -= sizeof(u_int32_t);
498 cgp->cg_clusteroff = cgp->cg_clustersumoff +
499 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
500 cgp->cg_nextfreeoff = cgp->cg_clusteroff +
501 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
508 * allocate a data block with the specified number of fragments
513 int i, j, k, cg, baseblk;
514 struct cg *cgp = &cgrp;
516 if (frags <= 0 || frags > sblock.fs_frag)
518 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
519 for (j = 0; j <= sblock.fs_frag - frags; j++) {
522 for (k = 1; k < frags; k++)
523 if (testbmap(i + j + k))
529 cg = dtog(&sblock, i + j);
530 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
531 if (!check_cgmagic(cg, cgp))
533 baseblk = dtogd(&sblock, i + j);
534 for (k = 0; k < frags; k++) {
536 clrbit(cg_blksfree(cgp), baseblk + k);
539 if (frags == sblock.fs_frag)
540 cgp->cg_cs.cs_nbfree--;
542 cgp->cg_cs.cs_nffree -= frags;
551 * Free a previously allocated block
554 freeblk(ufs2_daddr_t blkno, long frags)
556 struct inodesc idesc;
558 idesc.id_blkno = blkno;
559 idesc.id_numfrags = frags;
560 (void)pass4check(&idesc);
563 /* Slow down IO so as to leave some disk bandwidth for other processes */
568 /* Delay one in every 8 operations */
569 slowio_pollcnt = (slowio_pollcnt + 1) & 7;
570 if (slowio_pollcnt == 0) {
571 gettimeofday(&slowio_starttime, NULL);
581 if (slowio_pollcnt != 0)
584 /* Update the slowdown interval. */
585 gettimeofday(&tv, NULL);
586 delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
587 (tv.tv_usec - slowio_starttime.tv_usec);
590 if (delay_usec > 2500000)
591 delay_usec = 2500000;
592 slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
593 /* delay by 8 times the average IO delay */
594 if (slowio_delay_usec > 64)
595 usleep(slowio_delay_usec * 8);
602 getpathname(char *namebuf, ino_t curdir, ino_t ino)
606 struct inodesc idesc;
609 if (curdir == ino && ino == ROOTINO) {
610 (void)strcpy(namebuf, "/");
613 if (busy || !INO_IS_DVALID(curdir)) {
614 (void)strcpy(namebuf, "?");
618 memset(&idesc, 0, sizeof(struct inodesc));
619 idesc.id_type = DATA;
620 idesc.id_fix = IGNORE;
621 cp = &namebuf[MAXPATHLEN - 1];
624 idesc.id_parent = curdir;
627 while (ino != ROOTINO) {
628 idesc.id_number = ino;
629 idesc.id_func = findino;
630 idesc.id_name = strdup("..");
631 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
634 idesc.id_number = idesc.id_parent;
635 idesc.id_parent = ino;
636 idesc.id_func = findname;
637 idesc.id_name = namebuf;
638 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
640 len = strlen(namebuf);
642 memmove(cp, namebuf, (size_t)len);
644 if (cp < &namebuf[MAXNAMLEN])
646 ino = idesc.id_number;
651 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
655 catch(int sig __unused)
663 * When preening, allow a single quit to signal
664 * a special exit after file system checks complete
665 * so that reboot sequence may be interrupted.
668 catchquit(int sig __unused)
670 printf("returning to single-user after file system check\n");
672 (void)signal(SIGQUIT, SIG_DFL);
676 * determine whether an inode should be fixed.
679 dofix(struct inodesc *idesc, const char *msg)
682 switch (idesc->id_fix) {
685 if (idesc->id_type == DATA)
686 direrror(idesc->id_number, msg);
690 printf(" (SALVAGED)\n");
694 if (reply("SALVAGE") == 0) {
695 idesc->id_fix = NOFIX;
709 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
718 * An unexpected inconsistency occurred.
719 * Die if preening or file system is running with soft dependency protocol,
720 * otherwise just print message and continue.
723 pfatal(const char *fmt, ...)
728 (void)vfprintf(stdout, fmt, ap);
731 (void)fprintf(stdout,
732 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
734 * Force foreground fsck to clean up inconsistency.
737 cmd.value = FS_NEEDSFSCK;
739 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
740 &cmd, sizeof cmd) == -1)
741 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
742 fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
748 if (cdevname == NULL)
749 cdevname = strdup("fsck");
750 (void)fprintf(stdout, "%s: ", cdevname);
751 (void)vfprintf(stdout, fmt, ap);
752 (void)fprintf(stdout,
753 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
754 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
756 * Force foreground fsck to clean up inconsistency.
759 cmd.value = FS_NEEDSFSCK;
761 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
762 &cmd, sizeof cmd) == -1)
763 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
770 * Pwarn just prints a message when not preening or running soft dependency
771 * protocol, or a warning (preceded by filename) when preening.
774 pwarn(const char *fmt, ...)
779 (void)fprintf(stdout, "%s: ", cdevname);
780 (void)vfprintf(stdout, fmt, ap);
785 * Stub for routines from kernel.
788 panic(const char *fmt, ...)
792 pfatal("INTERNAL INCONSISTENCY:");
793 (void)vfprintf(stdout, fmt, ap);