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