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/extattr.h>
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ffs/fs.h>
59 #include <ufs/ufs/dir.h>
75 /* the optimization warning string template */
76 #define OPTWARN "should optimize for %s with minfree %s %d%%"
79 static char clrbuf[MAXBSIZE];
80 static struct uufsd disk;
81 #define sblock disk.d_fs
83 static void usage(void);
84 static void printfs(void);
85 static int journal_alloc(int64_t size);
86 static void journal_clear(void);
87 static void sbdirty(void);
90 main(int argc, char *argv[])
92 const char *avalue, *jvalue, *Jvalue, *Lvalue, *lvalue, *Nvalue, *nvalue;
97 int Aflag, aflag, eflag, evalue, fflag, fvalue, jflag, Jflag, kflag;
98 int kvalue, Lflag, lflag, mflag, mvalue, Nflag, nflag, oflag, ovalue;
99 int pflag, sflag, svalue, Svalue, tflag;
100 int ch, found_arg, i;
105 Aflag = aflag = eflag = fflag = jflag = Jflag = kflag = Lflag = 0;
106 lflag = mflag = Nflag = nflag = oflag = pflag = sflag = tflag = 0;
107 avalue = jvalue = Jvalue = Lvalue = lvalue = Nvalue = nvalue = NULL;
108 evalue = fvalue = mvalue = ovalue = svalue = Svalue = 0;
109 found_arg = 0; /* At least one arg is required. */
110 while ((ch = getopt(argc, argv, "Aa:e:f:j:J:k:L:l:m:N:n:o:ps:S:t:"))
121 name = "POSIX.1e ACLs";
123 if (strcmp(avalue, "enable") &&
124 strcmp(avalue, "disable")) {
125 errx(10, "bad %s (options are %s)",
126 name, "`enable' or `disable'");
133 name = "maximum blocks per file in a cylinder group";
134 evalue = atoi(optarg);
136 errx(10, "%s must be >= 1 (was %s)",
143 name = "average file size";
144 fvalue = atoi(optarg);
146 errx(10, "%s must be >= 1 (was %s)",
153 name = "softdep journaled file system";
155 if (strcmp(jvalue, "enable") &&
156 strcmp(jvalue, "disable")) {
157 errx(10, "bad %s (options are %s)",
158 name, "`enable' or `disable'");
165 name = "gjournaled file system";
167 if (strcmp(Jvalue, "enable") &&
168 strcmp(Jvalue, "disable")) {
169 errx(10, "bad %s (options are %s)",
170 name, "`enable' or `disable'");
177 name = "space to hold for metadata blocks";
178 kvalue = atoi(optarg);
180 errx(10, "bad %s (%s)", name, optarg);
186 name = "volume label";
189 while (isalnum(Lvalue[++i]) || Lvalue[i] == '_' ||
192 if (Lvalue[i] != '\0') {
193 errx(10, "bad %s. Valid characters are "
194 "alphanumerics, dashes, and underscores.",
197 if (strlen(Lvalue) >= MAXVOLLEN) {
198 errx(10, "bad %s. Length is longer than %d.",
199 name, MAXVOLLEN - 1);
206 name = "multilabel MAC file system";
208 if (strcmp(lvalue, "enable") &&
209 strcmp(lvalue, "disable")) {
210 errx(10, "bad %s (options are %s)",
211 name, "`enable' or `disable'");
218 name = "minimum percentage of free space";
219 mvalue = atoi(optarg);
220 if (mvalue < 0 || mvalue > 99)
221 errx(10, "bad %s (%s)", name, optarg);
229 if (strcmp(Nvalue, "enable") &&
230 strcmp(Nvalue, "disable")) {
231 errx(10, "bad %s (options are %s)",
232 name, "`enable' or `disable'");
239 name = "soft updates";
241 if (strcmp(nvalue, "enable") != 0 &&
242 strcmp(nvalue, "disable") != 0) {
243 errx(10, "bad %s (options are %s)",
244 name, "`enable' or `disable'");
251 name = "optimization preference";
252 if (strcmp(optarg, "space") == 0)
253 ovalue = FS_OPTSPACE;
254 else if (strcmp(optarg, "time") == 0)
258 "bad %s (options are `space' or `time')",
270 name = "expected number of files per directory";
271 svalue = atoi(optarg);
273 errx(10, "%s must be >= 1 (was %s)",
280 name = "Softdep Journal Size";
281 Svalue = atoi(optarg);
282 if (Svalue < SUJ_MIN)
283 errx(10, "%s must be >= %d (was %s)",
284 name, SUJ_MIN, optarg);
291 if (strcmp(tvalue, "enable") != 0 &&
292 strcmp(tvalue, "disable") != 0) {
293 errx(10, "bad %s (options are %s)",
294 name, "`enable' or `disable'");
304 if (found_arg == 0 || argc != 1)
308 if (ufs_disk_fillout(&disk, special) == -1)
311 * Check for unclean filesystem.
313 if ((sblock.fs_clean == 0 ||
314 (sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0) &&
315 (found_arg > 1 || !pflag))
316 errx(1, "%s is not clean - run fsck.\n", special);
322 name = "volume label";
323 strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
326 name = "POSIX.1e ACLs";
327 if (strcmp(avalue, "enable") == 0) {
328 if (sblock.fs_flags & FS_ACLS) {
329 warnx("%s remains unchanged as enabled", name);
330 } else if (sblock.fs_flags & FS_NFS4ACLS) {
331 warnx("%s and NFSv4 ACLs are mutually "
334 sblock.fs_flags |= FS_ACLS;
335 warnx("%s set", name);
337 } else if (strcmp(avalue, "disable") == 0) {
338 if ((~sblock.fs_flags & FS_ACLS) ==
340 warnx("%s remains unchanged as disabled",
343 sblock.fs_flags &= ~FS_ACLS;
344 warnx("%s cleared", name);
349 name = "maximum blocks per file in a cylinder group";
350 if (sblock.fs_maxbpg == evalue)
351 warnx("%s remains unchanged as %d", name, evalue);
353 warnx("%s changes from %d to %d",
354 name, sblock.fs_maxbpg, evalue);
355 sblock.fs_maxbpg = evalue;
359 name = "average file size";
360 if (sblock.fs_avgfilesize == (unsigned)fvalue) {
361 warnx("%s remains unchanged as %d", name, fvalue);
364 warnx("%s changes from %d to %d",
365 name, sblock.fs_avgfilesize, fvalue);
366 sblock.fs_avgfilesize = fvalue;
370 name = "soft updates journaling";
371 if (strcmp(jvalue, "enable") == 0) {
372 if ((sblock.fs_flags & (FS_DOSOFTDEP | FS_SUJ)) ==
373 (FS_DOSOFTDEP | FS_SUJ)) {
374 warnx("%s remains unchanged as enabled", name);
375 } else if (sblock.fs_clean == 0) {
376 warnx("%s cannot be enabled until fsck is run",
378 } else if (journal_alloc(Svalue) != 0) {
379 warnx("%s cannot be enabled", name);
381 sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
382 warnx("%s set", name);
384 } else if (strcmp(jvalue, "disable") == 0) {
385 if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
386 warnx("%s remains unchanged as disabled", name);
389 sblock.fs_flags &= ~FS_SUJ;
390 sblock.fs_sujfree = 0;
391 warnx("%s cleared but soft updates still set.",
394 warnx("remove .sujournal to reclaim space");
400 if (strcmp(Jvalue, "enable") == 0) {
401 if (sblock.fs_flags & FS_GJOURNAL) {
402 warnx("%s remains unchanged as enabled", name);
404 sblock.fs_flags |= FS_GJOURNAL;
405 warnx("%s set", name);
407 } else if (strcmp(Jvalue, "disable") == 0) {
408 if ((~sblock.fs_flags & FS_GJOURNAL) ==
410 warnx("%s remains unchanged as disabled",
413 sblock.fs_flags &= ~FS_GJOURNAL;
414 warnx("%s cleared", name);
419 name = "space to hold for metadata blocks";
420 if (sblock.fs_metaspace == kvalue)
421 warnx("%s remains unchanged as %d", name, kvalue);
423 kvalue = blknum(&sblock, kvalue);
424 if (kvalue > sblock.fs_fpg / 2) {
425 kvalue = blknum(&sblock, sblock.fs_fpg / 2);
426 warnx("%s cannot exceed half the file system "
429 warnx("%s changes from %jd to %d",
430 name, sblock.fs_metaspace, kvalue);
431 sblock.fs_metaspace = kvalue;
436 if (strcmp(lvalue, "enable") == 0) {
437 if (sblock.fs_flags & FS_MULTILABEL) {
438 warnx("%s remains unchanged as enabled", name);
440 sblock.fs_flags |= FS_MULTILABEL;
441 warnx("%s set", name);
443 } else if (strcmp(lvalue, "disable") == 0) {
444 if ((~sblock.fs_flags & FS_MULTILABEL) ==
446 warnx("%s remains unchanged as disabled",
449 sblock.fs_flags &= ~FS_MULTILABEL;
450 warnx("%s cleared", name);
455 name = "minimum percentage of free space";
456 if (sblock.fs_minfree == mvalue)
457 warnx("%s remains unchanged as %d%%", name, mvalue);
459 warnx("%s changes from %d%% to %d%%",
460 name, sblock.fs_minfree, mvalue);
461 sblock.fs_minfree = mvalue;
462 if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
463 warnx(OPTWARN, "time", ">=", MINFREE);
464 if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
465 warnx(OPTWARN, "space", "<", MINFREE);
470 if (strcmp(Nvalue, "enable") == 0) {
471 if (sblock.fs_flags & FS_NFS4ACLS) {
472 warnx("%s remains unchanged as enabled", name);
473 } else if (sblock.fs_flags & FS_ACLS) {
474 warnx("%s and POSIX.1e ACLs are mutually "
477 sblock.fs_flags |= FS_NFS4ACLS;
478 warnx("%s set", name);
480 } else if (strcmp(Nvalue, "disable") == 0) {
481 if ((~sblock.fs_flags & FS_NFS4ACLS) ==
483 warnx("%s remains unchanged as disabled",
486 sblock.fs_flags &= ~FS_NFS4ACLS;
487 warnx("%s cleared", name);
492 name = "soft updates";
493 if (strcmp(nvalue, "enable") == 0) {
494 if (sblock.fs_flags & FS_DOSOFTDEP)
495 warnx("%s remains unchanged as enabled", name);
496 else if (sblock.fs_clean == 0) {
497 warnx("%s cannot be enabled until fsck is run",
500 sblock.fs_flags |= FS_DOSOFTDEP;
501 warnx("%s set", name);
503 } else if (strcmp(nvalue, "disable") == 0) {
504 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
505 warnx("%s remains unchanged as disabled", name);
507 sblock.fs_flags &= ~FS_DOSOFTDEP;
508 warnx("%s cleared", name);
513 name = "optimization preference";
514 chg[FS_OPTSPACE] = "space";
515 chg[FS_OPTTIME] = "time";
516 if (sblock.fs_optim == ovalue)
517 warnx("%s remains unchanged as %s", name, chg[ovalue]);
519 warnx("%s changes from %s to %s",
520 name, chg[sblock.fs_optim], chg[ovalue]);
521 sblock.fs_optim = ovalue;
522 if (sblock.fs_minfree >= MINFREE &&
523 ovalue == FS_OPTSPACE)
524 warnx(OPTWARN, "time", ">=", MINFREE);
525 if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME)
526 warnx(OPTWARN, "space", "<", MINFREE);
530 name = "expected number of files per directory";
531 if (sblock.fs_avgfpdir == (unsigned)svalue) {
532 warnx("%s remains unchanged as %d", name, svalue);
535 warnx("%s changes from %d to %d",
536 name, sblock.fs_avgfpdir, svalue);
537 sblock.fs_avgfpdir = svalue;
541 name = "issue TRIM to the disk";
542 if (strcmp(tvalue, "enable") == 0) {
543 if (sblock.fs_flags & FS_TRIM)
544 warnx("%s remains unchanged as enabled", name);
546 sblock.fs_flags |= FS_TRIM;
547 warnx("%s set", name);
549 } else if (strcmp(tvalue, "disable") == 0) {
550 if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
551 warnx("%s remains unchanged as disabled", name);
553 sblock.fs_flags &= ~FS_TRIM;
554 warnx("%s cleared", name);
559 if (sbwrite(&disk, Aflag) == -1)
561 diskname = strdup(disk.d_name);
562 ufs_disk_close(&disk);
563 chkdoreload(getmntpoint(diskname), warnx);
566 if (disk.d_error != NULL)
567 errx(11, "%s: %s", special, disk.d_error);
569 err(12, "%s", special);
575 disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
576 disk.d_fs.fs_clean = 0;
585 static int contig = 1;
589 blk = cgballoc(&disk);
593 * If we failed to allocate a block from this cg, move to
596 if (cgwrite(&disk) < 0) {
597 warn("Failed to write updated cg");
600 while ((valid = cgread(&disk)) == 1) {
602 * Try to minimize fragmentation by requiring a minimum
603 * number of blocks present.
605 if (cgp->cg_cs.cs_nbfree > 256 * 1024)
607 if (contig == 0 && cgp->cg_cs.cs_nbfree)
613 * Try once through looking only for large contiguous regions
614 * and again taking any space we can find.
619 warnx("Journal file fragmented.");
622 warnx("Failed to find sufficient free blocks for the journal");
625 if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
626 sblock.fs_bsize) <= 0) {
627 warn("Failed to initialize new block");
634 * Search a directory block for the SUJ_FILE.
637 dir_search(ufs2_daddr_t blk, int bytes)
639 char block[MAXBSIZE];
643 if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
644 warn("Failed to read dir block");
647 for (off = 0; off < bytes; off += dp->d_reclen) {
648 dp = (struct direct *)&block[off];
649 if (dp->d_reclen == 0)
653 if (dp->d_namlen != strlen(SUJ_FILE))
655 if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
664 * Search in the UFS_ROOTINO for the SUJ_FILE. If it exists we can not enable
668 journal_findfile(void)
674 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
675 warn("Failed to get root inode: %s", disk.d_error);
678 if (sblock.fs_magic == FS_UFS1_MAGIC) {
679 if ((off_t)dp.dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
680 warnx("UFS_ROOTINO extends beyond direct blocks.");
683 for (i = 0; i < UFS_NDADDR; i++) {
684 if (dp.dp1->di_db[i] == 0)
686 if ((ino = dir_search(dp.dp1->di_db[i],
687 sblksize(&sblock, (off_t)dp.dp1->di_size, i))) != 0)
691 if ((off_t)dp.dp2->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.dp2->di_db[i] == 0)
698 if ((ino = dir_search(dp.dp2->di_db[i],
699 sblksize(&sblock, (off_t)dp.dp2->di_size, i))) != 0)
708 dir_clear_block(const char *block, off_t off)
712 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
713 dp = (struct direct *)&block[off];
715 dp->d_reclen = DIRBLKSIZ;
716 dp->d_type = DT_UNKNOWN;
721 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
722 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
726 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
729 char block[MAXBSIZE];
731 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
732 warn("Failed to read dir block");
735 bzero(&block[off], sblock.fs_bsize - off);
736 dp = (struct direct *)&block[off];
738 dp->d_reclen = DIRBLKSIZ;
740 dp->d_namlen = strlen(SUJ_FILE);
741 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
742 dir_clear_block(block, off + DIRBLKSIZ);
743 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
744 warn("Failed to write dir block");
751 * Extend a directory block in 'blk' by copying it to a full size block
752 * and inserting the new journal inode into .sujournal.
755 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
757 char block[MAXBSIZE];
759 if (bread(&disk, fsbtodb(&sblock, blk), block,
760 roundup(size, sblock.fs_fsize)) <= 0) {
761 warn("Failed to read dir block");
764 dir_clear_block(block, size);
765 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
767 warn("Failed to write dir block");
771 return (dir_insert(nblk, size, ino));
775 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
779 journal_insertfile(ino_t ino)
788 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
789 warn("Failed to get root inode: %s", disk.d_error);
795 nblk = journal_balloc();
799 * For simplicity sake we aways extend the UFS_ROOTINO into a new
800 * directory block rather than searching for space and inserting
801 * into an existing block. However, if the rootino has frags
802 * have to free them and extend the block.
804 if (sblock.fs_magic == FS_UFS1_MAGIC) {
805 lbn = lblkno(&sblock, dp.dp1->di_size);
806 off = blkoff(&sblock, dp.dp1->di_size);
807 blk = dp.dp1->di_db[lbn];
808 size = sblksize(&sblock, (off_t)dp.dp1->di_size, lbn);
810 lbn = lblkno(&sblock, dp.dp2->di_size);
811 off = blkoff(&sblock, dp.dp2->di_size);
812 blk = dp.dp2->di_db[lbn];
813 size = sblksize(&sblock, (off_t)dp.dp2->di_size, lbn);
816 if (dir_extend(blk, nblk, off, ino) == -1)
820 if (dir_insert(nblk, 0, ino) == -1)
823 if (sblock.fs_magic == FS_UFS1_MAGIC) {
824 dp.dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
825 dp.dp1->di_db[lbn] = nblk;
826 dp.dp1->di_size = lblktosize(&sblock, lbn+1);
828 dp.dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
829 dp.dp2->di_db[lbn] = nblk;
830 dp.dp2->di_size = lblktosize(&sblock, lbn+1);
832 if (putinode(&disk) < 0) {
833 warn("Failed to write root inode: %s", disk.d_error);
836 if (cgwrite(&disk) < 0) {
837 warn("Failed to write updated cg");
842 if (cgbfree(&disk, blk, size) < 0) {
843 warn("Failed to write cg");
852 indir_fill(ufs2_daddr_t blk, int level, int *resid)
854 char indirbuf[MAXBSIZE];
862 bzero(indirbuf, sizeof(indirbuf));
863 bap1 = (ufs1_daddr_t *)indirbuf;
866 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
867 nblk = journal_balloc();
871 if (sblock.fs_magic == FS_UFS1_MAGIC)
876 ncnt = indir_fill(nblk, level - 1, resid);
883 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
884 sblock.fs_bsize) <= 0) {
885 warn("Failed to write indirect");
892 * Clear the flag bits so the journal can be removed.
900 ino = journal_findfile();
901 if (ino == (ino_t)-1 || ino == 0) {
902 warnx("Journal file does not exist");
905 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
906 if (getinode(&disk, &dp, ino) != 0) {
907 warn("Failed to get journal inode: %s", disk.d_error);
910 if (sblock.fs_magic == FS_UFS1_MAGIC)
911 dp.dp1->di_flags = 0;
913 dp.dp2->di_flags = 0;
914 if (putinode(&disk) < 0) {
915 warn("Failed to write journal inode: %s", disk.d_error);
921 journal_alloc(int64_t size)
936 * If the journal file exists we can't allocate it.
938 ino = journal_findfile();
939 if (ino == (ino_t)-1) {
940 warnx("journal_findfile() failed.");
944 warnx("Journal file %s already exists, please remove.",
949 * If the user didn't supply a size pick one based on the filesystem
950 * size constrained with hardcoded MIN and MAX values. We opt for
951 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
952 * not less than the MIN.
955 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
956 if (size / sblock.fs_fsize > sblock.fs_fpg)
957 size = sblock.fs_fpg * sblock.fs_fsize;
958 size = MAX(SUJ_MIN, size);
960 /* fsck does not support fragments in journal files. */
961 size = roundup(size, sblock.fs_bsize);
962 resid = blocks = size / sblock.fs_bsize;
963 if (sblock.fs_cstotal.cs_nbfree < blocks) {
964 warn("Insufficient free space for %jd byte journal", size);
968 * Find a cg with enough blocks to satisfy the journal
969 * size. Presently the journal does not span cgs.
971 while (cgread(&disk) == 1) {
972 if (cgp->cg_cs.cs_nifree == 0)
974 ino = cgialloc(&disk);
977 printf("Using inode %ju in cg %d for %jd byte journal\n",
978 (uintmax_t)ino, cgp->cg_cgx, size);
979 if (getinode(&disk, &dp, ino) != 0) {
980 warn("Failed to get allocated inode: %s", disk.d_error);
985 * We leave fields unrelated to the number of allocated
986 * blocks and size uninitialized. This causes legacy
987 * fsck implementations to clear the inode.
990 if (sblock.fs_magic == FS_UFS1_MAGIC) {
991 bzero(dp.dp1, sizeof(*dp.dp1));
992 dp.dp1->di_size = size;
993 dp.dp1->di_mode = IFREG | IREAD;
994 dp.dp1->di_nlink = 1;
996 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
997 dp.dp1->di_atime = utime;
998 dp.dp1->di_mtime = utime;
999 dp.dp1->di_ctime = utime;
1001 bzero(dp.dp2, sizeof(*dp.dp2));
1002 dp.dp2->di_size = size;
1003 dp.dp2->di_mode = IFREG | IREAD;
1004 dp.dp2->di_nlink = 1;
1006 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1007 dp.dp2->di_atime = utime;
1008 dp.dp2->di_mtime = utime;
1009 dp.dp2->di_ctime = utime;
1010 dp.dp2->di_birthtime = utime;
1012 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1013 blk = journal_balloc();
1016 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1017 dp.dp1->di_db[i] = blk;
1018 dp.dp1->di_blocks++;
1020 dp.dp2->di_db[i] = blk;
1021 dp.dp2->di_blocks++;
1024 for (i = 0; i < UFS_NIADDR && resid; i++) {
1025 blk = journal_balloc();
1028 blks = indir_fill(blk, i, &resid) + 1;
1033 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1034 dp.dp1->di_ib[i] = blk;
1035 dp.dp1->di_blocks += blks;
1037 dp.dp2->di_ib[i] = blk;
1038 dp.dp2->di_blocks += blks;
1041 if (sblock.fs_magic == FS_UFS1_MAGIC)
1042 dp.dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1044 dp.dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1045 if (putinode(&disk) < 0) {
1046 warn("Failed to write allocated inode: %s",
1051 if (cgwrite(&disk) < 0) {
1052 warn("Failed to write updated cg");
1056 if (journal_insertfile(ino) < 0) {
1060 sblock.fs_sujfree = 0;
1063 warnx("Insufficient free space for the journal.");
1071 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1072 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1073 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1074 " [-L volname] [-l enable | disable] [-m minfree]",
1075 " [-N enable | disable] [-n enable | disable]",
1076 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1077 " special | filesystem");
1084 warnx("POSIX.1e ACLs: (-a) %s",
1085 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1086 warnx("NFSv4 ACLs: (-N) %s",
1087 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1088 warnx("MAC multilabel: (-l) %s",
1089 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1090 warnx("soft updates: (-n) %s",
1091 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1092 warnx("soft update journaling: (-j) %s",
1093 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1094 warnx("gjournal: (-J) %s",
1095 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1096 warnx("trim: (-t) %s",
1097 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1098 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1100 warnx("average file size: (-f) %d",
1101 sblock.fs_avgfilesize);
1102 warnx("average number of files in a directory: (-s) %d",
1103 sblock.fs_avgfpdir);
1104 warnx("minimum percentage of free space: (-m) %d%%",
1106 warnx("space to hold for metadata blocks: (-k) %jd",
1107 sblock.fs_metaspace);
1108 warnx("optimization preference: (-o) %s",
1109 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1110 if (sblock.fs_minfree >= MINFREE &&
1111 sblock.fs_optim == FS_OPTSPACE)
1112 warnx(OPTWARN, "time", ">=", MINFREE);
1113 if (sblock.fs_minfree < MINFREE &&
1114 sblock.fs_optim == FS_OPTTIME)
1115 warnx(OPTWARN, "space", "<", MINFREE);
1116 warnx("volume label: (-L) %s",