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