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