blob: 99590f61d624be66bef0248c93ba1e8a806b4215 [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;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000223 args.dfops = &dfops;
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000224 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000225 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000226
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700227 error = xfs_qm_dqattach(dp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200228 if (error)
229 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /*
232 * If the inode doesn't have an attribute fork, add one.
233 * (inode must not be locked when we call this routine)
234 */
235 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100236 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000237 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100238
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000239 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
240 if (error)
241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Christoph Hellwig253f4912016-04-06 09:19:55 +1000244 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
245 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
246 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
247 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /*
250 * Root fork attributes can use reserved data blocks for this
251 * operation if necessary
252 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000253 error = xfs_trans_alloc(mp, &tres, args.total, 0,
254 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
255 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000256 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Christoph Hellwig253f4912016-04-06 09:19:55 +1000258 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200259 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000260 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
261 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (error) {
263 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000264 xfs_trans_cancel(args.trans);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000265 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Christoph Hellwigddc34152011-09-19 15:00:54 +0000268 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000271 * If the attribute list is non-existent or a shortform list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * upgrade it to a single-leaf-block attribute list.
273 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000274 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
275 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
276 dp->i_d.di_anextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /*
279 * Build initial attribute list (if required).
280 */
281 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
Nathan Scottd8cc8902005-11-02 10:34:53 +1100282 xfs_attr_shortform_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /*
285 * Try to add the attr to the attribute list in
286 * the inode.
287 */
288 error = xfs_attr_shortform_addname(&args);
Dave Chinner24513372014-06-25 14:58:08 +1000289 if (error != -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /*
291 * Commit the shortform mods, and we're done.
292 * NOTE: this is also the error path (EEXIST, etc).
293 */
294 ASSERT(args.trans != NULL);
295
296 /*
297 * If this is a synchronous mount, make sure that
298 * the transaction goes to disk before returning
299 * to the user.
300 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000301 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_trans_set_sync(args.trans);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000303
304 if (!error && (flags & ATTR_KERNOTIME) == 0) {
305 xfs_trans_ichgtime(args.trans, dp,
306 XFS_ICHGTIME_CHG);
307 }
Christoph Hellwig70393312015-06-04 13:48:08 +1000308 err2 = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 xfs_iunlock(dp, XFS_ILOCK_EXCL);
310
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000311 return error ? error : err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 /*
315 * It won't fit in the shortform, transform to a leaf block.
316 * GROT: another possible req'mt for a double-split btree op.
317 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000318 xfs_defer_init(args.dfops, args.firstblock);
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);
328 xfs_defer_bjoin(args.dfops, leaf_bp);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700329 xfs_defer_ijoin(args.dfops, dp);
330 error = xfs_defer_finish(&args.trans, args.dfops);
331 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;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000409 args.dfops = &dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000412 * we have no control over the attribute names that userspace passes us
413 * to remove, so we have to allow the name lookup prior to attribute
414 * removal to fail.
415 */
416 args.op_flags = XFS_DA_OP_OKNOENT;
417
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700418 error = xfs_qm_dqattach(dp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200419 if (error)
420 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * Root fork attributes can use reserved data blocks for this
424 * operation if necessary
425 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000426 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
427 XFS_ATTRRM_SPACE_RES(mp), 0,
428 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
429 &args.trans);
430 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000431 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 xfs_ilock(dp, XFS_ILOCK_EXCL);
434 /*
435 * No need to make quota reservations here. We expect to release some
436 * blocks not allocate in the common case.
437 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000438 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +1000440 if (!xfs_inode_hasattr(dp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000441 error = -ENOATTR;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000442 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
444 error = xfs_attr_shortform_remove(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
446 error = xfs_attr_leaf_removename(&args);
447 } else {
448 error = xfs_attr_node_removename(&args);
449 }
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000450
451 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /*
455 * If this is a synchronous mount, make sure that the
456 * transaction goes to disk before returning to the user.
457 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000458 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000461 if ((flags & ATTR_KERNOTIME) == 0)
462 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * Commit the last in the sequence of transactions.
466 */
467 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000468 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_iunlock(dp, XFS_ILOCK_EXCL);
470
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000471 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000474 if (args.trans)
475 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000476 xfs_iunlock(dp, XFS_ILOCK_EXCL);
477 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*========================================================================
481 * External routines when attribute list is inside the inode
482 *========================================================================*/
483
484/*
485 * Add a name to the shortform attribute list structure
486 * This is the external routine.
487 */
488STATIC int
489xfs_attr_shortform_addname(xfs_da_args_t *args)
490{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100491 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Dave Chinner5a5881c2012-03-22 05:15:13 +0000493 trace_xfs_attr_sf_addname(args);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000496 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000497 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000498 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000500 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700502 if (retval)
503 return retval;
504 /*
505 * Since we have removed the old attr, clear ATTR_REPLACE so
506 * that the leaf format add routine won't trip over the attr
507 * not being around.
508 */
509 args->flags &= ~ATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Nathan Scottd8cc8902005-11-02 10:34:53 +1100512 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
513 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000514 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
517 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100518
519 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
520 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000521 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100522
523 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527
528/*========================================================================
529 * External routines when attribute list is one block
530 *========================================================================*/
531
532/*
533 * Add a name to the leaf attribute list structure
534 *
535 * This leaf block cannot have a "remote" value, we only call this routine
536 * if bmap_one_block() says there is only one block (ie: no remote blks).
537 */
David Chinnera8272ce2007-11-23 16:28:09 +1100538STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539xfs_attr_leaf_addname(xfs_da_args_t *args)
540{
541 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000542 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100543 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Chinner5a5881c2012-03-22 05:15:13 +0000545 trace_xfs_attr_leaf_addname(args);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /*
548 * Read the (only) block in the attribute list in.
549 */
550 dp = args->dp;
551 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000552 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100554 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /*
557 * Look up the given attribute in the leaf block. Figure out if
558 * the given flags produce an error or call for an atomic rename.
559 */
Dave Chinner517c2222013-04-24 18:58:55 +1000560 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000561 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000562 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000563 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000564 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000566 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000567 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000569
570 trace_xfs_attr_leaf_replace(args);
571
Dave Chinner8275cdd2014-05-06 07:37:31 +1000572 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000573 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 args->blkno2 = args->blkno; /* set 2nd entry info*/
575 args->index2 = args->index;
576 args->rmtblkno2 = args->rmtblkno;
577 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000578 args->rmtvaluelen2 = args->rmtvaluelen;
579
580 /*
581 * clear the remote attr state now that it is saved so that the
582 * values reflect the state of the attribute we are about to
583 * add, not the attribute we just found and will remove later.
584 */
585 args->rmtblkno = 0;
586 args->rmtblkcnt = 0;
587 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
590 /*
591 * Add the attribute to the leaf block, transitioning to a Btree
592 * if required.
593 */
Dave Chinner517c2222013-04-24 18:58:55 +1000594 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000595 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Promote the attribute list to the Btree format, then
598 * Commit that transaction so that the node_addname() call
599 * can manage its own transactions.
600 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000601 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000602 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700603 if (error)
604 goto out_defer_cancel;
605 xfs_defer_ijoin(args->dfops, dp);
606 error = xfs_defer_finish(&args->trans, args->dfops);
607 if (error)
608 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * Commit the current trans (including the inode) and start
612 * a new one.
613 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700614 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000615 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000616 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /*
619 * Fob the whole rest of the problem off on the Btree code.
620 */
621 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000622 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 /*
626 * Commit the transaction that added the attr name so that
627 * later routines can manage their own transactions.
628 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700629 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000630 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000631 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /*
634 * If there was an out-of-line value, allocate the blocks we
635 * identified for its storage and copy the value. This is done
636 * after we create the attribute so that we don't overflow the
637 * maximum size of a transaction and/or hit a deadlock.
638 */
639 if (args->rmtblkno > 0) {
640 error = xfs_attr_rmtval_set(args);
641 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000642 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 /*
646 * If this is an atomic rename operation, we must "flip" the
647 * incomplete flags on the "new" and "old" attribute/value pairs
648 * so that one disappears and one appears atomically. Then we
649 * must remove the "old" attribute/value pair.
650 */
Barry Naujok6a178102008-05-21 16:42:05 +1000651 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * In a separate transaction, set the incomplete flag on the
654 * "old" attr and clear the incomplete flag on the "new" attr.
655 */
Dave Chinner517c2222013-04-24 18:58:55 +1000656 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000658 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /*
661 * Dismantle the "old" attribute/value pair by removing
662 * a "remote" value (if it exists).
663 */
664 args->index = args->index2;
665 args->blkno = args->blkno2;
666 args->rmtblkno = args->rmtblkno2;
667 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000668 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (args->rmtblkno) {
670 error = xfs_attr_rmtval_remove(args);
671 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000672 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 /*
676 * Read in the block containing the "old" attr, then
677 * remove the "old" attr from that block (neat, huh!)
678 */
Dave Chinner517c2222013-04-24 18:58:55 +1000679 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100680 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100682 return error;
683
Dave Chinner517c2222013-04-24 18:58:55 +1000684 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /*
687 * If the result is small enough, shrink it all into the inode.
688 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100689 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000690 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000691 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700693 if (error)
694 goto out_defer_cancel;
695 xfs_defer_ijoin(args->dfops, dp);
696 error = xfs_defer_finish(&args->trans, args->dfops);
697 if (error)
698 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /*
702 * Commit the remove and start the next trans in series.
703 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700704 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 } else if (args->rmtblkno > 0) {
707 /*
708 * Added a "remote" value, just clear the incomplete flag.
709 */
Dave Chinner517c2222013-04-24 18:58:55 +1000710 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Dave Chinner517c2222013-04-24 18:58:55 +1000712 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700713out_defer_cancel:
714 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700715 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/*
719 * Remove a name from the leaf attribute list structure
720 *
721 * This leaf block cannot have a "remote" value, we only call this routine
722 * if bmap_one_block() says there is only one block (ie: no remote blks).
723 */
724STATIC int
725xfs_attr_leaf_removename(xfs_da_args_t *args)
726{
727 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000728 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100729 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dave Chinner5a5881c2012-03-22 05:15:13 +0000731 trace_xfs_attr_leaf_removename(args);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Remove the attribute.
735 */
736 dp = args->dp;
737 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000738 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100739 if (error)
740 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Dave Chinner517c2222013-04-24 18:58:55 +1000742 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000743 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000744 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000745 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Dave Chinner517c2222013-04-24 18:58:55 +1000748 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /*
751 * If the result is small enough, shrink it all into the inode.
752 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100753 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000754 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000755 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700757 if (error)
758 goto out_defer_cancel;
759 xfs_defer_ijoin(args->dfops, dp);
760 error = xfs_defer_finish(&args->trans, args->dfops);
761 if (error)
762 goto out_defer_cancel;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000763 }
Dave Chinner517c2222013-04-24 18:58:55 +1000764 return 0;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700765out_defer_cancel:
766 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700767 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/*
771 * Look up a name in a leaf attribute list structure.
772 *
773 * This leaf block cannot have a "remote" value, we only call this routine
774 * if bmap_one_block() says there is only one block (ie: no remote blks).
775 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000776STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777xfs_attr_leaf_get(xfs_da_args_t *args)
778{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000779 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int error;
781
Dave Chinneree732592012-11-12 22:53:53 +1100782 trace_xfs_attr_leaf_get(args);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000785 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100787 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Dave Chinner517c2222013-04-24 18:58:55 +1000789 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000790 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000791 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000792 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Dave Chinner517c2222013-04-24 18:58:55 +1000794 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000795 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
797 error = xfs_attr_rmtval_get(args);
798 }
Dave Chinner517c2222013-04-24 18:58:55 +1000799 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000803 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *========================================================================*/
805
806/*
807 * Add a name to a Btree-format attribute list.
808 *
809 * This will involve walking down the Btree, and may involve splitting
810 * leaf nodes and even splitting intermediate nodes up to and including
811 * the root node (a special case of an intermediate node).
812 *
813 * "Remote" attribute values confuse the issue and atomic rename operations
814 * add a whole extra layer of confusion on top of that.
815 */
816STATIC int
817xfs_attr_node_addname(xfs_da_args_t *args)
818{
819 xfs_da_state_t *state;
820 xfs_da_state_blk_t *blk;
821 xfs_inode_t *dp;
822 xfs_mount_t *mp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100823 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Dave Chinner5a5881c2012-03-22 05:15:13 +0000825 trace_xfs_attr_node_addname(args);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * Fill in bucket of arguments/results/context to carry around.
829 */
830 dp = args->dp;
831 mp = dp->i_mount;
832restart:
833 state = xfs_da_state_alloc();
834 state->args = args;
835 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /*
838 * Search to see if name already exists, and get back a pointer
839 * to where it should go.
840 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000841 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (error)
843 goto out;
844 blk = &state->path.blk[ state->path.active-1 ];
845 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000846 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000848 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (args->flags & ATTR_CREATE)
850 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000851
852 trace_xfs_attr_node_replace(args);
853
Dave Chinner8275cdd2014-05-06 07:37:31 +1000854 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000855 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 args->blkno2 = args->blkno; /* set 2nd entry info*/
857 args->index2 = args->index;
858 args->rmtblkno2 = args->rmtblkno;
859 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000860 args->rmtvaluelen2 = args->rmtvaluelen;
861
862 /*
863 * clear the remote attr state now that it is saved so that the
864 * values reflect the state of the attribute we are about to
865 * add, not the attribute we just found and will remove later.
866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 args->rmtblkno = 0;
868 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000869 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Dave Chinner517c2222013-04-24 18:58:55 +1000872 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000873 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (state->path.active == 1) {
875 /*
876 * Its really a single leaf node, but it had
877 * out-of-line values so it looked like it *might*
878 * have been a b-tree.
879 */
880 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500881 state = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000882 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +1000883 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700884 if (error)
885 goto out_defer_cancel;
886 xfs_defer_ijoin(args->dfops, dp);
887 error = xfs_defer_finish(&args->trans, args->dfops);
888 if (error)
889 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * Commit the node conversion and start the next
893 * trans in the chain.
894 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700895 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000896 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto out;
898
899 goto restart;
900 }
901
902 /*
903 * Split as many Btree elements as required.
904 * This code tracks the new and old attr's location
905 * in the index/blkno/rmtblkno/rmtblkcnt fields and
906 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
907 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000908 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000909 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700910 if (error)
911 goto out_defer_cancel;
912 xfs_defer_ijoin(args->dfops, dp);
913 error = xfs_defer_finish(&args->trans, args->dfops);
914 if (error)
915 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 } else {
917 /*
918 * Addition succeeded, update Btree hashvals.
919 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000920 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
923 /*
924 * Kill the state structure, we're done with it and need to
925 * allow the buffers to come back later.
926 */
927 xfs_da_state_free(state);
928 state = NULL;
929
930 /*
931 * Commit the leaf addition or btree split and start the next
932 * trans in the chain.
933 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700934 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000935 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 goto out;
937
938 /*
939 * If there was an out-of-line value, allocate the blocks we
940 * identified for its storage and copy the value. This is done
941 * after we create the attribute so that we don't overflow the
942 * maximum size of a transaction and/or hit a deadlock.
943 */
944 if (args->rmtblkno > 0) {
945 error = xfs_attr_rmtval_set(args);
946 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000947 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950 /*
951 * If this is an atomic rename operation, we must "flip" the
952 * incomplete flags on the "new" and "old" attribute/value pairs
953 * so that one disappears and one appears atomically. Then we
954 * must remove the "old" attribute/value pair.
955 */
Barry Naujok6a178102008-05-21 16:42:05 +1000956 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
958 * In a separate transaction, set the incomplete flag on the
959 * "old" attr and clear the incomplete flag on the "new" attr.
960 */
Dave Chinner517c2222013-04-24 18:58:55 +1000961 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (error)
963 goto out;
964
965 /*
966 * Dismantle the "old" attribute/value pair by removing
967 * a "remote" value (if it exists).
968 */
969 args->index = args->index2;
970 args->blkno = args->blkno2;
971 args->rmtblkno = args->rmtblkno2;
972 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000973 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (args->rmtblkno) {
975 error = xfs_attr_rmtval_remove(args);
976 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000977 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
980 /*
981 * Re-find the "old" attribute entry after any split ops.
982 * The INCOMPLETE flag means that we will find the "old"
983 * attr, not the "new" one.
984 */
985 args->flags |= XFS_ATTR_INCOMPLETE;
986 state = xfs_da_state_alloc();
987 state->args = args;
988 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000990 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (error)
992 goto out;
993
994 /*
995 * Remove the name and update the hashvals in the tree.
996 */
997 blk = &state->path.blk[ state->path.active-1 ];
998 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000999 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001000 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
1003 * Check to see if the tree needs to be collapsed.
1004 */
1005 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001006 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001007 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001008 if (error)
1009 goto out_defer_cancel;
1010 xfs_defer_ijoin(args->dfops, dp);
1011 error = xfs_defer_finish(&args->trans, args->dfops);
1012 if (error)
1013 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
1016 /*
1017 * Commit and start the next trans in the chain.
1018 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001019 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001020 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto out;
1022
1023 } else if (args->rmtblkno > 0) {
1024 /*
1025 * Added a "remote" value, just clear the incomplete flag.
1026 */
Dave Chinner517c2222013-04-24 18:58:55 +10001027 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (error)
1029 goto out;
1030 }
1031 retval = error = 0;
1032
1033out:
1034 if (state)
1035 xfs_da_state_free(state);
1036 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001037 return error;
1038 return retval;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001039out_defer_cancel:
1040 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/*
1045 * Remove a name from a B-tree attribute list.
1046 *
1047 * This will involve walking down the Btree, and may involve joining
1048 * leaf nodes and even joining intermediate nodes up to and including
1049 * the root node (a special case of an intermediate node).
1050 */
1051STATIC int
1052xfs_attr_node_removename(xfs_da_args_t *args)
1053{
1054 xfs_da_state_t *state;
1055 xfs_da_state_blk_t *blk;
1056 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001057 struct xfs_buf *bp;
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001058 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Dave Chinner5a5881c2012-03-22 05:15:13 +00001060 trace_xfs_attr_node_removename(args);
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /*
1063 * Tie a string around our finger to remind us where we are.
1064 */
1065 dp = args->dp;
1066 state = xfs_da_state_alloc();
1067 state->args = args;
1068 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 /*
1071 * Search to see if name exists, and get back a pointer to it.
1072 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001073 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001074 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (error == 0)
1076 error = retval;
1077 goto out;
1078 }
1079
1080 /*
1081 * If there is an out-of-line value, de-allocate the blocks.
1082 * This is done before we remove the attribute so that we don't
1083 * overflow the maximum size of a transaction and/or hit a deadlock.
1084 */
1085 blk = &state->path.blk[ state->path.active-1 ];
1086 ASSERT(blk->bp != NULL);
1087 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1088 if (args->rmtblkno > 0) {
1089 /*
1090 * Fill in disk block numbers in the state structure
1091 * so that we can get the buffers back after we commit
1092 * several transactions in the following calls.
1093 */
1094 error = xfs_attr_fillstate(state);
1095 if (error)
1096 goto out;
1097
1098 /*
1099 * Mark the attribute as INCOMPLETE, then bunmapi() the
1100 * remote value.
1101 */
Dave Chinner517c2222013-04-24 18:58:55 +10001102 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (error)
1104 goto out;
1105 error = xfs_attr_rmtval_remove(args);
1106 if (error)
1107 goto out;
1108
1109 /*
1110 * Refill the state structure with buffers, the prior calls
1111 * released our buffers.
1112 */
1113 error = xfs_attr_refillstate(state);
1114 if (error)
1115 goto out;
1116 }
1117
1118 /*
1119 * Remove the name and update the hashvals in the tree.
1120 */
1121 blk = &state->path.blk[ state->path.active-1 ];
1122 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001123 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001124 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * Check to see if the tree needs to be collapsed.
1128 */
1129 if (retval && (state->path.active > 1)) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001130 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001131 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001132 if (error)
1133 goto out_defer_cancel;
1134 xfs_defer_ijoin(args->dfops, dp);
1135 error = xfs_defer_finish(&args->trans, args->dfops);
1136 if (error)
1137 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /*
1139 * Commit the Btree join operation and start a new trans.
1140 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001141 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001142 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto out;
1144 }
1145
1146 /*
1147 * If the result is small enough, push it all into the inode.
1148 */
1149 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1150 /*
1151 * Have to get rid of the copy of this dabuf in the state.
1152 */
1153 ASSERT(state->path.active == 1);
1154 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 state->path.blk[0].bp = NULL;
1156
Dave Chinner517c2222013-04-24 18:58:55 +10001157 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (error)
1159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Nathan Scottd8cc8902005-11-02 10:34:53 +11001161 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001162 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner517c2222013-04-24 18:58:55 +10001163 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001165 if (error)
1166 goto out_defer_cancel;
1167 xfs_defer_ijoin(args->dfops, dp);
1168 error = xfs_defer_finish(&args->trans, args->dfops);
1169 if (error)
1170 goto out_defer_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001172 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174 error = 0;
1175
1176out:
1177 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001178 return error;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001179out_defer_cancel:
1180 xfs_defer_cancel(args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184/*
1185 * Fill in the disk block numbers in the state structure for the buffers
1186 * that are attached to the state structure.
1187 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001188 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190STATIC int
1191xfs_attr_fillstate(xfs_da_state_t *state)
1192{
1193 xfs_da_state_path_t *path;
1194 xfs_da_state_blk_t *blk;
1195 int level;
1196
Dave Chinneree732592012-11-12 22:53:53 +11001197 trace_xfs_attr_fillstate(state->args);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /*
1200 * Roll down the "path" in the state structure, storing the on-disk
1201 * block number for those buffers in the "path".
1202 */
1203 path = &state->path;
1204 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1205 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1206 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001207 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 blk->bp = NULL;
1209 } else {
1210 blk->disk_blkno = 0;
1211 }
1212 }
1213
1214 /*
1215 * Roll down the "altpath" in the state structure, storing the on-disk
1216 * block number for those buffers in the "altpath".
1217 */
1218 path = &state->altpath;
1219 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1220 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1221 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001222 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 blk->bp = NULL;
1224 } else {
1225 blk->disk_blkno = 0;
1226 }
1227 }
1228
Eric Sandeend99831f2014-06-22 15:03:54 +10001229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/*
1233 * Reattach the buffers to the state structure based on the disk block
1234 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001235 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * buffers from our grip.
1237 */
1238STATIC int
1239xfs_attr_refillstate(xfs_da_state_t *state)
1240{
1241 xfs_da_state_path_t *path;
1242 xfs_da_state_blk_t *blk;
1243 int level, error;
1244
Dave Chinneree732592012-11-12 22:53:53 +11001245 trace_xfs_attr_refillstate(state->args);
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /*
1248 * Roll down the "path" in the state structure, storing the on-disk
1249 * block number for those buffers in the "path".
1250 */
1251 path = &state->path;
1252 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1253 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1254 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001255 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 state->args->dp,
1257 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001258 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001260 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 } else {
1262 blk->bp = NULL;
1263 }
1264 }
1265
1266 /*
1267 * Roll down the "altpath" in the state structure, storing the on-disk
1268 * block number for those buffers in the "altpath".
1269 */
1270 path = &state->altpath;
1271 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1272 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1273 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001274 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 state->args->dp,
1276 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001277 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001279 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else {
1281 blk->bp = NULL;
1282 }
1283 }
1284
Eric Sandeend99831f2014-06-22 15:03:54 +10001285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288/*
1289 * Look up a filename in a node attribute list.
1290 *
1291 * This routine gets called for any attribute fork that has more than one
1292 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1293 * "remote" values taking up more blocks.
1294 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001295STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296xfs_attr_node_get(xfs_da_args_t *args)
1297{
1298 xfs_da_state_t *state;
1299 xfs_da_state_blk_t *blk;
1300 int error, retval;
1301 int i;
1302
Dave Chinneree732592012-11-12 22:53:53 +11001303 trace_xfs_attr_node_get(args);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 state = xfs_da_state_alloc();
1306 state->args = args;
1307 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /*
1310 * Search to see if name exists, and get back a pointer to it.
1311 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001312 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (error) {
1314 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001315 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 blk = &state->path.blk[ state->path.active-1 ];
1317 ASSERT(blk->bp != NULL);
1318 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1319
1320 /*
1321 * Get the value, local or "remote"
1322 */
Dave Chinner517c2222013-04-24 18:58:55 +10001323 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (!retval && (args->rmtblkno > 0)
1325 && !(args->flags & ATTR_KERNOVAL)) {
1326 retval = xfs_attr_rmtval_get(args);
1327 }
1328 }
1329
1330 /*
1331 * If not in a transaction, we have to release all the buffers.
1332 */
1333 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001334 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 state->path.blk[i].bp = NULL;
1336 }
1337
1338 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001339 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}