xref: /freebsd-12.1/sbin/tunefs/tunefs.c (revision db0a0e89)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 static char sccsid[] = "@(#)tunefs.c	8.2 (Berkeley) 4/19/94";
41 #endif /* not lint */
42 #endif
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 /*
47  * tunefs: change layout parameters to an existing file system.
48  */
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/disklabel.h>
52 #include <sys/stat.h>
53 
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
57 #include <ufs/ufs/dir.h>
58 
59 #include <ctype.h>
60 #include <err.h>
61 #include <fcntl.h>
62 #include <fstab.h>
63 #include <libufs.h>
64 #include <mntopts.h>
65 #include <paths.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <stdint.h>
69 #include <string.h>
70 #include <time.h>
71 #include <unistd.h>
72 
73 /* the optimization warning string template */
74 #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
75 
76 static int blocks;
77 static char clrbuf[MAXBSIZE];
78 static struct uufsd disk;
79 #define	sblock disk.d_fs
80 
81 static void usage(void);
82 static void printfs(void);
83 static int journal_alloc(int64_t size);
84 static void journal_clear(void);
85 static void sbdirty(void);
86 
87 int
main(int argc,char * argv[])88 main(int argc, char *argv[])
89 {
90 	const char *avalue, *jvalue, *Jvalue, *Lvalue, *lvalue, *Nvalue, *nvalue;
91 	const char *tvalue;
92 	const char *special, *on;
93 	const char *name;
94 	int active;
95 	int Aflag, aflag, eflag, evalue, fflag, fvalue, jflag, Jflag, kflag;
96 	int kvalue, Lflag, lflag, mflag, mvalue, Nflag, nflag, oflag, ovalue;
97 	int pflag, sflag, svalue, Svalue, tflag;
98 	int ch, found_arg, i;
99 	int iovlen = 0;
100 	const char *chg[2];
101 	struct statfs stfs;
102 	struct iovec *iov = NULL;
103 	char errmsg[255] = {0};
104 
105 	if (argc < 3)
106 		usage();
107 	Aflag = aflag = eflag = fflag = jflag = Jflag = kflag = Lflag = 0;
108 	lflag = mflag = Nflag = nflag = oflag = pflag = sflag = tflag = 0;
109 	avalue = jvalue = Jvalue = Lvalue = lvalue = Nvalue = nvalue = NULL;
110 	evalue = fvalue = mvalue = ovalue = svalue = Svalue = 0;
111 	active = 0;
112 	found_arg = 0;		/* At least one arg is required. */
113 	while ((ch = getopt(argc, argv, "Aa:e:f:j:J:k:L:l:m:N:n:o:ps:S:t:"))
114 	    != -1)
115 		switch (ch) {
116 
117 		case 'A':
118 			found_arg = 1;
119 			Aflag++;
120 			break;
121 
122 		case 'a':
123 			found_arg = 1;
124 			name = "POSIX.1e ACLs";
125 			avalue = optarg;
126 			if (strcmp(avalue, "enable") &&
127 			    strcmp(avalue, "disable")) {
128 				errx(10, "bad %s (options are %s)",
129 				    name, "`enable' or `disable'");
130 			}
131 			aflag = 1;
132 			break;
133 
134 		case 'e':
135 			found_arg = 1;
136 			name = "maximum blocks per file in a cylinder group";
137 			evalue = atoi(optarg);
138 			if (evalue < 1)
139 				errx(10, "%s must be >= 1 (was %s)",
140 				    name, optarg);
141 			eflag = 1;
142 			break;
143 
144 		case 'f':
145 			found_arg = 1;
146 			name = "average file size";
147 			fvalue = atoi(optarg);
148 			if (fvalue < 1)
149 				errx(10, "%s must be >= 1 (was %s)",
150 				    name, optarg);
151 			fflag = 1;
152 			break;
153 
154 		case 'j':
155 			found_arg = 1;
156 			name = "softdep journaled file system";
157 			jvalue = optarg;
158 			if (strcmp(jvalue, "enable") &&
159 			    strcmp(jvalue, "disable")) {
160 				errx(10, "bad %s (options are %s)",
161 				    name, "`enable' or `disable'");
162 			}
163 			jflag = 1;
164 			break;
165 
166 		case 'J':
167 			found_arg = 1;
168 			name = "gjournaled file system";
169 			Jvalue = optarg;
170 			if (strcmp(Jvalue, "enable") &&
171 			    strcmp(Jvalue, "disable")) {
172 				errx(10, "bad %s (options are %s)",
173 				    name, "`enable' or `disable'");
174 			}
175 			Jflag = 1;
176 			break;
177 
178 		case 'k':
179 			found_arg = 1;
180 			name = "space to hold for metadata blocks";
181 			kvalue = atoi(optarg);
182 			if (kvalue < 0)
183 				errx(10, "bad %s (%s)", name, optarg);
184 			kflag = 1;
185 			break;
186 
187 		case 'L':
188 			found_arg = 1;
189 			name = "volume label";
190 			Lvalue = optarg;
191 			i = -1;
192 			while (isalnum(Lvalue[++i]) || Lvalue[i] == '_' ||
193 			    Lvalue[i] == '-')
194 				;
195 			if (Lvalue[i] != '\0') {
196 				errx(10, "bad %s. Valid characters are "
197 				    "alphanumerics, dashes, and underscores.",
198 				    name);
199 			}
200 			if (strlen(Lvalue) >= MAXVOLLEN) {
201 				errx(10, "bad %s. Length is longer than %d.",
202 				    name, MAXVOLLEN - 1);
203 			}
204 			Lflag = 1;
205 			break;
206 
207 		case 'l':
208 			found_arg = 1;
209 			name = "multilabel MAC file system";
210 			lvalue = optarg;
211 			if (strcmp(lvalue, "enable") &&
212 			    strcmp(lvalue, "disable")) {
213 				errx(10, "bad %s (options are %s)",
214 				    name, "`enable' or `disable'");
215 			}
216 			lflag = 1;
217 			break;
218 
219 		case 'm':
220 			found_arg = 1;
221 			name = "minimum percentage of free space";
222 			mvalue = atoi(optarg);
223 			if (mvalue < 0 || mvalue > 99)
224 				errx(10, "bad %s (%s)", name, optarg);
225 			mflag = 1;
226 			break;
227 
228 		case 'N':
229 			found_arg = 1;
230 			name = "NFSv4 ACLs";
231 			Nvalue = optarg;
232 			if (strcmp(Nvalue, "enable") &&
233 			    strcmp(Nvalue, "disable")) {
234 				errx(10, "bad %s (options are %s)",
235 				    name, "`enable' or `disable'");
236 			}
237 			Nflag = 1;
238 			break;
239 
240 		case 'n':
241 			found_arg = 1;
242 			name = "soft updates";
243 			nvalue = optarg;
244 			if (strcmp(nvalue, "enable") != 0 &&
245 			    strcmp(nvalue, "disable") != 0) {
246 				errx(10, "bad %s (options are %s)",
247 				    name, "`enable' or `disable'");
248 			}
249 			nflag = 1;
250 			break;
251 
252 		case 'o':
253 			found_arg = 1;
254 			name = "optimization preference";
255 			if (strcmp(optarg, "space") == 0)
256 				ovalue = FS_OPTSPACE;
257 			else if (strcmp(optarg, "time") == 0)
258 				ovalue = FS_OPTTIME;
259 			else
260 				errx(10,
261 				    "bad %s (options are `space' or `time')",
262 				    name);
263 			oflag = 1;
264 			break;
265 
266 		case 'p':
267 			found_arg = 1;
268 			pflag = 1;
269 			break;
270 
271 		case 's':
272 			found_arg = 1;
273 			name = "expected number of files per directory";
274 			svalue = atoi(optarg);
275 			if (svalue < 1)
276 				errx(10, "%s must be >= 1 (was %s)",
277 				    name, optarg);
278 			sflag = 1;
279 			break;
280 
281 		case 'S':
282 			found_arg = 1;
283 			name = "Softdep Journal Size";
284 			Svalue = atoi(optarg);
285 			if (Svalue < SUJ_MIN)
286 				errx(10, "%s must be >= %d (was %s)",
287 				    name, SUJ_MIN, optarg);
288 			break;
289 
290 		case 't':
291 			found_arg = 1;
292 			name = "trim";
293 			tvalue = optarg;
294 			if (strcmp(tvalue, "enable") != 0 &&
295 			    strcmp(tvalue, "disable") != 0) {
296 				errx(10, "bad %s (options are %s)",
297 				    name, "`enable' or `disable'");
298 			}
299 			tflag = 1;
300 			break;
301 
302 		default:
303 			usage();
304 		}
305 	argc -= optind;
306 	argv += optind;
307 	if (found_arg == 0 || argc != 1)
308 		usage();
309 
310 	on = special = argv[0];
311 	if (ufs_disk_fillout(&disk, special) == -1)
312 		goto err;
313 	if (disk.d_name != special) {
314 		if (statfs(special, &stfs) != 0)
315 			warn("Can't stat %s", special);
316 		if (strcmp(special, stfs.f_mntonname) == 0)
317 			active = 1;
318 	}
319 
320 	if (pflag) {
321 		printfs();
322 		exit(0);
323 	}
324 	if (Lflag) {
325 		name = "volume label";
326 		strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
327 	}
328 	if (aflag) {
329 		name = "POSIX.1e ACLs";
330 		if (strcmp(avalue, "enable") == 0) {
331 			if (sblock.fs_flags & FS_ACLS) {
332 				warnx("%s remains unchanged as enabled", name);
333 			} else if (sblock.fs_flags & FS_NFS4ACLS) {
334 				warnx("%s and NFSv4 ACLs are mutually "
335 				    "exclusive", name);
336 			} else {
337 				sblock.fs_flags |= FS_ACLS;
338 				warnx("%s set", name);
339 			}
340 		} else if (strcmp(avalue, "disable") == 0) {
341 			if ((~sblock.fs_flags & FS_ACLS) ==
342 			    FS_ACLS) {
343 				warnx("%s remains unchanged as disabled",
344 				    name);
345 			} else {
346 				sblock.fs_flags &= ~FS_ACLS;
347 				warnx("%s cleared", name);
348 			}
349 		}
350 	}
351 	if (eflag) {
352 		name = "maximum blocks per file in a cylinder group";
353 		if (sblock.fs_maxbpg == evalue)
354 			warnx("%s remains unchanged as %d", name, evalue);
355 		else {
356 			warnx("%s changes from %d to %d",
357 			    name, sblock.fs_maxbpg, evalue);
358 			sblock.fs_maxbpg = evalue;
359 		}
360 	}
361 	if (fflag) {
362 		name = "average file size";
363 		if (sblock.fs_avgfilesize == (unsigned)fvalue) {
364 			warnx("%s remains unchanged as %d", name, fvalue);
365 		}
366 		else {
367 			warnx("%s changes from %d to %d",
368 					name, sblock.fs_avgfilesize, fvalue);
369 			sblock.fs_avgfilesize = fvalue;
370 		}
371 	}
372 	if (jflag) {
373  		name = "soft updates journaling";
374  		if (strcmp(jvalue, "enable") == 0) {
375 			if ((sblock.fs_flags & (FS_DOSOFTDEP | FS_SUJ)) ==
376 			    (FS_DOSOFTDEP | FS_SUJ)) {
377 				warnx("%s remains unchanged as enabled", name);
378 			} else if (sblock.fs_clean == 0) {
379 				warnx("%s cannot be enabled until fsck is run",
380 				    name);
381 			} else if (journal_alloc(Svalue) != 0) {
382 				warnx("%s cannot be enabled", name);
383 			} else {
384  				sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
385  				warnx("%s set", name);
386 			}
387  		} else if (strcmp(jvalue, "disable") == 0) {
388 			if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
389 				warnx("%s remains unchanged as disabled", name);
390 			} else {
391 				journal_clear();
392  				sblock.fs_flags &= ~FS_SUJ;
393 				sblock.fs_sujfree = 0;
394  				warnx("%s cleared but soft updates still set.",
395 				    name);
396 
397 				warnx("remove .sujournal to reclaim space");
398 			}
399  		}
400 	}
401 	if (Jflag) {
402 		name = "gjournal";
403 		if (strcmp(Jvalue, "enable") == 0) {
404 			if (sblock.fs_flags & FS_GJOURNAL) {
405 				warnx("%s remains unchanged as enabled", name);
406 			} else {
407 				sblock.fs_flags |= FS_GJOURNAL;
408 				warnx("%s set", name);
409 			}
410 		} else if (strcmp(Jvalue, "disable") == 0) {
411 			if ((~sblock.fs_flags & FS_GJOURNAL) ==
412 			    FS_GJOURNAL) {
413 				warnx("%s remains unchanged as disabled",
414 				    name);
415 			} else {
416 				sblock.fs_flags &= ~FS_GJOURNAL;
417 				warnx("%s cleared", name);
418 			}
419 		}
420 	}
421 	if (kflag) {
422 		name = "space to hold for metadata blocks";
423 		if (sblock.fs_metaspace == kvalue)
424 			warnx("%s remains unchanged as %d", name, kvalue);
425 		else {
426 			kvalue = blknum(&sblock, kvalue);
427 			if (kvalue > sblock.fs_fpg / 2) {
428 				kvalue = blknum(&sblock, sblock.fs_fpg / 2);
429 				warnx("%s cannot exceed half the file system "
430 				    "space", name);
431 			}
432 			warnx("%s changes from %jd to %d",
433 				    name, sblock.fs_metaspace, kvalue);
434 			sblock.fs_metaspace = kvalue;
435 		}
436 	}
437 	if (lflag) {
438 		name = "multilabel";
439 		if (strcmp(lvalue, "enable") == 0) {
440 			if (sblock.fs_flags & FS_MULTILABEL) {
441 				warnx("%s remains unchanged as enabled", name);
442 			} else {
443 				sblock.fs_flags |= FS_MULTILABEL;
444 				warnx("%s set", name);
445 			}
446 		} else if (strcmp(lvalue, "disable") == 0) {
447 			if ((~sblock.fs_flags & FS_MULTILABEL) ==
448 			    FS_MULTILABEL) {
449 				warnx("%s remains unchanged as disabled",
450 				    name);
451 			} else {
452 				sblock.fs_flags &= ~FS_MULTILABEL;
453 				warnx("%s cleared", name);
454 			}
455 		}
456 	}
457 	if (mflag) {
458 		name = "minimum percentage of free space";
459 		if (sblock.fs_minfree == mvalue)
460 			warnx("%s remains unchanged as %d%%", name, mvalue);
461 		else {
462 			warnx("%s changes from %d%% to %d%%",
463 				    name, sblock.fs_minfree, mvalue);
464 			sblock.fs_minfree = mvalue;
465 			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
466 				warnx(OPTWARN, "time", ">=", MINFREE);
467 			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
468 				warnx(OPTWARN, "space", "<", MINFREE);
469 		}
470 	}
471 	if (Nflag) {
472 		name = "NFSv4 ACLs";
473 		if (strcmp(Nvalue, "enable") == 0) {
474 			if (sblock.fs_flags & FS_NFS4ACLS) {
475 				warnx("%s remains unchanged as enabled", name);
476 			} else if (sblock.fs_flags & FS_ACLS) {
477 				warnx("%s and POSIX.1e ACLs are mutually "
478 				    "exclusive", name);
479 			} else {
480 				sblock.fs_flags |= FS_NFS4ACLS;
481 				warnx("%s set", name);
482 			}
483 		} else if (strcmp(Nvalue, "disable") == 0) {
484 			if ((~sblock.fs_flags & FS_NFS4ACLS) ==
485 			    FS_NFS4ACLS) {
486 				warnx("%s remains unchanged as disabled",
487 				    name);
488 			} else {
489 				sblock.fs_flags &= ~FS_NFS4ACLS;
490 				warnx("%s cleared", name);
491 			}
492 		}
493 	}
494 	if (nflag) {
495  		name = "soft updates";
496  		if (strcmp(nvalue, "enable") == 0) {
497 			if (sblock.fs_flags & FS_DOSOFTDEP)
498 				warnx("%s remains unchanged as enabled", name);
499 			else if (sblock.fs_clean == 0) {
500 				warnx("%s cannot be enabled until fsck is run",
501 				    name);
502 			} else {
503  				sblock.fs_flags |= FS_DOSOFTDEP;
504  				warnx("%s set", name);
505 			}
506  		} else if (strcmp(nvalue, "disable") == 0) {
507 			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
508 				warnx("%s remains unchanged as disabled", name);
509 			else {
510  				sblock.fs_flags &= ~FS_DOSOFTDEP;
511  				warnx("%s cleared", name);
512 			}
513  		}
514 	}
515 	if (oflag) {
516 		name = "optimization preference";
517 		chg[FS_OPTSPACE] = "space";
518 		chg[FS_OPTTIME] = "time";
519 		if (sblock.fs_optim == ovalue)
520 			warnx("%s remains unchanged as %s", name, chg[ovalue]);
521 		else {
522 			warnx("%s changes from %s to %s",
523 				    name, chg[sblock.fs_optim], chg[ovalue]);
524 			sblock.fs_optim = ovalue;
525 			if (sblock.fs_minfree >= MINFREE &&
526 			    ovalue == FS_OPTSPACE)
527 				warnx(OPTWARN, "time", ">=", MINFREE);
528 			if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME)
529 				warnx(OPTWARN, "space", "<", MINFREE);
530 		}
531 	}
532 	if (sflag) {
533 		name = "expected number of files per directory";
534 		if (sblock.fs_avgfpdir == (unsigned)svalue) {
535 			warnx("%s remains unchanged as %d", name, svalue);
536 		}
537 		else {
538 			warnx("%s changes from %d to %d",
539 					name, sblock.fs_avgfpdir, svalue);
540 			sblock.fs_avgfpdir = svalue;
541 		}
542 	}
543 	if (tflag) {
544 		name = "issue TRIM to the disk";
545  		if (strcmp(tvalue, "enable") == 0) {
546 			if (sblock.fs_flags & FS_TRIM)
547 				warnx("%s remains unchanged as enabled", name);
548 			else {
549  				sblock.fs_flags |= FS_TRIM;
550  				warnx("%s set", name);
551 			}
552  		} else if (strcmp(tvalue, "disable") == 0) {
553 			if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
554 				warnx("%s remains unchanged as disabled", name);
555 			else {
556  				sblock.fs_flags &= ~FS_TRIM;
557  				warnx("%s cleared", name);
558 			}
559  		}
560 	}
561 
562 	if (sbwrite(&disk, Aflag) == -1)
563 		goto err;
564 	ufs_disk_close(&disk);
565 	if (active) {
566 		build_iovec_argf(&iov, &iovlen, "fstype", "ufs");
567 		build_iovec_argf(&iov, &iovlen, "fspath", "%s", on);
568 		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
569 		if (nmount(iov, iovlen,
570 		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD) < 0) {
571 			if (errmsg[0])
572 				err(9, "%s: reload: %s", special, errmsg);
573 			else
574 				err(9, "%s: reload", special);
575 		}
576 		warnx("file system reloaded");
577 	}
578 	exit(0);
579 err:
580 	if (disk.d_error != NULL)
581 		errx(11, "%s: %s", special, disk.d_error);
582 	else
583 		err(12, "%s", special);
584 }
585 
586 static void
sbdirty(void)587 sbdirty(void)
588 {
589 	disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
590 	disk.d_fs.fs_clean = 0;
591 }
592 
593 static ufs2_daddr_t
journal_balloc(void)594 journal_balloc(void)
595 {
596 	ufs2_daddr_t blk;
597 	struct cg *cgp;
598 	int valid;
599 	static int contig = 1;
600 
601 	cgp = &disk.d_cg;
602 	for (;;) {
603 		blk = cgballoc(&disk);
604 		if (blk > 0)
605 			break;
606 		/*
607 		 * If we failed to allocate a block from this cg, move to
608 		 * the next.
609 		 */
610 		if (cgwrite(&disk) < 0) {
611 			warn("Failed to write updated cg");
612 			return (-1);
613 		}
614 		while ((valid = cgread(&disk)) == 1) {
615 			/*
616 			 * Try to minimize fragmentation by requiring a minimum
617 			 * number of blocks present.
618 			 */
619 			if (cgp->cg_cs.cs_nbfree > 256 * 1024)
620 				break;
621 			if (contig == 0 && cgp->cg_cs.cs_nbfree)
622 				break;
623 		}
624 		if (valid)
625 			continue;
626 		/*
627 		 * Try once through looking only for large contiguous regions
628 		 * and again taking any space we can find.
629 		 */
630 		if (contig) {
631 			contig = 0;
632 			disk.d_ccg = 0;
633 			warnx("Journal file fragmented.");
634 			continue;
635 		}
636 		warnx("Failed to find sufficient free blocks for the journal");
637 		return -1;
638 	}
639 	if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
640 	    sblock.fs_bsize) <= 0) {
641 		warn("Failed to initialize new block");
642 		return -1;
643 	}
644 	return (blk);
645 }
646 
647 /*
648  * Search a directory block for the SUJ_FILE.
649  */
650 static ino_t
dir_search(ufs2_daddr_t blk,int bytes)651 dir_search(ufs2_daddr_t blk, int bytes)
652 {
653 	char block[MAXBSIZE];
654 	struct direct *dp;
655 	int off;
656 
657 	if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
658 		warn("Failed to read dir block");
659 		return (-1);
660 	}
661 	for (off = 0; off < bytes; off += dp->d_reclen) {
662 		dp = (struct direct *)&block[off];
663 		if (dp->d_reclen == 0)
664 			break;
665 		if (dp->d_ino == 0)
666 			continue;
667 		if (dp->d_namlen != strlen(SUJ_FILE))
668 			continue;
669 		if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
670 			continue;
671 		return (dp->d_ino);
672 	}
673 
674 	return (0);
675 }
676 
677 /*
678  * Search in the UFS_ROOTINO for the SUJ_FILE.  If it exists we can not enable
679  * journaling.
680  */
681 static ino_t
journal_findfile(void)682 journal_findfile(void)
683 {
684 	struct ufs1_dinode *dp1;
685 	struct ufs2_dinode *dp2;
686 	ino_t ino;
687 	int mode;
688 	void *ip;
689 	int i;
690 
691 	if (getino(&disk, &ip, UFS_ROOTINO, &mode) != 0) {
692 		warn("Failed to get root inode");
693 		return (-1);
694 	}
695 	dp2 = ip;
696 	dp1 = ip;
697 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
698 		if ((off_t)dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
699 			warnx("UFS_ROOTINO extends beyond direct blocks.");
700 			return (-1);
701 		}
702 		for (i = 0; i < UFS_NDADDR; i++) {
703 			if (dp1->di_db[i] == 0)
704 				break;
705 			if ((ino = dir_search(dp1->di_db[i],
706 			    sblksize(&sblock, (off_t)dp1->di_size, i))) != 0)
707 				return (ino);
708 		}
709 	} else {
710 		if ((off_t)dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
711 			warnx("UFS_ROOTINO extends beyond direct blocks.");
712 			return (-1);
713 		}
714 		for (i = 0; i < UFS_NDADDR; i++) {
715 			if (dp2->di_db[i] == 0)
716 				break;
717 			if ((ino = dir_search(dp2->di_db[i],
718 			    sblksize(&sblock, (off_t)dp2->di_size, i))) != 0)
719 				return (ino);
720 		}
721 	}
722 
723 	return (0);
724 }
725 
726 static void
dir_clear_block(const char * block,off_t off)727 dir_clear_block(const char *block, off_t off)
728 {
729 	struct direct *dp;
730 
731 	for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
732 		dp = (struct direct *)&block[off];
733 		dp->d_ino = 0;
734 		dp->d_reclen = DIRBLKSIZ;
735 		dp->d_type = DT_UNKNOWN;
736 	}
737 }
738 
739 /*
740  * Insert the journal at inode 'ino' into directory blk 'blk' at the first
741  * free offset of 'off'.  DIRBLKSIZ blocks after off are initialized as
742  * empty.
743  */
744 static int
dir_insert(ufs2_daddr_t blk,off_t off,ino_t ino)745 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
746 {
747 	struct direct *dp;
748 	char block[MAXBSIZE];
749 
750 	if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
751 		warn("Failed to read dir block");
752 		return (-1);
753 	}
754 	bzero(&block[off], sblock.fs_bsize - off);
755 	dp = (struct direct *)&block[off];
756 	dp->d_ino = ino;
757 	dp->d_reclen = DIRBLKSIZ;
758 	dp->d_type = DT_REG;
759 	dp->d_namlen = strlen(SUJ_FILE);
760 	bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
761 	dir_clear_block(block, off + DIRBLKSIZ);
762 	if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
763 		warn("Failed to write dir block");
764 		return (-1);
765 	}
766 	return (0);
767 }
768 
769 /*
770  * Extend a directory block in 'blk' by copying it to a full size block
771  * and inserting the new journal inode into .sujournal.
772  */
773 static int
dir_extend(ufs2_daddr_t blk,ufs2_daddr_t nblk,off_t size,ino_t ino)774 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
775 {
776 	char block[MAXBSIZE];
777 
778 	if (bread(&disk, fsbtodb(&sblock, blk), block,
779 	    roundup(size, sblock.fs_fsize)) <= 0) {
780 		warn("Failed to read dir block");
781 		return (-1);
782 	}
783 	dir_clear_block(block, size);
784 	if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
785 	    <= 0) {
786 		warn("Failed to write dir block");
787 		return (-1);
788 	}
789 
790 	return (dir_insert(nblk, size, ino));
791 }
792 
793 /*
794  * Insert the journal file into the UFS_ROOTINO directory.  We always extend the
795  * last frag
796  */
797 static int
journal_insertfile(ino_t ino)798 journal_insertfile(ino_t ino)
799 {
800 	struct ufs1_dinode *dp1;
801 	struct ufs2_dinode *dp2;
802 	void *ip;
803 	ufs2_daddr_t nblk;
804 	ufs2_daddr_t blk;
805 	ufs_lbn_t lbn;
806 	int size;
807 	int mode;
808 	int off;
809 
810 	if (getino(&disk, &ip, UFS_ROOTINO, &mode) != 0) {
811 		warn("Failed to get root inode");
812 		sbdirty();
813 		return (-1);
814 	}
815 	dp2 = ip;
816 	dp1 = ip;
817 	blk = 0;
818 	size = 0;
819 	nblk = journal_balloc();
820 	if (nblk <= 0)
821 		return (-1);
822 	/*
823 	 * For simplicity sake we aways extend the UFS_ROOTINO into a new
824 	 * directory block rather than searching for space and inserting
825 	 * into an existing block.  However, if the rootino has frags
826 	 * have to free them and extend the block.
827 	 */
828 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
829 		lbn = lblkno(&sblock, dp1->di_size);
830 		off = blkoff(&sblock, dp1->di_size);
831 		blk = dp1->di_db[lbn];
832 		size = sblksize(&sblock, (off_t)dp1->di_size, lbn);
833 	} else {
834 		lbn = lblkno(&sblock, dp2->di_size);
835 		off = blkoff(&sblock, dp2->di_size);
836 		blk = dp2->di_db[lbn];
837 		size = sblksize(&sblock, (off_t)dp2->di_size, lbn);
838 	}
839 	if (off != 0) {
840 		if (dir_extend(blk, nblk, off, ino) == -1)
841 			return (-1);
842 	} else {
843 		blk = 0;
844 		if (dir_insert(nblk, 0, ino) == -1)
845 			return (-1);
846 	}
847 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
848 		dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
849 		dp1->di_db[lbn] = nblk;
850 		dp1->di_size = lblktosize(&sblock, lbn+1);
851 	} else {
852 		dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
853 		dp2->di_db[lbn] = nblk;
854 		dp2->di_size = lblktosize(&sblock, lbn+1);
855 	}
856 	if (putino(&disk) < 0) {
857 		warn("Failed to write root inode");
858 		return (-1);
859 	}
860 	if (cgwrite(&disk) < 0) {
861 		warn("Failed to write updated cg");
862 		sbdirty();
863 		return (-1);
864 	}
865 	if (blk) {
866 		if (cgbfree(&disk, blk, size) < 0) {
867 			warn("Failed to write cg");
868 			return (-1);
869 		}
870 	}
871 
872 	return (0);
873 }
874 
875 static int
indir_fill(ufs2_daddr_t blk,int level,int * resid)876 indir_fill(ufs2_daddr_t blk, int level, int *resid)
877 {
878 	char indirbuf[MAXBSIZE];
879 	ufs1_daddr_t *bap1;
880 	ufs2_daddr_t *bap2;
881 	ufs2_daddr_t nblk;
882 	int ncnt;
883 	int cnt;
884 	int i;
885 
886 	bzero(indirbuf, sizeof(indirbuf));
887 	bap1 = (ufs1_daddr_t *)indirbuf;
888 	bap2 = (void *)bap1;
889 	cnt = 0;
890 	for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
891 		nblk = journal_balloc();
892 		if (nblk <= 0)
893 			return (-1);
894 		cnt++;
895 		if (sblock.fs_magic == FS_UFS1_MAGIC)
896 			*bap1++ = nblk;
897 		else
898 			*bap2++ = nblk;
899 		if (level != 0) {
900 			ncnt = indir_fill(nblk, level - 1, resid);
901 			if (ncnt <= 0)
902 				return (-1);
903 			cnt += ncnt;
904 		} else
905 			(*resid)--;
906 	}
907 	if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
908 	    sblock.fs_bsize) <= 0) {
909 		warn("Failed to write indirect");
910 		return (-1);
911 	}
912 	return (cnt);
913 }
914 
915 /*
916  * Clear the flag bits so the journal can be removed.
917  */
918 static void
journal_clear(void)919 journal_clear(void)
920 {
921 	struct ufs1_dinode *dp1;
922 	struct ufs2_dinode *dp2;
923 	ino_t ino;
924 	int mode;
925 	void *ip;
926 
927 	ino = journal_findfile();
928 	if (ino == (ino_t)-1 || ino == 0) {
929 		warnx("Journal file does not exist");
930 		return;
931 	}
932 	printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
933 	if (getino(&disk, &ip, ino, &mode) != 0) {
934 		warn("Failed to get journal inode");
935 		return;
936 	}
937 	dp2 = ip;
938 	dp1 = ip;
939 	if (sblock.fs_magic == FS_UFS1_MAGIC)
940 		dp1->di_flags = 0;
941 	else
942 		dp2->di_flags = 0;
943 	if (putino(&disk) < 0) {
944 		warn("Failed to write journal inode");
945 		return;
946 	}
947 }
948 
949 static int
journal_alloc(int64_t size)950 journal_alloc(int64_t size)
951 {
952 	struct ufs1_dinode *dp1;
953 	struct ufs2_dinode *dp2;
954 	ufs2_daddr_t blk;
955 	void *ip;
956 	struct cg *cgp;
957 	int resid;
958 	ino_t ino;
959 	int blks;
960 	int mode;
961 	time_t utime;
962 	int i;
963 
964 	cgp = &disk.d_cg;
965 	ino = 0;
966 
967 	/*
968 	 * If the journal file exists we can't allocate it.
969 	 */
970 	ino = journal_findfile();
971 	if (ino == (ino_t)-1) {
972 		warnx("journal_findfile() failed.");
973 		return (-1);
974 	}
975 	if (ino > 0) {
976 		warnx("Journal file %s already exists, please remove.",
977 		    SUJ_FILE);
978 		return (-1);
979 	}
980 	/*
981 	 * If the user didn't supply a size pick one based on the filesystem
982 	 * size constrained with hardcoded MIN and MAX values.  We opt for
983 	 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
984 	 * not less than the MIN.
985 	 */
986 	if (size == 0) {
987 		size = (sblock.fs_size * sblock.fs_bsize) / 1024;
988 		size = MIN(SUJ_MAX, size);
989 		if (size / sblock.fs_fsize > sblock.fs_fpg)
990 			size = sblock.fs_fpg * sblock.fs_fsize;
991 		size = MAX(SUJ_MIN, size);
992 	}
993 	/* fsck does not support fragments in journal files. */
994 	size = roundup(size, sblock.fs_bsize);
995 	resid = blocks = size / sblock.fs_bsize;
996 	if (sblock.fs_cstotal.cs_nbfree < blocks) {
997 		warn("Insufficient free space for %jd byte journal", size);
998 		return (-1);
999 	}
1000 	/*
1001 	 * Find a cg with enough blocks to satisfy the journal
1002 	 * size.  Presently the journal does not span cgs.
1003 	 */
1004 	while (cgread(&disk) == 1) {
1005 		if (cgp->cg_cs.cs_nifree == 0)
1006 			continue;
1007 		ino = cgialloc(&disk);
1008 		if (ino <= 0)
1009 			break;
1010 		printf("Using inode %ju in cg %d for %jd byte journal\n",
1011 		    (uintmax_t)ino, cgp->cg_cgx, size);
1012 		if (getino(&disk, &ip, ino, &mode) != 0) {
1013 			warn("Failed to get allocated inode");
1014 			sbdirty();
1015 			goto out;
1016 		}
1017 		/*
1018 		 * We leave fields unrelated to the number of allocated
1019 		 * blocks and size uninitialized.  This causes legacy
1020 		 * fsck implementations to clear the inode.
1021 		 */
1022 		dp2 = ip;
1023 		dp1 = ip;
1024 		time(&utime);
1025 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
1026 			bzero(dp1, sizeof(*dp1));
1027 			dp1->di_size = size;
1028 			dp1->di_mode = IFREG | IREAD;
1029 			dp1->di_nlink = 1;
1030 			dp1->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1031 			dp1->di_atime = utime;
1032 			dp1->di_mtime = utime;
1033 			dp1->di_ctime = utime;
1034 		} else {
1035 			bzero(dp2, sizeof(*dp2));
1036 			dp2->di_size = size;
1037 			dp2->di_mode = IFREG | IREAD;
1038 			dp2->di_nlink = 1;
1039 			dp2->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1040 			dp2->di_atime = utime;
1041 			dp2->di_mtime = utime;
1042 			dp2->di_ctime = utime;
1043 			dp2->di_birthtime = utime;
1044 		}
1045 		for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1046 			blk = journal_balloc();
1047 			if (blk <= 0)
1048 				goto out;
1049 			if (sblock.fs_magic == FS_UFS1_MAGIC) {
1050 				dp1->di_db[i] = blk;
1051 				dp1->di_blocks++;
1052 			} else {
1053 				dp2->di_db[i] = blk;
1054 				dp2->di_blocks++;
1055 			}
1056 		}
1057 		for (i = 0; i < UFS_NIADDR && resid; i++) {
1058 			blk = journal_balloc();
1059 			if (blk <= 0)
1060 				goto out;
1061 			blks = indir_fill(blk, i, &resid) + 1;
1062 			if (blks <= 0) {
1063 				sbdirty();
1064 				goto out;
1065 			}
1066 			if (sblock.fs_magic == FS_UFS1_MAGIC) {
1067 				dp1->di_ib[i] = blk;
1068 				dp1->di_blocks += blks;
1069 			} else {
1070 				dp2->di_ib[i] = blk;
1071 				dp2->di_blocks += blks;
1072 			}
1073 		}
1074 		if (sblock.fs_magic == FS_UFS1_MAGIC)
1075 			dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1076 		else
1077 			dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1078 		if (putino(&disk) < 0) {
1079 			warn("Failed to write inode");
1080 			sbdirty();
1081 			return (-1);
1082 		}
1083 		if (cgwrite(&disk) < 0) {
1084 			warn("Failed to write updated cg");
1085 			sbdirty();
1086 			return (-1);
1087 		}
1088 		if (journal_insertfile(ino) < 0) {
1089 			sbdirty();
1090 			return (-1);
1091 		}
1092 		sblock.fs_sujfree = 0;
1093 		return (0);
1094 	}
1095 	warnx("Insufficient free space for the journal.");
1096 out:
1097 	return (-1);
1098 }
1099 
1100 static void
usage(void)1101 usage(void)
1102 {
1103 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1104 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1105 "              [-J enable | disable] [-j enable | disable] [-k metaspace]",
1106 "              [-L volname] [-l enable | disable] [-m minfree]",
1107 "              [-N enable | disable] [-n enable | disable]",
1108 "              [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1109 "              special | filesystem");
1110 	exit(2);
1111 }
1112 
1113 static void
printfs(void)1114 printfs(void)
1115 {
1116 	warnx("POSIX.1e ACLs: (-a)                                %s",
1117 		(sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1118 	warnx("NFSv4 ACLs: (-N)                                   %s",
1119 		(sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1120 	warnx("MAC multilabel: (-l)                               %s",
1121 		(sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1122 	warnx("soft updates: (-n)                                 %s",
1123 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1124 	warnx("soft update journaling: (-j)                       %s",
1125 		(sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1126 	warnx("gjournal: (-J)                                     %s",
1127 		(sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1128 	warnx("trim: (-t)                                         %s",
1129 		(sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1130 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
1131 	      sblock.fs_maxbpg);
1132 	warnx("average file size: (-f)                            %d",
1133 	      sblock.fs_avgfilesize);
1134 	warnx("average number of files in a directory: (-s)       %d",
1135 	      sblock.fs_avgfpdir);
1136 	warnx("minimum percentage of free space: (-m)             %d%%",
1137 	      sblock.fs_minfree);
1138 	warnx("space to hold for metadata blocks: (-k)            %jd",
1139 	      sblock.fs_metaspace);
1140 	warnx("optimization preference: (-o)                      %s",
1141 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1142 	if (sblock.fs_minfree >= MINFREE &&
1143 	    sblock.fs_optim == FS_OPTSPACE)
1144 		warnx(OPTWARN, "time", ">=", MINFREE);
1145 	if (sblock.fs_minfree < MINFREE &&
1146 	    sblock.fs_optim == FS_OPTTIME)
1147 		warnx(OPTWARN, "space", "<", MINFREE);
1148 	warnx("volume label: (-L)                                 %s",
1149 		sblock.fs_volname);
1150 }
1151