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