fsutil.c revision 107671
1/*
2 * Copyright (c) 1980, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/sbin/fsck_ffs/fsutil.c 107671 2002-12-07 21:40:31Z iedowse $";
40#endif /* not lint */
41
42#include <sys/param.h>
43#include <sys/time.h>
44#include <sys/types.h>
45#include <sys/sysctl.h>
46#include <sys/disklabel.h>
47#include <sys/stat.h>
48#include <sys/disklabel.h>
49
50#include <ufs/ufs/dinode.h>
51#include <ufs/ufs/dir.h>
52#include <ufs/ffs/fs.h>
53
54#include <err.h>
55#include <errno.h>
56#include <string.h>
57#include <ctype.h>
58#include <fstab.h>
59#include <stdint.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <unistd.h>
63
64#include "fsck.h"
65
66static void slowio_start(void);
67static void slowio_end(void);
68
69long	diskreads, totalreads;	/* Disk cache statistics */
70struct timeval slowio_starttime;
71int slowio_delay_usec = 10000;	/* Initial IO delay for background fsck */
72int slowio_pollcnt;
73
74int
75ftypeok(union dinode *dp)
76{
77	switch (DIP(dp, di_mode) & IFMT) {
78
79	case IFDIR:
80	case IFREG:
81	case IFBLK:
82	case IFCHR:
83	case IFLNK:
84	case IFSOCK:
85	case IFIFO:
86		return (1);
87
88	default:
89		if (debug)
90			printf("bad file type 0%o\n", DIP(dp, di_mode));
91		return (0);
92	}
93}
94
95int
96reply(const char *question)
97{
98	int persevere;
99	char c;
100
101	if (preen)
102		pfatal("INTERNAL ERROR: GOT TO reply()");
103	persevere = !strcmp(question, "CONTINUE");
104	printf("\n");
105	if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
106		printf("%s? no\n\n", question);
107		resolved = 0;
108		return (0);
109	}
110	if (yflag || (persevere && nflag)) {
111		printf("%s? yes\n\n", question);
112		return (1);
113	}
114	do	{
115		printf("%s? [yn] ", question);
116		(void) fflush(stdout);
117		c = getc(stdin);
118		while (c != '\n' && getc(stdin) != '\n') {
119			if (feof(stdin)) {
120				resolved = 0;
121				return (0);
122			}
123		}
124	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
125	printf("\n");
126	if (c == 'y' || c == 'Y')
127		return (1);
128	resolved = 0;
129	return (0);
130}
131
132/*
133 * Look up state information for an inode.
134 */
135struct inostat *
136inoinfo(ino_t inum)
137{
138	static struct inostat unallocated = { USTATE, 0, 0 };
139	struct inostatlist *ilp;
140	int iloff;
141
142	if (inum > maxino)
143		errx(EEXIT, "inoinfo: inumber %d out of range", inum);
144	ilp = &inostathead[inum / sblock.fs_ipg];
145	iloff = inum % sblock.fs_ipg;
146	if (iloff >= ilp->il_numalloced)
147		return (&unallocated);
148	return (&ilp->il_stat[iloff]);
149}
150
151/*
152 * Malloc buffers and set up cache.
153 */
154void
155bufinit(void)
156{
157	struct bufarea *bp;
158	long bufcnt, i;
159	char *bufp;
160
161	pbp = pdirbp = (struct bufarea *)0;
162	bufp = malloc((unsigned int)sblock.fs_bsize);
163	if (bufp == 0)
164		errx(EEXIT, "cannot allocate buffer pool");
165	cgblk.b_un.b_buf = bufp;
166	initbarea(&cgblk);
167	bufhead.b_next = bufhead.b_prev = &bufhead;
168	bufcnt = MAXBUFSPACE / sblock.fs_bsize;
169	if (bufcnt < MINBUFS)
170		bufcnt = MINBUFS;
171	for (i = 0; i < bufcnt; i++) {
172		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
173		bufp = malloc((unsigned int)sblock.fs_bsize);
174		if (bp == NULL || bufp == NULL) {
175			if (i >= MINBUFS)
176				break;
177			errx(EEXIT, "cannot allocate buffer pool");
178		}
179		bp->b_un.b_buf = bufp;
180		bp->b_prev = &bufhead;
181		bp->b_next = bufhead.b_next;
182		bufhead.b_next->b_prev = bp;
183		bufhead.b_next = bp;
184		initbarea(bp);
185	}
186	bufhead.b_size = i;	/* save number of buffers */
187}
188
189/*
190 * Manage a cache of directory blocks.
191 */
192struct bufarea *
193getdatablk(ufs2_daddr_t blkno, long size)
194{
195	struct bufarea *bp;
196
197	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
198		if (bp->b_bno == fsbtodb(&sblock, blkno))
199			goto foundit;
200	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
201		if ((bp->b_flags & B_INUSE) == 0)
202			break;
203	if (bp == &bufhead)
204		errx(EEXIT, "deadlocked buffer pool");
205	getblk(bp, blkno, size);
206	/* fall through */
207foundit:
208	bp->b_prev->b_next = bp->b_next;
209	bp->b_next->b_prev = bp->b_prev;
210	bp->b_prev = &bufhead;
211	bp->b_next = bufhead.b_next;
212	bufhead.b_next->b_prev = bp;
213	bufhead.b_next = bp;
214	bp->b_flags |= B_INUSE;
215	return (bp);
216}
217
218void
219getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
220{
221	ufs2_daddr_t dblk;
222
223	totalreads++;
224	dblk = fsbtodb(&sblock, blk);
225	if (bp->b_bno != dblk) {
226		flush(fswritefd, bp);
227		diskreads++;
228		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
229		bp->b_bno = dblk;
230		bp->b_size = size;
231	}
232}
233
234void
235flush(int fd, struct bufarea *bp)
236{
237	int i, j;
238
239	if (!bp->b_dirty)
240		return;
241	bp->b_dirty = 0;
242	if (fswritefd < 0) {
243		pfatal("WRITING IN READ_ONLY MODE.\n");
244		return;
245	}
246	if (bp->b_errs != 0)
247		pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
248		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
249		    (long long)bp->b_bno);
250	bp->b_errs = 0;
251	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
252	if (bp != &sblk)
253		return;
254	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
255		bwrite(fswritefd, (char *)sblock.fs_csp + i,
256		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
257		    sblock.fs_cssize - i < sblock.fs_bsize ?
258		    sblock.fs_cssize - i : sblock.fs_bsize);
259	}
260}
261
262void
263rwerror(const char *mesg, ufs2_daddr_t blk)
264{
265
266	if (bkgrdcheck)
267		exit(EEXIT);
268	if (preen == 0)
269		printf("\n");
270	pfatal("CANNOT %s: %ld", mesg, (long)blk);
271	if (reply("CONTINUE") == 0)
272		exit(EEXIT);
273}
274
275void
276ckfini(int markclean)
277{
278	struct bufarea *bp, *nbp;
279	int ofsmodified, cnt = 0;
280
281	if (bkgrdflag) {
282		unlink(snapname);
283		if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
284			cmd.value = FS_UNCLEAN;
285			cmd.size = markclean ? -1 : 1;
286			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
287			    &cmd, sizeof cmd) == -1)
288				rwerror("SET FILESYSTEM FLAGS", FS_UNCLEAN);
289			if (!preen) {
290				printf("\n***** FILE SYSTEM MARKED %s *****\n",
291				    markclean ? "CLEAN" : "DIRTY");
292				if (!markclean)
293					rerun = 1;
294			}
295		} else if (!preen && !markclean) {
296			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
297			rerun = 1;
298		}
299	}
300	if (fswritefd < 0) {
301		(void)close(fsreadfd);
302		return;
303	}
304	flush(fswritefd, &sblk);
305	if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
306	    sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
307	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
308		sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
309		sbdirty();
310		flush(fswritefd, &sblk);
311	}
312	flush(fswritefd, &cgblk);
313	free(cgblk.b_un.b_buf);
314	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
315		cnt++;
316		flush(fswritefd, bp);
317		nbp = bp->b_prev;
318		free(bp->b_un.b_buf);
319		free((char *)bp);
320	}
321	if (bufhead.b_size != cnt)
322		errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt);
323	pbp = pdirbp = (struct bufarea *)0;
324	if (cursnapshot == 0 && sblock.fs_clean != markclean) {
325		if ((sblock.fs_clean = markclean) != 0) {
326			sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
327			sblock.fs_pendingblocks = 0;
328			sblock.fs_pendinginodes = 0;
329		}
330		sbdirty();
331		ofsmodified = fsmodified;
332		flush(fswritefd, &sblk);
333		fsmodified = ofsmodified;
334		if (!preen) {
335			printf("\n***** FILE SYSTEM MARKED %s *****\n",
336			    markclean ? "CLEAN" : "DIRTY");
337			if (!markclean)
338				rerun = 1;
339		}
340	} else if (!preen && !markclean) {
341		printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
342		rerun = 1;
343	}
344	if (debug && totalreads > 0)
345		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
346		    totalreads, (int)(diskreads * 100 / totalreads));
347	(void)close(fsreadfd);
348	(void)close(fswritefd);
349}
350
351int
352bread(int fd, char *buf, ufs2_daddr_t blk, long size)
353{
354	char *cp;
355	int i, errs;
356	off_t offset;
357
358	offset = blk;
359	offset *= dev_bsize;
360	if (bkgrdflag)
361		slowio_start();
362	if (lseek(fd, offset, 0) < 0)
363		rwerror("SEEK BLK", blk);
364	else if (read(fd, buf, (int)size) == size) {
365		if (bkgrdflag)
366			slowio_end();
367		return (0);
368	}
369	rwerror("READ BLK", blk);
370	if (lseek(fd, offset, 0) < 0)
371		rwerror("SEEK BLK", blk);
372	errs = 0;
373	memset(buf, 0, (size_t)size);
374	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
375	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
376		if (read(fd, cp, (int)secsize) != secsize) {
377			(void)lseek(fd, offset + i + secsize, 0);
378			if (secsize != dev_bsize && dev_bsize != 1)
379				printf(" %jd (%jd),",
380				    (intmax_t)(blk * dev_bsize + i) / secsize,
381				    (intmax_t)blk + i / dev_bsize);
382			else
383				printf(" %jd,", (intmax_t)blk + i / dev_bsize);
384			errs++;
385		}
386	}
387	printf("\n");
388	if (errs)
389		resolved = 0;
390	return (errs);
391}
392
393void
394bwrite(int fd, char *buf, ufs2_daddr_t blk, long size)
395{
396	int i;
397	char *cp;
398	off_t offset;
399
400	if (fd < 0)
401		return;
402	offset = blk;
403	offset *= dev_bsize;
404	if (lseek(fd, offset, 0) < 0)
405		rwerror("SEEK BLK", blk);
406	else if (write(fd, buf, (int)size) == size) {
407		fsmodified = 1;
408		return;
409	}
410	resolved = 0;
411	rwerror("WRITE BLK", blk);
412	if (lseek(fd, offset, 0) < 0)
413		rwerror("SEEK BLK", blk);
414	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
415	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
416		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
417			(void)lseek(fd, offset + i + dev_bsize, 0);
418			printf(" %jd,", (intmax_t)blk + i / dev_bsize);
419		}
420	printf("\n");
421	return;
422}
423
424/*
425 * allocate a data block with the specified number of fragments
426 */
427ufs2_daddr_t
428allocblk(long frags)
429{
430	int i, j, k, cg, baseblk;
431	struct cg *cgp = &cgrp;
432
433	if (frags <= 0 || frags > sblock.fs_frag)
434		return (0);
435	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
436		for (j = 0; j <= sblock.fs_frag - frags; j++) {
437			if (testbmap(i + j))
438				continue;
439			for (k = 1; k < frags; k++)
440				if (testbmap(i + j + k))
441					break;
442			if (k < frags) {
443				j += k;
444				continue;
445			}
446			cg = dtog(&sblock, i + j);
447			getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
448			if (!cg_chkmagic(cgp))
449				pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
450			baseblk = dtogd(&sblock, i + j);
451			for (k = 0; k < frags; k++) {
452				setbmap(i + j + k);
453				clrbit(cg_blksfree(cgp), baseblk + k);
454			}
455			n_blks += frags;
456			if (frags == sblock.fs_frag)
457				cgp->cg_cs.cs_nbfree--;
458			else
459				cgp->cg_cs.cs_nffree -= frags;
460			cgdirty();
461			return (i + j);
462		}
463	}
464	return (0);
465}
466
467/*
468 * Free a previously allocated block
469 */
470void
471freeblk(ufs2_daddr_t blkno, long frags)
472{
473	struct inodesc idesc;
474
475	idesc.id_blkno = blkno;
476	idesc.id_numfrags = frags;
477	(void)pass4check(&idesc);
478}
479
480/* Slow down IO so as to leave some disk bandwidth for other processes */
481void
482slowio_start()
483{
484
485	/* Delay one in every 8 operations by 16 times the average IO delay */
486	slowio_pollcnt = (slowio_pollcnt + 1) & 7;
487	if (slowio_pollcnt == 0) {
488		usleep(slowio_delay_usec * 16);
489		gettimeofday(&slowio_starttime, NULL);
490	}
491}
492
493void
494slowio_end()
495{
496	struct timeval tv;
497	int delay_usec;
498
499	if (slowio_pollcnt != 0)
500		return;
501
502	/* Update the slowdown interval. */
503	gettimeofday(&tv, NULL);
504	delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
505	    (tv.tv_usec - slowio_starttime.tv_usec);
506	if (delay_usec < 64)
507		delay_usec = 64;
508	if (delay_usec > 1000000)
509		delay_usec = 1000000;
510	slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
511}
512
513/*
514 * Find a pathname
515 */
516void
517getpathname(char *namebuf, ino_t curdir, ino_t ino)
518{
519	int len;
520	char *cp;
521	struct inodesc idesc;
522	static int busy = 0;
523
524	if (curdir == ino && ino == ROOTINO) {
525		(void)strcpy(namebuf, "/");
526		return;
527	}
528	if (busy ||
529	    (inoinfo(curdir)->ino_state != DSTATE &&
530	     inoinfo(curdir)->ino_state != DFOUND)) {
531		(void)strcpy(namebuf, "?");
532		return;
533	}
534	busy = 1;
535	memset(&idesc, 0, sizeof(struct inodesc));
536	idesc.id_type = DATA;
537	idesc.id_fix = IGNORE;
538	cp = &namebuf[MAXPATHLEN - 1];
539	*cp = '\0';
540	if (curdir != ino) {
541		idesc.id_parent = curdir;
542		goto namelookup;
543	}
544	while (ino != ROOTINO) {
545		idesc.id_number = ino;
546		idesc.id_func = findino;
547		idesc.id_name = strdup("..");
548		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
549			break;
550	namelookup:
551		idesc.id_number = idesc.id_parent;
552		idesc.id_parent = ino;
553		idesc.id_func = findname;
554		idesc.id_name = namebuf;
555		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
556			break;
557		len = strlen(namebuf);
558		cp -= len;
559		memmove(cp, namebuf, (size_t)len);
560		*--cp = '/';
561		if (cp < &namebuf[MAXNAMLEN])
562			break;
563		ino = idesc.id_number;
564	}
565	busy = 0;
566	if (ino != ROOTINO)
567		*--cp = '?';
568	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
569}
570
571void
572catch(int sig __unused)
573{
574
575	ckfini(0);
576	exit(12);
577}
578
579/*
580 * When preening, allow a single quit to signal
581 * a special exit after file system checks complete
582 * so that reboot sequence may be interrupted.
583 */
584void
585catchquit(int sig __unused)
586{
587	printf("returning to single-user after file system check\n");
588	returntosingle = 1;
589	(void)signal(SIGQUIT, SIG_DFL);
590}
591
592/*
593 * determine whether an inode should be fixed.
594 */
595int
596dofix(struct inodesc *idesc, const char *msg)
597{
598
599	switch (idesc->id_fix) {
600
601	case DONTKNOW:
602		if (idesc->id_type == DATA)
603			direrror(idesc->id_number, msg);
604		else
605			pwarn("%s", msg);
606		if (preen) {
607			printf(" (SALVAGED)\n");
608			idesc->id_fix = FIX;
609			return (ALTERED);
610		}
611		if (reply("SALVAGE") == 0) {
612			idesc->id_fix = NOFIX;
613			return (0);
614		}
615		idesc->id_fix = FIX;
616		return (ALTERED);
617
618	case FIX:
619		return (ALTERED);
620
621	case NOFIX:
622	case IGNORE:
623		return (0);
624
625	default:
626		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
627	}
628	/* NOTREACHED */
629	return (0);
630}
631
632#include <stdarg.h>
633
634/*
635 * An unexpected inconsistency occured.
636 * Die if preening or file system is running with soft dependency protocol,
637 * otherwise just print message and continue.
638 */
639void
640pfatal(const char *fmt, ...)
641{
642	va_list ap;
643	va_start(ap, fmt);
644	if (!preen) {
645		(void)vfprintf(stdout, fmt, ap);
646		va_end(ap);
647		if (usedsoftdep)
648			(void)fprintf(stdout,
649			    "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
650		/*
651		 * Force foreground fsck to clean up inconsistency.
652		 */
653		if (bkgrdflag) {
654			cmd.value = FS_NEEDSFSCK;
655			cmd.size = 1;
656			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
657			    &cmd, sizeof cmd) == -1)
658				pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
659			fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
660			ckfini(0);
661			exit(EEXIT);
662		}
663		return;
664	}
665	if (cdevname == NULL)
666		cdevname = strdup("fsck");
667	(void)fprintf(stdout, "%s: ", cdevname);
668	(void)vfprintf(stdout, fmt, ap);
669	(void)fprintf(stdout,
670	    "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
671	    cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
672	/*
673	 * Force foreground fsck to clean up inconsistency.
674	 */
675	if (bkgrdflag) {
676		cmd.value = FS_NEEDSFSCK;
677		cmd.size = 1;
678		if (sysctlbyname("vfs.ffs.setflags", 0, 0,
679		    &cmd, sizeof cmd) == -1)
680			pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
681	}
682	ckfini(0);
683	exit(EEXIT);
684}
685
686/*
687 * Pwarn just prints a message when not preening or running soft dependency
688 * protocol, or a warning (preceded by filename) when preening.
689 */
690void
691pwarn(const char *fmt, ...)
692{
693	va_list ap;
694	va_start(ap, fmt);
695	if (preen)
696		(void)fprintf(stdout, "%s: ", cdevname);
697	(void)vfprintf(stdout, fmt, ap);
698	va_end(ap);
699}
700
701/*
702 * Stub for routines from kernel.
703 */
704void
705panic(const char *fmt, ...)
706{
707	va_list ap;
708	va_start(ap, fmt);
709	pfatal("INTERNAL INCONSISTENCY:");
710	(void)vfprintf(stdout, fmt, ap);
711	va_end(ap);
712	exit(EEXIT);
713}
714