blob: 82f7cc796c26d9af386a7e65266113d7282fe4b3 [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
Allison Collins6cc5b5f2020-07-20 21:47:24 -0700181 int error;
182
183 /*
184 * Build initial attribute list (if required).
185 */
186 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
187 xfs_attr_shortform_create(args);
Allison Henderson4c74a562018-10-18 17:20:50 +1100188
189 error = xfs_attr_shortform_addname(args);
190 if (error == -ENOSPC)
191 return error;
192
193 /*
194 * Commit the shortform mods, and we're done.
195 * NOTE: this is also the error path (EEXIST, etc).
196 */
Christoph Hellwig1d733012020-02-26 17:30:36 -0800197 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
Allison Henderson4c74a562018-10-18 17:20:50 +1100198 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
199
Allison Collins6cc5b5f2020-07-20 21:47:24 -0700200 if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
Allison Henderson4c74a562018-10-18 17:20:50 +1100201 xfs_trans_set_sync(args->trans);
202
Allison Collins6cc5b5f2020-07-20 21:47:24 -0700203 return error;
Allison Henderson4c74a562018-10-18 17:20:50 +1100204}
205
Allison Henderson2f3cd802018-10-18 17:21:16 +1100206/*
Allison Collinsdb1a28c2020-07-20 21:47:28 -0700207 * Check to see if the attr should be upgraded from non-existent or shortform to
208 * single-leaf-block attribute list.
209 */
210static inline bool
211xfs_attr_is_shortform(
212 struct xfs_inode *ip)
213{
214 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
215 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
216 ip->i_afp->if_nextents == 0);
217}
218
219/*
220 * Attempts to set an attr in shortform, or converts short form to leaf form if
221 * there is not enough room. If the attr is set, the transaction is committed
222 * and set to NULL.
223 */
224STATIC int
225xfs_attr_set_shortform(
226 struct xfs_da_args *args,
227 struct xfs_buf **leaf_bp)
228{
229 struct xfs_inode *dp = args->dp;
230 int error, error2 = 0;
231
232 /*
233 * Try to add the attr to the attribute list in the inode.
234 */
235 error = xfs_attr_try_sf_addname(dp, args);
236 if (error != -ENOSPC) {
237 error2 = xfs_trans_commit(args->trans);
238 args->trans = NULL;
239 return error ? error : error2;
240 }
241 /*
242 * It won't fit in the shortform, transform to a leaf block. GROT:
243 * another possible req'mt for a double-split btree op.
244 */
245 error = xfs_attr_shortform_to_leaf(args, leaf_bp);
246 if (error)
247 return error;
248
249 /*
250 * Prevent the leaf buffer from being unlocked so that a concurrent AIL
251 * push cannot grab the half-baked leaf buffer and run into problems
252 * with the write verifier. Once we're done rolling the transaction we
253 * can release the hold and add the attr to the leaf.
254 */
255 xfs_trans_bhold(args->trans, *leaf_bp);
256 error = xfs_defer_finish(&args->trans);
257 xfs_trans_bhold_release(args->trans, *leaf_bp);
258 if (error) {
259 xfs_trans_brelse(args->trans, *leaf_bp);
260 return error;
261 }
262
263 return 0;
264}
265
266/*
Allison Henderson2f3cd802018-10-18 17:21:16 +1100267 * Set the attribute specified in @args.
268 */
269int
270xfs_attr_set_args(
Darrick J. Wong710d7072019-04-24 09:27:41 -0700271 struct xfs_da_args *args)
Allison Henderson2f3cd802018-10-18 17:21:16 +1100272{
273 struct xfs_inode *dp = args->dp;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700274 struct xfs_buf *leaf_bp = NULL;
Allison Collinsdb1a28c2020-07-20 21:47:28 -0700275 int error = 0;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100276
277 /*
Allison Collinsdb1a28c2020-07-20 21:47:28 -0700278 * If the attribute list is already in leaf format, jump straight to
279 * leaf handling. Otherwise, try to add the attribute to the shortform
280 * list; if there's no room then convert the list to leaf format and try
281 * again.
Allison Henderson2f3cd802018-10-18 17:21:16 +1100282 */
Allison Collinsdb1a28c2020-07-20 21:47:28 -0700283 if (xfs_attr_is_shortform(dp)) {
Allison Henderson2f3cd802018-10-18 17:21:16 +1100284
285 /*
Allison Collinsdb1a28c2020-07-20 21:47:28 -0700286 * If the attr was successfully set in shortform, the
287 * transaction is committed and set to NULL. Otherwise, is it
288 * converted from shortform to leaf, and the transaction is
289 * retained.
Allison Henderson2f3cd802018-10-18 17:21:16 +1100290 */
Allison Collinsdb1a28c2020-07-20 21:47:28 -0700291 error = xfs_attr_set_shortform(args, &leaf_bp);
292 if (error || !args->trans)
Allison Henderson2f3cd802018-10-18 17:21:16 +1100293 return error;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100294 }
295
Allison Collins7c93d4a2020-07-20 21:47:24 -0700296 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
Allison Henderson2f3cd802018-10-18 17:21:16 +1100297 error = xfs_attr_leaf_addname(args);
Allison Collins7c93d4a2020-07-20 21:47:24 -0700298 if (error != -ENOSPC)
299 return error;
300
301 /*
302 * Finish any deferred work items and roll the transaction once
303 * more. The goal here is to call node_addname with the inode
304 * and transaction in the same state (inode locked and joined,
305 * transaction clean) no matter how we got to this step.
306 */
307 error = xfs_defer_finish(&args->trans);
308 if (error)
309 return error;
310
311 /*
312 * Commit the current trans (including the inode) and
313 * start a new one.
314 */
315 error = xfs_trans_roll_inode(&args->trans, dp);
316 if (error)
317 return error;
318 }
319
320 error = xfs_attr_node_addname(args);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100321 return error;
322}
323
Allison Henderson068f9852018-10-18 17:21:23 +1100324/*
Allison Collins07120f12020-07-20 21:47:22 -0700325 * Return EEXIST if attr is found, or ENOATTR if not
326 */
327int
328xfs_has_attr(
329 struct xfs_da_args *args)
330{
331 struct xfs_inode *dp = args->dp;
332 struct xfs_buf *bp = NULL;
333 int error;
334
335 if (!xfs_inode_hasattr(dp))
336 return -ENOATTR;
337
338 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
339 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
340 return xfs_attr_sf_findname(args, NULL, NULL);
341 }
342
343 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
344 error = xfs_attr_leaf_hasname(args, &bp);
345
346 if (bp)
347 xfs_trans_brelse(args->trans, bp);
348
349 return error;
350 }
351
352 return xfs_attr_node_hasname(args, NULL);
353}
354
355/*
Allison Henderson068f9852018-10-18 17:21:23 +1100356 * Remove the attribute specified in @args.
357 */
358int
359xfs_attr_remove_args(
360 struct xfs_da_args *args)
361{
362 struct xfs_inode *dp = args->dp;
363 int error;
364
365 if (!xfs_inode_hasattr(dp)) {
366 error = -ENOATTR;
Christoph Hellwigf7e67b22020-05-18 10:28:05 -0700367 } else if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
Allison Henderson068f9852018-10-18 17:21:23 +1100368 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
369 error = xfs_attr_shortform_remove(args);
370 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
371 error = xfs_attr_leaf_removename(args);
372 } else {
373 error = xfs_attr_node_removename(args);
374 }
375
376 return error;
377}
378
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800379/*
Christoph Hellwiga2544622020-02-26 17:30:33 -0800380 * Note: If args->value is NULL the attribute will be removed, just like the
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800381 * Linux ->setattr API.
382 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000383int
384xfs_attr_set(
Christoph Hellwiga2544622020-02-26 17:30:33 -0800385 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Christoph Hellwiga2544622020-02-26 17:30:33 -0800387 struct xfs_inode *dp = args->dp;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000388 struct xfs_mount *mp = dp->i_mount;
389 struct xfs_trans_res tres;
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800390 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
Allison Henderson4c74a562018-10-18 17:20:50 +1100391 int error, local;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800392 unsigned int total;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000393
394 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000395 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000396
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800397 error = xfs_qm_dqattach(dp);
398 if (error)
399 return error;
400
Christoph Hellwiga2544622020-02-26 17:30:33 -0800401 args->geo = mp->m_attr_geo;
402 args->whichfork = XFS_ATTR_FORK;
403 args->hashval = xfs_da_hashname(args->name, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /*
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800406 * We have no control over the attribute names that userspace passes us
407 * to remove, so we have to allow the name lookup prior to attribute
408 * removal to fail as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800410 args->op_flags = XFS_DA_OP_OKNOENT;
Barry Naujoke5889e92007-02-10 18:35:58 +1100411
Christoph Hellwiga2544622020-02-26 17:30:33 -0800412 if (args->value) {
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800413 XFS_STATS_INC(mp, xs_attr_set);
414
Christoph Hellwiga2544622020-02-26 17:30:33 -0800415 args->op_flags |= XFS_DA_OP_ADDNAME;
416 args->total = xfs_attr_calc_size(args, &local);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800417
418 /*
419 * If the inode doesn't have an attribute fork, add one.
420 * (inode must not be locked when we call this routine)
421 */
422 if (XFS_IFORK_Q(dp) == 0) {
423 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
Christoph Hellwiga2544622020-02-26 17:30:33 -0800424 XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen,
425 args->valuelen);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800426
427 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
428 if (error)
429 return error;
430 }
431
432 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
Christoph Hellwiga2544622020-02-26 17:30:33 -0800433 M_RES(mp)->tr_attrsetrt.tr_logres *
434 args->total;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800435 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
436 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800437 total = args->total;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800438 } else {
439 XFS_STATS_INC(mp, xs_attr_remove);
440
441 tres = M_RES(mp)->tr_attrrm;
442 total = XFS_ATTRRM_SPACE_RES(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * Root fork attributes can use reserved data blocks for this
447 * operation if necessary
448 */
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800449 error = xfs_trans_alloc(mp, &tres, total, 0,
Christoph Hellwiga2544622020-02-26 17:30:33 -0800450 rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000451 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000452 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Christoph Hellwig253f4912016-04-06 09:19:55 +1000454 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800455 xfs_trans_ijoin(args->trans, dp, 0);
456 if (args->value) {
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800457 unsigned int quota_flags = XFS_QMOPT_RES_REGBLKS;
458
459 if (rsvd)
460 quota_flags |= XFS_QMOPT_FORCE_RES;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800461 error = xfs_trans_reserve_quota_nblks(args->trans, dp,
462 args->total, 0, quota_flags);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800463 if (error)
464 goto out_trans_cancel;
Allison Collinsdeed9512020-07-20 21:47:23 -0700465
466 error = xfs_has_attr(args);
467 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
468 goto out_trans_cancel;
469 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
470 goto out_trans_cancel;
471 if (error != -ENOATTR && error != -EEXIST)
472 goto out_trans_cancel;
473
Christoph Hellwiga2544622020-02-26 17:30:33 -0800474 error = xfs_attr_set_args(args);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800475 if (error)
476 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100477 /* shortform attribute has already been committed */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800478 if (!args->trans)
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800479 goto out_unlock;
480 } else {
Allison Collinsdeed9512020-07-20 21:47:23 -0700481 error = xfs_has_attr(args);
482 if (error != -EEXIST)
483 goto out_trans_cancel;
484
Christoph Hellwiga2544622020-02-26 17:30:33 -0800485 error = xfs_attr_remove_args(args);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800486 if (error)
487 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /*
491 * If this is a synchronous mount, make sure that the
492 * transaction goes to disk before returning to the user.
493 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000494 if (mp->m_flags & XFS_MOUNT_WSYNC)
Christoph Hellwiga2544622020-02-26 17:30:33 -0800495 xfs_trans_set_sync(args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Christoph Hellwig1d733012020-02-26 17:30:36 -0800497 if (!(args->op_flags & XFS_DA_OP_NOTIME))
Christoph Hellwiga2544622020-02-26 17:30:33 -0800498 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * Commit the last in the sequence of transactions.
502 */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800503 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
504 error = xfs_trans_commit(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100505out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000507 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Allison Henderson2f3cd802018-10-18 17:21:16 +1100509out_trans_cancel:
Christoph Hellwiga2544622020-02-26 17:30:33 -0800510 if (args->trans)
511 xfs_trans_cancel(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100512 goto out_unlock;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/*========================================================================
516 * External routines when attribute list is inside the inode
517 *========================================================================*/
518
519/*
520 * Add a name to the shortform attribute list structure
521 * This is the external routine.
522 */
523STATIC int
524xfs_attr_shortform_addname(xfs_da_args_t *args)
525{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100526 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Dave Chinner5a5881c2012-03-22 05:15:13 +0000528 trace_xfs_attr_sf_addname(args);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 retval = xfs_attr_shortform_lookup(args);
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800531 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Eric Sandeend99831f2014-06-22 15:03:54 +1000532 return retval;
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800533 if (retval == -EEXIST) {
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800534 if (args->attr_flags & XATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700537 if (retval)
538 return retval;
539 /*
540 * Since we have removed the old attr, clear ATTR_REPLACE so
541 * that the leaf format add routine won't trip over the attr
542 * not being around.
543 */
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800544 args->attr_flags &= ~XATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Nathan Scottd8cc8902005-11-02 10:34:53 +1100547 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
548 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000549 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
552 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100553
554 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
555 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000556 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100557
558 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562
563/*========================================================================
564 * External routines when attribute list is one block
565 *========================================================================*/
566
Allison Collins410c1982020-07-20 21:47:29 -0700567/* Store info about a remote block */
568STATIC void
569xfs_attr_save_rmt_blk(
570 struct xfs_da_args *args)
571{
572 args->blkno2 = args->blkno;
573 args->index2 = args->index;
574 args->rmtblkno2 = args->rmtblkno;
575 args->rmtblkcnt2 = args->rmtblkcnt;
576 args->rmtvaluelen2 = args->rmtvaluelen;
577}
578
579/* Set stored info about a remote block */
580STATIC void
581xfs_attr_restore_rmt_blk(
582 struct xfs_da_args *args)
583{
584 args->blkno = args->blkno2;
585 args->index = args->index2;
586 args->rmtblkno = args->rmtblkno2;
587 args->rmtblkcnt = args->rmtblkcnt2;
588 args->rmtvaluelen = args->rmtvaluelen2;
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*
Allison Collins7c93d4a2020-07-20 21:47:24 -0700592 * Tries to add an attribute to an inode in leaf form
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
Allison Collins7c93d4a2020-07-20 21:47:24 -0700594 * This function is meant to execute as part of a delayed operation and leaves
595 * the transaction handling to the caller. On success the attribute is added
596 * and the inode and transaction are left dirty. If there is not enough space,
597 * the attr data is converted to node format and -ENOSPC is returned. Caller is
598 * responsible for handling the dirty inode and transaction or adding the attr
599 * in node format.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
David Chinnera8272ce2007-11-23 16:28:09 +1100601STATIC int
Allison Collins7c93d4a2020-07-20 21:47:24 -0700602xfs_attr_leaf_try_add(
603 struct xfs_da_args *args,
604 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Allison Collins7c93d4a2020-07-20 21:47:24 -0700606 int retval, error;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * Look up the given attribute in the leaf block. Figure out if
610 * the given flags produce an error or call for an atomic rename.
611 */
Allison Collins07120f12020-07-20 21:47:22 -0700612 retval = xfs_attr_leaf_hasname(args, &bp);
613 if (retval != -ENOATTR && retval != -EEXIST)
614 return retval;
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800615 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800616 goto out_brelse;
617 if (retval == -EEXIST) {
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800618 if (args->attr_flags & XATTR_CREATE)
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800619 goto out_brelse;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000620
621 trace_xfs_attr_leaf_replace(args);
622
Dave Chinner8275cdd2014-05-06 07:37:31 +1000623 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000624 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Allison Collins410c1982020-07-20 21:47:29 -0700625 xfs_attr_save_rmt_blk(args);
Dave Chinner8275cdd2014-05-06 07:37:31 +1000626
627 /*
628 * clear the remote attr state now that it is saved so that the
629 * values reflect the state of the attribute we are about to
630 * add, not the attribute we just found and will remove later.
631 */
632 args->rmtblkno = 0;
633 args->rmtblkcnt = 0;
634 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 /*
638 * Add the attribute to the leaf block, transitioning to a Btree
639 * if required.
640 */
Dave Chinner517c2222013-04-24 18:58:55 +1000641 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000642 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
Allison Collins7c93d4a2020-07-20 21:47:24 -0700644 * Promote the attribute list to the Btree format. Unless an
645 * error occurs, retain the -ENOSPC retval
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Dave Chinner517c2222013-04-24 18:58:55 +1000647 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700648 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000649 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Allison Collins7c93d4a2020-07-20 21:47:24 -0700651 return retval;
652out_brelse:
653 xfs_trans_brelse(args->trans, bp);
654 return retval;
655}
656
657
658/*
659 * Add a name to the leaf attribute list structure
660 *
661 * This leaf block cannot have a "remote" value, we only call this routine
662 * if bmap_one_block() says there is only one block (ie: no remote blks).
663 */
664STATIC int
665xfs_attr_leaf_addname(
666 struct xfs_da_args *args)
667{
668 int error, forkoff;
669 struct xfs_buf *bp = NULL;
670 struct xfs_inode *dp = args->dp;
671
672 trace_xfs_attr_leaf_addname(args);
673
674 error = xfs_attr_leaf_try_add(args, bp);
675 if (error)
676 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
679 * Commit the transaction that added the attr name so that
680 * later routines can manage their own transactions.
681 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700682 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000683 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /*
687 * If there was an out-of-line value, allocate the blocks we
688 * identified for its storage and copy the value. This is done
689 * after we create the attribute so that we don't overflow the
690 * maximum size of a transaction and/or hit a deadlock.
691 */
692 if (args->rmtblkno > 0) {
693 error = xfs_attr_rmtval_set(args);
694 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000695 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
698 /*
699 * If this is an atomic rename operation, we must "flip" the
700 * incomplete flags on the "new" and "old" attribute/value pairs
701 * so that one disappears and one appears atomically. Then we
702 * must remove the "old" attribute/value pair.
703 */
Barry Naujok6a178102008-05-21 16:42:05 +1000704 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
706 * In a separate transaction, set the incomplete flag on the
707 * "old" attr and clear the incomplete flag on the "new" attr.
708 */
Dave Chinner517c2222013-04-24 18:58:55 +1000709 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000711 return error;
Allison Collinse3be1272020-07-20 21:47:23 -0700712 /*
713 * Commit the flag value change and start the next trans in
714 * series.
715 */
716 error = xfs_trans_roll_inode(&args->trans, args->dp);
717 if (error)
718 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /*
721 * Dismantle the "old" attribute/value pair by removing
722 * a "remote" value (if it exists).
723 */
Allison Collins410c1982020-07-20 21:47:29 -0700724 xfs_attr_restore_rmt_blk(args);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (args->rmtblkno) {
Allison Collinsd4034c42020-07-20 21:47:26 -0700727 error = xfs_attr_rmtval_invalidate(args);
728 if (error)
729 return error;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 error = xfs_attr_rmtval_remove(args);
732 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000733 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
736 /*
737 * Read in the block containing the "old" attr, then
738 * remove the "old" attr from that block (neat, huh!)
739 */
Dave Chinner517c2222013-04-24 18:58:55 +1000740 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800741 &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100743 return error;
744
Dave Chinner517c2222013-04-24 18:58:55 +1000745 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /*
748 * If the result is small enough, shrink it all into the inode.
749 */
Allison Collins0feaef12020-07-20 21:47:27 -0700750 forkoff = xfs_attr_shortform_allfit(bp, dp);
751 if (forkoff)
Dave Chinner517c2222013-04-24 18:58:55 +1000752 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* bp is gone due to xfs_da_shrink_inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 } else if (args->rmtblkno > 0) {
755 /*
756 * Added a "remote" value, just clear the incomplete flag.
757 */
Dave Chinner517c2222013-04-24 18:58:55 +1000758 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Dave Chinner517c2222013-04-24 18:58:55 +1000760 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763/*
Allison Collins07120f12020-07-20 21:47:22 -0700764 * Return EEXIST if attr is found, or ENOATTR if not
765 */
766STATIC int
767xfs_attr_leaf_hasname(
768 struct xfs_da_args *args,
769 struct xfs_buf **bp)
770{
771 int error = 0;
772
773 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
774 if (error)
775 return error;
776
777 error = xfs_attr3_leaf_lookup_int(*bp, args);
778 if (error != -ENOATTR && error != -EEXIST)
779 xfs_trans_brelse(args->trans, *bp);
780
781 return error;
782}
783
784/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * Remove a name from the leaf attribute list structure
786 *
787 * This leaf block cannot have a "remote" value, we only call this routine
788 * if bmap_one_block() says there is only one block (ie: no remote blks).
789 */
790STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700791xfs_attr_leaf_removename(
792 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700794 struct xfs_inode *dp;
795 struct xfs_buf *bp;
796 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Chinner5a5881c2012-03-22 05:15:13 +0000798 trace_xfs_attr_leaf_removename(args);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /*
801 * Remove the attribute.
802 */
803 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Allison Collins07120f12020-07-20 21:47:22 -0700805 error = xfs_attr_leaf_hasname(args, &bp);
806
Dave Chinner24513372014-06-25 14:58:08 +1000807 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000809 return error;
Allison Collins07120f12020-07-20 21:47:22 -0700810 } else if (error != -EEXIST)
811 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Dave Chinner517c2222013-04-24 18:58:55 +1000813 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /*
816 * If the result is small enough, shrink it all into the inode.
817 */
Allison Collins0feaef12020-07-20 21:47:27 -0700818 forkoff = xfs_attr_shortform_allfit(bp, dp);
819 if (forkoff)
820 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* bp is gone due to xfs_da_shrink_inode */
Allison Collins0feaef12020-07-20 21:47:27 -0700822
Dave Chinner517c2222013-04-24 18:58:55 +1000823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826/*
827 * Look up a name in a leaf attribute list structure.
828 *
829 * This leaf block cannot have a "remote" value, we only call this routine
830 * if bmap_one_block() says there is only one block (ie: no remote blks).
Dave Chinner728bcaa2019-08-29 09:04:08 -0700831 *
832 * Returns 0 on successful retrieval, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000834STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835xfs_attr_leaf_get(xfs_da_args_t *args)
836{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000837 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int error;
839
Dave Chinneree732592012-11-12 22:53:53 +1100840 trace_xfs_attr_leaf_get(args);
841
Allison Collins07120f12020-07-20 21:47:22 -0700842 error = xfs_attr_leaf_hasname(args, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Allison Collins07120f12020-07-20 21:47:22 -0700844 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000845 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000846 return error;
Allison Collins07120f12020-07-20 21:47:22 -0700847 } else if (error != -EEXIST)
848 return error;
849
850
Dave Chinner517c2222013-04-24 18:58:55 +1000851 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000852 xfs_trans_brelse(args->trans, bp);
Dave Chinnere3cc4552019-08-29 09:04:09 -0700853 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Allison Collins07120f12020-07-20 21:47:22 -0700856/*
857 * Return EEXIST if attr is found, or ENOATTR if not
858 * statep: If not null is set to point at the found state. Caller will
859 * be responsible for freeing the state in this case.
860 */
861STATIC int
862xfs_attr_node_hasname(
863 struct xfs_da_args *args,
864 struct xfs_da_state **statep)
865{
866 struct xfs_da_state *state;
867 int retval, error;
868
869 state = xfs_da_state_alloc(args);
870 if (statep != NULL)
871 *statep = NULL;
872
873 /*
874 * Search to see if name exists, and get back a pointer to it.
875 */
876 error = xfs_da3_node_lookup_int(state, &retval);
877 if (error) {
878 xfs_da_state_free(state);
879 return error;
880 }
881
882 if (statep != NULL)
883 *statep = state;
884 else
885 xfs_da_state_free(state);
886 return retval;
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000890 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 *========================================================================*/
892
893/*
894 * Add a name to a Btree-format attribute list.
895 *
896 * This will involve walking down the Btree, and may involve splitting
897 * leaf nodes and even splitting intermediate nodes up to and including
898 * the root node (a special case of an intermediate node).
899 *
900 * "Remote" attribute values confuse the issue and atomic rename operations
901 * add a whole extra layer of confusion on top of that.
902 */
903STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700904xfs_attr_node_addname(
905 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700907 struct xfs_da_state *state;
908 struct xfs_da_state_blk *blk;
909 struct xfs_inode *dp;
Brian Foster32a9b7c2018-07-11 22:26:11 -0700910 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Dave Chinner5a5881c2012-03-22 05:15:13 +0000912 trace_xfs_attr_node_addname(args);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * Fill in bucket of arguments/results/context to carry around.
916 */
917 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /*
920 * Search to see if name already exists, and get back a pointer
921 * to where it should go.
922 */
Allison Collins07120f12020-07-20 21:47:22 -0700923 retval = xfs_attr_node_hasname(args, &state);
924 if (retval != -ENOATTR && retval != -EEXIST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto out;
Allison Collins07120f12020-07-20 21:47:22 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 blk = &state->path.blk[ state->path.active-1 ];
928 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800929 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto out;
Christoph Hellwigf3e93d92020-02-26 17:30:42 -0800931 if (retval == -EEXIST) {
Christoph Hellwigd5f0f492020-02-26 17:30:42 -0800932 if (args->attr_flags & XATTR_CREATE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000934
935 trace_xfs_attr_node_replace(args);
936
Dave Chinner8275cdd2014-05-06 07:37:31 +1000937 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000938 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Allison Collins410c1982020-07-20 21:47:29 -0700939 xfs_attr_save_rmt_blk(args);
Dave Chinner8275cdd2014-05-06 07:37:31 +1000940
941 /*
942 * clear the remote attr state now that it is saved so that the
943 * values reflect the state of the attribute we are about to
944 * add, not the attribute we just found and will remove later.
945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 args->rmtblkno = 0;
947 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000948 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Dave Chinner517c2222013-04-24 18:58:55 +1000951 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000952 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (state->path.active == 1) {
954 /*
955 * Its really a single leaf node, but it had
956 * out-of-line values so it looked like it *might*
957 * have been a b-tree.
958 */
959 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500960 state = NULL;
Dave Chinner517c2222013-04-24 18:58:55 +1000961 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700962 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000963 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700964 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700965 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700966 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * Commit the node conversion and start the next
970 * trans in the chain.
971 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700972 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000973 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto out;
975
976 goto restart;
977 }
978
979 /*
980 * Split as many Btree elements as required.
981 * This code tracks the new and old attr's location
982 * in the index/blkno/rmtblkno/rmtblkcnt fields and
983 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
984 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000985 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700986 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000987 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700988 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700989 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700990 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 } else {
992 /*
993 * Addition succeeded, update Btree hashvals.
994 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000995 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
998 /*
999 * Kill the state structure, we're done with it and need to
1000 * allow the buffers to come back later.
1001 */
1002 xfs_da_state_free(state);
1003 state = NULL;
1004
1005 /*
1006 * Commit the leaf addition or btree split and start the next
1007 * trans in the chain.
1008 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001009 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001010 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto out;
1012
1013 /*
1014 * If there was an out-of-line value, allocate the blocks we
1015 * identified for its storage and copy the value. This is done
1016 * after we create the attribute so that we don't overflow the
1017 * maximum size of a transaction and/or hit a deadlock.
1018 */
1019 if (args->rmtblkno > 0) {
1020 error = xfs_attr_rmtval_set(args);
1021 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001022 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
1025 /*
1026 * If this is an atomic rename operation, we must "flip" the
1027 * incomplete flags on the "new" and "old" attribute/value pairs
1028 * so that one disappears and one appears atomically. Then we
1029 * must remove the "old" attribute/value pair.
1030 */
Barry Naujok6a178102008-05-21 16:42:05 +10001031 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
1033 * In a separate transaction, set the incomplete flag on the
1034 * "old" attr and clear the incomplete flag on the "new" attr.
1035 */
Dave Chinner517c2222013-04-24 18:58:55 +10001036 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (error)
1038 goto out;
Allison Collinse3be1272020-07-20 21:47:23 -07001039 /*
1040 * Commit the flag value change and start the next trans in
1041 * series
1042 */
1043 error = xfs_trans_roll_inode(&args->trans, args->dp);
1044 if (error)
1045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /*
1048 * Dismantle the "old" attribute/value pair by removing
1049 * a "remote" value (if it exists).
1050 */
Allison Collins410c1982020-07-20 21:47:29 -07001051 xfs_attr_restore_rmt_blk(args);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (args->rmtblkno) {
Allison Collinsd4034c42020-07-20 21:47:26 -07001054 error = xfs_attr_rmtval_invalidate(args);
1055 if (error)
1056 return error;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 error = xfs_attr_rmtval_remove(args);
1059 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001060 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
1063 /*
1064 * Re-find the "old" attribute entry after any split ops.
1065 * The INCOMPLETE flag means that we will find the "old"
1066 * attr, not the "new" one.
1067 */
Christoph Hellwig254f8002020-02-26 17:30:43 -08001068 args->attr_filter |= XFS_ATTR_INCOMPLETE;
Carlos Maiolino4491a3d2020-07-22 09:23:18 -07001069 state = xfs_da_state_alloc(args);
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001072 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (error)
1074 goto out;
1075
1076 /*
1077 * Remove the name and update the hashvals in the tree.
1078 */
1079 blk = &state->path.blk[ state->path.active-1 ];
1080 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001081 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001082 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /*
1085 * Check to see if the tree needs to be collapsed.
1086 */
1087 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001088 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001089 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001090 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 } else if (args->rmtblkno > 0) {
1094 /*
1095 * Added a "remote" value, just clear the incomplete flag.
1096 */
Dave Chinner517c2222013-04-24 18:58:55 +10001097 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (error)
1099 goto out;
1100 }
1101 retval = error = 0;
1102
1103out:
1104 if (state)
1105 xfs_da_state_free(state);
1106 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001107 return error;
1108 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111/*
Allison Collins3f6e0112020-07-20 21:47:27 -07001112 * Shrink an attribute from leaf to shortform
1113 */
1114STATIC int
1115xfs_attr_node_shrink(
1116 struct xfs_da_args *args,
1117 struct xfs_da_state *state)
1118{
1119 struct xfs_inode *dp = args->dp;
1120 int error, forkoff;
1121 struct xfs_buf *bp;
1122
1123 /*
1124 * Have to get rid of the copy of this dabuf in the state.
1125 */
1126 ASSERT(state->path.active == 1);
1127 ASSERT(state->path.blk[0].bp);
1128 state->path.blk[0].bp = NULL;
1129
1130 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1131 if (error)
1132 return error;
1133
1134 forkoff = xfs_attr_shortform_allfit(bp, dp);
1135 if (forkoff) {
1136 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1137 /* bp is gone due to xfs_da_shrink_inode */
Allison Collins3f6e0112020-07-20 21:47:27 -07001138 } else
1139 xfs_trans_brelse(args->trans, bp);
1140
Allison Collins0feaef12020-07-20 21:47:27 -07001141 return error;
Allison Collins3f6e0112020-07-20 21:47:27 -07001142}
1143
1144/*
Allison Collinsf44df682020-07-20 21:47:28 -07001145 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1146 * for later deletion of the entry.
1147 */
1148STATIC int
1149xfs_attr_leaf_mark_incomplete(
1150 struct xfs_da_args *args,
1151 struct xfs_da_state *state)
1152{
1153 int error;
1154
1155 /*
1156 * Fill in disk block numbers in the state structure
1157 * so that we can get the buffers back after we commit
1158 * several transactions in the following calls.
1159 */
1160 error = xfs_attr_fillstate(state);
1161 if (error)
1162 return error;
1163
1164 /*
1165 * Mark the attribute as INCOMPLETE
1166 */
1167 return xfs_attr3_leaf_setflag(args);
1168}
1169
1170/*
Allison Collins674eb542020-07-20 21:47:30 -07001171 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1172 * the blocks are valid. Attr keys with remote blocks will be marked
1173 * incomplete.
1174 */
1175STATIC
1176int xfs_attr_node_removename_setup(
1177 struct xfs_da_args *args,
1178 struct xfs_da_state **state)
1179{
1180 int error;
1181
1182 error = xfs_attr_node_hasname(args, state);
1183 if (error != -EEXIST)
1184 return error;
1185
1186 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1187 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1188 XFS_ATTR_LEAF_MAGIC);
1189
1190 if (args->rmtblkno > 0) {
1191 error = xfs_attr_leaf_mark_incomplete(args, *state);
1192 if (error)
1193 return error;
1194
1195 return xfs_attr_rmtval_invalidate(args);
1196 }
1197
1198 return 0;
1199}
1200
Allison Collins72b97ea2020-07-20 21:47:30 -07001201STATIC int
1202xfs_attr_node_remove_rmt(
1203 struct xfs_da_args *args,
1204 struct xfs_da_state *state)
1205{
1206 int error = 0;
1207
1208 error = xfs_attr_rmtval_remove(args);
1209 if (error)
1210 return error;
1211
1212 /*
1213 * Refill the state structure with buffers, the prior calls released our
1214 * buffers.
1215 */
1216 return xfs_attr_refillstate(state);
1217}
1218
Allison Collins674eb542020-07-20 21:47:30 -07001219/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * Remove a name from a B-tree attribute list.
1221 *
1222 * This will involve walking down the Btree, and may involve joining
1223 * leaf nodes and even joining intermediate nodes up to and including
1224 * the root node (a special case of an intermediate node).
1225 */
1226STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -07001227xfs_attr_node_removename(
1228 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Brian Foster32a9b7c2018-07-11 22:26:11 -07001230 struct xfs_da_state *state;
1231 struct xfs_da_state_blk *blk;
Allison Collins3f6e0112020-07-20 21:47:27 -07001232 int retval, error;
Allison Collins07120f12020-07-20 21:47:22 -07001233 struct xfs_inode *dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Dave Chinner5a5881c2012-03-22 05:15:13 +00001235 trace_xfs_attr_node_removename(args);
1236
Allison Collins674eb542020-07-20 21:47:30 -07001237 error = xfs_attr_node_removename_setup(args, &state);
1238 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /*
1242 * If there is an out-of-line value, de-allocate the blocks.
1243 * This is done before we remove the attribute so that we don't
1244 * overflow the maximum size of a transaction and/or hit a deadlock.
1245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (args->rmtblkno > 0) {
Allison Collins72b97ea2020-07-20 21:47:30 -07001247 error = xfs_attr_node_remove_rmt(args, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (error)
1249 goto out;
1250 }
1251
1252 /*
1253 * Remove the name and update the hashvals in the tree.
1254 */
1255 blk = &state->path.blk[ state->path.active-1 ];
1256 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001257 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001258 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /*
1261 * Check to see if the tree needs to be collapsed.
1262 */
1263 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001264 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001265 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001266 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001267 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001268 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Commit the Btree join operation and start a new trans.
1272 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001273 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001274 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto out;
1276 }
1277
1278 /*
1279 * If the result is small enough, push it all into the inode.
1280 */
Allison Collins3f6e0112020-07-20 21:47:27 -07001281 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
1282 error = xfs_attr_node_shrink(args, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284out:
Allison Collins07120f12020-07-20 21:47:22 -07001285 if (state)
1286 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001287 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290/*
1291 * Fill in the disk block numbers in the state structure for the buffers
1292 * that are attached to the state structure.
1293 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001294 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
1296STATIC int
1297xfs_attr_fillstate(xfs_da_state_t *state)
1298{
1299 xfs_da_state_path_t *path;
1300 xfs_da_state_blk_t *blk;
1301 int level;
1302
Dave Chinneree732592012-11-12 22:53:53 +11001303 trace_xfs_attr_fillstate(state->args);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /*
1306 * Roll down the "path" in the state structure, storing the on-disk
1307 * block number for those buffers in the "path".
1308 */
1309 path = &state->path;
1310 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1311 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1312 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001313 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 blk->bp = NULL;
1315 } else {
1316 blk->disk_blkno = 0;
1317 }
1318 }
1319
1320 /*
1321 * Roll down the "altpath" in the state structure, storing the on-disk
1322 * block number for those buffers in the "altpath".
1323 */
1324 path = &state->altpath;
1325 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1326 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1327 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001328 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 blk->bp = NULL;
1330 } else {
1331 blk->disk_blkno = 0;
1332 }
1333 }
1334
Eric Sandeend99831f2014-06-22 15:03:54 +10001335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338/*
1339 * Reattach the buffers to the state structure based on the disk block
1340 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001341 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 * buffers from our grip.
1343 */
1344STATIC int
1345xfs_attr_refillstate(xfs_da_state_t *state)
1346{
1347 xfs_da_state_path_t *path;
1348 xfs_da_state_blk_t *blk;
1349 int level, error;
1350
Dave Chinneree732592012-11-12 22:53:53 +11001351 trace_xfs_attr_refillstate(state->args);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /*
1354 * Roll down the "path" in the state structure, storing the on-disk
1355 * block number for those buffers in the "path".
1356 */
1357 path = &state->path;
1358 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1359 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1360 if (blk->disk_blkno) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001361 error = xfs_da3_node_read_mapped(state->args->trans,
1362 state->args->dp, blk->disk_blkno,
1363 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001365 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 } else {
1367 blk->bp = NULL;
1368 }
1369 }
1370
1371 /*
1372 * Roll down the "altpath" in the state structure, storing the on-disk
1373 * block number for those buffers in the "altpath".
1374 */
1375 path = &state->altpath;
1376 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1377 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1378 if (blk->disk_blkno) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001379 error = xfs_da3_node_read_mapped(state->args->trans,
1380 state->args->dp, blk->disk_blkno,
1381 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001383 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 } else {
1385 blk->bp = NULL;
1386 }
1387 }
1388
Eric Sandeend99831f2014-06-22 15:03:54 +10001389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392/*
Dave Chinner728bcaa2019-08-29 09:04:08 -07001393 * Retrieve the attribute data from a node attribute list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *
1395 * This routine gets called for any attribute fork that has more than one
1396 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1397 * "remote" values taking up more blocks.
Dave Chinner728bcaa2019-08-29 09:04:08 -07001398 *
1399 * Returns 0 on successful retrieval, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001401STATIC int
Allison Collins07120f12020-07-20 21:47:22 -07001402xfs_attr_node_get(
1403 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Allison Collins07120f12020-07-20 21:47:22 -07001405 struct xfs_da_state *state;
1406 struct xfs_da_state_blk *blk;
1407 int i;
1408 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Dave Chinneree732592012-11-12 22:53:53 +11001410 trace_xfs_attr_node_get(args);
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /*
1413 * Search to see if name exists, and get back a pointer to it.
1414 */
Allison Collins07120f12020-07-20 21:47:22 -07001415 error = xfs_attr_node_hasname(args, &state);
1416 if (error != -EEXIST)
Dave Chinner728bcaa2019-08-29 09:04:08 -07001417 goto out_release;
1418
1419 /*
1420 * Get the value, local or "remote"
1421 */
1422 blk = &state->path.blk[state->path.active - 1];
Allison Collins07120f12020-07-20 21:47:22 -07001423 error = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /*
1426 * If not in a transaction, we have to release all the buffers.
1427 */
Dave Chinner728bcaa2019-08-29 09:04:08 -07001428out_release:
Allison Collins07120f12020-07-20 21:47:22 -07001429 for (i = 0; state != NULL && i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001430 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 state->path.blk[i].bp = NULL;
1432 }
1433
Allison Collins07120f12020-07-20 21:47:22 -07001434 if (state)
1435 xfs_da_state_free(state);
1436 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
Darrick J. Wong65480532019-02-01 09:08:54 -08001438
1439/* Returns true if the attribute entry name is valid. */
1440bool
1441xfs_attr_namecheck(
1442 const void *name,
1443 size_t length)
1444{
1445 /*
1446 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1447 * out, so use >= for the length check.
1448 */
1449 if (length >= MAXNAMELEN)
1450 return false;
1451
1452 /* There shouldn't be any nulls here */
1453 return !memchr(name, 0, length);
1454}