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