Skip to content

Commit b18c5b8

Browse files
committed
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: Viacheslav 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: Viacheslav Dubeyko <slava@dubeyko.com>
1 parent ed8889c commit b18c5b8

3 files changed

Lines changed: 136 additions & 55 deletions

File tree

fs/hfsplus/attributes.c

Lines changed: 131 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -193,46 +193,26 @@ int hfsplus_attr_exists(struct inode *inode, const char *name)
193193
return 0;
194194
}
195195

196-
int hfsplus_create_attr(struct inode *inode,
197-
const char *name,
198-
const void *value, size_t size)
196+
static
197+
int hfsplus_create_attr_nolock(struct inode *inode, const char *name,
198+
const void *value, size_t size,
199+
struct hfs_find_data *fd,
200+
hfsplus_attr_entry *entry_ptr)
199201
{
200202
struct super_block *sb = inode->i_sb;
201-
struct hfs_find_data fd;
202-
hfsplus_attr_entry *entry_ptr;
203203
int entry_size;
204204
int err;
205205

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

209-
if (!HFSPLUS_SB(sb)->attr_tree) {
210-
pr_err("attributes file doesn't exist\n");
211-
return -EINVAL;
212-
}
213-
214-
entry_ptr = hfsplus_alloc_attr_entry();
215-
if (!entry_ptr)
216-
return -ENOMEM;
217-
218-
err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
219-
if (err)
220-
goto failed_init_create_attr;
221-
222-
/* Fail early and avoid ENOSPC during the btree operation */
223-
err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
224-
if (err)
225-
goto failed_create_attr;
226-
227209
if (name) {
228-
err = hfsplus_attr_build_key(sb, fd.search_key,
210+
err = hfsplus_attr_build_key(sb, fd->search_key,
229211
inode->i_ino, name);
230212
if (err)
231-
goto failed_create_attr;
232-
} else {
233-
err = -EINVAL;
234-
goto failed_create_attr;
235-
}
213+
return err;
214+
} else
215+
return -EINVAL;
236216

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

251-
err = hfs_brec_find(&fd, hfs_find_rec_by_key);
231+
err = hfs_brec_find(fd, hfs_find_rec_by_key);
252232
if (err != -ENOENT) {
253233
if (!err)
254234
err = -EEXIST;
255-
goto failed_create_attr;
235+
return err;
256236
}
257237

258-
err = hfs_brec_insert(&fd, entry_ptr, entry_size);
238+
err = hfs_brec_insert(fd, entry_ptr, entry_size);
259239
if (err) {
260240
hfs_dbg("unable to store value: err %d\n", err);
261-
goto failed_create_attr;
241+
return err;
262242
}
263243

264244
hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ATTR_DIRTY);
265245

246+
return 0;
247+
}
248+
249+
int hfsplus_create_attr(struct inode *inode,
250+
const char *name,
251+
const void *value, size_t size)
252+
{
253+
struct super_block *sb = inode->i_sb;
254+
struct hfs_find_data fd;
255+
hfsplus_attr_entry *entry_ptr;
256+
int err;
257+
258+
hfs_dbg("name %s, ino %ld\n",
259+
name ? name : NULL, inode->i_ino);
260+
261+
if (!HFSPLUS_SB(sb)->attr_tree) {
262+
pr_err("attributes file doesn't exist\n");
263+
return -EINVAL;
264+
}
265+
266+
entry_ptr = hfsplus_alloc_attr_entry();
267+
if (!entry_ptr)
268+
return -ENOMEM;
269+
270+
err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
271+
if (err)
272+
goto failed_init_create_attr;
273+
274+
/* Fail early and avoid ENOSPC during the btree operation */
275+
err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
276+
if (err)
277+
goto failed_create_attr;
278+
279+
err = hfsplus_create_attr_nolock(inode, name, value, size,
280+
&fd, entry_ptr);
281+
if (err)
282+
goto failed_create_attr;
283+
266284
failed_create_attr:
267285
hfs_find_exit(&fd);
268286

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

333+
static
334+
int hfsplus_delete_attr_nolock(struct inode *inode, const char *name,
335+
struct hfs_find_data *fd)
336+
{
337+
struct super_block *sb = inode->i_sb;
338+
int err;
339+
340+
hfs_dbg("name %s, ino %ld\n",
341+
name ? name : NULL, inode->i_ino);
342+
343+
if (name) {
344+
err = hfsplus_attr_build_key(sb, fd->search_key,
345+
inode->i_ino, name);
346+
if (err)
347+
return err;
348+
} else {
349+
pr_err("invalid extended attribute name\n");
350+
return -EINVAL;
351+
}
352+
353+
err = hfs_brec_find(fd, hfs_find_rec_by_key);
354+
if (err)
355+
return err;
356+
357+
err = __hfsplus_delete_attr(inode, inode->i_ino, fd);
358+
if (err)
359+
return err;
360+
361+
return 0;
362+
}
363+
315364
int hfsplus_delete_attr(struct inode *inode, const char *name)
316365
{
317366
int err = 0;
@@ -335,22 +384,7 @@ int hfsplus_delete_attr(struct inode *inode, const char *name)
335384
if (err)
336385
goto out;
337386

338-
if (name) {
339-
err = hfsplus_attr_build_key(sb, fd.search_key,
340-
inode->i_ino, name);
341-
if (err)
342-
goto out;
343-
} else {
344-
pr_err("invalid extended attribute name\n");
345-
err = -EINVAL;
346-
goto out;
347-
}
348-
349-
err = hfs_brec_find(&fd, hfs_find_rec_by_key);
350-
if (err)
351-
goto out;
352-
353-
err = __hfsplus_delete_attr(inode, inode->i_ino, &fd);
387+
err = hfsplus_delete_attr_nolock(inode, name, &fd);
354388
if (err)
355389
goto out;
356390

@@ -392,3 +426,50 @@ int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid)
392426
hfs_find_exit(&fd);
393427
return err;
394428
}
429+
430+
int hfsplus_replace_attr(struct inode *inode,
431+
const char *name,
432+
const void *value, size_t size)
433+
{
434+
struct super_block *sb = inode->i_sb;
435+
struct hfs_find_data fd;
436+
hfsplus_attr_entry *entry_ptr;
437+
int err = 0;
438+
439+
hfs_dbg("name %s, ino %ld\n",
440+
name ? name : NULL, inode->i_ino);
441+
442+
if (!HFSPLUS_SB(sb)->attr_tree) {
443+
pr_err("attributes file doesn't exist\n");
444+
return -EINVAL;
445+
}
446+
447+
entry_ptr = hfsplus_alloc_attr_entry();
448+
if (!entry_ptr)
449+
return -ENOMEM;
450+
451+
err = hfs_find_init(HFSPLUS_SB(sb)->attr_tree, &fd);
452+
if (err)
453+
goto failed_init_replace_attr;
454+
455+
/* Fail early and avoid ENOSPC during the btree operation */
456+
err = hfs_bmap_reserve(fd.tree, fd.tree->depth + 1);
457+
if (err)
458+
goto failed_replace_attr;
459+
460+
err = hfsplus_delete_attr_nolock(inode, name, &fd);
461+
if (err)
462+
goto failed_replace_attr;
463+
464+
err = hfsplus_create_attr_nolock(inode, name, value, size,
465+
&fd, entry_ptr);
466+
if (err)
467+
goto failed_replace_attr;
468+
469+
failed_replace_attr:
470+
hfs_find_exit(&fd);
471+
472+
failed_init_replace_attr:
473+
hfsplus_destroy_attr_entry(entry_ptr);
474+
return err;
475+
}

fs/hfsplus/hfsplus_fs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ int hfsplus_create_attr(struct inode *inode, const char *name,
344344
const void *value, size_t size);
345345
int hfsplus_delete_attr(struct inode *inode, const char *name);
346346
int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid);
347+
int hfsplus_replace_attr(struct inode *inode,
348+
const char *name,
349+
const void *value, size_t size);
347350

348351
/* bitmap.c */
349352
int hfsplus_block_allocate(struct super_block *sb, u32 size, u32 offset,

fs/hfsplus/xattr.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,9 @@ int __hfsplus_setxattr(struct inode *inode, const char *name,
341341
err = -EOPNOTSUPP;
342342
goto end_setxattr;
343343
}
344-
err = hfsplus_delete_attr(inode, name);
345-
if (err)
346-
goto end_setxattr;
347-
err = hfsplus_create_attr(inode, name, value, size);
344+
err = hfsplus_replace_attr(inode, name, value, size);
348345
if (err) {
349-
hfs_dbg("unable to store value: err %d\n", err);
346+
hfs_dbg("unable to replace xattr: err %d\n", err);
350347
goto end_setxattr;
351348
}
352349
} else {

0 commit comments

Comments
 (0)