Home | History | Annotate | Line # | Download | only in tunefs
tunefs.c revision 1.44
      1 /*	$NetBSD: tunefs.c,v 1.44 2011/08/29 14:35:04 joerg 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.44 2011/08/29 14:35:04 joerg 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 
     51 #include <ufs/ffs/fs.h>
     52 #include <ufs/ffs/ffs_extern.h>
     53 #include <ufs/ufs/ufs_wapbl.h>
     54 #include <ufs/ufs/quota2.h>
     55 
     56 #include <machine/bswap.h>
     57 
     58 #include <err.h>
     59 #include <errno.h>
     60 #include <fcntl.h>
     61 #include <fstab.h>
     62 #include <paths.h>
     63 #include <stdio.h>
     64 #include <stdlib.h>
     65 #include <string.h>
     66 #include <unistd.h>
     67 #include <util.h>
     68 
     69 /* the optimization warning string template */
     70 #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
     71 
     72 union {
     73 	struct	fs sb;
     74 	char pad[MAXBSIZE];
     75 } sbun;
     76 #define	sblock sbun.sb
     77 char buf[MAXBSIZE];
     78 
     79 int	fi;
     80 long	dev_bsize = 512;
     81 int	needswap = 0;
     82 int	is_ufs2 = 0;
     83 off_t	sblockloc;
     84 int	userquota = 0;
     85 int	groupquota = 0;
     86 #define Q2_EN  (1)
     87 #define Q2_IGN (0)
     88 #define Q2_DIS (-1)
     89 
     90 static off_t sblock_try[] = SBLOCKSEARCH;
     91 
     92 static	void	bwrite(daddr_t, char *, int, const char *);
     93 static	void	bread(daddr_t, char *, int, const char *);
     94 static	void	change_log_info(long long);
     95 static	void	getsb(struct fs *, const char *);
     96 static	int	openpartition(const char *, int, char *, size_t);
     97 static	void	show_log_info(void);
     98 __dead static	void	usage(void);
     99 
    100 int
    101 main(int argc, char *argv[])
    102 {
    103 	int		i, ch, Aflag, Fflag, Nflag, openflags;
    104 	const char	*special, *chg[2];
    105 	char		device[MAXPATHLEN];
    106 	int		maxbpg, minfree, optim;
    107 	int		avgfilesize, avgfpdir;
    108 	long long	logfilesize;
    109 
    110 	Aflag = Fflag = Nflag = 0;
    111 	maxbpg = minfree = optim = -1;
    112 	avgfilesize = avgfpdir = -1;
    113 	logfilesize = -1;
    114 	chg[FS_OPTSPACE] = "space";
    115 	chg[FS_OPTTIME] = "time";
    116 
    117 	while ((ch = getopt(argc, argv, "AFNe:g:h:l:m:o:q:")) != -1) {
    118 		switch (ch) {
    119 
    120 		case 'A':
    121 			Aflag++;
    122 			break;
    123 
    124 		case 'F':
    125 			Fflag++;
    126 			break;
    127 
    128 		case 'N':
    129 			Nflag++;
    130 			break;
    131 
    132 		case 'e':
    133 			maxbpg = strsuftoll(
    134 			    "maximum blocks per file in a cylinder group",
    135 			    optarg, 1, INT_MAX);
    136 			break;
    137 
    138 		case 'g':
    139 			avgfilesize = strsuftoll("average file size", optarg,
    140 			    1, INT_MAX);
    141 			break;
    142 
    143 		case 'h':
    144 			avgfpdir = strsuftoll(
    145 			    "expected number of files per directory",
    146 			    optarg, 1, INT_MAX);
    147 			break;
    148 
    149 		case 'l':
    150 			logfilesize = strsuftoll("journal log file size",
    151 			    optarg, 0, INT_MAX);
    152 			break;
    153 
    154 		case 'm':
    155 			minfree = strsuftoll("minimum percentage of free space",
    156 			    optarg, 0, 99);
    157 			break;
    158 
    159 		case 'o':
    160 			if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
    161 				optim = FS_OPTSPACE;
    162 			else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
    163 				optim = FS_OPTTIME;
    164 			else
    165 				errx(10,
    166 				    "bad %s (options are `space' or `time')",
    167 				    "optimization preference");
    168 			break;
    169 		case 'q':
    170 			if      (strcmp(optarg, "user") == 0)
    171 				userquota = Q2_EN;
    172 			else if (strcmp(optarg, "group") == 0)
    173 				groupquota = Q2_EN;
    174 			else if (strcmp(optarg, "nouser") == 0)
    175 				userquota = Q2_DIS;
    176 			else if (strcmp(optarg, "nogroup") == 0)
    177 				groupquota = Q2_DIS;
    178 			else
    179 			    errx(11, "invalid quota type %s", optarg);
    180 			break;
    181 		default:
    182 			usage();
    183 		}
    184 	}
    185 	argc -= optind;
    186 	argv += optind;
    187 	if (argc != 1)
    188 		usage();
    189 
    190 	special = argv[0];
    191 	openflags = Nflag ? O_RDONLY : O_RDWR;
    192 	if (Fflag)
    193 		fi = open(special, openflags);
    194 	else {
    195 		fi = openpartition(special, openflags, device, sizeof(device));
    196 		special = device;
    197 	}
    198 	if (fi == -1)
    199 		err(1, "%s", special);
    200 	getsb(&sblock, special);
    201 
    202 #define CHANGEVAL(old, new, type, suffix) do				\
    203 	if ((new) != -1) {						\
    204 		if ((new) == (old))					\
    205 			warnx("%s remains unchanged at %d%s",		\
    206 			    (type), (old), (suffix));			\
    207 		else {							\
    208 			warnx("%s changes from %d%s to %d%s",		\
    209 			    (type), (old), (suffix), (new), (suffix));	\
    210 			(old) = (new);					\
    211 		}							\
    212 	} while (/* CONSTCOND */0)
    213 
    214 	warnx("tuning %s", special);
    215 	CHANGEVAL(sblock.fs_maxbpg, maxbpg,
    216 	    "maximum blocks per file in a cylinder group", "");
    217 	CHANGEVAL(sblock.fs_minfree, minfree,
    218 	    "minimum percentage of free space", "%");
    219 	if (minfree != -1) {
    220 		if (minfree >= MINFREE &&
    221 		    sblock.fs_optim == FS_OPTSPACE)
    222 			warnx(OPTWARN, "time", ">=", MINFREE);
    223 		if (minfree < MINFREE &&
    224 		    sblock.fs_optim == FS_OPTTIME)
    225 			warnx(OPTWARN, "space", "<", MINFREE);
    226 	}
    227 	if (optim != -1) {
    228 		if (sblock.fs_optim == optim) {
    229 			warnx("%s remains unchanged as %s",
    230 			    "optimization preference",
    231 			    chg[optim]);
    232 		} else {
    233 			warnx("%s changes from %s to %s",
    234 			    "optimization preference",
    235 			    chg[sblock.fs_optim], chg[optim]);
    236 			sblock.fs_optim = optim;
    237 			if (sblock.fs_minfree >= MINFREE &&
    238 			    optim == FS_OPTSPACE)
    239 				warnx(OPTWARN, "time", ">=", MINFREE);
    240 			if (sblock.fs_minfree < MINFREE &&
    241 			    optim == FS_OPTTIME)
    242 				warnx(OPTWARN, "space", "<", MINFREE);
    243 		}
    244 	}
    245 	CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
    246 	    "average file size", "");
    247 	CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
    248 	    "expected number of files per directory", "");
    249 
    250 	if (logfilesize >= 0)
    251 		change_log_info(logfilesize);
    252 	if (userquota == Q2_EN || groupquota == Q2_EN)
    253 		sblock.fs_flags |= FS_DOQUOTA2;
    254 	if (sblock.fs_flags & FS_DOQUOTA2) {
    255 		sblock.fs_quota_magic = Q2_HEAD_MAGIC;
    256 		switch(userquota) {
    257 		case Q2_EN:
    258 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
    259 			    == 0) {
    260 				printf("enabling user quotas\n");
    261 				sblock.fs_quota_flags |=
    262 				    FS_Q2_DO_TYPE(USRQUOTA);
    263 				sblock.fs_quotafile[USRQUOTA] = 0;
    264 			}
    265 			break;
    266 		case Q2_DIS:
    267 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
    268 			    != 0) {
    269 				printf("disabling user quotas\n");
    270 				sblock.fs_quota_flags &=
    271 				    ~FS_Q2_DO_TYPE(USRQUOTA);
    272 			}
    273 		}
    274 		switch(groupquota) {
    275 		case Q2_EN:
    276 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
    277 			    == 0) {
    278 				printf("enabling group quotas\n");
    279 				sblock.fs_quota_flags |=
    280 				    FS_Q2_DO_TYPE(GRPQUOTA);
    281 				sblock.fs_quotafile[GRPQUOTA] = 0;
    282 			}
    283 			break;
    284 		case Q2_DIS:
    285 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
    286 			    != 0) {
    287 				printf("disabling group quotas\n");
    288 				sblock.fs_quota_flags &=
    289 				    ~FS_Q2_DO_TYPE(GRPQUOTA);
    290 			}
    291 		}
    292 	}
    293 	/*
    294 	 * if we disabled all quotas, FS_DOQUOTA2 and associated inode(s) will
    295 	 * be cleared by kernel or fsck.
    296 	 */
    297 
    298 	if (Nflag) {
    299 		printf("tunefs: current settings of %s\n", special);
    300 		printf("\tmaximum contiguous block count %d\n",
    301 		    sblock.fs_maxcontig);
    302 		printf("\tmaximum blocks per file in a cylinder group %d\n",
    303 		    sblock.fs_maxbpg);
    304 		printf("\tminimum percentage of free space %d%%\n",
    305 		    sblock.fs_minfree);
    306 		printf("\toptimization preference: %s\n", chg[sblock.fs_optim]);
    307 		printf("\taverage file size: %d\n", sblock.fs_avgfilesize);
    308 		printf("\texpected number of files per directory: %d\n",
    309 		    sblock.fs_avgfpdir);
    310 		show_log_info();
    311 		printf("\tquotas");
    312 		if (sblock.fs_flags & FS_DOQUOTA2) {
    313 			if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) {
    314 				printf(" user");
    315 				if (sblock.fs_quota_flags &
    316 				    FS_Q2_DO_TYPE(GRPQUOTA))
    317 					printf(",");
    318 			}
    319 			if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
    320 				printf(" group");
    321 			printf(" enabled\n");
    322 		} else {
    323 			printf("disabled\n");
    324 		}
    325 		printf("tunefs: no changes made\n");
    326 		exit(0);
    327 	}
    328 
    329 	memcpy(buf, (char *)&sblock, SBLOCKSIZE);
    330 	if (needswap)
    331 		ffs_sb_swap((struct fs*)buf, (struct fs*)buf);
    332 	bwrite(sblockloc, buf, SBLOCKSIZE, special);
    333 	if (Aflag)
    334 		for (i = 0; i < sblock.fs_ncg; i++)
    335 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
    336 			    buf, SBLOCKSIZE, special);
    337 	close(fi);
    338 	exit(0);
    339 }
    340 
    341 static void
    342 show_log_info(void)
    343 {
    344 	const char *loc;
    345 	uint64_t size, blksize, logsize;
    346 	int print;
    347 
    348 	switch (sblock.fs_journal_location) {
    349 	case UFS_WAPBL_JOURNALLOC_NONE:
    350 		print = blksize = 0;
    351 		/* nothing */
    352 		break;
    353 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    354 		loc = "end of partition";
    355 		size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT];
    356 		blksize = sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
    357 		print = 1;
    358 		break;
    359 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    360 		loc = "in filesystem";
    361 		size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT];
    362 		blksize = sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
    363 		print = 1;
    364 		break;
    365 	default:
    366 		loc = "unknown";
    367 		size = blksize = 0;
    368 		print = 1;
    369 		break;
    370 	}
    371 
    372 	if (print) {
    373 		logsize = size * blksize;
    374 
    375 		printf("\tjournal log file location: %s\n", loc);
    376 		printf("\tjournal log file size: ");
    377 		if (logsize == 0)
    378 			printf("0\n");
    379 		else {
    380 			char sizebuf[8];
    381 			humanize_number(sizebuf, 6, size * blksize, "B",
    382 			    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    383 			printf("%s (%" PRId64 " bytes)", sizebuf, logsize);
    384 		}
    385 		printf("\n");
    386 		printf("\tjournal log flags:");
    387 		if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG)
    388 			printf(" create-log");
    389 		if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG)
    390 			printf(" clear-log");
    391 		printf("\n");
    392 	}
    393 }
    394 
    395 static void
    396 change_log_info(long long logfilesize)
    397 {
    398 	/*
    399 	 * NOTES:
    400 	 *  - only operate on in-filesystem log sizes
    401 	 *  - can't change size of existing log
    402 	 *  - if current is same, no action
    403 	 *  - if current is zero and new is non-zero, set flag to create log
    404 	 *    on next mount
    405 	 *  - if current is non-zero and new is zero, set flag to clear log
    406 	 *    on next mount
    407 	 */
    408 	int in_fs_log;
    409 	uint64_t old_size;
    410 
    411 	old_size = 0;
    412 	switch (sblock.fs_journal_location) {
    413 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
    414 		in_fs_log = 0;
    415 		old_size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT] *
    416 		    sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
    417 		break;
    418 
    419 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
    420 		in_fs_log = 1;
    421 		old_size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] *
    422 		    sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
    423 		break;
    424 
    425 	case UFS_WAPBL_JOURNALLOC_NONE:
    426 	default:
    427 		in_fs_log = 0;
    428 		old_size = 0;
    429 		break;
    430 	}
    431 
    432 	if (logfilesize == 0) {
    433 		/*
    434 		 * Don't clear out the locators - the kernel might need
    435 		 * these to find the log!  Just set the "clear the log"
    436 		 * flag and let the kernel do the rest.
    437 		 */
    438 		sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CLEAR_LOG;
    439 		sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CREATE_LOG;
    440 		warnx("log file size cleared from %" PRIu64 "", old_size);
    441 		return;
    442 	}
    443 
    444 	if (!in_fs_log && logfilesize > 0 && old_size > 0)
    445 		errx(1, "Can't change size of non-in-filesystem log");
    446 
    447 	if (old_size == (uint64_t)logfilesize && logfilesize > 0) {
    448 		/* no action */
    449 		warnx("log file size remains unchanged at %lld", logfilesize);
    450 		return;
    451 	}
    452 
    453 	if (old_size == 0) {
    454 		/* create new log of desired size next mount */
    455 		sblock.fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
    456 		sblock.fs_journallocs[UFS_WAPBL_INFS_ADDR] = 0;
    457 		sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] = logfilesize;
    458 		sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = 0;
    459 		sblock.fs_journallocs[UFS_WAPBL_INFS_INO] = 0;
    460 		sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CREATE_LOG;
    461 		sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CLEAR_LOG;
    462 		warnx("log file size set to %lld", logfilesize);
    463 	} else {
    464 		errx(1,
    465 		    "Can't change existing log size from %" PRIu64 " to %lld",
    466 		     old_size, logfilesize);
    467 	}
    468 }
    469 
    470 static void
    471 usage(void)
    472 {
    473 
    474 	fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n");
    475 	fprintf(stderr, "where tuneup-options are:\n");
    476 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    477 	fprintf(stderr, "\t-g average file size\n");
    478 	fprintf(stderr, "\t-h expected number of files per directory\n");
    479 	fprintf(stderr, "\t-l journal log file size (`0' to clear journal)\n");
    480 	fprintf(stderr, "\t-m minimum percentage of free space\n");
    481 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    482 	fprintf(stderr, "\t-q quota type (`[no]user' or `[no]group')\n");
    483 	exit(2);
    484 }
    485 
    486 static void
    487 getsb(struct fs *fs, const char *file)
    488 {
    489 	int i;
    490 
    491 	for (i = 0; ; i++) {
    492 		if (sblock_try[i] == -1)
    493 			errx(5, "cannot find filesystem superblock");
    494 		bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file);
    495 		switch(fs->fs_magic) {
    496 		case FS_UFS2_MAGIC:
    497 			is_ufs2 = 1;
    498 			/*FALLTHROUGH*/
    499 		case FS_UFS1_MAGIC:
    500 			break;
    501 		case FS_UFS2_MAGIC_SWAPPED:
    502 			is_ufs2 = 1;
    503 			/*FALLTHROUGH*/
    504 		case FS_UFS1_MAGIC_SWAPPED:
    505 			warnx("%s: swapping byte order", file);
    506 			needswap = 1;
    507 			ffs_sb_swap(fs, fs);
    508 			break;
    509 		default:
    510 			continue;
    511 		}
    512 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
    513 			continue;
    514 		if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED)
    515 		    && fs->fs_sblockloc != sblock_try[i])
    516 			continue;
    517 		break;
    518 	}
    519 
    520 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    521 	sblockloc = sblock_try[i] / dev_bsize;
    522 }
    523 
    524 static void
    525 bwrite(daddr_t blk, char *buffer, int size, const char *file)
    526 {
    527 	off_t	offset;
    528 
    529 	offset = (off_t)blk * dev_bsize;
    530 	if (lseek(fi, offset, SEEK_SET) == -1)
    531 		err(6, "%s: seeking to %lld", file, (long long)offset);
    532 	if (write(fi, buffer, size) != size)
    533 		err(7, "%s: writing %d bytes", file, size);
    534 }
    535 
    536 static void
    537 bread(daddr_t blk, char *buffer, int cnt, const char *file)
    538 {
    539 	off_t	offset;
    540 	int	i;
    541 
    542 	offset = (off_t)blk * dev_bsize;
    543 	if (lseek(fi, offset, SEEK_SET) == -1)
    544 		err(4, "%s: seeking to %lld", file, (long long)offset);
    545 	if ((i = read(fi, buffer, cnt)) != cnt)
    546 		errx(5, "%s: short read", file);
    547 }
    548 
    549 static int
    550 openpartition(const char *name, int flags, char *device, size_t devicelen)
    551 {
    552 	char		rawspec[MAXPATHLEN], *p;
    553 	struct fstab	*fs;
    554 	int		fd, oerrno;
    555 
    556 	fs = getfsfile(name);
    557 	if (fs) {
    558 		if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
    559 			snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
    560 			    (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
    561 			name = rawspec;
    562 		} else
    563 			name = fs->fs_spec;
    564 	}
    565 	fd = opendisk(name, flags, device, devicelen, 0);
    566 	if (fd == -1 && errno == ENOENT) {
    567 		oerrno = errno;
    568 		strlcpy(device, name, devicelen);
    569 		errno = oerrno;
    570 	}
    571 	return (fd);
    572 }
    573