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]) || Lvalue[i] == '_' ||
195 if (Lvalue[i] != '\0') {
196 errx(10, "bad %s. Valid characters are "
197 "alphanumerics, dashes, and underscores.",
200 if (strlen(Lvalue) >= MAXVOLLEN) {
201 errx(10, "bad %s. Length is longer than %d.",
202 name, MAXVOLLEN - 1);
209 name = "multilabel MAC file system";
211 if (strcmp(lvalue, "enable") &&
212 strcmp(lvalue, "disable")) {
213 errx(10, "bad %s (options are %s)",
214 name, "`enable' or `disable'");
221 name = "minimum percentage of free space";
222 mvalue = atoi(optarg);
223 if (mvalue < 0 || mvalue > 99)
224 errx(10, "bad %s (%s)", name, optarg);
232 if (strcmp(Nvalue, "enable") &&
233 strcmp(Nvalue, "disable")) {
234 errx(10, "bad %s (options are %s)",
235 name, "`enable' or `disable'");
242 name = "soft updates";
244 if (strcmp(nvalue, "enable") != 0 &&
245 strcmp(nvalue, "disable") != 0) {
246 errx(10, "bad %s (options are %s)",
247 name, "`enable' or `disable'");
254 name = "optimization preference";
255 if (strcmp(optarg, "space") == 0)
256 ovalue = FS_OPTSPACE;
257 else if (strcmp(optarg, "time") == 0)
261 "bad %s (options are `space' or `time')",
273 name = "expected number of files per directory";
274 svalue = atoi(optarg);
276 errx(10, "%s must be >= 1 (was %s)",
283 name = "Softdep Journal Size";
284 Svalue = atoi(optarg);
285 if (Svalue < SUJ_MIN)
286 errx(10, "%s must be >= %d (was %s)",
287 name, SUJ_MIN, optarg);
294 if (strcmp(tvalue, "enable") != 0 &&
295 strcmp(tvalue, "disable") != 0) {
296 errx(10, "bad %s (options are %s)",
297 name, "`enable' or `disable'");
307 if (found_arg == 0 || argc != 1)
310 on = special = argv[0];
311 if (ufs_disk_fillout(&disk, special) == -1)
314 * Check for unclean filesystem.
316 if ((sblock.fs_clean == 0 ||
317 (sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) != 0) &&
318 (found_arg > 1 || !pflag))
319 errx(1, "%s is not clean - run fsck.\n", special);
320 if (disk.d_name != special) {
321 if (statfs(special, &stfs) != 0)
322 warn("Can't stat %s", special);
323 if (strcmp(special, stfs.f_mntonname) == 0)
332 name = "volume label";
333 strncpy(sblock.fs_volname, Lvalue, MAXVOLLEN);
336 name = "POSIX.1e ACLs";
337 if (strcmp(avalue, "enable") == 0) {
338 if (sblock.fs_flags & FS_ACLS) {
339 warnx("%s remains unchanged as enabled", name);
340 } else if (sblock.fs_flags & FS_NFS4ACLS) {
341 warnx("%s and NFSv4 ACLs are mutually "
344 sblock.fs_flags |= FS_ACLS;
345 warnx("%s set", name);
347 } else if (strcmp(avalue, "disable") == 0) {
348 if ((~sblock.fs_flags & FS_ACLS) ==
350 warnx("%s remains unchanged as disabled",
353 sblock.fs_flags &= ~FS_ACLS;
354 warnx("%s cleared", name);
359 name = "maximum blocks per file in a cylinder group";
360 if (sblock.fs_maxbpg == evalue)
361 warnx("%s remains unchanged as %d", name, evalue);
363 warnx("%s changes from %d to %d",
364 name, sblock.fs_maxbpg, evalue);
365 sblock.fs_maxbpg = evalue;
369 name = "average file size";
370 if (sblock.fs_avgfilesize == (unsigned)fvalue) {
371 warnx("%s remains unchanged as %d", name, fvalue);
374 warnx("%s changes from %d to %d",
375 name, sblock.fs_avgfilesize, fvalue);
376 sblock.fs_avgfilesize = fvalue;
380 name = "soft updates journaling";
381 if (strcmp(jvalue, "enable") == 0) {
382 if ((sblock.fs_flags & (FS_DOSOFTDEP | FS_SUJ)) ==
383 (FS_DOSOFTDEP | FS_SUJ)) {
384 warnx("%s remains unchanged as enabled", name);
385 } else if (sblock.fs_clean == 0) {
386 warnx("%s cannot be enabled until fsck is run",
388 } else if (journal_alloc(Svalue) != 0) {
389 warnx("%s cannot be enabled", name);
391 sblock.fs_flags |= FS_DOSOFTDEP | FS_SUJ;
392 warnx("%s set", name);
394 } else if (strcmp(jvalue, "disable") == 0) {
395 if ((~sblock.fs_flags & FS_SUJ) == FS_SUJ) {
396 warnx("%s remains unchanged as disabled", name);
399 sblock.fs_flags &= ~FS_SUJ;
400 sblock.fs_sujfree = 0;
401 warnx("%s cleared but soft updates still set.",
404 warnx("remove .sujournal to reclaim space");
410 if (strcmp(Jvalue, "enable") == 0) {
411 if (sblock.fs_flags & FS_GJOURNAL) {
412 warnx("%s remains unchanged as enabled", name);
414 sblock.fs_flags |= FS_GJOURNAL;
415 warnx("%s set", name);
417 } else if (strcmp(Jvalue, "disable") == 0) {
418 if ((~sblock.fs_flags & FS_GJOURNAL) ==
420 warnx("%s remains unchanged as disabled",
423 sblock.fs_flags &= ~FS_GJOURNAL;
424 warnx("%s cleared", name);
429 name = "space to hold for metadata blocks";
430 if (sblock.fs_metaspace == kvalue)
431 warnx("%s remains unchanged as %d", name, kvalue);
433 kvalue = blknum(&sblock, kvalue);
434 if (kvalue > sblock.fs_fpg / 2) {
435 kvalue = blknum(&sblock, sblock.fs_fpg / 2);
436 warnx("%s cannot exceed half the file system "
439 warnx("%s changes from %jd to %d",
440 name, sblock.fs_metaspace, kvalue);
441 sblock.fs_metaspace = kvalue;
446 if (strcmp(lvalue, "enable") == 0) {
447 if (sblock.fs_flags & FS_MULTILABEL) {
448 warnx("%s remains unchanged as enabled", name);
450 sblock.fs_flags |= FS_MULTILABEL;
451 warnx("%s set", name);
453 } else if (strcmp(lvalue, "disable") == 0) {
454 if ((~sblock.fs_flags & FS_MULTILABEL) ==
456 warnx("%s remains unchanged as disabled",
459 sblock.fs_flags &= ~FS_MULTILABEL;
460 warnx("%s cleared", name);
465 name = "minimum percentage of free space";
466 if (sblock.fs_minfree == mvalue)
467 warnx("%s remains unchanged as %d%%", name, mvalue);
469 warnx("%s changes from %d%% to %d%%",
470 name, sblock.fs_minfree, mvalue);
471 sblock.fs_minfree = mvalue;
472 if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
473 warnx(OPTWARN, "time", ">=", MINFREE);
474 if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
475 warnx(OPTWARN, "space", "<", MINFREE);
480 if (strcmp(Nvalue, "enable") == 0) {
481 if (sblock.fs_flags & FS_NFS4ACLS) {
482 warnx("%s remains unchanged as enabled", name);
483 } else if (sblock.fs_flags & FS_ACLS) {
484 warnx("%s and POSIX.1e ACLs are mutually "
487 sblock.fs_flags |= FS_NFS4ACLS;
488 warnx("%s set", name);
490 } else if (strcmp(Nvalue, "disable") == 0) {
491 if ((~sblock.fs_flags & FS_NFS4ACLS) ==
493 warnx("%s remains unchanged as disabled",
496 sblock.fs_flags &= ~FS_NFS4ACLS;
497 warnx("%s cleared", name);
502 name = "soft updates";
503 if (strcmp(nvalue, "enable") == 0) {
504 if (sblock.fs_flags & FS_DOSOFTDEP)
505 warnx("%s remains unchanged as enabled", name);
506 else if (sblock.fs_clean == 0) {
507 warnx("%s cannot be enabled until fsck is run",
510 sblock.fs_flags |= FS_DOSOFTDEP;
511 warnx("%s set", name);
513 } else if (strcmp(nvalue, "disable") == 0) {
514 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP)
515 warnx("%s remains unchanged as disabled", name);
517 sblock.fs_flags &= ~FS_DOSOFTDEP;
518 warnx("%s cleared", name);
523 name = "optimization preference";
524 chg[FS_OPTSPACE] = "space";
525 chg[FS_OPTTIME] = "time";
526 if (sblock.fs_optim == ovalue)
527 warnx("%s remains unchanged as %s", name, chg[ovalue]);
529 warnx("%s changes from %s to %s",
530 name, chg[sblock.fs_optim], chg[ovalue]);
531 sblock.fs_optim = ovalue;
532 if (sblock.fs_minfree >= MINFREE &&
533 ovalue == FS_OPTSPACE)
534 warnx(OPTWARN, "time", ">=", MINFREE);
535 if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME)
536 warnx(OPTWARN, "space", "<", MINFREE);
540 name = "expected number of files per directory";
541 if (sblock.fs_avgfpdir == (unsigned)svalue) {
542 warnx("%s remains unchanged as %d", name, svalue);
545 warnx("%s changes from %d to %d",
546 name, sblock.fs_avgfpdir, svalue);
547 sblock.fs_avgfpdir = svalue;
551 name = "issue TRIM to the disk";
552 if (strcmp(tvalue, "enable") == 0) {
553 if (sblock.fs_flags & FS_TRIM)
554 warnx("%s remains unchanged as enabled", name);
556 sblock.fs_flags |= FS_TRIM;
557 warnx("%s set", name);
559 } else if (strcmp(tvalue, "disable") == 0) {
560 if ((~sblock.fs_flags & FS_TRIM) == FS_TRIM)
561 warnx("%s remains unchanged as disabled", name);
563 sblock.fs_flags &= ~FS_TRIM;
564 warnx("%s cleared", name);
569 if (sbwrite(&disk, Aflag) == -1)
571 ufs_disk_close(&disk);
573 build_iovec_argf(&iov, &iovlen, "fstype", "ufs");
574 build_iovec_argf(&iov, &iovlen, "fspath", "%s", on);
575 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
576 if (nmount(iov, iovlen,
577 stfs.f_flags | MNT_UPDATE | MNT_RELOAD) < 0) {
579 err(9, "%s: reload: %s", special, errmsg);
581 err(9, "%s: reload", special);
583 warnx("file system reloaded");
587 if (disk.d_error != NULL)
588 errx(11, "%s: %s", special, disk.d_error);
590 err(12, "%s", special);
596 disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
597 disk.d_fs.fs_clean = 0;
606 static int contig = 1;
610 blk = cgballoc(&disk);
614 * If we failed to allocate a block from this cg, move to
617 if (cgwrite(&disk) < 0) {
618 warn("Failed to write updated cg");
621 while ((valid = cgread(&disk)) == 1) {
623 * Try to minimize fragmentation by requiring a minimum
624 * number of blocks present.
626 if (cgp->cg_cs.cs_nbfree > 256 * 1024)
628 if (contig == 0 && cgp->cg_cs.cs_nbfree)
634 * Try once through looking only for large contiguous regions
635 * and again taking any space we can find.
640 warnx("Journal file fragmented.");
643 warnx("Failed to find sufficient free blocks for the journal");
646 if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
647 sblock.fs_bsize) <= 0) {
648 warn("Failed to initialize new block");
655 * Search a directory block for the SUJ_FILE.
658 dir_search(ufs2_daddr_t blk, int bytes)
660 char block[MAXBSIZE];
664 if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
665 warn("Failed to read dir block");
668 for (off = 0; off < bytes; off += dp->d_reclen) {
669 dp = (struct direct *)&block[off];
670 if (dp->d_reclen == 0)
674 if (dp->d_namlen != strlen(SUJ_FILE))
676 if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
685 * Search in the UFS_ROOTINO for the SUJ_FILE. If it exists we can not enable
689 journal_findfile(void)
695 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
696 warn("Failed to get root inode: %s", disk.d_error);
699 if (sblock.fs_magic == FS_UFS1_MAGIC) {
700 if ((off_t)dp.dp1->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
701 warnx("UFS_ROOTINO extends beyond direct blocks.");
704 for (i = 0; i < UFS_NDADDR; i++) {
705 if (dp.dp1->di_db[i] == 0)
707 if ((ino = dir_search(dp.dp1->di_db[i],
708 sblksize(&sblock, (off_t)dp.dp1->di_size, i))) != 0)
712 if ((off_t)dp.dp2->di_size >= lblktosize(&sblock, UFS_NDADDR)) {
713 warnx("UFS_ROOTINO extends beyond direct blocks.");
716 for (i = 0; i < UFS_NDADDR; i++) {
717 if (dp.dp2->di_db[i] == 0)
719 if ((ino = dir_search(dp.dp2->di_db[i],
720 sblksize(&sblock, (off_t)dp.dp2->di_size, i))) != 0)
729 dir_clear_block(const char *block, off_t off)
733 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
734 dp = (struct direct *)&block[off];
736 dp->d_reclen = DIRBLKSIZ;
737 dp->d_type = DT_UNKNOWN;
742 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
743 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
747 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
750 char block[MAXBSIZE];
752 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
753 warn("Failed to read dir block");
756 bzero(&block[off], sblock.fs_bsize - off);
757 dp = (struct direct *)&block[off];
759 dp->d_reclen = DIRBLKSIZ;
761 dp->d_namlen = strlen(SUJ_FILE);
762 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
763 dir_clear_block(block, off + DIRBLKSIZ);
764 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
765 warn("Failed to write dir block");
772 * Extend a directory block in 'blk' by copying it to a full size block
773 * and inserting the new journal inode into .sujournal.
776 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
778 char block[MAXBSIZE];
780 if (bread(&disk, fsbtodb(&sblock, blk), block,
781 roundup(size, sblock.fs_fsize)) <= 0) {
782 warn("Failed to read dir block");
785 dir_clear_block(block, size);
786 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
788 warn("Failed to write dir block");
792 return (dir_insert(nblk, size, ino));
796 * Insert the journal file into the UFS_ROOTINO directory. We always extend the
800 journal_insertfile(ino_t ino)
809 if (getinode(&disk, &dp, UFS_ROOTINO) != 0) {
810 warn("Failed to get root inode: %s", disk.d_error);
816 nblk = journal_balloc();
820 * For simplicity sake we aways extend the UFS_ROOTINO into a new
821 * directory block rather than searching for space and inserting
822 * into an existing block. However, if the rootino has frags
823 * have to free them and extend the block.
825 if (sblock.fs_magic == FS_UFS1_MAGIC) {
826 lbn = lblkno(&sblock, dp.dp1->di_size);
827 off = blkoff(&sblock, dp.dp1->di_size);
828 blk = dp.dp1->di_db[lbn];
829 size = sblksize(&sblock, (off_t)dp.dp1->di_size, lbn);
831 lbn = lblkno(&sblock, dp.dp2->di_size);
832 off = blkoff(&sblock, dp.dp2->di_size);
833 blk = dp.dp2->di_db[lbn];
834 size = sblksize(&sblock, (off_t)dp.dp2->di_size, lbn);
837 if (dir_extend(blk, nblk, off, ino) == -1)
841 if (dir_insert(nblk, 0, ino) == -1)
844 if (sblock.fs_magic == FS_UFS1_MAGIC) {
845 dp.dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
846 dp.dp1->di_db[lbn] = nblk;
847 dp.dp1->di_size = lblktosize(&sblock, lbn+1);
849 dp.dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
850 dp.dp2->di_db[lbn] = nblk;
851 dp.dp2->di_size = lblktosize(&sblock, lbn+1);
853 if (putinode(&disk) < 0) {
854 warn("Failed to write root inode: %s", disk.d_error);
857 if (cgwrite(&disk) < 0) {
858 warn("Failed to write updated cg");
863 if (cgbfree(&disk, blk, size) < 0) {
864 warn("Failed to write cg");
873 indir_fill(ufs2_daddr_t blk, int level, int *resid)
875 char indirbuf[MAXBSIZE];
883 bzero(indirbuf, sizeof(indirbuf));
884 bap1 = (ufs1_daddr_t *)indirbuf;
887 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
888 nblk = journal_balloc();
892 if (sblock.fs_magic == FS_UFS1_MAGIC)
897 ncnt = indir_fill(nblk, level - 1, resid);
904 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
905 sblock.fs_bsize) <= 0) {
906 warn("Failed to write indirect");
913 * Clear the flag bits so the journal can be removed.
921 ino = journal_findfile();
922 if (ino == (ino_t)-1 || ino == 0) {
923 warnx("Journal file does not exist");
926 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
927 if (getinode(&disk, &dp, ino) != 0) {
928 warn("Failed to get journal inode: %s", disk.d_error);
931 if (sblock.fs_magic == FS_UFS1_MAGIC)
932 dp.dp1->di_flags = 0;
934 dp.dp2->di_flags = 0;
935 if (putinode(&disk) < 0) {
936 warn("Failed to write journal inode: %s", disk.d_error);
942 journal_alloc(int64_t size)
957 * If the journal file exists we can't allocate it.
959 ino = journal_findfile();
960 if (ino == (ino_t)-1) {
961 warnx("journal_findfile() failed.");
965 warnx("Journal file %s already exists, please remove.",
970 * If the user didn't supply a size pick one based on the filesystem
971 * size constrained with hardcoded MIN and MAX values. We opt for
972 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
973 * not less than the MIN.
976 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
977 size = MIN(SUJ_MAX, size);
978 if (size / sblock.fs_fsize > sblock.fs_fpg)
979 size = sblock.fs_fpg * sblock.fs_fsize;
980 size = MAX(SUJ_MIN, size);
982 /* fsck does not support fragments in journal files. */
983 size = roundup(size, sblock.fs_bsize);
984 resid = blocks = size / sblock.fs_bsize;
985 if (sblock.fs_cstotal.cs_nbfree < blocks) {
986 warn("Insufficient free space for %jd byte journal", size);
990 * Find a cg with enough blocks to satisfy the journal
991 * size. Presently the journal does not span cgs.
993 while (cgread(&disk) == 1) {
994 if (cgp->cg_cs.cs_nifree == 0)
996 ino = cgialloc(&disk);
999 printf("Using inode %ju in cg %d for %jd byte journal\n",
1000 (uintmax_t)ino, cgp->cg_cgx, size);
1001 if (getinode(&disk, &dp, ino) != 0) {
1002 warn("Failed to get allocated inode: %s", disk.d_error);
1007 * We leave fields unrelated to the number of allocated
1008 * blocks and size uninitialized. This causes legacy
1009 * fsck implementations to clear the inode.
1012 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1013 bzero(dp.dp1, sizeof(*dp.dp1));
1014 dp.dp1->di_size = size;
1015 dp.dp1->di_mode = IFREG | IREAD;
1016 dp.dp1->di_nlink = 1;
1018 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1019 dp.dp1->di_atime = utime;
1020 dp.dp1->di_mtime = utime;
1021 dp.dp1->di_ctime = utime;
1023 bzero(dp.dp2, sizeof(*dp.dp2));
1024 dp.dp2->di_size = size;
1025 dp.dp2->di_mode = IFREG | IREAD;
1026 dp.dp2->di_nlink = 1;
1028 SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1029 dp.dp2->di_atime = utime;
1030 dp.dp2->di_mtime = utime;
1031 dp.dp2->di_ctime = utime;
1032 dp.dp2->di_birthtime = utime;
1034 for (i = 0; i < UFS_NDADDR && resid; i++, resid--) {
1035 blk = journal_balloc();
1038 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1039 dp.dp1->di_db[i] = blk;
1040 dp.dp1->di_blocks++;
1042 dp.dp2->di_db[i] = blk;
1043 dp.dp2->di_blocks++;
1046 for (i = 0; i < UFS_NIADDR && resid; i++) {
1047 blk = journal_balloc();
1050 blks = indir_fill(blk, i, &resid) + 1;
1055 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1056 dp.dp1->di_ib[i] = blk;
1057 dp.dp1->di_blocks += blks;
1059 dp.dp2->di_ib[i] = blk;
1060 dp.dp2->di_blocks += blks;
1063 if (sblock.fs_magic == FS_UFS1_MAGIC)
1064 dp.dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1066 dp.dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1067 if (putinode(&disk) < 0) {
1068 warn("Failed to write allocated inode: %s",
1073 if (cgwrite(&disk) < 0) {
1074 warn("Failed to write updated cg");
1078 if (journal_insertfile(ino) < 0) {
1082 sblock.fs_sujfree = 0;
1085 warnx("Insufficient free space for the journal.");
1093 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1094 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1095 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1096 " [-L volname] [-l enable | disable] [-m minfree]",
1097 " [-N enable | disable] [-n enable | disable]",
1098 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1099 " special | filesystem");
1106 warnx("POSIX.1e ACLs: (-a) %s",
1107 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1108 warnx("NFSv4 ACLs: (-N) %s",
1109 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1110 warnx("MAC multilabel: (-l) %s",
1111 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1112 warnx("soft updates: (-n) %s",
1113 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1114 warnx("soft update journaling: (-j) %s",
1115 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1116 warnx("gjournal: (-J) %s",
1117 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1118 warnx("trim: (-t) %s",
1119 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1120 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1122 warnx("average file size: (-f) %d",
1123 sblock.fs_avgfilesize);
1124 warnx("average number of files in a directory: (-s) %d",
1125 sblock.fs_avgfpdir);
1126 warnx("minimum percentage of free space: (-m) %d%%",
1128 warnx("space to hold for metadata blocks: (-k) %jd",
1129 sblock.fs_metaspace);
1130 warnx("optimization preference: (-o) %s",
1131 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1132 if (sblock.fs_minfree >= MINFREE &&
1133 sblock.fs_optim == FS_OPTSPACE)
1134 warnx(OPTWARN, "time", ">=", MINFREE);
1135 if (sblock.fs_minfree < MINFREE &&
1136 sblock.fs_optim == FS_OPTTIME)
1137 warnx(OPTWARN, "space", "<", MINFREE);
1138 warnx("volume label: (-L) %s",