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