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