tunefs.c revision 1.26
1/*	$NetBSD: tunefs.c,v 1.26 2001/11/09 11:48:39 lukem 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. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
39	The Regents of the University of California.  All rights reserved.\n");
40#endif /* not lint */
41
42#ifndef lint
43#if 0
44static char sccsid[] = "@(#)tunefs.c	8.3 (Berkeley) 5/3/95";
45#else
46__RCSID("$NetBSD: tunefs.c,v 1.26 2001/11/09 11:48:39 lukem Exp $");
47#endif
48#endif /* not lint */
49
50/*
51 * tunefs: change layout parameters to an existing file system.
52 */
53#include <sys/param.h>
54
55#include <ufs/ufs/dinode.h>
56#include <ufs/ffs/fs.h>
57#include <ufs/ffs/ffs_extern.h>
58
59#include <machine/bswap.h>
60
61#include <err.h>
62#include <errno.h>
63#include <fcntl.h>
64#include <fstab.h>
65#include <paths.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <unistd.h>
70#include <util.h>
71
72/* the optimization warning string template */
73#define	OPTWARN	"should optimize for %s with minfree %s %d%%"
74
75union {
76	struct	fs sb;
77	char pad[MAXBSIZE];
78} sbun;
79#define	sblock sbun.sb
80char buf[MAXBSIZE];
81
82int	fi;
83long	dev_bsize = 1;
84int	needswap = 0;
85
86static	void	bwrite(daddr_t, char *, int, const char *);
87static	void	bread(daddr_t, char *, int, const char *);
88static	int	getnum(const char *, const char *, int, int);
89static	void	getsb(struct fs *, const char *);
90static	int	openpartition(const char *, int, char *, size_t);
91static	void	usage(void);
92int		main(int, char *[]);
93
94int
95main(int argc, char *argv[])
96{
97#define	OPTSTRINGBASE	"AFNa:d:e:g:h:k:m:o:t:"
98#ifdef TUNEFS_SOFTDEP
99	int		softdep;
100#define	OPTSTRING	OPTSTRINGBASE ## "n:"
101#else
102#define	OPTSTRING	OPTSTRINGBASE
103#endif
104	int		i, ch, Aflag, Fflag, Nflag, openflags;
105	const char	*special, *chg[2];
106	char		device[MAXPATHLEN];
107	int		maxbpg, maxcontig, minfree, rotdelay, optim, trackskew;
108	int		avgfilesize, avgfpdir;
109
110	Aflag = Fflag = Nflag = 0;
111	maxbpg = maxcontig = minfree = rotdelay = optim = trackskew = -1;
112	avgfilesize = avgfpdir = -1;
113#ifdef TUNEFS_SOFTDEP
114	softdep = -1;
115#endif
116	chg[FS_OPTSPACE] = "space";
117	chg[FS_OPTTIME] = "time";
118
119	while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
120		switch (ch) {
121
122		case 'A':
123			Aflag++;
124			break;
125
126		case 'F':
127			Fflag++;
128			break;
129
130		case 'N':
131			Nflag++;
132			break;
133
134		case 'a':
135			maxcontig = getnum(optarg,
136			    "maximum contiguous block count", 1, INT_MAX);
137			break;
138
139		case 'd':
140			rotdelay = getnum(optarg,
141			    "rotational delay between contiguous blocks",
142			    0, INT_MAX);
143			break;
144
145		case 'e':
146			maxbpg = getnum(optarg,
147			    "maximum blocks per file in a cylinder group",
148			    1, INT_MAX);
149			break;
150
151		case 'g':
152			avgfilesize = getnum(optarg,
153			    "average file size", 1, INT_MAX);
154			break;
155
156		case 'h':
157			avgfpdir = getnum(optarg,
158			    "expected number of files per directory",
159			    1, INT_MAX);
160			break;
161
162		case 'm':
163			minfree = getnum(optarg,
164			    "minimum percentage of free space", 0, 99);
165			break;
166
167#ifdef TUNEFS_SOFTDEP
168		case 'n':
169			if (strcmp(optarg, "enable") == 0)
170				softdep = 1;
171			else if (strcmp(optarg, "disable") == 0)
172				softdep = 0;
173			else {
174				errx(10, "bad soft dependencies "
175					"(options are `enable' or `disable')");
176			}
177			break;
178#endif
179
180		case 'o':
181			if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
182				optim = FS_OPTSPACE;
183			else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
184				optim = FS_OPTTIME;
185			else
186				errx(10,
187				    "bad %s (options are `space' or `time')",
188				    "optimization preference");
189			break;
190
191		case 'k':
192		case 't':	/* for compatibility with old syntax */
193			trackskew = getnum(optarg,
194			    "track skew in sectors", 0, INT_MAX);
195			break;
196
197		default:
198			usage();
199		}
200	}
201	argc -= optind;
202	argv += optind;
203	if (argc != 1)
204		usage();
205
206	special = argv[0];
207	openflags = Nflag ? O_RDONLY : O_RDWR;
208	if (Fflag)
209		fi = open(special, openflags);
210	else {
211		fi = openpartition(special, openflags, device, sizeof(device));
212		special = device;
213	}
214	if (fi == -1)
215		err(1, "%s", special);
216	getsb(&sblock, special);
217
218#define CHANGEVAL(old, new, type, suffix) do				\
219	if ((new) != -1) {						\
220		if ((new) == (old))					\
221			warnx("%s remains unchanged at %d%s",		\
222			    (type), (old), (suffix));			\
223		else {							\
224			warnx("%s changes from %d%s to %d%s",		\
225			    (type), (old), (suffix), (new), (suffix));	\
226			(old) = (new);					\
227		}							\
228	} while (/* CONSTCOND */0)
229
230	warnx("tuning %s", special);
231	CHANGEVAL(sblock.fs_maxcontig, maxcontig,
232	    "maximum contiguous block count", "");
233	CHANGEVAL(sblock.fs_rotdelay, rotdelay,
234	    "rotational delay between contiguous blocks", "ms");
235	CHANGEVAL(sblock.fs_maxbpg, maxbpg,
236	    "maximum blocks per file in a cylinder group", "");
237	CHANGEVAL(sblock.fs_minfree, minfree,
238	    "minimum percentage of free space", "%");
239	if (minfree != -1) {
240		if (minfree >= MINFREE &&
241		    sblock.fs_optim == FS_OPTSPACE)
242			warnx(OPTWARN, "time", ">=", MINFREE);
243		if (minfree < MINFREE &&
244		    sblock.fs_optim == FS_OPTTIME)
245			warnx(OPTWARN, "space", "<", MINFREE);
246	}
247#ifdef TUNEFS_SOFTDEP
248	if (softdep == 1) {
249		sblock.fs_flags |= FS_DOSOFTDEP;
250		warnx("soft dependencies set");
251	} else if (softdep == 0) {
252		sblock.fs_flags &= ~FS_DOSOFTDEP;
253		warnx("soft dependencies cleared");
254	}
255#endif
256	if (optim != -1) {
257		if (sblock.fs_optim == optim) {
258			warnx("%s remains unchanged as %s",
259			    "optimization preference",
260			    chg[optim]);
261		} else {
262			warnx("%s changes from %s to %s",
263			    "optimization preference",
264			    chg[sblock.fs_optim], chg[optim]);
265			sblock.fs_optim = optim;
266			if (sblock.fs_minfree >= MINFREE &&
267			    optim == FS_OPTSPACE)
268				warnx(OPTWARN, "time", ">=", MINFREE);
269			if (sblock.fs_minfree < MINFREE &&
270			    optim == FS_OPTTIME)
271				warnx(OPTWARN, "space", "<", MINFREE);
272		}
273	}
274	CHANGEVAL(sblock.fs_trackskew, trackskew,
275	    "track skew in sectors", "");
276	CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
277	    "average file size", "");
278	CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
279	    "expected number of files per directory", "");
280
281	if (Nflag) {
282		fprintf(stdout, "tunefs: current settings of %s\n", special);
283		fprintf(stdout, "\tmaximum contiguous block count %d\n",
284		    sblock.fs_maxcontig);
285		fprintf(stdout,
286		    "\trotational delay between contiguous blocks %dms\n",
287		    sblock.fs_rotdelay);
288		fprintf(stdout,
289		    "\tmaximum blocks per file in a cylinder group %d\n",
290		    sblock.fs_maxbpg);
291		fprintf(stdout, "\tminimum percentage of free space %d%%\n",
292		    sblock.fs_minfree);
293#ifdef TUNEFS_SOFTDEP
294		fprintf(stdout, "\tsoft dependencies: %s\n",
295		    (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off");
296#endif
297		fprintf(stdout, "\toptimization preference: %s\n",
298		    chg[sblock.fs_optim]);
299		fprintf(stdout, "\taverage file size: %d\n",
300		    sblock.fs_avgfilesize);
301		fprintf(stdout,
302		    "\texpected number of files per directory: %d\n",
303		    sblock.fs_avgfpdir);
304		fprintf(stdout, "\ttrack skew %d sectors\n",
305		    sblock.fs_trackskew);
306		fprintf(stdout, "tunefs: no changes made\n");
307		exit(0);
308	}
309
310	memcpy(buf, (char *)&sblock, SBSIZE);
311	if (needswap)
312		ffs_sb_swap((struct fs*)buf, (struct fs*)buf);
313	bwrite((daddr_t)SBOFF / dev_bsize, buf, SBSIZE, special);
314	if (Aflag)
315		for (i = 0; i < sblock.fs_ncg; i++)
316			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
317			    buf, SBSIZE, special);
318	close(fi);
319	exit(0);
320}
321
322static int
323getnum(const char *num, const char *desc, int min, int max)
324{
325	long	n;
326	char	*ep;
327
328	n = strtol(num, &ep, 10);
329	if (ep[0] != '\0')
330		errx(1, "Invalid number `%s' for %s", num, desc);
331	if ((int) n < min)
332		errx(1, "%s `%s' too small (minimum is %d)", desc, num, min);
333	if ((int) n > max)
334		errx(1, "%s `%s' too large (maximum is %d)", desc, num, max);
335	return ((int)n);
336}
337
338static void
339usage(void)
340{
341
342	fprintf(stderr, "Usage: tunefs [-AFN] tuneup-options special-device\n");
343	fprintf(stderr, "where tuneup-options are:\n");
344	fprintf(stderr, "\t-a maximum contiguous blocks\n");
345	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
346	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
347	fprintf(stderr, "\t-g average file size\n");
348	fprintf(stderr, "\t-h expected number of files per directory\n");
349	fprintf(stderr, "\t-k track skew in sectors\n");
350	fprintf(stderr, "\t-m minimum percentage of free space\n");
351#ifdef TUNEFS_SOFTDEP
352	fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n");
353#endif
354	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
355	exit(2);
356}
357
358static void
359getsb(struct fs *fs, const char *file)
360{
361
362	bread((daddr_t)SBOFF, (char *)fs, SBSIZE, file);
363	if (fs->fs_magic != FS_MAGIC) {
364		if (fs->fs_magic == bswap32(FS_MAGIC)) {
365			warnx("%s: swapping byte order", file);
366			needswap = 1;
367			ffs_sb_swap(fs, fs);
368		} else
369			err(5, "%s: bad magic number", file);
370	}
371	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
372}
373
374static void
375bwrite(daddr_t blk, char *buffer, int size, const char *file)
376{
377	off_t	offset;
378
379	offset = (off_t)blk * dev_bsize;
380	if (lseek(fi, offset, SEEK_SET) == -1)
381		err(6, "%s: seeking to %lld", file, (long long)offset);
382	if (write(fi, buffer, size) != size)
383		err(7, "%s: writing %d bytes", file, size);
384}
385
386static void
387bread(daddr_t blk, char *buffer, int cnt, const char *file)
388{
389	off_t	offset;
390	int	i;
391
392	offset = (off_t)blk * dev_bsize;
393	if (lseek(fi, offset, SEEK_SET) == -1)
394		err(4, "%s: seeking to %lld", file, (long long)offset);
395	if ((i = read(fi, buffer, cnt)) != cnt)
396		errx(5, "%s: short read", file);
397}
398
399static int
400openpartition(const char *name, int flags, char *device, size_t devicelen)
401{
402	char		rawspec[MAXPATHLEN], *p;
403	struct fstab	*fs;
404	int		fd, oerrno;
405
406	fs = getfsfile(name);
407	if (fs) {
408		if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
409			snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
410			    (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
411			name = rawspec;
412		} else
413			name = fs->fs_spec;
414	}
415	fd = opendisk(name, flags, device, devicelen, 0);
416	if (fd == -1 && errno == ENOENT) {
417		oerrno = errno;
418		strlcpy(device, name, devicelen);
419		errno = oerrno;
420	}
421	return (fd);
422}
423