Skip to content

Commit 2ed5b09

Browse files
author
Darrick J. Wong
committed
xfs: make inode attribute forks a permanent part of struct xfs_inode
Syzkaller reported a UAF bug a while back: ================================================================== BUG: KASAN: use-after-free in xfs_ilock_attr_map_shared+0xe3/0xf6 fs/xfs/xfs_inode.c:127 Read of size 4 at addr ffff88802cec919c by task syz-executor262/2958 CPU: 2 PID: 2958 Comm: syz-executor262 Not tainted 5.15.0-0.30.3-20220406_1406 #3 Hardware name: Red Hat KVM, BIOS 1.13.0-2.module+el8.3.0+7860+a7792d29 04/01/2014 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0x82/0xa9 lib/dump_stack.c:106 print_address_description.constprop.9+0x21/0x2d5 mm/kasan/report.c:256 __kasan_report mm/kasan/report.c:442 [inline] kasan_report.cold.14+0x7f/0x11b mm/kasan/report.c:459 xfs_ilock_attr_map_shared+0xe3/0xf6 fs/xfs/xfs_inode.c:127 xfs_attr_get+0x378/0x4c2 fs/xfs/libxfs/xfs_attr.c:159 xfs_xattr_get+0xe3/0x150 fs/xfs/xfs_xattr.c:36 __vfs_getxattr+0xdf/0x13d fs/xattr.c:399 cap_inode_need_killpriv+0x41/0x5d security/commoncap.c:300 security_inode_need_killpriv+0x4c/0x97 security/security.c:1408 dentry_needs_remove_privs.part.28+0x21/0x63 fs/inode.c:1912 dentry_needs_remove_privs+0x80/0x9e fs/inode.c:1908 do_truncate+0xc3/0x1e0 fs/open.c:56 handle_truncate fs/namei.c:3084 [inline] do_open fs/namei.c:3432 [inline] path_openat+0x30ab/0x396d fs/namei.c:3561 do_filp_open+0x1c4/0x290 fs/namei.c:3588 do_sys_openat2+0x60d/0x98c fs/open.c:1212 do_sys_open+0xcf/0x13c fs/open.c:1228 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3a/0x7e arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0x0 RIP: 0033:0x7f7ef4bb753d Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 1b 79 2c 00 f7 d8 64 89 01 48 RSP: 002b:00007f7ef52c2ed8 EFLAGS: 00000246 ORIG_RAX: 0000000000000055 RAX: ffffffffffffffda RBX: 0000000000404148 RCX: 00007f7ef4bb753d RDX: 00007f7ef4bb753d RSI: 0000000000000000 RDI: 0000000020004fc0 RBP: 0000000000404140 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0030656c69662f2e R13: 00007ffd794db37f R14: 00007ffd794db470 R15: 00007f7ef52c2fc0 </TASK> Allocated by task 2953: kasan_save_stack+0x19/0x38 mm/kasan/common.c:38 kasan_set_track mm/kasan/common.c:46 [inline] set_alloc_info mm/kasan/common.c:434 [inline] __kasan_slab_alloc+0x68/0x7c mm/kasan/common.c:467 kasan_slab_alloc include/linux/kasan.h:254 [inline] slab_post_alloc_hook mm/slab.h:519 [inline] slab_alloc_node mm/slub.c:3213 [inline] slab_alloc mm/slub.c:3221 [inline] kmem_cache_alloc+0x11b/0x3eb mm/slub.c:3226 kmem_cache_zalloc include/linux/slab.h:711 [inline] xfs_ifork_alloc+0x25/0xa2 fs/xfs/libxfs/xfs_inode_fork.c:287 xfs_bmap_add_attrfork+0x3f2/0x9b1 fs/xfs/libxfs/xfs_bmap.c:1098 xfs_attr_set+0xe38/0x12a7 fs/xfs/libxfs/xfs_attr.c:746 xfs_xattr_set+0xeb/0x1a9 fs/xfs/xfs_xattr.c:59 __vfs_setxattr+0x11b/0x177 fs/xattr.c:180 __vfs_setxattr_noperm+0x128/0x5e0 fs/xattr.c:214 __vfs_setxattr_locked+0x1d4/0x258 fs/xattr.c:275 vfs_setxattr+0x154/0x33d fs/xattr.c:301 setxattr+0x216/0x29f fs/xattr.c:575 __do_sys_fsetxattr fs/xattr.c:632 [inline] __se_sys_fsetxattr fs/xattr.c:621 [inline] __x64_sys_fsetxattr+0x243/0x2fe fs/xattr.c:621 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3a/0x7e arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0x0 Freed by task 2949: kasan_save_stack+0x19/0x38 mm/kasan/common.c:38 kasan_set_track+0x1c/0x21 mm/kasan/common.c:46 kasan_set_free_info+0x20/0x30 mm/kasan/generic.c:360 ____kasan_slab_free mm/kasan/common.c:366 [inline] ____kasan_slab_free mm/kasan/common.c:328 [inline] __kasan_slab_free+0xe2/0x10e mm/kasan/common.c:374 kasan_slab_free include/linux/kasan.h:230 [inline] slab_free_hook mm/slub.c:1700 [inline] slab_free_freelist_hook mm/slub.c:1726 [inline] slab_free mm/slub.c:3492 [inline] kmem_cache_free+0xdc/0x3ce mm/slub.c:3508 xfs_attr_fork_remove+0x8d/0x132 fs/xfs/libxfs/xfs_attr_leaf.c:773 xfs_attr_sf_removename+0x5dd/0x6cb fs/xfs/libxfs/xfs_attr_leaf.c:822 xfs_attr_remove_iter+0x68c/0x805 fs/xfs/libxfs/xfs_attr.c:1413 xfs_attr_remove_args+0xb1/0x10d fs/xfs/libxfs/xfs_attr.c:684 xfs_attr_set+0xf1e/0x12a7 fs/xfs/libxfs/xfs_attr.c:802 xfs_xattr_set+0xeb/0x1a9 fs/xfs/xfs_xattr.c:59 __vfs_removexattr+0x106/0x16a fs/xattr.c:468 cap_inode_killpriv+0x24/0x47 security/commoncap.c:324 security_inode_killpriv+0x54/0xa1 security/security.c:1414 setattr_prepare+0x1a6/0x897 fs/attr.c:146 xfs_vn_change_ok+0x111/0x15e fs/xfs/xfs_iops.c:682 xfs_vn_setattr_size+0x5f/0x15a fs/xfs/xfs_iops.c:1065 xfs_vn_setattr+0x125/0x2ad fs/xfs/xfs_iops.c:1093 notify_change+0xae5/0x10a1 fs/attr.c:410 do_truncate+0x134/0x1e0 fs/open.c:64 handle_truncate fs/namei.c:3084 [inline] do_open fs/namei.c:3432 [inline] path_openat+0x30ab/0x396d fs/namei.c:3561 do_filp_open+0x1c4/0x290 fs/namei.c:3588 do_sys_openat2+0x60d/0x98c fs/open.c:1212 do_sys_open+0xcf/0x13c fs/open.c:1228 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3a/0x7e arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0x0 The buggy address belongs to the object at ffff88802cec9188 which belongs to the cache xfs_ifork of size 40 The buggy address is located 20 bytes inside of 40-byte region [ffff88802cec9188, ffff88802cec91b0) The buggy address belongs to the page: page:00000000c3af36a1 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x2cec9 flags: 0xfffffc0000200(slab|node=0|zone=1|lastcpupid=0x1fffff) raw: 000fffffc0000200 ffffea00009d2580 0000000600000006 ffff88801a9ffc80 raw: 0000000000000000 0000000080490049 00000001ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff88802cec9080: fb fb fb fc fc fa fb fb fb fb fc fc fb fb fb fb ffff88802cec9100: fb fc fc fb fb fb fb fb fc fc fb fb fb fb fb fc >ffff88802cec9180: fc fa fb fb fb fb fc fc fa fb fb fb fb fc fc fb ^ ffff88802cec9200: fb fb fb fb fc fc fb fb fb fb fb fc fc fb fb fb ffff88802cec9280: fb fb fc fc fa fb fb fb fb fc fc fa fb fb fb fb ================================================================== The root cause of this bug is the unlocked access to xfs_inode.i_afp from the getxattr code paths while trying to determine which ILOCK mode to use to stabilize the xattr data. Unfortunately, the VFS does not acquire i_rwsem when vfs_getxattr (or listxattr) call into the filesystem, which means that getxattr can race with a removexattr that's tearing down the attr fork and crash: xfs_attr_set: xfs_attr_get: xfs_attr_fork_remove: xfs_ilock_attr_map_shared: xfs_idestroy_fork(ip->i_afp); kmem_cache_free(xfs_ifork_cache, ip->i_afp); if (ip->i_afp && ip->i_afp = NULL; xfs_need_iread_extents(ip->i_afp)) <KABOOM> ip->i_forkoff = 0; Regrettably, the VFS is much more lax about i_rwsem and getxattr than is immediately obvious -- not only does it not guarantee that we hold i_rwsem, it actually doesn't guarantee that we *don't* hold it either. The getxattr system call won't acquire the lock before calling XFS, but the file capabilities code calls getxattr with and without i_rwsem held to determine if the "security.capabilities" xattr is set on the file. Fixing the VFS locking requires a treewide investigation into every code path that could touch an xattr and what i_rwsem state it expects or sets up. That could take years or even prove impossible; fortunately, we can fix this UAF problem inside XFS. An earlier version of this patch used smp_wmb in xfs_attr_fork_remove to ensure that i_forkoff is always zeroed before i_afp is set to null and changed the read paths to use smp_rmb before accessing i_forkoff and i_afp, which avoided these UAF problems. However, the patch author was too busy dealing with other problems in the meantime, and by the time he came back to this issue, the situation had changed a bit. On a modern system with selinux, each inode will always have at least one xattr for the selinux label, so it doesn't make much sense to keep incurring the extra pointer dereference. Furthermore, Allison's upcoming parent pointer patchset will also cause nearly every inode in the filesystem to have extended attributes. Therefore, make the inode attribute fork structure part of struct xfs_inode, at a cost of 40 more bytes. This patch adds a clunky if_present field where necessary to maintain the existing logic of xattr fork null pointer testing in the existing codebase. The next patch switches the logic over to XFS_IFORK_Q and it all goes away. Signed-off-by: Darrick J. Wong <djwong@kernel.org> Reviewed-by: Dave Chinner <dchinner@redhat.com>
1 parent 732436e commit 2ed5b09

16 files changed

Lines changed: 121 additions & 104 deletions

fs/xfs/libxfs/xfs_attr.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ xfs_inode_hasattr(
6969
{
7070
if (!XFS_IFORK_Q(ip))
7171
return 0;
72-
if (!ip->i_afp)
72+
if (!ip->i_af.if_present)
7373
return 0;
74-
if (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
75-
ip->i_afp->if_nextents == 0)
74+
if (ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
75+
ip->i_af.if_nextents == 0)
7676
return 0;
7777
return 1;
7878
}
@@ -85,7 +85,7 @@ bool
8585
xfs_attr_is_leaf(
8686
struct xfs_inode *ip)
8787
{
88-
struct xfs_ifork *ifp = ip->i_afp;
88+
struct xfs_ifork *ifp = &ip->i_af;
8989
struct xfs_iext_cursor icur;
9090
struct xfs_bmbt_irec imap;
9191

@@ -231,7 +231,7 @@ xfs_attr_get_ilocked(
231231
if (!xfs_inode_hasattr(args->dp))
232232
return -ENOATTR;
233233

234-
if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
234+
if (args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
235235
return xfs_attr_shortform_getvalue(args);
236236
if (xfs_attr_is_leaf(args->dp))
237237
return xfs_attr_leaf_get(args);
@@ -354,7 +354,7 @@ xfs_attr_try_sf_addname(
354354
/*
355355
* Build initial attribute list (if required).
356356
*/
357-
if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
357+
if (dp->i_af.if_format == XFS_DINODE_FMT_EXTENTS)
358358
xfs_attr_shortform_create(args);
359359

360360
error = xfs_attr_shortform_addname(args);
@@ -864,7 +864,7 @@ xfs_attr_lookup(
864864
if (!xfs_inode_hasattr(dp))
865865
return -ENOATTR;
866866

867-
if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
867+
if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
868868
return xfs_attr_sf_findname(args, NULL, NULL);
869869

870870
if (xfs_attr_is_leaf(dp)) {
@@ -1101,7 +1101,7 @@ static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
11011101
{
11021102
struct xfs_attr_shortform *sf;
11031103

1104-
sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
1104+
sf = (struct xfs_attr_shortform *)dp->i_af.if_u1.if_data;
11051105
return be16_to_cpu(sf->hdr.totsize);
11061106
}
11071107

fs/xfs/libxfs/xfs_attr.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,9 @@ static inline bool
560560
xfs_attr_is_shortform(
561561
struct xfs_inode *ip)
562562
{
563-
return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
564-
(ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
565-
ip->i_afp->if_nextents == 0);
563+
return ip->i_af.if_format == XFS_DINODE_FMT_LOCAL ||
564+
(ip->i_af.if_format == XFS_DINODE_FMT_EXTENTS &&
565+
ip->i_af.if_nextents == 0);
566566
}
567567

568568
static inline enum xfs_delattr_state
@@ -573,10 +573,10 @@ xfs_attr_init_add_state(struct xfs_da_args *args)
573573
* next state, the attribute fork may be null. This can occur only occur
574574
* on a pure remove, but we grab the next state before we check if a
575575
* replace operation is being performed. If we are called from any other
576-
* context, i_afp is guaranteed to exist. Hence if the attr fork is
576+
* context, i_af is guaranteed to exist. Hence if the attr fork is
577577
* null, we were called from a pure remove operation and so we are done.
578578
*/
579-
if (!args->dp->i_afp)
579+
if (!args->dp->i_af.if_present)
580580
return XFS_DAS_DONE;
581581

582582
args->op_flags |= XFS_DA_OP_ADDNAME;

fs/xfs/libxfs/xfs_attr_leaf.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ xfs_attr_shortform_create(
682682
struct xfs_da_args *args)
683683
{
684684
struct xfs_inode *dp = args->dp;
685-
struct xfs_ifork *ifp = dp->i_afp;
685+
struct xfs_ifork *ifp = &dp->i_af;
686686
struct xfs_attr_sf_hdr *hdr;
687687

688688
trace_xfs_attr_sf_create(args);
@@ -719,7 +719,7 @@ xfs_attr_sf_findname(
719719
int end;
720720
int i;
721721

722-
sf = (struct xfs_attr_shortform *)args->dp->i_afp->if_u1.if_data;
722+
sf = (struct xfs_attr_shortform *)args->dp->i_af.if_u1.if_data;
723723
sfe = &sf->list[0];
724724
end = sf->hdr.count;
725725
for (i = 0; i < end; sfe = xfs_attr_sf_nextentry(sfe),
@@ -764,7 +764,7 @@ xfs_attr_shortform_add(
764764
mp = dp->i_mount;
765765
dp->i_forkoff = forkoff;
766766

767-
ifp = dp->i_afp;
767+
ifp = &dp->i_af;
768768
ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
769769
sf = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
770770
if (xfs_attr_sf_findname(args, &sfe, NULL) == -EEXIST)
@@ -797,11 +797,10 @@ xfs_attr_fork_remove(
797797
struct xfs_inode *ip,
798798
struct xfs_trans *tp)
799799
{
800-
ASSERT(ip->i_afp->if_nextents == 0);
800+
ASSERT(ip->i_af.if_nextents == 0);
801801

802-
xfs_idestroy_fork(ip->i_afp);
803-
kmem_cache_free(xfs_ifork_cache, ip->i_afp);
804-
ip->i_afp = NULL;
802+
xfs_idestroy_fork(&ip->i_af);
803+
xfs_ifork_zap_attr(ip);
805804
ip->i_forkoff = 0;
806805
xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
807806
}
@@ -825,7 +824,7 @@ xfs_attr_sf_removename(
825824

826825
dp = args->dp;
827826
mp = dp->i_mount;
828-
sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
827+
sf = (struct xfs_attr_shortform *)dp->i_af.if_u1.if_data;
829828

830829
error = xfs_attr_sf_findname(args, &sfe, &base);
831830

@@ -889,7 +888,7 @@ xfs_attr_shortform_lookup(xfs_da_args_t *args)
889888

890889
trace_xfs_attr_sf_lookup(args);
891890

892-
ifp = args->dp->i_afp;
891+
ifp = &args->dp->i_af;
893892
ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
894893
sf = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
895894
sfe = &sf->list[0];
@@ -917,8 +916,8 @@ xfs_attr_shortform_getvalue(
917916
struct xfs_attr_sf_entry *sfe;
918917
int i;
919918

920-
ASSERT(args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL);
921-
sf = (struct xfs_attr_shortform *)args->dp->i_afp->if_u1.if_data;
919+
ASSERT(args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL);
920+
sf = (struct xfs_attr_shortform *)args->dp->i_af.if_u1.if_data;
922921
sfe = &sf->list[0];
923922
for (i = 0; i < sf->hdr.count;
924923
sfe = xfs_attr_sf_nextentry(sfe), i++) {
@@ -948,7 +947,7 @@ xfs_attr_shortform_to_leaf(
948947
trace_xfs_attr_sf_to_leaf(args);
949948

950949
dp = args->dp;
951-
ifp = dp->i_afp;
950+
ifp = &dp->i_af;
952951
sf = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
953952
size = be16_to_cpu(sf->hdr.totsize);
954953
tmpbuffer = kmem_alloc(size, 0);
@@ -1055,7 +1054,7 @@ xfs_attr_shortform_verify(
10551054
int i;
10561055
int64_t size;
10571056

1058-
ASSERT(ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL);
1057+
ASSERT(ip->i_af.if_format == XFS_DINODE_FMT_LOCAL);
10591058
ifp = xfs_ifork_ptr(ip, XFS_ATTR_FORK);
10601059
sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
10611060
size = ifp->if_bytes;

fs/xfs/libxfs/xfs_bmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,9 @@ xfs_bmap_add_attrfork(
10411041
error = xfs_bmap_set_attrforkoff(ip, size, &version);
10421042
if (error)
10431043
goto trans_cancel;
1044-
ASSERT(ip->i_afp == NULL);
1044+
ASSERT(!ip->i_af.if_present);
10451045

1046-
ip->i_afp = xfs_ifork_alloc(XFS_DINODE_FMT_EXTENTS, 0);
1046+
xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
10471047
logflags = 0;
10481048
switch (ip->i_df.if_format) {
10491049
case XFS_DINODE_FMT_LOCAL:

fs/xfs/libxfs/xfs_inode_buf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ xfs_inode_from_disk(
177177
xfs_failaddr_t fa;
178178

179179
ASSERT(ip->i_cowfp == NULL);
180-
ASSERT(ip->i_afp == NULL);
180+
ASSERT(!ip->i_af.if_present);
181181

182182
fa = xfs_dinode_verify(ip->i_mount, ip->i_ino, from);
183183
if (fa) {
@@ -285,15 +285,15 @@ xfs_inode_to_disk_iext_counters(
285285
{
286286
if (xfs_inode_has_large_extent_counts(ip)) {
287287
to->di_big_nextents = cpu_to_be64(xfs_ifork_nextents(&ip->i_df));
288-
to->di_big_anextents = cpu_to_be32(xfs_ifork_nextents(ip->i_afp));
288+
to->di_big_anextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_af));
289289
/*
290290
* We might be upgrading the inode to use larger extent counters
291291
* than was previously used. Hence zero the unused field.
292292
*/
293293
to->di_nrext64_pad = cpu_to_be16(0);
294294
} else {
295295
to->di_nextents = cpu_to_be32(xfs_ifork_nextents(&ip->i_df));
296-
to->di_anextents = cpu_to_be16(xfs_ifork_nextents(ip->i_afp));
296+
to->di_anextents = cpu_to_be16(xfs_ifork_nextents(&ip->i_af));
297297
}
298298
}
299299

@@ -325,7 +325,7 @@ xfs_inode_to_disk(
325325
to->di_nblocks = cpu_to_be64(ip->i_nblocks);
326326
to->di_extsize = cpu_to_be32(ip->i_extsize);
327327
to->di_forkoff = ip->i_forkoff;
328-
to->di_aformat = xfs_ifork_format(ip->i_afp);
328+
to->di_aformat = xfs_ifork_format(&ip->i_af);
329329
to->di_flags = cpu_to_be16(ip->i_diflags);
330330

331331
if (xfs_has_v3inodes(ip->i_mount)) {

fs/xfs/libxfs/xfs_inode_fork.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,30 @@ xfs_dfork_attr_shortform_size(
276276
return be16_to_cpu(atp->hdr.totsize);
277277
}
278278

279-
struct xfs_ifork *
280-
xfs_ifork_alloc(
279+
void
280+
xfs_ifork_init_attr(
281+
struct xfs_inode *ip,
281282
enum xfs_dinode_fmt format,
282283
xfs_extnum_t nextents)
283284
{
284-
struct xfs_ifork *ifp;
285+
ASSERT(!ip->i_af.if_present);
285286

286-
ifp = kmem_cache_zalloc(xfs_ifork_cache, GFP_NOFS | __GFP_NOFAIL);
287-
ifp->if_format = format;
288-
ifp->if_nextents = nextents;
289-
return ifp;
287+
ip->i_af.if_present = 1;
288+
ip->i_af.if_format = format;
289+
ip->i_af.if_nextents = nextents;
290+
}
291+
292+
void
293+
xfs_ifork_zap_attr(
294+
struct xfs_inode *ip)
295+
{
296+
ASSERT(ip->i_af.if_present);
297+
ASSERT(ip->i_af.if_broot == NULL);
298+
ASSERT(ip->i_af.if_u1.if_data == NULL);
299+
ASSERT(ip->i_af.if_height == 0);
300+
301+
memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
302+
ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
290303
}
291304

292305
int
@@ -301,9 +314,9 @@ xfs_iformat_attr_fork(
301314
* Initialize the extent count early, as the per-format routines may
302315
* depend on it.
303316
*/
304-
ip->i_afp = xfs_ifork_alloc(dip->di_aformat, naextents);
317+
xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
305318

306-
switch (ip->i_afp->if_format) {
319+
switch (ip->i_af.if_format) {
307320
case XFS_DINODE_FMT_LOCAL:
308321
error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
309322
xfs_dfork_attr_shortform_size(dip));
@@ -323,10 +336,8 @@ xfs_iformat_attr_fork(
323336
break;
324337
}
325338

326-
if (error) {
327-
kmem_cache_free(xfs_ifork_cache, ip->i_afp);
328-
ip->i_afp = NULL;
329-
}
339+
if (error)
340+
xfs_ifork_zap_attr(ip);
330341
return error;
331342
}
332343

@@ -656,7 +667,7 @@ xfs_iext_state_to_fork(
656667
if (state & BMAP_COWFORK)
657668
return ip->i_cowfp;
658669
else if (state & BMAP_ATTRFORK)
659-
return ip->i_afp;
670+
return &ip->i_af;
660671
return &ip->i_df;
661672
}
662673

@@ -672,6 +683,7 @@ xfs_ifork_init_cow(
672683

673684
ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
674685
GFP_NOFS | __GFP_NOFAIL);
686+
ip->i_cowfp->if_present = 1;
675687
ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
676688
}
677689

@@ -707,10 +719,10 @@ int
707719
xfs_ifork_verify_local_attr(
708720
struct xfs_inode *ip)
709721
{
710-
struct xfs_ifork *ifp = ip->i_afp;
722+
struct xfs_ifork *ifp = &ip->i_af;
711723
xfs_failaddr_t fa;
712724

713-
if (!ifp)
725+
if (!ifp->if_present)
714726
fa = __this_address;
715727
else
716728
fa = xfs_attr_shortform_verify(ip);

fs/xfs/libxfs/xfs_inode_fork.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct xfs_ifork {
2424
xfs_extnum_t if_nextents; /* # of extents in this fork */
2525
short if_broot_bytes; /* bytes allocated for root */
2626
int8_t if_format; /* format of this fork */
27+
int8_t if_present; /* 1 if present */
2728
};
2829

2930
/*
@@ -173,8 +174,9 @@ xfs_dfork_nextents(
173174
return 0;
174175
}
175176

176-
struct xfs_ifork *xfs_ifork_alloc(enum xfs_dinode_fmt format,
177-
xfs_extnum_t nextents);
177+
void xfs_ifork_zap_attr(struct xfs_inode *ip);
178+
void xfs_ifork_init_attr(struct xfs_inode *ip, enum xfs_dinode_fmt format,
179+
xfs_extnum_t nextents);
178180
struct xfs_ifork *xfs_iext_state_to_fork(struct xfs_inode *ip, int state);
179181

180182
int xfs_iformat_data_fork(struct xfs_inode *, struct xfs_dinode *);

fs/xfs/xfs_attr_inactive.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ xfs_attr_inactive(
367367
* removal below.
368368
*/
369369
if (xfs_inode_hasattr(dp) &&
370-
dp->i_afp->if_format != XFS_DINODE_FMT_LOCAL) {
370+
dp->i_af.if_format != XFS_DINODE_FMT_LOCAL) {
371371
error = xfs_attr3_root_inactive(&trans, dp);
372372
if (error)
373373
goto out_cancel;
@@ -388,10 +388,9 @@ xfs_attr_inactive(
388388
xfs_trans_cancel(trans);
389389
out_destroy_fork:
390390
/* kill the in-core attr fork before we drop the inode lock */
391-
if (dp->i_afp) {
392-
xfs_idestroy_fork(dp->i_afp);
393-
kmem_cache_free(xfs_ifork_cache, dp->i_afp);
394-
dp->i_afp = NULL;
391+
if (dp->i_af.if_present) {
392+
xfs_idestroy_fork(&dp->i_af);
393+
xfs_ifork_zap_attr(dp);
395394
}
396395
if (lock_mode)
397396
xfs_iunlock(dp, lock_mode);

fs/xfs/xfs_attr_list.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ xfs_attr_shortform_list(
6161
int sbsize, nsbuf, count, i;
6262
int error = 0;
6363

64-
ASSERT(dp->i_afp != NULL);
65-
sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
64+
ASSERT(dp->i_af.if_present);
65+
sf = (struct xfs_attr_shortform *)dp->i_af.if_u1.if_data;
6666
ASSERT(sf != NULL);
6767
if (!sf->hdr.count)
6868
return 0;
@@ -80,7 +80,7 @@ xfs_attr_shortform_list(
8080
*/
8181
if (context->bufsize == 0 ||
8282
(XFS_ISRESET_CURSOR(cursor) &&
83-
(dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
83+
(dp->i_af.if_bytes + sf->hdr.count * 16) < context->bufsize)) {
8484
for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
8585
if (XFS_IS_CORRUPT(context->dp->i_mount,
8686
!xfs_attr_namecheck(sfe->nameval,
@@ -121,7 +121,7 @@ xfs_attr_shortform_list(
121121
for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
122122
if (unlikely(
123123
((char *)sfe < (char *)sf) ||
124-
((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
124+
((char *)sfe >= ((char *)sf + dp->i_af.if_bytes)))) {
125125
XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
126126
XFS_ERRLEVEL_LOW,
127127
context->dp->i_mount, sfe,
@@ -513,7 +513,7 @@ xfs_attr_list_ilocked(
513513
*/
514514
if (!xfs_inode_hasattr(dp))
515515
return 0;
516-
if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
516+
if (dp->i_af.if_format == XFS_DINODE_FMT_LOCAL)
517517
return xfs_attr_shortform_list(context);
518518
if (xfs_attr_is_leaf(dp))
519519
return xfs_attr_leaf_list(context);

0 commit comments

Comments
 (0)