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