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