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