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