blob: a14ab9b2669efdaaf58f563cf8b1927a54f2a56e [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100014#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110015#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110016#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110017#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100023#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110024#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_attr.h"
26#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110027#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000031#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * xfs_attr.c
35 *
36 * Provide the external interfaces to manage attribute lists.
37 */
38
39/*========================================================================
40 * Function prototypes for the kernel.
41 *========================================================================*/
42
43/*
44 * Internal routines when attribute list fits inside the inode.
45 */
46STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
47
48/*
49 * Internal routines when attribute list is one block.
50 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100051STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
53STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * Internal routines when attribute list is more than one block.
57 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100058STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
60STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
62STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Barry Naujoke8b0eba2008-04-22 17:34:31 +100065STATIC int
Christoph Hellwig67fd7182014-05-13 16:34:43 +100066xfs_attr_args_init(
67 struct xfs_da_args *args,
68 struct xfs_inode *dp,
69 const unsigned char *name,
70 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100071{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100072
73 if (!name)
Dave Chinner24513372014-06-25 14:58:08 +100074 return -EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100075
76 memset(args, 0, sizeof(*args));
Dave Chinner0650b552014-06-06 15:01:58 +100077 args->geo = dp->i_mount->m_attr_geo;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100078 args->whichfork = XFS_ATTR_FORK;
79 args->dp = dp;
80 args->flags = flags;
81 args->name = name;
82 args->namelen = strlen((const char *)name);
83 if (args->namelen >= MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +100084 return -EFAULT; /* match IRIX behaviour */
Barry Naujoke8b0eba2008-04-22 17:34:31 +100085
Christoph Hellwig67fd7182014-05-13 16:34:43 +100086 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100087 return 0;
88}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Dave Chinnerabec5f22013-08-12 20:49:38 +100090int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100091xfs_inode_hasattr(
92 struct xfs_inode *ip)
93{
94 if (!XFS_IFORK_Q(ip) ||
95 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
96 ip->i_d.di_anextents == 0))
97 return 0;
98 return 1;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*========================================================================
102 * Overall external interface routines.
103 *========================================================================*/
104
Christoph Hellwigcf69f822017-07-13 12:14:33 -0700105/* Retrieve an extended attribute and its value. Must have ilock. */
Darrick J. Wongad017f62017-06-16 11:00:14 -0700106int
107xfs_attr_get_ilocked(
108 struct xfs_inode *ip,
109 struct xfs_da_args *args)
110{
Christoph Hellwigcf69f822017-07-13 12:14:33 -0700111 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
112
Darrick J. Wongad017f62017-06-16 11:00:14 -0700113 if (!xfs_inode_hasattr(ip))
114 return -ENOATTR;
115 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
116 return xfs_attr_shortform_getvalue(args);
117 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
118 return xfs_attr_leaf_get(args);
119 else
120 return xfs_attr_node_get(args);
121}
122
123/* Retrieve an extended attribute by name, and its value. */
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000124int
125xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000126 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000127 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100128 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000129 int *valuelenp,
130 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000132 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000133 uint lock_mode;
134 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100136 XFS_STATS_INC(ip->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000139 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000141 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000142 if (error)
143 return error;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 args.value = value;
146 args.valuelen = *valuelenp;
Eric Sandeenc400ee32015-08-19 10:30:48 +1000147 /* Entirely possible to look up a name which doesn't exist */
148 args.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Christoph Hellwig683cb942013-12-06 12:30:15 -0800150 lock_mode = xfs_ilock_attr_map_shared(ip);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700151 error = xfs_attr_get_ilocked(ip, &args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800152 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000153
154 *valuelenp = args.valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000155 return error == -EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000158/*
159 * Calculate how many blocks we need for the new attribute,
160 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000161STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000162xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000163 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000164 int *local)
165{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000166 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000167 int size;
168 int nblks;
169
170 /*
171 * Determine space new attribute will use, and if it would be
172 * "local" or "remote" (note: local != inline).
173 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000174 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000175 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
176 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000177 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000178 /* Double split possible */
179 nblks *= 2;
180 }
181 } else {
182 /*
183 * Out of line attribute, cannot double split, but
184 * make room for the attribute value itself.
185 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000186 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000187 nblks += dblocks;
188 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
189 }
190
191 return nblks;
192}
193
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000194int
195xfs_attr_set(
196 struct xfs_inode *dp,
197 const unsigned char *name,
198 unsigned char *value,
199 int valuelen,
200 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000202 struct xfs_mount *mp = dp->i_mount;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800203 struct xfs_buf *leaf_bp = NULL;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000204 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000205 struct xfs_defer_ops dfops;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000206 struct xfs_trans_res tres;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000207 xfs_fsblock_t firstblock;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000208 int rsvd = (flags & ATTR_ROOT) != 0;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100209 int error, err2, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100211 XFS_STATS_INC(mp, xs_attr_set);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000212
213 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000214 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000215
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000216 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000217 if (error)
218 return error;
219
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000220 args.value = value;
221 args.valuelen = valuelen;
222 args.firstblock = &firstblock;
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000223 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000224 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000225
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700226 error = xfs_qm_dqattach(dp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200227 if (error)
228 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /*
231 * If the inode doesn't have an attribute fork, add one.
232 * (inode must not be locked when we call this routine)
233 */
234 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100235 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000236 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100237
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000238 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
239 if (error)
240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Christoph Hellwig253f4912016-04-06 09:19:55 +1000243 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
244 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
245 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
246 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /*
249 * Root fork attributes can use reserved data blocks for this
250 * operation if necessary
251 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000252 error = xfs_trans_alloc(mp, &tres, args.total, 0,
253 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
254 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000255 return error;
Brian Foster40d03ac2018-07-11 22:26:09 -0700256 xfs_defer_init(&dfops, &firstblock);
257 args.trans->t_dfops = &dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Christoph Hellwig253f4912016-04-06 09:19:55 +1000259 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200260 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000261 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
262 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (error) {
264 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000265 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000266 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
Christoph Hellwigddc34152011-09-19 15:00:54 +0000269 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000272 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * upgrade it to a single-leaf-block attribute list.
274 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000275 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
276 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
277 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /*
280 * Build initial attribute list (if required).
281 */
282 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100283 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
286 * Try to add the attr to the attribute list in
287 * the inode.
288 */
289 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000290 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /*
292 * Commit the shortform mods, and we're done.
293 * NOTE: this is also the error path (EEXIST, etc).
294 */
295 ASSERT(args.trans != NULL);
296
297 /*
298 * If this is a synchronous mount, make sure that
299 * the transaction goes to disk before returning
300 * to the user.
301 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000302 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000304
305 if (!error && (flags & ATTR_KERNOTIME) == 0) {
306 xfs_trans_ichgtime(args.trans, dp,
307 XFS_ICHGTIME_CHG);
308 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000309 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 xfs_iunlock(dp, XFS_ILOCK_EXCL);
311
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000312 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 /*
316 * It won't fit in the shortform, transform to a leaf block.
317 * GROT: another possible req'mt for a double-split btree op.
318 */
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800319 error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700320 if (error)
321 goto out_defer_cancel;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800322 /*
323 * Prevent the leaf buffer from being unlocked so that a
324 * concurrent AIL push cannot grab the half-baked leaf
325 * buffer and run into problems with the write verifier.
326 */
327 xfs_trans_bhold(args.trans, leaf_bp);
Brian Foster40d03ac2018-07-11 22:26:09 -0700328 xfs_defer_bjoin(&dfops, leaf_bp);
329 xfs_defer_ijoin(&dfops, dp);
330 error = xfs_defer_finish(&args.trans, &dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700331 if (error)
332 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * Commit the leaf transformation. We'll need another (linked)
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800336 * transaction to add the new attribute to the leaf, which
337 * means that we have to hold & join the leaf buffer here too.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700339 error = xfs_trans_roll_inode(&args.trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000340 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto out;
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800342 xfs_trans_bjoin(args.trans, leaf_bp);
343 leaf_bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000346 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 error = xfs_attr_leaf_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000348 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 error = xfs_attr_node_addname(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000350 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /*
354 * If this is a synchronous mount, make sure that the
355 * transaction goes to disk before returning to the user.
356 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000357 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000360 if ((flags & ATTR_KERNOTIME) == 0)
361 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /*
364 * Commit the last in the sequence of transactions.
365 */
366 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000367 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 xfs_iunlock(dp, XFS_ILOCK_EXCL);
369
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000370 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700372out_defer_cancel:
373 xfs_defer_cancel(&dfops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374out:
Darrick J. Wong6e643cd2017-12-07 19:07:02 -0800375 if (leaf_bp)
376 xfs_trans_brelse(args.trans, leaf_bp);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000377 if (args.trans)
378 xfs_trans_cancel(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000380 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100381}
382
383/*
384 * Generic handler routine to remove a name from an attribute list.
385 * Transitions attribute list from Btree to shortform as necessary.
386 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000387int
388xfs_attr_remove(
389 struct xfs_inode *dp,
390 const unsigned char *name,
391 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100392{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000393 struct xfs_mount *mp = dp->i_mount;
394 struct xfs_da_args args;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000395 struct xfs_defer_ops dfops;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000396 xfs_fsblock_t firstblock;
397 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100398
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100399 XFS_STATS_INC(mp, xs_attr_remove);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000400
401 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000402 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000403
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000404 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000405 if (error)
406 return error;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 args.firstblock = &firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000411 * we have no control over the attribute names that userspace passes us
412 * to remove, so we have to allow the name lookup prior to attribute
413 * removal to fail.
414 */
415 args.op_flags = XFS_DA_OP_OKNOENT;
416
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700417 error = xfs_qm_dqattach(dp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200418 if (error)
419 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * Root fork attributes can use reserved data blocks for this
423 * operation if necessary
424 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000425 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
426 XFS_ATTRRM_SPACE_RES(mp), 0,
427 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
428 &args.trans);
429 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000430 return error;
Brian Foster40d03ac2018-07-11 22:26:09 -0700431 xfs_defer_init(&dfops, &firstblock);
432 args.trans->t_dfops = &dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 xfs_ilock(dp, XFS_ILOCK_EXCL);
435 /*
436 * No need to make quota reservations here. We expect to release some
437 * blocks not allocate in the common case.
438 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000439 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000441 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000442 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000443 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
445 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
447 error = xfs_attr_leaf_removename(&args);
448 } else {
449 error = xfs_attr_node_removename(&args);
450 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000451
452 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /*
456 * If this is a synchronous mount, make sure that the
457 * transaction goes to disk before returning to the user.
458 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000459 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000462 if ((flags & ATTR_KERNOTIME) == 0)
463 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * Commit the last in the sequence of transactions.
467 */
468 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000469 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 xfs_iunlock(dp, XFS_ILOCK_EXCL);
471
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000475 if (args.trans)
476 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000477 xfs_iunlock(dp, XFS_ILOCK_EXCL);
478 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*========================================================================
482 * External routines when attribute list is inside the inode
483 *========================================================================*/
484
485/*
486 * Add a name to the shortform attribute list structure
487 * This is the external routine.
488 */
489STATIC int
490xfs_attr_shortform_addname(xfs_da_args_t *args)
491{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100492 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Dave Chinner5a5881c2012-03-22 05:15:13 +0000494 trace_xfs_attr_sf_addname(args);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000497 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000498 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000499 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000501 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700503 if (retval)
504 return retval;
505 /*
506 * Since we have removed the old attr, clear ATTR_REPLACE so
507 * that the leaf format add routine won't trip over the attr
508 * not being around.
509 */
510 args->flags &= ~ATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Nathan Scottd8cc8902005-11-02 10:34:53 +1100513 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
514 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000515 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
518 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100519
520 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
521 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000522 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100523
524 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528
529/*========================================================================
530 * External routines when attribute list is one block
531 *========================================================================*/
532
533/*
534 * Add a name to the leaf attribute list structure
535 *
536 * This leaf block cannot have a "remote" value, we only call this routine
537 * if bmap_one_block() says there is only one block (ie: no remote blks).
538 */
David Chinnera8272ce2007-11-23 16:28:09 +1100539STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700540xfs_attr_leaf_addname(
541 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700543 struct xfs_inode *dp;
544 struct xfs_buf *bp;
545 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Dave Chinner5a5881c2012-03-22 05:15:13 +0000547 trace_xfs_attr_leaf_addname(args);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
550 * Read the (only) block in the attribute list in.
551 */
552 dp = args->dp;
553 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000554 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100556 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /*
559 * Look up the given attribute in the leaf block. Figure out if
560 * the given flags produce an error or call for an atomic rename.
561 */
Dave Chinner517c2222013-04-24 18:58:55 +1000562 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000563 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000564 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000565 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000566 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000568 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000569 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000571
572 trace_xfs_attr_leaf_replace(args);
573
Dave Chinner8275cdd2014-05-06 07:37:31 +1000574 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000575 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 args->blkno2 = args->blkno; /* set 2nd entry info*/
577 args->index2 = args->index;
578 args->rmtblkno2 = args->rmtblkno;
579 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000580 args->rmtvaluelen2 = args->rmtvaluelen;
581
582 /*
583 * clear the remote attr state now that it is saved so that the
584 * values reflect the state of the attribute we are about to
585 * add, not the attribute we just found and will remove later.
586 */
587 args->rmtblkno = 0;
588 args->rmtblkcnt = 0;
589 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 /*
593 * Add the attribute to the leaf block, transitioning to a Btree
594 * if required.
595 */
Dave Chinner517c2222013-04-24 18:58:55 +1000596 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000597 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Promote the attribute list to the Btree format, then
600 * Commit that transaction so that the node_addname() call
601 * can manage its own transactions.
602 */
Brian Foster32a9b7c2018-07-11 22:26:11 -0700603 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000604 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700605 if (error)
606 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700607 xfs_defer_ijoin(args->trans->t_dfops, dp);
608 error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700609 if (error)
610 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * Commit the current trans (including the inode) and start
614 * a new one.
615 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700616 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000617 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000618 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /*
621 * Fob the whole rest of the problem off on the Btree code.
622 */
623 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000624 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 /*
628 * Commit the transaction that added the attr name so that
629 * later routines can manage their own transactions.
630 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700631 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000632 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000633 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /*
636 * If there was an out-of-line value, allocate the blocks we
637 * identified for its storage and copy the value. This is done
638 * after we create the attribute so that we don't overflow the
639 * maximum size of a transaction and/or hit a deadlock.
640 */
641 if (args->rmtblkno > 0) {
642 error = xfs_attr_rmtval_set(args);
643 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000644 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 /*
648 * If this is an atomic rename operation, we must "flip" the
649 * incomplete flags on the "new" and "old" attribute/value pairs
650 * so that one disappears and one appears atomically. Then we
651 * must remove the "old" attribute/value pair.
652 */
Barry Naujok6a178102008-05-21 16:42:05 +1000653 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * In a separate transaction, set the incomplete flag on the
656 * "old" attr and clear the incomplete flag on the "new" attr.
657 */
Dave Chinner517c2222013-04-24 18:58:55 +1000658 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000660 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /*
663 * Dismantle the "old" attribute/value pair by removing
664 * a "remote" value (if it exists).
665 */
666 args->index = args->index2;
667 args->blkno = args->blkno2;
668 args->rmtblkno = args->rmtblkno2;
669 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000670 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (args->rmtblkno) {
672 error = xfs_attr_rmtval_remove(args);
673 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000674 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 /*
678 * Read in the block containing the "old" attr, then
679 * remove the "old" attr from that block (neat, huh!)
680 */
Dave Chinner517c2222013-04-24 18:58:55 +1000681 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100682 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100684 return error;
685
Dave Chinner517c2222013-04-24 18:58:55 +1000686 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /*
689 * If the result is small enough, shrink it all into the inode.
690 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100691 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Brian Foster32a9b7c2018-07-11 22:26:11 -0700692 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000693 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700695 if (error)
696 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700697 xfs_defer_ijoin(args->trans->t_dfops, dp);
698 error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700699 if (error)
700 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /*
704 * Commit the remove and start the next trans in series.
705 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700706 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 } else if (args->rmtblkno > 0) {
709 /*
710 * Added a "remote" value, just clear the incomplete flag.
711 */
Dave Chinner517c2222013-04-24 18:58:55 +1000712 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Dave Chinner517c2222013-04-24 18:58:55 +1000714 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700715out_defer_cancel:
Brian Foster32a9b7c2018-07-11 22:26:11 -0700716 xfs_defer_cancel(args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700717 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720/*
721 * Remove a name from the leaf attribute list structure
722 *
723 * This leaf block cannot have a "remote" value, we only call this routine
724 * if bmap_one_block() says there is only one block (ie: no remote blks).
725 */
726STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700727xfs_attr_leaf_removename(
728 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700730 struct xfs_inode *dp;
731 struct xfs_buf *bp;
732 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Dave Chinner5a5881c2012-03-22 05:15:13 +0000734 trace_xfs_attr_leaf_removename(args);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /*
737 * Remove the attribute.
738 */
739 dp = args->dp;
740 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000741 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100742 if (error)
743 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Chinner517c2222013-04-24 18:58:55 +1000745 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000746 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000747 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000748 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
Dave Chinner517c2222013-04-24 18:58:55 +1000751 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /*
754 * If the result is small enough, shrink it all into the inode.
755 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100756 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Brian Foster32a9b7c2018-07-11 22:26:11 -0700757 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000758 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700760 if (error)
761 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700762 xfs_defer_ijoin(args->trans->t_dfops, dp);
763 error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700764 if (error)
765 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000766 }
Dave Chinner517c2222013-04-24 18:58:55 +1000767 return 0;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700768out_defer_cancel:
Brian Foster32a9b7c2018-07-11 22:26:11 -0700769 xfs_defer_cancel(args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700770 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
774 * Look up a name in a leaf attribute list structure.
775 *
776 * This leaf block cannot have a "remote" value, we only call this routine
777 * if bmap_one_block() says there is only one block (ie: no remote blks).
778 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000779STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780xfs_attr_leaf_get(xfs_da_args_t *args)
781{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000782 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int error;
784
Dave Chinneree732592012-11-12 22:53:53 +1100785 trace_xfs_attr_leaf_get(args);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000788 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100790 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Dave Chinner517c2222013-04-24 18:58:55 +1000792 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000793 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000794 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000795 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Dave Chinner517c2222013-04-24 18:58:55 +1000797 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000798 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
800 error = xfs_attr_rmtval_get(args);
801 }
Dave Chinner517c2222013-04-24 18:58:55 +1000802 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000806 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *========================================================================*/
808
809/*
810 * Add a name to a Btree-format attribute list.
811 *
812 * This will involve walking down the Btree, and may involve splitting
813 * leaf nodes and even splitting intermediate nodes up to and including
814 * the root node (a special case of an intermediate node).
815 *
816 * "Remote" attribute values confuse the issue and atomic rename operations
817 * add a whole extra layer of confusion on top of that.
818 */
819STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700820xfs_attr_node_addname(
821 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700823 struct xfs_da_state *state;
824 struct xfs_da_state_blk *blk;
825 struct xfs_inode *dp;
826 struct xfs_mount *mp;
827 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Chinner5a5881c2012-03-22 05:15:13 +0000829 trace_xfs_attr_node_addname(args);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /*
832 * Fill in bucket of arguments/results/context to carry around.
833 */
834 dp = args->dp;
835 mp = dp->i_mount;
836restart:
837 state = xfs_da_state_alloc();
838 state->args = args;
839 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /*
842 * Search to see if name already exists, and get back a pointer
843 * to where it should go.
844 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000845 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (error)
847 goto out;
848 blk = &state->path.blk[ state->path.active-1 ];
849 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000850 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000852 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (args->flags & ATTR_CREATE)
854 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000855
856 trace_xfs_attr_node_replace(args);
857
Dave Chinner8275cdd2014-05-06 07:37:31 +1000858 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000859 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 args->blkno2 = args->blkno; /* set 2nd entry info*/
861 args->index2 = args->index;
862 args->rmtblkno2 = args->rmtblkno;
863 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000864 args->rmtvaluelen2 = args->rmtvaluelen;
865
866 /*
867 * clear the remote attr state now that it is saved so that the
868 * values reflect the state of the attribute we are about to
869 * add, not the attribute we just found and will remove later.
870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 args->rmtblkno = 0;
872 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000873 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Dave Chinner517c2222013-04-24 18:58:55 +1000876 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000877 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (state->path.active == 1) {
879 /*
880 * Its really a single leaf node, but it had
881 * out-of-line values so it looked like it *might*
882 * have been a b-tree.
883 */
884 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500885 state = NULL;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700886 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000887 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700888 if (error)
889 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700890 xfs_defer_ijoin(args->trans->t_dfops, dp);
891 error = xfs_defer_finish(&args->trans,
892 args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700893 if (error)
894 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * Commit the node conversion and start the next
898 * trans in the chain.
899 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700900 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000901 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto out;
903
904 goto restart;
905 }
906
907 /*
908 * Split as many Btree elements as required.
909 * This code tracks the new and old attr's location
910 * in the index/blkno/rmtblkno/rmtblkcnt fields and
911 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
912 */
Brian Foster32a9b7c2018-07-11 22:26:11 -0700913 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000914 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700915 if (error)
916 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700917 xfs_defer_ijoin(args->trans->t_dfops, dp);
918 error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700919 if (error)
920 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 } else {
922 /*
923 * Addition succeeded, update Btree hashvals.
924 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000925 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927
928 /*
929 * Kill the state structure, we're done with it and need to
930 * allow the buffers to come back later.
931 */
932 xfs_da_state_free(state);
933 state = NULL;
934
935 /*
936 * Commit the leaf addition or btree split and start the next
937 * trans in the chain.
938 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700939 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000940 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto out;
942
943 /*
944 * If there was an out-of-line value, allocate the blocks we
945 * identified for its storage and copy the value. This is done
946 * after we create the attribute so that we don't overflow the
947 * maximum size of a transaction and/or hit a deadlock.
948 */
949 if (args->rmtblkno > 0) {
950 error = xfs_attr_rmtval_set(args);
951 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000952 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
955 /*
956 * If this is an atomic rename operation, we must "flip" the
957 * incomplete flags on the "new" and "old" attribute/value pairs
958 * so that one disappears and one appears atomically. Then we
959 * must remove the "old" attribute/value pair.
960 */
Barry Naujok6a178102008-05-21 16:42:05 +1000961 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /*
963 * In a separate transaction, set the incomplete flag on the
964 * "old" attr and clear the incomplete flag on the "new" attr.
965 */
Dave Chinner517c2222013-04-24 18:58:55 +1000966 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (error)
968 goto out;
969
970 /*
971 * Dismantle the "old" attribute/value pair by removing
972 * a "remote" value (if it exists).
973 */
974 args->index = args->index2;
975 args->blkno = args->blkno2;
976 args->rmtblkno = args->rmtblkno2;
977 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000978 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (args->rmtblkno) {
980 error = xfs_attr_rmtval_remove(args);
981 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000982 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
985 /*
986 * Re-find the "old" attribute entry after any split ops.
987 * The INCOMPLETE flag means that we will find the "old"
988 * attr, not the "new" one.
989 */
990 args->flags |= XFS_ATTR_INCOMPLETE;
991 state = xfs_da_state_alloc();
992 state->args = args;
993 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000995 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (error)
997 goto out;
998
999 /*
1000 * Remove the name and update the hashvals in the tree.
1001 */
1002 blk = &state->path.blk[ state->path.active-1 ];
1003 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001004 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001005 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
1008 * Check to see if the tree needs to be collapsed.
1009 */
1010 if (retval && (state->path.active > 1)) {
Brian Foster32a9b7c2018-07-11 22:26:11 -07001011 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001012 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001013 if (error)
1014 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -07001015 xfs_defer_ijoin(args->trans->t_dfops, dp);
1016 error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001017 if (error)
1018 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
1021 /*
1022 * Commit and start the next trans in the chain.
1023 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001024 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001025 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 goto out;
1027
1028 } else if (args->rmtblkno > 0) {
1029 /*
1030 * Added a "remote" value, just clear the incomplete flag.
1031 */
Dave Chinner517c2222013-04-24 18:58:55 +10001032 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (error)
1034 goto out;
1035 }
1036 retval = error = 0;
1037
1038out:
1039 if (state)
1040 xfs_da_state_free(state);
1041 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001042 return error;
1043 return retval;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001044out_defer_cancel:
Brian Foster32a9b7c2018-07-11 22:26:11 -07001045 xfs_defer_cancel(args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049/*
1050 * Remove a name from a B-tree attribute list.
1051 *
1052 * This will involve walking down the Btree, and may involve joining
1053 * leaf nodes and even joining intermediate nodes up to and including
1054 * the root node (a special case of an intermediate node).
1055 */
1056STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -07001057xfs_attr_node_removename(
1058 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Brian Foster32a9b7c2018-07-11 22:26:11 -07001060 struct xfs_da_state *state;
1061 struct xfs_da_state_blk *blk;
1062 struct xfs_inode *dp;
1063 struct xfs_buf *bp;
1064 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dave Chinner5a5881c2012-03-22 05:15:13 +00001066 trace_xfs_attr_node_removename(args);
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * Tie a string around our finger to remind us where we are.
1070 */
1071 dp = args->dp;
1072 state = xfs_da_state_alloc();
1073 state->args = args;
1074 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /*
1077 * Search to see if name exists, and get back a pointer to it.
1078 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001079 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001080 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (error == 0)
1082 error = retval;
1083 goto out;
1084 }
1085
1086 /*
1087 * If there is an out-of-line value, de-allocate the blocks.
1088 * This is done before we remove the attribute so that we don't
1089 * overflow the maximum size of a transaction and/or hit a deadlock.
1090 */
1091 blk = &state->path.blk[ state->path.active-1 ];
1092 ASSERT(blk->bp != NULL);
1093 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1094 if (args->rmtblkno > 0) {
1095 /*
1096 * Fill in disk block numbers in the state structure
1097 * so that we can get the buffers back after we commit
1098 * several transactions in the following calls.
1099 */
1100 error = xfs_attr_fillstate(state);
1101 if (error)
1102 goto out;
1103
1104 /*
1105 * Mark the attribute as INCOMPLETE, then bunmapi() the
1106 * remote value.
1107 */
Dave Chinner517c2222013-04-24 18:58:55 +10001108 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (error)
1110 goto out;
1111 error = xfs_attr_rmtval_remove(args);
1112 if (error)
1113 goto out;
1114
1115 /*
1116 * Refill the state structure with buffers, the prior calls
1117 * released our buffers.
1118 */
1119 error = xfs_attr_refillstate(state);
1120 if (error)
1121 goto out;
1122 }
1123
1124 /*
1125 * Remove the name and update the hashvals in the tree.
1126 */
1127 blk = &state->path.blk[ state->path.active-1 ];
1128 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001129 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001130 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /*
1133 * Check to see if the tree needs to be collapsed.
1134 */
1135 if (retval && (state->path.active > 1)) {
Brian Foster32a9b7c2018-07-11 22:26:11 -07001136 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001137 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001138 if (error)
1139 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -07001140 xfs_defer_ijoin(args->trans->t_dfops, dp);
1141 error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001142 if (error)
1143 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
1145 * Commit the Btree join operation and start a new trans.
1146 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001147 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001148 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 goto out;
1150 }
1151
1152 /*
1153 * If the result is small enough, push it all into the inode.
1154 */
1155 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1156 /*
1157 * Have to get rid of the copy of this dabuf in the state.
1158 */
1159 ASSERT(state->path.active == 1);
1160 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 state->path.blk[0].bp = NULL;
1162
Dave Chinner517c2222013-04-24 18:58:55 +10001163 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (error)
1165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Nathan Scottd8cc8902005-11-02 10:34:53 +11001167 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Brian Foster32a9b7c2018-07-11 22:26:11 -07001168 xfs_defer_init(args->trans->t_dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001169 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001171 if (error)
1172 goto out_defer_cancel;
Brian Foster32a9b7c2018-07-11 22:26:11 -07001173 xfs_defer_ijoin(args->trans->t_dfops, dp);
1174 error = xfs_defer_finish(&args->trans, args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001175 if (error)
1176 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001178 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 error = 0;
1181
1182out:
1183 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001184 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001185out_defer_cancel:
Brian Foster32a9b7c2018-07-11 22:26:11 -07001186 xfs_defer_cancel(args->trans->t_dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190/*
1191 * Fill in the disk block numbers in the state structure for the buffers
1192 * that are attached to the state structure.
1193 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001194 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
1196STATIC int
1197xfs_attr_fillstate(xfs_da_state_t *state)
1198{
1199 xfs_da_state_path_t *path;
1200 xfs_da_state_blk_t *blk;
1201 int level;
1202
Dave Chinneree732592012-11-12 22:53:53 +11001203 trace_xfs_attr_fillstate(state->args);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /*
1206 * Roll down the "path" in the state structure, storing the on-disk
1207 * block number for those buffers in the "path".
1208 */
1209 path = &state->path;
1210 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1211 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1212 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001213 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 blk->bp = NULL;
1215 } else {
1216 blk->disk_blkno = 0;
1217 }
1218 }
1219
1220 /*
1221 * Roll down the "altpath" in the state structure, storing the on-disk
1222 * block number for those buffers in the "altpath".
1223 */
1224 path = &state->altpath;
1225 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1226 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1227 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001228 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 blk->bp = NULL;
1230 } else {
1231 blk->disk_blkno = 0;
1232 }
1233 }
1234
Eric Sandeend99831f2014-06-22 15:03:54 +10001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238/*
1239 * Reattach the buffers to the state structure based on the disk block
1240 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001241 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * buffers from our grip.
1243 */
1244STATIC int
1245xfs_attr_refillstate(xfs_da_state_t *state)
1246{
1247 xfs_da_state_path_t *path;
1248 xfs_da_state_blk_t *blk;
1249 int level, error;
1250
Dave Chinneree732592012-11-12 22:53:53 +11001251 trace_xfs_attr_refillstate(state->args);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * Roll down the "path" in the state structure, storing the on-disk
1255 * block number for those buffers in the "path".
1256 */
1257 path = &state->path;
1258 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1259 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1260 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001261 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 state->args->dp,
1263 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001264 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001266 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 } else {
1268 blk->bp = NULL;
1269 }
1270 }
1271
1272 /*
1273 * Roll down the "altpath" in the state structure, storing the on-disk
1274 * block number for those buffers in the "altpath".
1275 */
1276 path = &state->altpath;
1277 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1278 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1279 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001280 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 state->args->dp,
1282 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001283 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001285 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } else {
1287 blk->bp = NULL;
1288 }
1289 }
1290
Eric Sandeend99831f2014-06-22 15:03:54 +10001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/*
1295 * Look up a filename in a node attribute list.
1296 *
1297 * This routine gets called for any attribute fork that has more than one
1298 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1299 * "remote" values taking up more blocks.
1300 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001301STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302xfs_attr_node_get(xfs_da_args_t *args)
1303{
1304 xfs_da_state_t *state;
1305 xfs_da_state_blk_t *blk;
1306 int error, retval;
1307 int i;
1308
Dave Chinneree732592012-11-12 22:53:53 +11001309 trace_xfs_attr_node_get(args);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 state = xfs_da_state_alloc();
1312 state->args = args;
1313 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /*
1316 * Search to see if name exists, and get back a pointer to it.
1317 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001318 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (error) {
1320 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001321 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 blk = &state->path.blk[ state->path.active-1 ];
1323 ASSERT(blk->bp != NULL);
1324 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1325
1326 /*
1327 * Get the value, local or "remote"
1328 */
Dave Chinner517c2222013-04-24 18:58:55 +10001329 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (!retval && (args->rmtblkno > 0)
1331 && !(args->flags & ATTR_KERNOVAL)) {
1332 retval = xfs_attr_rmtval_get(args);
1333 }
1334 }
1335
1336 /*
1337 * If not in a transaction, we have to release all the buffers.
1338 */
1339 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001340 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 state->path.blk[i].bp = NULL;
1342 }
1343
1344 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001345 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}