xref: /freebsd/sbin/tunefs/tunefs.c (revision 77edab906e4d0c137fb6b4784ab34b44b7874fc7)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1983, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
138fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
148fae3551SRodney W. Grimes  *    must display the following acknowledgement:
158fae3551SRodney W. Grimes  *	This product includes software developed by the University of
168fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
178fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
188fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
198fae3551SRodney W. Grimes  *    without specific prior written permission.
208fae3551SRodney W. Grimes  *
218fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318fae3551SRodney W. Grimes  * SUCH DAMAGE.
328fae3551SRodney W. Grimes  */
338fae3551SRodney W. Grimes 
348fae3551SRodney W. Grimes #ifndef lint
358679b1b4SPhilippe Charnier static const char copyright[] =
368fae3551SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\
378fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #ifndef lint
418679b1b4SPhilippe Charnier #if 0
428fae3551SRodney W. Grimes static char sccsid[] = "@(#)tunefs.c	8.2 (Berkeley) 4/19/94";
438679b1b4SPhilippe Charnier #endif
448679b1b4SPhilippe Charnier static const char rcsid[] =
457f3dea24SPeter Wemm   "$FreeBSD$";
468fae3551SRodney W. Grimes #endif /* not lint */
478fae3551SRodney W. Grimes 
488fae3551SRodney W. Grimes /*
498fae3551SRodney W. Grimes  * tunefs: change layout parameters to an existing file system.
508fae3551SRodney W. Grimes  */
518fae3551SRodney W. Grimes #include <sys/param.h>
527382c45aSLuoqi Chen #include <sys/mount.h>
538fae3551SRodney W. Grimes #include <sys/stat.h>
548fae3551SRodney W. Grimes 
558fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
567382c45aSLuoqi Chen #include <ufs/ufs/ufsmount.h>
578fae3551SRodney W. Grimes 
588fae3551SRodney W. Grimes #include <err.h>
598fae3551SRodney W. Grimes #include <fcntl.h>
608fae3551SRodney W. Grimes #include <fstab.h>
618fae3551SRodney W. Grimes #include <paths.h>
628679b1b4SPhilippe Charnier #include <stdio.h>
638fae3551SRodney W. Grimes #include <stdlib.h>
64060ac658SSheldon Hearn #include <string.h>
658fae3551SRodney W. Grimes #include <unistd.h>
668fae3551SRodney W. Grimes 
678fae3551SRodney W. Grimes /* the optimization warning string template */
688fae3551SRodney W. Grimes #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
698fae3551SRodney W. Grimes 
708fae3551SRodney W. Grimes union {
718fae3551SRodney W. Grimes 	struct	fs sb;
728fae3551SRodney W. Grimes 	char pad[MAXBSIZE];
738fae3551SRodney W. Grimes } sbun;
748fae3551SRodney W. Grimes #define	sblock sbun.sb
758fae3551SRodney W. Grimes 
768fae3551SRodney W. Grimes int fi;
778fae3551SRodney W. Grimes long dev_bsize = 1;
788fae3551SRodney W. Grimes 
79060ac658SSheldon Hearn void bwrite __P((daddr_t, char *, int));
80060ac658SSheldon Hearn int bread __P((daddr_t, char *, int));
81060ac658SSheldon Hearn void getsb __P((struct fs *, char *));
82060ac658SSheldon Hearn void putsb __P((struct fs *, char *, int));
838fae3551SRodney W. Grimes void usage __P((void));
8416a7269eSJoerg Wunsch void printfs __P((void));
858fae3551SRodney W. Grimes 
868fae3551SRodney W. Grimes int
878fae3551SRodney W. Grimes main(argc, argv)
888fae3551SRodney W. Grimes 	int argc;
898fae3551SRodney W. Grimes 	char *argv[];
908fae3551SRodney W. Grimes {
9177edab90SPhilippe Charnier 	char *special, *name;
928fae3551SRodney W. Grimes 	struct stat st;
937382c45aSLuoqi Chen 	int Aflag = 0, active = 0;
9477edab90SPhilippe Charnier 	int aflag = 0, dflag = 0, eflag = 0, mflag = 0;
9577edab90SPhilippe Charnier 	int nflag = 0, oflag = 0, pflag = 0;
9677edab90SPhilippe Charnier 	int avalue = 0, dvalue = 0, evalue = 0, mvalue = 0, ovalue = 0;
9777edab90SPhilippe Charnier 	char *nvalue = NULL;
988fae3551SRodney W. Grimes 	struct fstab *fs;
998fae3551SRodney W. Grimes 	char *chg[2], device[MAXPATHLEN];
1007382c45aSLuoqi Chen 	struct ufs_args args;
1017382c45aSLuoqi Chen 	struct statfs stfs;
1022af14b60SPhilippe Charnier 	int found_arg, ch;
1038fae3551SRodney W. Grimes 
1042af14b60SPhilippe Charnier         if (argc < 3)
1058fae3551SRodney W. Grimes                 usage();
10677edab90SPhilippe Charnier 	found_arg = 0; /* at least one arg is required */
10777edab90SPhilippe Charnier 	while ((ch = getopt(argc, argv, "Aa:d:e:m:n:o:p")) != -1)
10877edab90SPhilippe Charnier 	  switch (ch) {
10977edab90SPhilippe Charnier 	  case 'A':
11077edab90SPhilippe Charnier 		found_arg = 1;
11177edab90SPhilippe Charnier 		Aflag++;
11277edab90SPhilippe Charnier 		break;
11377edab90SPhilippe Charnier 	  case 'a':
11477edab90SPhilippe Charnier 		found_arg = 1;
11577edab90SPhilippe Charnier 		name = "maximum contiguous block count";
11677edab90SPhilippe Charnier 		avalue = atoi(optarg);
11777edab90SPhilippe Charnier 		if (avalue < 1)
11877edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
11977edab90SPhilippe Charnier 		aflag = 1;
12077edab90SPhilippe Charnier 		break;
12177edab90SPhilippe Charnier 	  case 'd':
12277edab90SPhilippe Charnier 		found_arg = 1;
12377edab90SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
12477edab90SPhilippe Charnier 		dvalue = atoi(optarg);
12577edab90SPhilippe Charnier 		dflag = 1;
12677edab90SPhilippe Charnier 		break;
12777edab90SPhilippe Charnier 	  case 'e':
12877edab90SPhilippe Charnier 		found_arg = 1;
12977edab90SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
13077edab90SPhilippe Charnier 		evalue = atoi(optarg);
13177edab90SPhilippe Charnier 		if (evalue < 1)
13277edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
13377edab90SPhilippe Charnier 		eflag = 1;
13477edab90SPhilippe Charnier 		break;
13577edab90SPhilippe Charnier 	  case 'm':
13677edab90SPhilippe Charnier 		found_arg = 1;
13777edab90SPhilippe Charnier 		name = "minimum percentage of free space";
13877edab90SPhilippe Charnier 		mvalue = atoi(optarg);
13977edab90SPhilippe Charnier 		if (mvalue < 0 || mvalue > 99)
14077edab90SPhilippe Charnier 			errx(10, "bad %s (%s)", name, optarg);
14177edab90SPhilippe Charnier 		mflag = 1;
14277edab90SPhilippe Charnier 		break;
14377edab90SPhilippe Charnier 	  case 'n':
14477edab90SPhilippe Charnier 		found_arg = 1;
14577edab90SPhilippe Charnier  		name = "soft updates";
14677edab90SPhilippe Charnier  		nvalue = optarg;
14777edab90SPhilippe Charnier                 if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
14877edab90SPhilippe Charnier  			errx(10, "bad %s (options are %s)",
14977edab90SPhilippe Charnier  			    name, "`enable' or `disable'");
15077edab90SPhilippe Charnier  		}
15177edab90SPhilippe Charnier 		nflag = 1;
15277edab90SPhilippe Charnier  		break;
15377edab90SPhilippe Charnier 	  case 'o':
15477edab90SPhilippe Charnier 		found_arg = 1;
15577edab90SPhilippe Charnier 		name = "optimization preference";
15677edab90SPhilippe Charnier 		chg[FS_OPTSPACE] = "space";
15777edab90SPhilippe Charnier 		chg[FS_OPTTIME] = "time";
15877edab90SPhilippe Charnier 		if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
15977edab90SPhilippe Charnier 			ovalue = FS_OPTSPACE;
16077edab90SPhilippe Charnier 		else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
16177edab90SPhilippe Charnier 			ovalue = FS_OPTTIME;
16277edab90SPhilippe Charnier 		else
16377edab90SPhilippe Charnier 			errx(10, "bad %s (options are `space' or `time')",
16477edab90SPhilippe Charnier 					    name);
16577edab90SPhilippe Charnier 		oflag = 1;
16677edab90SPhilippe Charnier 		break;
16777edab90SPhilippe Charnier 	  case 'p':
16877edab90SPhilippe Charnier 		pflag = 1;
16977edab90SPhilippe Charnier 		break;
17077edab90SPhilippe Charnier 	  default:
17177edab90SPhilippe Charnier 		usage();
17277edab90SPhilippe Charnier 	  }
17377edab90SPhilippe Charnier 	argc -= optind;
17477edab90SPhilippe Charnier 	argv += optind;
17577edab90SPhilippe Charnier 
17677edab90SPhilippe Charnier 	if (found_arg == 0 || argc != 1)
17777edab90SPhilippe Charnier 	  usage();
17877edab90SPhilippe Charnier 
17977edab90SPhilippe Charnier 	special = argv[0];
1808fae3551SRodney W. Grimes 	fs = getfsfile(special);
1817382c45aSLuoqi Chen 	if (fs) {
1829ea6e95eSLuoqi Chen 		if (statfs(special, &stfs) == 0 &&
1839ea6e95eSLuoqi Chen 		    strcmp(special, stfs.f_mntonname) == 0) {
1847382c45aSLuoqi Chen 			active = 1;
185b20ae6a0SLuoqi Chen 		}
1868fae3551SRodney W. Grimes 		special = fs->fs_spec;
1877382c45aSLuoqi Chen 	}
1888fae3551SRodney W. Grimes again:
1898fae3551SRodney W. Grimes 	if (stat(special, &st) < 0) {
1908fae3551SRodney W. Grimes 		if (*special != '/') {
1918fae3551SRodney W. Grimes 			if (*special == 'r')
1928fae3551SRodney W. Grimes 				special++;
1938fae3551SRodney W. Grimes 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
1948fae3551SRodney W. Grimes 			special = device;
1958fae3551SRodney W. Grimes 			goto again;
1968fae3551SRodney W. Grimes 		}
1978fae3551SRodney W. Grimes 		err(1, "%s", special);
1988fae3551SRodney W. Grimes 	}
1998fae3551SRodney W. Grimes 	if ((st.st_mode & S_IFMT) != S_IFBLK &&
2008fae3551SRodney W. Grimes 	    (st.st_mode & S_IFMT) != S_IFCHR)
2018fae3551SRodney W. Grimes 		errx(10, "%s: not a block or character device", special);
2028fae3551SRodney W. Grimes 	getsb(&sblock, special);
2038fae3551SRodney W. Grimes 
20477edab90SPhilippe Charnier 	if (pflag) {
20577edab90SPhilippe Charnier 		printfs();
20677edab90SPhilippe Charnier 		exit(0);
20777edab90SPhilippe Charnier 	}
20877edab90SPhilippe Charnier 	if (aflag) {
2098fae3551SRodney W. Grimes 		name = "maximum contiguous block count";
21077edab90SPhilippe Charnier 		if (sblock.fs_maxcontig == avalue) {
21177edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, avalue);
2122af14b60SPhilippe Charnier 		}
21377edab90SPhilippe Charnier 		else {
21477edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
21577edab90SPhilippe Charnier 					name, sblock.fs_maxcontig, avalue);
21677edab90SPhilippe Charnier 			sblock.fs_maxcontig = avalue;
21777edab90SPhilippe Charnier 		}
21877edab90SPhilippe Charnier 	}
21977edab90SPhilippe Charnier 	if (dflag) {
2202af14b60SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
22177edab90SPhilippe Charnier 		if (sblock.fs_rotdelay == dvalue) {
22277edab90SPhilippe Charnier 			warnx("%s remains unchanged as %dms", name, dvalue);
2232af14b60SPhilippe Charnier 		}
22477edab90SPhilippe Charnier 		else {
2258fae3551SRodney W. Grimes 			warnx("%s changes from %dms to %dms",
22677edab90SPhilippe Charnier 				    name, sblock.fs_rotdelay, dvalue);
22777edab90SPhilippe Charnier 			sblock.fs_rotdelay = dvalue;
22877edab90SPhilippe Charnier 		}
22977edab90SPhilippe Charnier 	}
23077edab90SPhilippe Charnier 	if (eflag) {
2312af14b60SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
23277edab90SPhilippe Charnier 		if (sblock.fs_maxbpg == evalue) {
23377edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, evalue);
2342af14b60SPhilippe Charnier 		}
23577edab90SPhilippe Charnier 		else {
23677edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
23777edab90SPhilippe Charnier 					name, sblock.fs_maxbpg, evalue);
23877edab90SPhilippe Charnier 			sblock.fs_maxbpg = evalue;
23977edab90SPhilippe Charnier 		}
24077edab90SPhilippe Charnier 	}
24177edab90SPhilippe Charnier 	if (mflag) {
2428fae3551SRodney W. Grimes 		name = "minimum percentage of free space";
24377edab90SPhilippe Charnier 		if (sblock.fs_minfree == mvalue) {
24477edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d%%", name, mvalue);
2452af14b60SPhilippe Charnier 		}
24677edab90SPhilippe Charnier 		else {
2478fae3551SRodney W. Grimes 			warnx("%s changes from %d%% to %d%%",
24877edab90SPhilippe Charnier 				    name, sblock.fs_minfree, mvalue);
24977edab90SPhilippe Charnier 			sblock.fs_minfree = mvalue;
25077edab90SPhilippe Charnier 			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
2518fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
25277edab90SPhilippe Charnier 			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
2538fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
254b1897c19SJulian Elischer 		}
25577edab90SPhilippe Charnier 	}
25677edab90SPhilippe Charnier 	if (nflag) {
25777edab90SPhilippe Charnier  		name = "soft updates";
25877edab90SPhilippe Charnier  		if (strcmp(nvalue, "enable") == 0) {
25977edab90SPhilippe Charnier 			if ( sblock.fs_flags & FS_DOSOFTDEP ) {
26077edab90SPhilippe Charnier 				warnx("%s remains unchanged as enabled", name);
26177edab90SPhilippe Charnier 			} else {
26277edab90SPhilippe Charnier  				sblock.fs_flags |= FS_DOSOFTDEP;
26377edab90SPhilippe Charnier  				warnx("%s set", name);
26477edab90SPhilippe Charnier 			}
26577edab90SPhilippe Charnier  		} else if (strcmp(nvalue, "disable") == 0) {
26677edab90SPhilippe Charnier 			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
26777edab90SPhilippe Charnier 				warnx("%s remains unchanged as disabled", name);
26877edab90SPhilippe Charnier 			} else {
26977edab90SPhilippe Charnier  				sblock.fs_flags &= ~FS_DOSOFTDEP;
27077edab90SPhilippe Charnier  				warnx("%s cleared", name);
27177edab90SPhilippe Charnier 			}
27277edab90SPhilippe Charnier  		}
27377edab90SPhilippe Charnier 	}
27477edab90SPhilippe Charnier 	if (oflag) {
2758fae3551SRodney W. Grimes 		name = "optimization preference";
2768fae3551SRodney W. Grimes 		chg[FS_OPTSPACE] = "space";
2778fae3551SRodney W. Grimes 		chg[FS_OPTTIME] = "time";
27877edab90SPhilippe Charnier 		if (sblock.fs_optim == ovalue) {
27977edab90SPhilippe Charnier 			warnx("%s remains unchanged as %s", name, chg[ovalue]);
2808fae3551SRodney W. Grimes 		}
28177edab90SPhilippe Charnier 		else {
2828fae3551SRodney W. Grimes 			warnx("%s changes from %s to %s",
28377edab90SPhilippe Charnier 				    name, chg[sblock.fs_optim], chg[ovalue]);
28477edab90SPhilippe Charnier 			sblock.fs_optim = ovalue;
28577edab90SPhilippe Charnier 			if (sblock.fs_minfree >= MINFREE &&
28677edab90SPhilippe Charnier 					ovalue == FS_OPTSPACE)
2878fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
28877edab90SPhilippe Charnier 			if (sblock.fs_minfree < MINFREE &&
28977edab90SPhilippe Charnier 					ovalue == FS_OPTTIME)
2908fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
2918fae3551SRodney W. Grimes 		}
29277edab90SPhilippe Charnier 	}
2932af14b60SPhilippe Charnier 
294060ac658SSheldon Hearn 	putsb(&sblock, special, Aflag);
2957382c45aSLuoqi Chen 	if (active) {
2967382c45aSLuoqi Chen 		bzero(&args, sizeof(args));
2977382c45aSLuoqi Chen 		if (mount("ufs", fs->fs_file,
2987382c45aSLuoqi Chen 		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
2997382c45aSLuoqi Chen 			err(9, "%s: reload", special);
3007382c45aSLuoqi Chen 		warnx("file system reloaded");
3017382c45aSLuoqi Chen 	}
3028fae3551SRodney W. Grimes 	exit(0);
3038fae3551SRodney W. Grimes }
3048fae3551SRodney W. Grimes 
3058fae3551SRodney W. Grimes void
3068fae3551SRodney W. Grimes usage()
3078fae3551SRodney W. Grimes {
3088679b1b4SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n%s\n",
3098679b1b4SPhilippe Charnier "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-m minfree]",
31077edab90SPhilippe Charnier "              [-p] [-n enable | disable] [-o space | time]",
3112af14b60SPhilippe Charnier "              special | filesystem");
3128fae3551SRodney W. Grimes 	exit(2);
3138fae3551SRodney W. Grimes }
3148fae3551SRodney W. Grimes 
3158fae3551SRodney W. Grimes void
3168fae3551SRodney W. Grimes getsb(fs, file)
3178fae3551SRodney W. Grimes 	register struct fs *fs;
3188fae3551SRodney W. Grimes 	char *file;
3198fae3551SRodney W. Grimes {
3208fae3551SRodney W. Grimes 
321060ac658SSheldon Hearn 	fi = open(file, O_RDONLY);
3228fae3551SRodney W. Grimes 	if (fi < 0)
3238fae3551SRodney W. Grimes 		err(3, "cannot open %s", file);
3248fae3551SRodney W. Grimes 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
3258fae3551SRodney W. Grimes 		err(4, "%s: bad super block", file);
3268fae3551SRodney W. Grimes 	if (fs->fs_magic != FS_MAGIC)
3278fae3551SRodney W. Grimes 		err(5, "%s: bad magic number", file);
3288fae3551SRodney W. Grimes 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
3298fae3551SRodney W. Grimes }
3308fae3551SRodney W. Grimes 
3318fae3551SRodney W. Grimes void
332060ac658SSheldon Hearn putsb(fs, file, all)
333060ac658SSheldon Hearn 	register struct fs *fs;
334060ac658SSheldon Hearn 	char *file;
335060ac658SSheldon Hearn 	int all;
336060ac658SSheldon Hearn {
337060ac658SSheldon Hearn 	int i;
338060ac658SSheldon Hearn 
339060ac658SSheldon Hearn 	/*
340060ac658SSheldon Hearn 	 * Re-open the device read-write. Use the read-only file
341060ac658SSheldon Hearn 	 * descriptor as an interlock to prevent the device from
342060ac658SSheldon Hearn 	 * being mounted while we are switching mode.
343060ac658SSheldon Hearn 	 */
344060ac658SSheldon Hearn 	i = fi;
345060ac658SSheldon Hearn 	fi = open(file, O_RDWR);
346060ac658SSheldon Hearn 	close(i);
347060ac658SSheldon Hearn 	if (fi < 0)
348060ac658SSheldon Hearn 		err(3, "cannot open %s", file);
349060ac658SSheldon Hearn 	bwrite((daddr_t)SBOFF / dev_bsize, (char *)fs, SBSIZE);
350060ac658SSheldon Hearn 	if (all)
351060ac658SSheldon Hearn 		for (i = 0; i < fs->fs_ncg; i++)
352060ac658SSheldon Hearn 			bwrite(fsbtodb(fs, cgsblock(fs, i)),
353060ac658SSheldon Hearn 			    (char *)fs, SBSIZE);
354060ac658SSheldon Hearn 	close(fi);
355060ac658SSheldon Hearn }
356060ac658SSheldon Hearn 
357060ac658SSheldon Hearn void
35816a7269eSJoerg Wunsch printfs()
35916a7269eSJoerg Wunsch {
360b1897c19SJulian Elischer 	warnx("soft updates:  (-n)                                %s",
361b1897c19SJulian Elischer 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
36216a7269eSJoerg Wunsch 	warnx("maximum contiguous block count: (-a)               %d",
36316a7269eSJoerg Wunsch 	      sblock.fs_maxcontig);
36416a7269eSJoerg Wunsch 	warnx("rotational delay between contiguous blocks: (-d)   %d ms",
36516a7269eSJoerg Wunsch 	      sblock.fs_rotdelay);
36616a7269eSJoerg Wunsch 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
36716a7269eSJoerg Wunsch 	      sblock.fs_maxbpg);
36816a7269eSJoerg Wunsch 	warnx("minimum percentage of free space: (-m)             %d%%",
36916a7269eSJoerg Wunsch 	      sblock.fs_minfree);
37016a7269eSJoerg Wunsch 	warnx("optimization preference: (-o)                      %s",
37116a7269eSJoerg Wunsch 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
37216a7269eSJoerg Wunsch 	if (sblock.fs_minfree >= MINFREE &&
37316a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTSPACE)
37416a7269eSJoerg Wunsch 		warnx(OPTWARN, "time", ">=", MINFREE);
37516a7269eSJoerg Wunsch 	if (sblock.fs_minfree < MINFREE &&
37616a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTTIME)
37716a7269eSJoerg Wunsch 		warnx(OPTWARN, "space", "<", MINFREE);
37816a7269eSJoerg Wunsch }
37916a7269eSJoerg Wunsch 
38016a7269eSJoerg Wunsch void
3818fae3551SRodney W. Grimes bwrite(blk, buf, size)
3828fae3551SRodney W. Grimes 	daddr_t blk;
3838fae3551SRodney W. Grimes 	char *buf;
3848fae3551SRodney W. Grimes 	int size;
3858fae3551SRodney W. Grimes {
3868fae3551SRodney W. Grimes 
3878fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
3888fae3551SRodney W. Grimes 		err(6, "FS SEEK");
3898fae3551SRodney W. Grimes 	if (write(fi, buf, size) != size)
3908fae3551SRodney W. Grimes 		err(7, "FS WRITE");
3918fae3551SRodney W. Grimes }
3928fae3551SRodney W. Grimes 
3938fae3551SRodney W. Grimes int
3948fae3551SRodney W. Grimes bread(bno, buf, cnt)
3958fae3551SRodney W. Grimes 	daddr_t bno;
3968fae3551SRodney W. Grimes 	char *buf;
3978fae3551SRodney W. Grimes 	int cnt;
3988fae3551SRodney W. Grimes {
3998fae3551SRodney W. Grimes 	int i;
4008fae3551SRodney W. Grimes 
4018fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
4028fae3551SRodney W. Grimes 		return(1);
4038fae3551SRodney W. Grimes 	if ((i = read(fi, buf, cnt)) != cnt) {
4048fae3551SRodney W. Grimes 		for(i=0; i<sblock.fs_bsize; i++)
4058fae3551SRodney W. Grimes 			buf[i] = 0;
4068fae3551SRodney W. Grimes 		return (1);
4078fae3551SRodney W. Grimes 	}
4088fae3551SRodney W. Grimes 	return (0);
4098fae3551SRodney W. Grimes }
410