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