10Sstevel@tonic-gate /*
2*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
30Sstevel@tonic-gate */
40Sstevel@tonic-gate
50Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
60Sstevel@tonic-gate /* All Rights Reserved */
70Sstevel@tonic-gate
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
100Sstevel@tonic-gate * All rights reserved.
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
130Sstevel@tonic-gate * provided that: (1) source distributions retain this entire copyright
140Sstevel@tonic-gate * notice and comment, and (2) distributions including binaries display
150Sstevel@tonic-gate * the following acknowledgement: ``This product includes software
160Sstevel@tonic-gate * developed by the University of California, Berkeley and its contributors''
170Sstevel@tonic-gate * in the documentation or other materials provided with the distribution
180Sstevel@tonic-gate * and in all advertising materials mentioning features or use of this
190Sstevel@tonic-gate * software. Neither the name of the University nor the names of its
200Sstevel@tonic-gate * contributors may be used to endorse or promote products derived
210Sstevel@tonic-gate * from this software without specific prior written permission.
220Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
230Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
240Sstevel@tonic-gate * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <fcntl.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <stdlib.h>
321053Smaheshvs #include <stdarg.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <malloc.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <sys/param.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/mntent.h>
420Sstevel@tonic-gate #include <sys/filio.h>
430Sstevel@tonic-gate #include <sys/vnode.h>
440Sstevel@tonic-gate #include <sys/mnttab.h>
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/stat.h>
470Sstevel@tonic-gate #include <sys/vfstab.h>
480Sstevel@tonic-gate #include <sys/sysmacros.h>
490Sstevel@tonic-gate #include <sys/fs/udf_volume.h>
500Sstevel@tonic-gate #include "fsck.h"
510Sstevel@tonic-gate #include <sys/lockfs.h>
520Sstevel@tonic-gate #include <locale.h>
530Sstevel@tonic-gate
540Sstevel@tonic-gate extern int32_t verifytag(struct tag *, uint32_t, struct tag *, int);
550Sstevel@tonic-gate extern char *tagerrs[];
560Sstevel@tonic-gate extern void maketag(struct tag *, struct tag *);
570Sstevel@tonic-gate extern char *hasvfsopt(struct vfstab *, char *);
580Sstevel@tonic-gate static struct bufarea *getdatablk(daddr_t, long);
590Sstevel@tonic-gate static struct bufarea *getblk(struct bufarea *, daddr_t, long);
600Sstevel@tonic-gate
610Sstevel@tonic-gate void flush(int32_t, struct bufarea *);
620Sstevel@tonic-gate int32_t bread(int32_t, char *, daddr_t, long);
630Sstevel@tonic-gate void bwrite(int, char *, daddr_t, long);
64*13093SRoger.Faulkner@Oracle.COM static int32_t getaline(FILE *, char *, int32_t);
651053Smaheshvs void errexit(char *, ...) __NORETURN;
660Sstevel@tonic-gate static long diskreads, totalreads; /* Disk cache statistics */
670Sstevel@tonic-gate offset_t llseek();
680Sstevel@tonic-gate extern unsigned int largefile_count;
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * An unexpected inconsistency occured.
720Sstevel@tonic-gate * Die if preening, otherwise just print message and continue.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate /* VARARGS1 */
750Sstevel@tonic-gate void
pfatal(char * fmt,...)761053Smaheshvs pfatal(char *fmt, ...)
770Sstevel@tonic-gate {
781053Smaheshvs va_list args;
791053Smaheshvs va_start(args, fmt);
800Sstevel@tonic-gate if (preen) {
810Sstevel@tonic-gate (void) printf("%s: ", devname);
821053Smaheshvs (void) vprintf(fmt, args);
830Sstevel@tonic-gate (void) printf("\n");
840Sstevel@tonic-gate (void) printf(
850Sstevel@tonic-gate gettext("%s: UNEXPECTED INCONSISTENCY; RUN fsck "
86*13093SRoger.Faulkner@Oracle.COM "MANUALLY.\n"), devname);
871053Smaheshvs va_end(args);
880Sstevel@tonic-gate exit(36);
890Sstevel@tonic-gate }
901053Smaheshvs (void) vprintf(fmt, args);
911053Smaheshvs va_end(args);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * Pwarn just prints a message when not preening,
960Sstevel@tonic-gate * or a warning (preceded by filename) when preening.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate /* VARARGS1 */
990Sstevel@tonic-gate void
pwarn(char * fmt,...)1001053Smaheshvs pwarn(char *fmt, ...)
1010Sstevel@tonic-gate {
1021053Smaheshvs va_list args;
1031053Smaheshvs va_start(args, fmt);
1040Sstevel@tonic-gate if (preen)
1050Sstevel@tonic-gate (void) printf("%s: ", devname);
1061053Smaheshvs (void) vprintf(fmt, args);
1071053Smaheshvs va_end(args);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /* VARARGS1 */
1120Sstevel@tonic-gate void
errexit(char * fmt,...)1131053Smaheshvs errexit(char *fmt, ...)
1140Sstevel@tonic-gate {
1151053Smaheshvs va_list args;
1161053Smaheshvs va_start(args, fmt);
1171053Smaheshvs (void) vprintf(fmt, args);
1181053Smaheshvs va_end(args);
1190Sstevel@tonic-gate exit(39);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate void
markbusy(daddr_t block,long count)1230Sstevel@tonic-gate markbusy(daddr_t block, long count)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate register int i;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate count = roundup(count, secsize) / secsize;
1280Sstevel@tonic-gate for (i = 0; i < count; i++, block++) {
1290Sstevel@tonic-gate if ((unsigned)block > part_len) {
1300Sstevel@tonic-gate pwarn(gettext("Block %lx out of range\n"), block);
1310Sstevel@tonic-gate break;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate if (testbusy(block))
1340Sstevel@tonic-gate pwarn(gettext("Dup block %lx\n"), block);
1350Sstevel@tonic-gate else {
1360Sstevel@tonic-gate n_blks++;
1370Sstevel@tonic-gate setbusy(block);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate void
printfree()1430Sstevel@tonic-gate printfree()
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate int i, startfree, endfree;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate startfree = -1;
1480Sstevel@tonic-gate for (i = 0; i < part_len; i++) {
1490Sstevel@tonic-gate if (!testbusy(i)) {
1500Sstevel@tonic-gate if (startfree <= 0)
1510Sstevel@tonic-gate startfree = i;
1520Sstevel@tonic-gate endfree = i;
1530Sstevel@tonic-gate } else if (startfree >= 0) {
1540Sstevel@tonic-gate (void) printf("free: %x-%x\n", startfree, endfree - 1);
1550Sstevel@tonic-gate startfree = -1;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate if (startfree >= 0) {
1590Sstevel@tonic-gate (void) printf("free: %x-%x\n", startfree, endfree);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate struct bufarea *
getfilentry(uint32_t block,int len)1640Sstevel@tonic-gate getfilentry(uint32_t block, int len)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate struct bufarea *bp;
1670Sstevel@tonic-gate struct file_entry *fp;
1680Sstevel@tonic-gate int err;
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate if (len > fsbsize) {
1710Sstevel@tonic-gate (void) printf(gettext("File entry at %x is too long "
172*13093SRoger.Faulkner@Oracle.COM "(%d bytes)\n"), block, len);
1730Sstevel@tonic-gate len = fsbsize;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate bp = getdatablk((daddr_t)(block + part_start), fsbsize);
1760Sstevel@tonic-gate if (bp->b_errs) {
1770Sstevel@tonic-gate bp->b_flags &= ~B_INUSE;
1780Sstevel@tonic-gate return (NULL);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate /* LINTED */
1810Sstevel@tonic-gate fp = (struct file_entry *)bp->b_un.b_buf;
1820Sstevel@tonic-gate err = verifytag(&fp->fe_tag, block, &fp->fe_tag, UD_FILE_ENTRY);
1830Sstevel@tonic-gate if (err) {
1840Sstevel@tonic-gate (void) printf(gettext("Tag error %s or bad file entry, "
185*13093SRoger.Faulkner@Oracle.COM "tag=%d\n"), tagerrs[err], fp->fe_tag.tag_id);
1860Sstevel@tonic-gate bp->b_flags &= ~B_INUSE;
1870Sstevel@tonic-gate return (NULL);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate return (bp);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate void
putfilentry(struct bufarea * bp)1930Sstevel@tonic-gate putfilentry(struct bufarea *bp)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate struct file_entry *fp;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate /* LINTED */
1980Sstevel@tonic-gate fp = (struct file_entry *)bp->b_un.b_buf;
1990Sstevel@tonic-gate maketag(&fp->fe_tag, &fp->fe_tag);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate int32_t
reply(char * question)2040Sstevel@tonic-gate reply(char *question)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate char line[80];
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if (preen)
2090Sstevel@tonic-gate pfatal(gettext("INTERNAL ERROR: GOT TO reply()"));
2100Sstevel@tonic-gate (void) printf("\n%s? ", question);
2110Sstevel@tonic-gate if (nflag || fswritefd < 0) {
2120Sstevel@tonic-gate (void) printf(gettext(" no\n\n"));
2130Sstevel@tonic-gate iscorrupt = 1; /* known to be corrupt */
2140Sstevel@tonic-gate return (0);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate if (yflag) {
2170Sstevel@tonic-gate (void) printf(gettext(" yes\n\n"));
2180Sstevel@tonic-gate return (1);
2190Sstevel@tonic-gate }
220*13093SRoger.Faulkner@Oracle.COM if (getaline(stdin, line, sizeof (line)) == EOF)
2210Sstevel@tonic-gate errexit("\n");
2220Sstevel@tonic-gate (void) printf("\n");
2230Sstevel@tonic-gate if (line[0] == 'y' || line[0] == 'Y')
2240Sstevel@tonic-gate return (1);
2250Sstevel@tonic-gate else {
2260Sstevel@tonic-gate iscorrupt = 1; /* known to be corrupt */
2270Sstevel@tonic-gate return (0);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate int32_t
getaline(FILE * fp,char * loc,int32_t maxlen)232*13093SRoger.Faulkner@Oracle.COM getaline(FILE *fp, char *loc, int32_t maxlen)
2330Sstevel@tonic-gate {
2341053Smaheshvs int n;
2350Sstevel@tonic-gate register char *p, *lastloc;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate p = loc;
2380Sstevel@tonic-gate lastloc = &p[maxlen-1];
2390Sstevel@tonic-gate while ((n = getc(fp)) != '\n') {
2400Sstevel@tonic-gate if (n == EOF)
2410Sstevel@tonic-gate return (EOF);
2420Sstevel@tonic-gate if (!isspace(n) && p < lastloc)
2430Sstevel@tonic-gate *p++ = n;
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate *p = 0;
2460Sstevel@tonic-gate return (p - loc);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * Malloc buffers and set up cache.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate void
bufinit()2520Sstevel@tonic-gate bufinit()
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate register struct bufarea *bp;
2550Sstevel@tonic-gate long bufcnt, i;
2560Sstevel@tonic-gate char *bufp;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate bufp = malloc((unsigned int)fsbsize);
2590Sstevel@tonic-gate if (bufp == 0)
2600Sstevel@tonic-gate errexit(gettext("cannot allocate buffer pool\n"));
2610Sstevel@tonic-gate bufhead.b_next = bufhead.b_prev = &bufhead;
2620Sstevel@tonic-gate bufcnt = MAXBUFSPACE / fsbsize;
2630Sstevel@tonic-gate if (bufcnt < MINBUFS)
2640Sstevel@tonic-gate bufcnt = MINBUFS;
2650Sstevel@tonic-gate for (i = 0; i < bufcnt; i++) {
2660Sstevel@tonic-gate bp = (struct bufarea *)malloc(sizeof (struct bufarea));
2670Sstevel@tonic-gate bufp = malloc((unsigned int)fsbsize);
2680Sstevel@tonic-gate if (bp == NULL || bufp == NULL) {
2690Sstevel@tonic-gate if (i >= MINBUFS)
2700Sstevel@tonic-gate break;
2710Sstevel@tonic-gate errexit(gettext("cannot allocate buffer pool\n"));
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate bp->b_un.b_buf = bufp;
2740Sstevel@tonic-gate bp->b_prev = &bufhead;
2750Sstevel@tonic-gate bp->b_next = bufhead.b_next;
2760Sstevel@tonic-gate bufhead.b_next->b_prev = bp;
2770Sstevel@tonic-gate bufhead.b_next = bp;
2780Sstevel@tonic-gate initbarea(bp);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate bufhead.b_size = i; /* save number of buffers */
2810Sstevel@tonic-gate pbp = pdirbp = NULL;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate * Manage a cache of directory blocks.
2860Sstevel@tonic-gate */
2870Sstevel@tonic-gate static struct bufarea *
getdatablk(daddr_t blkno,long size)2880Sstevel@tonic-gate getdatablk(daddr_t blkno, long size)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate register struct bufarea *bp;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
2930Sstevel@tonic-gate if (bp->b_bno == fsbtodb(blkno))
2940Sstevel@tonic-gate goto foundit;
2950Sstevel@tonic-gate for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
2960Sstevel@tonic-gate if ((bp->b_flags & B_INUSE) == 0)
2970Sstevel@tonic-gate break;
2980Sstevel@tonic-gate if (bp == &bufhead)
2990Sstevel@tonic-gate errexit(gettext("deadlocked buffer pool\n"));
3000Sstevel@tonic-gate (void) getblk(bp, blkno, size);
3010Sstevel@tonic-gate /* fall through */
3020Sstevel@tonic-gate foundit:
3030Sstevel@tonic-gate totalreads++;
3040Sstevel@tonic-gate bp->b_prev->b_next = bp->b_next;
3050Sstevel@tonic-gate bp->b_next->b_prev = bp->b_prev;
3060Sstevel@tonic-gate bp->b_prev = &bufhead;
3070Sstevel@tonic-gate bp->b_next = bufhead.b_next;
3080Sstevel@tonic-gate bufhead.b_next->b_prev = bp;
3090Sstevel@tonic-gate bufhead.b_next = bp;
3100Sstevel@tonic-gate bp->b_flags |= B_INUSE;
3110Sstevel@tonic-gate return (bp);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate static struct bufarea *
getblk(struct bufarea * bp,daddr_t blk,long size)3150Sstevel@tonic-gate getblk(struct bufarea *bp, daddr_t blk, long size)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate daddr_t dblk;
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate dblk = fsbtodb(blk);
3200Sstevel@tonic-gate if (bp->b_bno == dblk)
3210Sstevel@tonic-gate return (bp);
3220Sstevel@tonic-gate flush(fswritefd, bp);
3230Sstevel@tonic-gate diskreads++;
3240Sstevel@tonic-gate bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
3250Sstevel@tonic-gate bp->b_bno = dblk;
3260Sstevel@tonic-gate bp->b_size = size;
3270Sstevel@tonic-gate return (bp);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate void
flush(int32_t fd,struct bufarea * bp)3310Sstevel@tonic-gate flush(int32_t fd, struct bufarea *bp)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate if (!bp->b_dirty)
3340Sstevel@tonic-gate return;
3350Sstevel@tonic-gate if (bp->b_errs != 0)
3360Sstevel@tonic-gate pfatal(gettext("WRITING ZERO'ED BLOCK %d TO DISK\n"),
337*13093SRoger.Faulkner@Oracle.COM bp->b_bno);
3380Sstevel@tonic-gate bp->b_dirty = 0;
3390Sstevel@tonic-gate bp->b_errs = 0;
3400Sstevel@tonic-gate bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate static void
rwerror(char * mesg,daddr_t blk)3440Sstevel@tonic-gate rwerror(char *mesg, daddr_t blk)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate if (preen == 0)
3480Sstevel@tonic-gate (void) printf("\n");
3490Sstevel@tonic-gate pfatal(gettext("CANNOT %s: BLK %ld"), mesg, blk);
3500Sstevel@tonic-gate if (reply(gettext("CONTINUE")) == 0)
3510Sstevel@tonic-gate errexit(gettext("Program terminated\n"));
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate void
ckfini()3550Sstevel@tonic-gate ckfini()
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate struct bufarea *bp, *nbp;
3580Sstevel@tonic-gate int cnt = 0;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
3610Sstevel@tonic-gate cnt++;
3620Sstevel@tonic-gate flush(fswritefd, bp);
3630Sstevel@tonic-gate nbp = bp->b_prev;
3640Sstevel@tonic-gate free(bp->b_un.b_buf);
3650Sstevel@tonic-gate free((char *)bp);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate pbp = pdirbp = NULL;
3680Sstevel@tonic-gate if (bufhead.b_size != cnt)
3690Sstevel@tonic-gate errexit(gettext("Panic: lost %d buffers\n"),
370*13093SRoger.Faulkner@Oracle.COM bufhead.b_size - cnt);
3710Sstevel@tonic-gate if (debug)
3720Sstevel@tonic-gate (void) printf("cache missed %ld of %ld (%ld%%)\n",
3730Sstevel@tonic-gate diskreads, totalreads,
3740Sstevel@tonic-gate totalreads ? diskreads * 100 / totalreads : 0);
3750Sstevel@tonic-gate (void) close(fsreadfd);
3760Sstevel@tonic-gate (void) close(fswritefd);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate int32_t
bread(int fd,char * buf,daddr_t blk,long size)3800Sstevel@tonic-gate bread(int fd, char *buf, daddr_t blk, long size)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate char *cp;
3830Sstevel@tonic-gate int i, errs;
3840Sstevel@tonic-gate offset_t offset = ldbtob(blk);
3850Sstevel@tonic-gate offset_t addr;
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate if (llseek(fd, offset, 0) < 0)
3880Sstevel@tonic-gate rwerror(gettext("SEEK"), blk);
3890Sstevel@tonic-gate else if (read(fd, buf, (int)size) == size)
3900Sstevel@tonic-gate return (0);
3910Sstevel@tonic-gate rwerror(gettext("READ"), blk);
3920Sstevel@tonic-gate if (llseek(fd, offset, 0) < 0)
3930Sstevel@tonic-gate rwerror(gettext("SEEK"), blk);
3940Sstevel@tonic-gate errs = 0;
3950Sstevel@tonic-gate bzero(buf, (int)size);
3960Sstevel@tonic-gate pwarn(gettext("THE FOLLOWING SECTORS COULD NOT BE READ:"));
3970Sstevel@tonic-gate for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
3980Sstevel@tonic-gate addr = ldbtob(blk + i);
3990Sstevel@tonic-gate if (llseek(fd, addr, SEEK_CUR) < 0 ||
4000Sstevel@tonic-gate read(fd, cp, (int)secsize) < 0) {
4010Sstevel@tonic-gate (void) printf(" %ld", blk + i);
4020Sstevel@tonic-gate errs++;
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate (void) printf("\n");
4060Sstevel@tonic-gate return (errs);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate void
bwrite(int fd,char * buf,daddr_t blk,long size)4100Sstevel@tonic-gate bwrite(int fd, char *buf, daddr_t blk, long size)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate int i, n;
4130Sstevel@tonic-gate char *cp;
4140Sstevel@tonic-gate offset_t offset = ldbtob(blk);
4150Sstevel@tonic-gate offset_t addr;
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate if (fd < 0)
4180Sstevel@tonic-gate return;
4190Sstevel@tonic-gate if (llseek(fd, offset, 0) < 0)
4200Sstevel@tonic-gate rwerror(gettext("SEEK"), blk);
4210Sstevel@tonic-gate else if (write(fd, buf, (int)size) == size) {
4220Sstevel@tonic-gate fsmodified = 1;
4230Sstevel@tonic-gate return;
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate rwerror(gettext("WRITE"), blk);
4260Sstevel@tonic-gate if (llseek(fd, offset, 0) < 0)
4270Sstevel@tonic-gate rwerror(gettext("SEEK"), blk);
4280Sstevel@tonic-gate pwarn(gettext("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"));
4290Sstevel@tonic-gate for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
4300Sstevel@tonic-gate n = 0;
4310Sstevel@tonic-gate addr = ldbtob(blk + i);
4320Sstevel@tonic-gate if (llseek(fd, addr, SEEK_CUR) < 0 ||
4330Sstevel@tonic-gate (n = write(fd, cp, DEV_BSIZE)) < 0) {
4340Sstevel@tonic-gate (void) printf(" %ld", blk + i);
4350Sstevel@tonic-gate } else if (n > 0) {
4360Sstevel@tonic-gate fsmodified = 1;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate (void) printf("\n");
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate void
catch()4440Sstevel@tonic-gate catch()
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate ckfini();
4470Sstevel@tonic-gate exit(37);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate * When preening, allow a single quit to signal
4520Sstevel@tonic-gate * a special exit after filesystem checks complete
4530Sstevel@tonic-gate * so that reboot sequence may be interrupted.
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate void
catchquit()4560Sstevel@tonic-gate catchquit()
4570Sstevel@tonic-gate {
4581053Smaheshvs extern int returntosingle;
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate (void) printf(gettext("returning to single-user after filesystem "
461*13093SRoger.Faulkner@Oracle.COM "check\n"));
4620Sstevel@tonic-gate returntosingle = 1;
4630Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_DFL);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate * determine whether an inode should be fixed.
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate /* ARGSUSED1 */
4700Sstevel@tonic-gate int32_t
dofix(struct inodesc * idesc,char * msg)4710Sstevel@tonic-gate dofix(struct inodesc *idesc, char *msg)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate switch (idesc->id_fix) {
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate case DONTKNOW:
4770Sstevel@tonic-gate pwarn(msg);
4780Sstevel@tonic-gate if (preen) {
4790Sstevel@tonic-gate (void) printf(gettext(" (SALVAGED)\n"));
4800Sstevel@tonic-gate idesc->id_fix = FIX;
4810Sstevel@tonic-gate return (ALTERED);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate if (reply(gettext("SALVAGE")) == 0) {
4840Sstevel@tonic-gate idesc->id_fix = NOFIX;
4850Sstevel@tonic-gate return (0);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate idesc->id_fix = FIX;
4880Sstevel@tonic-gate return (ALTERED);
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate case FIX:
4910Sstevel@tonic-gate return (ALTERED);
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate case NOFIX:
4940Sstevel@tonic-gate return (0);
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate default:
4970Sstevel@tonic-gate errexit(gettext("UNKNOWN INODESC FIX MODE %d\n"),
498*13093SRoger.Faulkner@Oracle.COM idesc->id_fix);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate /* NOTREACHED */
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate * Check to see if unraw version of name is already mounted.
5050Sstevel@tonic-gate * Since we do not believe /etc/mnttab, we stat the mount point
5060Sstevel@tonic-gate * to see if it is really looks mounted.
5070Sstevel@tonic-gate */
5081053Smaheshvs int
mounted(char * name)5090Sstevel@tonic-gate mounted(char *name)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate int found = 0;
5120Sstevel@tonic-gate struct mnttab mnt;
5130Sstevel@tonic-gate FILE *mnttab;
5140Sstevel@tonic-gate struct stat device_stat, mount_stat;
5150Sstevel@tonic-gate char *blkname, *unrawname();
5160Sstevel@tonic-gate int err;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate mnttab = fopen(MNTTAB, "r");
5190Sstevel@tonic-gate if (mnttab == NULL) {
5200Sstevel@tonic-gate (void) printf(gettext("can't open %s\n"), MNTTAB);
5210Sstevel@tonic-gate return (0);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate blkname = unrawname(name);
5240Sstevel@tonic-gate while ((getmntent(mnttab, &mnt)) == NULL) {
5250Sstevel@tonic-gate if (strcmp(mnt.mnt_fstype, MNTTYPE_UDFS) != 0) {
5260Sstevel@tonic-gate continue;
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate if (strcmp(blkname, mnt.mnt_special) == 0) {
5290Sstevel@tonic-gate err = stat(mnt.mnt_mountp, &mount_stat);
5300Sstevel@tonic-gate err |= stat(mnt.mnt_special, &device_stat);
5310Sstevel@tonic-gate if (err < 0)
5320Sstevel@tonic-gate continue;
5330Sstevel@tonic-gate if (device_stat.st_rdev == mount_stat.st_dev) {
5340Sstevel@tonic-gate (void) strncpy(mnt.mnt_mountp, mountpoint,
535*13093SRoger.Faulkner@Oracle.COM sizeof (mountpoint));
5360Sstevel@tonic-gate if (hasmntopt(&mnt, MNTOPT_RO) != 0)
5370Sstevel@tonic-gate found = 2; /* mounted as RO */
5380Sstevel@tonic-gate else
5390Sstevel@tonic-gate found = 1; /* mounted as R/W */
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate break;
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate (void) fclose(mnttab);
5450Sstevel@tonic-gate return (found);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate /*
5490Sstevel@tonic-gate * Check to see if name corresponds to an entry in vfstab, and that the entry
5500Sstevel@tonic-gate * does not have option ro.
5510Sstevel@tonic-gate */
5521053Smaheshvs int
writable(char * name)5530Sstevel@tonic-gate writable(char *name)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate int rw = 1;
5560Sstevel@tonic-gate struct vfstab vfsbuf;
5570Sstevel@tonic-gate FILE *vfstab;
5580Sstevel@tonic-gate char *blkname, *unrawname();
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate vfstab = fopen(VFSTAB, "r");
5610Sstevel@tonic-gate if (vfstab == NULL) {
5620Sstevel@tonic-gate (void) printf(gettext("can't open %s\n"), VFSTAB);
5630Sstevel@tonic-gate return (1);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate blkname = unrawname(name);
5660Sstevel@tonic-gate if ((getvfsspec(vfstab, &vfsbuf, blkname) == 0) &&
5670Sstevel@tonic-gate (vfsbuf.vfs_fstype != NULL) &&
5680Sstevel@tonic-gate (strcmp(vfsbuf.vfs_fstype, MNTTYPE_UDFS) == 0) &&
5690Sstevel@tonic-gate (hasvfsopt(&vfsbuf, MNTOPT_RO))) {
5700Sstevel@tonic-gate rw = 0;
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate (void) fclose(vfstab);
5730Sstevel@tonic-gate return (rw);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate * print out clean info
5780Sstevel@tonic-gate */
5790Sstevel@tonic-gate void
printclean()5800Sstevel@tonic-gate printclean()
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate char *s;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate switch (lvintp->lvid_int_type) {
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate case LVI_CLOSE:
5870Sstevel@tonic-gate s = gettext("clean");
5880Sstevel@tonic-gate break;
5890Sstevel@tonic-gate
5900Sstevel@tonic-gate case LVI_OPEN:
5910Sstevel@tonic-gate s = gettext("active");
5920Sstevel@tonic-gate break;
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate default:
5950Sstevel@tonic-gate s = gettext("unknown");
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate if (preen)
5990Sstevel@tonic-gate pwarn(gettext("is %s.\n"), s);
6000Sstevel@tonic-gate else
6010Sstevel@tonic-gate (void) printf("** %s is %s.\n", devname, s);
6020Sstevel@tonic-gate }
603