]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - mkfs/xfs_mkfs.c
mkfs: always explain why numeric inputs are invalid
[thirdparty/xfsprogs-dev.git] / mkfs / xfs_mkfs.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
46eca962 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
2bd0ea18 7 * published by the Free Software Foundation.
46eca962 8 *
da23017d
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
46eca962 13 *
da23017d
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2bd0ea18 17 */
24f4f998 18#include "libfrog.h"
6b803e5a 19#include "libxfs.h"
e4da9941 20#include <ctype.h>
4a32b9e9 21#include "xfs_multidisk.h"
82c3a179 22#include "libxcmd.h"
e4da9941 23
2bd0ea18 24
4eee66c5
DC
25
26#define TERABYTES(count, blog) ((uint64_t)(count) << (40 - (blog)))
27#define GIGABYTES(count, blog) ((uint64_t)(count) << (30 - (blog)))
28#define MEGABYTES(count, blog) ((uint64_t)(count) << (20 - (blog)))
29
30/*
31 * Use this macro before we have superblock and mount structure to
32 * convert from basic blocks to filesystem blocks.
33 */
34#define DTOBT(d, bl) ((xfs_rfsblock_t)((d) >> ((bl) - BBSHIFT)))
35
36/*
37 * amount (in bytes) we zero at the beginning and end of the device to
38 * remove traces of other filesystems, raid superblocks, etc.
39 */
40#define WHACK_SIZE (128 * 1024)
41
627e74fd 42/*
b449c79e
DC
43 * XXX: The configured block and sector sizes are defined as global variables so
44 * that they don't need to be passed to getnum/cvtnum().
627e74fd
DC
45 */
46unsigned int blocksize;
47unsigned int sectorsize;
48
64e924dd
DC
49/*
50 * Enums for each CLI parameter type are declared first so we can calculate the
51 * maximum array size needed to hold them automatically.
52 */
53enum {
2cf637cf 54 B_SIZE = 0,
64e924dd
DC
55 B_MAX_OPTS,
56};
57
58enum {
59 D_AGCOUNT = 0,
60 D_FILE,
61 D_NAME,
62 D_SIZE,
63 D_SUNIT,
64 D_SWIDTH,
65 D_AGSIZE,
66 D_SU,
67 D_SW,
64e924dd
DC
68 D_SECTSIZE,
69 D_NOALIGN,
70 D_RTINHERIT,
71 D_PROJINHERIT,
72 D_EXTSZINHERIT,
73 D_COWEXTSIZE,
74 D_MAX_OPTS,
75};
76
77enum {
78 I_ALIGN = 0,
64e924dd
DC
79 I_MAXPCT,
80 I_PERBLOCK,
81 I_SIZE,
82 I_ATTR,
83 I_PROJID32BIT,
84 I_SPINODES,
85 I_MAX_OPTS,
86};
87
88enum {
89 L_AGNUM = 0,
90 L_INTERNAL,
91 L_SIZE,
92 L_VERSION,
93 L_SUNIT,
94 L_SU,
95 L_DEV,
64e924dd
DC
96 L_SECTSIZE,
97 L_FILE,
98 L_NAME,
99 L_LAZYSBCNTR,
100 L_MAX_OPTS,
101};
102
103enum {
2cf637cf 104 N_SIZE = 0,
64e924dd
DC
105 N_VERSION,
106 N_FTYPE,
107 N_MAX_OPTS,
108};
109
110enum {
111 R_EXTSIZE = 0,
112 R_SIZE,
113 R_DEV,
114 R_FILE,
115 R_NAME,
116 R_NOALIGN,
117 R_MAX_OPTS,
118};
119
120enum {
2cf637cf 121 S_SIZE = 0,
64e924dd
DC
122 S_SECTSIZE,
123 S_MAX_OPTS,
124};
125
126enum {
127 M_CRC = 0,
128 M_FINOBT,
129 M_UUID,
130 M_RMAPBT,
131 M_REFLINK,
132 M_MAX_OPTS,
133};
134
135/* Just define the max options array size manually right now */
136#define MAX_SUBOPTS D_MAX_OPTS
137
56e4d368 138#define SUBOPT_NEEDS_VAL (-1LL)
3ec1956a
DC
139#define MAX_CONFLICTS 8
140#define LAST_CONFLICT (-1)
141
2bd0ea18 142/*
a9dad670
DC
143 * Table for parsing mkfs parameters.
144 *
145 * Description of the structure members follows:
146 *
147 * name MANDATORY
148 * Name is a single char, e.g., for '-d file', name is 'd'.
149 *
150 * subopts MANDATORY
151 * Subopts is a list of strings naming suboptions. In the example above,
152 * it would contain "file". The last entry of this list has to be NULL.
153 *
154 * subopt_params MANDATORY
155 * This is a list of structs tied with subopts. For each entry in subopts,
156 * a corresponding entry has to be defined:
157 *
158 * subopt_params struct:
159 * index MANDATORY
160 * This number, starting from zero, denotes which item in subopt_params
161 * it is. The index has to be the same as is the order in subopts list,
162 * so we can access the right item both in subopt_param and subopts.
163 *
9090e187
DC
164 * seen INTERNAL
165 * Do not set this flag when definning a subopt. It is used to remeber that
166 * this subopt was already seen, for example for conflicts detection.
167 *
27ae3a59
DC
168 * str_seen INTERNAL
169 * Do not set. It is used internally for respecification, when some options
170 * has to be parsed twice - at first as a string, then later as a number.
171 *
627e74fd
DC
172 * convert OPTIONAL
173 * A flag signalling whether the user-given value can use suffixes.
174 * If you want to allow the use of user-friendly values like 13k, 42G,
175 * set it to true.
176 *
177 * is_power_2 OPTIONAL
178 * An optional flag for subopts where the given value has to be a power
179 * of two.
180 *
3ec1956a
DC
181 * conflicts MANDATORY
182 * If your subopt is in a conflict with some other option, specify it.
183 * Accepts the .index values of the conflicting subopts and the last
184 * member of this list has to be LAST_CONFLICT.
185 *
a9dad670
DC
186 * minval, maxval OPTIONAL
187 * These options are used for automatic range check and they have to be
188 * always used together in pair. If you don't want to limit the max value,
189 * use something like UINT_MAX. If no value is given, then you must either
190 * supply your own validation, or refuse any value in the 'case
191 * X_SOMETHING' block. If you forget to define the min and max value, but
192 * call a standard function for validating user's value, it will cause an
193 * error message notifying you about this issue.
194 *
195 * (Said in another way, you can't have minval and maxval both equal
196 * to zero. But if one value is different: minval=0 and maxval=1,
197 * then it is OK.)
56e4d368
DC
198 *
199 * defaultval MANDATORY
200 * The value used if user specifies the subopt, but no value.
201 * If the subopt accepts some values (-d file=[1|0]), then this
202 * sets what is used with simple specifying the subopt (-d file).
203 * A special SUBOPT_NEEDS_VAL can be used to require a user-given
204 * value in any case.
2bd0ea18 205 */
a9dad670
DC
206struct opt_params {
207 const char name;
208 const char *subopts[MAX_SUBOPTS];
9090e187 209
a9dad670
DC
210 struct subopt_param {
211 int index;
9090e187 212 bool seen;
27ae3a59 213 bool str_seen;
627e74fd
DC
214 bool convert;
215 bool is_power_2;
cedf1c43
DC
216 struct _conflict {
217 struct opt_params *opts;
218 int subopt;
219 } conflicts[MAX_CONFLICTS];
a9dad670
DC
220 long long minval;
221 long long maxval;
56e4d368 222 long long defaultval;
a9dad670
DC
223 } subopt_params[MAX_SUBOPTS];
224};
225
6c75555e
DC
226/*
227 * The two dimensional conflict array requires some initialisations to know
228 * about tables that haven't yet been defined. Work around this ordering
229 * issue with extern definitions here.
230 */
231extern struct opt_params sopts;
232
a9dad670
DC
233struct opt_params bopts = {
234 .name = 'b',
235 .subopts = {
64e924dd 236 [B_SIZE] = "size",
a9dad670
DC
237 },
238 .subopt_params = {
a9dad670 239 { .index = B_SIZE,
627e74fd
DC
240 .convert = true,
241 .is_power_2 = true,
2cf637cf 242 .conflicts = { { NULL, LAST_CONFLICT } },
a9dad670
DC
243 .minval = XFS_MIN_BLOCKSIZE,
244 .maxval = XFS_MAX_BLOCKSIZE,
56e4d368 245 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
246 },
247 },
2bd0ea18
NS
248};
249
a9dad670
DC
250struct opt_params dopts = {
251 .name = 'd',
252 .subopts = {
64e924dd
DC
253 [D_AGCOUNT] = "agcount",
254 [D_FILE] = "file",
255 [D_NAME] = "name",
256 [D_SIZE] = "size",
257 [D_SUNIT] = "sunit",
258 [D_SWIDTH] = "swidth",
259 [D_AGSIZE] = "agsize",
260 [D_SU] = "su",
261 [D_SW] = "sw",
64e924dd
DC
262 [D_SECTSIZE] = "sectsize",
263 [D_NOALIGN] = "noalign",
264 [D_RTINHERIT] = "rtinherit",
265 [D_PROJINHERIT] = "projinherit",
266 [D_EXTSZINHERIT] = "extszinherit",
267 [D_COWEXTSIZE] = "cowextsize",
a9dad670
DC
268 },
269 .subopt_params = {
270 { .index = D_AGCOUNT,
cedf1c43
DC
271 .conflicts = { { &dopts, D_AGSIZE },
272 { NULL, LAST_CONFLICT } },
1974d3f1
DC
273 .minval = 1,
274 .maxval = XFS_MAX_AGNUMBER,
56e4d368 275 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
276 },
277 { .index = D_FILE,
cedf1c43 278 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
279 .minval = 0,
280 .maxval = 1,
281 .defaultval = 1,
a9dad670
DC
282 },
283 { .index = D_NAME,
cedf1c43 284 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368 285 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
286 },
287 { .index = D_SIZE,
cedf1c43 288 .conflicts = { { NULL, LAST_CONFLICT } },
627e74fd
DC
289 .convert = true,
290 .minval = XFS_AG_MIN_BYTES,
291 .maxval = LLONG_MAX,
56e4d368 292 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
293 },
294 { .index = D_SUNIT,
cedf1c43
DC
295 .conflicts = { { &dopts, D_NOALIGN },
296 { &dopts, D_SU },
297 { &dopts, D_SW },
298 { NULL, LAST_CONFLICT } },
1974d3f1
DC
299 .minval = 0,
300 .maxval = UINT_MAX,
56e4d368 301 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
302 },
303 { .index = D_SWIDTH,
cedf1c43
DC
304 .conflicts = { { &dopts, D_NOALIGN },
305 { &dopts, D_SU },
306 { &dopts, D_SW },
307 { NULL, LAST_CONFLICT } },
1974d3f1
DC
308 .minval = 0,
309 .maxval = UINT_MAX,
56e4d368 310 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
311 },
312 { .index = D_AGSIZE,
cedf1c43
DC
313 .conflicts = { { &dopts, D_AGCOUNT },
314 { NULL, LAST_CONFLICT } },
627e74fd 315 .convert = true,
1974d3f1
DC
316 .minval = XFS_AG_MIN_BYTES,
317 .maxval = XFS_AG_MAX_BYTES,
56e4d368 318 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
319 },
320 { .index = D_SU,
cedf1c43
DC
321 .conflicts = { { &dopts, D_NOALIGN },
322 { &dopts, D_SUNIT },
323 { &dopts, D_SWIDTH },
324 { NULL, LAST_CONFLICT } },
627e74fd
DC
325 .convert = true,
326 .minval = 0,
327 .maxval = UINT_MAX,
56e4d368 328 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
329 },
330 { .index = D_SW,
cedf1c43
DC
331 .conflicts = { { &dopts, D_NOALIGN },
332 { &dopts, D_SUNIT },
333 { &dopts, D_SWIDTH },
334 { NULL, LAST_CONFLICT } },
1974d3f1
DC
335 .minval = 0,
336 .maxval = UINT_MAX,
56e4d368 337 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670 338 },
a9dad670 339 { .index = D_SECTSIZE,
2cf637cf 340 .conflicts = { { &sopts, S_SIZE },
6c75555e 341 { &sopts, S_SECTSIZE },
cedf1c43 342 { NULL, LAST_CONFLICT } },
627e74fd
DC
343 .convert = true,
344 .is_power_2 = true,
a9dad670
DC
345 .minval = XFS_MIN_SECTORSIZE,
346 .maxval = XFS_MAX_SECTORSIZE,
56e4d368 347 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
348 },
349 { .index = D_NOALIGN,
cedf1c43
DC
350 .conflicts = { { &dopts, D_SU },
351 { &dopts, D_SW },
352 { &dopts, D_SUNIT },
353 { &dopts, D_SWIDTH },
354 { NULL, LAST_CONFLICT } },
56e4d368
DC
355 .minval = 0,
356 .maxval = 1,
357 .defaultval = 1,
a9dad670
DC
358 },
359 { .index = D_RTINHERIT,
cedf1c43 360 .conflicts = { { NULL, LAST_CONFLICT } },
1974d3f1
DC
361 .minval = 1,
362 .maxval = 1,
363 .defaultval = 1,
a9dad670
DC
364 },
365 { .index = D_PROJINHERIT,
cedf1c43 366 .conflicts = { { NULL, LAST_CONFLICT } },
1974d3f1
DC
367 .minval = 0,
368 .maxval = UINT_MAX,
56e4d368 369 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
370 },
371 { .index = D_EXTSZINHERIT,
cedf1c43 372 .conflicts = { { NULL, LAST_CONFLICT } },
1974d3f1
DC
373 .minval = 0,
374 .maxval = UINT_MAX,
56e4d368 375 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670 376 },
06b80354 377 { .index = D_COWEXTSIZE,
cedf1c43 378 .conflicts = { { NULL, LAST_CONFLICT } },
06b80354
DW
379 .minval = 0,
380 .maxval = UINT_MAX,
381 .defaultval = SUBOPT_NEEDS_VAL,
382 },
a9dad670 383 },
2bd0ea18
NS
384};
385
a9dad670
DC
386
387struct opt_params iopts = {
388 .name = 'i',
389 .subopts = {
64e924dd 390 [I_ALIGN] = "align",
64e924dd
DC
391 [I_MAXPCT] = "maxpct",
392 [I_PERBLOCK] = "perblock",
393 [I_SIZE] = "size",
394 [I_ATTR] = "attr",
395 [I_PROJID32BIT] = "projid32bit",
396 [I_SPINODES] = "sparse",
a9dad670
DC
397 },
398 .subopt_params = {
399 { .index = I_ALIGN,
cedf1c43 400 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
401 .minval = 0,
402 .maxval = 1,
403 .defaultval = 1,
a9dad670 404 },
a9dad670 405 { .index = I_MAXPCT,
cedf1c43 406 .conflicts = { { NULL, LAST_CONFLICT } },
1974d3f1
DC
407 .minval = 0,
408 .maxval = 100,
56e4d368 409 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
410 },
411 { .index = I_PERBLOCK,
2cf637cf 412 .conflicts = { { &iopts, I_SIZE },
cedf1c43 413 { NULL, LAST_CONFLICT } },
627e74fd 414 .is_power_2 = true,
1974d3f1
DC
415 .minval = XFS_MIN_INODE_PERBLOCK,
416 .maxval = XFS_MAX_BLOCKSIZE / XFS_DINODE_MIN_SIZE,
56e4d368 417 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
418 },
419 { .index = I_SIZE,
cedf1c43 420 .conflicts = { { &iopts, I_PERBLOCK },
cedf1c43 421 { NULL, LAST_CONFLICT } },
627e74fd 422 .is_power_2 = true,
1974d3f1
DC
423 .minval = XFS_DINODE_MIN_SIZE,
424 .maxval = XFS_DINODE_MAX_SIZE,
56e4d368 425 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
426 },
427 { .index = I_ATTR,
cedf1c43 428 .conflicts = { { NULL, LAST_CONFLICT } },
1974d3f1
DC
429 .minval = 0,
430 .maxval = 2,
56e4d368 431 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
432 },
433 { .index = I_PROJID32BIT,
cedf1c43 434 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
435 .minval = 0,
436 .maxval = 1,
437 .defaultval = 1,
a9dad670
DC
438 },
439 { .index = I_SPINODES,
cedf1c43 440 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
441 .minval = 0,
442 .maxval = 1,
443 .defaultval = 1,
a9dad670
DC
444 },
445 },
2bd0ea18
NS
446};
447
a9dad670
DC
448struct opt_params lopts = {
449 .name = 'l',
450 .subopts = {
64e924dd
DC
451 [L_AGNUM] = "agnum",
452 [L_INTERNAL] = "internal",
453 [L_SIZE] = "size",
454 [L_VERSION] = "version",
455 [L_SUNIT] = "sunit",
456 [L_SU] = "su",
457 [L_DEV] = "logdev",
64e924dd
DC
458 [L_SECTSIZE] = "sectsize",
459 [L_FILE] = "file",
460 [L_NAME] = "name",
461 [L_LAZYSBCNTR] = "lazy-count",
a9dad670
DC
462 },
463 .subopt_params = {
464 { .index = L_AGNUM,
cedf1c43
DC
465 .conflicts = { { &lopts, L_DEV },
466 { NULL, LAST_CONFLICT } },
1974d3f1
DC
467 .minval = 0,
468 .maxval = UINT_MAX,
56e4d368 469 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
470 },
471 { .index = L_INTERNAL,
cedf1c43
DC
472 .conflicts = { { &lopts, L_FILE },
473 { &lopts, L_DEV },
cedf1c43
DC
474 { &lopts, L_SECTSIZE },
475 { NULL, LAST_CONFLICT } },
56e4d368
DC
476 .minval = 0,
477 .maxval = 1,
478 .defaultval = 1,
a9dad670
DC
479 },
480 { .index = L_SIZE,
cedf1c43 481 .conflicts = { { NULL, LAST_CONFLICT } },
627e74fd
DC
482 .convert = true,
483 .minval = 2 * 1024 * 1024LL, /* XXX: XFS_MIN_LOG_BYTES */
484 .maxval = XFS_MAX_LOG_BYTES,
56e4d368 485 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
486 },
487 { .index = L_VERSION,
cedf1c43 488 .conflicts = { { NULL, LAST_CONFLICT } },
1974d3f1
DC
489 .minval = 1,
490 .maxval = 2,
56e4d368 491 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
492 },
493 { .index = L_SUNIT,
cedf1c43
DC
494 .conflicts = { { &lopts, L_SU },
495 { NULL, LAST_CONFLICT } },
2942ff49 496 .minval = 1,
1974d3f1 497 .maxval = BTOBB(XLOG_MAX_RECORD_BSIZE),
56e4d368 498 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
499 },
500 { .index = L_SU,
cedf1c43
DC
501 .conflicts = { { &lopts, L_SUNIT },
502 { NULL, LAST_CONFLICT } },
627e74fd 503 .convert = true,
2942ff49 504 .minval = BBTOB(1),
627e74fd 505 .maxval = XLOG_MAX_RECORD_BSIZE,
56e4d368 506 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
507 },
508 { .index = L_DEV,
cedf1c43 509 .conflicts = { { &lopts, L_AGNUM },
9502da21 510 { &lopts, L_NAME },
cedf1c43
DC
511 { &lopts, L_INTERNAL },
512 { NULL, LAST_CONFLICT } },
56e4d368 513 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670 514 },
a9dad670 515 { .index = L_SECTSIZE,
2cf637cf 516 .conflicts = { { &lopts, L_INTERNAL },
cedf1c43 517 { NULL, LAST_CONFLICT } },
627e74fd
DC
518 .convert = true,
519 .is_power_2 = true,
a9dad670
DC
520 .minval = XFS_MIN_SECTORSIZE,
521 .maxval = XFS_MAX_SECTORSIZE,
56e4d368 522 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
523 },
524 { .index = L_FILE,
cedf1c43
DC
525 .conflicts = { { &lopts, L_INTERNAL },
526 { NULL, LAST_CONFLICT } },
56e4d368
DC
527 .minval = 0,
528 .maxval = 1,
529 .defaultval = 1,
a9dad670
DC
530 },
531 { .index = L_NAME,
cedf1c43 532 .conflicts = { { &lopts, L_AGNUM },
9502da21 533 { &lopts, L_DEV },
cedf1c43
DC
534 { &lopts, L_INTERNAL },
535 { NULL, LAST_CONFLICT } },
56e4d368 536 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
537 },
538 { .index = L_LAZYSBCNTR,
cedf1c43 539 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
540 .minval = 0,
541 .maxval = 1,
542 .defaultval = 1,
a9dad670
DC
543 },
544 },
2bd0ea18
NS
545};
546
a9dad670
DC
547struct opt_params nopts = {
548 .name = 'n',
549 .subopts = {
64e924dd
DC
550 [N_SIZE] = "size",
551 [N_VERSION] = "version",
552 [N_FTYPE] = "ftype",
a9dad670
DC
553 },
554 .subopt_params = {
a9dad670 555 { .index = N_SIZE,
2cf637cf 556 .conflicts = { { NULL, LAST_CONFLICT } },
627e74fd
DC
557 .convert = true,
558 .is_power_2 = true,
a9dad670
DC
559 .minval = 1 << XFS_MIN_REC_DIRSIZE,
560 .maxval = XFS_MAX_BLOCKSIZE,
56e4d368 561 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
562 },
563 { .index = N_VERSION,
cedf1c43 564 .conflicts = { { NULL, LAST_CONFLICT } },
1974d3f1
DC
565 .minval = 2,
566 .maxval = 2,
56e4d368 567 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
568 },
569 { .index = N_FTYPE,
cedf1c43 570 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
571 .minval = 0,
572 .maxval = 1,
573 .defaultval = 1,
a9dad670
DC
574 },
575 },
2bd0ea18
NS
576};
577
a9dad670
DC
578struct opt_params ropts = {
579 .name = 'r',
580 .subopts = {
64e924dd
DC
581 [R_EXTSIZE] = "extsize",
582 [R_SIZE] = "size",
583 [R_DEV] = "rtdev",
584 [R_FILE] = "file",
585 [R_NAME] = "name",
586 [R_NOALIGN] = "noalign",
a9dad670
DC
587 },
588 .subopt_params = {
589 { .index = R_EXTSIZE,
cedf1c43 590 .conflicts = { { NULL, LAST_CONFLICT } },
627e74fd
DC
591 .convert = true,
592 .minval = XFS_MIN_RTEXTSIZE,
593 .maxval = XFS_MAX_RTEXTSIZE,
56e4d368 594 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
595 },
596 { .index = R_SIZE,
cedf1c43 597 .conflicts = { { NULL, LAST_CONFLICT } },
627e74fd
DC
598 .convert = true,
599 .minval = 0,
600 .maxval = LLONG_MAX,
56e4d368 601 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
602 },
603 { .index = R_DEV,
9502da21
ES
604 .conflicts = { { &ropts, R_NAME },
605 { NULL, LAST_CONFLICT } },
56e4d368 606 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
607 },
608 { .index = R_FILE,
56e4d368
DC
609 .minval = 0,
610 .maxval = 1,
611 .defaultval = 1,
cedf1c43 612 .conflicts = { { NULL, LAST_CONFLICT } },
a9dad670
DC
613 },
614 { .index = R_NAME,
9502da21
ES
615 .conflicts = { { &ropts, R_DEV },
616 { NULL, LAST_CONFLICT } },
56e4d368 617 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
618 },
619 { .index = R_NOALIGN,
56e4d368
DC
620 .minval = 0,
621 .maxval = 1,
622 .defaultval = 1,
cedf1c43 623 .conflicts = { { NULL, LAST_CONFLICT } },
a9dad670
DC
624 },
625 },
2bd0ea18
NS
626};
627
a9dad670
DC
628struct opt_params sopts = {
629 .name = 's',
630 .subopts = {
64e924dd
DC
631 [S_SIZE] = "size",
632 [S_SECTSIZE] = "sectsize",
a9dad670
DC
633 },
634 .subopt_params = {
a9dad670 635 { .index = S_SIZE,
2cf637cf 636 .conflicts = { { &sopts, S_SECTSIZE },
6c75555e 637 { &dopts, D_SECTSIZE },
cedf1c43 638 { NULL, LAST_CONFLICT } },
627e74fd
DC
639 .convert = true,
640 .is_power_2 = true,
a9dad670
DC
641 .minval = XFS_MIN_SECTORSIZE,
642 .maxval = XFS_MAX_SECTORSIZE,
56e4d368 643 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
644 },
645 { .index = S_SECTSIZE,
2cf637cf 646 .conflicts = { { &sopts, S_SIZE },
6c75555e 647 { &dopts, D_SECTSIZE },
cedf1c43 648 { NULL, LAST_CONFLICT } },
627e74fd
DC
649 .convert = true,
650 .is_power_2 = true,
a9dad670
DC
651 .minval = XFS_MIN_SECTORSIZE,
652 .maxval = XFS_MAX_SECTORSIZE,
56e4d368 653 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670
DC
654 },
655 },
2bd0ea18
NS
656};
657
a9dad670
DC
658struct opt_params mopts = {
659 .name = 'm',
660 .subopts = {
64e924dd
DC
661 [M_CRC] = "crc",
662 [M_FINOBT] = "finobt",
663 [M_UUID] = "uuid",
664 [M_RMAPBT] = "rmapbt",
665 [M_REFLINK] = "reflink",
a9dad670
DC
666 },
667 .subopt_params = {
668 { .index = M_CRC,
cedf1c43 669 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
670 .minval = 0,
671 .maxval = 1,
672 .defaultval = 1,
673 },
674 { .index = M_FINOBT,
cedf1c43 675 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368
DC
676 .minval = 0,
677 .maxval = 1,
678 .defaultval = 1,
679 },
680 { .index = M_UUID,
cedf1c43 681 .conflicts = { { NULL, LAST_CONFLICT } },
56e4d368 682 .defaultval = SUBOPT_NEEDS_VAL,
a9dad670 683 },
c563396a 684 { .index = M_RMAPBT,
cedf1c43 685 .conflicts = { { NULL, LAST_CONFLICT } },
c563396a
DW
686 .minval = 0,
687 .maxval = 1,
23069a93 688 .defaultval = 1,
c563396a 689 },
a5132d9b 690 { .index = M_REFLINK,
cedf1c43 691 .conflicts = { { NULL, LAST_CONFLICT } },
a5132d9b
DW
692 .minval = 0,
693 .maxval = 1,
23069a93 694 .defaultval = 1,
a5132d9b 695 },
a9dad670 696 },
f7b80291
DC
697};
698
cf627f3c
DC
699/* quick way of checking if a parameter was set on the CLI */
700static bool
701cli_opt_set(
702 struct opt_params *opts,
703 int subopt)
704{
705 return opts->subopt_params[subopt].seen ||
706 opts->subopt_params[subopt].str_seen;
707}
708
709/*
710 * Options configured on the command line.
711 *
712 * This stores all the specific config parameters the user sets on the command
713 * line. We do not use these values directly - they are inputs to the mkfs
714 * geometry validation and override any default configuration value we have.
715 *
716 * We don't keep flags to indicate what parameters are set - if we need to check
4ab08e3b 717 * if an option was set on the command line, we check the relevant entry in the
cf627f3c
DC
718 * option table which records whether it was specified in the .seen and
719 * .str_seen variables in the table.
720 *
721 * Some parameters are stored as strings for post-parsing after their dependent
722 * options have been resolved (e.g. block size and sector size have been parsed
723 * and validated).
724 *
725 * This allows us to check that values have been set without needing separate
726 * flags for each value, and hence avoids needing to record and check for each
727 * specific option that can set the value later on in the code. In the cases
728 * where we don't have a cli_params structure around, the above cli_opt_set()
729 * function can be used.
730 */
731struct sb_feat_args {
732 int log_version;
733 int attr_version;
734 int dir_version;
852b3258
ES
735 bool inode_align; /* XFS_SB_VERSION_ALIGNBIT */
736 bool nci; /* XFS_SB_VERSION_BORGBIT */
737 bool lazy_sb_counters; /* XFS_SB_VERSION2_LAZYSBCOUNTBIT */
738 bool parent_pointers; /* XFS_SB_VERSION2_PARENTBIT */
739 bool projid32bit; /* XFS_SB_VERSION2_PROJID32BIT */
740 bool crcs_enabled; /* XFS_SB_VERSION2_CRCBIT */
741 bool dirftype; /* XFS_SB_VERSION2_FTYPE */
742 bool finobt; /* XFS_SB_FEAT_RO_COMPAT_FINOBT */
743 bool spinodes; /* XFS_SB_FEAT_INCOMPAT_SPINODES */
744 bool rmapbt; /* XFS_SB_FEAT_RO_COMPAT_RMAPBT */
745 bool reflink; /* XFS_SB_FEAT_RO_COMPAT_REFLINK */
cf627f3c
DC
746 bool nodalign;
747 bool nortalign;
cf627f3c
DC
748};
749
750struct cli_params {
751 int sectorsize;
752 int blocksize;
753
754 /* parameters that depend on sector/block size being validated. */
755 char *dsize;
756 char *agsize;
757 char *dsu;
758 char *dirblocksize;
759 char *logsize;
760 char *lsu;
761 char *rtextsize;
762 char *rtsize;
763
764 /* parameters where 0 is a valid CLI value */
765 int dsunit;
766 int dswidth;
767 int dsw;
768 int64_t logagno;
769 int loginternal;
770 int lsunit;
771
772 /* parameters where 0 is not a valid value */
773 int64_t agcount;
cf627f3c
DC
774 int inodesize;
775 int inopblock;
776 int imaxpct;
777 int lsectorsize;
778 uuid_t uuid;
779
780 /* feature flags that are set */
781 struct sb_feat_args sb_feat;
782
783 /* root inode characteristics */
784 struct fsxattr fsx;
785
786 /* libxfs device setup */
787 struct libxfs_xinit *xi;
788};
789
4ab08e3b
DC
790/*
791 * Calculated filesystem feature and geometry information.
792 *
793 * This structure contains the information we will use to create the on-disk
794 * filesystem from. The validation and calculation code uses it to store all the
795 * temporary and final config state for the filesystem.
796 *
797 * The information in this structure will contain a mix of validated CLI input
798 * variables, default feature state and calculated values that are needed to
799 * construct the superblock and other on disk features. These are all in one
800 * place so that we don't have to pass handfuls of seemingly arbitrary variables
801 * around to different functions to do the work we need to do.
802 */
803struct mkfs_params {
804 int blocksize;
805 int blocklog;
806 int sectorsize;
807 int sectorlog;
808 int lsectorsize;
809 int lsectorlog;
810 int dirblocksize;
811 int dirblocklog;
812 int inodesize;
813 int inodelog;
814 int inopblock;
815
816 uint64_t dblocks;
817 uint64_t logblocks;
818 uint64_t rtblocks;
819 uint64_t rtextblocks;
820 uint64_t rtextents;
821 uint64_t rtbmblocks; /* rt bitmap blocks */
822
823 int dsunit; /* in FSBs */
824 int dswidth; /* in FSBs */
825 int lsunit; /* in FSBs */
826
827 uint64_t agsize;
828 uint64_t agcount;
829
830 int imaxpct;
831
832 bool loginternal;
833 uint64_t logstart;
834 uint64_t logagno;
835
836 uuid_t uuid;
837 char *label;
838
839 struct sb_feat_args sb_feat;
840};
841
68344ba0
DC
842/*
843 * Default filesystem features and configuration values
844 *
845 * This structure contains the default mkfs values that are to be used when
846 * a user does not specify the option on the command line. We do not use these
847 * values directly - they are inputs to the mkfs geometry validation and
848 * calculations.
849 */
850struct mkfs_default_params {
851 char *source; /* where the defaults came from */
852
853 int sectorsize;
854 int blocksize;
855
856 /* feature flags that are set */
857 struct sb_feat_args sb_feat;
858
859 /* root inode characteristics */
860 struct fsxattr fsx;
861};
862
dafb318d
DC
863static void __attribute__((noreturn))
864usage( void )
865{
866 fprintf(stderr, _("Usage: %s\n\
c66bd30e 867/* blocksize */ [-b size=num]\n\
dafb318d
DC
868/* metadata */ [-m crc=0|1,finobt=0|1,uuid=xxx,rmapbt=0|1,reflink=0|1]\n\
869/* data subvol */ [-d agcount=n,agsize=n,file,name=xxx,size=num,\n\
870 (sunit=value,swidth=value|su=num,sw=num|noalign),\n\
c66bd30e 871 sectsize=num\n\
dafb318d
DC
872/* force overwrite */ [-f]\n\
873/* inode size */ [-i log=n|perblock=n|size=num,maxpct=n,attr=0|1|2,\n\
874 projid32bit=0|1,sparse=0|1]\n\
875/* no discard */ [-K]\n\
876/* log subvol */ [-l agnum=n,internal,size=num,logdev=xxx,version=n\n\
c66bd30e 877 sunit=value|su=num,sectsize=num,lazy-count=0|1]\n\
dafb318d 878/* label */ [-L label (maximum 12 characters)]\n\
c66bd30e 879/* naming */ [-n size=num,version=2|ci,ftype=0|1]\n\
dafb318d
DC
880/* no-op info only */ [-N]\n\
881/* prototype file */ [-p fname]\n\
882/* quiet */ [-q]\n\
883/* realtime subvol */ [-r extsize=num,size=num,rtdev=xxx]\n\
c66bd30e 884/* sectorsize */ [-s size=num]\n\
dafb318d
DC
885/* version */ [-V]\n\
886 devicename\n\
887<devicename> is required unless -d name=xxx is given.\n\
888<num> is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxx KiB),\n\
889 xxxm (xxx MiB), xxxg (xxx GiB), xxxt (xxx TiB) or xxxp (xxx PiB).\n\
890<value> is xxx (512 byte blocks).\n"),
891 progname);
892 exit(1);
893}
894
895static void
896conflict(
cedf1c43
DC
897 struct opt_params *opts,
898 int option,
899 struct opt_params *con_opts,
900 int conflict)
dafb318d
DC
901{
902 fprintf(stderr, _("Cannot specify both -%c %s and -%c %s\n"),
6c75555e
DC
903 con_opts->name, con_opts->subopts[conflict],
904 opts->name, opts->subopts[option]);
dafb318d
DC
905 usage();
906}
907
908
909static void
910illegal(
911 const char *value,
912 const char *opt)
913{
0b1cf8bb 914 fprintf(stderr, _("Invalid value %s for -%s option\n"), value, opt);
dafb318d
DC
915 usage();
916}
917
918static int
919ispow2(
920 unsigned int i)
921{
922 return (i & (i - 1)) == 0;
923}
924
925static void __attribute__((noreturn))
926reqval(
927 char opt,
928 const char *tab[],
929 int idx)
930{
931 fprintf(stderr, _("-%c %s option requires a value\n"), opt, tab[idx]);
932 usage();
933}
934
935static void
936respec(
937 char opt,
938 const char *tab[],
939 int idx)
940{
941 fprintf(stderr, "-%c ", opt);
942 if (tab)
943 fprintf(stderr, "%s ", tab[idx]);
944 fprintf(stderr, _("option respecified\n"));
945 usage();
946}
947
948static void
949unknown(
950 char opt,
951 char *s)
952{
953 fprintf(stderr, _("unknown option -%c %s\n"), opt, s);
954 usage();
955}
956
957long long
958cvtnum(
959 unsigned int blksize,
960 unsigned int sectsize,
961 const char *s)
962{
963 long long i;
964 char *sp;
965 int c;
966
967 i = strtoll(s, &sp, 0);
968 if (i == 0 && sp == s)
969 return -1LL;
970 if (*sp == '\0')
971 return i;
972
973 if (sp[1] != '\0')
974 return -1LL;
975
976 if (*sp == 'b') {
977 if (!blksize) {
978 fprintf(stderr,
979_("Blocksize must be provided prior to using 'b' suffix.\n"));
980 usage();
981 } else {
982 return i * blksize;
983 }
984 }
985 if (*sp == 's') {
986 if (!sectsize) {
987 fprintf(stderr,
988_("Sectorsize must be specified prior to using 's' suffix.\n"));
989 usage();
990 } else {
991 return i * sectsize;
992 }
993 }
994
995 c = tolower(*sp);
996 switch (c) {
997 case 'e':
998 i *= 1024LL;
999 /* fall through */
1000 case 'p':
1001 i *= 1024LL;
1002 /* fall through */
1003 case 't':
1004 i *= 1024LL;
1005 /* fall through */
1006 case 'g':
1007 i *= 1024LL;
1008 /* fall through */
1009 case 'm':
1010 i *= 1024LL;
1011 /* fall through */
1012 case 'k':
1013 return i * 1024LL;
1014 default:
1015 break;
1016 }
1017 return -1LL;
1018}
1019
06ac92fd
DC
1020static void
1021check_device_type(
1022 const char *name,
1023 int *isfile,
1024 bool no_size,
1025 bool no_name,
1026 int *create,
1027 bool force_overwrite,
1028 const char *optname)
1029{
f594a0d1 1030 struct stat statbuf;
06ac92fd
DC
1031
1032 if (*isfile && (no_size || no_name)) {
1033 fprintf(stderr,
1034 _("if -%s file then -%s name and -%s size are required\n"),
1035 optname, optname, optname);
1036 usage();
1037 }
1038
1039 if (!name) {
1040 fprintf(stderr, _("No device name specified\n"));
1041 usage();
1042 }
1043
f594a0d1 1044 if (stat(name, &statbuf)) {
06ac92fd
DC
1045 if (errno == ENOENT && *isfile) {
1046 if (create)
1047 *create = 1;
1048 return;
1049 }
1050
1051 fprintf(stderr,
1052 _("Error accessing specified device %s: %s\n"),
1053 name, strerror(errno));
1054 usage();
1055 return;
1056 }
1057
1058 if (!force_overwrite && check_overwrite(name)) {
1059 fprintf(stderr,
1060 _("%s: Use the -f option to force overwrite.\n"),
1061 progname);
1062 exit(1);
1063 }
1064
1065 /*
1066 * We only want to completely truncate and recreate an existing file if
1067 * we were specifically told it was a file. Set the create flag only in
1068 * this case to trigger that behaviour.
1069 */
1070 if (S_ISREG(statbuf.st_mode)) {
1071 if (!*isfile)
1072 *isfile = 1;
1073 else if (create)
1074 *create = 1;
1075 return;
1076 }
1077
1078 if (S_ISBLK(statbuf.st_mode)) {
1079 if (*isfile) {
1080 fprintf(stderr,
1081 _("specified \"-%s file\" on a block device %s\n"),
1082 optname, name);
1083 usage();
1084 }
1085 return;
1086 }
1087
1088 fprintf(stderr,
1089 _("specified device %s not a file or block device\n"),
1090 name);
1091 usage();
1092}
1093
1f1b8be7
NS
1094static void
1095validate_ag_geometry(
1096 int blocklog,
14f8b681
DW
1097 uint64_t dblocks,
1098 uint64_t agsize,
1099 uint64_t agcount)
1f1b8be7
NS
1100{
1101 if (agsize < XFS_AG_MIN_BLOCKS(blocklog)) {
1102 fprintf(stderr,
ddf12ea5 1103 _("agsize (%lld blocks) too small, need at least %lld blocks\n"),
1f1b8be7
NS
1104 (long long)agsize,
1105 (long long)XFS_AG_MIN_BLOCKS(blocklog));
1106 usage();
1107 }
1108
1109 if (agsize > XFS_AG_MAX_BLOCKS(blocklog)) {
1110 fprintf(stderr,
ddf12ea5 1111 _("agsize (%lld blocks) too big, maximum is %lld blocks\n"),
1f1b8be7
NS
1112 (long long)agsize,
1113 (long long)XFS_AG_MAX_BLOCKS(blocklog));
1114 usage();
1115 }
1116
1117 if (agsize > dblocks) {
1118 fprintf(stderr,
ddf12ea5 1119 _("agsize (%lld blocks) too big, data area is %lld blocks\n"),
1f1b8be7
NS
1120 (long long)agsize, (long long)dblocks);
1121 usage();
1122 }
1123
1124 if (agsize < XFS_AG_MIN_BLOCKS(blocklog)) {
1125 fprintf(stderr,
1126 _("too many allocation groups for size = %lld\n"),
1127 (long long)agsize);
1128 fprintf(stderr, _("need at most %lld allocation groups\n"),
1129 (long long)(dblocks / XFS_AG_MIN_BLOCKS(blocklog) +
1130 (dblocks % XFS_AG_MIN_BLOCKS(blocklog) != 0)));
1131 usage();
1132 }
1133
1134 if (agsize > XFS_AG_MAX_BLOCKS(blocklog)) {
1135 fprintf(stderr,
1136 _("too few allocation groups for size = %lld\n"), (long long)agsize);
1137 fprintf(stderr,
1138 _("need at least %lld allocation groups\n"),
1e945ba2 1139 (long long)(dblocks / XFS_AG_MAX_BLOCKS(blocklog) +
1f1b8be7
NS
1140 (dblocks % XFS_AG_MAX_BLOCKS(blocklog) != 0)));
1141 usage();
1142 }
1143
1144 /*
1145 * If the last AG is too small, reduce the filesystem size
1146 * and drop the blocks.
1147 */
1148 if ( dblocks % agsize != 0 &&
1149 (dblocks % agsize < XFS_AG_MIN_BLOCKS(blocklog))) {
1150 fprintf(stderr,
1151 _("last AG size %lld blocks too small, minimum size is %lld blocks\n"),
1152 (long long)(dblocks % agsize),
1153 (long long)XFS_AG_MIN_BLOCKS(blocklog));
1154 usage();
1155 }
1156
1157 /*
1158 * If agcount is too large, make it smaller.
1159 */
1160 if (agcount > XFS_MAX_AGNUMBER + 1) {
1161 fprintf(stderr,
1162 _("%lld allocation groups is too many, maximum is %lld\n"),
1163 (long long)agcount, (long long)XFS_MAX_AGNUMBER + 1);
1164 usage();
1165 }
1166}
1167
1e945ba2
BN
1168static void
1169zero_old_xfs_structures(
1170 libxfs_init_t *xi,
1171 xfs_sb_t *new_sb)
1172{
1173 void *buf;
1174 xfs_sb_t sb;
14f8b681 1175 uint32_t bsize;
1e945ba2
BN
1176 int i;
1177 xfs_off_t off;
31daa90b
BF
1178
1179 /*
1180 * We open regular files with O_TRUNC|O_CREAT. Nothing to do here...
1181 */
1182 if (xi->disfile && xi->dcreat)
1183 return;
1e945ba2
BN
1184
1185 /*
ff1f79a7 1186 * read in existing filesystem superblock, use its geometry
1e945ba2
BN
1187 * settings and zero the existing secondary superblocks.
1188 */
1189 buf = memalign(libxfs_device_alignment(), new_sb->sb_sectsize);
1190 if (!buf) {
1191 fprintf(stderr,
1192 _("error reading existing superblock -- failed to memalign buffer\n"));
1193 return;
1194 }
dab9b8d6 1195 memset(buf, 0, new_sb->sb_sectsize);
1e945ba2 1196
06ac92fd
DC
1197 /*
1198 * If we are creating an image file, it might be of zero length at this
1199 * point in time. Hence reading the existing superblock is going to
1200 * return zero bytes. It's not a failure we need to warn about in this
1201 * case.
1202 */
1203 off = pread(xi->dfd, buf, new_sb->sb_sectsize, 0);
1204 if (off != new_sb->sb_sectsize) {
1205 if (!xi->disfile)
1206 fprintf(stderr,
1207 _("error reading existing superblock: %s\n"),
1208 strerror(errno));
31daa90b 1209 goto done;
1e945ba2 1210 }
5e656dbb 1211 libxfs_sb_from_disk(&sb, buf);
1e945ba2
BN
1212
1213 /*
1214 * perform same basic superblock validation to make sure we
1215 * actually zero secondary blocks
1216 */
1217 if (sb.sb_magicnum != XFS_SB_MAGIC || sb.sb_blocksize == 0)
1218 goto done;
1219
1220 for (bsize = 1, i = 0; bsize < sb.sb_blocksize &&
1221 i < sizeof(sb.sb_blocksize) * NBBY; i++)
1222 bsize <<= 1;
1223
1224 if (i < XFS_MIN_BLOCKSIZE_LOG || i > XFS_MAX_BLOCKSIZE_LOG ||
1225 i != sb.sb_blocklog)
1226 goto done;
1227
14f8b681
DW
1228 if (sb.sb_dblocks > ((uint64_t)sb.sb_agcount * sb.sb_agblocks) ||
1229 sb.sb_dblocks < ((uint64_t)(sb.sb_agcount - 1) *
1e945ba2
BN
1230 sb.sb_agblocks + XFS_MIN_AG_BLOCKS))
1231 goto done;
1232
1233 /*
c93b0a22 1234 * block size and basic geometry seems alright, zero the secondaries.
1e945ba2 1235 */
dab9b8d6 1236 memset(buf, 0, new_sb->sb_sectsize);
1e945ba2
BN
1237 off = 0;
1238 for (i = 1; i < sb.sb_agcount; i++) {
1239 off += sb.sb_agblocks;
2f9a125c 1240 if (pwrite(xi->dfd, buf, new_sb->sb_sectsize,
1e945ba2
BN
1241 off << sb.sb_blocklog) == -1)
1242 break;
1243 }
1244done:
1245 free(buf);
1246}
1247
ad136b33 1248static void
14f8b681 1249discard_blocks(dev_t dev, uint64_t nsectors)
ad136b33
CH
1250{
1251 int fd;
1252
1253 /*
1254 * We intentionally ignore errors from the discard ioctl. It is
1255 * not necessary for the mkfs functionality but just an optimization.
1256 */
1257 fd = libxfs_device_to_fd(dev);
1258 if (fd > 0)
1259 platform_discard_blocks(fd, 0, nsectors << 9);
1260}
1261
a9dad670
DC
1262static __attribute__((noreturn)) void
1263illegal_option(
aa3034d4 1264 const char *value,
a9dad670 1265 struct opt_params *opts,
aa3034d4
JT
1266 int index,
1267 const char *reason)
a9dad670
DC
1268{
1269 fprintf(stderr,
0b1cf8bb 1270 _("Invalid value %s for -%c %s option. %s\n"),
aa3034d4 1271 value, opts->name, opts->subopts[index],
7d25f65f 1272 reason);
a9dad670
DC
1273 usage();
1274}
1275
27ae3a59
DC
1276/*
1277 * Check for conflicts and option respecification.
1278 */
1279static void
1280check_opt(
a9dad670 1281 struct opt_params *opts,
27ae3a59
DC
1282 int index,
1283 bool str_seen)
147e0f31 1284{
27ae3a59
DC
1285 struct subopt_param *sp = &opts->subopt_params[index];
1286 int i;
147e0f31 1287
56e4d368
DC
1288 if (sp->index != index) {
1289 fprintf(stderr,
02c3e106 1290 _("Developer screwed up option parsing (%d/%d)! Please report!\n"),
56e4d368 1291 sp->index, index);
05abf43d 1292 reqval(opts->name, opts->subopts, index);
56e4d368
DC
1293 }
1294
27ae3a59
DC
1295 /*
1296 * Check for respecification of the option. This is more complex than it
1297 * seems because some options are parsed twice - once as a string during
1298 * input parsing, then later the string is passed to getnum for
1299 * conversion into a number and bounds checking. Hence the two variables
1300 * used to track the different uses based on the @str parameter passed
1301 * to us.
1302 */
1303 if (!str_seen) {
1304 if (sp->seen)
05abf43d 1305 respec(opts->name, opts->subopts, index);
27ae3a59
DC
1306 sp->seen = true;
1307 } else {
1308 if (sp->str_seen)
05abf43d 1309 respec(opts->name, opts->subopts, index);
27ae3a59
DC
1310 sp->str_seen = true;
1311 }
9090e187 1312
3ec1956a 1313 /* check for conflicts with the option */
27ae3a59 1314 for (i = 0; i < MAX_CONFLICTS; i++) {
cedf1c43 1315 struct _conflict *con = &sp->conflicts[i];
3ec1956a 1316
cedf1c43 1317 if (con->subopt == LAST_CONFLICT)
3ec1956a 1318 break;
cedf1c43
DC
1319 if (con->opts->subopt_params[con->subopt].seen ||
1320 con->opts->subopt_params[con->subopt].str_seen)
1321 conflict(opts, index, con->opts, con->subopt);
3ec1956a 1322 }
27ae3a59 1323}
3ec1956a 1324
27ae3a59
DC
1325static long long
1326getnum(
1327 const char *str,
1328 struct opt_params *opts,
1329 int index)
1330{
1331 struct subopt_param *sp = &opts->subopt_params[index];
1332 long long c;
1333
1334 check_opt(opts, index, false);
6c855628 1335 /* empty strings might just return a default value */
56e4d368
DC
1336 if (!str || *str == '\0') {
1337 if (sp->defaultval == SUBOPT_NEEDS_VAL)
05abf43d 1338 reqval(opts->name, opts->subopts, index);
56e4d368
DC
1339 return sp->defaultval;
1340 }
a9dad670 1341
56e4d368 1342 if (sp->minval == 0 && sp->maxval == 0) {
a9dad670
DC
1343 fprintf(stderr,
1344 _("Option -%c %s has undefined minval/maxval."
1345 "Can't verify value range. This is a bug.\n"),
1346 opts->name, opts->subopts[index]);
1347 exit(1);
1348 }
147e0f31 1349
6c855628
DC
1350 /*
1351 * Some values are pure numbers, others can have suffixes that define
1352 * the units of the number. Those get passed to cvtnum(), otherwise we
1353 * convert it ourselves to guarantee there is no trailing garbage in the
1354 * number.
1355 */
1356 if (sp->convert)
1357 c = cvtnum(blocksize, sectorsize, str);
1358 else {
1359 char *str_end;
1360
1361 c = strtoll(str, &str_end, 0);
1362 if (c == 0 && str_end == str)
7d25f65f
DW
1363 illegal_option(str, opts, index,
1364 _("Value not recognized as number."));
6c855628 1365 if (*str_end != '\0')
7d25f65f
DW
1366 illegal_option(str, opts, index,
1367 _("Unit suffixes are not allowed."));
6c855628
DC
1368 }
1369
1370 /* Validity check the result. */
aa3034d4 1371 if (c < sp->minval)
7d25f65f 1372 illegal_option(str, opts, index, _("Value is too small."));
aa3034d4 1373 else if (c > sp->maxval)
7d25f65f 1374 illegal_option(str, opts, index, _("Value is too large."));
627e74fd 1375 if (sp->is_power_2 && !ispow2(c))
7d25f65f 1376 illegal_option(str, opts, index, _("Value must be a power of 2."));
147e0f31
DC
1377 return c;
1378}
1379
27ae3a59
DC
1380/*
1381 * Option is a string - do all the option table work, and check there
1382 * is actually an option string. Otherwise we don't do anything with the string
1383 * here - validation will be done later when the string is converted to a value
1384 * or used as a file/device path.
1385 */
1386static char *
1387getstr(
1388 char *str,
1389 struct opt_params *opts,
1390 int index)
1391{
1392 check_opt(opts, index, true);
1393
1394 /* empty strings for string options are not valid */
1395 if (!str || *str == '\0')
05abf43d 1396 reqval(opts->name, opts->subopts, index);
27ae3a59
DC
1397 return str;
1398}
1399
a350bbc1
DC
1400static int
1401block_opts_parser(
1402 struct opt_params *opts,
1403 int subopt,
1404 char *value,
1405 struct cli_params *cli)
1406{
85d6f03d 1407 switch (subopt) {
85d6f03d 1408 case B_SIZE:
e5e612ae 1409 cli->blocksize = getnum(value, opts, subopt);
85d6f03d
DC
1410 break;
1411 default:
1412 return -EINVAL;
1413 }
a350bbc1
DC
1414 return 0;
1415}
1416
1417static int
1418data_opts_parser(
1419 struct opt_params *opts,
1420 int subopt,
1421 char *value,
1422 struct cli_params *cli)
1423{
a1273b7c
DC
1424 switch (subopt) {
1425 case D_AGCOUNT:
e5e612ae 1426 cli->agcount = getnum(value, opts, subopt);
a1273b7c
DC
1427 break;
1428 case D_AGSIZE:
e5e612ae 1429 cli->agsize = getstr(value, opts, subopt);
a1273b7c
DC
1430 break;
1431 case D_FILE:
e5e612ae 1432 cli->xi->disfile = getnum(value, opts, subopt);
a1273b7c
DC
1433 break;
1434 case D_NAME:
e5e612ae 1435 cli->xi->dname = getstr(value, opts, subopt);
a1273b7c
DC
1436 break;
1437 case D_SIZE:
e5e612ae 1438 cli->dsize = getstr(value, opts, subopt);
a1273b7c
DC
1439 break;
1440 case D_SUNIT:
e5e612ae 1441 cli->dsunit = getnum(value, opts, subopt);
a1273b7c
DC
1442 break;
1443 case D_SWIDTH:
e5e612ae 1444 cli->dswidth = getnum(value, opts, subopt);
a1273b7c
DC
1445 break;
1446 case D_SU:
e5e612ae 1447 cli->dsu = getstr(value, opts, subopt);
a1273b7c
DC
1448 break;
1449 case D_SW:
e5e612ae 1450 cli->dsw = getnum(value, opts, subopt);
a1273b7c
DC
1451 break;
1452 case D_NOALIGN:
e5e612ae 1453 cli->sb_feat.nodalign = getnum(value, opts, subopt);
a1273b7c 1454 break;
a1273b7c 1455 case D_SECTSIZE:
e5e612ae 1456 cli->sectorsize = getnum(value, opts, subopt);
a1273b7c
DC
1457 break;
1458 case D_RTINHERIT:
e5e612ae 1459 if (getnum(value, opts, subopt))
a1273b7c
DC
1460 cli->fsx.fsx_xflags |= XFS_DIFLAG_RTINHERIT;
1461 break;
1462 case D_PROJINHERIT:
e5e612ae 1463 cli->fsx.fsx_projid = getnum(value, opts, subopt);
a1273b7c
DC
1464 cli->fsx.fsx_xflags |= XFS_DIFLAG_PROJINHERIT;
1465 break;
1466 case D_EXTSZINHERIT:
e5e612ae 1467 cli->fsx.fsx_extsize = getnum(value, opts, subopt);
a1273b7c
DC
1468 cli->fsx.fsx_xflags |= XFS_DIFLAG_EXTSZINHERIT;
1469 break;
1470 case D_COWEXTSIZE:
e5e612ae 1471 cli->fsx.fsx_cowextsize = getnum(value, opts, subopt);
a1273b7c
DC
1472 cli->fsx.fsx_xflags |= FS_XFLAG_COWEXTSIZE;
1473 break;
1474 default:
1475 return -EINVAL;
1476 }
a350bbc1
DC
1477 return 0;
1478}
1479
1480static int
1481inode_opts_parser(
1482 struct opt_params *opts,
1483 int subopt,
1484 char *value,
1485 struct cli_params *cli)
1486{
a3ac5af1
DC
1487 switch (subopt) {
1488 case I_ALIGN:
e5e612ae 1489 cli->sb_feat.inode_align = getnum(value, opts, subopt);
a3ac5af1 1490 break;
a3ac5af1 1491 case I_MAXPCT:
e5e612ae 1492 cli->imaxpct = getnum(value, opts, subopt);
a3ac5af1
DC
1493 break;
1494 case I_PERBLOCK:
e5e612ae 1495 cli->inopblock = getnum(value, opts, subopt);
a3ac5af1
DC
1496 break;
1497 case I_SIZE:
e5e612ae 1498 cli->inodesize = getnum(value, opts, subopt);
a3ac5af1
DC
1499 break;
1500 case I_ATTR:
e5e612ae 1501 cli->sb_feat.attr_version = getnum(value, opts, subopt);
a3ac5af1
DC
1502 break;
1503 case I_PROJID32BIT:
e5e612ae 1504 cli->sb_feat.projid32bit = getnum(value, opts, subopt);
a3ac5af1
DC
1505 break;
1506 case I_SPINODES:
e5e612ae 1507 cli->sb_feat.spinodes = getnum(value, opts, subopt);
a3ac5af1
DC
1508 break;
1509 default:
1510 return -EINVAL;
1511 }
a350bbc1
DC
1512 return 0;
1513}
1514
1515static int
1516log_opts_parser(
1517 struct opt_params *opts,
1518 int subopt,
1519 char *value,
1520 struct cli_params *cli)
1521{
f3bc91a4
DC
1522 switch (subopt) {
1523 case L_AGNUM:
e5e612ae 1524 cli->logagno = getnum(value, opts, subopt);
f3bc91a4
DC
1525 break;
1526 case L_FILE:
e5e612ae 1527 cli->xi->lisfile = getnum(value, opts, subopt);
f3bc91a4
DC
1528 break;
1529 case L_INTERNAL:
e5e612ae 1530 cli->loginternal = getnum(value, opts, subopt);
f3bc91a4
DC
1531 break;
1532 case L_SU:
e5e612ae 1533 cli->lsu = getstr(value, opts, subopt);
f3bc91a4
DC
1534 break;
1535 case L_SUNIT:
e5e612ae 1536 cli->lsunit = getnum(value, opts, subopt);
f3bc91a4
DC
1537 break;
1538 case L_NAME:
1539 case L_DEV:
e5e612ae 1540 cli->xi->logname = getstr(value, opts, subopt);
f3bc91a4
DC
1541 cli->loginternal = 0;
1542 break;
1543 case L_VERSION:
e5e612ae 1544 cli->sb_feat.log_version = getnum(value, opts, subopt);
f3bc91a4
DC
1545 break;
1546 case L_SIZE:
e5e612ae 1547 cli->logsize = getstr(value, opts, subopt);
f3bc91a4 1548 break;
f3bc91a4 1549 case L_SECTSIZE:
e5e612ae 1550 cli->lsectorsize = getnum(value, opts, subopt);
f3bc91a4
DC
1551 break;
1552 case L_LAZYSBCNTR:
e5e612ae 1553 cli->sb_feat.lazy_sb_counters = getnum(value, opts, subopt);
f3bc91a4
DC
1554 break;
1555 default:
1556 return -EINVAL;
1557 }
a350bbc1
DC
1558 return 0;
1559}
1560
1561static int
1562meta_opts_parser(
1563 struct opt_params *opts,
1564 int subopt,
1565 char *value,
1566 struct cli_params *cli)
1567{
997136c6
DC
1568 switch (subopt) {
1569 case M_CRC:
e5e612ae 1570 cli->sb_feat.crcs_enabled = getnum(value, opts, subopt);
997136c6
DC
1571 if (cli->sb_feat.crcs_enabled)
1572 cli->sb_feat.dirftype = true;
1573 break;
1574 case M_FINOBT:
e5e612ae 1575 cli->sb_feat.finobt = getnum(value, opts, subopt);
997136c6
DC
1576 break;
1577 case M_UUID:
1578 if (!value || *value == '\0')
e5e612ae 1579 reqval('m', opts->subopts, subopt);
997136c6
DC
1580 if (platform_uuid_parse(value, &cli->uuid))
1581 illegal(value, "m uuid");
1582 break;
1583 case M_RMAPBT:
e5e612ae 1584 cli->sb_feat.rmapbt = getnum(value, opts, subopt);
997136c6
DC
1585 break;
1586 case M_REFLINK:
e5e612ae 1587 cli->sb_feat.reflink = getnum(value, opts, subopt);
997136c6
DC
1588 break;
1589 default:
1590 return -EINVAL;
1591 }
a350bbc1
DC
1592 return 0;
1593}
1594
1595static int
1596naming_opts_parser(
1597 struct opt_params *opts,
1598 int subopt,
1599 char *value,
1600 struct cli_params *cli)
1601{
98d40922 1602 switch (subopt) {
98d40922 1603 case N_SIZE:
e5e612ae 1604 cli->dirblocksize = getstr(value, opts, subopt);
98d40922
DC
1605 break;
1606 case N_VERSION:
e5e612ae 1607 value = getstr(value, &nopts, subopt);
98d40922
DC
1608 if (!strcasecmp(value, "ci")) {
1609 /* ASCII CI mode */
1610 cli->sb_feat.nci = true;
1611 } else {
e5e612ae 1612 cli->sb_feat.dir_version = getnum(value, opts, subopt);
98d40922
DC
1613 }
1614 break;
1615 case N_FTYPE:
e5e612ae 1616 cli->sb_feat.dirftype = getnum(value, opts, subopt);
98d40922
DC
1617 break;
1618 default:
1619 return -EINVAL;
1620 }
a350bbc1
DC
1621 return 0;
1622}
1623
1624static int
1625rtdev_opts_parser(
1626 struct opt_params *opts,
1627 int subopt,
1628 char *value,
1629 struct cli_params *cli)
1630{
d145f69d
DC
1631 switch (subopt) {
1632 case R_EXTSIZE:
e5e612ae 1633 cli->rtextsize = getstr(value, opts, subopt);
d145f69d
DC
1634 break;
1635 case R_FILE:
e5e612ae 1636 cli->xi->risfile = getnum(value, opts, subopt);
d145f69d
DC
1637 break;
1638 case R_NAME:
1639 case R_DEV:
e5e612ae 1640 cli->xi->rtname = getstr(value, opts, subopt);
d145f69d
DC
1641 break;
1642 case R_SIZE:
e5e612ae 1643 cli->rtsize = getstr(value, opts, subopt);
d145f69d
DC
1644 break;
1645 case R_NOALIGN:
e5e612ae 1646 cli->sb_feat.nortalign = getnum(value, opts, subopt);
d145f69d
DC
1647 break;
1648 default:
1649 return -EINVAL;
1650 }
a350bbc1
DC
1651 return 0;
1652}
1653
1654static int
1655sector_opts_parser(
1656 struct opt_params *opts,
1657 int subopt,
1658 char *value,
1659 struct cli_params *cli)
1660{
f948f00a 1661 switch (subopt) {
f948f00a
DC
1662 case S_SIZE:
1663 case S_SECTSIZE:
6c75555e 1664 cli->sectorsize = getnum(value, opts, subopt);
f948f00a
DC
1665 cli->lsectorsize = cli->sectorsize;
1666 break;
1667 default:
1668 return -EINVAL;
1669 }
a350bbc1
DC
1670 return 0;
1671}
1672
1673struct subopts {
1674 char opt;
1675 struct opt_params *opts;
1676 int (*parser)();
1677} subopt_tab[] = {
1678 { 'b', &bopts, block_opts_parser },
1679 { 'd', &dopts, data_opts_parser },
1680 { 'i', &iopts, inode_opts_parser },
1681 { 'l', &lopts, log_opts_parser },
1682 { 'm', &mopts, meta_opts_parser },
1683 { 'n', &nopts, naming_opts_parser },
1684 { 'r', &ropts, rtdev_opts_parser },
1685 { 's', &sopts, sector_opts_parser },
1686 { '\0', NULL, NULL },
1687};
1688
1689static void
1690parse_subopts(
1691 char opt,
1692 char *arg,
1693 struct cli_params *cli)
1694{
1695 struct subopts *sop = &subopt_tab[0];
1696 char *p;
1697 int ret = 0;
1698
1699 while (sop->opts) {
1700 if (sop->opt == opt)
1701 break;
1702 sop++;
1703 }
1704
1705 /* should never happen */
1706 if (!sop->opts)
1707 return;
1708
1709 p = arg;
1710 while (*p != '\0') {
1711 char **subopts = (char **)sop->opts->subopts;
1712 char *value;
1713 int subopt;
1714
1715 subopt = getsubopt(&p, subopts, &value);
1716
1717 ret = (sop->parser)(sop->opts, subopt, value, cli);
1718 if (ret)
1719 unknown(opt, value);
1720 }
1721}
1722
585f41bb
DC
1723static void
1724validate_sectorsize(
1725 struct mkfs_params *cfg,
1726 struct cli_params *cli,
1727 struct mkfs_default_params *dft,
1728 struct fs_topology *ft,
1729 char *dfile,
1730 int dry_run,
1731 int force_overwrite)
1732{
1733 /* set configured sector sizes in preparation for checks */
1734 if (!cli->sectorsize) {
1735 cfg->sectorsize = dft->sectorsize;
1736 } else {
1737 cfg->sectorsize = cli->sectorsize;
1738 }
1739 cfg->sectorlog = libxfs_highbit32(cfg->sectorsize);
1740
1741 /*
1742 * Before anything else, verify that we are correctly operating on
1743 * files or block devices and set the control parameters correctly.
1744 */
1745 check_device_type(dfile, &cli->xi->disfile, !cli->dsize, !dfile,
1746 dry_run ? NULL : &cli->xi->dcreat,
1747 force_overwrite, "d");
1748 if (!cli->loginternal)
1749 check_device_type(cli->xi->logname, &cli->xi->lisfile,
1750 !cli->logsize, !cli->xi->logname,
1751 dry_run ? NULL : &cli->xi->lcreat,
1752 force_overwrite, "l");
1753 if (cli->xi->rtname)
1754 check_device_type(cli->xi->rtname, &cli->xi->risfile,
1755 !cli->rtsize, !cli->xi->rtname,
1756 dry_run ? NULL : &cli->xi->rcreat,
1757 force_overwrite, "r");
1758
1759 /*
1760 * Explicitly disable direct IO for image files so we don't error out on
1761 * sector size mismatches between the new filesystem and the underlying
1762 * host filesystem.
1763 */
1764 if (cli->xi->disfile || cli->xi->lisfile || cli->xi->risfile)
1765 cli->xi->isdirect = 0;
1766
1767 memset(ft, 0, sizeof(*ft));
1768 get_topology(cli->xi, ft, force_overwrite);
1769
1770 if (!cli->sectorsize) {
1771 /*
1772 * Unless specified manually on the command line use the
1773 * advertised sector size of the device. We use the physical
1774 * sector size unless the requested block size is smaller
1775 * than that, then we can use logical, but warn about the
1776 * inefficiency.
1777 *
1778 * Set the topology sectors if they were not probed to the
1779 * minimum supported sector size.
1780 */
1781
1782 if (!ft->lsectorsize)
1783 ft->lsectorsize = XFS_MIN_SECTORSIZE;
1784
1785 /* Older kernels may not have physical/logical distinction */
1786 if (!ft->psectorsize)
1787 ft->psectorsize = ft->lsectorsize;
1788
1789 cfg->sectorsize = ft->psectorsize;
1790 if (cfg->blocksize < cfg->sectorsize &&
1791 cfg->blocksize >= ft->lsectorsize) {
1792 fprintf(stderr,
1793_("specified blocksize %d is less than device physical sector size %d\n"
1794 "switching to logical sector size %d\n"),
1795 cfg->blocksize, ft->psectorsize,
1796 ft->lsectorsize);
1797 cfg->sectorsize = ft->lsectorsize;
1798 }
1799
1800 cfg->sectorlog = libxfs_highbit32(cfg->sectorsize);
1801 }
1802
1803 /* validate specified/probed sector size */
1804 if (cfg->sectorsize < XFS_MIN_SECTORSIZE ||
1805 cfg->sectorsize > XFS_MAX_SECTORSIZE) {
1806 fprintf(stderr, _("illegal sector size %d\n"), cfg->sectorsize);
1807 usage();
1808 }
1809
1810 if (cfg->blocksize < cfg->sectorsize) {
1811 fprintf(stderr,
1812_("block size %d cannot be smaller than sector size %d\n"),
1813 cfg->blocksize, cfg->sectorsize);
1814 usage();
1815 }
1816
1817 if (cfg->sectorsize < ft->lsectorsize) {
1818 fprintf(stderr, _("illegal sector size %d; hw sector is %d\n"),
1819 cfg->sectorsize, ft->lsectorsize);
1820 usage();
1821 }
1822}
1823
b1b8e54e
DC
1824static void
1825validate_blocksize(
1826 struct mkfs_params *cfg,
1827 struct cli_params *cli,
1828 struct mkfs_default_params *dft)
1829{
1830 /*
1831 * Blocksize and sectorsize first, other things depend on them
1832 * For RAID4/5/6 we want to align sector size and block size,
1833 * so we need to start with the device geometry extraction too.
1834 */
1835 if (!cli->blocksize)
1836 cfg->blocksize = dft->blocksize;
1837 else
1838 cfg->blocksize = cli->blocksize;
1839 cfg->blocklog = libxfs_highbit32(cfg->blocksize);
1840
1841 /* validate block sizes are in range */
1842 if (cfg->blocksize < XFS_MIN_BLOCKSIZE ||
1843 cfg->blocksize > XFS_MAX_BLOCKSIZE) {
1844 fprintf(stderr, _("illegal block size %d\n"), cfg->blocksize);
1845 usage();
1846 }
1847
1848 if (cli->sb_feat.crcs_enabled &&
1849 cfg->blocksize < XFS_MIN_CRC_BLOCKSIZE) {
1850 fprintf(stderr,
1851_("Minimum block size for CRC enabled filesystems is %d bytes.\n"),
1852 XFS_MIN_CRC_BLOCKSIZE);
1853 usage();
1854 }
1855
1856}
1857
22319b56
DC
1858/*
1859 * Grab log sector size and validate.
1860 *
1861 * XXX: should we probe sector size on external log device rather than using
1862 * the data device sector size?
1863 */
1864static void
1865validate_log_sectorsize(
1866 struct mkfs_params *cfg,
1867 struct cli_params *cli,
1868 struct mkfs_default_params *dft)
1869{
1870
1871 if (cli->loginternal && cli->lsectorsize &&
1872 cli->lsectorsize != cfg->sectorsize) {
1873 fprintf(stderr,
1874_("Can't change sector size on internal log!\n"));
1875 usage();
1876 }
1877
1878 if (cli->lsectorsize)
1879 cfg->lsectorsize = cli->lsectorsize;
1880 else if (cli->loginternal)
1881 cfg->lsectorsize = cfg->sectorsize;
1882 else
1883 cfg->lsectorsize = dft->sectorsize;
1884 cfg->lsectorlog = libxfs_highbit32(cfg->lsectorsize);
1885
1886 if (cfg->lsectorsize < XFS_MIN_SECTORSIZE ||
1887 cfg->lsectorsize > XFS_MAX_SECTORSIZE ||
1888 cfg->lsectorsize > cfg->blocksize) {
1889 fprintf(stderr, _("illegal log sector size %d\n"),
1890 cfg->lsectorsize);
1891 usage();
1892 }
1893 if (cfg->lsectorsize > XFS_MIN_SECTORSIZE) {
1894 if (cli->sb_feat.log_version < 2) {
1895 /* user specified non-default log version */
1896 fprintf(stderr,
1897_("Version 1 logs do not support sector size %d\n"),
1898 cfg->lsectorsize);
1899 usage();
1900 }
1901 }
1902
1903 /* if lsu or lsunit was specified, automatically use v2 logs */
1904 if ((cli_opt_set(&lopts, L_SU) || cli_opt_set(&lopts, L_SUNIT)) &&
1905 cli->sb_feat.log_version == 1) {
1906 fprintf(stderr,
1907_("log stripe unit specified, using v2 logs\n"));
1908 cli->sb_feat.log_version = 2;
1909 }
1910
1911}
1912
a43e656b
DC
1913/*
1914 * Check that the incoming features make sense. The CLI structure was
1915 * initialised with the default values before parsing, so we can just
1916 * check it and copy it straight across to the cfg structure if it
1917 * checks out.
1918 */
1919static void
1920validate_sb_features(
1921 struct mkfs_params *cfg,
1922 struct cli_params *cli)
1923{
1924 /*
1925 * Now we have blocks and sector sizes set up, check parameters that are
1926 * no longer optional for CRC enabled filesystems. Catch them up front
1927 * here before doing anything else.
1928 */
1929 if (cli->sb_feat.crcs_enabled) {
1930 /* minimum inode size is 512 bytes, rest checked later */
1931 if (cli->inodesize &&
1932 cli->inodesize < (1 << XFS_DINODE_DFL_CRC_LOG)) {
1933 fprintf(stderr,
1934_("Minimum inode size for CRCs is %d bytes\n"),
1935 1 << XFS_DINODE_DFL_CRC_LOG);
1936 usage();
1937 }
1938
1939 /* inodes always aligned */
1940 if (!cli->sb_feat.inode_align) {
1941 fprintf(stderr,
1942_("Inodes always aligned for CRC enabled filesytems\n"));
1943 usage();
1944 }
1945
1946 /* lazy sb counters always on */
1947 if (!cli->sb_feat.lazy_sb_counters) {
1948 fprintf(stderr,
1949_("Lazy superblock counted always enabled for CRC enabled filesytems\n"));
1950 usage();
1951 }
1952
1953 /* version 2 logs always on */
1954 if (cli->sb_feat.log_version != 2) {
1955 fprintf(stderr,
1956_("V2 logs always enabled for CRC enabled filesytems\n"));
1957 usage();
1958 }
1959
1960 /* attr2 always on */
1961 if (cli->sb_feat.attr_version != 2) {
1962 fprintf(stderr,
1963_("V2 attribute format always enabled on CRC enabled filesytems\n"));
1964 usage();
1965 }
1966
1967 /* 32 bit project quota always on */
1968 /* attr2 always on */
639d0b0b 1969 if (!cli->sb_feat.projid32bit) {
a43e656b
DC
1970 fprintf(stderr,
1971_("32 bit Project IDs always enabled on CRC enabled filesytems\n"));
1972 usage();
1973 }
1974
1975 /* ftype always on */
1976 if (!cli->sb_feat.dirftype) {
1977 fprintf(stderr,
1978_("Directory ftype field always enabled on CRC enabled filesytems\n"));
1979 usage();
1980 }
1981
1982 } else {
1983 /*
1984 * The kernel doesn't currently support crc=0,finobt=1
1985 * filesystems. If crcs are not enabled and the user has not
1986 * explicitly turned finobt on, then silently turn it off to
1987 * avoid an unnecessary warning.
1988 * If the user explicitly tried to use crc=0,finobt=1,
1989 * then issue an error.
1990 * The same is also for sparse inodes.
1991 */
1992 if (cli->sb_feat.finobt && cli_opt_set(&mopts, M_FINOBT)) {
1993 fprintf(stderr,
1994_("finobt not supported without CRC support\n"));
1995 usage();
1996 }
1997 cli->sb_feat.finobt = false;
1998
1999 if (cli->sb_feat.spinodes) {
2000 fprintf(stderr,
2001_("sparse inodes not supported without CRC support\n"));
2002 usage();
2003 }
2004 cli->sb_feat.spinodes = false;
2005
2006 if (cli->sb_feat.rmapbt) {
2007 fprintf(stderr,
2008_("rmapbt not supported without CRC support\n"));
2009 usage();
2010 }
2011 cli->sb_feat.rmapbt = false;
2012
2013 if (cli->sb_feat.reflink) {
2014 fprintf(stderr,
2015_("reflink not supported without CRC support\n"));
2016 usage();
2017 }
2018 cli->sb_feat.reflink = false;
2019 }
2020
2021 if ((cli->fsx.fsx_xflags & FS_XFLAG_COWEXTSIZE) &&
2022 !cli->sb_feat.reflink) {
2023 fprintf(stderr,
2024_("cowextsize not supported without reflink support\n"));
2025 usage();
2026 }
2027
2028 if (cli->sb_feat.rmapbt && cli->xi->rtname) {
2029 fprintf(stderr,
2030_("rmapbt not supported with realtime devices\n"));
2031 usage();
2032 cli->sb_feat.rmapbt = false;
2033 }
2034
2035 /*
2036 * Copy features across to config structure now.
2037 */
2038 cfg->sb_feat = cli->sb_feat;
2039 if (!platform_uuid_is_null(&cli->uuid))
2040 platform_uuid_copy(&cfg->uuid, &cli->uuid);
2041}
2042
fdea8fbc
DC
2043static void
2044validate_dirblocksize(
2045 struct mkfs_params *cfg,
2046 struct cli_params *cli)
2047{
2048
2049 if (cli->dirblocksize)
2050 cfg->dirblocksize = getnum(cli->dirblocksize, &nopts, N_SIZE);
fdea8fbc
DC
2051
2052 if (cfg->dirblocksize) {
2053 if (cfg->dirblocksize < cfg->blocksize ||
2054 cfg->dirblocksize > XFS_MAX_BLOCKSIZE) {
2055 fprintf(stderr, _("illegal directory block size %d\n"),
2056 cfg->dirblocksize);
2057 usage();
2058 }
2059 cfg->dirblocklog = libxfs_highbit32(cfg->dirblocksize);
2060 return;
2061 }
2062
2063 /* use default size based on current block size */
2064 if (cfg->blocksize < (1 << XFS_MIN_REC_DIRSIZE))
2065 cfg->dirblocklog = XFS_MIN_REC_DIRSIZE;
2066 else
2067 cfg->dirblocklog = cfg->blocklog;
2068 cfg->dirblocksize = 1 << cfg->dirblocklog;
2069}
2070
8fe29028
DC
2071static void
2072validate_inodesize(
2073 struct mkfs_params *cfg,
2074 struct cli_params *cli)
2075{
2076
2077 if (cli->inopblock)
2078 cfg->inodelog = cfg->blocklog - libxfs_highbit32(cli->inopblock);
2079 else if (cli->inodesize)
2080 cfg->inodelog = libxfs_highbit32(cli->inodesize);
2081 else if (cfg->sb_feat.crcs_enabled)
2082 cfg->inodelog = XFS_DINODE_DFL_CRC_LOG;
2083 else
2084 cfg->inodelog = XFS_DINODE_DFL_LOG;
2085
2086 cfg->inodesize = 1 << cfg->inodelog;
2087 cfg->inopblock = cfg->blocksize / cfg->inodesize;
2088
2089 /* input parsing has already validated non-crc inode size range */
2090 if (cfg->sb_feat.crcs_enabled &&
2091 cfg->inodelog < XFS_DINODE_DFL_CRC_LOG) {
2092 fprintf(stderr,
2093 _("Minimum inode size for CRCs is %d bytes\n"),
2094 1 << XFS_DINODE_DFL_CRC_LOG);
2095 usage();
2096 }
2097
2098 if (cfg->inodesize > cfg->blocksize / XFS_MIN_INODE_PERBLOCK ||
2099 cfg->inopblock < XFS_MIN_INODE_PERBLOCK ||
2100 cfg->inodesize < XFS_DINODE_MIN_SIZE ||
2101 cfg->inodesize > XFS_DINODE_MAX_SIZE) {
2102 int maxsz;
2103
2104 fprintf(stderr, _("illegal inode size %d\n"), cfg->inodesize);
2105 maxsz = MIN(cfg->blocksize / XFS_MIN_INODE_PERBLOCK,
2106 XFS_DINODE_MAX_SIZE);
2107 if (XFS_DINODE_MIN_SIZE == maxsz)
2108 fprintf(stderr,
2109 _("allowable inode size with %d byte blocks is %d\n"),
2110 cfg->blocksize, XFS_DINODE_MIN_SIZE);
2111 else
2112 fprintf(stderr,
2113 _("allowable inode size with %d byte blocks is between %d and %d\n"),
2114 cfg->blocksize, XFS_DINODE_MIN_SIZE, maxsz);
2115 exit(1);
2116 }
2117}
2118
e24dfa22
DC
2119static xfs_rfsblock_t
2120calc_dev_size(
2121 char *size,
2122 struct mkfs_params *cfg,
2123 struct opt_params *opts,
2124 int sizeopt,
2125 char *type)
2126{
2127 uint64_t dbytes;
2128 xfs_rfsblock_t dblocks;
2129
2130 if (!size)
2131 return 0;
2132
2133 dbytes = getnum(size, opts, sizeopt);
2134 if (dbytes % XFS_MIN_BLOCKSIZE) {
2135 fprintf(stderr,
2136 _("illegal %s length %lld, not a multiple of %d\n"),
2137 type, (long long)dbytes, XFS_MIN_BLOCKSIZE);
2138 usage();
2139 }
2140 dblocks = (xfs_rfsblock_t)(dbytes >> cfg->blocklog);
2141 if (dbytes % cfg->blocksize) {
2142 fprintf(stderr,
2143_("warning: %s length %lld not a multiple of %d, truncated to %lld\n"),
2144 type, (long long)dbytes, cfg->blocksize,
2145 (long long)(dblocks << cfg->blocklog));
2146 }
2147 return dblocks;
2148}
2149
80b154f7
DC
2150static void
2151validate_rtextsize(
2152 struct mkfs_params *cfg,
2153 struct cli_params *cli,
2154 struct fs_topology *ft)
2155{
2156 uint64_t rtextbytes;
2157
2158 /*
2159 * If specified, check rt extent size against its constraints.
2160 */
2161 if (cli->rtextsize) {
2162
2163 rtextbytes = getnum(cli->rtextsize, &ropts, R_EXTSIZE);
2164 if (rtextbytes % cfg->blocksize) {
2165 fprintf(stderr,
2166 _("illegal rt extent size %lld, not a multiple of %d\n"),
2167 (long long)rtextbytes, cfg->blocksize);
2168 usage();
2169 }
2170 cfg->rtextblocks = (xfs_extlen_t)(rtextbytes >> cfg->blocklog);
2171 } else {
2172 /*
2173 * If realtime extsize has not been specified by the user,
2174 * and the underlying volume is striped, then set rtextblocks
2175 * to the stripe width.
2176 */
2177 uint64_t rswidth;
2178
2179 if (!cfg->sb_feat.nortalign && !cli->xi->risfile &&
2180 !(!cli->rtsize && cli->xi->disfile))
2181 rswidth = ft->rtswidth;
2182 else
2183 rswidth = 0;
2184
2185 /* check that rswidth is a multiple of fs blocksize */
2186 if (!cfg->sb_feat.nortalign && rswidth &&
2187 !(BBTOB(rswidth) % cfg->blocksize)) {
2188 rswidth = DTOBT(rswidth, cfg->blocklog);
2189 rtextbytes = rswidth << cfg->blocklog;
2190 if (rtextbytes > XFS_MIN_RTEXTSIZE &&
2191 rtextbytes <= XFS_MAX_RTEXTSIZE) {
2192 cfg->rtextblocks = rswidth;
2193 }
2194 }
2195 if (!cfg->rtextblocks) {
2196 cfg->rtextblocks = (cfg->blocksize < XFS_MIN_RTEXTSIZE)
2197 ? XFS_MIN_RTEXTSIZE >> cfg->blocklog
2198 : 1;
2199 }
2200 }
2201 ASSERT(cfg->rtextblocks);
2202}
2203
2f44b1b0
DC
2204/*
2205 * Validate the configured stripe geometry, or is none is specified, pull
2206 * the configuration from the underlying device.
2207 *
2208 * CLI parameters come in as different units, go out as filesystem blocks.
2209 */
2210static void
2211calc_stripe_factors(
2212 struct mkfs_params *cfg,
2213 struct cli_params *cli,
2214 struct fs_topology *ft)
2215{
91c7d131 2216 long long int big_dswidth;
2f44b1b0
DC
2217 int dsunit = 0;
2218 int dswidth = 0;
2219 int lsunit = 0;
2220 int dsu = 0;
2221 int dsw = 0;
2222 int lsu = 0;
2223 bool use_dev = false;
2224
2225 if (cli_opt_set(&dopts, D_SUNIT))
2226 dsunit = cli->dsunit;
2227 if (cli_opt_set(&dopts, D_SWIDTH))
2228 dswidth = cli->dswidth;
2229
2230 if (cli_opt_set(&dopts, D_SU))
2231 dsu = getnum(cli->dsu, &dopts, D_SU);
2232 if (cli_opt_set(&dopts, D_SW))
2233 dsw = cli->dsw;
2234
2235 /* data sunit/swidth options */
2236 if ((dsunit && !dswidth) || (!dsunit && dswidth)) {
2237 fprintf(stderr,
2238_("both data sunit and data swidth options must be specified\n"));
2239 usage();
2240 }
2241
2242 /* convert dsu/dsw to dsunit/dswidth and use them from now on */
2243 if (dsu || dsw) {
2244 if ((dsu && !dsw) || (!dsu && dsw)) {
2245 fprintf(stderr,
2246_("both data su and data sw options must be specified\n"));
2247 usage();
2248 }
2249
2250 if (dsu % cfg->sectorsize) {
2251 fprintf(stderr,
2252_("data su must be a multiple of the sector size (%d)\n"), cfg->sectorsize);
2253 usage();
2254 }
2255
2256 dsunit = (int)BTOBBT(dsu);
91c7d131
DW
2257 big_dswidth = (long long int)dsunit * dsw;
2258 if (big_dswidth > INT_MAX) {
2259 fprintf(stderr,
2260_("data stripe width (%lld) is too large of a multiple of the data stripe unit (%d)\n"),
2261 big_dswidth, dsunit);
2262 usage();
2263 }
2264 dswidth = big_dswidth;
2f44b1b0
DC
2265 }
2266
2267 if (dsunit && (dswidth % dsunit != 0)) {
2268 fprintf(stderr,
2269_("data stripe width (%d) must be a multiple of the data stripe unit (%d)\n"),
2270 dswidth, dsunit);
2271 usage();
2272 }
2273
2274 /* If sunit & swidth were manually specified as 0, same as noalign */
2275 if ((cli_opt_set(&dopts, D_SUNIT) || cli_opt_set(&dopts, D_SU)) &&
2276 !dsunit && !dswidth)
2277 cfg->sb_feat.nodalign = true;
2278
2279 /* if we are not using alignment, don't apply device defaults */
2280 if (cfg->sb_feat.nodalign) {
2281 cfg->dsunit = 0;
2282 cfg->dswidth = 0;
2283 goto check_lsunit;
2284 }
2285
2286 /* if no stripe config set, use the device default */
2287 if (!dsunit) {
2288 dsunit = ft->dsunit;
2289 dswidth = ft->dswidth;
2290 use_dev = true;
2291 } else {
2292 /* check and warn is alignment is sub-optimal */
2293 if (ft->dsunit && ft->dsunit != dsunit) {
2294 fprintf(stderr,
2295_("%s: Specified data stripe unit %d is not the same as the volume stripe unit %d\n"),
2296 progname, dsunit, ft->dsunit);
2297 }
2298 if (ft->dswidth && ft->dswidth != dswidth) {
2299 fprintf(stderr,
2300_("%s: Specified data stripe width %d is not the same as the volume stripe width %d\n"),
2301 progname, dswidth, ft->dswidth);
2302 }
2303 }
2304
2305 /*
2306 * now we have our stripe config, check it's a multiple of block
2307 * size.
2308 */
2309 if ((BBTOB(dsunit) % cfg->blocksize) ||
2310 (BBTOB(dswidth) % cfg->blocksize)) {
2311 /*
2312 * If we are using device defaults, just clear them and we're
2313 * good to go. Otherwise bail out with an error.
2314 */
2315 if (!use_dev) {
2316 fprintf(stderr,
2317_("%s: Stripe unit(%d) or stripe width(%d) is not a multiple of the block size(%d)\n"),
2318 progname, BBTOB(dsunit), BBTOB(dswidth),
2319 cfg->blocksize);
2320 exit(1);
2321 }
2322 dsunit = 0;
2323 dswidth = 0;
2324 cfg->sb_feat.nodalign = true;
2325 }
2326
2327 /* convert from 512 byte blocks to fs blocksize */
2328 cfg->dsunit = DTOBT(dsunit, cfg->blocklog);
2329 cfg->dswidth = DTOBT(dswidth, cfg->blocklog);
2330
2331check_lsunit:
2332 /* log sunit options */
2333 if (cli_opt_set(&lopts, L_SUNIT))
2334 lsunit = cli->lsunit;
2335 else if (cli_opt_set(&lopts, L_SU))
2336 lsu = getnum(cli->lsu, &lopts, L_SU);
2337 else if (cfg->lsectorsize > XLOG_HEADER_SIZE)
2338 lsu = cfg->blocksize; /* lsunit matches filesystem block size */
2339
2340 if (lsu) {
2341 /* verify if lsu is a multiple block size */
2342 if (lsu % cfg->blocksize != 0) {
2343 fprintf(stderr,
2344 _("log stripe unit (%d) must be a multiple of the block size (%d)\n"),
2345 lsu, cfg->blocksize);
2346 usage();
2347 }
2348 lsunit = (int)BTOBBT(lsu);
2349 }
2350 if (BBTOB(lsunit) % cfg->blocksize != 0) {
2351 fprintf(stderr,
2352_("log stripe unit (%d) must be a multiple of the block size (%d)\n"),
2353 BBTOB(lsunit), cfg->blocksize);
2354 usage();
2355 }
2356
2357 /*
2358 * check that log sunit is modulo fsblksize or default it to dsunit.
2359 */
2360 if (lsunit) {
2361 /* convert from 512 byte blocks to fs blocks */
2362 cfg->lsunit = DTOBT(lsunit, cfg->blocklog);
2363 } else if (cfg->sb_feat.log_version == 2 &&
2364 cfg->loginternal && cfg->dsunit) {
2365 /* lsunit and dsunit now in fs blocks */
2366 cfg->lsunit = cfg->dsunit;
2367 }
2368
2369 if (cfg->sb_feat.log_version == 2 &&
2370 cfg->lsunit * cfg->blocksize > 256 * 1024) {
2371 /* Warn only if specified on commandline */
2372 if (cli->lsu || cli->lsunit != -1) {
2373 fprintf(stderr,
2374_("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"
2375 "log stripe unit adjusted to 32KiB\n"),
2376 (cfg->lsunit * cfg->blocksize));
2377 }
2378 /* XXX: 64k block size? */
2379 cfg->lsunit = (32 * 1024) / cfg->blocksize;
2380 }
2381
2382}
2383
379f01d2
DC
2384static void
2385open_devices(
2386 struct mkfs_params *cfg,
2387 struct libxfs_xinit *xi,
2388 bool discard)
2389{
2390 uint64_t sector_mask;
2391
2392 /*
2393 * Initialize. This will open the log and rt devices as well.
2394 */
2395 xi->setblksize = cfg->sectorsize;
2396 if (!libxfs_init(xi))
2397 usage();
2398 if (!xi->ddev) {
2399 fprintf(stderr, _("no device name given in argument list\n"));
2400 usage();
2401 }
2402
2403 /*
2404 * Ok, Linux only has a 1024-byte resolution on device _size_,
2405 * and the sizes below are in basic 512-byte blocks,
2406 * so if we have (size % 2), on any partition, we can't get
2407 * to the last 512 bytes. The same issue exists for larger
2408 * sector sizes - we cannot write past the last sector.
2409 *
2410 * So, we reduce the size (in basic blocks) to a perfect
2411 * multiple of the sector size, or 1024, whichever is larger.
2412 */
2413 sector_mask = (uint64_t)-1 << (MAX(cfg->sectorlog, 10) - BBSHIFT);
2414 xi->dsize &= sector_mask;
2415 xi->rtsize &= sector_mask;
2416 xi->logBBsize &= (uint64_t)-1 << (MAX(cfg->lsectorlog, 10) - BBSHIFT);
2417
2418
2419 if (!discard)
2420 return;
2421
2422 if (!xi->disfile)
2423 discard_blocks(xi->ddev, xi->dsize);
2424 if (xi->rtdev && !xi->risfile)
2425 discard_blocks(xi->rtdev, xi->rtsize);
2426 if (xi->logdev && xi->logdev != xi->ddev && !xi->lisfile)
2427 discard_blocks(xi->logdev, xi->logBBsize);
2428}
2429
dd5ac314
DC
2430static void
2431validate_datadev(
2432 struct mkfs_params *cfg,
2433 struct cli_params *cli)
2434{
2435 struct libxfs_xinit *xi = cli->xi;
2436
2437 if (!xi->dsize) {
2438 /*
2439 * if the device is a file, we can't validate the size here.
2440 * Instead, the file will be truncated to the correct length
2441 * later on. if it's not a file, we've got a dud device.
2442 */
2443 if (!xi->disfile) {
2444 fprintf(stderr, _("can't get size of data subvolume\n"));
2445 usage();
2446 }
2447 ASSERT(cfg->dblocks);
2448 } else if (cfg->dblocks) {
2449 /* check the size fits into the underlying device */
2450 if (cfg->dblocks > DTOBT(xi->dsize, cfg->blocklog)) {
2451 fprintf(stderr,
2452_("size %s specified for data subvolume is too large, maximum is %lld blocks\n"),
2453 cli->dsize,
2454 (long long)DTOBT(xi->dsize, cfg->blocklog));
2455 usage();
2456 }
2457 } else {
2458 /* no user size, so use the full block device */
2459 cfg->dblocks = DTOBT(xi->dsize, cfg->blocklog);
2460 }
2461
2462 if (cfg->dblocks < XFS_MIN_DATA_BLOCKS) {
2463 fprintf(stderr,
2464_("size %lld of data subvolume is too small, minimum %d blocks\n"),
2465 (long long)cfg->dblocks, XFS_MIN_DATA_BLOCKS);
2466 usage();
2467 }
2468
2469 if (xi->dbsize > cfg->sectorsize) {
2470 fprintf(stderr, _(
2471"Warning: the data subvolume sector size %u is less than the sector size \n\
2472reported by the device (%u).\n"),
2473 cfg->sectorsize, xi->dbsize);
2474 }
2475}
2476
90b7e13d
DC
2477/*
2478 * This is more complex than it needs to be because we still support volume
2479 * based external logs. They are only discovered *after* the devices have been
2480 * opened, hence the crazy "is this really an internal log" checks here.
2481 */
2482static void
2483validate_logdev(
2484 struct mkfs_params *cfg,
2485 struct cli_params *cli,
2486 char **devname)
2487{
2488 struct libxfs_xinit *xi = cli->xi;
2489
2490 *devname = NULL;
2491
2492 /* check for volume log first */
2493 if (cli->loginternal && xi->volname && xi->logdev) {
2494 *devname = _("volume log");
2495 cfg->loginternal = false;
2496 } else
2497 cfg->loginternal = cli->loginternal;
2498
2499 /* now run device checks */
2500 if (cfg->loginternal) {
2501 if (xi->logdev) {
2502 fprintf(stderr,
2503_("can't have both external and internal logs\n"));
2504 usage();
2505 }
2506
2507 /*
2508 * if no sector size has been specified on the command line,
2509 * use what has been configured and validated for the data
2510 * device.
2511 */
2512 if (!cli->lsectorsize) {
2513 cfg->lsectorsize = cfg->sectorsize;
2514 cfg->lsectorlog = cfg->sectorlog;
2515 }
2516
2517 if (cfg->sectorsize != cfg->lsectorsize) {
2518 fprintf(stderr,
2519_("data and log sector sizes must be equal for internal logs\n"));
2520 usage();
2521 }
2522 if (cli->logsize && cfg->logblocks >= cfg->dblocks) {
2523 fprintf(stderr,
2524_("log size %lld too large for internal log\n"),
2525 (long long)cfg->logblocks);
2526 usage();
2527 }
2528 *devname = _("internal log");
2529 return;
2530 }
2531
2532 /* External/log subvolume checks */
2533 if (xi->logname)
2534 *devname = xi->logname;
2535 if (!*devname || !xi->logdev) {
2536 fprintf(stderr, _("no log subvolume or external log.\n"));
2537 usage();
2538 }
2539
2540 if (!cfg->logblocks) {
2541 if (xi->logBBsize == 0) {
2542 fprintf(stderr,
2543_("unable to get size of the log subvolume.\n"));
2544 usage();
2545 }
2546 cfg->logblocks = DTOBT(xi->logBBsize, cfg->blocklog);
2547 } else if (cfg->logblocks > DTOBT(xi->logBBsize, cfg->blocklog)) {
2548 fprintf(stderr,
2549_("size %s specified for log subvolume is too large, maximum is %lld blocks\n"),
2550 cli->logsize,
2551 (long long)DTOBT(xi->logBBsize, cfg->blocklog));
2552 usage();
2553 }
2554
2555 if (xi->lbsize > cfg->lsectorsize) {
2556 fprintf(stderr, _(
2557"Warning: the log subvolume sector size %u is less than the sector size\n\
2558reported by the device (%u).\n"),
2559 cfg->lsectorsize, xi->lbsize);
2560 }
2561}
2562
7a9af89a
DC
2563static void
2564validate_rtdev(
2565 struct mkfs_params *cfg,
2566 struct cli_params *cli,
2567 char **devname)
2568{
2569 struct libxfs_xinit *xi = cli->xi;
2570
2571 *devname = NULL;
2572
2573 if (!xi->rtdev) {
2574 if (cli->rtsize) {
2575 fprintf(stderr,
2576_("size specified for non-existent rt subvolume\n"));
2577 usage();
2578 }
2579
2580 *devname = _("none");
2581 cfg->rtblocks = 0;
2582 cfg->rtextents = 0;
2583 cfg->rtbmblocks = 0;
2584 return;
2585 }
2586 if (!xi->rtsize) {
2587 fprintf(stderr, _("Invalid zero length rt subvolume found\n"));
2588 usage();
2589 }
2590
2591 /* volume rtdev */
2592 if (xi->volname)
2593 *devname = _("volume rt");
2594 else
2595 *devname = xi->rtname;
2596
2597 if (cli->rtsize) {
2598 if (cfg->rtblocks > DTOBT(xi->rtsize, cfg->blocklog)) {
2599 fprintf(stderr,
2600_("size %s specified for rt subvolume is too large, maxi->um is %lld blocks\n"),
2601 cli->rtsize,
2602 (long long)DTOBT(xi->rtsize, cfg->blocklog));
2603 usage();
2604 }
2605 if (xi->rtbsize > cfg->sectorsize) {
2606 fprintf(stderr, _(
2607"Warning: the realtime subvolume sector size %u is less than the sector size\n\
2608reported by the device (%u).\n"),
2609 cfg->sectorsize, xi->rtbsize);
2610 }
2611 } else {
2612 /* grab volume size */
2613 cfg->rtblocks = DTOBT(xi->rtsize, cfg->blocklog);
2614 }
2615
2616 cfg->rtextents = cfg->rtblocks / cfg->rtextblocks;
2617 cfg->rtbmblocks = (xfs_extlen_t)howmany(cfg->rtextents,
2618 NBBY * cfg->blocksize);
2619}
2620
1de01446
DC
2621static void
2622calculate_initial_ag_geometry(
2623 struct mkfs_params *cfg,
2624 struct cli_params *cli)
2625{
2626 if (cli->agsize) { /* User-specified AG size */
2627 cfg->agsize = getnum(cli->agsize, &dopts, D_AGSIZE);
2628
2629 /*
2630 * Check specified agsize is a multiple of blocksize.
2631 */
2632 if (cfg->agsize % cfg->blocksize) {
2633 fprintf(stderr,
2634_("agsize (%s) not a multiple of fs blk size (%d)\n"),
2635 cli->agsize, cfg->blocksize);
2636 usage();
2637 }
2638 cfg->agsize /= cfg->blocksize;
2639 cfg->agcount = cfg->dblocks / cfg->agsize +
2640 (cfg->dblocks % cfg->agsize != 0);
2641
2642 } else if (cli->agcount) { /* User-specified AG count */
2643 cfg->agcount = cli->agcount;
2644 cfg->agsize = cfg->dblocks / cfg->agcount +
2645 (cfg->dblocks % cfg->agcount != 0);
2646 } else {
2647 calc_default_ag_geometry(cfg->blocklog, cfg->dblocks,
2648 cfg->dsunit, &cfg->agsize,
2649 &cfg->agcount);
2650 }
2651}
2652
051b4e37
DC
2653/*
2654 * Align the AG size to stripe geometry. If this fails and we are using
2655 * discovered stripe geometry, tell the caller to clear the stripe geometry.
2656 * Otherwise, set the aligned geometry (valid or invalid!) so that the
2657 * validation call will fail and exit.
2658 */
2659static void
2660align_ag_geometry(
2661 struct mkfs_params *cfg)
2662{
2663 uint64_t tmp_agsize;
2664 int dsunit = cfg->dsunit;
2665
2666 if (!dsunit)
2667 goto validate;
2668
2669 /*
2670 * agsize is not a multiple of dsunit
2671 */
2672 if ((cfg->agsize % dsunit) != 0) {
2673 /*
2674 * Round up to stripe unit boundary. Also make sure
2675 * that agsize is still larger than
2676 * XFS_AG_MIN_BLOCKS(blocklog)
2677 */
2678 tmp_agsize = ((cfg->agsize + dsunit - 1) / dsunit) * dsunit;
2679 /*
2680 * Round down to stripe unit boundary if rounding up
2681 * created an AG size that is larger than the AG max.
2682 */
2683 if (tmp_agsize > XFS_AG_MAX_BLOCKS(cfg->blocklog))
2684 tmp_agsize = (cfg->agsize / dsunit) * dsunit;
2685
2686 if (tmp_agsize < XFS_AG_MIN_BLOCKS(cfg->blocklog) &&
2687 tmp_agsize > XFS_AG_MAX_BLOCKS(cfg->blocklog)) {
2688
2689 /*
2690 * If the AG size is invalid and we are using device
2691 * probed stripe alignment, just clear the alignment
2692 * and continue on.
2693 */
2694 if (!cli_opt_set(&dopts, D_SUNIT) &&
2695 !cli_opt_set(&dopts, D_SU)) {
2696 cfg->dsunit = 0;
2697 cfg->dswidth = 0;
2698 goto validate;
2699 }
2700 /*
2701 * set the agsize to the invalid value so the following
2702 * validation of the ag will fail and print a nice error
2703 * and exit.
2704 */
2705 cfg->agsize = tmp_agsize;
2706 goto validate;
2707 }
2708
2709 /* update geometry to be stripe unit aligned */
2710 cfg->agsize = tmp_agsize;
2711 if (!cli_opt_set(&dopts, D_AGCOUNT))
2712 cfg->agcount = cfg->dblocks / cfg->agsize +
2713 (cfg->dblocks % cfg->agsize != 0);
2714 if (cli_opt_set(&dopts, D_AGSIZE))
2715 fprintf(stderr,
2716_("agsize rounded to %lld, sunit = %d\n"),
2717 (long long)cfg->agsize, dsunit);
2718 }
2719
2720 if ((cfg->agsize % cfg->dswidth) == 0 &&
2721 cfg->dswidth != cfg->dsunit &&
2722 cfg->agcount > 1) {
2723
2724 if (cli_opt_set(&dopts, D_AGCOUNT) ||
2725 cli_opt_set(&dopts, D_AGSIZE)) {
2726 fprintf(stderr, _(
2727"Warning: AG size is a multiple of stripe width. This can cause performance\n\
2728problems by aligning all AGs on the same disk. To avoid this, run mkfs with\n\
2729an AG size that is one stripe unit smaller or larger, for example %llu.\n"),
2730 (unsigned long long)cfg->agsize - dsunit);
2731 goto validate;
2732 }
2733
2734 /*
2735 * This is a non-optimal configuration because all AGs start on
2736 * the same disk in the stripe. Changing the AG size by one
2737 * sunit will guarantee that this does not happen.
2738 */
2739 tmp_agsize = cfg->agsize - dsunit;
2740 if (tmp_agsize < XFS_AG_MIN_BLOCKS(cfg->blocklog)) {
2741 tmp_agsize = cfg->agsize + dsunit;
2742 if (cfg->dblocks < cfg->agsize) {
2743 /* oh well, nothing to do */
2744 tmp_agsize = cfg->agsize;
2745 }
2746 }
2747
2748 cfg->agsize = tmp_agsize;
2749 cfg->agcount = cfg->dblocks / cfg->agsize +
2750 (cfg->dblocks % cfg->agsize != 0);
2751 }
2752
2753validate:
2754 /*
2755 * If the last AG is too small, reduce the filesystem size
2756 * and drop the blocks.
2757 */
2758 if (cfg->dblocks % cfg->agsize != 0 &&
2759 (cfg->dblocks % cfg->agsize < XFS_AG_MIN_BLOCKS(cfg->blocklog))) {
2760 ASSERT(!cli_opt_set(&dopts, D_AGCOUNT));
2761 cfg->dblocks = (xfs_rfsblock_t)((cfg->agcount - 1) * cfg->agsize);
2762 cfg->agcount--;
2763 ASSERT(cfg->agcount != 0);
2764 }
2765
2766 validate_ag_geometry(cfg->blocklog, cfg->dblocks,
2767 cfg->agsize, cfg->agcount);
2768}
2769
d7240c96
DC
2770static void
2771calculate_imaxpct(
2772 struct mkfs_params *cfg,
2773 struct cli_params *cli)
2774{
2775 cfg->imaxpct = cli->imaxpct;
2776 if (cfg->imaxpct)
2777 return;
2778
2779 /*
2780 * This returns the % of the disk space that is used for
2781 * inodes, it changes relatively to the FS size:
2782 * - over 50 TB, use 1%,
2783 * - 1TB - 50 TB, use 5%,
2784 * - under 1 TB, use XFS_DFL_IMAXIMUM_PCT (25%).
2785 */
2786
2787 if (cfg->dblocks < TERABYTES(1, cfg->blocklog))
2788 cfg->imaxpct = XFS_DFL_IMAXIMUM_PCT;
2789 else if (cfg->dblocks < TERABYTES(50, cfg->blocklog))
2790 cfg->imaxpct = 5;
2791 else
2792 cfg->imaxpct = 1;
2793}
2794
befcd768
DC
2795/*
2796 * Set up the initial state of the superblock so we can start using the
2797 * libxfs geometry macros.
2798 */
2799static void
2800sb_set_features(
2801 struct mkfs_params *cfg,
2802 struct xfs_sb *sbp)
2803{
2804 struct sb_feat_args *fp = &cfg->sb_feat;
2805
2806 sbp->sb_versionnum = XFS_DFL_SB_VERSION_BITS;
2807 if (fp->crcs_enabled)
2808 sbp->sb_versionnum |= XFS_SB_VERSION_5;
2809 else
2810 sbp->sb_versionnum |= XFS_SB_VERSION_4;
2811
2812 if (fp->inode_align) {
2813 int cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
2814
2815 sbp->sb_versionnum |= XFS_SB_VERSION_ALIGNBIT;
2816 if (cfg->sb_feat.crcs_enabled)
2817 cluster_size *= cfg->inodesize / XFS_DINODE_MIN_SIZE;
2818 sbp->sb_inoalignmt = cluster_size >> cfg->blocklog;
2819 } else
2820 sbp->sb_inoalignmt = 0;
2821
2822 if (cfg->dsunit)
2823 sbp->sb_versionnum |= XFS_SB_VERSION_DALIGNBIT;
2824 if (fp->log_version == 2)
2825 sbp->sb_versionnum |= XFS_SB_VERSION_LOGV2BIT;
2826 if (fp->attr_version == 1)
2827 sbp->sb_versionnum |= XFS_SB_VERSION_ATTRBIT;
2828 if (fp->nci)
2829 sbp->sb_versionnum |= XFS_SB_VERSION_BORGBIT;
2830
2831 if (cfg->sectorsize > BBSIZE || cfg->lsectorsize > BBSIZE) {
2832 sbp->sb_versionnum |= XFS_SB_VERSION_SECTORBIT;
2833 sbp->sb_logsectlog = (uint8_t)cfg->lsectorlog;
2834 sbp->sb_logsectsize = (uint16_t)cfg->lsectorsize;
2835 } else {
2836 sbp->sb_logsectlog = 0;
2837 sbp->sb_logsectsize = 0;
2838 }
2839
2840 sbp->sb_features2 = 0;
2841 if (fp->lazy_sb_counters)
2842 sbp->sb_features2 |= XFS_SB_VERSION2_LAZYSBCOUNTBIT;
639d0b0b 2843 if (fp->projid32bit)
befcd768
DC
2844 sbp->sb_features2 |= XFS_SB_VERSION2_PROJID32BIT;
2845 if (fp->parent_pointers)
2846 sbp->sb_features2 |= XFS_SB_VERSION2_PARENTBIT;
2847 if (fp->crcs_enabled)
2848 sbp->sb_features2 |= XFS_SB_VERSION2_CRCBIT;
2849 if (fp->attr_version == 2)
2850 sbp->sb_features2 |= XFS_SB_VERSION2_ATTR2BIT;
2851
2852 /* v5 superblocks have their own feature bit for dirftype */
2853 if (fp->dirftype && !fp->crcs_enabled)
2854 sbp->sb_features2 |= XFS_SB_VERSION2_FTYPE;
2855
2856 /* update whether extended features are in use */
2857 if (sbp->sb_features2 != 0)
2858 sbp->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
2859
2860 /*
2861 * Due to a structure alignment issue, sb_features2 ended up in one
2862 * of two locations, the second "incorrect" location represented by
2863 * the sb_bad_features2 field. To avoid older kernels mounting
2864 * filesystems they shouldn't, set both field to the same value.
2865 */
2866 sbp->sb_bad_features2 = sbp->sb_features2;
2867
2868 if (!fp->crcs_enabled)
2869 return;
2870
2871 /* default features for v5 filesystems */
2872 sbp->sb_features_compat = 0;
2873 sbp->sb_features_ro_compat = 0;
2874 sbp->sb_features_incompat = XFS_SB_FEAT_INCOMPAT_FTYPE;
2875 sbp->sb_features_log_incompat = 0;
2876
2877 if (fp->finobt)
2878 sbp->sb_features_ro_compat = XFS_SB_FEAT_RO_COMPAT_FINOBT;
2879 if (fp->rmapbt)
2880 sbp->sb_features_ro_compat |= XFS_SB_FEAT_RO_COMPAT_RMAPBT;
2881 if (fp->reflink)
2882 sbp->sb_features_ro_compat |= XFS_SB_FEAT_RO_COMPAT_REFLINK;
2883
2884 /*
2885 * Sparse inode chunk support has two main inode alignment requirements.
2886 * First, sparse chunk alignment must match the cluster size. Second,
2887 * full chunk alignment must match the inode chunk size.
2888 *
2889 * Copy the already calculated/scaled inoalignmt to spino_align and
2890 * update the former to the full inode chunk size.
2891 */
2892 if (fp->spinodes) {
2893 sbp->sb_spino_align = sbp->sb_inoalignmt;
2894 sbp->sb_inoalignmt = XFS_INODES_PER_CHUNK *
2895 cfg->inodesize >> cfg->blocklog;
2896 sbp->sb_features_incompat |= XFS_SB_FEAT_INCOMPAT_SPINODES;
2897 }
2898
2899}
2900
e3bc8390
DC
2901/*
2902 * Make sure that the log size is a multiple of the stripe unit
2903 */
2904static void
2905align_log_size(
2906 struct mkfs_params *cfg,
2907 int sunit)
2908{
2909 uint64_t tmp_logblocks;
2910
2911 /* nothing to do if it's already aligned. */
2912 if ((cfg->logblocks % sunit) == 0)
2913 return;
2914
2915 if (cli_opt_set(&lopts, L_SIZE)) {
2916 fprintf(stderr,
2917_("log size %lld is not a multiple of the log stripe unit %d\n"),
2918 (long long) cfg->logblocks, sunit);
2919 usage();
2920 }
2921
2922 tmp_logblocks = ((cfg->logblocks + (sunit - 1)) / sunit) * sunit;
2923
2924 /* If the log is too large, round down instead of round up */
2925 if ((tmp_logblocks > XFS_MAX_LOG_BLOCKS) ||
2926 ((tmp_logblocks << cfg->blocklog) > XFS_MAX_LOG_BYTES)) {
2927 tmp_logblocks = (cfg->logblocks / sunit) * sunit;
2928 }
2929 cfg->logblocks = tmp_logblocks;
2930}
2931
2932/*
2933 * Make sure that the internal log is correctly aligned to the specified
2934 * stripe unit.
2935 */
2936static void
2937align_internal_log(
2938 struct mkfs_params *cfg,
2939 struct xfs_mount *mp,
2940 int sunit)
2941{
2942 /* round up log start if necessary */
2943 if ((cfg->logstart % sunit) != 0)
2944 cfg->logstart = ((cfg->logstart + (sunit - 1)) / sunit) * sunit;
2945
2946 /* round up/down the log size now */
2947 align_log_size(cfg, sunit);
2948
2949 /* check the aligned log still fits in an AG. */
2950 if (cfg->logblocks > cfg->agsize - XFS_FSB_TO_AGBNO(mp, cfg->logstart)) {
2951 fprintf(stderr,
2952_("Due to stripe alignment, the internal log size (%lld) is too large.\n"
2953 "Must fit within an allocation group.\n"),
2954 (long long) cfg->logblocks);
2955 usage();
2956 }
2957}
2958
2959void
2960validate_log_size(uint64_t logblocks, int blocklog, int min_logblocks)
2961{
2962 if (logblocks < min_logblocks) {
2963 fprintf(stderr,
2964 _("log size %lld blocks too small, minimum size is %d blocks\n"),
2965 (long long)logblocks, min_logblocks);
2966 usage();
2967 }
2968 if (logblocks > XFS_MAX_LOG_BLOCKS) {
2969 fprintf(stderr,
2970 _("log size %lld blocks too large, maximum size is %lld blocks\n"),
2971 (long long)logblocks, XFS_MAX_LOG_BLOCKS);
2972 usage();
2973 }
2974 if ((logblocks << blocklog) > XFS_MAX_LOG_BYTES) {
2975 fprintf(stderr,
2976 _("log size %lld bytes too large, maximum size is %lld bytes\n"),
2977 (long long)(logblocks << blocklog), XFS_MAX_LOG_BYTES);
2978 usage();
2979 }
2980}
2981
2982static void
2983calculate_log_size(
2984 struct mkfs_params *cfg,
2985 struct cli_params *cli,
2986 struct xfs_mount *mp)
2987{
e3bc8390
DC
2988 struct xfs_sb *sbp = &mp->m_sb;
2989 int min_logblocks;
a6fb6abe 2990 struct xfs_mount mount;
e3bc8390 2991
a6fb6abe
DC
2992 /* we need a temporary mount to calculate the minimum log size. */
2993 memset(&mount, 0, sizeof(mount));
2994 mount.m_sb = *sbp;
2995 libxfs_mount(&mount, &mp->m_sb, 0, 0, 0, 0);
2996 min_logblocks = libxfs_log_calc_minimum_size(&mount);
2997 libxfs_umount(&mount);
e3bc8390
DC
2998
2999 ASSERT(min_logblocks);
3000 min_logblocks = MAX(XFS_MIN_LOG_BLOCKS, min_logblocks);
3001
3002 /* if we have lots of blocks, check against XFS_MIN_LOG_BYTES, too */
3003 if (!cli->logsize &&
3004 cfg->dblocks >= (1024*1024*1024) >> cfg->blocklog)
3005 min_logblocks = MAX(min_logblocks,
3006 XFS_MIN_LOG_BYTES >> cfg->blocklog);
3007
3008 /*
3009 * external logs will have a device and size by now, so all we have
3010 * to do is validate it against minimum size and align it.
3011 */
3012 if (!cfg->loginternal) {
3013 if (min_logblocks > cfg->logblocks) {
3014 fprintf(stderr,
3015_("external log device %lld too small, must be at least %lld blocks\n"),
3016 (long long)cfg->logblocks,
3017 (long long)min_logblocks);
3018 usage();
3019 }
3020 cfg->logstart = 0;
3021 cfg->logagno = 0;
3022 if (cfg->lsunit)
3023 align_log_size(cfg, cfg->lsunit);
3024
3025 validate_log_size(cfg->logblocks, cfg->blocklog, min_logblocks);
3026 return;
3027 }
3028
3029 /* internal log - if no size specified, calculate automatically */
3030 if (!cfg->logblocks) {
3031 if (cfg->dblocks < GIGABYTES(1, cfg->blocklog)) {
3032 /* tiny filesystems get minimum sized logs. */
3033 cfg->logblocks = min_logblocks;
3034 } else if (cfg->dblocks < GIGABYTES(16, cfg->blocklog)) {
3035
3036 /*
3037 * For small filesystems, we want to use the
3038 * XFS_MIN_LOG_BYTES for filesystems smaller than 16G if
3039 * at all possible, ramping up to 128MB at 256GB.
3040 */
3041 cfg->logblocks = MIN(XFS_MIN_LOG_BYTES >> cfg->blocklog,
3042 min_logblocks * XFS_DFL_LOG_FACTOR);
3043 } else {
3044 /*
3045 * With a 2GB max log size, default to maximum size
3046 * at 4TB. This keeps the same ratio from the older
3047 * max log size of 128M at 256GB fs size. IOWs,
3048 * the ratio of fs size to log size is 2048:1.
3049 */
3050 cfg->logblocks = (cfg->dblocks << cfg->blocklog) / 2048;
3051 cfg->logblocks = cfg->logblocks >> cfg->blocklog;
3052 }
3053
3054 /* Ensure the chosen size meets minimum log size requirements */
3055 cfg->logblocks = MAX(min_logblocks, cfg->logblocks);
3056
3057 /*
3058 * Make sure the log fits wholly within an AG
3059 *
3060 * XXX: If agf->freeblks ends up as 0 because the log uses all
3061 * the free space, it causes the kernel all sorts of problems
3062 * with per-ag reservations. Right now just back it off one
3063 * block, but there's a whole can of worms here that needs to be
3064 * opened to decide what is the valid maximum size of a log in
3065 * an AG.
3066 */
3067 cfg->logblocks = MIN(cfg->logblocks,
3068 libxfs_alloc_ag_max_usable(mp) - 1);
3069
3070 /* and now clamp the size to the maximum supported size */
3071 cfg->logblocks = MIN(cfg->logblocks, XFS_MAX_LOG_BLOCKS);
3072 if ((cfg->logblocks << cfg->blocklog) > XFS_MAX_LOG_BYTES)
3073 cfg->logblocks = XFS_MAX_LOG_BYTES >> cfg->blocklog;
3074
3075 validate_log_size(cfg->logblocks, cfg->blocklog, min_logblocks);
3076 }
3077
3078 if (cfg->logblocks > sbp->sb_agblocks - libxfs_prealloc_blocks(mp)) {
3079 fprintf(stderr,
3080_("internal log size %lld too large, must fit in allocation group\n"),
3081 (long long)cfg->logblocks);
3082 usage();
3083 }
3084
3085 if (cli_opt_set(&lopts, L_AGNUM)) {
3086 if (cli->logagno >= sbp->sb_agcount) {
3087 fprintf(stderr,
3088_("log ag number %lld too large, must be less than %lld\n"),
3089 (long long)cli->logagno,
3090 (long long)sbp->sb_agcount);
3091 usage();
3092 }
3093 cfg->logagno = cli->logagno;
3094 } else
3095 cfg->logagno = (xfs_agnumber_t)(sbp->sb_agcount / 2);
3096
3097 cfg->logstart = XFS_AGB_TO_FSB(mp, cfg->logagno,
3098 libxfs_prealloc_blocks(mp));
3099
3100 /*
3101 * Align the logstart at stripe unit boundary.
3102 */
3103 if (cfg->lsunit) {
3104 align_internal_log(cfg, mp, cfg->lsunit);
3105 } else if (cfg->dsunit) {
3106 align_internal_log(cfg, mp, cfg->dsunit);
3107 }
3108 validate_log_size(cfg->logblocks, cfg->blocklog, min_logblocks);
3109}
3110
befcd768 3111/*
a6fb6abe 3112 * Set up superblock with the minimum parameters required for
befcd768 3113 * the libxfs macros needed by the log sizing code to run successfully.
a6fb6abe
DC
3114 * This includes a minimum log size calculation, so we need everything
3115 * that goes into that calculation to be setup here including feature
3116 * flags.
befcd768
DC
3117 */
3118static void
a6fb6abe 3119start_superblock_setup(
befcd768
DC
3120 struct mkfs_params *cfg,
3121 struct xfs_mount *mp,
3122 struct xfs_sb *sbp)
3123{
a6fb6abe
DC
3124 sbp->sb_magicnum = XFS_SB_MAGIC;
3125 sbp->sb_sectsize = (uint16_t)cfg->sectorsize;
befcd768 3126 sbp->sb_sectlog = (uint8_t)cfg->sectorlog;
a6fb6abe
DC
3127 sbp->sb_blocksize = cfg->blocksize;
3128 sbp->sb_blocklog = (uint8_t)cfg->blocklog;
3129
befcd768 3130 sbp->sb_agblocks = (xfs_agblock_t)cfg->agsize;
a6fb6abe 3131 sbp->sb_agblklog = (uint8_t)log2_roundup(cfg->agsize);
befcd768 3132 sbp->sb_agcount = (xfs_agnumber_t)cfg->agcount;
a6fb6abe
DC
3133
3134 sbp->sb_inodesize = (uint16_t)cfg->inodesize;
3135 sbp->sb_inodelog = (uint8_t)cfg->inodelog;
3136 sbp->sb_inopblock = (uint16_t)(cfg->blocksize / cfg->inodesize);
3137 sbp->sb_inopblog = (uint8_t)(cfg->blocklog - cfg->inodelog);
3138
3139 sbp->sb_dirblklog = cfg->dirblocklog - cfg->blocklog;
3140
3141 sb_set_features(cfg, sbp);
befcd768
DC
3142
3143 /*
a6fb6abe
DC
3144 * log stripe unit is stored in bytes on disk and cannot be zero
3145 * for v2 logs.
befcd768 3146 */
a6fb6abe
DC
3147 if (cfg->sb_feat.log_version == 2) {
3148 if (cfg->lsunit)
3149 sbp->sb_logsunit = XFS_FSB_TO_B(mp, cfg->lsunit);
3150 else
3151 sbp->sb_logsunit = 1;
3152 } else
3153 sbp->sb_logsunit = 0;
3154
3155}
3156
3157static void
3158initialise_mount(
3159 struct mkfs_params *cfg,
3160 struct xfs_mount *mp,
3161 struct xfs_sb *sbp)
3162{
3163 /* Minimum needed for libxfs_prealloc_blocks() */
3164 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
3165 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
befcd768
DC
3166}
3167
d1138048
DC
3168static void
3169print_mkfs_cfg(
3170 struct mkfs_params *cfg,
3171 char *dfile,
3172 char *logfile,
3173 char *rtfile)
3174{
3175 struct sb_feat_args *fp = &cfg->sb_feat;
3176
3177 printf(_(
3178"meta-data=%-22s isize=%-6d agcount=%lld, agsize=%lld blks\n"
3179" =%-22s sectsz=%-5u attr=%u, projid32bit=%u\n"
3180" =%-22s crc=%-8u finobt=%u, sparse=%u, rmapbt=%u, reflink=%u\n"
3181"data =%-22s bsize=%-6u blocks=%llu, imaxpct=%u\n"
3182" =%-22s sunit=%-6u swidth=%u blks\n"
3183"naming =version %-14u bsize=%-6u ascii-ci=%d ftype=%d\n"
3184"log =%-22s bsize=%-6d blocks=%lld, version=%d\n"
3185" =%-22s sectsz=%-5u sunit=%d blks, lazy-count=%d\n"
3186"realtime =%-22s extsz=%-6d blocks=%lld, rtextents=%lld\n"),
3187 dfile, cfg->inodesize, (long long)cfg->agcount,
3188 (long long)cfg->agsize,
639d0b0b 3189 "", cfg->sectorsize, fp->attr_version, fp->projid32bit,
d1138048
DC
3190 "", fp->crcs_enabled, fp->finobt, fp->spinodes, fp->rmapbt,
3191 fp->reflink,
3192 "", cfg->blocksize, (long long)cfg->dblocks, cfg->imaxpct,
3193 "", cfg->dsunit, cfg->dswidth,
3194 fp->dir_version, cfg->dirblocksize, fp->nci, fp->dirftype,
3195 logfile, cfg->blocksize, (long long)cfg->logblocks,
3196 fp->log_version,
3197 "", cfg->lsectorsize, cfg->lsunit, fp->lazy_sb_counters,
3198 rtfile, (int)cfg->rtextblocks << cfg->blocklog,
3199 (long long)cfg->rtblocks, (long long)cfg->rtextents);
3200}
3201
afda75a5
DC
3202/*
3203 * Format everything from the generated config into the superblock that
3204 * will be used to initialise the on-disk superblock. This is the in-memory
3205 * copy, so no need to care about endian swapping here.
3206 */
3207static void
a6fb6abe 3208finish_superblock_setup(
afda75a5
DC
3209 struct mkfs_params *cfg,
3210 struct xfs_mount *mp,
3211 struct xfs_sb *sbp)
3212{
3213 if (cfg->label)
3214 strncpy(sbp->sb_fname, cfg->label, sizeof(sbp->sb_fname));
3215
afda75a5
DC
3216 sbp->sb_dblocks = cfg->dblocks;
3217 sbp->sb_rblocks = cfg->rtblocks;
3218 sbp->sb_rextents = cfg->rtextents;
3219 platform_uuid_copy(&sbp->sb_uuid, &cfg->uuid);
3220 /* Only in memory; libxfs expects this as if read from disk */
3221 platform_uuid_copy(&sbp->sb_meta_uuid, &cfg->uuid);
3222 sbp->sb_logstart = cfg->logstart;
3223 sbp->sb_rootino = sbp->sb_rbmino = sbp->sb_rsumino = NULLFSINO;
3224 sbp->sb_rextsize = cfg->rtextblocks;
3225 sbp->sb_agcount = (xfs_agnumber_t)cfg->agcount;
3226 sbp->sb_rbmblocks = cfg->rtbmblocks;
3227 sbp->sb_logblocks = (xfs_extlen_t)cfg->logblocks;
afda75a5
DC
3228 sbp->sb_rextslog = (uint8_t)(cfg->rtextents ?
3229 libxfs_highbit32((unsigned int)cfg->rtextents) : 0);
3230 sbp->sb_inprogress = 1; /* mkfs is in progress */
3231 sbp->sb_imax_pct = cfg->imaxpct;
3232 sbp->sb_icount = 0;
3233 sbp->sb_ifree = 0;
3234 sbp->sb_fdblocks = cfg->dblocks -
3235 cfg->agcount * libxfs_prealloc_blocks(mp) -
3236 (cfg->loginternal ? cfg->logblocks : 0);
3237 sbp->sb_frextents = 0; /* will do a free later */
3238 sbp->sb_uquotino = sbp->sb_gquotino = sbp->sb_pquotino = 0;
3239 sbp->sb_qflags = 0;
3240 sbp->sb_unit = cfg->dsunit;
3241 sbp->sb_width = cfg->dswidth;
afda75a5 3242
afda75a5
DC
3243}
3244
e99bf83d
DC
3245/*
3246 * Sanitise the data and log devices and prepare them so libxfs can mount the
3247 * device successfully. Also check we can access the rt device if configured.
3248 */
3249static void
3250prepare_devices(
3251 struct mkfs_params *cfg,
3252 struct libxfs_xinit *xi,
3253 struct xfs_mount *mp,
3254 struct xfs_sb *sbp,
3255 bool clear_stale)
3256{
3257 struct xfs_buf *buf;
3258 int whack_blks = BTOBB(WHACK_SIZE);
3259 int lsunit;
3260
3261 /*
3262 * If there's an old XFS filesystem on the device with enough intact
3263 * information that we can parse the superblock, there's enough
3264 * information on disk to confuse a future xfs_repair call. To avoid
3265 * this, whack all the old secondary superblocks that we can find.
3266 */
3267 if (clear_stale)
3268 zero_old_xfs_structures(xi, sbp);
3269
3270 /*
3271 * If the data device is a file, grow out the file to its final size if
3272 * needed so that the reads for the end of the device in the mount code
3273 * will succeed.
3274 */
3275 if (xi->disfile &&
3276 xi->dsize * xi->dbsize < cfg->dblocks * cfg->blocksize) {
3277 if (ftruncate(xi->dfd, cfg->dblocks * cfg->blocksize) < 0) {
3278 fprintf(stderr,
3279 _("%s: Growing the data section failed\n"),
3280 progname);
3281 exit(1);
3282 }
3283
3284 /* update size to be able to whack blocks correctly */
3285 xi->dsize = BTOBB(cfg->dblocks * cfg->blocksize);
3286 }
3287
3288 /*
3289 * Zero out the end to obliterate any old MD RAID (or other) metadata at
3290 * the end of the device. (MD sb is ~64k from the end, take out a wider
3291 * swath to be sure)
3292 */
3293 buf = libxfs_getbuf(mp->m_ddev_targp, (xi->dsize - whack_blks),
3294 whack_blks);
3295 memset(XFS_BUF_PTR(buf), 0, WHACK_SIZE);
3296 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3297 libxfs_purgebuf(buf);
3298
3299 /*
3300 * Now zero out the beginning of the device, to obliterate any old
3301 * filesystem signatures out there. This should take care of
3302 * swap (somewhere around the page size), jfs (32k),
3303 * ext[2,3] and reiserfs (64k) - and hopefully all else.
3304 */
3305 buf = libxfs_getbuf(mp->m_ddev_targp, 0, whack_blks);
3306 memset(XFS_BUF_PTR(buf), 0, WHACK_SIZE);
3307 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3308 libxfs_purgebuf(buf);
3309
3310 /* OK, now write the superblock... */
3311 buf = libxfs_getbuf(mp->m_ddev_targp, XFS_SB_DADDR, XFS_FSS_TO_BB(mp, 1));
3312 buf->b_ops = &xfs_sb_buf_ops;
3313 memset(XFS_BUF_PTR(buf), 0, cfg->sectorsize);
3314 libxfs_sb_to_disk((void *)XFS_BUF_PTR(buf), sbp);
3315 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3316 libxfs_purgebuf(buf);
3317
3318 /* ...and zero the log.... */
3319 lsunit = sbp->sb_logsunit;
3320 if (lsunit == 1)
3321 lsunit = sbp->sb_logsectsize;
3322
3323 libxfs_log_clear(mp->m_logdev_targp, NULL,
3324 XFS_FSB_TO_DADDR(mp, cfg->logstart),
3325 (xfs_extlen_t)XFS_FSB_TO_BB(mp, cfg->logblocks),
3326 &sbp->sb_uuid, cfg->sb_feat.log_version,
3327 lsunit, XLOG_FMT, XLOG_INIT_CYCLE, false);
3328
3329 /* finally, check we can write the last block in the realtime area */
3330 if (mp->m_rtdev_targp->dev && cfg->rtblocks > 0) {
3331 buf = libxfs_getbuf(mp->m_rtdev_targp,
3332 XFS_FSB_TO_BB(mp, cfg->rtblocks - 1LL),
3333 BTOBB(cfg->blocksize));
3334 memset(XFS_BUF_PTR(buf), 0, cfg->blocksize);
3335 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3336 libxfs_purgebuf(buf);
3337 }
3338
3339}
3340
0ff1b0ed
DC
3341/*
3342 * XXX: this code is mostly common with the kernel growfs code.
3343 * These initialisations should be pulled into libxfs to keep the
3344 * kernel/userspace header initialisation code the same.
3345 */
3346static void
3347initialise_ag_headers(
3348 struct mkfs_params *cfg,
3349 struct xfs_mount *mp,
3350 struct xfs_sb *sbp,
3351 xfs_agnumber_t agno,
3352 int *worst_freelist)
3353{
3354 struct xfs_perag *pag = libxfs_perag_get(mp, agno);
3355 struct xfs_agfl *agfl;
3356 struct xfs_agf *agf;
3357 struct xfs_agi *agi;
3358 struct xfs_buf *buf;
3359 struct xfs_btree_block *block;
3360 struct xfs_alloc_rec *arec;
3361 struct xfs_alloc_rec *nrec;
3362 int bucket;
3363 uint64_t agsize = cfg->agsize;
3364 xfs_agblock_t agblocks;
3365 bool is_log_ag = false;
3366 int c;
3367
3368 if (cfg->loginternal && agno == cfg->logagno)
3369 is_log_ag = true;
3370
3371 /*
3372 * Superblock.
3373 */
3374 buf = libxfs_getbuf(mp->m_ddev_targp,
3375 XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
3376 XFS_FSS_TO_BB(mp, 1));
3377 buf->b_ops = &xfs_sb_buf_ops;
3378 memset(XFS_BUF_PTR(buf), 0, cfg->sectorsize);
3379 libxfs_sb_to_disk((void *)XFS_BUF_PTR(buf), sbp);
3380 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3381
3382 /*
3383 * AG header block: freespace
3384 */
3385 buf = libxfs_getbuf(mp->m_ddev_targp,
3386 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
3387 XFS_FSS_TO_BB(mp, 1));
3388 buf->b_ops = &xfs_agf_buf_ops;
3389 agf = XFS_BUF_TO_AGF(buf);
3390 memset(agf, 0, cfg->sectorsize);
3391 if (agno == cfg->agcount - 1)
3392 agsize = cfg->dblocks - (xfs_rfsblock_t)(agno * agsize);
3393 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
3394 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
3395 agf->agf_seqno = cpu_to_be32(agno);
3396 agf->agf_length = cpu_to_be32(agsize);
3397 agf->agf_roots[XFS_BTNUM_BNOi] = cpu_to_be32(XFS_BNO_BLOCK(mp));
3398 agf->agf_roots[XFS_BTNUM_CNTi] = cpu_to_be32(XFS_CNT_BLOCK(mp));
3399 agf->agf_levels[XFS_BTNUM_BNOi] = cpu_to_be32(1);
3400 agf->agf_levels[XFS_BTNUM_CNTi] = cpu_to_be32(1);
3401 pag->pagf_levels[XFS_BTNUM_BNOi] = 1;
3402 pag->pagf_levels[XFS_BTNUM_CNTi] = 1;
3403
3404 if (xfs_sb_version_hasrmapbt(sbp)) {
3405 agf->agf_roots[XFS_BTNUM_RMAPi] = cpu_to_be32(XFS_RMAP_BLOCK(mp));
3406 agf->agf_levels[XFS_BTNUM_RMAPi] = cpu_to_be32(1);
3407 agf->agf_rmap_blocks = cpu_to_be32(1);
3408 }
3409
3410 if (xfs_sb_version_hasreflink(sbp)) {
3411 agf->agf_refcount_root = cpu_to_be32(libxfs_refc_block(mp));
3412 agf->agf_refcount_level = cpu_to_be32(1);
3413 agf->agf_refcount_blocks = cpu_to_be32(1);
3414 }
3415
3416 agf->agf_flfirst = 0;
3417 agf->agf_fllast = cpu_to_be32(XFS_AGFL_SIZE(mp) - 1);
3418 agf->agf_flcount = 0;
3419 agblocks = (xfs_agblock_t)(agsize - libxfs_prealloc_blocks(mp));
3420 agf->agf_freeblks = cpu_to_be32(agblocks);
3421 agf->agf_longest = cpu_to_be32(agblocks);
3422
3423 if (xfs_sb_version_hascrc(sbp))
3424 platform_uuid_copy(&agf->agf_uuid, &sbp->sb_uuid);
3425
3426 if (is_log_ag) {
3427 be32_add_cpu(&agf->agf_freeblks, -(int64_t)cfg->logblocks);
3428 agf->agf_longest = cpu_to_be32(agsize -
3429 XFS_FSB_TO_AGBNO(mp, cfg->logstart) - cfg->logblocks);
3430 }
3431 if (libxfs_alloc_min_freelist(mp, pag) > *worst_freelist)
3432 *worst_freelist = libxfs_alloc_min_freelist(mp, pag);
3433 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3434
3435 /*
3436 * AG freelist header block
3437 */
3438 buf = libxfs_getbuf(mp->m_ddev_targp,
3439 XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
3440 XFS_FSS_TO_BB(mp, 1));
3441 buf->b_ops = &xfs_agfl_buf_ops;
3442 agfl = XFS_BUF_TO_AGFL(buf);
3443 /* setting to 0xff results in initialisation to NULLAGBLOCK */
3444 memset(agfl, 0xff, cfg->sectorsize);
3445 if (xfs_sb_version_hascrc(sbp)) {
3446 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
3447 agfl->agfl_seqno = cpu_to_be32(agno);
3448 platform_uuid_copy(&agfl->agfl_uuid, &sbp->sb_uuid);
3449 for (bucket = 0; bucket < XFS_AGFL_SIZE(mp); bucket++)
3450 agfl->agfl_bno[bucket] = cpu_to_be32(NULLAGBLOCK);
3451 }
3452
3453 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3454
3455 /*
3456 * AG header block: inodes
3457 */
3458 buf = libxfs_getbuf(mp->m_ddev_targp,
3459 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
3460 XFS_FSS_TO_BB(mp, 1));
3461 agi = XFS_BUF_TO_AGI(buf);
3462 buf->b_ops = &xfs_agi_buf_ops;
3463 memset(agi, 0, cfg->sectorsize);
3464 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
3465 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
3466 agi->agi_seqno = cpu_to_be32(agno);
3467 agi->agi_length = cpu_to_be32(agsize);
3468 agi->agi_count = 0;
3469 agi->agi_root = cpu_to_be32(XFS_IBT_BLOCK(mp));
3470 agi->agi_level = cpu_to_be32(1);
3471 if (xfs_sb_version_hasfinobt(sbp)) {
3472 agi->agi_free_root = cpu_to_be32(XFS_FIBT_BLOCK(mp));
3473 agi->agi_free_level = cpu_to_be32(1);
3474 }
3475 agi->agi_freecount = 0;
3476 agi->agi_newino = cpu_to_be32(NULLAGINO);
3477 agi->agi_dirino = cpu_to_be32(NULLAGINO);
3478 if (xfs_sb_version_hascrc(sbp))
3479 platform_uuid_copy(&agi->agi_uuid, &sbp->sb_uuid);
3480 for (c = 0; c < XFS_AGI_UNLINKED_BUCKETS; c++)
3481 agi->agi_unlinked[c] = cpu_to_be32(NULLAGINO);
3482 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3483
3484 /*
3485 * BNO btree root block
3486 */
3487 buf = libxfs_getbuf(mp->m_ddev_targp,
3488 XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
3489 BTOBB(cfg->blocksize));
3490 buf->b_ops = &xfs_allocbt_buf_ops;
3491 block = XFS_BUF_TO_BLOCK(buf);
3492 memset(block, 0, cfg->blocksize);
3493 libxfs_btree_init_block(mp, buf, XFS_BTNUM_BNO, 0, 1, agno, 0);
3494
3495 arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
3496 arec->ar_startblock = cpu_to_be32(libxfs_prealloc_blocks(mp));
3497 if (is_log_ag) {
3498 xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp, cfg->logstart);
3499
3500 ASSERT(start >= libxfs_prealloc_blocks(mp));
3501 if (start != libxfs_prealloc_blocks(mp)) {
3502 /*
3503 * Modify first record to pad stripe align of log
3504 */
3505 arec->ar_blockcount = cpu_to_be32(start -
3506 libxfs_prealloc_blocks(mp));
3507 nrec = arec + 1;
3508 /*
3509 * Insert second record at start of internal log
3510 * which then gets trimmed.
3511 */
3512 nrec->ar_startblock = cpu_to_be32(
3513 be32_to_cpu(arec->ar_startblock) +
3514 be32_to_cpu(arec->ar_blockcount));
3515 arec = nrec;
3516 be16_add_cpu(&block->bb_numrecs, 1);
3517 }
3518 /*
3519 * Change record start to after the internal log
3520 */
3521 be32_add_cpu(&arec->ar_startblock, cfg->logblocks);
3522 }
3523 /*
3524 * Calculate the record block count and check for the case where
3525 * the log might have consumed all available space in the AG. If
3526 * so, reset the record count to 0 to avoid exposure of an invalid
3527 * record start block.
3528 */
3529 arec->ar_blockcount = cpu_to_be32(agsize -
3530 be32_to_cpu(arec->ar_startblock));
3531 if (!arec->ar_blockcount)
3532 block->bb_numrecs = 0;
3533
3534 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3535
3536 /*
3537 * CNT btree root block
3538 */
3539 buf = libxfs_getbuf(mp->m_ddev_targp,
3540 XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
3541 BTOBB(cfg->blocksize));
3542 buf->b_ops = &xfs_allocbt_buf_ops;
3543 block = XFS_BUF_TO_BLOCK(buf);
3544 memset(block, 0, cfg->blocksize);
3545 libxfs_btree_init_block(mp, buf, XFS_BTNUM_CNT, 0, 1, agno, 0);
3546
3547 arec = XFS_ALLOC_REC_ADDR(mp, block, 1);
3548 arec->ar_startblock = cpu_to_be32(libxfs_prealloc_blocks(mp));
3549 if (is_log_ag) {
3550 xfs_agblock_t start = XFS_FSB_TO_AGBNO(mp, cfg->logstart);
3551
3552 ASSERT(start >= libxfs_prealloc_blocks(mp));
3553 if (start != libxfs_prealloc_blocks(mp)) {
3554 arec->ar_blockcount = cpu_to_be32(start -
3555 libxfs_prealloc_blocks(mp));
3556 nrec = arec + 1;
3557 nrec->ar_startblock = cpu_to_be32(
3558 be32_to_cpu(arec->ar_startblock) +
3559 be32_to_cpu(arec->ar_blockcount));
3560 arec = nrec;
3561 be16_add_cpu(&block->bb_numrecs, 1);
3562 }
3563 be32_add_cpu(&arec->ar_startblock, cfg->logblocks);
3564 }
3565 /*
3566 * Calculate the record block count and check for the case where
3567 * the log might have consumed all available space in the AG. If
3568 * so, reset the record count to 0 to avoid exposure of an invalid
3569 * record start block.
3570 */
3571 arec->ar_blockcount = cpu_to_be32(agsize -
3572 be32_to_cpu(arec->ar_startblock));
3573 if (!arec->ar_blockcount)
3574 block->bb_numrecs = 0;
3575
3576 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3577
3578 /*
3579 * refcount btree root block
3580 */
3581 if (xfs_sb_version_hasreflink(sbp)) {
3582 buf = libxfs_getbuf(mp->m_ddev_targp,
3583 XFS_AGB_TO_DADDR(mp, agno, libxfs_refc_block(mp)),
3584 BTOBB(cfg->blocksize));
3585 buf->b_ops = &xfs_refcountbt_buf_ops;
3586
3587 block = XFS_BUF_TO_BLOCK(buf);
3588 memset(block, 0, cfg->blocksize);
3589 libxfs_btree_init_block(mp, buf, XFS_BTNUM_REFC, 0, 0, agno, 0);
3590 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3591 }
3592
3593 /*
3594 * INO btree root block
3595 */
3596 buf = libxfs_getbuf(mp->m_ddev_targp,
3597 XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
3598 BTOBB(cfg->blocksize));
3599 buf->b_ops = &xfs_inobt_buf_ops;
3600 block = XFS_BUF_TO_BLOCK(buf);
3601 memset(block, 0, cfg->blocksize);
3602 libxfs_btree_init_block(mp, buf, XFS_BTNUM_INO, 0, 0, agno, 0);
3603 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3604
3605 /*
3606 * Free INO btree root block
3607 */
3608 if (xfs_sb_version_hasfinobt(sbp)) {
3609 buf = libxfs_getbuf(mp->m_ddev_targp,
3610 XFS_AGB_TO_DADDR(mp, agno, XFS_FIBT_BLOCK(mp)),
3611 BTOBB(cfg->blocksize));
3612 buf->b_ops = &xfs_inobt_buf_ops;
3613 block = XFS_BUF_TO_BLOCK(buf);
3614 memset(block, 0, cfg->blocksize);
3615 libxfs_btree_init_block(mp, buf, XFS_BTNUM_FINO, 0, 0, agno, 0);
3616 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3617 }
3618
3619 /* RMAP btree root block */
3620 if (xfs_sb_version_hasrmapbt(sbp)) {
3621 struct xfs_rmap_rec *rrec;
3622
3623 buf = libxfs_getbuf(mp->m_ddev_targp,
3624 XFS_AGB_TO_DADDR(mp, agno, XFS_RMAP_BLOCK(mp)),
3625 BTOBB(cfg->blocksize));
3626 buf->b_ops = &xfs_rmapbt_buf_ops;
3627 block = XFS_BUF_TO_BLOCK(buf);
3628 memset(block, 0, cfg->blocksize);
3629
3630 libxfs_btree_init_block(mp, buf, XFS_BTNUM_RMAP, 0, 0, agno, 0);
3631
3632 /*
3633 * mark the AG header regions as static metadata
3634 * The BNO btree block is the first block after the
3635 * headers, so it's location defines the size of region
3636 * the static metadata consumes.
3637 */
3638 rrec = XFS_RMAP_REC_ADDR(block, 1);
3639 rrec->rm_startblock = 0;
3640 rrec->rm_blockcount = cpu_to_be32(XFS_BNO_BLOCK(mp));
3641 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_FS);
3642 rrec->rm_offset = 0;
3643 be16_add_cpu(&block->bb_numrecs, 1);
3644
3645 /* account freespace btree root blocks */
3646 rrec = XFS_RMAP_REC_ADDR(block, 2);
3647 rrec->rm_startblock = cpu_to_be32(XFS_BNO_BLOCK(mp));
3648 rrec->rm_blockcount = cpu_to_be32(2);
3649 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
3650 rrec->rm_offset = 0;
3651 be16_add_cpu(&block->bb_numrecs, 1);
3652
3653 /* account inode btree root blocks */
3654 rrec = XFS_RMAP_REC_ADDR(block, 3);
3655 rrec->rm_startblock = cpu_to_be32(XFS_IBT_BLOCK(mp));
3656 rrec->rm_blockcount = cpu_to_be32(XFS_RMAP_BLOCK(mp) -
3657 XFS_IBT_BLOCK(mp));
3658 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_INOBT);
3659 rrec->rm_offset = 0;
3660 be16_add_cpu(&block->bb_numrecs, 1);
3661
3662 /* account for rmap btree root */
3663 rrec = XFS_RMAP_REC_ADDR(block, 4);
3664 rrec->rm_startblock = cpu_to_be32(XFS_RMAP_BLOCK(mp));
3665 rrec->rm_blockcount = cpu_to_be32(1);
3666 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_AG);
3667 rrec->rm_offset = 0;
3668 be16_add_cpu(&block->bb_numrecs, 1);
3669
3670 /* account for refcount btree root */
3671 if (xfs_sb_version_hasreflink(sbp)) {
3672 rrec = XFS_RMAP_REC_ADDR(block, 5);
3673 rrec->rm_startblock = cpu_to_be32(libxfs_refc_block(mp));
3674 rrec->rm_blockcount = cpu_to_be32(1);
3675 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_REFC);
3676 rrec->rm_offset = 0;
3677 be16_add_cpu(&block->bb_numrecs, 1);
3678 }
3679
3680 /* account for the log space */
3681 if (is_log_ag) {
3682 rrec = XFS_RMAP_REC_ADDR(block,
3683 be16_to_cpu(block->bb_numrecs) + 1);
3684 rrec->rm_startblock = cpu_to_be32(
3685 XFS_FSB_TO_AGBNO(mp, cfg->logstart));
3686 rrec->rm_blockcount = cpu_to_be32(cfg->logblocks);
3687 rrec->rm_owner = cpu_to_be64(XFS_RMAP_OWN_LOG);
3688 rrec->rm_offset = 0;
3689 be16_add_cpu(&block->bb_numrecs, 1);
3690 }
3691
3692 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3693 }
3694
3695 libxfs_perag_put(pag);
3696}
3697
3698static void
3699initialise_ag_freespace(
3700 struct xfs_mount *mp,
3701 xfs_agnumber_t agno,
3702 int worst_freelist)
3703{
3704 struct xfs_alloc_arg args;
3705 struct xfs_trans *tp;
3706 struct xfs_trans_res tres = {0};
3707 int c;
3708
3709 c = libxfs_trans_alloc(mp, &tres, worst_freelist, 0, 0, &tp);
3710 if (c)
3711 res_failed(c);
3712
3713 memset(&args, 0, sizeof(args));
3714 args.tp = tp;
3715 args.mp = mp;
3716 args.agno = agno;
3717 args.alignment = 1;
3718 args.pag = libxfs_perag_get(mp, agno);
3719
3720 libxfs_alloc_fix_freelist(&args, 0);
3721 libxfs_perag_put(args.pag);
3722 libxfs_trans_commit(tp);
3723}
3724
e2847e5c
DC
3725/*
3726 * rewrite several secondary superblocks with the root inode number filled out.
3727 * This can help repair recovery from a trashed primary superblock without
3728 * losing the root inode.
3729 */
3730static void
3731rewrite_secondary_superblocks(
3732 struct xfs_mount *mp)
3733{
3734 struct xfs_buf *buf;
3735
3736 /* rewrite the last superblock */
3737 buf = libxfs_readbuf(mp->m_dev,
3738 XFS_AGB_TO_DADDR(mp, mp->m_sb.sb_agcount - 1,
3739 XFS_SB_DADDR),
3740 XFS_FSS_TO_BB(mp, 1),
3741 LIBXFS_EXIT_ON_FAILURE, &xfs_sb_buf_ops);
3742 XFS_BUF_TO_SBP(buf)->sb_rootino = cpu_to_be64(mp->m_sb.sb_rootino);
3743 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3744
3745 /* and one in the middle for luck if there's enough AGs for that */
3746 if (mp->m_sb.sb_agcount <= 2)
3747 return;
3748
3749 buf = libxfs_readbuf(mp->m_dev,
3750 XFS_AGB_TO_DADDR(mp, (mp->m_sb.sb_agcount - 1) / 2,
3751 XFS_SB_DADDR),
3752 XFS_FSS_TO_BB(mp, 1),
3753 LIBXFS_EXIT_ON_FAILURE, &xfs_sb_buf_ops);
3754 XFS_BUF_TO_SBP(buf)->sb_rootino = cpu_to_be64(mp->m_sb.sb_rootino);
3755 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
3756}
3757
2bd0ea18 3758int
9440d84d
NS
3759main(
3760 int argc,
3761 char **argv)
2bd0ea18 3762{
2bd0ea18 3763 xfs_agnumber_t agno;
2bd0ea18
NS
3764 xfs_buf_t *buf;
3765 int c;
b449c79e
DC
3766 char *dfile = NULL;
3767 char *logfile = NULL;
3768 char *rtfile = NULL;
fdea8fbc 3769 int dry_run = 0;
ad136b33 3770 int discard = 1;
b449c79e 3771 int force_overwrite = 0;
fdea8fbc 3772 int quiet = 0;
b449c79e
DC
3773 char *protofile = NULL;
3774 char *protostring = NULL;
3775 int worst_freelist = 0;
3776
3777 struct libxfs_xinit xi = {
3778 .isdirect = LIBXFS_DIRECT,
3779 .isreadonly = LIBXFS_EXCLUSIVELY,
3780 };
befcd768
DC
3781 struct xfs_mount mbuf = {};
3782 struct xfs_mount *mp = &mbuf;
3783 struct xfs_sb *sbp = &mp->m_sb;
b449c79e
DC
3784 struct fs_topology ft = {};
3785 struct cli_params cli = {
3786 .xi = &xi,
3787 .loginternal = 1,
3788 };
3789 struct mkfs_params cfg = {};
3790
68344ba0
DC
3791 /* build time defaults */
3792 struct mkfs_default_params dft = {
b729d144 3793 .source = _("package build definitions"),
68344ba0
DC
3794 .sectorsize = XFS_MIN_SECTORSIZE,
3795 .blocksize = 1 << XFS_DFL_BLOCKSIZE_LOG,
3796 .sb_feat = {
3797 .log_version = 2,
3798 .attr_version = 2,
129d98a9
ES
3799 .dir_version = 2,
3800 .inode_align = true,
68344ba0
DC
3801 .nci = false,
3802 .lazy_sb_counters = true,
639d0b0b 3803 .projid32bit = true,
68344ba0
DC
3804 .crcs_enabled = true,
3805 .dirftype = true,
3806 .finobt = true,
3807 .spinodes = false,
3808 .rmapbt = false,
3809 .reflink = false,
3810 .parent_pointers = false,
3811 .nodalign = false,
3812 .nortalign = false,
3813 },
5f1a2100 3814 };
2bd0ea18 3815
a43e656b 3816 platform_uuid_generate(&cli.uuid);
2bd0ea18 3817 progname = basename(argv[0]);
9440d84d
NS
3818 setlocale(LC_ALL, "");
3819 bindtextdomain(PACKAGE, LOCALEDIR);
3820 textdomain(PACKAGE);
3821
68344ba0
DC
3822 /*
3823 * TODO: Sourcing defaults from a config file
3824 *
3825 * Before anything else, see if there's a config file with different
3826 * defaults. If a file exists in <package location>, read in the new
3827 * default values and overwrite them in the &dft structure. This way the
3828 * new defaults will apply before we parse the CLI, and the CLI will
b729d144
ES
3829 * still be able to override them. When more than one source is
3830 * implemented, emit a message to indicate where the defaults being
3831 * used came from.
3832 *
3833 * printf(_("Default configuration sourced from %s\n"), dft.source);
68344ba0 3834 */
68344ba0
DC
3835
3836 /* copy new defaults into CLI parsing structure */
3837 memcpy(&cli.sb_feat, &dft.sb_feat, sizeof(cli.sb_feat));
3838 memcpy(&cli.fsx, &dft.fsx, sizeof(cli.fsx));
3839
f7b80291 3840 while ((c = getopt(argc, argv, "b:d:i:l:L:m:n:KNp:qr:s:CfV")) != EOF) {
2bd0ea18
NS
3841 switch (c) {
3842 case 'C':
2bd0ea18 3843 case 'f':
f937adac 3844 force_overwrite = 1;
2bd0ea18
NS
3845 break;
3846 case 'b':
b449c79e 3847 case 'd':
d7240c96 3848 case 'i':
e3bc8390 3849 case 'l':
b449c79e 3850 case 'm':
fdea8fbc 3851 case 'n':
7a9af89a 3852 case 'r':
22319b56 3853 case 's':
85d6f03d 3854 parse_subopts(c, optarg, &cli);
2bd0ea18 3855 break;
979f7189
NS
3856 case 'L':
3857 if (strlen(optarg) > sizeof(sbp->sb_fname))
3858 illegal(optarg, "L");
b449c79e 3859 cfg.label = optarg;
f7b80291 3860 break;
33a12367 3861 case 'N':
ebed4acf 3862 dry_run = 1;
33a12367 3863 break;
ad136b33
CH
3864 case 'K':
3865 discard = 0;
3866 break;
2bd0ea18
NS
3867 case 'p':
3868 if (protofile)
5e656dbb 3869 respec('p', NULL, 0);
2bd0ea18
NS
3870 protofile = optarg;
3871 break;
3872 case 'q':
ebed4acf 3873 quiet = 1;
2bd0ea18 3874 break;
2bd0ea18 3875 case 'V':
9440d84d 3876 printf(_("%s version %s\n"), progname, VERSION);
3d98fe63 3877 exit(0);
2bd0ea18
NS
3878 case '?':
3879 unknown(optopt, "");
3880 }
3881 }
3882 if (argc - optind > 1) {
9440d84d 3883 fprintf(stderr, _("extra arguments\n"));
2bd0ea18
NS
3884 usage();
3885 } else if (argc - optind == 1) {
27ae3a59 3886 dfile = xi.volname = getstr(argv[optind], &dopts, D_NAME);
2bd0ea18
NS
3887 } else
3888 dfile = xi.dname;
9440d84d 3889
b449c79e 3890 protostring = setup_proto(protofile);
a3ac5af1 3891
06ac92fd 3892 /*
585f41bb
DC
3893 * Extract as much of the valid config as we can from the CLI input
3894 * before opening the libxfs devices.
06ac92fd 3895 */
b1b8e54e 3896 validate_blocksize(&cfg, &cli, &dft);
585f41bb
DC
3897 validate_sectorsize(&cfg, &cli, &dft, &ft, dfile, dry_run,
3898 force_overwrite);
b449c79e
DC
3899
3900 /*
3901 * XXX: we still need to set block size and sector size global variables
3902 * so that getnum/cvtnum works correctly
3903 */
3904 blocksize = cfg.blocksize;
3905 sectorsize = cfg.sectorsize;
3906
22319b56 3907 validate_log_sectorsize(&cfg, &cli, &dft);
a43e656b 3908 validate_sb_features(&cfg, &cli);
fd5eda53 3909
fdea8fbc
DC
3910 /*
3911 * we've now completed basic validation of the features, sector and
3912 * block sizes, so from this point onwards we use the values found in
3913 * the cfg structure for them, not the command line structure.
3914 */
3915 validate_dirblocksize(&cfg, &cli);
8fe29028 3916 validate_inodesize(&cfg, &cli);
fdea8fbc 3917
e24dfa22
DC
3918 /*
3919 * if the device size was specified convert it to a block count
3920 * now we have a valid block size. These will be set to zero if
3921 * nothing was specified, indicating we should use the full device.
3922 */
3923 cfg.dblocks = calc_dev_size(cli.dsize, &cfg, &dopts, D_SIZE, "data");
3924 cfg.logblocks = calc_dev_size(cli.logsize, &cfg, &lopts, L_SIZE, "log");
3925 cfg.rtblocks = calc_dev_size(cli.rtsize, &cfg, &ropts, R_SIZE, "rt");
3926
80b154f7 3927 validate_rtextsize(&cfg, &cli, &ft);
2f44b1b0 3928 calc_stripe_factors(&cfg, &cli, &ft);
80b154f7 3929
379f01d2
DC
3930 /*
3931 * Open and validate the device configurations
3932 */
3933 open_devices(&cfg, &xi, (discard && !dry_run));
dd5ac314 3934 validate_datadev(&cfg, &cli);
90b7e13d 3935 validate_logdev(&cfg, &cli, &logfile);
7a9af89a 3936 validate_rtdev(&cfg, &cli, &rtfile);
379f01d2 3937
1de01446
DC
3938 /*
3939 * At this point when know exactly what size all the devices are,
3940 * so we can start validating and calculating layout options that are
3941 * dependent on device sizes. Once calculated, make sure everything
3942 * aligns to device geometry correctly.
3943 */
3944 calculate_initial_ag_geometry(&cfg, &cli);
051b4e37 3945 align_ag_geometry(&cfg);
1de01446 3946
d7240c96
DC
3947 calculate_imaxpct(&cfg, &cli);
3948
befcd768
DC
3949 /*
3950 * Set up the basic superblock parameters now so that we can use
3951 * the geometry information we've already validated in libxfs
3952 * provided functions to determine on-disk format information.
3953 */
a6fb6abe 3954 start_superblock_setup(&cfg, mp, sbp);
befcd768
DC
3955 initialise_mount(&cfg, mp, sbp);
3956
e3bc8390
DC
3957 /*
3958 * With the mount set up, we can finally calculate the log size
3959 * constraints and do default size calculations and final validation
3960 */
3961 calculate_log_size(&cfg, &cli, mp);
3962
ebed4acf 3963 if (!quiet || dry_run) {
d1138048 3964 print_mkfs_cfg(&cfg, dfile, logfile, rtfile);
ebed4acf 3965 if (dry_run)
9440d84d
NS
3966 exit(0);
3967 }
a6fb6abe 3968 finish_superblock_setup(&cfg, mp, sbp);
6003fd81 3969
2f012bf9 3970 /*
e99bf83d 3971 * we need the libxfs buffer cache from here on in.
2f012bf9 3972 */
75c8b434 3973 libxfs_buftarg_init(mp, xi.ddev, xi.logdev, xi.rtdev);
2bd0ea18 3974
2bd0ea18 3975 /*
e99bf83d
DC
3976 * Before we mount the filesystem we need to make sure the devices have
3977 * enough of the filesystem structure on them that allows libxfs to
3978 * mount.
2bd0ea18 3979 */
e99bf83d 3980 prepare_devices(&cfg, &xi, mp, sbp, force_overwrite);
9aa57116 3981 mp = libxfs_mount(mp, sbp, xi.ddev, xi.logdev, xi.rtdev, 0);
02e85e7f 3982 if (mp == NULL) {
9440d84d
NS
3983 fprintf(stderr, _("%s: filesystem failed to initialize\n"),
3984 progname);
2bd0ea18
NS
3985 exit(1);
3986 }
2bd0ea18 3987
75c8b434 3988 /*
0ff1b0ed 3989 * Initialise all the static on disk metadata.
75c8b434 3990 */
0ff1b0ed
DC
3991 for (agno = 0; agno < cfg.agcount; agno++)
3992 initialise_ag_headers(&cfg, mp, sbp, agno, &worst_freelist);
2bd0ea18 3993
2bd0ea18 3994 /*
0ff1b0ed 3995 * Initialise the freespace freelists (i.e. AGFLs) in each AG.
2bd0ea18 3996 */
0ff1b0ed
DC
3997 for (agno = 0; agno < cfg.agcount; agno++)
3998 initialise_ag_freespace(mp, agno, worst_freelist);
9440d84d 3999
2bd0ea18
NS
4000 /*
4001 * Allocate the root inode and anything else in the proto file.
4002 */
b449c79e 4003 parse_proto(mp, &cli.fsx, &protostring);
2bd0ea18
NS
4004
4005 /*
9440d84d 4006 * Protect ourselves against possible stupidity
2bd0ea18
NS
4007 */
4008 if (XFS_INO_TO_AGNO(mp, mp->m_sb.sb_rootino) != 0) {
9440d84d
NS
4009 fprintf(stderr,
4010 _("%s: root inode created in AG %u, not AG 0\n"),
2bd0ea18
NS
4011 progname, XFS_INO_TO_AGNO(mp, mp->m_sb.sb_rootino));
4012 exit(1);
4013 }
4014
4015 /*
e2847e5c 4016 * Re-write multiple secondary superblocks with rootinode field set
2bd0ea18 4017 */
e2847e5c
DC
4018 if (mp->m_sb.sb_agcount > 1)
4019 rewrite_secondary_superblocks(mp);
2bd0ea18 4020
f1b058f9
NS
4021 /*
4022 * Dump all inodes and buffers before marking us all done.
4023 * Need to drop references to inodes we still hold, first.
4024 */
4025 libxfs_rtmount_destroy(mp);
f1b058f9
NS
4026 libxfs_bcache_purge();
4027
2bd0ea18
NS
4028 /*
4029 * Mark the filesystem ok.
4030 */
9440d84d 4031 buf = libxfs_getsb(mp, LIBXFS_EXIT_ON_FAILURE);
2bd0ea18 4032 (XFS_BUF_TO_SBP(buf))->sb_inprogress = 0;
9440d84d 4033 libxfs_writebuf(buf, LIBXFS_EXIT_ON_FAILURE);
2bd0ea18
NS
4034
4035 libxfs_umount(mp);
4036 if (xi.rtdev)
4037 libxfs_device_close(xi.rtdev);
4038 if (xi.logdev && xi.logdev != xi.ddev)
4039 libxfs_device_close(xi.logdev);
4040 libxfs_device_close(xi.ddev);
4041
4042 return 0;
4043}