main.c revision 217769
110216Sphk/*
210216Sphk * Copyright (c) 1980, 1986, 1993
310216Sphk *	The Regents of the University of California.  All rights reserved.
410216Sphk *
510216Sphk * Redistribution and use in source and binary forms, with or without
610216Sphk * modification, are permitted provided that the following conditions
7139825Simp * are met:
8139825Simp * 1. Redistributions of source code must retain the above copyright
910216Sphk *    notice, this list of conditions and the following disclaimer.
1010216Sphk * 2. Redistributions in binary form must reproduce the above copyright
1110216Sphk *    notice, this list of conditions and the following disclaimer in the
1210216Sphk *    documentation and/or other materials provided with the distribution.
1310216Sphk * 4. Neither the name of the University nor the names of its contributors
1410216Sphk *    may be used to endorse or promote products derived from this software
1510216Sphk *    without specific prior written permission.
1610216Sphk *
1710216Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1810216Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1910216Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2010216Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2110216Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2210216Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2310216Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2410216Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2510216Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2610216Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2710216Sphk * SUCH DAMAGE.
2810216Sphk */
2910216Sphk
3010216Sphk#if 0
3110216Sphk#ifndef lint
3259656Siwasakistatic const char copyright[] =
3359656Siwasaki"@(#) Copyright (c) 1980, 1986, 1993\n\
3459656Siwasaki	The Regents of the University of California.  All rights reserved.\n";
3510216Sphk#endif /* not lint */
3610216Sphk
3710216Sphk#ifndef lint
3810216Sphkstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
3910216Sphk#endif /* not lint */
4010216Sphk#endif
4110216Sphk#include <sys/cdefs.h>
4210216Sphk__FBSDID("$FreeBSD: head/sbin/fsck_ffs/main.c 217769 2011-01-24 06:17:05Z mckusick $");
4310216Sphk
4410216Sphk#include <sys/param.h>
4510216Sphk#include <sys/file.h>
4610216Sphk#include <sys/mount.h>
4710216Sphk#include <sys/resource.h>
4810216Sphk#include <sys/stat.h>
4959656Siwasaki#include <sys/sysctl.h>
5059656Siwasaki#include <sys/uio.h>
5159656Siwasaki#include <sys/disklabel.h>
5210216Sphk
5310216Sphk#include <ufs/ufs/dinode.h>
5459656Siwasaki#include <ufs/ffs/fs.h>
5559656Siwasaki
5659656Siwasaki#include <err.h>
5759656Siwasaki#include <errno.h>
5859656Siwasaki#include <fstab.h>
5959656Siwasaki#include <grp.h>
6010216Sphk#include <mntopts.h>
6110216Sphk#include <paths.h>
6210216Sphk#include <stdint.h>
6310216Sphk#include <string.h>
6410216Sphk#include <time.h>
6510216Sphk
6610216Sphk#include "fsck.h"
6710216Sphk
6810216Sphkstatic void usage(void) __dead2;
6910216Sphkstatic int argtoi(int flag, const char *req, const char *str, int base);
7010216Sphkstatic int checkfilesys(char *filesys);
7110216Sphkstatic int chkdoreload(struct statfs *mntp);
7210216Sphkstatic struct statfs *getmntpt(const char *);
7310216Sphk
7410216Sphkint
7510216Sphkmain(int argc, char *argv[])
7610216Sphk{
7710216Sphk	int ch;
7810216Sphk	struct rlimit rlimit;
7910216Sphk	struct itimerval itimerval;
8010216Sphk	int ret = 0;
8110216Sphk
8210216Sphk	sync();
8310216Sphk	skipclean = 1;
8410216Sphk	inoopt = 0;
8510216Sphk	while ((ch = getopt(argc, argv, "b:Bc:CdfFm:npry")) != -1) {
8610216Sphk		switch (ch) {
8710216Sphk		case 'b':
8810216Sphk			skipclean = 0;
8913765Smpp			bflag = argtoi('b', "number", optarg, 10);
9010216Sphk			printf("Alternate super block location: %d\n", bflag);
9110216Sphk			break;
9210216Sphk
9310216Sphk		case 'B':
9410216Sphk			bkgrdflag = 1;
9510216Sphk			break;
9610216Sphk
9710216Sphk		case 'c':
9810216Sphk			skipclean = 0;
9910216Sphk			cvtlevel = argtoi('c', "conversion level", optarg, 10);
10010216Sphk			if (cvtlevel < 3)
10110216Sphk				errx(EEXIT, "cannot do level %d conversion",
10210216Sphk				    cvtlevel);
10310216Sphk			break;
10410216Sphk
10510216Sphk		case 'd':
10610216Sphk			debug++;
10710216Sphk			break;
10810216Sphk
10910216Sphk		case 'f':
11010216Sphk			skipclean = 0;
11110216Sphk			break;
11210216Sphk
11310216Sphk		case 'F':
11410216Sphk			bkgrdcheck = 1;
11510216Sphk			break;
11610216Sphk
11710216Sphk		case 'm':
11810216Sphk			lfmode = argtoi('m', "mode", optarg, 8);
11910216Sphk			if (lfmode &~ 07777)
12010216Sphk				errx(EEXIT, "bad mode to -m: %o", lfmode);
12110216Sphk			printf("** lost+found creation mode %o\n", lfmode);
12210216Sphk			break;
12310216Sphk
12410216Sphk		case 'n':
12510216Sphk			nflag++;
12610216Sphk			yflag = 0;
12710216Sphk			break;
12810216Sphk
12910216Sphk		case 'p':
13010216Sphk			preen++;
13110216Sphk			/*FALLTHROUGH*/
13210216Sphk
13310216Sphk		case 'C':
13410216Sphk			ckclean++;
13510216Sphk			break;
13610216Sphk
13710216Sphk		case 'r':
13810216Sphk			inoopt++;
13910216Sphk			break;
14010216Sphk
14110216Sphk		case 'y':
14210216Sphk			yflag++;
14310216Sphk			nflag = 0;
14410216Sphk			break;
14510216Sphk
14610216Sphk		default:
14710216Sphk			usage();
14810216Sphk		}
14910216Sphk	}
15010216Sphk	argc -= optind;
15110216Sphk	argv += optind;
15210216Sphk
15310216Sphk	if (!argc)
15410216Sphk		usage();
15510216Sphk
15610216Sphk	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
15710216Sphk		(void)signal(SIGINT, catch);
15810216Sphk	if (ckclean)
15910216Sphk		(void)signal(SIGQUIT, catchquit);
16010216Sphk	signal(SIGINFO, infohandler);
16110216Sphk	if (bkgrdflag) {
16210216Sphk		signal(SIGALRM, alarmhandler);
16310216Sphk		itimerval.it_interval.tv_sec = 5;
16410216Sphk		itimerval.it_interval.tv_usec = 0;
16510216Sphk		itimerval.it_value.tv_sec = 5;
16610216Sphk		itimerval.it_value.tv_usec = 0;
16710216Sphk		setitimer(ITIMER_REAL, &itimerval, NULL);
16810216Sphk	}
16910216Sphk	/*
17010216Sphk	 * Push up our allowed memory limit so we can cope
17110216Sphk	 * with huge file systems.
17210216Sphk	 */
17310216Sphk	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
17410216Sphk		rlimit.rlim_cur = rlimit.rlim_max;
17510216Sphk		(void)setrlimit(RLIMIT_DATA, &rlimit);
17610216Sphk	}
17710216Sphk	while (argc-- > 0)
17810216Sphk		(void)checkfilesys(*argv++);
17910216Sphk
18010216Sphk	if (returntosingle)
18110216Sphk		ret = 2;
18210216Sphk	exit(ret);
18310216Sphk}
18410216Sphk
18510216Sphkstatic int
18610216Sphkargtoi(int flag, const char *req, const char *str, int base)
18710216Sphk{
18810216Sphk	char *cp;
18910216Sphk	int ret;
19010216Sphk
19110216Sphk	ret = (int)strtol(str, &cp, base);
19210216Sphk	if (cp == str || *cp)
19310216Sphk		errx(EEXIT, "-%c flag requires a %s", flag, req);
19410216Sphk	return (ret);
19510216Sphk}
19610216Sphk
19710216Sphk/*
19810216Sphk * Check the specified file system.
19910216Sphk */
20010216Sphk/* ARGSUSED */
20110216Sphkstatic int
20210216Sphkcheckfilesys(char *filesys)
20310216Sphk{
20410216Sphk	ufs2_daddr_t n_ffree, n_bfree;
20510216Sphk	struct dups *dp;
20610216Sphk	struct statfs *mntp;
20710216Sphk	struct stat snapdir;
20810216Sphk	struct group *grp;
20910216Sphk	ufs2_daddr_t blks;
21010216Sphk	struct iovec *iov;
21110216Sphk	char errmsg[255];
21210216Sphk	int iovlen;
21310216Sphk	int cylno;
21410216Sphk	ino_t files;
21510216Sphk	size_t size;
21610216Sphk
21710216Sphk	iov = NULL;
21810216Sphk	iovlen = 0;
21910216Sphk	errmsg[0] = '\0';
22010216Sphk
22110216Sphk	cdevname = filesys;
22210216Sphk	if (debug && ckclean)
22310216Sphk		pwarn("starting\n");
22410216Sphk	/*
22510216Sphk	 * Make best effort to get the disk name. Check first to see
22610216Sphk	 * if it is listed among the mounted file systems. Failing that
22710216Sphk	 * check to see if it is listed in /etc/fstab.
22810216Sphk	 */
22910216Sphk	mntp = getmntpt(filesys);
23010216Sphk	if (mntp != NULL)
23110216Sphk		filesys = mntp->f_mntfromname;
23210216Sphk	else
23310216Sphk		filesys = blockcheck(filesys);
23410216Sphk	/*
23510216Sphk	 * If -F flag specified, check to see whether a background check
23610216Sphk	 * is possible and needed. If possible and needed, exit with
23710216Sphk	 * status zero. Otherwise exit with status non-zero. A non-zero
23810216Sphk	 * exit status will cause a foreground check to be run.
23910216Sphk	 */
24010216Sphk	sblock_init();
24110216Sphk	if (bkgrdcheck) {
24210216Sphk		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
24310216Sphk			exit(3);	/* Cannot read superblock */
24410216Sphk		close(fsreadfd);
24510216Sphk		/* Earlier background failed or journaled */
24610216Sphk		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
24710216Sphk			exit(4);
24810216Sphk		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
24910216Sphk			exit(5);	/* Not running soft updates */
25010216Sphk		size = MIBSIZE;
25110216Sphk		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
25210216Sphk			exit(6);	/* Lacks kernel support */
25310216Sphk		if ((mntp == NULL && sblock.fs_clean == 1) ||
25410216Sphk		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
25510216Sphk			exit(7);	/* Filesystem clean, report it now */
25610216Sphk		exit(0);
25710216Sphk	}
25810216Sphk	if (ckclean && skipclean) {
25910216Sphk		/*
26010216Sphk		 * If file system is gjournaled, check it here.
26110216Sphk		 */
26210216Sphk		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
26359656Siwasaki			exit(3);	/* Cannot read superblock */
26459656Siwasaki		close(fsreadfd);
26559656Siwasaki		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
26659656Siwasaki			//printf("GJournaled file system detected on %s.\n",
26759656Siwasaki			//    filesys);
26859656Siwasaki			if (sblock.fs_clean == 1) {
26959656Siwasaki				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
27059656Siwasaki				exit(0);
27159656Siwasaki			}
27259656Siwasaki			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
27359656Siwasaki				gjournal_check(filesys);
27459656Siwasaki				if (chkdoreload(mntp) == 0)
27559656Siwasaki					exit(0);
27659656Siwasaki				exit(4);
27759656Siwasaki			} else {
27859656Siwasaki				pfatal("UNEXPECTED INCONSISTENCY, %s\n",
27959656Siwasaki				    "CANNOT RUN FAST FSCK\n");
280			}
281		}
282	}
283	/*
284	 * If we are to do a background check:
285	 *	Get the mount point information of the file system
286	 *	create snapshot file
287	 *	return created snapshot file
288	 *	if not found, clear bkgrdflag and proceed with normal fsck
289	 */
290	if (bkgrdflag) {
291		if (mntp == NULL) {
292			bkgrdflag = 0;
293			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
294		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
295			bkgrdflag = 0;
296			pfatal("NOT USING SOFT UPDATES, %s\n",
297			    "CANNOT RUN IN BACKGROUND");
298		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
299			bkgrdflag = 0;
300			pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
301		} else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
302			if (readsb(0) != 0) {
303				if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
304					bkgrdflag = 0;
305					pfatal("UNEXPECTED INCONSISTENCY, %s\n",
306					    "CANNOT RUN IN BACKGROUND\n");
307				}
308				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
309				    skipclean && ckclean) {
310					/*
311					 * file system is clean;
312					 * skip snapshot and report it clean
313					 */
314					pwarn("FILE SYSTEM CLEAN; %s\n",
315					    "SKIPPING CHECKS");
316					goto clean;
317				}
318			}
319			close(fsreadfd);
320		}
321		if (bkgrdflag) {
322			snprintf(snapname, sizeof snapname, "%s/.snap",
323			    mntp->f_mntonname);
324			if (stat(snapname, &snapdir) < 0) {
325				if (errno != ENOENT) {
326					bkgrdflag = 0;
327					pfatal("CANNOT FIND %s %s: %s, %s\n",
328					    "SNAPSHOT DIRECTORY",
329					    snapname, strerror(errno),
330					    "CANNOT RUN IN BACKGROUND");
331				} else if ((grp = getgrnam("operator")) == 0 ||
332				    mkdir(snapname, 0770) < 0 ||
333				    chown(snapname, -1, grp->gr_gid) < 0 ||
334				    chmod(snapname, 0770) < 0) {
335					bkgrdflag = 0;
336					pfatal("CANNOT CREATE %s %s: %s, %s\n",
337					    "SNAPSHOT DIRECTORY",
338					    snapname, strerror(errno),
339					    "CANNOT RUN IN BACKGROUND");
340				}
341			} else if (!S_ISDIR(snapdir.st_mode)) {
342				bkgrdflag = 0;
343				pfatal("%s IS NOT A DIRECTORY, %s\n", snapname,
344				    "CANNOT RUN IN BACKGROUND");
345			}
346		}
347		if (bkgrdflag) {
348			snprintf(snapname, sizeof snapname,
349			    "%s/.snap/fsck_snapshot", mntp->f_mntonname);
350			build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
351			build_iovec(&iov, &iovlen, "from", snapname,
352			    (size_t)-1);
353			build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
354			    (size_t)-1);
355			build_iovec(&iov, &iovlen, "errmsg", errmsg,
356			    sizeof(errmsg));
357			build_iovec(&iov, &iovlen, "update", NULL, 0);
358			build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
359
360			while (nmount(iov, iovlen, mntp->f_flags) < 0) {
361				if (errno == EEXIST && unlink(snapname) == 0)
362					continue;
363				bkgrdflag = 0;
364				pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
365				    snapname, strerror(errno), errmsg);
366				break;
367			}
368			if (bkgrdflag != 0)
369				filesys = snapname;
370		}
371	}
372
373	switch (setup(filesys)) {
374	case 0:
375		if (preen)
376			pfatal("CAN'T CHECK FILE SYSTEM.");
377		return (0);
378	case -1:
379	clean:
380		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
381		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
382		printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
383		    (long long)sblock.fs_cstotal.cs_nffree,
384		    (long long)sblock.fs_cstotal.cs_nbfree,
385		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
386		return (0);
387	}
388	/*
389	 * Determine if we can and should do journal recovery.
390	 */
391	if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
392		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
393			if (preen || reply("USE JOURNAL")) {
394				if (suj_check(filesys) == 0) {
395					printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
396					if (chkdoreload(mntp) == 0)
397						exit(0);
398					exit(4);
399				}
400			}
401			printf("** Skipping journal, falling through to full fsck\n\n");
402		}
403		/*
404		 * Write the superblock so we don't try to recover the
405		 * journal on another pass.
406		 */
407		sblock.fs_mtime = time(NULL);
408		sbdirty();
409	}
410
411	/*
412	 * Cleared if any questions answered no. Used to decide if
413	 * the superblock should be marked clean.
414	 */
415	resolved = 1;
416	/*
417	 * 1: scan inodes tallying blocks used
418	 */
419	if (preen == 0) {
420		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
421		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
422			printf("** Root file system\n");
423		printf("** Phase 1 - Check Blocks and Sizes\n");
424	}
425	pass1();
426
427	/*
428	 * 1b: locate first references to duplicates, if any
429	 */
430	if (duplist) {
431		if (preen || usedsoftdep)
432			pfatal("INTERNAL ERROR: dups with %s%s%s",
433			    preen ? "-p" : "",
434			    (preen && usedsoftdep) ? " and " : "",
435			    usedsoftdep ? "softupdates" : "");
436		printf("** Phase 1b - Rescan For More DUPS\n");
437		pass1b();
438	}
439
440	/*
441	 * 2: traverse directories from root to mark all connected directories
442	 */
443	if (preen == 0)
444		printf("** Phase 2 - Check Pathnames\n");
445	pass2();
446
447	/*
448	 * 3: scan inodes looking for disconnected directories
449	 */
450	if (preen == 0)
451		printf("** Phase 3 - Check Connectivity\n");
452	pass3();
453
454	/*
455	 * 4: scan inodes looking for disconnected files; check reference counts
456	 */
457	if (preen == 0)
458		printf("** Phase 4 - Check Reference Counts\n");
459	pass4();
460
461	/*
462	 * 5: check and repair resource counts in cylinder groups
463	 */
464	if (preen == 0)
465		printf("** Phase 5 - Check Cyl groups\n");
466	pass5();
467
468	/*
469	 * print out summary statistics
470	 */
471	n_ffree = sblock.fs_cstotal.cs_nffree;
472	n_bfree = sblock.fs_cstotal.cs_nbfree;
473	files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
474	blks = n_blks +
475	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
476	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
477	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
478	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
479	if (bkgrdflag && (files > 0 || blks > 0)) {
480		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
481		pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n",
482		    countdirs, (long)files - countdirs, (long long)blks);
483	}
484	pwarn("%ld files, %jd used, %ju free ",
485	    (long)n_files, (intmax_t)n_blks,
486	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
487	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
488	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
489	    n_ffree * 100.0 / sblock.fs_dsize);
490	if (debug) {
491		if (files < 0)
492			printf("%d inodes missing\n", -files);
493		if (blks < 0)
494			printf("%lld blocks missing\n", -(long long)blks);
495		if (duplist != NULL) {
496			printf("The following duplicate blocks remain:");
497			for (dp = duplist; dp; dp = dp->next)
498				printf(" %lld,", (long long)dp->dup);
499			printf("\n");
500		}
501	}
502	duplist = (struct dups *)0;
503	muldup = (struct dups *)0;
504	inocleanup();
505	if (fsmodified) {
506		sblock.fs_time = time(NULL);
507		sbdirty();
508	}
509	if (cvtlevel && sblk.b_dirty) {
510		/*
511		 * Write out the duplicate super blocks
512		 */
513		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
514			blwrite(fswritefd, (char *)&sblock,
515			    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
516			    SBLOCKSIZE);
517	}
518	if (rerun)
519		resolved = 0;
520
521	/*
522	 * Check to see if the file system is mounted read-write.
523	 */
524	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
525		resolved = 0;
526	ckfini(resolved);
527
528	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
529		if (inostathead[cylno].il_stat != NULL)
530			free((char *)inostathead[cylno].il_stat);
531	free((char *)inostathead);
532	inostathead = NULL;
533	if (fsmodified && !preen)
534		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
535	if (rerun)
536		printf("\n***** PLEASE RERUN FSCK *****\n");
537	if (chkdoreload(mntp) != 0) {
538		if (!fsmodified)
539			return (0);
540		if (!preen)
541			printf("\n***** REBOOT NOW *****\n");
542		sync();
543		return (4);
544	}
545	return (0);
546}
547
548static int
549chkdoreload(struct statfs *mntp)
550{
551	struct iovec *iov;
552	int iovlen;
553	char errmsg[255];
554
555	if (mntp == NULL)
556		return (0);
557
558	iov = NULL;
559	iovlen = 0;
560	errmsg[0] = '\0';
561	/*
562	 * We modified a mounted file system.  Do a mount update on
563	 * it unless it is read-write, so we can continue using it
564	 * as safely as possible.
565	 */
566	if (mntp->f_flags & MNT_RDONLY) {
567		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
568		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
569		    (size_t)-1);
570		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
571		    (size_t)-1);
572		build_iovec(&iov, &iovlen, "errmsg", errmsg,
573		    sizeof(errmsg));
574		build_iovec(&iov, &iovlen, "update", NULL, 0);
575		build_iovec(&iov, &iovlen, "reload", NULL, 0);
576		/*
577		 * XX: We need the following line until we clean up
578		 * nmount parsing of root mounts and NFS root mounts.
579		 */
580		build_iovec(&iov, &iovlen, "ro", NULL, 0);
581		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
582			return (0);
583		}
584		pwarn("mount reload of '%s' failed: %s %s\n\n",
585		    mntp->f_mntonname, strerror(errno), errmsg);
586		return (1);
587	}
588	return (0);
589}
590
591/*
592 * Get the mount point information for name.
593 */
594static struct statfs *
595getmntpt(const char *name)
596{
597	struct stat devstat, mntdevstat;
598	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
599	char *ddevname;
600	struct statfs *mntbuf, *statfsp;
601	int i, mntsize, isdev;
602
603	if (stat(name, &devstat) != 0)
604		return (NULL);
605	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
606		isdev = 1;
607	else
608		isdev = 0;
609	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
610	for (i = 0; i < mntsize; i++) {
611		statfsp = &mntbuf[i];
612		ddevname = statfsp->f_mntfromname;
613		if (*ddevname != '/') {
614			strcpy(device, _PATH_DEV);
615			strcat(device, ddevname);
616			strcpy(statfsp->f_mntfromname, device);
617		}
618		if (isdev == 0) {
619			if (strcmp(name, statfsp->f_mntonname))
620				continue;
621			return (statfsp);
622		}
623		if (stat(ddevname, &mntdevstat) == 0 &&
624		    mntdevstat.st_rdev == devstat.st_rdev)
625			return (statfsp);
626	}
627	statfsp = NULL;
628	return (statfsp);
629}
630
631static void
632usage(void)
633{
634        (void) fprintf(stderr,
635            "usage: %s [-BFprfny] [-b block] [-c level] [-m mode] "
636                        "filesystem ...\n",
637            getprogname());
638        exit(1);
639}
640