xref: /freebsd/sbin/tunefs/tunefs.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1983, 1993
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes /*
338fae3551SRodney W. Grimes  * tunefs: change layout parameters to an existing file system.
348fae3551SRodney W. Grimes  */
358fae3551SRodney W. Grimes #include <sys/param.h>
367382c45aSLuoqi Chen #include <sys/mount.h>
3775766e17SPoul-Henning Kamp #include <sys/disklabel.h>
388fae3551SRodney W. Grimes #include <sys/stat.h>
398fae3551SRodney W. Grimes 
40d485c77fSKonstantin Belousov #include <ufs/ufs/extattr.h>
41d485c77fSKonstantin Belousov #include <ufs/ufs/quota.h>
427382c45aSLuoqi Chen #include <ufs/ufs/ufsmount.h>
431c85e6a3SKirk McKusick #include <ufs/ufs/dinode.h>
441c85e6a3SKirk McKusick #include <ufs/ffs/fs.h>
45113db2ddSJeff Roberson #include <ufs/ufs/dir.h>
468fae3551SRodney W. Grimes 
47c715b047SGordon Tetlow #include <ctype.h>
488fae3551SRodney W. Grimes #include <err.h>
498fae3551SRodney W. Grimes #include <fcntl.h>
508fae3551SRodney W. Grimes #include <fstab.h>
51b1f0fda0SJuli Mallett #include <libufs.h>
5298acad14SBrooks Davis #include <mntopts.h>
538fae3551SRodney W. Grimes #include <paths.h>
548679b1b4SPhilippe Charnier #include <stdio.h>
558fae3551SRodney W. Grimes #include <stdlib.h>
56113db2ddSJeff Roberson #include <stdint.h>
57060ac658SSheldon Hearn #include <string.h>
588ee53ea3SKirk McKusick #include <time.h>
598fae3551SRodney W. Grimes #include <unistd.h>
608fae3551SRodney W. Grimes 
618fae3551SRodney W. Grimes /* the optimization warning string template */
628fae3551SRodney W. Grimes #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
638fae3551SRodney W. Grimes 
6405d43d98SEdward Tomasz Napierala static int blocks;
6505d43d98SEdward Tomasz Napierala static char clrbuf[MAXBSIZE];
661efe3c6bSEd Schouten static struct uufsd disk;
67b1f0fda0SJuli Mallett #define	sblock disk.d_fs
688fae3551SRodney W. Grimes 
69*65f3be91SAlfonso Gregory static void usage(void) __dead2;
7005d43d98SEdward Tomasz Napierala static void printfs(void);
7105d43d98SEdward Tomasz Napierala static int journal_alloc(int64_t size);
7205d43d98SEdward Tomasz Napierala static void journal_clear(void);
7305d43d98SEdward Tomasz Napierala static void sbdirty(void);
748fae3551SRodney W. Grimes 
758fae3551SRodney W. Grimes int
main(int argc,char * argv[])76b1f0fda0SJuli Mallett main(int argc, char *argv[])
778fae3551SRodney W. Grimes {
781b83e8a3SXin LI 	const char *avalue, *jvalue, *Jvalue, *Lvalue, *lvalue, *Nvalue, *nvalue;
791b83e8a3SXin LI 	const char *tvalue;
80906c312bSKirk McKusick 	const char *special;
81c33fa91fSDima Dorfman 	const char *name;
82906c312bSKirk McKusick 	char *diskname;
83baa12a84SKirk McKusick 	int Aflag, aflag, eflag, evalue, fflag, fvalue, jflag, Jflag, kflag;
84baa12a84SKirk McKusick 	int kvalue, Lflag, lflag, mflag, mvalue, Nflag, nflag, oflag, ovalue;
85baa12a84SKirk McKusick 	int pflag, sflag, svalue, Svalue, tflag;
8629f9611dSBruce Evans 	int ch, found_arg, i;
87c33fa91fSDima Dorfman 	const char *chg[2];
888fae3551SRodney W. Grimes 
892af14b60SPhilippe Charnier 	if (argc < 3)
908fae3551SRodney W. Grimes 		usage();
91baa12a84SKirk McKusick 	Aflag = aflag = eflag = fflag = jflag = Jflag = kflag = Lflag = 0;
92baa12a84SKirk McKusick 	lflag = mflag = Nflag = nflag = oflag = pflag = sflag = tflag = 0;
93113db2ddSJeff Roberson 	avalue = jvalue = Jvalue = Lvalue = lvalue = Nvalue = nvalue = NULL;
94113db2ddSJeff Roberson 	evalue = fvalue = mvalue = ovalue = svalue = Svalue = 0;
9529f9611dSBruce Evans 	found_arg = 0;		/* At least one arg is required. */
96baa12a84SKirk McKusick 	while ((ch = getopt(argc, argv, "Aa:e:f:j:J:k:L:l:m:N:n:o:ps:S:t:"))
97a738d4cfSKonstantin Belousov 	    != -1)
9877edab90SPhilippe Charnier 		switch (ch) {
9929f9611dSBruce Evans 
100a2325efeSRobert Watson 		case 'A':
1016eb925f8SKirk McKusick 			found_arg++;
102a2325efeSRobert Watson 			Aflag++;
103a2325efeSRobert Watson 			break;
10429f9611dSBruce Evans 
105289e09eeSRobert Watson 		case 'a':
1066eb925f8SKirk McKusick 			found_arg++;
1079340fc72SEdward Tomasz Napierala 			name = "POSIX.1e ACLs";
108289e09eeSRobert Watson 			avalue = optarg;
10929f9611dSBruce Evans 			if (strcmp(avalue, "enable") &&
11029f9611dSBruce Evans 			    strcmp(avalue, "disable")) {
11129f9611dSBruce Evans 				errx(10, "bad %s (options are %s)",
11229f9611dSBruce Evans 				    name, "`enable' or `disable'");
113289e09eeSRobert Watson 			}
114289e09eeSRobert Watson 			aflag = 1;
115289e09eeSRobert Watson 			break;
11629f9611dSBruce Evans 
11777edab90SPhilippe Charnier 		case 'e':
1186eb925f8SKirk McKusick 			found_arg++;
11977edab90SPhilippe Charnier 			name = "maximum blocks per file in a cylinder group";
12077edab90SPhilippe Charnier 			evalue = atoi(optarg);
12177edab90SPhilippe Charnier 			if (evalue < 1)
12229f9611dSBruce Evans 				errx(10, "%s must be >= 1 (was %s)",
12329f9611dSBruce Evans 				    name, optarg);
12477edab90SPhilippe Charnier 			eflag = 1;
12577edab90SPhilippe Charnier 			break;
12629f9611dSBruce Evans 
127a61ab64aSKirk McKusick 		case 'f':
1286eb925f8SKirk McKusick 			found_arg++;
129a61ab64aSKirk McKusick 			name = "average file size";
130a61ab64aSKirk McKusick 			fvalue = atoi(optarg);
131a61ab64aSKirk McKusick 			if (fvalue < 1)
13229f9611dSBruce Evans 				errx(10, "%s must be >= 1 (was %s)",
13329f9611dSBruce Evans 				    name, optarg);
134a61ab64aSKirk McKusick 			fflag = 1;
135a61ab64aSKirk McKusick 			break;
13629f9611dSBruce Evans 
137113db2ddSJeff Roberson 		case 'j':
1386eb925f8SKirk McKusick 			found_arg++;
139113db2ddSJeff Roberson 			name = "softdep journaled file system";
140113db2ddSJeff Roberson 			jvalue = optarg;
141113db2ddSJeff Roberson 			if (strcmp(jvalue, "enable") &&
142113db2ddSJeff Roberson 			    strcmp(jvalue, "disable")) {
143113db2ddSJeff Roberson 				errx(10, "bad %s (options are %s)",
144113db2ddSJeff Roberson 				    name, "`enable' or `disable'");
145113db2ddSJeff Roberson 			}
146113db2ddSJeff Roberson 			jflag = 1;
147113db2ddSJeff Roberson 			break;
148113db2ddSJeff Roberson 
149868c68edSPawel Jakub Dawidek 		case 'J':
1506eb925f8SKirk McKusick 			found_arg++;
151868c68edSPawel Jakub Dawidek 			name = "gjournaled file system";
152868c68edSPawel Jakub Dawidek 			Jvalue = optarg;
153868c68edSPawel Jakub Dawidek 			if (strcmp(Jvalue, "enable") &&
154868c68edSPawel Jakub Dawidek 			    strcmp(Jvalue, "disable")) {
155868c68edSPawel Jakub Dawidek 				errx(10, "bad %s (options are %s)",
156868c68edSPawel Jakub Dawidek 				    name, "`enable' or `disable'");
157868c68edSPawel Jakub Dawidek 			}
158868c68edSPawel Jakub Dawidek 			Jflag = 1;
159868c68edSPawel Jakub Dawidek 			break;
160868c68edSPawel Jakub Dawidek 
161baa12a84SKirk McKusick 		case 'k':
1626eb925f8SKirk McKusick 			found_arg++;
163baa12a84SKirk McKusick 			name = "space to hold for metadata blocks";
164baa12a84SKirk McKusick 			kvalue = atoi(optarg);
165244dccb7SKirk McKusick 			if (kvalue < 0)
166baa12a84SKirk McKusick 				errx(10, "bad %s (%s)", name, optarg);
167baa12a84SKirk McKusick 			kflag = 1;
168baa12a84SKirk McKusick 			break;
169868c68edSPawel Jakub Dawidek 
1701f6a4631SRuslan Ermilov 		case 'L':
1716eb925f8SKirk McKusick 			found_arg++;
1721f6a4631SRuslan Ermilov 			name = "volume label";
1731f6a4631SRuslan Ermilov 			Lvalue = optarg;
1741f6a4631SRuslan Ermilov 			i = -1;
1751165591eSDmitry Morozovsky 			while (isalnum(Lvalue[++i]) || Lvalue[i] == '_' ||
1761165591eSDmitry Morozovsky 			    Lvalue[i] == '-')
1771165591eSDmitry Morozovsky 				;
1781f6a4631SRuslan Ermilov 			if (Lvalue[i] != '\0') {
179cd29c58eSKirk McKusick 				errx(10, "bad %s. Valid characters are "
1801165591eSDmitry Morozovsky 				    "alphanumerics, dashes, and underscores.",
1811165591eSDmitry Morozovsky 				    name);
1821f6a4631SRuslan Ermilov 			}
1831f6a4631SRuslan Ermilov 			if (strlen(Lvalue) >= MAXVOLLEN) {
1841f6a4631SRuslan Ermilov 				errx(10, "bad %s. Length is longer than %d.",
1851f6a4631SRuslan Ermilov 				    name, MAXVOLLEN - 1);
1861f6a4631SRuslan Ermilov 			}
1871f6a4631SRuslan Ermilov 			Lflag = 1;
1881f6a4631SRuslan Ermilov 			break;
18929f9611dSBruce Evans 
190289e09eeSRobert Watson 		case 'l':
1916eb925f8SKirk McKusick 			found_arg++;
192289e09eeSRobert Watson 			name = "multilabel MAC file system";
193289e09eeSRobert Watson 			lvalue = optarg;
19429f9611dSBruce Evans 			if (strcmp(lvalue, "enable") &&
19529f9611dSBruce Evans 			    strcmp(lvalue, "disable")) {
19629f9611dSBruce Evans 				errx(10, "bad %s (options are %s)",
19729f9611dSBruce Evans 				    name, "`enable' or `disable'");
198289e09eeSRobert Watson 			}
199289e09eeSRobert Watson 			lflag = 1;
200289e09eeSRobert Watson 			break;
20129f9611dSBruce Evans 
20277edab90SPhilippe Charnier 		case 'm':
2036eb925f8SKirk McKusick 			found_arg++;
20477edab90SPhilippe Charnier 			name = "minimum percentage of free space";
20577edab90SPhilippe Charnier 			mvalue = atoi(optarg);
20677edab90SPhilippe Charnier 			if (mvalue < 0 || mvalue > 99)
20777edab90SPhilippe Charnier 				errx(10, "bad %s (%s)", name, optarg);
20877edab90SPhilippe Charnier 			mflag = 1;
20977edab90SPhilippe Charnier 			break;
21029f9611dSBruce Evans 
2119340fc72SEdward Tomasz Napierala 		case 'N':
2126eb925f8SKirk McKusick 			found_arg++;
2139340fc72SEdward Tomasz Napierala 			name = "NFSv4 ACLs";
2149340fc72SEdward Tomasz Napierala 			Nvalue = optarg;
2159340fc72SEdward Tomasz Napierala 			if (strcmp(Nvalue, "enable") &&
2169340fc72SEdward Tomasz Napierala 			    strcmp(Nvalue, "disable")) {
2179340fc72SEdward Tomasz Napierala 				errx(10, "bad %s (options are %s)",
2189340fc72SEdward Tomasz Napierala 				    name, "`enable' or `disable'");
2199340fc72SEdward Tomasz Napierala 			}
2209340fc72SEdward Tomasz Napierala 			Nflag = 1;
2219340fc72SEdward Tomasz Napierala 			break;
2229340fc72SEdward Tomasz Napierala 
22377edab90SPhilippe Charnier 		case 'n':
2246eb925f8SKirk McKusick 			found_arg++;
22577edab90SPhilippe Charnier 			name = "soft updates";
22677edab90SPhilippe Charnier 			nvalue = optarg;
22776444424SBruce Evans 			if (strcmp(nvalue, "enable") != 0 &&
22876444424SBruce Evans 			    strcmp(nvalue, "disable") != 0) {
22977edab90SPhilippe Charnier 				errx(10, "bad %s (options are %s)",
23077edab90SPhilippe Charnier 				    name, "`enable' or `disable'");
23177edab90SPhilippe Charnier 			}
23277edab90SPhilippe Charnier 			nflag = 1;
23377edab90SPhilippe Charnier 			break;
23429f9611dSBruce Evans 
23577edab90SPhilippe Charnier 		case 'o':
2366eb925f8SKirk McKusick 			found_arg++;
23777edab90SPhilippe Charnier 			name = "optimization preference";
23876444424SBruce Evans 			if (strcmp(optarg, "space") == 0)
23977edab90SPhilippe Charnier 				ovalue = FS_OPTSPACE;
24076444424SBruce Evans 			else if (strcmp(optarg, "time") == 0)
24177edab90SPhilippe Charnier 				ovalue = FS_OPTTIME;
24277edab90SPhilippe Charnier 			else
24329f9611dSBruce Evans 				errx(10,
24429f9611dSBruce Evans 				    "bad %s (options are `space' or `time')",
24577edab90SPhilippe Charnier 				    name);
24677edab90SPhilippe Charnier 			oflag = 1;
24777edab90SPhilippe Charnier 			break;
24829f9611dSBruce Evans 
24977edab90SPhilippe Charnier 		case 'p':
2506eb925f8SKirk McKusick 			found_arg++;
25177edab90SPhilippe Charnier 			pflag = 1;
25277edab90SPhilippe Charnier 			break;
25329f9611dSBruce Evans 
254a61ab64aSKirk McKusick 		case 's':
2556eb925f8SKirk McKusick 			found_arg++;
256a61ab64aSKirk McKusick 			name = "expected number of files per directory";
257a61ab64aSKirk McKusick 			svalue = atoi(optarg);
258a61ab64aSKirk McKusick 			if (svalue < 1)
25929f9611dSBruce Evans 				errx(10, "%s must be >= 1 (was %s)",
26029f9611dSBruce Evans 				    name, optarg);
261a61ab64aSKirk McKusick 			sflag = 1;
262a61ab64aSKirk McKusick 			break;
26329f9611dSBruce Evans 
264113db2ddSJeff Roberson 		case 'S':
2656eb925f8SKirk McKusick 			found_arg++;
266113db2ddSJeff Roberson 			name = "Softdep Journal Size";
267113db2ddSJeff Roberson 			Svalue = atoi(optarg);
268113db2ddSJeff Roberson 			if (Svalue < SUJ_MIN)
269113db2ddSJeff Roberson 				errx(10, "%s must be >= %d (was %s)",
270113db2ddSJeff Roberson 				    name, SUJ_MIN, optarg);
271113db2ddSJeff Roberson 			break;
272113db2ddSJeff Roberson 
273a738d4cfSKonstantin Belousov 		case 't':
2746eb925f8SKirk McKusick 			found_arg++;
275a738d4cfSKonstantin Belousov 			name = "trim";
276a738d4cfSKonstantin Belousov 			tvalue = optarg;
277a738d4cfSKonstantin Belousov 			if (strcmp(tvalue, "enable") != 0 &&
278a738d4cfSKonstantin Belousov 			    strcmp(tvalue, "disable") != 0) {
279a738d4cfSKonstantin Belousov 				errx(10, "bad %s (options are %s)",
280a738d4cfSKonstantin Belousov 				    name, "`enable' or `disable'");
281a738d4cfSKonstantin Belousov 			}
282a738d4cfSKonstantin Belousov 			tflag = 1;
283a738d4cfSKonstantin Belousov 			break;
284a738d4cfSKonstantin Belousov 
28577edab90SPhilippe Charnier 		default:
28677edab90SPhilippe Charnier 			usage();
28777edab90SPhilippe Charnier 		}
28877edab90SPhilippe Charnier 	argc -= optind;
28977edab90SPhilippe Charnier 	argv += optind;
29077edab90SPhilippe Charnier 	if (found_arg == 0 || argc != 1)
29177edab90SPhilippe Charnier 		usage();
29277edab90SPhilippe Charnier 
293906c312bSKirk McKusick 	special = argv[0];
294907db4ddSJuli Mallett 	if (ufs_disk_fillout(&disk, special) == -1)
295907db4ddSJuli Mallett 		goto err;
2966eb925f8SKirk McKusick 	/*
2976eb925f8SKirk McKusick 	 * Check for unclean filesystem.
2986eb925f8SKirk McKusick 	 */
2996eb925f8SKirk McKusick 	if ((sblock.fs_clean == 0 ||
3006eb925f8SKirk McKusick 	    (sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0) &&
3016eb925f8SKirk McKusick 	    (found_arg > 1 || !pflag))
3026eb925f8SKirk McKusick 		errx(1, "%s is not clean - run fsck.\n", special);
30377edab90SPhilippe Charnier 	if (pflag) {
30477edab90SPhilippe Charnier 		printfs();
30577edab90SPhilippe Charnier 		exit(0);
30677edab90SPhilippe Charnier 	}
307c715b047SGordon Tetlow 	if (Lflag) {
308c715b047SGordon Tetlow 		name = "volume label";
309f9c82108SEd Maste 		strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
310c715b047SGordon Tetlow 	}
311289e09eeSRobert Watson 	if (aflag) {
3129340fc72SEdward Tomasz Napierala 		name = "POSIX.1e ACLs";
313289e09eeSRobert Watson 		if (strcmp(avalue, "enable") == 0) {
314289e09eeSRobert Watson 			if (sblock.fs_flags & FS_ACLS) {
315289e09eeSRobert Watson 				warnx("%s remains unchanged as enabled", name);
3169340fc72SEdward Tomasz Napierala 			} else if (sblock.fs_flags & FS_NFS4ACLS) {
3179340fc72SEdward Tomasz Napierala 				warnx("%s and NFSv4 ACLs are mutually "
3189340fc72SEdward Tomasz Napierala 				    "exclusive", name);
319289e09eeSRobert Watson 			} else {
320289e09eeSRobert Watson 				sblock.fs_flags |= FS_ACLS;
321289e09eeSRobert Watson 				warnx("%s set", name);
322289e09eeSRobert Watson 			}
323289e09eeSRobert Watson 		} else if (strcmp(avalue, "disable") == 0) {
324289e09eeSRobert Watson 			if ((~sblock.fs_flags & FS_ACLS) ==
325289e09eeSRobert Watson 			    FS_ACLS) {
326289e09eeSRobert Watson 				warnx("%s remains unchanged as disabled",
327289e09eeSRobert Watson 				    name);
328289e09eeSRobert Watson 			} else {
329289e09eeSRobert Watson 				sblock.fs_flags &= ~FS_ACLS;
330273500c2SRobert Watson 				warnx("%s cleared", name);
331289e09eeSRobert Watson 			}
332289e09eeSRobert Watson 		}
333289e09eeSRobert Watson 	}
33477edab90SPhilippe Charnier 	if (eflag) {
3352af14b60SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
33676444424SBruce Evans 		if (sblock.fs_maxbpg == evalue)
33777edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, evalue);
33877edab90SPhilippe Charnier 		else {
33977edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
34077edab90SPhilippe Charnier 			    name, sblock.fs_maxbpg, evalue);
34177edab90SPhilippe Charnier 			sblock.fs_maxbpg = evalue;
34277edab90SPhilippe Charnier 		}
34377edab90SPhilippe Charnier 	}
344a61ab64aSKirk McKusick 	if (fflag) {
345a61ab64aSKirk McKusick 		name = "average file size";
346a6cc0cf6SKirk McKusick 		if (sblock.fs_avgfilesize == (unsigned)fvalue) {
347a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, fvalue);
348a61ab64aSKirk McKusick 		}
349a61ab64aSKirk McKusick 		else {
350a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
351a61ab64aSKirk McKusick 					name, sblock.fs_avgfilesize, fvalue);
352a61ab64aSKirk McKusick 			sblock.fs_avgfilesize = fvalue;
353a61ab64aSKirk McKusick 		}
354a61ab64aSKirk McKusick 	}
355113db2ddSJeff Roberson 	if (jflag) {
356113db2ddSJeff Roberson  		name = "soft updates journaling";
357113db2ddSJeff Roberson  		if (strcmp(jvalue, "enable") == 0) {
358113db2ddSJeff Roberson 			if ((sblock.fs_flags & (FS_DOSOFTDEP | FS_SUJ)) ==
359113db2ddSJeff Roberson 			    (FS_DOSOFTDEP | FS_SUJ)) {
360113db2ddSJeff Roberson 				warnx("%s remains unchanged as enabled", name);
361113db2ddSJeff Roberson 			} else if (sblock.fs_clean == 0) {
362113db2ddSJeff Roberson 				warnx("%s cannot be enabled until fsck is run",
363113db2ddSJeff Roberson 				    name);
364113db2ddSJeff Roberson 			} else if (journal_alloc(Svalue) != 0) {
365113db2ddSJeff Roberson 				warnx("%s cannot be enabled", name);
366113db2ddSJeff Roberson 			} else {
367113db2ddSJeff Roberson  				sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
368113db2ddSJeff Roberson  				warnx("%s set", name);
369113db2ddSJeff Roberson 			}
370113db2ddSJeff Roberson  		} else if (strcmp(jvalue, "disable") == 0) {
371113db2ddSJeff Roberson 			if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
372113db2ddSJeff Roberson 				warnx("%s remains unchanged as disabled", name);
373113db2ddSJeff Roberson 			} else {
374113db2ddSJeff Roberson 				journal_clear();
37553053595SJeff Roberson  				sblock.fs_flags &= ~FS_SUJ;
376113db2ddSJeff Roberson 				sblock.fs_sujfree = 0;
37753053595SJeff Roberson  				warnx("%s cleared but soft updates still set.",
37853053595SJeff Roberson 				    name);
37953053595SJeff Roberson 
38053053595SJeff Roberson 				warnx("remove .sujournal to reclaim space");
381113db2ddSJeff Roberson 			}
382113db2ddSJeff Roberson  		}
383113db2ddSJeff Roberson 	}
384868c68edSPawel Jakub Dawidek 	if (Jflag) {
385868c68edSPawel Jakub Dawidek 		name = "gjournal";
386868c68edSPawel Jakub Dawidek 		if (strcmp(Jvalue, "enable") == 0) {
387868c68edSPawel Jakub Dawidek 			if (sblock.fs_flags & FS_GJOURNAL) {
388868c68edSPawel Jakub Dawidek 				warnx("%s remains unchanged as enabled", name);
389868c68edSPawel Jakub Dawidek 			} else {
390868c68edSPawel Jakub Dawidek 				sblock.fs_flags |= FS_GJOURNAL;
391868c68edSPawel Jakub Dawidek 				warnx("%s set", name);
392868c68edSPawel Jakub Dawidek 			}
393868c68edSPawel Jakub Dawidek 		} else if (strcmp(Jvalue, "disable") == 0) {
394868c68edSPawel Jakub Dawidek 			if ((~sblock.fs_flags & FS_GJOURNAL) ==
395868c68edSPawel Jakub Dawidek 			    FS_GJOURNAL) {
396868c68edSPawel Jakub Dawidek 				warnx("%s remains unchanged as disabled",
397868c68edSPawel Jakub Dawidek 				    name);
398868c68edSPawel Jakub Dawidek 			} else {
399868c68edSPawel Jakub Dawidek 				sblock.fs_flags &= ~FS_GJOURNAL;
400868c68edSPawel Jakub Dawidek 				warnx("%s cleared", name);
401868c68edSPawel Jakub Dawidek 			}
402868c68edSPawel Jakub Dawidek 		}
403868c68edSPawel Jakub Dawidek 	}
404baa12a84SKirk McKusick 	if (kflag) {
405baa12a84SKirk McKusick 		name = "space to hold for metadata blocks";
406baa12a84SKirk McKusick 		if (sblock.fs_metaspace == kvalue)
407baa12a84SKirk McKusick 			warnx("%s remains unchanged as %d", name, kvalue);
408baa12a84SKirk McKusick 		else {
409baa12a84SKirk McKusick 			kvalue = blknum(&sblock, kvalue);
410baa12a84SKirk McKusick 			if (kvalue > sblock.fs_fpg / 2) {
411baa12a84SKirk McKusick 				kvalue = blknum(&sblock, sblock.fs_fpg / 2);
412baa12a84SKirk McKusick 				warnx("%s cannot exceed half the file system "
413baa12a84SKirk McKusick 				    "space", name);
414baa12a84SKirk McKusick 			}
415baa12a84SKirk McKusick 			warnx("%s changes from %jd to %d",
416baa12a84SKirk McKusick 				    name, sblock.fs_metaspace, kvalue);
417baa12a84SKirk McKusick 			sblock.fs_metaspace = kvalue;
418baa12a84SKirk McKusick 		}
419baa12a84SKirk McKusick 	}
420289e09eeSRobert Watson 	if (lflag) {
421289e09eeSRobert Watson 		name = "multilabel";
422289e09eeSRobert Watson 		if (strcmp(lvalue, "enable") == 0) {
423289e09eeSRobert Watson 			if (sblock.fs_flags & FS_MULTILABEL) {
424289e09eeSRobert Watson 				warnx("%s remains unchanged as enabled", name);
425289e09eeSRobert Watson 			} else {
426289e09eeSRobert Watson 				sblock.fs_flags |= FS_MULTILABEL;
427289e09eeSRobert Watson 				warnx("%s set", name);
428289e09eeSRobert Watson 			}
429289e09eeSRobert Watson 		} else if (strcmp(lvalue, "disable") == 0) {
430289e09eeSRobert Watson 			if ((~sblock.fs_flags & FS_MULTILABEL) ==
431289e09eeSRobert Watson 			    FS_MULTILABEL) {
432289e09eeSRobert Watson 				warnx("%s remains unchanged as disabled",
433289e09eeSRobert Watson 				    name);
434289e09eeSRobert Watson 			} else {
435289e09eeSRobert Watson 				sblock.fs_flags &= ~FS_MULTILABEL;
436273500c2SRobert Watson 				warnx("%s cleared", name);
437289e09eeSRobert Watson 			}
438289e09eeSRobert Watson 		}
439289e09eeSRobert Watson 	}
44077edab90SPhilippe Charnier 	if (mflag) {
4418fae3551SRodney W. Grimes 		name = "minimum percentage of free space";
44276444424SBruce Evans 		if (sblock.fs_minfree == mvalue)
44377edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d%%", name, mvalue);
44477edab90SPhilippe Charnier 		else {
4458fae3551SRodney W. Grimes 			warnx("%s changes from %d%% to %d%%",
44677edab90SPhilippe Charnier 				    name, sblock.fs_minfree, mvalue);
44777edab90SPhilippe Charnier 			sblock.fs_minfree = mvalue;
44877edab90SPhilippe Charnier 			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
4498fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
45077edab90SPhilippe Charnier 			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
4518fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
452b1897c19SJulian Elischer 		}
45377edab90SPhilippe Charnier 	}
4549340fc72SEdward Tomasz Napierala 	if (Nflag) {
4559340fc72SEdward Tomasz Napierala 		name = "NFSv4 ACLs";
4569340fc72SEdward Tomasz Napierala 		if (strcmp(Nvalue, "enable") == 0) {
4579340fc72SEdward Tomasz Napierala 			if (sblock.fs_flags & FS_NFS4ACLS) {
4589340fc72SEdward Tomasz Napierala 				warnx("%s remains unchanged as enabled", name);
4599340fc72SEdward Tomasz Napierala 			} else if (sblock.fs_flags & FS_ACLS) {
4609340fc72SEdward Tomasz Napierala 				warnx("%s and POSIX.1e ACLs are mutually "
4619340fc72SEdward Tomasz Napierala 				    "exclusive", name);
4629340fc72SEdward Tomasz Napierala 			} else {
4639340fc72SEdward Tomasz Napierala 				sblock.fs_flags |= FS_NFS4ACLS;
4649340fc72SEdward Tomasz Napierala 				warnx("%s set", name);
4659340fc72SEdward Tomasz Napierala 			}
4669340fc72SEdward Tomasz Napierala 		} else if (strcmp(Nvalue, "disable") == 0) {
4679340fc72SEdward Tomasz Napierala 			if ((~sblock.fs_flags & FS_NFS4ACLS) ==
4689340fc72SEdward Tomasz Napierala 			    FS_NFS4ACLS) {
4699340fc72SEdward Tomasz Napierala 				warnx("%s remains unchanged as disabled",
4709340fc72SEdward Tomasz Napierala 				    name);
4719340fc72SEdward Tomasz Napierala 			} else {
4729340fc72SEdward Tomasz Napierala 				sblock.fs_flags &= ~FS_NFS4ACLS;
4739340fc72SEdward Tomasz Napierala 				warnx("%s cleared", name);
4749340fc72SEdward Tomasz Napierala 			}
4759340fc72SEdward Tomasz Napierala 		}
4769340fc72SEdward Tomasz Napierala 	}
47777edab90SPhilippe Charnier 	if (nflag) {
47877edab90SPhilippe Charnier  		name = "soft updates";
47977edab90SPhilippe Charnier  		if (strcmp(nvalue, "enable") == 0) {
48076444424SBruce Evans 			if (sblock.fs_flags & FS_DOSOFTDEP)
48177edab90SPhilippe Charnier 				warnx("%s remains unchanged as enabled", name);
48276444424SBruce Evans 			else if (sblock.fs_clean == 0) {
4831c2665d8SKirk McKusick 				warnx("%s cannot be enabled until fsck is run",
4841c2665d8SKirk McKusick 				    name);
48577edab90SPhilippe Charnier 			} else {
48677edab90SPhilippe Charnier  				sblock.fs_flags |= FS_DOSOFTDEP;
48777edab90SPhilippe Charnier  				warnx("%s set", name);
48877edab90SPhilippe Charnier 			}
48977edab90SPhilippe Charnier  		} else if (strcmp(nvalue, "disable") == 0) {
49076444424SBruce Evans 			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
49177edab90SPhilippe Charnier 				warnx("%s remains unchanged as disabled", name);
49276444424SBruce Evans 			else {
49377edab90SPhilippe Charnier  				sblock.fs_flags &= ~FS_DOSOFTDEP;
49477edab90SPhilippe Charnier  				warnx("%s cleared", name);
49577edab90SPhilippe Charnier 			}
49677edab90SPhilippe Charnier  		}
49777edab90SPhilippe Charnier 	}
49877edab90SPhilippe Charnier 	if (oflag) {
4998fae3551SRodney W. Grimes 		name = "optimization preference";
5008fae3551SRodney W. Grimes 		chg[FS_OPTSPACE] = "space";
5018fae3551SRodney W. Grimes 		chg[FS_OPTTIME] = "time";
50276444424SBruce Evans 		if (sblock.fs_optim == ovalue)
50377edab90SPhilippe Charnier 			warnx("%s remains unchanged as %s", name, chg[ovalue]);
50477edab90SPhilippe Charnier 		else {
5058fae3551SRodney W. Grimes 			warnx("%s changes from %s to %s",
50677edab90SPhilippe Charnier 				    name, chg[sblock.fs_optim], chg[ovalue]);
50777edab90SPhilippe Charnier 			sblock.fs_optim = ovalue;
50877edab90SPhilippe Charnier 			if (sblock.fs_minfree >= MINFREE &&
50977edab90SPhilippe Charnier 			    ovalue == FS_OPTSPACE)
5108fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
51176444424SBruce Evans 			if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME)
5128fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
5138fae3551SRodney W. Grimes 		}
51477edab90SPhilippe Charnier 	}
515a61ab64aSKirk McKusick 	if (sflag) {
516a61ab64aSKirk McKusick 		name = "expected number of files per directory";
517a6cc0cf6SKirk McKusick 		if (sblock.fs_avgfpdir == (unsigned)svalue) {
518a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, svalue);
519a61ab64aSKirk McKusick 		}
520a61ab64aSKirk McKusick 		else {
521a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
522a61ab64aSKirk McKusick 					name, sblock.fs_avgfpdir, svalue);
523a61ab64aSKirk McKusick 			sblock.fs_avgfpdir = svalue;
524a61ab64aSKirk McKusick 		}
525a61ab64aSKirk McKusick 	}
526a738d4cfSKonstantin Belousov 	if (tflag) {
527a738d4cfSKonstantin Belousov 		name = "issue TRIM to the disk";
528a738d4cfSKonstantin Belousov  		if (strcmp(tvalue, "enable") == 0) {
529a738d4cfSKonstantin Belousov 			if (sblock.fs_flags & FS_TRIM)
530a738d4cfSKonstantin Belousov 				warnx("%s remains unchanged as enabled", name);
531a738d4cfSKonstantin Belousov 			else {
532a738d4cfSKonstantin Belousov  				sblock.fs_flags |= FS_TRIM;
533a738d4cfSKonstantin Belousov  				warnx("%s set", name);
534a738d4cfSKonstantin Belousov 			}
535a738d4cfSKonstantin Belousov  		} else if (strcmp(tvalue, "disable") == 0) {
536a738d4cfSKonstantin Belousov 			if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
537a738d4cfSKonstantin Belousov 				warnx("%s remains unchanged as disabled", name);
538a738d4cfSKonstantin Belousov 			else {
539a738d4cfSKonstantin Belousov  				sblock.fs_flags &= ~FS_TRIM;
540a738d4cfSKonstantin Belousov  				warnx("%s cleared", name);
541a738d4cfSKonstantin Belousov 			}
542a738d4cfSKonstantin Belousov  		}
543a738d4cfSKonstantin Belousov 	}
5442af14b60SPhilippe Charnier 
545907db4ddSJuli Mallett 	if (sbwrite(&disk, Aflag) == -1)
546907db4ddSJuli Mallett 		goto err;
547906c312bSKirk McKusick 	diskname = strdup(disk.d_name);
548907db4ddSJuli Mallett 	ufs_disk_close(&disk);
549906c312bSKirk McKusick 	chkdoreload(getmntpoint(diskname), warnx);
5508fae3551SRodney W. Grimes 	exit(0);
551907db4ddSJuli Mallett err:
552907db4ddSJuli Mallett 	if (disk.d_error != NULL)
553907db4ddSJuli Mallett 		errx(11, "%s: %s", special, disk.d_error);
554907db4ddSJuli Mallett 	else
555907db4ddSJuli Mallett 		err(12, "%s", special);
5568fae3551SRodney W. Grimes }
5578fae3551SRodney W. Grimes 
55805d43d98SEdward Tomasz Napierala static void
sbdirty(void)559113db2ddSJeff Roberson sbdirty(void)
560113db2ddSJeff Roberson {
561113db2ddSJeff Roberson 	disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
562113db2ddSJeff Roberson 	disk.d_fs.fs_clean = 0;
563113db2ddSJeff Roberson }
564113db2ddSJeff Roberson 
565113db2ddSJeff Roberson static ufs2_daddr_t
journal_balloc(void)566113db2ddSJeff Roberson journal_balloc(void)
567113db2ddSJeff Roberson {
568113db2ddSJeff Roberson 	ufs2_daddr_t blk;
569113db2ddSJeff Roberson 	struct cg *cgp;
570113db2ddSJeff Roberson 	int valid;
571113db2ddSJeff Roberson 	static int contig = 1;
572113db2ddSJeff Roberson 
573113db2ddSJeff Roberson 	cgp = &disk.d_cg;
574113db2ddSJeff Roberson 	for (;;) {
575113db2ddSJeff Roberson 		blk = cgballoc(&disk);
576113db2ddSJeff Roberson 		if (blk > 0)
577113db2ddSJeff Roberson 			break;
578113db2ddSJeff Roberson 		/*
579113db2ddSJeff Roberson 		 * If we failed to allocate a block from this cg, move to
580113db2ddSJeff Roberson 		 * the next.
581113db2ddSJeff Roberson 		 */
582113db2ddSJeff Roberson 		if (cgwrite(&disk) < 0) {
583113db2ddSJeff Roberson 			warn("Failed to write updated cg");
584113db2ddSJeff Roberson 			return (-1);
585113db2ddSJeff Roberson 		}
586113db2ddSJeff Roberson 		while ((valid = cgread(&disk)) == 1) {
587113db2ddSJeff Roberson 			/*
588113db2ddSJeff Roberson 			 * Try to minimize fragmentation by requiring a minimum
589113db2ddSJeff Roberson 			 * number of blocks present.
590113db2ddSJeff Roberson 			 */
59153053595SJeff Roberson 			if (cgp->cg_cs.cs_nbfree > 256 * 1024)
592113db2ddSJeff Roberson 				break;
593113db2ddSJeff Roberson 			if (contig == 0 && cgp->cg_cs.cs_nbfree)
594113db2ddSJeff Roberson 				break;
595113db2ddSJeff Roberson 		}
596113db2ddSJeff Roberson 		if (valid)
597113db2ddSJeff Roberson 			continue;
598113db2ddSJeff Roberson 		/*
599113db2ddSJeff Roberson 		 * Try once through looking only for large contiguous regions
600113db2ddSJeff Roberson 		 * and again taking any space we can find.
601113db2ddSJeff Roberson 		 */
602113db2ddSJeff Roberson 		if (contig) {
603113db2ddSJeff Roberson 			contig = 0;
604113db2ddSJeff Roberson 			disk.d_ccg = 0;
605113db2ddSJeff Roberson 			warnx("Journal file fragmented.");
606113db2ddSJeff Roberson 			continue;
607113db2ddSJeff Roberson 		}
608113db2ddSJeff Roberson 		warnx("Failed to find sufficient free blocks for the journal");
609113db2ddSJeff Roberson 		return -1;
610113db2ddSJeff Roberson 	}
611113db2ddSJeff Roberson 	if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
612113db2ddSJeff Roberson 	    sblock.fs_bsize) <= 0) {
613113db2ddSJeff Roberson 		warn("Failed to initialize new block");
614113db2ddSJeff Roberson 		return -1;
615113db2ddSJeff Roberson 	}
616113db2ddSJeff Roberson 	return (blk);
617113db2ddSJeff Roberson }
618113db2ddSJeff Roberson 
619113db2ddSJeff Roberson /*
620113db2ddSJeff Roberson  * Search a directory block for the SUJ_FILE.
621113db2ddSJeff Roberson  */
622113db2ddSJeff Roberson static ino_t
dir_search(ufs2_daddr_t blk,int bytes)623113db2ddSJeff Roberson dir_search(ufs2_daddr_t blk, int bytes)
624113db2ddSJeff Roberson {
625113db2ddSJeff Roberson 	char block[MAXBSIZE];
626113db2ddSJeff Roberson 	struct direct *dp;
627113db2ddSJeff Roberson 	int off;
628113db2ddSJeff Roberson 
629113db2ddSJeff Roberson 	if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
630113db2ddSJeff Roberson 		warn("Failed to read dir block");
631113db2ddSJeff Roberson 		return (-1);
632113db2ddSJeff Roberson 	}
633113db2ddSJeff Roberson 	for (off = 0; off < bytes; off += dp->d_reclen) {
634113db2ddSJeff Roberson 		dp = (struct direct *)&block[off];
635113db2ddSJeff Roberson 		if (dp->d_reclen == 0)
636113db2ddSJeff Roberson 			break;
637113db2ddSJeff Roberson 		if (dp->d_ino == 0)
638113db2ddSJeff Roberson 			continue;
639113db2ddSJeff Roberson 		if (dp->d_namlen != strlen(SUJ_FILE))
640113db2ddSJeff Roberson 			continue;
641113db2ddSJeff Roberson 		if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
642113db2ddSJeff Roberson 			continue;
643113db2ddSJeff Roberson 		return (dp->d_ino);
644113db2ddSJeff Roberson 	}
645113db2ddSJeff Roberson 
646113db2ddSJeff Roberson 	return (0);
647113db2ddSJeff Roberson }
648113db2ddSJeff Roberson 
649113db2ddSJeff Roberson /*
6501dc349abSEd Maste  * Search in the UFS_ROOTINO for the SUJ_FILE.  If it exists we can not enable
651113db2ddSJeff Roberson  * journaling.
652113db2ddSJeff Roberson  */
653113db2ddSJeff Roberson static ino_t
journal_findfile(void)654113db2ddSJeff Roberson journal_findfile(void)
655113db2ddSJeff Roberson {
6569fc5d538SKirk McKusick 	union dinodep dp;
657113db2ddSJeff Roberson 	ino_t ino;
658113db2ddSJeff Roberson 	int i;
659113db2ddSJeff Roberson 
6609fc5d538SKirk McKusick 	if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
6619fc5d538SKirk McKusick 		warn("Failed to get root inode: %s", disk.d_error);
662113db2ddSJeff Roberson 		return (-1);
663113db2ddSJeff Roberson 	}
664113db2ddSJeff Roberson 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
6659fc5d538SKirk McKusick 		if ((off_t)dp.dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
6661dc349abSEd Maste 			warnx("UFS_ROOTINO extends beyond direct blocks.");
667113db2ddSJeff Roberson 			return (-1);
668113db2ddSJeff Roberson 		}
6691dc349abSEd Maste 		for (i = 0; i < UFS_NDADDR; i++) {
6709fc5d538SKirk McKusick 			if (dp.dp1->di_db[i] == 0)
671113db2ddSJeff Roberson 				break;
6729fc5d538SKirk McKusick 			if ((ino = dir_search(dp.dp1->di_db[i],
6739fc5d538SKirk McKusick 			    sblksize(&sblock, (off_t)dp.dp1->di_size, i))) != 0)
674113db2ddSJeff Roberson 				return (ino);
675113db2ddSJeff Roberson 		}
676113db2ddSJeff Roberson 	} else {
6779fc5d538SKirk McKusick 		if ((off_t)dp.dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
6781dc349abSEd Maste 			warnx("UFS_ROOTINO extends beyond direct blocks.");
679113db2ddSJeff Roberson 			return (-1);
680113db2ddSJeff Roberson 		}
6811dc349abSEd Maste 		for (i = 0; i < UFS_NDADDR; i++) {
6829fc5d538SKirk McKusick 			if (dp.dp2->di_db[i] == 0)
683113db2ddSJeff Roberson 				break;
6849fc5d538SKirk McKusick 			if ((ino = dir_search(dp.dp2->di_db[i],
6859fc5d538SKirk McKusick 			    sblksize(&sblock, (off_t)dp.dp2->di_size, i))) != 0)
686113db2ddSJeff Roberson 				return (ino);
687113db2ddSJeff Roberson 		}
688113db2ddSJeff Roberson 	}
689113db2ddSJeff Roberson 
690113db2ddSJeff Roberson 	return (0);
691113db2ddSJeff Roberson }
692113db2ddSJeff Roberson 
693e605011aSKonstantin Belousov static void
dir_clear_block(const char * block,off_t off)6941b83e8a3SXin LI dir_clear_block(const char *block, off_t off)
695e605011aSKonstantin Belousov {
696e605011aSKonstantin Belousov 	struct direct *dp;
697e605011aSKonstantin Belousov 
698e605011aSKonstantin Belousov 	for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
699e605011aSKonstantin Belousov 		dp = (struct direct *)&block[off];
700e605011aSKonstantin Belousov 		dp->d_ino = 0;
701e605011aSKonstantin Belousov 		dp->d_reclen = DIRBLKSIZ;
702e605011aSKonstantin Belousov 		dp->d_type = DT_UNKNOWN;
703e605011aSKonstantin Belousov 	}
704e605011aSKonstantin Belousov }
705e605011aSKonstantin Belousov 
706113db2ddSJeff Roberson /*
707113db2ddSJeff Roberson  * Insert the journal at inode 'ino' into directory blk 'blk' at the first
708113db2ddSJeff Roberson  * free offset of 'off'.  DIRBLKSIZ blocks after off are initialized as
709113db2ddSJeff Roberson  * empty.
710113db2ddSJeff Roberson  */
711113db2ddSJeff Roberson static int
dir_insert(ufs2_daddr_t blk,off_t off,ino_t ino)712113db2ddSJeff Roberson dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
713113db2ddSJeff Roberson {
714113db2ddSJeff Roberson 	struct direct *dp;
715113db2ddSJeff Roberson 	char block[MAXBSIZE];
716113db2ddSJeff Roberson 
717113db2ddSJeff Roberson 	if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
718113db2ddSJeff Roberson 		warn("Failed to read dir block");
719113db2ddSJeff Roberson 		return (-1);
720113db2ddSJeff Roberson 	}
721113db2ddSJeff Roberson 	bzero(&block[off], sblock.fs_bsize - off);
722113db2ddSJeff Roberson 	dp = (struct direct *)&block[off];
723113db2ddSJeff Roberson 	dp->d_ino = ino;
724113db2ddSJeff Roberson 	dp->d_reclen = DIRBLKSIZ;
725113db2ddSJeff Roberson 	dp->d_type = DT_REG;
726113db2ddSJeff Roberson 	dp->d_namlen = strlen(SUJ_FILE);
727113db2ddSJeff Roberson 	bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
728e605011aSKonstantin Belousov 	dir_clear_block(block, off + DIRBLKSIZ);
729113db2ddSJeff Roberson 	if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
730113db2ddSJeff Roberson 		warn("Failed to write dir block");
731113db2ddSJeff Roberson 		return (-1);
732113db2ddSJeff Roberson 	}
733113db2ddSJeff Roberson 	return (0);
734113db2ddSJeff Roberson }
735113db2ddSJeff Roberson 
736113db2ddSJeff Roberson /*
737113db2ddSJeff Roberson  * Extend a directory block in 'blk' by copying it to a full size block
738113db2ddSJeff Roberson  * and inserting the new journal inode into .sujournal.
739113db2ddSJeff Roberson  */
740113db2ddSJeff Roberson static int
dir_extend(ufs2_daddr_t blk,ufs2_daddr_t nblk,off_t size,ino_t ino)741113db2ddSJeff Roberson dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
742113db2ddSJeff Roberson {
743113db2ddSJeff Roberson 	char block[MAXBSIZE];
744113db2ddSJeff Roberson 
745e605011aSKonstantin Belousov 	if (bread(&disk, fsbtodb(&sblock, blk), block,
746e605011aSKonstantin Belousov 	    roundup(size, sblock.fs_fsize)) <= 0) {
747113db2ddSJeff Roberson 		warn("Failed to read dir block");
748113db2ddSJeff Roberson 		return (-1);
749113db2ddSJeff Roberson 	}
750e605011aSKonstantin Belousov 	dir_clear_block(block, size);
751e605011aSKonstantin Belousov 	if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
752e605011aSKonstantin Belousov 	    <= 0) {
753113db2ddSJeff Roberson 		warn("Failed to write dir block");
754113db2ddSJeff Roberson 		return (-1);
755113db2ddSJeff Roberson 	}
756113db2ddSJeff Roberson 
757e605011aSKonstantin Belousov 	return (dir_insert(nblk, size, ino));
758113db2ddSJeff Roberson }
759113db2ddSJeff Roberson 
760113db2ddSJeff Roberson /*
7611dc349abSEd Maste  * Insert the journal file into the UFS_ROOTINO directory.  We always extend the
762113db2ddSJeff Roberson  * last frag
763113db2ddSJeff Roberson  */
764113db2ddSJeff Roberson static int
journal_insertfile(ino_t ino)765113db2ddSJeff Roberson journal_insertfile(ino_t ino)
766113db2ddSJeff Roberson {
7679fc5d538SKirk McKusick 	union dinodep dp;
768113db2ddSJeff Roberson 	ufs2_daddr_t nblk;
769113db2ddSJeff Roberson 	ufs2_daddr_t blk;
770113db2ddSJeff Roberson 	ufs_lbn_t lbn;
771113db2ddSJeff Roberson 	int size;
772113db2ddSJeff Roberson 	int off;
773113db2ddSJeff Roberson 
7749fc5d538SKirk McKusick 	if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
7759fc5d538SKirk McKusick 		warn("Failed to get root inode: %s", disk.d_error);
776113db2ddSJeff Roberson 		sbdirty();
777113db2ddSJeff Roberson 		return (-1);
778113db2ddSJeff Roberson 	}
779113db2ddSJeff Roberson 	blk = 0;
780113db2ddSJeff Roberson 	size = 0;
781113db2ddSJeff Roberson 	nblk = journal_balloc();
782113db2ddSJeff Roberson 	if (nblk <= 0)
783113db2ddSJeff Roberson 		return (-1);
784113db2ddSJeff Roberson 	/*
7851dc349abSEd Maste 	 * For simplicity sake we aways extend the UFS_ROOTINO into a new
786113db2ddSJeff Roberson 	 * directory block rather than searching for space and inserting
787113db2ddSJeff Roberson 	 * into an existing block.  However, if the rootino has frags
788113db2ddSJeff Roberson 	 * have to free them and extend the block.
789113db2ddSJeff Roberson 	 */
790113db2ddSJeff Roberson 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
7919fc5d538SKirk McKusick 		lbn = lblkno(&sblock, dp.dp1->di_size);
7929fc5d538SKirk McKusick 		off = blkoff(&sblock, dp.dp1->di_size);
7939fc5d538SKirk McKusick 		blk = dp.dp1->di_db[lbn];
7949fc5d538SKirk McKusick 		size = sblksize(&sblock, (off_t)dp.dp1->di_size, lbn);
795113db2ddSJeff Roberson 	} else {
7969fc5d538SKirk McKusick 		lbn = lblkno(&sblock, dp.dp2->di_size);
7979fc5d538SKirk McKusick 		off = blkoff(&sblock, dp.dp2->di_size);
7989fc5d538SKirk McKusick 		blk = dp.dp2->di_db[lbn];
7999fc5d538SKirk McKusick 		size = sblksize(&sblock, (off_t)dp.dp2->di_size, lbn);
800113db2ddSJeff Roberson 	}
801113db2ddSJeff Roberson 	if (off != 0) {
802113db2ddSJeff Roberson 		if (dir_extend(blk, nblk, off, ino) == -1)
803113db2ddSJeff Roberson 			return (-1);
804113db2ddSJeff Roberson 	} else {
805113db2ddSJeff Roberson 		blk = 0;
806113db2ddSJeff Roberson 		if (dir_insert(nblk, 0, ino) == -1)
807113db2ddSJeff Roberson 			return (-1);
808113db2ddSJeff Roberson 	}
809113db2ddSJeff Roberson 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
8109fc5d538SKirk McKusick 		dp.dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
8119fc5d538SKirk McKusick 		dp.dp1->di_db[lbn] = nblk;
8129fc5d538SKirk McKusick 		dp.dp1->di_size = lblktosize(&sblock, lbn+1);
813113db2ddSJeff Roberson 	} else {
8149fc5d538SKirk McKusick 		dp.dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
8159fc5d538SKirk McKusick 		dp.dp2->di_db[lbn] = nblk;
8169fc5d538SKirk McKusick 		dp.dp2->di_size = lblktosize(&sblock, lbn+1);
817113db2ddSJeff Roberson 	}
8189fc5d538SKirk McKusick 	if (putinode(&disk) < 0) {
8199fc5d538SKirk McKusick 		warn("Failed to write root inode: %s", disk.d_error);
820113db2ddSJeff Roberson 		return (-1);
821113db2ddSJeff Roberson 	}
822113db2ddSJeff Roberson 	if (cgwrite(&disk) < 0) {
823113db2ddSJeff Roberson 		warn("Failed to write updated cg");
824113db2ddSJeff Roberson 		sbdirty();
825113db2ddSJeff Roberson 		return (-1);
826113db2ddSJeff Roberson 	}
827113db2ddSJeff Roberson 	if (blk) {
828113db2ddSJeff Roberson 		if (cgbfree(&disk, blk, size) < 0) {
829113db2ddSJeff Roberson 			warn("Failed to write cg");
830113db2ddSJeff Roberson 			return (-1);
831113db2ddSJeff Roberson 		}
832113db2ddSJeff Roberson 	}
833113db2ddSJeff Roberson 
834113db2ddSJeff Roberson 	return (0);
835113db2ddSJeff Roberson }
836113db2ddSJeff Roberson 
837113db2ddSJeff Roberson static int
indir_fill(ufs2_daddr_t blk,int level,int * resid)838113db2ddSJeff Roberson indir_fill(ufs2_daddr_t blk, int level, int *resid)
839113db2ddSJeff Roberson {
840113db2ddSJeff Roberson 	char indirbuf[MAXBSIZE];
841113db2ddSJeff Roberson 	ufs1_daddr_t *bap1;
842113db2ddSJeff Roberson 	ufs2_daddr_t *bap2;
843113db2ddSJeff Roberson 	ufs2_daddr_t nblk;
844113db2ddSJeff Roberson 	int ncnt;
845113db2ddSJeff Roberson 	int cnt;
846113db2ddSJeff Roberson 	int i;
847113db2ddSJeff Roberson 
848113db2ddSJeff Roberson 	bzero(indirbuf, sizeof(indirbuf));
849113db2ddSJeff Roberson 	bap1 = (ufs1_daddr_t *)indirbuf;
850113db2ddSJeff Roberson 	bap2 = (void *)bap1;
851113db2ddSJeff Roberson 	cnt = 0;
852113db2ddSJeff Roberson 	for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
853113db2ddSJeff Roberson 		nblk = journal_balloc();
854113db2ddSJeff Roberson 		if (nblk <= 0)
855113db2ddSJeff Roberson 			return (-1);
856113db2ddSJeff Roberson 		cnt++;
857113db2ddSJeff Roberson 		if (sblock.fs_magic == FS_UFS1_MAGIC)
858113db2ddSJeff Roberson 			*bap1++ = nblk;
859113db2ddSJeff Roberson 		else
860113db2ddSJeff Roberson 			*bap2++ = nblk;
861113db2ddSJeff Roberson 		if (level != 0) {
862113db2ddSJeff Roberson 			ncnt = indir_fill(nblk, level - 1, resid);
863113db2ddSJeff Roberson 			if (ncnt <= 0)
864113db2ddSJeff Roberson 				return (-1);
865113db2ddSJeff Roberson 			cnt += ncnt;
866113db2ddSJeff Roberson 		} else
867113db2ddSJeff Roberson 			(*resid)--;
868113db2ddSJeff Roberson 	}
869113db2ddSJeff Roberson 	if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
870113db2ddSJeff Roberson 	    sblock.fs_bsize) <= 0) {
871113db2ddSJeff Roberson 		warn("Failed to write indirect");
872113db2ddSJeff Roberson 		return (-1);
873113db2ddSJeff Roberson 	}
874113db2ddSJeff Roberson 	return (cnt);
875113db2ddSJeff Roberson }
876113db2ddSJeff Roberson 
877113db2ddSJeff Roberson /*
878113db2ddSJeff Roberson  * Clear the flag bits so the journal can be removed.
879113db2ddSJeff Roberson  */
88005d43d98SEdward Tomasz Napierala static void
journal_clear(void)881113db2ddSJeff Roberson journal_clear(void)
882113db2ddSJeff Roberson {
8839fc5d538SKirk McKusick 	union dinodep dp;
884113db2ddSJeff Roberson 	ino_t ino;
885113db2ddSJeff Roberson 
886113db2ddSJeff Roberson 	ino = journal_findfile();
887113db2ddSJeff Roberson 	if (ino == (ino_t)-1 || ino == 0) {
888113db2ddSJeff Roberson 		warnx("Journal file does not exist");
889113db2ddSJeff Roberson 		return;
890113db2ddSJeff Roberson 	}
891e25a029eSMatthew D Fleming 	printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
8929fc5d538SKirk McKusick 	if (getinode(&disk, &dp, ino) != 0) {
8939fc5d538SKirk McKusick 		warn("Failed to get journal inode: %s", disk.d_error);
894113db2ddSJeff Roberson 		return;
895113db2ddSJeff Roberson 	}
896113db2ddSJeff Roberson 	if (sblock.fs_magic == FS_UFS1_MAGIC)
8979fc5d538SKirk McKusick 		dp.dp1->di_flags = 0;
898113db2ddSJeff Roberson 	else
8999fc5d538SKirk McKusick 		dp.dp2->di_flags = 0;
9009fc5d538SKirk McKusick 	if (putinode(&disk) < 0) {
9019fc5d538SKirk McKusick 		warn("Failed to write journal inode: %s", disk.d_error);
902113db2ddSJeff Roberson 		return;
903113db2ddSJeff Roberson 	}
904113db2ddSJeff Roberson }
905113db2ddSJeff Roberson 
90605d43d98SEdward Tomasz Napierala static int
journal_alloc(int64_t size)907113db2ddSJeff Roberson journal_alloc(int64_t size)
908113db2ddSJeff Roberson {
9099fc5d538SKirk McKusick 	union dinodep dp;
910113db2ddSJeff Roberson 	ufs2_daddr_t blk;
911113db2ddSJeff Roberson 	struct cg *cgp;
912113db2ddSJeff Roberson 	int resid;
913113db2ddSJeff Roberson 	ino_t ino;
914113db2ddSJeff Roberson 	int blks;
9158ee53ea3SKirk McKusick 	time_t utime;
916113db2ddSJeff Roberson 	int i;
917113db2ddSJeff Roberson 
918113db2ddSJeff Roberson 	cgp = &disk.d_cg;
919113db2ddSJeff Roberson 	ino = 0;
920113db2ddSJeff Roberson 
921113db2ddSJeff Roberson 	/*
922113db2ddSJeff Roberson 	 * If the journal file exists we can't allocate it.
923113db2ddSJeff Roberson 	 */
924113db2ddSJeff Roberson 	ino = journal_findfile();
92569b468b9SBjoern A. Zeeb 	if (ino == (ino_t)-1) {
92669b468b9SBjoern A. Zeeb 		warnx("journal_findfile() failed.");
927113db2ddSJeff Roberson 		return (-1);
92869b468b9SBjoern A. Zeeb 	}
929113db2ddSJeff Roberson 	if (ino > 0) {
930113db2ddSJeff Roberson 		warnx("Journal file %s already exists, please remove.",
931113db2ddSJeff Roberson 		    SUJ_FILE);
932113db2ddSJeff Roberson 		return (-1);
933113db2ddSJeff Roberson 	}
934113db2ddSJeff Roberson 	/*
935113db2ddSJeff Roberson 	 * If the user didn't supply a size pick one based on the filesystem
936113db2ddSJeff Roberson 	 * size constrained with hardcoded MIN and MAX values.  We opt for
937113db2ddSJeff Roberson 	 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
938113db2ddSJeff Roberson 	 * not less than the MIN.
939113db2ddSJeff Roberson 	 */
940113db2ddSJeff Roberson 	if (size == 0) {
941113db2ddSJeff Roberson 		size = (sblock.fs_size * sblock.fs_bsize) / 1024;
942113db2ddSJeff Roberson 		if (size / sblock.fs_fsize > sblock.fs_fpg)
943113db2ddSJeff Roberson 			size = sblock.fs_fpg * sblock.fs_fsize;
944113db2ddSJeff Roberson 		size = MAX(SUJ_MIN, size);
945886e9f06SKirk McKusick 	}
94653053595SJeff Roberson 	/* fsck does not support fragments in journal files. */
94753053595SJeff Roberson 	size = roundup(size, sblock.fs_bsize);
948113db2ddSJeff Roberson 	resid = blocks = size / sblock.fs_bsize;
949113db2ddSJeff Roberson 	if (sblock.fs_cstotal.cs_nbfree < blocks) {
950113db2ddSJeff Roberson 		warn("Insufficient free space for %jd byte journal", size);
951113db2ddSJeff Roberson 		return (-1);
952113db2ddSJeff Roberson 	}
953113db2ddSJeff Roberson 	/*
954113db2ddSJeff Roberson 	 * Find a cg with enough blocks to satisfy the journal
955113db2ddSJeff Roberson 	 * size.  Presently the journal does not span cgs.
956113db2ddSJeff Roberson 	 */
957113db2ddSJeff Roberson 	while (cgread(&disk) == 1) {
958113db2ddSJeff Roberson 		if (cgp->cg_cs.cs_nifree == 0)
959113db2ddSJeff Roberson 			continue;
960113db2ddSJeff Roberson 		ino = cgialloc(&disk);
961113db2ddSJeff Roberson 		if (ino <= 0)
962113db2ddSJeff Roberson 			break;
963e25a029eSMatthew D Fleming 		printf("Using inode %ju in cg %d for %jd byte journal\n",
964e25a029eSMatthew D Fleming 		    (uintmax_t)ino, cgp->cg_cgx, size);
9659fc5d538SKirk McKusick 		if (getinode(&disk, &dp, ino) != 0) {
9669fc5d538SKirk McKusick 			warn("Failed to get allocated inode: %s", disk.d_error);
967113db2ddSJeff Roberson 			sbdirty();
968113db2ddSJeff Roberson 			goto out;
969113db2ddSJeff Roberson 		}
970113db2ddSJeff Roberson 		/*
971113db2ddSJeff Roberson 		 * We leave fields unrelated to the number of allocated
972113db2ddSJeff Roberson 		 * blocks and size uninitialized.  This causes legacy
973113db2ddSJeff Roberson 		 * fsck implementations to clear the inode.
974113db2ddSJeff Roberson 		 */
9758ee53ea3SKirk McKusick 		time(&utime);
976113db2ddSJeff Roberson 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
9779fc5d538SKirk McKusick 			bzero(dp.dp1, sizeof(*dp.dp1));
9789fc5d538SKirk McKusick 			dp.dp1->di_size = size;
9799fc5d538SKirk McKusick 			dp.dp1->di_mode = IFREG | IREAD;
9809fc5d538SKirk McKusick 			dp.dp1->di_nlink = 1;
9819fc5d538SKirk McKusick 			dp.dp1->di_flags =
9829fc5d538SKirk McKusick 			    SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
9839fc5d538SKirk McKusick 			dp.dp1->di_atime = utime;
9849fc5d538SKirk McKusick 			dp.dp1->di_mtime = utime;
9859fc5d538SKirk McKusick 			dp.dp1->di_ctime = utime;
986113db2ddSJeff Roberson 		} else {
9879fc5d538SKirk McKusick 			bzero(dp.dp2, sizeof(*dp.dp2));
9889fc5d538SKirk McKusick 			dp.dp2->di_size = size;
9899fc5d538SKirk McKusick 			dp.dp2->di_mode = IFREG | IREAD;
9909fc5d538SKirk McKusick 			dp.dp2->di_nlink = 1;
9919fc5d538SKirk McKusick 			dp.dp2->di_flags =
9929fc5d538SKirk McKusick 			    SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
9939fc5d538SKirk McKusick 			dp.dp2->di_atime = utime;
9949fc5d538SKirk McKusick 			dp.dp2->di_mtime = utime;
9959fc5d538SKirk McKusick 			dp.dp2->di_ctime = utime;
9969fc5d538SKirk McKusick 			dp.dp2->di_birthtime = utime;
997113db2ddSJeff Roberson 		}
9981dc349abSEd Maste 		for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
999113db2ddSJeff Roberson 			blk = journal_balloc();
1000113db2ddSJeff Roberson 			if (blk <= 0)
1001113db2ddSJeff Roberson 				goto out;
1002113db2ddSJeff Roberson 			if (sblock.fs_magic == FS_UFS1_MAGIC) {
10039fc5d538SKirk McKusick 				dp.dp1->di_db[i] = blk;
10049fc5d538SKirk McKusick 				dp.dp1->di_blocks++;
1005113db2ddSJeff Roberson 			} else {
10069fc5d538SKirk McKusick 				dp.dp2->di_db[i] = blk;
10079fc5d538SKirk McKusick 				dp.dp2->di_blocks++;
1008113db2ddSJeff Roberson 			}
1009113db2ddSJeff Roberson 		}
10101dc349abSEd Maste 		for (i = 0; i < UFS_NIADDR && resid; i++) {
1011113db2ddSJeff Roberson 			blk = journal_balloc();
1012113db2ddSJeff Roberson 			if (blk <= 0)
1013113db2ddSJeff Roberson 				goto out;
1014113db2ddSJeff Roberson 			blks = indir_fill(blk, i, &resid) + 1;
1015113db2ddSJeff Roberson 			if (blks <= 0) {
1016113db2ddSJeff Roberson 				sbdirty();
1017113db2ddSJeff Roberson 				goto out;
1018113db2ddSJeff Roberson 			}
1019113db2ddSJeff Roberson 			if (sblock.fs_magic == FS_UFS1_MAGIC) {
10209fc5d538SKirk McKusick 				dp.dp1->di_ib[i] = blk;
10219fc5d538SKirk McKusick 				dp.dp1->di_blocks += blks;
1022113db2ddSJeff Roberson 			} else {
10239fc5d538SKirk McKusick 				dp.dp2->di_ib[i] = blk;
10249fc5d538SKirk McKusick 				dp.dp2->di_blocks += blks;
1025113db2ddSJeff Roberson 			}
1026113db2ddSJeff Roberson 		}
1027113db2ddSJeff Roberson 		if (sblock.fs_magic == FS_UFS1_MAGIC)
10289fc5d538SKirk McKusick 			dp.dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1029113db2ddSJeff Roberson 		else
10309fc5d538SKirk McKusick 			dp.dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
10319fc5d538SKirk McKusick 		if (putinode(&disk) < 0) {
10329fc5d538SKirk McKusick 			warn("Failed to write allocated inode: %s",
10339fc5d538SKirk McKusick 			    disk.d_error);
1034113db2ddSJeff Roberson 			sbdirty();
1035113db2ddSJeff Roberson 			return (-1);
1036113db2ddSJeff Roberson 		}
1037113db2ddSJeff Roberson 		if (cgwrite(&disk) < 0) {
1038113db2ddSJeff Roberson 			warn("Failed to write updated cg");
1039113db2ddSJeff Roberson 			sbdirty();
1040113db2ddSJeff Roberson 			return (-1);
1041113db2ddSJeff Roberson 		}
1042113db2ddSJeff Roberson 		if (journal_insertfile(ino) < 0) {
1043113db2ddSJeff Roberson 			sbdirty();
1044113db2ddSJeff Roberson 			return (-1);
1045113db2ddSJeff Roberson 		}
1046113db2ddSJeff Roberson 		sblock.fs_sujfree = 0;
1047113db2ddSJeff Roberson 		return (0);
1048113db2ddSJeff Roberson 	}
1049113db2ddSJeff Roberson 	warnx("Insufficient free space for the journal.");
1050113db2ddSJeff Roberson out:
1051113db2ddSJeff Roberson 	return (-1);
1052113db2ddSJeff Roberson }
1053113db2ddSJeff Roberson 
105405d43d98SEdward Tomasz Napierala static void
usage(void)1055b1f0fda0SJuli Mallett usage(void)
10568fae3551SRodney W. Grimes {
1057a738d4cfSKonstantin Belousov 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
10581f6a4631SRuslan Ermilov "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1059baa12a84SKirk McKusick "              [-J enable | disable] [-j enable | disable] [-k metaspace]",
1060727c1288SEdwin Groothuis "              [-L volname] [-l enable | disable] [-m minfree]",
1061727c1288SEdwin Groothuis "              [-N enable | disable] [-n enable | disable]",
1062a738d4cfSKonstantin Belousov "              [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1063a738d4cfSKonstantin Belousov "              special | filesystem");
10648fae3551SRodney W. Grimes 	exit(2);
10658fae3551SRodney W. Grimes }
10668fae3551SRodney W. Grimes 
106705d43d98SEdward Tomasz Napierala static void
printfs(void)1068b1f0fda0SJuli Mallett printfs(void)
106916a7269eSJoerg Wunsch {
10709340fc72SEdward Tomasz Napierala 	warnx("POSIX.1e ACLs: (-a)                                %s",
107181dc101cSRobert Watson 		(sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
10729340fc72SEdward Tomasz Napierala 	warnx("NFSv4 ACLs: (-N)                                   %s",
10739340fc72SEdward Tomasz Napierala 		(sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
107481dc101cSRobert Watson 	warnx("MAC multilabel: (-l)                               %s",
107581dc101cSRobert Watson 		(sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1076b1897c19SJulian Elischer 	warnx("soft updates: (-n)                                 %s",
1077b1897c19SJulian Elischer 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1078113db2ddSJeff Roberson 	warnx("soft update journaling: (-j)                       %s",
1079113db2ddSJeff Roberson 		(sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1080868c68edSPawel Jakub Dawidek 	warnx("gjournal: (-J)                                     %s",
1081868c68edSPawel Jakub Dawidek 		(sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1082a738d4cfSKonstantin Belousov 	warnx("trim: (-t)                                         %s",
1083a738d4cfSKonstantin Belousov 		(sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
108416a7269eSJoerg Wunsch 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
108516a7269eSJoerg Wunsch 	      sblock.fs_maxbpg);
1086a61ab64aSKirk McKusick 	warnx("average file size: (-f)                            %d",
1087a61ab64aSKirk McKusick 	      sblock.fs_avgfilesize);
1088a61ab64aSKirk McKusick 	warnx("average number of files in a directory: (-s)       %d",
1089a61ab64aSKirk McKusick 	      sblock.fs_avgfpdir);
109016a7269eSJoerg Wunsch 	warnx("minimum percentage of free space: (-m)             %d%%",
109116a7269eSJoerg Wunsch 	      sblock.fs_minfree);
1092baa12a84SKirk McKusick 	warnx("space to hold for metadata blocks: (-k)            %jd",
1093baa12a84SKirk McKusick 	      sblock.fs_metaspace);
109416a7269eSJoerg Wunsch 	warnx("optimization preference: (-o)                      %s",
109516a7269eSJoerg Wunsch 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
109616a7269eSJoerg Wunsch 	if (sblock.fs_minfree >= MINFREE &&
109716a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTSPACE)
109816a7269eSJoerg Wunsch 		warnx(OPTWARN, "time", ">=", MINFREE);
109916a7269eSJoerg Wunsch 	if (sblock.fs_minfree < MINFREE &&
110016a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTTIME)
110116a7269eSJoerg Wunsch 		warnx(OPTWARN, "space", "<", MINFREE);
1102c715b047SGordon Tetlow 	warnx("volume label: (-L)                                 %s",
1103c715b047SGordon Tetlow 		sblock.fs_volname);
110416a7269eSJoerg Wunsch }
1105