blob: f7f796ea11fdefc3e7d686707e0a3861c4c3a31f [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);
Allison Collins07120f12020-07-20 21:47:22 -070049STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * Internal routines when attribute list is more than one block.
53 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100054STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
56STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Allison Collins07120f12020-07-20 21:47:22 -070057STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
58 struct xfs_da_state **state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
60STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
61
Dave Chinnerabec5f22013-08-12 20:49:38 +100062int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100063xfs_inode_hasattr(
64 struct xfs_inode *ip)
65{
66 if (!XFS_IFORK_Q(ip) ||
Christoph Hellwigf7e67b22020-05-18 10:28:05 -070067 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -070068 ip->i_afp->if_nextents == 0))
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100069 return 0;
70 return 1;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*========================================================================
74 * Overall external interface routines.
75 *========================================================================*/
76
Dave Chinner728bcaa2019-08-29 09:04:08 -070077/*
78 * Retrieve an extended attribute and its value. Must have ilock.
79 * Returns 0 on successful retrieval, otherwise an error.
80 */
Darrick J. Wongad017f62017-06-16 11:00:14 -070081int
82xfs_attr_get_ilocked(
Darrick J. Wongad017f62017-06-16 11:00:14 -070083 struct xfs_da_args *args)
84{
Christoph Hellwigc36f5332020-02-26 17:30:34 -080085 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Christoph Hellwigcf69f822017-07-13 12:14:33 -070086
Christoph Hellwigc36f5332020-02-26 17:30:34 -080087 if (!xfs_inode_hasattr(args->dp))
Darrick J. Wongad017f62017-06-16 11:00:14 -070088 return -ENOATTR;
Christoph Hellwigc36f5332020-02-26 17:30:34 -080089
Christoph Hellwigf7e67b22020-05-18 10:28:05 -070090 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
Darrick J. Wongad017f62017-06-16 11:00:14 -070091 return xfs_attr_shortform_getvalue(args);
Christoph Hellwigc36f5332020-02-26 17:30:34 -080092 if (xfs_bmap_one_block(args->dp, XFS_ATTR_FORK))
Darrick J. Wongad017f62017-06-16 11:00:14 -070093 return xfs_attr_leaf_get(args);
Christoph Hellwigc36f5332020-02-26 17:30:34 -080094 return xfs_attr_node_get(args);
Darrick J. Wongad017f62017-06-16 11:00:14 -070095}
96
Dave Chinnerddbca702019-08-29 09:04:10 -070097/*
98 * Retrieve an extended attribute by name, and its value if requested.
99 *
Christoph Hellwige513e252020-02-26 17:30:35 -0800100 * If args->valuelen is zero, then the caller does not want the value, just an
101 * indication whether the attribute exists and the size of the value if it
102 * exists. The size is returned in args.valuelen.
Dave Chinnerddbca702019-08-29 09:04:10 -0700103 *
Christoph Hellwigd49db182020-02-26 17:30:35 -0800104 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
105 * for the value after existence of the attribute has been determined. The
106 * caller always has to free args->value if it is set, no matter if this
107 * function was successful or not.
108 *
Dave Chinnerddbca702019-08-29 09:04:10 -0700109 * If the attribute is found, but exceeds the size limit set by the caller in
Christoph Hellwige5171d72020-02-26 17:30:34 -0800110 * args->valuelen, return -ERANGE with the size of the attribute that was found
111 * in args->valuelen.
Dave Chinnerddbca702019-08-29 09:04:10 -0700112 */
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000113int
114xfs_attr_get(
Christoph Hellwige5171d72020-02-26 17:30:34 -0800115 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000117 uint lock_mode;
118 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Christoph Hellwige5171d72020-02-26 17:30:34 -0800120 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Christoph Hellwige5171d72020-02-26 17:30:34 -0800122 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000123 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Christoph Hellwige5171d72020-02-26 17:30:34 -0800125 args->geo = args->dp->i_mount->m_attr_geo;
126 args->whichfork = XFS_ATTR_FORK;
127 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000128
Eric Sandeenc400ee32015-08-19 10:30:48 +1000129 /* Entirely possible to look up a name which doesn't exist */
Christoph Hellwige5171d72020-02-26 17:30:34 -0800130 args->op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Christoph Hellwige5171d72020-02-26 17:30:34 -0800132 lock_mode = xfs_ilock_attr_map_shared(args->dp);
Christoph Hellwigc36f5332020-02-26 17:30:34 -0800133 error = xfs_attr_get_ilocked(args);
Christoph Hellwige5171d72020-02-26 17:30:34 -0800134 xfs_iunlock(args->dp, lock_mode);
Dave Chinnerddbca702019-08-29 09:04:10 -0700135
Christoph Hellwige5171d72020-02-26 17:30:34 -0800136 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000139/*
140 * Calculate how many blocks we need for the new attribute,
141 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000142STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000143xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000144 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000145 int *local)
146{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000147 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000148 int size;
149 int nblks;
150
151 /*
152 * Determine space new attribute will use, and if it would be
153 * "local" or "remote" (note: local != inline).
154 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000155 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000156 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
157 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000158 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000159 /* Double split possible */
160 nblks *= 2;
161 }
162 } else {
163 /*
164 * Out of line attribute, cannot double split, but
165 * make room for the attribute value itself.
166 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000167 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000168 nblks += dblocks;
169 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
170 }
171
172 return nblks;
173}
174
Allison Henderson4c74a562018-10-18 17:20:50 +1100175STATIC int
176xfs_attr_try_sf_addname(
177 struct xfs_inode *dp,
178 struct xfs_da_args *args)
179{
180
181 struct xfs_mount *mp = dp->i_mount;
182 int error, error2;
183
184 error = xfs_attr_shortform_addname(args);
185 if (error == -ENOSPC)
186 return error;
187
188 /*
189 * Commit the shortform mods, and we're done.
190 * NOTE: this is also the error path (EEXIST, etc).
191 */
Christoph Hellwig1d733012020-02-26 17:30:36 -0800192 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
Allison Henderson4c74a562018-10-18 17:20:50 +1100193 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
194
195 if (mp->m_flags & XFS_MOUNT_WSYNC)
196 xfs_trans_set_sync(args->trans);
197
198 error2 = xfs_trans_commit(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100199 args->trans = NULL;
Allison Henderson4c74a562018-10-18 17:20:50 +1100200 return error ? error : error2;
201}
202
Allison Henderson2f3cd802018-10-18 17:21:16 +1100203/*
204 * Set the attribute specified in @args.
205 */
206int
207xfs_attr_set_args(
Darrick J. Wong710d7072019-04-24 09:27:41 -0700208 struct xfs_da_args *args)
Allison Henderson2f3cd802018-10-18 17:21:16 +1100209{
210 struct xfs_inode *dp = args->dp;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700211 struct xfs_buf *leaf_bp = NULL;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100212 int error;
213
214 /*
215 * If the attribute list is non-existent or a shortform list,
216 * upgrade it to a single-leaf-block attribute list.
217 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700218 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
219 (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwigdaf83962020-05-18 10:27:22 -0700220 dp->i_afp->if_nextents == 0)) {
Allison Henderson2f3cd802018-10-18 17:21:16 +1100221
222 /*
223 * Build initial attribute list (if required).
224 */
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700225 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
Allison Henderson2f3cd802018-10-18 17:21:16 +1100226 xfs_attr_shortform_create(args);
227
228 /*
229 * Try to add the attr to the attribute list in the inode.
230 */
231 error = xfs_attr_try_sf_addname(dp, args);
232 if (error != -ENOSPC)
233 return error;
234
235 /*
236 * It won't fit in the shortform, transform to a leaf block.
237 * GROT: another possible req'mt for a double-split btree op.
238 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700239 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100240 if (error)
241 return error;
242
243 /*
244 * Prevent the leaf buffer from being unlocked so that a
245 * concurrent AIL push cannot grab the half-baked leaf
246 * buffer and run into problems with the write verifier.
Darrick J. Wong710d7072019-04-24 09:27:41 -0700247 * Once we're done rolling the transaction we can release
248 * the hold and add the attr to the leaf.
Allison Henderson2f3cd802018-10-18 17:21:16 +1100249 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700250 xfs_trans_bhold(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100251 error = xfs_defer_finish(&args->trans);
Darrick J. Wong710d7072019-04-24 09:27:41 -0700252 xfs_trans_bhold_release(args->trans, leaf_bp);
253 if (error) {
254 xfs_trans_brelse(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100255 return error;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700256 }
Allison Henderson2f3cd802018-10-18 17:21:16 +1100257 }
258
259 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
260 error = xfs_attr_leaf_addname(args);
261 else
262 error = xfs_attr_node_addname(args);
263 return error;
264}
265
Allison Henderson068f9852018-10-18 17:21:23 +1100266/*
Allison Collins07120f12020-07-20 21:47:22 -0700267 * Return EEXIST if attr is found, or ENOATTR if not
268 */
269int
270xfs_has_attr(
271 struct xfs_da_args *args)
272{
273 struct xfs_inode *dp = args->dp;
274 struct xfs_buf *bp = NULL;
275 int error;
276
277 if (!xfs_inode_hasattr(dp))
278 return -ENOATTR;
279
280 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
281 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
282 return xfs_attr_sf_findname(args, NULL, NULL);
283 }
284
285 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
286 error = xfs_attr_leaf_hasname(args, &bp);
287
288 if (bp)
289 xfs_trans_brelse(args->trans, bp);
290
291 return error;
292 }
293
294 return xfs_attr_node_hasname(args, NULL);
295}
296
297/*
Allison Henderson068f9852018-10-18 17:21:23 +1100298 * Remove the attribute specified in @args.
299 */
300int
301xfs_attr_remove_args(
302 struct xfs_da_args *args)
303{
304 struct xfs_inode *dp = args->dp;
305 int error;
306
307 if (!xfs_inode_hasattr(dp)) {
308 error = -ENOATTR;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700309 } else if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
Allison Henderson068f9852018-10-18 17:21:23 +1100310 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
311 error = xfs_attr_shortform_remove(args);
312 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
313 error = xfs_attr_leaf_removename(args);
314 } else {
315 error = xfs_attr_node_removename(args);
316 }
317
318 return error;
319}
320
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800321/*
Christoph Hellwiga2544622020-02-26 17:30:33 -0800322 * Note: If args->value is NULL the attribute will be removed, just like the
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800323 * Linux ->setattr API.
324 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000325int
326xfs_attr_set(
Christoph Hellwiga2544622020-02-26 17:30:33 -0800327 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Christoph Hellwiga2544622020-02-26 17:30:33 -0800329 struct xfs_inode *dp = args->dp;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000330 struct xfs_mount *mp = dp->i_mount;
331 struct xfs_trans_res tres;
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800332 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
Allison Henderson4c74a562018-10-18 17:20:50 +1100333 int error, local;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800334 unsigned int total;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000335
336 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000337 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000338
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800339 error = xfs_qm_dqattach(dp);
340 if (error)
341 return error;
342
Christoph Hellwiga2544622020-02-26 17:30:33 -0800343 args->geo = mp->m_attr_geo;
344 args->whichfork = XFS_ATTR_FORK;
345 args->hashval = xfs_da_hashname(args->name, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /*
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800348 * We have no control over the attribute names that userspace passes us
349 * to remove, so we have to allow the name lookup prior to attribute
350 * removal to fail as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800352 args->op_flags = XFS_DA_OP_OKNOENT;
Barry Naujoke5889e92007-02-10 18:35:58 +1100353
Christoph Hellwiga2544622020-02-26 17:30:33 -0800354 if (args->value) {
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800355 XFS_STATS_INC(mp, xs_attr_set);
356
Christoph Hellwiga2544622020-02-26 17:30:33 -0800357 args->op_flags |= XFS_DA_OP_ADDNAME;
358 args->total = xfs_attr_calc_size(args, &local);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800359
360 /*
361 * If the inode doesn't have an attribute fork, add one.
362 * (inode must not be locked when we call this routine)
363 */
364 if (XFS_IFORK_Q(dp) == 0) {
365 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
Christoph Hellwiga2544622020-02-26 17:30:33 -0800366 XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen,
367 args->valuelen);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800368
369 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
370 if (error)
371 return error;
372 }
373
374 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
Christoph Hellwiga2544622020-02-26 17:30:33 -0800375 M_RES(mp)->tr_attrsetrt.tr_logres *
376 args->total;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800377 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
378 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800379 total = args->total;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800380 } else {
381 XFS_STATS_INC(mp, xs_attr_remove);
382
383 tres = M_RES(mp)->tr_attrrm;
384 total = XFS_ATTRRM_SPACE_RES(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
388 * Root fork attributes can use reserved data blocks for this
389 * operation if necessary
390 */
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800391 error = xfs_trans_alloc(mp, &tres, total, 0,
Christoph Hellwiga2544622020-02-26 17:30:33 -0800392 rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000393 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000394 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Christoph Hellwig253f4912016-04-06 09:19:55 +1000396 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800397 xfs_trans_ijoin(args->trans, dp, 0);
398 if (args->value) {
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800399 unsigned int quota_flags = XFS_QMOPT_RES_REGBLKS;
400
401 if (rsvd)
402 quota_flags |= XFS_QMOPT_FORCE_RES;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800403 error = xfs_trans_reserve_quota_nblks(args->trans, dp,
404 args->total, 0, quota_flags);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800405 if (error)
406 goto out_trans_cancel;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800407 error = xfs_attr_set_args(args);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800408 if (error)
409 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100410 /* shortform attribute has already been committed */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800411 if (!args->trans)
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800412 goto out_unlock;
413 } else {
Christoph Hellwiga2544622020-02-26 17:30:33 -0800414 error = xfs_attr_remove_args(args);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800415 if (error)
416 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /*
420 * If this is a synchronous mount, make sure that the
421 * transaction goes to disk before returning to the user.
422 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000423 if (mp->m_flags & XFS_MOUNT_WSYNC)
Christoph Hellwiga2544622020-02-26 17:30:33 -0800424 xfs_trans_set_sync(args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Christoph Hellwig1d733012020-02-26 17:30:36 -0800426 if (!(args->op_flags & XFS_DA_OP_NOTIME))
Christoph Hellwiga2544622020-02-26 17:30:33 -0800427 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
430 * Commit the last in the sequence of transactions.
431 */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800432 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
433 error = xfs_trans_commit(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100434out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000436 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Allison Henderson2f3cd802018-10-18 17:21:16 +1100438out_trans_cancel:
Christoph Hellwiga2544622020-02-26 17:30:33 -0800439 if (args->trans)
440 xfs_trans_cancel(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100441 goto out_unlock;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*========================================================================
445 * External routines when attribute list is inside the inode
446 *========================================================================*/
447
448/*
449 * Add a name to the shortform attribute list structure
450 * This is the external routine.
451 */
452STATIC int
453xfs_attr_shortform_addname(xfs_da_args_t *args)
454{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100455 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Dave Chinner5a5881c2012-03-22 05:15:13 +0000457 trace_xfs_attr_sf_addname(args);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 retval = xfs_attr_shortform_lookup(args);
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800460 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Eric Sandeend99831f2014-06-22 15:03:54 +1000461 return retval;
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800462 if (retval == -EEXIST) {
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800463 if (args->attr_flags & XATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000464 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700466 if (retval)
467 return retval;
468 /*
469 * Since we have removed the old attr, clear ATTR_REPLACE so
470 * that the leaf format add routine won't trip over the attr
471 * not being around.
472 */
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800473 args->attr_flags &= ~XATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Nathan Scottd8cc8902005-11-02 10:34:53 +1100476 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
477 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000478 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
481 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100482
483 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
484 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000485 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100486
487 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491
492/*========================================================================
493 * External routines when attribute list is one block
494 *========================================================================*/
495
496/*
497 * Add a name to the leaf attribute list structure
498 *
499 * This leaf block cannot have a "remote" value, we only call this routine
500 * if bmap_one_block() says there is only one block (ie: no remote blks).
501 */
David Chinnera8272ce2007-11-23 16:28:09 +1100502STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700503xfs_attr_leaf_addname(
504 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700506 struct xfs_buf *bp;
507 int retval, error, forkoff;
Allison Collins07120f12020-07-20 21:47:22 -0700508 struct xfs_inode *dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Dave Chinner5a5881c2012-03-22 05:15:13 +0000510 trace_xfs_attr_leaf_addname(args);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * Look up the given attribute in the leaf block. Figure out if
514 * the given flags produce an error or call for an atomic rename.
515 */
Allison Collins07120f12020-07-20 21:47:22 -0700516 retval = xfs_attr_leaf_hasname(args, &bp);
517 if (retval != -ENOATTR && retval != -EEXIST)
518 return retval;
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800519 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800520 goto out_brelse;
521 if (retval == -EEXIST) {
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800522 if (args->attr_flags & XATTR_CREATE)
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800523 goto out_brelse;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000524
525 trace_xfs_attr_leaf_replace(args);
526
Dave Chinner8275cdd2014-05-06 07:37:31 +1000527 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000528 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 args->blkno2 = args->blkno; /* set 2nd entry info*/
530 args->index2 = args->index;
531 args->rmtblkno2 = args->rmtblkno;
532 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000533 args->rmtvaluelen2 = args->rmtvaluelen;
534
535 /*
536 * clear the remote attr state now that it is saved so that the
537 * values reflect the state of the attribute we are about to
538 * add, not the attribute we just found and will remove later.
539 */
540 args->rmtblkno = 0;
541 args->rmtblkcnt = 0;
542 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
545 /*
546 * Add the attribute to the leaf block, transitioning to a Btree
547 * if required.
548 */
Dave Chinner517c2222013-04-24 18:58:55 +1000549 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000550 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * Promote the attribute list to the Btree format, then
553 * Commit that transaction so that the node_addname() call
554 * can manage its own transactions.
555 */
Dave Chinner517c2222013-04-24 18:58:55 +1000556 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700557 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000558 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700559 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700560 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700561 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * Commit the current trans (including the inode) and start
565 * a new one.
566 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700567 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000568 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000569 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /*
572 * Fob the whole rest of the problem off on the Btree code.
573 */
574 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000575 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 /*
579 * Commit the transaction that added the attr name so that
580 * later routines can manage their own transactions.
581 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700582 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000583 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000584 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /*
587 * If there was an out-of-line value, allocate the blocks we
588 * identified for its storage and copy the value. This is done
589 * after we create the attribute so that we don't overflow the
590 * maximum size of a transaction and/or hit a deadlock.
591 */
592 if (args->rmtblkno > 0) {
593 error = xfs_attr_rmtval_set(args);
594 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000595 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
598 /*
599 * If this is an atomic rename operation, we must "flip" the
600 * incomplete flags on the "new" and "old" attribute/value pairs
601 * so that one disappears and one appears atomically. Then we
602 * must remove the "old" attribute/value pair.
603 */
Barry Naujok6a178102008-05-21 16:42:05 +1000604 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
606 * In a separate transaction, set the incomplete flag on the
607 * "old" attr and clear the incomplete flag on the "new" attr.
608 */
Dave Chinner517c2222013-04-24 18:58:55 +1000609 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /*
614 * Dismantle the "old" attribute/value pair by removing
615 * a "remote" value (if it exists).
616 */
617 args->index = args->index2;
618 args->blkno = args->blkno2;
619 args->rmtblkno = args->rmtblkno2;
620 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000621 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (args->rmtblkno) {
623 error = xfs_attr_rmtval_remove(args);
624 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000625 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 /*
629 * Read in the block containing the "old" attr, then
630 * remove the "old" attr from that block (neat, huh!)
631 */
Dave Chinner517c2222013-04-24 18:58:55 +1000632 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800633 &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100635 return error;
636
Dave Chinner517c2222013-04-24 18:58:55 +1000637 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /*
640 * If the result is small enough, shrink it all into the inode.
641 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100642 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +1000643 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700645 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000646 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700647 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700648 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700649 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /*
653 * Commit the remove and start the next trans in series.
654 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700655 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 } else if (args->rmtblkno > 0) {
658 /*
659 * Added a "remote" value, just clear the incomplete flag.
660 */
Dave Chinner517c2222013-04-24 18:58:55 +1000661 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Dave Chinner517c2222013-04-24 18:58:55 +1000663 return error;
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800664out_brelse:
665 xfs_trans_brelse(args->trans, bp);
666 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*
Allison Collins07120f12020-07-20 21:47:22 -0700670 * Return EEXIST if attr is found, or ENOATTR if not
671 */
672STATIC int
673xfs_attr_leaf_hasname(
674 struct xfs_da_args *args,
675 struct xfs_buf **bp)
676{
677 int error = 0;
678
679 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
680 if (error)
681 return error;
682
683 error = xfs_attr3_leaf_lookup_int(*bp, args);
684 if (error != -ENOATTR && error != -EEXIST)
685 xfs_trans_brelse(args->trans, *bp);
686
687 return error;
688}
689
690/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * Remove a name from the leaf attribute list structure
692 *
693 * This leaf block cannot have a "remote" value, we only call this routine
694 * if bmap_one_block() says there is only one block (ie: no remote blks).
695 */
696STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700697xfs_attr_leaf_removename(
698 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700700 struct xfs_inode *dp;
701 struct xfs_buf *bp;
702 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Dave Chinner5a5881c2012-03-22 05:15:13 +0000704 trace_xfs_attr_leaf_removename(args);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /*
707 * Remove the attribute.
708 */
709 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Allison Collins07120f12020-07-20 21:47:22 -0700711 error = xfs_attr_leaf_hasname(args, &bp);
712
Dave Chinner24513372014-06-25 14:58:08 +1000713 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000714 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000715 return error;
Allison Collins07120f12020-07-20 21:47:22 -0700716 } else if (error != -EEXIST)
717 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Dave Chinner517c2222013-04-24 18:58:55 +1000719 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /*
722 * If the result is small enough, shrink it all into the inode.
723 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100724 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +1000725 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700727 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000728 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700729 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700730 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700731 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000732 }
Dave Chinner517c2222013-04-24 18:58:55 +1000733 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736/*
737 * Look up a name in a leaf attribute list structure.
738 *
739 * This leaf block cannot have a "remote" value, we only call this routine
740 * if bmap_one_block() says there is only one block (ie: no remote blks).
Dave Chinner728bcaa2019-08-29 09:04:08 -0700741 *
742 * Returns 0 on successful retrieval, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000744STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745xfs_attr_leaf_get(xfs_da_args_t *args)
746{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000747 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int error;
749
Dave Chinneree732592012-11-12 22:53:53 +1100750 trace_xfs_attr_leaf_get(args);
751
Allison Collins07120f12020-07-20 21:47:22 -0700752 error = xfs_attr_leaf_hasname(args, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Allison Collins07120f12020-07-20 21:47:22 -0700754 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000755 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000756 return error;
Allison Collins07120f12020-07-20 21:47:22 -0700757 } else if (error != -EEXIST)
758 return error;
759
760
Dave Chinner517c2222013-04-24 18:58:55 +1000761 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000762 xfs_trans_brelse(args->trans, bp);
Dave Chinnere3cc4552019-08-29 09:04:09 -0700763 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Allison Collins07120f12020-07-20 21:47:22 -0700766/*
767 * Return EEXIST if attr is found, or ENOATTR if not
768 * statep: If not null is set to point at the found state. Caller will
769 * be responsible for freeing the state in this case.
770 */
771STATIC int
772xfs_attr_node_hasname(
773 struct xfs_da_args *args,
774 struct xfs_da_state **statep)
775{
776 struct xfs_da_state *state;
777 int retval, error;
778
779 state = xfs_da_state_alloc(args);
780 if (statep != NULL)
781 *statep = NULL;
782
783 /*
784 * Search to see if name exists, and get back a pointer to it.
785 */
786 error = xfs_da3_node_lookup_int(state, &retval);
787 if (error) {
788 xfs_da_state_free(state);
789 return error;
790 }
791
792 if (statep != NULL)
793 *statep = state;
794 else
795 xfs_da_state_free(state);
796 return retval;
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000800 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 *========================================================================*/
802
803/*
804 * Add a name to a Btree-format attribute list.
805 *
806 * This will involve walking down the Btree, and may involve splitting
807 * leaf nodes and even splitting intermediate nodes up to and including
808 * the root node (a special case of an intermediate node).
809 *
810 * "Remote" attribute values confuse the issue and atomic rename operations
811 * add a whole extra layer of confusion on top of that.
812 */
813STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700814xfs_attr_node_addname(
815 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700817 struct xfs_da_state *state;
818 struct xfs_da_state_blk *blk;
819 struct xfs_inode *dp;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700820 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /*
830 * Search to see if name already exists, and get back a pointer
831 * to where it should go.
832 */
Allison Collins07120f12020-07-20 21:47:22 -0700833 retval = xfs_attr_node_hasname(args, &state);
834 if (retval != -ENOATTR && retval != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto out;
Allison Collins07120f12020-07-20 21:47:22 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 blk = &state->path.blk[ state->path.active-1 ];
838 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800839 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto out;
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800841 if (retval == -EEXIST) {
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800842 if (args->attr_flags & XATTR_CREATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000844
845 trace_xfs_attr_node_replace(args);
846
Dave Chinner8275cdd2014-05-06 07:37:31 +1000847 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000848 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 args->blkno2 = args->blkno; /* set 2nd entry info*/
850 args->index2 = args->index;
851 args->rmtblkno2 = args->rmtblkno;
852 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000853 args->rmtvaluelen2 = args->rmtvaluelen;
854
855 /*
856 * clear the remote attr state now that it is saved so that the
857 * values reflect the state of the attribute we are about to
858 * add, not the attribute we just found and will remove later.
859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 args->rmtblkno = 0;
861 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000862 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Dave Chinner517c2222013-04-24 18:58:55 +1000865 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000866 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (state->path.active == 1) {
868 /*
869 * Its really a single leaf node, but it had
870 * out-of-line values so it looked like it *might*
871 * have been a b-tree.
872 */
873 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500874 state = NULL;
Dave Chinner517c2222013-04-24 18:58:55 +1000875 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700876 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000877 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700878 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700879 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700880 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * Commit the node conversion and start the next
884 * trans in the chain.
885 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700886 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000887 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto out;
889
890 goto restart;
891 }
892
893 /*
894 * Split as many Btree elements as required.
895 * This code tracks the new and old attr's location
896 * in the index/blkno/rmtblkno/rmtblkcnt fields and
897 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
898 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000899 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700900 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000901 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700902 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700903 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 } else {
906 /*
907 * Addition succeeded, update Btree hashvals.
908 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000909 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
912 /*
913 * Kill the state structure, we're done with it and need to
914 * allow the buffers to come back later.
915 */
916 xfs_da_state_free(state);
917 state = NULL;
918
919 /*
920 * Commit the leaf addition or btree split and start the next
921 * trans in the chain.
922 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700923 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000924 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto out;
926
927 /*
928 * If there was an out-of-line value, allocate the blocks we
929 * identified for its storage and copy the value. This is done
930 * after we create the attribute so that we don't overflow the
931 * maximum size of a transaction and/or hit a deadlock.
932 */
933 if (args->rmtblkno > 0) {
934 error = xfs_attr_rmtval_set(args);
935 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000936 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
939 /*
940 * If this is an atomic rename operation, we must "flip" the
941 * incomplete flags on the "new" and "old" attribute/value pairs
942 * so that one disappears and one appears atomically. Then we
943 * must remove the "old" attribute/value pair.
944 */
Barry Naujok6a178102008-05-21 16:42:05 +1000945 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
947 * In a separate transaction, set the incomplete flag on the
948 * "old" attr and clear the incomplete flag on the "new" attr.
949 */
Dave Chinner517c2222013-04-24 18:58:55 +1000950 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (error)
952 goto out;
953
954 /*
955 * Dismantle the "old" attribute/value pair by removing
956 * a "remote" value (if it exists).
957 */
958 args->index = args->index2;
959 args->blkno = args->blkno2;
960 args->rmtblkno = args->rmtblkno2;
961 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000962 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (args->rmtblkno) {
964 error = xfs_attr_rmtval_remove(args);
965 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000966 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
969 /*
970 * Re-find the "old" attribute entry after any split ops.
971 * The INCOMPLETE flag means that we will find the "old"
972 * attr, not the "new" one.
973 */
Christoph Hellwig254f8002020-02-26 17:30:43 -0800974 args->attr_filter |= XFS_ATTR_INCOMPLETE;
Carlos Maiolino4491a3d2020-07-22 09:23:18 -0700975 state = xfs_da_state_alloc(args);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000978 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (error)
980 goto out;
981
982 /*
983 * Remove the name and update the hashvals in the tree.
984 */
985 blk = &state->path.blk[ state->path.active-1 ];
986 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000987 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000988 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /*
991 * Check to see if the tree needs to be collapsed.
992 */
993 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000994 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700995 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000996 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700997 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700998 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700999 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 /*
1003 * Commit and start the next trans in the chain.
1004 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001005 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001006 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto out;
1008
1009 } else if (args->rmtblkno > 0) {
1010 /*
1011 * Added a "remote" value, just clear the incomplete flag.
1012 */
Dave Chinner517c2222013-04-24 18:58:55 +10001013 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (error)
1015 goto out;
1016 }
1017 retval = error = 0;
1018
1019out:
1020 if (state)
1021 xfs_da_state_free(state);
1022 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001023 return error;
1024 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/*
1028 * Remove a name from a B-tree attribute list.
1029 *
1030 * This will involve walking down the Btree, and may involve joining
1031 * leaf nodes and even joining intermediate nodes up to and including
1032 * the root node (a special case of an intermediate node).
1033 */
1034STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -07001035xfs_attr_node_removename(
1036 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Brian Foster32a9b7c2018-07-11 22:26:11 -07001038 struct xfs_da_state *state;
1039 struct xfs_da_state_blk *blk;
Brian Foster32a9b7c2018-07-11 22:26:11 -07001040 struct xfs_buf *bp;
1041 int retval, error, forkoff;
Allison Collins07120f12020-07-20 21:47:22 -07001042 struct xfs_inode *dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Dave Chinner5a5881c2012-03-22 05:15:13 +00001044 trace_xfs_attr_node_removename(args);
1045
Allison Collins07120f12020-07-20 21:47:22 -07001046 error = xfs_attr_node_hasname(args, &state);
1047 if (error != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /*
1051 * If there is an out-of-line value, de-allocate the blocks.
1052 * This is done before we remove the attribute so that we don't
1053 * overflow the maximum size of a transaction and/or hit a deadlock.
1054 */
1055 blk = &state->path.blk[ state->path.active-1 ];
1056 ASSERT(blk->bp != NULL);
1057 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1058 if (args->rmtblkno > 0) {
1059 /*
1060 * Fill in disk block numbers in the state structure
1061 * so that we can get the buffers back after we commit
1062 * several transactions in the following calls.
1063 */
1064 error = xfs_attr_fillstate(state);
1065 if (error)
1066 goto out;
1067
1068 /*
1069 * Mark the attribute as INCOMPLETE, then bunmapi() the
1070 * remote value.
1071 */
Dave Chinner517c2222013-04-24 18:58:55 +10001072 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (error)
1074 goto out;
1075 error = xfs_attr_rmtval_remove(args);
1076 if (error)
1077 goto out;
1078
1079 /*
1080 * Refill the state structure with buffers, the prior calls
1081 * released our buffers.
1082 */
1083 error = xfs_attr_refillstate(state);
1084 if (error)
1085 goto out;
1086 }
1087
1088 /*
1089 * Remove the name and update the hashvals in the tree.
1090 */
1091 blk = &state->path.blk[ state->path.active-1 ];
1092 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001093 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001094 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /*
1097 * Check to see if the tree needs to be collapsed.
1098 */
1099 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001100 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001101 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001102 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001103 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001104 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * Commit the Btree join operation and start a new trans.
1108 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001109 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001110 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 goto out;
1112 }
1113
1114 /*
1115 * If the result is small enough, push it all into the inode.
1116 */
1117 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1118 /*
1119 * Have to get rid of the copy of this dabuf in the state.
1120 */
1121 ASSERT(state->path.active == 1);
1122 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 state->path.blk[0].bp = NULL;
1124
Christoph Hellwigdfb87592019-11-20 09:46:02 -08001125 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (error)
1127 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Nathan Scottd8cc8902005-11-02 10:34:53 +11001129 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +10001130 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001132 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001133 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001134 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001135 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001138 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 error = 0;
1141
1142out:
Allison Collins07120f12020-07-20 21:47:22 -07001143 if (state)
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
Allison Collins07120f12020-07-20 21:47:22 -07001260xfs_attr_node_get(
1261 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Allison Collins07120f12020-07-20 21:47:22 -07001263 struct xfs_da_state *state;
1264 struct xfs_da_state_blk *blk;
1265 int i;
1266 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Dave Chinneree732592012-11-12 22:53:53 +11001268 trace_xfs_attr_node_get(args);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Search to see if name exists, and get back a pointer to it.
1272 */
Allison Collins07120f12020-07-20 21:47:22 -07001273 error = xfs_attr_node_hasname(args, &state);
1274 if (error != -EEXIST)
Dave Chinner728bcaa2019-08-29 09:04:08 -07001275 goto out_release;
1276
1277 /*
1278 * Get the value, local or "remote"
1279 */
1280 blk = &state->path.blk[state->path.active - 1];
Allison Collins07120f12020-07-20 21:47:22 -07001281 error = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /*
1284 * If not in a transaction, we have to release all the buffers.
1285 */
Dave Chinner728bcaa2019-08-29 09:04:08 -07001286out_release:
Allison Collins07120f12020-07-20 21:47:22 -07001287 for (i = 0; state != NULL && i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001288 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 state->path.blk[i].bp = NULL;
1290 }
1291
Allison Collins07120f12020-07-20 21:47:22 -07001292 if (state)
1293 xfs_da_state_free(state);
1294 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
Darrick J. Wong65480532019-02-01 09:08:54 -08001296
1297/* Returns true if the attribute entry name is valid. */
1298bool
1299xfs_attr_namecheck(
1300 const void *name,
1301 size_t length)
1302{
1303 /*
1304 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1305 * out, so use >= for the length check.
1306 */
1307 if (length >= MAXNAMELEN)
1308 return false;
1309
1310 /* There shouldn't be any nulls here */
1311 return !memchr(name, 0, length);
1312}