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