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