From 53e0ead4c1a522f42486e47925b4c224945c6cda Mon Sep 17 00:00:00 2001
From: "Darrick J. Wong" <djwong@kernel.org>
Date: Fri, 29 Sep 2023 09:53:41 +0000
Subject: [PATCH 06/11] mkfs: add an extsize= option that allows units

Add a new mkfs option that allows the user to specify an extent size
hint with units.  This removes the need to specify the option in
filesystem block size, which eases the computation requirements in
deployment scripts.

# mkfs.xfs -d extsize=2m /dev/sda

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
Signed-off-by: John Garry <john.g.garry@oracle.com>

---
 man/man8/mkfs.xfs.8 | 15 ++++++++++++++
 mkfs/xfs_mkfs.c     | 48 +++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/man/man8/mkfs.xfs.8 b/man/man8/mkfs.xfs.8
index b2ffb3d..5cd69fa 100644
--- a/man/man8/mkfs.xfs.8
+++ b/man/man8/mkfs.xfs.8
@@ -479,6 +479,18 @@ will be assigned the project quota id provided in
 Directories will pass on the project id to newly created regular files and
 directories.
 .TP
+.BI extsize= num
+All inodes created by
+.B mkfs.xfs
+will have this
+.I value
+extent size hint applied.
+Directories will pass on this hint to newly created regular files and
+directories.
+This option cannot be combined with the
+.B extszinherit
+option.
+.TP
 .BI extszinherit= value
 All inodes created by
 .B mkfs.xfs
@@ -488,6 +500,9 @@ extent size hint applied.
 The value must be provided in units of filesystem blocks.
 Directories will pass on this hint to newly created regular files and
 directories.
+This option cannot be combined with the
+.B extsize
+option.
 .TP
 .BI daxinherit= value
 If
diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
index 2eb3a0a..fb0c53f 100644
--- a/mkfs/xfs_mkfs.c
+++ b/mkfs/xfs_mkfs.c
@@ -66,6 +66,7 @@ enum {
 	D_NOALIGN,
 	D_RTINHERIT,
 	D_PROJINHERIT,
+	D_EXTSIZE,
 	D_EXTSZINHERIT,
 	D_COWEXTSIZE,
 	D_DAXINHERIT,
@@ -300,6 +301,7 @@ static struct opt_params dopts = {
 		[D_NOALIGN] = "noalign",
 		[D_RTINHERIT] = "rtinherit",
 		[D_PROJINHERIT] = "projinherit",
+		[D_EXTSIZE] = "extsize",
 		[D_EXTSZINHERIT] = "extszinherit",
 		[D_COWEXTSIZE] = "cowextsize",
 		[D_DAXINHERIT] = "daxinherit",
@@ -407,8 +409,17 @@ static struct opt_params dopts = {
 		  .maxval = UINT_MAX,
 		  .defaultval = SUBOPT_NEEDS_VAL,
 		},
+		{ .index = D_EXTSIZE,
+		  .conflicts = { { &dopts, D_EXTSZINHERIT },
+				 { NULL, LAST_CONFLICT } },
+		  .convert = true,
+		  .minval = 0,
+		  .maxval = XFS_AG_MAX_BYTES,
+		  .defaultval = SUBOPT_NEEDS_VAL,
+		},
 		{ .index = D_EXTSZINHERIT,
-		  .conflicts = { { NULL, LAST_CONFLICT } },
+		  .conflicts = { { &dopts, D_EXTSIZE },
+				 { NULL, LAST_CONFLICT } },
 		  .minval = 0,
 		  .maxval = UINT_MAX,
 		  .defaultval = SUBOPT_NEEDS_VAL,
@@ -835,6 +846,7 @@ struct cli_params {
 	char	*lsu;
 	char	*rtextsize;
 	char	*rtsize;
+	char	*extsize;
 
 	/* parameters where 0 is a valid CLI value */
 	int	dsunit;
@@ -945,7 +957,7 @@ usage( void )
 			    inobtcount=0|1,bigtime=0|1]\n\
 /* data subvol */	[-d agcount=n,agsize=n,file,name=xxx,size=num,\n\
 			    (sunit=value,swidth=value|su=num,sw=num|noalign),\n\
-			    sectsize=num\n\
+			    sectsize=num,extsize=num\n\
 /* force overwrite */	[-f]\n\
 /* inode size */	[-i perblock=n|size=num,maxpct=n,attr=0|1|2,\n\
 			    projid32bit=0|1,sparse=0|1]\n\
@@ -1553,6 +1565,9 @@ data_opts_parser(
 		cli->fsx.fsx_projid = getnum(value, opts, subopt);
 		cli->fsx.fsx_xflags |= FS_XFLAG_PROJINHERIT;
 		break;
+	case D_EXTSIZE:
+		cli->extsize = getstr(value, opts, subopt);
+		break;
 	case D_EXTSZINHERIT:
 		cli->fsx.fsx_extsize = getnum(value, opts, subopt);
 		if (cli->fsx.fsx_extsize)
@@ -2002,6 +2017,33 @@ _("Minimum block size for CRC enabled filesystems is %d bytes.\n"),
 
 }
 
+/*
+ * Convert the -d extsize= option to a number, then set the extent size hint
+ * to that number.
+ */
+static void
+set_extsize(
+	struct cli_params	*cli,
+	char			*extsize,
+	struct opt_params	*opts,
+	int			subopt)
+{
+	uint64_t		extsz_bytes;
+	if (!extsize)
+		return;
+
+	extsz_bytes = getnum(extsize, opts, subopt);
+	if (extsz_bytes % blocksize)
+		illegal_option(extsize, opts, subopt,
+				_("Value must be a multiple of block size."));
+
+	cli->fsx.fsx_extsize = extsz_bytes / blocksize;
+	if (cli->fsx.fsx_extsize)
+		cli->fsx.fsx_xflags |= FS_XFLAG_EXTSZINHERIT;
+	else
+		cli->fsx.fsx_xflags &= ~FS_XFLAG_EXTSZINHERIT;
+}
+
 /*
  * Grab log sector size and validate.
  *
@@ -4081,6 +4123,8 @@ main(
 	blocksize = cfg.blocksize;
 	sectorsize = cfg.sectorsize;
 
+	set_extsize(&cli, cli.extsize, &dopts, D_EXTSIZE);
+
 	validate_log_sectorsize(&cfg, &cli, &dft);
 	validate_sb_features(&cfg, &cli);
 
-- 
2.33.0

