[PATCH 3/4] xfs_repair: allow sysadmins to add reflink

Darrick J. Wong
2023-12-31T23:01:43Z

Allow the sysadmin to use xfs_repair to upgrade an existing filesystem
to support the reference count btree, and therefore reflink.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
man/man8/xfs_admin.8 | 6 ++++++
repair/globals.c | 1 +
repair/globals.h | 1 +
repair/phase2.c | 31 +++++++++++++++++++++++++++++++
repair/rmap.c | 4 ++--
repair/xfs_repair.c | 11 +++++++++++
6 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/man/man8/xfs_admin.8 b/man/man8/xfs_admin.8
index efe2ce45fc2..3af201cadc3 100644
--- a/man/man8/xfs_admin.8
+++ b/man/man8/xfs_admin.8
@@ -163,6 +163,12 @@ allocation on old filesystems.
This upgrade can fail if any AG has less than 1% free space remaining.
The filesystem cannot be downgraded after this feature is enabled.
This feature was added to Linux 3.16.
+.TP 0.4i
+.B reflink
+Enable sharing of file data blocks.
+This upgrade can fail if any AG has less than 2% free space remaining.
+The filesystem cannot be downgraded after this feature is enabled.
+This feature was added to Linux 4.9.
.RE
.TP
.BI \-U " uuid"
diff --git a/repair/globals.c b/repair/globals.c
index 960dff28fba..f0754393ba2 100644
--- a/repair/globals.c
+++ b/repair/globals.c
@@ -53,6 +53,7 @@ bool add_inobtcount; /* add inode btree counts to AGI */
bool add_bigtime; /* add support for timestamps up to 2486 */
bool add_nrext64;
bool add_finobt; /* add free inode btrees */
+bool add_reflink; /* add reference count btrees */

/* misc status variables */

diff --git a/repair/globals.h b/repair/globals.h
index 4ec68ecd896..4013d8f0d24 100644
--- a/repair/globals.h
+++ b/repair/globals.h
@@ -94,6 +94,7 @@ extern bool add_inobtcount; /* add inode btree counts to AGI */
extern bool add_bigtime; /* add support for timestamps up to 2486 */
extern bool add_nrext64;
extern bool add_finobt; /* add free inode btrees */
+extern bool add_reflink; /* add reference count btrees */

/* misc status variables */

diff --git a/repair/phase2.c b/repair/phase2.c
index 5d2bb859514..9a8bf411333 100644
--- a/repair/phase2.c
+++ b/repair/phase2.c
@@ -204,6 +204,33 @@ set_finobt(
return true;
}

+static bool
+set_reflink(
+ struct xfs_mount *mp,
+ struct xfs_sb *new_sb)
+{
+ if (xfs_has_reflink(mp)) {
+ printf(_("Filesystem already supports reflink.\n"));
+ exit(0);
+ }
+
+ if (!xfs_has_crc(mp)) {
+ printf(
+ _("Reflink feature only supported on V5 filesystems.\n"));
+ exit(0);
+ }
+
+ if (xfs_has_realtime(mp)) {
+ printf(_("Reflink feature not supported with realtime.\n"));
+ exit(0);
+ }
+
+ printf(_("Adding reflink support to filesystem.\n"));
+ new_sb->sb_features_ro_compat |= XFS_SB_FEAT_RO_COMPAT_REFLINK;
+ new_sb->sb_features_incompat |= XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR;
+ return true;
+}
+
struct check_state {
struct xfs_sb sb;
uint64_t features;
@@ -373,6 +400,8 @@ need_check_fs_free_space(
{
if (xfs_has_finobt(mp) && !(old->features & XFS_FEAT_FINOBT))
return true;
+ if (xfs_has_reflink(mp) && !(old->features & XFS_FEAT_REFLINK))
+ return true;
return false;
}

@@ -450,6 +479,8 @@ upgrade_filesystem(
dirty |= set_nrext64(mp, &new_sb);
if (add_finobt)
dirty |= set_finobt(mp, &new_sb);
+ if (add_reflink)
+ dirty |= set_reflink(mp, &new_sb);
if (!dirty)
return;

diff --git a/repair/rmap.c b/repair/rmap.c
index 8895377aa2a..91a87d418e2 100644
--- a/repair/rmap.c
+++ b/repair/rmap.c
@@ -52,7 +52,7 @@ bool
rmap_needs_work(
struct xfs_mount *mp)
{
- return xfs_has_reflink(mp) ||
+ return xfs_has_reflink(mp) || add_reflink ||
xfs_has_rmapbt(mp);
}

@@ -1526,7 +1526,7 @@ check_refcounts(
int i;
int error;

- if (!xfs_has_reflink(mp))
+ if (!xfs_has_reflink(mp) || add_reflink)
return;
if (refcbt_suspect) {
if (no_modify && agno == 0)
diff --git a/repair/xfs_repair.c b/repair/xfs_repair.c
index b61af185c38..d53db25e618 100644
--- a/repair/xfs_repair.c
+++ b/repair/xfs_repair.c
@@ -70,6 +70,7 @@ enum c_opt_nums {
CONVERT_BIGTIME,
CONVERT_NREXT64,
CONVERT_FINOBT,
+ CONVERT_REFLINK,
C_MAX_OPTS,
};

@@ -79,6 +80,7 @@ static char *c_opts[] = {
[CONVERT_BIGTIME] = "bigtime",
[CONVERT_NREXT64] = "nrext64",
[CONVERT_FINOBT] = "finobt",
+ [CONVERT_REFLINK] = "reflink",
[C_MAX_OPTS] = NULL,
};

@@ -347,6 +349,15 @@ process_args(int argc, char **argv)
_("-c finobt only supports upgrades\n"));
add_finobt = true;
break;
+ case CONVERT_REFLINK:
+ if (!val)
+ do_abort(
+ _("-c reflink requires a parameter\n"));
+ if (strtol(val, NULL, 0) != 1)
+ do_abort(
+ _("-c reflink only supports upgrades\n"));
+ add_reflink = true;
+ break;
default:
unknown('c', val);
break;