Commit b18c5b84 authored by Viacheslav Dubeyko's avatar Viacheslav Dubeyko
Browse files

hfsplus: fix generic/037 xfstests failure

The xfstests' test-case generic/037 fails to execute
correctly:

FSTYP -- hfsplus
PLATFORM -- Linux/x86_64 hfsplus-testing-0001 6.15.0-rc4+ #8 SMP PREEMPT_DYNAMIC Thu May 1 16:43:22 PDT 2025
MKFS_OPTIONS -- /dev/loop51
MOUNT_OPTIONS -- /dev/loop51 /mnt/scratch

generic/037 - output mismatch (see xfstests-dev/results//generic/037.out.bad)

The goal of generic/037 test-case is to "verify that replacing
a xattr's value is an atomic operation". The test "consists of
removing the old value and then inserting the new value in a btree.
This made readers (getxattr and listxattrs) not getting neither
the old nor the new value during a short time window".

The HFS+ has the issue of executing the xattr replace operation
because __hfsplus_setxattr() method [1] implemented it as not
atomic operation [2]:

	if (hfsplus_attr_exists(inode, name)) {
		if (flags & XATTR_CREATE) {
			pr_err("xattr exists yet\n");
			err = -EOPNOTSUPP;
			goto end_setxattr;
		}
		err = hfsplus_delete_attr(inode, name);
		if (err)
			goto end_setxattr;
		err = hfsplus_create_attr(inode, name, value, size);
		if (err)
			goto end_setxattr;
	}

The main issue of the logic that it implements delete and
create of xattr as independent atomic operations, but the replace
operation at whole is not atomic operation. This patch implements
a new hfsplus_replace_attr() method that makes the xattr replace
operation by atomic one. Also, it reworks hfsplus_create_attr() and
hfsplus_delete_attr() with the goal of reusing the common logic
in hfsplus_replace_attr() method.

sudo ./check generic/037
FSTYP         -- hfsplus
PLATFORM      -- Linux/x86_64 hfsplus-testing-0001 6.19.0-rc1+ #47 SMP PREEMPT_DYNAMIC Thu Jan  8 15:37:20 PST 2026
MKFS_OPTIONS  -- /dev/loop51
MOUNT_OPTIONS -- /dev/loop51 /mnt/scratch

generic/037 37s ...  37s
Ran: generic/037
Passed all 1 tests

[1] https://elixir.bootlin.com/linux/v6.19-rc4/source/fs/hfsplus/xattr.c#L261
[2] https://elixir.bootlin.com/linux/v6.19-rc4/source/fs/hfsplus/xattr.c#L338



Signed-off-by: default avatarViacheslav Dubeyko <slava@dubeyko.com>
cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
cc: Yangtao Li <frank.li@vivo.com>
cc: linux-fsdevel@vger.kernel.org
Link: https://lore.kernel.org/r/20260109234213.2805400-1-slava@dubeyko.com


Signed-off-by: default avatarViacheslav Dubeyko <slava@dubeyko.com>
parent ed8889ca
Loading
Loading
Loading
Loading
+131 −50
Original line number Diff line number Diff line
@@ -193,46 +193,26 @@ int hfsplus_attr_exists(struct inode *inode, const char *name)
	return 0;
}

int hfsplus_create_attr(struct inode *inode,
				const char *name,
				const void *value, size_t size)
static
int hfsplus_create_attr_nolock(struct inode *inode, const char *name,
				const void *value, size_t size,
				struct hfs_find_data *fd,
				hfsplus_attr_entry *entry_ptr)
{
	struct super_block *sb = inode->i_sb;
	struct hfs_find_data fd;
	hfsplus_attr_entry *entry_ptr;
	int entry_size;
	int err;

	hfs_dbg("name %s, ino %ld\n",
		name ? name : NULL, inode->i_ino);

	if (!HFSPLUS_SB(sb)->attr_tree) {
		pr_err("attributes file doesn't exist\n");
		return -EINVAL;
	}

	entry_ptr = hfsplus_alloc_attr_entry();
	if (!entry_ptr)
		return -ENOMEM;

	err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
	if (err)
		goto failed_init_create_attr;

	/* Fail early and avoid ENOSPC during the btree operation */
	err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
	if (err)
		goto failed_create_attr;

	if (name) {
		err = hfsplus_attr_build_key(sb, fd.search_key,
		err = hfsplus_attr_build_key(sb, fd->search_key,
						inode->i_ino, name);
		if (err)
			goto failed_create_attr;
	} else {
		err = -EINVAL;
		goto failed_create_attr;
	}
			return err;
	} else
		return -EINVAL;

	/* Mac OS X supports only inline data attributes. */
	entry_size = hfsplus_attr_build_record(entry_ptr,
@@ -245,24 +225,62 @@ int hfsplus_create_attr(struct inode *inode,
		else
			err = -EINVAL;
		hfs_dbg("unable to store value: err %d\n", err);
		goto failed_create_attr;
		return err;
	}

	err = hfs_brec_find(&fd, hfs_find_rec_by_key);
	err = hfs_brec_find(fd, hfs_find_rec_by_key);
	if (err != -ENOENT) {
		if (!err)
			err = -EEXIST;
		goto failed_create_attr;
		return err;
	}

	err = hfs_brec_insert(&fd, entry_ptr, entry_size);
	err = hfs_brec_insert(fd, entry_ptr, entry_size);
	if (err) {
		hfs_dbg("unable to store value: err %d\n", err);
		goto failed_create_attr;
		return err;
	}

	hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);

	return 0;
}

int hfsplus_create_attr(struct inode *inode,
			const char *name,
			const void *value, size_t size)
{
	struct super_block *sb = inode->i_sb;
	struct hfs_find_data fd;
	hfsplus_attr_entry *entry_ptr;
	int err;

	hfs_dbg("name %s, ino %ld\n",
		name ? name : NULL, inode->i_ino);

	if (!HFSPLUS_SB(sb)->attr_tree) {
		pr_err("attributes file doesn't exist\n");
		return -EINVAL;
	}

	entry_ptr = hfsplus_alloc_attr_entry();
	if (!entry_ptr)
		return -ENOMEM;

	err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
	if (err)
		goto failed_init_create_attr;

	/* Fail early and avoid ENOSPC during the btree operation */
	err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
	if (err)
		goto failed_create_attr;

	err = hfsplus_create_attr_nolock(inode, name, value, size,
					 &fd, entry_ptr);
	if (err)
		goto failed_create_attr;

failed_create_attr:
	hfs_find_exit(&fd);

@@ -312,6 +330,37 @@ static int __hfsplus_delete_attr(struct inode *inode, u32 cnid,
	return err;
}

static
int hfsplus_delete_attr_nolock(struct inode *inode, const char *name,
				struct hfs_find_data *fd)
{
	struct super_block *sb = inode->i_sb;
	int err;

	hfs_dbg("name %s, ino %ld\n",
		name ? name : NULL, inode->i_ino);

	if (name) {
		err = hfsplus_attr_build_key(sb, fd->search_key,
						inode->i_ino, name);
		if (err)
			return err;
	} else {
		pr_err("invalid extended attribute name\n");
		return -EINVAL;
	}

	err = hfs_brec_find(fd, hfs_find_rec_by_key);
	if (err)
		return err;

	err = __hfsplus_delete_attr(inode, inode->i_ino, fd);
	if (err)
		return err;

	return 0;
}

int hfsplus_delete_attr(struct inode *inode, const char *name)
{
	int err = 0;
@@ -335,22 +384,7 @@ int hfsplus_delete_attr(struct inode *inode, const char *name)
	if (err)
		goto out;

	if (name) {
		err = hfsplus_attr_build_key(sb, fd.search_key,
						inode->i_ino, name);
		if (err)
			goto out;
	} else {
		pr_err("invalid extended attribute name\n");
		err = -EINVAL;
		goto out;
	}

	err = hfs_brec_find(&fd, hfs_find_rec_by_key);
	if (err)
		goto out;

	err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
	err = hfsplus_delete_attr_nolock(inode, name, &fd);
	if (err)
		goto out;

@@ -392,3 +426,50 @@ int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
	hfs_find_exit(&fd);
	return err;
}

int hfsplus_replace_attr(struct inode *inode,
			 const char *name,
			 const void *value, size_t size)
{
	struct super_block *sb = inode->i_sb;
	struct hfs_find_data fd;
	hfsplus_attr_entry *entry_ptr;
	int err = 0;

	hfs_dbg("name %s, ino %ld\n",
		name ? name : NULL, inode->i_ino);

	if (!HFSPLUS_SB(sb)->attr_tree) {
		pr_err("attributes file doesn't exist\n");
		return -EINVAL;
	}

	entry_ptr = hfsplus_alloc_attr_entry();
	if (!entry_ptr)
		return -ENOMEM;

	err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
	if (err)
		goto failed_init_replace_attr;

	/* Fail early and avoid ENOSPC during the btree operation */
	err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
	if (err)
		goto failed_replace_attr;

	err = hfsplus_delete_attr_nolock(inode, name, &fd);
	if (err)
		goto failed_replace_attr;

	err = hfsplus_create_attr_nolock(inode, name, value, size,
					 &fd, entry_ptr);
	if (err)
		goto failed_replace_attr;

failed_replace_attr:
	hfs_find_exit(&fd);

failed_init_replace_attr:
	hfsplus_destroy_attr_entry(entry_ptr);
	return err;
}
+3 −0
Original line number Diff line number Diff line
@@ -344,6 +344,9 @@ int hfsplus_create_attr(struct inode *inode, const char *name,
			const void *value, size_t size);
int hfsplus_delete_attr(struct inode *inode, const char *name);
int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid);
int hfsplus_replace_attr(struct inode *inode,
			 const char *name,
			 const void *value, size_t size);

/* bitmap.c */
int hfsplus_block_allocate(struct super_block *sb, u32 size, u32 offset,
+2 −5
Original line number Diff line number Diff line
@@ -341,12 +341,9 @@ int __hfsplus_setxattr(struct inode *inode, const char *name,
			err = -EOPNOTSUPP;
			goto end_setxattr;
		}
		err = hfsplus_delete_attr(inode, name);
		if (err)
			goto end_setxattr;
		err = hfsplus_create_attr(inode, name, value, size);
		err = hfsplus_replace_attr(inode, name, value, size);
		if (err) {
			hfs_dbg("unable to store value: err %d\n", err);
			hfs_dbg("unable to replace xattr: err %d\n", err);
			goto end_setxattr;
		}
	} else {