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 |
| 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 | */ |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 419 | STATIC int |
| 420 | xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags) |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 421 | { |
| 422 | xfs_da_args_t args; |
| 423 | xfs_fsblock_t firstblock; |
| 424 | xfs_bmap_free_t flist; |
| 425 | int error; |
| 426 | xfs_mount_t *mp = dp->i_mount; |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | /* |
| 429 | * Fill in the arg structure for this request. |
| 430 | */ |
| 431 | memset((char *)&args, 0, sizeof(args)); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 432 | args.name = name->name; |
| 433 | args.namelen = name->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | args.flags = flags; |
| 435 | args.hashval = xfs_da_hashname(args.name, args.namelen); |
| 436 | args.dp = dp; |
| 437 | args.firstblock = &firstblock; |
| 438 | args.flist = &flist; |
| 439 | args.total = 0; |
| 440 | args.whichfork = XFS_ATTR_FORK; |
| 441 | |
| 442 | /* |
Dave Chinner | 4a33821 | 2011-06-23 01:35:01 +0000 | [diff] [blame] | 443 | * we have no control over the attribute names that userspace passes us |
| 444 | * to remove, so we have to allow the name lookup prior to attribute |
| 445 | * removal to fail. |
| 446 | */ |
| 447 | args.op_flags = XFS_DA_OP_OKNOENT; |
| 448 | |
| 449 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | * Attach the dquots to the inode. |
| 451 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 452 | error = xfs_qm_dqattach(dp, 0); |
| 453 | if (error) |
| 454 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * Start our first transaction of the day. |
| 458 | * |
| 459 | * All future transactions during this code must be "chained" off |
| 460 | * this one via the trans_dup() call. All transactions will contain |
| 461 | * the inode, and the inode will always be marked with trans_ihold(). |
| 462 | * Since the inode will be locked in all transactions, we must log |
| 463 | * the inode in every transaction to let it float upward through |
| 464 | * the log. |
| 465 | */ |
| 466 | args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM); |
| 467 | |
| 468 | /* |
| 469 | * Root fork attributes can use reserved data blocks for this |
| 470 | * operation if necessary |
| 471 | */ |
| 472 | |
| 473 | if (flags & ATTR_ROOT) |
| 474 | args.trans->t_flags |= XFS_TRANS_RESERVE; |
| 475 | |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 476 | error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm, |
| 477 | XFS_ATTRRM_SPACE_RES(mp), 0); |
| 478 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | xfs_trans_cancel(args.trans, 0); |
| 480 | return(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | xfs_ilock(dp, XFS_ILOCK_EXCL); |
| 484 | /* |
| 485 | * No need to make quota reservations here. We expect to release some |
| 486 | * blocks not allocate in the common case. |
| 487 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 488 | xfs_trans_ijoin(args.trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Decide on what work routines to call based on the inode size. |
| 492 | */ |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 493 | if (!xfs_inode_hasattr(dp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | error = XFS_ERROR(ENOATTR); |
| 495 | goto out; |
| 496 | } |
| 497 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { |
| 498 | ASSERT(dp->i_afp->if_flags & XFS_IFINLINE); |
| 499 | error = xfs_attr_shortform_remove(&args); |
| 500 | if (error) { |
| 501 | goto out; |
| 502 | } |
| 503 | } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 504 | error = xfs_attr_leaf_removename(&args); |
| 505 | } else { |
| 506 | error = xfs_attr_node_removename(&args); |
| 507 | } |
| 508 | if (error) { |
| 509 | goto out; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * If this is a synchronous mount, make sure that the |
| 514 | * transaction goes to disk before returning to the user. |
| 515 | */ |
| 516 | if (mp->m_flags & XFS_MOUNT_WSYNC) { |
| 517 | xfs_trans_set_sync(args.trans); |
| 518 | } |
| 519 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 520 | if ((flags & ATTR_KERNOTIME) == 0) |
| 521 | xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG); |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | /* |
| 524 | * Commit the last in the sequence of transactions. |
| 525 | */ |
| 526 | xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 527 | error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return(error); |
| 531 | |
| 532 | out: |
| 533 | if (args.trans) |
| 534 | xfs_trans_cancel(args.trans, |
| 535 | XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 536 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 537 | return(error); |
| 538 | } |
| 539 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 540 | int |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 541 | xfs_attr_remove( |
| 542 | xfs_inode_t *dp, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 543 | const unsigned char *name, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 544 | int flags) |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 545 | { |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 546 | int error; |
| 547 | struct xfs_name xname; |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 548 | |
| 549 | XFS_STATS_INC(xs_attr_remove); |
| 550 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 551 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
| 552 | return (EIO); |
| 553 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 554 | error = xfs_attr_name_to_xname(&xname, name); |
| 555 | if (error) |
| 556 | return error; |
| 557 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 558 | xfs_ilock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 559 | if (!xfs_inode_hasattr(dp)) { |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 560 | xfs_iunlock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 561 | return XFS_ERROR(ENOATTR); |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 562 | } |
| 563 | xfs_iunlock(dp, XFS_ILOCK_SHARED); |
| 564 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 565 | return xfs_attr_remove_int(dp, &xname, flags); |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 566 | } |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | /*======================================================================== |
| 570 | * External routines when attribute list is inside the inode |
| 571 | *========================================================================*/ |
| 572 | |
| 573 | /* |
| 574 | * Add a name to the shortform attribute list structure |
| 575 | * This is the external routine. |
| 576 | */ |
| 577 | STATIC int |
| 578 | xfs_attr_shortform_addname(xfs_da_args_t *args) |
| 579 | { |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 580 | int newsize, forkoff, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 582 | trace_xfs_attr_sf_addname(args); |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | retval = xfs_attr_shortform_lookup(args); |
| 585 | if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) { |
| 586 | return(retval); |
| 587 | } else if (retval == EEXIST) { |
| 588 | if (args->flags & ATTR_CREATE) |
| 589 | return(retval); |
| 590 | retval = xfs_attr_shortform_remove(args); |
| 591 | ASSERT(retval == 0); |
| 592 | } |
| 593 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 594 | if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX || |
| 595 | args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
| 596 | return(XFS_ERROR(ENOSPC)); |
| 597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | newsize = XFS_ATTR_SF_TOTSIZE(args->dp); |
| 599 | newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 600 | |
| 601 | forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize); |
| 602 | if (!forkoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | return(XFS_ERROR(ENOSPC)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 604 | |
| 605 | xfs_attr_shortform_add(args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | return(0); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /*======================================================================== |
| 611 | * External routines when attribute list is one block |
| 612 | *========================================================================*/ |
| 613 | |
| 614 | /* |
| 615 | * Add a name to the leaf attribute list structure |
| 616 | * |
| 617 | * This leaf block cannot have a "remote" value, we only call this routine |
| 618 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 619 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 620 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | xfs_attr_leaf_addname(xfs_da_args_t *args) |
| 622 | { |
| 623 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 624 | struct xfs_buf *bp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 625 | int retval, error, committed, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 627 | trace_xfs_attr_leaf_addname(args); |
| 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | /* |
| 630 | * Read the (only) block in the attribute list in. |
| 631 | */ |
| 632 | dp = args->dp; |
| 633 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 634 | 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] | 635 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 636 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * Look up the given attribute in the leaf block. Figure out if |
| 640 | * the given flags produce an error or call for an atomic rename. |
| 641 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 642 | retval = xfs_attr3_leaf_lookup_int(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 644 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 645 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } else if (retval == EEXIST) { |
| 647 | if (args->flags & ATTR_CREATE) { /* pure create op */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 648 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 649 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 651 | |
| 652 | trace_xfs_attr_leaf_replace(args); |
| 653 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 654 | args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
| 656 | args->index2 = args->index; |
| 657 | args->rmtblkno2 = args->rmtblkno; |
| 658 | args->rmtblkcnt2 = args->rmtblkcnt; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Add the attribute to the leaf block, transitioning to a Btree |
| 663 | * if required. |
| 664 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 665 | retval = xfs_attr3_leaf_add(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | if (retval == ENOSPC) { |
| 667 | /* |
| 668 | * Promote the attribute list to the Btree format, then |
| 669 | * Commit that transaction so that the node_addname() call |
| 670 | * can manage its own transactions. |
| 671 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 672 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 673 | error = xfs_attr3_leaf_to_node(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (!error) { |
| 675 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 676 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | if (error) { |
| 679 | ASSERT(committed); |
| 680 | args->trans = NULL; |
| 681 | xfs_bmap_cancel(args->flist); |
| 682 | return(error); |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * bmap_finish() may have committed the last trans and started |
| 687 | * a new one. We need the inode to be in all transactions. |
| 688 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 689 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 690 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * Commit the current trans (including the inode) and start |
| 694 | * a new one. |
| 695 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 696 | error = xfs_trans_roll(&args->trans, dp); |
| 697 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | return (error); |
| 699 | |
| 700 | /* |
| 701 | * Fob the whole rest of the problem off on the Btree code. |
| 702 | */ |
| 703 | error = xfs_attr_node_addname(args); |
| 704 | return(error); |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Commit the transaction that added the attr name so that |
| 709 | * later routines can manage their own transactions. |
| 710 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 711 | error = xfs_trans_roll(&args->trans, dp); |
| 712 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | return (error); |
| 714 | |
| 715 | /* |
| 716 | * If there was an out-of-line value, allocate the blocks we |
| 717 | * identified for its storage and copy the value. This is done |
| 718 | * after we create the attribute so that we don't overflow the |
| 719 | * maximum size of a transaction and/or hit a deadlock. |
| 720 | */ |
| 721 | if (args->rmtblkno > 0) { |
| 722 | error = xfs_attr_rmtval_set(args); |
| 723 | if (error) |
| 724 | return(error); |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * If this is an atomic rename operation, we must "flip" the |
| 729 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 730 | * so that one disappears and one appears atomically. Then we |
| 731 | * must remove the "old" attribute/value pair. |
| 732 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 733 | if (args->op_flags & XFS_DA_OP_RENAME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | /* |
| 735 | * In a separate transaction, set the incomplete flag on the |
| 736 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 737 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 738 | error = xfs_attr3_leaf_flipflags(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | if (error) |
| 740 | return(error); |
| 741 | |
| 742 | /* |
| 743 | * Dismantle the "old" attribute/value pair by removing |
| 744 | * a "remote" value (if it exists). |
| 745 | */ |
| 746 | args->index = args->index2; |
| 747 | args->blkno = args->blkno2; |
| 748 | args->rmtblkno = args->rmtblkno2; |
| 749 | args->rmtblkcnt = args->rmtblkcnt2; |
| 750 | if (args->rmtblkno) { |
| 751 | error = xfs_attr_rmtval_remove(args); |
| 752 | if (error) |
| 753 | return(error); |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * Read in the block containing the "old" attr, then |
| 758 | * remove the "old" attr from that block (neat, huh!) |
| 759 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 760 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 761 | -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 763 | return error; |
| 764 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 765 | xfs_attr3_leaf_remove(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | /* |
| 768 | * If the result is small enough, shrink it all into the inode. |
| 769 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 770 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 771 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 772 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | /* bp is gone due to xfs_da_shrink_inode */ |
| 774 | if (!error) { |
| 775 | error = xfs_bmap_finish(&args->trans, |
| 776 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | &committed); |
| 778 | } |
| 779 | if (error) { |
| 780 | ASSERT(committed); |
| 781 | args->trans = NULL; |
| 782 | xfs_bmap_cancel(args->flist); |
| 783 | return(error); |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | * bmap_finish() may have committed the last trans |
| 788 | * and started a new one. We need the inode to be |
| 789 | * in all transactions. |
| 790 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 791 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 792 | xfs_trans_ijoin(args->trans, dp, 0); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 793 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | /* |
| 796 | * Commit the remove and start the next trans in series. |
| 797 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 798 | error = xfs_trans_roll(&args->trans, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
| 800 | } else if (args->rmtblkno > 0) { |
| 801 | /* |
| 802 | * Added a "remote" value, just clear the incomplete flag. |
| 803 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 804 | error = xfs_attr3_leaf_clearflag(args); |
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 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* |
| 810 | * Remove a name from the leaf attribute list structure |
| 811 | * |
| 812 | * This leaf block cannot have a "remote" value, we only call this routine |
| 813 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 814 | */ |
| 815 | STATIC int |
| 816 | xfs_attr_leaf_removename(xfs_da_args_t *args) |
| 817 | { |
| 818 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 819 | struct xfs_buf *bp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 820 | int error, committed, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 822 | trace_xfs_attr_leaf_removename(args); |
| 823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | /* |
| 825 | * Remove the attribute. |
| 826 | */ |
| 827 | dp = args->dp; |
| 828 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 829 | 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] | 830 | if (error) |
| 831 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 833 | error = xfs_attr3_leaf_lookup_int(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | if (error == ENOATTR) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 835 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 836 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 839 | xfs_attr3_leaf_remove(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | /* |
| 842 | * If the result is small enough, shrink it all into the inode. |
| 843 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 844 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 845 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 846 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | /* bp is gone due to xfs_da_shrink_inode */ |
| 848 | if (!error) { |
| 849 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 850 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | if (error) { |
| 853 | ASSERT(committed); |
| 854 | args->trans = NULL; |
| 855 | xfs_bmap_cancel(args->flist); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 856 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* |
| 860 | * bmap_finish() may have committed the last trans and started |
| 861 | * a new one. We need the inode to be in all transactions. |
| 862 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 863 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 864 | xfs_trans_ijoin(args->trans, dp, 0); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 865 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 866 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Look up a name in a leaf attribute list structure. |
| 871 | * |
| 872 | * This leaf block cannot have a "remote" value, we only call this routine |
| 873 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 874 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 875 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | xfs_attr_leaf_get(xfs_da_args_t *args) |
| 877 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 878 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | int error; |
| 880 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 881 | trace_xfs_attr_leaf_get(args); |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 884 | 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] | 885 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 886 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 888 | error = xfs_attr3_leaf_lookup_int(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | if (error != EEXIST) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 890 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 891 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 893 | error = xfs_attr3_leaf_getvalue(bp, args); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 894 | xfs_trans_brelse(args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) { |
| 896 | error = xfs_attr_rmtval_get(args); |
| 897 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 898 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | /*======================================================================== |
| 902 | * External routines when attribute list size > XFS_LBSIZE(mp). |
| 903 | *========================================================================*/ |
| 904 | |
| 905 | /* |
| 906 | * Add a name to a Btree-format attribute list. |
| 907 | * |
| 908 | * This will involve walking down the Btree, and may involve splitting |
| 909 | * leaf nodes and even splitting intermediate nodes up to and including |
| 910 | * the root node (a special case of an intermediate node). |
| 911 | * |
| 912 | * "Remote" attribute values confuse the issue and atomic rename operations |
| 913 | * add a whole extra layer of confusion on top of that. |
| 914 | */ |
| 915 | STATIC int |
| 916 | xfs_attr_node_addname(xfs_da_args_t *args) |
| 917 | { |
| 918 | xfs_da_state_t *state; |
| 919 | xfs_da_state_blk_t *blk; |
| 920 | xfs_inode_t *dp; |
| 921 | xfs_mount_t *mp; |
| 922 | int committed, retval, error; |
| 923 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 924 | trace_xfs_attr_node_addname(args); |
| 925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | /* |
| 927 | * Fill in bucket of arguments/results/context to carry around. |
| 928 | */ |
| 929 | dp = args->dp; |
| 930 | mp = dp->i_mount; |
| 931 | restart: |
| 932 | state = xfs_da_state_alloc(); |
| 933 | state->args = args; |
| 934 | state->mp = mp; |
| 935 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 936 | state->node_ents = state->mp->m_attr_node_ents; |
| 937 | |
| 938 | /* |
| 939 | * Search to see if name already exists, and get back a pointer |
| 940 | * to where it should go. |
| 941 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 942 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | if (error) |
| 944 | goto out; |
| 945 | blk = &state->path.blk[ state->path.active-1 ]; |
| 946 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 947 | if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) { |
| 948 | goto out; |
| 949 | } else if (retval == EEXIST) { |
| 950 | if (args->flags & ATTR_CREATE) |
| 951 | goto out; |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 952 | |
| 953 | trace_xfs_attr_node_replace(args); |
| 954 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 955 | args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
| 957 | args->index2 = args->index; |
| 958 | args->rmtblkno2 = args->rmtblkno; |
| 959 | args->rmtblkcnt2 = args->rmtblkcnt; |
| 960 | args->rmtblkno = 0; |
| 961 | args->rmtblkcnt = 0; |
| 962 | } |
| 963 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 964 | retval = xfs_attr3_leaf_add(blk->bp, state->args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | if (retval == ENOSPC) { |
| 966 | if (state->path.active == 1) { |
| 967 | /* |
| 968 | * Its really a single leaf node, but it had |
| 969 | * out-of-line values so it looked like it *might* |
| 970 | * have been a b-tree. |
| 971 | */ |
| 972 | xfs_da_state_free(state); |
Eric Sandeen | 6dd93e9 | 2013-07-31 20:18:54 -0500 | [diff] [blame] | 973 | state = NULL; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 974 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 975 | error = xfs_attr3_leaf_to_node(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | if (!error) { |
| 977 | error = xfs_bmap_finish(&args->trans, |
| 978 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | &committed); |
| 980 | } |
| 981 | if (error) { |
| 982 | ASSERT(committed); |
| 983 | args->trans = NULL; |
| 984 | xfs_bmap_cancel(args->flist); |
| 985 | goto out; |
| 986 | } |
| 987 | |
| 988 | /* |
| 989 | * bmap_finish() may have committed the last trans |
| 990 | * and started a new one. We need the inode to be |
| 991 | * in all transactions. |
| 992 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 993 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 994 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | /* |
| 997 | * Commit the node conversion and start the next |
| 998 | * trans in the chain. |
| 999 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1000 | error = xfs_trans_roll(&args->trans, dp); |
| 1001 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | goto out; |
| 1003 | |
| 1004 | goto restart; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * Split as many Btree elements as required. |
| 1009 | * This code tracks the new and old attr's location |
| 1010 | * in the index/blkno/rmtblkno/rmtblkcnt fields and |
| 1011 | * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields. |
| 1012 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1013 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1014 | error = xfs_da3_split(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (!error) { |
| 1016 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1017 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | if (error) { |
| 1020 | ASSERT(committed); |
| 1021 | args->trans = NULL; |
| 1022 | xfs_bmap_cancel(args->flist); |
| 1023 | goto out; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * bmap_finish() may have committed the last trans and started |
| 1028 | * a new one. We need the inode to be in all transactions. |
| 1029 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1030 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1031 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } else { |
| 1033 | /* |
| 1034 | * Addition succeeded, update Btree hashvals. |
| 1035 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1036 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Kill the state structure, we're done with it and need to |
| 1041 | * allow the buffers to come back later. |
| 1042 | */ |
| 1043 | xfs_da_state_free(state); |
| 1044 | state = NULL; |
| 1045 | |
| 1046 | /* |
| 1047 | * Commit the leaf addition or btree split and start the next |
| 1048 | * trans in the chain. |
| 1049 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1050 | error = xfs_trans_roll(&args->trans, dp); |
| 1051 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | goto out; |
| 1053 | |
| 1054 | /* |
| 1055 | * If there was an out-of-line value, allocate the blocks we |
| 1056 | * identified for its storage and copy the value. This is done |
| 1057 | * after we create the attribute so that we don't overflow the |
| 1058 | * maximum size of a transaction and/or hit a deadlock. |
| 1059 | */ |
| 1060 | if (args->rmtblkno > 0) { |
| 1061 | error = xfs_attr_rmtval_set(args); |
| 1062 | if (error) |
| 1063 | return(error); |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * If this is an atomic rename operation, we must "flip" the |
| 1068 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 1069 | * so that one disappears and one appears atomically. Then we |
| 1070 | * must remove the "old" attribute/value pair. |
| 1071 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1072 | if (args->op_flags & XFS_DA_OP_RENAME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | /* |
| 1074 | * In a separate transaction, set the incomplete flag on the |
| 1075 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 1076 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1077 | error = xfs_attr3_leaf_flipflags(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | if (error) |
| 1079 | goto out; |
| 1080 | |
| 1081 | /* |
| 1082 | * Dismantle the "old" attribute/value pair by removing |
| 1083 | * a "remote" value (if it exists). |
| 1084 | */ |
| 1085 | args->index = args->index2; |
| 1086 | args->blkno = args->blkno2; |
| 1087 | args->rmtblkno = args->rmtblkno2; |
| 1088 | args->rmtblkcnt = args->rmtblkcnt2; |
| 1089 | if (args->rmtblkno) { |
| 1090 | error = xfs_attr_rmtval_remove(args); |
| 1091 | if (error) |
| 1092 | return(error); |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Re-find the "old" attribute entry after any split ops. |
| 1097 | * The INCOMPLETE flag means that we will find the "old" |
| 1098 | * attr, not the "new" one. |
| 1099 | */ |
| 1100 | args->flags |= XFS_ATTR_INCOMPLETE; |
| 1101 | state = xfs_da_state_alloc(); |
| 1102 | state->args = args; |
| 1103 | state->mp = mp; |
| 1104 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 1105 | state->node_ents = state->mp->m_attr_node_ents; |
| 1106 | state->inleaf = 0; |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1107 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | if (error) |
| 1109 | goto out; |
| 1110 | |
| 1111 | /* |
| 1112 | * Remove the name and update the hashvals in the tree. |
| 1113 | */ |
| 1114 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1115 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1116 | error = xfs_attr3_leaf_remove(blk->bp, args); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1117 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
| 1119 | /* |
| 1120 | * Check to see if the tree needs to be collapsed. |
| 1121 | */ |
| 1122 | if (retval && (state->path.active > 1)) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1123 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1124 | error = xfs_da3_join(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | if (!error) { |
| 1126 | error = xfs_bmap_finish(&args->trans, |
| 1127 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | &committed); |
| 1129 | } |
| 1130 | if (error) { |
| 1131 | ASSERT(committed); |
| 1132 | args->trans = NULL; |
| 1133 | xfs_bmap_cancel(args->flist); |
| 1134 | goto out; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * bmap_finish() may have committed the last trans |
| 1139 | * and started a new one. We need the inode to be |
| 1140 | * in all transactions. |
| 1141 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1142 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1143 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Commit and start the next trans in the chain. |
| 1148 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1149 | error = xfs_trans_roll(&args->trans, dp); |
| 1150 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | goto out; |
| 1152 | |
| 1153 | } else if (args->rmtblkno > 0) { |
| 1154 | /* |
| 1155 | * Added a "remote" value, just clear the incomplete flag. |
| 1156 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1157 | error = xfs_attr3_leaf_clearflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | if (error) |
| 1159 | goto out; |
| 1160 | } |
| 1161 | retval = error = 0; |
| 1162 | |
| 1163 | out: |
| 1164 | if (state) |
| 1165 | xfs_da_state_free(state); |
| 1166 | if (error) |
| 1167 | return(error); |
| 1168 | return(retval); |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * Remove a name from a B-tree attribute list. |
| 1173 | * |
| 1174 | * This will involve walking down the Btree, and may involve joining |
| 1175 | * leaf nodes and even joining intermediate nodes up to and including |
| 1176 | * the root node (a special case of an intermediate node). |
| 1177 | */ |
| 1178 | STATIC int |
| 1179 | xfs_attr_node_removename(xfs_da_args_t *args) |
| 1180 | { |
| 1181 | xfs_da_state_t *state; |
| 1182 | xfs_da_state_blk_t *blk; |
| 1183 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1184 | struct xfs_buf *bp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1185 | int retval, error, committed, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1187 | trace_xfs_attr_node_removename(args); |
| 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | /* |
| 1190 | * Tie a string around our finger to remind us where we are. |
| 1191 | */ |
| 1192 | dp = args->dp; |
| 1193 | state = xfs_da_state_alloc(); |
| 1194 | state->args = args; |
| 1195 | state->mp = dp->i_mount; |
| 1196 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 1197 | state->node_ents = state->mp->m_attr_node_ents; |
| 1198 | |
| 1199 | /* |
| 1200 | * Search to see if name exists, and get back a pointer to it. |
| 1201 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1202 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | if (error || (retval != EEXIST)) { |
| 1204 | if (error == 0) |
| 1205 | error = retval; |
| 1206 | goto out; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * If there is an out-of-line value, de-allocate the blocks. |
| 1211 | * This is done before we remove the attribute so that we don't |
| 1212 | * overflow the maximum size of a transaction and/or hit a deadlock. |
| 1213 | */ |
| 1214 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1215 | ASSERT(blk->bp != NULL); |
| 1216 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1217 | if (args->rmtblkno > 0) { |
| 1218 | /* |
| 1219 | * Fill in disk block numbers in the state structure |
| 1220 | * so that we can get the buffers back after we commit |
| 1221 | * several transactions in the following calls. |
| 1222 | */ |
| 1223 | error = xfs_attr_fillstate(state); |
| 1224 | if (error) |
| 1225 | goto out; |
| 1226 | |
| 1227 | /* |
| 1228 | * Mark the attribute as INCOMPLETE, then bunmapi() the |
| 1229 | * remote value. |
| 1230 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1231 | error = xfs_attr3_leaf_setflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | if (error) |
| 1233 | goto out; |
| 1234 | error = xfs_attr_rmtval_remove(args); |
| 1235 | if (error) |
| 1236 | goto out; |
| 1237 | |
| 1238 | /* |
| 1239 | * Refill the state structure with buffers, the prior calls |
| 1240 | * released our buffers. |
| 1241 | */ |
| 1242 | error = xfs_attr_refillstate(state); |
| 1243 | if (error) |
| 1244 | goto out; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * Remove the name and update the hashvals in the tree. |
| 1249 | */ |
| 1250 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1251 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1252 | retval = xfs_attr3_leaf_remove(blk->bp, args); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1253 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | |
| 1255 | /* |
| 1256 | * Check to see if the tree needs to be collapsed. |
| 1257 | */ |
| 1258 | if (retval && (state->path.active > 1)) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1259 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1260 | error = xfs_da3_join(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | if (!error) { |
| 1262 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1263 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | if (error) { |
| 1266 | ASSERT(committed); |
| 1267 | args->trans = NULL; |
| 1268 | xfs_bmap_cancel(args->flist); |
| 1269 | goto out; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * bmap_finish() may have committed the last trans and started |
| 1274 | * a new one. We need the inode to be in all transactions. |
| 1275 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1276 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1277 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
| 1279 | /* |
| 1280 | * Commit the Btree join operation and start a new trans. |
| 1281 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1282 | error = xfs_trans_roll(&args->trans, dp); |
| 1283 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | goto out; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * If the result is small enough, push it all into the inode. |
| 1289 | */ |
| 1290 | if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 1291 | /* |
| 1292 | * Have to get rid of the copy of this dabuf in the state. |
| 1293 | */ |
| 1294 | ASSERT(state->path.active == 1); |
| 1295 | ASSERT(state->path.blk[0].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | state->path.blk[0].bp = NULL; |
| 1297 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1298 | error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | if (error) |
| 1300 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1302 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1303 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1304 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | /* bp is gone due to xfs_da_shrink_inode */ |
| 1306 | if (!error) { |
| 1307 | error = xfs_bmap_finish(&args->trans, |
| 1308 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | &committed); |
| 1310 | } |
| 1311 | if (error) { |
| 1312 | ASSERT(committed); |
| 1313 | args->trans = NULL; |
| 1314 | xfs_bmap_cancel(args->flist); |
| 1315 | goto out; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * bmap_finish() may have committed the last trans |
| 1320 | * and started a new one. We need the inode to be |
| 1321 | * in all transactions. |
| 1322 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1323 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1324 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } else |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1326 | xfs_trans_brelse(args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | } |
| 1328 | error = 0; |
| 1329 | |
| 1330 | out: |
| 1331 | xfs_da_state_free(state); |
| 1332 | return(error); |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * Fill in the disk block numbers in the state structure for the buffers |
| 1337 | * that are attached to the state structure. |
| 1338 | * 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] | 1339 | * after some set of transaction commits have released these buffers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | */ |
| 1341 | STATIC int |
| 1342 | xfs_attr_fillstate(xfs_da_state_t *state) |
| 1343 | { |
| 1344 | xfs_da_state_path_t *path; |
| 1345 | xfs_da_state_blk_t *blk; |
| 1346 | int level; |
| 1347 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1348 | trace_xfs_attr_fillstate(state->args); |
| 1349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | /* |
| 1351 | * Roll down the "path" in the state structure, storing the on-disk |
| 1352 | * block number for those buffers in the "path". |
| 1353 | */ |
| 1354 | path = &state->path; |
| 1355 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1356 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1357 | if (blk->bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1358 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | blk->bp = NULL; |
| 1360 | } else { |
| 1361 | blk->disk_blkno = 0; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /* |
| 1366 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1367 | * block number for those buffers in the "altpath". |
| 1368 | */ |
| 1369 | path = &state->altpath; |
| 1370 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1371 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1372 | if (blk->bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1373 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | blk->bp = NULL; |
| 1375 | } else { |
| 1376 | blk->disk_blkno = 0; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return(0); |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Reattach the buffers to the state structure based on the disk block |
| 1385 | * numbers stored in the state structure. |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1386 | * This is done after some set of transaction commits have released those |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | * buffers from our grip. |
| 1388 | */ |
| 1389 | STATIC int |
| 1390 | xfs_attr_refillstate(xfs_da_state_t *state) |
| 1391 | { |
| 1392 | xfs_da_state_path_t *path; |
| 1393 | xfs_da_state_blk_t *blk; |
| 1394 | int level, error; |
| 1395 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1396 | trace_xfs_attr_refillstate(state->args); |
| 1397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | /* |
| 1399 | * Roll down the "path" in the state structure, storing the on-disk |
| 1400 | * block number for those buffers in the "path". |
| 1401 | */ |
| 1402 | path = &state->path; |
| 1403 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1404 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1405 | if (blk->disk_blkno) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1406 | error = xfs_da3_node_read(state->args->trans, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | state->args->dp, |
| 1408 | blk->blkno, blk->disk_blkno, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1409 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | if (error) |
| 1411 | return(error); |
| 1412 | } else { |
| 1413 | blk->bp = NULL; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | /* |
| 1418 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1419 | * block number for those buffers in the "altpath". |
| 1420 | */ |
| 1421 | path = &state->altpath; |
| 1422 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1423 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1424 | if (blk->disk_blkno) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1425 | error = xfs_da3_node_read(state->args->trans, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | state->args->dp, |
| 1427 | blk->blkno, blk->disk_blkno, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1428 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | if (error) |
| 1430 | return(error); |
| 1431 | } else { |
| 1432 | blk->bp = NULL; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | return(0); |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | * Look up a filename in a node attribute list. |
| 1441 | * |
| 1442 | * This routine gets called for any attribute fork that has more than one |
| 1443 | * block, ie: both true Btree attr lists and for single-leaf-blocks with |
| 1444 | * "remote" values taking up more blocks. |
| 1445 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1446 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | xfs_attr_node_get(xfs_da_args_t *args) |
| 1448 | { |
| 1449 | xfs_da_state_t *state; |
| 1450 | xfs_da_state_blk_t *blk; |
| 1451 | int error, retval; |
| 1452 | int i; |
| 1453 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1454 | trace_xfs_attr_node_get(args); |
| 1455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | state = xfs_da_state_alloc(); |
| 1457 | state->args = args; |
| 1458 | state->mp = args->dp->i_mount; |
| 1459 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 1460 | state->node_ents = state->mp->m_attr_node_ents; |
| 1461 | |
| 1462 | /* |
| 1463 | * Search to see if name exists, and get back a pointer to it. |
| 1464 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1465 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | if (error) { |
| 1467 | retval = error; |
| 1468 | } else if (retval == EEXIST) { |
| 1469 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1470 | ASSERT(blk->bp != NULL); |
| 1471 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1472 | |
| 1473 | /* |
| 1474 | * Get the value, local or "remote" |
| 1475 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1476 | retval = xfs_attr3_leaf_getvalue(blk->bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (!retval && (args->rmtblkno > 0) |
| 1478 | && !(args->flags & ATTR_KERNOVAL)) { |
| 1479 | retval = xfs_attr_rmtval_get(args); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* |
| 1484 | * If not in a transaction, we have to release all the buffers. |
| 1485 | */ |
| 1486 | for (i = 0; i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1487 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | state->path.blk[i].bp = NULL; |
| 1489 | } |
| 1490 | |
| 1491 | xfs_da_state_free(state); |
| 1492 | return(retval); |
| 1493 | } |