blob: bb391b96cd780f1c91a78e2b09eebb7206dcb299 [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,
Allison Hendersond29f7812020-01-07 15:26:15 -080065 size_t namelen,
Christoph Hellwig67fd7182014-05-13 16:34:43 +100066 int flags)
Barry Naujoke8b0eba2008-04-22 17:34:31 +100067{
Christoph Hellwig67fd7182014-05-13 16:34:43 +100068
69 if (!name)
Dave Chinner24513372014-06-25 14:58:08 +100070 return -EINVAL;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100071
72 memset(args, 0, sizeof(*args));
Dave Chinner0650b552014-06-06 15:01:58 +100073 args->geo = dp->i_mount->m_attr_geo;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100074 args->whichfork = XFS_ATTR_FORK;
75 args->dp = dp;
76 args->flags = flags;
77 args->name = name;
Allison Hendersond29f7812020-01-07 15:26:15 -080078 args->namelen = namelen;
Christoph Hellwig67fd7182014-05-13 16:34:43 +100079 if (args->namelen >= MAXNAMELEN)
Dave Chinner24513372014-06-25 14:58:08 +100080 return -EFAULT; /* match IRIX behaviour */
Barry Naujoke8b0eba2008-04-22 17:34:31 +100081
Christoph Hellwig67fd7182014-05-13 16:34:43 +100082 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +100083 return 0;
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Dave Chinnerabec5f22013-08-12 20:49:38 +100086int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100087xfs_inode_hasattr(
88 struct xfs_inode *ip)
89{
90 if (!XFS_IFORK_Q(ip) ||
91 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
92 ip->i_d.di_anextents == 0))
93 return 0;
94 return 1;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*========================================================================
98 * Overall external interface routines.
99 *========================================================================*/
100
Dave Chinner728bcaa2019-08-29 09:04:08 -0700101/*
102 * Retrieve an extended attribute and its value. Must have ilock.
103 * Returns 0 on successful retrieval, otherwise an error.
104 */
Darrick J. Wongad017f62017-06-16 11:00:14 -0700105int
106xfs_attr_get_ilocked(
107 struct xfs_inode *ip,
108 struct xfs_da_args *args)
109{
Christoph Hellwigcf69f822017-07-13 12:14:33 -0700110 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
111
Darrick J. Wongad017f62017-06-16 11:00:14 -0700112 if (!xfs_inode_hasattr(ip))
113 return -ENOATTR;
114 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
115 return xfs_attr_shortform_getvalue(args);
116 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
117 return xfs_attr_leaf_get(args);
118 else
119 return xfs_attr_node_get(args);
120}
121
Dave Chinnerddbca702019-08-29 09:04:10 -0700122/*
123 * Retrieve an extended attribute by name, and its value if requested.
124 *
125 * If ATTR_KERNOVAL is set in @flags, then the caller does not want the value,
126 * just an indication whether the attribute exists and the size of the value if
127 * it exists. The size is returned in @valuelenp,
128 *
129 * If the attribute is found, but exceeds the size limit set by the caller in
130 * @valuelenp, return -ERANGE with the size of the attribute that was found in
131 * @valuelenp.
132 *
133 * If ATTR_ALLOC is set in @flags, allocate the buffer for the value after
134 * existence of the attribute has been determined. On success, return that
135 * buffer to the caller and leave them to free it. On failure, free any
136 * allocated buffer and ensure the buffer pointer returned to the caller is
137 * null.
138 */
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000139int
140xfs_attr_get(
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000141 struct xfs_inode *ip,
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000142 const unsigned char *name,
Allison Hendersond29f7812020-01-07 15:26:15 -0800143 size_t namelen,
Dave Chinnerddbca702019-08-29 09:04:10 -0700144 unsigned char **value,
Christoph Hellwige82fa0c2009-11-14 16:17:20 +0000145 int *valuelenp,
146 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000148 struct xfs_da_args args;
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000149 uint lock_mode;
150 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dave Chinnerddbca702019-08-29 09:04:10 -0700152 ASSERT((flags & (ATTR_ALLOC | ATTR_KERNOVAL)) || *value);
153
Bill O'Donnellff6d6af2015-10-12 18:21:22 +1100154 XFS_STATS_INC(ip->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000157 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Allison Hendersond29f7812020-01-07 15:26:15 -0800159 error = xfs_attr_args_init(&args, ip, name, namelen, flags);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000160 if (error)
161 return error;
162
Eric Sandeenc400ee32015-08-19 10:30:48 +1000163 /* Entirely possible to look up a name which doesn't exist */
164 args.op_flags = XFS_DA_OP_OKNOENT;
Dave Chinnerddbca702019-08-29 09:04:10 -0700165 if (flags & ATTR_ALLOC)
166 args.op_flags |= XFS_DA_OP_ALLOCVAL;
167 else
168 args.value = *value;
169 args.valuelen = *valuelenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Christoph Hellwig683cb942013-12-06 12:30:15 -0800171 lock_mode = xfs_ilock_attr_map_shared(ip);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700172 error = xfs_attr_get_ilocked(ip, &args);
Christoph Hellwig683cb942013-12-06 12:30:15 -0800173 xfs_iunlock(ip, lock_mode);
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000174 *valuelenp = args.valuelen;
Dave Chinnerddbca702019-08-29 09:04:10 -0700175
176 /* on error, we have to clean up allocated value buffers */
177 if (error) {
178 if (flags & ATTR_ALLOC) {
179 kmem_free(args.value);
180 *value = NULL;
181 }
182 return error;
183 }
184 *value = args.value;
185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000188/*
189 * Calculate how many blocks we need for the new attribute,
190 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000191STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000192xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000193 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000194 int *local)
195{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000196 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000197 int size;
198 int nblks;
199
200 /*
201 * Determine space new attribute will use, and if it would be
202 * "local" or "remote" (note: local != inline).
203 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000204 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000205 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
206 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000207 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000208 /* Double split possible */
209 nblks *= 2;
210 }
211 } else {
212 /*
213 * Out of line attribute, cannot double split, but
214 * make room for the attribute value itself.
215 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000216 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000217 nblks += dblocks;
218 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
219 }
220
221 return nblks;
222}
223
Allison Henderson4c74a562018-10-18 17:20:50 +1100224STATIC int
225xfs_attr_try_sf_addname(
226 struct xfs_inode *dp,
227 struct xfs_da_args *args)
228{
229
230 struct xfs_mount *mp = dp->i_mount;
231 int error, error2;
232
233 error = xfs_attr_shortform_addname(args);
234 if (error == -ENOSPC)
235 return error;
236
237 /*
238 * Commit the shortform mods, and we're done.
239 * NOTE: this is also the error path (EEXIST, etc).
240 */
241 if (!error && (args->flags & ATTR_KERNOTIME) == 0)
242 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
243
244 if (mp->m_flags & XFS_MOUNT_WSYNC)
245 xfs_trans_set_sync(args->trans);
246
247 error2 = xfs_trans_commit(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100248 args->trans = NULL;
Allison Henderson4c74a562018-10-18 17:20:50 +1100249 return error ? error : error2;
250}
251
Allison Henderson2f3cd802018-10-18 17:21:16 +1100252/*
253 * Set the attribute specified in @args.
254 */
255int
256xfs_attr_set_args(
Darrick J. Wong710d7072019-04-24 09:27:41 -0700257 struct xfs_da_args *args)
Allison Henderson2f3cd802018-10-18 17:21:16 +1100258{
259 struct xfs_inode *dp = args->dp;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700260 struct xfs_buf *leaf_bp = NULL;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100261 int error;
262
263 /*
264 * If the attribute list is non-existent or a shortform list,
265 * upgrade it to a single-leaf-block attribute list.
266 */
267 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
268 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
269 dp->i_d.di_anextents == 0)) {
270
271 /*
272 * Build initial attribute list (if required).
273 */
274 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
275 xfs_attr_shortform_create(args);
276
277 /*
278 * Try to add the attr to the attribute list in the inode.
279 */
280 error = xfs_attr_try_sf_addname(dp, args);
281 if (error != -ENOSPC)
282 return error;
283
284 /*
285 * It won't fit in the shortform, transform to a leaf block.
286 * GROT: another possible req'mt for a double-split btree op.
287 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700288 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100289 if (error)
290 return error;
291
292 /*
293 * Prevent the leaf buffer from being unlocked so that a
294 * concurrent AIL push cannot grab the half-baked leaf
295 * buffer and run into problems with the write verifier.
Darrick J. Wong710d7072019-04-24 09:27:41 -0700296 * Once we're done rolling the transaction we can release
297 * the hold and add the attr to the leaf.
Allison Henderson2f3cd802018-10-18 17:21:16 +1100298 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700299 xfs_trans_bhold(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100300 error = xfs_defer_finish(&args->trans);
Darrick J. Wong710d7072019-04-24 09:27:41 -0700301 xfs_trans_bhold_release(args->trans, leaf_bp);
302 if (error) {
303 xfs_trans_brelse(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100304 return error;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700305 }
Allison Henderson2f3cd802018-10-18 17:21:16 +1100306 }
307
308 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
309 error = xfs_attr_leaf_addname(args);
310 else
311 error = xfs_attr_node_addname(args);
312 return error;
313}
314
Allison Henderson068f9852018-10-18 17:21:23 +1100315/*
316 * Remove the attribute specified in @args.
317 */
318int
319xfs_attr_remove_args(
320 struct xfs_da_args *args)
321{
322 struct xfs_inode *dp = args->dp;
323 int error;
324
325 if (!xfs_inode_hasattr(dp)) {
326 error = -ENOATTR;
327 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
328 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
329 error = xfs_attr_shortform_remove(args);
330 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
331 error = xfs_attr_leaf_removename(args);
332 } else {
333 error = xfs_attr_node_removename(args);
334 }
335
336 return error;
337}
338
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800339/*
340 * Note: If value is NULL the attribute will be removed, just like the
341 * Linux ->setattr API.
342 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000343int
344xfs_attr_set(
345 struct xfs_inode *dp,
346 const unsigned char *name,
Allison Hendersond29f7812020-01-07 15:26:15 -0800347 size_t namelen,
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000348 unsigned char *value,
349 int valuelen,
350 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Jie Liu3d3c8b52013-08-12 20:49:59 +1000352 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000353 struct xfs_da_args args;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000354 struct xfs_trans_res tres;
355 int rsvd = (flags & ATTR_ROOT) != 0;
Allison Henderson4c74a562018-10-18 17:20:50 +1100356 int error, local;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800357 unsigned int total;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000358
359 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000360 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000361
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800362 error = xfs_qm_dqattach(dp);
363 if (error)
364 return error;
365
Allison Hendersond29f7812020-01-07 15:26:15 -0800366 error = xfs_attr_args_init(&args, dp, name, namelen, flags);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000367 if (error)
368 return error;
369
Christoph Hellwig67fd7182014-05-13 16:34:43 +1000370 args.value = value;
371 args.valuelen = valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /*
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800374 * We have no control over the attribute names that userspace passes us
375 * to remove, so we have to allow the name lookup prior to attribute
376 * removal to fail as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800378 args.op_flags = XFS_DA_OP_OKNOENT;
Barry Naujoke5889e92007-02-10 18:35:58 +1100379
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800380 if (value) {
381 XFS_STATS_INC(mp, xs_attr_set);
382
383 args.op_flags |= XFS_DA_OP_ADDNAME;
384 args.total = xfs_attr_calc_size(&args, &local);
385
386 /*
387 * If the inode doesn't have an attribute fork, add one.
388 * (inode must not be locked when we call this routine)
389 */
390 if (XFS_IFORK_Q(dp) == 0) {
391 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
392 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen,
393 valuelen);
394
395 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
396 if (error)
397 return error;
398 }
399
400 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
401 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
402 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
403 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
404 total = args.total;
405 } else {
406 XFS_STATS_INC(mp, xs_attr_remove);
407
408 tres = M_RES(mp)->tr_attrrm;
409 total = XFS_ATTRRM_SPACE_RES(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * Root fork attributes can use reserved data blocks for this
414 * operation if necessary
415 */
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800416 error = xfs_trans_alloc(mp, &tres, total, 0,
Christoph Hellwig253f4912016-04-06 09:19:55 +1000417 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
418 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000419 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Christoph Hellwig253f4912016-04-06 09:19:55 +1000421 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +0000422 xfs_trans_ijoin(args.trans, dp, 0);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800423 if (value) {
424 unsigned int quota_flags = XFS_QMOPT_RES_REGBLKS;
425
426 if (rsvd)
427 quota_flags |= XFS_QMOPT_FORCE_RES;
428 error = xfs_trans_reserve_quota_nblks(args.trans, dp,
429 args.total, 0, quota_flags);
430 if (error)
431 goto out_trans_cancel;
432 error = xfs_attr_set_args(&args);
433 if (error)
434 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100435 /* shortform attribute has already been committed */
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800436 if (!args.trans)
437 goto out_unlock;
438 } else {
439 error = xfs_attr_remove_args(&args);
440 if (error)
441 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /*
445 * If this is a synchronous mount, make sure that the
446 * transaction goes to disk before returning to the user.
447 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000448 if (mp->m_flags & XFS_MOUNT_WSYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 xfs_trans_set_sync(args.trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000451 if ((flags & ATTR_KERNOTIME) == 0)
452 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * Commit the last in the sequence of transactions.
456 */
457 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
Christoph Hellwig70393312015-06-04 13:48:08 +1000458 error = xfs_trans_commit(args.trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100459out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000461 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Allison Henderson2f3cd802018-10-18 17:21:16 +1100463out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +1000464 if (args.trans)
465 xfs_trans_cancel(args.trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100466 goto out_unlock;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*========================================================================
470 * External routines when attribute list is inside the inode
471 *========================================================================*/
472
473/*
474 * Add a name to the shortform attribute list structure
475 * This is the external routine.
476 */
477STATIC int
478xfs_attr_shortform_addname(xfs_da_args_t *args)
479{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100480 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Dave Chinner5a5881c2012-03-22 05:15:13 +0000482 trace_xfs_attr_sf_addname(args);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000485 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000486 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000487 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000489 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700491 if (retval)
492 return retval;
493 /*
494 * Since we have removed the old attr, clear ATTR_REPLACE so
495 * that the leaf format add routine won't trip over the attr
496 * not being around.
497 */
498 args->flags &= ~ATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
Nathan Scottd8cc8902005-11-02 10:34:53 +1100501 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
502 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000503 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
506 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100507
508 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
509 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000510 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100511
512 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516
517/*========================================================================
518 * External routines when attribute list is one block
519 *========================================================================*/
520
521/*
522 * Add a name to the leaf attribute list structure
523 *
524 * This leaf block cannot have a "remote" value, we only call this routine
525 * if bmap_one_block() says there is only one block (ie: no remote blks).
526 */
David Chinnera8272ce2007-11-23 16:28:09 +1100527STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700528xfs_attr_leaf_addname(
529 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700531 struct xfs_inode *dp;
532 struct xfs_buf *bp;
533 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Dave Chinner5a5881c2012-03-22 05:15:13 +0000535 trace_xfs_attr_leaf_addname(args);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * Read the (only) block in the attribute list in.
539 */
540 dp = args->dp;
541 args->blkno = 0;
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800542 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100544 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /*
547 * Look up the given attribute in the leaf block. Figure out if
548 * the given flags produce an error or call for an atomic rename.
549 */
Dave Chinner517c2222013-04-24 18:58:55 +1000550 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000551 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000552 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000553 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000554 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000556 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000557 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000559
560 trace_xfs_attr_leaf_replace(args);
561
Dave Chinner8275cdd2014-05-06 07:37:31 +1000562 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000563 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 args->blkno2 = args->blkno; /* set 2nd entry info*/
565 args->index2 = args->index;
566 args->rmtblkno2 = args->rmtblkno;
567 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000568 args->rmtvaluelen2 = args->rmtvaluelen;
569
570 /*
571 * clear the remote attr state now that it is saved so that the
572 * values reflect the state of the attribute we are about to
573 * add, not the attribute we just found and will remove later.
574 */
575 args->rmtblkno = 0;
576 args->rmtblkcnt = 0;
577 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 /*
581 * Add the attribute to the leaf block, transitioning to a Btree
582 * if required.
583 */
Dave Chinner517c2222013-04-24 18:58:55 +1000584 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000585 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
587 * Promote the attribute list to the Btree format, then
588 * Commit that transaction so that the node_addname() call
589 * can manage its own transactions.
590 */
Dave Chinner517c2222013-04-24 18:58:55 +1000591 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700592 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000593 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700594 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700595 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700596 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * Commit the current trans (including the inode) and start
600 * a new one.
601 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700602 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000603 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000604 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /*
607 * Fob the whole rest of the problem off on the Btree code.
608 */
609 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000610 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 /*
614 * Commit the transaction that added the attr name so that
615 * later routines can manage their own transactions.
616 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700617 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000618 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000619 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /*
622 * If there was an out-of-line value, allocate the blocks we
623 * identified for its storage and copy the value. This is done
624 * after we create the attribute so that we don't overflow the
625 * maximum size of a transaction and/or hit a deadlock.
626 */
627 if (args->rmtblkno > 0) {
628 error = xfs_attr_rmtval_set(args);
629 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000630 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
633 /*
634 * If this is an atomic rename operation, we must "flip" the
635 * incomplete flags on the "new" and "old" attribute/value pairs
636 * so that one disappears and one appears atomically. Then we
637 * must remove the "old" attribute/value pair.
638 */
Barry Naujok6a178102008-05-21 16:42:05 +1000639 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * In a separate transaction, set the incomplete flag on the
642 * "old" attr and clear the incomplete flag on the "new" attr.
643 */
Dave Chinner517c2222013-04-24 18:58:55 +1000644 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000646 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /*
649 * Dismantle the "old" attribute/value pair by removing
650 * a "remote" value (if it exists).
651 */
652 args->index = args->index2;
653 args->blkno = args->blkno2;
654 args->rmtblkno = args->rmtblkno2;
655 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000656 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (args->rmtblkno) {
658 error = xfs_attr_rmtval_remove(args);
659 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000660 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 /*
664 * Read in the block containing the "old" attr, then
665 * remove the "old" attr from that block (neat, huh!)
666 */
Dave Chinner517c2222013-04-24 18:58:55 +1000667 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800668 &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100670 return error;
671
Dave Chinner517c2222013-04-24 18:58:55 +1000672 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /*
675 * If the result is small enough, shrink it all into the inode.
676 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100677 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +1000678 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700680 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000681 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700682 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700683 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700684 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /*
688 * Commit the remove and start the next trans in series.
689 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700690 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 } else if (args->rmtblkno > 0) {
693 /*
694 * Added a "remote" value, just clear the incomplete flag.
695 */
Dave Chinner517c2222013-04-24 18:58:55 +1000696 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Dave Chinner517c2222013-04-24 18:58:55 +1000698 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701/*
702 * Remove a name from the leaf attribute list structure
703 *
704 * This leaf block cannot have a "remote" value, we only call this routine
705 * if bmap_one_block() says there is only one block (ie: no remote blks).
706 */
707STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700708xfs_attr_leaf_removename(
709 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700711 struct xfs_inode *dp;
712 struct xfs_buf *bp;
713 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dave Chinner5a5881c2012-03-22 05:15:13 +0000715 trace_xfs_attr_leaf_removename(args);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /*
718 * Remove the attribute.
719 */
720 dp = args->dp;
721 args->blkno = 0;
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800722 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100723 if (error)
724 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dave Chinner517c2222013-04-24 18:58:55 +1000726 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000727 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000728 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000729 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
Dave Chinner517c2222013-04-24 18:58:55 +1000732 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /*
735 * If the result is small enough, shrink it all into the inode.
736 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100737 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +1000738 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700740 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000741 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700742 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700743 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700744 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000745 }
Dave Chinner517c2222013-04-24 18:58:55 +1000746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*
750 * Look up a name in a leaf attribute list structure.
751 *
752 * This leaf block cannot have a "remote" value, we only call this routine
753 * if bmap_one_block() says there is only one block (ie: no remote blks).
Dave Chinner728bcaa2019-08-29 09:04:08 -0700754 *
755 * Returns 0 on successful retrieval, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000757STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758xfs_attr_leaf_get(xfs_da_args_t *args)
759{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000760 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int error;
762
Dave Chinneree732592012-11-12 22:53:53 +1100763 trace_xfs_attr_leaf_get(args);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 args->blkno = 0;
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800766 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100768 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Dave Chinner517c2222013-04-24 18:58:55 +1000770 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000771 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000772 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000773 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Dave Chinner517c2222013-04-24 18:58:55 +1000775 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000776 xfs_trans_brelse(args->trans, bp);
Dave Chinnere3cc4552019-08-29 09:04:09 -0700777 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000781 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *========================================================================*/
783
784/*
785 * Add a name to a Btree-format attribute list.
786 *
787 * This will involve walking down the Btree, and may involve splitting
788 * leaf nodes and even splitting intermediate nodes up to and including
789 * the root node (a special case of an intermediate node).
790 *
791 * "Remote" attribute values confuse the issue and atomic rename operations
792 * add a whole extra layer of confusion on top of that.
793 */
794STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700795xfs_attr_node_addname(
796 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700798 struct xfs_da_state *state;
799 struct xfs_da_state_blk *blk;
800 struct xfs_inode *dp;
801 struct xfs_mount *mp;
802 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dave Chinner5a5881c2012-03-22 05:15:13 +0000804 trace_xfs_attr_node_addname(args);
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * Fill in bucket of arguments/results/context to carry around.
808 */
809 dp = args->dp;
810 mp = dp->i_mount;
811restart:
812 state = xfs_da_state_alloc();
813 state->args = args;
814 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /*
817 * Search to see if name already exists, and get back a pointer
818 * to where it should go.
819 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000820 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (error)
822 goto out;
823 blk = &state->path.blk[ state->path.active-1 ];
824 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000825 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000827 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (args->flags & ATTR_CREATE)
829 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000830
831 trace_xfs_attr_node_replace(args);
832
Dave Chinner8275cdd2014-05-06 07:37:31 +1000833 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000834 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 args->blkno2 = args->blkno; /* set 2nd entry info*/
836 args->index2 = args->index;
837 args->rmtblkno2 = args->rmtblkno;
838 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000839 args->rmtvaluelen2 = args->rmtvaluelen;
840
841 /*
842 * clear the remote attr state now that it is saved so that the
843 * values reflect the state of the attribute we are about to
844 * add, not the attribute we just found and will remove later.
845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 args->rmtblkno = 0;
847 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000848 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
Dave Chinner517c2222013-04-24 18:58:55 +1000851 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000852 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (state->path.active == 1) {
854 /*
855 * Its really a single leaf node, but it had
856 * out-of-line values so it looked like it *might*
857 * have been a b-tree.
858 */
859 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500860 state = NULL;
Dave Chinner517c2222013-04-24 18:58:55 +1000861 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700862 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000863 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700864 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700865 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * Commit the node conversion and start the next
870 * trans in the chain.
871 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700872 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000873 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto out;
875
876 goto restart;
877 }
878
879 /*
880 * Split as many Btree elements as required.
881 * This code tracks the new and old attr's location
882 * in the index/blkno/rmtblkno/rmtblkcnt fields and
883 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
884 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000885 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700886 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000887 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700888 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700889 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700890 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 } else {
892 /*
893 * Addition succeeded, update Btree hashvals.
894 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000895 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 /*
899 * Kill the state structure, we're done with it and need to
900 * allow the buffers to come back later.
901 */
902 xfs_da_state_free(state);
903 state = NULL;
904
905 /*
906 * Commit the leaf addition or btree split and start the next
907 * trans in the chain.
908 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700909 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000910 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto out;
912
913 /*
914 * If there was an out-of-line value, allocate the blocks we
915 * identified for its storage and copy the value. This is done
916 * after we create the attribute so that we don't overflow the
917 * maximum size of a transaction and/or hit a deadlock.
918 */
919 if (args->rmtblkno > 0) {
920 error = xfs_attr_rmtval_set(args);
921 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000922 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 /*
926 * If this is an atomic rename operation, we must "flip" the
927 * incomplete flags on the "new" and "old" attribute/value pairs
928 * so that one disappears and one appears atomically. Then we
929 * must remove the "old" attribute/value pair.
930 */
Barry Naujok6a178102008-05-21 16:42:05 +1000931 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * In a separate transaction, set the incomplete flag on the
934 * "old" attr and clear the incomplete flag on the "new" attr.
935 */
Dave Chinner517c2222013-04-24 18:58:55 +1000936 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (error)
938 goto out;
939
940 /*
941 * Dismantle the "old" attribute/value pair by removing
942 * a "remote" value (if it exists).
943 */
944 args->index = args->index2;
945 args->blkno = args->blkno2;
946 args->rmtblkno = args->rmtblkno2;
947 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000948 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (args->rmtblkno) {
950 error = xfs_attr_rmtval_remove(args);
951 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000952 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
955 /*
956 * Re-find the "old" attribute entry after any split ops.
957 * The INCOMPLETE flag means that we will find the "old"
958 * attr, not the "new" one.
959 */
Christoph Hellwig780d2902020-01-07 15:25:39 -0800960 args->op_flags |= XFS_DA_OP_INCOMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 state = xfs_da_state_alloc();
962 state->args = args;
963 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000965 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (error)
967 goto out;
968
969 /*
970 * Remove the name and update the hashvals in the tree.
971 */
972 blk = &state->path.blk[ state->path.active-1 ];
973 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000974 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000975 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /*
978 * Check to see if the tree needs to be collapsed.
979 */
980 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000981 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700982 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000983 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700984 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700985 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
989 /*
990 * Commit and start the next trans in the chain.
991 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700992 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000993 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto out;
995
996 } else if (args->rmtblkno > 0) {
997 /*
998 * Added a "remote" value, just clear the incomplete flag.
999 */
Dave Chinner517c2222013-04-24 18:58:55 +10001000 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (error)
1002 goto out;
1003 }
1004 retval = error = 0;
1005
1006out:
1007 if (state)
1008 xfs_da_state_free(state);
1009 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001010 return error;
1011 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
1015 * Remove a name from a B-tree attribute list.
1016 *
1017 * This will involve walking down the Btree, and may involve joining
1018 * leaf nodes and even joining intermediate nodes up to and including
1019 * the root node (a special case of an intermediate node).
1020 */
1021STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -07001022xfs_attr_node_removename(
1023 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Brian Foster32a9b7c2018-07-11 22:26:11 -07001025 struct xfs_da_state *state;
1026 struct xfs_da_state_blk *blk;
1027 struct xfs_inode *dp;
1028 struct xfs_buf *bp;
1029 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dave Chinner5a5881c2012-03-22 05:15:13 +00001031 trace_xfs_attr_node_removename(args);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
1034 * Tie a string around our finger to remind us where we are.
1035 */
1036 dp = args->dp;
1037 state = xfs_da_state_alloc();
1038 state->args = args;
1039 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /*
1042 * Search to see if name exists, and get back a pointer to it.
1043 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001044 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +10001045 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (error == 0)
1047 error = retval;
1048 goto out;
1049 }
1050
1051 /*
1052 * If there is an out-of-line value, de-allocate the blocks.
1053 * This is done before we remove the attribute so that we don't
1054 * overflow the maximum size of a transaction and/or hit a deadlock.
1055 */
1056 blk = &state->path.blk[ state->path.active-1 ];
1057 ASSERT(blk->bp != NULL);
1058 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1059 if (args->rmtblkno > 0) {
1060 /*
1061 * Fill in disk block numbers in the state structure
1062 * so that we can get the buffers back after we commit
1063 * several transactions in the following calls.
1064 */
1065 error = xfs_attr_fillstate(state);
1066 if (error)
1067 goto out;
1068
1069 /*
1070 * Mark the attribute as INCOMPLETE, then bunmapi() the
1071 * remote value.
1072 */
Dave Chinner517c2222013-04-24 18:58:55 +10001073 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (error)
1075 goto out;
1076 error = xfs_attr_rmtval_remove(args);
1077 if (error)
1078 goto out;
1079
1080 /*
1081 * Refill the state structure with buffers, the prior calls
1082 * released our buffers.
1083 */
1084 error = xfs_attr_refillstate(state);
1085 if (error)
1086 goto out;
1087 }
1088
1089 /*
1090 * Remove the name and update the hashvals in the tree.
1091 */
1092 blk = &state->path.blk[ state->path.active-1 ];
1093 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001094 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001095 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /*
1098 * Check to see if the tree needs to be collapsed.
1099 */
1100 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001101 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001102 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001103 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001104 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001105 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /*
1108 * Commit the Btree join operation and start a new trans.
1109 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001110 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001111 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 goto out;
1113 }
1114
1115 /*
1116 * If the result is small enough, push it all into the inode.
1117 */
1118 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1119 /*
1120 * Have to get rid of the copy of this dabuf in the state.
1121 */
1122 ASSERT(state->path.active == 1);
1123 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 state->path.blk[0].bp = NULL;
1125
Christoph Hellwigdfb87592019-11-20 09:46:02 -08001126 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (error)
1128 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Nathan Scottd8cc8902005-11-02 10:34:53 +11001130 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +10001131 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001133 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001134 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001135 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001136 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001139 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 error = 0;
1142
1143out:
1144 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001145 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148/*
1149 * Fill in the disk block numbers in the state structure for the buffers
1150 * that are attached to the state structure.
1151 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001152 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
1154STATIC int
1155xfs_attr_fillstate(xfs_da_state_t *state)
1156{
1157 xfs_da_state_path_t *path;
1158 xfs_da_state_blk_t *blk;
1159 int level;
1160
Dave Chinneree732592012-11-12 22:53:53 +11001161 trace_xfs_attr_fillstate(state->args);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /*
1164 * Roll down the "path" in the state structure, storing the on-disk
1165 * block number for those buffers in the "path".
1166 */
1167 path = &state->path;
1168 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1169 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1170 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001171 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 blk->bp = NULL;
1173 } else {
1174 blk->disk_blkno = 0;
1175 }
1176 }
1177
1178 /*
1179 * Roll down the "altpath" in the state structure, storing the on-disk
1180 * block number for those buffers in the "altpath".
1181 */
1182 path = &state->altpath;
1183 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1184 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1185 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001186 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 blk->bp = NULL;
1188 } else {
1189 blk->disk_blkno = 0;
1190 }
1191 }
1192
Eric Sandeend99831f2014-06-22 15:03:54 +10001193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196/*
1197 * Reattach the buffers to the state structure based on the disk block
1198 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001199 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * buffers from our grip.
1201 */
1202STATIC int
1203xfs_attr_refillstate(xfs_da_state_t *state)
1204{
1205 xfs_da_state_path_t *path;
1206 xfs_da_state_blk_t *blk;
1207 int level, error;
1208
Dave Chinneree732592012-11-12 22:53:53 +11001209 trace_xfs_attr_refillstate(state->args);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Roll down the "path" in the state structure, storing the on-disk
1213 * block number for those buffers in the "path".
1214 */
1215 path = &state->path;
1216 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1217 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1218 if (blk->disk_blkno) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001219 error = xfs_da3_node_read_mapped(state->args->trans,
1220 state->args->dp, blk->disk_blkno,
1221 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001223 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 } else {
1225 blk->bp = NULL;
1226 }
1227 }
1228
1229 /*
1230 * Roll down the "altpath" in the state structure, storing the on-disk
1231 * block number for those buffers in the "altpath".
1232 */
1233 path = &state->altpath;
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) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001237 error = xfs_da3_node_read_mapped(state->args->trans,
1238 state->args->dp, blk->disk_blkno,
1239 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 } else {
1243 blk->bp = NULL;
1244 }
1245 }
1246
Eric Sandeend99831f2014-06-22 15:03:54 +10001247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250/*
Dave Chinner728bcaa2019-08-29 09:04:08 -07001251 * Retrieve the attribute data from a node attribute list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 *
1253 * This routine gets called for any attribute fork that has more than one
1254 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1255 * "remote" values taking up more blocks.
Dave Chinner728bcaa2019-08-29 09:04:08 -07001256 *
1257 * Returns 0 on successful retrieval, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001259STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260xfs_attr_node_get(xfs_da_args_t *args)
1261{
1262 xfs_da_state_t *state;
1263 xfs_da_state_blk_t *blk;
1264 int error, retval;
1265 int i;
1266
Dave Chinneree732592012-11-12 22:53:53 +11001267 trace_xfs_attr_node_get(args);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 state = xfs_da_state_alloc();
1270 state->args = args;
1271 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /*
1274 * Search to see if name exists, and get back a pointer to it.
1275 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001276 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (error) {
1278 retval = error;
Dave Chinner728bcaa2019-08-29 09:04:08 -07001279 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Dave Chinner728bcaa2019-08-29 09:04:08 -07001281 if (retval != -EEXIST)
1282 goto out_release;
1283
1284 /*
1285 * Get the value, local or "remote"
1286 */
1287 blk = &state->path.blk[state->path.active - 1];
1288 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /*
1291 * If not in a transaction, we have to release all the buffers.
1292 */
Dave Chinner728bcaa2019-08-29 09:04:08 -07001293out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001295 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 state->path.blk[i].bp = NULL;
1297 }
1298
1299 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001300 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
Darrick J. Wong65480532019-02-01 09:08:54 -08001302
1303/* Returns true if the attribute entry name is valid. */
1304bool
1305xfs_attr_namecheck(
1306 const void *name,
1307 size_t length)
1308{
1309 /*
1310 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1311 * out, so use >= for the length check.
1312 */
1313 if (length >= MAXNAMELEN)
1314 return false;
1315
1316 /* There shouldn't be any nulls here */
1317 return !memchr(name, 0, length);
1318}