blob: 96ac7e562b87136146cc0a0eab6f7605477e1b7a [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include "xfs_mount.h"
13#include "xfs_defer.h"
14#include "xfs_da_format.h"
15#include "xfs_da_btree.h"
16#include "xfs_attr_sf.h"
17#include "xfs_inode.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018#include "xfs_trans.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include "xfs_bmap.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020#include "xfs_bmap_btree.h"
21#include "xfs_attr.h"
22#include "xfs_attr_leaf.h"
23#include "xfs_attr_remote.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#include "xfs_quota.h"
25#include "xfs_trans_space.h"
26#include "xfs_trace.h"
27
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 */
46STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
47STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
48STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
Olivier Deprez157378f2022-04-04 15:47:50 +020049STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050
51/*
52 * Internal routines when attribute list is more than one block.
53 */
54STATIC int xfs_attr_node_get(xfs_da_args_t *args);
55STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
56STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
Olivier Deprez157378f2022-04-04 15:47:50 +020057STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
58 struct xfs_da_state **state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
60STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
61
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062int
63xfs_inode_hasattr(
64 struct xfs_inode *ip)
65{
66 if (!XFS_IFORK_Q(ip) ||
Olivier Deprez157378f2022-04-04 15:47:50 +020067 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
68 ip->i_afp->if_nextents == 0))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 return 0;
70 return 1;
71}
72
73/*========================================================================
74 * Overall external interface routines.
75 *========================================================================*/
76
David Brazdil0f672f62019-12-10 10:32:29 +000077/*
78 * Retrieve an extended attribute and its value. Must have ilock.
79 * Returns 0 on successful retrieval, otherwise an error.
80 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081int
82xfs_attr_get_ilocked(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 struct xfs_da_args *args)
84{
Olivier Deprez157378f2022-04-04 15:47:50 +020085 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086
Olivier Deprez157378f2022-04-04 15:47:50 +020087 if (!xfs_inode_hasattr(args->dp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088 return -ENOATTR;
Olivier Deprez157378f2022-04-04 15:47:50 +020089
90 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091 return xfs_attr_shortform_getvalue(args);
Olivier Deprez157378f2022-04-04 15:47:50 +020092 if (xfs_bmap_one_block(args->dp, XFS_ATTR_FORK))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093 return xfs_attr_leaf_get(args);
Olivier Deprez157378f2022-04-04 15:47:50 +020094 return xfs_attr_node_get(args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095}
96
David Brazdil0f672f62019-12-10 10:32:29 +000097/*
98 * Retrieve an extended attribute by name, and its value if requested.
99 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200100 * 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.
103 *
104 * 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.
David Brazdil0f672f62019-12-10 10:32:29 +0000108 *
109 * If the attribute is found, but exceeds the size limit set by the caller in
Olivier Deprez157378f2022-04-04 15:47:50 +0200110 * args->valuelen, return -ERANGE with the size of the attribute that was found
111 * in args->valuelen.
David Brazdil0f672f62019-12-10 10:32:29 +0000112 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113int
114xfs_attr_get(
Olivier Deprez157378f2022-04-04 15:47:50 +0200115 struct xfs_da_args *args)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117 uint lock_mode;
118 int error;
119
Olivier Deprez157378f2022-04-04 15:47:50 +0200120 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
David Brazdil0f672f62019-12-10 10:32:29 +0000121
Olivier Deprez157378f2022-04-04 15:47:50 +0200122 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123 return -EIO;
124
Olivier Deprez157378f2022-04-04 15:47:50 +0200125 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129 /* Entirely possible to look up a name which doesn't exist */
Olivier Deprez157378f2022-04-04 15:47:50 +0200130 args->op_flags = XFS_DA_OP_OKNOENT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131
Olivier Deprez157378f2022-04-04 15:47:50 +0200132 lock_mode = xfs_ilock_attr_map_shared(args->dp);
133 error = xfs_attr_get_ilocked(args);
134 xfs_iunlock(args->dp, lock_mode);
David Brazdil0f672f62019-12-10 10:32:29 +0000135
Olivier Deprez157378f2022-04-04 15:47:50 +0200136 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137}
138
139/*
140 * Calculate how many blocks we need for the new attribute,
141 */
142STATIC int
143xfs_attr_calc_size(
144 struct xfs_da_args *args,
145 int *local)
146{
147 struct xfs_mount *mp = args->dp->i_mount;
148 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 */
155 size = xfs_attr_leaf_newentsize(args, local);
156 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
157 if (*local) {
158 if (size > (args->geo->blksize / 2)) {
159 /* 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 */
167 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
168 nblks += dblocks;
169 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
170 }
171
172 return nblks;
173}
174
David Brazdil0f672f62019-12-10 10:32:29 +0000175STATIC int
176xfs_attr_try_sf_addname(
177 struct xfs_inode *dp,
178 struct xfs_da_args *args)
179{
180
Olivier Deprez157378f2022-04-04 15:47:50 +0200181 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);
David Brazdil0f672f62019-12-10 10:32:29 +0000188
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 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200197 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
David Brazdil0f672f62019-12-10 10:32:29 +0000198 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
199
Olivier Deprez157378f2022-04-04 15:47:50 +0200200 if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
David Brazdil0f672f62019-12-10 10:32:29 +0000201 xfs_trans_set_sync(args->trans);
202
Olivier Deprez157378f2022-04-04 15:47:50 +0200203 return error;
204}
205
206/*
207 * 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;
David Brazdil0f672f62019-12-10 10:32:29 +0000264}
265
266/*
267 * Set the attribute specified in @args.
268 */
269int
270xfs_attr_set_args(
271 struct xfs_da_args *args)
272{
273 struct xfs_inode *dp = args->dp;
274 struct xfs_buf *leaf_bp = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200275 int error = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000276
277 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200278 * 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.
David Brazdil0f672f62019-12-10 10:32:29 +0000282 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200283 if (xfs_attr_is_shortform(dp)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000284
285 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200286 * 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.
David Brazdil0f672f62019-12-10 10:32:29 +0000290 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200291 error = xfs_attr_set_shortform(args, &leaf_bp);
292 if (error || !args->trans)
293 return error;
294 }
David Brazdil0f672f62019-12-10 10:32:29 +0000295
Olivier Deprez157378f2022-04-04 15:47:50 +0200296 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
297 error = xfs_attr_leaf_addname(args);
David Brazdil0f672f62019-12-10 10:32:29 +0000298 if (error != -ENOSPC)
299 return error;
300
301 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200302 * Promote the attribute list to the Btree format.
David Brazdil0f672f62019-12-10 10:32:29 +0000303 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200304 error = xfs_attr3_leaf_to_node(args);
David Brazdil0f672f62019-12-10 10:32:29 +0000305 if (error)
306 return error;
307
308 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200309 * Finish any deferred work items and roll the transaction once
310 * more. The goal here is to call node_addname with the inode
311 * and transaction in the same state (inode locked and joined,
312 * transaction clean) no matter how we got to this step.
David Brazdil0f672f62019-12-10 10:32:29 +0000313 */
David Brazdil0f672f62019-12-10 10:32:29 +0000314 error = xfs_defer_finish(&args->trans);
Olivier Deprez157378f2022-04-04 15:47:50 +0200315 if (error)
David Brazdil0f672f62019-12-10 10:32:29 +0000316 return error;
Olivier Deprez157378f2022-04-04 15:47:50 +0200317
318 /*
319 * Commit the current trans (including the inode) and
320 * start a new one.
321 */
322 error = xfs_trans_roll_inode(&args->trans, dp);
323 if (error)
324 return error;
David Brazdil0f672f62019-12-10 10:32:29 +0000325 }
326
Olivier Deprez157378f2022-04-04 15:47:50 +0200327 error = xfs_attr_node_addname(args);
David Brazdil0f672f62019-12-10 10:32:29 +0000328 return error;
329}
330
331/*
Olivier Deprez157378f2022-04-04 15:47:50 +0200332 * Return EEXIST if attr is found, or ENOATTR if not
333 */
334int
335xfs_has_attr(
336 struct xfs_da_args *args)
337{
338 struct xfs_inode *dp = args->dp;
339 struct xfs_buf *bp = NULL;
340 int error;
341
342 if (!xfs_inode_hasattr(dp))
343 return -ENOATTR;
344
345 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
346 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
347 return xfs_attr_sf_findname(args, NULL, NULL);
348 }
349
350 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
351 error = xfs_attr_leaf_hasname(args, &bp);
352
353 if (bp)
354 xfs_trans_brelse(args->trans, bp);
355
356 return error;
357 }
358
359 return xfs_attr_node_hasname(args, NULL);
360}
361
362/*
David Brazdil0f672f62019-12-10 10:32:29 +0000363 * Remove the attribute specified in @args.
364 */
365int
366xfs_attr_remove_args(
367 struct xfs_da_args *args)
368{
369 struct xfs_inode *dp = args->dp;
370 int error;
371
372 if (!xfs_inode_hasattr(dp)) {
373 error = -ENOATTR;
Olivier Deprez157378f2022-04-04 15:47:50 +0200374 } else if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000375 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
376 error = xfs_attr_shortform_remove(args);
377 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
378 error = xfs_attr_leaf_removename(args);
379 } else {
380 error = xfs_attr_node_removename(args);
381 }
382
383 return error;
384}
385
Olivier Deprez157378f2022-04-04 15:47:50 +0200386/*
387 * Note: If args->value is NULL the attribute will be removed, just like the
388 * Linux ->setattr API.
389 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000390int
391xfs_attr_set(
Olivier Deprez157378f2022-04-04 15:47:50 +0200392 struct xfs_da_args *args)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393{
Olivier Deprez157378f2022-04-04 15:47:50 +0200394 struct xfs_inode *dp = args->dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000395 struct xfs_mount *mp = dp->i_mount;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000396 struct xfs_trans_res tres;
Olivier Deprez157378f2022-04-04 15:47:50 +0200397 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
David Brazdil0f672f62019-12-10 10:32:29 +0000398 int error, local;
Olivier Deprez157378f2022-04-04 15:47:50 +0200399 unsigned int total;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400
401 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
402 return -EIO;
403
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000404 error = xfs_qm_dqattach(dp);
405 if (error)
406 return error;
407
Olivier Deprez157378f2022-04-04 15:47:50 +0200408 args->geo = mp->m_attr_geo;
409 args->whichfork = XFS_ATTR_FORK;
410 args->hashval = xfs_da_hashname(args->name, args->namelen);
411
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000412 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200413 * We have no control over the attribute names that userspace passes us
414 * to remove, so we have to allow the name lookup prior to attribute
415 * removal to fail as well.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000416 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200417 args->op_flags = XFS_DA_OP_OKNOENT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000418
Olivier Deprez157378f2022-04-04 15:47:50 +0200419 if (args->value) {
420 XFS_STATS_INC(mp, xs_attr_set);
421
422 args->op_flags |= XFS_DA_OP_ADDNAME;
423 args->total = xfs_attr_calc_size(args, &local);
424
425 /*
426 * If the inode doesn't have an attribute fork, add one.
427 * (inode must not be locked when we call this routine)
428 */
429 if (XFS_IFORK_Q(dp) == 0) {
430 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
431 xfs_attr_sf_entsize_byname(args->namelen,
432 args->valuelen);
433
434 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
435 if (error)
436 return error;
437 }
438
439 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
440 M_RES(mp)->tr_attrsetrt.tr_logres *
441 args->total;
442 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
443 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
444 total = args->total;
445 } else {
446 XFS_STATS_INC(mp, xs_attr_remove);
447
448 tres = M_RES(mp)->tr_attrrm;
449 total = XFS_ATTRRM_SPACE_RES(mp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450 }
451
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452 /*
453 * Root fork attributes can use reserved data blocks for this
454 * operation if necessary
455 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200456 error = xfs_trans_alloc(mp, &tres, total, 0,
457 rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000458 if (error)
459 return error;
460
461 xfs_ilock(dp, XFS_ILOCK_EXCL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200462 xfs_trans_ijoin(args->trans, dp, 0);
463 if (args->value) {
464 unsigned int quota_flags = XFS_QMOPT_RES_REGBLKS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465
Olivier Deprez157378f2022-04-04 15:47:50 +0200466 if (rsvd)
467 quota_flags |= XFS_QMOPT_FORCE_RES;
468 error = xfs_trans_reserve_quota_nblks(args->trans, dp,
469 args->total, 0, quota_flags);
470 if (error)
471 goto out_trans_cancel;
472
473 error = xfs_has_attr(args);
474 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
475 goto out_trans_cancel;
476 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
477 goto out_trans_cancel;
478 if (error != -ENOATTR && error != -EEXIST)
479 goto out_trans_cancel;
480
481 error = xfs_attr_set_args(args);
482 if (error)
483 goto out_trans_cancel;
David Brazdil0f672f62019-12-10 10:32:29 +0000484 /* shortform attribute has already been committed */
Olivier Deprez157378f2022-04-04 15:47:50 +0200485 if (!args->trans)
486 goto out_unlock;
487 } else {
488 error = xfs_has_attr(args);
489 if (error != -EEXIST)
490 goto out_trans_cancel;
491
492 error = xfs_attr_remove_args(args);
493 if (error)
494 goto out_trans_cancel;
David Brazdil0f672f62019-12-10 10:32:29 +0000495 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000496
497 /*
498 * If this is a synchronous mount, make sure that the
499 * transaction goes to disk before returning to the user.
500 */
501 if (mp->m_flags & XFS_MOUNT_WSYNC)
Olivier Deprez157378f2022-04-04 15:47:50 +0200502 xfs_trans_set_sync(args->trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503
Olivier Deprez157378f2022-04-04 15:47:50 +0200504 if (!(args->op_flags & XFS_DA_OP_NOTIME))
505 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000506
507 /*
508 * Commit the last in the sequence of transactions.
509 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200510 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
511 error = xfs_trans_commit(args->trans);
David Brazdil0f672f62019-12-10 10:32:29 +0000512out_unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000513 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000514 return error;
515
David Brazdil0f672f62019-12-10 10:32:29 +0000516out_trans_cancel:
Olivier Deprez157378f2022-04-04 15:47:50 +0200517 if (args->trans)
518 xfs_trans_cancel(args->trans);
David Brazdil0f672f62019-12-10 10:32:29 +0000519 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520}
521
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522/*========================================================================
523 * External routines when attribute list is inside the inode
524 *========================================================================*/
525
Olivier Deprez157378f2022-04-04 15:47:50 +0200526static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
527{
528 struct xfs_attr_shortform *sf;
529
530 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
531 return be16_to_cpu(sf->hdr.totsize);
532}
533
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534/*
535 * Add a name to the shortform attribute list structure
536 * This is the external routine.
537 */
538STATIC int
539xfs_attr_shortform_addname(xfs_da_args_t *args)
540{
541 int newsize, forkoff, retval;
542
543 trace_xfs_attr_sf_addname(args);
544
545 retval = xfs_attr_shortform_lookup(args);
Olivier Deprez157378f2022-04-04 15:47:50 +0200546 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000547 return retval;
Olivier Deprez157378f2022-04-04 15:47:50 +0200548 if (retval == -EEXIST) {
549 if (args->attr_flags & XATTR_CREATE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000550 return retval;
551 retval = xfs_attr_shortform_remove(args);
552 if (retval)
553 return retval;
554 /*
555 * Since we have removed the old attr, clear ATTR_REPLACE so
556 * that the leaf format add routine won't trip over the attr
557 * not being around.
558 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200559 args->attr_flags &= ~XATTR_REPLACE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000560 }
561
562 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
563 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
564 return -ENOSPC;
565
Olivier Deprez157378f2022-04-04 15:47:50 +0200566 newsize = xfs_attr_sf_totsize(args->dp);
567 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568
569 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
570 if (!forkoff)
571 return -ENOSPC;
572
573 xfs_attr_shortform_add(args, forkoff);
574 return 0;
575}
576
577
578/*========================================================================
579 * External routines when attribute list is one block
580 *========================================================================*/
581
Olivier Deprez157378f2022-04-04 15:47:50 +0200582/* Store info about a remote block */
583STATIC void
584xfs_attr_save_rmt_blk(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000585 struct xfs_da_args *args)
586{
Olivier Deprez157378f2022-04-04 15:47:50 +0200587 args->blkno2 = args->blkno;
588 args->index2 = args->index;
589 args->rmtblkno2 = args->rmtblkno;
590 args->rmtblkcnt2 = args->rmtblkcnt;
591 args->rmtvaluelen2 = args->rmtvaluelen;
592}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000593
Olivier Deprez157378f2022-04-04 15:47:50 +0200594/* Set stored info about a remote block */
595STATIC void
596xfs_attr_restore_rmt_blk(
597 struct xfs_da_args *args)
598{
599 args->blkno = args->blkno2;
600 args->index = args->index2;
601 args->rmtblkno = args->rmtblkno2;
602 args->rmtblkcnt = args->rmtblkcnt2;
603 args->rmtvaluelen = args->rmtvaluelen2;
604}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000605
Olivier Deprez157378f2022-04-04 15:47:50 +0200606/*
607 * Tries to add an attribute to an inode in leaf form
608 *
609 * This function is meant to execute as part of a delayed operation and leaves
610 * the transaction handling to the caller. On success the attribute is added
611 * and the inode and transaction are left dirty. If there is not enough space,
612 * the attr data is converted to node format and -ENOSPC is returned. Caller is
613 * responsible for handling the dirty inode and transaction or adding the attr
614 * in node format.
615 */
616STATIC int
617xfs_attr_leaf_try_add(
618 struct xfs_da_args *args,
619 struct xfs_buf *bp)
620{
621 int retval;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622
623 /*
624 * Look up the given attribute in the leaf block. Figure out if
625 * the given flags produce an error or call for an atomic rename.
626 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200627 retval = xfs_attr_leaf_hasname(args, &bp);
628 if (retval != -ENOATTR && retval != -EEXIST)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000629 return retval;
Olivier Deprez157378f2022-04-04 15:47:50 +0200630 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
631 goto out_brelse;
632 if (retval == -EEXIST) {
633 if (args->attr_flags & XATTR_CREATE)
634 goto out_brelse;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635
636 trace_xfs_attr_leaf_replace(args);
637
638 /* save the attribute state for later removal*/
639 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
Olivier Deprez157378f2022-04-04 15:47:50 +0200640 xfs_attr_save_rmt_blk(args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000641
642 /*
643 * clear the remote attr state now that it is saved so that the
644 * values reflect the state of the attribute we are about to
645 * add, not the attribute we just found and will remove later.
646 */
647 args->rmtblkno = 0;
648 args->rmtblkcnt = 0;
649 args->rmtvaluelen = 0;
650 }
651
652 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200653 * Add the attribute to the leaf block
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000654 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200655 return xfs_attr3_leaf_add(bp, args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000656
Olivier Deprez157378f2022-04-04 15:47:50 +0200657out_brelse:
658 xfs_trans_brelse(args->trans, bp);
659 return retval;
660}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000661
Olivier Deprez157378f2022-04-04 15:47:50 +0200662
663/*
664 * Add a name to the leaf attribute list structure
665 *
666 * This leaf block cannot have a "remote" value, we only call this routine
667 * if bmap_one_block() says there is only one block (ie: no remote blks).
668 */
669STATIC int
670xfs_attr_leaf_addname(
671 struct xfs_da_args *args)
672{
673 int error, forkoff;
674 struct xfs_buf *bp = NULL;
675 struct xfs_inode *dp = args->dp;
676
677 trace_xfs_attr_leaf_addname(args);
678
679 error = xfs_attr_leaf_try_add(args, bp);
680 if (error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000682
683 /*
684 * Commit the transaction that added the attr name so that
685 * later routines can manage their own transactions.
686 */
687 error = xfs_trans_roll_inode(&args->trans, dp);
688 if (error)
689 return error;
690
691 /*
692 * If there was an out-of-line value, allocate the blocks we
693 * identified for its storage and copy the value. This is done
694 * after we create the attribute so that we don't overflow the
695 * maximum size of a transaction and/or hit a deadlock.
696 */
697 if (args->rmtblkno > 0) {
698 error = xfs_attr_rmtval_set(args);
699 if (error)
700 return error;
701 }
702
Olivier Deprez157378f2022-04-04 15:47:50 +0200703 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000704 /*
705 * Added a "remote" value, just clear the incomplete flag.
706 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200707 if (args->rmtblkno > 0)
708 error = xfs_attr3_leaf_clearflag(args);
709
710 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000711 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200712
713 /*
714 * If this is an atomic rename operation, we must "flip" the incomplete
715 * flags on the "new" and "old" attribute/value pairs so that one
716 * disappears and one appears atomically. Then we must remove the "old"
717 * attribute/value pair.
718 *
719 * In a separate transaction, set the incomplete flag on the "old" attr
720 * and clear the incomplete flag on the "new" attr.
721 */
722
723 error = xfs_attr3_leaf_flipflags(args);
724 if (error)
725 return error;
726 /*
727 * Commit the flag value change and start the next trans in series.
728 */
729 error = xfs_trans_roll_inode(&args->trans, args->dp);
730 if (error)
731 return error;
732
733 /*
734 * Dismantle the "old" attribute/value pair by removing a "remote" value
735 * (if it exists).
736 */
737 xfs_attr_restore_rmt_blk(args);
738
739 if (args->rmtblkno) {
740 error = xfs_attr_rmtval_invalidate(args);
741 if (error)
742 return error;
743
744 error = xfs_attr_rmtval_remove(args);
745 if (error)
746 return error;
747 }
748
749 /*
750 * Read in the block containing the "old" attr, then remove the "old"
751 * attr from that block (neat, huh!)
752 */
753 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
754 &bp);
755 if (error)
756 return error;
757
758 xfs_attr3_leaf_remove(bp, args);
759
760 /*
761 * If the result is small enough, shrink it all into the inode.
762 */
763 forkoff = xfs_attr_shortform_allfit(bp, dp);
764 if (forkoff)
765 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
766 /* bp is gone due to xfs_da_shrink_inode */
767
768 return error;
769}
770
771/*
772 * Return EEXIST if attr is found, or ENOATTR if not
773 */
774STATIC int
775xfs_attr_leaf_hasname(
776 struct xfs_da_args *args,
777 struct xfs_buf **bp)
778{
779 int error = 0;
780
781 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
782 if (error)
783 return error;
784
785 error = xfs_attr3_leaf_lookup_int(*bp, args);
786 if (error != -ENOATTR && error != -EEXIST)
787 xfs_trans_brelse(args->trans, *bp);
788
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789 return error;
790}
791
792/*
793 * Remove a name from the leaf attribute list structure
794 *
795 * This leaf block cannot have a "remote" value, we only call this routine
796 * if bmap_one_block() says there is only one block (ie: no remote blks).
797 */
798STATIC int
799xfs_attr_leaf_removename(
800 struct xfs_da_args *args)
801{
802 struct xfs_inode *dp;
803 struct xfs_buf *bp;
804 int error, forkoff;
805
806 trace_xfs_attr_leaf_removename(args);
807
808 /*
809 * Remove the attribute.
810 */
811 dp = args->dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000812
Olivier Deprez157378f2022-04-04 15:47:50 +0200813 error = xfs_attr_leaf_hasname(args, &bp);
814
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000815 if (error == -ENOATTR) {
816 xfs_trans_brelse(args->trans, bp);
817 return error;
Olivier Deprez157378f2022-04-04 15:47:50 +0200818 } else if (error != -EEXIST)
819 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820
821 xfs_attr3_leaf_remove(bp, args);
822
823 /*
824 * If the result is small enough, shrink it all into the inode.
825 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200826 forkoff = xfs_attr_shortform_allfit(bp, dp);
827 if (forkoff)
828 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000829 /* bp is gone due to xfs_da_shrink_inode */
Olivier Deprez157378f2022-04-04 15:47:50 +0200830
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000831 return 0;
832}
833
834/*
835 * Look up a name in a leaf attribute list structure.
836 *
837 * This leaf block cannot have a "remote" value, we only call this routine
838 * if bmap_one_block() says there is only one block (ie: no remote blks).
David Brazdil0f672f62019-12-10 10:32:29 +0000839 *
840 * Returns 0 on successful retrieval, otherwise an error.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000841 */
842STATIC int
843xfs_attr_leaf_get(xfs_da_args_t *args)
844{
845 struct xfs_buf *bp;
846 int error;
847
848 trace_xfs_attr_leaf_get(args);
849
Olivier Deprez157378f2022-04-04 15:47:50 +0200850 error = xfs_attr_leaf_hasname(args, &bp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000851
Olivier Deprez157378f2022-04-04 15:47:50 +0200852 if (error == -ENOATTR) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000853 xfs_trans_brelse(args->trans, bp);
854 return error;
Olivier Deprez157378f2022-04-04 15:47:50 +0200855 } else if (error != -EEXIST)
856 return error;
857
858
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000859 error = xfs_attr3_leaf_getvalue(bp, args);
860 xfs_trans_brelse(args->trans, bp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861 return error;
862}
863
Olivier Deprez157378f2022-04-04 15:47:50 +0200864/*
865 * Return EEXIST if attr is found, or ENOATTR if not
866 * statep: If not null is set to point at the found state. Caller will
867 * be responsible for freeing the state in this case.
868 */
869STATIC int
870xfs_attr_node_hasname(
871 struct xfs_da_args *args,
872 struct xfs_da_state **statep)
873{
874 struct xfs_da_state *state;
875 int retval, error;
876
877 state = xfs_da_state_alloc(args);
878 if (statep != NULL)
879 *statep = NULL;
880
881 /*
882 * Search to see if name exists, and get back a pointer to it.
883 */
884 error = xfs_da3_node_lookup_int(state, &retval);
885 if (error) {
886 xfs_da_state_free(state);
887 return error;
888 }
889
890 if (statep != NULL)
891 *statep = state;
892 else
893 xfs_da_state_free(state);
894 return retval;
895}
896
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000897/*========================================================================
898 * External routines when attribute list size > geo->blksize
899 *========================================================================*/
900
901/*
902 * Add a name to a Btree-format attribute list.
903 *
904 * This will involve walking down the Btree, and may involve splitting
905 * leaf nodes and even splitting intermediate nodes up to and including
906 * the root node (a special case of an intermediate node).
907 *
908 * "Remote" attribute values confuse the issue and atomic rename operations
909 * add a whole extra layer of confusion on top of that.
910 */
911STATIC int
912xfs_attr_node_addname(
913 struct xfs_da_args *args)
914{
915 struct xfs_da_state *state;
916 struct xfs_da_state_blk *blk;
917 struct xfs_inode *dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000918 int retval, error;
919
920 trace_xfs_attr_node_addname(args);
921
922 /*
923 * Fill in bucket of arguments/results/context to carry around.
924 */
925 dp = args->dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926restart:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000927 /*
928 * Search to see if name already exists, and get back a pointer
929 * to where it should go.
930 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200931 error = 0;
932 retval = xfs_attr_node_hasname(args, &state);
933 if (retval != -ENOATTR && retval != -EEXIST)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000934 goto out;
Olivier Deprez157378f2022-04-04 15:47:50 +0200935
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000936 blk = &state->path.blk[ state->path.active-1 ];
937 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
Olivier Deprez157378f2022-04-04 15:47:50 +0200938 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939 goto out;
Olivier Deprez157378f2022-04-04 15:47:50 +0200940 if (retval == -EEXIST) {
941 if (args->attr_flags & XATTR_CREATE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000942 goto out;
943
944 trace_xfs_attr_node_replace(args);
945
946 /* save the attribute state for later removal*/
947 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
Olivier Deprez157378f2022-04-04 15:47:50 +0200948 xfs_attr_save_rmt_blk(args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949
950 /*
951 * clear the remote attr state now that it is saved so that the
952 * values reflect the state of the attribute we are about to
953 * add, not the attribute we just found and will remove later.
954 */
955 args->rmtblkno = 0;
956 args->rmtblkcnt = 0;
957 args->rmtvaluelen = 0;
958 }
959
960 retval = xfs_attr3_leaf_add(blk->bp, state->args);
961 if (retval == -ENOSPC) {
962 if (state->path.active == 1) {
963 /*
964 * Its really a single leaf node, but it had
965 * out-of-line values so it looked like it *might*
966 * have been a b-tree.
967 */
968 xfs_da_state_free(state);
969 state = NULL;
970 error = xfs_attr3_leaf_to_node(args);
971 if (error)
972 goto out;
973 error = xfs_defer_finish(&args->trans);
974 if (error)
975 goto out;
976
977 /*
978 * Commit the node conversion and start the next
979 * trans in the chain.
980 */
981 error = xfs_trans_roll_inode(&args->trans, dp);
982 if (error)
983 goto out;
984
985 goto restart;
986 }
987
988 /*
989 * Split as many Btree elements as required.
990 * This code tracks the new and old attr's location
991 * in the index/blkno/rmtblkno/rmtblkcnt fields and
992 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
993 */
994 error = xfs_da3_split(state);
995 if (error)
996 goto out;
997 error = xfs_defer_finish(&args->trans);
998 if (error)
999 goto out;
1000 } else {
1001 /*
1002 * Addition succeeded, update Btree hashvals.
1003 */
1004 xfs_da3_fixhashpath(state, &state->path);
1005 }
1006
1007 /*
1008 * Kill the state structure, we're done with it and need to
1009 * allow the buffers to come back later.
1010 */
1011 xfs_da_state_free(state);
1012 state = NULL;
1013
1014 /*
1015 * Commit the leaf addition or btree split and start the next
1016 * trans in the chain.
1017 */
1018 error = xfs_trans_roll_inode(&args->trans, dp);
1019 if (error)
1020 goto out;
1021
1022 /*
1023 * If there was an out-of-line value, allocate the blocks we
1024 * identified for its storage and copy the value. This is done
1025 * after we create the attribute so that we don't overflow the
1026 * maximum size of a transaction and/or hit a deadlock.
1027 */
1028 if (args->rmtblkno > 0) {
1029 error = xfs_attr_rmtval_set(args);
1030 if (error)
1031 return error;
1032 }
1033
Olivier Deprez157378f2022-04-04 15:47:50 +02001034 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035 /*
1036 * Added a "remote" value, just clear the incomplete flag.
1037 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001038 if (args->rmtblkno > 0)
1039 error = xfs_attr3_leaf_clearflag(args);
1040 retval = error;
1041 goto out;
1042 }
1043
1044 /*
1045 * If this is an atomic rename operation, we must "flip" the incomplete
1046 * flags on the "new" and "old" attribute/value pairs so that one
1047 * disappears and one appears atomically. Then we must remove the "old"
1048 * attribute/value pair.
1049 *
1050 * In a separate transaction, set the incomplete flag on the "old" attr
1051 * and clear the incomplete flag on the "new" attr.
1052 */
1053 error = xfs_attr3_leaf_flipflags(args);
1054 if (error)
1055 goto out;
1056 /*
1057 * Commit the flag value change and start the next trans in series
1058 */
1059 error = xfs_trans_roll_inode(&args->trans, args->dp);
1060 if (error)
1061 goto out;
1062
1063 /*
1064 * Dismantle the "old" attribute/value pair by removing a "remote" value
1065 * (if it exists).
1066 */
1067 xfs_attr_restore_rmt_blk(args);
1068
1069 if (args->rmtblkno) {
1070 error = xfs_attr_rmtval_invalidate(args);
1071 if (error)
1072 return error;
1073
1074 error = xfs_attr_rmtval_remove(args);
1075 if (error)
1076 return error;
1077 }
1078
1079 /*
1080 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1081 * flag means that we will find the "old" attr, not the "new" one.
1082 */
1083 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1084 state = xfs_da_state_alloc(args);
1085 state->inleaf = 0;
1086 error = xfs_da3_node_lookup_int(state, &retval);
1087 if (error)
1088 goto out;
1089
1090 /*
1091 * Remove the name and update the hashvals in the tree.
1092 */
1093 blk = &state->path.blk[state->path.active-1];
1094 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1095 error = xfs_attr3_leaf_remove(blk->bp, args);
1096 xfs_da3_fixhashpath(state, &state->path);
1097
1098 /*
1099 * Check to see if the tree needs to be collapsed.
1100 */
1101 if (retval && (state->path.active > 1)) {
1102 error = xfs_da3_join(state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001103 if (error)
1104 goto out;
1105 }
1106 retval = error = 0;
1107
1108out:
1109 if (state)
1110 xfs_da_state_free(state);
1111 if (error)
1112 return error;
1113 return retval;
1114}
1115
1116/*
Olivier Deprez157378f2022-04-04 15:47:50 +02001117 * Shrink an attribute from leaf to shortform
1118 */
1119STATIC int
1120xfs_attr_node_shrink(
1121 struct xfs_da_args *args,
1122 struct xfs_da_state *state)
1123{
1124 struct xfs_inode *dp = args->dp;
1125 int error, forkoff;
1126 struct xfs_buf *bp;
1127
1128 /*
1129 * Have to get rid of the copy of this dabuf in the state.
1130 */
1131 ASSERT(state->path.active == 1);
1132 ASSERT(state->path.blk[0].bp);
1133 state->path.blk[0].bp = NULL;
1134
1135 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1136 if (error)
1137 return error;
1138
1139 forkoff = xfs_attr_shortform_allfit(bp, dp);
1140 if (forkoff) {
1141 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1142 /* bp is gone due to xfs_da_shrink_inode */
1143 } else
1144 xfs_trans_brelse(args->trans, bp);
1145
1146 return error;
1147}
1148
1149/*
1150 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1151 * for later deletion of the entry.
1152 */
1153STATIC int
1154xfs_attr_leaf_mark_incomplete(
1155 struct xfs_da_args *args,
1156 struct xfs_da_state *state)
1157{
1158 int error;
1159
1160 /*
1161 * Fill in disk block numbers in the state structure
1162 * so that we can get the buffers back after we commit
1163 * several transactions in the following calls.
1164 */
1165 error = xfs_attr_fillstate(state);
1166 if (error)
1167 return error;
1168
1169 /*
1170 * Mark the attribute as INCOMPLETE
1171 */
1172 return xfs_attr3_leaf_setflag(args);
1173}
1174
1175/*
1176 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1177 * the blocks are valid. Attr keys with remote blocks will be marked
1178 * incomplete.
1179 */
1180STATIC
1181int xfs_attr_node_removename_setup(
1182 struct xfs_da_args *args,
1183 struct xfs_da_state **state)
1184{
1185 int error;
1186
1187 error = xfs_attr_node_hasname(args, state);
1188 if (error != -EEXIST)
1189 return error;
1190
1191 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1192 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1193 XFS_ATTR_LEAF_MAGIC);
1194
1195 if (args->rmtblkno > 0) {
1196 error = xfs_attr_leaf_mark_incomplete(args, *state);
1197 if (error)
1198 return error;
1199
1200 return xfs_attr_rmtval_invalidate(args);
1201 }
1202
1203 return 0;
1204}
1205
1206STATIC int
1207xfs_attr_node_remove_rmt(
1208 struct xfs_da_args *args,
1209 struct xfs_da_state *state)
1210{
1211 int error = 0;
1212
1213 error = xfs_attr_rmtval_remove(args);
1214 if (error)
1215 return error;
1216
1217 /*
1218 * Refill the state structure with buffers, the prior calls released our
1219 * buffers.
1220 */
1221 return xfs_attr_refillstate(state);
1222}
1223
1224/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001225 * Remove a name from a B-tree attribute list.
1226 *
1227 * This will involve walking down the Btree, and may involve joining
1228 * leaf nodes and even joining intermediate nodes up to and including
1229 * the root node (a special case of an intermediate node).
1230 */
1231STATIC int
1232xfs_attr_node_removename(
1233 struct xfs_da_args *args)
1234{
1235 struct xfs_da_state *state;
1236 struct xfs_da_state_blk *blk;
Olivier Deprez157378f2022-04-04 15:47:50 +02001237 int retval, error;
1238 struct xfs_inode *dp = args->dp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001239
1240 trace_xfs_attr_node_removename(args);
1241
Olivier Deprez157378f2022-04-04 15:47:50 +02001242 error = xfs_attr_node_removename_setup(args, &state);
1243 if (error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001244 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001245
1246 /*
1247 * If there is an out-of-line value, de-allocate the blocks.
1248 * This is done before we remove the attribute so that we don't
1249 * overflow the maximum size of a transaction and/or hit a deadlock.
1250 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001251 if (args->rmtblkno > 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001252 error = xfs_attr_node_remove_rmt(args, state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001253 if (error)
1254 goto out;
1255 }
1256
1257 /*
1258 * Remove the name and update the hashvals in the tree.
1259 */
1260 blk = &state->path.blk[ state->path.active-1 ];
1261 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1262 retval = xfs_attr3_leaf_remove(blk->bp, args);
1263 xfs_da3_fixhashpath(state, &state->path);
1264
1265 /*
1266 * Check to see if the tree needs to be collapsed.
1267 */
1268 if (retval && (state->path.active > 1)) {
1269 error = xfs_da3_join(state);
1270 if (error)
1271 goto out;
1272 error = xfs_defer_finish(&args->trans);
1273 if (error)
1274 goto out;
1275 /*
1276 * Commit the Btree join operation and start a new trans.
1277 */
1278 error = xfs_trans_roll_inode(&args->trans, dp);
1279 if (error)
1280 goto out;
1281 }
1282
1283 /*
1284 * If the result is small enough, push it all into the inode.
1285 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001286 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
1287 error = xfs_attr_node_shrink(args, state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001288
1289out:
Olivier Deprez157378f2022-04-04 15:47:50 +02001290 if (state)
1291 xfs_da_state_free(state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001292 return error;
1293}
1294
1295/*
1296 * Fill in the disk block numbers in the state structure for the buffers
1297 * that are attached to the state structure.
1298 * This is done so that we can quickly reattach ourselves to those buffers
1299 * after some set of transaction commits have released these buffers.
1300 */
1301STATIC int
1302xfs_attr_fillstate(xfs_da_state_t *state)
1303{
1304 xfs_da_state_path_t *path;
1305 xfs_da_state_blk_t *blk;
1306 int level;
1307
1308 trace_xfs_attr_fillstate(state->args);
1309
1310 /*
1311 * Roll down the "path" in the state structure, storing the on-disk
1312 * block number for those buffers in the "path".
1313 */
1314 path = &state->path;
1315 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1316 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1317 if (blk->bp) {
1318 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1319 blk->bp = NULL;
1320 } else {
1321 blk->disk_blkno = 0;
1322 }
1323 }
1324
1325 /*
1326 * Roll down the "altpath" in the state structure, storing the on-disk
1327 * block number for those buffers in the "altpath".
1328 */
1329 path = &state->altpath;
1330 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1331 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1332 if (blk->bp) {
1333 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1334 blk->bp = NULL;
1335 } else {
1336 blk->disk_blkno = 0;
1337 }
1338 }
1339
1340 return 0;
1341}
1342
1343/*
1344 * Reattach the buffers to the state structure based on the disk block
1345 * numbers stored in the state structure.
1346 * This is done after some set of transaction commits have released those
1347 * buffers from our grip.
1348 */
1349STATIC int
1350xfs_attr_refillstate(xfs_da_state_t *state)
1351{
1352 xfs_da_state_path_t *path;
1353 xfs_da_state_blk_t *blk;
1354 int level, error;
1355
1356 trace_xfs_attr_refillstate(state->args);
1357
1358 /*
1359 * Roll down the "path" in the state structure, storing the on-disk
1360 * block number for those buffers in the "path".
1361 */
1362 path = &state->path;
1363 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1364 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1365 if (blk->disk_blkno) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001366 error = xfs_da3_node_read_mapped(state->args->trans,
1367 state->args->dp, blk->disk_blkno,
1368 &blk->bp, XFS_ATTR_FORK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001369 if (error)
1370 return error;
1371 } else {
1372 blk->bp = NULL;
1373 }
1374 }
1375
1376 /*
1377 * Roll down the "altpath" in the state structure, storing the on-disk
1378 * block number for those buffers in the "altpath".
1379 */
1380 path = &state->altpath;
1381 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1382 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1383 if (blk->disk_blkno) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001384 error = xfs_da3_node_read_mapped(state->args->trans,
1385 state->args->dp, blk->disk_blkno,
1386 &blk->bp, XFS_ATTR_FORK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001387 if (error)
1388 return error;
1389 } else {
1390 blk->bp = NULL;
1391 }
1392 }
1393
1394 return 0;
1395}
1396
1397/*
David Brazdil0f672f62019-12-10 10:32:29 +00001398 * Retrieve the attribute data from a node attribute list.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001399 *
1400 * This routine gets called for any attribute fork that has more than one
1401 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1402 * "remote" values taking up more blocks.
David Brazdil0f672f62019-12-10 10:32:29 +00001403 *
1404 * Returns 0 on successful retrieval, otherwise an error.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001405 */
1406STATIC int
Olivier Deprez157378f2022-04-04 15:47:50 +02001407xfs_attr_node_get(
1408 struct xfs_da_args *args)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001409{
Olivier Deprez157378f2022-04-04 15:47:50 +02001410 struct xfs_da_state *state;
1411 struct xfs_da_state_blk *blk;
1412 int i;
1413 int error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001414
1415 trace_xfs_attr_node_get(args);
1416
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001417 /*
1418 * Search to see if name exists, and get back a pointer to it.
1419 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001420 error = xfs_attr_node_hasname(args, &state);
1421 if (error != -EEXIST)
David Brazdil0f672f62019-12-10 10:32:29 +00001422 goto out_release;
1423
1424 /*
1425 * Get the value, local or "remote"
1426 */
1427 blk = &state->path.blk[state->path.active - 1];
Olivier Deprez157378f2022-04-04 15:47:50 +02001428 error = xfs_attr3_leaf_getvalue(blk->bp, args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001429
1430 /*
1431 * If not in a transaction, we have to release all the buffers.
1432 */
David Brazdil0f672f62019-12-10 10:32:29 +00001433out_release:
Olivier Deprez157378f2022-04-04 15:47:50 +02001434 for (i = 0; state != NULL && i < state->path.active; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001435 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1436 state->path.blk[i].bp = NULL;
1437 }
1438
Olivier Deprez157378f2022-04-04 15:47:50 +02001439 if (state)
1440 xfs_da_state_free(state);
1441 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001442}
David Brazdil0f672f62019-12-10 10:32:29 +00001443
1444/* Returns true if the attribute entry name is valid. */
1445bool
1446xfs_attr_namecheck(
1447 const void *name,
1448 size_t length)
1449{
1450 /*
1451 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1452 * out, so use >= for the length check.
1453 */
1454 if (length >= MAXNAMELEN)
1455 return false;
1456
1457 /* There shouldn't be any nulls here */
1458 return !memchr(name, 0, length);
1459}