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]));
193 if (Lvalue[i] != '\0') {
195 "bad %s. Valid characters are alphanumerics.",
198 if (strlen(Lvalue) >= MAXVOLLEN) {
199 errx(10, "bad %s. Length is longer than %d.",
200 name, MAXVOLLEN - 1);
207 name = "multilabel MAC file system";
209 if (strcmp(lvalue, "enable") &&
210 strcmp(lvalue, "disable")) {
211 errx(10, "bad %s (options are %s)",
212 name, "`enable' or `disable'");
219 name = "minimum percentage of free space";
220 mvalue = atoi(optarg);
221 if (mvalue < 0 || mvalue > 99)
222 errx(10, "bad %s (%s)", name, optarg);
230 if (strcmp(Nvalue, "enable") &&
231 strcmp(Nvalue, "disable")) {
232 errx(10, "bad %s (options are %s)",
233 name, "`enable' or `disable'");
240 name = "soft updates";
242 if (strcmp(nvalue, "enable") != 0 &&
243 strcmp(nvalue, "disable") != 0) {
244 errx(10, "bad %s (options are %s)",
245 name, "`enable' or `disable'");
252 name = "optimization preference";
253 if (strcmp(optarg, "space") == 0)
254 ovalue = FS_OPTSPACE;
255 else if (strcmp(optarg, "time") == 0)
259 "bad %s (options are `space' or `time')",
271 name = "expected number of files per directory";
272 svalue = atoi(optarg);
274 errx(10, "%s must be >= 1 (was %s)",
281 name = "Softdep Journal Size";
282 Svalue = atoi(optarg);
283 if (Svalue < SUJ_MIN)
284 errx(10, "%s must be >= %d (was %s)",
285 name, SUJ_MIN, optarg);
292 if (strcmp(tvalue, "enable") != 0 &&
293 strcmp(tvalue, "disable") != 0) {
294 errx(10, "bad %s (options are %s)",
295 name, "`enable' or `disable'");
305 if (found_arg == 0 || argc != 1)
308 on = special = argv[0];
309 if (ufs_disk_fillout(&disk, special) == -1)
311 if (disk.d_name != special) {
312 if (statfs(special, &stfs) != 0)
313 warn("Can't stat %s", special);
314 if (strcmp(special, stfs.f_mntonname) == 0)
323 name = "volume label";
324 strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
327 name = "POSIX.1e ACLs";
328 if (strcmp(avalue, "enable") == 0) {
329 if (sblock.fs_flags & FS_ACLS) {
330 warnx("%s remains unchanged as enabled", name);
331 } else if (sblock.fs_flags & FS_NFS4ACLS) {
332 warnx("%s and NFSv4 ACLs are mutually "
335 sblock.fs_flags |= FS_ACLS;
336 warnx("%s set", name);
338 } else if (strcmp(avalue, "disable") == 0) {
339 if ((~sblock.fs_flags & FS_ACLS) ==
341 warnx("%s remains unchanged as disabled",
344 sblock.fs_flags &= ~FS_ACLS;
345 warnx("%s cleared", name);
350 name = "maximum blocks per file in a cylinder group";
351 if (sblock.fs_maxbpg == evalue)
352 warnx("%s remains unchanged as %d", name, evalue);
354 warnx("%s changes from %d to %d",
355 name, sblock.fs_maxbpg, evalue);
356 sblock.fs_maxbpg = evalue;
360 name = "average file size";
361 if (sblock.fs_avgfilesize == (unsigned)fvalue) {
362 warnx("%s remains unchanged as %d", name, fvalue);
365 warnx("%s changes from %d to %d",
366 name, sblock.fs_avgfilesize, fvalue);
367 sblock.fs_avgfilesize = fvalue;
371 name = "soft updates journaling";
372 if (strcmp(jvalue, "enable") == 0) {
373 if ((sblock.fs_flags & (FS_DOSOFTDEP | FS_SUJ)) ==
374 (FS_DOSOFTDEP | FS_SUJ)) {
375 warnx("%s remains unchanged as enabled", name);
376 } else if (sblock.fs_clean == 0) {
377 warnx("%s cannot be enabled until fsck is run",
379 } else if (journal_alloc(Svalue) != 0) {
380 warnx("%s can not be enabled", name);
382 sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
383 warnx("%s set", name);
385 } else if (strcmp(jvalue, "disable") == 0) {
386 if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
387 warnx("%s remains unchanged as disabled", name);
390 sblock.fs_flags &= ~FS_SUJ;
391 sblock.fs_sujfree = 0;
392 warnx("%s cleared but soft updates still set.",
395 warnx("remove .sujournal to reclaim space");
401 if (strcmp(Jvalue, "enable") == 0) {
402 if (sblock.fs_flags & FS_GJOURNAL) {
403 warnx("%s remains unchanged as enabled", name);
405 sblock.fs_flags |= FS_GJOURNAL;
406 warnx("%s set", name);
408 } else if (strcmp(Jvalue, "disable") == 0) {
409 if ((~sblock.fs_flags & FS_GJOURNAL) ==
411 warnx("%s remains unchanged as disabled",
414 sblock.fs_flags &= ~FS_GJOURNAL;
415 warnx("%s cleared", name);
420 name = "space to hold for metadata blocks";
421 if (sblock.fs_metaspace == kvalue)
422 warnx("%s remains unchanged as %d", name, kvalue);
424 kvalue = blknum(&sblock, kvalue);
425 if (kvalue > sblock.fs_fpg / 2) {
426 kvalue = blknum(&sblock, sblock.fs_fpg / 2);
427 warnx("%s cannot exceed half the file system "
430 warnx("%s changes from %jd to %d",
431 name, sblock.fs_metaspace, kvalue);
432 sblock.fs_metaspace = kvalue;
437 if (strcmp(lvalue, "enable") == 0) {
438 if (sblock.fs_flags & FS_MULTILABEL) {
439 warnx("%s remains unchanged as enabled", name);
441 sblock.fs_flags |= FS_MULTILABEL;
442 warnx("%s set", name);
444 } else if (strcmp(lvalue, "disable") == 0) {
445 if ((~sblock.fs_flags & FS_MULTILABEL) ==
447 warnx("%s remains unchanged as disabled",
450 sblock.fs_flags &= ~FS_MULTILABEL;
451 warnx("%s cleared", name);
456 name = "minimum percentage of free space";
457 if (sblock.fs_minfree == mvalue)
458 warnx("%s remains unchanged as %d%%", name, mvalue);
460 warnx("%s changes from %d%% to %d%%",
461 name, sblock.fs_minfree, mvalue);
462 sblock.fs_minfree = mvalue;
463 if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
464 warnx(OPTWARN, "time", ">=", MINFREE);
465 if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
466 warnx(OPTWARN, "space", "<", MINFREE);
471 if (strcmp(Nvalue, "enable") == 0) {
472 if (sblock.fs_flags & FS_NFS4ACLS) {
473 warnx("%s remains unchanged as enabled", name);
474 } else if (sblock.fs_flags & FS_ACLS) {
475 warnx("%s and POSIX.1e ACLs are mutually "
478 sblock.fs_flags |= FS_NFS4ACLS;
479 warnx("%s set", name);
481 } else if (strcmp(Nvalue, "disable") == 0) {
482 if ((~sblock.fs_flags & FS_NFS4ACLS) ==
484 warnx("%s remains unchanged as disabled",
487 sblock.fs_flags &= ~FS_NFS4ACLS;
488 warnx("%s cleared", name);
493 name = "soft updates";
494 if (strcmp(nvalue, "enable") == 0) {
495 if (sblock.fs_flags & FS_DOSOFTDEP)
496 warnx("%s remains unchanged as enabled", name);
497 else if (sblock.fs_clean == 0) {
498 warnx("%s cannot be enabled until fsck is run",
501 sblock.fs_flags |= FS_DOSOFTDEP;
502 warnx("%s set", name);
504 } else if (strcmp(nvalue, "disable") == 0) {
505 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
506 warnx("%s remains unchanged as disabled", name);
508 sblock.fs_flags &= ~FS_DOSOFTDEP;
509 warnx("%s cleared", name);
514 name = "optimization preference";
515 chg[FS_OPTSPACE] = "space";
516 chg[FS_OPTTIME] = "time";
517 if (sblock.fs_optim == ovalue)
518 warnx("%s remains unchanged as %s", name, chg[ovalue]);
520 warnx("%s changes from %s to %s",
521 name, chg[sblock.fs_optim], chg[ovalue]);
522 sblock.fs_optim = ovalue;
523 if (sblock.fs_minfree >= MINFREE &&
524 ovalue == FS_OPTSPACE)
525 warnx(OPTWARN, "time", ">=", MINFREE);
526 if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME)
527 warnx(OPTWARN, "space", "<", MINFREE);
531 name = "expected number of files per directory";
532 if (sblock.fs_avgfpdir == (unsigned)svalue) {
533 warnx("%s remains unchanged as %d", name, svalue);
536 warnx("%s changes from %d to %d",
537 name, sblock.fs_avgfpdir, svalue);
538 sblock.fs_avgfpdir = svalue;
542 name = "issue TRIM to the disk";
543 if (strcmp(tvalue, "enable") == 0) {
544 if (sblock.fs_flags & FS_TRIM)
545 warnx("%s remains unchanged as enabled", name);
547 sblock.fs_flags |= FS_TRIM;
548 warnx("%s set", name);
550 } else if (strcmp(tvalue, "disable") == 0) {
551 if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
552 warnx("%s remains unchanged as disabled", name);
554 sblock.fs_flags &= ~FS_TRIM;
555 warnx("%s cleared", name);
560 if (sbwrite(&disk, Aflag) == -1)
562 ufs_disk_close(&disk);
564 build_iovec_argf(&iov, &iovlen, "fstype", "ufs");
565 build_iovec_argf(&iov, &iovlen, "fspath", "%s", on);
566 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
567 if (nmount(iov, iovlen,
568 stfs.f_flags | MNT_UPDATE | MNT_RELOAD) < 0) {
570 err(9, "%s: reload: %s", special, errmsg);
572 err(9, "%s: reload", special);
574 warnx("file system reloaded");
578 if (disk.d_error != NULL)
579 errx(11, "%s: %s", special, disk.d_error);
581 err(12, "%s", special);
587 disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
588 disk.d_fs.fs_clean = 0;
597 static int contig = 1;
601 blk = cgballoc(&disk);
605 * If we failed to allocate a block from this cg, move to
608 if (cgwrite(&disk) < 0) {
609 warn("Failed to write updated cg");
612 while ((valid = cgread(&disk)) == 1) {
614 * Try to minimize fragmentation by requiring a minimum
615 * number of blocks present.
617 if (cgp->cg_cs.cs_nbfree > 256 * 1024)
619 if (contig == 0 && cgp->cg_cs.cs_nbfree)
625 * Try once through looking only for large contiguous regions
626 * and again taking any space we can find.
631 warnx("Journal file fragmented.");
634 warnx("Failed to find sufficient free blocks for the journal");
637 if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
638 sblock.fs_bsize) <= 0) {
639 warn("Failed to initialize new block");
646 * Search a directory block for the SUJ_FILE.
649 dir_search(ufs2_daddr_t blk, int bytes)
651 char block[MAXBSIZE];
655 if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
656 warn("Failed to read dir block");
659 for (off = 0; off < bytes; off += dp->d_reclen) {
660 dp = (struct direct *)&block[off];
661 if (dp->d_reclen == 0)
665 if (dp->d_namlen != strlen(SUJ_FILE))
667 if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
676 * Search in the UFS_ROOTINO for the SUJ_FILE. If it exists we can not enable
680 journal_findfile(void)
682 struct ufs1_dinode *dp1;
683 struct ufs2_dinode *dp2;
689 if (getino(&disk, &ip, UFS_ROOTINO, &mode) != 0) {
690 warn("Failed to get root inode");
695 if (sblock.fs_magic == FS_UFS1_MAGIC) {
696 if ((off_t)dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
697 warnx("UFS_ROOTINO extends beyond direct blocks.");
700 for (i = 0; i < UFS_NDADDR; i++) {
701 if (dp1->di_db[i] == 0)
703 if ((ino = dir_search(dp1->di_db[i],
704 sblksize(&sblock, (off_t)dp1->di_size, i))) != 0)
708 if ((off_t)dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
709 warnx("UFS_ROOTINO extends beyond direct blocks.");
712 for (i = 0; i < UFS_NDADDR; i++) {
713 if (dp2->di_db[i] == 0)
715 if ((ino = dir_search(dp2->di_db[i],
716 sblksize(&sblock, (off_t)dp2->di_size, i))) != 0)
725 dir_clear_block(const char *block, off_t off)
729 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
730 dp = (struct direct *)&block[off];
732 dp->d_reclen = DIRBLKSIZ;
733 dp->d_type = DT_UNKNOWN;
738 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
739 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
743 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
746 char block[MAXBSIZE];
748 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
749 warn("Failed to read dir block");
752 bzero(&block[off], sblock.fs_bsize - off);
753 dp = (struct direct *)&block[off];
755 dp->d_reclen = DIRBLKSIZ;
757 dp->d_namlen = strlen(SUJ_FILE);
758 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
759 dir_clear_block(block, off + DIRBLKSIZ);
760 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
761 warn("Failed to write dir block");
768 * Extend a directory block in 'blk' by copying it to a full size block
769 * and inserting the new journal inode into .sujournal.
772 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
774 char block[MAXBSIZE];
776 if (bread(&disk, fsbtodb(&sblock, blk), block,
777 roundup(size, sblock.fs_fsize)) <= 0) {
778 warn("Failed to read dir block");
781 dir_clear_block(block, size);
782 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
784 warn("Failed to write dir block");
788 return (dir_insert(nblk, size, ino));
792 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
796 journal_insertfile(ino_t ino)
798 struct ufs1_dinode *dp1;
799 struct ufs2_dinode *dp2;
808 if (getino(&disk, &ip, UFS_ROOTINO, &mode) != 0) {
809 warn("Failed to get root inode");
817 nblk = journal_balloc();
821 * For simplicity sake we aways extend the UFS_ROOTINO into a new
822 * directory block rather than searching for space and inserting
823 * into an existing block. However, if the rootino has frags
824 * have to free them and extend the block.
826 if (sblock.fs_magic == FS_UFS1_MAGIC) {
827 lbn = lblkno(&sblock, dp1->di_size);
828 off = blkoff(&sblock, dp1->di_size);
829 blk = dp1->di_db[lbn];
830 size = sblksize(&sblock, (off_t)dp1->di_size, lbn);
832 lbn = lblkno(&sblock, dp2->di_size);
833 off = blkoff(&sblock, dp2->di_size);
834 blk = dp2->di_db[lbn];
835 size = sblksize(&sblock, (off_t)dp2->di_size, lbn);
838 if (dir_extend(blk, nblk, off, ino) == -1)
842 if (dir_insert(nblk, 0, ino) == -1)
845 if (sblock.fs_magic == FS_UFS1_MAGIC) {
846 dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
847 dp1->di_db[lbn] = nblk;
848 dp1->di_size = lblktosize(&sblock, lbn+1);
850 dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
851 dp2->di_db[lbn] = nblk;
852 dp2->di_size = lblktosize(&sblock, lbn+1);
854 if (putino(&disk) < 0) {
855 warn("Failed to write root inode");
858 if (cgwrite(&disk) < 0) {
859 warn("Failed to write updated cg");
864 if (cgbfree(&disk, blk, size) < 0) {
865 warn("Failed to write cg");
874 indir_fill(ufs2_daddr_t blk, int level, int *resid)
876 char indirbuf[MAXBSIZE];
884 bzero(indirbuf, sizeof(indirbuf));
885 bap1 = (ufs1_daddr_t *)indirbuf;
888 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
889 nblk = journal_balloc();
893 if (sblock.fs_magic == FS_UFS1_MAGIC)
898 ncnt = indir_fill(nblk, level - 1, resid);
905 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
906 sblock.fs_bsize) <= 0) {
907 warn("Failed to write indirect");
914 * Clear the flag bits so the journal can be removed.
919 struct ufs1_dinode *dp1;
920 struct ufs2_dinode *dp2;
925 ino = journal_findfile();
926 if (ino == (ino_t)-1 || ino == 0) {
927 warnx("Journal file does not exist");
930 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
931 if (getino(&disk, &ip, ino, &mode) != 0) {
932 warn("Failed to get journal inode");
937 if (sblock.fs_magic == FS_UFS1_MAGIC)
941 if (putino(&disk) < 0) {
942 warn("Failed to write journal inode");
948 journal_alloc(int64_t size)
950 struct ufs1_dinode *dp1;
951 struct ufs2_dinode *dp2;
966 * If the journal file exists we can't allocate it.
968 ino = journal_findfile();
969 if (ino == (ino_t)-1)
972 warnx("Journal file %s already exists, please remove.",
977 * If the user didn't supply a size pick one based on the filesystem
978 * size constrained with hardcoded MIN and MAX values. We opt for
979 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
980 * not less than the MIN.
983 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
984 size = MIN(SUJ_MAX, size);
985 if (size / sblock.fs_fsize > sblock.fs_fpg)
986 size = sblock.fs_fpg * sblock.fs_fsize;
987 size = MAX(SUJ_MIN, size);
988 /* fsck does not support fragments in journal files. */
989 size = roundup(size, sblock.fs_bsize);
991 resid = blocks = size / sblock.fs_bsize;
992 if (sblock.fs_cstotal.cs_nbfree < blocks) {
993 warn("Insufficient free space for %jd byte journal", size);
997 * Find a cg with enough blocks to satisfy the journal
998 * size. Presently the journal does not span cgs.
1000 while (cgread(&disk) == 1) {
1001 if (cgp->cg_cs.cs_nifree == 0)
1003 ino = cgialloc(&disk);
1006 printf("Using inode %ju in cg %d for %jd byte journal\n",
1007 (uintmax_t)ino, cgp->cg_cgx, size);
1008 if (getino(&disk, &ip, ino, &mode) != 0) {
1009 warn("Failed to get allocated inode");
1014 * We leave fields unrelated to the number of allocated
1015 * blocks and size uninitialized. This causes legacy
1016 * fsck implementations to clear the inode.
1021 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1022 bzero(dp1, sizeof(*dp1));
1023 dp1->di_size = size;
1024 dp1->di_mode = IFREG | IREAD;
1026 dp1->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1027 dp1->di_atime = utime;
1028 dp1->di_mtime = utime;
1029 dp1->di_ctime = utime;
1031 bzero(dp2, sizeof(*dp2));
1032 dp2->di_size = size;
1033 dp2->di_mode = IFREG | IREAD;
1035 dp2->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1036 dp2->di_atime = utime;
1037 dp2->di_mtime = utime;
1038 dp2->di_ctime = utime;
1039 dp2->di_birthtime = utime;
1041 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1042 blk = journal_balloc();
1045 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1046 dp1->di_db[i] = blk;
1049 dp2->di_db[i] = blk;
1053 for (i = 0; i < UFS_NIADDR && resid; i++) {
1054 blk = journal_balloc();
1057 blks = indir_fill(blk, i, &resid) + 1;
1062 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1063 dp1->di_ib[i] = blk;
1064 dp1->di_blocks += blks;
1066 dp2->di_ib[i] = blk;
1067 dp2->di_blocks += blks;
1070 if (sblock.fs_magic == FS_UFS1_MAGIC)
1071 dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1073 dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1074 if (putino(&disk) < 0) {
1075 warn("Failed to write inode");
1079 if (cgwrite(&disk) < 0) {
1080 warn("Failed to write updated cg");
1084 if (journal_insertfile(ino) < 0) {
1088 sblock.fs_sujfree = 0;
1091 warnx("Insufficient free space for the journal.");
1099 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1100 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1101 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1102 " [-L volname] [-l enable | disable] [-m minfree]",
1103 " [-N enable | disable] [-n enable | disable]",
1104 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1105 " special | filesystem");
1112 warnx("POSIX.1e ACLs: (-a) %s",
1113 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1114 warnx("NFSv4 ACLs: (-N) %s",
1115 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1116 warnx("MAC multilabel: (-l) %s",
1117 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1118 warnx("soft updates: (-n) %s",
1119 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1120 warnx("soft update journaling: (-j) %s",
1121 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1122 warnx("gjournal: (-J) %s",
1123 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1124 warnx("trim: (-t) %s",
1125 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1126 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1128 warnx("average file size: (-f) %d",
1129 sblock.fs_avgfilesize);
1130 warnx("average number of files in a directory: (-s) %d",
1131 sblock.fs_avgfpdir);
1132 warnx("minimum percentage of free space: (-m) %d%%",
1134 warnx("space to hold for metadata blocks: (-k) %jd",
1135 sblock.fs_metaspace);
1136 warnx("optimization preference: (-o) %s",
1137 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1138 if (sblock.fs_minfree >= MINFREE &&
1139 sblock.fs_optim == FS_OPTSPACE)
1140 warnx(OPTWARN, "time", ">=", MINFREE);
1141 if (sblock.fs_minfree < MINFREE &&
1142 sblock.fs_optim == FS_OPTTIME)
1143 warnx(OPTWARN, "space", "<", MINFREE);
1144 warnx("volume label: (-L) %s",