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