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