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