Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 7 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 8 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_format.h" |
| 10 | #include "xfs_log_format.h" |
| 11 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 13 | #include "xfs_defer.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 14 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 15 | #include "xfs_da_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 16 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 18 | #include "xfs_trans.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs_bmap.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 20 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_attr.h" |
| 22 | #include "xfs_attr_leaf.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 23 | #include "xfs_attr_remote.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_trans_space.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 26 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * xfs_attr.c |
| 30 | * |
| 31 | * Provide the external interfaces to manage attribute lists. |
| 32 | */ |
| 33 | |
| 34 | /*======================================================================== |
| 35 | * Function prototypes for the kernel. |
| 36 | *========================================================================*/ |
| 37 | |
| 38 | /* |
| 39 | * Internal routines when attribute list fits inside the inode. |
| 40 | */ |
| 41 | STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args); |
| 42 | |
| 43 | /* |
| 44 | * Internal routines when attribute list is one block. |
| 45 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 46 | STATIC int xfs_attr_leaf_get(xfs_da_args_t *args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args); |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 48 | STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp); |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 49 | STATIC int xfs_attr_leaf_try_add(struct xfs_da_args *args, struct xfs_buf *bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Internal routines when attribute list is more than one block. |
| 53 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 54 | STATIC int xfs_attr_node_get(xfs_da_args_t *args); |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 55 | STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 56 | STATIC int xfs_attr_node_addname(struct xfs_delattr_context *dac); |
| 57 | STATIC int xfs_attr_node_addname_find_attr(struct xfs_delattr_context *dac); |
| 58 | STATIC int xfs_attr_node_addname_clear_incomplete( |
| 59 | struct xfs_delattr_context *dac); |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 60 | STATIC int xfs_attr_node_hasname(xfs_da_args_t *args, |
| 61 | struct xfs_da_state **state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | STATIC int xfs_attr_fillstate(xfs_da_state_t *state); |
| 63 | STATIC int xfs_attr_refillstate(xfs_da_state_t *state); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 64 | STATIC int xfs_attr_set_iter(struct xfs_delattr_context *dac, |
| 65 | struct xfs_buf **leaf_bp); |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 66 | STATIC int xfs_attr_node_removename(struct xfs_da_args *args, |
| 67 | struct xfs_da_state *state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Dave Chinner | abec5f2 | 2013-08-12 20:49:38 +1000 | [diff] [blame] | 69 | int |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 70 | xfs_inode_hasattr( |
| 71 | struct xfs_inode *ip) |
| 72 | { |
| 73 | if (!XFS_IFORK_Q(ip) || |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 74 | (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | daf8396 | 2020-05-18 10:27:22 -0700 | [diff] [blame] | 75 | ip->i_afp->if_nextents == 0)) |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 76 | return 0; |
| 77 | return 1; |
| 78 | } |
| 79 | |
Christoph Hellwig | 2ac131d | 2021-04-13 11:15:10 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Returns true if the there is exactly only block in the attr fork, in which |
| 82 | * case the attribute fork consists of a single leaf block entry. |
| 83 | */ |
| 84 | bool |
| 85 | xfs_attr_is_leaf( |
| 86 | struct xfs_inode *ip) |
| 87 | { |
| 88 | struct xfs_ifork *ifp = ip->i_afp; |
| 89 | struct xfs_iext_cursor icur; |
| 90 | struct xfs_bmbt_irec imap; |
| 91 | |
| 92 | if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
| 93 | return false; |
| 94 | |
| 95 | xfs_iext_first(ifp, &icur); |
| 96 | xfs_iext_get_extent(ifp, &icur, &imap); |
| 97 | return imap.br_startoff == 0 && imap.br_blockcount == 1; |
| 98 | } |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /*======================================================================== |
| 101 | * Overall external interface routines. |
| 102 | *========================================================================*/ |
| 103 | |
Dave Chinner | 728bcaa | 2019-08-29 09:04:08 -0700 | [diff] [blame] | 104 | /* |
| 105 | * Retrieve an extended attribute and its value. Must have ilock. |
| 106 | * Returns 0 on successful retrieval, otherwise an error. |
| 107 | */ |
Darrick J. Wong | ad017f6 | 2017-06-16 11:00:14 -0700 | [diff] [blame] | 108 | int |
| 109 | xfs_attr_get_ilocked( |
Darrick J. Wong | ad017f6 | 2017-06-16 11:00:14 -0700 | [diff] [blame] | 110 | struct xfs_da_args *args) |
| 111 | { |
Christoph Hellwig | c36f533 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 112 | ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); |
Christoph Hellwig | cf69f82 | 2017-07-13 12:14:33 -0700 | [diff] [blame] | 113 | |
Christoph Hellwig | c36f533 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 114 | if (!xfs_inode_hasattr(args->dp)) |
Darrick J. Wong | ad017f6 | 2017-06-16 11:00:14 -0700 | [diff] [blame] | 115 | return -ENOATTR; |
Christoph Hellwig | c36f533 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 116 | |
Christoph Hellwig | f7e67b2 | 2020-05-18 10:28:05 -0700 | [diff] [blame] | 117 | if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) |
Darrick J. Wong | ad017f6 | 2017-06-16 11:00:14 -0700 | [diff] [blame] | 118 | return xfs_attr_shortform_getvalue(args); |
Christoph Hellwig | 2ac131d | 2021-04-13 11:15:10 -0700 | [diff] [blame] | 119 | if (xfs_attr_is_leaf(args->dp)) |
Darrick J. Wong | ad017f6 | 2017-06-16 11:00:14 -0700 | [diff] [blame] | 120 | return xfs_attr_leaf_get(args); |
Christoph Hellwig | c36f533 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 121 | return xfs_attr_node_get(args); |
Darrick J. Wong | ad017f6 | 2017-06-16 11:00:14 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dave Chinner | ddbca70 | 2019-08-29 09:04:10 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Retrieve an extended attribute by name, and its value if requested. |
| 126 | * |
Christoph Hellwig | e513e25 | 2020-02-26 17:30:35 -0800 | [diff] [blame] | 127 | * If args->valuelen is zero, then the caller does not want the value, just an |
| 128 | * indication whether the attribute exists and the size of the value if it |
| 129 | * exists. The size is returned in args.valuelen. |
Dave Chinner | ddbca70 | 2019-08-29 09:04:10 -0700 | [diff] [blame] | 130 | * |
Christoph Hellwig | d49db18 | 2020-02-26 17:30:35 -0800 | [diff] [blame] | 131 | * If args->value is NULL but args->valuelen is non-zero, allocate the buffer |
| 132 | * for the value after existence of the attribute has been determined. The |
| 133 | * caller always has to free args->value if it is set, no matter if this |
| 134 | * function was successful or not. |
| 135 | * |
Dave Chinner | ddbca70 | 2019-08-29 09:04:10 -0700 | [diff] [blame] | 136 | * If the attribute is found, but exceeds the size limit set by the caller in |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 137 | * args->valuelen, return -ERANGE with the size of the attribute that was found |
| 138 | * in args->valuelen. |
Dave Chinner | ddbca70 | 2019-08-29 09:04:10 -0700 | [diff] [blame] | 139 | */ |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 140 | int |
| 141 | xfs_attr_get( |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 142 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Christoph Hellwig | b87d022 | 2014-05-13 16:34:24 +1000 | [diff] [blame] | 144 | uint lock_mode; |
| 145 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 147 | XFS_STATS_INC(args->dp->i_mount, xs_attr_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 149 | if (xfs_is_shutdown(args->dp->i_mount)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 150 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 152 | args->geo = args->dp->i_mount->m_attr_geo; |
| 153 | args->whichfork = XFS_ATTR_FORK; |
| 154 | args->hashval = xfs_da_hashname(args->name, args->namelen); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 155 | |
Eric Sandeen | c400ee3 | 2015-08-19 10:30:48 +1000 | [diff] [blame] | 156 | /* Entirely possible to look up a name which doesn't exist */ |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 157 | args->op_flags = XFS_DA_OP_OKNOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 159 | lock_mode = xfs_ilock_attr_map_shared(args->dp); |
Christoph Hellwig | c36f533 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 160 | error = xfs_attr_get_ilocked(args); |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 161 | xfs_iunlock(args->dp, lock_mode); |
Dave Chinner | ddbca70 | 2019-08-29 09:04:10 -0700 | [diff] [blame] | 162 | |
Christoph Hellwig | e5171d7 | 2020-02-26 17:30:34 -0800 | [diff] [blame] | 163 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 166 | /* |
| 167 | * Calculate how many blocks we need for the new attribute, |
| 168 | */ |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 169 | STATIC int |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 170 | xfs_attr_calc_size( |
Christoph Hellwig | 6c888af | 2014-05-13 16:40:19 +1000 | [diff] [blame] | 171 | struct xfs_da_args *args, |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 172 | int *local) |
| 173 | { |
Christoph Hellwig | 6c888af | 2014-05-13 16:40:19 +1000 | [diff] [blame] | 174 | struct xfs_mount *mp = args->dp->i_mount; |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 175 | int size; |
| 176 | int nblks; |
| 177 | |
| 178 | /* |
| 179 | * Determine space new attribute will use, and if it would be |
| 180 | * "local" or "remote" (note: local != inline). |
| 181 | */ |
Dave Chinner | c59f0ad | 2014-06-06 15:21:27 +1000 | [diff] [blame] | 182 | size = xfs_attr_leaf_newentsize(args, local); |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 183 | nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK); |
| 184 | if (*local) { |
Dave Chinner | 33a6039 | 2014-06-06 15:21:10 +1000 | [diff] [blame] | 185 | if (size > (args->geo->blksize / 2)) { |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 186 | /* Double split possible */ |
| 187 | nblks *= 2; |
| 188 | } |
| 189 | } else { |
| 190 | /* |
| 191 | * Out of line attribute, cannot double split, but |
| 192 | * make room for the attribute value itself. |
| 193 | */ |
Dave Chinner | 2d6dcc6 | 2014-05-15 09:39:28 +1000 | [diff] [blame] | 194 | uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen); |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 195 | nblks += dblocks; |
| 196 | nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK); |
| 197 | } |
| 198 | |
| 199 | return nblks; |
| 200 | } |
| 201 | |
Allison Henderson | 4c74a56 | 2018-10-18 17:20:50 +1100 | [diff] [blame] | 202 | STATIC int |
| 203 | xfs_attr_try_sf_addname( |
| 204 | struct xfs_inode *dp, |
| 205 | struct xfs_da_args *args) |
| 206 | { |
| 207 | |
Allison Collins | 6cc5b5f | 2020-07-20 21:47:24 -0700 | [diff] [blame] | 208 | int error; |
| 209 | |
| 210 | /* |
| 211 | * Build initial attribute list (if required). |
| 212 | */ |
| 213 | if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS) |
| 214 | xfs_attr_shortform_create(args); |
Allison Henderson | 4c74a56 | 2018-10-18 17:20:50 +1100 | [diff] [blame] | 215 | |
| 216 | error = xfs_attr_shortform_addname(args); |
| 217 | if (error == -ENOSPC) |
| 218 | return error; |
| 219 | |
| 220 | /* |
| 221 | * Commit the shortform mods, and we're done. |
| 222 | * NOTE: this is also the error path (EEXIST, etc). |
| 223 | */ |
Christoph Hellwig | 1d73301 | 2020-02-26 17:30:36 -0800 | [diff] [blame] | 224 | if (!error && !(args->op_flags & XFS_DA_OP_NOTIME)) |
Allison Henderson | 4c74a56 | 2018-10-18 17:20:50 +1100 | [diff] [blame] | 225 | xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG); |
| 226 | |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 227 | if (xfs_has_wsync(dp->i_mount)) |
Allison Henderson | 4c74a56 | 2018-10-18 17:20:50 +1100 | [diff] [blame] | 228 | xfs_trans_set_sync(args->trans); |
| 229 | |
Allison Collins | 6cc5b5f | 2020-07-20 21:47:24 -0700 | [diff] [blame] | 230 | return error; |
Allison Henderson | 4c74a56 | 2018-10-18 17:20:50 +1100 | [diff] [blame] | 231 | } |
| 232 | |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 233 | /* |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 234 | * Check to see if the attr should be upgraded from non-existent or shortform to |
| 235 | * single-leaf-block attribute list. |
| 236 | */ |
| 237 | static inline bool |
| 238 | xfs_attr_is_shortform( |
| 239 | struct xfs_inode *ip) |
| 240 | { |
| 241 | return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL || |
| 242 | (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS && |
| 243 | ip->i_afp->if_nextents == 0); |
| 244 | } |
| 245 | |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 246 | /* |
| 247 | * Checks to see if a delayed attribute transaction should be rolled. If so, |
| 248 | * transaction is finished or rolled as needed. |
| 249 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 250 | STATIC int |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 251 | xfs_attr_trans_roll( |
| 252 | struct xfs_delattr_context *dac) |
| 253 | { |
| 254 | struct xfs_da_args *args = dac->da_args; |
| 255 | int error; |
| 256 | |
| 257 | if (dac->flags & XFS_DAC_DEFER_FINISH) { |
| 258 | /* |
| 259 | * The caller wants us to finish all the deferred ops so that we |
| 260 | * avoid pinning the log tail with a large number of deferred |
| 261 | * ops. |
| 262 | */ |
| 263 | dac->flags &= ~XFS_DAC_DEFER_FINISH; |
| 264 | error = xfs_defer_finish(&args->trans); |
| 265 | } else |
| 266 | error = xfs_trans_roll_inode(&args->trans, args->dp); |
| 267 | |
| 268 | return error; |
| 269 | } |
| 270 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 271 | /* |
| 272 | * Set the attribute specified in @args. |
| 273 | */ |
| 274 | int |
| 275 | xfs_attr_set_args( |
| 276 | struct xfs_da_args *args) |
| 277 | { |
| 278 | struct xfs_buf *leaf_bp = NULL; |
| 279 | int error = 0; |
| 280 | struct xfs_delattr_context dac = { |
| 281 | .da_args = args, |
| 282 | }; |
| 283 | |
| 284 | do { |
| 285 | error = xfs_attr_set_iter(&dac, &leaf_bp); |
| 286 | if (error != -EAGAIN) |
| 287 | break; |
| 288 | |
| 289 | error = xfs_attr_trans_roll(&dac); |
| 290 | if (error) { |
| 291 | if (leaf_bp) |
| 292 | xfs_trans_brelse(args->trans, leaf_bp); |
| 293 | return error; |
| 294 | } |
| 295 | } while (true); |
| 296 | |
| 297 | return error; |
| 298 | } |
| 299 | |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 300 | STATIC int |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 301 | xfs_attr_sf_addname( |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 302 | struct xfs_delattr_context *dac, |
| 303 | struct xfs_buf **leaf_bp) |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 304 | { |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 305 | struct xfs_da_args *args = dac->da_args; |
| 306 | struct xfs_inode *dp = args->dp; |
| 307 | int error = 0; |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Try to add the attr to the attribute list in the inode. |
| 311 | */ |
| 312 | error = xfs_attr_try_sf_addname(dp, args); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 313 | |
| 314 | /* Should only be 0, -EEXIST or -ENOSPC */ |
| 315 | if (error != -ENOSPC) |
| 316 | return error; |
Allison Henderson | 6286514 | 2021-02-18 01:09:18 -0700 | [diff] [blame] | 317 | |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 318 | /* |
| 319 | * It won't fit in the shortform, transform to a leaf block. GROT: |
| 320 | * another possible req'mt for a double-split btree op. |
| 321 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 322 | error = xfs_attr_shortform_to_leaf(args, leaf_bp); |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 323 | if (error) |
| 324 | return error; |
| 325 | |
| 326 | /* |
| 327 | * Prevent the leaf buffer from being unlocked so that a concurrent AIL |
| 328 | * push cannot grab the half-baked leaf buffer and run into problems |
Allison Henderson | 6286514 | 2021-02-18 01:09:18 -0700 | [diff] [blame] | 329 | * with the write verifier. |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 330 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 331 | xfs_trans_bhold(args->trans, *leaf_bp); |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 332 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 333 | /* |
| 334 | * We're still in XFS_DAS_UNINIT state here. We've converted |
| 335 | * the attr fork to leaf format and will restart with the leaf |
| 336 | * add. |
| 337 | */ |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 338 | trace_xfs_attr_sf_addname_return(XFS_DAS_UNINIT, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 339 | dac->flags |= XFS_DAC_DEFER_FINISH; |
Allison Henderson | 6286514 | 2021-02-18 01:09:18 -0700 | [diff] [blame] | 340 | return -EAGAIN; |
Allison Collins | db1a28c | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 344 | * Set the attribute specified in @args. |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 345 | * This routine is meant to function as a delayed operation, and may return |
| 346 | * -EAGAIN when the transaction needs to be rolled. Calling functions will need |
| 347 | * to handle this, and recall the function until a successful error code is |
| 348 | * returned. |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 349 | */ |
| 350 | int |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 351 | xfs_attr_set_iter( |
| 352 | struct xfs_delattr_context *dac, |
| 353 | struct xfs_buf **leaf_bp) |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 354 | { |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 355 | struct xfs_da_args *args = dac->da_args; |
| 356 | struct xfs_inode *dp = args->dp; |
| 357 | struct xfs_buf *bp = NULL; |
| 358 | int forkoff, error = 0; |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 359 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 360 | /* State machine switch */ |
| 361 | switch (dac->dela_state) { |
| 362 | case XFS_DAS_UNINIT: |
| 363 | /* |
| 364 | * If the fork is shortform, attempt to add the attr. If there |
| 365 | * is no space, this converts to leaf format and returns |
| 366 | * -EAGAIN with the leaf buffer held across the roll. The caller |
| 367 | * will deal with a transaction roll error, but otherwise |
| 368 | * release the hold once we return with a clean transaction. |
| 369 | */ |
| 370 | if (xfs_attr_is_shortform(dp)) |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 371 | return xfs_attr_sf_addname(dac, leaf_bp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 372 | if (*leaf_bp != NULL) { |
| 373 | xfs_trans_bhold_release(args->trans, *leaf_bp); |
| 374 | *leaf_bp = NULL; |
Allison Henderson | 3f562d0 | 2021-02-12 12:27:14 -0700 | [diff] [blame] | 375 | } |
Allison Collins | 7c93d4a | 2020-07-20 21:47:24 -0700 | [diff] [blame] | 376 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 377 | if (xfs_attr_is_leaf(dp)) { |
| 378 | error = xfs_attr_leaf_try_add(args, *leaf_bp); |
| 379 | if (error == -ENOSPC) { |
| 380 | error = xfs_attr3_leaf_to_node(args); |
| 381 | if (error) |
| 382 | return error; |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 383 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 384 | /* |
| 385 | * Finish any deferred work items and roll the |
| 386 | * transaction once more. The goal here is to |
| 387 | * call node_addname with the inode and |
| 388 | * transaction in the same state (inode locked |
| 389 | * and joined, transaction clean) no matter how |
| 390 | * we got to this step. |
| 391 | * |
| 392 | * At this point, we are still in |
| 393 | * XFS_DAS_UNINIT, but when we come back, we'll |
| 394 | * be a node, so we'll fall down into the node |
| 395 | * handling code below |
| 396 | */ |
| 397 | dac->flags |= XFS_DAC_DEFER_FINISH; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 398 | trace_xfs_attr_set_iter_return( |
| 399 | dac->dela_state, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 400 | return -EAGAIN; |
| 401 | } else if (error) { |
| 402 | return error; |
| 403 | } |
| 404 | |
| 405 | dac->dela_state = XFS_DAS_FOUND_LBLK; |
| 406 | } else { |
| 407 | error = xfs_attr_node_addname_find_attr(dac); |
| 408 | if (error) |
| 409 | return error; |
| 410 | |
| 411 | error = xfs_attr_node_addname(dac); |
| 412 | if (error) |
| 413 | return error; |
| 414 | |
| 415 | dac->dela_state = XFS_DAS_FOUND_NBLK; |
| 416 | } |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 417 | trace_xfs_attr_set_iter_return(dac->dela_state, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 418 | return -EAGAIN; |
| 419 | case XFS_DAS_FOUND_LBLK: |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 420 | /* |
| 421 | * If there was an out-of-line value, allocate the blocks we |
| 422 | * identified for its storage and copy the value. This is done |
| 423 | * after we create the attribute so that we don't overflow the |
| 424 | * maximum size of a transaction and/or hit a deadlock. |
| 425 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 426 | |
| 427 | /* Open coded xfs_attr_rmtval_set without trans handling */ |
| 428 | if ((dac->flags & XFS_DAC_LEAF_ADDNAME_INIT) == 0) { |
| 429 | dac->flags |= XFS_DAC_LEAF_ADDNAME_INIT; |
| 430 | if (args->rmtblkno > 0) { |
| 431 | error = xfs_attr_rmtval_find_space(dac); |
| 432 | if (error) |
| 433 | return error; |
| 434 | } |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 437 | /* |
| 438 | * Repeat allocating remote blocks for the attr value until |
| 439 | * blkcnt drops to zero. |
| 440 | */ |
| 441 | if (dac->blkcnt > 0) { |
| 442 | error = xfs_attr_rmtval_set_blk(dac); |
| 443 | if (error) |
| 444 | return error; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 445 | trace_xfs_attr_set_iter_return(dac->dela_state, |
| 446 | args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 447 | return -EAGAIN; |
| 448 | } |
| 449 | |
| 450 | error = xfs_attr_rmtval_set_value(args); |
| 451 | if (error) |
| 452 | return error; |
| 453 | |
| 454 | /* |
| 455 | * If this is not a rename, clear the incomplete flag and we're |
| 456 | * done. |
| 457 | */ |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 458 | if (!(args->op_flags & XFS_DA_OP_RENAME)) { |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 459 | if (args->rmtblkno > 0) |
| 460 | error = xfs_attr3_leaf_clearflag(args); |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 461 | return error; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * If this is an atomic rename operation, we must "flip" the |
| 466 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 467 | * so that one disappears and one appears atomically. Then we |
| 468 | * must remove the "old" attribute/value pair. |
| 469 | * |
| 470 | * In a separate transaction, set the incomplete flag on the |
| 471 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 472 | */ |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 473 | error = xfs_attr3_leaf_flipflags(args); |
| 474 | if (error) |
| 475 | return error; |
| 476 | /* |
| 477 | * Commit the flag value change and start the next trans in |
| 478 | * series. |
| 479 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 480 | dac->dela_state = XFS_DAS_FLIP_LFLAG; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 481 | trace_xfs_attr_set_iter_return(dac->dela_state, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 482 | return -EAGAIN; |
| 483 | case XFS_DAS_FLIP_LFLAG: |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 484 | /* |
| 485 | * Dismantle the "old" attribute/value pair by removing a |
| 486 | * "remote" value (if it exists). |
| 487 | */ |
| 488 | xfs_attr_restore_rmt_blk(args); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 489 | error = xfs_attr_rmtval_invalidate(args); |
| 490 | if (error) |
| 491 | return error; |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 492 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 493 | fallthrough; |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 494 | case XFS_DAS_RM_LBLK: |
| 495 | /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */ |
| 496 | dac->dela_state = XFS_DAS_RM_LBLK; |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 497 | if (args->rmtblkno) { |
Allison Henderson | 5e68b4c | 2021-08-08 08:27:14 -0700 | [diff] [blame] | 498 | error = xfs_attr_rmtval_remove(dac); |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 499 | if (error == -EAGAIN) |
| 500 | trace_xfs_attr_set_iter_return( |
| 501 | dac->dela_state, args->dp); |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 502 | if (error) |
| 503 | return error; |
| 504 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 505 | dac->dela_state = XFS_DAS_RD_LEAF; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 506 | trace_xfs_attr_set_iter_return(dac->dela_state, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 507 | return -EAGAIN; |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 510 | fallthrough; |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 511 | case XFS_DAS_RD_LEAF: |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 512 | /* |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 513 | * This is the last step for leaf format. Read the block with |
| 514 | * the old attr, remove the old attr, check for shortform |
| 515 | * conversion and return. |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 516 | */ |
| 517 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, |
| 518 | &bp); |
| 519 | if (error) |
| 520 | return error; |
| 521 | |
| 522 | xfs_attr3_leaf_remove(bp, args); |
| 523 | |
Allison Henderson | 83c6e70 | 2021-04-19 12:55:26 -0700 | [diff] [blame] | 524 | forkoff = xfs_attr_shortform_allfit(bp, dp); |
| 525 | if (forkoff) |
| 526 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
| 527 | /* bp is gone due to xfs_da_shrink_inode */ |
| 528 | |
| 529 | return error; |
Allison Henderson | 3f562d0 | 2021-02-12 12:27:14 -0700 | [diff] [blame] | 530 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 531 | case XFS_DAS_FOUND_NBLK: |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 532 | /* |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 533 | * Find space for remote blocks and fall into the allocation |
| 534 | * state. |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 535 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 536 | if (args->rmtblkno > 0) { |
| 537 | error = xfs_attr_rmtval_find_space(dac); |
| 538 | if (error) |
| 539 | return error; |
| 540 | } |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 541 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 542 | fallthrough; |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 543 | case XFS_DAS_ALLOC_NODE: |
| 544 | /* |
| 545 | * If there was an out-of-line value, allocate the blocks we |
| 546 | * identified for its storage and copy the value. This is done |
| 547 | * after we create the attribute so that we don't overflow the |
| 548 | * maximum size of a transaction and/or hit a deadlock. |
| 549 | */ |
| 550 | dac->dela_state = XFS_DAS_ALLOC_NODE; |
| 551 | if (args->rmtblkno > 0) { |
| 552 | if (dac->blkcnt > 0) { |
| 553 | error = xfs_attr_rmtval_set_blk(dac); |
| 554 | if (error) |
| 555 | return error; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 556 | trace_xfs_attr_set_iter_return( |
| 557 | dac->dela_state, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 558 | return -EAGAIN; |
| 559 | } |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 560 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 561 | error = xfs_attr_rmtval_set_value(args); |
| 562 | if (error) |
| 563 | return error; |
| 564 | } |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 565 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 566 | /* |
| 567 | * If this was not a rename, clear the incomplete flag and we're |
| 568 | * done. |
| 569 | */ |
| 570 | if (!(args->op_flags & XFS_DA_OP_RENAME)) { |
| 571 | if (args->rmtblkno > 0) |
| 572 | error = xfs_attr3_leaf_clearflag(args); |
| 573 | goto out; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * If this is an atomic rename operation, we must "flip" the |
| 578 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 579 | * so that one disappears and one appears atomically. Then we |
| 580 | * must remove the "old" attribute/value pair. |
| 581 | * |
| 582 | * In a separate transaction, set the incomplete flag on the |
| 583 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 584 | */ |
| 585 | error = xfs_attr3_leaf_flipflags(args); |
| 586 | if (error) |
| 587 | goto out; |
| 588 | /* |
| 589 | * Commit the flag value change and start the next trans in |
| 590 | * series |
| 591 | */ |
| 592 | dac->dela_state = XFS_DAS_FLIP_NFLAG; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 593 | trace_xfs_attr_set_iter_return(dac->dela_state, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 594 | return -EAGAIN; |
| 595 | |
| 596 | case XFS_DAS_FLIP_NFLAG: |
| 597 | /* |
| 598 | * Dismantle the "old" attribute/value pair by removing a |
| 599 | * "remote" value (if it exists). |
| 600 | */ |
| 601 | xfs_attr_restore_rmt_blk(args); |
| 602 | |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 603 | error = xfs_attr_rmtval_invalidate(args); |
| 604 | if (error) |
| 605 | return error; |
| 606 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 607 | fallthrough; |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 608 | case XFS_DAS_RM_NBLK: |
| 609 | /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */ |
| 610 | dac->dela_state = XFS_DAS_RM_NBLK; |
| 611 | if (args->rmtblkno) { |
Allison Henderson | 5e68b4c | 2021-08-08 08:27:14 -0700 | [diff] [blame] | 612 | error = xfs_attr_rmtval_remove(dac); |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 613 | if (error == -EAGAIN) |
| 614 | trace_xfs_attr_set_iter_return( |
| 615 | dac->dela_state, args->dp); |
| 616 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 617 | if (error) |
| 618 | return error; |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 619 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 620 | dac->dela_state = XFS_DAS_CLR_FLAG; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 621 | trace_xfs_attr_set_iter_return(dac->dela_state, args->dp); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 622 | return -EAGAIN; |
| 623 | } |
| 624 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 625 | fallthrough; |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 626 | case XFS_DAS_CLR_FLAG: |
| 627 | /* |
| 628 | * The last state for node format. Look up the old attr and |
| 629 | * remove it. |
| 630 | */ |
| 631 | error = xfs_attr_node_addname_clear_incomplete(dac); |
| 632 | break; |
| 633 | default: |
Allison Henderson | 4a4957c | 2021-05-21 00:57:15 -0700 | [diff] [blame] | 634 | ASSERT(0); |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 635 | break; |
| 636 | } |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 637 | out: |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 638 | return error; |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 639 | } |
| 640 | |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 641 | |
Allison Henderson | 068f985 | 2018-10-18 17:21:23 +1100 | [diff] [blame] | 642 | /* |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 643 | * Return EEXIST if attr is found, or ENOATTR if not |
| 644 | */ |
Dave Chinner | 51b495e | 2021-08-18 18:46:25 -0700 | [diff] [blame] | 645 | static int |
| 646 | xfs_attr_lookup( |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 647 | struct xfs_da_args *args) |
| 648 | { |
| 649 | struct xfs_inode *dp = args->dp; |
| 650 | struct xfs_buf *bp = NULL; |
| 651 | int error; |
| 652 | |
| 653 | if (!xfs_inode_hasattr(dp)) |
| 654 | return -ENOATTR; |
| 655 | |
Christoph Hellwig | 0779f4a | 2021-04-13 11:15:11 -0700 | [diff] [blame] | 656 | if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 657 | return xfs_attr_sf_findname(args, NULL, NULL); |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 658 | |
Christoph Hellwig | 2ac131d | 2021-04-13 11:15:10 -0700 | [diff] [blame] | 659 | if (xfs_attr_is_leaf(dp)) { |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 660 | error = xfs_attr_leaf_hasname(args, &bp); |
| 661 | |
| 662 | if (bp) |
| 663 | xfs_trans_brelse(args->trans, bp); |
| 664 | |
| 665 | return error; |
| 666 | } |
| 667 | |
| 668 | return xfs_attr_node_hasname(args, NULL); |
| 669 | } |
| 670 | |
| 671 | /* |
Allison Henderson | 068f985 | 2018-10-18 17:21:23 +1100 | [diff] [blame] | 672 | * Remove the attribute specified in @args. |
| 673 | */ |
| 674 | int |
| 675 | xfs_attr_remove_args( |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 676 | struct xfs_da_args *args) |
Allison Henderson | 068f985 | 2018-10-18 17:21:23 +1100 | [diff] [blame] | 677 | { |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 678 | int error; |
| 679 | struct xfs_delattr_context dac = { |
| 680 | .da_args = args, |
| 681 | }; |
Allison Henderson | 068f985 | 2018-10-18 17:21:23 +1100 | [diff] [blame] | 682 | |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 683 | do { |
| 684 | error = xfs_attr_remove_iter(&dac); |
| 685 | if (error != -EAGAIN) |
| 686 | break; |
| 687 | |
| 688 | error = xfs_attr_trans_roll(&dac); |
| 689 | if (error) |
| 690 | return error; |
| 691 | |
| 692 | } while (true); |
| 693 | |
| 694 | return error; |
Allison Henderson | 068f985 | 2018-10-18 17:21:23 +1100 | [diff] [blame] | 695 | } |
| 696 | |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 697 | /* |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 698 | * Note: If args->value is NULL the attribute will be removed, just like the |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 699 | * Linux ->setattr API. |
| 700 | */ |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 701 | int |
| 702 | xfs_attr_set( |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 703 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | { |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 705 | struct xfs_inode *dp = args->dp; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 706 | struct xfs_mount *mp = dp->i_mount; |
| 707 | struct xfs_trans_res tres; |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 708 | bool rsvd = (args->attr_filter & XFS_ATTR_ROOT); |
Allison Henderson | 4c74a56 | 2018-10-18 17:20:50 +1100 | [diff] [blame] | 709 | int error, local; |
Chandan Babu R | 3a19bb1 | 2021-01-22 16:48:13 -0800 | [diff] [blame] | 710 | int rmt_blks = 0; |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 711 | unsigned int total; |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 712 | |
Dave Chinner | 75c8c50f | 2021-08-18 18:46:53 -0700 | [diff] [blame] | 713 | if (xfs_is_shutdown(dp->i_mount)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 714 | return -EIO; |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 715 | |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 716 | error = xfs_qm_dqattach(dp); |
| 717 | if (error) |
| 718 | return error; |
| 719 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 720 | args->geo = mp->m_attr_geo; |
| 721 | args->whichfork = XFS_ATTR_FORK; |
| 722 | args->hashval = xfs_da_hashname(args->name, args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | /* |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 725 | * We have no control over the attribute names that userspace passes us |
| 726 | * to remove, so we have to allow the name lookup prior to attribute |
| 727 | * removal to fail as well. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | */ |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 729 | args->op_flags = XFS_DA_OP_OKNOENT; |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 730 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 731 | if (args->value) { |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 732 | XFS_STATS_INC(mp, xs_attr_set); |
| 733 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 734 | args->op_flags |= XFS_DA_OP_ADDNAME; |
| 735 | args->total = xfs_attr_calc_size(args, &local); |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * If the inode doesn't have an attribute fork, add one. |
| 739 | * (inode must not be locked when we call this routine) |
| 740 | */ |
| 741 | if (XFS_IFORK_Q(dp) == 0) { |
| 742 | int sf_size = sizeof(struct xfs_attr_sf_hdr) + |
Carlos Maiolino | e01b7ee | 2020-09-07 08:08:50 -0700 | [diff] [blame] | 743 | xfs_attr_sf_entsize_byname(args->namelen, |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 744 | args->valuelen); |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 745 | |
| 746 | error = xfs_bmap_add_attrfork(dp, sf_size, rsvd); |
| 747 | if (error) |
| 748 | return error; |
| 749 | } |
| 750 | |
| 751 | tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres + |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 752 | M_RES(mp)->tr_attrsetrt.tr_logres * |
| 753 | args->total; |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 754 | tres.tr_logcount = XFS_ATTRSET_LOG_COUNT; |
| 755 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 756 | total = args->total; |
Chandan Babu R | 3a19bb1 | 2021-01-22 16:48:13 -0800 | [diff] [blame] | 757 | |
| 758 | if (!local) |
| 759 | rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen); |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 760 | } else { |
| 761 | XFS_STATS_INC(mp, xs_attr_remove); |
| 762 | |
| 763 | tres = M_RES(mp)->tr_attrrm; |
| 764 | total = XFS_ATTRRM_SPACE_RES(mp); |
Chandan Babu R | 3a19bb1 | 2021-01-22 16:48:13 -0800 | [diff] [blame] | 765 | rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | /* |
| 769 | * Root fork attributes can use reserved data blocks for this |
| 770 | * operation if necessary |
| 771 | */ |
Darrick J. Wong | 3de4eb1 | 2021-01-26 16:44:07 -0800 | [diff] [blame] | 772 | error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 773 | if (error) |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 774 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Chandan Babu R | 3a19bb1 | 2021-01-22 16:48:13 -0800 | [diff] [blame] | 776 | if (args->value || xfs_inode_hasattr(dp)) { |
| 777 | error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK, |
| 778 | XFS_IEXT_ATTR_MANIP_CNT(rmt_blks)); |
| 779 | if (error) |
| 780 | goto out_trans_cancel; |
| 781 | } |
| 782 | |
Dave Chinner | 51b495e | 2021-08-18 18:46:25 -0700 | [diff] [blame] | 783 | error = xfs_attr_lookup(args); |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 784 | if (args->value) { |
Allison Collins | deed951 | 2020-07-20 21:47:23 -0700 | [diff] [blame] | 785 | if (error == -EEXIST && (args->attr_flags & XATTR_CREATE)) |
| 786 | goto out_trans_cancel; |
| 787 | if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE)) |
| 788 | goto out_trans_cancel; |
| 789 | if (error != -ENOATTR && error != -EEXIST) |
| 790 | goto out_trans_cancel; |
| 791 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 792 | error = xfs_attr_set_args(args); |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 793 | if (error) |
| 794 | goto out_trans_cancel; |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 795 | /* shortform attribute has already been committed */ |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 796 | if (!args->trans) |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 797 | goto out_unlock; |
| 798 | } else { |
Allison Collins | deed951 | 2020-07-20 21:47:23 -0700 | [diff] [blame] | 799 | if (error != -EEXIST) |
| 800 | goto out_trans_cancel; |
| 801 | |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 802 | error = xfs_attr_remove_args(args); |
Christoph Hellwig | 0eb81a5 | 2020-02-26 17:30:29 -0800 | [diff] [blame] | 803 | if (error) |
| 804 | goto out_trans_cancel; |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | /* |
| 808 | * If this is a synchronous mount, make sure that the |
| 809 | * transaction goes to disk before returning to the user. |
| 810 | */ |
Dave Chinner | 0560f31 | 2021-08-18 18:46:52 -0700 | [diff] [blame] | 811 | if (xfs_has_wsync(mp)) |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 812 | xfs_trans_set_sync(args->trans); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
Christoph Hellwig | 1d73301 | 2020-02-26 17:30:36 -0800 | [diff] [blame] | 814 | if (!(args->op_flags & XFS_DA_OP_NOTIME)) |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 815 | xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG); |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | /* |
| 818 | * Commit the last in the sequence of transactions. |
| 819 | */ |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 820 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE); |
| 821 | error = xfs_trans_commit(args->trans); |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 822 | out_unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
Christoph Hellwig | c5b4ac3 | 2014-05-13 16:34:14 +1000 | [diff] [blame] | 824 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 826 | out_trans_cancel: |
Christoph Hellwig | a254462 | 2020-02-26 17:30:33 -0800 | [diff] [blame] | 827 | if (args->trans) |
| 828 | xfs_trans_cancel(args->trans); |
Allison Henderson | 2f3cd80 | 2018-10-18 17:21:16 +1100 | [diff] [blame] | 829 | goto out_unlock; |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 830 | } |
| 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | /*======================================================================== |
| 833 | * External routines when attribute list is inside the inode |
| 834 | *========================================================================*/ |
| 835 | |
Carlos Maiolino | e01b7ee | 2020-09-07 08:08:50 -0700 | [diff] [blame] | 836 | static inline int xfs_attr_sf_totsize(struct xfs_inode *dp) |
| 837 | { |
| 838 | struct xfs_attr_shortform *sf; |
| 839 | |
| 840 | sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data; |
| 841 | return be16_to_cpu(sf->hdr.totsize); |
| 842 | } |
| 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | /* |
| 845 | * Add a name to the shortform attribute list structure |
| 846 | * This is the external routine. |
| 847 | */ |
| 848 | STATIC int |
| 849 | xfs_attr_shortform_addname(xfs_da_args_t *args) |
| 850 | { |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 851 | int newsize, forkoff, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 853 | trace_xfs_attr_sf_addname(args); |
| 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | retval = xfs_attr_shortform_lookup(args); |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 856 | if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE)) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 857 | return retval; |
Christoph Hellwig | f3e93d9 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 858 | if (retval == -EEXIST) { |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 859 | if (args->attr_flags & XATTR_CREATE) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 860 | return retval; |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 861 | retval = xfs_attr_sf_removename(args); |
Darrick J. Wong | 7b38460 | 2018-04-17 19:10:15 -0700 | [diff] [blame] | 862 | if (retval) |
| 863 | return retval; |
| 864 | /* |
| 865 | * Since we have removed the old attr, clear ATTR_REPLACE so |
| 866 | * that the leaf format add routine won't trip over the attr |
| 867 | * not being around. |
| 868 | */ |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 869 | args->attr_flags &= ~XATTR_REPLACE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 872 | if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX || |
| 873 | args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 874 | return -ENOSPC; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 875 | |
Carlos Maiolino | e01b7ee | 2020-09-07 08:08:50 -0700 | [diff] [blame] | 876 | newsize = xfs_attr_sf_totsize(args->dp); |
| 877 | newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 878 | |
| 879 | forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize); |
| 880 | if (!forkoff) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 881 | return -ENOSPC; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 882 | |
| 883 | xfs_attr_shortform_add(args, forkoff); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 884 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | |
| 888 | /*======================================================================== |
| 889 | * External routines when attribute list is one block |
| 890 | *========================================================================*/ |
| 891 | |
Allison Collins | 410c198 | 2020-07-20 21:47:29 -0700 | [diff] [blame] | 892 | /* Store info about a remote block */ |
| 893 | STATIC void |
| 894 | xfs_attr_save_rmt_blk( |
| 895 | struct xfs_da_args *args) |
| 896 | { |
| 897 | args->blkno2 = args->blkno; |
| 898 | args->index2 = args->index; |
| 899 | args->rmtblkno2 = args->rmtblkno; |
| 900 | args->rmtblkcnt2 = args->rmtblkcnt; |
| 901 | args->rmtvaluelen2 = args->rmtvaluelen; |
| 902 | } |
| 903 | |
| 904 | /* Set stored info about a remote block */ |
| 905 | STATIC void |
| 906 | xfs_attr_restore_rmt_blk( |
| 907 | struct xfs_da_args *args) |
| 908 | { |
| 909 | args->blkno = args->blkno2; |
| 910 | args->index = args->index2; |
| 911 | args->rmtblkno = args->rmtblkno2; |
| 912 | args->rmtblkcnt = args->rmtblkcnt2; |
| 913 | args->rmtvaluelen = args->rmtvaluelen2; |
| 914 | } |
| 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | /* |
Allison Collins | 7c93d4a | 2020-07-20 21:47:24 -0700 | [diff] [blame] | 917 | * Tries to add an attribute to an inode in leaf form |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | * |
Allison Collins | 7c93d4a | 2020-07-20 21:47:24 -0700 | [diff] [blame] | 919 | * This function is meant to execute as part of a delayed operation and leaves |
| 920 | * the transaction handling to the caller. On success the attribute is added |
| 921 | * and the inode and transaction are left dirty. If there is not enough space, |
| 922 | * the attr data is converted to node format and -ENOSPC is returned. Caller is |
| 923 | * responsible for handling the dirty inode and transaction or adding the attr |
| 924 | * in node format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 926 | STATIC int |
Allison Collins | 7c93d4a | 2020-07-20 21:47:24 -0700 | [diff] [blame] | 927 | xfs_attr_leaf_try_add( |
| 928 | struct xfs_da_args *args, |
| 929 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | { |
Allison Collins | 0f89edc | 2020-07-20 21:47:31 -0700 | [diff] [blame] | 931 | int retval; |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | * Look up the given attribute in the leaf block. Figure out if |
| 935 | * the given flags produce an error or call for an atomic rename. |
| 936 | */ |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 937 | retval = xfs_attr_leaf_hasname(args, &bp); |
| 938 | if (retval != -ENOATTR && retval != -EEXIST) |
| 939 | return retval; |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 940 | if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE)) |
Christoph Hellwig | f3e93d9 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 941 | goto out_brelse; |
| 942 | if (retval == -EEXIST) { |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 943 | if (args->attr_flags & XATTR_CREATE) |
Christoph Hellwig | f3e93d9 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 944 | goto out_brelse; |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 945 | |
| 946 | trace_xfs_attr_leaf_replace(args); |
| 947 | |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 948 | /* save the attribute state for later removal*/ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 949 | args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */ |
Allison Collins | 410c198 | 2020-07-20 21:47:29 -0700 | [diff] [blame] | 950 | xfs_attr_save_rmt_blk(args); |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 951 | |
| 952 | /* |
| 953 | * clear the remote attr state now that it is saved so that the |
| 954 | * values reflect the state of the attribute we are about to |
| 955 | * add, not the attribute we just found and will remove later. |
| 956 | */ |
| 957 | args->rmtblkno = 0; |
| 958 | args->rmtblkcnt = 0; |
| 959 | args->rmtvaluelen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* |
Allison Collins | 0f89edc | 2020-07-20 21:47:31 -0700 | [diff] [blame] | 963 | * Add the attribute to the leaf block |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | */ |
Allison Collins | 0f89edc | 2020-07-20 21:47:31 -0700 | [diff] [blame] | 965 | return xfs_attr3_leaf_add(bp, args); |
| 966 | |
Allison Collins | 7c93d4a | 2020-07-20 21:47:24 -0700 | [diff] [blame] | 967 | out_brelse: |
| 968 | xfs_trans_brelse(args->trans, bp); |
| 969 | return retval; |
| 970 | } |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | /* |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 973 | * Return EEXIST if attr is found, or ENOATTR if not |
| 974 | */ |
| 975 | STATIC int |
| 976 | xfs_attr_leaf_hasname( |
| 977 | struct xfs_da_args *args, |
| 978 | struct xfs_buf **bp) |
| 979 | { |
| 980 | int error = 0; |
| 981 | |
| 982 | error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp); |
| 983 | if (error) |
| 984 | return error; |
| 985 | |
| 986 | error = xfs_attr3_leaf_lookup_int(*bp, args); |
| 987 | if (error != -ENOATTR && error != -EEXIST) |
| 988 | xfs_trans_brelse(args->trans, *bp); |
| 989 | |
| 990 | return error; |
| 991 | } |
| 992 | |
| 993 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | * Remove a name from the leaf attribute list structure |
| 995 | * |
| 996 | * This leaf block cannot have a "remote" value, we only call this routine |
| 997 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 998 | */ |
| 999 | STATIC int |
Brian Foster | 32a9b7c | 2018-07-11 22:26:11 -0700 | [diff] [blame] | 1000 | xfs_attr_leaf_removename( |
| 1001 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | { |
Brian Foster | 32a9b7c | 2018-07-11 22:26:11 -0700 | [diff] [blame] | 1003 | struct xfs_inode *dp; |
| 1004 | struct xfs_buf *bp; |
| 1005 | int error, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1007 | trace_xfs_attr_leaf_removename(args); |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | /* |
| 1010 | * Remove the attribute. |
| 1011 | */ |
| 1012 | dp = args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1014 | error = xfs_attr_leaf_hasname(args, &bp); |
| 1015 | |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1016 | if (error == -ENOATTR) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1017 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1018 | return error; |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1019 | } else if (error != -EEXIST) |
| 1020 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1022 | xfs_attr3_leaf_remove(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | /* |
| 1025 | * If the result is small enough, shrink it all into the inode. |
| 1026 | */ |
Allison Collins | 0feaef1 | 2020-07-20 21:47:27 -0700 | [diff] [blame] | 1027 | forkoff = xfs_attr_shortform_allfit(bp, dp); |
| 1028 | if (forkoff) |
| 1029 | return xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | /* bp is gone due to xfs_da_shrink_inode */ |
Allison Collins | 0feaef1 | 2020-07-20 21:47:27 -0700 | [diff] [blame] | 1031 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1032 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Look up a name in a leaf attribute list structure. |
| 1037 | * |
| 1038 | * This leaf block cannot have a "remote" value, we only call this routine |
| 1039 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
Dave Chinner | 728bcaa | 2019-08-29 09:04:08 -0700 | [diff] [blame] | 1040 | * |
| 1041 | * Returns 0 on successful retrieval, otherwise an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1043 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | xfs_attr_leaf_get(xfs_da_args_t *args) |
| 1045 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1046 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | int error; |
| 1048 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1049 | trace_xfs_attr_leaf_get(args); |
| 1050 | |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1051 | error = xfs_attr_leaf_hasname(args, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1053 | if (error == -ENOATTR) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1054 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1055 | return error; |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1056 | } else if (error != -EEXIST) |
| 1057 | return error; |
| 1058 | |
| 1059 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1060 | error = xfs_attr3_leaf_getvalue(bp, args); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1061 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | e3cc455 | 2019-08-29 09:04:09 -0700 | [diff] [blame] | 1062 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1065 | /* |
| 1066 | * Return EEXIST if attr is found, or ENOATTR if not |
| 1067 | * statep: If not null is set to point at the found state. Caller will |
| 1068 | * be responsible for freeing the state in this case. |
| 1069 | */ |
| 1070 | STATIC int |
| 1071 | xfs_attr_node_hasname( |
| 1072 | struct xfs_da_args *args, |
| 1073 | struct xfs_da_state **statep) |
| 1074 | { |
| 1075 | struct xfs_da_state *state; |
| 1076 | int retval, error; |
| 1077 | |
| 1078 | state = xfs_da_state_alloc(args); |
| 1079 | if (statep != NULL) |
Yang Xu | a1de97f | 2021-11-24 10:06:02 -0800 | [diff] [blame] | 1080 | *statep = state; |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1081 | |
| 1082 | /* |
| 1083 | * Search to see if name exists, and get back a pointer to it. |
| 1084 | */ |
| 1085 | error = xfs_da3_node_lookup_int(state, &retval); |
Yang Xu | a1de97f | 2021-11-24 10:06:02 -0800 | [diff] [blame] | 1086 | if (error) |
| 1087 | retval = error; |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1088 | |
Yang Xu | a1de97f | 2021-11-24 10:06:02 -0800 | [diff] [blame] | 1089 | if (!statep) |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1090 | xfs_da_state_free(state); |
Yang Xu | a1de97f | 2021-11-24 10:06:02 -0800 | [diff] [blame] | 1091 | |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1092 | return retval; |
| 1093 | } |
| 1094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | /*======================================================================== |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 1096 | * External routines when attribute list size > geo->blksize |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | *========================================================================*/ |
| 1098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | STATIC int |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1100 | xfs_attr_node_addname_find_attr( |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1101 | struct xfs_delattr_context *dac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | { |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1103 | struct xfs_da_args *args = dac->da_args; |
| 1104 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | /* |
| 1107 | * Search to see if name already exists, and get back a pointer |
| 1108 | * to where it should go. |
| 1109 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1110 | retval = xfs_attr_node_hasname(args, &dac->da_state); |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1111 | if (retval != -ENOATTR && retval != -EEXIST) |
Yang Xu | a1de97f | 2021-11-24 10:06:02 -0800 | [diff] [blame] | 1112 | goto error; |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1113 | |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 1114 | if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE)) |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1115 | goto error; |
Christoph Hellwig | f3e93d9 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 1116 | if (retval == -EEXIST) { |
Christoph Hellwig | d5f0f49 | 2020-02-26 17:30:42 -0800 | [diff] [blame] | 1117 | if (args->attr_flags & XATTR_CREATE) |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1118 | goto error; |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1119 | |
| 1120 | trace_xfs_attr_node_replace(args); |
| 1121 | |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 1122 | /* save the attribute state for later removal*/ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1123 | args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */ |
Allison Collins | 410c198 | 2020-07-20 21:47:29 -0700 | [diff] [blame] | 1124 | xfs_attr_save_rmt_blk(args); |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 1125 | |
| 1126 | /* |
| 1127 | * clear the remote attr state now that it is saved so that the |
| 1128 | * values reflect the state of the attribute we are about to |
| 1129 | * add, not the attribute we just found and will remove later. |
| 1130 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | args->rmtblkno = 0; |
| 1132 | args->rmtblkcnt = 0; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 1133 | args->rmtvaluelen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1136 | return 0; |
| 1137 | error: |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1138 | if (dac->da_state) |
| 1139 | xfs_da_state_free(dac->da_state); |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1140 | return retval; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Add a name to a Btree-format attribute list. |
| 1145 | * |
| 1146 | * This will involve walking down the Btree, and may involve splitting |
| 1147 | * leaf nodes and even splitting intermediate nodes up to and including |
| 1148 | * the root node (a special case of an intermediate node). |
| 1149 | * |
| 1150 | * "Remote" attribute values confuse the issue and atomic rename operations |
| 1151 | * add a whole extra layer of confusion on top of that. |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1152 | * |
| 1153 | * This routine is meant to function as a delayed operation, and may return |
| 1154 | * -EAGAIN when the transaction needs to be rolled. Calling functions will need |
| 1155 | * to handle this, and recall the function until a successful error code is |
| 1156 | *returned. |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1157 | */ |
| 1158 | STATIC int |
| 1159 | xfs_attr_node_addname( |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1160 | struct xfs_delattr_context *dac) |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1161 | { |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1162 | struct xfs_da_args *args = dac->da_args; |
| 1163 | struct xfs_da_state *state = dac->da_state; |
| 1164 | struct xfs_da_state_blk *blk; |
| 1165 | int error; |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1166 | |
| 1167 | trace_xfs_attr_node_addname(args); |
| 1168 | |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1169 | blk = &state->path.blk[state->path.active-1]; |
| 1170 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1171 | |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 1172 | error = xfs_attr3_leaf_add(blk->bp, state->args); |
| 1173 | if (error == -ENOSPC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | if (state->path.active == 1) { |
| 1175 | /* |
| 1176 | * Its really a single leaf node, but it had |
| 1177 | * out-of-line values so it looked like it *might* |
| 1178 | * have been a b-tree. |
| 1179 | */ |
| 1180 | xfs_da_state_free(state); |
Eric Sandeen | 6dd93e9 | 2013-07-31 20:18:54 -0500 | [diff] [blame] | 1181 | state = NULL; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1182 | error = xfs_attr3_leaf_to_node(args); |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1183 | if (error) |
Brian Foster | d5a2e28 | 2018-09-29 13:41:58 +1000 | [diff] [blame] | 1184 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| 1186 | /* |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1187 | * Now that we have converted the leaf to a node, we can |
| 1188 | * roll the transaction, and try xfs_attr3_leaf_add |
| 1189 | * again on re-entry. No need to set dela_state to do |
| 1190 | * this. dela_state is still unset by this function at |
| 1191 | * this point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | */ |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1193 | dac->flags |= XFS_DAC_DEFER_FINISH; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 1194 | trace_xfs_attr_node_addname_return( |
| 1195 | dac->dela_state, args->dp); |
Allison Henderson | 6ca5a4a | 2021-04-12 14:15:31 -0700 | [diff] [blame] | 1196 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Split as many Btree elements as required. |
| 1201 | * This code tracks the new and old attr's location |
| 1202 | * in the index/blkno/rmtblkno/rmtblkcnt fields and |
| 1203 | * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields. |
| 1204 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1205 | error = xfs_da3_split(state); |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1206 | if (error) |
Brian Foster | d5a2e28 | 2018-09-29 13:41:58 +1000 | [diff] [blame] | 1207 | goto out; |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1208 | dac->flags |= XFS_DAC_DEFER_FINISH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | } else { |
| 1210 | /* |
| 1211 | * Addition succeeded, update Btree hashvals. |
| 1212 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1213 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Allison Henderson | f0f7c50 | 2021-02-18 01:24:24 -0700 | [diff] [blame] | 1216 | out: |
| 1217 | if (state) |
| 1218 | xfs_da_state_free(state); |
Allison Henderson | 5d954cc | 2021-04-26 16:50:26 -0700 | [diff] [blame] | 1219 | return error; |
Allison Henderson | f0f7c50 | 2021-02-18 01:24:24 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | STATIC int |
| 1224 | xfs_attr_node_addname_clear_incomplete( |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1225 | struct xfs_delattr_context *dac) |
Allison Henderson | f0f7c50 | 2021-02-18 01:24:24 -0700 | [diff] [blame] | 1226 | { |
Allison Henderson | 8f502a4 | 2021-05-21 15:48:13 -0700 | [diff] [blame] | 1227 | struct xfs_da_args *args = dac->da_args; |
Allison Henderson | f0f7c50 | 2021-02-18 01:24:24 -0700 | [diff] [blame] | 1228 | struct xfs_da_state *state = NULL; |
Allison Henderson | f0f7c50 | 2021-02-18 01:24:24 -0700 | [diff] [blame] | 1229 | int retval = 0; |
| 1230 | int error = 0; |
| 1231 | |
Allison Collins | bf4a5cf | 2020-07-20 21:47:31 -0700 | [diff] [blame] | 1232 | /* |
| 1233 | * Re-find the "old" attribute entry after any split ops. The INCOMPLETE |
| 1234 | * flag means that we will find the "old" attr, not the "new" one. |
| 1235 | */ |
| 1236 | args->attr_filter |= XFS_ATTR_INCOMPLETE; |
| 1237 | state = xfs_da_state_alloc(args); |
| 1238 | state->inleaf = 0; |
| 1239 | error = xfs_da3_node_lookup_int(state, &retval); |
| 1240 | if (error) |
| 1241 | goto out; |
| 1242 | |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 1243 | error = xfs_attr_node_removename(args, state); |
Allison Collins | bf4a5cf | 2020-07-20 21:47:31 -0700 | [diff] [blame] | 1244 | |
| 1245 | /* |
| 1246 | * Check to see if the tree needs to be collapsed. |
| 1247 | */ |
| 1248 | if (retval && (state->path.active > 1)) { |
| 1249 | error = xfs_da3_join(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | if (error) |
| 1251 | goto out; |
| 1252 | } |
| 1253 | retval = error = 0; |
| 1254 | |
| 1255 | out: |
| 1256 | if (state) |
| 1257 | xfs_da_state_free(state); |
| 1258 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1259 | return error; |
| 1260 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | /* |
Allison Collins | 3f6e011 | 2020-07-20 21:47:27 -0700 | [diff] [blame] | 1264 | * Shrink an attribute from leaf to shortform |
| 1265 | */ |
| 1266 | STATIC int |
| 1267 | xfs_attr_node_shrink( |
| 1268 | struct xfs_da_args *args, |
| 1269 | struct xfs_da_state *state) |
| 1270 | { |
| 1271 | struct xfs_inode *dp = args->dp; |
| 1272 | int error, forkoff; |
| 1273 | struct xfs_buf *bp; |
| 1274 | |
| 1275 | /* |
| 1276 | * Have to get rid of the copy of this dabuf in the state. |
| 1277 | */ |
| 1278 | ASSERT(state->path.active == 1); |
| 1279 | ASSERT(state->path.blk[0].bp); |
| 1280 | state->path.blk[0].bp = NULL; |
| 1281 | |
| 1282 | error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp); |
| 1283 | if (error) |
| 1284 | return error; |
| 1285 | |
| 1286 | forkoff = xfs_attr_shortform_allfit(bp, dp); |
| 1287 | if (forkoff) { |
| 1288 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
| 1289 | /* bp is gone due to xfs_da_shrink_inode */ |
Allison Collins | 3f6e011 | 2020-07-20 21:47:27 -0700 | [diff] [blame] | 1290 | } else |
| 1291 | xfs_trans_brelse(args->trans, bp); |
| 1292 | |
Allison Collins | 0feaef1 | 2020-07-20 21:47:27 -0700 | [diff] [blame] | 1293 | return error; |
Allison Collins | 3f6e011 | 2020-07-20 21:47:27 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /* |
Allison Collins | f44df68 | 2020-07-20 21:47:28 -0700 | [diff] [blame] | 1297 | * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers |
| 1298 | * for later deletion of the entry. |
| 1299 | */ |
| 1300 | STATIC int |
| 1301 | xfs_attr_leaf_mark_incomplete( |
| 1302 | struct xfs_da_args *args, |
| 1303 | struct xfs_da_state *state) |
| 1304 | { |
| 1305 | int error; |
| 1306 | |
| 1307 | /* |
| 1308 | * Fill in disk block numbers in the state structure |
| 1309 | * so that we can get the buffers back after we commit |
| 1310 | * several transactions in the following calls. |
| 1311 | */ |
| 1312 | error = xfs_attr_fillstate(state); |
| 1313 | if (error) |
| 1314 | return error; |
| 1315 | |
| 1316 | /* |
| 1317 | * Mark the attribute as INCOMPLETE |
| 1318 | */ |
| 1319 | return xfs_attr3_leaf_setflag(args); |
| 1320 | } |
| 1321 | |
| 1322 | /* |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1323 | * Initial setup for xfs_attr_node_removename. Make sure the attr is there and |
| 1324 | * the blocks are valid. Attr keys with remote blocks will be marked |
| 1325 | * incomplete. |
| 1326 | */ |
| 1327 | STATIC |
| 1328 | int xfs_attr_node_removename_setup( |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1329 | struct xfs_delattr_context *dac) |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1330 | { |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1331 | struct xfs_da_args *args = dac->da_args; |
| 1332 | struct xfs_da_state **state = &dac->da_state; |
| 1333 | int error; |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1334 | |
| 1335 | error = xfs_attr_node_hasname(args, state); |
| 1336 | if (error != -EEXIST) |
Yang Xu | a1de97f | 2021-11-24 10:06:02 -0800 | [diff] [blame] | 1337 | goto out; |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1338 | error = 0; |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1339 | |
| 1340 | ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL); |
| 1341 | ASSERT((*state)->path.blk[(*state)->path.active - 1].magic == |
| 1342 | XFS_ATTR_LEAF_MAGIC); |
| 1343 | |
| 1344 | if (args->rmtblkno > 0) { |
| 1345 | error = xfs_attr_leaf_mark_incomplete(args, *state); |
| 1346 | if (error) |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1347 | goto out; |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1348 | |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1349 | error = xfs_attr_rmtval_invalidate(args); |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1350 | } |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1351 | out: |
| 1352 | if (error) |
| 1353 | xfs_da_state_free(*state); |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1354 | |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1355 | return error; |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Allison Henderson | a8490f6 | 2021-03-18 17:25:59 -0700 | [diff] [blame] | 1358 | STATIC int |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 1359 | xfs_attr_node_removename( |
Allison Henderson | a8490f6 | 2021-03-18 17:25:59 -0700 | [diff] [blame] | 1360 | struct xfs_da_args *args, |
| 1361 | struct xfs_da_state *state) |
| 1362 | { |
| 1363 | struct xfs_da_state_blk *blk; |
| 1364 | int retval; |
| 1365 | |
| 1366 | /* |
| 1367 | * Remove the name and update the hashvals in the tree. |
| 1368 | */ |
| 1369 | blk = &state->path.blk[state->path.active-1]; |
| 1370 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1371 | retval = xfs_attr3_leaf_remove(blk->bp, args); |
| 1372 | xfs_da3_fixhashpath(state, &state->path); |
| 1373 | |
| 1374 | return retval; |
| 1375 | } |
| 1376 | |
Allison Collins | 674eb54 | 2020-07-20 21:47:30 -0700 | [diff] [blame] | 1377 | /* |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1378 | * Remove the attribute specified in @args. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | * |
| 1380 | * This will involve walking down the Btree, and may involve joining |
| 1381 | * leaf nodes and even joining intermediate nodes up to and including |
| 1382 | * the root node (a special case of an intermediate node). |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1383 | * |
| 1384 | * This routine is meant to function as either an in-line or delayed operation, |
| 1385 | * and may return -EAGAIN when the transaction needs to be rolled. Calling |
| 1386 | * functions will need to handle this, and call the function until a |
| 1387 | * successful error code is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | */ |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1389 | int |
| 1390 | xfs_attr_remove_iter( |
| 1391 | struct xfs_delattr_context *dac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | { |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1393 | struct xfs_da_args *args = dac->da_args; |
| 1394 | struct xfs_da_state *state = dac->da_state; |
Allison Henderson | d3a3340 | 2021-06-25 11:19:58 -0700 | [diff] [blame] | 1395 | int retval, error = 0; |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1396 | struct xfs_inode *dp = args->dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1398 | trace_xfs_attr_node_removename(args); |
| 1399 | |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1400 | switch (dac->dela_state) { |
| 1401 | case XFS_DAS_UNINIT: |
| 1402 | if (!xfs_inode_hasattr(dp)) |
| 1403 | return -ENOATTR; |
| 1404 | |
| 1405 | /* |
| 1406 | * Shortform or leaf formats don't require transaction rolls and |
| 1407 | * thus state transitions. Call the right helper and return. |
| 1408 | */ |
| 1409 | if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 1410 | return xfs_attr_sf_removename(args); |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1411 | |
| 1412 | if (xfs_attr_is_leaf(dp)) |
| 1413 | return xfs_attr_leaf_removename(args); |
| 1414 | |
| 1415 | /* |
| 1416 | * Node format may require transaction rolls. Set up the |
| 1417 | * state context and fall into the state machine. |
| 1418 | */ |
| 1419 | if (!dac->da_state) { |
| 1420 | error = xfs_attr_node_removename_setup(dac); |
| 1421 | if (error) |
| 1422 | return error; |
| 1423 | state = dac->da_state; |
| 1424 | } |
| 1425 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 1426 | fallthrough; |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1427 | case XFS_DAS_RMTBLK: |
| 1428 | dac->dela_state = XFS_DAS_RMTBLK; |
| 1429 | |
| 1430 | /* |
| 1431 | * If there is an out-of-line value, de-allocate the blocks. |
| 1432 | * This is done before we remove the attribute so that we don't |
| 1433 | * overflow the maximum size of a transaction and/or hit a |
| 1434 | * deadlock. |
| 1435 | */ |
| 1436 | if (args->rmtblkno > 0) { |
| 1437 | /* |
| 1438 | * May return -EAGAIN. Roll and repeat until all remote |
| 1439 | * blocks are removed. |
| 1440 | */ |
Allison Henderson | 5e68b4c | 2021-08-08 08:27:14 -0700 | [diff] [blame] | 1441 | error = xfs_attr_rmtval_remove(dac); |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 1442 | if (error == -EAGAIN) { |
| 1443 | trace_xfs_attr_remove_iter_return( |
| 1444 | dac->dela_state, args->dp); |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1445 | return error; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 1446 | } else if (error) { |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1447 | goto out; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 1448 | } |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1449 | |
| 1450 | /* |
| 1451 | * Refill the state structure with buffers (the prior |
| 1452 | * calls released our buffers) and close out this |
| 1453 | * transaction before proceeding. |
| 1454 | */ |
| 1455 | ASSERT(args->rmtblkno == 0); |
| 1456 | error = xfs_attr_refillstate(state); |
| 1457 | if (error) |
| 1458 | goto out; |
| 1459 | dac->dela_state = XFS_DAS_RM_NAME; |
| 1460 | dac->flags |= XFS_DAC_DEFER_FINISH; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 1461 | trace_xfs_attr_remove_iter_return(dac->dela_state, args->dp); |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1462 | return -EAGAIN; |
| 1463 | } |
| 1464 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 1465 | fallthrough; |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1466 | case XFS_DAS_RM_NAME: |
| 1467 | /* |
| 1468 | * If we came here fresh from a transaction roll, reattach all |
| 1469 | * the buffers to the current transaction. |
| 1470 | */ |
| 1471 | if (dac->dela_state == XFS_DAS_RM_NAME) { |
| 1472 | error = xfs_attr_refillstate(state); |
| 1473 | if (error) |
| 1474 | goto out; |
| 1475 | } |
| 1476 | |
Allison Henderson | 816c8e3 | 2021-05-28 15:15:05 -0700 | [diff] [blame] | 1477 | retval = xfs_attr_node_removename(args, state); |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * Check to see if the tree needs to be collapsed. If so, roll |
| 1481 | * the transacton and fall into the shrink state. |
| 1482 | */ |
| 1483 | if (retval && (state->path.active > 1)) { |
| 1484 | error = xfs_da3_join(state); |
| 1485 | if (error) |
| 1486 | goto out; |
| 1487 | |
| 1488 | dac->flags |= XFS_DAC_DEFER_FINISH; |
| 1489 | dac->dela_state = XFS_DAS_RM_SHRINK; |
Allison Henderson | df08263 | 2021-08-08 08:27:13 -0700 | [diff] [blame] | 1490 | trace_xfs_attr_remove_iter_return( |
| 1491 | dac->dela_state, args->dp); |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1492 | return -EAGAIN; |
| 1493 | } |
| 1494 | |
Gustavo A. R. Silva | 5937e00 | 2021-06-15 09:09:14 -0500 | [diff] [blame] | 1495 | fallthrough; |
Allison Henderson | 2b74b03 | 2021-04-26 15:00:33 -0700 | [diff] [blame] | 1496 | case XFS_DAS_RM_SHRINK: |
| 1497 | /* |
| 1498 | * If the result is small enough, push it all into the inode. |
| 1499 | * This is our final state so it's safe to return a dirty |
| 1500 | * transaction. |
| 1501 | */ |
| 1502 | if (xfs_attr_is_leaf(dp)) |
| 1503 | error = xfs_attr_node_shrink(args, state); |
| 1504 | ASSERT(error != -EAGAIN); |
| 1505 | break; |
| 1506 | default: |
| 1507 | ASSERT(0); |
| 1508 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | out: |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1512 | if (state) |
| 1513 | xfs_da_state_free(state); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1514 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Fill in the disk block numbers in the state structure for the buffers |
| 1519 | * that are attached to the state structure. |
| 1520 | * This is done so that we can quickly reattach ourselves to those buffers |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1521 | * after some set of transaction commits have released these buffers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | */ |
| 1523 | STATIC int |
| 1524 | xfs_attr_fillstate(xfs_da_state_t *state) |
| 1525 | { |
| 1526 | xfs_da_state_path_t *path; |
| 1527 | xfs_da_state_blk_t *blk; |
| 1528 | int level; |
| 1529 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1530 | trace_xfs_attr_fillstate(state->args); |
| 1531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | /* |
| 1533 | * Roll down the "path" in the state structure, storing the on-disk |
| 1534 | * block number for those buffers in the "path". |
| 1535 | */ |
| 1536 | path = &state->path; |
| 1537 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1538 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1539 | if (blk->bp) { |
Dave Chinner | 04fcad8 | 2021-08-18 18:46:57 -0700 | [diff] [blame] | 1540 | blk->disk_blkno = xfs_buf_daddr(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | blk->bp = NULL; |
| 1542 | } else { |
| 1543 | blk->disk_blkno = 0; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1549 | * block number for those buffers in the "altpath". |
| 1550 | */ |
| 1551 | path = &state->altpath; |
| 1552 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1553 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1554 | if (blk->bp) { |
Dave Chinner | 04fcad8 | 2021-08-18 18:46:57 -0700 | [diff] [blame] | 1555 | blk->disk_blkno = xfs_buf_daddr(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | blk->bp = NULL; |
| 1557 | } else { |
| 1558 | blk->disk_blkno = 0; |
| 1559 | } |
| 1560 | } |
| 1561 | |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1562 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * Reattach the buffers to the state structure based on the disk block |
| 1567 | * numbers stored in the state structure. |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1568 | * This is done after some set of transaction commits have released those |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | * buffers from our grip. |
| 1570 | */ |
| 1571 | STATIC int |
| 1572 | xfs_attr_refillstate(xfs_da_state_t *state) |
| 1573 | { |
| 1574 | xfs_da_state_path_t *path; |
| 1575 | xfs_da_state_blk_t *blk; |
| 1576 | int level, error; |
| 1577 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1578 | trace_xfs_attr_refillstate(state->args); |
| 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | /* |
| 1581 | * Roll down the "path" in the state structure, storing the on-disk |
| 1582 | * block number for those buffers in the "path". |
| 1583 | */ |
| 1584 | path = &state->path; |
| 1585 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1586 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1587 | if (blk->disk_blkno) { |
Christoph Hellwig | 02c57f0 | 2019-11-20 09:46:04 -0800 | [diff] [blame] | 1588 | error = xfs_da3_node_read_mapped(state->args->trans, |
| 1589 | state->args->dp, blk->disk_blkno, |
| 1590 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1592 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } else { |
| 1594 | blk->bp = NULL; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1600 | * block number for those buffers in the "altpath". |
| 1601 | */ |
| 1602 | path = &state->altpath; |
| 1603 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1604 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1605 | if (blk->disk_blkno) { |
Christoph Hellwig | 02c57f0 | 2019-11-20 09:46:04 -0800 | [diff] [blame] | 1606 | error = xfs_da3_node_read_mapped(state->args->trans, |
| 1607 | state->args->dp, blk->disk_blkno, |
| 1608 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1610 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | } else { |
| 1612 | blk->bp = NULL; |
| 1613 | } |
| 1614 | } |
| 1615 | |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1616 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | /* |
Dave Chinner | 728bcaa | 2019-08-29 09:04:08 -0700 | [diff] [blame] | 1620 | * Retrieve the attribute data from a node attribute list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | * |
| 1622 | * This routine gets called for any attribute fork that has more than one |
| 1623 | * block, ie: both true Btree attr lists and for single-leaf-blocks with |
| 1624 | * "remote" values taking up more blocks. |
Dave Chinner | 728bcaa | 2019-08-29 09:04:08 -0700 | [diff] [blame] | 1625 | * |
| 1626 | * Returns 0 on successful retrieval, otherwise an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1628 | STATIC int |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1629 | xfs_attr_node_get( |
| 1630 | struct xfs_da_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | { |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1632 | struct xfs_da_state *state; |
| 1633 | struct xfs_da_state_blk *blk; |
| 1634 | int i; |
| 1635 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1637 | trace_xfs_attr_node_get(args); |
| 1638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | /* |
| 1640 | * Search to see if name exists, and get back a pointer to it. |
| 1641 | */ |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1642 | error = xfs_attr_node_hasname(args, &state); |
| 1643 | if (error != -EEXIST) |
Dave Chinner | 728bcaa | 2019-08-29 09:04:08 -0700 | [diff] [blame] | 1644 | goto out_release; |
| 1645 | |
| 1646 | /* |
| 1647 | * Get the value, local or "remote" |
| 1648 | */ |
| 1649 | blk = &state->path.blk[state->path.active - 1]; |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1650 | error = xfs_attr3_leaf_getvalue(blk->bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
| 1652 | /* |
| 1653 | * If not in a transaction, we have to release all the buffers. |
| 1654 | */ |
Dave Chinner | 728bcaa | 2019-08-29 09:04:08 -0700 | [diff] [blame] | 1655 | out_release: |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1656 | for (i = 0; state != NULL && i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1657 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | state->path.blk[i].bp = NULL; |
| 1659 | } |
| 1660 | |
Allison Collins | 07120f1 | 2020-07-20 21:47:22 -0700 | [diff] [blame] | 1661 | if (state) |
| 1662 | xfs_da_state_free(state); |
| 1663 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | } |
Darrick J. Wong | 6548053 | 2019-02-01 09:08:54 -0800 | [diff] [blame] | 1665 | |
| 1666 | /* Returns true if the attribute entry name is valid. */ |
| 1667 | bool |
| 1668 | xfs_attr_namecheck( |
| 1669 | const void *name, |
| 1670 | size_t length) |
| 1671 | { |
| 1672 | /* |
| 1673 | * MAXNAMELEN includes the trailing null, but (name/length) leave it |
| 1674 | * out, so use >= for the length check. |
| 1675 | */ |
| 1676 | if (length >= MAXNAMELEN) |
| 1677 | return false; |
| 1678 | |
| 1679 | /* There shouldn't be any nulls here */ |
| 1680 | return !memchr(name, 0, length); |
| 1681 | } |