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