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 | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 20 | #include "xfs_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
| 23 | #include "xfs_trans.h" |
Dave Chinner | d386b32 | 2013-08-12 20:49:31 +1000 | [diff] [blame] | 24 | #include "xfs_trans_priv.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" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_bmap_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_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_alloc.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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_attr.h" |
| 37 | #include "xfs_attr_leaf.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 38 | #include "xfs_attr_remote.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_trans_space.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 42 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 43 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * xfs_attr.c |
| 47 | * |
| 48 | * Provide the external interfaces to manage attribute lists. |
| 49 | */ |
| 50 | |
| 51 | /*======================================================================== |
| 52 | * Function prototypes for the kernel. |
| 53 | *========================================================================*/ |
| 54 | |
| 55 | /* |
| 56 | * Internal routines when attribute list fits inside the inode. |
| 57 | */ |
| 58 | STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args); |
| 59 | |
| 60 | /* |
| 61 | * Internal routines when attribute list is one block. |
| 62 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 63 | STATIC int xfs_attr_leaf_get(xfs_da_args_t *args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args); |
| 65 | STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args); |
| 66 | STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context); |
| 67 | |
| 68 | /* |
| 69 | * Internal routines when attribute list is more than one block. |
| 70 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 71 | STATIC int xfs_attr_node_get(xfs_da_args_t *args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | STATIC int xfs_attr_node_addname(xfs_da_args_t *args); |
| 73 | STATIC int xfs_attr_node_removename(xfs_da_args_t *args); |
| 74 | STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context); |
| 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 | |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 94 | STATIC int |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | XFS_STATS_INC(xs_attr_get); |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 171 | return(EIO); |
| 172 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 173 | error = xfs_attr_name_to_xname(&xname, name); |
| 174 | if (error) |
| 175 | return error; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | e82fa0c | 2009-11-14 16:17:20 +0000 | [diff] [blame] | 178 | error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 180 | return(error); |
| 181 | } |
| 182 | |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 183 | /* |
| 184 | * Calculate how many blocks we need for the new attribute, |
| 185 | */ |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 186 | STATIC int |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 187 | xfs_attr_calc_size( |
| 188 | struct xfs_inode *ip, |
| 189 | int namelen, |
| 190 | int valuelen, |
| 191 | int *local) |
| 192 | { |
| 193 | struct xfs_mount *mp = ip->i_mount; |
| 194 | int size; |
| 195 | int nblks; |
| 196 | |
| 197 | /* |
| 198 | * Determine space new attribute will use, and if it would be |
| 199 | * "local" or "remote" (note: local != inline). |
| 200 | */ |
| 201 | size = xfs_attr_leaf_newentsize(namelen, valuelen, |
| 202 | mp->m_sb.sb_blocksize, local); |
| 203 | |
| 204 | nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK); |
| 205 | if (*local) { |
| 206 | if (size > (mp->m_sb.sb_blocksize >> 1)) { |
| 207 | /* Double split possible */ |
| 208 | nblks *= 2; |
| 209 | } |
| 210 | } else { |
| 211 | /* |
| 212 | * Out of line attribute, cannot double split, but |
| 213 | * make room for the attribute value itself. |
| 214 | */ |
| 215 | uint dblocks = XFS_B_TO_FSB(mp, valuelen); |
| 216 | nblks += dblocks; |
| 217 | nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK); |
| 218 | } |
| 219 | |
| 220 | return nblks; |
| 221 | } |
| 222 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 223 | STATIC int |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 224 | xfs_attr_set_int( |
| 225 | struct xfs_inode *dp, |
| 226 | struct xfs_name *name, |
| 227 | unsigned char *value, |
| 228 | int valuelen, |
| 229 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
| 231 | xfs_da_args_t args; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | xfs_fsblock_t firstblock; |
| 233 | xfs_bmap_free_t flist; |
| 234 | int error, err2, committed; |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 235 | xfs_mount_t *mp = dp->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | int rsvd = (flags & ATTR_ROOT) != 0; |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 237 | int local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * Attach the dquots to the inode. |
| 241 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 242 | error = xfs_qm_dqattach(dp, 0); |
| 243 | if (error) |
| 244 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * If the inode doesn't have an attribute fork, add one. |
| 248 | * (inode must not be locked when we call this routine) |
| 249 | */ |
| 250 | if (XFS_IFORK_Q(dp) == 0) { |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 251 | int sf_size = sizeof(xfs_attr_sf_hdr_t) + |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 252 | XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen); |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 253 | |
| 254 | if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return(error); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Fill in the arg structure for this request. |
| 260 | */ |
| 261 | memset((char *)&args, 0, sizeof(args)); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 262 | args.name = name->name; |
| 263 | args.namelen = name->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | args.value = value; |
| 265 | args.valuelen = valuelen; |
| 266 | args.flags = flags; |
| 267 | args.hashval = xfs_da_hashname(args.name, args.namelen); |
| 268 | args.dp = dp; |
| 269 | args.firstblock = &firstblock; |
| 270 | args.flist = &flist; |
| 271 | args.whichfork = XFS_ATTR_FORK; |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 272 | args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | /* Size is now blocks for attribute data */ |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 275 | args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * Start our first transaction of the day. |
| 279 | * |
| 280 | * All future transactions during this code must be "chained" off |
| 281 | * this one via the trans_dup() call. All transactions will contain |
| 282 | * the inode, and the inode will always be marked with trans_ihold(). |
| 283 | * Since the inode will be locked in all transactions, we must log |
| 284 | * the inode in every transaction to let it float upward through |
| 285 | * the log. |
| 286 | */ |
| 287 | args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET); |
| 288 | |
| 289 | /* |
| 290 | * Root fork attributes can use reserved data blocks for this |
| 291 | * operation if necessary |
| 292 | */ |
| 293 | |
| 294 | if (rsvd) |
| 295 | args.trans->t_flags |= XFS_TRANS_RESERVE; |
| 296 | |
Jeff Liu | a21cd50 | 2013-01-28 21:27:53 +0800 | [diff] [blame] | 297 | error = xfs_trans_reserve(args.trans, args.total, |
| 298 | XFS_ATTRSETM_LOG_RES(mp) + |
| 299 | XFS_ATTRSETRT_LOG_RES(mp) * args.total, |
| 300 | 0, XFS_TRANS_PERM_LOG_RES, |
| 301 | XFS_ATTRSET_LOG_COUNT); |
| 302 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | xfs_trans_cancel(args.trans, 0); |
| 304 | return(error); |
| 305 | } |
| 306 | xfs_ilock(dp, XFS_ILOCK_EXCL); |
| 307 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 308 | error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0, |
Niv Sardi | 5e9da7b | 2008-08-13 16:03:35 +1000 | [diff] [blame] | 309 | rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 310 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (error) { |
| 312 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 313 | xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES); |
| 314 | return (error); |
| 315 | } |
| 316 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 317 | xfs_trans_ijoin(args.trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 320 | * If the attribute list is non-existent or a shortform list, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | * upgrade it to a single-leaf-block attribute list. |
| 322 | */ |
| 323 | if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) || |
| 324 | ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) && |
| 325 | (dp->i_d.di_anextents == 0))) { |
| 326 | |
| 327 | /* |
| 328 | * Build initial attribute list (if required). |
| 329 | */ |
| 330 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 331 | xfs_attr_shortform_create(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * Try to add the attr to the attribute list in |
| 335 | * the inode. |
| 336 | */ |
| 337 | error = xfs_attr_shortform_addname(&args); |
| 338 | if (error != ENOSPC) { |
| 339 | /* |
| 340 | * Commit the shortform mods, and we're done. |
| 341 | * NOTE: this is also the error path (EEXIST, etc). |
| 342 | */ |
| 343 | ASSERT(args.trans != NULL); |
| 344 | |
| 345 | /* |
| 346 | * If this is a synchronous mount, make sure that |
| 347 | * the transaction goes to disk before returning |
| 348 | * to the user. |
| 349 | */ |
| 350 | if (mp->m_flags & XFS_MOUNT_WSYNC) { |
| 351 | xfs_trans_set_sync(args.trans); |
| 352 | } |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 353 | |
| 354 | if (!error && (flags & ATTR_KERNOTIME) == 0) { |
| 355 | xfs_trans_ichgtime(args.trans, dp, |
| 356 | XFS_ICHGTIME_CHG); |
| 357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | err2 = xfs_trans_commit(args.trans, |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 359 | XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return(error == 0 ? err2 : error); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * It won't fit in the shortform, transform to a leaf block. |
| 367 | * GROT: another possible req'mt for a double-split btree op. |
| 368 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 369 | xfs_bmap_init(args.flist, args.firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | error = xfs_attr_shortform_to_leaf(&args); |
| 371 | if (!error) { |
| 372 | error = xfs_bmap_finish(&args.trans, args.flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 373 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | if (error) { |
| 376 | ASSERT(committed); |
| 377 | args.trans = NULL; |
| 378 | xfs_bmap_cancel(&flist); |
| 379 | goto out; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * bmap_finish() may have committed the last trans and started |
| 384 | * a new one. We need the inode to be in all transactions. |
| 385 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 386 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 387 | xfs_trans_ijoin(args.trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | /* |
| 390 | * Commit the leaf transformation. We'll need another (linked) |
| 391 | * transaction to add the new attribute to the leaf. |
| 392 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 393 | |
| 394 | error = xfs_trans_roll(&args.trans, dp); |
| 395 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | goto out; |
| 397 | |
| 398 | } |
| 399 | |
| 400 | if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 401 | error = xfs_attr_leaf_addname(&args); |
| 402 | } else { |
| 403 | error = xfs_attr_node_addname(&args); |
| 404 | } |
| 405 | if (error) { |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * If this is a synchronous mount, make sure that the |
| 411 | * transaction goes to disk before returning to the user. |
| 412 | */ |
| 413 | if (mp->m_flags & XFS_MOUNT_WSYNC) { |
| 414 | xfs_trans_set_sync(args.trans); |
| 415 | } |
| 416 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 417 | if ((flags & ATTR_KERNOTIME) == 0) |
| 418 | xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG); |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* |
| 421 | * Commit the last in the sequence of transactions. |
| 422 | */ |
| 423 | xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 424 | error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return(error); |
| 428 | |
| 429 | out: |
| 430 | if (args.trans) |
| 431 | xfs_trans_cancel(args.trans, |
| 432 | XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 433 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 434 | return(error); |
| 435 | } |
| 436 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 437 | int |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 438 | xfs_attr_set( |
| 439 | xfs_inode_t *dp, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 440 | const unsigned char *name, |
| 441 | unsigned char *value, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 442 | int valuelen, |
| 443 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 445 | int error; |
| 446 | struct xfs_name xname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 448 | XFS_STATS_INC(xs_attr_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 450 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | return (EIO); |
| 452 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 453 | error = xfs_attr_name_to_xname(&xname, name); |
| 454 | if (error) |
| 455 | return error; |
| 456 | |
| 457 | return xfs_attr_set_int(dp, &xname, value, valuelen, flags); |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Generic handler routine to remove a name from an attribute list. |
| 462 | * Transitions attribute list from Btree to shortform as necessary. |
| 463 | */ |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 464 | STATIC int |
| 465 | 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] | 466 | { |
| 467 | xfs_da_args_t args; |
| 468 | xfs_fsblock_t firstblock; |
| 469 | xfs_bmap_free_t flist; |
| 470 | int error; |
| 471 | xfs_mount_t *mp = dp->i_mount; |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | /* |
| 474 | * Fill in the arg structure for this request. |
| 475 | */ |
| 476 | memset((char *)&args, 0, sizeof(args)); |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 477 | args.name = name->name; |
| 478 | args.namelen = name->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | args.flags = flags; |
| 480 | args.hashval = xfs_da_hashname(args.name, args.namelen); |
| 481 | args.dp = dp; |
| 482 | args.firstblock = &firstblock; |
| 483 | args.flist = &flist; |
| 484 | args.total = 0; |
| 485 | args.whichfork = XFS_ATTR_FORK; |
| 486 | |
| 487 | /* |
Dave Chinner | 4a33821 | 2011-06-23 01:35:01 +0000 | [diff] [blame] | 488 | * we have no control over the attribute names that userspace passes us |
| 489 | * to remove, so we have to allow the name lookup prior to attribute |
| 490 | * removal to fail. |
| 491 | */ |
| 492 | args.op_flags = XFS_DA_OP_OKNOENT; |
| 493 | |
| 494 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | * Attach the dquots to the inode. |
| 496 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 497 | error = xfs_qm_dqattach(dp, 0); |
| 498 | if (error) |
| 499 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * Start our first transaction of the day. |
| 503 | * |
| 504 | * All future transactions during this code must be "chained" off |
| 505 | * this one via the trans_dup() call. All transactions will contain |
| 506 | * the inode, and the inode will always be marked with trans_ihold(). |
| 507 | * Since the inode will be locked in all transactions, we must log |
| 508 | * the inode in every transaction to let it float upward through |
| 509 | * the log. |
| 510 | */ |
| 511 | args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM); |
| 512 | |
| 513 | /* |
| 514 | * Root fork attributes can use reserved data blocks for this |
| 515 | * operation if necessary |
| 516 | */ |
| 517 | |
| 518 | if (flags & ATTR_ROOT) |
| 519 | args.trans->t_flags |= XFS_TRANS_RESERVE; |
| 520 | |
| 521 | if ((error = xfs_trans_reserve(args.trans, |
| 522 | XFS_ATTRRM_SPACE_RES(mp), |
| 523 | XFS_ATTRRM_LOG_RES(mp), |
| 524 | 0, XFS_TRANS_PERM_LOG_RES, |
| 525 | XFS_ATTRRM_LOG_COUNT))) { |
| 526 | xfs_trans_cancel(args.trans, 0); |
| 527 | return(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | xfs_ilock(dp, XFS_ILOCK_EXCL); |
| 531 | /* |
| 532 | * No need to make quota reservations here. We expect to release some |
| 533 | * blocks not allocate in the common case. |
| 534 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 535 | xfs_trans_ijoin(args.trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Decide on what work routines to call based on the inode size. |
| 539 | */ |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 540 | if (!xfs_inode_hasattr(dp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | error = XFS_ERROR(ENOATTR); |
| 542 | goto out; |
| 543 | } |
| 544 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { |
| 545 | ASSERT(dp->i_afp->if_flags & XFS_IFINLINE); |
| 546 | error = xfs_attr_shortform_remove(&args); |
| 547 | if (error) { |
| 548 | goto out; |
| 549 | } |
| 550 | } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 551 | error = xfs_attr_leaf_removename(&args); |
| 552 | } else { |
| 553 | error = xfs_attr_node_removename(&args); |
| 554 | } |
| 555 | if (error) { |
| 556 | goto out; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * If this is a synchronous mount, make sure that the |
| 561 | * transaction goes to disk before returning to the user. |
| 562 | */ |
| 563 | if (mp->m_flags & XFS_MOUNT_WSYNC) { |
| 564 | xfs_trans_set_sync(args.trans); |
| 565 | } |
| 566 | |
Dave Chinner | dcd79a1 | 2010-09-28 12:27:25 +1000 | [diff] [blame] | 567 | if ((flags & ATTR_KERNOTIME) == 0) |
| 568 | xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG); |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | /* |
| 571 | * Commit the last in the sequence of transactions. |
| 572 | */ |
| 573 | xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 574 | error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return(error); |
| 578 | |
| 579 | out: |
| 580 | if (args.trans) |
| 581 | xfs_trans_cancel(args.trans, |
| 582 | XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 583 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 584 | return(error); |
| 585 | } |
| 586 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 587 | int |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 588 | xfs_attr_remove( |
| 589 | xfs_inode_t *dp, |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 590 | const unsigned char *name, |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 591 | int flags) |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 592 | { |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 593 | int error; |
| 594 | struct xfs_name xname; |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 595 | |
| 596 | XFS_STATS_INC(xs_attr_remove); |
| 597 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 598 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
| 599 | return (EIO); |
| 600 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 601 | error = xfs_attr_name_to_xname(&xname, name); |
| 602 | if (error) |
| 603 | return error; |
| 604 | |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 605 | xfs_ilock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 606 | if (!xfs_inode_hasattr(dp)) { |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 607 | xfs_iunlock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 608 | return XFS_ERROR(ENOATTR); |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 609 | } |
| 610 | xfs_iunlock(dp, XFS_ILOCK_SHARED); |
| 611 | |
Barry Naujok | e8b0eba | 2008-04-22 17:34:31 +1000 | [diff] [blame] | 612 | return xfs_attr_remove_int(dp, &xname, flags); |
Nathan Scott | aa82daa | 2005-11-02 10:33:33 +1100 | [diff] [blame] | 613 | } |
| 614 | |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 615 | int |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 616 | xfs_attr_list_int(xfs_attr_list_context_t *context) |
| 617 | { |
| 618 | int error; |
| 619 | xfs_inode_t *dp = context->dp; |
| 620 | |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 621 | XFS_STATS_INC(xs_attr_list); |
| 622 | |
| 623 | if (XFS_FORCED_SHUTDOWN(dp->i_mount)) |
| 624 | return EIO; |
| 625 | |
| 626 | xfs_ilock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 627 | |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 628 | /* |
| 629 | * Decide on what work routines to call based on the inode size. |
| 630 | */ |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 631 | if (!xfs_inode_hasattr(dp)) { |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 632 | error = 0; |
| 633 | } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { |
| 634 | error = xfs_attr_shortform_list(context); |
| 635 | } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 636 | error = xfs_attr_leaf_list(context); |
| 637 | } else { |
| 638 | error = xfs_attr_node_list(context); |
| 639 | } |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 640 | |
| 641 | xfs_iunlock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 642 | |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 643 | return error; |
| 644 | } |
| 645 | |
| 646 | #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \ |
| 647 | (((struct attrlist_ent *) 0)->a_name - (char *) 0) |
| 648 | #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \ |
| 649 | ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \ |
| 650 | & ~(sizeof(u_int32_t)-1)) |
| 651 | |
| 652 | /* |
| 653 | * Format an attribute and copy it out to the user's buffer. |
| 654 | * Take care to check values and protect against them changing later, |
| 655 | * we may be reading them directly out of a user buffer. |
| 656 | */ |
| 657 | /*ARGSUSED*/ |
| 658 | STATIC int |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 659 | xfs_attr_put_listent( |
| 660 | xfs_attr_list_context_t *context, |
| 661 | int flags, |
| 662 | unsigned char *name, |
| 663 | int namelen, |
| 664 | int valuelen, |
| 665 | unsigned char *value) |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 666 | { |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 667 | struct attrlist *alist = (struct attrlist *)context->alist; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 668 | attrlist_ent_t *aep; |
| 669 | int arraytop; |
| 670 | |
| 671 | ASSERT(!(context->flags & ATTR_KERNOVAL)); |
| 672 | ASSERT(context->count >= 0); |
| 673 | ASSERT(context->count < (ATTR_MAX_VALUELEN/8)); |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 674 | ASSERT(context->firstu >= sizeof(*alist)); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 675 | ASSERT(context->firstu <= context->bufsize); |
| 676 | |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 677 | /* |
| 678 | * Only list entries in the right namespace. |
| 679 | */ |
| 680 | if (((context->flags & ATTR_SECURE) == 0) != |
| 681 | ((flags & XFS_ATTR_SECURE) == 0)) |
| 682 | return 0; |
| 683 | if (((context->flags & ATTR_ROOT) == 0) != |
| 684 | ((flags & XFS_ATTR_ROOT) == 0)) |
| 685 | return 0; |
| 686 | |
| 687 | arraytop = sizeof(*alist) + |
| 688 | context->count * sizeof(alist->al_offset[0]); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 689 | context->firstu -= ATTR_ENTSIZE(namelen); |
| 690 | if (context->firstu < arraytop) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 691 | trace_xfs_attr_list_full(context); |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 692 | alist->al_more = 1; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 693 | context->seen_enough = 1; |
| 694 | return 1; |
| 695 | } |
| 696 | |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 697 | aep = (attrlist_ent_t *)&context->alist[context->firstu]; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 698 | aep->a_valuelen = valuelen; |
| 699 | memcpy(aep->a_name, name, namelen); |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 700 | aep->a_name[namelen] = 0; |
| 701 | alist->al_offset[context->count++] = context->firstu; |
| 702 | alist->al_count = context->count; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 703 | trace_xfs_attr_list_add(context); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | /* |
| 708 | * Generate a list of extended attribute names and optionally |
| 709 | * also value lengths. Positive return value follows the XFS |
| 710 | * convention of being an error, zero or negative return code |
| 711 | * is the length of the buffer returned (negated), indicating |
| 712 | * success. |
| 713 | */ |
| 714 | int |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 715 | xfs_attr_list( |
| 716 | xfs_inode_t *dp, |
| 717 | char *buffer, |
| 718 | int bufsize, |
| 719 | int flags, |
| 720 | attrlist_cursor_kern_t *cursor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
| 722 | xfs_attr_list_context_t context; |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 723 | struct attrlist *alist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | int error; |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | /* |
| 727 | * Validate the cursor. |
| 728 | */ |
| 729 | if (cursor->pad1 || cursor->pad2) |
| 730 | return(XFS_ERROR(EINVAL)); |
| 731 | if ((cursor->initted == 0) && |
| 732 | (cursor->hashval || cursor->blkno || cursor->offset)) |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 733 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Check for a properly aligned buffer. |
| 737 | */ |
| 738 | if (((long)buffer) & (sizeof(int)-1)) |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 739 | return XFS_ERROR(EFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (flags & ATTR_KERNOVAL) |
| 741 | bufsize = 0; |
| 742 | |
| 743 | /* |
| 744 | * Initialize the output buffer. |
| 745 | */ |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 746 | memset(&context, 0, sizeof(context)); |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 747 | context.dp = dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | context.cursor = cursor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | context.resynch = 1; |
| 750 | context.flags = flags; |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 751 | context.alist = buffer; |
| 752 | context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */ |
| 753 | context.firstu = context.bufsize; |
| 754 | context.put_listent = xfs_attr_put_listent; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 755 | |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 756 | alist = (struct attrlist *)context.alist; |
| 757 | alist->al_count = 0; |
| 758 | alist->al_more = 0; |
| 759 | alist->al_offset[0] = context.bufsize; |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 760 | |
| 761 | error = xfs_attr_list_int(&context); |
Christoph Hellwig | ad9b463 | 2008-06-23 13:23:48 +1000 | [diff] [blame] | 762 | ASSERT(error >= 0); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 763 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | int /* error */ |
| 767 | xfs_attr_inactive(xfs_inode_t *dp) |
| 768 | { |
| 769 | xfs_trans_t *trans; |
| 770 | xfs_mount_t *mp; |
| 771 | int error; |
| 772 | |
| 773 | mp = dp->i_mount; |
| 774 | ASSERT(! XFS_NOT_DQATTACHED(mp, dp)); |
| 775 | |
| 776 | xfs_ilock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 777 | if (!xfs_inode_hasattr(dp) || |
| 778 | dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | xfs_iunlock(dp, XFS_ILOCK_SHARED); |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 780 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | xfs_iunlock(dp, XFS_ILOCK_SHARED); |
| 783 | |
| 784 | /* |
| 785 | * Start our first transaction of the day. |
| 786 | * |
| 787 | * All future transactions during this code must be "chained" off |
| 788 | * this one via the trans_dup() call. All transactions will contain |
| 789 | * the inode, and the inode will always be marked with trans_ihold(). |
| 790 | * Since the inode will be locked in all transactions, we must log |
| 791 | * the inode in every transaction to let it float upward through |
| 792 | * the log. |
| 793 | */ |
| 794 | trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL); |
| 795 | if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0, |
| 796 | XFS_TRANS_PERM_LOG_RES, |
| 797 | XFS_ATTRINVAL_LOG_COUNT))) { |
| 798 | xfs_trans_cancel(trans, 0); |
| 799 | return(error); |
| 800 | } |
| 801 | xfs_ilock(dp, XFS_ILOCK_EXCL); |
| 802 | |
| 803 | /* |
| 804 | * No need to make quota reservations here. We expect to release some |
| 805 | * blocks, not allocate, in the common case. |
| 806 | */ |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 807 | xfs_trans_ijoin(trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | /* |
| 810 | * Decide on what work routines to call based on the inode size. |
| 811 | */ |
Christoph Hellwig | caf8aab | 2008-06-23 13:23:41 +1000 | [diff] [blame] | 812 | if (!xfs_inode_hasattr(dp) || |
| 813 | dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | error = 0; |
| 815 | goto out; |
| 816 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 817 | error = xfs_attr3_root_inactive(&trans, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | if (error) |
| 819 | goto out; |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 820 | |
Christoph Hellwig | 8f04c47 | 2011-07-08 14:34:34 +0200 | [diff] [blame] | 821 | error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0); |
| 822 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | goto out; |
| 824 | |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 825 | error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 827 | |
| 828 | return(error); |
| 829 | |
| 830 | out: |
| 831 | xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 832 | xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 833 | return(error); |
| 834 | } |
| 835 | |
| 836 | |
| 837 | |
| 838 | /*======================================================================== |
| 839 | * External routines when attribute list is inside the inode |
| 840 | *========================================================================*/ |
| 841 | |
| 842 | /* |
| 843 | * Add a name to the shortform attribute list structure |
| 844 | * This is the external routine. |
| 845 | */ |
| 846 | STATIC int |
| 847 | xfs_attr_shortform_addname(xfs_da_args_t *args) |
| 848 | { |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 849 | int newsize, forkoff, retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 851 | trace_xfs_attr_sf_addname(args); |
| 852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | retval = xfs_attr_shortform_lookup(args); |
| 854 | if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) { |
| 855 | return(retval); |
| 856 | } else if (retval == EEXIST) { |
| 857 | if (args->flags & ATTR_CREATE) |
| 858 | return(retval); |
| 859 | retval = xfs_attr_shortform_remove(args); |
| 860 | ASSERT(retval == 0); |
| 861 | } |
| 862 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 863 | if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX || |
| 864 | args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
| 865 | return(XFS_ERROR(ENOSPC)); |
| 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | newsize = XFS_ATTR_SF_TOTSIZE(args->dp); |
| 868 | newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 869 | |
| 870 | forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize); |
| 871 | if (!forkoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | return(XFS_ERROR(ENOSPC)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 873 | |
| 874 | xfs_attr_shortform_add(args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | return(0); |
| 876 | } |
| 877 | |
| 878 | |
| 879 | /*======================================================================== |
| 880 | * External routines when attribute list is one block |
| 881 | *========================================================================*/ |
| 882 | |
| 883 | /* |
| 884 | * Add a name to the leaf attribute list structure |
| 885 | * |
| 886 | * This leaf block cannot have a "remote" value, we only call this routine |
| 887 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 888 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 889 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | xfs_attr_leaf_addname(xfs_da_args_t *args) |
| 891 | { |
| 892 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 893 | struct xfs_buf *bp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 894 | int retval, error, committed, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 896 | trace_xfs_attr_leaf_addname(args); |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | /* |
| 899 | * Read the (only) block in the attribute list in. |
| 900 | */ |
| 901 | dp = args->dp; |
| 902 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 903 | 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] | 904 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 905 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| 907 | /* |
| 908 | * Look up the given attribute in the leaf block. Figure out if |
| 909 | * the given flags produce an error or call for an atomic rename. |
| 910 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 911 | retval = xfs_attr3_leaf_lookup_int(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 913 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 914 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } else if (retval == EEXIST) { |
| 916 | if (args->flags & ATTR_CREATE) { /* pure create op */ |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 917 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 918 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 920 | |
| 921 | trace_xfs_attr_leaf_replace(args); |
| 922 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 923 | args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
| 925 | args->index2 = args->index; |
| 926 | args->rmtblkno2 = args->rmtblkno; |
| 927 | args->rmtblkcnt2 = args->rmtblkcnt; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Add the attribute to the leaf block, transitioning to a Btree |
| 932 | * if required. |
| 933 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 934 | retval = xfs_attr3_leaf_add(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | if (retval == ENOSPC) { |
| 936 | /* |
| 937 | * Promote the attribute list to the Btree format, then |
| 938 | * Commit that transaction so that the node_addname() call |
| 939 | * can manage its own transactions. |
| 940 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 941 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 942 | error = xfs_attr3_leaf_to_node(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | if (!error) { |
| 944 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 945 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | if (error) { |
| 948 | ASSERT(committed); |
| 949 | args->trans = NULL; |
| 950 | xfs_bmap_cancel(args->flist); |
| 951 | return(error); |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * bmap_finish() may have committed the last trans and started |
| 956 | * a new one. We need the inode to be in all transactions. |
| 957 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 958 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 959 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
| 961 | /* |
| 962 | * Commit the current trans (including the inode) and start |
| 963 | * a new one. |
| 964 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 965 | error = xfs_trans_roll(&args->trans, dp); |
| 966 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | return (error); |
| 968 | |
| 969 | /* |
| 970 | * Fob the whole rest of the problem off on the Btree code. |
| 971 | */ |
| 972 | error = xfs_attr_node_addname(args); |
| 973 | return(error); |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Commit the transaction that added the attr name so that |
| 978 | * later routines can manage their own transactions. |
| 979 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 980 | error = xfs_trans_roll(&args->trans, dp); |
| 981 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | return (error); |
| 983 | |
| 984 | /* |
| 985 | * If there was an out-of-line value, allocate the blocks we |
| 986 | * identified for its storage and copy the value. This is done |
| 987 | * after we create the attribute so that we don't overflow the |
| 988 | * maximum size of a transaction and/or hit a deadlock. |
| 989 | */ |
| 990 | if (args->rmtblkno > 0) { |
| 991 | error = xfs_attr_rmtval_set(args); |
| 992 | if (error) |
| 993 | return(error); |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * If this is an atomic rename operation, we must "flip" the |
| 998 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 999 | * so that one disappears and one appears atomically. Then we |
| 1000 | * must remove the "old" attribute/value pair. |
| 1001 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1002 | if (args->op_flags & XFS_DA_OP_RENAME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | /* |
| 1004 | * In a separate transaction, set the incomplete flag on the |
| 1005 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 1006 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1007 | error = xfs_attr3_leaf_flipflags(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (error) |
| 1009 | return(error); |
| 1010 | |
| 1011 | /* |
| 1012 | * Dismantle the "old" attribute/value pair by removing |
| 1013 | * a "remote" value (if it exists). |
| 1014 | */ |
| 1015 | args->index = args->index2; |
| 1016 | args->blkno = args->blkno2; |
| 1017 | args->rmtblkno = args->rmtblkno2; |
| 1018 | args->rmtblkcnt = args->rmtblkcnt2; |
| 1019 | if (args->rmtblkno) { |
| 1020 | error = xfs_attr_rmtval_remove(args); |
| 1021 | if (error) |
| 1022 | return(error); |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * Read in the block containing the "old" attr, then |
| 1027 | * remove the "old" attr from that block (neat, huh!) |
| 1028 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1029 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 1030 | -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 1032 | return error; |
| 1033 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1034 | xfs_attr3_leaf_remove(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* |
| 1037 | * If the result is small enough, shrink it all into the inode. |
| 1038 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1039 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1040 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1041 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | /* bp is gone due to xfs_da_shrink_inode */ |
| 1043 | if (!error) { |
| 1044 | error = xfs_bmap_finish(&args->trans, |
| 1045 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | &committed); |
| 1047 | } |
| 1048 | if (error) { |
| 1049 | ASSERT(committed); |
| 1050 | args->trans = NULL; |
| 1051 | xfs_bmap_cancel(args->flist); |
| 1052 | return(error); |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * bmap_finish() may have committed the last trans |
| 1057 | * and started a new one. We need the inode to be |
| 1058 | * in all transactions. |
| 1059 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1060 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1061 | xfs_trans_ijoin(args->trans, dp, 0); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * Commit the remove and start the next trans in series. |
| 1066 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1067 | error = xfs_trans_roll(&args->trans, dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
| 1069 | } else if (args->rmtblkno > 0) { |
| 1070 | /* |
| 1071 | * Added a "remote" value, just clear the incomplete flag. |
| 1072 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1073 | error = xfs_attr3_leaf_clearflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1075 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * Remove a name from the leaf attribute list structure |
| 1080 | * |
| 1081 | * This leaf block cannot have a "remote" value, we only call this routine |
| 1082 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 1083 | */ |
| 1084 | STATIC int |
| 1085 | xfs_attr_leaf_removename(xfs_da_args_t *args) |
| 1086 | { |
| 1087 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1088 | struct xfs_buf *bp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1089 | int error, committed, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1091 | trace_xfs_attr_leaf_removename(args); |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | /* |
| 1094 | * Remove the attribute. |
| 1095 | */ |
| 1096 | dp = args->dp; |
| 1097 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1098 | 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] | 1099 | if (error) |
| 1100 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1102 | error = xfs_attr3_leaf_lookup_int(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | if (error == ENOATTR) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1104 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1105 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1108 | xfs_attr3_leaf_remove(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
| 1110 | /* |
| 1111 | * If the result is small enough, shrink it all into the inode. |
| 1112 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1113 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1114 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1115 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | /* bp is gone due to xfs_da_shrink_inode */ |
| 1117 | if (!error) { |
| 1118 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1119 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } |
| 1121 | if (error) { |
| 1122 | ASSERT(committed); |
| 1123 | args->trans = NULL; |
| 1124 | xfs_bmap_cancel(args->flist); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1125 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * bmap_finish() may have committed the last trans and started |
| 1130 | * a new one. We need the inode to be in all transactions. |
| 1131 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1132 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1133 | xfs_trans_ijoin(args->trans, dp, 0); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1134 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1135 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Look up a name in a leaf attribute list structure. |
| 1140 | * |
| 1141 | * This leaf block cannot have a "remote" value, we only call this routine |
| 1142 | * if bmap_one_block() says there is only one block (ie: no remote blks). |
| 1143 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1144 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | xfs_attr_leaf_get(xfs_da_args_t *args) |
| 1146 | { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1147 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | int error; |
| 1149 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1150 | trace_xfs_attr_leaf_get(args); |
| 1151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | args->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1153 | 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] | 1154 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 1155 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1157 | error = xfs_attr3_leaf_lookup_int(bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | if (error != EEXIST) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1159 | xfs_trans_brelse(args->trans, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1160 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1162 | error = xfs_attr3_leaf_getvalue(bp, args); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1163 | xfs_trans_brelse(args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) { |
| 1165 | error = xfs_attr_rmtval_get(args); |
| 1166 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1167 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * Copy out attribute entries for attr_list(), for leaf attribute lists. |
| 1172 | */ |
| 1173 | STATIC int |
| 1174 | xfs_attr_leaf_list(xfs_attr_list_context_t *context) |
| 1175 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | int error; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1177 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1179 | trace_xfs_attr_leaf_list(context); |
| 1180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | context->cursor->blkno = 0; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1182 | error = xfs_attr3_leaf_read(NULL, context->dp, 0, -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | if (error) |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 1184 | return XFS_ERROR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1186 | error = xfs_attr3_leaf_list_int(bp, context); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1187 | xfs_trans_brelse(NULL, bp); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 1188 | return XFS_ERROR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | /*======================================================================== |
| 1193 | * External routines when attribute list size > XFS_LBSIZE(mp). |
| 1194 | *========================================================================*/ |
| 1195 | |
| 1196 | /* |
| 1197 | * Add a name to a Btree-format attribute list. |
| 1198 | * |
| 1199 | * This will involve walking down the Btree, and may involve splitting |
| 1200 | * leaf nodes and even splitting intermediate nodes up to and including |
| 1201 | * the root node (a special case of an intermediate node). |
| 1202 | * |
| 1203 | * "Remote" attribute values confuse the issue and atomic rename operations |
| 1204 | * add a whole extra layer of confusion on top of that. |
| 1205 | */ |
| 1206 | STATIC int |
| 1207 | xfs_attr_node_addname(xfs_da_args_t *args) |
| 1208 | { |
| 1209 | xfs_da_state_t *state; |
| 1210 | xfs_da_state_blk_t *blk; |
| 1211 | xfs_inode_t *dp; |
| 1212 | xfs_mount_t *mp; |
| 1213 | int committed, retval, error; |
| 1214 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1215 | trace_xfs_attr_node_addname(args); |
| 1216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | /* |
| 1218 | * Fill in bucket of arguments/results/context to carry around. |
| 1219 | */ |
| 1220 | dp = args->dp; |
| 1221 | mp = dp->i_mount; |
| 1222 | restart: |
| 1223 | state = xfs_da_state_alloc(); |
| 1224 | state->args = args; |
| 1225 | state->mp = mp; |
| 1226 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 1227 | state->node_ents = state->mp->m_attr_node_ents; |
| 1228 | |
| 1229 | /* |
| 1230 | * Search to see if name already exists, and get back a pointer |
| 1231 | * to where it should go. |
| 1232 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1233 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | if (error) |
| 1235 | goto out; |
| 1236 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1237 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1238 | if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) { |
| 1239 | goto out; |
| 1240 | } else if (retval == EEXIST) { |
| 1241 | if (args->flags & ATTR_CREATE) |
| 1242 | goto out; |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1243 | |
| 1244 | trace_xfs_attr_node_replace(args); |
| 1245 | |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1246 | args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | args->blkno2 = args->blkno; /* set 2nd entry info*/ |
| 1248 | args->index2 = args->index; |
| 1249 | args->rmtblkno2 = args->rmtblkno; |
| 1250 | args->rmtblkcnt2 = args->rmtblkcnt; |
| 1251 | args->rmtblkno = 0; |
| 1252 | args->rmtblkcnt = 0; |
| 1253 | } |
| 1254 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1255 | retval = xfs_attr3_leaf_add(blk->bp, state->args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | if (retval == ENOSPC) { |
| 1257 | if (state->path.active == 1) { |
| 1258 | /* |
| 1259 | * Its really a single leaf node, but it had |
| 1260 | * out-of-line values so it looked like it *might* |
| 1261 | * have been a b-tree. |
| 1262 | */ |
| 1263 | xfs_da_state_free(state); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1264 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1265 | error = xfs_attr3_leaf_to_node(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | if (!error) { |
| 1267 | error = xfs_bmap_finish(&args->trans, |
| 1268 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | &committed); |
| 1270 | } |
| 1271 | if (error) { |
| 1272 | ASSERT(committed); |
| 1273 | args->trans = NULL; |
| 1274 | xfs_bmap_cancel(args->flist); |
| 1275 | goto out; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * bmap_finish() may have committed the last trans |
| 1280 | * and started a new one. We need the inode to be |
| 1281 | * in all transactions. |
| 1282 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1283 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1284 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
| 1286 | /* |
| 1287 | * Commit the node conversion and start the next |
| 1288 | * trans in the chain. |
| 1289 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1290 | error = xfs_trans_roll(&args->trans, dp); |
| 1291 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | goto out; |
| 1293 | |
| 1294 | goto restart; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Split as many Btree elements as required. |
| 1299 | * This code tracks the new and old attr's location |
| 1300 | * in the index/blkno/rmtblkno/rmtblkcnt fields and |
| 1301 | * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields. |
| 1302 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1303 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1304 | error = xfs_da3_split(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | if (!error) { |
| 1306 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1307 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | } |
| 1309 | if (error) { |
| 1310 | ASSERT(committed); |
| 1311 | args->trans = NULL; |
| 1312 | xfs_bmap_cancel(args->flist); |
| 1313 | goto out; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * bmap_finish() may have committed the last trans and started |
| 1318 | * a new one. We need the inode to be in all transactions. |
| 1319 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1320 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1321 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } else { |
| 1323 | /* |
| 1324 | * Addition succeeded, update Btree hashvals. |
| 1325 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1326 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | * Kill the state structure, we're done with it and need to |
| 1331 | * allow the buffers to come back later. |
| 1332 | */ |
| 1333 | xfs_da_state_free(state); |
| 1334 | state = NULL; |
| 1335 | |
| 1336 | /* |
| 1337 | * Commit the leaf addition or btree split and start the next |
| 1338 | * trans in the chain. |
| 1339 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1340 | error = xfs_trans_roll(&args->trans, dp); |
| 1341 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | goto out; |
| 1343 | |
| 1344 | /* |
| 1345 | * If there was an out-of-line value, allocate the blocks we |
| 1346 | * identified for its storage and copy the value. This is done |
| 1347 | * after we create the attribute so that we don't overflow the |
| 1348 | * maximum size of a transaction and/or hit a deadlock. |
| 1349 | */ |
| 1350 | if (args->rmtblkno > 0) { |
| 1351 | error = xfs_attr_rmtval_set(args); |
| 1352 | if (error) |
| 1353 | return(error); |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * If this is an atomic rename operation, we must "flip" the |
| 1358 | * incomplete flags on the "new" and "old" attribute/value pairs |
| 1359 | * so that one disappears and one appears atomically. Then we |
| 1360 | * must remove the "old" attribute/value pair. |
| 1361 | */ |
Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 1362 | if (args->op_flags & XFS_DA_OP_RENAME) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | /* |
| 1364 | * In a separate transaction, set the incomplete flag on the |
| 1365 | * "old" attr and clear the incomplete flag on the "new" attr. |
| 1366 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1367 | error = xfs_attr3_leaf_flipflags(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | if (error) |
| 1369 | goto out; |
| 1370 | |
| 1371 | /* |
| 1372 | * Dismantle the "old" attribute/value pair by removing |
| 1373 | * a "remote" value (if it exists). |
| 1374 | */ |
| 1375 | args->index = args->index2; |
| 1376 | args->blkno = args->blkno2; |
| 1377 | args->rmtblkno = args->rmtblkno2; |
| 1378 | args->rmtblkcnt = args->rmtblkcnt2; |
| 1379 | if (args->rmtblkno) { |
| 1380 | error = xfs_attr_rmtval_remove(args); |
| 1381 | if (error) |
| 1382 | return(error); |
| 1383 | } |
| 1384 | |
| 1385 | /* |
| 1386 | * Re-find the "old" attribute entry after any split ops. |
| 1387 | * The INCOMPLETE flag means that we will find the "old" |
| 1388 | * attr, not the "new" one. |
| 1389 | */ |
| 1390 | args->flags |= XFS_ATTR_INCOMPLETE; |
| 1391 | state = xfs_da_state_alloc(); |
| 1392 | state->args = args; |
| 1393 | state->mp = mp; |
| 1394 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 1395 | state->node_ents = state->mp->m_attr_node_ents; |
| 1396 | state->inleaf = 0; |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1397 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | if (error) |
| 1399 | goto out; |
| 1400 | |
| 1401 | /* |
| 1402 | * Remove the name and update the hashvals in the tree. |
| 1403 | */ |
| 1404 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1405 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1406 | error = xfs_attr3_leaf_remove(blk->bp, args); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1407 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
| 1409 | /* |
| 1410 | * Check to see if the tree needs to be collapsed. |
| 1411 | */ |
| 1412 | if (retval && (state->path.active > 1)) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1413 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1414 | error = xfs_da3_join(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | if (!error) { |
| 1416 | error = xfs_bmap_finish(&args->trans, |
| 1417 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | &committed); |
| 1419 | } |
| 1420 | if (error) { |
| 1421 | ASSERT(committed); |
| 1422 | args->trans = NULL; |
| 1423 | xfs_bmap_cancel(args->flist); |
| 1424 | goto out; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * bmap_finish() may have committed the last trans |
| 1429 | * and started a new one. We need the inode to be |
| 1430 | * in all transactions. |
| 1431 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1432 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1433 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * Commit and start the next trans in the chain. |
| 1438 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1439 | error = xfs_trans_roll(&args->trans, dp); |
| 1440 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | goto out; |
| 1442 | |
| 1443 | } else if (args->rmtblkno > 0) { |
| 1444 | /* |
| 1445 | * Added a "remote" value, just clear the incomplete flag. |
| 1446 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1447 | error = xfs_attr3_leaf_clearflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | if (error) |
| 1449 | goto out; |
| 1450 | } |
| 1451 | retval = error = 0; |
| 1452 | |
| 1453 | out: |
| 1454 | if (state) |
| 1455 | xfs_da_state_free(state); |
| 1456 | if (error) |
| 1457 | return(error); |
| 1458 | return(retval); |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * Remove a name from a B-tree attribute list. |
| 1463 | * |
| 1464 | * This will involve walking down the Btree, and may involve joining |
| 1465 | * leaf nodes and even joining intermediate nodes up to and including |
| 1466 | * the root node (a special case of an intermediate node). |
| 1467 | */ |
| 1468 | STATIC int |
| 1469 | xfs_attr_node_removename(xfs_da_args_t *args) |
| 1470 | { |
| 1471 | xfs_da_state_t *state; |
| 1472 | xfs_da_state_blk_t *blk; |
| 1473 | xfs_inode_t *dp; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1474 | struct xfs_buf *bp; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1475 | int retval, error, committed, forkoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Dave Chinner | 5a5881c | 2012-03-22 05:15:13 +0000 | [diff] [blame] | 1477 | trace_xfs_attr_node_removename(args); |
| 1478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | /* |
| 1480 | * Tie a string around our finger to remind us where we are. |
| 1481 | */ |
| 1482 | dp = args->dp; |
| 1483 | state = xfs_da_state_alloc(); |
| 1484 | state->args = args; |
| 1485 | state->mp = dp->i_mount; |
| 1486 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 1487 | state->node_ents = state->mp->m_attr_node_ents; |
| 1488 | |
| 1489 | /* |
| 1490 | * Search to see if name exists, and get back a pointer to it. |
| 1491 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1492 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | if (error || (retval != EEXIST)) { |
| 1494 | if (error == 0) |
| 1495 | error = retval; |
| 1496 | goto out; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | * If there is an out-of-line value, de-allocate the blocks. |
| 1501 | * This is done before we remove the attribute so that we don't |
| 1502 | * overflow the maximum size of a transaction and/or hit a deadlock. |
| 1503 | */ |
| 1504 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1505 | ASSERT(blk->bp != NULL); |
| 1506 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1507 | if (args->rmtblkno > 0) { |
| 1508 | /* |
| 1509 | * Fill in disk block numbers in the state structure |
| 1510 | * so that we can get the buffers back after we commit |
| 1511 | * several transactions in the following calls. |
| 1512 | */ |
| 1513 | error = xfs_attr_fillstate(state); |
| 1514 | if (error) |
| 1515 | goto out; |
| 1516 | |
| 1517 | /* |
| 1518 | * Mark the attribute as INCOMPLETE, then bunmapi() the |
| 1519 | * remote value. |
| 1520 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1521 | error = xfs_attr3_leaf_setflag(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | if (error) |
| 1523 | goto out; |
| 1524 | error = xfs_attr_rmtval_remove(args); |
| 1525 | if (error) |
| 1526 | goto out; |
| 1527 | |
| 1528 | /* |
| 1529 | * Refill the state structure with buffers, the prior calls |
| 1530 | * released our buffers. |
| 1531 | */ |
| 1532 | error = xfs_attr_refillstate(state); |
| 1533 | if (error) |
| 1534 | goto out; |
| 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * Remove the name and update the hashvals in the tree. |
| 1539 | */ |
| 1540 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1541 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1542 | retval = xfs_attr3_leaf_remove(blk->bp, args); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1543 | xfs_da3_fixhashpath(state, &state->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
| 1545 | /* |
| 1546 | * Check to see if the tree needs to be collapsed. |
| 1547 | */ |
| 1548 | if (retval && (state->path.active > 1)) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1549 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1550 | error = xfs_da3_join(state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | if (!error) { |
| 1552 | error = xfs_bmap_finish(&args->trans, args->flist, |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 1553 | &committed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | } |
| 1555 | if (error) { |
| 1556 | ASSERT(committed); |
| 1557 | args->trans = NULL; |
| 1558 | xfs_bmap_cancel(args->flist); |
| 1559 | goto out; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * bmap_finish() may have committed the last trans and started |
| 1564 | * a new one. We need the inode to be in all transactions. |
| 1565 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1566 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1567 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
| 1569 | /* |
| 1570 | * Commit the Btree join operation and start a new trans. |
| 1571 | */ |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1572 | error = xfs_trans_roll(&args->trans, dp); |
| 1573 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | goto out; |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * If the result is small enough, push it all into the inode. |
| 1579 | */ |
| 1580 | if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) { |
| 1581 | /* |
| 1582 | * Have to get rid of the copy of this dabuf in the state. |
| 1583 | */ |
| 1584 | ASSERT(state->path.active == 1); |
| 1585 | ASSERT(state->path.blk[0].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | state->path.blk[0].bp = NULL; |
| 1587 | |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1588 | error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | if (error) |
| 1590 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 1592 | if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1593 | xfs_bmap_init(args->flist, args->firstblock); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1594 | error = xfs_attr3_leaf_to_shortform(bp, args, forkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | /* bp is gone due to xfs_da_shrink_inode */ |
| 1596 | if (!error) { |
| 1597 | error = xfs_bmap_finish(&args->trans, |
| 1598 | args->flist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | &committed); |
| 1600 | } |
| 1601 | if (error) { |
| 1602 | ASSERT(committed); |
| 1603 | args->trans = NULL; |
| 1604 | xfs_bmap_cancel(args->flist); |
| 1605 | goto out; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * bmap_finish() may have committed the last trans |
| 1610 | * and started a new one. We need the inode to be |
| 1611 | * in all transactions. |
| 1612 | */ |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 1613 | if (committed) |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 1614 | xfs_trans_ijoin(args->trans, dp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | } else |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1616 | xfs_trans_brelse(args->trans, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } |
| 1618 | error = 0; |
| 1619 | |
| 1620 | out: |
| 1621 | xfs_da_state_free(state); |
| 1622 | return(error); |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * Fill in the disk block numbers in the state structure for the buffers |
| 1627 | * that are attached to the state structure. |
| 1628 | * 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] | 1629 | * after some set of transaction commits have released these buffers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | */ |
| 1631 | STATIC int |
| 1632 | xfs_attr_fillstate(xfs_da_state_t *state) |
| 1633 | { |
| 1634 | xfs_da_state_path_t *path; |
| 1635 | xfs_da_state_blk_t *blk; |
| 1636 | int level; |
| 1637 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1638 | trace_xfs_attr_fillstate(state->args); |
| 1639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | /* |
| 1641 | * Roll down the "path" in the state structure, storing the on-disk |
| 1642 | * block number for those buffers in the "path". |
| 1643 | */ |
| 1644 | path = &state->path; |
| 1645 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1646 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1647 | if (blk->bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1648 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | blk->bp = NULL; |
| 1650 | } else { |
| 1651 | blk->disk_blkno = 0; |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1657 | * block number for those buffers in the "altpath". |
| 1658 | */ |
| 1659 | path = &state->altpath; |
| 1660 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1661 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1662 | if (blk->bp) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1663 | blk->disk_blkno = XFS_BUF_ADDR(blk->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | blk->bp = NULL; |
| 1665 | } else { |
| 1666 | blk->disk_blkno = 0; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | return(0); |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | * Reattach the buffers to the state structure based on the disk block |
| 1675 | * numbers stored in the state structure. |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1676 | * This is done after some set of transaction commits have released those |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | * buffers from our grip. |
| 1678 | */ |
| 1679 | STATIC int |
| 1680 | xfs_attr_refillstate(xfs_da_state_t *state) |
| 1681 | { |
| 1682 | xfs_da_state_path_t *path; |
| 1683 | xfs_da_state_blk_t *blk; |
| 1684 | int level, error; |
| 1685 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1686 | trace_xfs_attr_refillstate(state->args); |
| 1687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | /* |
| 1689 | * Roll down the "path" in the state structure, storing the on-disk |
| 1690 | * block number for those buffers in the "path". |
| 1691 | */ |
| 1692 | path = &state->path; |
| 1693 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1694 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1695 | if (blk->disk_blkno) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1696 | error = xfs_da3_node_read(state->args->trans, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | state->args->dp, |
| 1698 | blk->blkno, blk->disk_blkno, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1699 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | if (error) |
| 1701 | return(error); |
| 1702 | } else { |
| 1703 | blk->bp = NULL; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | /* |
| 1708 | * Roll down the "altpath" in the state structure, storing the on-disk |
| 1709 | * block number for those buffers in the "altpath". |
| 1710 | */ |
| 1711 | path = &state->altpath; |
| 1712 | ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH)); |
| 1713 | for (blk = path->blk, level = 0; level < path->active; blk++, level++) { |
| 1714 | if (blk->disk_blkno) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1715 | error = xfs_da3_node_read(state->args->trans, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | state->args->dp, |
| 1717 | blk->blkno, blk->disk_blkno, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1718 | &blk->bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | if (error) |
| 1720 | return(error); |
| 1721 | } else { |
| 1722 | blk->bp = NULL; |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | return(0); |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * Look up a filename in a node attribute list. |
| 1731 | * |
| 1732 | * This routine gets called for any attribute fork that has more than one |
| 1733 | * block, ie: both true Btree attr lists and for single-leaf-blocks with |
| 1734 | * "remote" values taking up more blocks. |
| 1735 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1736 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | xfs_attr_node_get(xfs_da_args_t *args) |
| 1738 | { |
| 1739 | xfs_da_state_t *state; |
| 1740 | xfs_da_state_blk_t *blk; |
| 1741 | int error, retval; |
| 1742 | int i; |
| 1743 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1744 | trace_xfs_attr_node_get(args); |
| 1745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | state = xfs_da_state_alloc(); |
| 1747 | state->args = args; |
| 1748 | state->mp = args->dp->i_mount; |
| 1749 | state->blocksize = state->mp->m_sb.sb_blocksize; |
| 1750 | state->node_ents = state->mp->m_attr_node_ents; |
| 1751 | |
| 1752 | /* |
| 1753 | * Search to see if name exists, and get back a pointer to it. |
| 1754 | */ |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1755 | error = xfs_da3_node_lookup_int(state, &retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | if (error) { |
| 1757 | retval = error; |
| 1758 | } else if (retval == EEXIST) { |
| 1759 | blk = &state->path.blk[ state->path.active-1 ]; |
| 1760 | ASSERT(blk->bp != NULL); |
| 1761 | ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 1762 | |
| 1763 | /* |
| 1764 | * Get the value, local or "remote" |
| 1765 | */ |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1766 | retval = xfs_attr3_leaf_getvalue(blk->bp, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | if (!retval && (args->rmtblkno > 0) |
| 1768 | && !(args->flags & ATTR_KERNOVAL)) { |
| 1769 | retval = xfs_attr_rmtval_get(args); |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | /* |
| 1774 | * If not in a transaction, we have to release all the buffers. |
| 1775 | */ |
| 1776 | for (i = 0; i < state->path.active; i++) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1777 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | state->path.blk[i].bp = NULL; |
| 1779 | } |
| 1780 | |
| 1781 | xfs_da_state_free(state); |
| 1782 | return(retval); |
| 1783 | } |
| 1784 | |
| 1785 | STATIC int /* error */ |
| 1786 | xfs_attr_node_list(xfs_attr_list_context_t *context) |
| 1787 | { |
| 1788 | attrlist_cursor_kern_t *cursor; |
| 1789 | xfs_attr_leafblock_t *leaf; |
| 1790 | xfs_da_intnode_t *node; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1791 | struct xfs_attr3_icleaf_hdr leafhdr; |
| 1792 | struct xfs_da3_icnode_hdr nodehdr; |
| 1793 | struct xfs_da_node_entry *btree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | int error, i; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1795 | struct xfs_buf *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | |
Dave Chinner | ee73259 | 2012-11-12 22:53:53 +1100 | [diff] [blame] | 1797 | trace_xfs_attr_node_list(context); |
| 1798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | cursor = context->cursor; |
| 1800 | cursor->initted = 1; |
| 1801 | |
| 1802 | /* |
| 1803 | * Do all sorts of validation on the passed-in cursor structure. |
| 1804 | * If anything is amiss, ignore the cursor and look up the hashval |
| 1805 | * starting from the btree root. |
| 1806 | */ |
| 1807 | bp = NULL; |
| 1808 | if (cursor->blkno > 0) { |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1809 | error = xfs_da3_node_read(NULL, context->dp, cursor->blkno, -1, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1810 | &bp, XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | if ((error != 0) && (error != EFSCORRUPTED)) |
| 1812 | return(error); |
| 1813 | if (bp) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1814 | struct xfs_attr_leaf_entry *entries; |
| 1815 | |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1816 | node = bp->b_addr; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1817 | switch (be16_to_cpu(node->hdr.info.magic)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | case XFS_DA_NODE_MAGIC: |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1819 | case XFS_DA3_NODE_MAGIC: |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1820 | trace_xfs_attr_list_wrong_blk(context); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1821 | xfs_trans_brelse(NULL, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | bp = NULL; |
| 1823 | break; |
| 1824 | case XFS_ATTR_LEAF_MAGIC: |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1825 | case XFS_ATTR3_LEAF_MAGIC: |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1826 | leaf = bp->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1827 | xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf); |
| 1828 | entries = xfs_attr3_leaf_entryp(leaf); |
| 1829 | if (cursor->hashval > be32_to_cpu( |
| 1830 | entries[leafhdr.count - 1].hashval)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1831 | trace_xfs_attr_list_wrong_blk(context); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1832 | xfs_trans_brelse(NULL, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | bp = NULL; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1834 | } else if (cursor->hashval <= be32_to_cpu( |
| 1835 | entries[0].hashval)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1836 | trace_xfs_attr_list_wrong_blk(context); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1837 | xfs_trans_brelse(NULL, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | bp = NULL; |
| 1839 | } |
| 1840 | break; |
| 1841 | default: |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1842 | trace_xfs_attr_list_wrong_blk(context); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1843 | xfs_trans_brelse(NULL, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | bp = NULL; |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | /* |
| 1850 | * We did not find what we expected given the cursor's contents, |
| 1851 | * so we start from the top and work down based on the hash value. |
| 1852 | * Note that start of node block is same as start of leaf block. |
| 1853 | */ |
| 1854 | if (bp == NULL) { |
| 1855 | cursor->blkno = 0; |
| 1856 | for (;;) { |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1857 | __uint16_t magic; |
| 1858 | |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1859 | error = xfs_da3_node_read(NULL, context->dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | cursor->blkno, -1, &bp, |
Dave Chinner | d9392a4 | 2012-11-12 22:54:17 +1100 | [diff] [blame] | 1861 | XFS_ATTR_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | if (error) |
| 1863 | return(error); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1864 | node = bp->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1865 | magic = be16_to_cpu(node->hdr.info.magic); |
| 1866 | if (magic == XFS_ATTR_LEAF_MAGIC || |
| 1867 | magic == XFS_ATTR3_LEAF_MAGIC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | break; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1869 | if (magic != XFS_DA_NODE_MAGIC && |
| 1870 | magic != XFS_DA3_NODE_MAGIC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)", |
| 1872 | XFS_ERRLEVEL_LOW, |
| 1873 | context->dp->i_mount, |
| 1874 | node); |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1875 | xfs_trans_brelse(NULL, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1876 | return XFS_ERROR(EFSCORRUPTED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1878 | |
| 1879 | xfs_da3_node_hdr_from_disk(&nodehdr, node); |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 1880 | btree = xfs_da3_node_tree_p(node); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1881 | for (i = 0; i < nodehdr.count; btree++, i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | if (cursor->hashval |
Nathan Scott | 403432d | 2006-03-17 17:29:46 +1100 | [diff] [blame] | 1883 | <= be32_to_cpu(btree->hashval)) { |
| 1884 | cursor->blkno = be32_to_cpu(btree->before); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1885 | trace_xfs_attr_list_node_descend(context, |
| 1886 | btree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | break; |
| 1888 | } |
| 1889 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1890 | if (i == nodehdr.count) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1891 | xfs_trans_brelse(NULL, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1892 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1894 | xfs_trans_brelse(NULL, bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | ASSERT(bp != NULL); |
| 1898 | |
| 1899 | /* |
| 1900 | * Roll upward through the blocks, processing each leaf block in |
| 1901 | * order. As long as there is space in the result buffer, keep |
| 1902 | * adding the information. |
| 1903 | */ |
| 1904 | for (;;) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1905 | leaf = bp->b_addr; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1906 | error = xfs_attr3_leaf_list_int(bp, context); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 1907 | if (error) { |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1908 | xfs_trans_brelse(NULL, bp); |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 1909 | return error; |
| 1910 | } |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1911 | xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf); |
| 1912 | if (context->seen_enough || leafhdr.forw == 0) |
Tim Shimmin | 726801b | 2006-09-28 11:01:37 +1000 | [diff] [blame] | 1913 | break; |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1914 | cursor->blkno = leafhdr.forw; |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1915 | xfs_trans_brelse(NULL, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1916 | error = xfs_attr3_leaf_read(NULL, context->dp, cursor->blkno, -1, |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 1917 | &bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | if (error) |
Dave Chinner | ad14c33 | 2012-11-12 22:54:16 +1100 | [diff] [blame] | 1919 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | } |
Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 1921 | xfs_trans_brelse(NULL, bp); |
Dave Chinner | 517c222 | 2013-04-24 18:58:55 +1000 | [diff] [blame] | 1922 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | } |