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>
45 * tunefs: change layout parameters to an existing file system.
47 #include <sys/param.h>
48 #include <sys/mount.h>
49 #include <sys/disklabel.h>
52 #include <ufs/ufs/extattr.h>
53 #include <ufs/ufs/quota.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) __dead2;
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;
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;
103 Aflag = aflag = eflag = fflag = jflag = Jflag = kflag = Lflag = 0;
104 lflag = mflag = Nflag = nflag = oflag = pflag = sflag = tflag = 0;
105 avalue = jvalue = Jvalue = Lvalue = lvalue = Nvalue = nvalue = NULL;
106 evalue = fvalue = mvalue = ovalue = svalue = Svalue = 0;
107 found_arg = 0; /* At least one arg is required. */
108 while ((ch = getopt(argc, argv, "Aa:e:f:j:J:k:L:l:m:N:n:o:ps:S:t:"))
119 name = "POSIX.1e ACLs";
121 if (strcmp(avalue, "enable") &&
122 strcmp(avalue, "disable")) {
123 errx(10, "bad %s (options are %s)",
124 name, "`enable' or `disable'");
131 name = "maximum blocks per file in a cylinder group";
132 evalue = atoi(optarg);
134 errx(10, "%s must be >= 1 (was %s)",
141 name = "average file size";
142 fvalue = atoi(optarg);
144 errx(10, "%s must be >= 1 (was %s)",
151 name = "softdep journaled file system";
153 if (strcmp(jvalue, "enable") &&
154 strcmp(jvalue, "disable")) {
155 errx(10, "bad %s (options are %s)",
156 name, "`enable' or `disable'");
163 name = "gjournaled file system";
165 if (strcmp(Jvalue, "enable") &&
166 strcmp(Jvalue, "disable")) {
167 errx(10, "bad %s (options are %s)",
168 name, "`enable' or `disable'");
175 name = "space to hold for metadata blocks";
176 kvalue = atoi(optarg);
178 errx(10, "bad %s (%s)", name, optarg);
184 name = "volume label";
187 while (isalnum(Lvalue[++i]) || Lvalue[i] == '_' ||
190 if (Lvalue[i] != '\0') {
191 errx(10, "bad %s. Valid characters are "
192 "alphanumerics, dashes, and underscores.",
195 if (strlen(Lvalue) >= MAXVOLLEN) {
196 errx(10, "bad %s. Length is longer than %d.",
197 name, MAXVOLLEN - 1);
204 name = "multilabel MAC file system";
206 if (strcmp(lvalue, "enable") &&
207 strcmp(lvalue, "disable")) {
208 errx(10, "bad %s (options are %s)",
209 name, "`enable' or `disable'");
216 name = "minimum percentage of free space";
217 mvalue = atoi(optarg);
218 if (mvalue < 0 || mvalue > 99)
219 errx(10, "bad %s (%s)", name, optarg);
227 if (strcmp(Nvalue, "enable") &&
228 strcmp(Nvalue, "disable")) {
229 errx(10, "bad %s (options are %s)",
230 name, "`enable' or `disable'");
237 name = "soft updates";
239 if (strcmp(nvalue, "enable") != 0 &&
240 strcmp(nvalue, "disable") != 0) {
241 errx(10, "bad %s (options are %s)",
242 name, "`enable' or `disable'");
249 name = "optimization preference";
250 if (strcmp(optarg, "space") == 0)
251 ovalue = FS_OPTSPACE;
252 else if (strcmp(optarg, "time") == 0)
256 "bad %s (options are `space' or `time')",
268 name = "expected number of files per directory";
269 svalue = atoi(optarg);
271 errx(10, "%s must be >= 1 (was %s)",
278 name = "Softdep Journal Size";
279 Svalue = atoi(optarg);
280 if (Svalue < SUJ_MIN)
281 errx(10, "%s must be >= %d (was %s)",
282 name, SUJ_MIN, optarg);
289 if (strcmp(tvalue, "enable") != 0 &&
290 strcmp(tvalue, "disable") != 0) {
291 errx(10, "bad %s (options are %s)",
292 name, "`enable' or `disable'");
302 if (found_arg == 0 || argc != 1)
306 if (ufs_disk_fillout(&disk, special) == -1)
309 * Check for unclean filesystem.
311 if ((sblock.fs_clean == 0 ||
312 (sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0) &&
313 (found_arg > 1 || !pflag))
314 errx(1, "%s is not clean - run fsck.\n", special);
320 name = "volume label";
321 strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
324 name = "POSIX.1e ACLs";
325 if (strcmp(avalue, "enable") == 0) {
326 if (sblock.fs_flags & FS_ACLS) {
327 warnx("%s remains unchanged as enabled", name);
328 } else if (sblock.fs_flags & FS_NFS4ACLS) {
329 warnx("%s and NFSv4 ACLs are mutually "
332 sblock.fs_flags |= FS_ACLS;
333 warnx("%s set", name);
335 } else if (strcmp(avalue, "disable") == 0) {
336 if ((~sblock.fs_flags & FS_ACLS) ==
338 warnx("%s remains unchanged as disabled",
341 sblock.fs_flags &= ~FS_ACLS;
342 warnx("%s cleared", name);
347 name = "maximum blocks per file in a cylinder group";
348 if (sblock.fs_maxbpg == evalue)
349 warnx("%s remains unchanged as %d", name, evalue);
351 warnx("%s changes from %d to %d",
352 name, sblock.fs_maxbpg, evalue);
353 sblock.fs_maxbpg = evalue;
357 name = "average file size";
358 if (sblock.fs_avgfilesize == (unsigned)fvalue) {
359 warnx("%s remains unchanged as %d", name, fvalue);
362 warnx("%s changes from %d to %d",
363 name, sblock.fs_avgfilesize, fvalue);
364 sblock.fs_avgfilesize = fvalue;
368 name = "soft updates journaling";
369 if (strcmp(jvalue, "enable") == 0) {
370 if ((sblock.fs_flags & (FS_DOSOFTDEP | FS_SUJ)) ==
371 (FS_DOSOFTDEP | FS_SUJ)) {
372 warnx("%s remains unchanged as enabled", name);
373 } else if (sblock.fs_clean == 0) {
374 warnx("%s cannot be enabled until fsck is run",
376 } else if (journal_alloc(Svalue) != 0) {
377 warnx("%s cannot be enabled", name);
379 sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
380 warnx("%s set", name);
382 } else if (strcmp(jvalue, "disable") == 0) {
383 if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
384 warnx("%s remains unchanged as disabled", name);
387 sblock.fs_flags &= ~FS_SUJ;
388 sblock.fs_sujfree = 0;
389 warnx("%s cleared but soft updates still set.",
392 warnx("remove .sujournal to reclaim space");
398 if (strcmp(Jvalue, "enable") == 0) {
399 if (sblock.fs_flags & FS_GJOURNAL) {
400 warnx("%s remains unchanged as enabled", name);
402 sblock.fs_flags |= FS_GJOURNAL;
403 warnx("%s set", name);
405 } else if (strcmp(Jvalue, "disable") == 0) {
406 if ((~sblock.fs_flags & FS_GJOURNAL) ==
408 warnx("%s remains unchanged as disabled",
411 sblock.fs_flags &= ~FS_GJOURNAL;
412 warnx("%s cleared", name);
417 name = "space to hold for metadata blocks";
418 if (sblock.fs_metaspace == kvalue)
419 warnx("%s remains unchanged as %d", name, kvalue);
421 kvalue = blknum(&sblock, kvalue);
422 if (kvalue > sblock.fs_fpg / 2) {
423 kvalue = blknum(&sblock, sblock.fs_fpg / 2);
424 warnx("%s cannot exceed half the file system "
427 warnx("%s changes from %jd to %d",
428 name, sblock.fs_metaspace, kvalue);
429 sblock.fs_metaspace = kvalue;
434 if (strcmp(lvalue, "enable") == 0) {
435 if (sblock.fs_flags & FS_MULTILABEL) {
436 warnx("%s remains unchanged as enabled", name);
438 sblock.fs_flags |= FS_MULTILABEL;
439 warnx("%s set", name);
441 } else if (strcmp(lvalue, "disable") == 0) {
442 if ((~sblock.fs_flags & FS_MULTILABEL) ==
444 warnx("%s remains unchanged as disabled",
447 sblock.fs_flags &= ~FS_MULTILABEL;
448 warnx("%s cleared", name);
453 name = "minimum percentage of free space";
454 if (sblock.fs_minfree == mvalue)
455 warnx("%s remains unchanged as %d%%", name, mvalue);
457 warnx("%s changes from %d%% to %d%%",
458 name, sblock.fs_minfree, mvalue);
459 sblock.fs_minfree = mvalue;
460 if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
461 warnx(OPTWARN, "time", ">=", MINFREE);
462 if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
463 warnx(OPTWARN, "space", "<", MINFREE);
468 if (strcmp(Nvalue, "enable") == 0) {
469 if (sblock.fs_flags & FS_NFS4ACLS) {
470 warnx("%s remains unchanged as enabled", name);
471 } else if (sblock.fs_flags & FS_ACLS) {
472 warnx("%s and POSIX.1e ACLs are mutually "
475 sblock.fs_flags |= FS_NFS4ACLS;
476 warnx("%s set", name);
478 } else if (strcmp(Nvalue, "disable") == 0) {
479 if ((~sblock.fs_flags & FS_NFS4ACLS) ==
481 warnx("%s remains unchanged as disabled",
484 sblock.fs_flags &= ~FS_NFS4ACLS;
485 warnx("%s cleared", name);
490 name = "soft updates";
491 if (strcmp(nvalue, "enable") == 0) {
492 if (sblock.fs_flags & FS_DOSOFTDEP)
493 warnx("%s remains unchanged as enabled", name);
494 else if (sblock.fs_clean == 0) {
495 warnx("%s cannot be enabled until fsck is run",
498 sblock.fs_flags |= FS_DOSOFTDEP;
499 warnx("%s set", name);
501 } else if (strcmp(nvalue, "disable") == 0) {
502 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
503 warnx("%s remains unchanged as disabled", name);
505 sblock.fs_flags &= ~FS_DOSOFTDEP;
506 warnx("%s cleared", name);
511 name = "optimization preference";
512 chg[FS_OPTSPACE] = "space";
513 chg[FS_OPTTIME] = "time";
514 if (sblock.fs_optim == ovalue)
515 warnx("%s remains unchanged as %s", name, chg[ovalue]);
517 warnx("%s changes from %s to %s",
518 name, chg[sblock.fs_optim], chg[ovalue]);
519 sblock.fs_optim = ovalue;
520 if (sblock.fs_minfree >= MINFREE &&
521 ovalue == FS_OPTSPACE)
522 warnx(OPTWARN, "time", ">=", MINFREE);
523 if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME)
524 warnx(OPTWARN, "space", "<", MINFREE);
528 name = "expected number of files per directory";
529 if (sblock.fs_avgfpdir == (unsigned)svalue) {
530 warnx("%s remains unchanged as %d", name, svalue);
533 warnx("%s changes from %d to %d",
534 name, sblock.fs_avgfpdir, svalue);
535 sblock.fs_avgfpdir = svalue;
539 name = "issue TRIM to the disk";
540 if (strcmp(tvalue, "enable") == 0) {
541 if (sblock.fs_flags & FS_TRIM)
542 warnx("%s remains unchanged as enabled", name);
544 sblock.fs_flags |= FS_TRIM;
545 warnx("%s set", name);
547 } else if (strcmp(tvalue, "disable") == 0) {
548 if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
549 warnx("%s remains unchanged as disabled", name);
551 sblock.fs_flags &= ~FS_TRIM;
552 warnx("%s cleared", name);
557 if (sbwrite(&disk, Aflag) == -1)
559 diskname = strdup(disk.d_name);
560 ufs_disk_close(&disk);
561 chkdoreload(getmntpoint(diskname), warnx);
564 if (disk.d_error != NULL)
565 errx(11, "%s: %s", special, disk.d_error);
567 err(12, "%s", special);
573 disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
574 disk.d_fs.fs_clean = 0;
583 static int contig = 1;
587 blk = cgballoc(&disk);
591 * If we failed to allocate a block from this cg, move to
594 if (cgwrite(&disk) < 0) {
595 warn("Failed to write updated cg");
598 while ((valid = cgread(&disk)) == 1) {
600 * Try to minimize fragmentation by requiring a minimum
601 * number of blocks present.
603 if (cgp->cg_cs.cs_nbfree > 256 * 1024)
605 if (contig == 0 && cgp->cg_cs.cs_nbfree)
611 * Try once through looking only for large contiguous regions
612 * and again taking any space we can find.
617 warnx("Journal file fragmented.");
620 warnx("Failed to find sufficient free blocks for the journal");
623 if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
624 sblock.fs_bsize) <= 0) {
625 warn("Failed to initialize new block");
632 * Search a directory block for the SUJ_FILE.
635 dir_search(ufs2_daddr_t blk, int bytes)
637 char block[MAXBSIZE];
641 if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
642 warn("Failed to read dir block");
645 for (off = 0; off < bytes; off += dp->d_reclen) {
646 dp = (struct direct *)&block[off];
647 if (dp->d_reclen == 0)
651 if (dp->d_namlen != strlen(SUJ_FILE))
653 if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
662 * Search in the UFS_ROOTINO for the SUJ_FILE. If it exists we can not enable
666 journal_findfile(void)
672 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
673 warn("Failed to get root inode: %s", disk.d_error);
676 if (sblock.fs_magic == FS_UFS1_MAGIC) {
677 if ((off_t)dp.dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
678 warnx("UFS_ROOTINO extends beyond direct blocks.");
681 for (i = 0; i < UFS_NDADDR; i++) {
682 if (dp.dp1->di_db[i] == 0)
684 if ((ino = dir_search(dp.dp1->di_db[i],
685 sblksize(&sblock, (off_t)dp.dp1->di_size, i))) != 0)
689 if ((off_t)dp.dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
690 warnx("UFS_ROOTINO extends beyond direct blocks.");
693 for (i = 0; i < UFS_NDADDR; i++) {
694 if (dp.dp2->di_db[i] == 0)
696 if ((ino = dir_search(dp.dp2->di_db[i],
697 sblksize(&sblock, (off_t)dp.dp2->di_size, i))) != 0)
706 dir_clear_block(const char *block, off_t off)
710 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
711 dp = (struct direct *)&block[off];
713 dp->d_reclen = DIRBLKSIZ;
714 dp->d_type = DT_UNKNOWN;
719 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
720 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
724 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
727 char block[MAXBSIZE];
729 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
730 warn("Failed to read dir block");
733 bzero(&block[off], sblock.fs_bsize - off);
734 dp = (struct direct *)&block[off];
736 dp->d_reclen = DIRBLKSIZ;
738 dp->d_namlen = strlen(SUJ_FILE);
739 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
740 dir_clear_block(block, off + DIRBLKSIZ);
741 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
742 warn("Failed to write dir block");
749 * Extend a directory block in 'blk' by copying it to a full size block
750 * and inserting the new journal inode into .sujournal.
753 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
755 char block[MAXBSIZE];
757 if (bread(&disk, fsbtodb(&sblock, blk), block,
758 roundup(size, sblock.fs_fsize)) <= 0) {
759 warn("Failed to read dir block");
762 dir_clear_block(block, size);
763 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
765 warn("Failed to write dir block");
769 return (dir_insert(nblk, size, ino));
773 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
777 journal_insertfile(ino_t ino)
786 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
787 warn("Failed to get root inode: %s", disk.d_error);
793 nblk = journal_balloc();
797 * For simplicity sake we aways extend the UFS_ROOTINO into a new
798 * directory block rather than searching for space and inserting
799 * into an existing block. However, if the rootino has frags
800 * have to free them and extend the block.
802 if (sblock.fs_magic == FS_UFS1_MAGIC) {
803 lbn = lblkno(&sblock, dp.dp1->di_size);
804 off = blkoff(&sblock, dp.dp1->di_size);
805 blk = dp.dp1->di_db[lbn];
806 size = sblksize(&sblock, (off_t)dp.dp1->di_size, lbn);
808 lbn = lblkno(&sblock, dp.dp2->di_size);
809 off = blkoff(&sblock, dp.dp2->di_size);
810 blk = dp.dp2->di_db[lbn];
811 size = sblksize(&sblock, (off_t)dp.dp2->di_size, lbn);
814 if (dir_extend(blk, nblk, off, ino) == -1)
818 if (dir_insert(nblk, 0, ino) == -1)
821 if (sblock.fs_magic == FS_UFS1_MAGIC) {
822 dp.dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
823 dp.dp1->di_db[lbn] = nblk;
824 dp.dp1->di_size = lblktosize(&sblock, lbn+1);
826 dp.dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
827 dp.dp2->di_db[lbn] = nblk;
828 dp.dp2->di_size = lblktosize(&sblock, lbn+1);
830 if (putinode(&disk) < 0) {
831 warn("Failed to write root inode: %s", disk.d_error);
834 if (cgwrite(&disk) < 0) {
835 warn("Failed to write updated cg");
840 if (cgbfree(&disk, blk, size) < 0) {
841 warn("Failed to write cg");
850 indir_fill(ufs2_daddr_t blk, int level, int *resid)
852 char indirbuf[MAXBSIZE];
860 bzero(indirbuf, sizeof(indirbuf));
861 bap1 = (ufs1_daddr_t *)indirbuf;
864 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
865 nblk = journal_balloc();
869 if (sblock.fs_magic == FS_UFS1_MAGIC)
874 ncnt = indir_fill(nblk, level - 1, resid);
881 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
882 sblock.fs_bsize) <= 0) {
883 warn("Failed to write indirect");
890 * Clear the flag bits so the journal can be removed.
898 ino = journal_findfile();
899 if (ino == (ino_t)-1 || ino == 0) {
900 warnx("Journal file does not exist");
903 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
904 if (getinode(&disk, &dp, ino) != 0) {
905 warn("Failed to get journal inode: %s", disk.d_error);
908 if (sblock.fs_magic == FS_UFS1_MAGIC)
909 dp.dp1->di_flags = 0;
911 dp.dp2->di_flags = 0;
912 if (putinode(&disk) < 0) {
913 warn("Failed to write journal inode: %s", disk.d_error);
919 journal_alloc(int64_t size)
934 * If the journal file exists we can't allocate it.
936 ino = journal_findfile();
937 if (ino == (ino_t)-1) {
938 warnx("journal_findfile() failed.");
942 warnx("Journal file %s already exists, please remove.",
947 * If the user didn't supply a size pick one based on the filesystem
948 * size constrained with hardcoded MIN and MAX values. We opt for
949 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
950 * not less than the MIN.
953 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
954 if (size / sblock.fs_fsize > sblock.fs_fpg)
955 size = sblock.fs_fpg * sblock.fs_fsize;
956 size = MAX(SUJ_MIN, size);
958 /* fsck does not support fragments in journal files. */
959 size = roundup(size, sblock.fs_bsize);
960 resid = blocks = size / sblock.fs_bsize;
961 if (sblock.fs_cstotal.cs_nbfree < blocks) {
962 warn("Insufficient free space for %jd byte journal", size);
966 * Find a cg with enough blocks to satisfy the journal
967 * size. Presently the journal does not span cgs.
969 while (cgread(&disk) == 1) {
970 if (cgp->cg_cs.cs_nifree == 0)
972 ino = cgialloc(&disk);
975 printf("Using inode %ju in cg %d for %jd byte journal\n",
976 (uintmax_t)ino, cgp->cg_cgx, size);
977 if (getinode(&disk, &dp, ino) != 0) {
978 warn("Failed to get allocated inode: %s", disk.d_error);
983 * We leave fields unrelated to the number of allocated
984 * blocks and size uninitialized. This causes legacy
985 * fsck implementations to clear the inode.
988 if (sblock.fs_magic == FS_UFS1_MAGIC) {
989 bzero(dp.dp1, sizeof(*dp.dp1));
990 dp.dp1->di_size = size;
991 dp.dp1->di_mode = IFREG | IREAD;
992 dp.dp1->di_nlink = 1;
994 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
995 dp.dp1->di_atime = utime;
996 dp.dp1->di_mtime = utime;
997 dp.dp1->di_ctime = utime;
999 bzero(dp.dp2, sizeof(*dp.dp2));
1000 dp.dp2->di_size = size;
1001 dp.dp2->di_mode = IFREG | IREAD;
1002 dp.dp2->di_nlink = 1;
1004 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1005 dp.dp2->di_atime = utime;
1006 dp.dp2->di_mtime = utime;
1007 dp.dp2->di_ctime = utime;
1008 dp.dp2->di_birthtime = utime;
1010 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1011 blk = journal_balloc();
1014 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1015 dp.dp1->di_db[i] = blk;
1016 dp.dp1->di_blocks++;
1018 dp.dp2->di_db[i] = blk;
1019 dp.dp2->di_blocks++;
1022 for (i = 0; i < UFS_NIADDR && resid; i++) {
1023 blk = journal_balloc();
1026 blks = indir_fill(blk, i, &resid) + 1;
1031 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1032 dp.dp1->di_ib[i] = blk;
1033 dp.dp1->di_blocks += blks;
1035 dp.dp2->di_ib[i] = blk;
1036 dp.dp2->di_blocks += blks;
1039 if (sblock.fs_magic == FS_UFS1_MAGIC)
1040 dp.dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1042 dp.dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1043 if (putinode(&disk) < 0) {
1044 warn("Failed to write allocated inode: %s",
1049 if (cgwrite(&disk) < 0) {
1050 warn("Failed to write updated cg");
1054 if (journal_insertfile(ino) < 0) {
1058 sblock.fs_sujfree = 0;
1061 warnx("Insufficient free space for the journal.");
1069 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1070 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1071 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1072 " [-L volname] [-l enable | disable] [-m minfree]",
1073 " [-N enable | disable] [-n enable | disable]",
1074 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1075 " special | filesystem");
1082 warnx("POSIX.1e ACLs: (-a) %s",
1083 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1084 warnx("NFSv4 ACLs: (-N) %s",
1085 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1086 warnx("MAC multilabel: (-l) %s",
1087 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1088 warnx("soft updates: (-n) %s",
1089 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1090 warnx("soft update journaling: (-j) %s",
1091 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1092 warnx("gjournal: (-J) %s",
1093 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1094 warnx("trim: (-t) %s",
1095 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1096 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1098 warnx("average file size: (-f) %d",
1099 sblock.fs_avgfilesize);
1100 warnx("average number of files in a directory: (-s) %d",
1101 sblock.fs_avgfpdir);
1102 warnx("minimum percentage of free space: (-m) %d%%",
1104 warnx("space to hold for metadata blocks: (-k) %jd",
1105 sblock.fs_metaspace);
1106 warnx("optimization preference: (-o) %s",
1107 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1108 if (sblock.fs_minfree >= MINFREE &&
1109 sblock.fs_optim == FS_OPTSPACE)
1110 warnx(OPTWARN, "time", ">=", MINFREE);
1111 if (sblock.fs_minfree < MINFREE &&
1112 sblock.fs_optim == FS_OPTTIME)
1113 warnx(OPTWARN, "space", "<", MINFREE);
1114 warnx("volume label: (-L) %s",