blob: 3b1db2afb1040a14013a96be842d12c5f6563886 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100013#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110014#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110015#include "xfs_da_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110016#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110018#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110020#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_attr.h"
22#include "xfs_attr_leaf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110023#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_quota.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans_space.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000026#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * xfs_attr.c
30 *
31 * Provide the external interfaces to manage attribute lists.
32 */
33
34/*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
37
38/*
39 * Internal routines when attribute list fits inside the inode.
40 */
41STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
42
43/*
44 * Internal routines when attribute list is one block.
45 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100046STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
48STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * Internal routines when attribute list is more than one block.
52 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100053STATIC int xfs_attr_node_get(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
55STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
57STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
58
Dave Chinnerabec5f22013-08-12 20:49:38 +100059int
Christoph Hellwigcaf8aab2008-06-23 13:23:41 +100060xfs_inode_hasattr(
61 struct xfs_inode *ip)
62{
63 if (!XFS_IFORK_Q(ip) ||
64 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
65 ip->i_d.di_anextents == 0))
66 return 0;
67 return 1;
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*========================================================================
71 * Overall external interface routines.
72 *========================================================================*/
73
Dave Chinner728bcaa2019-08-29 09:04:08 -070074/*
75 * Retrieve an extended attribute and its value. Must have ilock.
76 * Returns 0 on successful retrieval, otherwise an error.
77 */
Darrick J. Wongad017f62017-06-16 11:00:14 -070078int
79xfs_attr_get_ilocked(
Darrick J. Wongad017f62017-06-16 11:00:14 -070080 struct xfs_da_args *args)
81{
Christoph Hellwigc36f5332020-02-26 17:30:34 -080082 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Christoph Hellwigcf69f822017-07-13 12:14:33 -070083
Christoph Hellwigc36f5332020-02-26 17:30:34 -080084 if (!xfs_inode_hasattr(args->dp))
Darrick J. Wongad017f62017-06-16 11:00:14 -070085 return -ENOATTR;
Christoph Hellwigc36f5332020-02-26 17:30:34 -080086
87 if (args->dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
Darrick J. Wongad017f62017-06-16 11:00:14 -070088 return xfs_attr_shortform_getvalue(args);
Christoph Hellwigc36f5332020-02-26 17:30:34 -080089 if (xfs_bmap_one_block(args->dp, XFS_ATTR_FORK))
Darrick J. Wongad017f62017-06-16 11:00:14 -070090 return xfs_attr_leaf_get(args);
Christoph Hellwigc36f5332020-02-26 17:30:34 -080091 return xfs_attr_node_get(args);
Darrick J. Wongad017f62017-06-16 11:00:14 -070092}
93
Dave Chinnerddbca702019-08-29 09:04:10 -070094/*
95 * Retrieve an extended attribute by name, and its value if requested.
96 *
Christoph Hellwige513e252020-02-26 17:30:35 -080097 * If args->valuelen is zero, then the caller does not want the value, just an
98 * indication whether the attribute exists and the size of the value if it
99 * exists. The size is returned in args.valuelen.
Dave Chinnerddbca702019-08-29 09:04:10 -0700100 *
Christoph Hellwigd49db182020-02-26 17:30:35 -0800101 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
102 * for the value after existence of the attribute has been determined. The
103 * caller always has to free args->value if it is set, no matter if this
104 * function was successful or not.
105 *
Dave Chinnerddbca702019-08-29 09:04:10 -0700106 * If the attribute is found, but exceeds the size limit set by the caller in
Christoph Hellwige5171d72020-02-26 17:30:34 -0800107 * args->valuelen, return -ERANGE with the size of the attribute that was found
108 * in args->valuelen.
Dave Chinnerddbca702019-08-29 09:04:10 -0700109 */
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000110int
111xfs_attr_get(
Christoph Hellwige5171d72020-02-26 17:30:34 -0800112 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Christoph Hellwigb87d0222014-05-13 16:34:24 +1000114 uint lock_mode;
115 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Christoph Hellwige5171d72020-02-26 17:30:34 -0800117 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Christoph Hellwige5171d72020-02-26 17:30:34 -0800119 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000120 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Christoph Hellwige5171d72020-02-26 17:30:34 -0800122 args->geo = args->dp->i_mount->m_attr_geo;
123 args->whichfork = XFS_ATTR_FORK;
124 args->hashval = xfs_da_hashname(args->name, args->namelen);
Barry Naujoke8b0eba2008-04-22 17:34:31 +1000125
Eric Sandeenc400ee32015-08-19 10:30:48 +1000126 /* Entirely possible to look up a name which doesn't exist */
Christoph Hellwige5171d72020-02-26 17:30:34 -0800127 args->op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Christoph Hellwige5171d72020-02-26 17:30:34 -0800129 lock_mode = xfs_ilock_attr_map_shared(args->dp);
Christoph Hellwigc36f5332020-02-26 17:30:34 -0800130 error = xfs_attr_get_ilocked(args);
Christoph Hellwige5171d72020-02-26 17:30:34 -0800131 xfs_iunlock(args->dp, lock_mode);
Dave Chinnerddbca702019-08-29 09:04:10 -0700132
Christoph Hellwige5171d72020-02-26 17:30:34 -0800133 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000136/*
137 * Calculate how many blocks we need for the new attribute,
138 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000139STATIC int
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000140xfs_attr_calc_size(
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000141 struct xfs_da_args *args,
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000142 int *local)
143{
Christoph Hellwig6c888af2014-05-13 16:40:19 +1000144 struct xfs_mount *mp = args->dp->i_mount;
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000145 int size;
146 int nblks;
147
148 /*
149 * Determine space new attribute will use, and if it would be
150 * "local" or "remote" (note: local != inline).
151 */
Dave Chinnerc59f0ad2014-06-06 15:21:27 +1000152 size = xfs_attr_leaf_newentsize(args, local);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000153 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
154 if (*local) {
Dave Chinner33a60392014-06-06 15:21:10 +1000155 if (size > (args->geo->blksize / 2)) {
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000156 /* Double split possible */
157 nblks *= 2;
158 }
159 } else {
160 /*
161 * Out of line attribute, cannot double split, but
162 * make room for the attribute value itself.
163 */
Dave Chinner2d6dcc62014-05-15 09:39:28 +1000164 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
Niv Sardi5e9da7b2008-08-13 16:03:35 +1000165 nblks += dblocks;
166 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
167 }
168
169 return nblks;
170}
171
Allison Henderson4c74a562018-10-18 17:20:50 +1100172STATIC int
173xfs_attr_try_sf_addname(
174 struct xfs_inode *dp,
175 struct xfs_da_args *args)
176{
177
178 struct xfs_mount *mp = dp->i_mount;
179 int error, error2;
180
181 error = xfs_attr_shortform_addname(args);
182 if (error == -ENOSPC)
183 return error;
184
185 /*
186 * Commit the shortform mods, and we're done.
187 * NOTE: this is also the error path (EEXIST, etc).
188 */
Christoph Hellwig1d733012020-02-26 17:30:36 -0800189 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
Allison Henderson4c74a562018-10-18 17:20:50 +1100190 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
191
192 if (mp->m_flags & XFS_MOUNT_WSYNC)
193 xfs_trans_set_sync(args->trans);
194
195 error2 = xfs_trans_commit(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100196 args->trans = NULL;
Allison Henderson4c74a562018-10-18 17:20:50 +1100197 return error ? error : error2;
198}
199
Allison Henderson2f3cd802018-10-18 17:21:16 +1100200/*
201 * Set the attribute specified in @args.
202 */
203int
204xfs_attr_set_args(
Darrick J. Wong710d7072019-04-24 09:27:41 -0700205 struct xfs_da_args *args)
Allison Henderson2f3cd802018-10-18 17:21:16 +1100206{
207 struct xfs_inode *dp = args->dp;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700208 struct xfs_buf *leaf_bp = NULL;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100209 int error;
210
211 /*
212 * If the attribute list is non-existent or a shortform list,
213 * upgrade it to a single-leaf-block attribute list.
214 */
215 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
216 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
217 dp->i_d.di_anextents == 0)) {
218
219 /*
220 * Build initial attribute list (if required).
221 */
222 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
223 xfs_attr_shortform_create(args);
224
225 /*
226 * Try to add the attr to the attribute list in the inode.
227 */
228 error = xfs_attr_try_sf_addname(dp, args);
229 if (error != -ENOSPC)
230 return error;
231
232 /*
233 * It won't fit in the shortform, transform to a leaf block.
234 * GROT: another possible req'mt for a double-split btree op.
235 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700236 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100237 if (error)
238 return error;
239
240 /*
241 * Prevent the leaf buffer from being unlocked so that a
242 * concurrent AIL push cannot grab the half-baked leaf
243 * buffer and run into problems with the write verifier.
Darrick J. Wong710d7072019-04-24 09:27:41 -0700244 * Once we're done rolling the transaction we can release
245 * the hold and add the attr to the leaf.
Allison Henderson2f3cd802018-10-18 17:21:16 +1100246 */
Darrick J. Wong710d7072019-04-24 09:27:41 -0700247 xfs_trans_bhold(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100248 error = xfs_defer_finish(&args->trans);
Darrick J. Wong710d7072019-04-24 09:27:41 -0700249 xfs_trans_bhold_release(args->trans, leaf_bp);
250 if (error) {
251 xfs_trans_brelse(args->trans, leaf_bp);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100252 return error;
Darrick J. Wong710d7072019-04-24 09:27:41 -0700253 }
Allison Henderson2f3cd802018-10-18 17:21:16 +1100254 }
255
256 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
257 error = xfs_attr_leaf_addname(args);
258 else
259 error = xfs_attr_node_addname(args);
260 return error;
261}
262
Allison Henderson068f9852018-10-18 17:21:23 +1100263/*
264 * Remove the attribute specified in @args.
265 */
266int
267xfs_attr_remove_args(
268 struct xfs_da_args *args)
269{
270 struct xfs_inode *dp = args->dp;
271 int error;
272
273 if (!xfs_inode_hasattr(dp)) {
274 error = -ENOATTR;
275 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
276 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
277 error = xfs_attr_shortform_remove(args);
278 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
279 error = xfs_attr_leaf_removename(args);
280 } else {
281 error = xfs_attr_node_removename(args);
282 }
283
284 return error;
285}
286
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800287/*
Christoph Hellwiga2544622020-02-26 17:30:33 -0800288 * Note: If args->value is NULL the attribute will be removed, just like the
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800289 * Linux ->setattr API.
290 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000291int
292xfs_attr_set(
Christoph Hellwiga2544622020-02-26 17:30:33 -0800293 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Christoph Hellwiga2544622020-02-26 17:30:33 -0800295 struct xfs_inode *dp = args->dp;
Jie Liu3d3c8b52013-08-12 20:49:59 +1000296 struct xfs_mount *mp = dp->i_mount;
297 struct xfs_trans_res tres;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800298 int rsvd = (args->flags & ATTR_ROOT) != 0;
Allison Henderson4c74a562018-10-18 17:20:50 +1100299 int error, local;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800300 unsigned int total;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000301
302 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
Dave Chinner24513372014-06-25 14:58:08 +1000303 return -EIO;
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000304
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800305 error = xfs_qm_dqattach(dp);
306 if (error)
307 return error;
308
Christoph Hellwiga2544622020-02-26 17:30:33 -0800309 args->geo = mp->m_attr_geo;
310 args->whichfork = XFS_ATTR_FORK;
311 args->hashval = xfs_da_hashname(args->name, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /*
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800314 * We have no control over the attribute names that userspace passes us
315 * to remove, so we have to allow the name lookup prior to attribute
316 * removal to fail as well.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800318 args->op_flags = XFS_DA_OP_OKNOENT;
Barry Naujoke5889e92007-02-10 18:35:58 +1100319
Christoph Hellwiga2544622020-02-26 17:30:33 -0800320 if (args->value) {
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800321 XFS_STATS_INC(mp, xs_attr_set);
322
Christoph Hellwiga2544622020-02-26 17:30:33 -0800323 args->op_flags |= XFS_DA_OP_ADDNAME;
324 args->total = xfs_attr_calc_size(args, &local);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800325
326 /*
327 * If the inode doesn't have an attribute fork, add one.
328 * (inode must not be locked when we call this routine)
329 */
330 if (XFS_IFORK_Q(dp) == 0) {
331 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
Christoph Hellwiga2544622020-02-26 17:30:33 -0800332 XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen,
333 args->valuelen);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800334
335 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
336 if (error)
337 return error;
338 }
339
340 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
Christoph Hellwiga2544622020-02-26 17:30:33 -0800341 M_RES(mp)->tr_attrsetrt.tr_logres *
342 args->total;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800343 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
344 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800345 total = args->total;
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800346 } else {
347 XFS_STATS_INC(mp, xs_attr_remove);
348
349 tres = M_RES(mp)->tr_attrrm;
350 total = XFS_ATTRRM_SPACE_RES(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
354 * Root fork attributes can use reserved data blocks for this
355 * operation if necessary
356 */
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800357 error = xfs_trans_alloc(mp, &tres, total, 0,
Christoph Hellwiga2544622020-02-26 17:30:33 -0800358 rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
Christoph Hellwig253f4912016-04-06 09:19:55 +1000359 if (error)
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000360 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Christoph Hellwig253f4912016-04-06 09:19:55 +1000362 xfs_ilock(dp, XFS_ILOCK_EXCL);
Christoph Hellwiga2544622020-02-26 17:30:33 -0800363 xfs_trans_ijoin(args->trans, dp, 0);
364 if (args->value) {
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800365 unsigned int quota_flags = XFS_QMOPT_RES_REGBLKS;
366
367 if (rsvd)
368 quota_flags |= XFS_QMOPT_FORCE_RES;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800369 error = xfs_trans_reserve_quota_nblks(args->trans, dp,
370 args->total, 0, quota_flags);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800371 if (error)
372 goto out_trans_cancel;
Christoph Hellwiga2544622020-02-26 17:30:33 -0800373 error = xfs_attr_set_args(args);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800374 if (error)
375 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100376 /* shortform attribute has already been committed */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800377 if (!args->trans)
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800378 goto out_unlock;
379 } else {
Christoph Hellwiga2544622020-02-26 17:30:33 -0800380 error = xfs_attr_remove_args(args);
Christoph Hellwig0eb81a52020-02-26 17:30:29 -0800381 if (error)
382 goto out_trans_cancel;
Allison Henderson2f3cd802018-10-18 17:21:16 +1100383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /*
386 * If this is a synchronous mount, make sure that the
387 * transaction goes to disk before returning to the user.
388 */
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000389 if (mp->m_flags & XFS_MOUNT_WSYNC)
Christoph Hellwiga2544622020-02-26 17:30:33 -0800390 xfs_trans_set_sync(args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Christoph Hellwig1d733012020-02-26 17:30:36 -0800392 if (!(args->op_flags & XFS_DA_OP_NOTIME))
Christoph Hellwiga2544622020-02-26 17:30:33 -0800393 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
Dave Chinnerdcd79a12010-09-28 12:27:25 +1000394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * Commit the last in the sequence of transactions.
397 */
Christoph Hellwiga2544622020-02-26 17:30:33 -0800398 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
399 error = xfs_trans_commit(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100400out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Christoph Hellwigc5b4ac32014-05-13 16:34:14 +1000402 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Allison Henderson2f3cd802018-10-18 17:21:16 +1100404out_trans_cancel:
Christoph Hellwiga2544622020-02-26 17:30:33 -0800405 if (args->trans)
406 xfs_trans_cancel(args->trans);
Allison Henderson2f3cd802018-10-18 17:21:16 +1100407 goto out_unlock;
Nathan Scottaa82daa2005-11-02 10:33:33 +1100408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*========================================================================
411 * External routines when attribute list is inside the inode
412 *========================================================================*/
413
414/*
415 * Add a name to the shortform attribute list structure
416 * This is the external routine.
417 */
418STATIC int
419xfs_attr_shortform_addname(xfs_da_args_t *args)
420{
Nathan Scottd8cc8902005-11-02 10:34:53 +1100421 int newsize, forkoff, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dave Chinner5a5881c2012-03-22 05:15:13 +0000423 trace_xfs_attr_sf_addname(args);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 retval = xfs_attr_shortform_lookup(args);
Dave Chinner24513372014-06-25 14:58:08 +1000426 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000427 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000428 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (args->flags & ATTR_CREATE)
Eric Sandeend99831f2014-06-22 15:03:54 +1000430 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 retval = xfs_attr_shortform_remove(args);
Darrick J. Wong7b384602018-04-17 19:10:15 -0700432 if (retval)
433 return retval;
434 /*
435 * Since we have removed the old attr, clear ATTR_REPLACE so
436 * that the leaf format add routine won't trip over the attr
437 * not being around.
438 */
439 args->flags &= ~ATTR_REPLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Nathan Scottd8cc8902005-11-02 10:34:53 +1100442 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
443 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
Dave Chinner24513372014-06-25 14:58:08 +1000444 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
447 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100448
449 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
450 if (!forkoff)
Dave Chinner24513372014-06-25 14:58:08 +1000451 return -ENOSPC;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100452
453 xfs_attr_shortform_add(args, forkoff);
Eric Sandeend99831f2014-06-22 15:03:54 +1000454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457
458/*========================================================================
459 * External routines when attribute list is one block
460 *========================================================================*/
461
462/*
463 * Add a name to the leaf attribute list structure
464 *
465 * This leaf block cannot have a "remote" value, we only call this routine
466 * if bmap_one_block() says there is only one block (ie: no remote blks).
467 */
David Chinnera8272ce2007-11-23 16:28:09 +1100468STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700469xfs_attr_leaf_addname(
470 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700472 struct xfs_inode *dp;
473 struct xfs_buf *bp;
474 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dave Chinner5a5881c2012-03-22 05:15:13 +0000476 trace_xfs_attr_leaf_addname(args);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /*
479 * Read the (only) block in the attribute list in.
480 */
481 dp = args->dp;
482 args->blkno = 0;
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800483 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100485 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /*
488 * Look up the given attribute in the leaf block. Figure out if
489 * the given flags produce an error or call for an atomic rename.
490 */
Dave Chinner517c2222013-04-24 18:58:55 +1000491 retval = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000492 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000493 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000494 return retval;
Dave Chinner24513372014-06-25 14:58:08 +1000495 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (args->flags & ATTR_CREATE) { /* pure create op */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000497 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000498 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Dave Chinner5a5881c2012-03-22 05:15:13 +0000500
501 trace_xfs_attr_leaf_replace(args);
502
Dave Chinner8275cdd2014-05-06 07:37:31 +1000503 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000504 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 args->blkno2 = args->blkno; /* set 2nd entry info*/
506 args->index2 = args->index;
507 args->rmtblkno2 = args->rmtblkno;
508 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000509 args->rmtvaluelen2 = args->rmtvaluelen;
510
511 /*
512 * clear the remote attr state now that it is saved so that the
513 * values reflect the state of the attribute we are about to
514 * add, not the attribute we just found and will remove later.
515 */
516 args->rmtblkno = 0;
517 args->rmtblkcnt = 0;
518 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 /*
522 * Add the attribute to the leaf block, transitioning to a Btree
523 * if required.
524 */
Dave Chinner517c2222013-04-24 18:58:55 +1000525 retval = xfs_attr3_leaf_add(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000526 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * Promote the attribute list to the Btree format, then
529 * Commit that transaction so that the node_addname() call
530 * can manage its own transactions.
531 */
Dave Chinner517c2222013-04-24 18:58:55 +1000532 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700533 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000534 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700535 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700536 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700537 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * Commit the current trans (including the inode) and start
541 * a new one.
542 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700543 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000544 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000545 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /*
548 * Fob the whole rest of the problem off on the Btree code.
549 */
550 error = xfs_attr_node_addname(args);
Eric Sandeend99831f2014-06-22 15:03:54 +1000551 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
554 /*
555 * Commit the transaction that added the attr name so that
556 * later routines can manage their own transactions.
557 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700558 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000559 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000560 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /*
563 * If there was an out-of-line value, allocate the blocks we
564 * identified for its storage and copy the value. This is done
565 * after we create the attribute so that we don't overflow the
566 * maximum size of a transaction and/or hit a deadlock.
567 */
568 if (args->rmtblkno > 0) {
569 error = xfs_attr_rmtval_set(args);
570 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000571 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 /*
575 * If this is an atomic rename operation, we must "flip" the
576 * incomplete flags on the "new" and "old" attribute/value pairs
577 * so that one disappears and one appears atomically. Then we
578 * must remove the "old" attribute/value pair.
579 */
Barry Naujok6a178102008-05-21 16:42:05 +1000580 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * In a separate transaction, set the incomplete flag on the
583 * "old" attr and clear the incomplete flag on the "new" attr.
584 */
Dave Chinner517c2222013-04-24 18:58:55 +1000585 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000587 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /*
590 * Dismantle the "old" attribute/value pair by removing
591 * a "remote" value (if it exists).
592 */
593 args->index = args->index2;
594 args->blkno = args->blkno2;
595 args->rmtblkno = args->rmtblkno2;
596 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000597 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (args->rmtblkno) {
599 error = xfs_attr_rmtval_remove(args);
600 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000601 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
604 /*
605 * Read in the block containing the "old" attr, then
606 * remove the "old" attr from that block (neat, huh!)
607 */
Dave Chinner517c2222013-04-24 18:58:55 +1000608 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800609 &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100611 return error;
612
Dave Chinner517c2222013-04-24 18:58:55 +1000613 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /*
616 * If the result is small enough, shrink it all into the inode.
617 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100618 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +1000619 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700621 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000622 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700623 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700624 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700625 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /*
629 * Commit the remove and start the next trans in series.
630 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700631 error = xfs_trans_roll_inode(&args->trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 } else if (args->rmtblkno > 0) {
634 /*
635 * Added a "remote" value, just clear the incomplete flag.
636 */
Dave Chinner517c2222013-04-24 18:58:55 +1000637 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Dave Chinner517c2222013-04-24 18:58:55 +1000639 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642/*
643 * Remove a name from the leaf attribute list structure
644 *
645 * This leaf block cannot have a "remote" value, we only call this routine
646 * if bmap_one_block() says there is only one block (ie: no remote blks).
647 */
648STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700649xfs_attr_leaf_removename(
650 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700652 struct xfs_inode *dp;
653 struct xfs_buf *bp;
654 int error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dave Chinner5a5881c2012-03-22 05:15:13 +0000656 trace_xfs_attr_leaf_removename(args);
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * Remove the attribute.
660 */
661 dp = args->dp;
662 args->blkno = 0;
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800663 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +1100664 if (error)
665 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Dave Chinner517c2222013-04-24 18:58:55 +1000667 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000668 if (error == -ENOATTR) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000669 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000670 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Dave Chinner517c2222013-04-24 18:58:55 +1000673 xfs_attr3_leaf_remove(bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /*
676 * If the result is small enough, shrink it all into the inode.
677 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100678 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +1000679 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700681 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000682 return error;
Brian Foster9e28a242018-07-24 13:43:15 -0700683 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700684 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700685 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000686 }
Dave Chinner517c2222013-04-24 18:58:55 +1000687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690/*
691 * Look up a name in a 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).
Dave Chinner728bcaa2019-08-29 09:04:08 -0700695 *
696 * Returns 0 on successful retrieval, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000698STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699xfs_attr_leaf_get(xfs_da_args_t *args)
700{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000701 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 int error;
703
Dave Chinneree732592012-11-12 22:53:53 +1100704 trace_xfs_attr_leaf_get(args);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 args->blkno = 0;
Christoph Hellwigdfb87592019-11-20 09:46:02 -0800707 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (error)
Dave Chinnerad14c332012-11-12 22:54:16 +1100709 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Chinner517c2222013-04-24 18:58:55 +1000711 error = xfs_attr3_leaf_lookup_int(bp, args);
Dave Chinner24513372014-06-25 14:58:08 +1000712 if (error != -EEXIST) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000713 xfs_trans_brelse(args->trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +1000714 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Dave Chinner517c2222013-04-24 18:58:55 +1000716 error = xfs_attr3_leaf_getvalue(bp, args);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000717 xfs_trans_brelse(args->trans, bp);
Dave Chinnere3cc4552019-08-29 09:04:09 -0700718 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*========================================================================
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000722 * External routines when attribute list size > geo->blksize
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 *========================================================================*/
724
725/*
726 * Add a name to a Btree-format attribute list.
727 *
728 * This will involve walking down the Btree, and may involve splitting
729 * leaf nodes and even splitting intermediate nodes up to and including
730 * the root node (a special case of an intermediate node).
731 *
732 * "Remote" attribute values confuse the issue and atomic rename operations
733 * add a whole extra layer of confusion on top of that.
734 */
735STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700736xfs_attr_node_addname(
737 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700739 struct xfs_da_state *state;
740 struct xfs_da_state_blk *blk;
741 struct xfs_inode *dp;
742 struct xfs_mount *mp;
743 int retval, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Chinner5a5881c2012-03-22 05:15:13 +0000745 trace_xfs_attr_node_addname(args);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /*
748 * Fill in bucket of arguments/results/context to carry around.
749 */
750 dp = args->dp;
751 mp = dp->i_mount;
752restart:
753 state = xfs_da_state_alloc();
754 state->args = args;
755 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /*
758 * Search to see if name already exists, and get back a pointer
759 * to where it should go.
760 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000761 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (error)
763 goto out;
764 blk = &state->path.blk[ state->path.active-1 ];
765 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner24513372014-06-25 14:58:08 +1000766 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto out;
Dave Chinner24513372014-06-25 14:58:08 +1000768 } else if (retval == -EEXIST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (args->flags & ATTR_CREATE)
770 goto out;
Dave Chinner5a5881c2012-03-22 05:15:13 +0000771
772 trace_xfs_attr_node_replace(args);
773
Dave Chinner8275cdd2014-05-06 07:37:31 +1000774 /* save the attribute state for later removal*/
Barry Naujok6a178102008-05-21 16:42:05 +1000775 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 args->blkno2 = args->blkno; /* set 2nd entry info*/
777 args->index2 = args->index;
778 args->rmtblkno2 = args->rmtblkno;
779 args->rmtblkcnt2 = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000780 args->rmtvaluelen2 = args->rmtvaluelen;
781
782 /*
783 * clear the remote attr state now that it is saved so that the
784 * values reflect the state of the attribute we are about to
785 * add, not the attribute we just found and will remove later.
786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 args->rmtblkno = 0;
788 args->rmtblkcnt = 0;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000789 args->rmtvaluelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Dave Chinner517c2222013-04-24 18:58:55 +1000792 retval = xfs_attr3_leaf_add(blk->bp, state->args);
Dave Chinner24513372014-06-25 14:58:08 +1000793 if (retval == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (state->path.active == 1) {
795 /*
796 * Its really a single leaf node, but it had
797 * out-of-line values so it looked like it *might*
798 * have been a b-tree.
799 */
800 xfs_da_state_free(state);
Eric Sandeen6dd93e92013-07-31 20:18:54 -0500801 state = NULL;
Dave Chinner517c2222013-04-24 18:58:55 +1000802 error = xfs_attr3_leaf_to_node(args);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700803 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000804 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700805 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700806 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 * Commit the node conversion and start the next
811 * trans in the chain.
812 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700813 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000814 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto out;
816
817 goto restart;
818 }
819
820 /*
821 * Split as many Btree elements as required.
822 * This code tracks the new and old attr's location
823 * in the index/blkno/rmtblkno/rmtblkcnt fields and
824 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
825 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000826 error = xfs_da3_split(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700827 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000828 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700829 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700830 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 } else {
833 /*
834 * Addition succeeded, update Btree hashvals.
835 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000836 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 /*
840 * Kill the state structure, we're done with it and need to
841 * allow the buffers to come back later.
842 */
843 xfs_da_state_free(state);
844 state = NULL;
845
846 /*
847 * Commit the leaf addition or btree split and start the next
848 * trans in the chain.
849 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700850 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000851 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto out;
853
854 /*
855 * If there was an out-of-line value, allocate the blocks we
856 * identified for its storage and copy the value. This is done
857 * after we create the attribute so that we don't overflow the
858 * maximum size of a transaction and/or hit a deadlock.
859 */
860 if (args->rmtblkno > 0) {
861 error = xfs_attr_rmtval_set(args);
862 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000863 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
866 /*
867 * If this is an atomic rename operation, we must "flip" the
868 * incomplete flags on the "new" and "old" attribute/value pairs
869 * so that one disappears and one appears atomically. Then we
870 * must remove the "old" attribute/value pair.
871 */
Barry Naujok6a178102008-05-21 16:42:05 +1000872 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /*
874 * In a separate transaction, set the incomplete flag on the
875 * "old" attr and clear the incomplete flag on the "new" attr.
876 */
Dave Chinner517c2222013-04-24 18:58:55 +1000877 error = xfs_attr3_leaf_flipflags(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (error)
879 goto out;
880
881 /*
882 * Dismantle the "old" attribute/value pair by removing
883 * a "remote" value (if it exists).
884 */
885 args->index = args->index2;
886 args->blkno = args->blkno2;
887 args->rmtblkno = args->rmtblkno2;
888 args->rmtblkcnt = args->rmtblkcnt2;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000889 args->rmtvaluelen = args->rmtvaluelen2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (args->rmtblkno) {
891 error = xfs_attr_rmtval_remove(args);
892 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000893 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
896 /*
897 * Re-find the "old" attribute entry after any split ops.
898 * The INCOMPLETE flag means that we will find the "old"
899 * attr, not the "new" one.
900 */
Christoph Hellwig780d2902020-01-07 15:25:39 -0800901 args->op_flags |= XFS_DA_OP_INCOMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 state = xfs_da_state_alloc();
903 state->args = args;
904 state->mp = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 state->inleaf = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000906 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (error)
908 goto out;
909
910 /*
911 * Remove the name and update the hashvals in the tree.
912 */
913 blk = &state->path.blk[ state->path.active-1 ];
914 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +1000915 error = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000916 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /*
919 * Check to see if the tree needs to be collapsed.
920 */
921 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000922 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700923 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +1000924 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -0700925 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700926 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -0700927 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
930 /*
931 * Commit and start the next trans in the chain.
932 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700933 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +1000934 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto out;
936
937 } else if (args->rmtblkno > 0) {
938 /*
939 * Added a "remote" value, just clear the incomplete flag.
940 */
Dave Chinner517c2222013-04-24 18:58:55 +1000941 error = xfs_attr3_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (error)
943 goto out;
944 }
945 retval = error = 0;
946
947out:
948 if (state)
949 xfs_da_state_free(state);
950 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000951 return error;
952 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955/*
956 * Remove a name from a B-tree attribute list.
957 *
958 * This will involve walking down the Btree, and may involve joining
959 * leaf nodes and even joining intermediate nodes up to and including
960 * the root node (a special case of an intermediate node).
961 */
962STATIC int
Brian Foster32a9b7c2018-07-11 22:26:11 -0700963xfs_attr_node_removename(
964 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Brian Foster32a9b7c2018-07-11 22:26:11 -0700966 struct xfs_da_state *state;
967 struct xfs_da_state_blk *blk;
968 struct xfs_inode *dp;
969 struct xfs_buf *bp;
970 int retval, error, forkoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dave Chinner5a5881c2012-03-22 05:15:13 +0000972 trace_xfs_attr_node_removename(args);
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /*
975 * Tie a string around our finger to remind us where we are.
976 */
977 dp = args->dp;
978 state = xfs_da_state_alloc();
979 state->args = args;
980 state->mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /*
983 * Search to see if name exists, and get back a pointer to it.
984 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000985 error = xfs_da3_node_lookup_int(state, &retval);
Dave Chinner24513372014-06-25 14:58:08 +1000986 if (error || (retval != -EEXIST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (error == 0)
988 error = retval;
989 goto out;
990 }
991
992 /*
993 * If there is an out-of-line value, de-allocate the blocks.
994 * This is done before we remove the attribute so that we don't
995 * overflow the maximum size of a transaction and/or hit a deadlock.
996 */
997 blk = &state->path.blk[ state->path.active-1 ];
998 ASSERT(blk->bp != NULL);
999 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1000 if (args->rmtblkno > 0) {
1001 /*
1002 * Fill in disk block numbers in the state structure
1003 * so that we can get the buffers back after we commit
1004 * several transactions in the following calls.
1005 */
1006 error = xfs_attr_fillstate(state);
1007 if (error)
1008 goto out;
1009
1010 /*
1011 * Mark the attribute as INCOMPLETE, then bunmapi() the
1012 * remote value.
1013 */
Dave Chinner517c2222013-04-24 18:58:55 +10001014 error = xfs_attr3_leaf_setflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (error)
1016 goto out;
1017 error = xfs_attr_rmtval_remove(args);
1018 if (error)
1019 goto out;
1020
1021 /*
1022 * Refill the state structure with buffers, the prior calls
1023 * released our buffers.
1024 */
1025 error = xfs_attr_refillstate(state);
1026 if (error)
1027 goto out;
1028 }
1029
1030 /*
1031 * Remove the name and update the hashvals in the tree.
1032 */
1033 blk = &state->path.blk[ state->path.active-1 ];
1034 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner517c2222013-04-24 18:58:55 +10001035 retval = xfs_attr3_leaf_remove(blk->bp, args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001036 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /*
1039 * Check to see if the tree needs to be collapsed.
1040 */
1041 if (retval && (state->path.active > 1)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001042 error = xfs_da3_join(state);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001043 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001044 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001045 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001046 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Commit the Btree join operation and start a new trans.
1050 */
Christoph Hellwig411350d2017-08-28 10:21:03 -07001051 error = xfs_trans_roll_inode(&args->trans, dp);
Niv Sardi322ff6b2008-08-13 16:05:49 +10001052 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 goto out;
1054 }
1055
1056 /*
1057 * If the result is small enough, push it all into the inode.
1058 */
1059 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1060 /*
1061 * Have to get rid of the copy of this dabuf in the state.
1062 */
1063 ASSERT(state->path.active == 1);
1064 ASSERT(state->path.blk[0].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 state->path.blk[0].bp = NULL;
1066
Christoph Hellwigdfb87592019-11-20 09:46:02 -08001067 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (error)
1069 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Nathan Scottd8cc8902005-11-02 10:34:53 +11001071 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
Dave Chinner517c2222013-04-24 18:58:55 +10001072 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* bp is gone due to xfs_da_shrink_inode */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001074 if (error)
Brian Fosterd5a2e282018-09-29 13:41:58 +10001075 goto out;
Brian Foster9e28a242018-07-24 13:43:15 -07001076 error = xfs_defer_finish(&args->trans);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001077 if (error)
Brian Foster9b1f4e92018-08-01 07:20:33 -07001078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 } else
Dave Chinner1d9025e2012-06-22 18:50:14 +10001080 xfs_trans_brelse(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082 error = 0;
1083
1084out:
1085 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001086 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
1090 * Fill in the disk block numbers in the state structure for the buffers
1091 * that are attached to the state structure.
1092 * This is done so that we can quickly reattach ourselves to those buffers
Nathan Scottc41564b2006-03-29 08:55:14 +10001093 * after some set of transaction commits have released these buffers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
1095STATIC int
1096xfs_attr_fillstate(xfs_da_state_t *state)
1097{
1098 xfs_da_state_path_t *path;
1099 xfs_da_state_blk_t *blk;
1100 int level;
1101
Dave Chinneree732592012-11-12 22:53:53 +11001102 trace_xfs_attr_fillstate(state->args);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * Roll down the "path" in the state structure, storing the on-disk
1106 * block number for those buffers in the "path".
1107 */
1108 path = &state->path;
1109 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1110 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1111 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001112 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 blk->bp = NULL;
1114 } else {
1115 blk->disk_blkno = 0;
1116 }
1117 }
1118
1119 /*
1120 * Roll down the "altpath" in the state structure, storing the on-disk
1121 * block number for those buffers in the "altpath".
1122 */
1123 path = &state->altpath;
1124 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1125 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1126 if (blk->bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001127 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 blk->bp = NULL;
1129 } else {
1130 blk->disk_blkno = 0;
1131 }
1132 }
1133
Eric Sandeend99831f2014-06-22 15:03:54 +10001134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/*
1138 * Reattach the buffers to the state structure based on the disk block
1139 * numbers stored in the state structure.
Nathan Scottc41564b2006-03-29 08:55:14 +10001140 * This is done after some set of transaction commits have released those
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * buffers from our grip.
1142 */
1143STATIC int
1144xfs_attr_refillstate(xfs_da_state_t *state)
1145{
1146 xfs_da_state_path_t *path;
1147 xfs_da_state_blk_t *blk;
1148 int level, error;
1149
Dave Chinneree732592012-11-12 22:53:53 +11001150 trace_xfs_attr_refillstate(state->args);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /*
1153 * Roll down the "path" in the state structure, storing the on-disk
1154 * block number for those buffers in the "path".
1155 */
1156 path = &state->path;
1157 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1158 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1159 if (blk->disk_blkno) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001160 error = xfs_da3_node_read_mapped(state->args->trans,
1161 state->args->dp, blk->disk_blkno,
1162 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001164 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } else {
1166 blk->bp = NULL;
1167 }
1168 }
1169
1170 /*
1171 * Roll down the "altpath" in the state structure, storing the on-disk
1172 * block number for those buffers in the "altpath".
1173 */
1174 path = &state->altpath;
1175 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1176 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1177 if (blk->disk_blkno) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001178 error = xfs_da3_node_read_mapped(state->args->trans,
1179 state->args->dp, blk->disk_blkno,
1180 &blk->bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001182 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 } else {
1184 blk->bp = NULL;
1185 }
1186 }
1187
Eric Sandeend99831f2014-06-22 15:03:54 +10001188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191/*
Dave Chinner728bcaa2019-08-29 09:04:08 -07001192 * Retrieve the attribute data from a node attribute list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 *
1194 * This routine gets called for any attribute fork that has more than one
1195 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1196 * "remote" values taking up more blocks.
Dave Chinner728bcaa2019-08-29 09:04:08 -07001197 *
1198 * Returns 0 on successful retrieval, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001200STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201xfs_attr_node_get(xfs_da_args_t *args)
1202{
1203 xfs_da_state_t *state;
1204 xfs_da_state_blk_t *blk;
1205 int error, retval;
1206 int i;
1207
Dave Chinneree732592012-11-12 22:53:53 +11001208 trace_xfs_attr_node_get(args);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 state = xfs_da_state_alloc();
1211 state->args = args;
1212 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /*
1215 * Search to see if name exists, and get back a pointer to it.
1216 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001217 error = xfs_da3_node_lookup_int(state, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (error) {
1219 retval = error;
Dave Chinner728bcaa2019-08-29 09:04:08 -07001220 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Dave Chinner728bcaa2019-08-29 09:04:08 -07001222 if (retval != -EEXIST)
1223 goto out_release;
1224
1225 /*
1226 * Get the value, local or "remote"
1227 */
1228 blk = &state->path.blk[state->path.active - 1];
1229 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /*
1232 * If not in a transaction, we have to release all the buffers.
1233 */
Dave Chinner728bcaa2019-08-29 09:04:08 -07001234out_release:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001236 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 state->path.blk[i].bp = NULL;
1238 }
1239
1240 xfs_da_state_free(state);
Eric Sandeend99831f2014-06-22 15:03:54 +10001241 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
Darrick J. Wong65480532019-02-01 09:08:54 -08001243
1244/* Returns true if the attribute entry name is valid. */
1245bool
1246xfs_attr_namecheck(
1247 const void *name,
1248 size_t length)
1249{
1250 /*
1251 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1252 * out, so use >= for the length check.
1253 */
1254 if (length >= MAXNAMELEN)
1255 return false;
1256
1257 /* There shouldn't be any nulls here */
1258 return !memchr(name, 0, length);
1259}