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)
684 struct ufs1_dinode *dp1;
685 struct ufs2_dinode *dp2;
691 if (getino(&disk, &ip, UFS_ROOTINO, &mode) != 0) {
692 warn("Failed to get root inode");
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.");
702 for (i = 0; i < UFS_NDADDR; i++) {
703 if (dp1->di_db[i] == 0)
705 if ((ino = dir_search(dp1->di_db[i],
706 sblksize(&sblock, (off_t)dp1->di_size, i))) != 0)
710 if ((off_t)dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
711 warnx("UFS_ROOTINO extends beyond direct blocks.");
714 for (i = 0; i < UFS_NDADDR; i++) {
715 if (dp2->di_db[i] == 0)
717 if ((ino = dir_search(dp2->di_db[i],
718 sblksize(&sblock, (off_t)dp2->di_size, i))) != 0)
727 dir_clear_block(const char *block, off_t off)
731 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
732 dp = (struct direct *)&block[off];
734 dp->d_reclen = DIRBLKSIZ;
735 dp->d_type = DT_UNKNOWN;
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
745 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
748 char block[MAXBSIZE];
750 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
751 warn("Failed to read dir block");
754 bzero(&block[off], sblock.fs_bsize - off);
755 dp = (struct direct *)&block[off];
757 dp->d_reclen = DIRBLKSIZ;
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");
770 * Extend a directory block in 'blk' by copying it to a full size block
771 * and inserting the new journal inode into .sujournal.
774 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
776 char block[MAXBSIZE];
778 if (bread(&disk, fsbtodb(&sblock, blk), block,
779 roundup(size, sblock.fs_fsize)) <= 0) {
780 warn("Failed to read dir block");
783 dir_clear_block(block, size);
784 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
786 warn("Failed to write dir block");
790 return (dir_insert(nblk, size, ino));
794 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
798 journal_insertfile(ino_t ino)
800 struct ufs1_dinode *dp1;
801 struct ufs2_dinode *dp2;
810 if (getino(&disk, &ip, UFS_ROOTINO, &mode) != 0) {
811 warn("Failed to get root inode");
819 nblk = journal_balloc();
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.
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);
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);
840 if (dir_extend(blk, nblk, off, ino) == -1)
844 if (dir_insert(nblk, 0, ino) == -1)
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);
852 dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
853 dp2->di_db[lbn] = nblk;
854 dp2->di_size = lblktosize(&sblock, lbn+1);
856 if (putino(&disk) < 0) {
857 warn("Failed to write root inode");
860 if (cgwrite(&disk) < 0) {
861 warn("Failed to write updated cg");
866 if (cgbfree(&disk, blk, size) < 0) {
867 warn("Failed to write cg");
876 indir_fill(ufs2_daddr_t blk, int level, int *resid)
878 char indirbuf[MAXBSIZE];
886 bzero(indirbuf, sizeof(indirbuf));
887 bap1 = (ufs1_daddr_t *)indirbuf;
890 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
891 nblk = journal_balloc();
895 if (sblock.fs_magic == FS_UFS1_MAGIC)
900 ncnt = indir_fill(nblk, level - 1, resid);
907 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
908 sblock.fs_bsize) <= 0) {
909 warn("Failed to write indirect");
916 * Clear the flag bits so the journal can be removed.
921 struct ufs1_dinode *dp1;
922 struct ufs2_dinode *dp2;
927 ino = journal_findfile();
928 if (ino == (ino_t)-1 || ino == 0) {
929 warnx("Journal file does not exist");
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");
939 if (sblock.fs_magic == FS_UFS1_MAGIC)
943 if (putino(&disk) < 0) {
944 warn("Failed to write journal inode");
950 journal_alloc(int64_t size)
952 struct ufs1_dinode *dp1;
953 struct ufs2_dinode *dp2;
968 * If the journal file exists we can't allocate it.
970 ino = journal_findfile();
971 if (ino == (ino_t)-1) {
972 warnx("journal_findfile() failed.");
976 warnx("Journal file %s already exists, please remove.",
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.
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);
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);
1001 * Find a cg with enough blocks to satisfy the journal
1002 * size. Presently the journal does not span cgs.
1004 while (cgread(&disk) == 1) {
1005 if (cgp->cg_cs.cs_nifree == 0)
1007 ino = cgialloc(&disk);
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");
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.
1025 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1026 bzero(dp1, sizeof(*dp1));
1027 dp1->di_size = size;
1028 dp1->di_mode = IFREG | IREAD;
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;
1035 bzero(dp2, sizeof(*dp2));
1036 dp2->di_size = size;
1037 dp2->di_mode = IFREG | IREAD;
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;
1045 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1046 blk = journal_balloc();
1049 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1050 dp1->di_db[i] = blk;
1053 dp2->di_db[i] = blk;
1057 for (i = 0; i < UFS_NIADDR && resid; i++) {
1058 blk = journal_balloc();
1061 blks = indir_fill(blk, i, &resid) + 1;
1066 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1067 dp1->di_ib[i] = blk;
1068 dp1->di_blocks += blks;
1070 dp2->di_ib[i] = blk;
1071 dp2->di_blocks += blks;
1074 if (sblock.fs_magic == FS_UFS1_MAGIC)
1075 dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1077 dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1078 if (putino(&disk) < 0) {
1079 warn("Failed to write inode");
1083 if (cgwrite(&disk) < 0) {
1084 warn("Failed to write updated cg");
1088 if (journal_insertfile(ino) < 0) {
1092 sblock.fs_sujfree = 0;
1095 warnx("Insufficient free space for the journal.");
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");
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",
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%%",
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",