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