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"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_da_format.h"
16 #include "xfs_da_btree.h"
17 #include "xfs_attr_sf.h"
18 #include "xfs_inode.h"
19 #include "xfs_alloc.h"
20 #include "xfs_trans.h"
21 #include "xfs_inode_item.h"
23 #include "xfs_bmap_util.h"
24 #include "xfs_bmap_btree.h"
26 #include "xfs_attr_leaf.h"
27 #include "xfs_attr_remote.h"
28 #include "xfs_error.h"
29 #include "xfs_quota.h"
30 #include "xfs_trans_space.h"
31 #include "xfs_trace.h"
36 * Provide the external interfaces to manage attribute lists.
39 /*========================================================================
40 * Function prototypes for the kernel.
41 *========================================================================*/
44 * Internal routines when attribute list fits inside the inode.
46 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
49 * Internal routines when attribute list is one block.
51 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
52 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
53 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
56 * Internal routines when attribute list is more than one block.
58 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
59 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
60 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
61 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
62 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
67 struct xfs_da_args *args,
69 const unsigned char *name,
76 memset(args, 0, sizeof(*args));
77 args->geo = dp->i_mount->m_attr_geo;
78 args->whichfork = XFS_ATTR_FORK;
82 args->namelen = strlen((const char *)name);
83 if (args->namelen >= MAXNAMELEN)
84 return -EFAULT; /* match IRIX behaviour */
86 args->hashval = xfs_da_hashname(args->name, args->namelen);
94 if (!XFS_IFORK_Q(ip) ||
95 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
96 ip->i_d.di_anextents == 0))
101 /*========================================================================
102 * Overall external interface routines.
103 *========================================================================*/
105 /* Retrieve an extended attribute and its value. Must have ilock. */
107 xfs_attr_get_ilocked(
108 struct xfs_inode *ip,
109 struct xfs_da_args *args)
111 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
113 if (!xfs_inode_hasattr(ip))
115 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
116 return xfs_attr_shortform_getvalue(args);
117 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
118 return xfs_attr_leaf_get(args);
120 return xfs_attr_node_get(args);
123 /* Retrieve an extended attribute by name, and its value. */
126 struct xfs_inode *ip,
127 const unsigned char *name,
128 unsigned char *value,
132 struct xfs_da_args args;
136 XFS_STATS_INC(ip->i_mount, xs_attr_get);
138 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
141 error = xfs_attr_args_init(&args, ip, name, flags);
146 args.valuelen = *valuelenp;
147 /* Entirely possible to look up a name which doesn't exist */
148 args.op_flags = XFS_DA_OP_OKNOENT;
150 lock_mode = xfs_ilock_attr_map_shared(ip);
151 error = xfs_attr_get_ilocked(ip, &args);
152 xfs_iunlock(ip, lock_mode);
154 *valuelenp = args.valuelen;
155 return error == -EEXIST ? 0 : error;
159 * Calculate how many blocks we need for the new attribute,
163 struct xfs_da_args *args,
166 struct xfs_mount *mp = args->dp->i_mount;
171 * Determine space new attribute will use, and if it would be
172 * "local" or "remote" (note: local != inline).
174 size = xfs_attr_leaf_newentsize(args, local);
175 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
177 if (size > (args->geo->blksize / 2)) {
178 /* Double split possible */
183 * Out of line attribute, cannot double split, but
184 * make room for the attribute value itself.
186 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
188 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
195 xfs_attr_try_sf_addname(
196 struct xfs_inode *dp,
197 struct xfs_da_args *args)
200 struct xfs_mount *mp = dp->i_mount;
203 error = xfs_attr_shortform_addname(args);
204 if (error == -ENOSPC)
208 * Commit the shortform mods, and we're done.
209 * NOTE: this is also the error path (EEXIST, etc).
211 if (!error && (args->flags & ATTR_KERNOTIME) == 0)
212 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
214 if (mp->m_flags & XFS_MOUNT_WSYNC)
215 xfs_trans_set_sync(args->trans);
217 error2 = xfs_trans_commit(args->trans);
219 return error ? error : error2;
223 * Set the attribute specified in @args.
227 struct xfs_da_args *args)
229 struct xfs_inode *dp = args->dp;
230 struct xfs_buf *leaf_bp = NULL;
234 * If the attribute list is non-existent or a shortform list,
235 * upgrade it to a single-leaf-block attribute list.
237 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
238 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
239 dp->i_d.di_anextents == 0)) {
242 * Build initial attribute list (if required).
244 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
245 xfs_attr_shortform_create(args);
248 * Try to add the attr to the attribute list in the inode.
250 error = xfs_attr_try_sf_addname(dp, args);
251 if (error != -ENOSPC)
255 * It won't fit in the shortform, transform to a leaf block.
256 * GROT: another possible req'mt for a double-split btree op.
258 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
263 * Prevent the leaf buffer from being unlocked so that a
264 * concurrent AIL push cannot grab the half-baked leaf
265 * buffer and run into problems with the write verifier.
266 * Once we're done rolling the transaction we can release
267 * the hold and add the attr to the leaf.
269 xfs_trans_bhold(args->trans, leaf_bp);
270 error = xfs_defer_finish(&args->trans);
271 xfs_trans_bhold_release(args->trans, leaf_bp);
273 xfs_trans_brelse(args->trans, leaf_bp);
278 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
279 error = xfs_attr_leaf_addname(args);
281 error = xfs_attr_node_addname(args);
286 * Remove the attribute specified in @args.
289 xfs_attr_remove_args(
290 struct xfs_da_args *args)
292 struct xfs_inode *dp = args->dp;
295 if (!xfs_inode_hasattr(dp)) {
297 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
298 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
299 error = xfs_attr_shortform_remove(args);
300 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
301 error = xfs_attr_leaf_removename(args);
303 error = xfs_attr_node_removename(args);
311 struct xfs_inode *dp,
312 const unsigned char *name,
313 unsigned char *value,
317 struct xfs_mount *mp = dp->i_mount;
318 struct xfs_da_args args;
319 struct xfs_trans_res tres;
320 int rsvd = (flags & ATTR_ROOT) != 0;
323 XFS_STATS_INC(mp, xs_attr_set);
325 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
328 error = xfs_attr_args_init(&args, dp, name, flags);
333 args.valuelen = valuelen;
334 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
335 args.total = xfs_attr_calc_size(&args, &local);
337 error = xfs_qm_dqattach(dp);
342 * If the inode doesn't have an attribute fork, add one.
343 * (inode must not be locked when we call this routine)
345 if (XFS_IFORK_Q(dp) == 0) {
346 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
347 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
349 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
354 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
355 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
356 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
357 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
360 * Root fork attributes can use reserved data blocks for this
361 * operation if necessary
363 error = xfs_trans_alloc(mp, &tres, args.total, 0,
364 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
368 xfs_ilock(dp, XFS_ILOCK_EXCL);
369 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
370 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
371 XFS_QMOPT_RES_REGBLKS);
373 goto out_trans_cancel;
375 xfs_trans_ijoin(args.trans, dp, 0);
376 error = xfs_attr_set_args(&args);
378 goto out_trans_cancel;
380 /* shortform attribute has already been committed */
385 * If this is a synchronous mount, make sure that the
386 * transaction goes to disk before returning to the user.
388 if (mp->m_flags & XFS_MOUNT_WSYNC)
389 xfs_trans_set_sync(args.trans);
391 if ((flags & ATTR_KERNOTIME) == 0)
392 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
395 * Commit the last in the sequence of transactions.
397 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
398 error = xfs_trans_commit(args.trans);
400 xfs_iunlock(dp, XFS_ILOCK_EXCL);
405 xfs_trans_cancel(args.trans);
410 * Generic handler routine to remove a name from an attribute list.
411 * Transitions attribute list from Btree to shortform as necessary.
415 struct xfs_inode *dp,
416 const unsigned char *name,
419 struct xfs_mount *mp = dp->i_mount;
420 struct xfs_da_args args;
423 XFS_STATS_INC(mp, xs_attr_remove);
425 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
428 error = xfs_attr_args_init(&args, dp, name, flags);
433 * we have no control over the attribute names that userspace passes us
434 * to remove, so we have to allow the name lookup prior to attribute
437 args.op_flags = XFS_DA_OP_OKNOENT;
439 error = xfs_qm_dqattach(dp);
444 * Root fork attributes can use reserved data blocks for this
445 * operation if necessary
447 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
448 XFS_ATTRRM_SPACE_RES(mp), 0,
449 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
454 xfs_ilock(dp, XFS_ILOCK_EXCL);
456 * No need to make quota reservations here. We expect to release some
457 * blocks not allocate in the common case.
459 xfs_trans_ijoin(args.trans, dp, 0);
461 error = xfs_attr_remove_args(&args);
466 * If this is a synchronous mount, make sure that the
467 * transaction goes to disk before returning to the user.
469 if (mp->m_flags & XFS_MOUNT_WSYNC)
470 xfs_trans_set_sync(args.trans);
472 if ((flags & ATTR_KERNOTIME) == 0)
473 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
476 * Commit the last in the sequence of transactions.
478 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
479 error = xfs_trans_commit(args.trans);
480 xfs_iunlock(dp, XFS_ILOCK_EXCL);
486 xfs_trans_cancel(args.trans);
487 xfs_iunlock(dp, XFS_ILOCK_EXCL);
491 /*========================================================================
492 * External routines when attribute list is inside the inode
493 *========================================================================*/
496 * Add a name to the shortform attribute list structure
497 * This is the external routine.
500 xfs_attr_shortform_addname(xfs_da_args_t *args)
502 int newsize, forkoff, retval;
504 trace_xfs_attr_sf_addname(args);
506 retval = xfs_attr_shortform_lookup(args);
507 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
509 } else if (retval == -EEXIST) {
510 if (args->flags & ATTR_CREATE)
512 retval = xfs_attr_shortform_remove(args);
516 * Since we have removed the old attr, clear ATTR_REPLACE so
517 * that the leaf format add routine won't trip over the attr
520 args->flags &= ~ATTR_REPLACE;
523 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
524 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
527 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
528 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
530 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
534 xfs_attr_shortform_add(args, forkoff);
539 /*========================================================================
540 * External routines when attribute list is one block
541 *========================================================================*/
544 * Add a name to the leaf attribute list structure
546 * This leaf block cannot have a "remote" value, we only call this routine
547 * if bmap_one_block() says there is only one block (ie: no remote blks).
550 xfs_attr_leaf_addname(
551 struct xfs_da_args *args)
553 struct xfs_inode *dp;
555 int retval, error, forkoff;
557 trace_xfs_attr_leaf_addname(args);
560 * Read the (only) block in the attribute list in.
564 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
569 * Look up the given attribute in the leaf block. Figure out if
570 * the given flags produce an error or call for an atomic rename.
572 retval = xfs_attr3_leaf_lookup_int(bp, args);
573 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
574 xfs_trans_brelse(args->trans, bp);
576 } else if (retval == -EEXIST) {
577 if (args->flags & ATTR_CREATE) { /* pure create op */
578 xfs_trans_brelse(args->trans, bp);
582 trace_xfs_attr_leaf_replace(args);
584 /* save the attribute state for later removal*/
585 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
586 args->blkno2 = args->blkno; /* set 2nd entry info*/
587 args->index2 = args->index;
588 args->rmtblkno2 = args->rmtblkno;
589 args->rmtblkcnt2 = args->rmtblkcnt;
590 args->rmtvaluelen2 = args->rmtvaluelen;
593 * clear the remote attr state now that it is saved so that the
594 * values reflect the state of the attribute we are about to
595 * add, not the attribute we just found and will remove later.
599 args->rmtvaluelen = 0;
603 * Add the attribute to the leaf block, transitioning to a Btree
606 retval = xfs_attr3_leaf_add(bp, args);
607 if (retval == -ENOSPC) {
609 * Promote the attribute list to the Btree format, then
610 * Commit that transaction so that the node_addname() call
611 * can manage its own transactions.
613 error = xfs_attr3_leaf_to_node(args);
616 error = xfs_defer_finish(&args->trans);
621 * Commit the current trans (including the inode) and start
624 error = xfs_trans_roll_inode(&args->trans, dp);
629 * Fob the whole rest of the problem off on the Btree code.
631 error = xfs_attr_node_addname(args);
636 * Commit the transaction that added the attr name so that
637 * later routines can manage their own transactions.
639 error = xfs_trans_roll_inode(&args->trans, dp);
644 * If there was an out-of-line value, allocate the blocks we
645 * identified for its storage and copy the value. This is done
646 * after we create the attribute so that we don't overflow the
647 * maximum size of a transaction and/or hit a deadlock.
649 if (args->rmtblkno > 0) {
650 error = xfs_attr_rmtval_set(args);
656 * If this is an atomic rename operation, we must "flip" the
657 * incomplete flags on the "new" and "old" attribute/value pairs
658 * so that one disappears and one appears atomically. Then we
659 * must remove the "old" attribute/value pair.
661 if (args->op_flags & XFS_DA_OP_RENAME) {
663 * In a separate transaction, set the incomplete flag on the
664 * "old" attr and clear the incomplete flag on the "new" attr.
666 error = xfs_attr3_leaf_flipflags(args);
671 * Dismantle the "old" attribute/value pair by removing
672 * a "remote" value (if it exists).
674 args->index = args->index2;
675 args->blkno = args->blkno2;
676 args->rmtblkno = args->rmtblkno2;
677 args->rmtblkcnt = args->rmtblkcnt2;
678 args->rmtvaluelen = args->rmtvaluelen2;
679 if (args->rmtblkno) {
680 error = xfs_attr_rmtval_remove(args);
686 * Read in the block containing the "old" attr, then
687 * remove the "old" attr from that block (neat, huh!)
689 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
694 xfs_attr3_leaf_remove(bp, args);
697 * If the result is small enough, shrink it all into the inode.
699 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
700 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
701 /* bp is gone due to xfs_da_shrink_inode */
704 error = xfs_defer_finish(&args->trans);
710 * Commit the remove and start the next trans in series.
712 error = xfs_trans_roll_inode(&args->trans, dp);
714 } else if (args->rmtblkno > 0) {
716 * Added a "remote" value, just clear the incomplete flag.
718 error = xfs_attr3_leaf_clearflag(args);
724 * Remove a name from the leaf attribute list structure
726 * This leaf block cannot have a "remote" value, we only call this routine
727 * if bmap_one_block() says there is only one block (ie: no remote blks).
730 xfs_attr_leaf_removename(
731 struct xfs_da_args *args)
733 struct xfs_inode *dp;
737 trace_xfs_attr_leaf_removename(args);
740 * Remove the attribute.
744 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
748 error = xfs_attr3_leaf_lookup_int(bp, args);
749 if (error == -ENOATTR) {
750 xfs_trans_brelse(args->trans, bp);
754 xfs_attr3_leaf_remove(bp, args);
757 * If the result is small enough, shrink it all into the inode.
759 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
760 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
761 /* bp is gone due to xfs_da_shrink_inode */
764 error = xfs_defer_finish(&args->trans);
772 * Look up a name in a leaf attribute list structure.
774 * This leaf block cannot have a "remote" value, we only call this routine
775 * if bmap_one_block() says there is only one block (ie: no remote blks).
778 xfs_attr_leaf_get(xfs_da_args_t *args)
783 trace_xfs_attr_leaf_get(args);
786 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
790 error = xfs_attr3_leaf_lookup_int(bp, args);
791 if (error != -EEXIST) {
792 xfs_trans_brelse(args->trans, bp);
795 error = xfs_attr3_leaf_getvalue(bp, args);
796 xfs_trans_brelse(args->trans, bp);
797 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
798 error = xfs_attr_rmtval_get(args);
803 /*========================================================================
804 * External routines when attribute list size > geo->blksize
805 *========================================================================*/
808 * Add a name to a Btree-format attribute list.
810 * This will involve walking down the Btree, and may involve splitting
811 * leaf nodes and even splitting intermediate nodes up to and including
812 * the root node (a special case of an intermediate node).
814 * "Remote" attribute values confuse the issue and atomic rename operations
815 * add a whole extra layer of confusion on top of that.
818 xfs_attr_node_addname(
819 struct xfs_da_args *args)
821 struct xfs_da_state *state;
822 struct xfs_da_state_blk *blk;
823 struct xfs_inode *dp;
824 struct xfs_mount *mp;
827 trace_xfs_attr_node_addname(args);
830 * Fill in bucket of arguments/results/context to carry around.
835 state = xfs_da_state_alloc();
840 * Search to see if name already exists, and get back a pointer
841 * to where it should go.
843 error = xfs_da3_node_lookup_int(state, &retval);
846 blk = &state->path.blk[ state->path.active-1 ];
847 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
848 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
850 } else if (retval == -EEXIST) {
851 if (args->flags & ATTR_CREATE)
854 trace_xfs_attr_node_replace(args);
856 /* save the attribute state for later removal*/
857 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
858 args->blkno2 = args->blkno; /* set 2nd entry info*/
859 args->index2 = args->index;
860 args->rmtblkno2 = args->rmtblkno;
861 args->rmtblkcnt2 = args->rmtblkcnt;
862 args->rmtvaluelen2 = args->rmtvaluelen;
865 * clear the remote attr state now that it is saved so that the
866 * values reflect the state of the attribute we are about to
867 * add, not the attribute we just found and will remove later.
871 args->rmtvaluelen = 0;
874 retval = xfs_attr3_leaf_add(blk->bp, state->args);
875 if (retval == -ENOSPC) {
876 if (state->path.active == 1) {
878 * Its really a single leaf node, but it had
879 * out-of-line values so it looked like it *might*
880 * have been a b-tree.
882 xfs_da_state_free(state);
884 error = xfs_attr3_leaf_to_node(args);
887 error = xfs_defer_finish(&args->trans);
892 * Commit the node conversion and start the next
893 * trans in the chain.
895 error = xfs_trans_roll_inode(&args->trans, dp);
903 * Split as many Btree elements as required.
904 * This code tracks the new and old attr's location
905 * in the index/blkno/rmtblkno/rmtblkcnt fields and
906 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
908 error = xfs_da3_split(state);
911 error = xfs_defer_finish(&args->trans);
916 * Addition succeeded, update Btree hashvals.
918 xfs_da3_fixhashpath(state, &state->path);
922 * Kill the state structure, we're done with it and need to
923 * allow the buffers to come back later.
925 xfs_da_state_free(state);
929 * Commit the leaf addition or btree split and start the next
930 * trans in the chain.
932 error = xfs_trans_roll_inode(&args->trans, dp);
937 * If there was an out-of-line value, allocate the blocks we
938 * identified for its storage and copy the value. This is done
939 * after we create the attribute so that we don't overflow the
940 * maximum size of a transaction and/or hit a deadlock.
942 if (args->rmtblkno > 0) {
943 error = xfs_attr_rmtval_set(args);
949 * If this is an atomic rename operation, we must "flip" the
950 * incomplete flags on the "new" and "old" attribute/value pairs
951 * so that one disappears and one appears atomically. Then we
952 * must remove the "old" attribute/value pair.
954 if (args->op_flags & XFS_DA_OP_RENAME) {
956 * In a separate transaction, set the incomplete flag on the
957 * "old" attr and clear the incomplete flag on the "new" attr.
959 error = xfs_attr3_leaf_flipflags(args);
964 * Dismantle the "old" attribute/value pair by removing
965 * a "remote" value (if it exists).
967 args->index = args->index2;
968 args->blkno = args->blkno2;
969 args->rmtblkno = args->rmtblkno2;
970 args->rmtblkcnt = args->rmtblkcnt2;
971 args->rmtvaluelen = args->rmtvaluelen2;
972 if (args->rmtblkno) {
973 error = xfs_attr_rmtval_remove(args);
979 * Re-find the "old" attribute entry after any split ops.
980 * The INCOMPLETE flag means that we will find the "old"
981 * attr, not the "new" one.
983 args->flags |= XFS_ATTR_INCOMPLETE;
984 state = xfs_da_state_alloc();
988 error = xfs_da3_node_lookup_int(state, &retval);
993 * Remove the name and update the hashvals in the tree.
995 blk = &state->path.blk[ state->path.active-1 ];
996 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
997 error = xfs_attr3_leaf_remove(blk->bp, args);
998 xfs_da3_fixhashpath(state, &state->path);
1001 * Check to see if the tree needs to be collapsed.
1003 if (retval && (state->path.active > 1)) {
1004 error = xfs_da3_join(state);
1007 error = xfs_defer_finish(&args->trans);
1013 * Commit and start the next trans in the chain.
1015 error = xfs_trans_roll_inode(&args->trans, dp);
1019 } else if (args->rmtblkno > 0) {
1021 * Added a "remote" value, just clear the incomplete flag.
1023 error = xfs_attr3_leaf_clearflag(args);
1031 xfs_da_state_free(state);
1038 * Remove a name from a B-tree attribute list.
1040 * This will involve walking down the Btree, and may involve joining
1041 * leaf nodes and even joining intermediate nodes up to and including
1042 * the root node (a special case of an intermediate node).
1045 xfs_attr_node_removename(
1046 struct xfs_da_args *args)
1048 struct xfs_da_state *state;
1049 struct xfs_da_state_blk *blk;
1050 struct xfs_inode *dp;
1052 int retval, error, forkoff;
1054 trace_xfs_attr_node_removename(args);
1057 * Tie a string around our finger to remind us where we are.
1060 state = xfs_da_state_alloc();
1062 state->mp = dp->i_mount;
1065 * Search to see if name exists, and get back a pointer to it.
1067 error = xfs_da3_node_lookup_int(state, &retval);
1068 if (error || (retval != -EEXIST)) {
1075 * If there is an out-of-line value, de-allocate the blocks.
1076 * This is done before we remove the attribute so that we don't
1077 * overflow the maximum size of a transaction and/or hit a deadlock.
1079 blk = &state->path.blk[ state->path.active-1 ];
1080 ASSERT(blk->bp != NULL);
1081 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1082 if (args->rmtblkno > 0) {
1084 * Fill in disk block numbers in the state structure
1085 * so that we can get the buffers back after we commit
1086 * several transactions in the following calls.
1088 error = xfs_attr_fillstate(state);
1093 * Mark the attribute as INCOMPLETE, then bunmapi() the
1096 error = xfs_attr3_leaf_setflag(args);
1099 error = xfs_attr_rmtval_remove(args);
1104 * Refill the state structure with buffers, the prior calls
1105 * released our buffers.
1107 error = xfs_attr_refillstate(state);
1113 * Remove the name and update the hashvals in the tree.
1115 blk = &state->path.blk[ state->path.active-1 ];
1116 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1117 retval = xfs_attr3_leaf_remove(blk->bp, args);
1118 xfs_da3_fixhashpath(state, &state->path);
1121 * Check to see if the tree needs to be collapsed.
1123 if (retval && (state->path.active > 1)) {
1124 error = xfs_da3_join(state);
1127 error = xfs_defer_finish(&args->trans);
1131 * Commit the Btree join operation and start a new trans.
1133 error = xfs_trans_roll_inode(&args->trans, dp);
1139 * If the result is small enough, push it all into the inode.
1141 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1143 * Have to get rid of the copy of this dabuf in the state.
1145 ASSERT(state->path.active == 1);
1146 ASSERT(state->path.blk[0].bp);
1147 state->path.blk[0].bp = NULL;
1149 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1153 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1154 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1155 /* bp is gone due to xfs_da_shrink_inode */
1158 error = xfs_defer_finish(&args->trans);
1162 xfs_trans_brelse(args->trans, bp);
1167 xfs_da_state_free(state);
1172 * Fill in the disk block numbers in the state structure for the buffers
1173 * that are attached to the state structure.
1174 * This is done so that we can quickly reattach ourselves to those buffers
1175 * after some set of transaction commits have released these buffers.
1178 xfs_attr_fillstate(xfs_da_state_t *state)
1180 xfs_da_state_path_t *path;
1181 xfs_da_state_blk_t *blk;
1184 trace_xfs_attr_fillstate(state->args);
1187 * Roll down the "path" in the state structure, storing the on-disk
1188 * block number for those buffers in the "path".
1190 path = &state->path;
1191 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1192 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1194 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1197 blk->disk_blkno = 0;
1202 * Roll down the "altpath" in the state structure, storing the on-disk
1203 * block number for those buffers in the "altpath".
1205 path = &state->altpath;
1206 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1207 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1209 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1212 blk->disk_blkno = 0;
1220 * Reattach the buffers to the state structure based on the disk block
1221 * numbers stored in the state structure.
1222 * This is done after some set of transaction commits have released those
1223 * buffers from our grip.
1226 xfs_attr_refillstate(xfs_da_state_t *state)
1228 xfs_da_state_path_t *path;
1229 xfs_da_state_blk_t *blk;
1232 trace_xfs_attr_refillstate(state->args);
1235 * Roll down the "path" in the state structure, storing the on-disk
1236 * block number for those buffers in the "path".
1238 path = &state->path;
1239 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1240 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1241 if (blk->disk_blkno) {
1242 error = xfs_da3_node_read(state->args->trans,
1244 blk->blkno, blk->disk_blkno,
1245 &blk->bp, XFS_ATTR_FORK);
1254 * Roll down the "altpath" in the state structure, storing the on-disk
1255 * block number for those buffers in the "altpath".
1257 path = &state->altpath;
1258 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1259 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1260 if (blk->disk_blkno) {
1261 error = xfs_da3_node_read(state->args->trans,
1263 blk->blkno, blk->disk_blkno,
1264 &blk->bp, XFS_ATTR_FORK);
1276 * Look up a filename in a node attribute list.
1278 * This routine gets called for any attribute fork that has more than one
1279 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1280 * "remote" values taking up more blocks.
1283 xfs_attr_node_get(xfs_da_args_t *args)
1285 xfs_da_state_t *state;
1286 xfs_da_state_blk_t *blk;
1290 trace_xfs_attr_node_get(args);
1292 state = xfs_da_state_alloc();
1294 state->mp = args->dp->i_mount;
1297 * Search to see if name exists, and get back a pointer to it.
1299 error = xfs_da3_node_lookup_int(state, &retval);
1302 } else if (retval == -EEXIST) {
1303 blk = &state->path.blk[ state->path.active-1 ];
1304 ASSERT(blk->bp != NULL);
1305 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1308 * Get the value, local or "remote"
1310 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1311 if (!retval && (args->rmtblkno > 0)
1312 && !(args->flags & ATTR_KERNOVAL)) {
1313 retval = xfs_attr_rmtval_get(args);
1318 * If not in a transaction, we have to release all the buffers.
1320 for (i = 0; i < state->path.active; i++) {
1321 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1322 state->path.blk[i].bp = NULL;
1325 xfs_da_state_free(state);