Skip to contentSkip to conversation

btrfs-progs: filesystems: Check DATA profile before creating swapfile

PATCH In review Marcos Paulo de Souza 2 participants 4 messages Jun 6, 2025 – Jun 22, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: filesystems: Check DATA profile before creating swapfile

---

I'm not sure if it would be better to just add a new helper method to check for profiles, please let me know if you would like to have a helper instead.

 cmds/filesystem.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
cmds/filesystem.c +10 -1
index 64373532..21ff4d7a 100644
@@ -1718,12 +1718,21 @@ static int cmd_filesystem_mkswapfile(const struct cmd_struct *cmd, int argc, cha
 		return 1;
 
 	fname = argv[optind];
-	pr_verbose(LOG_INFO, "create file %s with mode 0600\n", fname);
 	fd = open(fname, O_RDWR | O_CREAT | O_EXCL, 0600);
 	if (fd < 0) {
 		error("cannot create new swapfile: %m");
 		return 1;
 	}
+
+	ret = sysfs_open_fsid_file(fd, "allocation/data/single/total_bytes");
+	if (ret < 0) {
+		error("swapfile isn't supported on a filesystem with DATA profile different from single");
+		ret = 1;
+		goto out;
+	}
+
+	pr_verbose(LOG_INFO, "create file %s with mode 0600\n", fname);
+
 	ret = ftruncate(fd, 0);
 	if (ret < 0) {
 		error("cannot truncate file: %m");
Signature · 1 line
2.49.0
#1 lore raw
Quoted 10 lines · Marcos Paulo de Souza

---

I'm not sure if it would be better to just add a new helper method to check
for profiles, please let me know if you would like to have a helper instead.

gentle ping :)

Quoted 35 lines

 cmds/filesystem.c | 11 ++++++++++-

 1 file changed, 10 insertions(+), 1 deletion(-)
cmds/filesystem.c +0 -0
index 64373532..21ff4d7a 100644
@@ -1718,12 +1718,21 @@ static int cmd_filesystem_mkswapfile(const

struct cmd_struct *cmd, int argc, cha
  return 1;

  fname = argv[optind];

cmds/filesystem.c +0 -1
-	pr_verbose(LOG_INFO, "create file %s with mode 0600\n",

fname);
  fd = open(fname, O_RDWR | O_CREAT | O_EXCL, 0600); if (fd < 0) {
  error("cannot create new swapfile: %m"); return 1;
  }

cmds/filesystem.c +2 -0
+
+	ret = sysfs_open_fsid_file(fd,

"allocation/data/single/total_bytes");

cmds/filesystem.c +2 -0
+	if (ret < 0) {
+		error("swapfile isn't supported on a filesystem with

DATA profile different from single");

cmds/filesystem.c +5 -0
+		ret = 1;
+		goto out;
+	}
+
+	pr_verbose(LOG_INFO, "create file %s with mode 0600\n",

fname);
+
  ret = ftruncate(fd, 0);
  if (ret < 0) {
  error("cannot truncate file: %m");

#2 lore raw
In reply to Marcos Paulo de Souza

cmds/filesystem.c static int cmd_filesystem_mkswapfile(const struct cmd_struct *cmd, int argc, cha · line 1732

Quoted 13 lines · Marcos Paulo de Souza

---

I'm not sure if it would be better to just add a new helper method to check for profiles, please let me know if you would like to have a helper instead.

cmds/filesystem.c +6 -0
+	ret = sysfs_open_fsid_file(fd, "allocation/data/single/total_bytes");
+	if (ret < 0) {
+		error("swapfile isn't supported on a filesystem with DATA profile different from single");
+		ret = 1;
+		goto out;
+	}

This works, new helper can be added in case we'd need this pattern more often.

But I'm not sure the error and exit is right here, it's up to the kernel implementation to check the constraints. In progs it's better to warn and say kernel may not support that, for features where this may change in the future.

Do you have a usecase where you rely on this command failing? Maybe we can enhance it so it verifies the known limitations, separately from the actual file creation.

#3 lore raw
Quoted 24 lines · David Sterba
Quoted 14 lines · Marcos Paulo de Souza

---

I'm not sure if it would be better to just add a new helper method to check
for profiles, please let me know if you would like to have a helper instead.

(quoted diff) +1 -0
+	ret = sysfs_open_fsid_file(fd,

"allocation/data/single/total_bytes");

(quoted diff) +2 -0
+	if (ret < 0) {
+		error("swapfile isn't supported on a filesystem

with DATA profile different from single");

cmds/filesystem.c static int cmd_filesystem_mkswapfile(const struct cmd_struct *cmd, int argc, cha · line 1732

Quoted 3 lines
cmds/filesystem.c +3 -0
+		ret = 1;
+		goto out;
+	}

This works, new helper can be added in case we'd need this pattern more
often.

Quoted 6 lines

But I'm not sure the error and exit is right here, it's up to the kernel
implementation to check the constraints. In progs it's better to warn and say kernel may not support that, for features where this may change
in the future.

I agree, just printing a warning should be good enough for cases like this, since the file creation works but later on swapon fails due to the nature of the data profile not being single, returning -EINVAL. This also throws a btrfs_warn.

Quoted 6 lines

Do you have a usecase where you rely on this command failing? Maybe we
can enhance it so it verifies the known limitations, separately from the
actual file creation.

Not really, just wanted to fix this behavior after seeing it on btrfs- progs issues on GitHub.

In this case, do you think this is worth solving? Maybe keeping the same check for the data profile and just show a warning on btrfs-progs?

Thanks in advance,
Marcos