Home | History | Annotate | Line # | Download | only in tunefs
tunefs.c revision 1.54
      1 /*	$NetBSD: tunefs.c,v 1.54 2020/11/26 02:06:01 dholland Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)tunefs.c	8.3 (Berkeley) 5/3/95";
     41 #else
     42 __RCSID("$NetBSD: tunefs.c,v 1.54 2020/11/26 02:06:01 dholland Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 /*
     47  * tunefs: change layout parameters to an existing file system.
     48  */
     49 #include <sys/param.h>
     50 #include <sys/statvfs.h>
     51 
     52 #include <ufs/ffs/fs.h>
     53 #include <ufs/ffs/ffs_extern.h>
     54 #include <ufs/ufs/ufs_wapbl.h>
     55 #include <ufs/ufs/ufsmount.h>
     56 #include <ufs/ufs/quota2.h>
     57 
     58 #include <machine/bswap.h>
     59 
     60 #include <err.h>
     61 #include <errno.h>
     62 #include <fcntl.h>
     63 #include <fstab.h>
     64 #include <paths.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <unistd.h>
     69 #include <util.h>
     70 
     71 /* the optimization warning string template */
     72 #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
     73 
     74 union {
     75 	struct	fs sb;
     76 	char data[MAXBSIZE];
     77 } sbun, buf;
     78 #define	sblock sbun.sb
     79 
     80 int	fi;
     81 long	dev_bsize = 512;
     82 int	needswap = 0;
     83 int	is_ufs2 = 0;
     84 off_t	sblockloc;
     85 int	userquota = 0;
     86 int	groupquota = 0;
     87 #define Q2_EN  (1)
     88 #define Q2_IGN (0)
     89 #define Q2_DIS (-1)
     90 
     91 static off_t sblock_try[] = SBLOCKSEARCH;
     92 
     93 static	void	bwrite(daddr_t, char *, int, const char *);
     94 static	void	bread(daddr_t, char *, int, const char *);
     95 static	void	change_log_info(long long);
     96 static	void	getsb(struct fs *, const char *);
     97 static	int	openpartition(const char *, int, char *, size_t);
     98 static	int	isactive(int, struct statvfs *);
     99 static	void	show_log_info(void);
    100 __dead static	void	usage(void);
    101 
    102 int
    103 main(int argc, char *argv[])
    104 {
    105 	int		i, ch, aflag, pflag, Aflag, Fflag, Nflag, openflags;
    106 	const char	*special, *chg[2];
    107 	char		device[MAXPATHLEN];
    108 	int		maxbpg, minfree, optim, secsize;
    109 	int		avgfilesize, avgfpdir, active;
    110 	long long	logfilesize;
    111 	int		secshift, fsbtodb;
    112 	const char  	*avalue, *pvalue, *name;
    113 	struct statvfs	sfs;
    114 
    115 	aflag = pflag = Aflag = Fflag = Nflag = 0;
    116 	avalue = pvalue = NULL;
    117 	maxbpg = minfree = optim = secsize = -1;
    118 	avgfilesize = avgfpdir = -1;
    119 	logfilesize = -1;
    120 	secshift = 0;
    121 	chg[FS_OPTSPACE] = "space";
    122 	chg[FS_OPTTIME] = "time";
    123 
    124 	while ((ch = getopt(argc, argv, "AFNa:e:g:h:l:m:o:p:q:S:")) != -1) {
    125 		switch (ch) {
    126 
    127 		case 'A':
    128 			Aflag++;
    129 			break;
    130 
    131 		case 'F':
    132 			Fflag++;
    133 			break;
    134 
    135 		case 'N':
    136 			Nflag++;
    137 			break;
    138 
    139 		case 'a':
    140 			name = "ACLs";
    141 			avalue = optarg;
    142 			if (strcmp(avalue, "enable") &&
    143 			    strcmp(avalue, "disable")) {
    144 				errx(10, "bad %s (options are %s)",
    145 				    name, "`enable' or `disable'");
    146 			}
    147 			aflag = 1;
    148  			break;
    149 
    150 		case 'e':
    151 			maxbpg = strsuftoll(
    152 			    "maximum blocks per file in a cylinder group",
    153 			    optarg, 1, INT_MAX);
    154 			break;
    155 
    156 		case 'g':
    157 			avgfilesize = strsuftoll("average file size", optarg,
    158 			    1, INT_MAX);
    159 			break;
    160 
    161 		case 'h':
    162 			avgfpdir = strsuftoll(
    163 			    "expected number of files per directory",
    164 			    optarg, 1, INT_MAX);
    165 			break;
    166 
    167 		case 'l':
    168 			logfilesize = strsuftoll("journal log file size",
    169 			    optarg, 0, INT_MAX);
    170 			break;
    171 
    172 		case 'm':
    173 			minfree = strsuftoll("minimum percentage of free space",
    174 			    optarg, 0, 99);
    175 			break;
    176 
    177 		case 'o':
    178 			if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
    179 				optim = FS_OPTSPACE;
    180 			else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
    181 				optim = FS_OPTTIME;
    182 			else
    183 				errx(10,
    184 				    "bad %s (options are `space' or `time')",
    185 				    "optimization preference");
    186 			break;
    187 
    188 		case 'p':
    189 			name = "POSIX1e ACLs";
    190 			pvalue = optarg;
    191 			if (strcmp(pvalue, "enable") &&
    192 			    strcmp(pvalue, "disable")) {
    193 				errx(10, "bad %s (options are %s)",
    194 				    name, "`enable' or `disable'");
    195 			}
    196 			pflag = 1;
    197 			break;
    198 
    199 		case 'q':
    200 			if      (strcmp(optarg, "user") == 0)
    201 				userquota = Q2_EN;
    202 			else if (strcmp(optarg, "group") == 0)
    203 				groupquota = Q2_EN;
    204 			else if (strcmp(optarg, "nouser") == 0)
    205 				userquota = Q2_DIS;
    206 			else if (strcmp(optarg, "nogroup") == 0)
    207 				groupquota = Q2_DIS;
    208 			else
    209 			    errx(11, "invalid quota type %s", optarg);
    210 			break;
    211 
    212 		case 'S':
    213 			secsize = strsuftoll("physical sector size",
    214 			    optarg, 0, INT_MAX);
    215 			secshift = ffs(secsize) - 1;
    216 			if (secsize != 0 && 1 << secshift != secsize)
    217 				errx(12, "sector size %d is not a power of two", secsize);
    218 			break;
    219 
    220 		default:
    221 			usage();
    222 		}
    223 	}
    224 	argc -= optind;
    225 	argv += optind;
    226 	if (argc != 1)
    227 		usage();
    228 
    229 	special = argv[0];
    230 	openflags = Nflag ? O_RDONLY : O_RDWR;
    231 	if (Fflag)
    232 		fi = open(special, openflags);
    233 	else {
    234 		fi = openpartition(special, openflags, device, sizeof(device));
    235 		special = device;
    236 	}
    237 	if (fi == -1)
    238 		err(1, "%s", special);
    239 	active = !Fflag && isactive(fi, &sfs);
    240 	getsb(&sblock, special);
    241 
    242 #define CHANGEVAL(old, new, type, suffix) do				\
    243 	if ((new) != -1) {						\
    244 		if ((new) == (old))					\
    245 			warnx("%s remains unchanged at %d%s",		\
    246 			    (type), (old), (suffix));			\
    247 		else {							\
    248 			warnx("%s changes from %d%s to %d%s",		\
    249 			    (type), (old), (suffix), (new), (suffix));	\
    250 			(old) = (new);					\
    251 		}							\
    252 	} while (/* CONSTCOND */0)
    253 
    254 	warnx("tuning %s", special);
    255 	CHANGEVAL(sblock.fs_maxbpg, maxbpg,
    256 	    "maximum blocks per file in a cylinder group", "");
    257 	CHANGEVAL(sblock.fs_minfree, minfree,
    258 	    "minimum percentage of free space", "%");
    259 	if (minfree != -1) {
    260 		if (minfree >= MINFREE &&
    261 		    sblock.fs_optim == FS_OPTSPACE)
    262 			warnx(OPTWARN, "time", ">=", MINFREE);
    263 		if (minfree < MINFREE &&
    264 		    sblock.fs_optim == FS_OPTTIME)
    265 			warnx(OPTWARN, "space", "<", MINFREE);
    266 	}
    267 	if (optim != -1) {
    268 		if (sblock.fs_optim == optim) {
    269 			warnx("%s remains unchanged as %s",
    270 			    "optimization preference",
    271 			    chg[optim]);
    272 		} else {
    273 			warnx("%s changes from %s to %s",
    274 			    "optimization preference",
    275 			    chg[sblock.fs_optim], chg[optim]);
    276 			sblock.fs_optim = optim;
    277 			if (sblock.fs_minfree >= MINFREE &&
    278 			    optim == FS_OPTSPACE)
    279 				warnx(OPTWARN, "time", ">=", MINFREE);
    280 			if (sblock.fs_minfree < MINFREE &&
    281 			    optim == FS_OPTTIME)
    282 				warnx(OPTWARN, "space", "<", MINFREE);
    283 		}
    284 	}
    285 	if (secsize != -1) {
    286 		if (secsize == 0) {
    287 			secsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
    288 			secshift = ffs(secsize) - 1;
    289 		}
    290 
    291 		if (secshift < DEV_BSHIFT)
    292 			warnx("sector size must be at least %d", DEV_BSIZE);
    293 		else if (secshift > sblock.fs_fshift)
    294 			warnx("sector size %d cannot be larger than fragment size %d",
    295 				secsize, sblock.fs_fsize);
    296 		else {
    297 			fsbtodb = sblock.fs_fshift - secshift;
    298 			if (fsbtodb == sblock.fs_fsbtodb) {
    299 				warnx("sector size remains unchanged as %d",
    300 					sblock.fs_fsize / FFS_FSBTODB(&sblock, 1));
    301 			} else {
    302 				warnx("sector size changed from %d to %d",
    303 					sblock.fs_fsize / FFS_FSBTODB(&sblock, 1),
    304 					secsize);
    305 				sblock.fs_fsbtodb = fsbtodb;
    306 			}
    307 		}
    308 	}
    309 	CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
    310 	    "average file size", "");
    311 	CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
    312 	    "expected number of files per directory", "");
    313 
    314 	if (logfilesize >= 0)
    315 		change_log_info(logfilesize);
    316 	if (userquota == Q2_EN || groupquota == Q2_EN)
    317 		sblock.fs_flags |= FS_DOQUOTA2;
    318 	if (sblock.fs_flags & FS_DOQUOTA2) {
    319 		sblock.fs_quota_magic = Q2_HEAD_MAGIC;
    320 		switch(userquota) {
    321 		case Q2_EN:
    322 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
    323 			    == 0) {
    324 				printf("enabling user quotas\n");
    325 				sblock.fs_quota_flags |=
    326 				    FS_Q2_DO_TYPE(USRQUOTA);
    327 				sblock.fs_quotafile[USRQUOTA] = 0;
    328 			}
    329 			break;
    330 		case Q2_DIS:
    331 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
    332 			    != 0) {
    333 				printf("disabling user quotas\n");
    334 				sblock.fs_quota_flags &=
    335 				    ~FS_Q2_DO_TYPE(USRQUOTA);
    336 			}
    337 		}
    338 		switch(groupquota) {
    339 		case Q2_EN:
    340 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
    341 			    == 0) {
    342 				printf("enabling group quotas\n");
    343 				sblock.fs_quota_flags |=
    344 				    FS_Q2_DO_TYPE(GRPQUOTA);
    345 				sblock.fs_quotafile[GRPQUOTA] = 0;
    346 			}
    347 			break;
    348 		case Q2_DIS:
    349 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
    350 			    != 0) {
    351 				printf("disabling group quotas\n");
    352 				sblock.fs_quota_flags &=
    353 				    ~FS_Q2_DO_TYPE(GRPQUOTA);
    354 			}
    355 		}
    356 	}
    357 	/*
    358 	 * if we disabled all quotas, FS_DOQUOTA2 and associated inode(s) will
    359 	 * be cleared by kernel or fsck.
    360 	 */
    361 	if (aflag) {
    362 		name = "ACLs";
    363 		if (strcmp(avalue, "enable") == 0) {
    364 			if (sblock.fs_flags & FS_ACLS) {
    365 				warnx("%s remains unchanged as enabled", name);
    366 			} else if (sblock.fs_flags & FS_POSIX1EACLS) {
    367 				warnx("%s and POSIX.1e ACLs are mutually "
    368 				    "exclusive", name);
    369 			} else {
    370 				sblock.fs_flags |= FS_ACLS;
    371 				printf("%s set\n", name);
    372 			}
    373 		} else if (strcmp(avalue, "disable") == 0) {
    374 			if ((~sblock.fs_flags & FS_ACLS) == FS_ACLS) {
    375 				warnx("%s remains unchanged as disabled",
    376 				    name);
    377 			} else {
    378 				sblock.fs_flags &= ~FS_ACLS;
    379 				printf("%s cleared\n", name);
    380 			}
    381  		}
    382  	}
    383 
    384 	if (pflag) {
    385 		name = "POSIX1e ACLs";
    386 		if (strcmp(pvalue, "enable") == 0) {
    387 			if (sblock.fs_flags & FS_POSIX1EACLS) {
    388 				warnx("%s remains unchanged as enabled", name);
    389 			} else if (sblock.fs_flags & FS_ACLS) {
    390 				warnx("%s and ACLs are mutually "
    391 				    "exclusive", name);
    392 			} else {
    393 				sblock.fs_flags |= FS_POSIX1EACLS;
    394 				printf("%s set\n", name);
    395 			}
    396 		} else if (strcmp(pvalue, "disable") == 0) {
    397 			if ((~sblock.fs_flags & FS_POSIX1EACLS) ==
    398 			    FS_POSIX1EACLS) {
    399 				warnx("%s remains unchanged as disabled",
    400 				    name);
    401 			} else {
    402 				sblock.fs_flags &= ~FS_POSIX1EACLS;
    403 				printf("%s cleared\n", name);
    404 			}
    405 		}
    406 	}
    407 
    408 	if (Nflag) {
    409 		printf("%s: current settings of %s\n", getprogname(), special);
    410 		printf("\tmaximum contiguous block count %d\n",
    411 		    sblock.fs_maxcontig);
    412 		printf("\tmaximum blocks per file in a cylinder group %d\n",
    413 		    sblock.fs_maxbpg);
    414 		printf("\tminimum percentage of free space %d%%\n",
    415 		    sblock.fs_minfree);
    416 		printf("\toptimization preference: %s\n", chg[sblock.fs_optim]);
    417 		printf("\taverage file size: %d\n", sblock.fs_avgfilesize);
    418 		printf("\texpected number of files per directory: %d\n",
    419 		    sblock.fs_avgfpdir);
    420 		show_log_info();
    421 		printf("\tquotas");
    422 		if (sblock.fs_flags & FS_DOQUOTA2) {
    423 			if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) {
    424 				printf(" user");
    425 				if (sblock.fs_quota_flags &
    426 				    FS_Q2_DO_TYPE(GRPQUOTA))
    427 					printf(",");
    428 			}
    429 			if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
    430 				printf(" group");
    431 			printf(" enabled\n");
    432 		} else {
    433 			printf(" disabled\n");
    434 		}
    435 		printf("\tPOSIX.1e ACLs %s\n",
    436 		    (sblock.fs_flags & FS_POSIX1EACLS) ? "enabled" : "disabled");
    437 		printf("\tACLs %s\n",
    438 		    (sblock.fs_flags & FS_ACLS) ? "enabled" : "disabled");
    439 		printf("%s: no changes made\n", getprogname());
    440 		return 0;
    441 	}
    442 
    443 	memcpy(&buf, (char *)&sblock, SBLOCKSIZE);
    444 	if (needswap)
    445 		ffs_sb_swap((struct fs*)&buf, (struct fs*)&buf);
    446 
    447 	/* write superblock to original coordinates (use old dev_bsize!) */
    448 	bwrite(sblockloc, buf.data, SBLOCKSIZE, special);
    449 
    450 	if (active) {
    451 		struct ufs_args args;
    452 		args.fspec = sfs.f_mntfromname;
    453 		if (mount(MOUNT_FFS, sfs.f_mntonname, sfs.f_flag | MNT_UPDATE,
    454 		     &args, sizeof args) == -1)
    455 			 warn("mount");
    456 		else
    457 			printf("%s: mount of %s on %s updated\n",
    458 			    getprogname(), sfs.f_mntfromname, sfs.f_mntonname);
    459 	}
    460 
    461 
    462 	/* correct dev_bsize from possibly changed superblock data */
    463 	dev_bsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
    464 
    465 	if (Aflag)
    466 		for (i = 0; i < sblock.fs_ncg; i++)
    467 			bwrite(FFS_FSBTODB(&sblock, cgsblock(&sblock, i)),
    468 			    buf.data, SBLOCKSIZE, special);
    469 	close(fi);
    470 	exit(0);
    471 }
    472 
    473 static int
    474 isactive(int fd, struct statvfs *rsfs)
    475 {
    476 	struct stat st0, st;
    477 	struct statvfs *sfs;
    478 	int n;
    479 
    480 	if (fstat(fd, &st0) == -1) {
    481 		warn("stat");
    482 		return 0;
    483 	}
    484 
    485 	if ((n = getmntinfo(&sfs, 0)) == -1) {
    486 		warn("getmntinfo");
    487 		return 0;
    488 	}
    489 
    490 	for (int i = 0; i < n; i++) {
    491 		if (stat(sfs[i].f_mntfromname, &st) == -1)
    492 			continue;
    493 		if (st.st_rdev != st0.st_rdev)
    494 			continue;
    495 		*rsfs = sfs[i];
    496 		return 1;
    497 
    498 	}
    499 	return 0;
    500 }
    501 
    502 static void
    503 show_log_info(void)
    504 {
    505 	const char *loc;
    506 	uint64_t size, blksize, logsize;
    507 	int print;
    508 
    509 	switch (sblock.fs_journal_location) {
    510 	case UFS_WAPBL_JOURNALLOC_NONE:
    511 		print = blksize = 0;
    512 		/* nothing */
    513 		break;
    514 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    515 		loc = "end of partition";
    516 		size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT];
    517 		blksize = sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
    518 		print = 1;
    519 		break;
    520 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    521 		loc = "in filesystem";
    522 		size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT];
    523 		blksize = sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
    524 		print = 1;
    525 		break;
    526 	default:
    527 		loc = "unknown";
    528 		size = blksize = 0;
    529 		print = 1;
    530 		break;
    531 	}
    532 
    533 	if (print) {
    534 		logsize = size * blksize;
    535 
    536 		printf("\tjournal log file location: %s\n", loc);
    537 		printf("\tjournal log file size: ");
    538 		if (logsize == 0)
    539 			printf("0\n");
    540 		else {
    541 			char sizebuf[8];
    542 			humanize_number(sizebuf, 6, size * blksize, "B",
    543 			    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    544 			printf("%s (%" PRId64 " bytes)", sizebuf, logsize);
    545 		}
    546 		printf("\n");
    547 		printf("\tjournal log flags:");
    548 		if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG)
    549 			printf(" create-log");
    550 		if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG)
    551 			printf(" clear-log");
    552 		printf("\n");
    553 	}
    554 }
    555 
    556 static void
    557 change_log_info(long long logfilesize)
    558 {
    559 	/*
    560 	 * NOTES:
    561 	 *  - only operate on in-filesystem log sizes
    562 	 *  - can't change size of existing log
    563 	 *  - if current is same, no action
    564 	 *  - if current is zero and new is non-zero, set flag to create log
    565 	 *    on next mount
    566 	 *  - if current is non-zero and new is zero, set flag to clear log
    567 	 *    on next mount
    568 	 */
    569 	int in_fs_log;
    570 	uint64_t old_size;
    571 
    572 	old_size = 0;
    573 	switch (sblock.fs_journal_location) {
    574 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    575 		in_fs_log = 0;
    576 		old_size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT] *
    577 		    sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
    578 		break;
    579 
    580 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    581 		in_fs_log = 1;
    582 		old_size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] *
    583 		    sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
    584 		break;
    585 
    586 	case UFS_WAPBL_JOURNALLOC_NONE:
    587 	default:
    588 		in_fs_log = 0;
    589 		old_size = 0;
    590 		break;
    591 	}
    592 
    593 	if (logfilesize == 0) {
    594 		/*
    595 		 * Don't clear out the locators - the kernel might need
    596 		 * these to find the log!  Just set the "clear the log"
    597 		 * flag and let the kernel do the rest.
    598 		 */
    599 		sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CLEAR_LOG;
    600 		sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CREATE_LOG;
    601 		warnx("log file size cleared from %" PRIu64 "", old_size);
    602 		return;
    603 	}
    604 
    605 	if (!in_fs_log && logfilesize > 0 && old_size > 0)
    606 		errx(1, "Can't change size of non-in-filesystem log");
    607 
    608 	if (old_size == (uint64_t)logfilesize && logfilesize > 0) {
    609 		/* no action */
    610 		warnx("log file size remains unchanged at %lld", logfilesize);
    611 		return;
    612 	}
    613 
    614 	if (old_size == 0) {
    615 		/* create new log of desired size next mount */
    616 		sblock.fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
    617 		sblock.fs_journallocs[UFS_WAPBL_INFS_ADDR] = 0;
    618 		sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] = logfilesize;
    619 		sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = 0;
    620 		sblock.fs_journallocs[UFS_WAPBL_INFS_INO] = 0;
    621 		sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CREATE_LOG;
    622 		sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CLEAR_LOG;
    623 		warnx("log file size set to %lld", logfilesize);
    624 	} else {
    625 		errx(1,
    626 		    "Can't change existing log size from %" PRIu64 " to %lld",
    627 		     old_size, logfilesize);
    628 	}
    629 }
    630 
    631 static void
    632 usage(void)
    633 {
    634 
    635 	fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n");
    636 	fprintf(stderr, "where tuneup-options are:\n");
    637 	fprintf(stderr, "\t-a ACLS: `enable' or `disable'\n");
    638 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    639 	fprintf(stderr, "\t-g average file size\n");
    640 	fprintf(stderr, "\t-h expected number of files per directory\n");
    641 	fprintf(stderr, "\t-l journal log file size (`0' to clear journal)\n");
    642 	fprintf(stderr, "\t-m minimum percentage of free space\n");
    643 	fprintf(stderr, "\t-p POSIX.1e ACLS: `enable' or `disable'\n");
    644 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    645 	fprintf(stderr, "\t-q quota type (`[no]user' or `[no]group')\n");
    646 	fprintf(stderr, "\t-S sector size\n");
    647 	exit(2);
    648 }
    649 
    650 static void
    651 getsb(struct fs *fs, const char *file)
    652 {
    653 	int i;
    654 
    655 	for (i = 0; ; i++) {
    656 		if (sblock_try[i] == -1)
    657 			errx(5, "cannot find filesystem superblock");
    658 		bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file);
    659 		switch(fs->fs_magic) {
    660 		case FS_UFS2_MAGIC:
    661 			is_ufs2 = 1;
    662 			/*FALLTHROUGH*/
    663 		case FS_UFS1_MAGIC:
    664 			break;
    665 		case FS_UFS2_MAGIC_SWAPPED:
    666 			is_ufs2 = 1;
    667 			/*FALLTHROUGH*/
    668 		case FS_UFS1_MAGIC_SWAPPED:
    669 			warnx("%s: swapping byte order", file);
    670 			needswap = 1;
    671 			ffs_sb_swap(fs, fs);
    672 			break;
    673 		default:
    674 			continue;
    675 		}
    676 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
    677 			continue;
    678 		if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED)
    679 		    && fs->fs_sblockloc != sblock_try[i])
    680 			continue;
    681 		break;
    682 	}
    683 
    684 	dev_bsize = fs->fs_fsize / FFS_FSBTODB(fs, 1);
    685 	sblockloc = sblock_try[i] / dev_bsize;
    686 }
    687 
    688 static void
    689 bwrite(daddr_t blk, char *buffer, int size, const char *file)
    690 {
    691 	off_t	offset;
    692 
    693 	offset = (off_t)blk * dev_bsize;
    694 	if (lseek(fi, offset, SEEK_SET) == -1)
    695 		err(6, "%s: seeking to %lld", file, (long long)offset);
    696 	if (write(fi, buffer, size) != size)
    697 		err(7, "%s: writing %d bytes", file, size);
    698 }
    699 
    700 static void
    701 bread(daddr_t blk, char *buffer, int cnt, const char *file)
    702 {
    703 	off_t	offset;
    704 	int	i;
    705 
    706 	offset = (off_t)blk * dev_bsize;
    707 	if (lseek(fi, offset, SEEK_SET) == -1)
    708 		err(4, "%s: seeking to %lld", file, (long long)offset);
    709 	if ((i = read(fi, buffer, cnt)) != cnt)
    710 		errx(5, "%s: short read", file);
    711 }
    712 
    713 static int
    714 openpartition(const char *name, int flags, char *device, size_t devicelen)
    715 {
    716 	char		specname[MAXPATHLEN];
    717 	char		rawname[MAXPATHLEN];
    718 	const char	*special, *raw;
    719 	struct fstab	*fs;
    720 	int		fd, oerrno;
    721 
    722 	fs = getfsfile(name);
    723 	special = fs ? fs->fs_spec : name;
    724 
    725 	raw = getfsspecname(specname, sizeof(specname), special);
    726 	if (raw == NULL)
    727 		err(1, "%s: %s", name, specname);
    728 	special = getdiskrawname(rawname, sizeof(rawname), raw);
    729 	if (special == NULL)
    730 		special = raw;
    731 
    732 	fd = opendisk(special, flags, device, devicelen, 0);
    733 	if (fd == -1 && errno == ENOENT) {
    734 		oerrno = errno;
    735 		strlcpy(device, special, devicelen);
    736 		errno = oerrno;
    737 	}
    738 	return (fd);
    739 }
    740