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);
49 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
52 * Internal routines when attribute list is more than one block.
54 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
55 STATIC int xfs_attr_node_addname(struct xfs_da_args *args,
56 struct xfs_da_state *state);
57 STATIC int xfs_attr_node_addname_find_attr(struct xfs_da_args *args,
58 struct xfs_da_state **state);
59 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
60 STATIC int xfs_attr_node_addname_clear_incomplete(struct xfs_da_args *args);
61 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
62 struct xfs_da_state **state);
63 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
64 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
70 if (!XFS_IFORK_Q(ip) ||
71 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
72 ip->i_afp->if_nextents == 0))
78 * Returns true if the there is exactly only block in the attr fork, in which
79 * case the attribute fork consists of a single leaf block entry.
85 struct xfs_ifork *ifp = ip->i_afp;
86 struct xfs_iext_cursor icur;
87 struct xfs_bmbt_irec imap;
89 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
92 xfs_iext_first(ifp, &icur);
93 xfs_iext_get_extent(ifp, &icur, &imap);
94 return imap.br_startoff == 0 && imap.br_blockcount == 1;
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_da_args *args)
109 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
111 if (!xfs_inode_hasattr(args->dp))
114 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
115 return xfs_attr_shortform_getvalue(args);
116 if (xfs_attr_is_leaf(args->dp))
117 return xfs_attr_leaf_get(args);
118 return xfs_attr_node_get(args);
122 * Retrieve an extended attribute by name, and its value if requested.
124 * If args->valuelen is zero, then the caller does not want the value, just an
125 * indication whether the attribute exists and the size of the value if it
126 * exists. The size is returned in args.valuelen.
128 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
129 * for the value after existence of the attribute has been determined. The
130 * caller always has to free args->value if it is set, no matter if this
131 * function was successful or not.
133 * If the attribute is found, but exceeds the size limit set by the caller in
134 * args->valuelen, return -ERANGE with the size of the attribute that was found
139 struct xfs_da_args *args)
144 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
146 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
149 args->geo = args->dp->i_mount->m_attr_geo;
150 args->whichfork = XFS_ATTR_FORK;
151 args->hashval = xfs_da_hashname(args->name, args->namelen);
153 /* Entirely possible to look up a name which doesn't exist */
154 args->op_flags = XFS_DA_OP_OKNOENT;
156 lock_mode = xfs_ilock_attr_map_shared(args->dp);
157 error = xfs_attr_get_ilocked(args);
158 xfs_iunlock(args->dp, lock_mode);
164 * Calculate how many blocks we need for the new attribute,
168 struct xfs_da_args *args,
171 struct xfs_mount *mp = args->dp->i_mount;
176 * Determine space new attribute will use, and if it would be
177 * "local" or "remote" (note: local != inline).
179 size = xfs_attr_leaf_newentsize(args, local);
180 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
182 if (size > (args->geo->blksize / 2)) {
183 /* Double split possible */
188 * Out of line attribute, cannot double split, but
189 * make room for the attribute value itself.
191 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
193 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
200 xfs_attr_try_sf_addname(
201 struct xfs_inode *dp,
202 struct xfs_da_args *args)
208 * Build initial attribute list (if required).
210 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
211 xfs_attr_shortform_create(args);
213 error = xfs_attr_shortform_addname(args);
214 if (error == -ENOSPC)
218 * Commit the shortform mods, and we're done.
219 * NOTE: this is also the error path (EEXIST, etc).
221 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
222 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
224 if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
225 xfs_trans_set_sync(args->trans);
231 * Check to see if the attr should be upgraded from non-existent or shortform to
232 * single-leaf-block attribute list.
235 xfs_attr_is_shortform(
236 struct xfs_inode *ip)
238 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
239 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
240 ip->i_afp->if_nextents == 0);
245 struct xfs_da_args *args)
247 struct xfs_buf *leaf_bp = NULL;
248 struct xfs_inode *dp = args->dp;
249 int error, error2 = 0;
252 * Try to add the attr to the attribute list in the inode.
254 error = xfs_attr_try_sf_addname(dp, args);
255 if (error != -ENOSPC) {
256 error2 = xfs_trans_commit(args->trans);
258 return error ? error : error2;
262 * It won't fit in the shortform, transform to a leaf block. GROT:
263 * another possible req'mt for a double-split btree op.
265 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
270 * Prevent the leaf buffer from being unlocked so that a concurrent AIL
271 * push cannot grab the half-baked leaf buffer and run into problems
272 * with the write verifier.
274 xfs_trans_bhold(args->trans, leaf_bp);
275 error = xfs_defer_finish(&args->trans);
276 xfs_trans_bhold_release(args->trans, leaf_bp);
278 xfs_trans_brelse(args->trans, leaf_bp);
286 * Set the attribute specified in @args.
290 struct xfs_da_args *args)
292 struct xfs_inode *dp = args->dp;
293 struct xfs_da_state *state;
297 * If the attribute list is already in leaf format, jump straight to
298 * leaf handling. Otherwise, try to add the attribute to the shortform
299 * list; if there's no room then convert the list to leaf format and try
302 if (xfs_attr_is_shortform(dp)) {
303 error = xfs_attr_set_fmt(args);
304 if (error != -EAGAIN)
308 if (xfs_attr_is_leaf(dp)) {
309 error = xfs_attr_leaf_addname(args);
310 if (error != -ENOSPC)
314 * Promote the attribute list to the Btree format.
316 error = xfs_attr3_leaf_to_node(args);
321 * Finish any deferred work items and roll the transaction once
322 * more. The goal here is to call node_addname with the inode
323 * and transaction in the same state (inode locked and joined,
324 * transaction clean) no matter how we got to this step.
326 error = xfs_defer_finish(&args->trans);
331 * Commit the current trans (including the inode) and
334 error = xfs_trans_roll_inode(&args->trans, dp);
340 error = xfs_attr_node_addname_find_attr(args, &state);
343 error = xfs_attr_node_addname(args, state);
344 } while (error == -EAGAIN);
350 * Return EEXIST if attr is found, or ENOATTR if not
354 struct xfs_da_args *args)
356 struct xfs_inode *dp = args->dp;
357 struct xfs_buf *bp = NULL;
360 if (!xfs_inode_hasattr(dp))
363 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
364 return xfs_attr_sf_findname(args, NULL, NULL);
366 if (xfs_attr_is_leaf(dp)) {
367 error = xfs_attr_leaf_hasname(args, &bp);
370 xfs_trans_brelse(args->trans, bp);
375 return xfs_attr_node_hasname(args, NULL);
379 * Remove the attribute specified in @args.
382 xfs_attr_remove_args(
383 struct xfs_da_args *args)
385 if (!xfs_inode_hasattr(args->dp))
388 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
389 return xfs_attr_shortform_remove(args);
390 if (xfs_attr_is_leaf(args->dp))
391 return xfs_attr_leaf_removename(args);
392 return xfs_attr_node_removename(args);
396 * Note: If args->value is NULL the attribute will be removed, just like the
397 * Linux ->setattr API.
401 struct xfs_da_args *args)
403 struct xfs_inode *dp = args->dp;
404 struct xfs_mount *mp = dp->i_mount;
405 struct xfs_trans_res tres;
406 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
411 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
414 error = xfs_qm_dqattach(dp);
418 args->geo = mp->m_attr_geo;
419 args->whichfork = XFS_ATTR_FORK;
420 args->hashval = xfs_da_hashname(args->name, args->namelen);
423 * We have no control over the attribute names that userspace passes us
424 * to remove, so we have to allow the name lookup prior to attribute
425 * removal to fail as well.
427 args->op_flags = XFS_DA_OP_OKNOENT;
430 XFS_STATS_INC(mp, xs_attr_set);
432 args->op_flags |= XFS_DA_OP_ADDNAME;
433 args->total = xfs_attr_calc_size(args, &local);
436 * If the inode doesn't have an attribute fork, add one.
437 * (inode must not be locked when we call this routine)
439 if (XFS_IFORK_Q(dp) == 0) {
440 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
441 xfs_attr_sf_entsize_byname(args->namelen,
444 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
449 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
450 M_RES(mp)->tr_attrsetrt.tr_logres *
452 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
453 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
457 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
459 XFS_STATS_INC(mp, xs_attr_remove);
461 tres = M_RES(mp)->tr_attrrm;
462 total = XFS_ATTRRM_SPACE_RES(mp);
463 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
467 * Root fork attributes can use reserved data blocks for this
468 * operation if necessary
470 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
474 if (args->value || xfs_inode_hasattr(dp)) {
475 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
476 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
478 goto out_trans_cancel;
482 error = xfs_has_attr(args);
483 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
484 goto out_trans_cancel;
485 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
486 goto out_trans_cancel;
487 if (error != -ENOATTR && error != -EEXIST)
488 goto out_trans_cancel;
490 error = xfs_attr_set_args(args);
492 goto out_trans_cancel;
493 /* shortform attribute has already been committed */
497 error = xfs_has_attr(args);
498 if (error != -EEXIST)
499 goto out_trans_cancel;
501 error = xfs_attr_remove_args(args);
503 goto out_trans_cancel;
507 * If this is a synchronous mount, make sure that the
508 * transaction goes to disk before returning to the user.
510 if (mp->m_flags & XFS_MOUNT_WSYNC)
511 xfs_trans_set_sync(args->trans);
513 if (!(args->op_flags & XFS_DA_OP_NOTIME))
514 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
517 * Commit the last in the sequence of transactions.
519 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
520 error = xfs_trans_commit(args->trans);
522 xfs_iunlock(dp, XFS_ILOCK_EXCL);
527 xfs_trans_cancel(args->trans);
531 /*========================================================================
532 * External routines when attribute list is inside the inode
533 *========================================================================*/
535 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
537 struct xfs_attr_shortform *sf;
539 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
540 return be16_to_cpu(sf->hdr.totsize);
544 * Add a name to the shortform attribute list structure
545 * This is the external routine.
548 xfs_attr_shortform_addname(xfs_da_args_t *args)
550 int newsize, forkoff, retval;
552 trace_xfs_attr_sf_addname(args);
554 retval = xfs_attr_shortform_lookup(args);
555 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
557 if (retval == -EEXIST) {
558 if (args->attr_flags & XATTR_CREATE)
560 retval = xfs_attr_shortform_remove(args);
564 * Since we have removed the old attr, clear ATTR_REPLACE so
565 * that the leaf format add routine won't trip over the attr
568 args->attr_flags &= ~XATTR_REPLACE;
571 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
572 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
575 newsize = xfs_attr_sf_totsize(args->dp);
576 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
578 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
582 xfs_attr_shortform_add(args, forkoff);
587 /*========================================================================
588 * External routines when attribute list is one block
589 *========================================================================*/
591 /* Store info about a remote block */
593 xfs_attr_save_rmt_blk(
594 struct xfs_da_args *args)
596 args->blkno2 = args->blkno;
597 args->index2 = args->index;
598 args->rmtblkno2 = args->rmtblkno;
599 args->rmtblkcnt2 = args->rmtblkcnt;
600 args->rmtvaluelen2 = args->rmtvaluelen;
603 /* Set stored info about a remote block */
605 xfs_attr_restore_rmt_blk(
606 struct xfs_da_args *args)
608 args->blkno = args->blkno2;
609 args->index = args->index2;
610 args->rmtblkno = args->rmtblkno2;
611 args->rmtblkcnt = args->rmtblkcnt2;
612 args->rmtvaluelen = args->rmtvaluelen2;
616 * Tries to add an attribute to an inode in leaf form
618 * This function is meant to execute as part of a delayed operation and leaves
619 * the transaction handling to the caller. On success the attribute is added
620 * and the inode and transaction are left dirty. If there is not enough space,
621 * the attr data is converted to node format and -ENOSPC is returned. Caller is
622 * responsible for handling the dirty inode and transaction or adding the attr
626 xfs_attr_leaf_try_add(
627 struct xfs_da_args *args,
633 * Look up the given attribute in the leaf block. Figure out if
634 * the given flags produce an error or call for an atomic rename.
636 retval = xfs_attr_leaf_hasname(args, &bp);
637 if (retval != -ENOATTR && retval != -EEXIST)
639 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
641 if (retval == -EEXIST) {
642 if (args->attr_flags & XATTR_CREATE)
645 trace_xfs_attr_leaf_replace(args);
647 /* save the attribute state for later removal*/
648 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
649 xfs_attr_save_rmt_blk(args);
652 * clear the remote attr state now that it is saved so that the
653 * values reflect the state of the attribute we are about to
654 * add, not the attribute we just found and will remove later.
658 args->rmtvaluelen = 0;
662 * Add the attribute to the leaf block
664 return xfs_attr3_leaf_add(bp, args);
667 xfs_trans_brelse(args->trans, bp);
673 * Add a name to the leaf attribute list structure
675 * This leaf block cannot have a "remote" value, we only call this routine
676 * if bmap_one_block() says there is only one block (ie: no remote blks).
679 xfs_attr_leaf_addname(
680 struct xfs_da_args *args)
683 struct xfs_buf *bp = NULL;
684 struct xfs_inode *dp = args->dp;
686 trace_xfs_attr_leaf_addname(args);
688 error = xfs_attr_leaf_try_add(args, bp);
693 * Commit the transaction that added the attr name so that
694 * later routines can manage their own transactions.
696 error = xfs_trans_roll_inode(&args->trans, dp);
701 * If there was an out-of-line value, allocate the blocks we
702 * identified for its storage and copy the value. This is done
703 * after we create the attribute so that we don't overflow the
704 * maximum size of a transaction and/or hit a deadlock.
706 if (args->rmtblkno > 0) {
707 error = xfs_attr_rmtval_set(args);
712 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
714 * Added a "remote" value, just clear the incomplete flag.
716 if (args->rmtblkno > 0)
717 error = xfs_attr3_leaf_clearflag(args);
723 * If this is an atomic rename operation, we must "flip" the incomplete
724 * flags on the "new" and "old" attribute/value pairs so that one
725 * disappears and one appears atomically. Then we must remove the "old"
726 * attribute/value pair.
728 * In a separate transaction, set the incomplete flag on the "old" attr
729 * and clear the incomplete flag on the "new" attr.
732 error = xfs_attr3_leaf_flipflags(args);
736 * Commit the flag value change and start the next trans in series.
738 error = xfs_trans_roll_inode(&args->trans, args->dp);
743 * Dismantle the "old" attribute/value pair by removing a "remote" value
746 xfs_attr_restore_rmt_blk(args);
748 if (args->rmtblkno) {
749 error = xfs_attr_rmtval_invalidate(args);
753 error = xfs_attr_rmtval_remove(args);
759 * Read in the block containing the "old" attr, then remove the "old"
760 * attr from that block (neat, huh!)
762 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
767 xfs_attr3_leaf_remove(bp, args);
770 * If the result is small enough, shrink it all into the inode.
772 forkoff = xfs_attr_shortform_allfit(bp, dp);
774 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
775 /* bp is gone due to xfs_da_shrink_inode */
781 * Return EEXIST if attr is found, or ENOATTR if not
784 xfs_attr_leaf_hasname(
785 struct xfs_da_args *args,
790 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
794 error = xfs_attr3_leaf_lookup_int(*bp, args);
795 if (error != -ENOATTR && error != -EEXIST)
796 xfs_trans_brelse(args->trans, *bp);
802 * Remove a name from the leaf attribute list structure
804 * This leaf block cannot have a "remote" value, we only call this routine
805 * if bmap_one_block() says there is only one block (ie: no remote blks).
808 xfs_attr_leaf_removename(
809 struct xfs_da_args *args)
811 struct xfs_inode *dp;
815 trace_xfs_attr_leaf_removename(args);
818 * Remove the attribute.
822 error = xfs_attr_leaf_hasname(args, &bp);
824 if (error == -ENOATTR) {
825 xfs_trans_brelse(args->trans, bp);
827 } else if (error != -EEXIST)
830 xfs_attr3_leaf_remove(bp, args);
833 * If the result is small enough, shrink it all into the inode.
835 forkoff = xfs_attr_shortform_allfit(bp, dp);
837 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
838 /* bp is gone due to xfs_da_shrink_inode */
844 * Look up a name in a leaf attribute list structure.
846 * This leaf block cannot have a "remote" value, we only call this routine
847 * if bmap_one_block() says there is only one block (ie: no remote blks).
849 * Returns 0 on successful retrieval, otherwise an error.
852 xfs_attr_leaf_get(xfs_da_args_t *args)
857 trace_xfs_attr_leaf_get(args);
859 error = xfs_attr_leaf_hasname(args, &bp);
861 if (error == -ENOATTR) {
862 xfs_trans_brelse(args->trans, bp);
864 } else if (error != -EEXIST)
868 error = xfs_attr3_leaf_getvalue(bp, args);
869 xfs_trans_brelse(args->trans, bp);
874 * Return EEXIST if attr is found, or ENOATTR if not
875 * statep: If not null is set to point at the found state. Caller will
876 * be responsible for freeing the state in this case.
879 xfs_attr_node_hasname(
880 struct xfs_da_args *args,
881 struct xfs_da_state **statep)
883 struct xfs_da_state *state;
886 state = xfs_da_state_alloc(args);
891 * Search to see if name exists, and get back a pointer to it.
893 error = xfs_da3_node_lookup_int(state, &retval);
895 xfs_da_state_free(state);
902 xfs_da_state_free(state);
906 /*========================================================================
907 * External routines when attribute list size > geo->blksize
908 *========================================================================*/
911 xfs_attr_node_addname_find_attr(
912 struct xfs_da_args *args,
913 struct xfs_da_state **state)
918 * Search to see if name already exists, and get back a pointer
919 * to where it should go.
921 retval = xfs_attr_node_hasname(args, state);
922 if (retval != -ENOATTR && retval != -EEXIST)
925 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
927 if (retval == -EEXIST) {
928 if (args->attr_flags & XATTR_CREATE)
931 trace_xfs_attr_node_replace(args);
933 /* save the attribute state for later removal*/
934 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
935 xfs_attr_save_rmt_blk(args);
938 * clear the remote attr state now that it is saved so that the
939 * values reflect the state of the attribute we are about to
940 * add, not the attribute we just found and will remove later.
944 args->rmtvaluelen = 0;
950 xfs_da_state_free(*state);
955 * Add a name to a Btree-format attribute list.
957 * This will involve walking down the Btree, and may involve splitting
958 * leaf nodes and even splitting intermediate nodes up to and including
959 * the root node (a special case of an intermediate node).
961 * "Remote" attribute values confuse the issue and atomic rename operations
962 * add a whole extra layer of confusion on top of that.
965 xfs_attr_node_addname(
966 struct xfs_da_args *args,
967 struct xfs_da_state *state)
969 struct xfs_da_state_blk *blk;
970 struct xfs_inode *dp;
973 trace_xfs_attr_node_addname(args);
976 blk = &state->path.blk[state->path.active-1];
977 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
979 retval = xfs_attr3_leaf_add(blk->bp, state->args);
980 if (retval == -ENOSPC) {
981 if (state->path.active == 1) {
983 * Its really a single leaf node, but it had
984 * out-of-line values so it looked like it *might*
985 * have been a b-tree.
987 xfs_da_state_free(state);
989 error = xfs_attr3_leaf_to_node(args);
992 error = xfs_defer_finish(&args->trans);
997 * Commit the node conversion and start the next
998 * trans in the chain.
1000 error = xfs_trans_roll_inode(&args->trans, dp);
1008 * Split as many Btree elements as required.
1009 * This code tracks the new and old attr's location
1010 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1011 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1013 error = xfs_da3_split(state);
1016 error = xfs_defer_finish(&args->trans);
1021 * Addition succeeded, update Btree hashvals.
1023 xfs_da3_fixhashpath(state, &state->path);
1027 * Kill the state structure, we're done with it and need to
1028 * allow the buffers to come back later.
1030 xfs_da_state_free(state);
1034 * Commit the leaf addition or btree split and start the next
1035 * trans in the chain.
1037 error = xfs_trans_roll_inode(&args->trans, dp);
1042 * If there was an out-of-line value, allocate the blocks we
1043 * identified for its storage and copy the value. This is done
1044 * after we create the attribute so that we don't overflow the
1045 * maximum size of a transaction and/or hit a deadlock.
1047 if (args->rmtblkno > 0) {
1048 error = xfs_attr_rmtval_set(args);
1053 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
1055 * Added a "remote" value, just clear the incomplete flag.
1057 if (args->rmtblkno > 0)
1058 error = xfs_attr3_leaf_clearflag(args);
1064 * If this is an atomic rename operation, we must "flip" the incomplete
1065 * flags on the "new" and "old" attribute/value pairs so that one
1066 * disappears and one appears atomically. Then we must remove the "old"
1067 * attribute/value pair.
1069 * In a separate transaction, set the incomplete flag on the "old" attr
1070 * and clear the incomplete flag on the "new" attr.
1072 error = xfs_attr3_leaf_flipflags(args);
1076 * Commit the flag value change and start the next trans in series
1078 error = xfs_trans_roll_inode(&args->trans, args->dp);
1083 * Dismantle the "old" attribute/value pair by removing a "remote" value
1086 xfs_attr_restore_rmt_blk(args);
1088 if (args->rmtblkno) {
1089 error = xfs_attr_rmtval_invalidate(args);
1093 error = xfs_attr_rmtval_remove(args);
1098 error = xfs_attr_node_addname_clear_incomplete(args);
1104 xfs_da_state_free(state);
1112 xfs_attr_node_addname_clear_incomplete(
1113 struct xfs_da_args *args)
1115 struct xfs_da_state *state = NULL;
1116 struct xfs_da_state_blk *blk;
1121 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1122 * flag means that we will find the "old" attr, not the "new" one.
1124 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1125 state = xfs_da_state_alloc(args);
1127 error = xfs_da3_node_lookup_int(state, &retval);
1132 * Remove the name and update the hashvals in the tree.
1134 blk = &state->path.blk[state->path.active-1];
1135 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1136 error = xfs_attr3_leaf_remove(blk->bp, args);
1137 xfs_da3_fixhashpath(state, &state->path);
1140 * Check to see if the tree needs to be collapsed.
1142 if (retval && (state->path.active > 1)) {
1143 error = xfs_da3_join(state);
1151 xfs_da_state_free(state);
1158 * Shrink an attribute from leaf to shortform
1161 xfs_attr_node_shrink(
1162 struct xfs_da_args *args,
1163 struct xfs_da_state *state)
1165 struct xfs_inode *dp = args->dp;
1170 * Have to get rid of the copy of this dabuf in the state.
1172 ASSERT(state->path.active == 1);
1173 ASSERT(state->path.blk[0].bp);
1174 state->path.blk[0].bp = NULL;
1176 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1180 forkoff = xfs_attr_shortform_allfit(bp, dp);
1182 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1183 /* bp is gone due to xfs_da_shrink_inode */
1185 xfs_trans_brelse(args->trans, bp);
1191 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1192 * for later deletion of the entry.
1195 xfs_attr_leaf_mark_incomplete(
1196 struct xfs_da_args *args,
1197 struct xfs_da_state *state)
1202 * Fill in disk block numbers in the state structure
1203 * so that we can get the buffers back after we commit
1204 * several transactions in the following calls.
1206 error = xfs_attr_fillstate(state);
1211 * Mark the attribute as INCOMPLETE
1213 return xfs_attr3_leaf_setflag(args);
1217 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1218 * the blocks are valid. Attr keys with remote blocks will be marked
1222 int xfs_attr_node_removename_setup(
1223 struct xfs_da_args *args,
1224 struct xfs_da_state **state)
1228 error = xfs_attr_node_hasname(args, state);
1229 if (error != -EEXIST)
1232 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1233 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1234 XFS_ATTR_LEAF_MAGIC);
1236 if (args->rmtblkno > 0) {
1237 error = xfs_attr_leaf_mark_incomplete(args, *state);
1241 return xfs_attr_rmtval_invalidate(args);
1248 xfs_attr_node_remove_name(
1249 struct xfs_da_args *args,
1250 struct xfs_da_state *state)
1252 struct xfs_da_state_blk *blk;
1256 * Remove the name and update the hashvals in the tree.
1258 blk = &state->path.blk[state->path.active-1];
1259 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1260 retval = xfs_attr3_leaf_remove(blk->bp, args);
1261 xfs_da3_fixhashpath(state, &state->path);
1267 * Remove a name from a B-tree attribute list.
1269 * This will involve walking down the Btree, and may involve joining
1270 * leaf nodes and even joining intermediate nodes up to and including
1271 * the root node (a special case of an intermediate node).
1274 xfs_attr_node_removename(
1275 struct xfs_da_args *args)
1277 struct xfs_da_state *state;
1279 struct xfs_inode *dp = args->dp;
1281 trace_xfs_attr_node_removename(args);
1283 error = xfs_attr_node_removename_setup(args, &state);
1288 * If there is an out-of-line value, de-allocate the blocks.
1289 * This is done before we remove the attribute so that we don't
1290 * overflow the maximum size of a transaction and/or hit a deadlock.
1292 if (args->rmtblkno > 0) {
1293 error = xfs_attr_rmtval_remove(args);
1298 * Refill the state structure with buffers, the prior calls
1299 * released our buffers.
1301 error = xfs_attr_refillstate(state);
1305 retval = xfs_attr_node_remove_name(args, state);
1308 * Check to see if the tree needs to be collapsed.
1310 if (retval && (state->path.active > 1)) {
1311 error = xfs_da3_join(state);
1314 error = xfs_defer_finish(&args->trans);
1318 * Commit the Btree join operation and start a new trans.
1320 error = xfs_trans_roll_inode(&args->trans, dp);
1326 * If the result is small enough, push it all into the inode.
1328 if (xfs_attr_is_leaf(dp))
1329 error = xfs_attr_node_shrink(args, state);
1333 xfs_da_state_free(state);
1338 * Fill in the disk block numbers in the state structure for the buffers
1339 * that are attached to the state structure.
1340 * This is done so that we can quickly reattach ourselves to those buffers
1341 * after some set of transaction commits have released these buffers.
1344 xfs_attr_fillstate(xfs_da_state_t *state)
1346 xfs_da_state_path_t *path;
1347 xfs_da_state_blk_t *blk;
1350 trace_xfs_attr_fillstate(state->args);
1353 * Roll down the "path" in the state structure, storing the on-disk
1354 * block number for those buffers in the "path".
1356 path = &state->path;
1357 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1358 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1360 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1363 blk->disk_blkno = 0;
1368 * Roll down the "altpath" in the state structure, storing the on-disk
1369 * block number for those buffers in the "altpath".
1371 path = &state->altpath;
1372 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1373 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1375 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1378 blk->disk_blkno = 0;
1386 * Reattach the buffers to the state structure based on the disk block
1387 * numbers stored in the state structure.
1388 * This is done after some set of transaction commits have released those
1389 * buffers from our grip.
1392 xfs_attr_refillstate(xfs_da_state_t *state)
1394 xfs_da_state_path_t *path;
1395 xfs_da_state_blk_t *blk;
1398 trace_xfs_attr_refillstate(state->args);
1401 * Roll down the "path" in the state structure, storing the on-disk
1402 * block number for those buffers in the "path".
1404 path = &state->path;
1405 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1406 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1407 if (blk->disk_blkno) {
1408 error = xfs_da3_node_read_mapped(state->args->trans,
1409 state->args->dp, blk->disk_blkno,
1410 &blk->bp, XFS_ATTR_FORK);
1419 * Roll down the "altpath" in the state structure, storing the on-disk
1420 * block number for those buffers in the "altpath".
1422 path = &state->altpath;
1423 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1424 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1425 if (blk->disk_blkno) {
1426 error = xfs_da3_node_read_mapped(state->args->trans,
1427 state->args->dp, blk->disk_blkno,
1428 &blk->bp, XFS_ATTR_FORK);
1440 * Retrieve the attribute data from a node attribute list.
1442 * This routine gets called for any attribute fork that has more than one
1443 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1444 * "remote" values taking up more blocks.
1446 * Returns 0 on successful retrieval, otherwise an error.
1450 struct xfs_da_args *args)
1452 struct xfs_da_state *state;
1453 struct xfs_da_state_blk *blk;
1457 trace_xfs_attr_node_get(args);
1460 * Search to see if name exists, and get back a pointer to it.
1462 error = xfs_attr_node_hasname(args, &state);
1463 if (error != -EEXIST)
1467 * Get the value, local or "remote"
1469 blk = &state->path.blk[state->path.active - 1];
1470 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1473 * If not in a transaction, we have to release all the buffers.
1476 for (i = 0; state != NULL && i < state->path.active; i++) {
1477 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1478 state->path.blk[i].bp = NULL;
1482 xfs_da_state_free(state);
1486 /* Returns true if the attribute entry name is valid. */
1493 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1494 * out, so use >= for the length check.
1496 if (length >= MAXNAMELEN)
1499 /* There shouldn't be any nulls here */
1500 return !memchr(name, 0, length);