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