2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * tunefs: change layout parameters to an existing file system.
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/disklabel.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
57 #include <ufs/ufs/dir.h>
73 /* the optimization warning string template */
74 #define OPTWARN "should optimize for %s with minfree %s %d%%"
77 static char clrbuf[MAXBSIZE];
78 static struct uufsd disk;
79 #define sblock disk.d_fs
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);
88 main(int argc, char *argv[])
90 const char *avalue, *jvalue, *Jvalue, *Lvalue, *lvalue, *Nvalue, *nvalue;
92 const char *special, *on;
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;
102 struct iovec *iov = NULL;
103 char errmsg[255] = {0};
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;
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:"))
124 name = "POSIX.1e ACLs";
126 if (strcmp(avalue, "enable") &&
127 strcmp(avalue, "disable")) {
128 errx(10, "bad %s (options are %s)",
129 name, "`enable' or `disable'");
136 name = "maximum blocks per file in a cylinder group";
137 evalue = atoi(optarg);
139 errx(10, "%s must be >= 1 (was %s)",
146 name = "average file size";
147 fvalue = atoi(optarg);
149 errx(10, "%s must be >= 1 (was %s)",
156 name = "softdep journaled file system";
158 if (strcmp(jvalue, "enable") &&
159 strcmp(jvalue, "disable")) {
160 errx(10, "bad %s (options are %s)",
161 name, "`enable' or `disable'");
168 name = "gjournaled file system";
170 if (strcmp(Jvalue, "enable") &&
171 strcmp(Jvalue, "disable")) {
172 errx(10, "bad %s (options are %s)",
173 name, "`enable' or `disable'");
180 name = "space to hold for metadata blocks";
181 kvalue = atoi(optarg);
183 errx(10, "bad %s (%s)", name, optarg);
189 name = "volume label";
192 while (isalnum(Lvalue[++i]) || Lvalue[i] == '_' ||
195 if (Lvalue[i] != '\0') {
196 errx(10, "bad %s. Valid characters are "
197 "alphanumerics, dashes, and underscores.",
200 if (strlen(Lvalue) >= MAXVOLLEN) {
201 errx(10, "bad %s. Length is longer than %d.",
202 name, MAXVOLLEN - 1);
209 name = "multilabel MAC file system";
211 if (strcmp(lvalue, "enable") &&
212 strcmp(lvalue, "disable")) {
213 errx(10, "bad %s (options are %s)",
214 name, "`enable' or `disable'");
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);
232 if (strcmp(Nvalue, "enable") &&
233 strcmp(Nvalue, "disable")) {
234 errx(10, "bad %s (options are %s)",
235 name, "`enable' or `disable'");
242 name = "soft updates";
244 if (strcmp(nvalue, "enable") != 0 &&
245 strcmp(nvalue, "disable") != 0) {
246 errx(10, "bad %s (options are %s)",
247 name, "`enable' or `disable'");
254 name = "optimization preference";
255 if (strcmp(optarg, "space") == 0)
256 ovalue = FS_OPTSPACE;
257 else if (strcmp(optarg, "time") == 0)
261 "bad %s (options are `space' or `time')",
273 name = "expected number of files per directory";
274 svalue = atoi(optarg);
276 errx(10, "%s must be >= 1 (was %s)",
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);
294 if (strcmp(tvalue, "enable") != 0 &&
295 strcmp(tvalue, "disable") != 0) {
296 errx(10, "bad %s (options are %s)",
297 name, "`enable' or `disable'");
307 if (found_arg == 0 || argc != 1)
310 on = special = argv[0];
311 if (ufs_disk_fillout(&disk, special) == -1)
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)
325 name = "volume label";
326 strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
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 "
337 sblock.fs_flags |= FS_ACLS;
338 warnx("%s set", name);
340 } else if (strcmp(avalue, "disable") == 0) {
341 if ((~sblock.fs_flags & FS_ACLS) ==
343 warnx("%s remains unchanged as disabled",
346 sblock.fs_flags &= ~FS_ACLS;
347 warnx("%s cleared", name);
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);
356 warnx("%s changes from %d to %d",
357 name, sblock.fs_maxbpg, evalue);
358 sblock.fs_maxbpg = evalue;
362 name = "average file size";
363 if (sblock.fs_avgfilesize == (unsigned)fvalue) {
364 warnx("%s remains unchanged as %d", name, fvalue);
367 warnx("%s changes from %d to %d",
368 name, sblock.fs_avgfilesize, fvalue);
369 sblock.fs_avgfilesize = fvalue;
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",
381 } else if (journal_alloc(Svalue) != 0) {
382 warnx("%s cannot be enabled", name);
384 sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
385 warnx("%s set", name);
387 } else if (strcmp(jvalue, "disable") == 0) {
388 if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
389 warnx("%s remains unchanged as disabled", name);
392 sblock.fs_flags &= ~FS_SUJ;
393 sblock.fs_sujfree = 0;
394 warnx("%s cleared but soft updates still set.",
397 warnx("remove .sujournal to reclaim space");
403 if (strcmp(Jvalue, "enable") == 0) {
404 if (sblock.fs_flags & FS_GJOURNAL) {
405 warnx("%s remains unchanged as enabled", name);
407 sblock.fs_flags |= FS_GJOURNAL;
408 warnx("%s set", name);
410 } else if (strcmp(Jvalue, "disable") == 0) {
411 if ((~sblock.fs_flags & FS_GJOURNAL) ==
413 warnx("%s remains unchanged as disabled",
416 sblock.fs_flags &= ~FS_GJOURNAL;
417 warnx("%s cleared", name);
422 name = "space to hold for metadata blocks";
423 if (sblock.fs_metaspace == kvalue)
424 warnx("%s remains unchanged as %d", name, kvalue);
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 "
432 warnx("%s changes from %jd to %d",
433 name, sblock.fs_metaspace, kvalue);
434 sblock.fs_metaspace = kvalue;
439 if (strcmp(lvalue, "enable") == 0) {
440 if (sblock.fs_flags & FS_MULTILABEL) {
441 warnx("%s remains unchanged as enabled", name);
443 sblock.fs_flags |= FS_MULTILABEL;
444 warnx("%s set", name);
446 } else if (strcmp(lvalue, "disable") == 0) {
447 if ((~sblock.fs_flags & FS_MULTILABEL) ==
449 warnx("%s remains unchanged as disabled",
452 sblock.fs_flags &= ~FS_MULTILABEL;
453 warnx("%s cleared", name);
458 name = "minimum percentage of free space";
459 if (sblock.fs_minfree == mvalue)
460 warnx("%s remains unchanged as %d%%", name, mvalue);
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);
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 "
480 sblock.fs_flags |= FS_NFS4ACLS;
481 warnx("%s set", name);
483 } else if (strcmp(Nvalue, "disable") == 0) {
484 if ((~sblock.fs_flags & FS_NFS4ACLS) ==
486 warnx("%s remains unchanged as disabled",
489 sblock.fs_flags &= ~FS_NFS4ACLS;
490 warnx("%s cleared", name);
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",
503 sblock.fs_flags |= FS_DOSOFTDEP;
504 warnx("%s set", name);
506 } else if (strcmp(nvalue, "disable") == 0) {
507 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
508 warnx("%s remains unchanged as disabled", name);
510 sblock.fs_flags &= ~FS_DOSOFTDEP;
511 warnx("%s cleared", name);
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]);
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);
533 name = "expected number of files per directory";
534 if (sblock.fs_avgfpdir == (unsigned)svalue) {
535 warnx("%s remains unchanged as %d", name, svalue);
538 warnx("%s changes from %d to %d",
539 name, sblock.fs_avgfpdir, svalue);
540 sblock.fs_avgfpdir = svalue;
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);
549 sblock.fs_flags |= FS_TRIM;
550 warnx("%s set", name);
552 } else if (strcmp(tvalue, "disable") == 0) {
553 if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
554 warnx("%s remains unchanged as disabled", name);
556 sblock.fs_flags &= ~FS_TRIM;
557 warnx("%s cleared", name);
562 if (sbwrite(&disk, Aflag) == -1)
564 ufs_disk_close(&disk);
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) {
572 err(9, "%s: reload: %s", special, errmsg);
574 err(9, "%s: reload", special);
576 warnx("file system reloaded");
580 if (disk.d_error != NULL)
581 errx(11, "%s: %s", special, disk.d_error);
583 err(12, "%s", special);
589 disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
590 disk.d_fs.fs_clean = 0;
599 static int contig = 1;
603 blk = cgballoc(&disk);
607 * If we failed to allocate a block from this cg, move to
610 if (cgwrite(&disk) < 0) {
611 warn("Failed to write updated cg");
614 while ((valid = cgread(&disk)) == 1) {
616 * Try to minimize fragmentation by requiring a minimum
617 * number of blocks present.
619 if (cgp->cg_cs.cs_nbfree > 256 * 1024)
621 if (contig == 0 && cgp->cg_cs.cs_nbfree)
627 * Try once through looking only for large contiguous regions
628 * and again taking any space we can find.
633 warnx("Journal file fragmented.");
636 warnx("Failed to find sufficient free blocks for the journal");
639 if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
640 sblock.fs_bsize) <= 0) {
641 warn("Failed to initialize new block");
648 * Search a directory block for the SUJ_FILE.
651 dir_search(ufs2_daddr_t blk, int bytes)
653 char block[MAXBSIZE];
657 if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
658 warn("Failed to read dir block");
661 for (off = 0; off < bytes; off += dp->d_reclen) {
662 dp = (struct direct *)&block[off];
663 if (dp->d_reclen == 0)
667 if (dp->d_namlen != strlen(SUJ_FILE))
669 if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
678 * Search in the UFS_ROOTINO for the SUJ_FILE. If it exists we can not enable
682 journal_findfile(void)
688 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
689 warn("Failed to get root inode: %s", disk.d_error);
692 if (sblock.fs_magic == FS_UFS1_MAGIC) {
693 if ((off_t)dp.dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
694 warnx("UFS_ROOTINO extends beyond direct blocks.");
697 for (i = 0; i < UFS_NDADDR; i++) {
698 if (dp.dp1->di_db[i] == 0)
700 if ((ino = dir_search(dp.dp1->di_db[i],
701 sblksize(&sblock, (off_t)dp.dp1->di_size, i))) != 0)
705 if ((off_t)dp.dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
706 warnx("UFS_ROOTINO extends beyond direct blocks.");
709 for (i = 0; i < UFS_NDADDR; i++) {
710 if (dp.dp2->di_db[i] == 0)
712 if ((ino = dir_search(dp.dp2->di_db[i],
713 sblksize(&sblock, (off_t)dp.dp2->di_size, i))) != 0)
722 dir_clear_block(const char *block, off_t off)
726 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
727 dp = (struct direct *)&block[off];
729 dp->d_reclen = DIRBLKSIZ;
730 dp->d_type = DT_UNKNOWN;
735 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
736 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
740 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
743 char block[MAXBSIZE];
745 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
746 warn("Failed to read dir block");
749 bzero(&block[off], sblock.fs_bsize - off);
750 dp = (struct direct *)&block[off];
752 dp->d_reclen = DIRBLKSIZ;
754 dp->d_namlen = strlen(SUJ_FILE);
755 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
756 dir_clear_block(block, off + DIRBLKSIZ);
757 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
758 warn("Failed to write dir block");
765 * Extend a directory block in 'blk' by copying it to a full size block
766 * and inserting the new journal inode into .sujournal.
769 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
771 char block[MAXBSIZE];
773 if (bread(&disk, fsbtodb(&sblock, blk), block,
774 roundup(size, sblock.fs_fsize)) <= 0) {
775 warn("Failed to read dir block");
778 dir_clear_block(block, size);
779 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
781 warn("Failed to write dir block");
785 return (dir_insert(nblk, size, ino));
789 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
793 journal_insertfile(ino_t ino)
802 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
803 warn("Failed to get root inode: %s", disk.d_error);
809 nblk = journal_balloc();
813 * For simplicity sake we aways extend the UFS_ROOTINO into a new
814 * directory block rather than searching for space and inserting
815 * into an existing block. However, if the rootino has frags
816 * have to free them and extend the block.
818 if (sblock.fs_magic == FS_UFS1_MAGIC) {
819 lbn = lblkno(&sblock, dp.dp1->di_size);
820 off = blkoff(&sblock, dp.dp1->di_size);
821 blk = dp.dp1->di_db[lbn];
822 size = sblksize(&sblock, (off_t)dp.dp1->di_size, lbn);
824 lbn = lblkno(&sblock, dp.dp2->di_size);
825 off = blkoff(&sblock, dp.dp2->di_size);
826 blk = dp.dp2->di_db[lbn];
827 size = sblksize(&sblock, (off_t)dp.dp2->di_size, lbn);
830 if (dir_extend(blk, nblk, off, ino) == -1)
834 if (dir_insert(nblk, 0, ino) == -1)
837 if (sblock.fs_magic == FS_UFS1_MAGIC) {
838 dp.dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
839 dp.dp1->di_db[lbn] = nblk;
840 dp.dp1->di_size = lblktosize(&sblock, lbn+1);
842 dp.dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
843 dp.dp2->di_db[lbn] = nblk;
844 dp.dp2->di_size = lblktosize(&sblock, lbn+1);
846 if (putinode(&disk) < 0) {
847 warn("Failed to write root inode: %s", disk.d_error);
850 if (cgwrite(&disk) < 0) {
851 warn("Failed to write updated cg");
856 if (cgbfree(&disk, blk, size) < 0) {
857 warn("Failed to write cg");
866 indir_fill(ufs2_daddr_t blk, int level, int *resid)
868 char indirbuf[MAXBSIZE];
876 bzero(indirbuf, sizeof(indirbuf));
877 bap1 = (ufs1_daddr_t *)indirbuf;
880 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
881 nblk = journal_balloc();
885 if (sblock.fs_magic == FS_UFS1_MAGIC)
890 ncnt = indir_fill(nblk, level - 1, resid);
897 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
898 sblock.fs_bsize) <= 0) {
899 warn("Failed to write indirect");
906 * Clear the flag bits so the journal can be removed.
914 ino = journal_findfile();
915 if (ino == (ino_t)-1 || ino == 0) {
916 warnx("Journal file does not exist");
919 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
920 if (getinode(&disk, &dp, ino) != 0) {
921 warn("Failed to get journal inode: %s", disk.d_error);
924 if (sblock.fs_magic == FS_UFS1_MAGIC)
925 dp.dp1->di_flags = 0;
927 dp.dp2->di_flags = 0;
928 if (putinode(&disk) < 0) {
929 warn("Failed to write journal inode: %s", disk.d_error);
935 journal_alloc(int64_t size)
950 * If the journal file exists we can't allocate it.
952 ino = journal_findfile();
953 if (ino == (ino_t)-1) {
954 warnx("journal_findfile() failed.");
958 warnx("Journal file %s already exists, please remove.",
963 * If the user didn't supply a size pick one based on the filesystem
964 * size constrained with hardcoded MIN and MAX values. We opt for
965 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
966 * not less than the MIN.
969 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
970 size = MIN(SUJ_MAX, size);
971 if (size / sblock.fs_fsize > sblock.fs_fpg)
972 size = sblock.fs_fpg * sblock.fs_fsize;
973 size = MAX(SUJ_MIN, size);
975 /* fsck does not support fragments in journal files. */
976 size = roundup(size, sblock.fs_bsize);
977 resid = blocks = size / sblock.fs_bsize;
978 if (sblock.fs_cstotal.cs_nbfree < blocks) {
979 warn("Insufficient free space for %jd byte journal", size);
983 * Find a cg with enough blocks to satisfy the journal
984 * size. Presently the journal does not span cgs.
986 while (cgread(&disk) == 1) {
987 if (cgp->cg_cs.cs_nifree == 0)
989 ino = cgialloc(&disk);
992 printf("Using inode %ju in cg %d for %jd byte journal\n",
993 (uintmax_t)ino, cgp->cg_cgx, size);
994 if (getinode(&disk, &dp, ino) != 0) {
995 warn("Failed to get allocated inode: %s", disk.d_error);
1000 * We leave fields unrelated to the number of allocated
1001 * blocks and size uninitialized. This causes legacy
1002 * fsck implementations to clear the inode.
1005 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1006 bzero(dp.dp1, sizeof(*dp.dp1));
1007 dp.dp1->di_size = size;
1008 dp.dp1->di_mode = IFREG | IREAD;
1009 dp.dp1->di_nlink = 1;
1011 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1012 dp.dp1->di_atime = utime;
1013 dp.dp1->di_mtime = utime;
1014 dp.dp1->di_ctime = utime;
1016 bzero(dp.dp2, sizeof(*dp.dp2));
1017 dp.dp2->di_size = size;
1018 dp.dp2->di_mode = IFREG | IREAD;
1019 dp.dp2->di_nlink = 1;
1021 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1022 dp.dp2->di_atime = utime;
1023 dp.dp2->di_mtime = utime;
1024 dp.dp2->di_ctime = utime;
1025 dp.dp2->di_birthtime = utime;
1027 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1028 blk = journal_balloc();
1031 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1032 dp.dp1->di_db[i] = blk;
1033 dp.dp1->di_blocks++;
1035 dp.dp2->di_db[i] = blk;
1036 dp.dp2->di_blocks++;
1039 for (i = 0; i < UFS_NIADDR && resid; i++) {
1040 blk = journal_balloc();
1043 blks = indir_fill(blk, i, &resid) + 1;
1048 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1049 dp.dp1->di_ib[i] = blk;
1050 dp.dp1->di_blocks += blks;
1052 dp.dp2->di_ib[i] = blk;
1053 dp.dp2->di_blocks += blks;
1056 if (sblock.fs_magic == FS_UFS1_MAGIC)
1057 dp.dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1059 dp.dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1060 if (putinode(&disk) < 0) {
1061 warn("Failed to write allocated inode: %s",
1066 if (cgwrite(&disk) < 0) {
1067 warn("Failed to write updated cg");
1071 if (journal_insertfile(ino) < 0) {
1075 sblock.fs_sujfree = 0;
1078 warnx("Insufficient free space for the journal.");
1086 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1087 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1088 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1089 " [-L volname] [-l enable | disable] [-m minfree]",
1090 " [-N enable | disable] [-n enable | disable]",
1091 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1092 " special | filesystem");
1099 warnx("POSIX.1e ACLs: (-a) %s",
1100 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1101 warnx("NFSv4 ACLs: (-N) %s",
1102 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1103 warnx("MAC multilabel: (-l) %s",
1104 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1105 warnx("soft updates: (-n) %s",
1106 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1107 warnx("soft update journaling: (-j) %s",
1108 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1109 warnx("gjournal: (-J) %s",
1110 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1111 warnx("trim: (-t) %s",
1112 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1113 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1115 warnx("average file size: (-f) %d",
1116 sblock.fs_avgfilesize);
1117 warnx("average number of files in a directory: (-s) %d",
1118 sblock.fs_avgfpdir);
1119 warnx("minimum percentage of free space: (-m) %d%%",
1121 warnx("space to hold for metadata blocks: (-k) %jd",
1122 sblock.fs_metaspace);
1123 warnx("optimization preference: (-o) %s",
1124 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1125 if (sblock.fs_minfree >= MINFREE &&
1126 sblock.fs_optim == FS_OPTSPACE)
1127 warnx(OPTWARN, "time", ">=", MINFREE);
1128 if (sblock.fs_minfree < MINFREE &&
1129 sblock.fs_optim == FS_OPTTIME)
1130 warnx(OPTWARN, "space", "<", MINFREE);
1131 warnx("volume label: (-L) %s",