2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
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/ufsmount.h>
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ffs/fs.h>
55 #include <ufs/ufs/dir.h>
70 /* the optimization warning string template */
71 #define OPTWARN "should optimize for %s with minfree %s %d%%"
74 static char clrbuf[MAXBSIZE];
75 static struct uufsd disk;
76 #define sblock disk.d_fs
78 static void usage(void);
79 static void printfs(void);
80 static int journal_alloc(int64_t size);
81 static void journal_clear(void);
82 static void sbdirty(void);
85 main(int argc, char *argv[])
87 const char *avalue, *jvalue, *Jvalue, *Lvalue, *lvalue, *Nvalue, *nvalue;
89 const char *special, *on;
92 int Aflag, aflag, eflag, evalue, fflag, fvalue, jflag, Jflag, kflag;
93 int kvalue, Lflag, lflag, mflag, mvalue, Nflag, nflag, oflag, ovalue;
94 int pflag, sflag, svalue, Svalue, tflag;
102 Aflag = aflag = eflag = fflag = jflag = Jflag = kflag = Lflag = 0;
103 lflag = mflag = Nflag = nflag = oflag = pflag = sflag = tflag = 0;
104 avalue = jvalue = Jvalue = Lvalue = lvalue = Nvalue = nvalue = NULL;
105 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)
305 on = special = argv[0];
306 if (ufs_disk_fillout(&disk, special) == -1)
308 if (disk.d_name != special) {
309 if (statfs(special, &stfs) != 0)
310 warn("Can't stat %s", special);
311 if (strcmp(special, stfs.f_mntonname) == 0)
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 can not 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 ufs_disk_close(&disk);
561 bzero(&args, sizeof(args));
563 stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
564 err(9, "%s: reload", special);
565 warnx("file system reloaded");
569 if (disk.d_error != NULL)
570 errx(11, "%s: %s", special, disk.d_error);
572 err(12, "%s", special);
578 disk.d_fs.fs_flags |= FS_UNCLEAN | FS_NEEDSFSCK;
579 disk.d_fs.fs_clean = 0;
588 static int contig = 1;
592 blk = cgballoc(&disk);
596 * If we failed to allocate a block from this cg, move to
599 if (cgwrite(&disk) < 0) {
600 warn("Failed to write updated cg");
603 while ((valid = cgread(&disk)) == 1) {
605 * Try to minimize fragmentation by requiring a minimum
606 * number of blocks present.
608 if (cgp->cg_cs.cs_nbfree > 256 * 1024)
610 if (contig == 0 && cgp->cg_cs.cs_nbfree)
616 * Try once through looking only for large contiguous regions
617 * and again taking any space we can find.
622 warnx("Journal file fragmented.");
625 warnx("Failed to find sufficient free blocks for the journal");
628 if (bwrite(&disk, fsbtodb(&sblock, blk), clrbuf,
629 sblock.fs_bsize) <= 0) {
630 warn("Failed to initialize new block");
637 * Search a directory block for the SUJ_FILE.
640 dir_search(ufs2_daddr_t blk, int bytes)
642 char block[MAXBSIZE];
646 if (bread(&disk, fsbtodb(&sblock, blk), block, bytes) <= 0) {
647 warn("Failed to read dir block");
650 for (off = 0; off < bytes; off += dp->d_reclen) {
651 dp = (struct direct *)&block[off];
652 if (dp->d_reclen == 0)
656 if (dp->d_namlen != strlen(SUJ_FILE))
658 if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
667 * Search in the ROOTINO for the SUJ_FILE. If it exists we can not enable
671 journal_findfile(void)
673 struct ufs1_dinode *dp1;
674 struct ufs2_dinode *dp2;
680 if (getino(&disk, &ip, ROOTINO, &mode) != 0) {
681 warn("Failed to get root inode");
686 if (sblock.fs_magic == FS_UFS1_MAGIC) {
687 if ((off_t)dp1->di_size >= lblktosize(&sblock, NDADDR)) {
688 warnx("ROOTINO extends beyond direct blocks.");
691 for (i = 0; i < NDADDR; i++) {
692 if (dp1->di_db[i] == 0)
694 if ((ino = dir_search(dp1->di_db[i],
695 sblksize(&sblock, (off_t)dp1->di_size, i))) != 0)
699 if ((off_t)dp2->di_size >= lblktosize(&sblock, NDADDR)) {
700 warnx("ROOTINO extends beyond direct blocks.");
703 for (i = 0; i < NDADDR; i++) {
704 if (dp2->di_db[i] == 0)
706 if ((ino = dir_search(dp2->di_db[i],
707 sblksize(&sblock, (off_t)dp2->di_size, i))) != 0)
716 dir_clear_block(const char *block, off_t off)
720 for (; off < sblock.fs_bsize; off += DIRBLKSIZ) {
721 dp = (struct direct *)&block[off];
723 dp->d_reclen = DIRBLKSIZ;
724 dp->d_type = DT_UNKNOWN;
729 * Insert the journal at inode 'ino' into directory blk 'blk' at the first
730 * free offset of 'off'. DIRBLKSIZ blocks after off are initialized as
734 dir_insert(ufs2_daddr_t blk, off_t off, ino_t ino)
737 char block[MAXBSIZE];
739 if (bread(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
740 warn("Failed to read dir block");
743 bzero(&block[off], sblock.fs_bsize - off);
744 dp = (struct direct *)&block[off];
746 dp->d_reclen = DIRBLKSIZ;
748 dp->d_namlen = strlen(SUJ_FILE);
749 bcopy(SUJ_FILE, &dp->d_name, strlen(SUJ_FILE));
750 dir_clear_block(block, off + DIRBLKSIZ);
751 if (bwrite(&disk, fsbtodb(&sblock, blk), block, sblock.fs_bsize) <= 0) {
752 warn("Failed to write dir block");
759 * Extend a directory block in 'blk' by copying it to a full size block
760 * and inserting the new journal inode into .sujournal.
763 dir_extend(ufs2_daddr_t blk, ufs2_daddr_t nblk, off_t size, ino_t ino)
765 char block[MAXBSIZE];
767 if (bread(&disk, fsbtodb(&sblock, blk), block,
768 roundup(size, sblock.fs_fsize)) <= 0) {
769 warn("Failed to read dir block");
772 dir_clear_block(block, size);
773 if (bwrite(&disk, fsbtodb(&sblock, nblk), block, sblock.fs_bsize)
775 warn("Failed to write dir block");
779 return (dir_insert(nblk, size, ino));
783 * Insert the journal file into the ROOTINO directory. We always extend the
787 journal_insertfile(ino_t ino)
789 struct ufs1_dinode *dp1;
790 struct ufs2_dinode *dp2;
799 if (getino(&disk, &ip, ROOTINO, &mode) != 0) {
800 warn("Failed to get root inode");
808 nblk = journal_balloc();
812 * For simplicity sake we aways extend the ROOTINO into a new
813 * directory block rather than searching for space and inserting
814 * into an existing block. However, if the rootino has frags
815 * have to free them and extend the block.
817 if (sblock.fs_magic == FS_UFS1_MAGIC) {
818 lbn = lblkno(&sblock, dp1->di_size);
819 off = blkoff(&sblock, dp1->di_size);
820 blk = dp1->di_db[lbn];
821 size = sblksize(&sblock, (off_t)dp1->di_size, lbn);
823 lbn = lblkno(&sblock, dp2->di_size);
824 off = blkoff(&sblock, dp2->di_size);
825 blk = dp2->di_db[lbn];
826 size = sblksize(&sblock, (off_t)dp2->di_size, lbn);
829 if (dir_extend(blk, nblk, off, ino) == -1)
833 if (dir_insert(nblk, 0, ino) == -1)
836 if (sblock.fs_magic == FS_UFS1_MAGIC) {
837 dp1->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
838 dp1->di_db[lbn] = nblk;
839 dp1->di_size = lblktosize(&sblock, lbn+1);
841 dp2->di_blocks += (sblock.fs_bsize - size) / DEV_BSIZE;
842 dp2->di_db[lbn] = nblk;
843 dp2->di_size = lblktosize(&sblock, lbn+1);
845 if (putino(&disk) < 0) {
846 warn("Failed to write root inode");
849 if (cgwrite(&disk) < 0) {
850 warn("Failed to write updated cg");
855 if (cgbfree(&disk, blk, size) < 0) {
856 warn("Failed to write cg");
865 indir_fill(ufs2_daddr_t blk, int level, int *resid)
867 char indirbuf[MAXBSIZE];
875 bzero(indirbuf, sizeof(indirbuf));
876 bap1 = (ufs1_daddr_t *)indirbuf;
879 for (i = 0; i < NINDIR(&sblock) && *resid != 0; i++) {
880 nblk = journal_balloc();
884 if (sblock.fs_magic == FS_UFS1_MAGIC)
889 ncnt = indir_fill(nblk, level - 1, resid);
896 if (bwrite(&disk, fsbtodb(&sblock, blk), indirbuf,
897 sblock.fs_bsize) <= 0) {
898 warn("Failed to write indirect");
905 * Clear the flag bits so the journal can be removed.
910 struct ufs1_dinode *dp1;
911 struct ufs2_dinode *dp2;
916 ino = journal_findfile();
917 if (ino == (ino_t)-1 || ino == 0) {
918 warnx("Journal file does not exist");
921 printf("Clearing journal flags from inode %ju\n", (uintmax_t)ino);
922 if (getino(&disk, &ip, ino, &mode) != 0) {
923 warn("Failed to get journal inode");
928 if (sblock.fs_magic == FS_UFS1_MAGIC)
932 if (putino(&disk) < 0) {
933 warn("Failed to write journal inode");
939 journal_alloc(int64_t size)
941 struct ufs1_dinode *dp1;
942 struct ufs2_dinode *dp2;
957 * If the journal file exists we can't allocate it.
959 ino = journal_findfile();
960 if (ino == (ino_t)-1)
963 warnx("Journal file %s already exists, please remove.",
968 * If the user didn't supply a size pick one based on the filesystem
969 * size constrained with hardcoded MIN and MAX values. We opt for
970 * 1/1024th of the filesystem up to MAX but not exceeding one CG and
971 * not less than the MIN.
974 size = (sblock.fs_size * sblock.fs_bsize) / 1024;
975 size = MIN(SUJ_MAX, size);
976 if (size / sblock.fs_fsize > sblock.fs_fpg)
977 size = sblock.fs_fpg * sblock.fs_fsize;
978 size = MAX(SUJ_MIN, size);
980 /* fsck does not support fragments in journal files. */
981 size = roundup(size, sblock.fs_bsize);
982 resid = blocks = size / sblock.fs_bsize;
983 if (sblock.fs_cstotal.cs_nbfree < blocks) {
984 warn("Insufficient free space for %jd byte journal", size);
988 * Find a cg with enough blocks to satisfy the journal
989 * size. Presently the journal does not span cgs.
991 while (cgread(&disk) == 1) {
992 if (cgp->cg_cs.cs_nifree == 0)
994 ino = cgialloc(&disk);
997 printf("Using inode %ju in cg %d for %jd byte journal\n",
998 (uintmax_t)ino, cgp->cg_cgx, size);
999 if (getino(&disk, &ip, ino, &mode) != 0) {
1000 warn("Failed to get allocated inode");
1005 * We leave fields unrelated to the number of allocated
1006 * blocks and size uninitialized. This causes legacy
1007 * fsck implementations to clear the inode.
1012 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1013 bzero(dp1, sizeof(*dp1));
1014 dp1->di_size = size;
1015 dp1->di_mode = IFREG | IREAD;
1017 dp1->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1018 dp1->di_atime = utime;
1019 dp1->di_mtime = utime;
1020 dp1->di_ctime = utime;
1022 bzero(dp2, sizeof(*dp2));
1023 dp2->di_size = size;
1024 dp2->di_mode = IFREG | IREAD;
1026 dp2->di_flags = SF_IMMUTABLE | SF_NOUNLINK | UF_NODUMP;
1027 dp2->di_atime = utime;
1028 dp2->di_mtime = utime;
1029 dp2->di_ctime = utime;
1030 dp2->di_birthtime = utime;
1032 for (i = 0; i < NDADDR && resid; i++, resid--) {
1033 blk = journal_balloc();
1036 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1037 dp1->di_db[i] = blk;
1040 dp2->di_db[i] = blk;
1044 for (i = 0; i < NIADDR && resid; i++) {
1045 blk = journal_balloc();
1048 blks = indir_fill(blk, i, &resid) + 1;
1053 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1054 dp1->di_ib[i] = blk;
1055 dp1->di_blocks += blks;
1057 dp2->di_ib[i] = blk;
1058 dp2->di_blocks += blks;
1061 if (sblock.fs_magic == FS_UFS1_MAGIC)
1062 dp1->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1064 dp2->di_blocks *= sblock.fs_bsize / disk.d_bsize;
1065 if (putino(&disk) < 0) {
1066 warn("Failed to write inode");
1070 if (cgwrite(&disk) < 0) {
1071 warn("Failed to write updated cg");
1075 if (journal_insertfile(ino) < 0) {
1079 sblock.fs_sujfree = 0;
1082 warnx("Insufficient free space for the journal.");
1090 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
1091 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
1092 " [-J enable | disable] [-j enable | disable] [-k metaspace]",
1093 " [-L volname] [-l enable | disable] [-m minfree]",
1094 " [-N enable | disable] [-n enable | disable]",
1095 " [-o space | time] [-p] [-s avgfpdir] [-t enable | disable]",
1096 " special | filesystem");
1103 warnx("POSIX.1e ACLs: (-a) %s",
1104 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
1105 warnx("NFSv4 ACLs: (-N) %s",
1106 (sblock.fs_flags & FS_NFS4ACLS)? "enabled" : "disabled");
1107 warnx("MAC multilabel: (-l) %s",
1108 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
1109 warnx("soft updates: (-n) %s",
1110 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
1111 warnx("soft update journaling: (-j) %s",
1112 (sblock.fs_flags & FS_SUJ)? "enabled" : "disabled");
1113 warnx("gjournal: (-J) %s",
1114 (sblock.fs_flags & FS_GJOURNAL)? "enabled" : "disabled");
1115 warnx("trim: (-t) %s",
1116 (sblock.fs_flags & FS_TRIM)? "enabled" : "disabled");
1117 warnx("maximum blocks per file in a cylinder group: (-e) %d",
1119 warnx("average file size: (-f) %d",
1120 sblock.fs_avgfilesize);
1121 warnx("average number of files in a directory: (-s) %d",
1122 sblock.fs_avgfpdir);
1123 warnx("minimum percentage of free space: (-m) %d%%",
1125 warnx("space to hold for metadata blocks: (-k) %jd",
1126 sblock.fs_metaspace);
1127 warnx("optimization preference: (-o) %s",
1128 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
1129 if (sblock.fs_minfree >= MINFREE &&
1130 sblock.fs_optim == FS_OPTSPACE)
1131 warnx(OPTWARN, "time", ">=", MINFREE);
1132 if (sblock.fs_minfree < MINFREE &&
1133 sblock.fs_optim == FS_OPTTIME)
1134 warnx(OPTWARN, "space", "<", MINFREE);
1135 warnx("volume label: (-L) %s",