main.c revision 253155
1139738Simp/*
2164010Smarcel * Copyright (c) 1980, 1986, 1993
3138140Smarcel *	The Regents of the University of California.  All rights reserved.
4138140Smarcel *
5138140Smarcel * Redistribution and use in source and binary forms, with or without
6138140Smarcel * modification, are permitted provided that the following conditions
7138140Smarcel * are met:
8138140Smarcel * 1. Redistributions of source code must retain the above copyright
9138140Smarcel *    notice, this list of conditions and the following disclaimer.
10138140Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11138140Smarcel *    notice, this list of conditions and the following disclaimer in the
12138140Smarcel *    documentation and/or other materials provided with the distribution.
13138140Smarcel * 4. Neither the name of the University nor the names of its contributors
14138140Smarcel *    may be used to endorse or promote products derived from this software
15138140Smarcel *    without specific prior written permission.
16138140Smarcel *
17138140Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18138140Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19138140Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20138140Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21138140Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22138140Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23138140Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24138140Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25138140Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26138140Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27138140Smarcel * SUCH DAMAGE.
28138140Smarcel */
29138140Smarcel
30164010Smarcel#if 0
31164010Smarcel#ifndef lint
32138140Smarcelstatic const char copyright[] =
33138140Smarcel"@(#) Copyright (c) 1980, 1986, 1993\n\
34138140Smarcel	The Regents of the University of California.  All rights reserved.\n";
35164010Smarcel#endif /* not lint */
36138140Smarcel
37164010Smarcel#ifndef lint
38164010Smarcelstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
39164010Smarcel#endif /* not lint */
40164010Smarcel#endif
41164010Smarcel#include <sys/cdefs.h>
42164010Smarcel__FBSDID("$FreeBSD: stable/9/sbin/fsck_ffs/main.c 253155 2013-07-10 14:13:37Z des $");
43164010Smarcel
44164010Smarcel#include <sys/param.h>
45164010Smarcel#include <sys/file.h>
46164010Smarcel#include <sys/mount.h>
47164010Smarcel#include <sys/resource.h>
48219691Smarcel#include <sys/stat.h>
49219691Smarcel#include <sys/sysctl.h>
50164010Smarcel#include <sys/uio.h>
51219691Smarcel#include <sys/disklabel.h>
52219691Smarcel
53164010Smarcel#include <ufs/ufs/dinode.h>
54219691Smarcel#include <ufs/ffs/fs.h>
55219691Smarcel
56219691Smarcel#include <err.h>
57219691Smarcel#include <errno.h>
58219691Smarcel#include <fstab.h>
59219691Smarcel#include <grp.h>
60219691Smarcel#include <mntopts.h>
61219691Smarcel#include <paths.h>
62219691Smarcel#include <stdint.h>
63219691Smarcel#include <string.h>
64219691Smarcel#include <time.h>
65219691Smarcel
66138140Smarcel#include "fsck.h"
67219691Smarcel
68138140Smarcelstatic void usage(void) __dead2;
69219691Smarcelstatic int argtoi(int flag, const char *req, const char *str, int base);
70219691Smarcelstatic int checkfilesys(char *filesys);
71219691Smarcelstatic int chkdoreload(struct statfs *mntp);
72219691Smarcelstatic struct statfs *getmntpt(const char *);
73219691Smarcel
74219691Smarcelint
75219691Smarcelmain(int argc, char *argv[])
76219691Smarcel{
77219691Smarcel	int ch;
78219691Smarcel	struct rlimit rlimit;
79219691Smarcel	struct itimerval itimerval;
80219691Smarcel	int ret = 0;
81219691Smarcel
82219691Smarcel	sync();
83219691Smarcel	skipclean = 1;
84219691Smarcel	inoopt = 0;
85219691Smarcel	while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npryZ")) != -1) {
86219691Smarcel		switch (ch) {
87219691Smarcel		case 'b':
88219691Smarcel			skipclean = 0;
89219691Smarcel			bflag = argtoi('b', "number", optarg, 10);
90219691Smarcel			printf("Alternate super block location: %d\n", bflag);
91219691Smarcel			break;
92219691Smarcel
93221269Smarcel		case 'B':
94221269Smarcel			bkgrdflag = 1;
95221269Smarcel			break;
96221269Smarcel
97219691Smarcel		case 'c':
98219691Smarcel			skipclean = 0;
99219691Smarcel			cvtlevel = argtoi('c', "conversion level", optarg, 10);
100219691Smarcel			if (cvtlevel < 3)
101219691Smarcel				errx(EEXIT, "cannot do level %d conversion",
102219691Smarcel				    cvtlevel);
103219691Smarcel			break;
104219691Smarcel
105219691Smarcel		case 'd':
106219691Smarcel			debug++;
107219691Smarcel			break;
108219691Smarcel
109219691Smarcel		case 'E':
110219691Smarcel			Eflag++;
111219691Smarcel			break;
112219691Smarcel
113219691Smarcel		case 'f':
114219691Smarcel			skipclean = 0;
115219691Smarcel			break;
116219691Smarcel
117219691Smarcel		case 'F':
118219691Smarcel			bkgrdcheck = 1;
119219691Smarcel			break;
120219691Smarcel
121219691Smarcel		case 'm':
122219691Smarcel			lfmode = argtoi('m', "mode", optarg, 8);
123219691Smarcel			if (lfmode &~ 07777)
124219691Smarcel				errx(EEXIT, "bad mode to -m: %o", lfmode);
125219691Smarcel			printf("** lost+found creation mode %o\n", lfmode);
126219691Smarcel			break;
127219691Smarcel
128219691Smarcel		case 'n':
129219691Smarcel			nflag++;
130219691Smarcel			yflag = 0;
131219691Smarcel			break;
132219691Smarcel
133219691Smarcel		case 'p':
134219691Smarcel			preen++;
135219691Smarcel			/*FALLTHROUGH*/
136219691Smarcel
137219691Smarcel		case 'C':
138219691Smarcel			ckclean++;
139219691Smarcel			break;
140219691Smarcel
141219691Smarcel		case 'r':
142219691Smarcel			inoopt++;
143219691Smarcel			break;
144219691Smarcel
145164010Smarcel		case 'y':
146138140Smarcel			yflag++;
147164010Smarcel			nflag = 0;
148219691Smarcel			break;
149219691Smarcel
150219691Smarcel		case 'Z':
151221269Smarcel			Zflag++;
152219691Smarcel			break;
153219691Smarcel
154219691Smarcel		default:
155219691Smarcel			usage();
156221269Smarcel		}
157219691Smarcel	}
158219691Smarcel	argc -= optind;
159219691Smarcel	argv += optind;
160219691Smarcel
161219691Smarcel	if (!argc)
162219691Smarcel		usage();
163219691Smarcel
164221269Smarcel	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
165219691Smarcel		(void)signal(SIGINT, catch);
166219691Smarcel	if (ckclean)
167219691Smarcel		(void)signal(SIGQUIT, catchquit);
168219691Smarcel	signal(SIGINFO, infohandler);
169219691Smarcel	if (bkgrdflag) {
170219691Smarcel		signal(SIGALRM, alarmhandler);
171219691Smarcel		itimerval.it_interval.tv_sec = 5;
172219691Smarcel		itimerval.it_interval.tv_usec = 0;
173219691Smarcel		itimerval.it_value.tv_sec = 5;
174219691Smarcel		itimerval.it_value.tv_usec = 0;
175219691Smarcel		setitimer(ITIMER_REAL, &itimerval, NULL);
176219691Smarcel	}
177221269Smarcel	/*
178221269Smarcel	 * Push up our allowed memory limit so we can cope
179219691Smarcel	 * with huge file systems.
180219691Smarcel	 */
181219691Smarcel	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
182219691Smarcel		rlimit.rlim_cur = rlimit.rlim_max;
183219691Smarcel		(void)setrlimit(RLIMIT_DATA, &rlimit);
184219691Smarcel	}
185219691Smarcel	while (argc-- > 0)
186219691Smarcel		(void)checkfilesys(*argv++);
187219691Smarcel
188164010Smarcel	if (returntosingle)
189219691Smarcel		ret = 2;
190164010Smarcel	exit(ret);
191164010Smarcel}
192164010Smarcel
193164010Smarcelstatic int
194219691Smarcelargtoi(int flag, const char *req, const char *str, int base)
195164010Smarcel{
196164010Smarcel	char *cp;
197164010Smarcel	int ret;
198164010Smarcel
199164010Smarcel	ret = (int)strtol(str, &cp, base);
200164010Smarcel	if (cp == str || *cp)
201164010Smarcel		errx(EEXIT, "-%c flag requires a %s", flag, req);
202164010Smarcel	return (ret);
203164010Smarcel}
204164010Smarcel
205219691Smarcel/*
206164010Smarcel * Check the specified file system.
207164010Smarcel */
208219691Smarcel/* ARGSUSED */
209219691Smarcelstatic int
210219691Smarcelcheckfilesys(char *filesys)
211219691Smarcel{
212164010Smarcel	ufs2_daddr_t n_ffree, n_bfree;
213219691Smarcel	struct dups *dp;
214219691Smarcel	struct statfs *mntp;
215219691Smarcel	struct stat snapdir;
216164010Smarcel	struct group *grp;
217164010Smarcel	ufs2_daddr_t blks;
218164010Smarcel	struct iovec *iov;
219164010Smarcel	char errmsg[255];
220219691Smarcel	int iovlen;
221164010Smarcel	int cylno;
222164010Smarcel	ino_t files;
223164010Smarcel	size_t size;
224164010Smarcel
225164010Smarcel	iov = NULL;
226164010Smarcel	iovlen = 0;
227164010Smarcel	errmsg[0] = '\0';
228164010Smarcel
229164010Smarcel	cdevname = filesys;
230164010Smarcel	if (debug && ckclean)
231164010Smarcel		pwarn("starting\n");
232164010Smarcel	/*
233222799Smarcel	 * Make best effort to get the disk name. Check first to see
234222799Smarcel	 * if it is listed among the mounted file systems. Failing that
235222799Smarcel	 * check to see if it is listed in /etc/fstab.
236222799Smarcel	 */
237222799Smarcel	mntp = getmntpt(filesys);
238222799Smarcel	if (mntp != NULL)
239222799Smarcel		filesys = mntp->f_mntfromname;
240222799Smarcel	else
241222799Smarcel		filesys = blockcheck(filesys);
242222799Smarcel	/*
243222799Smarcel	 * If -F flag specified, check to see whether a background check
244222799Smarcel	 * is possible and needed. If possible and needed, exit with
245222799Smarcel	 * status zero. Otherwise exit with status non-zero. A non-zero
246222799Smarcel	 * exit status will cause a foreground check to be run.
247222799Smarcel	 */
248222799Smarcel	sblock_init();
249222799Smarcel	if (bkgrdcheck) {
250222799Smarcel		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
251222799Smarcel			exit(3);	/* Cannot read superblock */
252222799Smarcel		close(fsreadfd);
253222799Smarcel		/* Earlier background failed or journaled */
254222799Smarcel		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
255222799Smarcel			exit(4);
256222799Smarcel		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
257222799Smarcel			exit(5);	/* Not running soft updates */
258222799Smarcel		size = MIBSIZE;
259222799Smarcel		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
260222799Smarcel			exit(6);	/* Lacks kernel support */
261222799Smarcel		if ((mntp == NULL && sblock.fs_clean == 1) ||
262222799Smarcel		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
263222799Smarcel			exit(7);	/* Filesystem clean, report it now */
264222799Smarcel		exit(0);
265	}
266	if (ckclean && skipclean) {
267		/*
268		 * If file system is gjournaled, check it here.
269		 */
270		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
271			exit(3);	/* Cannot read superblock */
272		close(fsreadfd);
273		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
274			//printf("GJournaled file system detected on %s.\n",
275			//    filesys);
276			if (sblock.fs_clean == 1) {
277				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
278				exit(0);
279			}
280			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
281				gjournal_check(filesys);
282				if (chkdoreload(mntp) == 0)
283					exit(0);
284				exit(4);
285			} else {
286				pfatal("UNEXPECTED INCONSISTENCY, %s\n",
287				    "CANNOT RUN FAST FSCK\n");
288			}
289		}
290	}
291	/*
292	 * If we are to do a background check:
293	 *	Get the mount point information of the file system
294	 *	create snapshot file
295	 *	return created snapshot file
296	 *	if not found, clear bkgrdflag and proceed with normal fsck
297	 */
298	if (bkgrdflag) {
299		if (mntp == NULL) {
300			bkgrdflag = 0;
301			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
302		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
303			bkgrdflag = 0;
304			pfatal("NOT USING SOFT UPDATES, %s\n",
305			    "CANNOT RUN IN BACKGROUND");
306		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
307			bkgrdflag = 0;
308			pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
309		} else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
310			if (readsb(0) != 0) {
311				if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
312					bkgrdflag = 0;
313					pfatal("UNEXPECTED INCONSISTENCY, %s\n",
314					    "CANNOT RUN IN BACKGROUND\n");
315				}
316				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
317				    skipclean && ckclean) {
318					/*
319					 * file system is clean;
320					 * skip snapshot and report it clean
321					 */
322					pwarn("FILE SYSTEM CLEAN; %s\n",
323					    "SKIPPING CHECKS");
324					goto clean;
325				}
326			}
327			close(fsreadfd);
328		}
329		if (bkgrdflag) {
330			snprintf(snapname, sizeof snapname, "%s/.snap",
331			    mntp->f_mntonname);
332			if (stat(snapname, &snapdir) < 0) {
333				if (errno != ENOENT) {
334					bkgrdflag = 0;
335					pfatal("CANNOT FIND %s %s: %s, %s\n",
336					    "SNAPSHOT DIRECTORY",
337					    snapname, strerror(errno),
338					    "CANNOT RUN IN BACKGROUND");
339				} else if ((grp = getgrnam("operator")) == 0 ||
340				    mkdir(snapname, 0770) < 0 ||
341				    chown(snapname, -1, grp->gr_gid) < 0 ||
342				    chmod(snapname, 0770) < 0) {
343					bkgrdflag = 0;
344					pfatal("CANNOT CREATE %s %s: %s, %s\n",
345					    "SNAPSHOT DIRECTORY",
346					    snapname, strerror(errno),
347					    "CANNOT RUN IN BACKGROUND");
348				}
349			} else if (!S_ISDIR(snapdir.st_mode)) {
350				bkgrdflag = 0;
351				pfatal("%s IS NOT A DIRECTORY, %s\n", snapname,
352				    "CANNOT RUN IN BACKGROUND");
353			}
354		}
355		if (bkgrdflag) {
356			snprintf(snapname, sizeof snapname,
357			    "%s/.snap/fsck_snapshot", mntp->f_mntonname);
358			build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
359			build_iovec(&iov, &iovlen, "from", snapname,
360			    (size_t)-1);
361			build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
362			    (size_t)-1);
363			build_iovec(&iov, &iovlen, "errmsg", errmsg,
364			    sizeof(errmsg));
365			build_iovec(&iov, &iovlen, "update", NULL, 0);
366			build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
367
368			while (nmount(iov, iovlen, mntp->f_flags) < 0) {
369				if (errno == EEXIST && unlink(snapname) == 0)
370					continue;
371				bkgrdflag = 0;
372				pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
373				    snapname, strerror(errno), errmsg);
374				break;
375			}
376			if (bkgrdflag != 0)
377				filesys = snapname;
378		}
379	}
380
381	switch (setup(filesys)) {
382	case 0:
383		if (preen)
384			pfatal("CAN'T CHECK FILE SYSTEM.");
385		return (0);
386	case -1:
387	clean:
388		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
389		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
390		printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
391		    (long long)sblock.fs_cstotal.cs_nffree,
392		    (long long)sblock.fs_cstotal.cs_nbfree,
393		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
394		return (0);
395	}
396	/*
397	 * Determine if we can and should do journal recovery.
398	 */
399	if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
400		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
401			if (preen || reply("USE JOURNAL")) {
402				if (suj_check(filesys) == 0) {
403					printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
404					if (chkdoreload(mntp) == 0)
405						exit(0);
406					exit(4);
407				}
408			}
409			printf("** Skipping journal, falling through to full fsck\n\n");
410		}
411		/*
412		 * Write the superblock so we don't try to recover the
413		 * journal on another pass.
414		 */
415		sblock.fs_mtime = time(NULL);
416		sbdirty();
417	}
418
419	/*
420	 * Cleared if any questions answered no. Used to decide if
421	 * the superblock should be marked clean.
422	 */
423	resolved = 1;
424	/*
425	 * 1: scan inodes tallying blocks used
426	 */
427	if (preen == 0) {
428		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
429		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
430			printf("** Root file system\n");
431		printf("** Phase 1 - Check Blocks and Sizes\n");
432	}
433	clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
434	pass1();
435	IOstats("Pass1");
436
437	/*
438	 * 1b: locate first references to duplicates, if any
439	 */
440	if (duplist) {
441		if (preen || usedsoftdep)
442			pfatal("INTERNAL ERROR: dups with %s%s%s",
443			    preen ? "-p" : "",
444			    (preen && usedsoftdep) ? " and " : "",
445			    usedsoftdep ? "softupdates" : "");
446		printf("** Phase 1b - Rescan For More DUPS\n");
447		pass1b();
448		IOstats("Pass1b");
449	}
450
451	/*
452	 * 2: traverse directories from root to mark all connected directories
453	 */
454	if (preen == 0)
455		printf("** Phase 2 - Check Pathnames\n");
456	pass2();
457	IOstats("Pass2");
458
459	/*
460	 * 3: scan inodes looking for disconnected directories
461	 */
462	if (preen == 0)
463		printf("** Phase 3 - Check Connectivity\n");
464	pass3();
465	IOstats("Pass3");
466
467	/*
468	 * 4: scan inodes looking for disconnected files; check reference counts
469	 */
470	if (preen == 0)
471		printf("** Phase 4 - Check Reference Counts\n");
472	pass4();
473	IOstats("Pass4");
474
475	/*
476	 * 5: check and repair resource counts in cylinder groups
477	 */
478	if (preen == 0)
479		printf("** Phase 5 - Check Cyl groups\n");
480	pass5();
481	IOstats("Pass5");
482
483	/*
484	 * print out summary statistics
485	 */
486	n_ffree = sblock.fs_cstotal.cs_nffree;
487	n_bfree = sblock.fs_cstotal.cs_nbfree;
488	files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
489	blks = n_blks +
490	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
491	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
492	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
493	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
494	if (bkgrdflag && (files > 0 || blks > 0)) {
495		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
496		pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n",
497		    countdirs, (long)files - countdirs, (long long)blks);
498	}
499	pwarn("%ld files, %jd used, %ju free ",
500	    (long)n_files, (intmax_t)n_blks,
501	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
502	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
503	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
504	    n_ffree * 100.0 / sblock.fs_dsize);
505	if (debug) {
506		if (files < 0)
507			printf("%d inodes missing\n", -files);
508		if (blks < 0)
509			printf("%lld blocks missing\n", -(long long)blks);
510		if (duplist != NULL) {
511			printf("The following duplicate blocks remain:");
512			for (dp = duplist; dp; dp = dp->next)
513				printf(" %lld,", (long long)dp->dup);
514			printf("\n");
515		}
516	}
517	duplist = (struct dups *)0;
518	muldup = (struct dups *)0;
519	inocleanup();
520	if (fsmodified) {
521		sblock.fs_time = time(NULL);
522		sbdirty();
523	}
524	if (cvtlevel && sblk.b_dirty) {
525		/*
526		 * Write out the duplicate super blocks
527		 */
528		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
529			blwrite(fswritefd, (char *)&sblock,
530			    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
531			    SBLOCKSIZE);
532	}
533	if (rerun)
534		resolved = 0;
535	finalIOstats();
536
537	/*
538	 * Check to see if the file system is mounted read-write.
539	 */
540	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
541		resolved = 0;
542	ckfini(resolved);
543
544	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
545		if (inostathead[cylno].il_stat != NULL)
546			free((char *)inostathead[cylno].il_stat);
547	free((char *)inostathead);
548	inostathead = NULL;
549	if (fsmodified && !preen)
550		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
551	if (rerun)
552		printf("\n***** PLEASE RERUN FSCK *****\n");
553	if (chkdoreload(mntp) != 0) {
554		if (!fsmodified)
555			return (0);
556		if (!preen)
557			printf("\n***** REBOOT NOW *****\n");
558		sync();
559		return (4);
560	}
561	return (0);
562}
563
564static int
565chkdoreload(struct statfs *mntp)
566{
567	struct iovec *iov;
568	int iovlen;
569	char errmsg[255];
570
571	if (mntp == NULL)
572		return (0);
573
574	iov = NULL;
575	iovlen = 0;
576	errmsg[0] = '\0';
577	/*
578	 * We modified a mounted file system.  Do a mount update on
579	 * it unless it is read-write, so we can continue using it
580	 * as safely as possible.
581	 */
582	if (mntp->f_flags & MNT_RDONLY) {
583		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
584		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
585		    (size_t)-1);
586		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
587		    (size_t)-1);
588		build_iovec(&iov, &iovlen, "errmsg", errmsg,
589		    sizeof(errmsg));
590		build_iovec(&iov, &iovlen, "update", NULL, 0);
591		build_iovec(&iov, &iovlen, "reload", NULL, 0);
592		/*
593		 * XX: We need the following line until we clean up
594		 * nmount parsing of root mounts and NFS root mounts.
595		 */
596		build_iovec(&iov, &iovlen, "ro", NULL, 0);
597		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
598			return (0);
599		}
600		pwarn("mount reload of '%s' failed: %s %s\n\n",
601		    mntp->f_mntonname, strerror(errno), errmsg);
602		return (1);
603	}
604	return (0);
605}
606
607/*
608 * Get the mount point information for name.
609 */
610static struct statfs *
611getmntpt(const char *name)
612{
613	struct stat devstat, mntdevstat;
614	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
615	char *ddevname;
616	struct statfs *mntbuf, *statfsp;
617	int i, mntsize, isdev;
618
619	if (stat(name, &devstat) != 0)
620		return (NULL);
621	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
622		isdev = 1;
623	else
624		isdev = 0;
625	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
626	for (i = 0; i < mntsize; i++) {
627		statfsp = &mntbuf[i];
628		ddevname = statfsp->f_mntfromname;
629		if (*ddevname != '/') {
630			strcpy(device, _PATH_DEV);
631			strcat(device, ddevname);
632			strcpy(statfsp->f_mntfromname, device);
633		}
634		if (isdev == 0) {
635			if (strcmp(name, statfsp->f_mntonname))
636				continue;
637			return (statfsp);
638		}
639		if (stat(ddevname, &mntdevstat) == 0 &&
640		    mntdevstat.st_rdev == devstat.st_rdev)
641			return (statfsp);
642	}
643	statfsp = NULL;
644	return (statfsp);
645}
646
647static void
648usage(void)
649{
650	(void) fprintf(stderr,
651	    "usage: %s [-BEFfnpry] [-b block] [-c level] [-m mode] "
652			"filesystem ...\n",
653	    getprogname());
654	exit(1);
655}
656