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