xref: /netbsd-src/sbin/tunefs/tunefs.c (revision f298a94b738ab130659689d3f04c17f6991b6de5)
1 /*	$NetBSD: tunefs.c,v 1.58 2023/01/07 19:41:30 chs 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.58 2023/01/07 19:41:30 chs 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 #include <sys/statvfs.h>
51 
52 #include <ufs/ffs/fs.h>
53 #include <ufs/ffs/ffs_extern.h>
54 #include <ufs/ufs/ufs_wapbl.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/quota2.h>
57 
58 #include <machine/bswap.h>
59 
60 #include <err.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <fstab.h>
64 #include <paths.h>
65 #include <stdbool.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 
75 union {
76 	struct	fs sb;
77 	char data[MAXBSIZE];
78 } sbun, buf;
79 #define	sblock sbun.sb
80 
81 int	fi;
82 long	dev_bsize = 512;
83 int	needswap = 0;
84 int	is_ufs2 = 0;
85 int	extattr = 0;
86 off_t	sblockloc;
87 int	userquota = 0;
88 int	groupquota = 0;
89 #define Q2_EN  (1)
90 #define Q2_IGN (0)
91 #define Q2_DIS (-1)
92 
93 static off_t sblock_try[] = SBLOCKSEARCH;
94 
95 static	void	bwrite(daddr_t, char *, int, const char *);
96 static	void	bread(daddr_t, char *, int, const char *);
97 static	void	change_log_info(long long);
98 static	void	getsb(struct fs *, const char *);
99 static	int	openpartition(const char *, int, char *, size_t);
100 static	int	isactive(int, struct statvfs *);
101 static	void	show_log_info(void);
102 __dead static	void	usage(void);
103 
104 int
main(int argc,char * argv[])105 main(int argc, char *argv[])
106 {
107 	int		ch, aflag, pflag, Aflag, Fflag, Nflag, openflags;
108 	const char	*special, *chg[2];
109 	char		device[MAXPATHLEN];
110 	int		maxbpg, minfree, optim, secsize;
111 	uint32_t	i, avgfilesize, avgfpdir;
112 	bool		active;
113 	long long	logfilesize;
114 	int		secshift, fsbtodb;
115 	const char  	*avalue, *pvalue, *name;
116 	struct statvfs	sfs;
117 
118 	aflag = pflag = Aflag = Fflag = Nflag = 0;
119 	avalue = pvalue = NULL;
120 	maxbpg = minfree = optim = secsize = -1;
121 	avgfilesize = avgfpdir = -1;
122 	logfilesize = -1;
123 	secshift = 0;
124 	chg[FS_OPTSPACE] = "space";
125 	chg[FS_OPTTIME] = "time";
126 
127 	while ((ch = getopt(argc, argv, "AFNa:e:g:h:l:m:o:p:q:S:")) != -1) {
128 		switch (ch) {
129 
130 		case 'A':
131 			Aflag++;
132 			break;
133 
134 		case 'F':
135 			Fflag++;
136 			break;
137 
138 		case 'N':
139 			Nflag++;
140 			break;
141 
142 		case 'a':
143 			name = "ACLs";
144 			avalue = optarg;
145 			if (strcmp(avalue, "enable") &&
146 			    strcmp(avalue, "disable")) {
147 				errx(10, "bad %s (options are %s)",
148 				    name, "`enable' or `disable'");
149 			}
150 			aflag = 1;
151  			break;
152 
153 		case 'e':
154 			maxbpg = strsuftoll(
155 			    "maximum blocks per file in a cylinder group",
156 			    optarg, 1, INT_MAX);
157 			break;
158 
159 		case 'g':
160 			avgfilesize = strsuftoll("average file size", optarg,
161 			    1, INT_MAX);
162 			break;
163 
164 		case 'h':
165 			avgfpdir = strsuftoll(
166 			    "expected number of files per directory",
167 			    optarg, 1, INT_MAX);
168 			break;
169 
170 		case 'l':
171 			logfilesize = strsuftoll("journal log file size",
172 			    optarg, 0, INT_MAX);
173 			break;
174 
175 		case 'm':
176 			minfree = strsuftoll("minimum percentage of free space",
177 			    optarg, 0, 99);
178 			break;
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 'p':
192 			name = "POSIX1e ACLs";
193 			pvalue = optarg;
194 			if (strcmp(pvalue, "enable") &&
195 			    strcmp(pvalue, "disable")) {
196 				errx(10, "bad %s (options are %s)",
197 				    name, "`enable' or `disable'");
198 			}
199 			pflag = 1;
200 			break;
201 
202 		case 'q':
203 			if      (strcmp(optarg, "user") == 0)
204 				userquota = Q2_EN;
205 			else if (strcmp(optarg, "group") == 0)
206 				groupquota = Q2_EN;
207 			else if (strcmp(optarg, "nouser") == 0)
208 				userquota = Q2_DIS;
209 			else if (strcmp(optarg, "nogroup") == 0)
210 				groupquota = Q2_DIS;
211 			else
212 			    errx(11, "invalid quota type %s", optarg);
213 			break;
214 
215 		case 'S':
216 			secsize = strsuftoll("physical sector size",
217 			    optarg, 0, INT_MAX);
218 			secshift = ffs(secsize) - 1;
219 			if (secsize != 0 && 1 << secshift != secsize)
220 				errx(12, "sector size %d is not a power of two", secsize);
221 			break;
222 
223 		default:
224 			usage();
225 		}
226 	}
227 	argc -= optind;
228 	argv += optind;
229 	if (argc != 1)
230 		usage();
231 
232 	special = argv[0];
233 	openflags = Nflag ? O_RDONLY : O_RDWR;
234 	if (Fflag)
235 		fi = open(special, openflags);
236 	else {
237 		fi = openpartition(special, openflags, device, sizeof(device));
238 		special = device;
239 	}
240 	if (fi == -1)
241 		err(1, "%s", special);
242 	active = !Fflag && isactive(fi, &sfs);
243 	getsb(&sblock, special);
244 
245 #define CHANGEVAL(old, new, type, suffix) do				\
246 	if ((uint32_t)(new) != (uint32_t)-1) {				\
247 		if ((new) == (old))					\
248 			warnx("%s remains unchanged at %d%s",		\
249 			    (type), (old), (suffix));			\
250 		else {							\
251 			warnx("%s changes from %d%s to %d%s",		\
252 			    (type), (old), (suffix), (new), (suffix));	\
253 			(old) = (new);					\
254 		}							\
255 	} while (/* CONSTCOND */0)
256 
257 	warnx("tuning %s", special);
258 	CHANGEVAL(sblock.fs_maxbpg, maxbpg,
259 	    "maximum blocks per file in a cylinder group", "");
260 	CHANGEVAL(sblock.fs_minfree, minfree,
261 	    "minimum percentage of free space", "%");
262 	if (minfree != -1) {
263 		if (minfree >= MINFREE &&
264 		    sblock.fs_optim == FS_OPTSPACE)
265 			warnx(OPTWARN, "time", ">=", MINFREE);
266 		if (minfree < MINFREE &&
267 		    sblock.fs_optim == FS_OPTTIME)
268 			warnx(OPTWARN, "space", "<", MINFREE);
269 	}
270 	if (optim != -1) {
271 		if (sblock.fs_optim == optim) {
272 			warnx("%s remains unchanged as %s",
273 			    "optimization preference",
274 			    chg[optim]);
275 		} else {
276 			warnx("%s changes from %s to %s",
277 			    "optimization preference",
278 			    chg[sblock.fs_optim], chg[optim]);
279 			sblock.fs_optim = optim;
280 			if (sblock.fs_minfree >= MINFREE &&
281 			    optim == FS_OPTSPACE)
282 				warnx(OPTWARN, "time", ">=", MINFREE);
283 			if (sblock.fs_minfree < MINFREE &&
284 			    optim == FS_OPTTIME)
285 				warnx(OPTWARN, "space", "<", MINFREE);
286 		}
287 	}
288 	if (secsize != -1) {
289 		if (secsize == 0) {
290 			secsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
291 			secshift = ffs(secsize) - 1;
292 		}
293 
294 		if (secshift < DEV_BSHIFT)
295 			warnx("sector size must be at least %d", DEV_BSIZE);
296 		else if (secshift > sblock.fs_fshift)
297 			warnx("sector size %d cannot be larger than fragment size %d",
298 				secsize, sblock.fs_fsize);
299 		else {
300 			fsbtodb = sblock.fs_fshift - secshift;
301 			if (fsbtodb == sblock.fs_fsbtodb) {
302 				warnx("sector size remains unchanged as %d",
303 					sblock.fs_fsize / FFS_FSBTODB(&sblock, 1));
304 			} else {
305 				warnx("sector size changed from %d to %d",
306 					sblock.fs_fsize / FFS_FSBTODB(&sblock, 1),
307 					secsize);
308 				sblock.fs_fsbtodb = fsbtodb;
309 			}
310 		}
311 	}
312 	CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
313 	    "average file size", "");
314 	CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
315 	    "expected number of files per directory", "");
316 
317 	if (logfilesize >= 0)
318 		change_log_info(logfilesize);
319 	if (userquota == Q2_EN || groupquota == Q2_EN)
320 		sblock.fs_flags |= FS_DOQUOTA2;
321 	if (sblock.fs_flags & FS_DOQUOTA2) {
322 		sblock.fs_quota_magic = Q2_HEAD_MAGIC;
323 		switch(userquota) {
324 		case Q2_EN:
325 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
326 			    == 0) {
327 				printf("enabling user quotas\n");
328 				sblock.fs_quota_flags |=
329 				    FS_Q2_DO_TYPE(USRQUOTA);
330 				sblock.fs_quotafile[USRQUOTA] = 0;
331 			}
332 			break;
333 		case Q2_DIS:
334 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
335 			    != 0) {
336 				printf("disabling user quotas\n");
337 				sblock.fs_quota_flags &=
338 				    ~FS_Q2_DO_TYPE(USRQUOTA);
339 			}
340 		}
341 		switch(groupquota) {
342 		case Q2_EN:
343 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
344 			    == 0) {
345 				printf("enabling group quotas\n");
346 				sblock.fs_quota_flags |=
347 				    FS_Q2_DO_TYPE(GRPQUOTA);
348 				sblock.fs_quotafile[GRPQUOTA] = 0;
349 			}
350 			break;
351 		case Q2_DIS:
352 			if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
353 			    != 0) {
354 				printf("disabling group quotas\n");
355 				sblock.fs_quota_flags &=
356 				    ~FS_Q2_DO_TYPE(GRPQUOTA);
357 			}
358 		}
359 	}
360 	/*
361 	 * if we disabled all quotas, FS_DOQUOTA2 and associated inode(s) will
362 	 * be cleared by kernel or fsck.
363 	 */
364 	if (aflag) {
365 		name = "NFSv4 ACLs";
366 		if (strcmp(avalue, "enable") == 0) {
367 			if (is_ufs2 && !extattr) {
368 				warnx("%s not supported by this fs", name);
369 			} else if (sblock.fs_flags & FS_NFS4ACLS) {
370 				warnx("%s remains unchanged as enabled", name);
371 			} else if (sblock.fs_flags & FS_POSIX1EACLS) {
372 				warnx("%s and POSIX.1e ACLs are mutually "
373 				    "exclusive", name);
374 			} else {
375 				sblock.fs_flags |= FS_NFS4ACLS;
376 				printf("%s set\n", name);
377 			}
378 		} else if (strcmp(avalue, "disable") == 0) {
379 			if ((~sblock.fs_flags & FS_NFS4ACLS) == FS_NFS4ACLS) {
380 				warnx("%s remains unchanged as disabled",
381 				    name);
382 			} else {
383 				sblock.fs_flags &= ~FS_NFS4ACLS;
384 				printf("%s cleared\n", name);
385 			}
386  		}
387  	}
388 
389 	if (pflag) {
390 		name = "POSIX1e ACLs";
391 		if (strcmp(pvalue, "enable") == 0) {
392 			if (is_ufs2 && !extattr) {
393 				warnx("%s not supported by this fs", name);
394 			} else if (sblock.fs_flags & FS_POSIX1EACLS) {
395 				warnx("%s remains unchanged as enabled", name);
396 			} else if (sblock.fs_flags & FS_NFS4ACLS) {
397 				warnx("%s and ACLs are mutually "
398 				    "exclusive", name);
399 			} else {
400 				sblock.fs_flags |= FS_POSIX1EACLS;
401 				printf("%s set\n", name);
402 			}
403 		} else if (strcmp(pvalue, "disable") == 0) {
404 			if ((~sblock.fs_flags & FS_POSIX1EACLS) ==
405 			    FS_POSIX1EACLS) {
406 				warnx("%s remains unchanged as disabled",
407 				    name);
408 			} else {
409 				sblock.fs_flags &= ~FS_POSIX1EACLS;
410 				printf("%s cleared\n", name);
411 			}
412 		}
413 	}
414 
415 	if (Nflag) {
416 		printf("%s: current settings of %s\n", getprogname(), special);
417 		printf("\tmaximum contiguous block count %d\n",
418 		    sblock.fs_maxcontig);
419 		printf("\tmaximum blocks per file in a cylinder group %d\n",
420 		    sblock.fs_maxbpg);
421 		printf("\tminimum percentage of free space %d%%\n",
422 		    sblock.fs_minfree);
423 		printf("\toptimization preference: %s\n", chg[sblock.fs_optim]);
424 		printf("\taverage file size: %d\n", sblock.fs_avgfilesize);
425 		printf("\texpected number of files per directory: %d\n",
426 		    sblock.fs_avgfpdir);
427 		show_log_info();
428 		printf("\tquotas");
429 		if (sblock.fs_flags & FS_DOQUOTA2) {
430 			if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) {
431 				printf(" user");
432 				if (sblock.fs_quota_flags &
433 				    FS_Q2_DO_TYPE(GRPQUOTA))
434 					printf(",");
435 			}
436 			if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
437 				printf(" group");
438 			printf(" enabled\n");
439 		} else {
440 			printf(" disabled\n");
441 		}
442 		printf("\tPOSIX.1e ACLs %s\n",
443 		    (sblock.fs_flags & FS_POSIX1EACLS) ? "enabled" : "disabled");
444 		printf("\tNFS4 ACLs %s\n",
445 		    (sblock.fs_flags & FS_NFS4ACLS) ? "enabled" : "disabled");
446 		printf("%s: no changes made\n", getprogname());
447 		return 0;
448 	}
449 
450 	memcpy(&buf, (char *)&sblock, SBLOCKSIZE);
451 	if (needswap)
452 		ffs_sb_swap((struct fs*)&buf, (struct fs*)&buf);
453 
454 	/* write superblock to original coordinates (use old dev_bsize!) */
455 	bwrite(sblockloc, buf.data, SBLOCKSIZE, special);
456 
457 	if (active) {
458 		struct ufs_args args;
459 		args.fspec = sfs.f_mntfromname;
460 		if (mount(MOUNT_FFS, sfs.f_mntonname, sfs.f_flag | MNT_UPDATE,
461 		     &args, sizeof args) == -1)
462 			 warn("mount");
463 		else
464 			printf("%s: mount of %s on %s updated\n",
465 			    getprogname(), sfs.f_mntfromname, sfs.f_mntonname);
466 	}
467 
468 
469 	/* correct dev_bsize from possibly changed superblock data */
470 	dev_bsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
471 
472 	if (Aflag)
473 		for (i = 0; i < sblock.fs_ncg; i++)
474 			bwrite(FFS_FSBTODB(&sblock, cgsblock(&sblock, i)),
475 			    buf.data, SBLOCKSIZE, special);
476 	close(fi);
477 	exit(0);
478 }
479 
480 static int
isactive(int fd,struct statvfs * rsfs)481 isactive(int fd, struct statvfs *rsfs)
482 {
483 	struct stat st0, st;
484 	struct statvfs *sfs;
485 	int n;
486 
487 	if (fstat(fd, &st0) == -1) {
488 		warn("stat");
489 		return 0;
490 	}
491 
492 	if ((n = getmntinfo(&sfs, 0)) == -1) {
493 		warn("getmntinfo");
494 		return 0;
495 	}
496 
497 	for (int i = 0; i < n; i++) {
498 		if (stat(sfs[i].f_mntfromname, &st) == -1)
499 			continue;
500 		if (st.st_rdev != st0.st_rdev)
501 			continue;
502 		*rsfs = sfs[i];
503 		return 1;
504 
505 	}
506 	return 0;
507 }
508 
509 static void
show_log_info(void)510 show_log_info(void)
511 {
512 	const char *loc;
513 	uint64_t size, blksize, logsize;
514 	int print;
515 
516 	switch (sblock.fs_journal_location) {
517 	case UFS_WAPBL_JOURNALLOC_NONE:
518 		print = blksize = 0;
519 		/* nothing */
520 		break;
521 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
522 		loc = "end of partition";
523 		size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT];
524 		blksize = sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
525 		print = 1;
526 		break;
527 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
528 		loc = "in filesystem";
529 		size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT];
530 		blksize = sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
531 		print = 1;
532 		break;
533 	default:
534 		loc = "unknown";
535 		size = blksize = 0;
536 		print = 1;
537 		break;
538 	}
539 
540 	if (print) {
541 		logsize = size * blksize;
542 
543 		printf("\tjournal log file location: %s\n", loc);
544 		printf("\tjournal log file size: ");
545 		if (logsize == 0)
546 			printf("0\n");
547 		else {
548 			char sizebuf[8];
549 			humanize_number(sizebuf, 6, size * blksize, "B",
550 			    HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
551 			printf("%s (%" PRId64 " bytes)", sizebuf, logsize);
552 		}
553 		printf("\n");
554 		printf("\tjournal log flags:");
555 		if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG)
556 			printf(" create-log");
557 		if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG)
558 			printf(" clear-log");
559 		printf("\n");
560 	}
561 }
562 
563 static void
change_log_info(long long logfilesize)564 change_log_info(long long logfilesize)
565 {
566 	/*
567 	 * NOTES:
568 	 *  - only operate on in-filesystem log sizes
569 	 *  - can't change size of existing log
570 	 *  - if current is same, no action
571 	 *  - if current is zero and new is non-zero, set flag to create log
572 	 *    on next mount
573 	 *  - if current is non-zero and new is zero, set flag to clear log
574 	 *    on next mount
575 	 */
576 	int in_fs_log;
577 	uint64_t old_size;
578 
579 	old_size = 0;
580 	switch (sblock.fs_journal_location) {
581 	case UFS_WAPBL_JOURNALLOC_END_PARTITION:
582 		in_fs_log = 0;
583 		old_size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT] *
584 		    sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
585 		break;
586 
587 	case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
588 		in_fs_log = 1;
589 		old_size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] *
590 		    sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
591 		break;
592 
593 	case UFS_WAPBL_JOURNALLOC_NONE:
594 	default:
595 		in_fs_log = 0;
596 		old_size = 0;
597 		break;
598 	}
599 
600 	if (logfilesize == 0) {
601 		/*
602 		 * Don't clear out the locators - the kernel might need
603 		 * these to find the log!  Just set the "clear the log"
604 		 * flag and let the kernel do the rest.
605 		 */
606 		sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CLEAR_LOG;
607 		sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CREATE_LOG;
608 		warnx("log file size cleared from %" PRIu64 "", old_size);
609 		return;
610 	}
611 
612 	if (!in_fs_log && logfilesize > 0 && old_size > 0)
613 		errx(1, "Can't change size of non-in-filesystem log");
614 
615 	if (old_size == (uint64_t)logfilesize && logfilesize > 0) {
616 		/* no action */
617 		warnx("log file size remains unchanged at %lld", logfilesize);
618 		return;
619 	}
620 
621 	if (old_size == 0) {
622 		/* create new log of desired size next mount */
623 		sblock.fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
624 		sblock.fs_journallocs[UFS_WAPBL_INFS_ADDR] = 0;
625 		sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] = logfilesize;
626 		sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = 0;
627 		sblock.fs_journallocs[UFS_WAPBL_INFS_INO] = 0;
628 		sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CREATE_LOG;
629 		sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CLEAR_LOG;
630 		warnx("log file size set to %lld", logfilesize);
631 	} else {
632 		errx(1,
633 		    "Can't change existing log size from %" PRIu64 " to %lld",
634 		     old_size, logfilesize);
635 	}
636 }
637 
638 static void
usage(void)639 usage(void)
640 {
641 
642 	fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n");
643 	fprintf(stderr, "where tuneup-options are:\n");
644 	fprintf(stderr, "\t-a ACLS: `enable' or `disable'\n");
645 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
646 	fprintf(stderr, "\t-g average file size\n");
647 	fprintf(stderr, "\t-h expected number of files per directory\n");
648 	fprintf(stderr, "\t-l journal log file size (`0' to clear journal)\n");
649 	fprintf(stderr, "\t-m minimum percentage of free space\n");
650 	fprintf(stderr, "\t-p POSIX.1e ACLS: `enable' or `disable'\n");
651 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
652 	fprintf(stderr, "\t-q quota type (`[no]user' or `[no]group')\n");
653 	fprintf(stderr, "\t-S sector size\n");
654 	exit(2);
655 }
656 
657 static void
getsb(struct fs * fs,const char * file)658 getsb(struct fs *fs, const char *file)
659 {
660 	int i;
661 
662 	for (i = 0; ; i++) {
663 		if (sblock_try[i] == -1)
664 			errx(5, "cannot find filesystem superblock");
665 		bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file);
666 		switch(fs->fs_magic) {
667 		case FS_UFS2EA_MAGIC:
668 			extattr = 1;
669 			/*FALLTHROUGH*/
670 		case FS_UFS2_MAGIC:
671 			is_ufs2 = 1;
672 			/*FALLTHROUGH*/
673 		case FS_UFS1_MAGIC:
674 			break;
675 		case FS_UFS2EA_MAGIC_SWAPPED:
676 			extattr = 1;
677 			/*FALLTHROUGH*/
678 		case FS_UFS2_MAGIC_SWAPPED:
679 			is_ufs2 = 1;
680 			/*FALLTHROUGH*/
681 		case FS_UFS1_MAGIC_SWAPPED:
682 			warnx("%s: swapping byte order", file);
683 			needswap = 1;
684 			ffs_sb_swap(fs, fs);
685 			break;
686 		default:
687 			continue;
688 		}
689 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
690 			continue;
691 		if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED)
692 		    && fs->fs_sblockloc != sblock_try[i])
693 			continue;
694 		break;
695 	}
696 
697 	dev_bsize = fs->fs_fsize / FFS_FSBTODB(fs, 1);
698 	sblockloc = sblock_try[i] / dev_bsize;
699 }
700 
701 static void
bwrite(daddr_t blk,char * buffer,int size,const char * file)702 bwrite(daddr_t blk, char *buffer, int size, const char *file)
703 {
704 	off_t	offset;
705 
706 	offset = (off_t)blk * dev_bsize;
707 	if (lseek(fi, offset, SEEK_SET) == -1)
708 		err(6, "%s: seeking to %lld", file, (long long)offset);
709 	if (write(fi, buffer, size) != size)
710 		err(7, "%s: writing %d bytes", file, size);
711 }
712 
713 static void
bread(daddr_t blk,char * buffer,int cnt,const char * file)714 bread(daddr_t blk, char *buffer, int cnt, const char *file)
715 {
716 	off_t	offset;
717 	int	i;
718 
719 	offset = (off_t)blk * dev_bsize;
720 	if (lseek(fi, offset, SEEK_SET) == -1)
721 		err(4, "%s: seeking to %lld", file, (long long)offset);
722 	if ((i = read(fi, buffer, cnt)) != cnt)
723 		errx(5, "%s: short read", file);
724 }
725 
726 static int
openpartition(const char * name,int flags,char * device,size_t devicelen)727 openpartition(const char *name, int flags, char *device, size_t devicelen)
728 {
729 	char		specname[MAXPATHLEN];
730 	char		rawname[MAXPATHLEN];
731 	const char	*special, *raw;
732 	struct fstab	*fs;
733 	int		fd, oerrno;
734 
735 	fs = getfsfile(name);
736 	special = fs ? fs->fs_spec : name;
737 
738 	raw = getfsspecname(specname, sizeof(specname), special);
739 	if (raw == NULL)
740 		err(1, "%s: %s", name, specname);
741 	special = getdiskrawname(rawname, sizeof(rawname), raw);
742 	if (special == NULL)
743 		special = raw;
744 
745 	fd = opendisk(special, flags, device, devicelen, 0);
746 	if (fd == -1 && errno == ENOENT) {
747 		oerrno = errno;
748 		strlcpy(device, special, devicelen);
749 		errno = oerrno;
750 	}
751 	return (fd);
752 }
753