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
36 static const char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
38 static const char rcsid[] =
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
45 #include <sys/disklabel.h>
47 #include <sys/disklabel.h>
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ffs/fs.h>
65 long diskreads, totalreads; /* Disk cache statistics */
68 ftypeok(union dinode *dp)
70 switch (DIP(dp, di_mode) & IFMT) {
83 printf("bad file type 0%o\n", DIP(dp, di_mode));
89 reply(const char *question)
95 pfatal("INTERNAL ERROR: GOT TO reply()");
96 persevere = !strcmp(question, "CONTINUE");
98 if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
99 printf("%s? no\n\n", question);
103 if (yflag || (persevere && nflag)) {
104 printf("%s? yes\n\n", question);
108 printf("%s? [yn] ", question);
109 (void) fflush(stdout);
111 while (c != '\n' && getc(stdin) != '\n') {
117 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
119 if (c == 'y' || c == 'Y')
126 * Look up state information for an inode.
131 static struct inostat unallocated = { USTATE, 0, 0 };
132 struct inostatlist *ilp;
136 errx(EEXIT, "inoinfo: inumber %d out of range", inum);
137 ilp = &inostathead[inum / sblock.fs_ipg];
138 iloff = inum % sblock.fs_ipg;
139 if (iloff >= ilp->il_numalloced)
140 return (&unallocated);
141 return (&ilp->il_stat[iloff]);
145 * Malloc buffers and set up cache.
154 pbp = pdirbp = (struct bufarea *)0;
155 bufp = malloc((unsigned int)sblock.fs_bsize);
157 errx(EEXIT, "cannot allocate buffer pool");
158 cgblk.b_un.b_buf = bufp;
160 bufhead.b_next = bufhead.b_prev = &bufhead;
161 bufcnt = MAXBUFSPACE / sblock.fs_bsize;
162 if (bufcnt < MINBUFS)
164 for (i = 0; i < bufcnt; i++) {
165 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
166 bufp = malloc((unsigned int)sblock.fs_bsize);
167 if (bp == NULL || bufp == NULL) {
170 errx(EEXIT, "cannot allocate buffer pool");
172 bp->b_un.b_buf = bufp;
173 bp->b_prev = &bufhead;
174 bp->b_next = bufhead.b_next;
175 bufhead.b_next->b_prev = bp;
179 bufhead.b_size = i; /* save number of buffers */
183 * Manage a cache of directory blocks.
186 getdatablk(ufs2_daddr_t blkno, long size)
190 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
191 if (bp->b_bno == fsbtodb(&sblock, blkno))
193 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
194 if ((bp->b_flags & B_INUSE) == 0)
197 errx(EEXIT, "deadlocked buffer pool");
198 getblk(bp, blkno, size);
201 bp->b_prev->b_next = bp->b_next;
202 bp->b_next->b_prev = bp->b_prev;
203 bp->b_prev = &bufhead;
204 bp->b_next = bufhead.b_next;
205 bufhead.b_next->b_prev = bp;
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 = bread(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 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
247 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
248 bwrite(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 = 0;
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 FILESYSTEM 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");
294 (void)close(fsreadfd);
297 flush(fswritefd, &sblk);
298 if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
299 sblk.b_bno != fsbtodb(&sblock, sblock.fs_sblockloc) &&
300 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
301 sblk.b_bno = fsbtodb(&sblock, sblock.fs_sblockloc);
303 flush(fswritefd, &sblk);
305 flush(fswritefd, &cgblk);
306 free(cgblk.b_un.b_buf);
307 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
309 flush(fswritefd, bp);
311 free(bp->b_un.b_buf);
314 if (bufhead.b_size != cnt)
315 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
316 pbp = pdirbp = (struct bufarea *)0;
317 if (cursnapshot == 0 && sblock.fs_clean != markclean) {
318 if ((sblock.fs_clean = markclean) != 0)
319 sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
321 ofsmodified = fsmodified;
322 flush(fswritefd, &sblk);
323 fsmodified = ofsmodified;
325 printf("\n***** FILE SYSTEM MARKED %s *****\n",
326 markclean ? "CLEAN" : "DIRTY");
330 } else if (!preen && !markclean) {
331 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
334 if (debug && totalreads > 0)
335 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
336 totalreads, (int)(diskreads * 100 / totalreads));
337 (void)close(fsreadfd);
338 (void)close(fswritefd);
342 bread(int fd, char *buf, ufs2_daddr_t blk, long size)
350 if (lseek(fd, offset, 0) < 0)
351 rwerror("SEEK BLK", blk);
352 else if (read(fd, buf, (int)size) == size)
354 rwerror("READ BLK", blk);
355 if (lseek(fd, offset, 0) < 0)
356 rwerror("SEEK BLK", blk);
358 memset(buf, 0, (size_t)size);
359 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
360 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
361 if (read(fd, cp, (int)secsize) != secsize) {
362 (void)lseek(fd, offset + i + secsize, 0);
363 if (secsize != dev_bsize && dev_bsize != 1)
364 printf(" %jd (%jd),",
365 (intmax_t)(blk * dev_bsize + i) / secsize,
366 (intmax_t)blk + i / dev_bsize);
368 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
379 bwrite(int fd, char *buf, ufs2_daddr_t blk, long size)
389 if (lseek(fd, offset, 0) < 0)
390 rwerror("SEEK BLK", blk);
391 else if (write(fd, buf, (int)size) == size) {
396 rwerror("WRITE BLK", blk);
397 if (lseek(fd, offset, 0) < 0)
398 rwerror("SEEK BLK", blk);
399 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
400 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
401 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
402 (void)lseek(fd, offset + i + dev_bsize, 0);
403 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
410 * allocate a data block with the specified number of fragments
415 int i, j, k, cg, baseblk;
416 struct cg *cgp = &cgrp;
418 if (frags <= 0 || frags > sblock.fs_frag)
420 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
421 for (j = 0; j <= sblock.fs_frag - frags; j++) {
424 for (k = 1; k < frags; k++)
425 if (testbmap(i + j + k))
431 cg = dtog(&sblock, i + j);
432 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
433 if (!cg_chkmagic(cgp))
434 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
435 baseblk = dtogd(&sblock, i + j);
436 for (k = 0; k < frags; k++) {
438 clrbit(cg_blksfree(cgp), baseblk + k);
441 if (frags == sblock.fs_frag)
442 cgp->cg_cs.cs_nbfree--;
444 cgp->cg_cs.cs_nffree -= frags;
453 * Free a previously allocated block
456 freeblk(ufs2_daddr_t blkno, long frags)
458 struct inodesc idesc;
460 idesc.id_blkno = blkno;
461 idesc.id_numfrags = frags;
462 (void)pass4check(&idesc);
469 getpathname(char *namebuf, ino_t curdir, ino_t ino)
473 struct inodesc idesc;
476 if (curdir == ino && ino == ROOTINO) {
477 (void)strcpy(namebuf, "/");
481 (inoinfo(curdir)->ino_state != DSTATE &&
482 inoinfo(curdir)->ino_state != DFOUND)) {
483 (void)strcpy(namebuf, "?");
487 memset(&idesc, 0, sizeof(struct inodesc));
488 idesc.id_type = DATA;
489 idesc.id_fix = IGNORE;
490 cp = &namebuf[MAXPATHLEN - 1];
493 idesc.id_parent = curdir;
496 while (ino != ROOTINO) {
497 idesc.id_number = ino;
498 idesc.id_func = findino;
499 idesc.id_name = strdup("..");
500 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
503 idesc.id_number = idesc.id_parent;
504 idesc.id_parent = ino;
505 idesc.id_func = findname;
506 idesc.id_name = namebuf;
507 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
509 len = strlen(namebuf);
511 memmove(cp, namebuf, (size_t)len);
513 if (cp < &namebuf[MAXNAMLEN])
515 ino = idesc.id_number;
520 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
524 catch(int sig __unused)
532 * When preening, allow a single quit to signal
533 * a special exit after file system checks complete
534 * so that reboot sequence may be interrupted.
537 catchquit(int sig __unused)
539 printf("returning to single-user after file system check\n");
541 (void)signal(SIGQUIT, SIG_DFL);
545 * determine whether an inode should be fixed.
548 dofix(struct inodesc *idesc, const char *msg)
551 switch (idesc->id_fix) {
554 if (idesc->id_type == DATA)
555 direrror(idesc->id_number, msg);
559 printf(" (SALVAGED)\n");
563 if (reply("SALVAGE") == 0) {
564 idesc->id_fix = NOFIX;
578 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
587 * An unexpected inconsistency occured.
588 * Die if preening or file system is running with soft dependency protocol,
589 * otherwise just print message and continue.
592 pfatal(const char *fmt, ...)
597 (void)vfprintf(stdout, fmt, ap);
600 (void)fprintf(stdout,
601 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
603 * Force foreground fsck to clean up inconsistency.
606 cmd.value = FS_NEEDSFSCK;
608 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
609 &cmd, sizeof cmd) == -1)
610 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
611 fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
617 if (cdevname == NULL)
618 cdevname = strdup("fsck");
619 (void)fprintf(stdout, "%s: ", cdevname);
620 (void)vfprintf(stdout, fmt, ap);
621 (void)fprintf(stdout,
622 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
623 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
625 * Force foreground fsck to clean up inconsistency.
628 cmd.value = FS_NEEDSFSCK;
630 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
631 &cmd, sizeof cmd) == -1)
632 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
639 * Pwarn just prints a message when not preening or running soft dependency
640 * protocol, or a warning (preceded by filename) when preening.
643 pwarn(const char *fmt, ...)
648 (void)fprintf(stdout, "%s: ", cdevname);
649 (void)vfprintf(stdout, fmt, ap);
654 * Stub for routines from kernel.
657 panic(const char *fmt, ...)
661 pfatal("INTERNAL INCONSISTENCY:");
662 (void)vfprintf(stdout, fmt, ap);