2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
21 #include <blkid/blkid.h>
25 * Device topology information.
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 */
36 * Prototypes for internal functions.
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
);
47 * option tables for getsubopt calls
82 #define D_RTINHERIT 12
84 #define D_PROJINHERIT 13
86 #define D_EXTSZINHERIT 14
104 #define I_PROJID32BIT 6
134 #define L_LAZYSBCNTR 11
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)))
192 * Use this macro before we have superblock and mount structure
194 #define DTOBT(d) ((xfs_rfsblock_t)((d) >> (blocklog - BBSHIFT)))
197 * Use this for block reservations needed for mkfs's conditions
198 * (basically no fragmentation).
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)))
206 /* amount (in bytes) we zero at the beginning and end of the device to
207 * remove traces of other filesystems, raid superblocks, etc.
209 #define WHACK_SIZE (128 * 1024)
222 /* Handle data sunit/swidth options */
223 if (*dsunit
|| *dswidth
) {
226 _("data su/sw must not be used in "
227 "conjunction with data sunit/swidth\n"));
231 if ((*dsunit
&& !*dswidth
) || (!*dsunit
&& *dswidth
)) {
233 _("both data sunit and data swidth options "
234 "must be specified\n"));
240 if (*dsunit
|| *dswidth
) {
242 _("data sunit/swidth must not be used in "
243 "conjunction with data su/sw\n"));
247 if ((dsu
&& !dsw
) || (!dsu
&& dsw
)) {
249 _("both data su and data sw options "
250 "must be specified\n"));
256 _("data su must be a multiple of the "
257 "sector size (%d)\n"), dsectsz
);
261 *dsunit
= (int)BTOBBT(dsu
);
262 *dswidth
= *dsunit
* dsw
;
265 if (*dsunit
&& (*dswidth
% *dsunit
!= 0)) {
267 _("data stripe width (%d) must be a multiple of the "
268 "data stripe unit (%d)\n"), *dswidth
, *dsunit
);
272 /* Handle log sunit options */
277 _("log su should not be used in "
278 "conjunction with log sunit\n"));
286 _("log sunit should not be used in "
287 "conjunction with log su\n"));
290 *lsunit
= (int)BTOBBT(lsu
);
295 * Check for existing filesystem or partition table on device.
297 * 1 for existing fs or partition
298 * 0 for nothing found
299 * -1 for internal error
306 blkid_probe pr
= NULL
;
312 if (!device
|| !*device
)
315 ret
= -1; /* will reset on success of all setup calls */
317 fd
= open(device
, O_RDONLY
);
320 platform_findsizes(device
, fd
, &size
, &bsz
);
323 /* nothing to overwrite on a 0-length device */
329 pr
= blkid_new_probe_from_filename(device
);
333 ret
= blkid_probe_enable_partitions(pr
, 1);
337 ret
= blkid_do_fullprobe(pr
);
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.
345 * In addition print some useful diagnostics about what actually is
353 if (!blkid_probe_lookup_value(pr
, "TYPE", &type
, NULL
)) {
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
)) {
359 _("%s: %s appears to contain a partition "
360 "table (%s).\n"), progname
, device
, type
);
363 _("%s: %s appears to contain something weird "
364 "according to blkid\n"), progname
, device
);
370 blkid_free_probe(pr
);
373 _("%s: probe of %s failed, cannot detect "
374 "existing filesystem.\n"), progname
, device
);
378 static void blkid_get_topology(
392 /* can't get topology info from a file */
393 if (!stat(device
, &statbuf
) && S_ISREG(statbuf
.st_mode
))
396 pr
= blkid_new_probe_from_filename(device
);
400 tp
= blkid_probe_get_topology(pr
);
404 val
= blkid_topology_get_logical_sector_size(tp
);
406 val
= blkid_topology_get_physical_sector_size(tp
);
408 val
= blkid_topology_get_minimum_io_size(tp
);
410 val
= blkid_topology_get_optimal_io_size(tp
);
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.
418 if (*sunit
== *psectorsize
|| *swidth
== *psectorsize
) {
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..)
427 *sunit
= *sunit
>> 9;
428 *swidth
= *swidth
>> 9;
430 if (blkid_topology_get_alignment_offset(tp
) != 0) {
432 _("warning: device is not properly aligned %s\n"),
435 if (!force_overwrite
) {
437 _("Use -f to force usage of a misaligned device\n"));
441 /* Do not use physical sector size if the device is misaligned */
442 *psectorsize
= *lsectorsize
;
445 blkid_free_probe(pr
);
449 blkid_free_probe(pr
);
451 _("warning: unable to probe device topology for device %s\n"),
455 static void get_topology(
457 struct fs_topology
*ft
,
461 char *dfile
= xi
->volname
? xi
->volname
: xi
->dname
;
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.
469 (!stat(dfile
, &statbuf
) && S_ISREG(statbuf
.st_mode
))) {
471 int flags
= O_RDONLY
;
474 /* with xi->disfile we may not have the file yet! */
478 fd
= open(dfile
, flags
, 0666);
480 platform_findsizes(dfile
, fd
, &dummy
, &ft
->lsectorsize
);
482 ft
->psectorsize
= ft
->lsectorsize
;
484 ft
->psectorsize
= ft
->lsectorsize
= BBSIZE
;
486 blkid_get_topology(dfile
, &ft
->dsunit
, &ft
->dswidth
,
487 &ft
->lsectorsize
, &ft
->psectorsize
,
491 if (xi
->rtname
&& !xi
->risfile
) {
492 int sunit
, lsectorsize
, psectorsize
;
494 blkid_get_topology(xi
->rtname
, &sunit
, &ft
->rtswidth
,
495 &lsectorsize
, &psectorsize
, force_overwrite
);
500 fixup_log_stripe_unit(
503 xfs_rfsblock_t
*logblocks
,
506 __uint64_t tmp_logblocks
;
509 * Make sure that the log size is a multiple of the stripe unit
511 if ((*logblocks
% sunit
) != 0) {
513 tmp_logblocks
= ((*logblocks
+ (sunit
- 1))
516 * If the log is too large, round down
517 * instead of round up
519 if ((tmp_logblocks
> XFS_MAX_LOG_BLOCKS
) ||
520 ((tmp_logblocks
<< blocklog
) > XFS_MAX_LOG_BYTES
)) {
521 tmp_logblocks
= (*logblocks
/ sunit
) * sunit
;
523 *logblocks
= tmp_logblocks
;
525 fprintf(stderr
, _("log size %lld is not a multiple "
526 "of the log stripe unit %d\n"),
527 (long long) *logblocks
, sunit
);
534 fixup_internal_log_stripe(
537 xfs_fsblock_t logstart
,
540 xfs_rfsblock_t
*logblocks
,
544 if ((logstart
% sunit
) != 0) {
545 logstart
= ((logstart
+ (sunit
- 1))/sunit
) * sunit
;
549 fixup_log_stripe_unit(lsflag
, sunit
, logblocks
, blocklog
);
551 if (*logblocks
> agsize
- XFS_FSB_TO_AGBNO(mp
, logstart
)) {
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"));
562 validate_log_size(__uint64_t logblocks
, int blocklog
, int min_logblocks
)
564 if (logblocks
< min_logblocks
) {
566 _("log size %lld blocks too small, minimum size is %d blocks\n"),
567 (long long)logblocks
, min_logblocks
);
570 if (logblocks
> XFS_MAX_LOG_BLOCKS
) {
572 _("log size %lld blocks too large, maximum size is %lld blocks\n"),
573 (long long)logblocks
, XFS_MAX_LOG_BLOCKS
);
576 if ((logblocks
<< blocklog
) > XFS_MAX_LOG_BYTES
) {
578 _("log size %lld bytes too large, maximum size is %lld bytes\n"),
579 (long long)(logblocks
<< blocklog
), XFS_MAX_LOG_BYTES
);
585 calc_default_imaxpct(
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%).
597 if (dblocks
< TERABYTES(1, blocklog
)) {
598 return XFS_DFL_IMAXIMUM_PCT
;
599 } else if (dblocks
< TERABYTES(50, blocklog
)) {
608 calc_default_ag_geometry(
615 __uint64_t blocks
= 0;
619 * First handle the high extreme - the point at which we will
620 * always use the maximum AG size.
622 * This applies regardless of storage configuration.
624 if (dblocks
>= TERABYTES(32, blocklog
)) {
625 blocks
= XFS_AG_MAX_BLOCKS(blocklog
);
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.
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.
643 if (!multidisk
&& dblocks
>= MEGABYTES(128, blocklog
)) {
644 if (dblocks
>= TERABYTES(4, blocklog
)) {
645 blocks
= XFS_AG_MAX_BLOCKS(blocklog
);
649 } else if (dblocks
> GIGABYTES(512, blocklog
))
651 else if (dblocks
> GIGABYTES(8, blocklog
))
653 else if (dblocks
>= MEGABYTES(128, blocklog
))
655 else if (dblocks
>= MEGABYTES(64, blocklog
))
657 else if (dblocks
>= MEGABYTES(32, blocklog
))
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!
667 blocks
= dblocks
>> shift
;
668 if (dblocks
& xfs_mask32lo(shift
)) {
669 if (blocks
< XFS_AG_MAX_BLOCKS(blocklog
))
674 *agcount
= dblocks
/ blocks
+ (dblocks
% blocks
!= 0);
678 validate_ag_geometry(
684 if (agsize
< XFS_AG_MIN_BLOCKS(blocklog
)) {
686 _("agsize (%lld blocks) too small, need at least %lld blocks\n"),
688 (long long)XFS_AG_MIN_BLOCKS(blocklog
));
692 if (agsize
> XFS_AG_MAX_BLOCKS(blocklog
)) {
694 _("agsize (%lld blocks) too big, maximum is %lld blocks\n"),
696 (long long)XFS_AG_MAX_BLOCKS(blocklog
));
700 if (agsize
> dblocks
) {
702 _("agsize (%lld blocks) too big, data area is %lld blocks\n"),
703 (long long)agsize
, (long long)dblocks
);
707 if (agsize
< XFS_AG_MIN_BLOCKS(blocklog
)) {
709 _("too many allocation groups for size = %lld\n"),
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)));
717 if (agsize
> XFS_AG_MAX_BLOCKS(blocklog
)) {
719 _("too few allocation groups for size = %lld\n"), (long long)agsize
);
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)));
728 * If the last AG is too small, reduce the filesystem size
729 * and drop the blocks.
731 if ( dblocks
% agsize
!= 0 &&
732 (dblocks
% agsize
< XFS_AG_MIN_BLOCKS(blocklog
))) {
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
));
741 * If agcount is too large, make it smaller.
743 if (agcount
> XFS_MAX_AGNUMBER
+ 1) {
745 _("%lld allocation groups is too many, maximum is %lld\n"),
746 (long long)agcount
, (long long)XFS_MAX_AGNUMBER
+ 1);
752 zero_old_xfs_structures(
764 * We open regular files with O_TRUNC|O_CREAT. Nothing to do here...
766 if (xi
->disfile
&& xi
->dcreat
)
770 * read in existing filesystem superblock, use its geometry
771 * settings and zero the existing secondary superblocks.
773 buf
= memalign(libxfs_device_alignment(), new_sb
->sb_sectsize
);
776 _("error reading existing superblock -- failed to memalign buffer\n"));
779 memset(buf
, 0, new_sb
->sb_sectsize
);
781 tmp
= pread(xi
->dfd
, buf
, new_sb
->sb_sectsize
, 0);
783 fprintf(stderr
, _("existing superblock read failed: %s\n"),
787 if (tmp
!= new_sb
->sb_sectsize
) {
789 _("warning: could not read existing superblock, skip zeroing\n"));
792 libxfs_sb_from_disk(&sb
, buf
);
795 * perform same basic superblock validation to make sure we
796 * actually zero secondary blocks
798 if (sb
.sb_magicnum
!= XFS_SB_MAGIC
|| sb
.sb_blocksize
== 0)
801 for (bsize
= 1, i
= 0; bsize
< sb
.sb_blocksize
&&
802 i
< sizeof(sb
.sb_blocksize
) * NBBY
; i
++)
805 if (i
< XFS_MIN_BLOCKSIZE_LOG
|| i
> XFS_MAX_BLOCKSIZE_LOG
||
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
))
815 * block size and basic geometry seems alright, zero the secondaries.
817 memset(buf
, 0, new_sb
->sb_sectsize
);
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)
830 discard_blocks(dev_t dev
, __uint64_t nsectors
)
835 * We intentionally ignore errors from the discard ioctl. It is
836 * not necessary for the mkfs functionality but just an optimization.
838 fd
= libxfs_device_to_fd(dev
);
840 platform_discard_blocks(fd
, 0, nsectors
<< 9);
853 xfs_alloc_rec_t
*arec
;
856 struct xfs_btree_block
*block
;
859 unsigned int blocksize
;
866 xfs_rfsblock_t dblocks
;
893 xfs_agnumber_t logagno
;
894 xfs_rfsblock_t logblocks
;
898 xfs_fsblock_t logstart
;
913 xfs_extlen_t nbmblocks
;
917 xfs_alloc_rec_t
*nrec
;
928 xfs_rfsblock_t rtblocks
;
929 xfs_extlen_t rtextblocks
;
930 xfs_rtblock_t rtextents
;
936 unsigned int sectorsize
;
937 __uint64_t sector_mask
;
940 __uint64_t tmp_agsize
;
944 struct fs_topology ft
;
945 int lazy_sb_counters
;
951 progname
= basename(argv
[0]);
952 setlocale(LC_ALL
, "");
953 bindtextdomain(PACKAGE
, LOCALEDIR
);
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;
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
;
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;
981 lazy_sb_counters
= 1;
986 memset(&fsx
, 0, sizeof(fsx
));
988 memset(&xi
, 0, sizeof(xi
));
989 xi
.isdirect
= LIBXFS_DIRECT
;
990 xi
.isreadonly
= LIBXFS_EXCLUSIVELY
;
992 while ((c
= getopt(argc
, argv
, "b:d:i:l:L:m:n:KNp:qr:s:CfV")) != EOF
) {
1000 while (*p
!= '\0') {
1003 switch (getsubopt(&p
, (constpp
)bopts
, &value
)) {
1005 if (!value
|| *value
== '\0')
1006 reqval('b', bopts
, B_LOG
);
1008 respec('b', bopts
, B_LOG
);
1010 conflict('b', bopts
, B_SIZE
,
1012 blocklog
= atoi(value
);
1014 illegal(value
, "b log");
1015 blocksize
= 1 << blocklog
;
1019 if (!value
|| *value
== '\0')
1020 reqval('b', bopts
, B_SIZE
);
1022 respec('b', bopts
, B_SIZE
);
1024 conflict('b', bopts
, B_LOG
,
1027 blocksize
, sectorsize
, value
);
1028 if (blocksize
<= 0 ||
1030 illegal(value
, "b size");
1031 blocklog
= libxfs_highbit32(blocksize
);
1035 unknown('b', value
);
1041 while (*p
!= '\0') {
1044 switch (getsubopt(&p
, (constpp
)dopts
, &value
)) {
1046 if (!value
|| *value
== '\0')
1047 reqval('d', dopts
, D_AGCOUNT
);
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");
1057 if (!value
|| *value
== '\0')
1058 reqval('d', dopts
, D_AGSIZE
);
1060 respec('d', dopts
, D_AGSIZE
);
1062 blocksize
, sectorsize
, value
);
1066 if (!value
|| *value
== '\0')
1068 xi
.disfile
= atoi(value
);
1069 if (xi
.disfile
< 0 || xi
.disfile
> 1)
1070 illegal(value
, "d file");
1071 if (xi
.disfile
&& !Nflag
)
1075 if (!value
|| *value
== '\0')
1076 reqval('d', dopts
, D_NAME
);
1078 respec('d', dopts
, D_NAME
);
1082 if (!value
|| *value
== '\0')
1083 reqval('d', dopts
, D_SIZE
);
1085 respec('d', dopts
, D_SIZE
);
1089 if (!value
|| *value
== '\0')
1090 reqval('d', dopts
, D_SUNIT
);
1092 respec('d', dopts
, D_SUNIT
);
1094 conflict('d', dopts
, D_NOALIGN
,
1096 if (!isdigits(value
)) {
1098 _("%s: Specify data sunit in 512-byte blocks, no unit suffix\n"),
1102 dsunit
= cvtnum(0, 0, value
);
1105 if (!value
|| *value
== '\0')
1106 reqval('d', dopts
, D_SWIDTH
);
1108 respec('d', dopts
, D_SWIDTH
);
1110 conflict('d', dopts
, D_NOALIGN
,
1112 if (!isdigits(value
)) {
1114 _("%s: Specify data swidth in 512-byte blocks, no unit suffix\n"),
1118 dswidth
= cvtnum(0, 0, value
);
1121 if (!value
|| *value
== '\0')
1122 reqval('d', dopts
, D_SU
);
1124 respec('d', dopts
, D_SU
);
1126 conflict('d', dopts
, D_NOALIGN
,
1129 blocksize
, sectorsize
, value
);
1132 if (!value
|| *value
== '\0')
1133 reqval('d', dopts
, D_SW
);
1135 respec('d', dopts
, D_SW
);
1137 conflict('d', dopts
, D_NOALIGN
,
1139 if (!isdigits(value
)) {
1141 _("%s: Specify data sw as multiple of su, no unit suffix\n"),
1145 dsw
= cvtnum(0, 0, value
);
1149 conflict('d', dopts
, D_SU
,
1152 conflict('d', dopts
, D_SUNIT
,
1155 conflict('d', dopts
, D_SW
,
1158 conflict('d', dopts
, D_SWIDTH
,
1163 if (!value
|| *value
== '\0')
1164 reqval('d', dopts
, D_SECTLOG
);
1166 respec('d', dopts
, D_SECTLOG
);
1168 conflict('d', dopts
, D_SECTSIZE
,
1170 sectorlog
= atoi(value
);
1172 illegal(value
, "d sectlog");
1173 sectorsize
= 1 << sectorlog
;
1177 if (!value
|| *value
== '\0')
1178 reqval('d', dopts
, D_SECTSIZE
);
1180 respec('d', dopts
, D_SECTSIZE
);
1182 conflict('d', dopts
, D_SECTLOG
,
1184 sectorsize
= cvtnum(
1185 blocksize
, sectorsize
, value
);
1186 if (sectorsize
<= 0 ||
1187 !ispow2(sectorsize
))
1188 illegal(value
, "d sectsize");
1190 libxfs_highbit32(sectorsize
);
1195 XFS_DIFLAG_RTINHERIT
;
1198 if (!value
|| *value
== '\0')
1199 reqval('d', dopts
, D_PROJINHERIT
);
1200 fsx
.fsx_projid
= atoi(value
);
1202 XFS_DIFLAG_PROJINHERIT
;
1204 case D_EXTSZINHERIT
:
1205 if (!value
|| *value
== '\0')
1206 reqval('d', dopts
, D_EXTSZINHERIT
);
1207 fsx
.fsx_extsize
= atoi(value
);
1209 XFS_DIFLAG_EXTSZINHERIT
;
1212 unknown('d', value
);
1218 while (*p
!= '\0') {
1221 switch (getsubopt(&p
, (constpp
)iopts
, &value
)) {
1223 if (!value
|| *value
== '\0')
1225 iaflag
= atoi(value
);
1226 if (iaflag
< 0 || iaflag
> 1)
1227 illegal(value
, "i align");
1230 if (!value
|| *value
== '\0')
1231 reqval('i', iopts
, I_LOG
);
1233 respec('i', iopts
, I_LOG
);
1235 conflict('i', iopts
, I_PERBLOCK
,
1238 conflict('i', iopts
, I_SIZE
,
1240 inodelog
= atoi(value
);
1242 illegal(value
, "i log");
1243 isize
= 1 << inodelog
;
1247 if (!value
|| *value
== '\0')
1248 reqval('i', iopts
, I_MAXPCT
);
1250 respec('i', iopts
, I_MAXPCT
);
1251 imaxpct
= atoi(value
);
1252 if (imaxpct
< 0 || imaxpct
> 100)
1253 illegal(value
, "i maxpct");
1257 if (!value
|| *value
== '\0')
1258 reqval('i', iopts
, I_PERBLOCK
);
1260 conflict('i', iopts
, I_LOG
,
1263 respec('i', iopts
, I_PERBLOCK
);
1265 conflict('i', iopts
, I_SIZE
,
1267 inopblock
= atoi(value
);
1269 XFS_MIN_INODE_PERBLOCK
||
1271 illegal(value
, "i perblock");
1275 if (!value
|| *value
== '\0')
1276 reqval('i', iopts
, I_SIZE
);
1278 conflict('i', iopts
, I_LOG
,
1281 conflict('i', iopts
, I_PERBLOCK
,
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
);
1292 if (!value
|| *value
== '\0')
1293 reqval('i', iopts
, I_ATTR
);
1296 illegal(value
, "i attr");
1300 if (!value
|| *value
== '\0')
1304 illegal(value
, "i projid32bit");
1305 projid16bit
= c
? 0 : 1;
1308 if (!value
|| *value
== '\0')
1310 spinodes
= atoi(value
);
1311 if (spinodes
< 0 || spinodes
> 1)
1312 illegal(value
, "i spinodes");
1315 unknown('i', value
);
1321 while (*p
!= '\0') {
1324 switch (getsubopt(&p
, (constpp
)lopts
, &value
)) {
1326 if (!value
|| *value
== '\0')
1327 reqval('l', lopts
, L_AGNUM
);
1329 respec('l', lopts
, L_AGNUM
);
1331 conflict('l', lopts
, L_AGNUM
, L_DEV
);
1332 logagno
= atoi(value
);
1336 if (!value
|| *value
== '\0')
1339 conflict('l', lopts
, L_INTERNAL
,
1341 xi
.lisfile
= atoi(value
);
1342 if (xi
.lisfile
< 0 || xi
.lisfile
> 1)
1343 illegal(value
, "l file");
1348 if (!value
|| *value
== '\0')
1351 conflict('l', lopts
, L_INTERNAL
, L_DEV
);
1353 conflict('l', lopts
, L_FILE
,
1356 respec('l', lopts
, L_INTERNAL
);
1357 loginternal
= atoi(value
);
1358 if (loginternal
< 0 || loginternal
> 1)
1359 illegal(value
, "l internal");
1363 if (!value
|| *value
== '\0')
1364 reqval('l', lopts
, L_SU
);
1366 respec('l', lopts
, L_SU
);
1368 blocksize
, sectorsize
, value
);
1372 if (!value
|| *value
== '\0')
1373 reqval('l', lopts
, L_SUNIT
);
1375 respec('l', lopts
, L_SUNIT
);
1376 if (!isdigits(value
)) {
1378 _("Specify log sunit in 512-byte blocks, no size suffix\n"));
1381 lsunit
= cvtnum(0, 0, value
);
1387 conflict('l', lopts
, L_AGNUM
, L_DEV
);
1389 conflict('l', lopts
, L_INTERNAL
, L_DEV
);
1390 if (!value
|| *value
== '\0')
1391 reqval('l', lopts
, L_NAME
);
1393 respec('l', lopts
, L_NAME
);
1400 if (!value
|| *value
== '\0')
1401 reqval('l', lopts
, L_VERSION
);
1403 respec('l', lopts
, L_VERSION
);
1404 logversion
= atoi(value
);
1405 if (logversion
< 1 || logversion
> 2)
1406 illegal(value
, "l version");
1410 if (!value
|| *value
== '\0')
1411 reqval('l', lopts
, L_SIZE
);
1413 respec('l', lopts
, L_SIZE
);
1418 if (!value
|| *value
== '\0')
1419 reqval('l', lopts
, L_SECTLOG
);
1421 respec('l', lopts
, L_SECTLOG
);
1423 conflict('l', lopts
, L_SECTSIZE
,
1425 lsectorlog
= atoi(value
);
1426 if (lsectorlog
<= 0)
1427 illegal(value
, "l sectlog");
1428 lsectorsize
= 1 << lsectorlog
;
1432 if (!value
|| *value
== '\0')
1433 reqval('l', lopts
, L_SECTSIZE
);
1435 respec('l', lopts
, L_SECTSIZE
);
1437 conflict('l', lopts
, L_SECTLOG
,
1439 lsectorsize
= cvtnum(
1440 blocksize
, sectorsize
, value
);
1441 if (lsectorsize
<= 0 ||
1442 !ispow2(lsectorsize
))
1443 illegal(value
, "l sectsize");
1445 libxfs_highbit32(lsectorsize
);
1449 if (!value
|| *value
== '\0')
1454 illegal(value
, "l lazy-count");
1455 lazy_sb_counters
= c
;
1458 unknown('l', value
);
1463 if (strlen(optarg
) > sizeof(sbp
->sb_fname
))
1464 illegal(optarg
, "L");
1469 while (*p
!= '\0') {
1472 switch (getsubopt(&p
, (constpp
)mopts
, &value
)) {
1474 if (!value
|| *value
== '\0')
1475 reqval('m', mopts
, M_CRC
);
1478 illegal(value
, "m crc");
1480 if (nftype
&& crcs_enabled
) {
1482 _("cannot specify both crc and ftype\n"));
1487 if (!value
|| *value
== '\0')
1488 reqval('m', mopts
, M_CRC
);
1491 illegal(value
, "m finobt");
1496 unknown('m', value
);
1502 while (*p
!= '\0') {
1505 switch (getsubopt(&p
, (constpp
)nopts
, &value
)) {
1507 if (!value
|| *value
== '\0')
1508 reqval('n', nopts
, N_LOG
);
1510 respec('n', nopts
, N_LOG
);
1512 conflict('n', nopts
, N_SIZE
,
1514 dirblocklog
= atoi(value
);
1515 if (dirblocklog
<= 0)
1516 illegal(value
, "n log");
1517 dirblocksize
= 1 << dirblocklog
;
1521 if (!value
|| *value
== '\0')
1522 reqval('n', nopts
, N_SIZE
);
1524 respec('n', nopts
, N_SIZE
);
1526 conflict('n', nopts
, N_LOG
,
1528 dirblocksize
= cvtnum(
1529 blocksize
, sectorsize
, value
);
1530 if (dirblocksize
<= 0 ||
1531 !ispow2(dirblocksize
))
1532 illegal(value
, "n size");
1534 libxfs_highbit32(dirblocksize
);
1538 if (!value
|| *value
== '\0')
1539 reqval('n', nopts
, N_VERSION
);
1541 respec('n', nopts
, N_VERSION
);
1542 if (!strcasecmp(value
, "ci")) {
1543 nci
= 1; /* ASCII CI mode */
1545 dirversion
= atoi(value
);
1546 if (dirversion
!= 2)
1553 if (!value
|| *value
== '\0')
1554 reqval('n', nopts
, N_FTYPE
);
1556 respec('n', nopts
, N_FTYPE
);
1557 dirftype
= atoi(value
);
1560 _("cannot specify both crc and ftype\n"));
1566 unknown('n', value
);
1578 respec('p', NULL
, 0);
1586 while (*p
!= '\0') {
1589 switch (getsubopt(&p
, (constpp
)ropts
, &value
)) {
1591 if (!value
|| *value
== '\0')
1592 reqval('r', ropts
, R_EXTSIZE
);
1594 respec('r', ropts
, R_EXTSIZE
);
1598 if (!value
|| *value
== '\0')
1600 xi
.risfile
= atoi(value
);
1601 if (xi
.risfile
< 0 || xi
.risfile
> 1)
1602 illegal(value
, "r file");
1608 if (!value
|| *value
== '\0')
1609 reqval('r', ropts
, R_NAME
);
1611 respec('r', ropts
, R_NAME
);
1615 if (!value
|| *value
== '\0')
1616 reqval('r', ropts
, R_SIZE
);
1618 respec('r', ropts
, R_SIZE
);
1625 unknown('r', value
);
1631 while (*p
!= '\0') {
1634 switch (getsubopt(&p
, (constpp
)sopts
, &value
)) {
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
,
1644 sectorlog
= atoi(value
);
1646 illegal(value
, "s sectlog");
1647 lsectorlog
= sectorlog
;
1648 sectorsize
= 1 << sectorlog
;
1649 lsectorsize
= sectorsize
;
1650 lslflag
= slflag
= 1;
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
,
1661 sectorsize
= cvtnum(
1662 blocksize
, sectorsize
, value
);
1663 if (sectorsize
<= 0 ||
1664 !ispow2(sectorsize
))
1665 illegal(value
, "s sectsize");
1666 lsectorsize
= sectorsize
;
1668 libxfs_highbit32(sectorsize
);
1669 lsectorlog
= sectorlog
;
1670 lssflag
= ssflag
= 1;
1673 unknown('s', value
);
1678 printf(_("%s version %s\n"), progname
, VERSION
);
1681 unknown(optopt
, "");
1684 if (argc
- optind
> 1) {
1685 fprintf(stderr
, _("extra arguments\n"));
1687 } else if (argc
- optind
== 1) {
1688 dfile
= xi
.volname
= argv
[optind
];
1691 _("cannot specify both %s and -d name=%s\n"),
1692 xi
.volname
, xi
.dname
);
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.
1703 if (!blflag
&& !bsflag
) {
1704 blocklog
= XFS_DFL_BLOCKSIZE_LOG
;
1705 blocksize
= 1 << XFS_DFL_BLOCKSIZE_LOG
;
1707 if (blocksize
< XFS_MIN_BLOCKSIZE
|| blocksize
> XFS_MAX_BLOCKSIZE
) {
1708 fprintf(stderr
, _("illegal block size %d\n"), blocksize
);
1711 if (crcs_enabled
&& blocksize
< XFS_MIN_CRC_BLOCKSIZE
) {
1713 _("Minimum block size for CRC enabled filesystems is %d bytes.\n"),
1714 XFS_MIN_CRC_BLOCKSIZE
);
1718 memset(&ft
, 0, sizeof(ft
));
1719 get_topology(&xi
, &ft
, force_overwrite
);
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
1730 /* Older kernels may not have physical/logical distinction */
1731 if (!ft
.psectorsize
)
1732 ft
.psectorsize
= ft
.lsectorsize
;
1734 sectorsize
= ft
.psectorsize
? ft
.psectorsize
:
1737 if ((blocksize
< sectorsize
) && (blocksize
>= ft
.lsectorsize
)) {
1739 _("specified blocksize %d is less than device physical sector size %d\n"),
1740 blocksize
, ft
.psectorsize
);
1742 _("switching to logical sector size %d\n"),
1744 sectorsize
= ft
.lsectorsize
? ft
.lsectorsize
:
1750 sectorlog
= libxfs_highbit32(sectorsize
);
1752 lsectorsize
= sectorsize
;
1753 lsectorlog
= sectorlog
;
1757 if (sectorsize
< XFS_MIN_SECTORSIZE
||
1758 sectorsize
> XFS_MAX_SECTORSIZE
|| sectorsize
> blocksize
) {
1760 fprintf(stderr
, _("illegal sector size %d\n"), sectorsize
);
1763 _("block size %d cannot be smaller than logical sector size %d\n"),
1764 blocksize
, ft
.lsectorsize
);
1767 if (sectorsize
< ft
.lsectorsize
) {
1768 fprintf(stderr
, _("illegal sector size %d; hw sector is %d\n"),
1769 sectorsize
, ft
.lsectorsize
);
1772 if (lsectorsize
< XFS_MIN_SECTORSIZE
||
1773 lsectorsize
> XFS_MAX_SECTORSIZE
|| lsectorsize
> blocksize
) {
1774 fprintf(stderr
, _("illegal log sector size %d\n"), lsectorsize
);
1776 } else if (lsectorsize
> XFS_MIN_SECTORSIZE
&& !lsu
&& !lsunit
) {
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.
1787 /* minimum inode size is 512 bytes, ipflag checked later */
1788 if ((isflag
|| ilflag
) && inodelog
< XFS_DINODE_DFL_CRC_LOG
) {
1790 _("Minimum inode size for CRCs is %d bytes\n"),
1791 1 << XFS_DINODE_DFL_CRC_LOG
);
1795 /* inodes always aligned */
1798 _("Inodes always aligned for CRC enabled filesytems\n"));
1802 /* lazy sb counters always on */
1803 if (lazy_sb_counters
!= 1) {
1805 _("Lazy superblock counted always enabled for CRC enabled filesytems\n"));
1809 /* version 2 logs always on */
1810 if (logversion
!= 2) {
1812 _("V2 logs always enabled for CRC enabled filesytems\n"));
1816 /* attr2 always on */
1817 if (attrversion
!= 2) {
1819 _("V2 attribute format always enabled on CRC enabled filesytems\n"));
1823 /* 32 bit project quota always on */
1824 /* attr2 always on */
1825 if (projid16bit
== 1) {
1827 _("32 bit Project IDs always enabled on CRC enabled filesytems\n"));
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
1839 if (finobt
&& finobtflag
) {
1841 _("warning: finobt not supported without CRC support, disabled.\n"));
1846 if (spinodes
&& !crcs_enabled
) {
1848 _("warning: sparse inodes not supported without CRC support, disabled.\n"));
1852 if (nsflag
|| nlflag
) {
1853 if (dirblocksize
< blocksize
||
1854 dirblocksize
> XFS_MAX_BLOCKSIZE
) {
1855 fprintf(stderr
, _("illegal directory block size %d\n"),
1860 if (blocksize
< (1 << XFS_MIN_REC_DIRSIZE
))
1861 dirblocklog
= XFS_MIN_REC_DIRSIZE
;
1863 dirblocklog
= blocklog
;
1864 dirblocksize
= 1 << dirblocklog
;
1867 if (daflag
&& dasize
) {
1869 _("both -d agcount= and agsize= specified, use one or the other\n"));
1873 if (xi
.disfile
&& (!dsize
|| !xi
.dname
)) {
1875 _("if -d file then -d name and -d size are required\n"));
1881 dbytes
= cvtnum(blocksize
, sectorsize
, dsize
);
1882 if (dbytes
% XFS_MIN_BLOCKSIZE
) {
1884 _("illegal data length %lld, not a multiple of %d\n"),
1885 (long long)dbytes
, XFS_MIN_BLOCKSIZE
);
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
));
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
;
1903 if (crcs_enabled
&& inodelog
< XFS_DINODE_DFL_CRC_LOG
) {
1905 _("Minimum inode size for CRCs is %d bytes\n"),
1906 1 << XFS_DINODE_DFL_CRC_LOG
);
1910 if (xi
.lisfile
&& (!logsize
|| !xi
.logname
)) {
1912 _("if -l file then -l name and -l size are required\n"));
1916 __uint64_t logbytes
;
1918 logbytes
= cvtnum(blocksize
, sectorsize
, logsize
);
1919 if (logbytes
% XFS_MIN_BLOCKSIZE
) {
1921 _("illegal log length %lld, not a multiple of %d\n"),
1922 (long long)logbytes
, XFS_MIN_BLOCKSIZE
);
1925 logblocks
= (xfs_rfsblock_t
)(logbytes
>> blocklog
);
1926 if (logbytes
% blocksize
)
1928 _("warning: log length %lld not a multiple of %d, truncated to %lld\n"),
1929 (long long)logbytes
, blocksize
,
1930 (long long)(logblocks
<< blocklog
));
1932 if (xi
.risfile
&& (!rtsize
|| !xi
.rtname
)) {
1934 _("if -r file then -r name and -r size are required\n"));
1940 rtbytes
= cvtnum(blocksize
, sectorsize
, rtsize
);
1941 if (rtbytes
% XFS_MIN_BLOCKSIZE
) {
1943 _("illegal rt length %lld, not a multiple of %d\n"),
1944 (long long)rtbytes
, XFS_MIN_BLOCKSIZE
);
1947 rtblocks
= (xfs_rfsblock_t
)(rtbytes
>> blocklog
);
1948 if (rtbytes
% blocksize
)
1950 _("warning: rt length %lld not a multiple of %d, truncated to %lld\n"),
1951 (long long)rtbytes
, blocksize
,
1952 (long long)(rtblocks
<< blocklog
));
1955 * If specified, check rt extent size against its constraints.
1958 __uint64_t rtextbytes
;
1960 rtextbytes
= cvtnum(blocksize
, sectorsize
, rtextsize
);
1961 if (rtextbytes
% blocksize
) {
1963 _("illegal rt extent size %lld, not a multiple of %d\n"),
1964 (long long)rtextbytes
, blocksize
);
1967 if (rtextbytes
> XFS_MAX_RTEXTSIZE
) {
1969 _("rt extent size %s too large, maximum %d\n"),
1970 rtextsize
, XFS_MAX_RTEXTSIZE
);
1973 if (rtextbytes
< XFS_MIN_RTEXTSIZE
) {
1975 _("rt extent size %s too small, minimum %d\n"),
1976 rtextsize
, XFS_MIN_RTEXTSIZE
);
1979 rtextblocks
= (xfs_extlen_t
)(rtextbytes
>> blocklog
);
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.
1987 __uint64_t rtextbytes
;
1989 if (!norsflag
&& !xi
.risfile
&& !(!rtsize
&& xi
.disfile
))
1990 rswidth
= ft
.rtswidth
;
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
;
2004 rtextblocks
= (blocksize
< XFS_MIN_RTEXTSIZE
) ?
2005 XFS_MIN_RTEXTSIZE
>> blocklog
: 1;
2008 ASSERT(rtextblocks
);
2011 * Check some argument sizes against mins, maxes.
2013 if (isize
> blocksize
/ XFS_MIN_INODE_PERBLOCK
||
2014 isize
< XFS_DINODE_MIN_SIZE
||
2015 isize
> XFS_DINODE_MAX_SIZE
) {
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
)
2023 _("allowable inode size with %d byte blocks is %d\n"),
2024 blocksize
, XFS_DINODE_MIN_SIZE
);
2027 _("allowable inode size with %d byte blocks is between %d and %d\n"),
2028 blocksize
, XFS_DINODE_MIN_SIZE
, maxsz
);
2032 /* if lsu or lsunit was specified, automatically use v2 logs */
2033 if ((lsu
|| lsunit
) && logversion
== 1) {
2035 _("log stripe unit specified, using v2 logs\n"));
2039 calc_stripe_factors(dsu
, dsw
, sectorsize
, lsu
, lsectorsize
,
2040 &dsunit
, &dswidth
, &lsunit
);
2042 xi
.setblksize
= sectorsize
;
2045 * Initialize. This will open the log and rt devices as well.
2047 if (!libxfs_init(&xi
))
2050 fprintf(stderr
, _("no device name given in argument list\n"));
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.
2061 * So, we reduce the size (in basic blocks) to a perfect
2062 * multiple of the sector size, or 1024, whichever is larger.
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
);
2070 if (!force_overwrite
) {
2071 if (check_overwrite(dfile
) ||
2072 check_overwrite(logfile
) ||
2073 check_overwrite(xi
.rtname
)) {
2075 _("%s: Use the -f option to force overwrite.\n"),
2081 if (discard
&& !Nflag
) {
2082 discard_blocks(xi
.ddev
, xi
.dsize
);
2084 discard_blocks(xi
.rtdev
, xi
.rtsize
);
2085 if (xi
.logdev
&& xi
.logdev
!= xi
.ddev
)
2086 discard_blocks(xi
.logdev
, xi
.logBBsize
);
2089 if (!liflag
&& !ldflag
)
2090 loginternal
= xi
.logdev
== 0;
2092 logfile
= xi
.logname
;
2093 else if (loginternal
)
2094 logfile
= _("internal log");
2095 else if (xi
.volname
&& xi
.logdev
)
2096 logfile
= _("volume log");
2098 fprintf(stderr
, _("no log subvolume or internal log\n"));
2104 if (xi
.volname
&& xi
.rtdev
)
2105 rtfile
= _("volume rt");
2108 if (dsize
&& xi
.dsize
> 0 && dblocks
> DTOBT(xi
.dsize
)) {
2110 _("size %s specified for data subvolume is too large, "
2111 "maximum is %lld blocks\n"),
2112 dsize
, (long long)DTOBT(xi
.dsize
));
2114 } else if (!dsize
&& xi
.dsize
> 0)
2115 dblocks
= DTOBT(xi
.dsize
);
2117 fprintf(stderr
, _("can't get size of data subvolume\n"));
2120 if (dblocks
< XFS_MIN_DATA_BLOCKS
) {
2122 _("size %lld of data subvolume is too small, minimum %d blocks\n"),
2123 (long long)dblocks
, XFS_MIN_DATA_BLOCKS
);
2127 if (loginternal
&& xi
.logdev
) {
2129 _("can't have both external and internal logs\n"));
2131 } else if (loginternal
&& sectorsize
!= lsectorsize
) {
2133 _("data and log sector sizes must be equal for internal logs\n"));
2137 if (xi
.dbsize
> sectorsize
) {
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
);
2143 if (!loginternal
&& xi
.lbsize
> lsectorsize
) {
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
);
2149 if (rtsize
&& xi
.rtsize
> 0 && xi
.rtbsize
> sectorsize
) {
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
);
2156 if (rtsize
&& xi
.rtsize
> 0 && rtblocks
> DTOBT(xi
.rtsize
)) {
2158 _("size %s specified for rt subvolume is too large, "
2159 "maximum is %lld blocks\n"),
2160 rtsize
, (long long)DTOBT(xi
.rtsize
));
2162 } else if (!rtsize
&& xi
.rtsize
> 0)
2163 rtblocks
= DTOBT(xi
.rtsize
);
2164 else if (rtsize
&& !xi
.rtdev
) {
2166 _("size specified for non-existent rt subvolume\n"));
2170 rtextents
= rtblocks
/ rtextblocks
;
2171 nbmblocks
= (xfs_extlen_t
)howmany(rtextents
, NBBY
* blocksize
);
2173 rtextents
= rtblocks
= 0;
2179 if (ft
.dsunit
&& ft
.dsunit
!= dsunit
) {
2181 _("%s: Specified data stripe unit %d "
2182 "is not the same as the volume stripe "
2184 progname
, dsunit
, ft
.dsunit
);
2186 if (ft
.dswidth
&& ft
.dswidth
!= dswidth
) {
2188 _("%s: Specified data stripe width %d "
2189 "is not the same as the volume stripe "
2191 progname
, dswidth
, ft
.dswidth
);
2195 dswidth
= ft
.dswidth
;
2198 } /* else dsunit & dswidth can't be set if nodsflag is set */
2200 if (dasize
) { /* User-specified AG size */
2202 * Check specified agsize is a multiple of blocksize.
2204 if (agsize
% blocksize
) {
2206 _("agsize (%lld) not a multiple of fs blk size (%d)\n"),
2207 (long long)agsize
, blocksize
);
2210 agsize
/= blocksize
;
2211 agcount
= dblocks
/ agsize
+ (dblocks
% agsize
!= 0);
2213 } else if (daflag
) { /* User-specified AG count */
2214 agsize
= dblocks
/ agcount
+ (dblocks
% agcount
!= 0);
2216 calc_default_ag_geometry(blocklog
, dblocks
,
2217 dsunit
| dswidth
, &agsize
, &agcount
);
2221 * If dsunit is a multiple of fs blocksize, then check that is a
2222 * multiple of the agsize too
2224 if (dsunit
&& !(BBTOB(dsunit
) % blocksize
) &&
2225 dswidth
&& !(BBTOB(dswidth
) % blocksize
)) {
2227 /* convert from 512 byte blocks to fs blocksize */
2228 dsunit
= DTOBT(dsunit
);
2229 dswidth
= DTOBT(dswidth
);
2232 * agsize is not a multiple of dsunit
2234 if ((agsize
% dsunit
) != 0) {
2236 * Round up to stripe unit boundary. Also make sure
2237 * that agsize is still larger than
2238 * XFS_AG_MIN_BLOCKS(blocklog)
2240 tmp_agsize
= ((agsize
+ (dsunit
- 1))/ dsunit
) * dsunit
;
2242 * Round down to stripe unit boundary if rounding up
2243 * created an AG size that is larger than the AG max.
2245 if (tmp_agsize
> XFS_AG_MAX_BLOCKS(blocklog
))
2246 tmp_agsize
= ((agsize
) / dsunit
) * dsunit
;
2248 if ((tmp_agsize
>= XFS_AG_MIN_BLOCKS(blocklog
)) &&
2249 (tmp_agsize
<= XFS_AG_MAX_BLOCKS(blocklog
))) {
2250 agsize
= tmp_agsize
;
2252 agcount
= dblocks
/agsize
+
2253 (dblocks
% agsize
!= 0);
2256 _("agsize rounded to %lld, swidth = %d\n"),
2257 (long long)agsize
, dswidth
);
2260 dsunit
= dswidth
= 0;
2263 * agsize is out of bounds, this will
2264 * print nice details & exit.
2266 validate_ag_geometry(blocklog
, dblocks
,
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
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
;
2286 if (daflag
|| dasize
) {
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
);
2293 agsize
= tmp_agsize
;
2294 agcount
= dblocks
/agsize
+ (dblocks
% agsize
!= 0);
2296 * If the last AG is too small, reduce the
2297 * filesystem size and drop the blocks.
2299 if ( dblocks
% agsize
!= 0 &&
2301 XFS_AG_MIN_BLOCKS(blocklog
))) {
2302 dblocks
= (xfs_rfsblock_t
)((agcount
- 1) * agsize
);
2304 ASSERT(agcount
!= 0);
2310 dsunit
= dswidth
= 0;
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
),
2322 * If the last AG is too small, reduce the filesystem size
2323 * and drop the blocks.
2325 if ( dblocks
% agsize
!= 0 &&
2326 (dblocks
% agsize
< XFS_AG_MIN_BLOCKS(blocklog
))) {
2328 dblocks
= (xfs_rfsblock_t
)((agcount
- 1) * agsize
);
2330 ASSERT(agcount
!= 0);
2333 validate_ag_geometry(blocklog
, dblocks
, agsize
, agcount
);
2336 imaxpct
= calc_default_imaxpct(blocklog
, dblocks
);
2339 * check that log sunit is modulo fsblksize or default it to dsunit.
2343 if ((BBTOB(lsunit
) % blocksize
!= 0)) {
2345 _("log stripe unit (%d) must be a multiple of the block size (%d)\n"),
2346 BBTOB(lsunit
), blocksize
);
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 */
2356 if (logversion
== 2 && (lsunit
* blocksize
) > 256 * 1024) {
2357 /* Warn only if specified on commandline */
2358 if (lsuflag
|| lsunitflag
) {
2360 _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n"),
2361 (lsunit
* blocksize
));
2363 _("log stripe unit adjusted to 32KiB\n"));
2365 lsunit
= (32 * 1024) >> blocklog
;
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
)) {
2377 _("size %s specified for log subvolume is too large, maximum is %lld blocks\n"),
2378 logsize
, (long long)DTOBT(xi
.logBBsize
));
2380 } else if (!logsize
&& xi
.logBBsize
> 0) {
2381 logblocks
= DTOBT(xi
.logBBsize
);
2382 } else if (logsize
&& !xi
.logdev
&& !loginternal
) {
2384 _("size specified for non-existent log subvolume\n"));
2386 } else if (loginternal
&& logsize
&& logblocks
>= dblocks
) {
2387 fprintf(stderr
, _("size %lld too large for internal log\n"),
2388 (long long)logblocks
);
2390 } else if (!loginternal
&& !xi
.logdev
) {
2392 } else if (loginternal
&& !logsize
) {
2394 if (dblocks
< GIGABYTES(1, blocklog
)) {
2395 /* tiny filesystems get minimum sized logs. */
2396 logblocks
= min_logblocks
;
2397 } else if (dblocks
< GIGABYTES(16, blocklog
)) {
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.
2404 logblocks
= MIN(XFS_MIN_LOG_BYTES
>> blocklog
,
2405 min_logblocks
* XFS_DFL_LOG_FACTOR
);
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.
2413 logblocks
= (dblocks
<< blocklog
) / 2048;
2414 logblocks
= logblocks
>> blocklog
;
2417 /* Ensure the chosen size meets minimum log size requirements */
2418 logblocks
= MAX(min_logblocks
, logblocks
);
2420 /* make sure the log fits wholly within an AG */
2421 if (logblocks
>= agsize
)
2422 logblocks
= min_logblocks
;
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
;
2430 validate_log_size(logblocks
, blocklog
, min_logblocks
);
2432 protostring
= setup_proto(protofile
);
2433 bsize
= 1 << (blocklog
- BBSHIFT
);
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
;
2445 * sb_versionnum and finobt flags must be set before we use
2446 * XFS_PREALLOC_BLOCKS().
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
,
2453 logversion
== 2, attrversion
== 1,
2454 (sectorsize
!= BBSIZE
||
2455 lsectorsize
!= BBSIZE
),
2456 nci
, sbp
->sb_features2
!= 0);
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.
2463 sbp
->sb_bad_features2
= sbp
->sb_features2
;
2466 sbp
->sb_features_ro_compat
= XFS_SB_FEAT_RO_COMPAT_FINOBT
;
2470 * Readjust the log size to fit within an AG if it was sized
2474 logblocks
= MIN(logblocks
,
2475 XFS_ALLOC_AG_MAX_USABLE(mp
));
2477 /* revalidate the log size is valid if we changed it */
2478 validate_log_size(logblocks
, blocklog
, min_logblocks
);
2480 if (logblocks
> agsize
- XFS_PREALLOC_BLOCKS(mp
)) {
2482 _("internal log size %lld too large, must fit in allocation group\n"),
2483 (long long)logblocks
);
2488 if (logagno
>= agcount
) {
2490 _("log ag number %d too large, must be less than %lld\n"),
2491 logagno
, (long long)agcount
);
2495 logagno
= (xfs_agnumber_t
)(agcount
/ 2);
2497 logstart
= XFS_AGB_TO_FSB(mp
, logagno
, XFS_PREALLOC_BLOCKS(mp
));
2499 * Align the logstart at stripe unit boundary.
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
);
2513 fixup_log_stripe_unit(lsflag
, lsunit
,
2514 &logblocks
, blocklog
);
2516 validate_log_size(logblocks
, blocklog
, min_logblocks
);
2519 * dirent filetype field always enabled on v5 superblocks
2522 sbp
->sb_features_incompat
= XFS_SB_FEAT_INCOMPAT_FTYPE
;
2526 if (!qflag
|| Nflag
) {
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
);
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
);
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
;
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;
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
;
2593 sbp
->sb_logsunit
= 0;
2595 int cluster_size
= XFS_INODE_BIG_CLUSTER_SIZE
;
2597 cluster_size
*= isize
/ XFS_DINODE_MIN_SIZE
;
2598 sbp
->sb_inoalignmt
= cluster_size
>> blocklog
;
2599 iaflag
= sbp
->sb_inoalignmt
!= 0;
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
;
2606 sbp
->sb_logsectlog
= 0;
2607 sbp
->sb_logsectsize
= 0;
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.
2615 * Copy the already calculated/scaled inoalignmt to spino_align and
2616 * update the former to the full inode chunk size.
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
;
2624 if (force_overwrite
)
2625 zero_old_xfs_structures(&xi
, sbp
);
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.
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
);
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
);
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
2652 if (xi
.disfile
&& ftruncate64(xi
.dfd
, dblocks
* blocksize
) < 0) {
2653 fprintf(stderr
, _("%s: Growing the data section failed\n"),
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)
2664 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
2665 (xi
.dsize
- BTOBB(WHACK_SIZE
)),
2667 memset(XFS_BUF_PTR(buf
), 0, WHACK_SIZE
);
2668 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
2669 libxfs_purgebuf(buf
);
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
);
2680 mp
= libxfs_mount(mp
, sbp
, xi
.ddev
, xi
.logdev
, xi
.rtdev
, 0);
2682 fprintf(stderr
, _("%s: filesystem failed to initialize\n"),
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.
2692 for (agno
= 0; agno
< agcount
; agno
++) {
2693 struct xfs_agfl
*agfl
;
2695 struct xfs_perag
*pag
= xfs_perag_get(mp
, agno
);
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
);
2709 * AG header block: freespace
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
);
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
);
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
);
2748 * AG freelist header block
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
);
2765 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
2768 * AG header block: inodes
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
);
2781 agi
->agi_root
= cpu_to_be32(XFS_IBT_BLOCK(mp
));
2782 agi
->agi_level
= cpu_to_be32(1);
2784 agi
->agi_free_root
= cpu_to_be32(XFS_FIBT_BLOCK(mp
));
2785 agi
->agi_free_level
= cpu_to_be32(1);
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
);
2797 * BNO btree root block
2799 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
2800 XFS_AGB_TO_DADDR(mp
, agno
, XFS_BNO_BLOCK(mp
)),
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
);
2809 xfs_btree_init_block(mp
, buf
, XFS_ABTB_MAGIC
, 0, 1,
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
) {
2817 * Have to insert two records
2818 * Insert pad record for stripe align of log
2820 arec
->ar_blockcount
= cpu_to_be32(
2821 XFS_FSB_TO_AGBNO(mp
, logstart
) -
2822 be32_to_cpu(arec
->ar_startblock
));
2825 * Insert record at start of internal log
2827 nrec
->ar_startblock
= cpu_to_be32(
2828 be32_to_cpu(arec
->ar_startblock
) +
2829 be32_to_cpu(arec
->ar_blockcount
));
2831 be16_add_cpu(&block
->bb_numrecs
, 1);
2834 * Change record start to after the internal log
2836 be32_add_cpu(&arec
->ar_startblock
, logblocks
);
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.
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;
2849 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
2852 * CNT btree root block
2854 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
2855 XFS_AGB_TO_DADDR(mp
, agno
, XFS_CNT_BLOCK(mp
)),
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
);
2864 xfs_btree_init_block(mp
, buf
, XFS_ABTC_MAGIC
, 0, 1,
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
) {
2871 arec
->ar_blockcount
= cpu_to_be32(
2872 XFS_FSB_TO_AGBNO(mp
, logstart
) -
2873 be32_to_cpu(arec
->ar_startblock
));
2875 nrec
->ar_startblock
= cpu_to_be32(
2876 be32_to_cpu(arec
->ar_startblock
) +
2877 be32_to_cpu(arec
->ar_blockcount
));
2879 be16_add_cpu(&block
->bb_numrecs
, 1);
2881 be32_add_cpu(&arec
->ar_startblock
, logblocks
);
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.
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;
2894 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
2897 * INO btree root block
2899 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
2900 XFS_AGB_TO_DADDR(mp
, agno
, XFS_IBT_BLOCK(mp
)),
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
);
2909 xfs_btree_init_block(mp
, buf
, XFS_IBT_MAGIC
, 0, 0,
2911 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
2914 * Free INO btree root block
2921 buf
= libxfs_getbuf(mp
->m_ddev_targp
,
2922 XFS_AGB_TO_DADDR(mp
, agno
, XFS_FIBT_BLOCK(mp
)),
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
);
2931 xfs_btree_init_block(mp
, buf
, XFS_FIBT_MAGIC
, 0, 0,
2933 libxfs_writebuf(buf
, LIBXFS_EXIT_ON_FAILURE
);
2938 * Touch last block, make fs the right size if it's a file.
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
);
2946 * Make sure we can write the last block in the realtime area.
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
);
2956 * BNO, CNT free block list
2958 for (agno
= 0; agno
< agcount
; agno
++) {
2959 xfs_alloc_arg_t args
;
2961 struct xfs_trans_res tres
= {0};
2963 memset(&args
, 0, sizeof(args
));
2964 args
.tp
= tp
= libxfs_trans_alloc(mp
, 0);
2968 args
.pag
= xfs_perag_get(mp
,agno
);
2969 c
= -libxfs_trans_reserve(tp
, &tres
, worst_freelist
, 0);
2973 libxfs_alloc_fix_freelist(&args
, 0);
2974 xfs_perag_put(args
.pag
);
2975 libxfs_trans_commit(tp
);
2979 * Allocate the root inode and anything else in the proto file.
2981 parse_proto(mp
, &fsx
, &protostring
);
2984 * Protect ourselves against possible stupidity
2986 if (XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
) != 0) {
2988 _("%s: root inode created in AG %u, not AG 0\n"),
2989 progname
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
));
2994 * Write out multiple secondary superblocks with rootinode field set
2996 if (mp
->m_sb
.sb_agcount
> 1) {
2998 * the last superblock
3000 buf
= libxfs_readbuf(mp
->m_dev
,
3001 XFS_AGB_TO_DADDR(mp
, mp
->m_sb
.sb_agcount
-1,
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
);
3009 * and one in the middle for luck
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,
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
);
3024 * Dump all inodes and buffers before marking us all done.
3025 * Need to drop references to inodes we still hold, first.
3027 libxfs_rtmount_destroy(mp
);
3028 libxfs_bcache_purge();
3031 * Mark the filesystem ok.
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
);
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
);
3054 fprintf(stderr
, _("Cannot specify both -%c %s and -%c %s\n"),
3055 opt
, tab
[oldidx
], opt
, tab
[newidx
]);
3065 fprintf(stderr
, _("Illegal value %s for -%s option\n"), value
, opt
);
3073 return (i
& (i
- 1)) == 0;
3076 static void __attribute__((noreturn
))
3082 fprintf(stderr
, _("-%c %s option requires a value\n"), opt
, tab
[idx
]);
3092 fprintf(stderr
, "-%c ", opt
);
3094 fprintf(stderr
, "%s ", tab
[idx
]);
3095 fprintf(stderr
, _("option respecified\n"));
3104 fprintf(stderr
, _("unknown option -%c %s\n"), opt
, s
);
3109 * isdigits -- returns 1 if string contains nothing but [0-9], 0 otherwise
3116 int n
= strlen(str
);
3118 for (i
= 0; i
< n
; i
++) {
3119 if (!isdigit((int)str
[i
]))
3127 unsigned int blocksize
,
3128 unsigned int sectorsize
,
3134 i
= strtoll(s
, &sp
, 0);
3135 if (i
== 0 && sp
== s
)
3140 if (*sp
== 'b' && sp
[1] == '\0') {
3142 return i
* blocksize
;
3143 fprintf(stderr
, _("blocksize not available yet.\n"));
3146 if (*sp
== 's' && sp
[1] == '\0') {
3148 return i
* sectorsize
;
3151 if (*sp
== 'k' && sp
[1] == '\0')
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
;
3166 static void __attribute__((noreturn
))
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\
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\
3187 /* realtime subvol */ [-r extsize=num,size=num,rtdev=xxx]\n\
3188 /* sectorsize */ [-s log=n|size=num]\n\
3189 /* version */ [-V]\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"),