1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
6 #include "libfrog/util.h"
9 #include "xfs_multidisk.h"
11 #include "libfrog/fsgeom.h"
12 #include "libfrog/topology.h"
14 #define TERABYTES(count, blog) ((uint64_t)(count) << (40 - (blog)))
15 #define GIGABYTES(count, blog) ((uint64_t)(count) << (30 - (blog)))
16 #define MEGABYTES(count, blog) ((uint64_t)(count) << (20 - (blog)))
19 * Use this macro before we have superblock and mount structure to
20 * convert from basic blocks to filesystem blocks.
22 #define DTOBT(d, bl) ((xfs_rfsblock_t)((d) >> ((bl) - BBSHIFT)))
25 * amount (in bytes) we zero at the beginning and end of the device to
26 * remove traces of other filesystems, raid superblocks, etc.
28 #define WHACK_SIZE (128 * 1024)
31 * XXX: The configured block and sector sizes are defined as global variables so
32 * that they don't need to be passed to getnum/cvtnum().
34 static unsigned int blocksize
;
35 static unsigned int sectorsize
;
38 * Enums for each CLI parameter type are declared first so we can calculate the
39 * maximum array size needed to hold them automatically.
123 /* Just define the max options array size manually right now */
124 #define MAX_SUBOPTS D_MAX_OPTS
126 #define SUBOPT_NEEDS_VAL (-1LL)
127 #define MAX_CONFLICTS 8
128 #define LAST_CONFLICT (-1)
131 * Table for parsing mkfs parameters.
133 * Description of the structure members follows:
136 * Name is a single char, e.g., for '-d file', name is 'd'.
139 * Subopts is a list of strings naming suboptions. In the example above,
140 * it would contain "file". The last entry of this list has to be NULL.
142 * subopt_params MANDATORY
143 * This is a list of structs tied with subopts. For each entry in subopts,
144 * a corresponding entry has to be defined:
146 * subopt_params struct:
148 * This number, starting from zero, denotes which item in subopt_params
149 * it is. The index has to be the same as is the order in subopts list,
150 * so we can access the right item both in subopt_param and subopts.
153 * Do not set this flag when definning a subopt. It is used to remeber that
154 * this subopt was already seen, for example for conflicts detection.
157 * Do not set. It is used internally for respecification, when some options
158 * has to be parsed twice - at first as a string, then later as a number.
161 * A flag signalling whether the user-given value can use suffixes.
162 * If you want to allow the use of user-friendly values like 13k, 42G,
165 * is_power_2 OPTIONAL
166 * An optional flag for subopts where the given value has to be a power
169 * conflicts MANDATORY
170 * If your subopt is in a conflict with some other option, specify it.
171 * Accepts the .index values of the conflicting subopts and the last
172 * member of this list has to be LAST_CONFLICT.
174 * minval, maxval OPTIONAL
175 * These options are used for automatic range check and they have to be
176 * always used together in pair. If you don't want to limit the max value,
177 * use something like UINT_MAX. If no value is given, then you must either
178 * supply your own validation, or refuse any value in the 'case
179 * X_SOMETHING' block. If you forget to define the min and max value, but
180 * call a standard function for validating user's value, it will cause an
181 * error message notifying you about this issue.
183 * (Said in another way, you can't have minval and maxval both equal
184 * to zero. But if one value is different: minval=0 and maxval=1,
187 * defaultval MANDATORY
188 * The value used if user specifies the subopt, but no value.
189 * If the subopt accepts some values (-d file=[1|0]), then this
190 * sets what is used with simple specifying the subopt (-d file).
191 * A special SUBOPT_NEEDS_VAL can be used to require a user-given
196 const char *subopts
[MAX_SUBOPTS
];
198 struct subopt_param
{
205 struct opt_params
*opts
;
207 } conflicts
[MAX_CONFLICTS
];
210 long long defaultval
;
211 } subopt_params
[MAX_SUBOPTS
];
215 * The two dimensional conflict array requires some initialisations to know
216 * about tables that haven't yet been defined. Work around this ordering
217 * issue with extern definitions here.
219 static struct opt_params sopts
;
221 static struct opt_params bopts
= {
230 .conflicts
= { { NULL
, LAST_CONFLICT
} },
231 .minval
= XFS_MIN_BLOCKSIZE
,
232 .maxval
= XFS_MAX_BLOCKSIZE
,
233 .defaultval
= SUBOPT_NEEDS_VAL
,
238 static struct opt_params dopts
= {
241 [D_AGCOUNT
] = "agcount",
246 [D_SWIDTH
] = "swidth",
247 [D_AGSIZE
] = "agsize",
250 [D_SECTSIZE
] = "sectsize",
251 [D_NOALIGN
] = "noalign",
252 [D_RTINHERIT
] = "rtinherit",
253 [D_PROJINHERIT
] = "projinherit",
254 [D_EXTSZINHERIT
] = "extszinherit",
255 [D_COWEXTSIZE
] = "cowextsize",
258 { .index
= D_AGCOUNT
,
259 .conflicts
= { { &dopts
, D_AGSIZE
},
260 { NULL
, LAST_CONFLICT
} },
262 .maxval
= XFS_MAX_AGNUMBER
,
263 .defaultval
= SUBOPT_NEEDS_VAL
,
266 .conflicts
= { { NULL
, LAST_CONFLICT
} },
272 .conflicts
= { { NULL
, LAST_CONFLICT
} },
273 .defaultval
= SUBOPT_NEEDS_VAL
,
276 .conflicts
= { { NULL
, LAST_CONFLICT
} },
278 .minval
= XFS_AG_MIN_BYTES
,
280 .defaultval
= SUBOPT_NEEDS_VAL
,
283 .conflicts
= { { &dopts
, D_NOALIGN
},
286 { NULL
, LAST_CONFLICT
} },
289 .defaultval
= SUBOPT_NEEDS_VAL
,
292 .conflicts
= { { &dopts
, D_NOALIGN
},
295 { NULL
, LAST_CONFLICT
} },
298 .defaultval
= SUBOPT_NEEDS_VAL
,
301 .conflicts
= { { &dopts
, D_AGCOUNT
},
302 { NULL
, LAST_CONFLICT
} },
304 .minval
= XFS_AG_MIN_BYTES
,
305 .maxval
= XFS_AG_MAX_BYTES
,
306 .defaultval
= SUBOPT_NEEDS_VAL
,
309 .conflicts
= { { &dopts
, D_NOALIGN
},
311 { &dopts
, D_SWIDTH
},
312 { NULL
, LAST_CONFLICT
} },
316 .defaultval
= SUBOPT_NEEDS_VAL
,
319 .conflicts
= { { &dopts
, D_NOALIGN
},
321 { &dopts
, D_SWIDTH
},
322 { NULL
, LAST_CONFLICT
} },
325 .defaultval
= SUBOPT_NEEDS_VAL
,
327 { .index
= D_SECTSIZE
,
328 .conflicts
= { { &sopts
, S_SIZE
},
329 { &sopts
, S_SECTSIZE
},
330 { NULL
, LAST_CONFLICT
} },
333 .minval
= XFS_MIN_SECTORSIZE
,
334 .maxval
= XFS_MAX_SECTORSIZE
,
335 .defaultval
= SUBOPT_NEEDS_VAL
,
337 { .index
= D_NOALIGN
,
338 .conflicts
= { { &dopts
, D_SU
},
341 { &dopts
, D_SWIDTH
},
342 { NULL
, LAST_CONFLICT
} },
347 { .index
= D_RTINHERIT
,
348 .conflicts
= { { NULL
, LAST_CONFLICT
} },
353 { .index
= D_PROJINHERIT
,
354 .conflicts
= { { NULL
, LAST_CONFLICT
} },
357 .defaultval
= SUBOPT_NEEDS_VAL
,
359 { .index
= D_EXTSZINHERIT
,
360 .conflicts
= { { NULL
, LAST_CONFLICT
} },
363 .defaultval
= SUBOPT_NEEDS_VAL
,
365 { .index
= D_COWEXTSIZE
,
366 .conflicts
= { { NULL
, LAST_CONFLICT
} },
369 .defaultval
= SUBOPT_NEEDS_VAL
,
375 static struct opt_params iopts
= {
379 [I_MAXPCT
] = "maxpct",
380 [I_PERBLOCK
] = "perblock",
383 [I_PROJID32BIT
] = "projid32bit",
384 [I_SPINODES
] = "sparse",
388 .conflicts
= { { NULL
, LAST_CONFLICT
} },
394 .conflicts
= { { NULL
, LAST_CONFLICT
} },
397 .defaultval
= SUBOPT_NEEDS_VAL
,
399 { .index
= I_PERBLOCK
,
400 .conflicts
= { { &iopts
, I_SIZE
},
401 { NULL
, LAST_CONFLICT
} },
403 .minval
= XFS_MIN_INODE_PERBLOCK
,
404 .maxval
= XFS_MAX_BLOCKSIZE
/ XFS_DINODE_MIN_SIZE
,
405 .defaultval
= SUBOPT_NEEDS_VAL
,
408 .conflicts
= { { &iopts
, I_PERBLOCK
},
409 { NULL
, LAST_CONFLICT
} },
411 .minval
= XFS_DINODE_MIN_SIZE
,
412 .maxval
= XFS_DINODE_MAX_SIZE
,
413 .defaultval
= SUBOPT_NEEDS_VAL
,
416 .conflicts
= { { NULL
, LAST_CONFLICT
} },
419 .defaultval
= SUBOPT_NEEDS_VAL
,
421 { .index
= I_PROJID32BIT
,
422 .conflicts
= { { NULL
, LAST_CONFLICT
} },
427 { .index
= I_SPINODES
,
428 .conflicts
= { { NULL
, LAST_CONFLICT
} },
436 static struct opt_params lopts
= {
440 [L_INTERNAL
] = "internal",
442 [L_VERSION
] = "version",
446 [L_SECTSIZE
] = "sectsize",
449 [L_LAZYSBCNTR
] = "lazy-count",
453 .conflicts
= { { &lopts
, L_DEV
},
454 { NULL
, LAST_CONFLICT
} },
457 .defaultval
= SUBOPT_NEEDS_VAL
,
459 { .index
= L_INTERNAL
,
460 .conflicts
= { { &lopts
, L_FILE
},
462 { &lopts
, L_SECTSIZE
},
463 { NULL
, LAST_CONFLICT
} },
469 .conflicts
= { { NULL
, LAST_CONFLICT
} },
471 .minval
= 2 * 1024 * 1024LL, /* XXX: XFS_MIN_LOG_BYTES */
472 .maxval
= XFS_MAX_LOG_BYTES
,
473 .defaultval
= SUBOPT_NEEDS_VAL
,
475 { .index
= L_VERSION
,
476 .conflicts
= { { NULL
, LAST_CONFLICT
} },
479 .defaultval
= SUBOPT_NEEDS_VAL
,
482 .conflicts
= { { &lopts
, L_SU
},
483 { NULL
, LAST_CONFLICT
} },
485 .maxval
= BTOBB(XLOG_MAX_RECORD_BSIZE
),
486 .defaultval
= SUBOPT_NEEDS_VAL
,
489 .conflicts
= { { &lopts
, L_SUNIT
},
490 { NULL
, LAST_CONFLICT
} },
493 .maxval
= XLOG_MAX_RECORD_BSIZE
,
494 .defaultval
= SUBOPT_NEEDS_VAL
,
497 .conflicts
= { { &lopts
, L_AGNUM
},
499 { &lopts
, L_INTERNAL
},
500 { NULL
, LAST_CONFLICT
} },
501 .defaultval
= SUBOPT_NEEDS_VAL
,
503 { .index
= L_SECTSIZE
,
504 .conflicts
= { { &lopts
, L_INTERNAL
},
505 { NULL
, LAST_CONFLICT
} },
508 .minval
= XFS_MIN_SECTORSIZE
,
509 .maxval
= XFS_MAX_SECTORSIZE
,
510 .defaultval
= SUBOPT_NEEDS_VAL
,
513 .conflicts
= { { &lopts
, L_INTERNAL
},
514 { NULL
, LAST_CONFLICT
} },
520 .conflicts
= { { &lopts
, L_AGNUM
},
522 { &lopts
, L_INTERNAL
},
523 { NULL
, LAST_CONFLICT
} },
524 .defaultval
= SUBOPT_NEEDS_VAL
,
526 { .index
= L_LAZYSBCNTR
,
527 .conflicts
= { { NULL
, LAST_CONFLICT
} },
535 static struct opt_params nopts
= {
539 [N_VERSION
] = "version",
544 .conflicts
= { { NULL
, LAST_CONFLICT
} },
547 .minval
= 1 << XFS_MIN_REC_DIRSIZE
,
548 .maxval
= XFS_MAX_BLOCKSIZE
,
549 .defaultval
= SUBOPT_NEEDS_VAL
,
551 { .index
= N_VERSION
,
552 .conflicts
= { { NULL
, LAST_CONFLICT
} },
555 .defaultval
= SUBOPT_NEEDS_VAL
,
558 .conflicts
= { { NULL
, LAST_CONFLICT
} },
566 static struct opt_params ropts
= {
569 [R_EXTSIZE
] = "extsize",
574 [R_NOALIGN
] = "noalign",
577 { .index
= R_EXTSIZE
,
578 .conflicts
= { { NULL
, LAST_CONFLICT
} },
580 .minval
= XFS_MIN_RTEXTSIZE
,
581 .maxval
= XFS_MAX_RTEXTSIZE
,
582 .defaultval
= SUBOPT_NEEDS_VAL
,
585 .conflicts
= { { NULL
, LAST_CONFLICT
} },
589 .defaultval
= SUBOPT_NEEDS_VAL
,
592 .conflicts
= { { &ropts
, R_NAME
},
593 { NULL
, LAST_CONFLICT
} },
594 .defaultval
= SUBOPT_NEEDS_VAL
,
600 .conflicts
= { { NULL
, LAST_CONFLICT
} },
603 .conflicts
= { { &ropts
, R_DEV
},
604 { NULL
, LAST_CONFLICT
} },
605 .defaultval
= SUBOPT_NEEDS_VAL
,
607 { .index
= R_NOALIGN
,
611 .conflicts
= { { NULL
, LAST_CONFLICT
} },
616 static struct opt_params sopts
= {
620 [S_SECTSIZE
] = "sectsize",
624 .conflicts
= { { &sopts
, S_SECTSIZE
},
625 { &dopts
, D_SECTSIZE
},
626 { NULL
, LAST_CONFLICT
} },
629 .minval
= XFS_MIN_SECTORSIZE
,
630 .maxval
= XFS_MAX_SECTORSIZE
,
631 .defaultval
= SUBOPT_NEEDS_VAL
,
633 { .index
= S_SECTSIZE
,
634 .conflicts
= { { &sopts
, S_SIZE
},
635 { &dopts
, D_SECTSIZE
},
636 { NULL
, LAST_CONFLICT
} },
639 .minval
= XFS_MIN_SECTORSIZE
,
640 .maxval
= XFS_MAX_SECTORSIZE
,
641 .defaultval
= SUBOPT_NEEDS_VAL
,
646 static struct opt_params mopts
= {
650 [M_FINOBT
] = "finobt",
652 [M_RMAPBT
] = "rmapbt",
653 [M_REFLINK
] = "reflink",
657 .conflicts
= { { NULL
, LAST_CONFLICT
} },
663 .conflicts
= { { NULL
, LAST_CONFLICT
} },
669 .conflicts
= { { NULL
, LAST_CONFLICT
} },
670 .defaultval
= SUBOPT_NEEDS_VAL
,
673 .conflicts
= { { NULL
, LAST_CONFLICT
} },
678 { .index
= M_REFLINK
,
679 .conflicts
= { { NULL
, LAST_CONFLICT
} },
687 /* quick way of checking if a parameter was set on the CLI */
690 struct opt_params
*opts
,
693 return opts
->subopt_params
[subopt
].seen
||
694 opts
->subopt_params
[subopt
].str_seen
;
698 * Options configured on the command line.
700 * This stores all the specific config parameters the user sets on the command
701 * line. We do not use these values directly - they are inputs to the mkfs
702 * geometry validation and override any default configuration value we have.
704 * We don't keep flags to indicate what parameters are set - if we need to check
705 * if an option was set on the command line, we check the relevant entry in the
706 * option table which records whether it was specified in the .seen and
707 * .str_seen variables in the table.
709 * Some parameters are stored as strings for post-parsing after their dependent
710 * options have been resolved (e.g. block size and sector size have been parsed
713 * This allows us to check that values have been set without needing separate
714 * flags for each value, and hence avoids needing to record and check for each
715 * specific option that can set the value later on in the code. In the cases
716 * where we don't have a cli_params structure around, the above cli_opt_set()
717 * function can be used.
719 struct sb_feat_args
{
723 bool inode_align
; /* XFS_SB_VERSION_ALIGNBIT */
724 bool nci
; /* XFS_SB_VERSION_BORGBIT */
725 bool lazy_sb_counters
; /* XFS_SB_VERSION2_LAZYSBCOUNTBIT */
726 bool parent_pointers
; /* XFS_SB_VERSION2_PARENTBIT */
727 bool projid32bit
; /* XFS_SB_VERSION2_PROJID32BIT */
728 bool crcs_enabled
; /* XFS_SB_VERSION2_CRCBIT */
729 bool dirftype
; /* XFS_SB_VERSION2_FTYPE */
730 bool finobt
; /* XFS_SB_FEAT_RO_COMPAT_FINOBT */
731 bool spinodes
; /* XFS_SB_FEAT_INCOMPAT_SPINODES */
732 bool rmapbt
; /* XFS_SB_FEAT_RO_COMPAT_RMAPBT */
733 bool reflink
; /* XFS_SB_FEAT_RO_COMPAT_REFLINK */
742 /* parameters that depend on sector/block size being validated. */
752 /* parameters where 0 is a valid CLI value */
760 /* parameters where 0 is not a valid value */
768 /* feature flags that are set */
769 struct sb_feat_args sb_feat
;
771 /* root inode characteristics */
774 /* libxfs device setup */
775 struct libxfs_xinit
*xi
;
779 * Calculated filesystem feature and geometry information.
781 * This structure contains the information we will use to create the on-disk
782 * filesystem from. The validation and calculation code uses it to store all the
783 * temporary and final config state for the filesystem.
785 * The information in this structure will contain a mix of validated CLI input
786 * variables, default feature state and calculated values that are needed to
787 * construct the superblock and other on disk features. These are all in one
788 * place so that we don't have to pass handfuls of seemingly arbitrary variables
789 * around to different functions to do the work we need to do.
807 uint64_t rtextblocks
;
809 uint64_t rtbmblocks
; /* rt bitmap blocks */
811 int dsunit
; /* in FSBs */
812 int dswidth
; /* in FSBs */
813 int lsunit
; /* in FSBs */
827 struct sb_feat_args sb_feat
;
831 * Default filesystem features and configuration values
833 * This structure contains the default mkfs values that are to be used when
834 * a user does not specify the option on the command line. We do not use these
835 * values directly - they are inputs to the mkfs geometry validation and
838 struct mkfs_default_params
{
839 char *source
; /* where the defaults came from */
844 /* feature flags that are set */
845 struct sb_feat_args sb_feat
;
847 /* root inode characteristics */
851 static void __attribute__((noreturn
))
854 fprintf(stderr
, _("Usage: %s\n\
855 /* blocksize */ [-b size=num]\n\
856 /* metadata */ [-m crc=0|1,finobt=0|1,uuid=xxx,rmapbt=0|1,reflink=0|1]\n\
857 /* data subvol */ [-d agcount=n,agsize=n,file,name=xxx,size=num,\n\
858 (sunit=value,swidth=value|su=num,sw=num|noalign),\n\
860 /* force overwrite */ [-f]\n\
861 /* inode size */ [-i perblock=n|size=num,maxpct=n,attr=0|1|2,\n\
862 projid32bit=0|1,sparse=0|1]\n\
863 /* no discard */ [-K]\n\
864 /* log subvol */ [-l agnum=n,internal,size=num,logdev=xxx,version=n\n\
865 sunit=value|su=num,sectsize=num,lazy-count=0|1]\n\
866 /* label */ [-L label (maximum 12 characters)]\n\
867 /* naming */ [-n size=num,version=2|ci,ftype=0|1]\n\
868 /* no-op info only */ [-N]\n\
869 /* prototype file */ [-p fname]\n\
871 /* realtime subvol */ [-r extsize=num,size=num,rtdev=xxx]\n\
872 /* sectorsize */ [-s size=num]\n\
873 /* version */ [-V]\n\
875 <devicename> is required unless -d name=xxx is given.\n\
876 <num> is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxx KiB),\n\
877 xxxm (xxx MiB), xxxg (xxx GiB), xxxt (xxx TiB) or xxxp (xxx PiB).\n\
878 <value> is xxx (512 byte blocks).\n"),
885 struct opt_params
*opts
,
887 struct opt_params
*con_opts
,
890 fprintf(stderr
, _("Cannot specify both -%c %s and -%c %s\n"),
891 con_opts
->name
, con_opts
->subopts
[conflict
],
892 opts
->name
, opts
->subopts
[option
]);
902 fprintf(stderr
, _("Invalid value %s for -%s option\n"), value
, opt
);
910 return (i
& (i
- 1)) == 0;
913 static void __attribute__((noreturn
))
919 fprintf(stderr
, _("-%c %s option requires a value\n"), opt
, tab
[idx
]);
929 fprintf(stderr
, "-%c ", opt
);
931 fprintf(stderr
, "%s ", tab
[idx
]);
932 fprintf(stderr
, _("option respecified\n"));
941 fprintf(stderr
, _("unknown option -%c %s\n"), opt
, s
);
947 unsigned int blksize
,
948 unsigned int sectsize
,
955 i
= strtoll(s
, &sp
, 0);
956 if (i
== 0 && sp
== s
)
967 _("Blocksize must be provided prior to using 'b' suffix.\n"));
976 _("Sectorsize must be specified prior to using 's' suffix.\n"));
1015 const char *optname
)
1017 struct stat statbuf
;
1019 if (*isfile
&& (no_size
|| no_name
)) {
1021 _("if -%s file then -%s name and -%s size are required\n"),
1022 optname
, optname
, optname
);
1027 fprintf(stderr
, _("No device name specified\n"));
1031 if (stat(name
, &statbuf
)) {
1032 if (errno
== ENOENT
&& *isfile
) {
1039 _("Error accessing specified device %s: %s\n"),
1040 name
, strerror(errno
));
1046 * We only want to completely truncate and recreate an existing file if
1047 * we were specifically told it was a file. Set the create flag only in
1048 * this case to trigger that behaviour.
1050 if (S_ISREG(statbuf
.st_mode
)) {
1058 if (S_ISBLK(statbuf
.st_mode
)) {
1061 _("specified \"-%s file\" on a block device %s\n"),
1069 _("specified device %s not a file or block device\n"),
1077 bool force_overwrite
)
1079 if (!force_overwrite
&& check_overwrite(name
)) {
1081 _("%s: Use the -f option to force overwrite.\n"),
1089 validate_ag_geometry(
1095 if (agsize
< XFS_AG_MIN_BLOCKS(blocklog
)) {
1097 _("agsize (%lld blocks) too small, need at least %lld blocks\n"),
1099 (long long)XFS_AG_MIN_BLOCKS(blocklog
));
1103 if (agsize
> XFS_AG_MAX_BLOCKS(blocklog
)) {
1105 _("agsize (%lld blocks) too big, maximum is %lld blocks\n"),
1107 (long long)XFS_AG_MAX_BLOCKS(blocklog
));
1111 if (agsize
> dblocks
) {
1113 _("agsize (%lld blocks) too big, data area is %lld blocks\n"),
1114 (long long)agsize
, (long long)dblocks
);
1118 if (agsize
< XFS_AG_MIN_BLOCKS(blocklog
)) {
1120 _("too many allocation groups for size = %lld\n"),
1122 fprintf(stderr
, _("need at most %lld allocation groups\n"),
1123 (long long)(dblocks
/ XFS_AG_MIN_BLOCKS(blocklog
) +
1124 (dblocks
% XFS_AG_MIN_BLOCKS(blocklog
) != 0)));
1128 if (agsize
> XFS_AG_MAX_BLOCKS(blocklog
)) {
1130 _("too few allocation groups for size = %lld\n"), (long long)agsize
);
1132 _("need at least %lld allocation groups\n"),
1133 (long long)(dblocks
/ XFS_AG_MAX_BLOCKS(blocklog
) +
1134 (dblocks
% XFS_AG_MAX_BLOCKS(blocklog
) != 0)));
1139 * If the last AG is too small, reduce the filesystem size
1140 * and drop the blocks.
1142 if ( dblocks
% agsize
!= 0 &&
1143 (dblocks
% agsize
< XFS_AG_MIN_BLOCKS(blocklog
))) {
1145 _("last AG size %lld blocks too small, minimum size is %lld blocks\n"),
1146 (long long)(dblocks
% agsize
),
1147 (long long)XFS_AG_MIN_BLOCKS(blocklog
));
1152 * If agcount is too large, make it smaller.
1154 if (agcount
> XFS_MAX_AGNUMBER
+ 1) {
1156 _("%lld allocation groups is too many, maximum is %lld\n"),
1157 (long long)agcount
, (long long)XFS_MAX_AGNUMBER
+ 1);
1163 zero_old_xfs_structures(
1174 * We open regular files with O_TRUNC|O_CREAT. Nothing to do here...
1176 if (xi
->disfile
&& xi
->dcreat
)
1180 * read in existing filesystem superblock, use its geometry
1181 * settings and zero the existing secondary superblocks.
1183 buf
= memalign(libxfs_device_alignment(), new_sb
->sb_sectsize
);
1186 _("error reading existing superblock -- failed to memalign buffer\n"));
1189 memset(buf
, 0, new_sb
->sb_sectsize
);
1192 * If we are creating an image file, it might be of zero length at this
1193 * point in time. Hence reading the existing superblock is going to
1194 * return zero bytes. It's not a failure we need to warn about in this
1197 off
= pread(xi
->dfd
, buf
, new_sb
->sb_sectsize
, 0);
1198 if (off
!= new_sb
->sb_sectsize
) {
1201 _("error reading existing superblock: %s\n"),
1205 libxfs_sb_from_disk(&sb
, buf
);
1208 * perform same basic superblock validation to make sure we
1209 * actually zero secondary blocks
1211 if (sb
.sb_magicnum
!= XFS_SB_MAGIC
|| sb
.sb_blocksize
== 0)
1214 for (bsize
= 1, i
= 0; bsize
< sb
.sb_blocksize
&&
1215 i
< sizeof(sb
.sb_blocksize
) * NBBY
; i
++)
1218 if (i
< XFS_MIN_BLOCKSIZE_LOG
|| i
> XFS_MAX_BLOCKSIZE_LOG
||
1219 i
!= sb
.sb_blocklog
)
1222 if (sb
.sb_dblocks
> ((uint64_t)sb
.sb_agcount
* sb
.sb_agblocks
) ||
1223 sb
.sb_dblocks
< ((uint64_t)(sb
.sb_agcount
- 1) *
1224 sb
.sb_agblocks
+ XFS_MIN_AG_BLOCKS
))
1228 * block size and basic geometry seems alright, zero the secondaries.
1230 memset(buf
, 0, new_sb
->sb_sectsize
);
1232 for (i
= 1; i
< sb
.sb_agcount
; i
++) {
1233 off
+= sb
.sb_agblocks
;
1234 if (pwrite(xi
->dfd
, buf
, new_sb
->sb_sectsize
,
1235 off
<< sb
.sb_blocklog
) == -1)
1243 discard_blocks(dev_t dev
, uint64_t nsectors
)
1248 * We intentionally ignore errors from the discard ioctl. It is
1249 * not necessary for the mkfs functionality but just an optimization.
1251 fd
= libxfs_device_to_fd(dev
);
1253 platform_discard_blocks(fd
, 0, nsectors
<< 9);
1256 static __attribute__((noreturn
)) void
1259 struct opt_params
*opts
,
1264 _("Invalid value %s for -%c %s option. %s\n"),
1265 value
, opts
->name
, opts
->subopts
[index
],
1271 * Check for conflicts and option respecification.
1275 struct opt_params
*opts
,
1279 struct subopt_param
*sp
= &opts
->subopt_params
[index
];
1282 if (sp
->index
!= index
) {
1284 _("Developer screwed up option parsing (%d/%d)! Please report!\n"),
1286 reqval(opts
->name
, opts
->subopts
, index
);
1290 * Check for respecification of the option. This is more complex than it
1291 * seems because some options are parsed twice - once as a string during
1292 * input parsing, then later the string is passed to getnum for
1293 * conversion into a number and bounds checking. Hence the two variables
1294 * used to track the different uses based on the @str parameter passed
1299 respec(opts
->name
, opts
->subopts
, index
);
1303 respec(opts
->name
, opts
->subopts
, index
);
1304 sp
->str_seen
= true;
1307 /* check for conflicts with the option */
1308 for (i
= 0; i
< MAX_CONFLICTS
; i
++) {
1309 struct _conflict
*con
= &sp
->conflicts
[i
];
1311 if (con
->subopt
== LAST_CONFLICT
)
1313 if (con
->opts
->subopt_params
[con
->subopt
].seen
||
1314 con
->opts
->subopt_params
[con
->subopt
].str_seen
)
1315 conflict(opts
, index
, con
->opts
, con
->subopt
);
1322 struct opt_params
*opts
,
1325 struct subopt_param
*sp
= &opts
->subopt_params
[index
];
1328 check_opt(opts
, index
, false);
1329 /* empty strings might just return a default value */
1330 if (!str
|| *str
== '\0') {
1331 if (sp
->defaultval
== SUBOPT_NEEDS_VAL
)
1332 reqval(opts
->name
, opts
->subopts
, index
);
1333 return sp
->defaultval
;
1336 if (sp
->minval
== 0 && sp
->maxval
== 0) {
1338 _("Option -%c %s has undefined minval/maxval."
1339 "Can't verify value range. This is a bug.\n"),
1340 opts
->name
, opts
->subopts
[index
]);
1345 * Some values are pure numbers, others can have suffixes that define
1346 * the units of the number. Those get passed to cvtnum(), otherwise we
1347 * convert it ourselves to guarantee there is no trailing garbage in the
1351 c
= cvtnum(blocksize
, sectorsize
, str
);
1355 c
= strtoll(str
, &str_end
, 0);
1356 if (c
== 0 && str_end
== str
)
1357 illegal_option(str
, opts
, index
,
1358 _("Value not recognized as number."));
1359 if (*str_end
!= '\0')
1360 illegal_option(str
, opts
, index
,
1361 _("Unit suffixes are not allowed."));
1364 /* Validity check the result. */
1366 illegal_option(str
, opts
, index
, _("Value is too small."));
1367 else if (c
> sp
->maxval
)
1368 illegal_option(str
, opts
, index
, _("Value is too large."));
1369 if (sp
->is_power_2
&& !ispow2(c
))
1370 illegal_option(str
, opts
, index
, _("Value must be a power of 2."));
1375 * Option is a string - do all the option table work, and check there
1376 * is actually an option string. Otherwise we don't do anything with the string
1377 * here - validation will be done later when the string is converted to a value
1378 * or used as a file/device path.
1383 struct opt_params
*opts
,
1386 check_opt(opts
, index
, true);
1388 /* empty strings for string options are not valid */
1389 if (!str
|| *str
== '\0')
1390 reqval(opts
->name
, opts
->subopts
, index
);
1396 struct opt_params
*opts
,
1399 struct cli_params
*cli
)
1403 cli
->blocksize
= getnum(value
, opts
, subopt
);
1413 struct opt_params
*opts
,
1416 struct cli_params
*cli
)
1420 cli
->agcount
= getnum(value
, opts
, subopt
);
1423 cli
->agsize
= getstr(value
, opts
, subopt
);
1426 cli
->xi
->disfile
= getnum(value
, opts
, subopt
);
1429 cli
->xi
->dname
= getstr(value
, opts
, subopt
);
1432 cli
->dsize
= getstr(value
, opts
, subopt
);
1435 cli
->dsunit
= getnum(value
, opts
, subopt
);
1438 cli
->dswidth
= getnum(value
, opts
, subopt
);
1441 cli
->dsu
= getstr(value
, opts
, subopt
);
1444 cli
->dsw
= getnum(value
, opts
, subopt
);
1447 cli
->sb_feat
.nodalign
= getnum(value
, opts
, subopt
);
1450 cli
->sectorsize
= getnum(value
, opts
, subopt
);
1453 if (getnum(value
, opts
, subopt
))
1454 cli
->fsx
.fsx_xflags
|= FS_XFLAG_RTINHERIT
;
1457 cli
->fsx
.fsx_projid
= getnum(value
, opts
, subopt
);
1458 cli
->fsx
.fsx_xflags
|= FS_XFLAG_PROJINHERIT
;
1460 case D_EXTSZINHERIT
:
1461 cli
->fsx
.fsx_extsize
= getnum(value
, opts
, subopt
);
1462 cli
->fsx
.fsx_xflags
|= FS_XFLAG_EXTSZINHERIT
;
1465 cli
->fsx
.fsx_cowextsize
= getnum(value
, opts
, subopt
);
1466 cli
->fsx
.fsx_xflags
|= FS_XFLAG_COWEXTSIZE
;
1476 struct opt_params
*opts
,
1479 struct cli_params
*cli
)
1483 cli
->sb_feat
.inode_align
= getnum(value
, opts
, subopt
);
1486 cli
->imaxpct
= getnum(value
, opts
, subopt
);
1489 cli
->inopblock
= getnum(value
, opts
, subopt
);
1492 cli
->inodesize
= getnum(value
, opts
, subopt
);
1495 cli
->sb_feat
.attr_version
= getnum(value
, opts
, subopt
);
1498 cli
->sb_feat
.projid32bit
= getnum(value
, opts
, subopt
);
1501 cli
->sb_feat
.spinodes
= getnum(value
, opts
, subopt
);
1511 struct opt_params
*opts
,
1514 struct cli_params
*cli
)
1518 cli
->logagno
= getnum(value
, opts
, subopt
);
1521 cli
->xi
->lisfile
= getnum(value
, opts
, subopt
);
1524 cli
->loginternal
= getnum(value
, opts
, subopt
);
1527 cli
->lsu
= getstr(value
, opts
, subopt
);
1530 cli
->lsunit
= getnum(value
, opts
, subopt
);
1534 cli
->xi
->logname
= getstr(value
, opts
, subopt
);
1535 cli
->loginternal
= 0;
1538 cli
->sb_feat
.log_version
= getnum(value
, opts
, subopt
);
1541 cli
->logsize
= getstr(value
, opts
, subopt
);
1544 cli
->lsectorsize
= getnum(value
, opts
, subopt
);
1547 cli
->sb_feat
.lazy_sb_counters
= getnum(value
, opts
, subopt
);
1557 struct opt_params
*opts
,
1560 struct cli_params
*cli
)
1564 cli
->sb_feat
.crcs_enabled
= getnum(value
, opts
, subopt
);
1565 if (cli
->sb_feat
.crcs_enabled
)
1566 cli
->sb_feat
.dirftype
= true;
1569 cli
->sb_feat
.finobt
= getnum(value
, opts
, subopt
);
1572 if (!value
|| *value
== '\0')
1573 reqval('m', opts
->subopts
, subopt
);
1574 if (platform_uuid_parse(value
, &cli
->uuid
))
1575 illegal(value
, "m uuid");
1578 cli
->sb_feat
.rmapbt
= getnum(value
, opts
, subopt
);
1581 cli
->sb_feat
.reflink
= getnum(value
, opts
, subopt
);
1591 struct opt_params
*opts
,
1594 struct cli_params
*cli
)
1598 cli
->dirblocksize
= getstr(value
, opts
, subopt
);
1601 value
= getstr(value
, &nopts
, subopt
);
1602 if (!strcasecmp(value
, "ci")) {
1604 cli
->sb_feat
.nci
= true;
1606 cli
->sb_feat
.dir_version
= getnum(value
, opts
, subopt
);
1610 cli
->sb_feat
.dirftype
= getnum(value
, opts
, subopt
);
1620 struct opt_params
*opts
,
1623 struct cli_params
*cli
)
1627 cli
->rtextsize
= getstr(value
, opts
, subopt
);
1630 cli
->xi
->risfile
= getnum(value
, opts
, subopt
);
1634 cli
->xi
->rtname
= getstr(value
, opts
, subopt
);
1637 cli
->rtsize
= getstr(value
, opts
, subopt
);
1640 cli
->sb_feat
.nortalign
= getnum(value
, opts
, subopt
);
1650 struct opt_params
*opts
,
1653 struct cli_params
*cli
)
1658 cli
->sectorsize
= getnum(value
, opts
, subopt
);
1659 cli
->lsectorsize
= cli
->sectorsize
;
1667 static struct subopts
{
1669 struct opt_params
*opts
;
1670 int (*parser
)(struct opt_params
*opts
,
1673 struct cli_params
*cli
);
1675 { 'b', &bopts
, block_opts_parser
},
1676 { 'd', &dopts
, data_opts_parser
},
1677 { 'i', &iopts
, inode_opts_parser
},
1678 { 'l', &lopts
, log_opts_parser
},
1679 { 'm', &mopts
, meta_opts_parser
},
1680 { 'n', &nopts
, naming_opts_parser
},
1681 { 'r', &ropts
, rtdev_opts_parser
},
1682 { 's', &sopts
, sector_opts_parser
},
1683 { '\0', NULL
, NULL
},
1690 struct cli_params
*cli
)
1692 struct subopts
*sop
= &subopt_tab
[0];
1697 if (sop
->opt
== opt
)
1702 /* should never happen */
1707 while (*p
!= '\0') {
1708 char **subopts
= (char **)sop
->opts
->subopts
;
1712 subopt
= getsubopt(&p
, subopts
, &value
);
1714 ret
= (sop
->parser
)(sop
->opts
, subopt
, value
, cli
);
1716 unknown(opt
, value
);
1721 validate_sectorsize(
1722 struct mkfs_params
*cfg
,
1723 struct cli_params
*cli
,
1724 struct mkfs_default_params
*dft
,
1725 struct fs_topology
*ft
,
1728 int force_overwrite
)
1730 /* set configured sector sizes in preparation for checks */
1731 if (!cli
->sectorsize
) {
1732 cfg
->sectorsize
= dft
->sectorsize
;
1734 cfg
->sectorsize
= cli
->sectorsize
;
1736 cfg
->sectorlog
= libxfs_highbit32(cfg
->sectorsize
);
1739 * Before anything else, verify that we are correctly operating on
1740 * files or block devices and set the control parameters correctly.
1742 check_device_type(dfile
, &cli
->xi
->disfile
, !cli
->dsize
, !dfile
,
1743 dry_run
? NULL
: &cli
->xi
->dcreat
, "d");
1744 if (!cli
->loginternal
)
1745 check_device_type(cli
->xi
->logname
, &cli
->xi
->lisfile
,
1746 !cli
->logsize
, !cli
->xi
->logname
,
1747 dry_run
? NULL
: &cli
->xi
->lcreat
, "l");
1748 if (cli
->xi
->rtname
)
1749 check_device_type(cli
->xi
->rtname
, &cli
->xi
->risfile
,
1750 !cli
->rtsize
, !cli
->xi
->rtname
,
1751 dry_run
? NULL
: &cli
->xi
->rcreat
, "r");
1754 * Explicitly disable direct IO for image files so we don't error out on
1755 * sector size mismatches between the new filesystem and the underlying
1758 if (cli
->xi
->disfile
|| cli
->xi
->lisfile
|| cli
->xi
->risfile
)
1759 cli
->xi
->isdirect
= 0;
1761 memset(ft
, 0, sizeof(*ft
));
1762 get_topology(cli
->xi
, ft
, force_overwrite
);
1764 if (!cli
->sectorsize
) {
1766 * Unless specified manually on the command line use the
1767 * advertised sector size of the device. We use the physical
1768 * sector size unless the requested block size is smaller
1769 * than that, then we can use logical, but warn about the
1772 * Set the topology sectors if they were not probed to the
1773 * minimum supported sector size.
1776 if (!ft
->lsectorsize
)
1777 ft
->lsectorsize
= XFS_MIN_SECTORSIZE
;
1779 /* Older kernels may not have physical/logical distinction */
1780 if (!ft
->psectorsize
)
1781 ft
->psectorsize
= ft
->lsectorsize
;
1783 cfg
->sectorsize
= ft
->psectorsize
;
1784 if (cfg
->blocksize
< cfg
->sectorsize
&&
1785 cfg
->blocksize
>= ft
->lsectorsize
) {
1787 _("specified blocksize %d is less than device physical sector size %d\n"
1788 "switching to logical sector size %d\n"),
1789 cfg
->blocksize
, ft
->psectorsize
,
1791 cfg
->sectorsize
= ft
->lsectorsize
;
1794 cfg
->sectorlog
= libxfs_highbit32(cfg
->sectorsize
);
1797 /* validate specified/probed sector size */
1798 if (cfg
->sectorsize
< XFS_MIN_SECTORSIZE
||
1799 cfg
->sectorsize
> XFS_MAX_SECTORSIZE
) {
1800 fprintf(stderr
, _("illegal sector size %d\n"), cfg
->sectorsize
);
1804 if (cfg
->blocksize
< cfg
->sectorsize
) {
1806 _("block size %d cannot be smaller than sector size %d\n"),
1807 cfg
->blocksize
, cfg
->sectorsize
);
1811 if (cfg
->sectorsize
< ft
->lsectorsize
) {
1812 fprintf(stderr
, _("illegal sector size %d; hw sector is %d\n"),
1813 cfg
->sectorsize
, ft
->lsectorsize
);
1820 struct mkfs_params
*cfg
,
1821 struct cli_params
*cli
,
1822 struct mkfs_default_params
*dft
)
1825 * Blocksize and sectorsize first, other things depend on them
1826 * For RAID4/5/6 we want to align sector size and block size,
1827 * so we need to start with the device geometry extraction too.
1829 if (!cli
->blocksize
)
1830 cfg
->blocksize
= dft
->blocksize
;
1832 cfg
->blocksize
= cli
->blocksize
;
1833 cfg
->blocklog
= libxfs_highbit32(cfg
->blocksize
);
1835 /* validate block sizes are in range */
1836 if (cfg
->blocksize
< XFS_MIN_BLOCKSIZE
||
1837 cfg
->blocksize
> XFS_MAX_BLOCKSIZE
) {
1838 fprintf(stderr
, _("illegal block size %d\n"), cfg
->blocksize
);
1842 if (cli
->sb_feat
.crcs_enabled
&&
1843 cfg
->blocksize
< XFS_MIN_CRC_BLOCKSIZE
) {
1845 _("Minimum block size for CRC enabled filesystems is %d bytes.\n"),
1846 XFS_MIN_CRC_BLOCKSIZE
);
1853 * Grab log sector size and validate.
1855 * XXX: should we probe sector size on external log device rather than using
1856 * the data device sector size?
1859 validate_log_sectorsize(
1860 struct mkfs_params
*cfg
,
1861 struct cli_params
*cli
,
1862 struct mkfs_default_params
*dft
)
1865 if (cli
->loginternal
&& cli
->lsectorsize
&&
1866 cli
->lsectorsize
!= cfg
->sectorsize
) {
1868 _("Can't change sector size on internal log!\n"));
1872 if (cli
->lsectorsize
)
1873 cfg
->lsectorsize
= cli
->lsectorsize
;
1874 else if (cli
->loginternal
)
1875 cfg
->lsectorsize
= cfg
->sectorsize
;
1877 cfg
->lsectorsize
= dft
->sectorsize
;
1878 cfg
->lsectorlog
= libxfs_highbit32(cfg
->lsectorsize
);
1880 if (cfg
->lsectorsize
< XFS_MIN_SECTORSIZE
||
1881 cfg
->lsectorsize
> XFS_MAX_SECTORSIZE
||
1882 cfg
->lsectorsize
> cfg
->blocksize
) {
1883 fprintf(stderr
, _("illegal log sector size %d\n"),
1887 if (cfg
->lsectorsize
> XFS_MIN_SECTORSIZE
) {
1888 if (cli
->sb_feat
.log_version
< 2) {
1889 /* user specified non-default log version */
1891 _("Version 1 logs do not support sector size %d\n"),
1897 /* if lsu or lsunit was specified, automatically use v2 logs */
1898 if ((cli_opt_set(&lopts
, L_SU
) || cli_opt_set(&lopts
, L_SUNIT
)) &&
1899 cli
->sb_feat
.log_version
== 1) {
1901 _("log stripe unit specified, using v2 logs\n"));
1902 cli
->sb_feat
.log_version
= 2;
1908 * Check that the incoming features make sense. The CLI structure was
1909 * initialised with the default values before parsing, so we can just
1910 * check it and copy it straight across to the cfg structure if it
1914 validate_sb_features(
1915 struct mkfs_params
*cfg
,
1916 struct cli_params
*cli
)
1919 * Now we have blocks and sector sizes set up, check parameters that are
1920 * no longer optional for CRC enabled filesystems. Catch them up front
1921 * here before doing anything else.
1923 if (cli
->sb_feat
.crcs_enabled
) {
1924 /* minimum inode size is 512 bytes, rest checked later */
1925 if (cli
->inodesize
&&
1926 cli
->inodesize
< (1 << XFS_DINODE_DFL_CRC_LOG
)) {
1928 _("Minimum inode size for CRCs is %d bytes\n"),
1929 1 << XFS_DINODE_DFL_CRC_LOG
);
1933 /* inodes always aligned */
1934 if (!cli
->sb_feat
.inode_align
) {
1936 _("Inodes always aligned for CRC enabled filesystems\n"));
1940 /* lazy sb counters always on */
1941 if (!cli
->sb_feat
.lazy_sb_counters
) {
1943 _("Lazy superblock counters always enabled for CRC enabled filesystems\n"));
1947 /* version 2 logs always on */
1948 if (cli
->sb_feat
.log_version
!= 2) {
1950 _("V2 logs always enabled for CRC enabled filesystems\n"));
1954 /* attr2 always on */
1955 if (cli
->sb_feat
.attr_version
!= 2) {
1957 _("V2 attribute format always enabled on CRC enabled filesystems\n"));
1961 /* 32 bit project quota always on */
1962 /* attr2 always on */
1963 if (!cli
->sb_feat
.projid32bit
) {
1965 _("32 bit Project IDs always enabled on CRC enabled filesystems\n"));
1969 /* ftype always on */
1970 if (!cli
->sb_feat
.dirftype
) {
1972 _("Directory ftype field always enabled on CRC enabled filesystems\n"));
1976 } else { /* !crcs_enabled */
1978 * The kernel doesn't support crc=0,finobt=1 filesystems.
1979 * If crcs are not enabled and the user has not explicitly
1980 * turned finobt on, then silently turn it off to avoid an
1981 * unnecessary warning.
1982 * If the user explicitly tried to use crc=0,finobt=1,
1983 * then issue an error.
1984 * The same is also true for sparse inodes and reflink.
1986 if (cli
->sb_feat
.finobt
&& cli_opt_set(&mopts
, M_FINOBT
)) {
1988 _("finobt not supported without CRC support\n"));
1991 cli
->sb_feat
.finobt
= false;
1993 if (cli
->sb_feat
.spinodes
&& cli_opt_set(&iopts
, I_SPINODES
)) {
1995 _("sparse inodes not supported without CRC support\n"));
1998 cli
->sb_feat
.spinodes
= false;
2000 if (cli
->sb_feat
.rmapbt
) {
2002 _("rmapbt not supported without CRC support\n"));
2005 cli
->sb_feat
.rmapbt
= false;
2007 if (cli
->sb_feat
.reflink
&& cli_opt_set(&mopts
, M_REFLINK
)) {
2009 _("reflink not supported without CRC support\n"));
2012 cli
->sb_feat
.reflink
= false;
2015 if ((cli
->fsx
.fsx_xflags
& FS_XFLAG_COWEXTSIZE
) &&
2016 !cli
->sb_feat
.reflink
) {
2018 _("cowextsize not supported without reflink support\n"));
2022 if (cli
->sb_feat
.reflink
&& cli
->xi
->rtname
) {
2024 _("reflink not supported with realtime devices\n"));
2026 cli
->sb_feat
.reflink
= false;
2029 if (cli
->sb_feat
.rmapbt
&& cli
->xi
->rtname
) {
2031 _("rmapbt not supported with realtime devices\n"));
2033 cli
->sb_feat
.rmapbt
= false;
2037 * Copy features across to config structure now.
2039 cfg
->sb_feat
= cli
->sb_feat
;
2040 if (!platform_uuid_is_null(&cli
->uuid
))
2041 platform_uuid_copy(&cfg
->uuid
, &cli
->uuid
);
2045 validate_dirblocksize(
2046 struct mkfs_params
*cfg
,
2047 struct cli_params
*cli
)
2050 if (cli
->dirblocksize
)
2051 cfg
->dirblocksize
= getnum(cli
->dirblocksize
, &nopts
, N_SIZE
);
2053 if (cfg
->dirblocksize
) {
2054 if (cfg
->dirblocksize
< cfg
->blocksize
||
2055 cfg
->dirblocksize
> XFS_MAX_BLOCKSIZE
) {
2056 fprintf(stderr
, _("illegal directory block size %d\n"),
2060 cfg
->dirblocklog
= libxfs_highbit32(cfg
->dirblocksize
);
2064 /* use default size based on current block size */
2065 if (cfg
->blocksize
< (1 << XFS_MIN_REC_DIRSIZE
))
2066 cfg
->dirblocklog
= XFS_MIN_REC_DIRSIZE
;
2068 cfg
->dirblocklog
= cfg
->blocklog
;
2069 cfg
->dirblocksize
= 1 << cfg
->dirblocklog
;
2074 struct mkfs_params
*cfg
,
2075 struct cli_params
*cli
)
2079 cfg
->inodelog
= cfg
->blocklog
- libxfs_highbit32(cli
->inopblock
);
2080 else if (cli
->inodesize
)
2081 cfg
->inodelog
= libxfs_highbit32(cli
->inodesize
);
2082 else if (cfg
->sb_feat
.crcs_enabled
)
2083 cfg
->inodelog
= XFS_DINODE_DFL_CRC_LOG
;
2085 cfg
->inodelog
= XFS_DINODE_DFL_LOG
;
2087 cfg
->inodesize
= 1 << cfg
->inodelog
;
2088 cfg
->inopblock
= cfg
->blocksize
/ cfg
->inodesize
;
2090 /* input parsing has already validated non-crc inode size range */
2091 if (cfg
->sb_feat
.crcs_enabled
&&
2092 cfg
->inodelog
< XFS_DINODE_DFL_CRC_LOG
) {
2094 _("Minimum inode size for CRCs is %d bytes\n"),
2095 1 << XFS_DINODE_DFL_CRC_LOG
);
2099 if (cfg
->inodesize
> cfg
->blocksize
/ XFS_MIN_INODE_PERBLOCK
||
2100 cfg
->inopblock
< XFS_MIN_INODE_PERBLOCK
||
2101 cfg
->inodesize
< XFS_DINODE_MIN_SIZE
||
2102 cfg
->inodesize
> XFS_DINODE_MAX_SIZE
) {
2105 fprintf(stderr
, _("illegal inode size %d\n"), cfg
->inodesize
);
2106 maxsz
= min(cfg
->blocksize
/ XFS_MIN_INODE_PERBLOCK
,
2107 XFS_DINODE_MAX_SIZE
);
2108 if (XFS_DINODE_MIN_SIZE
== maxsz
)
2110 _("allowable inode size with %d byte blocks is %d\n"),
2111 cfg
->blocksize
, XFS_DINODE_MIN_SIZE
);
2114 _("allowable inode size with %d byte blocks is between %d and %d\n"),
2115 cfg
->blocksize
, XFS_DINODE_MIN_SIZE
, maxsz
);
2120 static xfs_rfsblock_t
2123 struct mkfs_params
*cfg
,
2124 struct opt_params
*opts
,
2129 xfs_rfsblock_t dblocks
;
2134 dbytes
= getnum(size
, opts
, sizeopt
);
2135 if (dbytes
% XFS_MIN_BLOCKSIZE
) {
2137 _("illegal %s length %lld, not a multiple of %d\n"),
2138 type
, (long long)dbytes
, XFS_MIN_BLOCKSIZE
);
2141 dblocks
= (xfs_rfsblock_t
)(dbytes
>> cfg
->blocklog
);
2142 if (dbytes
% cfg
->blocksize
) {
2144 _("warning: %s length %lld not a multiple of %d, truncated to %lld\n"),
2145 type
, (long long)dbytes
, cfg
->blocksize
,
2146 (long long)(dblocks
<< cfg
->blocklog
));
2153 struct mkfs_params
*cfg
,
2154 struct cli_params
*cli
,
2155 struct fs_topology
*ft
)
2157 uint64_t rtextbytes
;
2160 * If specified, check rt extent size against its constraints.
2162 if (cli
->rtextsize
) {
2164 rtextbytes
= getnum(cli
->rtextsize
, &ropts
, R_EXTSIZE
);
2165 if (rtextbytes
% cfg
->blocksize
) {
2167 _("illegal rt extent size %lld, not a multiple of %d\n"),
2168 (long long)rtextbytes
, cfg
->blocksize
);
2171 cfg
->rtextblocks
= (xfs_extlen_t
)(rtextbytes
>> cfg
->blocklog
);
2174 * If realtime extsize has not been specified by the user,
2175 * and the underlying volume is striped, then set rtextblocks
2176 * to the stripe width.
2180 if (!cfg
->sb_feat
.nortalign
&& !cli
->xi
->risfile
&&
2181 !(!cli
->rtsize
&& cli
->xi
->disfile
))
2182 rswidth
= ft
->rtswidth
;
2186 /* check that rswidth is a multiple of fs blocksize */
2187 if (!cfg
->sb_feat
.nortalign
&& rswidth
&&
2188 !(BBTOB(rswidth
) % cfg
->blocksize
)) {
2189 rswidth
= DTOBT(rswidth
, cfg
->blocklog
);
2190 rtextbytes
= rswidth
<< cfg
->blocklog
;
2191 if (rtextbytes
> XFS_MIN_RTEXTSIZE
&&
2192 rtextbytes
<= XFS_MAX_RTEXTSIZE
) {
2193 cfg
->rtextblocks
= rswidth
;
2196 if (!cfg
->rtextblocks
) {
2197 cfg
->rtextblocks
= (cfg
->blocksize
< XFS_MIN_RTEXTSIZE
)
2198 ? XFS_MIN_RTEXTSIZE
>> cfg
->blocklog
2202 ASSERT(cfg
->rtextblocks
);
2205 /* Validate the incoming extsize hint. */
2207 validate_extsize_hint(
2208 struct xfs_mount
*mp
,
2209 struct cli_params
*cli
)
2215 * First we validate the extent size inherit hint on a directory so
2216 * that we know that we'll be propagating a correct hint and flag to
2217 * new files on the data device.
2219 if (cli
->fsx
.fsx_xflags
& FS_XFLAG_EXTSZINHERIT
)
2220 flags
|= XFS_DIFLAG_EXTSZINHERIT
;
2222 fa
= libxfs_inode_validate_extsize(mp
, cli
->fsx
.fsx_extsize
, S_IFDIR
,
2226 _("illegal extent size hint %lld, must be less than %u.\n"),
2227 (long long)cli
->fsx
.fsx_extsize
,
2228 min(MAXEXTLEN
, mp
->m_sb
.sb_agblocks
/ 2));
2233 * Now we do it again with a realtime file so that we know the hint and
2234 * flag that get passed on to realtime files will be correct.
2236 if (mp
->m_sb
.sb_rextsize
== 0)
2239 flags
= XFS_DIFLAG_REALTIME
;
2240 if (cli
->fsx
.fsx_xflags
& FS_XFLAG_EXTSZINHERIT
)
2241 flags
|= XFS_DIFLAG_EXTSIZE
;
2243 fa
= libxfs_inode_validate_extsize(mp
, cli
->fsx
.fsx_extsize
, S_IFREG
,
2248 _("illegal extent size hint %lld, must be less than %u and a multiple of %u.\n"),
2249 (long long)cli
->fsx
.fsx_extsize
,
2250 min(MAXEXTLEN
, mp
->m_sb
.sb_agblocks
/ 2),
2251 mp
->m_sb
.sb_rextsize
);
2256 /* Validate the incoming CoW extsize hint. */
2258 validate_cowextsize_hint(
2259 struct xfs_mount
*mp
,
2260 struct cli_params
*cli
)
2263 uint64_t flags2
= 0;
2266 * Validate the copy on write extent size inherit hint on a directory
2267 * so that we know that we'll be propagating a correct hint and flag to
2268 * new files on the data device.
2270 if (cli
->fsx
.fsx_xflags
& FS_XFLAG_COWEXTSIZE
)
2271 flags2
|= XFS_DIFLAG2_COWEXTSIZE
;
2273 fa
= libxfs_inode_validate_cowextsize(mp
, cli
->fsx
.fsx_cowextsize
,
2274 S_IFDIR
, 0, flags2
);
2277 _("illegal CoW extent size hint %lld, must be less than %u.\n"),
2278 (long long)cli
->fsx
.fsx_cowextsize
,
2279 min(MAXEXTLEN
, mp
->m_sb
.sb_agblocks
/ 2));
2285 * Validate the configured stripe geometry, or is none is specified, pull
2286 * the configuration from the underlying device.
2288 * CLI parameters come in as different units, go out as filesystem blocks.
2291 calc_stripe_factors(
2292 struct mkfs_params
*cfg
,
2293 struct cli_params
*cli
,
2294 struct fs_topology
*ft
)
2296 long long int big_dswidth
;
2303 bool use_dev
= false;
2305 if (cli_opt_set(&dopts
, D_SUNIT
))
2306 dsunit
= cli
->dsunit
;
2307 if (cli_opt_set(&dopts
, D_SWIDTH
))
2308 dswidth
= cli
->dswidth
;
2310 if (cli_opt_set(&dopts
, D_SU
))
2311 dsu
= getnum(cli
->dsu
, &dopts
, D_SU
);
2312 if (cli_opt_set(&dopts
, D_SW
))
2315 /* data sunit/swidth options */
2316 if (cli_opt_set(&dopts
, D_SUNIT
) != cli_opt_set(&dopts
, D_SWIDTH
)) {
2318 _("both data sunit and data swidth options must be specified\n"));
2322 /* convert dsu/dsw to dsunit/dswidth and use them from now on */
2324 if (cli_opt_set(&dopts
, D_SU
) != cli_opt_set(&dopts
, D_SW
)) {
2326 _("both data su and data sw options must be specified\n"));
2330 if (dsu
% cfg
->sectorsize
) {
2332 _("data su must be a multiple of the sector size (%d)\n"), cfg
->sectorsize
);
2336 dsunit
= (int)BTOBBT(dsu
);
2337 big_dswidth
= (long long int)dsunit
* dsw
;
2338 if (big_dswidth
> INT_MAX
) {
2340 _("data stripe width (%lld) is too large of a multiple of the data stripe unit (%d)\n"),
2341 big_dswidth
, dsunit
);
2344 dswidth
= big_dswidth
;
2347 if ((dsunit
&& !dswidth
) || (!dsunit
&& dswidth
) ||
2348 (dsunit
&& (dswidth
% dsunit
!= 0))) {
2350 _("data stripe width (%d) must be a multiple of the data stripe unit (%d)\n"),
2355 /* If sunit & swidth were manually specified as 0, same as noalign */
2356 if ((cli_opt_set(&dopts
, D_SUNIT
) || cli_opt_set(&dopts
, D_SU
)) &&
2357 !dsunit
&& !dswidth
)
2358 cfg
->sb_feat
.nodalign
= true;
2360 /* if we are not using alignment, don't apply device defaults */
2361 if (cfg
->sb_feat
.nodalign
) {
2367 /* if no stripe config set, use the device default */
2369 /* Ignore nonsense from device. XXX add more validation */
2370 if (ft
->dsunit
&& ft
->dswidth
== 0) {
2372 _("%s: Volume reports stripe unit of %d bytes and stripe width of 0, ignoring.\n"),
2373 progname
, BBTOB(ft
->dsunit
));
2377 dsunit
= ft
->dsunit
;
2378 dswidth
= ft
->dswidth
;
2382 /* check and warn if user-specified alignment is sub-optimal */
2383 if (ft
->dsunit
&& ft
->dsunit
!= dsunit
) {
2385 _("%s: Specified data stripe unit %d is not the same as the volume stripe unit %d\n"),
2386 progname
, dsunit
, ft
->dsunit
);
2388 if (ft
->dswidth
&& ft
->dswidth
!= dswidth
) {
2390 _("%s: Specified data stripe width %d is not the same as the volume stripe width %d\n"),
2391 progname
, dswidth
, ft
->dswidth
);
2396 * now we have our stripe config, check it's a multiple of block
2399 if ((BBTOB(dsunit
) % cfg
->blocksize
) ||
2400 (BBTOB(dswidth
) % cfg
->blocksize
)) {
2402 * If we are using device defaults, just clear them and we're
2403 * good to go. Otherwise bail out with an error.
2407 _("%s: Stripe unit(%d) or stripe width(%d) is not a multiple of the block size(%d)\n"),
2408 progname
, BBTOB(dsunit
), BBTOB(dswidth
),
2414 cfg
->sb_feat
.nodalign
= true;
2417 /* convert from 512 byte blocks to fs blocksize */
2418 cfg
->dsunit
= DTOBT(dsunit
, cfg
->blocklog
);
2419 cfg
->dswidth
= DTOBT(dswidth
, cfg
->blocklog
);
2422 /* log sunit options */
2423 if (cli_opt_set(&lopts
, L_SUNIT
))
2424 lsunit
= cli
->lsunit
;
2425 else if (cli_opt_set(&lopts
, L_SU
))
2426 lsu
= getnum(cli
->lsu
, &lopts
, L_SU
);
2427 else if (cfg
->lsectorsize
> XLOG_HEADER_SIZE
)
2428 lsu
= cfg
->blocksize
; /* lsunit matches filesystem block size */
2431 /* verify if lsu is a multiple block size */
2432 if (lsu
% cfg
->blocksize
!= 0) {
2434 _("log stripe unit (%d) must be a multiple of the block size (%d)\n"),
2435 lsu
, cfg
->blocksize
);
2438 lsunit
= (int)BTOBBT(lsu
);
2440 if (BBTOB(lsunit
) % cfg
->blocksize
!= 0) {
2442 _("log stripe unit (%d) must be a multiple of the block size (%d)\n"),
2443 BBTOB(lsunit
), cfg
->blocksize
);
2448 * check that log sunit is modulo fsblksize or default it to dsunit.
2451 /* convert from 512 byte blocks to fs blocks */
2452 cfg
->lsunit
= DTOBT(lsunit
, cfg
->blocklog
);
2453 } else if (cfg
->sb_feat
.log_version
== 2 &&
2454 cfg
->loginternal
&& cfg
->dsunit
) {
2455 /* lsunit and dsunit now in fs blocks */
2456 cfg
->lsunit
= cfg
->dsunit
;
2459 if (cfg
->sb_feat
.log_version
== 2 &&
2460 cfg
->lsunit
* cfg
->blocksize
> 256 * 1024) {
2461 /* Warn only if specified on commandline */
2462 if (cli
->lsu
|| cli
->lsunit
!= -1) {
2464 _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
2465 "log stripe unit adjusted to 32KiB\n"),
2466 (cfg
->lsunit
* cfg
->blocksize
));
2468 /* XXX: 64k block size? */
2469 cfg
->lsunit
= (32 * 1024) / cfg
->blocksize
;
2476 struct mkfs_params
*cfg
,
2477 struct libxfs_xinit
*xi
)
2479 uint64_t sector_mask
;
2482 * Initialize. This will open the log and rt devices as well.
2484 xi
->setblksize
= cfg
->sectorsize
;
2485 if (!libxfs_init(xi
))
2488 fprintf(stderr
, _("no device name given in argument list\n"));
2493 * Ok, Linux only has a 1024-byte resolution on device _size_,
2494 * and the sizes below are in basic 512-byte blocks,
2495 * so if we have (size % 2), on any partition, we can't get
2496 * to the last 512 bytes. The same issue exists for larger
2497 * sector sizes - we cannot write past the last sector.
2499 * So, we reduce the size (in basic blocks) to a perfect
2500 * multiple of the sector size, or 1024, whichever is larger.
2502 sector_mask
= (uint64_t)-1 << (max(cfg
->sectorlog
, 10) - BBSHIFT
);
2503 xi
->dsize
&= sector_mask
;
2504 xi
->rtsize
&= sector_mask
;
2505 xi
->logBBsize
&= (uint64_t)-1 << (max(cfg
->lsectorlog
, 10) - BBSHIFT
);
2510 struct libxfs_xinit
*xi
)
2513 *Â This function has to be called after libxfs has been initialized.
2517 discard_blocks(xi
->ddev
, xi
->dsize
);
2518 if (xi
->rtdev
&& !xi
->risfile
)
2519 discard_blocks(xi
->rtdev
, xi
->rtsize
);
2520 if (xi
->logdev
&& xi
->logdev
!= xi
->ddev
&& !xi
->lisfile
)
2521 discard_blocks(xi
->logdev
, xi
->logBBsize
);
2526 struct mkfs_params
*cfg
,
2527 struct cli_params
*cli
)
2529 struct libxfs_xinit
*xi
= cli
->xi
;
2533 * if the device is a file, we can't validate the size here.
2534 * Instead, the file will be truncated to the correct length
2535 * later on. if it's not a file, we've got a dud device.
2538 fprintf(stderr
, _("can't get size of data subvolume\n"));
2541 ASSERT(cfg
->dblocks
);
2542 } else if (cfg
->dblocks
) {
2543 /* check the size fits into the underlying device */
2544 if (cfg
->dblocks
> DTOBT(xi
->dsize
, cfg
->blocklog
)) {
2546 _("size %s specified for data subvolume is too large, maximum is %lld blocks\n"),
2548 (long long)DTOBT(xi
->dsize
, cfg
->blocklog
));
2552 /* no user size, so use the full block device */
2553 cfg
->dblocks
= DTOBT(xi
->dsize
, cfg
->blocklog
);
2556 if (cfg
->dblocks
< XFS_MIN_DATA_BLOCKS
) {
2558 _("size %lld of data subvolume is too small, minimum %d blocks\n"),
2559 (long long)cfg
->dblocks
, XFS_MIN_DATA_BLOCKS
);
2563 if (xi
->dbsize
> cfg
->sectorsize
) {
2565 "Warning: the data subvolume sector size %u is less than the sector size \n\
2566 reported by the device (%u).\n"),
2567 cfg
->sectorsize
, xi
->dbsize
);
2572 * This is more complex than it needs to be because we still support volume
2573 * based external logs. They are only discovered *after* the devices have been
2574 * opened, hence the crazy "is this really an internal log" checks here.
2578 struct mkfs_params
*cfg
,
2579 struct cli_params
*cli
,
2582 struct libxfs_xinit
*xi
= cli
->xi
;
2586 /* check for volume log first */
2587 if (cli
->loginternal
&& xi
->volname
&& xi
->logdev
) {
2588 *devname
= _("volume log");
2589 cfg
->loginternal
= false;
2591 cfg
->loginternal
= cli
->loginternal
;
2593 /* now run device checks */
2594 if (cfg
->loginternal
) {
2597 _("can't have both external and internal logs\n"));
2602 * if no sector size has been specified on the command line,
2603 * use what has been configured and validated for the data
2606 if (!cli
->lsectorsize
) {
2607 cfg
->lsectorsize
= cfg
->sectorsize
;
2608 cfg
->lsectorlog
= cfg
->sectorlog
;
2611 if (cfg
->sectorsize
!= cfg
->lsectorsize
) {
2613 _("data and log sector sizes must be equal for internal logs\n"));
2616 if (cli
->logsize
&& cfg
->logblocks
>= cfg
->dblocks
) {
2618 _("log size %lld too large for internal log\n"),
2619 (long long)cfg
->logblocks
);
2622 *devname
= _("internal log");
2626 /* External/log subvolume checks */
2628 *devname
= xi
->logname
;
2629 if (!*devname
|| !xi
->logdev
) {
2630 fprintf(stderr
, _("no log subvolume or external log.\n"));
2634 if (!cfg
->logblocks
) {
2635 if (xi
->logBBsize
== 0) {
2637 _("unable to get size of the log subvolume.\n"));
2640 cfg
->logblocks
= DTOBT(xi
->logBBsize
, cfg
->blocklog
);
2641 } else if (cfg
->logblocks
> DTOBT(xi
->logBBsize
, cfg
->blocklog
)) {
2643 _("size %s specified for log subvolume is too large, maximum is %lld blocks\n"),
2645 (long long)DTOBT(xi
->logBBsize
, cfg
->blocklog
));
2649 if (xi
->lbsize
> cfg
->lsectorsize
) {
2651 "Warning: the log subvolume sector size %u is less than the sector size\n\
2652 reported by the device (%u).\n"),
2653 cfg
->lsectorsize
, xi
->lbsize
);
2659 struct mkfs_params
*cfg
,
2660 struct cli_params
*cli
,
2663 struct libxfs_xinit
*xi
= cli
->xi
;
2670 _("size specified for non-existent rt subvolume\n"));
2674 *devname
= _("none");
2677 cfg
->rtbmblocks
= 0;
2681 fprintf(stderr
, _("Invalid zero length rt subvolume found\n"));
2687 *devname
= _("volume rt");
2689 *devname
= xi
->rtname
;
2692 if (cfg
->rtblocks
> DTOBT(xi
->rtsize
, cfg
->blocklog
)) {
2694 _("size %s specified for rt subvolume is too large, maxi->um is %lld blocks\n"),
2696 (long long)DTOBT(xi
->rtsize
, cfg
->blocklog
));
2699 if (xi
->rtbsize
> cfg
->sectorsize
) {
2701 "Warning: the realtime subvolume sector size %u is less than the sector size\n\
2702 reported by the device (%u).\n"),
2703 cfg
->sectorsize
, xi
->rtbsize
);
2706 /* grab volume size */
2707 cfg
->rtblocks
= DTOBT(xi
->rtsize
, cfg
->blocklog
);
2710 cfg
->rtextents
= cfg
->rtblocks
/ cfg
->rtextblocks
;
2711 cfg
->rtbmblocks
= (xfs_extlen_t
)howmany(cfg
->rtextents
,
2712 NBBY
* cfg
->blocksize
);
2716 calculate_initial_ag_geometry(
2717 struct mkfs_params
*cfg
,
2718 struct cli_params
*cli
)
2720 if (cli
->agsize
) { /* User-specified AG size */
2721 cfg
->agsize
= getnum(cli
->agsize
, &dopts
, D_AGSIZE
);
2724 * Check specified agsize is a multiple of blocksize.
2726 if (cfg
->agsize
% cfg
->blocksize
) {
2728 _("agsize (%s) not a multiple of fs blk size (%d)\n"),
2729 cli
->agsize
, cfg
->blocksize
);
2732 cfg
->agsize
/= cfg
->blocksize
;
2733 cfg
->agcount
= cfg
->dblocks
/ cfg
->agsize
+
2734 (cfg
->dblocks
% cfg
->agsize
!= 0);
2736 } else if (cli
->agcount
) { /* User-specified AG count */
2737 cfg
->agcount
= cli
->agcount
;
2738 cfg
->agsize
= cfg
->dblocks
/ cfg
->agcount
+
2739 (cfg
->dblocks
% cfg
->agcount
!= 0);
2741 calc_default_ag_geometry(cfg
->blocklog
, cfg
->dblocks
,
2742 cfg
->dsunit
, &cfg
->agsize
,
2748 * Align the AG size to stripe geometry. If this fails and we are using
2749 * discovered stripe geometry, tell the caller to clear the stripe geometry.
2750 * Otherwise, set the aligned geometry (valid or invalid!) so that the
2751 * validation call will fail and exit.
2755 struct mkfs_params
*cfg
)
2757 uint64_t tmp_agsize
;
2758 int dsunit
= cfg
->dsunit
;
2764 * agsize is not a multiple of dsunit
2766 if ((cfg
->agsize
% dsunit
) != 0) {
2768 * Round up to stripe unit boundary. Also make sure
2769 * that agsize is still larger than
2770 * XFS_AG_MIN_BLOCKS(blocklog)
2772 tmp_agsize
= ((cfg
->agsize
+ dsunit
- 1) / dsunit
) * dsunit
;
2774 * Round down to stripe unit boundary if rounding up
2775 * created an AG size that is larger than the AG max.
2777 if (tmp_agsize
> XFS_AG_MAX_BLOCKS(cfg
->blocklog
))
2778 tmp_agsize
= (cfg
->agsize
/ dsunit
) * dsunit
;
2780 if (tmp_agsize
< XFS_AG_MIN_BLOCKS(cfg
->blocklog
) &&
2781 tmp_agsize
> XFS_AG_MAX_BLOCKS(cfg
->blocklog
)) {
2784 * If the AG size is invalid and we are using device
2785 * probed stripe alignment, just clear the alignment
2788 if (!cli_opt_set(&dopts
, D_SUNIT
) &&
2789 !cli_opt_set(&dopts
, D_SU
)) {
2795 * set the agsize to the invalid value so the following
2796 * validation of the ag will fail and print a nice error
2799 cfg
->agsize
= tmp_agsize
;
2803 /* update geometry to be stripe unit aligned */
2804 cfg
->agsize
= tmp_agsize
;
2805 if (!cli_opt_set(&dopts
, D_AGCOUNT
))
2806 cfg
->agcount
= cfg
->dblocks
/ cfg
->agsize
+
2807 (cfg
->dblocks
% cfg
->agsize
!= 0);
2808 if (cli_opt_set(&dopts
, D_AGSIZE
))
2810 _("agsize rounded to %lld, sunit = %d\n"),
2811 (long long)cfg
->agsize
, dsunit
);
2814 if ((cfg
->agsize
% cfg
->dswidth
) == 0 &&
2815 cfg
->dswidth
!= cfg
->dsunit
&&
2818 if (cli_opt_set(&dopts
, D_AGCOUNT
) ||
2819 cli_opt_set(&dopts
, D_AGSIZE
)) {
2821 "Warning: AG size is a multiple of stripe width. This can cause performance\n\
2822 problems by aligning all AGs on the same disk. To avoid this, run mkfs with\n\
2823 an AG size that is one stripe unit smaller or larger, for example %llu.\n"),
2824 (unsigned long long)cfg
->agsize
- dsunit
);
2829 * This is a non-optimal configuration because all AGs start on
2830 * the same disk in the stripe. Changing the AG size by one
2831 * sunit will guarantee that this does not happen.
2833 tmp_agsize
= cfg
->agsize
- dsunit
;
2834 if (tmp_agsize
< XFS_AG_MIN_BLOCKS(cfg
->blocklog
)) {
2835 tmp_agsize
= cfg
->agsize
+ dsunit
;
2836 if (cfg
->dblocks
< cfg
->agsize
) {
2837 /* oh well, nothing to do */
2838 tmp_agsize
= cfg
->agsize
;
2842 cfg
->agsize
= tmp_agsize
;
2843 cfg
->agcount
= cfg
->dblocks
/ cfg
->agsize
+
2844 (cfg
->dblocks
% cfg
->agsize
!= 0);
2849 * If the last AG is too small, reduce the filesystem size
2850 * and drop the blocks.
2852 if (cfg
->dblocks
% cfg
->agsize
!= 0 &&
2853 (cfg
->dblocks
% cfg
->agsize
< XFS_AG_MIN_BLOCKS(cfg
->blocklog
))) {
2854 ASSERT(!cli_opt_set(&dopts
, D_AGCOUNT
));
2855 cfg
->dblocks
= (xfs_rfsblock_t
)((cfg
->agcount
- 1) * cfg
->agsize
);
2857 ASSERT(cfg
->agcount
!= 0);
2860 validate_ag_geometry(cfg
->blocklog
, cfg
->dblocks
,
2861 cfg
->agsize
, cfg
->agcount
);
2866 struct mkfs_params
*cfg
,
2867 struct cli_params
*cli
)
2869 cfg
->imaxpct
= cli
->imaxpct
;
2874 * This returns the % of the disk space that is used for
2875 * inodes, it changes relatively to the FS size:
2876 * - over 50 TB, use 1%,
2877 * - 1TB - 50 TB, use 5%,
2878 * - under 1 TB, use XFS_DFL_IMAXIMUM_PCT (25%).
2881 if (cfg
->dblocks
< TERABYTES(1, cfg
->blocklog
))
2882 cfg
->imaxpct
= XFS_DFL_IMAXIMUM_PCT
;
2883 else if (cfg
->dblocks
< TERABYTES(50, cfg
->blocklog
))
2890 * Set up the initial state of the superblock so we can start using the
2891 * libxfs geometry macros.
2895 struct mkfs_params
*cfg
,
2898 struct sb_feat_args
*fp
= &cfg
->sb_feat
;
2900 sbp
->sb_versionnum
= XFS_DFL_SB_VERSION_BITS
;
2901 if (fp
->crcs_enabled
)
2902 sbp
->sb_versionnum
|= XFS_SB_VERSION_5
;
2904 sbp
->sb_versionnum
|= XFS_SB_VERSION_4
;
2906 if (fp
->inode_align
) {
2907 int cluster_size
= XFS_INODE_BIG_CLUSTER_SIZE
;
2909 sbp
->sb_versionnum
|= XFS_SB_VERSION_ALIGNBIT
;
2910 if (cfg
->sb_feat
.crcs_enabled
)
2911 cluster_size
*= cfg
->inodesize
/ XFS_DINODE_MIN_SIZE
;
2912 sbp
->sb_inoalignmt
= cluster_size
>> cfg
->blocklog
;
2914 sbp
->sb_inoalignmt
= 0;
2917 sbp
->sb_versionnum
|= XFS_SB_VERSION_DALIGNBIT
;
2918 if (fp
->log_version
== 2)
2919 sbp
->sb_versionnum
|= XFS_SB_VERSION_LOGV2BIT
;
2920 if (fp
->attr_version
== 1)
2921 sbp
->sb_versionnum
|= XFS_SB_VERSION_ATTRBIT
;
2923 sbp
->sb_versionnum
|= XFS_SB_VERSION_BORGBIT
;
2925 if (cfg
->sectorsize
> BBSIZE
|| cfg
->lsectorsize
> BBSIZE
) {
2926 sbp
->sb_versionnum
|= XFS_SB_VERSION_SECTORBIT
;
2927 sbp
->sb_logsectlog
= (uint8_t)cfg
->lsectorlog
;
2928 sbp
->sb_logsectsize
= (uint16_t)cfg
->lsectorsize
;
2930 sbp
->sb_logsectlog
= 0;
2931 sbp
->sb_logsectsize
= 0;
2934 sbp
->sb_features2
= 0;
2935 if (fp
->lazy_sb_counters
)
2936 sbp
->sb_features2
|= XFS_SB_VERSION2_LAZYSBCOUNTBIT
;
2937 if (fp
->projid32bit
)
2938 sbp
->sb_features2
|= XFS_SB_VERSION2_PROJID32BIT
;
2939 if (fp
->parent_pointers
)
2940 sbp
->sb_features2
|= XFS_SB_VERSION2_PARENTBIT
;
2941 if (fp
->crcs_enabled
)
2942 sbp
->sb_features2
|= XFS_SB_VERSION2_CRCBIT
;
2943 if (fp
->attr_version
== 2)
2944 sbp
->sb_features2
|= XFS_SB_VERSION2_ATTR2BIT
;
2946 /* v5 superblocks have their own feature bit for dirftype */
2947 if (fp
->dirftype
&& !fp
->crcs_enabled
)
2948 sbp
->sb_features2
|= XFS_SB_VERSION2_FTYPE
;
2950 /* update whether extended features are in use */
2951 if (sbp
->sb_features2
!= 0)
2952 sbp
->sb_versionnum
|= XFS_SB_VERSION_MOREBITSBIT
;
2955 * Due to a structure alignment issue, sb_features2 ended up in one
2956 * of two locations, the second "incorrect" location represented by
2957 * the sb_bad_features2 field. To avoid older kernels mounting
2958 * filesystems they shouldn't, set both field to the same value.
2960 sbp
->sb_bad_features2
= sbp
->sb_features2
;
2962 if (!fp
->crcs_enabled
)
2965 /* default features for v5 filesystems */
2966 sbp
->sb_features_compat
= 0;
2967 sbp
->sb_features_ro_compat
= 0;
2968 sbp
->sb_features_incompat
= XFS_SB_FEAT_INCOMPAT_FTYPE
;
2969 sbp
->sb_features_log_incompat
= 0;
2972 sbp
->sb_features_ro_compat
= XFS_SB_FEAT_RO_COMPAT_FINOBT
;
2974 sbp
->sb_features_ro_compat
|= XFS_SB_FEAT_RO_COMPAT_RMAPBT
;
2976 sbp
->sb_features_ro_compat
|= XFS_SB_FEAT_RO_COMPAT_REFLINK
;
2979 * Sparse inode chunk support has two main inode alignment requirements.
2980 * First, sparse chunk alignment must match the cluster size. Second,
2981 * full chunk alignment must match the inode chunk size.
2983 * Copy the already calculated/scaled inoalignmt to spino_align and
2984 * update the former to the full inode chunk size.
2987 sbp
->sb_spino_align
= sbp
->sb_inoalignmt
;
2988 sbp
->sb_inoalignmt
= XFS_INODES_PER_CHUNK
*
2989 cfg
->inodesize
>> cfg
->blocklog
;
2990 sbp
->sb_features_incompat
|= XFS_SB_FEAT_INCOMPAT_SPINODES
;
2996 * Make sure that the log size is a multiple of the stripe unit
3000 struct mkfs_params
*cfg
,
3003 uint64_t tmp_logblocks
;
3005 /* nothing to do if it's already aligned. */
3006 if ((cfg
->logblocks
% sunit
) == 0)
3009 if (cli_opt_set(&lopts
, L_SIZE
)) {
3011 _("log size %lld is not a multiple of the log stripe unit %d\n"),
3012 (long long) cfg
->logblocks
, sunit
);
3016 tmp_logblocks
= ((cfg
->logblocks
+ (sunit
- 1)) / sunit
) * sunit
;
3018 /* If the log is too large, round down instead of round up */
3019 if ((tmp_logblocks
> XFS_MAX_LOG_BLOCKS
) ||
3020 ((tmp_logblocks
<< cfg
->blocklog
) > XFS_MAX_LOG_BYTES
)) {
3021 tmp_logblocks
= (cfg
->logblocks
/ sunit
) * sunit
;
3023 cfg
->logblocks
= tmp_logblocks
;
3027 * Make sure that the internal log is correctly aligned to the specified
3032 struct mkfs_params
*cfg
,
3033 struct xfs_mount
*mp
,
3038 /* round up log start if necessary */
3039 if ((cfg
->logstart
% sunit
) != 0)
3040 cfg
->logstart
= ((cfg
->logstart
+ (sunit
- 1)) / sunit
) * sunit
;
3042 /* If our log start overlaps the next AG's metadata, fail. */
3043 if (XFS_FSB_TO_AGBNO(mp
, cfg
->logstart
) <= XFS_AGFL_BLOCK(mp
)) {
3045 _("Due to stripe alignment, the internal log start (%lld) cannot be aligned\n"
3046 "within an allocation group.\n"),
3047 (long long) cfg
->logstart
);
3051 /* round up/down the log size now */
3052 align_log_size(cfg
, sunit
);
3054 /* check the aligned log still starts and ends in the same AG. */
3055 logend
= cfg
->logstart
+ cfg
->logblocks
- 1;
3056 if (XFS_FSB_TO_AGNO(mp
, cfg
->logstart
) != XFS_FSB_TO_AGNO(mp
, logend
)) {
3058 _("Due to stripe alignment, the internal log size (%lld) is too large.\n"
3059 "Must fit within an allocation group.\n"),
3060 (long long) cfg
->logblocks
);
3066 validate_log_size(uint64_t logblocks
, int blocklog
, int min_logblocks
)
3068 if (logblocks
< min_logblocks
) {
3070 _("log size %lld blocks too small, minimum size is %d blocks\n"),
3071 (long long)logblocks
, min_logblocks
);
3074 if (logblocks
> XFS_MAX_LOG_BLOCKS
) {
3076 _("log size %lld blocks too large, maximum size is %lld blocks\n"),
3077 (long long)logblocks
, XFS_MAX_LOG_BLOCKS
);
3080 if ((logblocks
<< blocklog
) > XFS_MAX_LOG_BYTES
) {
3082 _("log size %lld bytes too large, maximum size is %lld bytes\n"),
3083 (long long)(logblocks
<< blocklog
), XFS_MAX_LOG_BYTES
);
3090 struct mkfs_params
*cfg
,
3091 struct cli_params
*cli
,
3092 struct xfs_mount
*mp
)
3094 struct xfs_sb
*sbp
= &mp
->m_sb
;
3096 struct xfs_mount mount
;
3098 /* we need a temporary mount to calculate the minimum log size. */
3099 memset(&mount
, 0, sizeof(mount
));
3101 libxfs_mount(&mount
, &mp
->m_sb
, 0, 0, 0, 0);
3102 min_logblocks
= libxfs_log_calc_minimum_size(&mount
);
3103 libxfs_umount(&mount
);
3105 ASSERT(min_logblocks
);
3106 min_logblocks
= max(XFS_MIN_LOG_BLOCKS
, min_logblocks
);
3108 /* if we have lots of blocks, check against XFS_MIN_LOG_BYTES, too */
3109 if (!cli
->logsize
&&
3110 cfg
->dblocks
>= (1024*1024*1024) >> cfg
->blocklog
)
3111 min_logblocks
= max(min_logblocks
,
3112 XFS_MIN_LOG_BYTES
>> cfg
->blocklog
);
3115 * external logs will have a device and size by now, so all we have
3116 * to do is validate it against minimum size and align it.
3118 if (!cfg
->loginternal
) {
3119 if (min_logblocks
> cfg
->logblocks
) {
3121 _("external log device %lld too small, must be at least %lld blocks\n"),
3122 (long long)cfg
->logblocks
,
3123 (long long)min_logblocks
);
3129 align_log_size(cfg
, cfg
->lsunit
);
3131 validate_log_size(cfg
->logblocks
, cfg
->blocklog
, min_logblocks
);
3135 /* internal log - if no size specified, calculate automatically */
3136 if (!cfg
->logblocks
) {
3137 if (cfg
->dblocks
< GIGABYTES(1, cfg
->blocklog
)) {
3138 /* tiny filesystems get minimum sized logs. */
3139 cfg
->logblocks
= min_logblocks
;
3140 } else if (cfg
->dblocks
< GIGABYTES(16, cfg
->blocklog
)) {
3143 * For small filesystems, we want to use the
3144 * XFS_MIN_LOG_BYTES for filesystems smaller than 16G if
3145 * at all possible, ramping up to 128MB at 256GB.
3147 cfg
->logblocks
= min(XFS_MIN_LOG_BYTES
>> cfg
->blocklog
,
3148 min_logblocks
* XFS_DFL_LOG_FACTOR
);
3151 * With a 2GB max log size, default to maximum size
3152 * at 4TB. This keeps the same ratio from the older
3153 * max log size of 128M at 256GB fs size. IOWs,
3154 * the ratio of fs size to log size is 2048:1.
3156 cfg
->logblocks
= (cfg
->dblocks
<< cfg
->blocklog
) / 2048;
3157 cfg
->logblocks
= cfg
->logblocks
>> cfg
->blocklog
;
3160 /* Ensure the chosen size meets minimum log size requirements */
3161 cfg
->logblocks
= max(min_logblocks
, cfg
->logblocks
);
3164 * Make sure the log fits wholly within an AG
3166 * XXX: If agf->freeblks ends up as 0 because the log uses all
3167 * the free space, it causes the kernel all sorts of problems
3168 * with per-ag reservations. Right now just back it off one
3169 * block, but there's a whole can of worms here that needs to be
3170 * opened to decide what is the valid maximum size of a log in
3173 cfg
->logblocks
= min(cfg
->logblocks
,
3174 libxfs_alloc_ag_max_usable(mp
) - 1);
3176 /* and now clamp the size to the maximum supported size */
3177 cfg
->logblocks
= min(cfg
->logblocks
, XFS_MAX_LOG_BLOCKS
);
3178 if ((cfg
->logblocks
<< cfg
->blocklog
) > XFS_MAX_LOG_BYTES
)
3179 cfg
->logblocks
= XFS_MAX_LOG_BYTES
>> cfg
->blocklog
;
3181 validate_log_size(cfg
->logblocks
, cfg
->blocklog
, min_logblocks
);
3184 if (cfg
->logblocks
> sbp
->sb_agblocks
- libxfs_prealloc_blocks(mp
)) {
3186 _("internal log size %lld too large, must fit in allocation group\n"),
3187 (long long)cfg
->logblocks
);
3191 if (cli_opt_set(&lopts
, L_AGNUM
)) {
3192 if (cli
->logagno
>= sbp
->sb_agcount
) {
3194 _("log ag number %lld too large, must be less than %lld\n"),
3195 (long long)cli
->logagno
,
3196 (long long)sbp
->sb_agcount
);
3199 cfg
->logagno
= cli
->logagno
;
3201 cfg
->logagno
= (xfs_agnumber_t
)(sbp
->sb_agcount
/ 2);
3203 cfg
->logstart
= XFS_AGB_TO_FSB(mp
, cfg
->logagno
,
3204 libxfs_prealloc_blocks(mp
));
3207 * Align the logstart at stripe unit boundary.
3210 align_internal_log(cfg
, mp
, cfg
->lsunit
);
3211 } else if (cfg
->dsunit
) {
3212 align_internal_log(cfg
, mp
, cfg
->dsunit
);
3214 validate_log_size(cfg
->logblocks
, cfg
->blocklog
, min_logblocks
);
3218 * Set up superblock with the minimum parameters required for
3219 * the libxfs macros needed by the log sizing code to run successfully.
3220 * This includes a minimum log size calculation, so we need everything
3221 * that goes into that calculation to be setup here including feature
3225 start_superblock_setup(
3226 struct mkfs_params
*cfg
,
3227 struct xfs_mount
*mp
,
3230 sbp
->sb_magicnum
= XFS_SB_MAGIC
;
3231 sbp
->sb_sectsize
= (uint16_t)cfg
->sectorsize
;
3232 sbp
->sb_sectlog
= (uint8_t)cfg
->sectorlog
;
3233 sbp
->sb_blocksize
= cfg
->blocksize
;
3234 sbp
->sb_blocklog
= (uint8_t)cfg
->blocklog
;
3236 sbp
->sb_agblocks
= (xfs_agblock_t
)cfg
->agsize
;
3237 sbp
->sb_agblklog
= (uint8_t)log2_roundup(cfg
->agsize
);
3238 sbp
->sb_agcount
= (xfs_agnumber_t
)cfg
->agcount
;
3240 sbp
->sb_inodesize
= (uint16_t)cfg
->inodesize
;
3241 sbp
->sb_inodelog
= (uint8_t)cfg
->inodelog
;
3242 sbp
->sb_inopblock
= (uint16_t)(cfg
->blocksize
/ cfg
->inodesize
);
3243 sbp
->sb_inopblog
= (uint8_t)(cfg
->blocklog
- cfg
->inodelog
);
3245 sbp
->sb_dirblklog
= cfg
->dirblocklog
- cfg
->blocklog
;
3247 sb_set_features(cfg
, sbp
);
3250 * log stripe unit is stored in bytes on disk and cannot be zero
3253 if (cfg
->sb_feat
.log_version
== 2) {
3255 sbp
->sb_logsunit
= XFS_FSB_TO_B(mp
, cfg
->lsunit
);
3257 sbp
->sb_logsunit
= 1;
3259 sbp
->sb_logsunit
= 0;
3265 struct mkfs_params
*cfg
,
3266 struct xfs_mount
*mp
,
3269 /* Minimum needed for libxfs_prealloc_blocks() */
3270 mp
->m_blkbb_log
= sbp
->sb_blocklog
- BBSHIFT
;
3271 mp
->m_sectbb_log
= sbp
->sb_sectlog
- BBSHIFT
;
3275 * Format everything from the generated config into the superblock that
3276 * will be used to initialise the on-disk superblock. This is the in-memory
3277 * copy, so no need to care about endian swapping here.
3280 finish_superblock_setup(
3281 struct mkfs_params
*cfg
,
3282 struct xfs_mount
*mp
,
3289 * Labels are null terminated unless the string fits exactly
3290 * in the label field, so assume sb_fname is zeroed and then
3291 * do a memcpy because the destination isn't a normal C string.
3293 label_len
= min(sizeof(sbp
->sb_fname
), strlen(cfg
->label
));
3294 memcpy(sbp
->sb_fname
, cfg
->label
, label_len
);
3297 sbp
->sb_dblocks
= cfg
->dblocks
;
3298 sbp
->sb_rblocks
= cfg
->rtblocks
;
3299 sbp
->sb_rextents
= cfg
->rtextents
;
3300 platform_uuid_copy(&sbp
->sb_uuid
, &cfg
->uuid
);
3301 /* Only in memory; libxfs expects this as if read from disk */
3302 platform_uuid_copy(&sbp
->sb_meta_uuid
, &cfg
->uuid
);
3303 sbp
->sb_logstart
= cfg
->logstart
;
3304 sbp
->sb_rootino
= sbp
->sb_rbmino
= sbp
->sb_rsumino
= NULLFSINO
;
3305 sbp
->sb_rextsize
= cfg
->rtextblocks
;
3306 sbp
->sb_agcount
= (xfs_agnumber_t
)cfg
->agcount
;
3307 sbp
->sb_rbmblocks
= cfg
->rtbmblocks
;
3308 sbp
->sb_logblocks
= (xfs_extlen_t
)cfg
->logblocks
;
3309 sbp
->sb_rextslog
= (uint8_t)(cfg
->rtextents
?
3310 libxfs_highbit32((unsigned int)cfg
->rtextents
) : 0);
3311 sbp
->sb_inprogress
= 1; /* mkfs is in progress */
3312 sbp
->sb_imax_pct
= cfg
->imaxpct
;
3315 sbp
->sb_fdblocks
= cfg
->dblocks
-
3316 cfg
->agcount
* libxfs_prealloc_blocks(mp
) -
3317 (cfg
->loginternal
? cfg
->logblocks
: 0);
3318 sbp
->sb_frextents
= 0; /* will do a free later */
3319 sbp
->sb_uquotino
= sbp
->sb_gquotino
= sbp
->sb_pquotino
= 0;
3321 sbp
->sb_unit
= cfg
->dsunit
;
3322 sbp
->sb_width
= cfg
->dswidth
;
3327 * Sanitise the data and log devices and prepare them so libxfs can mount the
3328 * device successfully. Also check we can access the rt device if configured.
3332 struct mkfs_params
*cfg
,
3333 struct libxfs_xinit
*xi
,
3334 struct xfs_mount
*mp
,
3338 struct xfs_buf
*buf
;
3339 int whack_blks
= BTOBB(WHACK_SIZE
);
3343 * If there's an old XFS filesystem on the device with enough intact
3344 * information that we can parse the superblock, there's enough
3345 * information on disk to confuse a future xfs_repair call. To avoid
3346 * this, whack all the old secondary superblocks that we can find.
3349 zero_old_xfs_structures(xi
, sbp
);
3352 * If the data device is a file, grow out the file to its final size if
3353 * needed so that the reads for the end of the device in the mount code
3357 xi
->dsize
* xi
->dbsize
< cfg
->dblocks
* cfg
->blocksize
) {
3358 if (ftruncate(xi
->dfd
, cfg
->dblocks
* cfg
->blocksize
) < 0) {
3360 _("%s: Growing the data section failed\n"),
3365 /* update size to be able to whack blocks correctly */
3366 xi
->dsize
= BTOBB(cfg
->dblocks
* cfg
->blocksize
);
3370 * Zero out the end to obliterate any old MD RAID (or other) metadata at
3371 * the end of the device. (MD sb is ~64k from the end, take out a wider
3374 buf
= libxfs_getbuf(mp
->m_ddev_targp
, (xi
->dsize
- whack_blks
),
3376 memset(buf
->b_addr
, 0, WHACK_SIZE
);
3377 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3378 libxfs_purgebuf(buf
);
3381 * Now zero out the beginning of the device, to obliterate any old
3382 * filesystem signatures out there. This should take care of
3383 * swap (somewhere around the page size), jfs (32k),
3384 * ext[2,3] and reiserfs (64k) - and hopefully all else.
3386 buf
= libxfs_getbuf(mp
->m_ddev_targp
, 0, whack_blks
);
3387 memset(buf
->b_addr
, 0, WHACK_SIZE
);
3388 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3389 libxfs_purgebuf(buf
);
3391 /* OK, now write the superblock... */
3392 buf
= libxfs_getbuf(mp
->m_ddev_targp
, XFS_SB_DADDR
, XFS_FSS_TO_BB(mp
, 1));
3393 buf
->b_ops
= &xfs_sb_buf_ops
;
3394 memset(buf
->b_addr
, 0, cfg
->sectorsize
);
3395 libxfs_sb_to_disk(buf
->b_addr
, sbp
);
3396 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3397 libxfs_purgebuf(buf
);
3399 /* ...and zero the log.... */
3400 lsunit
= sbp
->sb_logsunit
;
3402 lsunit
= sbp
->sb_logsectsize
;
3404 libxfs_log_clear(mp
->m_logdev_targp
, NULL
,
3405 XFS_FSB_TO_DADDR(mp
, cfg
->logstart
),
3406 (xfs_extlen_t
)XFS_FSB_TO_BB(mp
, cfg
->logblocks
),
3407 &sbp
->sb_uuid
, cfg
->sb_feat
.log_version
,
3408 lsunit
, XLOG_FMT
, XLOG_INIT_CYCLE
, false);
3410 /* finally, check we can write the last block in the realtime area */
3411 if (mp
->m_rtdev_targp
->dev
&& cfg
->rtblocks
> 0) {
3412 buf
= libxfs_getbuf(mp
->m_rtdev_targp
,
3413 XFS_FSB_TO_BB(mp
, cfg
->rtblocks
- 1LL),
3414 BTOBB(cfg
->blocksize
));
3415 memset(buf
->b_addr
, 0, cfg
->blocksize
);
3416 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3417 libxfs_purgebuf(buf
);
3423 * XXX: this code is mostly common with the kernel growfs code.
3424 * These initialisations should be pulled into libxfs to keep the
3425 * kernel/userspace header initialisation code the same.
3428 initialise_ag_headers(
3429 struct mkfs_params
*cfg
,
3430 struct xfs_mount
*mp
,
3432 xfs_agnumber_t agno
,
3433 int *worst_freelist
)
3435 struct xfs_perag
*pag
= libxfs_perag_get(mp
, agno
);
3436 struct xfs_agfl
*agfl
;
3437 struct xfs_agf
*agf
;
3438 struct xfs_agi
*agi
;
3439 struct xfs_buf
*buf
;
3440 struct xfs_btree_block
*block
;
3441 struct xfs_alloc_rec
*arec
;
3442 struct xfs_alloc_rec
*nrec
;
3444 uint64_t agsize
= cfg
->agsize
;
3445 xfs_agblock_t agblocks
;
3446 bool is_log_ag
= false;
3449 if (cfg
->loginternal
&& agno
== cfg
->logagno
)
3455 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3456 XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
),
3457 XFS_FSS_TO_BB(mp
, 1));
3458 buf
->b_ops
= &xfs_sb_buf_ops
;
3459 memset(buf
->b_addr
, 0, cfg
->sectorsize
);
3460 libxfs_sb_to_disk(buf
->b_addr
, sbp
);
3461 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3464 * AG header block: freespace
3466 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3467 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
3468 XFS_FSS_TO_BB(mp
, 1));
3469 buf
->b_ops
= &xfs_agf_buf_ops
;
3470 agf
= XFS_BUF_TO_AGF(buf
);
3471 memset(agf
, 0, cfg
->sectorsize
);
3472 if (agno
== cfg
->agcount
- 1)
3473 agsize
= cfg
->dblocks
- (xfs_rfsblock_t
)(agno
* agsize
);
3474 agf
->agf_magicnum
= cpu_to_be32(XFS_AGF_MAGIC
);
3475 agf
->agf_versionnum
= cpu_to_be32(XFS_AGF_VERSION
);
3476 agf
->agf_seqno
= cpu_to_be32(agno
);
3477 agf
->agf_length
= cpu_to_be32(agsize
);
3478 agf
->agf_roots
[XFS_BTNUM_BNOi
] = cpu_to_be32(XFS_BNO_BLOCK(mp
));
3479 agf
->agf_roots
[XFS_BTNUM_CNTi
] = cpu_to_be32(XFS_CNT_BLOCK(mp
));
3480 agf
->agf_levels
[XFS_BTNUM_BNOi
] = cpu_to_be32(1);
3481 agf
->agf_levels
[XFS_BTNUM_CNTi
] = cpu_to_be32(1);
3482 pag
->pagf_levels
[XFS_BTNUM_BNOi
] = 1;
3483 pag
->pagf_levels
[XFS_BTNUM_CNTi
] = 1;
3485 if (xfs_sb_version_hasrmapbt(sbp
)) {
3486 agf
->agf_roots
[XFS_BTNUM_RMAPi
] = cpu_to_be32(XFS_RMAP_BLOCK(mp
));
3487 agf
->agf_levels
[XFS_BTNUM_RMAPi
] = cpu_to_be32(1);
3488 agf
->agf_rmap_blocks
= cpu_to_be32(1);
3491 if (xfs_sb_version_hasreflink(sbp
)) {
3492 agf
->agf_refcount_root
= cpu_to_be32(libxfs_refc_block(mp
));
3493 agf
->agf_refcount_level
= cpu_to_be32(1);
3494 agf
->agf_refcount_blocks
= cpu_to_be32(1);
3497 agf
->agf_flfirst
= 0;
3498 agf
->agf_fllast
= cpu_to_be32(libxfs_agfl_size(mp
) - 1);
3499 agf
->agf_flcount
= 0;
3500 agblocks
= (xfs_agblock_t
)(agsize
- libxfs_prealloc_blocks(mp
));
3501 agf
->agf_freeblks
= cpu_to_be32(agblocks
);
3502 agf
->agf_longest
= cpu_to_be32(agblocks
);
3504 if (xfs_sb_version_hascrc(sbp
))
3505 platform_uuid_copy(&agf
->agf_uuid
, &sbp
->sb_uuid
);
3508 be32_add_cpu(&agf
->agf_freeblks
, -(int64_t)cfg
->logblocks
);
3509 agf
->agf_longest
= cpu_to_be32(agsize
-
3510 XFS_FSB_TO_AGBNO(mp
, cfg
->logstart
) - cfg
->logblocks
);
3512 if (libxfs_alloc_min_freelist(mp
, pag
) > *worst_freelist
)
3513 *worst_freelist
= libxfs_alloc_min_freelist(mp
, pag
);
3514 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3517 * AG freelist header block
3519 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3520 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
3521 XFS_FSS_TO_BB(mp
, 1));
3522 buf
->b_ops
= &xfs_agfl_buf_ops
;
3523 agfl
= XFS_BUF_TO_AGFL(buf
);
3524 /* setting to 0xff results in initialisation to NULLAGBLOCK */
3525 memset(agfl
, 0xff, cfg
->sectorsize
);
3526 if (xfs_sb_version_hascrc(sbp
)) {
3527 agfl
->agfl_magicnum
= cpu_to_be32(XFS_AGFL_MAGIC
);
3528 agfl
->agfl_seqno
= cpu_to_be32(agno
);
3529 platform_uuid_copy(&agfl
->agfl_uuid
, &sbp
->sb_uuid
);
3530 for (bucket
= 0; bucket
< libxfs_agfl_size(mp
); bucket
++)
3531 agfl
->agfl_bno
[bucket
] = cpu_to_be32(NULLAGBLOCK
);
3534 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3537 * AG header block: inodes
3539 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3540 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
3541 XFS_FSS_TO_BB(mp
, 1));
3542 agi
= XFS_BUF_TO_AGI(buf
);
3543 buf
->b_ops
= &xfs_agi_buf_ops
;
3544 memset(agi
, 0, cfg
->sectorsize
);
3545 agi
->agi_magicnum
= cpu_to_be32(XFS_AGI_MAGIC
);
3546 agi
->agi_versionnum
= cpu_to_be32(XFS_AGI_VERSION
);
3547 agi
->agi_seqno
= cpu_to_be32(agno
);
3548 agi
->agi_length
= cpu_to_be32(agsize
);
3550 agi
->agi_root
= cpu_to_be32(XFS_IBT_BLOCK(mp
));
3551 agi
->agi_level
= cpu_to_be32(1);
3552 if (xfs_sb_version_hasfinobt(sbp
)) {
3553 agi
->agi_free_root
= cpu_to_be32(XFS_FIBT_BLOCK(mp
));
3554 agi
->agi_free_level
= cpu_to_be32(1);
3556 agi
->agi_freecount
= 0;
3557 agi
->agi_newino
= cpu_to_be32(NULLAGINO
);
3558 agi
->agi_dirino
= cpu_to_be32(NULLAGINO
);
3559 if (xfs_sb_version_hascrc(sbp
))
3560 platform_uuid_copy(&agi
->agi_uuid
, &sbp
->sb_uuid
);
3561 for (c
= 0; c
< XFS_AGI_UNLINKED_BUCKETS
; c
++)
3562 agi
->agi_unlinked
[c
] = cpu_to_be32(NULLAGINO
);
3563 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3566 * BNO btree root block
3568 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3569 XFS_AGB_TO_DADDR(mp
, agno
, XFS_BNO_BLOCK(mp
)),
3570 BTOBB(cfg
->blocksize
));
3571 buf
->b_ops
= &xfs_bnobt_buf_ops
;
3572 block
= XFS_BUF_TO_BLOCK(buf
);
3573 memset(block
, 0, cfg
->blocksize
);
3574 libxfs_btree_init_block(mp
, buf
, XFS_BTNUM_BNO
, 0, 1, agno
);
3576 arec
= XFS_ALLOC_REC_ADDR(mp
, block
, 1);
3577 arec
->ar_startblock
= cpu_to_be32(libxfs_prealloc_blocks(mp
));
3579 xfs_agblock_t start
= XFS_FSB_TO_AGBNO(mp
, cfg
->logstart
);
3581 ASSERT(start
>= libxfs_prealloc_blocks(mp
));
3582 if (start
!= libxfs_prealloc_blocks(mp
)) {
3584 * Modify first record to pad stripe align of log
3586 arec
->ar_blockcount
= cpu_to_be32(start
-
3587 libxfs_prealloc_blocks(mp
));
3590 * Insert second record at start of internal log
3591 * which then gets trimmed.
3593 nrec
->ar_startblock
= cpu_to_be32(
3594 be32_to_cpu(arec
->ar_startblock
) +
3595 be32_to_cpu(arec
->ar_blockcount
));
3597 be16_add_cpu(&block
->bb_numrecs
, 1);
3600 * Change record start to after the internal log
3602 be32_add_cpu(&arec
->ar_startblock
, cfg
->logblocks
);
3605 * Calculate the record block count and check for the case where
3606 * the log might have consumed all available space in the AG. If
3607 * so, reset the record count to 0 to avoid exposure of an invalid
3608 * record start block.
3610 arec
->ar_blockcount
= cpu_to_be32(agsize
-
3611 be32_to_cpu(arec
->ar_startblock
));
3612 if (!arec
->ar_blockcount
)
3613 block
->bb_numrecs
= 0;
3615 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3618 * CNT btree root block
3620 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3621 XFS_AGB_TO_DADDR(mp
, agno
, XFS_CNT_BLOCK(mp
)),
3622 BTOBB(cfg
->blocksize
));
3623 buf
->b_ops
= &xfs_cntbt_buf_ops
;
3624 block
= XFS_BUF_TO_BLOCK(buf
);
3625 memset(block
, 0, cfg
->blocksize
);
3626 libxfs_btree_init_block(mp
, buf
, XFS_BTNUM_CNT
, 0, 1, agno
);
3628 arec
= XFS_ALLOC_REC_ADDR(mp
, block
, 1);
3629 arec
->ar_startblock
= cpu_to_be32(libxfs_prealloc_blocks(mp
));
3631 xfs_agblock_t start
= XFS_FSB_TO_AGBNO(mp
, cfg
->logstart
);
3633 ASSERT(start
>= libxfs_prealloc_blocks(mp
));
3634 if (start
!= libxfs_prealloc_blocks(mp
)) {
3635 arec
->ar_blockcount
= cpu_to_be32(start
-
3636 libxfs_prealloc_blocks(mp
));
3638 nrec
->ar_startblock
= cpu_to_be32(
3639 be32_to_cpu(arec
->ar_startblock
) +
3640 be32_to_cpu(arec
->ar_blockcount
));
3642 be16_add_cpu(&block
->bb_numrecs
, 1);
3644 be32_add_cpu(&arec
->ar_startblock
, cfg
->logblocks
);
3647 * Calculate the record block count and check for the case where
3648 * the log might have consumed all available space in the AG. If
3649 * so, reset the record count to 0 to avoid exposure of an invalid
3650 * record start block.
3652 arec
->ar_blockcount
= cpu_to_be32(agsize
-
3653 be32_to_cpu(arec
->ar_startblock
));
3654 if (!arec
->ar_blockcount
)
3655 block
->bb_numrecs
= 0;
3657 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3660 * refcount btree root block
3662 if (xfs_sb_version_hasreflink(sbp
)) {
3663 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3664 XFS_AGB_TO_DADDR(mp
, agno
, libxfs_refc_block(mp
)),
3665 BTOBB(cfg
->blocksize
));
3666 buf
->b_ops
= &xfs_refcountbt_buf_ops
;
3668 block
= XFS_BUF_TO_BLOCK(buf
);
3669 memset(block
, 0, cfg
->blocksize
);
3670 libxfs_btree_init_block(mp
, buf
, XFS_BTNUM_REFC
, 0, 0, agno
);
3671 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3675 * INO btree root block
3677 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3678 XFS_AGB_TO_DADDR(mp
, agno
, XFS_IBT_BLOCK(mp
)),
3679 BTOBB(cfg
->blocksize
));
3680 buf
->b_ops
= &xfs_inobt_buf_ops
;
3681 block
= XFS_BUF_TO_BLOCK(buf
);
3682 memset(block
, 0, cfg
->blocksize
);
3683 libxfs_btree_init_block(mp
, buf
, XFS_BTNUM_INO
, 0, 0, agno
);
3684 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3687 * Free INO btree root block
3689 if (xfs_sb_version_hasfinobt(sbp
)) {
3690 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3691 XFS_AGB_TO_DADDR(mp
, agno
, XFS_FIBT_BLOCK(mp
)),
3692 BTOBB(cfg
->blocksize
));
3693 buf
->b_ops
= &xfs_finobt_buf_ops
;
3694 block
= XFS_BUF_TO_BLOCK(buf
);
3695 memset(block
, 0, cfg
->blocksize
);
3696 libxfs_btree_init_block(mp
, buf
, XFS_BTNUM_FINO
, 0, 0, agno
);
3697 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3700 /* RMAP btree root block */
3701 if (xfs_sb_version_hasrmapbt(sbp
)) {
3702 struct xfs_rmap_rec
*rrec
;
3704 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
3705 XFS_AGB_TO_DADDR(mp
, agno
, XFS_RMAP_BLOCK(mp
)),
3706 BTOBB(cfg
->blocksize
));
3707 buf
->b_ops
= &xfs_rmapbt_buf_ops
;
3708 block
= XFS_BUF_TO_BLOCK(buf
);
3709 memset(block
, 0, cfg
->blocksize
);
3711 libxfs_btree_init_block(mp
, buf
, XFS_BTNUM_RMAP
, 0, 0, agno
);
3714 * mark the AG header regions as static metadata
3715 * The BNO btree block is the first block after the
3716 * headers, so it's location defines the size of region
3717 * the static metadata consumes.
3719 rrec
= XFS_RMAP_REC_ADDR(block
, 1);
3720 rrec
->rm_startblock
= 0;
3721 rrec
->rm_blockcount
= cpu_to_be32(XFS_BNO_BLOCK(mp
));
3722 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_FS
);
3723 rrec
->rm_offset
= 0;
3724 be16_add_cpu(&block
->bb_numrecs
, 1);
3726 /* account freespace btree root blocks */
3727 rrec
= XFS_RMAP_REC_ADDR(block
, 2);
3728 rrec
->rm_startblock
= cpu_to_be32(XFS_BNO_BLOCK(mp
));
3729 rrec
->rm_blockcount
= cpu_to_be32(2);
3730 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_AG
);
3731 rrec
->rm_offset
= 0;
3732 be16_add_cpu(&block
->bb_numrecs
, 1);
3734 /* account inode btree root blocks */
3735 rrec
= XFS_RMAP_REC_ADDR(block
, 3);
3736 rrec
->rm_startblock
= cpu_to_be32(XFS_IBT_BLOCK(mp
));
3737 rrec
->rm_blockcount
= cpu_to_be32(XFS_RMAP_BLOCK(mp
) -
3739 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_INOBT
);
3740 rrec
->rm_offset
= 0;
3741 be16_add_cpu(&block
->bb_numrecs
, 1);
3743 /* account for rmap btree root */
3744 rrec
= XFS_RMAP_REC_ADDR(block
, 4);
3745 rrec
->rm_startblock
= cpu_to_be32(XFS_RMAP_BLOCK(mp
));
3746 rrec
->rm_blockcount
= cpu_to_be32(1);
3747 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_AG
);
3748 rrec
->rm_offset
= 0;
3749 be16_add_cpu(&block
->bb_numrecs
, 1);
3751 /* account for refcount btree root */
3752 if (xfs_sb_version_hasreflink(sbp
)) {
3753 rrec
= XFS_RMAP_REC_ADDR(block
, 5);
3754 rrec
->rm_startblock
= cpu_to_be32(libxfs_refc_block(mp
));
3755 rrec
->rm_blockcount
= cpu_to_be32(1);
3756 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_REFC
);
3757 rrec
->rm_offset
= 0;
3758 be16_add_cpu(&block
->bb_numrecs
, 1);
3761 /* account for the log space */
3763 rrec
= XFS_RMAP_REC_ADDR(block
,
3764 be16_to_cpu(block
->bb_numrecs
) + 1);
3765 rrec
->rm_startblock
= cpu_to_be32(
3766 XFS_FSB_TO_AGBNO(mp
, cfg
->logstart
));
3767 rrec
->rm_blockcount
= cpu_to_be32(cfg
->logblocks
);
3768 rrec
->rm_owner
= cpu_to_be64(XFS_RMAP_OWN_LOG
);
3769 rrec
->rm_offset
= 0;
3770 be16_add_cpu(&block
->bb_numrecs
, 1);
3773 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3776 libxfs_perag_put(pag
);
3780 initialise_ag_freespace(
3781 struct xfs_mount
*mp
,
3782 xfs_agnumber_t agno
,
3785 struct xfs_alloc_arg args
;
3786 struct xfs_trans
*tp
;
3789 c
= -libxfs_trans_alloc_rollable(mp
, worst_freelist
, &tp
);
3793 memset(&args
, 0, sizeof(args
));
3798 args
.pag
= libxfs_perag_get(mp
, agno
);
3800 libxfs_alloc_fix_freelist(&args
, 0);
3801 libxfs_perag_put(args
.pag
);
3802 c
= -libxfs_trans_commit(tp
);
3808 * rewrite several secondary superblocks with the root inode number filled out.
3809 * This can help repair recovery from a trashed primary superblock without
3810 * losing the root inode.
3813 rewrite_secondary_superblocks(
3814 struct xfs_mount
*mp
)
3816 struct xfs_buf
*buf
;
3818 /* rewrite the last superblock */
3819 buf
= libxfs_readbuf(mp
->m_dev
,
3820 XFS_AGB_TO_DADDR(mp
, mp
->m_sb
.sb_agcount
- 1,
3822 XFS_FSS_TO_BB(mp
, 1),
3823 LIBXFS_EXIT_ON_FAILURE
, &xfs_sb_buf_ops
);
3824 XFS_BUF_TO_SBP(buf
)->sb_rootino
= cpu_to_be64(mp
->m_sb
.sb_rootino
);
3825 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3827 /* and one in the middle for luck if there's enough AGs for that */
3828 if (mp
->m_sb
.sb_agcount
<= 2)
3831 buf
= libxfs_readbuf(mp
->m_dev
,
3832 XFS_AGB_TO_DADDR(mp
, (mp
->m_sb
.sb_agcount
- 1) / 2,
3834 XFS_FSS_TO_BB(mp
, 1),
3835 LIBXFS_EXIT_ON_FAILURE
, &xfs_sb_buf_ops
);
3836 XFS_BUF_TO_SBP(buf
)->sb_rootino
= cpu_to_be64(mp
->m_sb
.sb_rootino
);
3837 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
3845 xfs_agnumber_t agno
;
3849 char *logfile
= NULL
;
3850 char *rtfile
= NULL
;
3853 int force_overwrite
= 0;
3855 char *protofile
= NULL
;
3856 char *protostring
= NULL
;
3857 int worst_freelist
= 0;
3859 struct libxfs_xinit xi
= {
3860 .isdirect
= LIBXFS_DIRECT
,
3861 .isreadonly
= LIBXFS_EXCLUSIVELY
,
3863 struct xfs_mount mbuf
= {};
3864 struct xfs_mount
*mp
= &mbuf
;
3865 struct xfs_sb
*sbp
= &mp
->m_sb
;
3866 struct fs_topology ft
= {};
3867 struct cli_params cli
= {
3871 struct mkfs_params cfg
= {};
3873 /* build time defaults */
3874 struct mkfs_default_params dft
= {
3875 .source
= _("package build definitions"),
3876 .sectorsize
= XFS_MIN_SECTORSIZE
,
3877 .blocksize
= 1 << XFS_DFL_BLOCKSIZE_LOG
,
3882 .inode_align
= true,
3884 .lazy_sb_counters
= true,
3885 .projid32bit
= true,
3886 .crcs_enabled
= true,
3892 .parent_pointers
= false,
3898 platform_uuid_generate(&cli
.uuid
);
3899 progname
= basename(argv
[0]);
3900 setlocale(LC_ALL
, "");
3901 bindtextdomain(PACKAGE
, LOCALEDIR
);
3902 textdomain(PACKAGE
);
3905 * TODO: Sourcing defaults from a config file
3907 * Before anything else, see if there's a config file with different
3908 * defaults. If a file exists in <package location>, read in the new
3909 * default values and overwrite them in the &dft structure. This way the
3910 * new defaults will apply before we parse the CLI, and the CLI will
3911 * still be able to override them. When more than one source is
3912 * implemented, emit a message to indicate where the defaults being
3915 * printf(_("Default configuration sourced from %s\n"), dft.source);
3918 /* copy new defaults into CLI parsing structure */
3919 memcpy(&cli
.sb_feat
, &dft
.sb_feat
, sizeof(cli
.sb_feat
));
3920 memcpy(&cli
.fsx
, &dft
.fsx
, sizeof(cli
.fsx
));
3922 while ((c
= getopt(argc
, argv
, "b:d:i:l:L:m:n:KNp:qr:s:CfV")) != EOF
) {
3926 force_overwrite
= 1;
3936 parse_subopts(c
, optarg
, &cli
);
3939 if (strlen(optarg
) > sizeof(sbp
->sb_fname
))
3940 illegal(optarg
, "L");
3951 respec('p', NULL
, 0);
3958 printf(_("%s version %s\n"), progname
, VERSION
);
3961 unknown(optopt
, "");
3964 if (argc
- optind
> 1) {
3965 fprintf(stderr
, _("extra arguments\n"));
3967 } else if (argc
- optind
== 1) {
3968 dfile
= xi
.volname
= getstr(argv
[optind
], &dopts
, D_NAME
);
3972 protostring
= setup_proto(protofile
);
3975 * Extract as much of the valid config as we can from the CLI input
3976 * before opening the libxfs devices.
3978 validate_blocksize(&cfg
, &cli
, &dft
);
3979 validate_sectorsize(&cfg
, &cli
, &dft
, &ft
, dfile
, dry_run
,
3983 * XXX: we still need to set block size and sector size global variables
3984 * so that getnum/cvtnum works correctly
3986 blocksize
= cfg
.blocksize
;
3987 sectorsize
= cfg
.sectorsize
;
3989 validate_log_sectorsize(&cfg
, &cli
, &dft
);
3990 validate_sb_features(&cfg
, &cli
);
3993 * we've now completed basic validation of the features, sector and
3994 * block sizes, so from this point onwards we use the values found in
3995 * the cfg structure for them, not the command line structure.
3997 validate_dirblocksize(&cfg
, &cli
);
3998 validate_inodesize(&cfg
, &cli
);
4001 * if the device size was specified convert it to a block count
4002 * now we have a valid block size. These will be set to zero if
4003 * nothing was specified, indicating we should use the full device.
4005 cfg
.dblocks
= calc_dev_size(cli
.dsize
, &cfg
, &dopts
, D_SIZE
, "data");
4006 cfg
.logblocks
= calc_dev_size(cli
.logsize
, &cfg
, &lopts
, L_SIZE
, "log");
4007 cfg
.rtblocks
= calc_dev_size(cli
.rtsize
, &cfg
, &ropts
, R_SIZE
, "rt");
4009 validate_rtextsize(&cfg
, &cli
, &ft
);
4012 * Open and validate the device configurations
4014 open_devices(&cfg
, &xi
);
4015 validate_overwrite(dfile
, force_overwrite
);
4016 validate_datadev(&cfg
, &cli
);
4017 validate_logdev(&cfg
, &cli
, &logfile
);
4018 validate_rtdev(&cfg
, &cli
, &rtfile
);
4019 calc_stripe_factors(&cfg
, &cli
, &ft
);
4022 * At this point when know exactly what size all the devices are,
4023 * so we can start validating and calculating layout options that are
4024 * dependent on device sizes. Once calculated, make sure everything
4025 * aligns to device geometry correctly.
4027 calculate_initial_ag_geometry(&cfg
, &cli
);
4028 align_ag_geometry(&cfg
);
4030 calculate_imaxpct(&cfg
, &cli
);
4033 * Set up the basic superblock parameters now so that we can use
4034 * the geometry information we've already validated in libxfs
4035 * provided functions to determine on-disk format information.
4037 start_superblock_setup(&cfg
, mp
, sbp
);
4038 initialise_mount(&cfg
, mp
, sbp
);
4041 * With the mount set up, we can finally calculate the log size
4042 * constraints and do default size calculations and final validation
4044 calculate_log_size(&cfg
, &cli
, mp
);
4046 finish_superblock_setup(&cfg
, mp
, sbp
);
4048 /* Validate the extent size hints now that @mp is fully set up. */
4049 validate_extsize_hint(mp
, &cli
);
4050 validate_cowextsize_hint(mp
, &cli
);
4052 /* Print the intended geometry of the fs. */
4053 if (!quiet
|| dry_run
) {
4054 struct xfs_fsop_geom geo
;
4056 libxfs_fs_geometry(sbp
, &geo
, XFS_FS_GEOM_MAX_STRUCT_VER
);
4057 xfs_report_geom(&geo
, dfile
, logfile
, rtfile
);
4063 * All values have been validated, discard the old device layout.
4065 if (discard
&& !dry_run
)
4066 discard_devices(&xi
);
4069 * we need the libxfs buffer cache from here on in.
4071 libxfs_buftarg_init(mp
, xi
.ddev
, xi
.logdev
, xi
.rtdev
);
4074 * Before we mount the filesystem we need to make sure the devices have
4075 * enough of the filesystem structure on them that allows libxfs to
4078 prepare_devices(&cfg
, &xi
, mp
, sbp
, force_overwrite
);
4079 mp
= libxfs_mount(mp
, sbp
, xi
.ddev
, xi
.logdev
, xi
.rtdev
, 0);
4081 fprintf(stderr
, _("%s: filesystem failed to initialize\n"),
4087 * Initialise all the static on disk metadata.
4089 for (agno
= 0; agno
< cfg
.agcount
; agno
++)
4090 initialise_ag_headers(&cfg
, mp
, sbp
, agno
, &worst_freelist
);
4093 * Initialise the freespace freelists (i.e. AGFLs) in each AG.
4095 for (agno
= 0; agno
< cfg
.agcount
; agno
++)
4096 initialise_ag_freespace(mp
, agno
, worst_freelist
);
4099 * Allocate the root inode and anything else in the proto file.
4101 parse_proto(mp
, &cli
.fsx
, &protostring
);
4104 * Protect ourselves against possible stupidity
4106 if (XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
) != 0) {
4108 _("%s: root inode created in AG %u, not AG 0\n"),
4109 progname
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
));
4114 * Re-write multiple secondary superblocks with rootinode field set
4116 if (mp
->m_sb
.sb_agcount
> 1)
4117 rewrite_secondary_superblocks(mp
);
4120 * Dump all inodes and buffers before marking us all done.
4121 * Need to drop references to inodes we still hold, first.
4123 libxfs_rtmount_destroy(mp
);
4124 libxfs_bcache_purge();
4127 * Mark the filesystem ok.
4129 buf
= libxfs_getsb(mp
);
4130 if (!buf
|| buf
->b_error
)
4132 (XFS_BUF_TO_SBP(buf
))->sb_inprogress
= 0;
4133 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
4137 libxfs_device_close(xi
.rtdev
);
4138 if (xi
.logdev
&& xi
.logdev
!= xi
.ddev
)
4139 libxfs_device_close(xi
.logdev
);
4140 libxfs_device_close(xi
.ddev
);