1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #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"
18 #include "xfs_trans.h"
20 #include "xfs_bmap_btree.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_quota.h"
25 #include "xfs_trans_space.h"
26 #include "xfs_trace.h"
31 * Provide the external interfaces to manage attribute lists.
34 /*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
39 * Internal routines when attribute list fits inside the inode.
41 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
44 * Internal routines when attribute list is one block.
46 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
47 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
48 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
51 * Internal routines when attribute list is more than one block.
53 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
54 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
55 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
56 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
57 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
63 if (!XFS_IFORK_Q(ip) ||
64 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
65 ip->i_afp->if_nextents == 0))
70 /*========================================================================
71 * Overall external interface routines.
72 *========================================================================*/
75 * Retrieve an extended attribute and its value. Must have ilock.
76 * Returns 0 on successful retrieval, otherwise an error.
80 struct xfs_da_args *args)
82 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
84 if (!xfs_inode_hasattr(args->dp))
87 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
88 return xfs_attr_shortform_getvalue(args);
89 if (xfs_bmap_one_block(args->dp, XFS_ATTR_FORK))
90 return xfs_attr_leaf_get(args);
91 return xfs_attr_node_get(args);
95 * Retrieve an extended attribute by name, and its value if requested.
97 * 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.
101 * 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.
106 * If the attribute is found, but exceeds the size limit set by the caller in
107 * args->valuelen, return -ERANGE with the size of the attribute that was found
112 struct xfs_da_args *args)
117 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
119 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
122 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);
126 /* Entirely possible to look up a name which doesn't exist */
127 args->op_flags = XFS_DA_OP_OKNOENT;
129 lock_mode = xfs_ilock_attr_map_shared(args->dp);
130 error = xfs_attr_get_ilocked(args);
131 xfs_iunlock(args->dp, lock_mode);
137 * Calculate how many blocks we need for the new attribute,
141 struct xfs_da_args *args,
144 struct xfs_mount *mp = args->dp->i_mount;
149 * Determine space new attribute will use, and if it would be
150 * "local" or "remote" (note: local != inline).
152 size = xfs_attr_leaf_newentsize(args, local);
153 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
155 if (size > (args->geo->blksize / 2)) {
156 /* Double split possible */
161 * Out of line attribute, cannot double split, but
162 * make room for the attribute value itself.
164 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
166 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
173 xfs_attr_try_sf_addname(
174 struct xfs_inode *dp,
175 struct xfs_da_args *args)
178 struct xfs_mount *mp = dp->i_mount;
181 error = xfs_attr_shortform_addname(args);
182 if (error == -ENOSPC)
186 * Commit the shortform mods, and we're done.
187 * NOTE: this is also the error path (EEXIST, etc).
189 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
190 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
192 if (mp->m_flags & XFS_MOUNT_WSYNC)
193 xfs_trans_set_sync(args->trans);
195 error2 = xfs_trans_commit(args->trans);
197 return error ? error : error2;
201 * Set the attribute specified in @args.
205 struct xfs_da_args *args)
207 struct xfs_inode *dp = args->dp;
208 struct xfs_buf *leaf_bp = NULL;
212 * If the attribute list is non-existent or a shortform list,
213 * upgrade it to a single-leaf-block attribute list.
215 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
216 (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
217 dp->i_afp->if_nextents == 0)) {
220 * Build initial attribute list (if required).
222 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
223 xfs_attr_shortform_create(args);
226 * Try to add the attr to the attribute list in the inode.
228 error = xfs_attr_try_sf_addname(dp, args);
229 if (error != -ENOSPC)
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.
236 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
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.
244 * Once we're done rolling the transaction we can release
245 * the hold and add the attr to the leaf.
247 xfs_trans_bhold(args->trans, leaf_bp);
248 error = xfs_defer_finish(&args->trans);
249 xfs_trans_bhold_release(args->trans, leaf_bp);
251 xfs_trans_brelse(args->trans, leaf_bp);
256 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
257 error = xfs_attr_leaf_addname(args);
259 error = xfs_attr_node_addname(args);
264 * Remove the attribute specified in @args.
267 xfs_attr_remove_args(
268 struct xfs_da_args *args)
270 struct xfs_inode *dp = args->dp;
273 if (!xfs_inode_hasattr(dp)) {
275 } else if (dp->i_afp->if_format == 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);
281 error = xfs_attr_node_removename(args);
288 * Note: If args->value is NULL the attribute will be removed, just like the
289 * Linux ->setattr API.
293 struct xfs_da_args *args)
295 struct xfs_inode *dp = args->dp;
296 struct xfs_mount *mp = dp->i_mount;
297 struct xfs_trans_res tres;
298 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
302 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
305 error = xfs_qm_dqattach(dp);
309 args->geo = mp->m_attr_geo;
310 args->whichfork = XFS_ATTR_FORK;
311 args->hashval = xfs_da_hashname(args->name, args->namelen);
314 * 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.
318 args->op_flags = XFS_DA_OP_OKNOENT;
321 XFS_STATS_INC(mp, xs_attr_set);
323 args->op_flags |= XFS_DA_OP_ADDNAME;
324 args->total = xfs_attr_calc_size(args, &local);
327 * If the inode doesn't have an attribute fork, add one.
328 * (inode must not be locked when we call this routine)
330 if (XFS_IFORK_Q(dp) == 0) {
331 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
332 XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen,
335 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
340 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
341 M_RES(mp)->tr_attrsetrt.tr_logres *
343 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
344 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
347 XFS_STATS_INC(mp, xs_attr_remove);
349 tres = M_RES(mp)->tr_attrrm;
350 total = XFS_ATTRRM_SPACE_RES(mp);
354 * Root fork attributes can use reserved data blocks for this
355 * operation if necessary
357 error = xfs_trans_alloc(mp, &tres, total, 0,
358 rsvd ? XFS_TRANS_RESERVE : 0, &args->trans);
362 xfs_ilock(dp, XFS_ILOCK_EXCL);
363 xfs_trans_ijoin(args->trans, dp, 0);
365 unsigned int quota_flags = XFS_QMOPT_RES_REGBLKS;
368 quota_flags |= XFS_QMOPT_FORCE_RES;
369 error = xfs_trans_reserve_quota_nblks(args->trans, dp,
370 args->total, 0, quota_flags);
372 goto out_trans_cancel;
373 error = xfs_attr_set_args(args);
375 goto out_trans_cancel;
376 /* shortform attribute has already been committed */
380 error = xfs_attr_remove_args(args);
382 goto out_trans_cancel;
386 * If this is a synchronous mount, make sure that the
387 * transaction goes to disk before returning to the user.
389 if (mp->m_flags & XFS_MOUNT_WSYNC)
390 xfs_trans_set_sync(args->trans);
392 if (!(args->op_flags & XFS_DA_OP_NOTIME))
393 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
396 * Commit the last in the sequence of transactions.
398 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
399 error = xfs_trans_commit(args->trans);
401 xfs_iunlock(dp, XFS_ILOCK_EXCL);
406 xfs_trans_cancel(args->trans);
410 /*========================================================================
411 * External routines when attribute list is inside the inode
412 *========================================================================*/
415 * Add a name to the shortform attribute list structure
416 * This is the external routine.
419 xfs_attr_shortform_addname(xfs_da_args_t *args)
421 int newsize, forkoff, retval;
423 trace_xfs_attr_sf_addname(args);
425 retval = xfs_attr_shortform_lookup(args);
426 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
428 if (retval == -EEXIST) {
429 if (args->attr_flags & XATTR_CREATE)
431 retval = xfs_attr_shortform_remove(args);
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
439 args->attr_flags &= ~XATTR_REPLACE;
442 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
443 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
446 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
447 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
449 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
453 xfs_attr_shortform_add(args, forkoff);
458 /*========================================================================
459 * External routines when attribute list is one block
460 *========================================================================*/
463 * Add a name to the leaf attribute list structure
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).
469 xfs_attr_leaf_addname(
470 struct xfs_da_args *args)
472 struct xfs_inode *dp;
474 int retval, error, forkoff;
476 trace_xfs_attr_leaf_addname(args);
479 * Read the (only) block in the attribute list in.
483 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
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.
491 retval = xfs_attr3_leaf_lookup_int(bp, args);
492 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
494 if (retval == -EEXIST) {
495 if (args->attr_flags & XATTR_CREATE)
498 trace_xfs_attr_leaf_replace(args);
500 /* save the attribute state for later removal*/
501 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
502 args->blkno2 = args->blkno; /* set 2nd entry info*/
503 args->index2 = args->index;
504 args->rmtblkno2 = args->rmtblkno;
505 args->rmtblkcnt2 = args->rmtblkcnt;
506 args->rmtvaluelen2 = args->rmtvaluelen;
509 * clear the remote attr state now that it is saved so that the
510 * values reflect the state of the attribute we are about to
511 * add, not the attribute we just found and will remove later.
515 args->rmtvaluelen = 0;
519 * Add the attribute to the leaf block, transitioning to a Btree
522 retval = xfs_attr3_leaf_add(bp, args);
523 if (retval == -ENOSPC) {
525 * Promote the attribute list to the Btree format, then
526 * Commit that transaction so that the node_addname() call
527 * can manage its own transactions.
529 error = xfs_attr3_leaf_to_node(args);
532 error = xfs_defer_finish(&args->trans);
537 * Commit the current trans (including the inode) and start
540 error = xfs_trans_roll_inode(&args->trans, dp);
545 * Fob the whole rest of the problem off on the Btree code.
547 error = xfs_attr_node_addname(args);
552 * Commit the transaction that added the attr name so that
553 * later routines can manage their own transactions.
555 error = xfs_trans_roll_inode(&args->trans, dp);
560 * If there was an out-of-line value, allocate the blocks we
561 * identified for its storage and copy the value. This is done
562 * after we create the attribute so that we don't overflow the
563 * maximum size of a transaction and/or hit a deadlock.
565 if (args->rmtblkno > 0) {
566 error = xfs_attr_rmtval_set(args);
572 * If this is an atomic rename operation, we must "flip" the
573 * incomplete flags on the "new" and "old" attribute/value pairs
574 * so that one disappears and one appears atomically. Then we
575 * must remove the "old" attribute/value pair.
577 if (args->op_flags & XFS_DA_OP_RENAME) {
579 * In a separate transaction, set the incomplete flag on the
580 * "old" attr and clear the incomplete flag on the "new" attr.
582 error = xfs_attr3_leaf_flipflags(args);
587 * Dismantle the "old" attribute/value pair by removing
588 * a "remote" value (if it exists).
590 args->index = args->index2;
591 args->blkno = args->blkno2;
592 args->rmtblkno = args->rmtblkno2;
593 args->rmtblkcnt = args->rmtblkcnt2;
594 args->rmtvaluelen = args->rmtvaluelen2;
595 if (args->rmtblkno) {
596 error = xfs_attr_rmtval_remove(args);
602 * Read in the block containing the "old" attr, then
603 * remove the "old" attr from that block (neat, huh!)
605 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
610 xfs_attr3_leaf_remove(bp, args);
613 * If the result is small enough, shrink it all into the inode.
615 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
616 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
617 /* bp is gone due to xfs_da_shrink_inode */
620 error = xfs_defer_finish(&args->trans);
626 * Commit the remove and start the next trans in series.
628 error = xfs_trans_roll_inode(&args->trans, dp);
630 } else if (args->rmtblkno > 0) {
632 * Added a "remote" value, just clear the incomplete flag.
634 error = xfs_attr3_leaf_clearflag(args);
638 xfs_trans_brelse(args->trans, bp);
643 * Remove a name from the leaf attribute list structure
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).
649 xfs_attr_leaf_removename(
650 struct xfs_da_args *args)
652 struct xfs_inode *dp;
656 trace_xfs_attr_leaf_removename(args);
659 * Remove the attribute.
663 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
667 error = xfs_attr3_leaf_lookup_int(bp, args);
668 if (error == -ENOATTR) {
669 xfs_trans_brelse(args->trans, bp);
673 xfs_attr3_leaf_remove(bp, args);
676 * If the result is small enough, shrink it all into the inode.
678 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
679 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
680 /* bp is gone due to xfs_da_shrink_inode */
683 error = xfs_defer_finish(&args->trans);
691 * Look up a name in a leaf attribute list structure.
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).
696 * Returns 0 on successful retrieval, otherwise an error.
699 xfs_attr_leaf_get(xfs_da_args_t *args)
704 trace_xfs_attr_leaf_get(args);
707 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
711 error = xfs_attr3_leaf_lookup_int(bp, args);
712 if (error != -EEXIST) {
713 xfs_trans_brelse(args->trans, bp);
716 error = xfs_attr3_leaf_getvalue(bp, args);
717 xfs_trans_brelse(args->trans, bp);
721 /*========================================================================
722 * External routines when attribute list size > geo->blksize
723 *========================================================================*/
726 * Add a name to a Btree-format attribute list.
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).
732 * "Remote" attribute values confuse the issue and atomic rename operations
733 * add a whole extra layer of confusion on top of that.
736 xfs_attr_node_addname(
737 struct xfs_da_args *args)
739 struct xfs_da_state *state;
740 struct xfs_da_state_blk *blk;
741 struct xfs_inode *dp;
742 struct xfs_mount *mp;
745 trace_xfs_attr_node_addname(args);
748 * Fill in bucket of arguments/results/context to carry around.
753 state = xfs_da_state_alloc();
758 * Search to see if name already exists, and get back a pointer
759 * to where it should go.
761 error = xfs_da3_node_lookup_int(state, &retval);
764 blk = &state->path.blk[ state->path.active-1 ];
765 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
766 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
768 if (retval == -EEXIST) {
769 if (args->attr_flags & XATTR_CREATE)
772 trace_xfs_attr_node_replace(args);
774 /* save the attribute state for later removal*/
775 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
776 args->blkno2 = args->blkno; /* set 2nd entry info*/
777 args->index2 = args->index;
778 args->rmtblkno2 = args->rmtblkno;
779 args->rmtblkcnt2 = args->rmtblkcnt;
780 args->rmtvaluelen2 = args->rmtvaluelen;
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.
789 args->rmtvaluelen = 0;
792 retval = xfs_attr3_leaf_add(blk->bp, state->args);
793 if (retval == -ENOSPC) {
794 if (state->path.active == 1) {
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.
800 xfs_da_state_free(state);
802 error = xfs_attr3_leaf_to_node(args);
805 error = xfs_defer_finish(&args->trans);
810 * Commit the node conversion and start the next
811 * trans in the chain.
813 error = xfs_trans_roll_inode(&args->trans, dp);
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.
826 error = xfs_da3_split(state);
829 error = xfs_defer_finish(&args->trans);
834 * Addition succeeded, update Btree hashvals.
836 xfs_da3_fixhashpath(state, &state->path);
840 * Kill the state structure, we're done with it and need to
841 * allow the buffers to come back later.
843 xfs_da_state_free(state);
847 * Commit the leaf addition or btree split and start the next
848 * trans in the chain.
850 error = xfs_trans_roll_inode(&args->trans, dp);
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.
860 if (args->rmtblkno > 0) {
861 error = xfs_attr_rmtval_set(args);
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.
872 if (args->op_flags & XFS_DA_OP_RENAME) {
874 * In a separate transaction, set the incomplete flag on the
875 * "old" attr and clear the incomplete flag on the "new" attr.
877 error = xfs_attr3_leaf_flipflags(args);
882 * Dismantle the "old" attribute/value pair by removing
883 * a "remote" value (if it exists).
885 args->index = args->index2;
886 args->blkno = args->blkno2;
887 args->rmtblkno = args->rmtblkno2;
888 args->rmtblkcnt = args->rmtblkcnt2;
889 args->rmtvaluelen = args->rmtvaluelen2;
890 if (args->rmtblkno) {
891 error = xfs_attr_rmtval_remove(args);
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.
901 args->attr_filter |= XFS_ATTR_INCOMPLETE;
902 state = xfs_da_state_alloc();
906 error = xfs_da3_node_lookup_int(state, &retval);
911 * Remove the name and update the hashvals in the tree.
913 blk = &state->path.blk[ state->path.active-1 ];
914 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
915 error = xfs_attr3_leaf_remove(blk->bp, args);
916 xfs_da3_fixhashpath(state, &state->path);
919 * Check to see if the tree needs to be collapsed.
921 if (retval && (state->path.active > 1)) {
922 error = xfs_da3_join(state);
925 error = xfs_defer_finish(&args->trans);
931 * Commit and start the next trans in the chain.
933 error = xfs_trans_roll_inode(&args->trans, dp);
937 } else if (args->rmtblkno > 0) {
939 * Added a "remote" value, just clear the incomplete flag.
941 error = xfs_attr3_leaf_clearflag(args);
949 xfs_da_state_free(state);
956 * Remove a name from a B-tree attribute list.
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).
963 xfs_attr_node_removename(
964 struct xfs_da_args *args)
966 struct xfs_da_state *state;
967 struct xfs_da_state_blk *blk;
968 struct xfs_inode *dp;
970 int retval, error, forkoff;
972 trace_xfs_attr_node_removename(args);
975 * Tie a string around our finger to remind us where we are.
978 state = xfs_da_state_alloc();
980 state->mp = dp->i_mount;
983 * Search to see if name exists, and get back a pointer to it.
985 error = xfs_da3_node_lookup_int(state, &retval);
986 if (error || (retval != -EEXIST)) {
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.
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) {
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.
1006 error = xfs_attr_fillstate(state);
1011 * Mark the attribute as INCOMPLETE, then bunmapi() the
1014 error = xfs_attr3_leaf_setflag(args);
1017 error = xfs_attr_rmtval_remove(args);
1022 * Refill the state structure with buffers, the prior calls
1023 * released our buffers.
1025 error = xfs_attr_refillstate(state);
1031 * Remove the name and update the hashvals in the tree.
1033 blk = &state->path.blk[ state->path.active-1 ];
1034 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1035 retval = xfs_attr3_leaf_remove(blk->bp, args);
1036 xfs_da3_fixhashpath(state, &state->path);
1039 * Check to see if the tree needs to be collapsed.
1041 if (retval && (state->path.active > 1)) {
1042 error = xfs_da3_join(state);
1045 error = xfs_defer_finish(&args->trans);
1049 * Commit the Btree join operation and start a new trans.
1051 error = xfs_trans_roll_inode(&args->trans, dp);
1057 * If the result is small enough, push it all into the inode.
1059 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1061 * Have to get rid of the copy of this dabuf in the state.
1063 ASSERT(state->path.active == 1);
1064 ASSERT(state->path.blk[0].bp);
1065 state->path.blk[0].bp = NULL;
1067 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1071 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1072 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1073 /* bp is gone due to xfs_da_shrink_inode */
1076 error = xfs_defer_finish(&args->trans);
1080 xfs_trans_brelse(args->trans, bp);
1085 xfs_da_state_free(state);
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
1093 * after some set of transaction commits have released these buffers.
1096 xfs_attr_fillstate(xfs_da_state_t *state)
1098 xfs_da_state_path_t *path;
1099 xfs_da_state_blk_t *blk;
1102 trace_xfs_attr_fillstate(state->args);
1105 * Roll down the "path" in the state structure, storing the on-disk
1106 * block number for those buffers in the "path".
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++) {
1112 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1115 blk->disk_blkno = 0;
1120 * Roll down the "altpath" in the state structure, storing the on-disk
1121 * block number for those buffers in the "altpath".
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++) {
1127 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1130 blk->disk_blkno = 0;
1138 * Reattach the buffers to the state structure based on the disk block
1139 * numbers stored in the state structure.
1140 * This is done after some set of transaction commits have released those
1141 * buffers from our grip.
1144 xfs_attr_refillstate(xfs_da_state_t *state)
1146 xfs_da_state_path_t *path;
1147 xfs_da_state_blk_t *blk;
1150 trace_xfs_attr_refillstate(state->args);
1153 * Roll down the "path" in the state structure, storing the on-disk
1154 * block number for those buffers in the "path".
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) {
1160 error = xfs_da3_node_read_mapped(state->args->trans,
1161 state->args->dp, blk->disk_blkno,
1162 &blk->bp, XFS_ATTR_FORK);
1171 * Roll down the "altpath" in the state structure, storing the on-disk
1172 * block number for those buffers in the "altpath".
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) {
1178 error = xfs_da3_node_read_mapped(state->args->trans,
1179 state->args->dp, blk->disk_blkno,
1180 &blk->bp, XFS_ATTR_FORK);
1192 * Retrieve the attribute data from a node attribute list.
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.
1198 * Returns 0 on successful retrieval, otherwise an error.
1201 xfs_attr_node_get(xfs_da_args_t *args)
1203 xfs_da_state_t *state;
1204 xfs_da_state_blk_t *blk;
1208 trace_xfs_attr_node_get(args);
1210 state = xfs_da_state_alloc();
1212 state->mp = args->dp->i_mount;
1215 * Search to see if name exists, and get back a pointer to it.
1217 error = xfs_da3_node_lookup_int(state, &retval);
1222 if (retval != -EEXIST)
1226 * Get the value, local or "remote"
1228 blk = &state->path.blk[state->path.active - 1];
1229 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1232 * If not in a transaction, we have to release all the buffers.
1235 for (i = 0; i < state->path.active; i++) {
1236 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1237 state->path.blk[i].bp = NULL;
1240 xfs_da_state_free(state);
1244 /* Returns true if the attribute entry name is valid. */
1251 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1252 * out, so use >= for the length check.
1254 if (length >= MAXNAMELEN)
1257 /* There shouldn't be any nulls here */
1258 return !memchr(name, 0, length);