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 cannot 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)
686 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
687 warn("Failed to get root inode: %s", disk.d_error);
690 if (sblock.fs_magic == FS_UFS1_MAGIC) {
691 if ((off_t)dp.dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
692 warnx("UFS_ROOTINO extends beyond direct blocks.");
695 for (i = 0; i < UFS_NDADDR; i++) {
696 if (dp.dp1->di_db[i] == 0)
698 if ((ino = dir_search(dp.dp1->di_db[i],
699 sblksize(&sblock, (off_t)dp.dp1->di_size, i))) != 0)
703 if ((off_t)dp.dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
704 warnx("UFS_ROOTINO extends beyond direct blocks.");
707 for (i = 0; i < UFS_NDADDR; i++) {
708 if (dp.dp2->di_db[i] == 0)
710 if ((ino = dir_search(dp.dp2->di_db[i],
711 sblksize(&sblock, (off_t)dp.dp2->di_size, i))) != 0)
720 dir_clear_block(const char *block, off_t off)
724 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
725 dp = (struct direct *)&block[off];
727 dp->d_reclen = DIRBLKSIZ;
728 dp->d_type = DT_UNKNOWN;
733 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
734 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
738 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
741 char block[MAXBSIZE];
743 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
744 warn("Failed to read dir block");
747 bzero(&block[off], sblock.fs_bsize - off);
748 dp = (struct direct *)&block[off];
750 dp->d_reclen = DIRBLKSIZ;
752 dp->d_namlen = strlen(SUJ_FILE);
753 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
754 dir_clear_block(block, off + DIRBLKSIZ);
755 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
756 warn("Failed to write dir block");
763 * Extend a directory block in 'blk' by copying it to a full size block
764 * and inserting the new journal inode into .sujournal.
767 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
769 char block[MAXBSIZE];
771 if (bread(&disk, fsbtodb(&sblock, blk), block,
772 roundup(size, sblock.fs_fsize)) <= 0) {
773 warn("Failed to read dir block");
776 dir_clear_block(block, size);
777 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
779 warn("Failed to write dir block");
783 return (dir_insert(nblk, size, ino));
787 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
791 journal_insertfile(ino_t ino)
800 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
801 warn("Failed to get root inode: %s", disk.d_error);
807 nblk = journal_balloc();
811 * For simplicity sake we aways extend the UFS_ROOTINO into a new
812 * directory block rather than searching for space and inserting
813 * into an existing block. However, if the rootino has frags
814 * have to free them and extend the block.
816 if (sblock.fs_magic == FS_UFS1_MAGIC) {
817 lbn = lblkno(&sblock, dp.dp1->di_size);
818 off = blkoff(&sblock, dp.dp1->di_size);
819 blk = dp.dp1->di_db[lbn];
820 size = sblksize(&sblock, (off_t)dp.dp1->di_size, lbn);
822 lbn = lblkno(&sblock, dp.dp2->di_size);
823 off = blkoff(&sblock, dp.dp2->di_size);
824 blk = dp.dp2->di_db[lbn];
825 size = sblksize(&sblock, (off_t)dp.dp2->di_size, lbn);
828 if (dir_extend(blk, nblk, off, ino) == -1)
832 if (dir_insert(nblk, 0, ino) == -1)
835 if (sblock.fs_magic == FS_UFS1_MAGIC) {
836 dp.dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
837 dp.dp1->di_db[lbn] = nblk;
838 dp.dp1->di_size = lblktosize(&sblock, lbn+1);
840 dp.dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
841 dp.dp2->di_db[lbn] = nblk;
842 dp.dp2->di_size = lblktosize(&sblock, lbn+1);
844 if (putinode(&disk) < 0) {
845 warn("Failed to write root inode: %s", disk.d_error);
848 if (cgwrite(&disk) < 0) {
849 warn("Failed to write updated cg");
854 if (cgbfree(&disk, blk, size) < 0) {
855 warn("Failed to write cg");
864 indir_fill(ufs2_daddr_t blk, int level, int *resid)
866 char indirbuf[MAXBSIZE];
874 bzero(indirbuf, sizeof(indirbuf));
875 bap1 = (ufs1_daddr_t *)indirbuf;
878 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
879 nblk = journal_balloc();
883 if (sblock.fs_magic == FS_UFS1_MAGIC)
888 ncnt = indir_fill(nblk, level - 1, resid);
895 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
896 sblock.fs_bsize) <= 0) {
897 warn("Failed to write indirect");
904 * Clear the flag bits so the journal can be removed.
912 ino = journal_findfile();
913 if (ino == (ino_t)-1 || ino == 0) {
914 warnx("Journal file does not exist");
917 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
918 if (getinode(&disk, &dp, ino) != 0) {
919 warn("Failed to get journal inode: %s", disk.d_error);
922 if (sblock.fs_magic == FS_UFS1_MAGIC)
923 dp.dp1->di_flags = 0;
925 dp.dp2->di_flags = 0;
926 if (putinode(&disk) < 0) {
927 warn("Failed to write journal inode: %s", disk.d_error);
933 journal_alloc(int64_t size)
948 * If the journal file exists we can't allocate it.
950 ino = journal_findfile();
951 if (ino == (ino_t)-1) {
952 warnx("journal_findfile() failed.");
956 warnx("Journal file %s already exists, please remove.",
961 * If the user didn't supply a size pick one based on the filesystem
962 * size constrained with hardcoded MIN and MAX values. We opt for
963 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
964 * not less than the MIN.
967 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
968 size = MIN(SUJ_MAX, size);
969 if (size / sblock.fs_fsize > sblock.fs_fpg)
970 size = sblock.fs_fpg * sblock.fs_fsize;
971 size = MAX(SUJ_MIN, size);
972 /* fsck does not support fragments in journal files. */
973 size = roundup(size, sblock.fs_bsize);
975 resid = blocks = size / sblock.fs_bsize;
976 if (sblock.fs_cstotal.cs_nbfree < blocks) {
977 warn("Insufficient free space for %jd byte journal", size);
981 * Find a cg with enough blocks to satisfy the journal
982 * size. Presently the journal does not span cgs.
984 while (cgread(&disk) == 1) {
985 if (cgp->cg_cs.cs_nifree == 0)
987 ino = cgialloc(&disk);
990 printf("Using inode %ju in cg %d for %jd byte journal\n",
991 (uintmax_t)ino, cgp->cg_cgx, size);
992 if (getinode(&disk, &dp, ino) != 0) {
993 warn("Failed to get allocated inode: %s", disk.d_error);
998 * We leave fields unrelated to the number of allocated
999 * blocks and size uninitialized. This causes legacy
1000 * fsck implementations to clear the inode.
1003 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1004 bzero(dp.dp1, sizeof(*dp.dp1));
1005 dp.dp1->di_size = size;
1006 dp.dp1->di_mode = IFREG | IREAD;
1007 dp.dp1->di_nlink = 1;
1009 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1010 dp.dp1->di_atime = utime;
1011 dp.dp1->di_mtime = utime;
1012 dp.dp1->di_ctime = utime;
1014 bzero(dp.dp2, sizeof(*dp.dp2));
1015 dp.dp2->di_size = size;
1016 dp.dp2->di_mode = IFREG | IREAD;
1017 dp.dp2->di_nlink = 1;
1019 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1020 dp.dp2->di_atime = utime;
1021 dp.dp2->di_mtime = utime;
1022 dp.dp2->di_ctime = utime;
1023 dp.dp2->di_birthtime = utime;
1025 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1026 blk = journal_balloc();
1029 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1030 dp.dp1->di_db[i] = blk;
1031 dp.dp1->di_blocks++;
1033 dp.dp2->di_db[i] = blk;
1034 dp.dp2->di_blocks++;
1037 for (i = 0; i < UFS_NIADDR && resid; i++) {
1038 blk = journal_balloc();
1041 blks = indir_fill(blk, i, &resid) + 1;
1046 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1047 dp.dp1->di_ib[i] = blk;
1048 dp.dp1->di_blocks += blks;
1050 dp.dp2->di_ib[i] = blk;
1051 dp.dp2->di_blocks += blks;
1054 if (sblock.fs_magic == FS_UFS1_MAGIC)
1055 dp.dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1057 dp.dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1058 if (putinode(&disk) < 0) {
1059 warn("Failed to write allocated inode: %s",
1064 if (cgwrite(&disk) < 0) {
1065 warn("Failed to write updated cg");
1069 if (journal_insertfile(ino) < 0) {
1073 sblock.fs_sujfree = 0;
1076 warnx("Insufficient free space for the journal.");
1084 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1085 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1086 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1087 " [-L volname] [-l enable | disable] [-m minfree]",
1088 " [-N enable | disable] [-n enable | disable]",
1089 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1090 " special | filesystem");
1097 warnx("POSIX.1e ACLs: (-a) %s",
1098 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1099 warnx("NFSv4 ACLs: (-N) %s",
1100 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1101 warnx("MAC multilabel: (-l) %s",
1102 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1103 warnx("soft updates: (-n) %s",
1104 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1105 warnx("soft update journaling: (-j) %s",
1106 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1107 warnx("gjournal: (-J) %s",
1108 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1109 warnx("trim: (-t) %s",
1110 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1111 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1113 warnx("average file size: (-f) %d",
1114 sblock.fs_avgfilesize);
1115 warnx("average number of files in a directory: (-s) %d",
1116 sblock.fs_avgfpdir);
1117 warnx("minimum percentage of free space: (-m) %d%%",
1119 warnx("space to hold for metadata blocks: (-k) %jd",
1120 sblock.fs_metaspace);
1121 warnx("optimization preference: (-o) %s",
1122 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1123 if (sblock.fs_minfree >= MINFREE &&
1124 sblock.fs_optim == FS_OPTSPACE)
1125 warnx(OPTWARN, "time", ">=", MINFREE);
1126 if (sblock.fs_minfree < MINFREE &&
1127 sblock.fs_optim == FS_OPTTIME)
1128 warnx(OPTWARN, "space", "<", MINFREE);
1129 warnx("volume label: (-L) %s",