xref: /illumos-gate/usr/src/cmd/fs.d/ufs/fsck/utilities.c (revision 48bbca81)
17c478bd9Sstevel@tonic-gate /*
223a1cceaSRoger A. Faulkner  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
147c478bd9Sstevel@tonic-gate  * provided that: (1) source distributions retain this entire copyright
157c478bd9Sstevel@tonic-gate  * notice and comment, and (2) distributions including binaries display
167c478bd9Sstevel@tonic-gate  * the following acknowledgement:  ``This product includes software
177c478bd9Sstevel@tonic-gate  * developed by the University of California, Berkeley and its contributors''
187c478bd9Sstevel@tonic-gate  * in the documentation or other materials provided with the distribution
197c478bd9Sstevel@tonic-gate  * and in all advertising materials mentioning features or use of this
207c478bd9Sstevel@tonic-gate  * software. Neither the name of the University nor the names of its
217c478bd9Sstevel@tonic-gate  * contributors may be used to endorse or promote products derived
227c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
237c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
247c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
257c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
29355d6bb5Sswilcox #include <stdlib.h>
30355d6bb5Sswilcox #include <unistd.h>
31355d6bb5Sswilcox #include <stdarg.h>
32355d6bb5Sswilcox #include <libadm.h>
33355d6bb5Sswilcox #include <note.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
377c478bd9Sstevel@tonic-gate #include <sys/filio.h>
387c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_acl.h>
41355d6bb5Sswilcox #include <sys/fs/ufs_inode.h>
42355d6bb5Sswilcox #include <sys/fs/ufs_log.h>
437c478bd9Sstevel@tonic-gate #define	_KERNEL
447c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
457c478bd9Sstevel@tonic-gate #undef _KERNEL
467c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
49355d6bb5Sswilcox #include <fcntl.h>
50355d6bb5Sswilcox #include <signal.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <ctype.h>
537c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
547c478bd9Sstevel@tonic-gate #include <sys/lockfs.h>
557c478bd9Sstevel@tonic-gate #include <errno.h>
56355d6bb5Sswilcox #include <sys/cmn_err.h>
57355d6bb5Sswilcox #include <sys/dkio.h>
58355d6bb5Sswilcox #include <sys/vtoc.h>
59355d6bb5Sswilcox #include <sys/efi_partition.h>
60355d6bb5Sswilcox #include <fslib.h>
61355d6bb5Sswilcox #include <inttypes.h>
62355d6bb5Sswilcox #include "fsck.h"
637c478bd9Sstevel@tonic-gate 
64355d6bb5Sswilcox caddr_t mount_point = NULL;
65355d6bb5Sswilcox 
66355d6bb5Sswilcox static int64_t diskreads, totalreads;	/* Disk cache statistics */
67355d6bb5Sswilcox 
68355d6bb5Sswilcox static int log_checksum(int32_t *, int32_t *, int);
69355d6bb5Sswilcox static void vdirerror(fsck_ino_t, caddr_t, va_list);
70355d6bb5Sswilcox static struct mnttab *search_mnttab(caddr_t, caddr_t, caddr_t, size_t);
71355d6bb5Sswilcox static struct vfstab *search_vfstab(caddr_t, caddr_t, caddr_t, size_t);
72355d6bb5Sswilcox static void vpwarn(caddr_t, va_list);
7323a1cceaSRoger A. Faulkner static int getaline(FILE *, caddr_t, int);
74355d6bb5Sswilcox static struct bufarea *alloc_bufarea(void);
75355d6bb5Sswilcox static void rwerror(caddr_t, diskaddr_t, int rval);
76355d6bb5Sswilcox static void debugclean(void);
77355d6bb5Sswilcox static void report_io_prob(caddr_t, diskaddr_t, size_t, ssize_t);
78355d6bb5Sswilcox static void freelogblk(daddr32_t);
79355d6bb5Sswilcox static void verrexit(caddr_t, va_list);
80355d6bb5Sswilcox static void vpfatal(caddr_t, va_list);
81355d6bb5Sswilcox static diskaddr_t get_device_size(int, caddr_t);
82355d6bb5Sswilcox static diskaddr_t brute_force_get_device_size(int);
83355d6bb5Sswilcox static void cg_constants(int, daddr32_t *, daddr32_t *, daddr32_t *,
84355d6bb5Sswilcox 	    daddr32_t *, daddr32_t *, daddr32_t *);
857c478bd9Sstevel@tonic-gate 
86355d6bb5Sswilcox int
87355d6bb5Sswilcox ftypeok(struct dinode *dp)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	switch (dp->di_mode & IFMT) {
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	case IFDIR:
927c478bd9Sstevel@tonic-gate 	case IFREG:
937c478bd9Sstevel@tonic-gate 	case IFBLK:
947c478bd9Sstevel@tonic-gate 	case IFCHR:
957c478bd9Sstevel@tonic-gate 	case IFLNK:
967c478bd9Sstevel@tonic-gate 	case IFSOCK:
977c478bd9Sstevel@tonic-gate 	case IFIFO:
987c478bd9Sstevel@tonic-gate 	case IFSHAD:
997c478bd9Sstevel@tonic-gate 	case IFATTRDIR:
1007c478bd9Sstevel@tonic-gate 		return (1);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	default:
1037c478bd9Sstevel@tonic-gate 		if (debug)
104355d6bb5Sswilcox 			(void) printf("bad file type 0%o\n", dp->di_mode);
1057c478bd9Sstevel@tonic-gate 		return (0);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
109355d6bb5Sswilcox int
110355d6bb5Sswilcox acltypeok(struct dinode *dp)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	if (CHECK_ACL_ALLOWED(dp->di_mode & IFMT))
1137c478bd9Sstevel@tonic-gate 		return (1);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (debug)
116355d6bb5Sswilcox 		(void) printf("bad file type for acl I=%d: 0%o\n",
117355d6bb5Sswilcox 		    dp->di_shadow, dp->di_mode);
1187c478bd9Sstevel@tonic-gate 	return (0);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
121355d6bb5Sswilcox NOTE(PRINTFLIKE(1))
122355d6bb5Sswilcox int
123355d6bb5Sswilcox reply(caddr_t fmt, ...)
1247c478bd9Sstevel@tonic-gate {
125355d6bb5Sswilcox 	va_list ap;
1267c478bd9Sstevel@tonic-gate 	char line[80];
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (preen)
129355d6bb5Sswilcox 		pfatal("INTERNAL ERROR: GOT TO reply() in preen mode");
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (mflag) {
132355d6bb5Sswilcox 		/*
133355d6bb5Sswilcox 		 * We don't know what's going on, so don't potentially
134355d6bb5Sswilcox 		 * make things worse by having errexit() write stuff
135355d6bb5Sswilcox 		 * out to disk.
136355d6bb5Sswilcox 		 */
137355d6bb5Sswilcox 		(void) printf(
138355d6bb5Sswilcox 		    "\n%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
139355d6bb5Sswilcox 		    devname);
140355d6bb5Sswilcox 		exit(EXERRFATAL);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
143355d6bb5Sswilcox 	va_start(ap, fmt);
144355d6bb5Sswilcox 	(void) putchar('\n');
145355d6bb5Sswilcox 	(void) vprintf(fmt, ap);
146355d6bb5Sswilcox 	(void) putchar('?');
147355d6bb5Sswilcox 	(void) putchar(' ');
148355d6bb5Sswilcox 	va_end(ap);
149355d6bb5Sswilcox 
1507c478bd9Sstevel@tonic-gate 	if (nflag || fswritefd < 0) {
151355d6bb5Sswilcox 		(void) printf(" no\n\n");
1527c478bd9Sstevel@tonic-gate 		return (0);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	if (yflag) {
155355d6bb5Sswilcox 		(void) printf(" yes\n\n");
1567c478bd9Sstevel@tonic-gate 		return (1);
1577c478bd9Sstevel@tonic-gate 	}
158355d6bb5Sswilcox 	(void) fflush(stdout);
15923a1cceaSRoger A. Faulkner 	if (getaline(stdin, line, sizeof (line)) == EOF)
1607c478bd9Sstevel@tonic-gate 		errexit("\n");
161355d6bb5Sswilcox 	(void) printf("\n");
162355d6bb5Sswilcox 	if (line[0] == 'y' || line[0] == 'Y') {
1637c478bd9Sstevel@tonic-gate 		return (1);
164355d6bb5Sswilcox 	} else {
1657c478bd9Sstevel@tonic-gate 		return (0);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
169355d6bb5Sswilcox int
17023a1cceaSRoger A. Faulkner getaline(FILE *fp, caddr_t loc, int maxlen)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	int n;
173355d6bb5Sswilcox 	caddr_t p, lastloc;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	p = loc;
1767c478bd9Sstevel@tonic-gate 	lastloc = &p[maxlen-1];
1777c478bd9Sstevel@tonic-gate 	while ((n = getc(fp)) != '\n') {
1787c478bd9Sstevel@tonic-gate 		if (n == EOF)
1797c478bd9Sstevel@tonic-gate 			return (EOF);
1807c478bd9Sstevel@tonic-gate 		if (!isspace(n) && p < lastloc)
181355d6bb5Sswilcox 			*p++ = (char)n;
1827c478bd9Sstevel@tonic-gate 	}
183355d6bb5Sswilcox 	*p = '\0';
184355d6bb5Sswilcox 	/* LINTED pointer difference won't overflow */
1857c478bd9Sstevel@tonic-gate 	return (p - loc);
1867c478bd9Sstevel@tonic-gate }
187355d6bb5Sswilcox 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Malloc buffers and set up cache.
1907c478bd9Sstevel@tonic-gate  */
191355d6bb5Sswilcox void
192355d6bb5Sswilcox bufinit(void)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	struct bufarea *bp;
1957c478bd9Sstevel@tonic-gate 	int bufcnt, i;
196355d6bb5Sswilcox 	caddr_t bufp;
1977c478bd9Sstevel@tonic-gate 
198355d6bb5Sswilcox 	bufp = malloc((size_t)sblock.fs_bsize);
199355d6bb5Sswilcox 	if (bufp == NULL)
200355d6bb5Sswilcox 		goto nomem;
2017c478bd9Sstevel@tonic-gate 	initbarea(&cgblk);
202355d6bb5Sswilcox 	cgblk.b_un.b_buf = bufp;
2037c478bd9Sstevel@tonic-gate 	bufhead.b_next = bufhead.b_prev = &bufhead;
2047c478bd9Sstevel@tonic-gate 	bufcnt = MAXBUFSPACE / sblock.fs_bsize;
2057c478bd9Sstevel@tonic-gate 	if (bufcnt < MINBUFS)
2067c478bd9Sstevel@tonic-gate 		bufcnt = MINBUFS;
2077c478bd9Sstevel@tonic-gate 	for (i = 0; i < bufcnt; i++) {
2087c478bd9Sstevel@tonic-gate 		bp = (struct bufarea *)malloc(sizeof (struct bufarea));
209355d6bb5Sswilcox 		if (bp == NULL) {
2107c478bd9Sstevel@tonic-gate 			if (i >= MINBUFS)
211355d6bb5Sswilcox 				goto noalloc;
212355d6bb5Sswilcox 			goto nomem;
213355d6bb5Sswilcox 		}
214355d6bb5Sswilcox 
215355d6bb5Sswilcox 		bufp = malloc((size_t)sblock.fs_bsize);
216355d6bb5Sswilcox 		if (bufp == NULL) {
217355d6bb5Sswilcox 			free((void *)bp);
218355d6bb5Sswilcox 			if (i >= MINBUFS)
219355d6bb5Sswilcox 				goto noalloc;
220355d6bb5Sswilcox 			goto nomem;
2217c478bd9Sstevel@tonic-gate 		}
222355d6bb5Sswilcox 		initbarea(bp);
2237c478bd9Sstevel@tonic-gate 		bp->b_un.b_buf = bufp;
2247c478bd9Sstevel@tonic-gate 		bp->b_prev = &bufhead;
2257c478bd9Sstevel@tonic-gate 		bp->b_next = bufhead.b_next;
2267c478bd9Sstevel@tonic-gate 		bufhead.b_next->b_prev = bp;
2277c478bd9Sstevel@tonic-gate 		bufhead.b_next = bp;
2287c478bd9Sstevel@tonic-gate 	}
229355d6bb5Sswilcox noalloc:
2307c478bd9Sstevel@tonic-gate 	bufhead.b_size = i;	/* save number of buffers */
2317c478bd9Sstevel@tonic-gate 	pbp = pdirbp = NULL;
232355d6bb5Sswilcox 	return;
233355d6bb5Sswilcox 
234355d6bb5Sswilcox nomem:
235355d6bb5Sswilcox 	errexit("cannot allocate buffer pool\n");
236355d6bb5Sswilcox 	/* NOTREACHED */
237355d6bb5Sswilcox }
238355d6bb5Sswilcox 
239355d6bb5Sswilcox /*
240355d6bb5Sswilcox  * Undo a bufinit().
241355d6bb5Sswilcox  */
242355d6bb5Sswilcox void
243355d6bb5Sswilcox unbufinit(void)
244355d6bb5Sswilcox {
245355d6bb5Sswilcox 	int cnt;
246355d6bb5Sswilcox 	struct bufarea *bp, *nbp;
247355d6bb5Sswilcox 
248355d6bb5Sswilcox 	cnt = 0;
249355d6bb5Sswilcox 	for (bp = bufhead.b_prev; bp != NULL && bp != &bufhead; bp = nbp) {
250355d6bb5Sswilcox 		cnt++;
251355d6bb5Sswilcox 		flush(fswritefd, bp);
252355d6bb5Sswilcox 		nbp = bp->b_prev;
253355d6bb5Sswilcox 		/*
254355d6bb5Sswilcox 		 * We're discarding the entire chain, so this isn't
255355d6bb5Sswilcox 		 * technically necessary.  However, it doesn't hurt
256355d6bb5Sswilcox 		 * and lint's data flow analysis is much happier
257355d6bb5Sswilcox 		 * (this prevents it from thinking there's a chance
258355d6bb5Sswilcox 		 * of our using memory elsewhere after it's been released).
259355d6bb5Sswilcox 		 */
260355d6bb5Sswilcox 		nbp->b_next = bp->b_next;
261355d6bb5Sswilcox 		bp->b_next->b_prev = nbp;
262355d6bb5Sswilcox 		free((void *)bp->b_un.b_buf);
263355d6bb5Sswilcox 		free((void *)bp);
264355d6bb5Sswilcox 	}
265355d6bb5Sswilcox 
266355d6bb5Sswilcox 	if (bufhead.b_size != cnt)
267355d6bb5Sswilcox 		errexit("Panic: cache lost %d buffers\n",
2681493b746SMilan Cermak 		    bufhead.b_size - cnt);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Manage a cache of directory blocks.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate struct bufarea *
275355d6bb5Sswilcox getdatablk(daddr32_t blkno, size_t size)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	struct bufarea *bp;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
280355d6bb5Sswilcox 		if (bp->b_bno == fsbtodb(&sblock, blkno)) {
2817c478bd9Sstevel@tonic-gate 			goto foundit;
282355d6bb5Sswilcox 		}
2837c478bd9Sstevel@tonic-gate 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
2847c478bd9Sstevel@tonic-gate 		if ((bp->b_flags & B_INUSE) == 0)
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 	if (bp == &bufhead) {
2877c478bd9Sstevel@tonic-gate 		bp = alloc_bufarea();
288355d6bb5Sswilcox 		if (bp == NULL) {
2897c478bd9Sstevel@tonic-gate 			errexit("deadlocked buffer pool\n");
290355d6bb5Sswilcox 			/* NOTREACHED */
291355d6bb5Sswilcox 		}
2927c478bd9Sstevel@tonic-gate 	}
293355d6bb5Sswilcox 	/*
294355d6bb5Sswilcox 	 * We're at the same logical level as getblk(), so if there
295355d6bb5Sswilcox 	 * are any errors, we'll let our caller handle them.
296355d6bb5Sswilcox 	 */
297355d6bb5Sswilcox 	diskreads++;
298355d6bb5Sswilcox 	(void) getblk(bp, blkno, size);
299355d6bb5Sswilcox 
3007c478bd9Sstevel@tonic-gate foundit:
3017c478bd9Sstevel@tonic-gate 	totalreads++;
3027c478bd9Sstevel@tonic-gate 	bp->b_cnt++;
3037c478bd9Sstevel@tonic-gate 	/*
304355d6bb5Sswilcox 	 * Move the buffer to head of linked list if it isn't
3057c478bd9Sstevel@tonic-gate 	 * already there.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	if (bufhead.b_next != bp) {
3087c478bd9Sstevel@tonic-gate 		bp->b_prev->b_next = bp->b_next;
3097c478bd9Sstevel@tonic-gate 		bp->b_next->b_prev = bp->b_prev;
3107c478bd9Sstevel@tonic-gate 		bp->b_prev = &bufhead;
3117c478bd9Sstevel@tonic-gate 		bp->b_next = bufhead.b_next;
3127c478bd9Sstevel@tonic-gate 		bufhead.b_next->b_prev = bp;
3137c478bd9Sstevel@tonic-gate 		bufhead.b_next = bp;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	bp->b_flags |= B_INUSE;
3167c478bd9Sstevel@tonic-gate 	return (bp);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
319355d6bb5Sswilcox void
3207c478bd9Sstevel@tonic-gate brelse(struct bufarea *bp)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	bp->b_cnt--;
3237c478bd9Sstevel@tonic-gate 	if (bp->b_cnt == 0) {
3247c478bd9Sstevel@tonic-gate 		bp->b_flags &= ~B_INUSE;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate struct bufarea *
329355d6bb5Sswilcox getblk(struct bufarea *bp, daddr32_t blk, size_t size)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	diskaddr_t dblk;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	dblk = fsbtodb(&sblock, blk);
3347c478bd9Sstevel@tonic-gate 	if (bp->b_bno == dblk)
3357c478bd9Sstevel@tonic-gate 		return (bp);
3367c478bd9Sstevel@tonic-gate 	flush(fswritefd, bp);
337355d6bb5Sswilcox 	bp->b_errs = fsck_bread(fsreadfd, bp->b_un.b_buf, dblk, size);
3387c478bd9Sstevel@tonic-gate 	bp->b_bno = dblk;
3397c478bd9Sstevel@tonic-gate 	bp->b_size = size;
3407c478bd9Sstevel@tonic-gate 	return (bp);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
343355d6bb5Sswilcox void
344355d6bb5Sswilcox flush(int fd, struct bufarea *bp)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	int i, j;
3477c478bd9Sstevel@tonic-gate 	caddr_t sip;
3487c478bd9Sstevel@tonic-gate 	long size;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (!bp->b_dirty)
3517c478bd9Sstevel@tonic-gate 		return;
352355d6bb5Sswilcox 
353355d6bb5Sswilcox 	/*
354355d6bb5Sswilcox 	 * It's not our buf, so if there are errors, let whoever
355355d6bb5Sswilcox 	 * acquired it deal with the actual problem.
356355d6bb5Sswilcox 	 */
3577c478bd9Sstevel@tonic-gate 	if (bp->b_errs != 0)
3587c478bd9Sstevel@tonic-gate 		pfatal("WRITING ZERO'ED BLOCK %lld TO DISK\n", bp->b_bno);
3597c478bd9Sstevel@tonic-gate 	bp->b_dirty = 0;
3607c478bd9Sstevel@tonic-gate 	bp->b_errs = 0;
3617c478bd9Sstevel@tonic-gate 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
362355d6bb5Sswilcox 	if (bp != &sblk) {
3637c478bd9Sstevel@tonic-gate 		return;
364355d6bb5Sswilcox 	}
365355d6bb5Sswilcox 
366355d6bb5Sswilcox 	/*
367355d6bb5Sswilcox 	 * We're flushing the superblock, so make sure all the
368355d6bb5Sswilcox 	 * ancillary bits go out as well.
369355d6bb5Sswilcox 	 */
3707c478bd9Sstevel@tonic-gate 	sip = (caddr_t)sblock.fs_u.fs_csp;
3717c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
3727c478bd9Sstevel@tonic-gate 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
3737c478bd9Sstevel@tonic-gate 		    sblock.fs_cssize - i : sblock.fs_bsize;
3747c478bd9Sstevel@tonic-gate 		bwrite(fswritefd, sip,
3757c478bd9Sstevel@tonic-gate 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
3767c478bd9Sstevel@tonic-gate 		    size);
3777c478bd9Sstevel@tonic-gate 		sip += size;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
381355d6bb5Sswilcox static void
382355d6bb5Sswilcox rwerror(caddr_t mesg, diskaddr_t blk, int rval)
3837c478bd9Sstevel@tonic-gate {
384355d6bb5Sswilcox 	int olderr = errno;
385355d6bb5Sswilcox 
386355d6bb5Sswilcox 	if (!preen)
387355d6bb5Sswilcox 		(void) printf("\n");
3887c478bd9Sstevel@tonic-gate 
389355d6bb5Sswilcox 	if (rval == -1)
390355d6bb5Sswilcox 		pfatal("CANNOT %s: DISK BLOCK %lld: %s",
391355d6bb5Sswilcox 		    mesg, blk, strerror(olderr));
392355d6bb5Sswilcox 	else
393355d6bb5Sswilcox 		pfatal("CANNOT %s: DISK BLOCK %lld", mesg, blk);
394355d6bb5Sswilcox 
395355d6bb5Sswilcox 	if (reply("CONTINUE") == 0) {
396355d6bb5Sswilcox 		exitstat = EXERRFATAL;
3977c478bd9Sstevel@tonic-gate 		errexit("Program terminated\n");
398355d6bb5Sswilcox 	}
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
401355d6bb5Sswilcox void
402355d6bb5Sswilcox ckfini(void)
4037c478bd9Sstevel@tonic-gate {
404355d6bb5Sswilcox 	int64_t percentage;
405355d6bb5Sswilcox 
406355d6bb5Sswilcox 	if (fswritefd < 0)
407355d6bb5Sswilcox 		return;
4087c478bd9Sstevel@tonic-gate 
409355d6bb5Sswilcox 	flush(fswritefd, &sblk);
4107c478bd9Sstevel@tonic-gate 	/*
411355d6bb5Sswilcox 	 * Were we using a backup superblock?
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	if (havesb && sblk.b_bno != SBOFF / dev_bsize) {
414355d6bb5Sswilcox 		if (preen || reply("UPDATE STANDARD SUPERBLOCK") == 1) {
415355d6bb5Sswilcox 			sblk.b_bno = SBOFF / dev_bsize;
416355d6bb5Sswilcox 			sbdirty();
417355d6bb5Sswilcox 			flush(fswritefd, &sblk);
418355d6bb5Sswilcox 		}
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	flush(fswritefd, &cgblk);
421355d6bb5Sswilcox 	if (cgblk.b_un.b_buf != NULL) {
422355d6bb5Sswilcox 		free((void *)cgblk.b_un.b_buf);
4237c478bd9Sstevel@tonic-gate 		cgblk.b_un.b_buf = NULL;
4247c478bd9Sstevel@tonic-gate 	}
425355d6bb5Sswilcox 	unbufinit();
426355d6bb5Sswilcox 	pbp = NULL;
427355d6bb5Sswilcox 	pdirbp = NULL;
428355d6bb5Sswilcox 	if (debug) {
429355d6bb5Sswilcox 		/*
430355d6bb5Sswilcox 		 * Note that we only count cache-related reads.
431355d6bb5Sswilcox 		 * Anything that called fsck_bread() or getblk()
432355d6bb5Sswilcox 		 * directly are explicitly not cached, so they're not
433355d6bb5Sswilcox 		 * included here.
434355d6bb5Sswilcox 		 */
435355d6bb5Sswilcox 		if (totalreads != 0)
436355d6bb5Sswilcox 			percentage = diskreads * 100 / totalreads;
437355d6bb5Sswilcox 		else
438355d6bb5Sswilcox 			percentage = 0;
439355d6bb5Sswilcox 
440355d6bb5Sswilcox 		(void) printf("cache missed %lld of %lld reads (%lld%%)\n",
441355d6bb5Sswilcox 		    (longlong_t)diskreads, (longlong_t)totalreads,
442355d6bb5Sswilcox 		    (longlong_t)percentage);
4437c478bd9Sstevel@tonic-gate 	}
444355d6bb5Sswilcox 
4457c478bd9Sstevel@tonic-gate 	(void) close(fsreadfd);
4467c478bd9Sstevel@tonic-gate 	(void) close(fswritefd);
447355d6bb5Sswilcox 	fsreadfd = -1;
448355d6bb5Sswilcox 	fswritefd = -1;
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
451355d6bb5Sswilcox int
452355d6bb5Sswilcox fsck_bread(int fd, caddr_t buf, diskaddr_t blk, size_t size)
4537c478bd9Sstevel@tonic-gate {
454355d6bb5Sswilcox 	caddr_t cp;
455355d6bb5Sswilcox 	int i;
4567c478bd9Sstevel@tonic-gate 	int errs;
4577c478bd9Sstevel@tonic-gate 	offset_t offset = ldbtob(blk);
4587c478bd9Sstevel@tonic-gate 	offset_t addr;
4597c478bd9Sstevel@tonic-gate 
460355d6bb5Sswilcox 	/*
461355d6bb5Sswilcox 	 * In our universe, nothing exists before the superblock, so
462355d6bb5Sswilcox 	 * just pretend it's always zeros.  This is the complement of
463355d6bb5Sswilcox 	 * bwrite()'s ignoring write requests into that space.
464355d6bb5Sswilcox 	 */
465355d6bb5Sswilcox 	if (blk < SBLOCK) {
466355d6bb5Sswilcox 		if (debug)
467355d6bb5Sswilcox 			(void) printf(
468355d6bb5Sswilcox 			    "WARNING: fsck_bread() passed blkno < %d (%lld)\n",
469355d6bb5Sswilcox 			    SBLOCK, (longlong_t)blk);
470355d6bb5Sswilcox 		(void) memset(buf, 0, (size_t)size);
471355d6bb5Sswilcox 		return (1);
4727c478bd9Sstevel@tonic-gate 	}
473355d6bb5Sswilcox 
474f763a6cdScasper 	if (llseek(fd, offset, SEEK_SET) < 0) {
475355d6bb5Sswilcox 		rwerror("SEEK", blk, -1);
476355d6bb5Sswilcox 	}
477355d6bb5Sswilcox 
478355d6bb5Sswilcox 	if ((i = read(fd, buf, size)) == size) {
4797c478bd9Sstevel@tonic-gate 		return (0);
480355d6bb5Sswilcox 	}
481355d6bb5Sswilcox 	rwerror("READ", blk, i);
482f763a6cdScasper 	if (llseek(fd, offset, SEEK_SET) < 0) {
483355d6bb5Sswilcox 		rwerror("SEEK", blk, -1);
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	errs = 0;
486355d6bb5Sswilcox 	(void) memset(buf, 0, (size_t)size);
4877c478bd9Sstevel@tonic-gate 	pwarn("THE FOLLOWING SECTORS COULD NOT BE READ:");
4887c478bd9Sstevel@tonic-gate 	for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
4897c478bd9Sstevel@tonic-gate 		addr = ldbtob(blk + i);
490f763a6cdScasper 		if (llseek(fd, addr, SEEK_SET) < 0 ||
4917c478bd9Sstevel@tonic-gate 		    read(fd, cp, (int)secsize) < 0) {
492355d6bb5Sswilcox 			iscorrupt = 1;
493355d6bb5Sswilcox 			(void) printf(" %llu", blk + (u_longlong_t)i);
4947c478bd9Sstevel@tonic-gate 			errs++;
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 	}
497355d6bb5Sswilcox 	(void) printf("\n");
4987c478bd9Sstevel@tonic-gate 	return (errs);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
501355d6bb5Sswilcox void
502355d6bb5Sswilcox bwrite(int fd, caddr_t buf, diskaddr_t blk, int64_t size)
5037c478bd9Sstevel@tonic-gate {
504355d6bb5Sswilcox 	int i;
5057c478bd9Sstevel@tonic-gate 	int n;
506355d6bb5Sswilcox 	caddr_t cp;
5077c478bd9Sstevel@tonic-gate 	offset_t offset = ldbtob(blk);
5087c478bd9Sstevel@tonic-gate 	offset_t addr;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (fd < 0)
5117c478bd9Sstevel@tonic-gate 		return;
5127c478bd9Sstevel@tonic-gate 	if (blk < SBLOCK) {
5137c478bd9Sstevel@tonic-gate 		if (debug)
514355d6bb5Sswilcox 			(void) printf(
515355d6bb5Sswilcox 		    "WARNING: Attempt to write illegal blkno %lld on %s\n",
516355d6bb5Sswilcox 			    (longlong_t)blk, devname);
5177c478bd9Sstevel@tonic-gate 		return;
5187c478bd9Sstevel@tonic-gate 	}
519f763a6cdScasper 	if (llseek(fd, offset, SEEK_SET) < 0) {
520355d6bb5Sswilcox 		rwerror("SEEK", blk, -1);
521355d6bb5Sswilcox 	}
522355d6bb5Sswilcox 	if ((i = write(fd, buf, (int)size)) == size) {
5237c478bd9Sstevel@tonic-gate 		fsmodified = 1;
5247c478bd9Sstevel@tonic-gate 		return;
5257c478bd9Sstevel@tonic-gate 	}
526355d6bb5Sswilcox 	rwerror("WRITE", blk, i);
527f763a6cdScasper 	if (llseek(fd, offset, SEEK_SET) < 0) {
528355d6bb5Sswilcox 		rwerror("SEEK", blk, -1);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 	pwarn("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
5317c478bd9Sstevel@tonic-gate 	for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) {
5327c478bd9Sstevel@tonic-gate 		n = 0;
5337c478bd9Sstevel@tonic-gate 		addr = ldbtob(blk + i);
534f763a6cdScasper 		if (llseek(fd, addr, SEEK_SET) < 0 ||
5357c478bd9Sstevel@tonic-gate 		    (n = write(fd, cp, DEV_BSIZE)) < 0) {
536355d6bb5Sswilcox 			iscorrupt = 1;
537355d6bb5Sswilcox 			(void) printf(" %llu", blk + (u_longlong_t)i);
5387c478bd9Sstevel@tonic-gate 		} else if (n > 0) {
5397c478bd9Sstevel@tonic-gate 			fsmodified = 1;
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	}
543355d6bb5Sswilcox 	(void) printf("\n");
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
547355d6bb5Sswilcox  * Allocates the specified number of contiguous fragments.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate daddr32_t
550355d6bb5Sswilcox allocblk(int wantedfrags)
5517c478bd9Sstevel@tonic-gate {
552355d6bb5Sswilcox 	int block, leadfrag, tailfrag;
553355d6bb5Sswilcox 	daddr32_t selected;
554355d6bb5Sswilcox 	size_t size;
555355d6bb5Sswilcox 	struct bufarea *bp;
5567c478bd9Sstevel@tonic-gate 
557355d6bb5Sswilcox 	/*
558355d6bb5Sswilcox 	 * It's arguable whether we should just fail, or instead
559355d6bb5Sswilcox 	 * error out here.  Since we should only ever be asked for
560355d6bb5Sswilcox 	 * a single fragment or an entire block (i.e., sblock.fs_frag),
561355d6bb5Sswilcox 	 * we'll fail out because anything else means somebody
562355d6bb5Sswilcox 	 * changed code without considering all of the ramifications.
563355d6bb5Sswilcox 	 */
564355d6bb5Sswilcox 	if (wantedfrags <= 0 || wantedfrags > sblock.fs_frag) {
565355d6bb5Sswilcox 		exitstat = EXERRFATAL;
566355d6bb5Sswilcox 		errexit("allocblk() asked for %d frags.  "
5671493b746SMilan Cermak 		    "Legal range is 1 to %d",
5681493b746SMilan Cermak 		    wantedfrags, sblock.fs_frag);
569355d6bb5Sswilcox 	}
570355d6bb5Sswilcox 
571355d6bb5Sswilcox 	/*
572355d6bb5Sswilcox 	 * For each filesystem block, look at every possible starting
573355d6bb5Sswilcox 	 * offset within the block such that we can get the number of
574355d6bb5Sswilcox 	 * contiguous fragments that we need.  This is a drastically
575355d6bb5Sswilcox 	 * simplified version of the kernel's mapsearch() and alloc*().
576355d6bb5Sswilcox 	 * It's also correspondingly slower.
577355d6bb5Sswilcox 	 */
578355d6bb5Sswilcox 	for (block = 0; block < maxfsblock - sblock.fs_frag;
579355d6bb5Sswilcox 	    block += sblock.fs_frag) {
580355d6bb5Sswilcox 		for (leadfrag = 0; leadfrag <= sblock.fs_frag - wantedfrags;
581355d6bb5Sswilcox 		    leadfrag++) {
582355d6bb5Sswilcox 			/*
583355d6bb5Sswilcox 			 * Is first fragment of candidate run available?
584355d6bb5Sswilcox 			 */
585355d6bb5Sswilcox 			if (testbmap(block + leadfrag))
5867c478bd9Sstevel@tonic-gate 				continue;
587355d6bb5Sswilcox 			/*
588355d6bb5Sswilcox 			 * Are the rest of them available?
589355d6bb5Sswilcox 			 */
590355d6bb5Sswilcox 			for (tailfrag = 1; tailfrag < wantedfrags; tailfrag++)
591355d6bb5Sswilcox 				if (testbmap(block + leadfrag + tailfrag))
5927c478bd9Sstevel@tonic-gate 					break;
593355d6bb5Sswilcox 			if (tailfrag < wantedfrags) {
594355d6bb5Sswilcox 				/*
595355d6bb5Sswilcox 				 * No, skip the known-unusable run.
596355d6bb5Sswilcox 				 */
597355d6bb5Sswilcox 				leadfrag += tailfrag;
5987c478bd9Sstevel@tonic-gate 				continue;
5997c478bd9Sstevel@tonic-gate 			}
600355d6bb5Sswilcox 			/*
601355d6bb5Sswilcox 			 * Found what we need, so claim them.
602355d6bb5Sswilcox 			 */
603355d6bb5Sswilcox 			for (tailfrag = 0; tailfrag < wantedfrags; tailfrag++)
604355d6bb5Sswilcox 				setbmap(block + leadfrag + tailfrag);
605355d6bb5Sswilcox 			n_blks += wantedfrags;
606355d6bb5Sswilcox 			size = wantedfrags * sblock.fs_fsize;
607355d6bb5Sswilcox 			selected = block + leadfrag;
608355d6bb5Sswilcox 			bp = getdatablk(selected, size);
609355d6bb5Sswilcox 			(void) memset((void *)bp->b_un.b_buf, 0, size);
610355d6bb5Sswilcox 			dirty(bp);
611355d6bb5Sswilcox 			brelse(bp);
612355d6bb5Sswilcox 			if (debug)
613355d6bb5Sswilcox 				(void) printf(
614355d6bb5Sswilcox 		    "allocblk: selected %d (in block %d), frags %d, size %d\n",
615355d6bb5Sswilcox 				    selected, selected % sblock.fs_bsize,
616355d6bb5Sswilcox 				    wantedfrags, (int)size);
617355d6bb5Sswilcox 			return (selected);
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	return (0);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * Free a previously allocated block
6257c478bd9Sstevel@tonic-gate  */
626355d6bb5Sswilcox void
627355d6bb5Sswilcox freeblk(fsck_ino_t ino, daddr32_t blkno, int frags)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	struct inodesc idesc;
6307c478bd9Sstevel@tonic-gate 
631355d6bb5Sswilcox 	if (debug)
632355d6bb5Sswilcox 		(void) printf("debug: freeing %d fragments starting at %d\n",
633355d6bb5Sswilcox 		    frags, blkno);
634355d6bb5Sswilcox 
635355d6bb5Sswilcox 	init_inodesc(&idesc);
636355d6bb5Sswilcox 
637355d6bb5Sswilcox 	idesc.id_number = ino;
6387c478bd9Sstevel@tonic-gate 	idesc.id_blkno = blkno;
6397c478bd9Sstevel@tonic-gate 	idesc.id_numfrags = frags;
640355d6bb5Sswilcox 	idesc.id_truncto = -1;
641355d6bb5Sswilcox 
642355d6bb5Sswilcox 	/*
643355d6bb5Sswilcox 	 * Nothing in the return status has any relevance to how
644355d6bb5Sswilcox 	 * we're using pass4check(), so just ignore it.
645355d6bb5Sswilcox 	 */
646355d6bb5Sswilcox 	(void) pass4check(&idesc);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate /*
650355d6bb5Sswilcox  * Fill NAMEBUF with a path starting in CURDIR for INO.  Assumes
651355d6bb5Sswilcox  * that the given buffer is at least MAXPATHLEN + 1 characters.
6527c478bd9Sstevel@tonic-gate  */
653355d6bb5Sswilcox void
654355d6bb5Sswilcox getpathname(caddr_t namebuf, fsck_ino_t curdir, fsck_ino_t ino)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	int len;
657355d6bb5Sswilcox 	caddr_t cp;
658355d6bb5Sswilcox 	struct dinode *dp;
6597c478bd9Sstevel@tonic-gate 	struct inodesc idesc;
6607c478bd9Sstevel@tonic-gate 	struct inoinfo *inp;
6617c478bd9Sstevel@tonic-gate 
662355d6bb5Sswilcox 	if (debug)
663355d6bb5Sswilcox 		(void) printf("debug: getpathname(curdir %d, ino %d)\n",
664355d6bb5Sswilcox 		    curdir, ino);
665355d6bb5Sswilcox 
666355d6bb5Sswilcox 	if ((curdir == 0) || (!INO_IS_DVALID(curdir))) {
667355d6bb5Sswilcox 		(void) strcpy(namebuf, "?");
668355d6bb5Sswilcox 		return;
669355d6bb5Sswilcox 	}
670355d6bb5Sswilcox 
671355d6bb5Sswilcox 	if ((curdir == UFSROOTINO) && (ino == UFSROOTINO)) {
672355d6bb5Sswilcox 		(void) strcpy(namebuf, "/");
6737c478bd9Sstevel@tonic-gate 		return;
6747c478bd9Sstevel@tonic-gate 	}
675355d6bb5Sswilcox 
676355d6bb5Sswilcox 	init_inodesc(&idesc);
6777c478bd9Sstevel@tonic-gate 	idesc.id_type = DATA;
6787c478bd9Sstevel@tonic-gate 	cp = &namebuf[MAXPATHLEN - 1];
6797c478bd9Sstevel@tonic-gate 	*cp = '\0';
680355d6bb5Sswilcox 
681355d6bb5Sswilcox 	/*
682355d6bb5Sswilcox 	 * In the case of extended attributes, our
683355d6bb5Sswilcox 	 * parent won't necessarily be a directory, so just
684355d6bb5Sswilcox 	 * return what we've found with a prefix indicating
685355d6bb5Sswilcox 	 * that it's an XATTR.  Presumably our caller will
686355d6bb5Sswilcox 	 * know what's going on and do something useful, like
687355d6bb5Sswilcox 	 * work out the path of the parent and then combine
688355d6bb5Sswilcox 	 * the two names.
689355d6bb5Sswilcox 	 *
690355d6bb5Sswilcox 	 * Can't use strcpy(), etc, because we've probably
691355d6bb5Sswilcox 	 * already got some name information in the buffer and
692355d6bb5Sswilcox 	 * the usual trailing \0 would lose it.
693355d6bb5Sswilcox 	 */
694355d6bb5Sswilcox 	dp = ginode(curdir);
695355d6bb5Sswilcox 	if ((dp->di_mode & IFMT) == IFATTRDIR) {
696355d6bb5Sswilcox 		idesc.id_number = curdir;
697355d6bb5Sswilcox 		idesc.id_parent = ino;
698355d6bb5Sswilcox 		idesc.id_func = findname;
699355d6bb5Sswilcox 		idesc.id_name = namebuf;
700355d6bb5Sswilcox 		idesc.id_fix = NOFIX;
701355d6bb5Sswilcox 		if ((ckinode(dp, &idesc, CKI_TRAVERSE) & FOUND) == 0) {
702355d6bb5Sswilcox 			*cp-- = '?';
703355d6bb5Sswilcox 		}
704355d6bb5Sswilcox 
705355d6bb5Sswilcox 		len = sizeof (XATTR_DIR_NAME) - 1;
706355d6bb5Sswilcox 		cp -= len;
707355d6bb5Sswilcox 		(void) memmove(cp, XATTR_DIR_NAME, len);
708355d6bb5Sswilcox 		goto attrname;
709355d6bb5Sswilcox 	}
710355d6bb5Sswilcox 
711355d6bb5Sswilcox 	/*
712355d6bb5Sswilcox 	 * If curdir == ino, need to get a handle on .. so we
713355d6bb5Sswilcox 	 * can search it for ino's name.  Otherwise, just search
714355d6bb5Sswilcox 	 * the given directory for ino.  Repeat until out of space
715355d6bb5Sswilcox 	 * or a full path has been built.
716355d6bb5Sswilcox 	 */
7177c478bd9Sstevel@tonic-gate 	if (curdir != ino) {
7187c478bd9Sstevel@tonic-gate 		idesc.id_parent = curdir;
7197c478bd9Sstevel@tonic-gate 		goto namelookup;
7207c478bd9Sstevel@tonic-gate 	}
721355d6bb5Sswilcox 	while (ino != UFSROOTINO && ino != 0) {
7227c478bd9Sstevel@tonic-gate 		idesc.id_number = ino;
7237c478bd9Sstevel@tonic-gate 		idesc.id_func = findino;
7247c478bd9Sstevel@tonic-gate 		idesc.id_name = "..";
7257c478bd9Sstevel@tonic-gate 		idesc.id_fix = NOFIX;
726355d6bb5Sswilcox 		if ((ckinode(ginode(ino), &idesc, CKI_TRAVERSE) & FOUND) == 0) {
7277c478bd9Sstevel@tonic-gate 			inp = getinoinfo(ino);
728355d6bb5Sswilcox 			if ((inp == NULL) || (inp->i_parent == 0)) {
7297c478bd9Sstevel@tonic-gate 				break;
730355d6bb5Sswilcox 			}
7317c478bd9Sstevel@tonic-gate 			idesc.id_parent = inp->i_parent;
7327c478bd9Sstevel@tonic-gate 		}
733355d6bb5Sswilcox 
734355d6bb5Sswilcox 		/*
735355d6bb5Sswilcox 		 * To get this far, id_parent must have the inode
736355d6bb5Sswilcox 		 * number for `..' in it.  By definition, that's got
737355d6bb5Sswilcox 		 * to be a directory, so search it for the inode of
738355d6bb5Sswilcox 		 * interest.
739355d6bb5Sswilcox 		 */
740355d6bb5Sswilcox namelookup:
7417c478bd9Sstevel@tonic-gate 		idesc.id_number = idesc.id_parent;
7427c478bd9Sstevel@tonic-gate 		idesc.id_parent = ino;
7437c478bd9Sstevel@tonic-gate 		idesc.id_func = findname;
7447c478bd9Sstevel@tonic-gate 		idesc.id_name = namebuf;
7457c478bd9Sstevel@tonic-gate 		idesc.id_fix = NOFIX;
746355d6bb5Sswilcox 		if ((ckinode(ginode(idesc.id_number),
747355d6bb5Sswilcox 		    &idesc, CKI_TRAVERSE) & FOUND) == 0) {
7487c478bd9Sstevel@tonic-gate 			break;
749355d6bb5Sswilcox 		}
750355d6bb5Sswilcox 		/*
751355d6bb5Sswilcox 		 * Prepend to what we've accumulated so far.  If
752355d6bb5Sswilcox 		 * there's not enough room for even one more path element
753355d6bb5Sswilcox 		 * (of the worst-case length), then bail out.
754355d6bb5Sswilcox 		 */
7557c478bd9Sstevel@tonic-gate 		len = strlen(namebuf);
7567c478bd9Sstevel@tonic-gate 		cp -= len;
7577c478bd9Sstevel@tonic-gate 		if (cp < &namebuf[MAXNAMLEN])
7587c478bd9Sstevel@tonic-gate 			break;
759355d6bb5Sswilcox 		(void) memmove(cp, namebuf, len);
7607c478bd9Sstevel@tonic-gate 		*--cp = '/';
761355d6bb5Sswilcox 
762355d6bb5Sswilcox 		/*
763355d6bb5Sswilcox 		 * Corner case for a looped-to-itself directory.
764355d6bb5Sswilcox 		 */
765355d6bb5Sswilcox 		if (ino == idesc.id_number)
766355d6bb5Sswilcox 			break;
767355d6bb5Sswilcox 
768355d6bb5Sswilcox 		/*
769355d6bb5Sswilcox 		 * Climb one level of the hierarchy.  In other words,
770355d6bb5Sswilcox 		 * the current .. becomes the inode to search for and
771355d6bb5Sswilcox 		 * its parent becomes the directory to search in.
772355d6bb5Sswilcox 		 */
7737c478bd9Sstevel@tonic-gate 		ino = idesc.id_number;
7747c478bd9Sstevel@tonic-gate 	}
775355d6bb5Sswilcox 
776355d6bb5Sswilcox 	/*
777355d6bb5Sswilcox 	 * If we hit a discontinuity in the hierarchy, indicate it by
778355d6bb5Sswilcox 	 * prefixing the path so far with `?'.  Otherwise, the first
779355d6bb5Sswilcox 	 * character will be `/' as a side-effect of the *--cp above.
780355d6bb5Sswilcox 	 *
781355d6bb5Sswilcox 	 * The special case is to handle the situation where we're
782355d6bb5Sswilcox 	 * trying to look something up in UFSROOTINO, but didn't find
783355d6bb5Sswilcox 	 * it.
784355d6bb5Sswilcox 	 */
785355d6bb5Sswilcox 	if (ino != UFSROOTINO || cp == &namebuf[MAXPATHLEN - 1]) {
786355d6bb5Sswilcox 		if (cp > namebuf)
787355d6bb5Sswilcox 			cp--;
788355d6bb5Sswilcox 		*cp = '?';
7897c478bd9Sstevel@tonic-gate 	}
790355d6bb5Sswilcox 
791355d6bb5Sswilcox 	/*
792355d6bb5Sswilcox 	 * The invariants being used for buffer integrity are:
793355d6bb5Sswilcox 	 * - namebuf[] is terminated with \0 before anything else
794355d6bb5Sswilcox 	 * - cp is always <= the last element of namebuf[]
795355d6bb5Sswilcox 	 * - the new path element is always stored at the
796355d6bb5Sswilcox 	 *   beginning of namebuf[], and is no more than MAXNAMLEN-1
797355d6bb5Sswilcox 	 *   characters
798355d6bb5Sswilcox 	 * - cp is is decremented by the number of characters in
799355d6bb5Sswilcox 	 *   the new path element
800355d6bb5Sswilcox 	 * - if, after the above accounting for the new element's
801355d6bb5Sswilcox 	 *   size, there is no longer enough room at the beginning of
802355d6bb5Sswilcox 	 *   namebuf[] for a full-sized path element and a slash,
803355d6bb5Sswilcox 	 *   terminate the loop.  cp is in the range
804355d6bb5Sswilcox 	 *   &namebuf[0]..&namebuf[MAXNAMLEN - 1]
805355d6bb5Sswilcox 	 */
806355d6bb5Sswilcox attrname:
807355d6bb5Sswilcox 	/* LINTED per the above discussion */
808355d6bb5Sswilcox 	(void) memmove(namebuf, cp, &namebuf[MAXPATHLEN] - cp);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
811355d6bb5Sswilcox /* ARGSUSED */
8127c478bd9Sstevel@tonic-gate void
813355d6bb5Sswilcox catch(int dummy)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	ckfini();
816355d6bb5Sswilcox 	exit(EXSIGNAL);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate /*
8207c478bd9Sstevel@tonic-gate  * When preening, allow a single quit to signal
8217c478bd9Sstevel@tonic-gate  * a special exit after filesystem checks complete
8227c478bd9Sstevel@tonic-gate  * so that reboot sequence may be interrupted.
8237c478bd9Sstevel@tonic-gate  */
824355d6bb5Sswilcox /* ARGSUSED */
8257c478bd9Sstevel@tonic-gate void
826355d6bb5Sswilcox catchquit(int dummy)
8277c478bd9Sstevel@tonic-gate {
828355d6bb5Sswilcox 	(void) printf("returning to single-user after filesystem check\n");
829355d6bb5Sswilcox 	interrupted = 1;
8307c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_DFL);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate  * determine whether an inode should be fixed.
8367c478bd9Sstevel@tonic-gate  */
837355d6bb5Sswilcox NOTE(PRINTFLIKE(2))
838355d6bb5Sswilcox int
839355d6bb5Sswilcox dofix(struct inodesc *idesc, caddr_t msg, ...)
8407c478bd9Sstevel@tonic-gate {
841355d6bb5Sswilcox 	int rval = 0;
842355d6bb5Sswilcox 	va_list ap;
843355d6bb5Sswilcox 
844355d6bb5Sswilcox 	va_start(ap, msg);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	switch (idesc->id_fix) {
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	case DONTKNOW:
8497c478bd9Sstevel@tonic-gate 		if (idesc->id_type == DATA)
850355d6bb5Sswilcox 			vdirerror(idesc->id_number, msg, ap);
8517c478bd9Sstevel@tonic-gate 		else
852355d6bb5Sswilcox 			vpwarn(msg, ap);
8537c478bd9Sstevel@tonic-gate 		if (preen) {
8547c478bd9Sstevel@tonic-gate 			idesc->id_fix = FIX;
855355d6bb5Sswilcox 			rval = ALTERED;
856355d6bb5Sswilcox 			break;
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 		if (reply("SALVAGE") == 0) {
8597c478bd9Sstevel@tonic-gate 			idesc->id_fix = NOFIX;
860355d6bb5Sswilcox 			break;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 		idesc->id_fix = FIX;
863355d6bb5Sswilcox 		rval = ALTERED;
864355d6bb5Sswilcox 		break;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	case FIX:
867355d6bb5Sswilcox 		rval = ALTERED;
868355d6bb5Sswilcox 		break;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	case NOFIX:
871355d6bb5Sswilcox 		break;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	default:
874355d6bb5Sswilcox 		errexit("UNKNOWN INODESC FIX MODE %d\n", (int)idesc->id_fix);
8757c478bd9Sstevel@tonic-gate 	}
876355d6bb5Sswilcox 
877355d6bb5Sswilcox 	va_end(ap);
878355d6bb5Sswilcox 	return (rval);
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
881355d6bb5Sswilcox NOTE(PRINTFLIKE(1))
882355d6bb5Sswilcox void
883355d6bb5Sswilcox errexit(caddr_t fmt, ...)
8847c478bd9Sstevel@tonic-gate {
885355d6bb5Sswilcox 	va_list ap;
8867c478bd9Sstevel@tonic-gate 
887355d6bb5Sswilcox 	va_start(ap, fmt);
888355d6bb5Sswilcox 	verrexit(fmt, ap);
889355d6bb5Sswilcox 	/* NOTREACHED */
890355d6bb5Sswilcox }
891355d6bb5Sswilcox 
892355d6bb5Sswilcox NOTE(PRINTFLIKE(1))
893355d6bb5Sswilcox static void
894355d6bb5Sswilcox verrexit(caddr_t fmt, va_list ap)
895355d6bb5Sswilcox {
896355d6bb5Sswilcox 	static int recursing = 0;
897355d6bb5Sswilcox 
898355d6bb5Sswilcox 	if (!recursing) {
899355d6bb5Sswilcox 		recursing = 1;
900355d6bb5Sswilcox 		if (errorlocked || iscorrupt) {
90173f4c598SMilan Cermak 			if (havesb && fswritefd >= 0) {
902355d6bb5Sswilcox 				sblock.fs_clean = FSBAD;
903355d6bb5Sswilcox 				sblock.fs_state = FSOKAY - (long)sblock.fs_time;
904355d6bb5Sswilcox 				sblock.fs_state = -sblock.fs_state;
905355d6bb5Sswilcox 				sbdirty();
906355d6bb5Sswilcox 				write_altsb(fswritefd);
907355d6bb5Sswilcox 				flush(fswritefd, &sblk);
908355d6bb5Sswilcox 			}
9097c478bd9Sstevel@tonic-gate 		}
910355d6bb5Sswilcox 		ckfini();
911355d6bb5Sswilcox 		recursing = 0;
9127c478bd9Sstevel@tonic-gate 	}
913355d6bb5Sswilcox 	(void) vprintf(fmt, ap);
914355d6bb5Sswilcox 	if (fmt[strlen(fmt) - 1] != '\n')
915355d6bb5Sswilcox 		(void) putchar('\n');
916355d6bb5Sswilcox 	exit((exitstat != 0) ? exitstat : EXERRFATAL);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate /*
9207c478bd9Sstevel@tonic-gate  * An unexpected inconsistency occured.
9217c478bd9Sstevel@tonic-gate  * Die if preening, otherwise just print message and continue.
9227c478bd9Sstevel@tonic-gate  */
923355d6bb5Sswilcox NOTE(PRINTFLIKE(1))
924355d6bb5Sswilcox void
925355d6bb5Sswilcox pfatal(caddr_t fmt, ...)
926355d6bb5Sswilcox {
927355d6bb5Sswilcox 	va_list ap;
928355d6bb5Sswilcox 
929355d6bb5Sswilcox 	va_start(ap, fmt);
930355d6bb5Sswilcox 	vpfatal(fmt, ap);
931355d6bb5Sswilcox 	va_end(ap);
932355d6bb5Sswilcox }
933355d6bb5Sswilcox 
934355d6bb5Sswilcox NOTE(PRINTFLIKE(1))
935355d6bb5Sswilcox static void
936355d6bb5Sswilcox vpfatal(caddr_t fmt, va_list ap)
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	if (preen) {
939355d6bb5Sswilcox 		if (*fmt != '\0') {
940355d6bb5Sswilcox 			(void) printf("%s: ", devname);
941355d6bb5Sswilcox 			(void) vprintf(fmt, ap);
942355d6bb5Sswilcox 			(void) printf("\n");
943355d6bb5Sswilcox 		}
944355d6bb5Sswilcox 		(void) printf(
945355d6bb5Sswilcox 		    "%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n",
946355d6bb5Sswilcox 		    devname);
94773f4c598SMilan Cermak 		if (havesb && fswritefd >= 0) {
9487c478bd9Sstevel@tonic-gate 			sblock.fs_clean = FSBAD;
9497c478bd9Sstevel@tonic-gate 			sblock.fs_state = -(FSOKAY - (long)sblock.fs_time);
9507c478bd9Sstevel@tonic-gate 			sbdirty();
9517c478bd9Sstevel@tonic-gate 			flush(fswritefd, &sblk);
9527c478bd9Sstevel@tonic-gate 		}
953355d6bb5Sswilcox 		/*
954355d6bb5Sswilcox 		 * We're exiting, it doesn't really matter that our
955355d6bb5Sswilcox 		 * caller doesn't get to call va_end().
956355d6bb5Sswilcox 		 */
957355d6bb5Sswilcox 		if (exitstat == 0)
958355d6bb5Sswilcox 			exitstat = EXFNDERRS;
959355d6bb5Sswilcox 		exit(exitstat);
960355d6bb5Sswilcox 	}
961355d6bb5Sswilcox 	if (*fmt != '\0') {
962355d6bb5Sswilcox 		(void) vprintf(fmt, ap);
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate /*
9677c478bd9Sstevel@tonic-gate  * Pwarn just prints a message when not preening,
9687c478bd9Sstevel@tonic-gate  * or a warning (preceded by filename) when preening.
9697c478bd9Sstevel@tonic-gate  */
970355d6bb5Sswilcox NOTE(PRINTFLIKE(1))
971355d6bb5Sswilcox void
972355d6bb5Sswilcox pwarn(caddr_t fmt, ...)
9737c478bd9Sstevel@tonic-gate {
974355d6bb5Sswilcox 	va_list ap;
9757c478bd9Sstevel@tonic-gate 
976355d6bb5Sswilcox 	va_start(ap, fmt);
977355d6bb5Sswilcox 	vpwarn(fmt, ap);
978355d6bb5Sswilcox 	va_end(ap);
979355d6bb5Sswilcox }
980355d6bb5Sswilcox 
981355d6bb5Sswilcox NOTE(PRINTFLIKE(1))
982355d6bb5Sswilcox static void
983355d6bb5Sswilcox vpwarn(caddr_t fmt, va_list ap)
984355d6bb5Sswilcox {
985355d6bb5Sswilcox 	if (*fmt != '\0') {
986355d6bb5Sswilcox 		if (preen)
987355d6bb5Sswilcox 			(void) printf("%s: ", devname);
988355d6bb5Sswilcox 		(void) vprintf(fmt, ap);
989355d6bb5Sswilcox 	}
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
993355d6bb5Sswilcox  * Like sprintf(), except the buffer is dynamically allocated
994355d6bb5Sswilcox  * and returned, instead of being passed in.  A pointer to the
995355d6bb5Sswilcox  * buffer is stored in *RET, and FMT is the usual format string.
996355d6bb5Sswilcox  * The number of characters in *RET (excluding the trailing \0,
997355d6bb5Sswilcox  * to be consistent with the other *printf() routines) is returned.
998355d6bb5Sswilcox  *
999355d6bb5Sswilcox  * Solaris doesn't have asprintf(3C) yet, unfortunately.
10007c478bd9Sstevel@tonic-gate  */
1001355d6bb5Sswilcox NOTE(PRINTFLIKE(2))
1002355d6bb5Sswilcox int
1003355d6bb5Sswilcox fsck_asprintf(caddr_t *ret, caddr_t fmt, ...)
10047c478bd9Sstevel@tonic-gate {
1005355d6bb5Sswilcox 	int len;
1006355d6bb5Sswilcox 	caddr_t buffer;
1007355d6bb5Sswilcox 	va_list ap;
10087c478bd9Sstevel@tonic-gate 
1009355d6bb5Sswilcox 	va_start(ap, fmt);
1010355d6bb5Sswilcox 	len = vsnprintf(NULL, 0, fmt, ap);
1011355d6bb5Sswilcox 	va_end(ap);
1012355d6bb5Sswilcox 
1013355d6bb5Sswilcox 	buffer = malloc((len + 1) * sizeof (char));
1014355d6bb5Sswilcox 	if (buffer == NULL) {
1015355d6bb5Sswilcox 		errexit("Out of memory in asprintf\n");
1016355d6bb5Sswilcox 		/* NOTREACHED */
1017355d6bb5Sswilcox 	}
1018355d6bb5Sswilcox 
1019355d6bb5Sswilcox 	va_start(ap, fmt);
1020355d6bb5Sswilcox 	(void) vsnprintf(buffer, len + 1, fmt, ap);
1021355d6bb5Sswilcox 	va_end(ap);
1022355d6bb5Sswilcox 
1023355d6bb5Sswilcox 	*ret = buffer;
1024355d6bb5Sswilcox 	return (len);
10257c478bd9Sstevel@tonic-gate }
1026355d6bb5Sswilcox 
1027355d6bb5Sswilcox /*
1028355d6bb5Sswilcox  * So we can take advantage of kernel routines in ufs_subr.c.
1029355d6bb5Sswilcox  */
1030355d6bb5Sswilcox /* PRINTFLIKE2 */
10317c478bd9Sstevel@tonic-gate void
1032355d6bb5Sswilcox cmn_err(int level, caddr_t fmt, ...)
10337c478bd9Sstevel@tonic-gate {
1034355d6bb5Sswilcox 	va_list ap;
10357c478bd9Sstevel@tonic-gate 
1036355d6bb5Sswilcox 	va_start(ap, fmt);
10377c478bd9Sstevel@tonic-gate 	if (level == CE_PANIC) {
1038355d6bb5Sswilcox 		(void) printf("INTERNAL INCONSISTENCY:");
1039355d6bb5Sswilcox 		verrexit(fmt, ap);
1040355d6bb5Sswilcox 	} else {
1041355d6bb5Sswilcox 		(void) vprintf(fmt, ap);
10427c478bd9Sstevel@tonic-gate 	}
1043355d6bb5Sswilcox 	va_end(ap);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate  * Check to see if unraw version of name is already mounted.
1048355d6bb5Sswilcox  * Updates devstr with the device name if devstr is not NULL
1049355d6bb5Sswilcox  * and str_size is positive.
10507c478bd9Sstevel@tonic-gate  */
1051355d6bb5Sswilcox int
1052355d6bb5Sswilcox mounted(caddr_t name, caddr_t devstr, size_t str_size)
10537c478bd9Sstevel@tonic-gate {
1054355d6bb5Sswilcox 	int found;
1055355d6bb5Sswilcox 	struct mnttab *mntent;
10567c478bd9Sstevel@tonic-gate 
1057355d6bb5Sswilcox 	mntent = search_mnttab(NULL, unrawname(name), devstr, str_size);
1058355d6bb5Sswilcox 	if (mntent == NULL)
1059355d6bb5Sswilcox 		return (M_NOMNT);
1060355d6bb5Sswilcox 
1061355d6bb5Sswilcox 	/*
1062355d6bb5Sswilcox 	 * It's mounted.  With or without write access?
1063355d6bb5Sswilcox 	 */
1064355d6bb5Sswilcox 	if (hasmntopt(mntent, MNTOPT_RO) != 0)
1065355d6bb5Sswilcox 		found = M_RO;	/* mounted as RO */
1066355d6bb5Sswilcox 	else
1067355d6bb5Sswilcox 		found = M_RW; 	/* mounted as R/W */
1068355d6bb5Sswilcox 
1069355d6bb5Sswilcox 	if (mount_point == NULL) {
1070355d6bb5Sswilcox 		mount_point = strdup(mntent->mnt_mountp);
1071355d6bb5Sswilcox 		if (mount_point == NULL) {
1072355d6bb5Sswilcox 			errexit("fsck: memory allocation failure: %s",
10731493b746SMilan Cermak 			    strerror(errno));
1074355d6bb5Sswilcox 			/* NOTREACHED */
10757c478bd9Sstevel@tonic-gate 		}
1076355d6bb5Sswilcox 
1077355d6bb5Sswilcox 		if (devstr != NULL && str_size > 0)
1078355d6bb5Sswilcox 			(void) strlcpy(devstr, mntent->mnt_special, str_size);
10797c478bd9Sstevel@tonic-gate 	}
1080355d6bb5Sswilcox 
10817c478bd9Sstevel@tonic-gate 	return (found);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Check to see if name corresponds to an entry in vfstab, and that the entry
10867c478bd9Sstevel@tonic-gate  * does not have option ro.
10877c478bd9Sstevel@tonic-gate  */
1088355d6bb5Sswilcox int
1089355d6bb5Sswilcox writable(caddr_t name)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate 	int rw = 1;
1092355d6bb5Sswilcox 	struct vfstab vfsbuf, vfskey;
10937c478bd9Sstevel@tonic-gate 	FILE *vfstab;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	vfstab = fopen(VFSTAB, "r");
10967c478bd9Sstevel@tonic-gate 	if (vfstab == NULL) {
1097355d6bb5Sswilcox 		(void) printf("can't open %s\n", VFSTAB);
10987c478bd9Sstevel@tonic-gate 		return (1);
10997c478bd9Sstevel@tonic-gate 	}
1100355d6bb5Sswilcox 	(void) memset((void *)&vfskey, 0, sizeof (vfskey));
1101355d6bb5Sswilcox 	vfsnull(&vfskey);
1102355d6bb5Sswilcox 	vfskey.vfs_special = unrawname(name);
1103355d6bb5Sswilcox 	vfskey.vfs_fstype = MNTTYPE_UFS;
1104355d6bb5Sswilcox 	if ((getvfsany(vfstab, &vfsbuf, &vfskey) == 0) &&
11057c478bd9Sstevel@tonic-gate 	    (hasvfsopt(&vfsbuf, MNTOPT_RO))) {
11067c478bd9Sstevel@tonic-gate 		rw = 0;
11077c478bd9Sstevel@tonic-gate 	}
1108355d6bb5Sswilcox 	(void) fclose(vfstab);
11097c478bd9Sstevel@tonic-gate 	return (rw);
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate /*
11137c478bd9Sstevel@tonic-gate  * debugclean
11147c478bd9Sstevel@tonic-gate  */
1115355d6bb5Sswilcox static void
1116355d6bb5Sswilcox debugclean(void)
11177c478bd9Sstevel@tonic-gate {
1118355d6bb5Sswilcox 	if (!debug)
11197c478bd9Sstevel@tonic-gate 		return;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if ((iscorrupt == 0) && (isdirty == 0))
11227c478bd9Sstevel@tonic-gate 		return;
11237c478bd9Sstevel@tonic-gate 
1124355d6bb5Sswilcox 	if ((sblock.fs_clean == FSSTABLE) || (sblock.fs_clean == FSCLEAN) ||
1125355d6bb5Sswilcox 	    (sblock.fs_clean == FSLOG && islog && islogok) ||
1126355d6bb5Sswilcox 	    ((FSOKAY == (sblock.fs_state + sblock.fs_time)) && !errorlocked))
11277c478bd9Sstevel@tonic-gate 		return;
11287c478bd9Sstevel@tonic-gate 
1129355d6bb5Sswilcox 	(void) printf("WARNING: inconsistencies detected on %s filesystem %s\n",
11307c478bd9Sstevel@tonic-gate 	    sblock.fs_clean == FSSTABLE ? "stable" :
11317c478bd9Sstevel@tonic-gate 	    sblock.fs_clean == FSLOG ? "logging" :
1132355d6bb5Sswilcox 	    sblock.fs_clean == FSFIX ? "being fixed" : "clean",
1133355d6bb5Sswilcox 	    devname);
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate /*
11377c478bd9Sstevel@tonic-gate  * updateclean
11387c478bd9Sstevel@tonic-gate  *	Carefully and transparently update the clean flag.
1139355d6bb5Sswilcox  *
1140355d6bb5Sswilcox  * `iscorrupt' has to be in its final state before this is called.
11417c478bd9Sstevel@tonic-gate  */
1142355d6bb5Sswilcox int
1143355d6bb5Sswilcox updateclean(void)
1144355d6bb5Sswilcox {
1145355d6bb5Sswilcox 	int freedlog = 0;
1146355d6bb5Sswilcox 	struct bufarea cleanbuf;
1147355d6bb5Sswilcox 	size_t size;
1148355d6bb5Sswilcox 	ssize_t io_res;
1149355d6bb5Sswilcox 	diskaddr_t bno;
1150355d6bb5Sswilcox 	char fsclean;
1151355d6bb5Sswilcox 	int fsreclaim;
1152355d6bb5Sswilcox 	char fsflags;
115320a1ae8aSjkennedy 	int flags_ok = 1;
1154355d6bb5Sswilcox 	daddr32_t fslogbno;
11557c478bd9Sstevel@tonic-gate 	offset_t sblkoff;
11567c478bd9Sstevel@tonic-gate 	time_t t;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/*
11597c478bd9Sstevel@tonic-gate 	 * debug stuff
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	debugclean();
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * set fsclean to its appropriate value
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 	fslogbno = sblock.fs_logbno;
11677c478bd9Sstevel@tonic-gate 	fsclean = sblock.fs_clean;
11687c478bd9Sstevel@tonic-gate 	fsreclaim = sblock.fs_reclaim;
11697c478bd9Sstevel@tonic-gate 	fsflags = sblock.fs_flags;
1170355d6bb5Sswilcox 	if (FSOKAY != (sblock.fs_state + sblock.fs_time) && !errorlocked) {
11717c478bd9Sstevel@tonic-gate 		fsclean = FSACTIVE;
1172355d6bb5Sswilcox 	}
1173355d6bb5Sswilcox 	/*
1174355d6bb5Sswilcox 	 * If ufs log is not okay, note that we need to clear it.
1175355d6bb5Sswilcox 	 */
117639542a18Sabalfour 	examinelog(NULL);
11777c478bd9Sstevel@tonic-gate 	if (fslogbno && !(islog && islogok)) {
11787c478bd9Sstevel@tonic-gate 		fsclean = FSACTIVE;
11797c478bd9Sstevel@tonic-gate 		fslogbno = 0;
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	/*
11837c478bd9Sstevel@tonic-gate 	 * if necessary, update fs_clean and fs_state
11847c478bd9Sstevel@tonic-gate 	 */
11857c478bd9Sstevel@tonic-gate 	switch (fsclean) {
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	case FSACTIVE:
11887c478bd9Sstevel@tonic-gate 		if (!iscorrupt) {
11897c478bd9Sstevel@tonic-gate 			fsclean = FSSTABLE;
11907c478bd9Sstevel@tonic-gate 			fsreclaim = 0;
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 		break;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	case FSCLEAN:
11957c478bd9Sstevel@tonic-gate 	case FSSTABLE:
1196355d6bb5Sswilcox 		if (iscorrupt) {
11977c478bd9Sstevel@tonic-gate 			fsclean = FSACTIVE;
1198355d6bb5Sswilcox 		} else {
11997c478bd9Sstevel@tonic-gate 			fsreclaim = 0;
1200355d6bb5Sswilcox 		}
12017c478bd9Sstevel@tonic-gate 		break;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	case FSLOG:
1204355d6bb5Sswilcox 		if (iscorrupt) {
12057c478bd9Sstevel@tonic-gate 			fsclean = FSACTIVE;
1206355d6bb5Sswilcox 		} else if (!islog || fslogbno == 0) {
12077c478bd9Sstevel@tonic-gate 			fsclean = FSSTABLE;
12087c478bd9Sstevel@tonic-gate 			fsreclaim = 0;
1209355d6bb5Sswilcox 		} else if (fflag) {
1210355d6bb5Sswilcox 			fsreclaim = 0;
1211355d6bb5Sswilcox 		}
12127c478bd9Sstevel@tonic-gate 		break;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	case FSFIX:
12157c478bd9Sstevel@tonic-gate 		fsclean = FSBAD;
12167c478bd9Sstevel@tonic-gate 		if (errorlocked && !iscorrupt) {
1217355d6bb5Sswilcox 			fsclean = islog ? FSLOG : FSCLEAN;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 		break;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	default:
1222355d6bb5Sswilcox 		if (iscorrupt) {
12237c478bd9Sstevel@tonic-gate 			fsclean = FSACTIVE;
1224355d6bb5Sswilcox 		} else {
12257c478bd9Sstevel@tonic-gate 			fsclean = FSSTABLE;
12267c478bd9Sstevel@tonic-gate 			fsreclaim = 0;
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 	}
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	if (largefile_count > 0)
12317c478bd9Sstevel@tonic-gate 		fsflags |= FSLARGEFILES;
12327c478bd9Sstevel@tonic-gate 	else
12337c478bd9Sstevel@tonic-gate 		fsflags &= ~FSLARGEFILES;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/*
123620a1ae8aSjkennedy 	 * There can be two discrepencies here.  A) The superblock
123720a1ae8aSjkennedy 	 * shows no largefiles but we found some while scanning.
123820a1ae8aSjkennedy 	 * B) The superblock indicates the presence of largefiles,
123920a1ae8aSjkennedy 	 * but none are present.  Note that if preening, the superblock
124020a1ae8aSjkennedy 	 * is silently corrected.
12417c478bd9Sstevel@tonic-gate 	 */
124220a1ae8aSjkennedy 	if ((fsflags == FSLARGEFILES && sblock.fs_flags != FSLARGEFILES) ||
124320a1ae8aSjkennedy 	    (fsflags != FSLARGEFILES && sblock.fs_flags == FSLARGEFILES))
1244355d6bb5Sswilcox 		flags_ok = 0;
1245355d6bb5Sswilcox 
12467c478bd9Sstevel@tonic-gate 	if (debug)
1247355d6bb5Sswilcox 		(void) printf(
1248355d6bb5Sswilcox 		    "** largefile count=%d, fs.fs_flags=%x, flags_ok %d\n",
1249355d6bb5Sswilcox 		    largefile_count, sblock.fs_flags, flags_ok);
12507c478bd9Sstevel@tonic-gate 
1251355d6bb5Sswilcox 	/*
1252355d6bb5Sswilcox 	 * If fs is unchanged, do nothing.
1253355d6bb5Sswilcox 	 */
1254355d6bb5Sswilcox 	if ((!isdirty) && (flags_ok) &&
12557c478bd9Sstevel@tonic-gate 	    (fslogbno == sblock.fs_logbno) &&
1256355d6bb5Sswilcox 	    (sblock.fs_clean == fsclean) &&
1257355d6bb5Sswilcox 	    (sblock.fs_reclaim == fsreclaim) &&
12587c478bd9Sstevel@tonic-gate 	    (FSOKAY == (sblock.fs_state + sblock.fs_time))) {
12597c478bd9Sstevel@tonic-gate 		if (errorlocked) {
12607c478bd9Sstevel@tonic-gate 			if (!do_errorlock(LOCKFS_ULOCK))
12617c478bd9Sstevel@tonic-gate 				pwarn(
12627c478bd9Sstevel@tonic-gate 		    "updateclean(unchanged): unlock(LOCKFS_ULOCK) failed\n");
12637c478bd9Sstevel@tonic-gate 		}
1264355d6bb5Sswilcox 		return (freedlog);
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	/*
12687c478bd9Sstevel@tonic-gate 	 * if user allows, update superblock state
12697c478bd9Sstevel@tonic-gate 	 */
1270355d6bb5Sswilcox 	if (debug) {
1271355d6bb5Sswilcox 		(void) printf(
1272355d6bb5Sswilcox 	    "superblock: flags 0x%x logbno %d clean %d reclaim %d state 0x%x\n",
1273355d6bb5Sswilcox 		    sblock.fs_flags, sblock.fs_logbno,
1274355d6bb5Sswilcox 		    sblock.fs_clean, sblock.fs_reclaim,
1275355d6bb5Sswilcox 		    sblock.fs_state + sblock.fs_time);
1276355d6bb5Sswilcox 		(void) printf(
1277355d6bb5Sswilcox 	    "calculated: flags 0x%x logbno %d clean %d reclaim %d state 0x%x\n",
1278355d6bb5Sswilcox 		    fsflags, fslogbno, fsclean, fsreclaim, FSOKAY);
1279355d6bb5Sswilcox 	}
1280355d6bb5Sswilcox 	if (!isdirty && !preen && !rerun &&
12817c478bd9Sstevel@tonic-gate 	    (reply("FILE SYSTEM STATE IN SUPERBLOCK IS WRONG; FIX") == 0))
1282355d6bb5Sswilcox 		return (freedlog);
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	(void) time(&t);
12857c478bd9Sstevel@tonic-gate 	sblock.fs_time = (time32_t)t;
12867c478bd9Sstevel@tonic-gate 	if (debug)
12877c478bd9Sstevel@tonic-gate 		printclean();
1288355d6bb5Sswilcox 
1289355d6bb5Sswilcox 	if (sblock.fs_logbno != fslogbno) {
129039542a18Sabalfour 		examinelog(&freelogblk);
1291355d6bb5Sswilcox 		freedlog++;
1292355d6bb5Sswilcox 	}
1293355d6bb5Sswilcox 
12947c478bd9Sstevel@tonic-gate 	sblock.fs_logbno = fslogbno;
12957c478bd9Sstevel@tonic-gate 	sblock.fs_clean = fsclean;
12967c478bd9Sstevel@tonic-gate 	sblock.fs_state = FSOKAY - (long)sblock.fs_time;
12977c478bd9Sstevel@tonic-gate 	sblock.fs_reclaim = fsreclaim;
12987c478bd9Sstevel@tonic-gate 	sblock.fs_flags = fsflags;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/*
13017c478bd9Sstevel@tonic-gate 	 * if superblock can't be written, return
13027c478bd9Sstevel@tonic-gate 	 */
13037c478bd9Sstevel@tonic-gate 	if (fswritefd < 0)
1304355d6bb5Sswilcox 		return (freedlog);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/*
1307355d6bb5Sswilcox 	 * Read private copy of superblock, update clean flag, and write it.
13087c478bd9Sstevel@tonic-gate 	 */
13097c478bd9Sstevel@tonic-gate 	bno  = sblk.b_bno;
13107c478bd9Sstevel@tonic-gate 	size = sblk.b_size;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	sblkoff = ldbtob(bno);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if ((cleanbuf.b_un.b_buf = malloc(size)) == NULL)
13157c478bd9Sstevel@tonic-gate 		errexit("out of memory");
1316f763a6cdScasper 	if (llseek(fsreadfd, sblkoff, SEEK_SET) == -1) {
1317355d6bb5Sswilcox 		(void) printf("COULD NOT SEEK TO SUPERBLOCK AT %lld: %s\n",
1318355d6bb5Sswilcox 		    (longlong_t)bno, strerror(errno));
1319355d6bb5Sswilcox 		goto out;
1320355d6bb5Sswilcox 	}
13217c478bd9Sstevel@tonic-gate 
1322355d6bb5Sswilcox 	if ((io_res = read(fsreadfd, cleanbuf.b_un.b_buf, size)) != size) {
1323355d6bb5Sswilcox 		report_io_prob("READ FROM", bno, size, io_res);
1324355d6bb5Sswilcox 		goto out;
1325355d6bb5Sswilcox 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	cleanbuf.b_un.b_fs->fs_logbno  = sblock.fs_logbno;
13287c478bd9Sstevel@tonic-gate 	cleanbuf.b_un.b_fs->fs_clean   = sblock.fs_clean;
13297c478bd9Sstevel@tonic-gate 	cleanbuf.b_un.b_fs->fs_state   = sblock.fs_state;
13307c478bd9Sstevel@tonic-gate 	cleanbuf.b_un.b_fs->fs_time    = sblock.fs_time;
13317c478bd9Sstevel@tonic-gate 	cleanbuf.b_un.b_fs->fs_reclaim = sblock.fs_reclaim;
13327c478bd9Sstevel@tonic-gate 	cleanbuf.b_un.b_fs->fs_flags   = sblock.fs_flags;
13337c478bd9Sstevel@tonic-gate 
1334f763a6cdScasper 	if (llseek(fswritefd, sblkoff, SEEK_SET) == -1) {
1335355d6bb5Sswilcox 		(void) printf("COULD NOT SEEK TO SUPERBLOCK AT %lld: %s\n",
1336355d6bb5Sswilcox 		    (longlong_t)bno, strerror(errno));
1337355d6bb5Sswilcox 		goto out;
1338355d6bb5Sswilcox 	}
1339355d6bb5Sswilcox 
1340355d6bb5Sswilcox 	if ((io_res = write(fswritefd, cleanbuf.b_un.b_buf, size)) != size) {
1341355d6bb5Sswilcox 		report_io_prob("WRITE TO", bno, size, io_res);
1342355d6bb5Sswilcox 		goto out;
1343355d6bb5Sswilcox 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/*
13467c478bd9Sstevel@tonic-gate 	 * 1208040
13477c478bd9Sstevel@tonic-gate 	 * If we had to use -b to grab an alternate superblock, then we
13487c478bd9Sstevel@tonic-gate 	 * likely had to do so because of unacceptable differences between
1349355d6bb5Sswilcox 	 * the main and alternate superblocks.  So, we had better update
13507c478bd9Sstevel@tonic-gate 	 * the alternate superblock as well, or we'll just fail again
13517c478bd9Sstevel@tonic-gate 	 * the next time we attempt to run fsck!
13527c478bd9Sstevel@tonic-gate 	 */
1353355d6bb5Sswilcox 	if (bflag != 0) {
1354355d6bb5Sswilcox 		write_altsb(fswritefd);
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	if (errorlocked) {
13587c478bd9Sstevel@tonic-gate 		if (!do_errorlock(LOCKFS_ULOCK))
13597c478bd9Sstevel@tonic-gate 			pwarn(
13607c478bd9Sstevel@tonic-gate 		    "updateclean(changed): unlock(LOCKFS_ULOCK) failed\n");
13617c478bd9Sstevel@tonic-gate 	}
1362355d6bb5Sswilcox 
1363355d6bb5Sswilcox out:
1364355d6bb5Sswilcox 	if (cleanbuf.b_un.b_buf != NULL) {
1365355d6bb5Sswilcox 		free((void *)cleanbuf.b_un.b_buf);
1366355d6bb5Sswilcox 	}
1367355d6bb5Sswilcox 
1368355d6bb5Sswilcox 	return (freedlog);
1369355d6bb5Sswilcox }
1370355d6bb5Sswilcox 
1371355d6bb5Sswilcox static void
1372355d6bb5Sswilcox report_io_prob(caddr_t what, diskaddr_t bno, size_t expected, ssize_t failure)
1373355d6bb5Sswilcox {
1374355d6bb5Sswilcox 	if (failure < 0)
1375355d6bb5Sswilcox 		(void) printf("COULD NOT %s SUPERBLOCK AT %d: %s\n",
1376355d6bb5Sswilcox 		    what, (int)bno, strerror(errno));
1377355d6bb5Sswilcox 	else if (failure == 0)
1378355d6bb5Sswilcox 		(void) printf("COULD NOT %s SUPERBLOCK AT %d: EOF\n",
1379355d6bb5Sswilcox 		    what, (int)bno);
1380355d6bb5Sswilcox 	else
1381355d6bb5Sswilcox 		(void) printf("SHORT %s SUPERBLOCK AT %d: %u out of %u bytes\n",
1382355d6bb5Sswilcox 		    what, (int)bno, (unsigned)failure, (unsigned)expected);
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate /*
13867c478bd9Sstevel@tonic-gate  * print out clean info
13877c478bd9Sstevel@tonic-gate  */
1388355d6bb5Sswilcox void
1389355d6bb5Sswilcox printclean(void)
13907c478bd9Sstevel@tonic-gate {
1391355d6bb5Sswilcox 	caddr_t s;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	if (FSOKAY != (sblock.fs_state + sblock.fs_time) && !errorlocked)
13947c478bd9Sstevel@tonic-gate 		s = "unknown";
13957c478bd9Sstevel@tonic-gate 	else
13967c478bd9Sstevel@tonic-gate 		switch (sblock.fs_clean) {
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 		case FSACTIVE:
13997c478bd9Sstevel@tonic-gate 			s = "active";
14007c478bd9Sstevel@tonic-gate 			break;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 		case FSCLEAN:
14037c478bd9Sstevel@tonic-gate 			s = "clean";
14047c478bd9Sstevel@tonic-gate 			break;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 		case FSSTABLE:
14077c478bd9Sstevel@tonic-gate 			s = "stable";
14087c478bd9Sstevel@tonic-gate 			break;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 		case FSLOG:
14117c478bd9Sstevel@tonic-gate 			s = "logging";
14127c478bd9Sstevel@tonic-gate 			break;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 		case FSBAD:
14157c478bd9Sstevel@tonic-gate 			s = "is bad";
14167c478bd9Sstevel@tonic-gate 			break;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 		case FSFIX:
14197c478bd9Sstevel@tonic-gate 			s = "being fixed";
14207c478bd9Sstevel@tonic-gate 			break;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 		default:
14237c478bd9Sstevel@tonic-gate 			s = "unknown";
14247c478bd9Sstevel@tonic-gate 		}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	if (preen)
14277c478bd9Sstevel@tonic-gate 		pwarn("is %s.\n", s);
14287c478bd9Sstevel@tonic-gate 	else
1429355d6bb5Sswilcox 		(void) printf("** %s is %s.\n", devname, s);
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate 
1432355d6bb5Sswilcox int
1433355d6bb5Sswilcox is_errorlocked(caddr_t fs)
14347c478bd9Sstevel@tonic-gate {
1435355d6bb5Sswilcox 	int		retval;
1436355d6bb5Sswilcox 	struct stat64	statb;
1437355d6bb5Sswilcox 	caddr_t		mountp;
1438355d6bb5Sswilcox 	struct mnttab	*mntent;
14397c478bd9Sstevel@tonic-gate 
1440355d6bb5Sswilcox 	retval = 0;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	if (!fs)
14437c478bd9Sstevel@tonic-gate 		return (0);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (stat64(fs, &statb) < 0)
14467c478bd9Sstevel@tonic-gate 		return (0);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (S_ISDIR(statb.st_mode)) {
14497c478bd9Sstevel@tonic-gate 		mountp = fs;
14507c478bd9Sstevel@tonic-gate 	} else if (S_ISBLK(statb.st_mode) || S_ISCHR(statb.st_mode)) {
1451355d6bb5Sswilcox 		mntent = search_mnttab(NULL, fs, NULL, 0);
1452355d6bb5Sswilcox 		if (mntent == NULL)
1453355d6bb5Sswilcox 			return (0);
1454355d6bb5Sswilcox 		mountp = mntent->mnt_mountp;
1455355d6bb5Sswilcox 		if (mountp == NULL) /* theoretically a can't-happen */
14567c478bd9Sstevel@tonic-gate 			return (0);
14577c478bd9Sstevel@tonic-gate 	} else {
14587c478bd9Sstevel@tonic-gate 		return (0);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
1461355d6bb5Sswilcox 	/*
1462355d6bb5Sswilcox 	 * From here on, must `goto out' to avoid memory leakage.
1463355d6bb5Sswilcox 	 */
1464355d6bb5Sswilcox 
1465355d6bb5Sswilcox 	if (elock_combuf == NULL)
14667c478bd9Sstevel@tonic-gate 		elock_combuf =
14671493b746SMilan Cermak 		    (caddr_t)calloc(LOCKFS_MAXCOMMENTLEN, sizeof (char));
1468355d6bb5Sswilcox 	else
14697c478bd9Sstevel@tonic-gate 		elock_combuf =
14701493b746SMilan Cermak 		    (caddr_t)realloc(elock_combuf, LOCKFS_MAXCOMMENTLEN);
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	if (elock_combuf == NULL)
1473355d6bb5Sswilcox 		goto out;
14747c478bd9Sstevel@tonic-gate 
1475355d6bb5Sswilcox 	(void) memset((void *)elock_combuf, 0, LOCKFS_MAXCOMMENTLEN);
1476355d6bb5Sswilcox 
1477355d6bb5Sswilcox 	if (elock_mountp != NULL) {
1478355d6bb5Sswilcox 		free(elock_mountp);
1479355d6bb5Sswilcox 	}
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	elock_mountp = strdup(mountp);
1482355d6bb5Sswilcox 	if (elock_mountp == NULL)
1483355d6bb5Sswilcox 		goto out;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	if (mountfd < 0) {
14867c478bd9Sstevel@tonic-gate 		if ((mountfd = open64(mountp, O_RDONLY)) == -1)
1487355d6bb5Sswilcox 			goto out;
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 
1490355d6bb5Sswilcox 	if (lfp == NULL) {
14917c478bd9Sstevel@tonic-gate 		lfp = (struct lockfs *)malloc(sizeof (struct lockfs));
1492355d6bb5Sswilcox 		if (lfp == NULL)
1493355d6bb5Sswilcox 			goto out;
1494355d6bb5Sswilcox 		(void) memset((void *)lfp, 0, sizeof (struct lockfs));
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	lfp->lf_comlen = LOCKFS_MAXCOMMENTLEN;
14987c478bd9Sstevel@tonic-gate 	lfp->lf_comment = elock_combuf;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	if (ioctl(mountfd, _FIOLFSS, lfp) == -1)
1501355d6bb5Sswilcox 		goto out;
1502355d6bb5Sswilcox 
1503355d6bb5Sswilcox 	/*
1504355d6bb5Sswilcox 	 * lint believes that the ioctl() (or any other function
1505355d6bb5Sswilcox 	 * taking lfp as an arg) could free lfp.  This is not the
1506355d6bb5Sswilcox 	 * case, however.
1507355d6bb5Sswilcox 	 */
1508355d6bb5Sswilcox 	retval = LOCKFS_IS_ELOCK(lfp);
15097c478bd9Sstevel@tonic-gate 
1510355d6bb5Sswilcox out:
1511355d6bb5Sswilcox 	return (retval);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
1514355d6bb5Sswilcox /*
1515355d6bb5Sswilcox  * Given a name which is known to be a directory, see if it appears
1516355d6bb5Sswilcox  * in the vfstab.  If so, return the entry's block (special) device
1517355d6bb5Sswilcox  * field via devstr.
1518355d6bb5Sswilcox  */
1519355d6bb5Sswilcox int
1520355d6bb5Sswilcox check_vfstab(caddr_t name, caddr_t devstr, size_t str_size)
1521355d6bb5Sswilcox {
1522355d6bb5Sswilcox 	return (NULL != search_vfstab(name, NULL, devstr, str_size));
1523355d6bb5Sswilcox }
15247c478bd9Sstevel@tonic-gate 
1525355d6bb5Sswilcox /*
1526355d6bb5Sswilcox  * Given a name which is known to be a directory, see if it appears
1527355d6bb5Sswilcox  * in the mnttab.  If so, return the entry's block (special) device
1528355d6bb5Sswilcox  * field via devstr.
1529355d6bb5Sswilcox  */
1530355d6bb5Sswilcox int
1531355d6bb5Sswilcox check_mnttab(caddr_t name, caddr_t devstr, size_t str_size)
1532355d6bb5Sswilcox {
1533355d6bb5Sswilcox 	return (NULL != search_mnttab(name, NULL, devstr, str_size));
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
1536355d6bb5Sswilcox /*
1537355d6bb5Sswilcox  * Search for mount point and/or special device in the given file.
1538355d6bb5Sswilcox  * The first matching entry is returned.
1539355d6bb5Sswilcox  *
1540355d6bb5Sswilcox  * If an entry is found and str_size is greater than zero, then
1541355d6bb5Sswilcox  * up to size_str bytes of the special device name from the entry
1542355d6bb5Sswilcox  * are copied to devstr.
1543355d6bb5Sswilcox  */
1544355d6bb5Sswilcox 
1545355d6bb5Sswilcox #define	SEARCH_TAB_BODY(st_type, st_file, st_mount, st_special, \
1546355d6bb5Sswilcox 			st_nuller, st_init, st_searcher) \
1547355d6bb5Sswilcox 	{ \
1548355d6bb5Sswilcox 		FILE *fp; \
1549355d6bb5Sswilcox 		struct st_type *retval = NULL; \
1550355d6bb5Sswilcox 		struct st_type key; \
1551355d6bb5Sswilcox 		static struct st_type buffer; \
1552355d6bb5Sswilcox 		\
1553355d6bb5Sswilcox 		/* LINTED ``assigned value never used'' */ \
1554355d6bb5Sswilcox 		st_nuller(&key); \
1555355d6bb5Sswilcox 		key.st_mount = mountp; \
1556355d6bb5Sswilcox 		key.st_special = special; \
1557355d6bb5Sswilcox 		st_init; \
1558355d6bb5Sswilcox 		\
1559355d6bb5Sswilcox 		if ((fp = fopen(st_file, "r")) == NULL) \
1560355d6bb5Sswilcox 			return (NULL); \
1561355d6bb5Sswilcox 		\
1562355d6bb5Sswilcox 		if (st_searcher(fp, &buffer, &key) == 0) { \
1563355d6bb5Sswilcox 			retval = &buffer; \
1564355d6bb5Sswilcox 			if (devstr != NULL && str_size > 0 && \
1565355d6bb5Sswilcox 			    buffer.st_special != NULL) { \
1566355d6bb5Sswilcox 				(void) strlcpy(devstr, buffer.st_special, \
1567355d6bb5Sswilcox 				    str_size); \
1568355d6bb5Sswilcox 			} \
1569355d6bb5Sswilcox 		} \
1570355d6bb5Sswilcox 		(void) fclose(fp); \
1571355d6bb5Sswilcox 		return (retval); \
1572355d6bb5Sswilcox 	}
1573355d6bb5Sswilcox 
1574355d6bb5Sswilcox static struct vfstab *
1575355d6bb5Sswilcox search_vfstab(caddr_t mountp, caddr_t special, caddr_t devstr, size_t str_size)
1576355d6bb5Sswilcox SEARCH_TAB_BODY(vfstab, VFSTAB, vfs_mountp, vfs_special, vfsnull,
1577355d6bb5Sswilcox 		(retval = retval), getvfsany)
1578355d6bb5Sswilcox 
1579355d6bb5Sswilcox static struct mnttab *
1580355d6bb5Sswilcox search_mnttab(caddr_t mountp, caddr_t special, caddr_t devstr, size_t str_size)
1581355d6bb5Sswilcox SEARCH_TAB_BODY(mnttab, MNTTAB, mnt_mountp, mnt_special, mntnull,
1582355d6bb5Sswilcox 		(key.mnt_fstype = MNTTYPE_UFS), getmntany)
1583355d6bb5Sswilcox 
1584355d6bb5Sswilcox int
15857c478bd9Sstevel@tonic-gate do_errorlock(int lock_type)
15867c478bd9Sstevel@tonic-gate {
1587355d6bb5Sswilcox 	caddr_t	   buf;
1588355d6bb5Sswilcox 	time_t	   now;
1589355d6bb5Sswilcox 	struct tm *local;
1590355d6bb5Sswilcox 	int	   rc;
15917c478bd9Sstevel@tonic-gate 
1592355d6bb5Sswilcox 	if (elock_combuf == NULL)
15937c478bd9Sstevel@tonic-gate 		errexit("do_errorlock(%s, %d): unallocated elock_combuf\n",
15941493b746SMilan Cermak 		    elock_mountp ? elock_mountp : "<null>",
15951493b746SMilan Cermak 		    lock_type);
15967c478bd9Sstevel@tonic-gate 
1597355d6bb5Sswilcox 	if ((buf = (caddr_t)calloc(LOCKFS_MAXCOMMENTLEN, sizeof (char))) ==
1598355d6bb5Sswilcox 	    NULL) {
15997c478bd9Sstevel@tonic-gate 		errexit("Couldn't alloc memory for temp. lock status buffer\n");
1600355d6bb5Sswilcox 	}
1601355d6bb5Sswilcox 	if (lfp == NULL) {
16027c478bd9Sstevel@tonic-gate 		errexit("do_errorlock(%s, %d): lockfs status unallocated\n",
16031493b746SMilan Cermak 		    elock_mountp, lock_type);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
1606355d6bb5Sswilcox 	(void) memmove((void *)buf, (void *)elock_combuf,
1607355d6bb5Sswilcox 	    LOCKFS_MAXCOMMENTLEN-1);
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	switch (lock_type) {
16107c478bd9Sstevel@tonic-gate 	case LOCKFS_ELOCK:
1611355d6bb5Sswilcox 		/*
1612355d6bb5Sswilcox 		 * Note that if it is error-locked, we won't get an
1613355d6bb5Sswilcox 		 * error back if we try to error-lock it again.
1614355d6bb5Sswilcox 		 */
16157c478bd9Sstevel@tonic-gate 		if (time(&now) != (time_t)-1) {
16167c478bd9Sstevel@tonic-gate 			if ((local = localtime(&now)) != NULL)
1617355d6bb5Sswilcox 				(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
16187c478bd9Sstevel@tonic-gate 		    "%s [pid:%d fsck start:%02d/%02d/%02d %02d:%02d:%02d",
1619355d6bb5Sswilcox 				    elock_combuf, (int)pid,
1620355d6bb5Sswilcox 				    local->tm_mon + 1, local->tm_mday,
16217c478bd9Sstevel@tonic-gate 				    (local->tm_year % 100), local->tm_hour,
16227c478bd9Sstevel@tonic-gate 				    local->tm_min, local->tm_sec);
16237c478bd9Sstevel@tonic-gate 			else
1624355d6bb5Sswilcox 				(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
1625355d6bb5Sswilcox 				    "%s [fsck pid %d", elock_combuf, pid);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		} else {
1628355d6bb5Sswilcox 			(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
1629355d6bb5Sswilcox 			    "%s [fsck pid %d", elock_combuf, pid);
16307c478bd9Sstevel@tonic-gate 		}
16317c478bd9Sstevel@tonic-gate 		break;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	case LOCKFS_ULOCK:
16347c478bd9Sstevel@tonic-gate 		if (time(&now) != (time_t)-1) {
16357c478bd9Sstevel@tonic-gate 			if ((local = localtime(&now)) != NULL) {
1636355d6bb5Sswilcox 				(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
16377c478bd9Sstevel@tonic-gate 				    "%s, done:%02d/%02d/%02d %02d:%02d:%02d]",
16387c478bd9Sstevel@tonic-gate 				    elock_combuf,
1639355d6bb5Sswilcox 				    local->tm_mon + 1, local->tm_mday,
16407c478bd9Sstevel@tonic-gate 				    (local->tm_year % 100), local->tm_hour,
16417c478bd9Sstevel@tonic-gate 				    local->tm_min, local->tm_sec);
16427c478bd9Sstevel@tonic-gate 			} else {
1643355d6bb5Sswilcox 				(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
1644355d6bb5Sswilcox 				    "%s]", elock_combuf);
16457c478bd9Sstevel@tonic-gate 			}
16467c478bd9Sstevel@tonic-gate 		} else {
1647355d6bb5Sswilcox 			(void) snprintf(buf, LOCKFS_MAXCOMMENTLEN,
1648355d6bb5Sswilcox 			    "%s]", elock_combuf);
16497c478bd9Sstevel@tonic-gate 		}
16507c478bd9Sstevel@tonic-gate 		if ((rc = ioctl(mountfd, _FIOLFSS, lfp)) == -1) {
1651355d6bb5Sswilcox 			pwarn("do_errorlock: unlock failed: %s\n",
1652355d6bb5Sswilcox 			    strerror(errno));
16537c478bd9Sstevel@tonic-gate 			goto out;
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 		break;
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	default:
16587c478bd9Sstevel@tonic-gate 		break;
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate 
1661355d6bb5Sswilcox 	(void) memmove((void *)elock_combuf, (void *)buf,
1662355d6bb5Sswilcox 	    LOCKFS_MAXCOMMENTLEN - 1);
16637c478bd9Sstevel@tonic-gate 
1664355d6bb5Sswilcox 	lfp->lf_lock = lock_type;
1665355d6bb5Sswilcox 	lfp->lf_comlen = LOCKFS_MAXCOMMENTLEN;
1666355d6bb5Sswilcox 	lfp->lf_comment = elock_combuf;
1667355d6bb5Sswilcox 	lfp->lf_flags = 0;
1668355d6bb5Sswilcox 	errno = 0;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	if ((rc = ioctl(mountfd, _FIOLFS, lfp)) == -1) {
16717c478bd9Sstevel@tonic-gate 		if (errno == EINVAL) {
16727c478bd9Sstevel@tonic-gate 			pwarn("Another fsck active?\n");
16737c478bd9Sstevel@tonic-gate 			iscorrupt = 0;	/* don't go away mad, just go away */
16747c478bd9Sstevel@tonic-gate 		} else {
1675355d6bb5Sswilcox 			pwarn("do_errorlock(lock_type:%d, %s) failed: %s\n",
1676355d6bb5Sswilcox 			    lock_type, elock_combuf, strerror(errno));
16777c478bd9Sstevel@tonic-gate 		}
16787c478bd9Sstevel@tonic-gate 	}
16797c478bd9Sstevel@tonic-gate out:
1680355d6bb5Sswilcox 	if (buf != NULL) {
1681355d6bb5Sswilcox 		free((void *)buf);
1682355d6bb5Sswilcox 	}
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	return (rc != -1);
16857c478bd9Sstevel@tonic-gate }
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate /*
1688355d6bb5Sswilcox  * Shadow inode support.  To register a shadow with a client is to note
1689355d6bb5Sswilcox  * that an inode (the client) refers to the shadow.
16907c478bd9Sstevel@tonic-gate  */
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate static struct shadowclients *
16937c478bd9Sstevel@tonic-gate newshadowclient(struct shadowclients *prev)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate 	struct shadowclients *rc;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	rc = (struct shadowclients *)malloc(sizeof (*rc));
16987c478bd9Sstevel@tonic-gate 	if (rc == NULL)
1699355d6bb5Sswilcox 		errexit("newshadowclient: cannot malloc shadow client");
17007c478bd9Sstevel@tonic-gate 	rc->next = prev;
17017c478bd9Sstevel@tonic-gate 	rc->nclients = 0;
17027c478bd9Sstevel@tonic-gate 
1703355d6bb5Sswilcox 	rc->client = (fsck_ino_t *)malloc(sizeof (fsck_ino_t) *
1704355d6bb5Sswilcox 	    maxshadowclients);
17057c478bd9Sstevel@tonic-gate 	if (rc->client == NULL)
1706355d6bb5Sswilcox 		errexit("newshadowclient: cannot malloc client array");
17077c478bd9Sstevel@tonic-gate 	return (rc);
17087c478bd9Sstevel@tonic-gate }
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate void
1711355d6bb5Sswilcox registershadowclient(fsck_ino_t shadow, fsck_ino_t client,
1712355d6bb5Sswilcox 	struct shadowclientinfo **info)
17137c478bd9Sstevel@tonic-gate {
17147c478bd9Sstevel@tonic-gate 	struct shadowclientinfo *sci;
17157c478bd9Sstevel@tonic-gate 	struct shadowclients *scc;
17167c478bd9Sstevel@tonic-gate 
1717355d6bb5Sswilcox 	/*
1718355d6bb5Sswilcox 	 * Already have a record for this shadow?
1719355d6bb5Sswilcox 	 */
1720355d6bb5Sswilcox 	for (sci = *info; sci != NULL; sci = sci->next)
17217c478bd9Sstevel@tonic-gate 		if (sci->shadow == shadow)
17227c478bd9Sstevel@tonic-gate 			break;
17237c478bd9Sstevel@tonic-gate 	if (sci == NULL) {
1724355d6bb5Sswilcox 		/*
1725355d6bb5Sswilcox 		 * It's a new shadow, add it to the list
1726355d6bb5Sswilcox 		 */
17277c478bd9Sstevel@tonic-gate 		sci = (struct shadowclientinfo *)malloc(sizeof (*sci));
17287c478bd9Sstevel@tonic-gate 		if (sci == NULL)
17297c478bd9Sstevel@tonic-gate 			errexit("registershadowclient: cannot malloc");
17307c478bd9Sstevel@tonic-gate 		sci->next = *info;
17317c478bd9Sstevel@tonic-gate 		*info = sci;
17327c478bd9Sstevel@tonic-gate 		sci->shadow = shadow;
17337c478bd9Sstevel@tonic-gate 		sci->totalClients = 0;
17347c478bd9Sstevel@tonic-gate 		sci->clients = newshadowclient(NULL);
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	sci->totalClients++;
17387c478bd9Sstevel@tonic-gate 	scc = sci->clients;
17397c478bd9Sstevel@tonic-gate 	if (scc->nclients >= maxshadowclients) {
17407c478bd9Sstevel@tonic-gate 		scc = newshadowclient(sci->clients);
17417c478bd9Sstevel@tonic-gate 		sci->clients = scc;
17427c478bd9Sstevel@tonic-gate 	}
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	scc->client[scc->nclients++] = client;
17457c478bd9Sstevel@tonic-gate }
17467c478bd9Sstevel@tonic-gate 
1747355d6bb5Sswilcox /*
1748355d6bb5Sswilcox  * Locate and discard a shadow.
1749355d6bb5Sswilcox  */
1750355d6bb5Sswilcox void
1751355d6bb5Sswilcox clearshadow(fsck_ino_t shadow, struct shadowclientinfo **info)
1752355d6bb5Sswilcox {
1753355d6bb5Sswilcox 	struct shadowclientinfo *sci, *prev;
1754355d6bb5Sswilcox 
1755355d6bb5Sswilcox 	/*
1756355d6bb5Sswilcox 	 * Do we have a record for this shadow?
1757355d6bb5Sswilcox 	 */
1758355d6bb5Sswilcox 	prev = NULL;
1759355d6bb5Sswilcox 	for (sci = *info; sci != NULL; sci = sci->next) {
1760355d6bb5Sswilcox 		if (sci->shadow == shadow)
1761355d6bb5Sswilcox 			break;
1762355d6bb5Sswilcox 		prev = sci;
1763355d6bb5Sswilcox 	}
1764355d6bb5Sswilcox 
1765355d6bb5Sswilcox 	if (sci != NULL) {
1766355d6bb5Sswilcox 		/*
1767355d6bb5Sswilcox 		 * First, pull it off the list, since we know there
1768355d6bb5Sswilcox 		 * shouldn't be any future references to this one.
1769355d6bb5Sswilcox 		 */
1770355d6bb5Sswilcox 		if (prev == NULL)
1771355d6bb5Sswilcox 			*info = sci->next;
1772355d6bb5Sswilcox 		else
1773355d6bb5Sswilcox 			prev->next = sci->next;
1774355d6bb5Sswilcox 		deshadow(sci, clearattrref);
1775355d6bb5Sswilcox 	}
1776355d6bb5Sswilcox }
1777355d6bb5Sswilcox 
1778355d6bb5Sswilcox /*
1779355d6bb5Sswilcox  * Discard all memory used to track clients of a shadow.
1780355d6bb5Sswilcox  */
1781355d6bb5Sswilcox void
1782355d6bb5Sswilcox deshadow(struct shadowclientinfo *sci, void (*cb)(fsck_ino_t))
1783355d6bb5Sswilcox {
1784355d6bb5Sswilcox 	struct shadowclients *clients, *discard;
1785355d6bb5Sswilcox 	int idx;
1786355d6bb5Sswilcox 
1787355d6bb5Sswilcox 	clients = sci->clients;
1788355d6bb5Sswilcox 	while (clients != NULL) {
1789355d6bb5Sswilcox 		discard = clients;
1790355d6bb5Sswilcox 		clients = clients->next;
1791355d6bb5Sswilcox 		if (discard->client != NULL) {
1792355d6bb5Sswilcox 			if (cb != NULL) {
1793355d6bb5Sswilcox 				for (idx = 0; idx < discard->nclients; idx++)
1794355d6bb5Sswilcox 					(*cb)(discard->client[idx]);
1795355d6bb5Sswilcox 			}
1796355d6bb5Sswilcox 			free((void *)discard->client);
1797355d6bb5Sswilcox 		}
1798355d6bb5Sswilcox 		free((void *)discard);
1799355d6bb5Sswilcox 	}
1800355d6bb5Sswilcox 
1801355d6bb5Sswilcox 	free((void *)sci);
1802355d6bb5Sswilcox }
1803355d6bb5Sswilcox 
18047c478bd9Sstevel@tonic-gate /*
18057c478bd9Sstevel@tonic-gate  * Allocate more buffer as need arises but allocate one at a time.
18067c478bd9Sstevel@tonic-gate  * This is done to make sure that fsck does not exit with error if it
1807355d6bb5Sswilcox  * needs more buffer to complete its task.
18087c478bd9Sstevel@tonic-gate  */
18097c478bd9Sstevel@tonic-gate static struct bufarea *
1810355d6bb5Sswilcox alloc_bufarea(void)
18117c478bd9Sstevel@tonic-gate {
1812355d6bb5Sswilcox 	struct bufarea *newbp;
1813355d6bb5Sswilcox 	caddr_t bufp;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	bufp = malloc((unsigned int)sblock.fs_bsize);
1816355d6bb5Sswilcox 	if (bufp == NULL)
1817355d6bb5Sswilcox 		return (NULL);
1818355d6bb5Sswilcox 
1819355d6bb5Sswilcox 	newbp = (struct bufarea *)malloc(sizeof (struct bufarea));
1820355d6bb5Sswilcox 	if (newbp == NULL) {
1821355d6bb5Sswilcox 		free((void *)bufp);
18227c478bd9Sstevel@tonic-gate 		return (NULL);
18237c478bd9Sstevel@tonic-gate 	}
1824355d6bb5Sswilcox 
1825355d6bb5Sswilcox 	initbarea(newbp);
1826355d6bb5Sswilcox 	newbp->b_un.b_buf = bufp;
1827355d6bb5Sswilcox 	newbp->b_prev = &bufhead;
1828355d6bb5Sswilcox 	newbp->b_next = bufhead.b_next;
1829355d6bb5Sswilcox 	bufhead.b_next->b_prev = newbp;
1830355d6bb5Sswilcox 	bufhead.b_next = newbp;
18317c478bd9Sstevel@tonic-gate 	bufhead.b_size++;
1832355d6bb5Sswilcox 	return (newbp);
1833355d6bb5Sswilcox }
1834355d6bb5Sswilcox 
1835355d6bb5Sswilcox /*
1836355d6bb5Sswilcox  * We length-limit in both unrawname() and rawname() to avoid
1837355d6bb5Sswilcox  * overflowing our arrays or those of our naive, trusting callers.
1838355d6bb5Sswilcox  */
1839355d6bb5Sswilcox 
1840355d6bb5Sswilcox caddr_t
1841355d6bb5Sswilcox unrawname(caddr_t name)
1842355d6bb5Sswilcox {
1843355d6bb5Sswilcox 	caddr_t dp;
1844355d6bb5Sswilcox 	static char fullname[MAXPATHLEN + 1];
1845355d6bb5Sswilcox 
1846355d6bb5Sswilcox 	if ((dp = getfullblkname(name)) == NULL)
1847355d6bb5Sswilcox 		return ("");
1848355d6bb5Sswilcox 
1849355d6bb5Sswilcox 	(void) strlcpy(fullname, dp, sizeof (fullname));
1850355d6bb5Sswilcox 	/*
1851355d6bb5Sswilcox 	 * Not reporting under debug, as the allocation isn't
1852355d6bb5Sswilcox 	 * reported by getfullblkname.  The idea is that we
1853355d6bb5Sswilcox 	 * produce balanced alloc/free instances.
1854355d6bb5Sswilcox 	 */
1855355d6bb5Sswilcox 	free(dp);
1856355d6bb5Sswilcox 
1857355d6bb5Sswilcox 	return (fullname);
1858355d6bb5Sswilcox }
1859355d6bb5Sswilcox 
1860355d6bb5Sswilcox caddr_t
1861355d6bb5Sswilcox rawname(caddr_t name)
1862355d6bb5Sswilcox {
1863355d6bb5Sswilcox 	caddr_t dp;
1864355d6bb5Sswilcox 	static char fullname[MAXPATHLEN + 1];
1865355d6bb5Sswilcox 
1866355d6bb5Sswilcox 	if ((dp = getfullrawname(name)) == NULL)
1867355d6bb5Sswilcox 		return ("");
1868355d6bb5Sswilcox 
1869355d6bb5Sswilcox 	(void) strlcpy(fullname, dp, sizeof (fullname));
1870355d6bb5Sswilcox 	/*
1871355d6bb5Sswilcox 	 * Not reporting under debug, as the allocation isn't
1872355d6bb5Sswilcox 	 * reported by getfullblkname.  The idea is that we
1873355d6bb5Sswilcox 	 * produce balanced alloc/free instances.
1874355d6bb5Sswilcox 	 */
1875355d6bb5Sswilcox 	free(dp);
1876355d6bb5Sswilcox 
1877355d6bb5Sswilcox 	return (fullname);
1878355d6bb5Sswilcox }
1879355d6bb5Sswilcox 
1880355d6bb5Sswilcox /*
1881355d6bb5Sswilcox  * Make sure that a cg header looks at least moderately reasonable.
1882355d6bb5Sswilcox  * We want to be able to trust the contents enough to be able to use
1883355d6bb5Sswilcox  * the standard accessor macros.  So, besides looking at the obvious
1884355d6bb5Sswilcox  * such as the magic number, we verify that the offset field values
1885355d6bb5Sswilcox  * are properly aligned and not too big or small.
1886355d6bb5Sswilcox  *
1887355d6bb5Sswilcox  * Returns a NULL pointer if the cg is sane enough for our needs, else
1888355d6bb5Sswilcox  * a dynamically-allocated string describing all of its faults.
1889355d6bb5Sswilcox  */
1890355d6bb5Sswilcox #define	Append_Error(full, full_len, addition, addition_len) \
1891355d6bb5Sswilcox 	if (full == NULL) { \
1892355d6bb5Sswilcox 		full = addition; \
1893355d6bb5Sswilcox 		full_len = addition_len; \
1894355d6bb5Sswilcox 	} else { \
1895355d6bb5Sswilcox 		/* lint doesn't think realloc() understands NULLs */ \
1896355d6bb5Sswilcox 		full = realloc(full, full_len + addition_len + 1); \
1897355d6bb5Sswilcox 		if (full == NULL) { \
1898355d6bb5Sswilcox 			errexit("Out of memory in cg_sanity"); \
1899355d6bb5Sswilcox 			/* NOTREACHED */ \
1900355d6bb5Sswilcox 		} \
1901355d6bb5Sswilcox 		(void) strcpy(full + full_len, addition); \
1902355d6bb5Sswilcox 		full_len += addition_len; \
1903355d6bb5Sswilcox 		free(addition); \
1904355d6bb5Sswilcox 	}
1905355d6bb5Sswilcox 
1906355d6bb5Sswilcox caddr_t
190777a343abSabalfour cg_sanity(struct cg *cgp, int cgno)
1908355d6bb5Sswilcox {
1909355d6bb5Sswilcox 	caddr_t full_err;
1910355d6bb5Sswilcox 	caddr_t this_err = NULL;
1911355d6bb5Sswilcox 	int full_len, this_len;
1912355d6bb5Sswilcox 	daddr32_t ndblk;
1913355d6bb5Sswilcox 	daddr32_t exp_btotoff, exp_boff, exp_iusedoff;
1914355d6bb5Sswilcox 	daddr32_t exp_freeoff, exp_nextfreeoff;
1915355d6bb5Sswilcox 
1916355d6bb5Sswilcox 	cg_constants(cgno, &exp_btotoff, &exp_boff, &exp_iusedoff,
1917355d6bb5Sswilcox 	    &exp_freeoff, &exp_nextfreeoff, &ndblk);
1918355d6bb5Sswilcox 
1919355d6bb5Sswilcox 	full_err = NULL;
1920355d6bb5Sswilcox 	full_len = 0;
1921355d6bb5Sswilcox 
1922355d6bb5Sswilcox 	if (!cg_chkmagic(cgp)) {
1923355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1924355d6bb5Sswilcox 		    "BAD CG MAGIC NUMBER (0x%x should be 0x%x)\n",
1925355d6bb5Sswilcox 		    cgp->cg_magic, CG_MAGIC);
1926355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1927355d6bb5Sswilcox 	}
1928355d6bb5Sswilcox 
1929355d6bb5Sswilcox 	if (cgp->cg_cgx != cgno) {
1930355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1931355d6bb5Sswilcox 		    "WRONG CG NUMBER (%d should be %d)\n",
1932355d6bb5Sswilcox 		    cgp->cg_cgx, cgno);
1933355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1934355d6bb5Sswilcox 	}
1935355d6bb5Sswilcox 
1936355d6bb5Sswilcox 	if ((cgp->cg_btotoff & 3) != 0) {
1937355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1938355d6bb5Sswilcox 		    "BLOCK TOTALS OFFSET %d NOT FOUR-BYTE ALIGNED\n",
1939355d6bb5Sswilcox 		    cgp->cg_btotoff);
1940355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1941355d6bb5Sswilcox 	}
1942355d6bb5Sswilcox 
1943355d6bb5Sswilcox 	if ((cgp->cg_boff & 1) != 0) {
1944355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1945355d6bb5Sswilcox 	    "FREE BLOCK POSITIONS TABLE OFFSET %d NOT TWO-BYTE ALIGNED\n",
1946355d6bb5Sswilcox 		    cgp->cg_boff);
1947355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1948355d6bb5Sswilcox 	}
1949355d6bb5Sswilcox 
1950355d6bb5Sswilcox 	if ((cgp->cg_ncyl < 1) || (cgp->cg_ncyl > sblock.fs_cpg)) {
1951355d6bb5Sswilcox 		if (cgp->cg_ncyl < 1) {
1952355d6bb5Sswilcox 			this_len = fsck_asprintf(&this_err,
1953355d6bb5Sswilcox 	    "IMPOSSIBLE NUMBER OF CYLINDERS IN GROUP (%d is less than 1)\n",
1954355d6bb5Sswilcox 			    cgp->cg_ncyl);
1955355d6bb5Sswilcox 		} else {
1956355d6bb5Sswilcox 			this_len = fsck_asprintf(&this_err,
1957355d6bb5Sswilcox 	    "IMPOSSIBLE NUMBER OF CYLINDERS IN GROUP (%d is greater than %d)\n",
1958355d6bb5Sswilcox 			    cgp->cg_ncyl, sblock.fs_cpg);
1959355d6bb5Sswilcox 		}
1960355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1961355d6bb5Sswilcox 	}
1962355d6bb5Sswilcox 
1963355d6bb5Sswilcox 	if (cgp->cg_niblk != sblock.fs_ipg) {
1964355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1965355d6bb5Sswilcox 		    "INCORRECT NUMBER OF INODES IN GROUP (%d should be %d)\n",
1966355d6bb5Sswilcox 		    cgp->cg_niblk, sblock.fs_ipg);
1967355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1968355d6bb5Sswilcox 	}
1969355d6bb5Sswilcox 
1970355d6bb5Sswilcox 	if (cgp->cg_ndblk != ndblk) {
1971355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1972355d6bb5Sswilcox 	    "INCORRECT NUMBER OF DATA BLOCKS IN GROUP (%d should be %d)\n",
1973355d6bb5Sswilcox 		    cgp->cg_ndblk, ndblk);
1974355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1975355d6bb5Sswilcox 	}
1976355d6bb5Sswilcox 
1977355d6bb5Sswilcox 	if ((cgp->cg_rotor < 0) || (cgp->cg_rotor >= ndblk)) {
1978355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1979355d6bb5Sswilcox 		    "IMPOSSIBLE BLOCK ALLOCATION ROTOR POSITION "
1980355d6bb5Sswilcox 		    "(%d should be at least 0 and less than %d)\n",
1981355d6bb5Sswilcox 		    cgp->cg_rotor, ndblk);
1982355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1983355d6bb5Sswilcox 	}
1984355d6bb5Sswilcox 
1985355d6bb5Sswilcox 	if ((cgp->cg_frotor < 0) || (cgp->cg_frotor >= ndblk)) {
1986355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1987355d6bb5Sswilcox 		    "IMPOSSIBLE FRAGMENT ALLOCATION ROTOR POSITION "
1988355d6bb5Sswilcox 		    "(%d should be at least 0 and less than %d)\n",
1989355d6bb5Sswilcox 		    cgp->cg_frotor, ndblk);
1990355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1991355d6bb5Sswilcox 	}
1992355d6bb5Sswilcox 
1993355d6bb5Sswilcox 	if ((cgp->cg_irotor < 0) || (cgp->cg_irotor >= sblock.fs_ipg)) {
1994355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
1995355d6bb5Sswilcox 		    "IMPOSSIBLE INODE ALLOCATION ROTOR POSITION "
1996355d6bb5Sswilcox 		    "(%d should be at least 0 and less than %d)\n",
1997355d6bb5Sswilcox 		    cgp->cg_irotor, sblock.fs_ipg);
1998355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
1999355d6bb5Sswilcox 	}
2000355d6bb5Sswilcox 
2001355d6bb5Sswilcox 	if (cgp->cg_btotoff != exp_btotoff) {
2002355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
2003355d6bb5Sswilcox 		    "INCORRECT BLOCK TOTALS OFFSET (%d should be %d)\n",
2004355d6bb5Sswilcox 		    cgp->cg_btotoff, exp_btotoff);
2005355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
2006355d6bb5Sswilcox 	}
2007355d6bb5Sswilcox 
2008355d6bb5Sswilcox 	if (cgp->cg_boff != exp_boff) {
2009355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
2010355d6bb5Sswilcox 		    "BAD FREE BLOCK POSITIONS TABLE OFFSET (%d should %d)\n",
2011355d6bb5Sswilcox 		    cgp->cg_boff, exp_boff);
2012355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
2013355d6bb5Sswilcox 	}
2014355d6bb5Sswilcox 
2015355d6bb5Sswilcox 	if (cgp->cg_iusedoff != exp_iusedoff) {
2016355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
2017355d6bb5Sswilcox 		    "INCORRECT USED INODE MAP OFFSET (%d should be %d)\n",
2018355d6bb5Sswilcox 		    cgp->cg_iusedoff, exp_iusedoff);
2019355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
2020355d6bb5Sswilcox 	}
2021355d6bb5Sswilcox 
2022355d6bb5Sswilcox 	if (cgp->cg_freeoff != exp_freeoff) {
2023355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
2024355d6bb5Sswilcox 		    "INCORRECT FREE FRAGMENT MAP OFFSET (%d should be %d)\n",
2025355d6bb5Sswilcox 		    cgp->cg_freeoff, exp_freeoff);
2026355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
2027355d6bb5Sswilcox 	}
2028355d6bb5Sswilcox 
2029355d6bb5Sswilcox 	if (cgp->cg_nextfreeoff != exp_nextfreeoff) {
2030355d6bb5Sswilcox 		this_len = fsck_asprintf(&this_err,
2031355d6bb5Sswilcox 		    "END OF HEADER POSITION INCORRECT (%d should be %d)\n",
2032355d6bb5Sswilcox 		    cgp->cg_nextfreeoff, exp_nextfreeoff);
2033355d6bb5Sswilcox 		Append_Error(full_err, full_len, this_err, this_len);
2034355d6bb5Sswilcox 	}
2035355d6bb5Sswilcox 
2036355d6bb5Sswilcox 	return (full_err);
2037355d6bb5Sswilcox }
2038355d6bb5Sswilcox 
2039355d6bb5Sswilcox #undef	Append_Error
2040355d6bb5Sswilcox 
2041355d6bb5Sswilcox /*
2042355d6bb5Sswilcox  * This is taken from mkfs, and is what is used to come up with the
2043355d6bb5Sswilcox  * original values for a struct cg.  This implies that, since these
2044355d6bb5Sswilcox  * are all constants, recalculating them now should give us the same
2045355d6bb5Sswilcox  * thing as what's on disk.
2046355d6bb5Sswilcox  */
2047355d6bb5Sswilcox static void
2048355d6bb5Sswilcox cg_constants(int cgno, daddr32_t *btotoff, daddr32_t *boff,
2049355d6bb5Sswilcox 	daddr32_t *iusedoff, daddr32_t *freeoff, daddr32_t *nextfreeoff,
2050355d6bb5Sswilcox 	daddr32_t *ndblk)
2051355d6bb5Sswilcox {
2052355d6bb5Sswilcox 	daddr32_t cbase, dmax;
2053355d6bb5Sswilcox 	struct cg *cgp;
2054355d6bb5Sswilcox 
2055355d6bb5Sswilcox 	(void) getblk(&cgblk, (diskaddr_t)cgtod(&sblock, cgno),
2056355d6bb5Sswilcox 	    (size_t)sblock.fs_cgsize);
2057355d6bb5Sswilcox 	cgp = cgblk.b_un.b_cg;
2058355d6bb5Sswilcox 
2059355d6bb5Sswilcox 	cbase = cgbase(&sblock, cgno);
2060355d6bb5Sswilcox 	dmax = cbase + sblock.fs_fpg;
2061355d6bb5Sswilcox 	if (dmax > sblock.fs_size)
2062355d6bb5Sswilcox 		dmax = sblock.fs_size;
2063355d6bb5Sswilcox 
2064355d6bb5Sswilcox 	/* LINTED pointer difference won't overflow */
2065355d6bb5Sswilcox 	*btotoff = &cgp->cg_space[0] - (uchar_t *)(&cgp->cg_link);
2066355d6bb5Sswilcox 	*boff = *btotoff + sblock.fs_cpg * sizeof (daddr32_t);
2067355d6bb5Sswilcox 	*iusedoff = *boff + sblock.fs_cpg * sblock.fs_nrpos * sizeof (int16_t);
2068355d6bb5Sswilcox 	*freeoff = *iusedoff + howmany(sblock.fs_ipg, NBBY);
2069355d6bb5Sswilcox 	*nextfreeoff = *freeoff +
20701493b746SMilan Cermak 	    howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
2071355d6bb5Sswilcox 	*ndblk = dmax - cbase;
2072355d6bb5Sswilcox }
2073355d6bb5Sswilcox 
2074355d6bb5Sswilcox /*
2075355d6bb5Sswilcox  * Corrects all fields in the cg that can be done with the available
2076355d6bb5Sswilcox  * redundant data.
2077355d6bb5Sswilcox  */
2078355d6bb5Sswilcox void
2079355d6bb5Sswilcox fix_cg(struct cg *cgp, int cgno)
2080355d6bb5Sswilcox {
2081355d6bb5Sswilcox 	daddr32_t exp_btotoff, exp_boff, exp_iusedoff;
2082355d6bb5Sswilcox 	daddr32_t exp_freeoff, exp_nextfreeoff;
2083355d6bb5Sswilcox 	daddr32_t ndblk;
2084355d6bb5Sswilcox 
2085355d6bb5Sswilcox 	cg_constants(cgno, &exp_btotoff, &exp_boff, &exp_iusedoff,
2086355d6bb5Sswilcox 	    &exp_freeoff, &exp_nextfreeoff, &ndblk);
2087355d6bb5Sswilcox 
2088355d6bb5Sswilcox 	if (cgp->cg_cgx != cgno) {
2089355d6bb5Sswilcox 		cgp->cg_cgx = cgno;
2090355d6bb5Sswilcox 	}
2091355d6bb5Sswilcox 
2092355d6bb5Sswilcox 	if ((cgp->cg_ncyl < 1) || (cgp->cg_ncyl > sblock.fs_cpg)) {
2093ef31a55cSvk 		if (cgno == (sblock.fs_ncg - 1)) {
2094355d6bb5Sswilcox 			cgp->cg_ncyl = sblock.fs_ncyl -
20951493b746SMilan Cermak 			    (sblock.fs_cpg * cgno);
2096355d6bb5Sswilcox 		} else {
2097355d6bb5Sswilcox 			cgp->cg_ncyl = sblock.fs_cpg;
2098355d6bb5Sswilcox 		}
2099355d6bb5Sswilcox 	}
2100355d6bb5Sswilcox 
2101355d6bb5Sswilcox 	if (cgp->cg_niblk != sblock.fs_ipg) {
2102355d6bb5Sswilcox 		/*
2103355d6bb5Sswilcox 		 * This is not used by the kernel, so it's pretty
2104355d6bb5Sswilcox 		 * harmless if it's wrong.
2105355d6bb5Sswilcox 		 */
2106355d6bb5Sswilcox 		cgp->cg_niblk = sblock.fs_ipg;
2107355d6bb5Sswilcox 	}
2108355d6bb5Sswilcox 
2109355d6bb5Sswilcox 	if (cgp->cg_ndblk != ndblk) {
2110355d6bb5Sswilcox 		cgp->cg_ndblk = ndblk;
2111355d6bb5Sswilcox 	}
2112355d6bb5Sswilcox 
2113355d6bb5Sswilcox 	/*
2114355d6bb5Sswilcox 	 * For the rotors, any position's valid, so pick the one we know
2115355d6bb5Sswilcox 	 * will always exist.
2116355d6bb5Sswilcox 	 */
2117355d6bb5Sswilcox 	if ((cgp->cg_rotor < 0) || (cgp->cg_rotor >= cgp->cg_ndblk)) {
2118355d6bb5Sswilcox 		cgp->cg_rotor = 0;
2119355d6bb5Sswilcox 	}
2120355d6bb5Sswilcox 
2121355d6bb5Sswilcox 	if ((cgp->cg_frotor < 0) || (cgp->cg_frotor >= cgp->cg_ndblk)) {
2122355d6bb5Sswilcox 		cgp->cg_frotor = 0;
2123355d6bb5Sswilcox 	}
2124355d6bb5Sswilcox 
2125355d6bb5Sswilcox 	if ((cgp->cg_irotor < 0) || (cgp->cg_irotor >= sblock.fs_ipg)) {
2126355d6bb5Sswilcox 		cgp->cg_irotor = 0;
2127355d6bb5Sswilcox 	}
2128355d6bb5Sswilcox 
2129355d6bb5Sswilcox 	/*
2130355d6bb5Sswilcox 	 * For btotoff and boff, if they're misaligned they won't
2131355d6bb5Sswilcox 	 * match the expected values, so we're catching both cases
2132355d6bb5Sswilcox 	 * here.  Of course, if any of these are off, it seems likely
2133355d6bb5Sswilcox 	 * that the tables really won't be where we calculate they
2134355d6bb5Sswilcox 	 * should be anyway.
2135355d6bb5Sswilcox 	 */
2136355d6bb5Sswilcox 	if (cgp->cg_btotoff != exp_btotoff) {
2137355d6bb5Sswilcox 		cgp->cg_btotoff = exp_btotoff;
2138355d6bb5Sswilcox 	}
2139355d6bb5Sswilcox 
2140355d6bb5Sswilcox 	if (cgp->cg_boff != exp_boff) {
2141355d6bb5Sswilcox 		cgp->cg_boff = exp_boff;
2142355d6bb5Sswilcox 	}
2143355d6bb5Sswilcox 
2144355d6bb5Sswilcox 	if (cgp->cg_iusedoff != exp_iusedoff) {
2145355d6bb5Sswilcox 		cgp->cg_iusedoff = exp_iusedoff;
2146355d6bb5Sswilcox 	}
2147355d6bb5Sswilcox 
2148355d6bb5Sswilcox 	if (cgp->cg_freeoff != exp_freeoff) {
2149355d6bb5Sswilcox 		cgp->cg_freeoff = exp_freeoff;
2150355d6bb5Sswilcox 	}
2151355d6bb5Sswilcox 
2152355d6bb5Sswilcox 	if (cgp->cg_nextfreeoff != exp_nextfreeoff) {
2153355d6bb5Sswilcox 		cgp->cg_nextfreeoff = exp_nextfreeoff;
2154355d6bb5Sswilcox 	}
2155355d6bb5Sswilcox 
215677a343abSabalfour 	/*
215777a343abSabalfour 	 * Reset the magic, as we've recreated this cg, also
215877a343abSabalfour 	 * update the cg_time, as we're writing out the cg
215977a343abSabalfour 	 */
216077a343abSabalfour 	cgp->cg_magic = CG_MAGIC;
216177a343abSabalfour 	cgp->cg_time = time(NULL);
216277a343abSabalfour 
2163355d6bb5Sswilcox 	/*
2164355d6bb5Sswilcox 	 * We know there was at least one correctable problem,
2165355d6bb5Sswilcox 	 * or else we wouldn't have been called.  So instead of
2166355d6bb5Sswilcox 	 * marking the buffer dirty N times above, just do it
2167355d6bb5Sswilcox 	 * once here.
2168355d6bb5Sswilcox 	 */
2169355d6bb5Sswilcox 	cgdirty();
2170355d6bb5Sswilcox }
2171355d6bb5Sswilcox 
2172355d6bb5Sswilcox void
217339542a18Sabalfour examinelog(void (*cb)(daddr32_t))
2174355d6bb5Sswilcox {
2175355d6bb5Sswilcox 	struct bufarea *bp;
2176355d6bb5Sswilcox 	extent_block_t *ebp;
2177355d6bb5Sswilcox 	extent_t *ep;
2178355d6bb5Sswilcox 	daddr32_t nfno, fno;
2179355d6bb5Sswilcox 	int i;
2180355d6bb5Sswilcox 	int j;
2181355d6bb5Sswilcox 
218239542a18Sabalfour 	/*
218339542a18Sabalfour 	 * Since ufs stores fs_logbno as blocks and MTBufs stores it as frags
218439542a18Sabalfour 	 * we need to translate accordingly using logbtodb()
218539542a18Sabalfour 	 */
218639542a18Sabalfour 
218739542a18Sabalfour 	if (logbtodb(&sblock, sblock.fs_logbno) < SBLOCK) {
218839542a18Sabalfour 		if (debug) {
218939542a18Sabalfour 			(void) printf("fs_logbno < SBLOCK: %ld < %ld\n" \
219039542a18Sabalfour 			    "Aborting log examination\n", \
219139542a18Sabalfour 			    logbtodb(&sblock, sblock.fs_logbno), SBLOCK);
219239542a18Sabalfour 		}
2193355d6bb5Sswilcox 		return;
219439542a18Sabalfour 	}
2195355d6bb5Sswilcox 
2196355d6bb5Sswilcox 	/*
2197355d6bb5Sswilcox 	 * Read errors will return zeros, which will cause us
2198355d6bb5Sswilcox 	 * to do nothing harmful, so don't need to handle it.
2199355d6bb5Sswilcox 	 */
2200355d6bb5Sswilcox 	bp = getdatablk(logbtofrag(&sblock, sblock.fs_logbno),
22011493b746SMilan Cermak 	    (size_t)sblock.fs_bsize);
2202355d6bb5Sswilcox 	ebp = (void *)bp->b_un.b_buf;
2203355d6bb5Sswilcox 
2204355d6bb5Sswilcox 	/*
2205355d6bb5Sswilcox 	 * Does it look like a log allocation table?
2206355d6bb5Sswilcox 	 */
2207355d6bb5Sswilcox 	/* LINTED pointer cast is aligned */
2208355d6bb5Sswilcox 	if (!log_checksum(&ebp->chksum, (int32_t *)bp->b_un.b_buf,
2209355d6bb5Sswilcox 	    sblock.fs_bsize))
2210355d6bb5Sswilcox 		return;
2211355d6bb5Sswilcox 	if (ebp->type != LUFS_EXTENTS || ebp->nextents == 0)
2212355d6bb5Sswilcox 		return;
2213355d6bb5Sswilcox 
2214355d6bb5Sswilcox 	ep = &ebp->extents[0];
2215355d6bb5Sswilcox 	for (i = 0; i < ebp->nextents; ++i, ++ep) {
2216355d6bb5Sswilcox 		fno = logbtofrag(&sblock, ep->pbno);
2217355d6bb5Sswilcox 		nfno = dbtofsb(&sblock, ep->nbno);
2218355d6bb5Sswilcox 		for (j = 0; j < nfno; ++j, ++fno) {
2219355d6bb5Sswilcox 			/*
2220355d6bb5Sswilcox 			 * Invoke the callback first, so that pass1 can
2221355d6bb5Sswilcox 			 * mark the log blocks in-use.  Then, if any
2222355d6bb5Sswilcox 			 * subsequent pass over the log shows us that a
2223355d6bb5Sswilcox 			 * block got freed (say, it was also claimed by
2224355d6bb5Sswilcox 			 * an inode that we cleared), we can safely declare
2225355d6bb5Sswilcox 			 * the log bad.
2226355d6bb5Sswilcox 			 */
2227355d6bb5Sswilcox 			if (cb != NULL)
2228355d6bb5Sswilcox 				(*cb)(fno);
2229355d6bb5Sswilcox 			if (!testbmap(fno))
2230355d6bb5Sswilcox 				islogok = 0;
2231355d6bb5Sswilcox 		}
2232355d6bb5Sswilcox 	}
2233355d6bb5Sswilcox 	brelse(bp);
2234355d6bb5Sswilcox 
2235355d6bb5Sswilcox 	if (cb != NULL) {
2236355d6bb5Sswilcox 		fno = logbtofrag(&sblock, sblock.fs_logbno);
2237355d6bb5Sswilcox 		for (j = 0; j < sblock.fs_frag; ++j, ++fno)
2238355d6bb5Sswilcox 			(*cb)(fno);
2239355d6bb5Sswilcox 	}
2240355d6bb5Sswilcox }
2241355d6bb5Sswilcox 
2242355d6bb5Sswilcox static void
2243355d6bb5Sswilcox freelogblk(daddr32_t frag)
2244355d6bb5Sswilcox {
2245355d6bb5Sswilcox 	freeblk(sblock.fs_logbno, frag, 1);
2246355d6bb5Sswilcox }
2247355d6bb5Sswilcox 
2248355d6bb5Sswilcox caddr_t
2249355d6bb5Sswilcox file_id(fsck_ino_t inum, mode_t mode)
2250355d6bb5Sswilcox {
2251355d6bb5Sswilcox 	static char name[MAXPATHLEN + 1];
2252355d6bb5Sswilcox 
2253355d6bb5Sswilcox 	if (lfdir == inum) {
2254355d6bb5Sswilcox 		return (lfname);
2255355d6bb5Sswilcox 	}
2256355d6bb5Sswilcox 
2257355d6bb5Sswilcox 	if ((mode & IFMT) == IFDIR) {
2258355d6bb5Sswilcox 		(void) strcpy(name, "DIR");
2259355d6bb5Sswilcox 	} else if ((mode & IFMT) == IFATTRDIR) {
2260355d6bb5Sswilcox 		(void) strcpy(name, "ATTR DIR");
2261355d6bb5Sswilcox 	} else if ((mode & IFMT) == IFSHAD) {
2262355d6bb5Sswilcox 		(void) strcpy(name, "ACL");
2263355d6bb5Sswilcox 	} else {
2264355d6bb5Sswilcox 		(void) strcpy(name, "FILE");
2265355d6bb5Sswilcox 	}
2266355d6bb5Sswilcox 
2267355d6bb5Sswilcox 	return (name);
2268355d6bb5Sswilcox }
2269355d6bb5Sswilcox 
2270355d6bb5Sswilcox /*
2271355d6bb5Sswilcox  * Simple initializer for inodesc structures, so users of only a few
2272355d6bb5Sswilcox  * fields don't have to worry about getting the right defaults for
2273355d6bb5Sswilcox  * everything out.
2274355d6bb5Sswilcox  */
2275355d6bb5Sswilcox void
2276355d6bb5Sswilcox init_inodesc(struct inodesc *idesc)
2277355d6bb5Sswilcox {
2278355d6bb5Sswilcox 	/*
2279355d6bb5Sswilcox 	 * Most fields should be zero, just hit the special cases.
2280355d6bb5Sswilcox 	 */
2281355d6bb5Sswilcox 	(void) memset((void *)idesc, 0, sizeof (struct inodesc));
2282355d6bb5Sswilcox 	idesc->id_fix = DONTKNOW;
2283355d6bb5Sswilcox 	idesc->id_lbn = -1;
2284355d6bb5Sswilcox 	idesc->id_truncto = -1;
2285355d6bb5Sswilcox 	idesc->id_firsthole = -1;
2286355d6bb5Sswilcox }
2287355d6bb5Sswilcox 
2288355d6bb5Sswilcox /*
2289355d6bb5Sswilcox  * Compare routine for tsearch(C) to use on ino_t instances.
2290355d6bb5Sswilcox  */
2291355d6bb5Sswilcox int
2292355d6bb5Sswilcox ino_t_cmp(const void *left, const void *right)
2293355d6bb5Sswilcox {
2294355d6bb5Sswilcox 	const fsck_ino_t lino = (const fsck_ino_t)left;
2295355d6bb5Sswilcox 	const fsck_ino_t rino = (const fsck_ino_t)right;
2296355d6bb5Sswilcox 
2297355d6bb5Sswilcox 	return (lino - rino);
2298355d6bb5Sswilcox }
2299355d6bb5Sswilcox 
2300355d6bb5Sswilcox int
2301355d6bb5Sswilcox cgisdirty(void)
2302355d6bb5Sswilcox {
2303355d6bb5Sswilcox 	return (cgblk.b_dirty);
2304355d6bb5Sswilcox }
2305355d6bb5Sswilcox 
2306355d6bb5Sswilcox void
2307355d6bb5Sswilcox cgflush(void)
2308355d6bb5Sswilcox {
2309355d6bb5Sswilcox 	flush(fswritefd, &cgblk);
2310355d6bb5Sswilcox }
2311355d6bb5Sswilcox 
2312355d6bb5Sswilcox void
2313355d6bb5Sswilcox dirty(struct bufarea *bp)
2314355d6bb5Sswilcox {
2315355d6bb5Sswilcox 	if (fswritefd < 0) {
23161493b746SMilan Cermak 		/*
23171493b746SMilan Cermak 		 * No one should call dirty() in read only mode.
2318*48bbca81SDaniel Hoffman 		 * But if one does, it's not fatal issue. Just warn them.
23191493b746SMilan Cermak 		 */
23201493b746SMilan Cermak 		pwarn("WON'T SET DIRTY FLAG IN READ_ONLY MODE\n");
2321355d6bb5Sswilcox 	} else {
2322355d6bb5Sswilcox 		(bp)->b_dirty = 1;
2323355d6bb5Sswilcox 		isdirty = 1;
2324355d6bb5Sswilcox 	}
2325355d6bb5Sswilcox }
2326355d6bb5Sswilcox 
2327355d6bb5Sswilcox void
2328355d6bb5Sswilcox initbarea(struct bufarea *bp)
2329355d6bb5Sswilcox {
2330355d6bb5Sswilcox 	(bp)->b_dirty = 0;
2331355d6bb5Sswilcox 	(bp)->b_bno = (diskaddr_t)-1LL;
2332355d6bb5Sswilcox 	(bp)->b_flags = 0;
2333355d6bb5Sswilcox 	(bp)->b_cnt = 0;
2334355d6bb5Sswilcox 	(bp)->b_errs = 0;
2335355d6bb5Sswilcox }
2336355d6bb5Sswilcox 
2337355d6bb5Sswilcox /*
2338355d6bb5Sswilcox  * Partition-sizing routines adapted from ../newfs/newfs.c.
2339355d6bb5Sswilcox  * Needed because calcsb() needs to use mkfs to work out what the
2340355d6bb5Sswilcox  * superblock should be, and mkfs insists on being told how many
2341355d6bb5Sswilcox  * sectors to use.
2342355d6bb5Sswilcox  *
2343355d6bb5Sswilcox  * Error handling assumes we're never called while preening.
2344355d6bb5Sswilcox  *
2345355d6bb5Sswilcox  * XXX This should be extracted into a ../ufslib.{c,h},
2346355d6bb5Sswilcox  *     in the same spirit to ../../fslib.{c,h}.  Once that is
2347355d6bb5Sswilcox  *     done, both fsck and newfs should be modified to link
2348355d6bb5Sswilcox  *     against it.
2349355d6bb5Sswilcox  */
2350355d6bb5Sswilcox 
2351355d6bb5Sswilcox static int label_type;
2352355d6bb5Sswilcox 
2353355d6bb5Sswilcox #define	LABEL_TYPE_VTOC		1
2354355d6bb5Sswilcox #define	LABEL_TYPE_EFI		2
2355355d6bb5Sswilcox #define	LABEL_TYPE_OTHER	3
2356355d6bb5Sswilcox 
2357355d6bb5Sswilcox #define	MB			(1024 * 1024)
2358355d6bb5Sswilcox #define	SECTORS_PER_TERABYTE	(1LL << 31)
2359355d6bb5Sswilcox #define	FS_SIZE_UPPER_LIMIT	0x100000000000LL
2360355d6bb5Sswilcox 
2361355d6bb5Sswilcox diskaddr_t
2362355d6bb5Sswilcox getdisksize(caddr_t disk, int fd)
2363355d6bb5Sswilcox {
2364355d6bb5Sswilcox 	int rpm;
2365355d6bb5Sswilcox 	struct dk_geom g;
2366355d6bb5Sswilcox 	struct dk_cinfo ci;
2367355d6bb5Sswilcox 	diskaddr_t actual_size;
2368355d6bb5Sswilcox 
2369355d6bb5Sswilcox 	/*
2370355d6bb5Sswilcox 	 * get_device_size() determines the actual size of the
2371355d6bb5Sswilcox 	 * device, and also the disk's attributes, such as geometry.
2372355d6bb5Sswilcox 	 */
2373355d6bb5Sswilcox 	actual_size = get_device_size(fd, disk);
2374355d6bb5Sswilcox 
2375355d6bb5Sswilcox 	if (label_type == LABEL_TYPE_VTOC) {
2376355d6bb5Sswilcox 		if (ioctl(fd, DKIOCGGEOM, &g)) {
2377355d6bb5Sswilcox 			pwarn("%s: Unable to read Disk geometry", disk);
2378355d6bb5Sswilcox 			return (0);
2379355d6bb5Sswilcox 		}
2380355d6bb5Sswilcox 		if (sblock.fs_nsect == 0)
2381355d6bb5Sswilcox 			sblock.fs_nsect = g.dkg_nsect;
2382355d6bb5Sswilcox 		if (sblock.fs_ntrak == 0)
2383355d6bb5Sswilcox 			sblock.fs_ntrak = g.dkg_nhead;
2384355d6bb5Sswilcox 		if (sblock.fs_rps == 0) {
2385355d6bb5Sswilcox 			rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm;
2386355d6bb5Sswilcox 			sblock.fs_rps = rpm / 60;
2387355d6bb5Sswilcox 		}
2388355d6bb5Sswilcox 	}
2389355d6bb5Sswilcox 
2390355d6bb5Sswilcox 	if (sblock.fs_bsize == 0)
2391355d6bb5Sswilcox 		sblock.fs_bsize = MAXBSIZE;
2392355d6bb5Sswilcox 
2393355d6bb5Sswilcox 	/*
2394355d6bb5Sswilcox 	 * Adjust maxcontig by the device's maxtransfer. If maxtransfer
2395355d6bb5Sswilcox 	 * information is not available, default to the min of a MB and
2396355d6bb5Sswilcox 	 * maxphys.
2397355d6bb5Sswilcox 	 */
2398355d6bb5Sswilcox 	if (sblock.fs_maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) {
2399355d6bb5Sswilcox 		sblock.fs_maxcontig = ci.dki_maxtransfer * DEV_BSIZE;
2400355d6bb5Sswilcox 		if (sblock.fs_maxcontig < 0) {
2401355d6bb5Sswilcox 			int gotit, maxphys;
2402355d6bb5Sswilcox 
2403355d6bb5Sswilcox 			gotit = fsgetmaxphys(&maxphys, NULL);
2404355d6bb5Sswilcox 
2405355d6bb5Sswilcox 			/*
2406355d6bb5Sswilcox 			 * If we cannot get the maxphys value, default
2407355d6bb5Sswilcox 			 * to ufs_maxmaxphys (MB).
2408355d6bb5Sswilcox 			 */
2409355d6bb5Sswilcox 			if (gotit) {
2410355d6bb5Sswilcox 				sblock.fs_maxcontig = MIN(maxphys, MB);
2411355d6bb5Sswilcox 			} else {
2412355d6bb5Sswilcox 				sblock.fs_maxcontig = MB;
2413355d6bb5Sswilcox 			}
2414355d6bb5Sswilcox 		}
2415355d6bb5Sswilcox 		sblock.fs_maxcontig /= sblock.fs_bsize;
2416355d6bb5Sswilcox 	}
2417355d6bb5Sswilcox 
2418355d6bb5Sswilcox 	return (actual_size);
2419355d6bb5Sswilcox }
2420355d6bb5Sswilcox 
2421355d6bb5Sswilcox /*
2422355d6bb5Sswilcox  * Figure out how big the partition we're dealing with is.
2423355d6bb5Sswilcox  */
2424355d6bb5Sswilcox static diskaddr_t
2425355d6bb5Sswilcox get_device_size(int fd, caddr_t name)
2426355d6bb5Sswilcox {
2427342440ecSPrasad Singamsetty 	struct extvtoc vtoc;
2428355d6bb5Sswilcox 	struct dk_gpt *efi_vtoc;
2429355d6bb5Sswilcox 	diskaddr_t slicesize = 0;
2430355d6bb5Sswilcox 
2431342440ecSPrasad Singamsetty 	int index = read_extvtoc(fd, &vtoc);
2432355d6bb5Sswilcox 
2433355d6bb5Sswilcox 	if (index >= 0) {
2434355d6bb5Sswilcox 		label_type = LABEL_TYPE_VTOC;
2435355d6bb5Sswilcox 	} else {
2436355d6bb5Sswilcox 		if (index == VT_ENOTSUP || index == VT_ERROR) {
2437355d6bb5Sswilcox 			/* it might be an EFI label */
2438355d6bb5Sswilcox 			index = efi_alloc_and_read(fd, &efi_vtoc);
2439355d6bb5Sswilcox 			if (index >= 0)
2440355d6bb5Sswilcox 				label_type = LABEL_TYPE_EFI;
2441355d6bb5Sswilcox 		}
2442355d6bb5Sswilcox 	}
2443355d6bb5Sswilcox 
2444355d6bb5Sswilcox 	if (index < 0) {
2445355d6bb5Sswilcox 		/*
2446355d6bb5Sswilcox 		 * Since both attempts to read the label failed, we're
2447355d6bb5Sswilcox 		 * going to fall back to a brute force approach to
2448355d6bb5Sswilcox 		 * determining the device's size:  see how far out we can
2449355d6bb5Sswilcox 		 * perform reads on the device.
2450355d6bb5Sswilcox 		 */
2451355d6bb5Sswilcox 
2452355d6bb5Sswilcox 		slicesize = brute_force_get_device_size(fd);
2453355d6bb5Sswilcox 		if (slicesize == 0) {
2454355d6bb5Sswilcox 			switch (index) {
2455355d6bb5Sswilcox 			case VT_ERROR:
2456355d6bb5Sswilcox 				pwarn("%s: %s\n", name, strerror(errno));
2457355d6bb5Sswilcox 				break;
2458355d6bb5Sswilcox 			case VT_EIO:
2459355d6bb5Sswilcox 				pwarn("%s: I/O error accessing VTOC", name);
2460355d6bb5Sswilcox 				break;
2461355d6bb5Sswilcox 			case VT_EINVAL:
2462355d6bb5Sswilcox 				pwarn("%s: Invalid field in VTOC", name);
2463355d6bb5Sswilcox 				break;
2464355d6bb5Sswilcox 			default:
2465355d6bb5Sswilcox 				pwarn("%s: unknown error %d accessing VTOC",
2466355d6bb5Sswilcox 				    name, index);
2467355d6bb5Sswilcox 				break;
2468355d6bb5Sswilcox 			}
2469355d6bb5Sswilcox 			return (0);
2470355d6bb5Sswilcox 		} else {
2471355d6bb5Sswilcox 			label_type = LABEL_TYPE_OTHER;
2472355d6bb5Sswilcox 		}
2473355d6bb5Sswilcox 	}
2474355d6bb5Sswilcox 
2475355d6bb5Sswilcox 	if (label_type == LABEL_TYPE_EFI) {
2476355d6bb5Sswilcox 		slicesize = efi_vtoc->efi_parts[index].p_size;
2477355d6bb5Sswilcox 		efi_free(efi_vtoc);
2478355d6bb5Sswilcox 	} else if (label_type == LABEL_TYPE_VTOC) {
2479342440ecSPrasad Singamsetty 		slicesize = vtoc.v_part[index].p_size;
2480355d6bb5Sswilcox 	}
2481355d6bb5Sswilcox 
2482355d6bb5Sswilcox 	return (slicesize);
2483355d6bb5Sswilcox }
2484355d6bb5Sswilcox 
2485355d6bb5Sswilcox /*
2486355d6bb5Sswilcox  * brute_force_get_device_size
2487355d6bb5Sswilcox  *
2488355d6bb5Sswilcox  * Determine the size of the device by seeing how far we can
2489355d6bb5Sswilcox  * read.  Doing an llseek( , , SEEK_END) would probably work
2490355d6bb5Sswilcox  * in most cases, but we've seen at least one third-party driver
2491355d6bb5Sswilcox  * which doesn't correctly support the SEEK_END option when the
2492355d6bb5Sswilcox  * the device is greater than a terabyte.
2493355d6bb5Sswilcox  */
2494355d6bb5Sswilcox 
2495355d6bb5Sswilcox static diskaddr_t
2496355d6bb5Sswilcox brute_force_get_device_size(int fd)
2497355d6bb5Sswilcox {
2498355d6bb5Sswilcox 	diskaddr_t	min_fail = 0;
2499355d6bb5Sswilcox 	diskaddr_t	max_succeed = 0;
2500355d6bb5Sswilcox 	diskaddr_t	cur_db_off;
2501355d6bb5Sswilcox 	char 		buf[DEV_BSIZE];
2502355d6bb5Sswilcox 
2503355d6bb5Sswilcox 	/*
2504355d6bb5Sswilcox 	 * First, see if we can read the device at all, just to
2505355d6bb5Sswilcox 	 * eliminate errors that have nothing to do with the
2506355d6bb5Sswilcox 	 * device's size.
2507355d6bb5Sswilcox 	 */
2508355d6bb5Sswilcox 
2509355d6bb5Sswilcox 	if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) ||
2510355d6bb5Sswilcox 	    ((read(fd, buf, DEV_BSIZE)) == -1))
2511355d6bb5Sswilcox 		return (0);  /* can't determine size */
2512355d6bb5Sswilcox 
2513355d6bb5Sswilcox 	/*
2514355d6bb5Sswilcox 	 * Now, go sequentially through the multiples of 4TB
2515355d6bb5Sswilcox 	 * to find the first read that fails (this isn't strictly
2516355d6bb5Sswilcox 	 * the most efficient way to find the actual size if the
2517355d6bb5Sswilcox 	 * size really could be anything between 0 and 2**64 bytes.
2518355d6bb5Sswilcox 	 * We expect the sizes to be less than 16 TB for some time,
2519355d6bb5Sswilcox 	 * so why do a bunch of reads that are larger than that?
2520355d6bb5Sswilcox 	 * However, this algorithm *will* work for sizes of greater
2521355d6bb5Sswilcox 	 * than 16 TB.  We're just not optimizing for those sizes.)
2522355d6bb5Sswilcox 	 */
2523355d6bb5Sswilcox 
2524355d6bb5Sswilcox 	/*
2525355d6bb5Sswilcox 	 * XXX lint uses 32-bit arithmetic for doing flow analysis.
2526355d6bb5Sswilcox 	 * We're using > 32-bit constants here.  Therefore, its flow
2527355d6bb5Sswilcox 	 * analysis is wrong.  For the time being, ignore complaints
2528355d6bb5Sswilcox 	 * from it about the body of the for() being unreached.
2529355d6bb5Sswilcox 	 */
2530355d6bb5Sswilcox 	for (cur_db_off = SECTORS_PER_TERABYTE * 4;
2531355d6bb5Sswilcox 	    (min_fail == 0) && (cur_db_off < FS_SIZE_UPPER_LIMIT);
2532355d6bb5Sswilcox 	    cur_db_off += 4 * SECTORS_PER_TERABYTE) {
2533355d6bb5Sswilcox 		if ((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
2534355d6bb5Sswilcox 		    SEEK_SET) == -1) ||
2535355d6bb5Sswilcox 		    (read(fd, buf, DEV_BSIZE) != DEV_BSIZE))
2536355d6bb5Sswilcox 			min_fail = cur_db_off;
2537355d6bb5Sswilcox 		else
2538355d6bb5Sswilcox 			max_succeed = cur_db_off;
2539355d6bb5Sswilcox 	}
2540355d6bb5Sswilcox 
2541355d6bb5Sswilcox 	/*
2542355d6bb5Sswilcox 	 * XXX Same lint flow analysis problem as above.
2543355d6bb5Sswilcox 	 */
2544355d6bb5Sswilcox 	if (min_fail == 0)
2545355d6bb5Sswilcox 		return (0);
2546355d6bb5Sswilcox 
2547355d6bb5Sswilcox 	/*
2548355d6bb5Sswilcox 	 * We now know that the size of the device is less than
2549355d6bb5Sswilcox 	 * min_fail and greater than or equal to max_succeed.  Now
2550355d6bb5Sswilcox 	 * keep splitting the difference until the actual size in
2551355d6bb5Sswilcox 	 * sectors in known.  We also know that the difference
2552355d6bb5Sswilcox 	 * between max_succeed and min_fail at this time is
2553355d6bb5Sswilcox 	 * 4 * SECTORS_PER_TERABYTE, which is a power of two, which
2554355d6bb5Sswilcox 	 * simplifies the math below.
2555355d6bb5Sswilcox 	 */
2556355d6bb5Sswilcox 
2557355d6bb5Sswilcox 	while (min_fail - max_succeed > 1) {
2558355d6bb5Sswilcox 		cur_db_off = max_succeed + (min_fail - max_succeed)/2;
2559355d6bb5Sswilcox 		if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE),
2560355d6bb5Sswilcox 		    SEEK_SET)) == -1) ||
2561355d6bb5Sswilcox 		    ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE))
2562355d6bb5Sswilcox 			min_fail = cur_db_off;
2563355d6bb5Sswilcox 		else
2564355d6bb5Sswilcox 			max_succeed = cur_db_off;
2565355d6bb5Sswilcox 	}
2566355d6bb5Sswilcox 
2567355d6bb5Sswilcox 	/* the size is the last successfully read sector offset plus one */
2568355d6bb5Sswilcox 	return (max_succeed + 1);
2569355d6bb5Sswilcox }
2570355d6bb5Sswilcox 
2571355d6bb5Sswilcox static void
2572355d6bb5Sswilcox vfileerror(fsck_ino_t cwd, fsck_ino_t ino, caddr_t fmt, va_list ap)
2573355d6bb5Sswilcox {
2574355d6bb5Sswilcox 	struct dinode *dp;
2575355d6bb5Sswilcox 	char pathbuf[MAXPATHLEN + 1];
2576355d6bb5Sswilcox 
2577355d6bb5Sswilcox 	vpwarn(fmt, ap);
2578355d6bb5Sswilcox 	(void) putchar(' ');
2579355d6bb5Sswilcox 	pinode(ino);
2580355d6bb5Sswilcox 	(void) printf("\n");
2581355d6bb5Sswilcox 	getpathname(pathbuf, cwd, ino);
2582355d6bb5Sswilcox 	if (ino < UFSROOTINO || ino > maxino) {
2583355d6bb5Sswilcox 		pfatal("NAME=%s\n", pathbuf);
2584355d6bb5Sswilcox 		return;
2585355d6bb5Sswilcox 	}
2586355d6bb5Sswilcox 	dp = ginode(ino);
2587355d6bb5Sswilcox 	if (ftypeok(dp))
2588355d6bb5Sswilcox 		pfatal("%s=%s\n", file_id(ino, dp->di_mode), pathbuf);
2589355d6bb5Sswilcox 	else
2590355d6bb5Sswilcox 		pfatal("NAME=%s\n", pathbuf);
2591355d6bb5Sswilcox }
2592355d6bb5Sswilcox 
2593355d6bb5Sswilcox void
2594355d6bb5Sswilcox direrror(fsck_ino_t ino, caddr_t fmt, ...)
2595355d6bb5Sswilcox {
2596355d6bb5Sswilcox 	va_list ap;
2597355d6bb5Sswilcox 
2598355d6bb5Sswilcox 	va_start(ap, fmt);
2599355d6bb5Sswilcox 	vfileerror(ino, ino, fmt, ap);
2600355d6bb5Sswilcox 	va_end(ap);
2601355d6bb5Sswilcox }
2602355d6bb5Sswilcox 
2603355d6bb5Sswilcox static void
2604355d6bb5Sswilcox vdirerror(fsck_ino_t ino, caddr_t fmt, va_list ap)
2605355d6bb5Sswilcox {
2606355d6bb5Sswilcox 	vfileerror(ino, ino, fmt, ap);
2607355d6bb5Sswilcox }
2608355d6bb5Sswilcox 
2609355d6bb5Sswilcox void
2610355d6bb5Sswilcox fileerror(fsck_ino_t cwd, fsck_ino_t ino, caddr_t fmt, ...)
2611355d6bb5Sswilcox {
2612355d6bb5Sswilcox 	va_list ap;
2613355d6bb5Sswilcox 
2614355d6bb5Sswilcox 	va_start(ap, fmt);
2615355d6bb5Sswilcox 	vfileerror(cwd, ino, fmt, ap);
2616355d6bb5Sswilcox 	va_end(ap);
2617355d6bb5Sswilcox }
2618355d6bb5Sswilcox 
2619355d6bb5Sswilcox /*
2620355d6bb5Sswilcox  * Adds the given inode to the orphaned-directories list, limbo_dirs.
2621355d6bb5Sswilcox  * Assumes that the caller has set INCLEAR in the inode's statemap[]
2622355d6bb5Sswilcox  * entry.
2623355d6bb5Sswilcox  *
2624355d6bb5Sswilcox  * With INCLEAR set, the inode will get ignored by passes 2 and 3,
2625355d6bb5Sswilcox  * meaning it's effectively an orphan.  It needs to be noted now, so
2626355d6bb5Sswilcox  * it will be remembered in pass 4.
2627355d6bb5Sswilcox  */
2628355d6bb5Sswilcox 
2629355d6bb5Sswilcox void
2630355d6bb5Sswilcox add_orphan_dir(fsck_ino_t ino)
2631355d6bb5Sswilcox {
2632355d6bb5Sswilcox 	if (tsearch((void *)ino, &limbo_dirs, ino_t_cmp) == NULL)
2633355d6bb5Sswilcox 		errexit("add_orphan_dir: out of memory");
2634355d6bb5Sswilcox }
2635355d6bb5Sswilcox 
2636355d6bb5Sswilcox /*
2637355d6bb5Sswilcox  * Remove an inode from the orphaned-directories list, presumably
2638355d6bb5Sswilcox  * because it's been cleared.
2639355d6bb5Sswilcox  */
2640355d6bb5Sswilcox void
2641355d6bb5Sswilcox remove_orphan_dir(fsck_ino_t ino)
2642355d6bb5Sswilcox {
2643355d6bb5Sswilcox 	(void) tdelete((void *)ino, &limbo_dirs, ino_t_cmp);
2644355d6bb5Sswilcox }
2645355d6bb5Sswilcox 
2646355d6bb5Sswilcox /*
2647355d6bb5Sswilcox  * log_setsum() and log_checksum() are equivalent to lufs.c:setsum()
2648355d6bb5Sswilcox  * and lufs.c:checksum().
2649355d6bb5Sswilcox  */
2650355d6bb5Sswilcox static void
2651355d6bb5Sswilcox log_setsum(int32_t *sp, int32_t *lp, int nb)
2652355d6bb5Sswilcox {
2653355d6bb5Sswilcox 	int32_t csum = 0;
2654355d6bb5Sswilcox 
2655355d6bb5Sswilcox 	*sp = 0;
2656355d6bb5Sswilcox 	nb /= sizeof (int32_t);
2657355d6bb5Sswilcox 	while (nb--)
2658355d6bb5Sswilcox 		csum += *lp++;
2659355d6bb5Sswilcox 	*sp = csum;
2660355d6bb5Sswilcox }
2661355d6bb5Sswilcox 
2662355d6bb5Sswilcox static int
2663355d6bb5Sswilcox log_checksum(int32_t *sp, int32_t *lp, int nb)
2664355d6bb5Sswilcox {
2665355d6bb5Sswilcox 	int32_t ssum = *sp;
2666355d6bb5Sswilcox 
2667355d6bb5Sswilcox 	log_setsum(sp, lp, nb);
2668355d6bb5Sswilcox 	if (ssum != *sp) {
2669355d6bb5Sswilcox 		*sp = ssum;
2670355d6bb5Sswilcox 		return (0);
2671355d6bb5Sswilcox 	}
2672355d6bb5Sswilcox 	return (1);
26737c478bd9Sstevel@tonic-gate }
2674