blob: d48fcf11cc35a40616423f8de015b60817f2b74a [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100013#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110014#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110015#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110016#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110018#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110020#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_attr.h"
22#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110023#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000026#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * xfs_attr.c
30 *
31 * Provide the external interfaces to manage attribute lists.
32 */
33
34/*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
37
38/*
39 * Internal routines when attribute list fits inside the inode.
40 */
41STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
42
43/*
44 * Internal routines when attribute list is one block.
45 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100046STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
48STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * Internal routines when attribute list is more than one block.
52 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100053STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
55STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
57STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Barry Naujoke8b0eba2008-04-22 17:34:31 +100060STATIC int
Christoph Hellwig67fd7182014-05-13 16:34:43 +100061xfs_attr_args_init(
62 struct xfs_da_args *args,
63 struct xfs_inode *dp,
64 const unsigned char *name,
65 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100066{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100067
68 if (!name)
Dave Chinner24513372014-06-25 14:58:08 +100069 return -EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100070
71 memset(args, 0, sizeof(*args));
Dave Chinner0650b552014-06-06 15:01:58 +100072 args->geo = dp->i_mount->m_attr_geo;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100073 args->whichfork = XFS_ATTR_FORK;
74 args->dp = dp;
75 args->flags = flags;
76 args->name = name;
77 args->namelen = strlen((const char *)name);
78 if (args->namelen >= MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +100079 return -EFAULT; /* match IRIX behaviour */
Barry Naujoke8b0eba2008-04-22 17:34:31 +100080
Christoph Hellwig67fd7182014-05-13 16:34:43 +100081 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100082 return 0;
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dave Chinnerabec5f22013-08-12 20:49:38 +100085int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100086xfs_inode_hasattr(
87 struct xfs_inode *ip)
88{
89 if (!XFS_IFORK_Q(ip) ||
90 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
91 ip->i_d.di_anextents == 0))
92 return 0;
93 return 1;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*========================================================================
97 * Overall external interface routines.
98 *========================================================================*/
99
Christoph Hellwigcf69f822017-07-13 12:14:33 -0700100/* Retrieve an extended attribute and its value. Must have ilock. */
Darrick J. Wongad017f62017-06-16 11:00:14 -0700101int
102xfs_attr_get_ilocked(
103 struct xfs_inode *ip,
104 struct xfs_da_args *args)
105{
Christoph Hellwigcf69f822017-07-13 12:14:33 -0700106 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
107
Darrick J. Wongad017f62017-06-16 11:00:14 -0700108 if (!xfs_inode_hasattr(ip))
109 return -ENOATTR;
110 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
111 return xfs_attr_shortform_getvalue(args);
112 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
113 return xfs_attr_leaf_get(args);
114 else
115 return xfs_attr_node_get(args);
116}
117
118/* Retrieve an extended attribute by name, and its value. */
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000119int
120xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000121 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000122 const unsigned char *name,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100123 unsigned char *value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000124 int *valuelenp,
125 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000127 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000128 uint lock_mode;
129 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100131 XFS_STATS_INC(ip->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000134 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000136 error = xfs_attr_args_init(&args, ip, name, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000137 if (error)
138 return error;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 args.value = value;
141 args.valuelen = *valuelenp;
Eric Sandeenc400ee32015-08-19 10:30:48 +1000142 /* Entirely possible to look up a name which doesn't exist */
143 args.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Christoph Hellwig683cb942013-12-06 12:30:15 -0800145 lock_mode = xfs_ilock_attr_map_shared(ip);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700146 error = xfs_attr_get_ilocked(ip, &args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800147 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000148
149 *valuelenp = args.valuelen;
Dave Chinner24513372014-06-25 14:58:08 +1000150 return error == -EEXIST ? 0 : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000153/*
154 * Calculate how many blocks we need for the new attribute,
155 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000156STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000157xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000158 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000159 int *local)
160{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000161 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000162 int size;
163 int nblks;
164
165 /*
166 * Determine space new attribute will use, and if it would be
167 * "local" or "remote" (note: local != inline).
168 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000169 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000170 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
171 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000172 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000173 /* Double split possible */
174 nblks *= 2;
175 }
176 } else {
177 /*
178 * Out of line attribute, cannot double split, but
179 * make room for the attribute value itself.
180 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000181 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000182 nblks += dblocks;
183 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
184 }
185
186 return nblks;
187}
188
Allison Henderson4c74a562018-10-18 17:20:50 +1100189STATIC int
190xfs_attr_try_sf_addname(
191 struct xfs_inode *dp,
192 struct xfs_da_args *args)
193{
194
195 struct xfs_mount *mp = dp->i_mount;
196 int error, error2;
197
198 error = xfs_attr_shortform_addname(args);
199 if (error == -ENOSPC)
200 return error;
201
202 /*
203 * Commit the shortform mods, and we're done.
204 * NOTE: this is also the error path (EEXIST, etc).
205 */
206 if (!error && (args->flags & ATTR_KERNOTIME) == 0)
207 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
208
209 if (mp->m_flags & XFS_MOUNT_WSYNC)
210 xfs_trans_set_sync(args->trans);
211
212 error2 = xfs_trans_commit(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100213 args->trans = NULL;
Allison Henderson4c74a562018-10-18 17:20:50 +1100214 return error ? error : error2;
215}
216
Allison Henderson2f3cd802018-10-18 17:21:16 +1100217/*
218 * Set the attribute specified in @args.
219 */
220int
221xfs_attr_set_args(
Darrick J. Wong710d7072019-04-24 09:27:41 -0700222 struct xfs_da_args *args)
Allison Henderson2f3cd802018-10-18 17:21:16 +1100223{
224 struct xfs_inode *dp = args->dp;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700225 struct xfs_buf *leaf_bp = NULL;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100226 int error;
227
228 /*
229 * If the attribute list is non-existent or a shortform list,
230 * upgrade it to a single-leaf-block attribute list.
231 */
232 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
233 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
234 dp->i_d.di_anextents == 0)) {
235
236 /*
237 * Build initial attribute list (if required).
238 */
239 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
240 xfs_attr_shortform_create(args);
241
242 /*
243 * Try to add the attr to the attribute list in the inode.
244 */
245 error = xfs_attr_try_sf_addname(dp, args);
246 if (error != -ENOSPC)
247 return error;
248
249 /*
250 * It won't fit in the shortform, transform to a leaf block.
251 * GROT: another possible req'mt for a double-split btree op.
252 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700253 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100254 if (error)
255 return error;
256
257 /*
258 * Prevent the leaf buffer from being unlocked so that a
259 * concurrent AIL push cannot grab the half-baked leaf
260 * buffer and run into problems with the write verifier.
Darrick J. Wong710d7072019-04-24 09:27:41 -0700261 * Once we're done rolling the transaction we can release
262 * the hold and add the attr to the leaf.
Allison Henderson2f3cd802018-10-18 17:21:16 +1100263 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700264 xfs_trans_bhold(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100265 error = xfs_defer_finish(&args->trans);
Darrick J. Wong710d7072019-04-24 09:27:41 -0700266 xfs_trans_bhold_release(args->trans, leaf_bp);
267 if (error) {
268 xfs_trans_brelse(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100269 return error;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700270 }
Allison Henderson2f3cd802018-10-18 17:21:16 +1100271 }
272
273 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
274 error = xfs_attr_leaf_addname(args);
275 else
276 error = xfs_attr_node_addname(args);
277 return error;
278}
279
Allison Henderson068f9852018-10-18 17:21:23 +1100280/*
281 * Remove the attribute specified in @args.
282 */
283int
284xfs_attr_remove_args(
285 struct xfs_da_args *args)
286{
287 struct xfs_inode *dp = args->dp;
288 int error;
289
290 if (!xfs_inode_hasattr(dp)) {
291 error = -ENOATTR;
292 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
293 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
294 error = xfs_attr_shortform_remove(args);
295 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
296 error = xfs_attr_leaf_removename(args);
297 } else {
298 error = xfs_attr_node_removename(args);
299 }
300
301 return error;
302}
303
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000304int
305xfs_attr_set(
306 struct xfs_inode *dp,
307 const unsigned char *name,
308 unsigned char *value,
309 int valuelen,
310 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000312 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000313 struct xfs_da_args args;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000314 struct xfs_trans_res tres;
315 int rsvd = (flags & ATTR_ROOT) != 0;
Allison Henderson4c74a562018-10-18 17:20:50 +1100316 int error, local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100318 XFS_STATS_INC(mp, xs_attr_set);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000319
320 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000321 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000322
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000323 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000324 if (error)
325 return error;
326
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000327 args.value = value;
328 args.valuelen = valuelen;
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000329 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000330 args.total = xfs_attr_calc_size(&args, &local);
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000331
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700332 error = xfs_qm_dqattach(dp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200333 if (error)
334 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /*
337 * If the inode doesn't have an attribute fork, add one.
338 * (inode must not be locked when we call this routine)
339 */
340 if (XFS_IFORK_Q(dp) == 0) {
Barry Naujoke5889e92007-02-10 18:35:58 +1100341 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000342 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
Barry Naujoke5889e92007-02-10 18:35:58 +1100343
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000344 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
345 if (error)
346 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
Christoph Hellwig253f4912016-04-06 09:19:55 +1000349 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
350 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
351 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
352 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /*
355 * Root fork attributes can use reserved data blocks for this
356 * operation if necessary
357 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000358 error = xfs_trans_alloc(mp, &tres, args.total, 0,
359 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
360 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000361 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Christoph Hellwig253f4912016-04-06 09:19:55 +1000363 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200364 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000365 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
366 XFS_QMOPT_RES_REGBLKS);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100367 if (error)
368 goto out_trans_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Christoph Hellwigddc34152011-09-19 15:00:54 +0000370 xfs_trans_ijoin(args.trans, dp, 0);
Darrick J. Wong710d7072019-04-24 09:27:41 -0700371 error = xfs_attr_set_args(&args);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000372 if (error)
Darrick J. Wong710d7072019-04-24 09:27:41 -0700373 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100374 if (!args.trans) {
375 /* shortform attribute has already been committed */
376 goto out_unlock;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /*
380 * If this is a synchronous mount, make sure that the
381 * transaction goes to disk before returning to the user.
382 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000383 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000386 if ((flags & ATTR_KERNOTIME) == 0)
387 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
390 * Commit the last in the sequence of transactions.
391 */
392 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000393 error = xfs_trans_commit(args.trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100394out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000396 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Allison Henderson2f3cd802018-10-18 17:21:16 +1100398out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000399 if (args.trans)
400 xfs_trans_cancel(args.trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100401 goto out_unlock;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100402}
403
404/*
405 * Generic handler routine to remove a name from an attribute list.
406 * Transitions attribute list from Btree to shortform as necessary.
407 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000408int
409xfs_attr_remove(
410 struct xfs_inode *dp,
411 const unsigned char *name,
412 int flags)
Nathan Scottaa82daa2005-11-02 10:33:33 +1100413{
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000414 struct xfs_mount *mp = dp->i_mount;
415 struct xfs_da_args args;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000416 int error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100417
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100418 XFS_STATS_INC(mp, xs_attr_remove);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000419
420 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000421 return -EIO;
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000422
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000423 error = xfs_attr_args_init(&args, dp, name, flags);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000424 if (error)
425 return error;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /*
Dave Chinner4a338212011-06-23 01:35:01 +0000428 * we have no control over the attribute names that userspace passes us
429 * to remove, so we have to allow the name lookup prior to attribute
430 * removal to fail.
431 */
432 args.op_flags = XFS_DA_OP_OKNOENT;
433
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700434 error = xfs_qm_dqattach(dp);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200435 if (error)
436 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * Root fork attributes can use reserved data blocks for this
440 * operation if necessary
441 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000442 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
443 XFS_ATTRRM_SPACE_RES(mp), 0,
444 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
445 &args.trans);
446 if (error)
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000447 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 xfs_ilock(dp, XFS_ILOCK_EXCL);
450 /*
451 * No need to make quota reservations here. We expect to release some
452 * blocks not allocate in the common case.
453 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000454 xfs_trans_ijoin(args.trans, dp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Allison Henderson068f9852018-10-18 17:21:23 +1100456 error = xfs_attr_remove_args(&args);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000457 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * If this is a synchronous mount, make sure that the
462 * transaction goes to disk before returning to the user.
463 */
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000464 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000467 if ((flags & ATTR_KERNOTIME) == 0)
468 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * Commit the last in the sequence of transactions.
472 */
473 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000474 error = xfs_trans_commit(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 xfs_iunlock(dp, XFS_ILOCK_EXCL);
476
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000477 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479out:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000480 if (args.trans)
481 xfs_trans_cancel(args.trans);
Christoph Hellwig1bc426a2014-05-13 16:34:33 +1000482 xfs_iunlock(dp, XFS_ILOCK_EXCL);
483 return error;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*========================================================================
487 * External routines when attribute list is inside the inode
488 *========================================================================*/
489
490/*
491 * Add a name to the shortform attribute list structure
492 * This is the external routine.
493 */
494STATIC int
495xfs_attr_shortform_addname(xfs_da_args_t *args)
496{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100497 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dave Chinner5a5881c2012-03-22 05:15:13 +0000499 trace_xfs_attr_sf_addname(args);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000502 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000503 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000504 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000506 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700508 if (retval)
509 return retval;
510 /*
511 * Since we have removed the old attr, clear ATTR_REPLACE so
512 * that the leaf format add routine won't trip over the attr
513 * not being around.
514 */
515 args->flags &= ~ATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
Nathan Scottd8cc8902005-11-02 10:34:53 +1100518 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
519 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000520 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
523 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100524
525 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
526 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000527 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100528
529 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533
534/*========================================================================
535 * External routines when attribute list is one block
536 *========================================================================*/
537
538/*
539 * Add a name to the leaf attribute list structure
540 *
541 * This leaf block cannot have a "remote" value, we only call this routine
542 * if bmap_one_block() says there is only one block (ie: no remote blks).
543 */
David Chinnera8272ce2007-11-23 16:28:09 +1100544STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700545xfs_attr_leaf_addname(
546 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700548 struct xfs_inode *dp;
549 struct xfs_buf *bp;
550 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dave Chinner5a5881c2012-03-22 05:15:13 +0000552 trace_xfs_attr_leaf_addname(args);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /*
555 * Read the (only) block in the attribute list in.
556 */
557 dp = args->dp;
558 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000559 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100561 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /*
564 * Look up the given attribute in the leaf block. Figure out if
565 * the given flags produce an error or call for an atomic rename.
566 */
Dave Chinner517c2222013-04-24 18:58:55 +1000567 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000568 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000569 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000570 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000571 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000573 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000574 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000576
577 trace_xfs_attr_leaf_replace(args);
578
Dave Chinner8275cdd2014-05-06 07:37:31 +1000579 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000580 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 args->blkno2 = args->blkno; /* set 2nd entry info*/
582 args->index2 = args->index;
583 args->rmtblkno2 = args->rmtblkno;
584 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000585 args->rmtvaluelen2 = args->rmtvaluelen;
586
587 /*
588 * clear the remote attr state now that it is saved so that the
589 * values reflect the state of the attribute we are about to
590 * add, not the attribute we just found and will remove later.
591 */
592 args->rmtblkno = 0;
593 args->rmtblkcnt = 0;
594 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
597 /*
598 * Add the attribute to the leaf block, transitioning to a Btree
599 * if required.
600 */
Dave Chinner517c2222013-04-24 18:58:55 +1000601 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000602 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
604 * Promote the attribute list to the Btree format, then
605 * Commit that transaction so that the node_addname() call
606 * can manage its own transactions.
607 */
Dave Chinner517c2222013-04-24 18:58:55 +1000608 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700609 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000610 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700611 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700612 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700613 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * Commit the current trans (including the inode) and start
617 * a new one.
618 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700619 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000620 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000621 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /*
624 * Fob the whole rest of the problem off on the Btree code.
625 */
626 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000627 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 /*
631 * Commit the transaction that added the attr name so that
632 * later routines can manage their own transactions.
633 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700634 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000635 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000636 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /*
639 * If there was an out-of-line value, allocate the blocks we
640 * identified for its storage and copy the value. This is done
641 * after we create the attribute so that we don't overflow the
642 * maximum size of a transaction and/or hit a deadlock.
643 */
644 if (args->rmtblkno > 0) {
645 error = xfs_attr_rmtval_set(args);
646 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 /*
651 * If this is an atomic rename operation, we must "flip" the
652 * incomplete flags on the "new" and "old" attribute/value pairs
653 * so that one disappears and one appears atomically. Then we
654 * must remove the "old" attribute/value pair.
655 */
Barry Naujok6a178102008-05-21 16:42:05 +1000656 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * In a separate transaction, set the incomplete flag on the
659 * "old" attr and clear the incomplete flag on the "new" attr.
660 */
Dave Chinner517c2222013-04-24 18:58:55 +1000661 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000663 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /*
666 * Dismantle the "old" attribute/value pair by removing
667 * a "remote" value (if it exists).
668 */
669 args->index = args->index2;
670 args->blkno = args->blkno2;
671 args->rmtblkno = args->rmtblkno2;
672 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000673 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (args->rmtblkno) {
675 error = xfs_attr_rmtval_remove(args);
676 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000677 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 /*
681 * Read in the block containing the "old" attr, then
682 * remove the "old" attr from that block (neat, huh!)
683 */
Dave Chinner517c2222013-04-24 18:58:55 +1000684 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Dave Chinnerad14c332012-11-12 22:54:16 +1100685 -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100687 return error;
688
Dave Chinner517c2222013-04-24 18:58:55 +1000689 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /*
692 * If the result is small enough, shrink it all into the inode.
693 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100694 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +1000695 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700697 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000698 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700699 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700700 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700701 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /*
705 * Commit the remove and start the next trans in series.
706 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700707 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 } else if (args->rmtblkno > 0) {
710 /*
711 * Added a "remote" value, just clear the incomplete flag.
712 */
Dave Chinner517c2222013-04-24 18:58:55 +1000713 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Dave Chinner517c2222013-04-24 18:58:55 +1000715 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
Brian Foster32a9b7c2018-07-11 22:26:11 -0700725xfs_attr_leaf_removename(
726 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700728 struct xfs_inode *dp;
729 struct xfs_buf *bp;
730 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Dave Chinner5a5881c2012-03-22 05:15:13 +0000732 trace_xfs_attr_leaf_removename(args);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Remove the attribute.
736 */
737 dp = args->dp;
738 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000739 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100740 if (error)
741 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Dave Chinner517c2222013-04-24 18:58:55 +1000743 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000744 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000745 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000746 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Dave Chinner517c2222013-04-24 18:58:55 +1000749 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
752 * If the result is small enough, shrink it all into the inode.
753 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100754 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
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)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000758 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700759 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700760 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700761 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000762 }
Dave Chinner517c2222013-04-24 18:58:55 +1000763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * Look up a name in a leaf attribute list structure.
768 *
769 * This leaf block cannot have a "remote" value, we only call this routine
770 * if bmap_one_block() says there is only one block (ie: no remote blks).
771 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000772STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773xfs_attr_leaf_get(xfs_da_args_t *args)
774{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000775 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int error;
777
Dave Chinneree732592012-11-12 22:53:53 +1100778 trace_xfs_attr_leaf_get(args);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 args->blkno = 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000781 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Dave Chinner517c2222013-04-24 18:58:55 +1000785 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000786 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000787 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000788 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Dave Chinner517c2222013-04-24 18:58:55 +1000790 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000791 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
793 error = xfs_attr_rmtval_get(args);
794 }
Dave Chinner517c2222013-04-24 18:58:55 +1000795 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000799 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *========================================================================*/
801
802/*
803 * Add a name to a Btree-format attribute list.
804 *
805 * This will involve walking down the Btree, and may involve splitting
806 * leaf nodes and even splitting intermediate nodes up to and including
807 * the root node (a special case of an intermediate node).
808 *
809 * "Remote" attribute values confuse the issue and atomic rename operations
810 * add a whole extra layer of confusion on top of that.
811 */
812STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700813xfs_attr_node_addname(
814 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700816 struct xfs_da_state *state;
817 struct xfs_da_state_blk *blk;
818 struct xfs_inode *dp;
819 struct xfs_mount *mp;
820 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dave Chinner5a5881c2012-03-22 05:15:13 +0000822 trace_xfs_attr_node_addname(args);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
825 * Fill in bucket of arguments/results/context to carry around.
826 */
827 dp = args->dp;
828 mp = dp->i_mount;
829restart:
830 state = xfs_da_state_alloc();
831 state->args = args;
832 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /*
835 * Search to see if name already exists, and get back a pointer
836 * to where it should go.
837 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000838 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (error)
840 goto out;
841 blk = &state->path.blk[ state->path.active-1 ];
842 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000843 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000845 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (args->flags & ATTR_CREATE)
847 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000848
849 trace_xfs_attr_node_replace(args);
850
Dave Chinner8275cdd2014-05-06 07:37:31 +1000851 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000852 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 args->blkno2 = args->blkno; /* set 2nd entry info*/
854 args->index2 = args->index;
855 args->rmtblkno2 = args->rmtblkno;
856 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000857 args->rmtvaluelen2 = args->rmtvaluelen;
858
859 /*
860 * clear the remote attr state now that it is saved so that the
861 * values reflect the state of the attribute we are about to
862 * add, not the attribute we just found and will remove later.
863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 args->rmtblkno = 0;
865 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000866 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
Dave Chinner517c2222013-04-24 18:58:55 +1000869 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000870 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (state->path.active == 1) {
872 /*
873 * Its really a single leaf node, but it had
874 * out-of-line values so it looked like it *might*
875 * have been a b-tree.
876 */
877 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500878 state = NULL;
Dave Chinner517c2222013-04-24 18:58:55 +1000879 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700880 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000881 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700882 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700883 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700884 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 * Commit the node conversion and start the next
888 * trans in the chain.
889 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700890 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000891 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto out;
893
894 goto restart;
895 }
896
897 /*
898 * Split as many Btree elements as required.
899 * This code tracks the new and old attr's location
900 * in the index/blkno/rmtblkno/rmtblkcnt fields and
901 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
902 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000903 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700904 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000905 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700906 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700907 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 } else {
910 /*
911 * Addition succeeded, update Btree hashvals.
912 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000913 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
916 /*
917 * Kill the state structure, we're done with it and need to
918 * allow the buffers to come back later.
919 */
920 xfs_da_state_free(state);
921 state = NULL;
922
923 /*
924 * Commit the leaf addition or btree split and start the next
925 * trans in the chain.
926 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700927 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000928 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto out;
930
931 /*
932 * If there was an out-of-line value, allocate the blocks we
933 * identified for its storage and copy the value. This is done
934 * after we create the attribute so that we don't overflow the
935 * maximum size of a transaction and/or hit a deadlock.
936 */
937 if (args->rmtblkno > 0) {
938 error = xfs_attr_rmtval_set(args);
939 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000940 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
943 /*
944 * If this is an atomic rename operation, we must "flip" the
945 * incomplete flags on the "new" and "old" attribute/value pairs
946 * so that one disappears and one appears atomically. Then we
947 * must remove the "old" attribute/value pair.
948 */
Barry Naujok6a178102008-05-21 16:42:05 +1000949 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /*
951 * In a separate transaction, set the incomplete flag on the
952 * "old" attr and clear the incomplete flag on the "new" attr.
953 */
Dave Chinner517c2222013-04-24 18:58:55 +1000954 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (error)
956 goto out;
957
958 /*
959 * Dismantle the "old" attribute/value pair by removing
960 * a "remote" value (if it exists).
961 */
962 args->index = args->index2;
963 args->blkno = args->blkno2;
964 args->rmtblkno = args->rmtblkno2;
965 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000966 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (args->rmtblkno) {
968 error = xfs_attr_rmtval_remove(args);
969 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000970 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
973 /*
974 * Re-find the "old" attribute entry after any split ops.
975 * The INCOMPLETE flag means that we will find the "old"
976 * attr, not the "new" one.
977 */
978 args->flags |= XFS_ATTR_INCOMPLETE;
979 state = xfs_da_state_alloc();
980 state->args = args;
981 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000983 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (error)
985 goto out;
986
987 /*
988 * Remove the name and update the hashvals in the tree.
989 */
990 blk = &state->path.blk[ state->path.active-1 ];
991 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000992 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000993 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * Check to see if the tree needs to be collapsed.
997 */
998 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000999 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001000 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001001 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001002 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001003 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001004 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
1007 /*
1008 * Commit and start the next trans in the chain.
1009 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001010 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001011 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto out;
1013
1014 } else if (args->rmtblkno > 0) {
1015 /*
1016 * Added a "remote" value, just clear the incomplete flag.
1017 */
Dave Chinner517c2222013-04-24 18:58:55 +10001018 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (error)
1020 goto out;
1021 }
1022 retval = error = 0;
1023
1024out:
1025 if (state)
1026 xfs_da_state_free(state);
1027 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001028 return error;
1029 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
1033 * Remove a name from a B-tree attribute list.
1034 *
1035 * This will involve walking down the Btree, and may involve joining
1036 * leaf nodes and even joining intermediate nodes up to and including
1037 * the root node (a special case of an intermediate node).
1038 */
1039STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -07001040xfs_attr_node_removename(
1041 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Brian Foster32a9b7c2018-07-11 22:26:11 -07001043 struct xfs_da_state *state;
1044 struct xfs_da_state_blk *blk;
1045 struct xfs_inode *dp;
1046 struct xfs_buf *bp;
1047 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Dave Chinner5a5881c2012-03-22 05:15:13 +00001049 trace_xfs_attr_node_removename(args);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
1052 * Tie a string around our finger to remind us where we are.
1053 */
1054 dp = args->dp;
1055 state = xfs_da_state_alloc();
1056 state->args = args;
1057 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /*
1060 * Search to see if name exists, and get back a pointer to it.
1061 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001062 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001063 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (error == 0)
1065 error = retval;
1066 goto out;
1067 }
1068
1069 /*
1070 * If there is an out-of-line value, de-allocate the blocks.
1071 * This is done before we remove the attribute so that we don't
1072 * overflow the maximum size of a transaction and/or hit a deadlock.
1073 */
1074 blk = &state->path.blk[ state->path.active-1 ];
1075 ASSERT(blk->bp != NULL);
1076 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1077 if (args->rmtblkno > 0) {
1078 /*
1079 * Fill in disk block numbers in the state structure
1080 * so that we can get the buffers back after we commit
1081 * several transactions in the following calls.
1082 */
1083 error = xfs_attr_fillstate(state);
1084 if (error)
1085 goto out;
1086
1087 /*
1088 * Mark the attribute as INCOMPLETE, then bunmapi() the
1089 * remote value.
1090 */
Dave Chinner517c2222013-04-24 18:58:55 +10001091 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (error)
1093 goto out;
1094 error = xfs_attr_rmtval_remove(args);
1095 if (error)
1096 goto out;
1097
1098 /*
1099 * Refill the state structure with buffers, the prior calls
1100 * released our buffers.
1101 */
1102 error = xfs_attr_refillstate(state);
1103 if (error)
1104 goto out;
1105 }
1106
1107 /*
1108 * Remove the name and update the hashvals in the tree.
1109 */
1110 blk = &state->path.blk[ state->path.active-1 ];
1111 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001112 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001113 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /*
1116 * Check to see if the tree needs to be collapsed.
1117 */
1118 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001119 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001120 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001121 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001122 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001123 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
1126 * Commit the Btree join operation and start a new trans.
1127 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001128 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001129 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto out;
1131 }
1132
1133 /*
1134 * If the result is small enough, push it all into the inode.
1135 */
1136 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1137 /*
1138 * Have to get rid of the copy of this dabuf in the state.
1139 */
1140 ASSERT(state->path.active == 1);
1141 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 state->path.blk[0].bp = NULL;
1143
Dave Chinner517c2222013-04-24 18:58:55 +10001144 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (error)
1146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Nathan Scottd8cc8902005-11-02 10:34:53 +11001148 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +10001149 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001151 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001152 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001153 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001154 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001157 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 error = 0;
1160
1161out:
1162 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001163 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
1166/*
1167 * Fill in the disk block numbers in the state structure for the buffers
1168 * that are attached to the state structure.
1169 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001170 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
1172STATIC int
1173xfs_attr_fillstate(xfs_da_state_t *state)
1174{
1175 xfs_da_state_path_t *path;
1176 xfs_da_state_blk_t *blk;
1177 int level;
1178
Dave Chinneree732592012-11-12 22:53:53 +11001179 trace_xfs_attr_fillstate(state->args);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * Roll down the "path" in the state structure, storing the on-disk
1183 * block number for those buffers in the "path".
1184 */
1185 path = &state->path;
1186 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1187 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1188 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001189 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 blk->bp = NULL;
1191 } else {
1192 blk->disk_blkno = 0;
1193 }
1194 }
1195
1196 /*
1197 * Roll down the "altpath" in the state structure, storing the on-disk
1198 * block number for those buffers in the "altpath".
1199 */
1200 path = &state->altpath;
1201 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1202 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1203 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001204 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 blk->bp = NULL;
1206 } else {
1207 blk->disk_blkno = 0;
1208 }
1209 }
1210
Eric Sandeend99831f2014-06-22 15:03:54 +10001211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214/*
1215 * Reattach the buffers to the state structure based on the disk block
1216 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001217 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * buffers from our grip.
1219 */
1220STATIC int
1221xfs_attr_refillstate(xfs_da_state_t *state)
1222{
1223 xfs_da_state_path_t *path;
1224 xfs_da_state_blk_t *blk;
1225 int level, error;
1226
Dave Chinneree732592012-11-12 22:53:53 +11001227 trace_xfs_attr_refillstate(state->args);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /*
1230 * Roll down the "path" in the state structure, storing the on-disk
1231 * block number for those buffers in the "path".
1232 */
1233 path = &state->path;
1234 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1235 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1236 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001237 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 state->args->dp,
1239 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001240 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001242 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 } else {
1244 blk->bp = NULL;
1245 }
1246 }
1247
1248 /*
1249 * Roll down the "altpath" in the state structure, storing the on-disk
1250 * block number for those buffers in the "altpath".
1251 */
1252 path = &state->altpath;
1253 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1254 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1255 if (blk->disk_blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001256 error = xfs_da3_node_read(state->args->trans,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 state->args->dp,
1258 blk->blkno, blk->disk_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001259 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001261 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
1263 blk->bp = NULL;
1264 }
1265 }
1266
Eric Sandeend99831f2014-06-22 15:03:54 +10001267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270/*
1271 * Look up a filename in a node attribute list.
1272 *
1273 * This routine gets called for any attribute fork that has more than one
1274 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1275 * "remote" values taking up more blocks.
1276 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001277STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278xfs_attr_node_get(xfs_da_args_t *args)
1279{
1280 xfs_da_state_t *state;
1281 xfs_da_state_blk_t *blk;
1282 int error, retval;
1283 int i;
1284
Dave Chinneree732592012-11-12 22:53:53 +11001285 trace_xfs_attr_node_get(args);
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 state = xfs_da_state_alloc();
1288 state->args = args;
1289 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /*
1292 * Search to see if name exists, and get back a pointer to it.
1293 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001294 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (error) {
1296 retval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001297 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 blk = &state->path.blk[ state->path.active-1 ];
1299 ASSERT(blk->bp != NULL);
1300 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1301
1302 /*
1303 * Get the value, local or "remote"
1304 */
Dave Chinner517c2222013-04-24 18:58:55 +10001305 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!retval && (args->rmtblkno > 0)
1307 && !(args->flags & ATTR_KERNOVAL)) {
1308 retval = xfs_attr_rmtval_get(args);
1309 }
1310 }
1311
1312 /*
1313 * If not in a transaction, we have to release all the buffers.
1314 */
1315 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001316 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 state->path.blk[i].bp = NULL;
1318 }
1319
1320 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001321 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
Darrick J. Wong65480532019-02-01 09:08:54 -08001323
1324/* Returns true if the attribute entry name is valid. */
1325bool
1326xfs_attr_namecheck(
1327 const void *name,
1328 size_t length)
1329{
1330 /*
1331 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1332 * out, so use >= for the length check.
1333 */
1334 if (length >= MAXNAMELEN)
1335 return false;
1336
1337 /* There shouldn't be any nulls here */
1338 return !memchr(name, 0, length);
1339}