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;
94 const char *special, *on;
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;
104 struct iovec *iov = NULL;
105 char errmsg[255] = {0};
109 Aflag = aflag = eflag = fflag = jflag = Jflag = kflag = Lflag = 0;
110 lflag = mflag = Nflag = nflag = oflag = pflag = sflag = tflag = 0;
111 avalue = jvalue = Jvalue = Lvalue = lvalue = Nvalue = nvalue = NULL;
112 evalue = fvalue = mvalue = ovalue = svalue = Svalue = 0;
114 found_arg = 0; /* At least one arg is required. */
115 while ((ch = getopt(argc, argv, "Aa:e:f:j:J:k:L:l:m:N:n:o:ps:S:t:"))
126 name = "POSIX.1e ACLs";
128 if (strcmp(avalue, "enable") &&
129 strcmp(avalue, "disable")) {
130 errx(10, "bad %s (options are %s)",
131 name, "`enable' or `disable'");
138 name = "maximum blocks per file in a cylinder group";
139 evalue = atoi(optarg);
141 errx(10, "%s must be >= 1 (was %s)",
148 name = "average file size";
149 fvalue = atoi(optarg);
151 errx(10, "%s must be >= 1 (was %s)",
158 name = "softdep journaled file system";
160 if (strcmp(jvalue, "enable") &&
161 strcmp(jvalue, "disable")) {
162 errx(10, "bad %s (options are %s)",
163 name, "`enable' or `disable'");
170 name = "gjournaled file system";
172 if (strcmp(Jvalue, "enable") &&
173 strcmp(Jvalue, "disable")) {
174 errx(10, "bad %s (options are %s)",
175 name, "`enable' or `disable'");
182 name = "space to hold for metadata blocks";
183 kvalue = atoi(optarg);
185 errx(10, "bad %s (%s)", name, optarg);
191 name = "volume label";
194 while (isalnum(Lvalue[++i]) || Lvalue[i] == '_' ||
197 if (Lvalue[i] != '\0') {
198 errx(10, "bad %s. Valid characters are "
199 "alphanumerics, dashes, and underscores.",
202 if (strlen(Lvalue) >= MAXVOLLEN) {
203 errx(10, "bad %s. Length is longer than %d.",
204 name, MAXVOLLEN - 1);
211 name = "multilabel MAC file system";
213 if (strcmp(lvalue, "enable") &&
214 strcmp(lvalue, "disable")) {
215 errx(10, "bad %s (options are %s)",
216 name, "`enable' or `disable'");
223 name = "minimum percentage of free space";
224 mvalue = atoi(optarg);
225 if (mvalue < 0 || mvalue > 99)
226 errx(10, "bad %s (%s)", name, optarg);
234 if (strcmp(Nvalue, "enable") &&
235 strcmp(Nvalue, "disable")) {
236 errx(10, "bad %s (options are %s)",
237 name, "`enable' or `disable'");
244 name = "soft updates";
246 if (strcmp(nvalue, "enable") != 0 &&
247 strcmp(nvalue, "disable") != 0) {
248 errx(10, "bad %s (options are %s)",
249 name, "`enable' or `disable'");
256 name = "optimization preference";
257 if (strcmp(optarg, "space") == 0)
258 ovalue = FS_OPTSPACE;
259 else if (strcmp(optarg, "time") == 0)
263 "bad %s (options are `space' or `time')",
275 name = "expected number of files per directory";
276 svalue = atoi(optarg);
278 errx(10, "%s must be >= 1 (was %s)",
285 name = "Softdep Journal Size";
286 Svalue = atoi(optarg);
287 if (Svalue < SUJ_MIN)
288 errx(10, "%s must be >= %d (was %s)",
289 name, SUJ_MIN, optarg);
296 if (strcmp(tvalue, "enable") != 0 &&
297 strcmp(tvalue, "disable") != 0) {
298 errx(10, "bad %s (options are %s)",
299 name, "`enable' or `disable'");
309 if (found_arg == 0 || argc != 1)
312 on = special = argv[0];
313 if (ufs_disk_fillout(&disk, special) == -1)
316 * Check for unclean filesystem.
318 if ((sblock.fs_clean == 0 ||
319 (sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0) &&
320 (found_arg > 1 || !pflag))
321 errx(1, "%s is not clean - run fsck.\n", special);
322 if (disk.d_name != special) {
323 if (statfs(special, &stfs) != 0)
324 warn("Can't stat %s", special);
325 if (strcmp(special, stfs.f_mntonname) == 0)
334 name = "volume label";
335 strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
338 name = "POSIX.1e ACLs";
339 if (strcmp(avalue, "enable") == 0) {
340 if (sblock.fs_flags & FS_ACLS) {
341 warnx("%s remains unchanged as enabled", name);
342 } else if (sblock.fs_flags & FS_NFS4ACLS) {
343 warnx("%s and NFSv4 ACLs are mutually "
346 sblock.fs_flags |= FS_ACLS;
347 warnx("%s set", name);
349 } else if (strcmp(avalue, "disable") == 0) {
350 if ((~sblock.fs_flags & FS_ACLS) ==
352 warnx("%s remains unchanged as disabled",
355 sblock.fs_flags &= ~FS_ACLS;
356 warnx("%s cleared", name);
361 name = "maximum blocks per file in a cylinder group";
362 if (sblock.fs_maxbpg == evalue)
363 warnx("%s remains unchanged as %d", name, evalue);
365 warnx("%s changes from %d to %d",
366 name, sblock.fs_maxbpg, evalue);
367 sblock.fs_maxbpg = evalue;
371 name = "average file size";
372 if (sblock.fs_avgfilesize == (unsigned)fvalue) {
373 warnx("%s remains unchanged as %d", name, fvalue);
376 warnx("%s changes from %d to %d",
377 name, sblock.fs_avgfilesize, fvalue);
378 sblock.fs_avgfilesize = fvalue;
382 name = "soft updates journaling";
383 if (strcmp(jvalue, "enable") == 0) {
384 if ((sblock.fs_flags & (FS_DOSOFTDEP | FS_SUJ)) ==
385 (FS_DOSOFTDEP | FS_SUJ)) {
386 warnx("%s remains unchanged as enabled", name);
387 } else if (sblock.fs_clean == 0) {
388 warnx("%s cannot be enabled until fsck is run",
390 } else if (journal_alloc(Svalue) != 0) {
391 warnx("%s cannot be enabled", name);
393 sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
394 warnx("%s set", name);
396 } else if (strcmp(jvalue, "disable") == 0) {
397 if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
398 warnx("%s remains unchanged as disabled", name);
401 sblock.fs_flags &= ~FS_SUJ;
402 sblock.fs_sujfree = 0;
403 warnx("%s cleared but soft updates still set.",
406 warnx("remove .sujournal to reclaim space");
412 if (strcmp(Jvalue, "enable") == 0) {
413 if (sblock.fs_flags & FS_GJOURNAL) {
414 warnx("%s remains unchanged as enabled", name);
416 sblock.fs_flags |= FS_GJOURNAL;
417 warnx("%s set", name);
419 } else if (strcmp(Jvalue, "disable") == 0) {
420 if ((~sblock.fs_flags & FS_GJOURNAL) ==
422 warnx("%s remains unchanged as disabled",
425 sblock.fs_flags &= ~FS_GJOURNAL;
426 warnx("%s cleared", name);
431 name = "space to hold for metadata blocks";
432 if (sblock.fs_metaspace == kvalue)
433 warnx("%s remains unchanged as %d", name, kvalue);
435 kvalue = blknum(&sblock, kvalue);
436 if (kvalue > sblock.fs_fpg / 2) {
437 kvalue = blknum(&sblock, sblock.fs_fpg / 2);
438 warnx("%s cannot exceed half the file system "
441 warnx("%s changes from %jd to %d",
442 name, sblock.fs_metaspace, kvalue);
443 sblock.fs_metaspace = kvalue;
448 if (strcmp(lvalue, "enable") == 0) {
449 if (sblock.fs_flags & FS_MULTILABEL) {
450 warnx("%s remains unchanged as enabled", name);
452 sblock.fs_flags |= FS_MULTILABEL;
453 warnx("%s set", name);
455 } else if (strcmp(lvalue, "disable") == 0) {
456 if ((~sblock.fs_flags & FS_MULTILABEL) ==
458 warnx("%s remains unchanged as disabled",
461 sblock.fs_flags &= ~FS_MULTILABEL;
462 warnx("%s cleared", name);
467 name = "minimum percentage of free space";
468 if (sblock.fs_minfree == mvalue)
469 warnx("%s remains unchanged as %d%%", name, mvalue);
471 warnx("%s changes from %d%% to %d%%",
472 name, sblock.fs_minfree, mvalue);
473 sblock.fs_minfree = mvalue;
474 if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
475 warnx(OPTWARN, "time", ">=", MINFREE);
476 if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
477 warnx(OPTWARN, "space", "<", MINFREE);
482 if (strcmp(Nvalue, "enable") == 0) {
483 if (sblock.fs_flags & FS_NFS4ACLS) {
484 warnx("%s remains unchanged as enabled", name);
485 } else if (sblock.fs_flags & FS_ACLS) {
486 warnx("%s and POSIX.1e ACLs are mutually "
489 sblock.fs_flags |= FS_NFS4ACLS;
490 warnx("%s set", name);
492 } else if (strcmp(Nvalue, "disable") == 0) {
493 if ((~sblock.fs_flags & FS_NFS4ACLS) ==
495 warnx("%s remains unchanged as disabled",
498 sblock.fs_flags &= ~FS_NFS4ACLS;
499 warnx("%s cleared", name);
504 name = "soft updates";
505 if (strcmp(nvalue, "enable") == 0) {
506 if (sblock.fs_flags & FS_DOSOFTDEP)
507 warnx("%s remains unchanged as enabled", name);
508 else if (sblock.fs_clean == 0) {
509 warnx("%s cannot be enabled until fsck is run",
512 sblock.fs_flags |= FS_DOSOFTDEP;
513 warnx("%s set", name);
515 } else if (strcmp(nvalue, "disable") == 0) {
516 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
517 warnx("%s remains unchanged as disabled", name);
519 sblock.fs_flags &= ~FS_DOSOFTDEP;
520 warnx("%s cleared", name);
525 name = "optimization preference";
526 chg[FS_OPTSPACE] = "space";
527 chg[FS_OPTTIME] = "time";
528 if (sblock.fs_optim == ovalue)
529 warnx("%s remains unchanged as %s", name, chg[ovalue]);
531 warnx("%s changes from %s to %s",
532 name, chg[sblock.fs_optim], chg[ovalue]);
533 sblock.fs_optim = ovalue;
534 if (sblock.fs_minfree >= MINFREE &&
535 ovalue == FS_OPTSPACE)
536 warnx(OPTWARN, "time", ">=", MINFREE);
537 if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME)
538 warnx(OPTWARN, "space", "<", MINFREE);
542 name = "expected number of files per directory";
543 if (sblock.fs_avgfpdir == (unsigned)svalue) {
544 warnx("%s remains unchanged as %d", name, svalue);
547 warnx("%s changes from %d to %d",
548 name, sblock.fs_avgfpdir, svalue);
549 sblock.fs_avgfpdir = svalue;
553 name = "issue TRIM to the disk";
554 if (strcmp(tvalue, "enable") == 0) {
555 if (sblock.fs_flags & FS_TRIM)
556 warnx("%s remains unchanged as enabled", name);
558 sblock.fs_flags |= FS_TRIM;
559 warnx("%s set", name);
561 } else if (strcmp(tvalue, "disable") == 0) {
562 if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
563 warnx("%s remains unchanged as disabled", name);
565 sblock.fs_flags &= ~FS_TRIM;
566 warnx("%s cleared", name);
571 if (sbwrite(&disk, Aflag) == -1)
573 ufs_disk_close(&disk);
575 build_iovec_argf(&iov, &iovlen, "fstype", "ufs");
576 build_iovec_argf(&iov, &iovlen, "fspath", "%s", on);
577 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
578 if (nmount(iov, iovlen,
579 stfs.f_flags | MNT_UPDATE | MNT_RELOAD) < 0) {
581 err(9, "%s: reload: %s", special, errmsg);
583 err(9, "%s: reload", special);
585 warnx("file system reloaded");
589 if (disk.d_error != NULL)
590 errx(11, "%s: %s", special, disk.d_error);
592 err(12, "%s", special);
598 disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
599 disk.d_fs.fs_clean = 0;
608 static int contig = 1;
612 blk = cgballoc(&disk);
616 * If we failed to allocate a block from this cg, move to
619 if (cgwrite(&disk) < 0) {
620 warn("Failed to write updated cg");
623 while ((valid = cgread(&disk)) == 1) {
625 * Try to minimize fragmentation by requiring a minimum
626 * number of blocks present.
628 if (cgp->cg_cs.cs_nbfree > 256 * 1024)
630 if (contig == 0 && cgp->cg_cs.cs_nbfree)
636 * Try once through looking only for large contiguous regions
637 * and again taking any space we can find.
642 warnx("Journal file fragmented.");
645 warnx("Failed to find sufficient free blocks for the journal");
648 if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
649 sblock.fs_bsize) <= 0) {
650 warn("Failed to initialize new block");
657 * Search a directory block for the SUJ_FILE.
660 dir_search(ufs2_daddr_t blk, int bytes)
662 char block[MAXBSIZE];
666 if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
667 warn("Failed to read dir block");
670 for (off = 0; off < bytes; off += dp->d_reclen) {
671 dp = (struct direct *)&block[off];
672 if (dp->d_reclen == 0)
676 if (dp->d_namlen != strlen(SUJ_FILE))
678 if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
687 * Search in the UFS_ROOTINO for the SUJ_FILE. If it exists we can not enable
691 journal_findfile(void)
697 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
698 warn("Failed to get root inode: %s", disk.d_error);
701 if (sblock.fs_magic == FS_UFS1_MAGIC) {
702 if ((off_t)dp.dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
703 warnx("UFS_ROOTINO extends beyond direct blocks.");
706 for (i = 0; i < UFS_NDADDR; i++) {
707 if (dp.dp1->di_db[i] == 0)
709 if ((ino = dir_search(dp.dp1->di_db[i],
710 sblksize(&sblock, (off_t)dp.dp1->di_size, i))) != 0)
714 if ((off_t)dp.dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
715 warnx("UFS_ROOTINO extends beyond direct blocks.");
718 for (i = 0; i < UFS_NDADDR; i++) {
719 if (dp.dp2->di_db[i] == 0)
721 if ((ino = dir_search(dp.dp2->di_db[i],
722 sblksize(&sblock, (off_t)dp.dp2->di_size, i))) != 0)
731 dir_clear_block(const char *block, off_t off)
735 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
736 dp = (struct direct *)&block[off];
738 dp->d_reclen = DIRBLKSIZ;
739 dp->d_type = DT_UNKNOWN;
744 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
745 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
749 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
752 char block[MAXBSIZE];
754 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
755 warn("Failed to read dir block");
758 bzero(&block[off], sblock.fs_bsize - off);
759 dp = (struct direct *)&block[off];
761 dp->d_reclen = DIRBLKSIZ;
763 dp->d_namlen = strlen(SUJ_FILE);
764 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
765 dir_clear_block(block, off + DIRBLKSIZ);
766 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
767 warn("Failed to write dir block");
774 * Extend a directory block in 'blk' by copying it to a full size block
775 * and inserting the new journal inode into .sujournal.
778 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
780 char block[MAXBSIZE];
782 if (bread(&disk, fsbtodb(&sblock, blk), block,
783 roundup(size, sblock.fs_fsize)) <= 0) {
784 warn("Failed to read dir block");
787 dir_clear_block(block, size);
788 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
790 warn("Failed to write dir block");
794 return (dir_insert(nblk, size, ino));
798 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
802 journal_insertfile(ino_t ino)
811 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
812 warn("Failed to get root inode: %s", disk.d_error);
818 nblk = journal_balloc();
822 * For simplicity sake we aways extend the UFS_ROOTINO into a new
823 * directory block rather than searching for space and inserting
824 * into an existing block. However, if the rootino has frags
825 * have to free them and extend the block.
827 if (sblock.fs_magic == FS_UFS1_MAGIC) {
828 lbn = lblkno(&sblock, dp.dp1->di_size);
829 off = blkoff(&sblock, dp.dp1->di_size);
830 blk = dp.dp1->di_db[lbn];
831 size = sblksize(&sblock, (off_t)dp.dp1->di_size, lbn);
833 lbn = lblkno(&sblock, dp.dp2->di_size);
834 off = blkoff(&sblock, dp.dp2->di_size);
835 blk = dp.dp2->di_db[lbn];
836 size = sblksize(&sblock, (off_t)dp.dp2->di_size, lbn);
839 if (dir_extend(blk, nblk, off, ino) == -1)
843 if (dir_insert(nblk, 0, ino) == -1)
846 if (sblock.fs_magic == FS_UFS1_MAGIC) {
847 dp.dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
848 dp.dp1->di_db[lbn] = nblk;
849 dp.dp1->di_size = lblktosize(&sblock, lbn+1);
851 dp.dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
852 dp.dp2->di_db[lbn] = nblk;
853 dp.dp2->di_size = lblktosize(&sblock, lbn+1);
855 if (putinode(&disk) < 0) {
856 warn("Failed to write root inode: %s", disk.d_error);
859 if (cgwrite(&disk) < 0) {
860 warn("Failed to write updated cg");
865 if (cgbfree(&disk, blk, size) < 0) {
866 warn("Failed to write cg");
875 indir_fill(ufs2_daddr_t blk, int level, int *resid)
877 char indirbuf[MAXBSIZE];
885 bzero(indirbuf, sizeof(indirbuf));
886 bap1 = (ufs1_daddr_t *)indirbuf;
889 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
890 nblk = journal_balloc();
894 if (sblock.fs_magic == FS_UFS1_MAGIC)
899 ncnt = indir_fill(nblk, level - 1, resid);
906 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
907 sblock.fs_bsize) <= 0) {
908 warn("Failed to write indirect");
915 * Clear the flag bits so the journal can be removed.
923 ino = journal_findfile();
924 if (ino == (ino_t)-1 || ino == 0) {
925 warnx("Journal file does not exist");
928 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
929 if (getinode(&disk, &dp, ino) != 0) {
930 warn("Failed to get journal inode: %s", disk.d_error);
933 if (sblock.fs_magic == FS_UFS1_MAGIC)
934 dp.dp1->di_flags = 0;
936 dp.dp2->di_flags = 0;
937 if (putinode(&disk) < 0) {
938 warn("Failed to write journal inode: %s", disk.d_error);
944 journal_alloc(int64_t size)
959 * If the journal file exists we can't allocate it.
961 ino = journal_findfile();
962 if (ino == (ino_t)-1) {
963 warnx("journal_findfile() failed.");
967 warnx("Journal file %s already exists, please remove.",
972 * If the user didn't supply a size pick one based on the filesystem
973 * size constrained with hardcoded MIN and MAX values. We opt for
974 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
975 * not less than the MIN.
978 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
979 if (size / sblock.fs_fsize > sblock.fs_fpg)
980 size = sblock.fs_fpg * sblock.fs_fsize;
981 size = MAX(SUJ_MIN, size);
983 /* fsck does not support fragments in journal files. */
984 size = roundup(size, sblock.fs_bsize);
985 resid = blocks = size / sblock.fs_bsize;
986 if (sblock.fs_cstotal.cs_nbfree < blocks) {
987 warn("Insufficient free space for %jd byte journal", size);
991 * Find a cg with enough blocks to satisfy the journal
992 * size. Presently the journal does not span cgs.
994 while (cgread(&disk) == 1) {
995 if (cgp->cg_cs.cs_nifree == 0)
997 ino = cgialloc(&disk);
1000 printf("Using inode %ju in cg %d for %jd byte journal\n",
1001 (uintmax_t)ino, cgp->cg_cgx, size);
1002 if (getinode(&disk, &dp, ino) != 0) {
1003 warn("Failed to get allocated inode: %s", disk.d_error);
1008 * We leave fields unrelated to the number of allocated
1009 * blocks and size uninitialized. This causes legacy
1010 * fsck implementations to clear the inode.
1013 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1014 bzero(dp.dp1, sizeof(*dp.dp1));
1015 dp.dp1->di_size = size;
1016 dp.dp1->di_mode = IFREG | IREAD;
1017 dp.dp1->di_nlink = 1;
1019 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1020 dp.dp1->di_atime = utime;
1021 dp.dp1->di_mtime = utime;
1022 dp.dp1->di_ctime = utime;
1024 bzero(dp.dp2, sizeof(*dp.dp2));
1025 dp.dp2->di_size = size;
1026 dp.dp2->di_mode = IFREG | IREAD;
1027 dp.dp2->di_nlink = 1;
1029 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1030 dp.dp2->di_atime = utime;
1031 dp.dp2->di_mtime = utime;
1032 dp.dp2->di_ctime = utime;
1033 dp.dp2->di_birthtime = utime;
1035 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1036 blk = journal_balloc();
1039 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1040 dp.dp1->di_db[i] = blk;
1041 dp.dp1->di_blocks++;
1043 dp.dp2->di_db[i] = blk;
1044 dp.dp2->di_blocks++;
1047 for (i = 0; i < UFS_NIADDR && resid; i++) {
1048 blk = journal_balloc();
1051 blks = indir_fill(blk, i, &resid) + 1;
1056 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1057 dp.dp1->di_ib[i] = blk;
1058 dp.dp1->di_blocks += blks;
1060 dp.dp2->di_ib[i] = blk;
1061 dp.dp2->di_blocks += blks;
1064 if (sblock.fs_magic == FS_UFS1_MAGIC)
1065 dp.dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1067 dp.dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1068 if (putinode(&disk) < 0) {
1069 warn("Failed to write allocated inode: %s",
1074 if (cgwrite(&disk) < 0) {
1075 warn("Failed to write updated cg");
1079 if (journal_insertfile(ino) < 0) {
1083 sblock.fs_sujfree = 0;
1086 warnx("Insufficient free space for the journal.");
1094 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1095 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1096 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1097 " [-L volname] [-l enable | disable] [-m minfree]",
1098 " [-N enable | disable] [-n enable | disable]",
1099 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1100 " special | filesystem");
1107 warnx("POSIX.1e ACLs: (-a) %s",
1108 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1109 warnx("NFSv4 ACLs: (-N) %s",
1110 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1111 warnx("MAC multilabel: (-l) %s",
1112 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1113 warnx("soft updates: (-n) %s",
1114 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1115 warnx("soft update journaling: (-j) %s",
1116 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1117 warnx("gjournal: (-J) %s",
1118 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1119 warnx("trim: (-t) %s",
1120 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1121 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1123 warnx("average file size: (-f) %d",
1124 sblock.fs_avgfilesize);
1125 warnx("average number of files in a directory: (-s) %d",
1126 sblock.fs_avgfpdir);
1127 warnx("minimum percentage of free space: (-m) %d%%",
1129 warnx("space to hold for metadata blocks: (-k) %jd",
1130 sblock.fs_metaspace);
1131 warnx("optimization preference: (-o) %s",
1132 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1133 if (sblock.fs_minfree >= MINFREE &&
1134 sblock.fs_optim == FS_OPTSPACE)
1135 warnx(OPTWARN, "time", ">=", MINFREE);
1136 if (sblock.fs_minfree < MINFREE &&
1137 sblock.fs_optim == FS_OPTTIME)
1138 warnx(OPTWARN, "space", "<", MINFREE);
1139 warnx("volume label: (-L) %s",