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);
62 struct xfs_da_args *args,
64 const unsigned char *name,
72 memset(args, 0, sizeof(*args));
73 args->geo = dp->i_mount->m_attr_geo;
74 args->whichfork = XFS_ATTR_FORK;
78 args->namelen = namelen;
79 if (args->namelen >= MAXNAMELEN)
80 return -EFAULT; /* match IRIX behaviour */
82 args->hashval = xfs_da_hashname(args->name, args->namelen);
90 if (!XFS_IFORK_Q(ip) ||
91 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
92 ip->i_d.di_anextents == 0))
97 /*========================================================================
98 * Overall external interface routines.
99 *========================================================================*/
102 * Retrieve an extended attribute and its value. Must have ilock.
103 * Returns 0 on successful retrieval, otherwise an error.
106 xfs_attr_get_ilocked(
107 struct xfs_inode *ip,
108 struct xfs_da_args *args)
110 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
112 if (!xfs_inode_hasattr(ip))
114 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
115 return xfs_attr_shortform_getvalue(args);
116 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
117 return xfs_attr_leaf_get(args);
119 return xfs_attr_node_get(args);
123 * Retrieve an extended attribute by name, and its value if requested.
125 * If ATTR_KERNOVAL is set in @flags, then the caller does not want the value,
126 * just an indication whether the attribute exists and the size of the value if
127 * it exists. The size is returned in @valuelenp,
129 * If the attribute is found, but exceeds the size limit set by the caller in
130 * @valuelenp, return -ERANGE with the size of the attribute that was found in
133 * If ATTR_ALLOC is set in @flags, allocate the buffer for the value after
134 * existence of the attribute has been determined. On success, return that
135 * buffer to the caller and leave them to free it. On failure, free any
136 * allocated buffer and ensure the buffer pointer returned to the caller is
141 struct xfs_inode *ip,
142 const unsigned char *name,
144 unsigned char **value,
148 struct xfs_da_args args;
152 ASSERT((flags & (ATTR_ALLOC | ATTR_KERNOVAL)) || *value);
154 XFS_STATS_INC(ip->i_mount, xs_attr_get);
156 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
159 error = xfs_attr_args_init(&args, ip, name, namelen, flags);
163 /* Entirely possible to look up a name which doesn't exist */
164 args.op_flags = XFS_DA_OP_OKNOENT;
165 if (flags & ATTR_ALLOC)
166 args.op_flags |= XFS_DA_OP_ALLOCVAL;
169 args.valuelen = *valuelenp;
171 lock_mode = xfs_ilock_attr_map_shared(ip);
172 error = xfs_attr_get_ilocked(ip, &args);
173 xfs_iunlock(ip, lock_mode);
174 *valuelenp = args.valuelen;
176 /* on error, we have to clean up allocated value buffers */
178 if (flags & ATTR_ALLOC) {
179 kmem_free(args.value);
189 * Calculate how many blocks we need for the new attribute,
193 struct xfs_da_args *args,
196 struct xfs_mount *mp = args->dp->i_mount;
201 * Determine space new attribute will use, and if it would be
202 * "local" or "remote" (note: local != inline).
204 size = xfs_attr_leaf_newentsize(args, local);
205 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
207 if (size > (args->geo->blksize / 2)) {
208 /* Double split possible */
213 * Out of line attribute, cannot double split, but
214 * make room for the attribute value itself.
216 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
218 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
225 xfs_attr_try_sf_addname(
226 struct xfs_inode *dp,
227 struct xfs_da_args *args)
230 struct xfs_mount *mp = dp->i_mount;
233 error = xfs_attr_shortform_addname(args);
234 if (error == -ENOSPC)
238 * Commit the shortform mods, and we're done.
239 * NOTE: this is also the error path (EEXIST, etc).
241 if (!error && (args->flags & ATTR_KERNOTIME) == 0)
242 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
244 if (mp->m_flags & XFS_MOUNT_WSYNC)
245 xfs_trans_set_sync(args->trans);
247 error2 = xfs_trans_commit(args->trans);
249 return error ? error : error2;
253 * Set the attribute specified in @args.
257 struct xfs_da_args *args)
259 struct xfs_inode *dp = args->dp;
260 struct xfs_buf *leaf_bp = NULL;
264 * If the attribute list is non-existent or a shortform list,
265 * upgrade it to a single-leaf-block attribute list.
267 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
268 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
269 dp->i_d.di_anextents == 0)) {
272 * Build initial attribute list (if required).
274 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
275 xfs_attr_shortform_create(args);
278 * Try to add the attr to the attribute list in the inode.
280 error = xfs_attr_try_sf_addname(dp, args);
281 if (error != -ENOSPC)
285 * It won't fit in the shortform, transform to a leaf block.
286 * GROT: another possible req'mt for a double-split btree op.
288 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
293 * Prevent the leaf buffer from being unlocked so that a
294 * concurrent AIL push cannot grab the half-baked leaf
295 * buffer and run into problems with the write verifier.
296 * Once we're done rolling the transaction we can release
297 * the hold and add the attr to the leaf.
299 xfs_trans_bhold(args->trans, leaf_bp);
300 error = xfs_defer_finish(&args->trans);
301 xfs_trans_bhold_release(args->trans, leaf_bp);
303 xfs_trans_brelse(args->trans, leaf_bp);
308 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
309 error = xfs_attr_leaf_addname(args);
311 error = xfs_attr_node_addname(args);
316 * Remove the attribute specified in @args.
319 xfs_attr_remove_args(
320 struct xfs_da_args *args)
322 struct xfs_inode *dp = args->dp;
325 if (!xfs_inode_hasattr(dp)) {
327 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
328 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
329 error = xfs_attr_shortform_remove(args);
330 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
331 error = xfs_attr_leaf_removename(args);
333 error = xfs_attr_node_removename(args);
341 struct xfs_inode *dp,
342 const unsigned char *name,
344 unsigned char *value,
348 struct xfs_mount *mp = dp->i_mount;
349 struct xfs_da_args args;
350 struct xfs_trans_res tres;
351 int rsvd = (flags & ATTR_ROOT) != 0;
354 XFS_STATS_INC(mp, xs_attr_set);
356 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
359 error = xfs_attr_args_init(&args, dp, name, namelen, flags);
364 args.valuelen = valuelen;
365 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
366 args.total = xfs_attr_calc_size(&args, &local);
368 error = xfs_qm_dqattach(dp);
373 * If the inode doesn't have an attribute fork, add one.
374 * (inode must not be locked when we call this routine)
376 if (XFS_IFORK_Q(dp) == 0) {
377 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
378 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
380 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
385 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
386 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
387 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
388 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
391 * Root fork attributes can use reserved data blocks for this
392 * operation if necessary
394 error = xfs_trans_alloc(mp, &tres, args.total, 0,
395 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
399 xfs_ilock(dp, XFS_ILOCK_EXCL);
400 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
401 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
402 XFS_QMOPT_RES_REGBLKS);
404 goto out_trans_cancel;
406 xfs_trans_ijoin(args.trans, dp, 0);
407 error = xfs_attr_set_args(&args);
409 goto out_trans_cancel;
411 /* shortform attribute has already been committed */
416 * If this is a synchronous mount, make sure that the
417 * transaction goes to disk before returning to the user.
419 if (mp->m_flags & XFS_MOUNT_WSYNC)
420 xfs_trans_set_sync(args.trans);
422 if ((flags & ATTR_KERNOTIME) == 0)
423 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
426 * Commit the last in the sequence of transactions.
428 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
429 error = xfs_trans_commit(args.trans);
431 xfs_iunlock(dp, XFS_ILOCK_EXCL);
436 xfs_trans_cancel(args.trans);
441 * Generic handler routine to remove a name from an attribute list.
442 * Transitions attribute list from Btree to shortform as necessary.
446 struct xfs_inode *dp,
447 const unsigned char *name,
451 struct xfs_mount *mp = dp->i_mount;
452 struct xfs_da_args args;
455 XFS_STATS_INC(mp, xs_attr_remove);
457 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
460 error = xfs_attr_args_init(&args, dp, name, namelen, flags);
465 * we have no control over the attribute names that userspace passes us
466 * to remove, so we have to allow the name lookup prior to attribute
469 args.op_flags = XFS_DA_OP_OKNOENT;
471 error = xfs_qm_dqattach(dp);
476 * Root fork attributes can use reserved data blocks for this
477 * operation if necessary
479 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
480 XFS_ATTRRM_SPACE_RES(mp), 0,
481 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
486 xfs_ilock(dp, XFS_ILOCK_EXCL);
488 * No need to make quota reservations here. We expect to release some
489 * blocks not allocate in the common case.
491 xfs_trans_ijoin(args.trans, dp, 0);
493 error = xfs_attr_remove_args(&args);
498 * If this is a synchronous mount, make sure that the
499 * transaction goes to disk before returning to the user.
501 if (mp->m_flags & XFS_MOUNT_WSYNC)
502 xfs_trans_set_sync(args.trans);
504 if ((flags & ATTR_KERNOTIME) == 0)
505 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
508 * Commit the last in the sequence of transactions.
510 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
511 error = xfs_trans_commit(args.trans);
512 xfs_iunlock(dp, XFS_ILOCK_EXCL);
518 xfs_trans_cancel(args.trans);
519 xfs_iunlock(dp, XFS_ILOCK_EXCL);
523 /*========================================================================
524 * External routines when attribute list is inside the inode
525 *========================================================================*/
528 * Add a name to the shortform attribute list structure
529 * This is the external routine.
532 xfs_attr_shortform_addname(xfs_da_args_t *args)
534 int newsize, forkoff, retval;
536 trace_xfs_attr_sf_addname(args);
538 retval = xfs_attr_shortform_lookup(args);
539 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
541 } else if (retval == -EEXIST) {
542 if (args->flags & ATTR_CREATE)
544 retval = xfs_attr_shortform_remove(args);
548 * Since we have removed the old attr, clear ATTR_REPLACE so
549 * that the leaf format add routine won't trip over the attr
552 args->flags &= ~ATTR_REPLACE;
555 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
556 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
559 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
560 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
562 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
566 xfs_attr_shortform_add(args, forkoff);
571 /*========================================================================
572 * External routines when attribute list is one block
573 *========================================================================*/
576 * Add a name to the leaf attribute list structure
578 * This leaf block cannot have a "remote" value, we only call this routine
579 * if bmap_one_block() says there is only one block (ie: no remote blks).
582 xfs_attr_leaf_addname(
583 struct xfs_da_args *args)
585 struct xfs_inode *dp;
587 int retval, error, forkoff;
589 trace_xfs_attr_leaf_addname(args);
592 * Read the (only) block in the attribute list in.
596 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
601 * Look up the given attribute in the leaf block. Figure out if
602 * the given flags produce an error or call for an atomic rename.
604 retval = xfs_attr3_leaf_lookup_int(bp, args);
605 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
606 xfs_trans_brelse(args->trans, bp);
608 } else if (retval == -EEXIST) {
609 if (args->flags & ATTR_CREATE) { /* pure create op */
610 xfs_trans_brelse(args->trans, bp);
614 trace_xfs_attr_leaf_replace(args);
616 /* save the attribute state for later removal*/
617 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
618 args->blkno2 = args->blkno; /* set 2nd entry info*/
619 args->index2 = args->index;
620 args->rmtblkno2 = args->rmtblkno;
621 args->rmtblkcnt2 = args->rmtblkcnt;
622 args->rmtvaluelen2 = args->rmtvaluelen;
625 * clear the remote attr state now that it is saved so that the
626 * values reflect the state of the attribute we are about to
627 * add, not the attribute we just found and will remove later.
631 args->rmtvaluelen = 0;
635 * Add the attribute to the leaf block, transitioning to a Btree
638 retval = xfs_attr3_leaf_add(bp, args);
639 if (retval == -ENOSPC) {
641 * Promote the attribute list to the Btree format, then
642 * Commit that transaction so that the node_addname() call
643 * can manage its own transactions.
645 error = xfs_attr3_leaf_to_node(args);
648 error = xfs_defer_finish(&args->trans);
653 * Commit the current trans (including the inode) and start
656 error = xfs_trans_roll_inode(&args->trans, dp);
661 * Fob the whole rest of the problem off on the Btree code.
663 error = xfs_attr_node_addname(args);
668 * Commit the transaction that added the attr name so that
669 * later routines can manage their own transactions.
671 error = xfs_trans_roll_inode(&args->trans, dp);
676 * If there was an out-of-line value, allocate the blocks we
677 * identified for its storage and copy the value. This is done
678 * after we create the attribute so that we don't overflow the
679 * maximum size of a transaction and/or hit a deadlock.
681 if (args->rmtblkno > 0) {
682 error = xfs_attr_rmtval_set(args);
688 * If this is an atomic rename operation, we must "flip" the
689 * incomplete flags on the "new" and "old" attribute/value pairs
690 * so that one disappears and one appears atomically. Then we
691 * must remove the "old" attribute/value pair.
693 if (args->op_flags & XFS_DA_OP_RENAME) {
695 * In a separate transaction, set the incomplete flag on the
696 * "old" attr and clear the incomplete flag on the "new" attr.
698 error = xfs_attr3_leaf_flipflags(args);
703 * Dismantle the "old" attribute/value pair by removing
704 * a "remote" value (if it exists).
706 args->index = args->index2;
707 args->blkno = args->blkno2;
708 args->rmtblkno = args->rmtblkno2;
709 args->rmtblkcnt = args->rmtblkcnt2;
710 args->rmtvaluelen = args->rmtvaluelen2;
711 if (args->rmtblkno) {
712 error = xfs_attr_rmtval_remove(args);
718 * Read in the block containing the "old" attr, then
719 * remove the "old" attr from that block (neat, huh!)
721 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
726 xfs_attr3_leaf_remove(bp, args);
729 * If the result is small enough, shrink it all into the inode.
731 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
732 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
733 /* bp is gone due to xfs_da_shrink_inode */
736 error = xfs_defer_finish(&args->trans);
742 * Commit the remove and start the next trans in series.
744 error = xfs_trans_roll_inode(&args->trans, dp);
746 } else if (args->rmtblkno > 0) {
748 * Added a "remote" value, just clear the incomplete flag.
750 error = xfs_attr3_leaf_clearflag(args);
756 * Remove a name from the leaf attribute list structure
758 * This leaf block cannot have a "remote" value, we only call this routine
759 * if bmap_one_block() says there is only one block (ie: no remote blks).
762 xfs_attr_leaf_removename(
763 struct xfs_da_args *args)
765 struct xfs_inode *dp;
769 trace_xfs_attr_leaf_removename(args);
772 * Remove the attribute.
776 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
780 error = xfs_attr3_leaf_lookup_int(bp, args);
781 if (error == -ENOATTR) {
782 xfs_trans_brelse(args->trans, bp);
786 xfs_attr3_leaf_remove(bp, args);
789 * If the result is small enough, shrink it all into the inode.
791 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
792 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
793 /* bp is gone due to xfs_da_shrink_inode */
796 error = xfs_defer_finish(&args->trans);
804 * Look up a name in a leaf attribute list structure.
806 * This leaf block cannot have a "remote" value, we only call this routine
807 * if bmap_one_block() says there is only one block (ie: no remote blks).
809 * Returns 0 on successful retrieval, otherwise an error.
812 xfs_attr_leaf_get(xfs_da_args_t *args)
817 trace_xfs_attr_leaf_get(args);
820 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, &bp);
824 error = xfs_attr3_leaf_lookup_int(bp, args);
825 if (error != -EEXIST) {
826 xfs_trans_brelse(args->trans, bp);
829 error = xfs_attr3_leaf_getvalue(bp, args);
830 xfs_trans_brelse(args->trans, bp);
834 /*========================================================================
835 * External routines when attribute list size > geo->blksize
836 *========================================================================*/
839 * Add a name to a Btree-format attribute list.
841 * This will involve walking down the Btree, and may involve splitting
842 * leaf nodes and even splitting intermediate nodes up to and including
843 * the root node (a special case of an intermediate node).
845 * "Remote" attribute values confuse the issue and atomic rename operations
846 * add a whole extra layer of confusion on top of that.
849 xfs_attr_node_addname(
850 struct xfs_da_args *args)
852 struct xfs_da_state *state;
853 struct xfs_da_state_blk *blk;
854 struct xfs_inode *dp;
855 struct xfs_mount *mp;
858 trace_xfs_attr_node_addname(args);
861 * Fill in bucket of arguments/results/context to carry around.
866 state = xfs_da_state_alloc();
871 * Search to see if name already exists, and get back a pointer
872 * to where it should go.
874 error = xfs_da3_node_lookup_int(state, &retval);
877 blk = &state->path.blk[ state->path.active-1 ];
878 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
879 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
881 } else if (retval == -EEXIST) {
882 if (args->flags & ATTR_CREATE)
885 trace_xfs_attr_node_replace(args);
887 /* save the attribute state for later removal*/
888 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
889 args->blkno2 = args->blkno; /* set 2nd entry info*/
890 args->index2 = args->index;
891 args->rmtblkno2 = args->rmtblkno;
892 args->rmtblkcnt2 = args->rmtblkcnt;
893 args->rmtvaluelen2 = args->rmtvaluelen;
896 * clear the remote attr state now that it is saved so that the
897 * values reflect the state of the attribute we are about to
898 * add, not the attribute we just found and will remove later.
902 args->rmtvaluelen = 0;
905 retval = xfs_attr3_leaf_add(blk->bp, state->args);
906 if (retval == -ENOSPC) {
907 if (state->path.active == 1) {
909 * Its really a single leaf node, but it had
910 * out-of-line values so it looked like it *might*
911 * have been a b-tree.
913 xfs_da_state_free(state);
915 error = xfs_attr3_leaf_to_node(args);
918 error = xfs_defer_finish(&args->trans);
923 * Commit the node conversion and start the next
924 * trans in the chain.
926 error = xfs_trans_roll_inode(&args->trans, dp);
934 * Split as many Btree elements as required.
935 * This code tracks the new and old attr's location
936 * in the index/blkno/rmtblkno/rmtblkcnt fields and
937 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
939 error = xfs_da3_split(state);
942 error = xfs_defer_finish(&args->trans);
947 * Addition succeeded, update Btree hashvals.
949 xfs_da3_fixhashpath(state, &state->path);
953 * Kill the state structure, we're done with it and need to
954 * allow the buffers to come back later.
956 xfs_da_state_free(state);
960 * Commit the leaf addition or btree split and start the next
961 * trans in the chain.
963 error = xfs_trans_roll_inode(&args->trans, dp);
968 * If there was an out-of-line value, allocate the blocks we
969 * identified for its storage and copy the value. This is done
970 * after we create the attribute so that we don't overflow the
971 * maximum size of a transaction and/or hit a deadlock.
973 if (args->rmtblkno > 0) {
974 error = xfs_attr_rmtval_set(args);
980 * If this is an atomic rename operation, we must "flip" the
981 * incomplete flags on the "new" and "old" attribute/value pairs
982 * so that one disappears and one appears atomically. Then we
983 * must remove the "old" attribute/value pair.
985 if (args->op_flags & XFS_DA_OP_RENAME) {
987 * In a separate transaction, set the incomplete flag on the
988 * "old" attr and clear the incomplete flag on the "new" attr.
990 error = xfs_attr3_leaf_flipflags(args);
995 * Dismantle the "old" attribute/value pair by removing
996 * a "remote" value (if it exists).
998 args->index = args->index2;
999 args->blkno = args->blkno2;
1000 args->rmtblkno = args->rmtblkno2;
1001 args->rmtblkcnt = args->rmtblkcnt2;
1002 args->rmtvaluelen = args->rmtvaluelen2;
1003 if (args->rmtblkno) {
1004 error = xfs_attr_rmtval_remove(args);
1010 * Re-find the "old" attribute entry after any split ops.
1011 * The INCOMPLETE flag means that we will find the "old"
1012 * attr, not the "new" one.
1014 args->op_flags |= XFS_DA_OP_INCOMPLETE;
1015 state = xfs_da_state_alloc();
1019 error = xfs_da3_node_lookup_int(state, &retval);
1024 * Remove the name and update the hashvals in the tree.
1026 blk = &state->path.blk[ state->path.active-1 ];
1027 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1028 error = xfs_attr3_leaf_remove(blk->bp, args);
1029 xfs_da3_fixhashpath(state, &state->path);
1032 * Check to see if the tree needs to be collapsed.
1034 if (retval && (state->path.active > 1)) {
1035 error = xfs_da3_join(state);
1038 error = xfs_defer_finish(&args->trans);
1044 * Commit and start the next trans in the chain.
1046 error = xfs_trans_roll_inode(&args->trans, dp);
1050 } else if (args->rmtblkno > 0) {
1052 * Added a "remote" value, just clear the incomplete flag.
1054 error = xfs_attr3_leaf_clearflag(args);
1062 xfs_da_state_free(state);
1069 * Remove a name from a B-tree attribute list.
1071 * This will involve walking down the Btree, and may involve joining
1072 * leaf nodes and even joining intermediate nodes up to and including
1073 * the root node (a special case of an intermediate node).
1076 xfs_attr_node_removename(
1077 struct xfs_da_args *args)
1079 struct xfs_da_state *state;
1080 struct xfs_da_state_blk *blk;
1081 struct xfs_inode *dp;
1083 int retval, error, forkoff;
1085 trace_xfs_attr_node_removename(args);
1088 * Tie a string around our finger to remind us where we are.
1091 state = xfs_da_state_alloc();
1093 state->mp = dp->i_mount;
1096 * Search to see if name exists, and get back a pointer to it.
1098 error = xfs_da3_node_lookup_int(state, &retval);
1099 if (error || (retval != -EEXIST)) {
1106 * If there is an out-of-line value, de-allocate the blocks.
1107 * This is done before we remove the attribute so that we don't
1108 * overflow the maximum size of a transaction and/or hit a deadlock.
1110 blk = &state->path.blk[ state->path.active-1 ];
1111 ASSERT(blk->bp != NULL);
1112 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1113 if (args->rmtblkno > 0) {
1115 * Fill in disk block numbers in the state structure
1116 * so that we can get the buffers back after we commit
1117 * several transactions in the following calls.
1119 error = xfs_attr_fillstate(state);
1124 * Mark the attribute as INCOMPLETE, then bunmapi() the
1127 error = xfs_attr3_leaf_setflag(args);
1130 error = xfs_attr_rmtval_remove(args);
1135 * Refill the state structure with buffers, the prior calls
1136 * released our buffers.
1138 error = xfs_attr_refillstate(state);
1144 * Remove the name and update the hashvals in the tree.
1146 blk = &state->path.blk[ state->path.active-1 ];
1147 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1148 retval = xfs_attr3_leaf_remove(blk->bp, args);
1149 xfs_da3_fixhashpath(state, &state->path);
1152 * Check to see if the tree needs to be collapsed.
1154 if (retval && (state->path.active > 1)) {
1155 error = xfs_da3_join(state);
1158 error = xfs_defer_finish(&args->trans);
1162 * Commit the Btree join operation and start a new trans.
1164 error = xfs_trans_roll_inode(&args->trans, dp);
1170 * If the result is small enough, push it all into the inode.
1172 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1174 * Have to get rid of the copy of this dabuf in the state.
1176 ASSERT(state->path.active == 1);
1177 ASSERT(state->path.blk[0].bp);
1178 state->path.blk[0].bp = NULL;
1180 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1184 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1185 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1186 /* bp is gone due to xfs_da_shrink_inode */
1189 error = xfs_defer_finish(&args->trans);
1193 xfs_trans_brelse(args->trans, bp);
1198 xfs_da_state_free(state);
1203 * Fill in the disk block numbers in the state structure for the buffers
1204 * that are attached to the state structure.
1205 * This is done so that we can quickly reattach ourselves to those buffers
1206 * after some set of transaction commits have released these buffers.
1209 xfs_attr_fillstate(xfs_da_state_t *state)
1211 xfs_da_state_path_t *path;
1212 xfs_da_state_blk_t *blk;
1215 trace_xfs_attr_fillstate(state->args);
1218 * Roll down the "path" in the state structure, storing the on-disk
1219 * block number for those buffers in the "path".
1221 path = &state->path;
1222 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1223 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1225 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1228 blk->disk_blkno = 0;
1233 * Roll down the "altpath" in the state structure, storing the on-disk
1234 * block number for those buffers in the "altpath".
1236 path = &state->altpath;
1237 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1238 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1240 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1243 blk->disk_blkno = 0;
1251 * Reattach the buffers to the state structure based on the disk block
1252 * numbers stored in the state structure.
1253 * This is done after some set of transaction commits have released those
1254 * buffers from our grip.
1257 xfs_attr_refillstate(xfs_da_state_t *state)
1259 xfs_da_state_path_t *path;
1260 xfs_da_state_blk_t *blk;
1263 trace_xfs_attr_refillstate(state->args);
1266 * Roll down the "path" in the state structure, storing the on-disk
1267 * block number for those buffers in the "path".
1269 path = &state->path;
1270 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1271 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1272 if (blk->disk_blkno) {
1273 error = xfs_da3_node_read_mapped(state->args->trans,
1274 state->args->dp, blk->disk_blkno,
1275 &blk->bp, XFS_ATTR_FORK);
1284 * Roll down the "altpath" in the state structure, storing the on-disk
1285 * block number for those buffers in the "altpath".
1287 path = &state->altpath;
1288 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1289 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1290 if (blk->disk_blkno) {
1291 error = xfs_da3_node_read_mapped(state->args->trans,
1292 state->args->dp, blk->disk_blkno,
1293 &blk->bp, XFS_ATTR_FORK);
1305 * Retrieve the attribute data from a node attribute list.
1307 * This routine gets called for any attribute fork that has more than one
1308 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1309 * "remote" values taking up more blocks.
1311 * Returns 0 on successful retrieval, otherwise an error.
1314 xfs_attr_node_get(xfs_da_args_t *args)
1316 xfs_da_state_t *state;
1317 xfs_da_state_blk_t *blk;
1321 trace_xfs_attr_node_get(args);
1323 state = xfs_da_state_alloc();
1325 state->mp = args->dp->i_mount;
1328 * Search to see if name exists, and get back a pointer to it.
1330 error = xfs_da3_node_lookup_int(state, &retval);
1335 if (retval != -EEXIST)
1339 * Get the value, local or "remote"
1341 blk = &state->path.blk[state->path.active - 1];
1342 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1345 * If not in a transaction, we have to release all the buffers.
1348 for (i = 0; i < state->path.active; i++) {
1349 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1350 state->path.blk[i].bp = NULL;
1353 xfs_da_state_free(state);
1357 /* Returns true if the attribute entry name is valid. */
1364 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1365 * out, so use >= for the length check.
1367 if (length >= MAXNAMELEN)
1370 /* There shouldn't be any nulls here */
1371 return !memchr(name, 0, length);