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_removename(xfs_da_args_t *args);
48 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
49 STATIC int xfs_attr_leaf_try_add(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 void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
56 STATIC int xfs_attr_node_addname(struct xfs_delattr_context *dac);
57 STATIC int xfs_attr_node_addname_find_attr(struct xfs_delattr_context *dac);
58 STATIC int xfs_attr_node_addname_clear_incomplete(
59 struct xfs_delattr_context *dac);
60 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
61 struct xfs_da_state **state);
62 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
63 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
64 STATIC int xfs_attr_set_iter(struct xfs_delattr_context *dac,
65 struct xfs_buf **leaf_bp);
66 STATIC int xfs_attr_node_removename(struct xfs_da_args *args,
67 struct xfs_da_state *state);
73 if (!XFS_IFORK_Q(ip) ||
74 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
75 ip->i_afp->if_nextents == 0))
81 * Returns true if the there is exactly only block in the attr fork, in which
82 * case the attribute fork consists of a single leaf block entry.
88 struct xfs_ifork *ifp = ip->i_afp;
89 struct xfs_iext_cursor icur;
90 struct xfs_bmbt_irec imap;
92 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
95 xfs_iext_first(ifp, &icur);
96 xfs_iext_get_extent(ifp, &icur, &imap);
97 return imap.br_startoff == 0 && imap.br_blockcount == 1;
100 /*========================================================================
101 * Overall external interface routines.
102 *========================================================================*/
105 * Retrieve an extended attribute and its value. Must have ilock.
106 * Returns 0 on successful retrieval, otherwise an error.
109 xfs_attr_get_ilocked(
110 struct xfs_da_args *args)
112 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
114 if (!xfs_inode_hasattr(args->dp))
117 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
118 return xfs_attr_shortform_getvalue(args);
119 if (xfs_attr_is_leaf(args->dp))
120 return xfs_attr_leaf_get(args);
121 return xfs_attr_node_get(args);
125 * Retrieve an extended attribute by name, and its value if requested.
127 * If args->valuelen is zero, then the caller does not want the value, just an
128 * indication whether the attribute exists and the size of the value if it
129 * exists. The size is returned in args.valuelen.
131 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
132 * for the value after existence of the attribute has been determined. The
133 * caller always has to free args->value if it is set, no matter if this
134 * function was successful or not.
136 * If the attribute is found, but exceeds the size limit set by the caller in
137 * args->valuelen, return -ERANGE with the size of the attribute that was found
142 struct xfs_da_args *args)
147 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
149 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
152 args->geo = args->dp->i_mount->m_attr_geo;
153 args->whichfork = XFS_ATTR_FORK;
154 args->hashval = xfs_da_hashname(args->name, args->namelen);
156 /* Entirely possible to look up a name which doesn't exist */
157 args->op_flags = XFS_DA_OP_OKNOENT;
159 lock_mode = xfs_ilock_attr_map_shared(args->dp);
160 error = xfs_attr_get_ilocked(args);
161 xfs_iunlock(args->dp, lock_mode);
167 * Calculate how many blocks we need for the new attribute,
171 struct xfs_da_args *args,
174 struct xfs_mount *mp = args->dp->i_mount;
179 * Determine space new attribute will use, and if it would be
180 * "local" or "remote" (note: local != inline).
182 size = xfs_attr_leaf_newentsize(args, local);
183 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
185 if (size > (args->geo->blksize / 2)) {
186 /* Double split possible */
191 * Out of line attribute, cannot double split, but
192 * make room for the attribute value itself.
194 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
196 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
203 xfs_attr_try_sf_addname(
204 struct xfs_inode *dp,
205 struct xfs_da_args *args)
211 * Build initial attribute list (if required).
213 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
214 xfs_attr_shortform_create(args);
216 error = xfs_attr_shortform_addname(args);
217 if (error == -ENOSPC)
221 * Commit the shortform mods, and we're done.
222 * NOTE: this is also the error path (EEXIST, etc).
224 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
225 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
227 if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
228 xfs_trans_set_sync(args->trans);
234 * Check to see if the attr should be upgraded from non-existent or shortform to
235 * single-leaf-block attribute list.
238 xfs_attr_is_shortform(
239 struct xfs_inode *ip)
241 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
242 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
243 ip->i_afp->if_nextents == 0);
247 * Checks to see if a delayed attribute transaction should be rolled. If so,
248 * transaction is finished or rolled as needed.
252 struct xfs_delattr_context *dac)
254 struct xfs_da_args *args = dac->da_args;
257 if (dac->flags & XFS_DAC_DEFER_FINISH) {
259 * The caller wants us to finish all the deferred ops so that we
260 * avoid pinning the log tail with a large number of deferred
263 dac->flags &= ~XFS_DAC_DEFER_FINISH;
264 error = xfs_defer_finish(&args->trans);
266 error = xfs_trans_roll_inode(&args->trans, args->dp);
272 * Set the attribute specified in @args.
276 struct xfs_da_args *args)
278 struct xfs_buf *leaf_bp = NULL;
280 struct xfs_delattr_context dac = {
285 error = xfs_attr_set_iter(&dac, &leaf_bp);
286 if (error != -EAGAIN)
289 error = xfs_attr_trans_roll(&dac);
292 xfs_trans_brelse(args->trans, leaf_bp);
302 struct xfs_delattr_context *dac,
303 struct xfs_buf **leaf_bp)
305 struct xfs_da_args *args = dac->da_args;
306 struct xfs_inode *dp = args->dp;
310 * Try to add the attr to the attribute list in the inode.
312 error = xfs_attr_try_sf_addname(dp, args);
314 /* Should only be 0, -EEXIST or -ENOSPC */
315 if (error != -ENOSPC)
319 * It won't fit in the shortform, transform to a leaf block. GROT:
320 * another possible req'mt for a double-split btree op.
322 error = xfs_attr_shortform_to_leaf(args, leaf_bp);
327 * Prevent the leaf buffer from being unlocked so that a concurrent AIL
328 * push cannot grab the half-baked leaf buffer and run into problems
329 * with the write verifier.
331 xfs_trans_bhold(args->trans, *leaf_bp);
334 * We're still in XFS_DAS_UNINIT state here. We've converted
335 * the attr fork to leaf format and will restart with the leaf
338 dac->flags |= XFS_DAC_DEFER_FINISH;
343 * Set the attribute specified in @args.
344 * This routine is meant to function as a delayed operation, and may return
345 * -EAGAIN when the transaction needs to be rolled. Calling functions will need
346 * to handle this, and recall the function until a successful error code is
351 struct xfs_delattr_context *dac,
352 struct xfs_buf **leaf_bp)
354 struct xfs_da_args *args = dac->da_args;
355 struct xfs_inode *dp = args->dp;
356 struct xfs_buf *bp = NULL;
357 int forkoff, error = 0;
359 /* State machine switch */
360 switch (dac->dela_state) {
363 * If the fork is shortform, attempt to add the attr. If there
364 * is no space, this converts to leaf format and returns
365 * -EAGAIN with the leaf buffer held across the roll. The caller
366 * will deal with a transaction roll error, but otherwise
367 * release the hold once we return with a clean transaction.
369 if (xfs_attr_is_shortform(dp))
370 return xfs_attr_sf_addname(dac, leaf_bp);
371 if (*leaf_bp != NULL) {
372 xfs_trans_bhold_release(args->trans, *leaf_bp);
376 if (xfs_attr_is_leaf(dp)) {
377 error = xfs_attr_leaf_try_add(args, *leaf_bp);
378 if (error == -ENOSPC) {
379 error = xfs_attr3_leaf_to_node(args);
384 * Finish any deferred work items and roll the
385 * transaction once more. The goal here is to
386 * call node_addname with the inode and
387 * transaction in the same state (inode locked
388 * and joined, transaction clean) no matter how
389 * we got to this step.
391 * At this point, we are still in
392 * XFS_DAS_UNINIT, but when we come back, we'll
393 * be a node, so we'll fall down into the node
394 * handling code below
396 dac->flags |= XFS_DAC_DEFER_FINISH;
402 dac->dela_state = XFS_DAS_FOUND_LBLK;
404 error = xfs_attr_node_addname_find_attr(dac);
408 error = xfs_attr_node_addname(dac);
412 dac->dela_state = XFS_DAS_FOUND_NBLK;
415 case XFS_DAS_FOUND_LBLK:
417 * If there was an out-of-line value, allocate the blocks we
418 * identified for its storage and copy the value. This is done
419 * after we create the attribute so that we don't overflow the
420 * maximum size of a transaction and/or hit a deadlock.
423 /* Open coded xfs_attr_rmtval_set without trans handling */
424 if ((dac->flags & XFS_DAC_LEAF_ADDNAME_INIT) == 0) {
425 dac->flags |= XFS_DAC_LEAF_ADDNAME_INIT;
426 if (args->rmtblkno > 0) {
427 error = xfs_attr_rmtval_find_space(dac);
434 * Repeat allocating remote blocks for the attr value until
435 * blkcnt drops to zero.
437 if (dac->blkcnt > 0) {
438 error = xfs_attr_rmtval_set_blk(dac);
444 error = xfs_attr_rmtval_set_value(args);
449 * If this is not a rename, clear the incomplete flag and we're
452 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
453 if (args->rmtblkno > 0)
454 error = xfs_attr3_leaf_clearflag(args);
459 * If this is an atomic rename operation, we must "flip" the
460 * incomplete flags on the "new" and "old" attribute/value pairs
461 * so that one disappears and one appears atomically. Then we
462 * must remove the "old" attribute/value pair.
464 * In a separate transaction, set the incomplete flag on the
465 * "old" attr and clear the incomplete flag on the "new" attr.
467 error = xfs_attr3_leaf_flipflags(args);
471 * Commit the flag value change and start the next trans in
474 dac->dela_state = XFS_DAS_FLIP_LFLAG;
476 case XFS_DAS_FLIP_LFLAG:
478 * Dismantle the "old" attribute/value pair by removing a
479 * "remote" value (if it exists).
481 xfs_attr_restore_rmt_blk(args);
482 error = xfs_attr_rmtval_invalidate(args);
487 case XFS_DAS_RM_LBLK:
488 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
489 dac->dela_state = XFS_DAS_RM_LBLK;
490 if (args->rmtblkno) {
491 error = __xfs_attr_rmtval_remove(dac);
495 dac->dela_state = XFS_DAS_RD_LEAF;
500 case XFS_DAS_RD_LEAF:
502 * This is the last step for leaf format. Read the block with
503 * the old attr, remove the old attr, check for shortform
504 * conversion and return.
506 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
511 xfs_attr3_leaf_remove(bp, args);
513 forkoff = xfs_attr_shortform_allfit(bp, dp);
515 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
516 /* bp is gone due to xfs_da_shrink_inode */
520 case XFS_DAS_FOUND_NBLK:
522 * Find space for remote blocks and fall into the allocation
525 if (args->rmtblkno > 0) {
526 error = xfs_attr_rmtval_find_space(dac);
532 case XFS_DAS_ALLOC_NODE:
534 * If there was an out-of-line value, allocate the blocks we
535 * identified for its storage and copy the value. This is done
536 * after we create the attribute so that we don't overflow the
537 * maximum size of a transaction and/or hit a deadlock.
539 dac->dela_state = XFS_DAS_ALLOC_NODE;
540 if (args->rmtblkno > 0) {
541 if (dac->blkcnt > 0) {
542 error = xfs_attr_rmtval_set_blk(dac);
548 error = xfs_attr_rmtval_set_value(args);
554 * If this was not a rename, clear the incomplete flag and we're
557 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
558 if (args->rmtblkno > 0)
559 error = xfs_attr3_leaf_clearflag(args);
564 * If this is an atomic rename operation, we must "flip" the
565 * incomplete flags on the "new" and "old" attribute/value pairs
566 * so that one disappears and one appears atomically. Then we
567 * must remove the "old" attribute/value pair.
569 * In a separate transaction, set the incomplete flag on the
570 * "old" attr and clear the incomplete flag on the "new" attr.
572 error = xfs_attr3_leaf_flipflags(args);
576 * Commit the flag value change and start the next trans in
579 dac->dela_state = XFS_DAS_FLIP_NFLAG;
582 case XFS_DAS_FLIP_NFLAG:
584 * Dismantle the "old" attribute/value pair by removing a
585 * "remote" value (if it exists).
587 xfs_attr_restore_rmt_blk(args);
589 error = xfs_attr_rmtval_invalidate(args);
594 case XFS_DAS_RM_NBLK:
595 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
596 dac->dela_state = XFS_DAS_RM_NBLK;
597 if (args->rmtblkno) {
598 error = __xfs_attr_rmtval_remove(dac);
602 dac->dela_state = XFS_DAS_CLR_FLAG;
607 case XFS_DAS_CLR_FLAG:
609 * The last state for node format. Look up the old attr and
612 error = xfs_attr_node_addname_clear_incomplete(dac);
624 * Return EEXIST if attr is found, or ENOATTR if not
628 struct xfs_da_args *args)
630 struct xfs_inode *dp = args->dp;
631 struct xfs_buf *bp = NULL;
634 if (!xfs_inode_hasattr(dp))
637 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
638 return xfs_attr_sf_findname(args, NULL, NULL);
640 if (xfs_attr_is_leaf(dp)) {
641 error = xfs_attr_leaf_hasname(args, &bp);
644 xfs_trans_brelse(args->trans, bp);
649 return xfs_attr_node_hasname(args, NULL);
653 * Remove the attribute specified in @args.
656 xfs_attr_remove_args(
657 struct xfs_da_args *args)
660 struct xfs_delattr_context dac = {
665 error = xfs_attr_remove_iter(&dac);
666 if (error != -EAGAIN)
669 error = xfs_attr_trans_roll(&dac);
679 * Note: If args->value is NULL the attribute will be removed, just like the
680 * Linux ->setattr API.
684 struct xfs_da_args *args)
686 struct xfs_inode *dp = args->dp;
687 struct xfs_mount *mp = dp->i_mount;
688 struct xfs_trans_res tres;
689 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
694 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
697 error = xfs_qm_dqattach(dp);
701 args->geo = mp->m_attr_geo;
702 args->whichfork = XFS_ATTR_FORK;
703 args->hashval = xfs_da_hashname(args->name, args->namelen);
706 * We have no control over the attribute names that userspace passes us
707 * to remove, so we have to allow the name lookup prior to attribute
708 * removal to fail as well.
710 args->op_flags = XFS_DA_OP_OKNOENT;
713 XFS_STATS_INC(mp, xs_attr_set);
715 args->op_flags |= XFS_DA_OP_ADDNAME;
716 args->total = xfs_attr_calc_size(args, &local);
719 * If the inode doesn't have an attribute fork, add one.
720 * (inode must not be locked when we call this routine)
722 if (XFS_IFORK_Q(dp) == 0) {
723 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
724 xfs_attr_sf_entsize_byname(args->namelen,
727 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
732 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
733 M_RES(mp)->tr_attrsetrt.tr_logres *
735 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
736 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
740 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
742 XFS_STATS_INC(mp, xs_attr_remove);
744 tres = M_RES(mp)->tr_attrrm;
745 total = XFS_ATTRRM_SPACE_RES(mp);
746 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
750 * Root fork attributes can use reserved data blocks for this
751 * operation if necessary
753 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
757 if (args->value || xfs_inode_hasattr(dp)) {
758 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
759 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
761 goto out_trans_cancel;
765 error = xfs_has_attr(args);
766 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
767 goto out_trans_cancel;
768 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
769 goto out_trans_cancel;
770 if (error != -ENOATTR && error != -EEXIST)
771 goto out_trans_cancel;
773 error = xfs_attr_set_args(args);
775 goto out_trans_cancel;
776 /* shortform attribute has already been committed */
780 error = xfs_has_attr(args);
781 if (error != -EEXIST)
782 goto out_trans_cancel;
784 error = xfs_attr_remove_args(args);
786 goto out_trans_cancel;
790 * If this is a synchronous mount, make sure that the
791 * transaction goes to disk before returning to the user.
793 if (mp->m_flags & XFS_MOUNT_WSYNC)
794 xfs_trans_set_sync(args->trans);
796 if (!(args->op_flags & XFS_DA_OP_NOTIME))
797 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
800 * Commit the last in the sequence of transactions.
802 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
803 error = xfs_trans_commit(args->trans);
805 xfs_iunlock(dp, XFS_ILOCK_EXCL);
810 xfs_trans_cancel(args->trans);
814 /*========================================================================
815 * External routines when attribute list is inside the inode
816 *========================================================================*/
818 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
820 struct xfs_attr_shortform *sf;
822 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
823 return be16_to_cpu(sf->hdr.totsize);
827 * Add a name to the shortform attribute list structure
828 * This is the external routine.
831 xfs_attr_shortform_addname(xfs_da_args_t *args)
833 int newsize, forkoff, retval;
835 trace_xfs_attr_sf_addname(args);
837 retval = xfs_attr_shortform_lookup(args);
838 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
840 if (retval == -EEXIST) {
841 if (args->attr_flags & XATTR_CREATE)
843 retval = xfs_attr_sf_removename(args);
847 * Since we have removed the old attr, clear ATTR_REPLACE so
848 * that the leaf format add routine won't trip over the attr
851 args->attr_flags &= ~XATTR_REPLACE;
854 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
855 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
858 newsize = xfs_attr_sf_totsize(args->dp);
859 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
861 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
865 xfs_attr_shortform_add(args, forkoff);
870 /*========================================================================
871 * External routines when attribute list is one block
872 *========================================================================*/
874 /* Store info about a remote block */
876 xfs_attr_save_rmt_blk(
877 struct xfs_da_args *args)
879 args->blkno2 = args->blkno;
880 args->index2 = args->index;
881 args->rmtblkno2 = args->rmtblkno;
882 args->rmtblkcnt2 = args->rmtblkcnt;
883 args->rmtvaluelen2 = args->rmtvaluelen;
886 /* Set stored info about a remote block */
888 xfs_attr_restore_rmt_blk(
889 struct xfs_da_args *args)
891 args->blkno = args->blkno2;
892 args->index = args->index2;
893 args->rmtblkno = args->rmtblkno2;
894 args->rmtblkcnt = args->rmtblkcnt2;
895 args->rmtvaluelen = args->rmtvaluelen2;
899 * Tries to add an attribute to an inode in leaf form
901 * This function is meant to execute as part of a delayed operation and leaves
902 * the transaction handling to the caller. On success the attribute is added
903 * and the inode and transaction are left dirty. If there is not enough space,
904 * the attr data is converted to node format and -ENOSPC is returned. Caller is
905 * responsible for handling the dirty inode and transaction or adding the attr
909 xfs_attr_leaf_try_add(
910 struct xfs_da_args *args,
916 * Look up the given attribute in the leaf block. Figure out if
917 * the given flags produce an error or call for an atomic rename.
919 retval = xfs_attr_leaf_hasname(args, &bp);
920 if (retval != -ENOATTR && retval != -EEXIST)
922 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
924 if (retval == -EEXIST) {
925 if (args->attr_flags & XATTR_CREATE)
928 trace_xfs_attr_leaf_replace(args);
930 /* save the attribute state for later removal*/
931 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
932 xfs_attr_save_rmt_blk(args);
935 * clear the remote attr state now that it is saved so that the
936 * values reflect the state of the attribute we are about to
937 * add, not the attribute we just found and will remove later.
941 args->rmtvaluelen = 0;
945 * Add the attribute to the leaf block
947 return xfs_attr3_leaf_add(bp, args);
950 xfs_trans_brelse(args->trans, bp);
955 * Return EEXIST if attr is found, or ENOATTR if not
958 xfs_attr_leaf_hasname(
959 struct xfs_da_args *args,
964 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
968 error = xfs_attr3_leaf_lookup_int(*bp, args);
969 if (error != -ENOATTR && error != -EEXIST)
970 xfs_trans_brelse(args->trans, *bp);
976 * Remove a name from the leaf attribute list structure
978 * This leaf block cannot have a "remote" value, we only call this routine
979 * if bmap_one_block() says there is only one block (ie: no remote blks).
982 xfs_attr_leaf_removename(
983 struct xfs_da_args *args)
985 struct xfs_inode *dp;
989 trace_xfs_attr_leaf_removename(args);
992 * Remove the attribute.
996 error = xfs_attr_leaf_hasname(args, &bp);
998 if (error == -ENOATTR) {
999 xfs_trans_brelse(args->trans, bp);
1001 } else if (error != -EEXIST)
1004 xfs_attr3_leaf_remove(bp, args);
1007 * If the result is small enough, shrink it all into the inode.
1009 forkoff = xfs_attr_shortform_allfit(bp, dp);
1011 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1012 /* bp is gone due to xfs_da_shrink_inode */
1018 * Look up a name in a leaf attribute list structure.
1020 * This leaf block cannot have a "remote" value, we only call this routine
1021 * if bmap_one_block() says there is only one block (ie: no remote blks).
1023 * Returns 0 on successful retrieval, otherwise an error.
1026 xfs_attr_leaf_get(xfs_da_args_t *args)
1031 trace_xfs_attr_leaf_get(args);
1033 error = xfs_attr_leaf_hasname(args, &bp);
1035 if (error == -ENOATTR) {
1036 xfs_trans_brelse(args->trans, bp);
1038 } else if (error != -EEXIST)
1042 error = xfs_attr3_leaf_getvalue(bp, args);
1043 xfs_trans_brelse(args->trans, bp);
1048 * Return EEXIST if attr is found, or ENOATTR if not
1049 * statep: If not null is set to point at the found state. Caller will
1050 * be responsible for freeing the state in this case.
1053 xfs_attr_node_hasname(
1054 struct xfs_da_args *args,
1055 struct xfs_da_state **statep)
1057 struct xfs_da_state *state;
1060 state = xfs_da_state_alloc(args);
1065 * Search to see if name exists, and get back a pointer to it.
1067 error = xfs_da3_node_lookup_int(state, &retval);
1069 xfs_da_state_free(state);
1076 xfs_da_state_free(state);
1080 /*========================================================================
1081 * External routines when attribute list size > geo->blksize
1082 *========================================================================*/
1085 xfs_attr_node_addname_find_attr(
1086 struct xfs_delattr_context *dac)
1088 struct xfs_da_args *args = dac->da_args;
1092 * Search to see if name already exists, and get back a pointer
1093 * to where it should go.
1095 retval = xfs_attr_node_hasname(args, &dac->da_state);
1096 if (retval != -ENOATTR && retval != -EEXIST)
1099 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
1101 if (retval == -EEXIST) {
1102 if (args->attr_flags & XATTR_CREATE)
1105 trace_xfs_attr_node_replace(args);
1107 /* save the attribute state for later removal*/
1108 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1109 xfs_attr_save_rmt_blk(args);
1112 * clear the remote attr state now that it is saved so that the
1113 * values reflect the state of the attribute we are about to
1114 * add, not the attribute we just found and will remove later.
1117 args->rmtblkcnt = 0;
1118 args->rmtvaluelen = 0;
1124 xfs_da_state_free(dac->da_state);
1129 * Add a name to a Btree-format attribute list.
1131 * This will involve walking down the Btree, and may involve splitting
1132 * leaf nodes and even splitting intermediate nodes up to and including
1133 * the root node (a special case of an intermediate node).
1135 * "Remote" attribute values confuse the issue and atomic rename operations
1136 * add a whole extra layer of confusion on top of that.
1138 * This routine is meant to function as a delayed operation, and may return
1139 * -EAGAIN when the transaction needs to be rolled. Calling functions will need
1140 * to handle this, and recall the function until a successful error code is
1144 xfs_attr_node_addname(
1145 struct xfs_delattr_context *dac)
1147 struct xfs_da_args *args = dac->da_args;
1148 struct xfs_da_state *state = dac->da_state;
1149 struct xfs_da_state_blk *blk;
1152 trace_xfs_attr_node_addname(args);
1154 blk = &state->path.blk[state->path.active-1];
1155 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1157 error = xfs_attr3_leaf_add(blk->bp, state->args);
1158 if (error == -ENOSPC) {
1159 if (state->path.active == 1) {
1161 * Its really a single leaf node, but it had
1162 * out-of-line values so it looked like it *might*
1163 * have been a b-tree.
1165 xfs_da_state_free(state);
1167 error = xfs_attr3_leaf_to_node(args);
1172 * Now that we have converted the leaf to a node, we can
1173 * roll the transaction, and try xfs_attr3_leaf_add
1174 * again on re-entry. No need to set dela_state to do
1175 * this. dela_state is still unset by this function at
1178 dac->flags |= XFS_DAC_DEFER_FINISH;
1183 * Split as many Btree elements as required.
1184 * This code tracks the new and old attr's location
1185 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1186 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1188 error = xfs_da3_split(state);
1191 dac->flags |= XFS_DAC_DEFER_FINISH;
1194 * Addition succeeded, update Btree hashvals.
1196 xfs_da3_fixhashpath(state, &state->path);
1201 xfs_da_state_free(state);
1207 xfs_attr_node_addname_clear_incomplete(
1208 struct xfs_delattr_context *dac)
1210 struct xfs_da_args *args = dac->da_args;
1211 struct xfs_da_state *state = NULL;
1216 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1217 * flag means that we will find the "old" attr, not the "new" one.
1219 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1220 state = xfs_da_state_alloc(args);
1222 error = xfs_da3_node_lookup_int(state, &retval);
1226 error = xfs_attr_node_removename(args, state);
1229 * Check to see if the tree needs to be collapsed.
1231 if (retval && (state->path.active > 1)) {
1232 error = xfs_da3_join(state);
1240 xfs_da_state_free(state);
1247 * Shrink an attribute from leaf to shortform
1250 xfs_attr_node_shrink(
1251 struct xfs_da_args *args,
1252 struct xfs_da_state *state)
1254 struct xfs_inode *dp = args->dp;
1259 * Have to get rid of the copy of this dabuf in the state.
1261 ASSERT(state->path.active == 1);
1262 ASSERT(state->path.blk[0].bp);
1263 state->path.blk[0].bp = NULL;
1265 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1269 forkoff = xfs_attr_shortform_allfit(bp, dp);
1271 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1272 /* bp is gone due to xfs_da_shrink_inode */
1274 xfs_trans_brelse(args->trans, bp);
1280 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1281 * for later deletion of the entry.
1284 xfs_attr_leaf_mark_incomplete(
1285 struct xfs_da_args *args,
1286 struct xfs_da_state *state)
1291 * Fill in disk block numbers in the state structure
1292 * so that we can get the buffers back after we commit
1293 * several transactions in the following calls.
1295 error = xfs_attr_fillstate(state);
1300 * Mark the attribute as INCOMPLETE
1302 return xfs_attr3_leaf_setflag(args);
1306 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1307 * the blocks are valid. Attr keys with remote blocks will be marked
1311 int xfs_attr_node_removename_setup(
1312 struct xfs_delattr_context *dac)
1314 struct xfs_da_args *args = dac->da_args;
1315 struct xfs_da_state **state = &dac->da_state;
1318 error = xfs_attr_node_hasname(args, state);
1319 if (error != -EEXIST)
1323 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1324 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1325 XFS_ATTR_LEAF_MAGIC);
1327 if (args->rmtblkno > 0) {
1328 error = xfs_attr_leaf_mark_incomplete(args, *state);
1332 error = xfs_attr_rmtval_invalidate(args);
1336 xfs_da_state_free(*state);
1342 xfs_attr_node_removename(
1343 struct xfs_da_args *args,
1344 struct xfs_da_state *state)
1346 struct xfs_da_state_blk *blk;
1350 * Remove the name and update the hashvals in the tree.
1352 blk = &state->path.blk[state->path.active-1];
1353 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1354 retval = xfs_attr3_leaf_remove(blk->bp, args);
1355 xfs_da3_fixhashpath(state, &state->path);
1361 * Remove the attribute specified in @args.
1363 * This will involve walking down the Btree, and may involve joining
1364 * leaf nodes and even joining intermediate nodes up to and including
1365 * the root node (a special case of an intermediate node).
1367 * This routine is meant to function as either an in-line or delayed operation,
1368 * and may return -EAGAIN when the transaction needs to be rolled. Calling
1369 * functions will need to handle this, and call the function until a
1370 * successful error code is returned.
1373 xfs_attr_remove_iter(
1374 struct xfs_delattr_context *dac)
1376 struct xfs_da_args *args = dac->da_args;
1377 struct xfs_da_state *state = dac->da_state;
1378 int retval, error = 0;
1379 struct xfs_inode *dp = args->dp;
1381 trace_xfs_attr_node_removename(args);
1383 switch (dac->dela_state) {
1384 case XFS_DAS_UNINIT:
1385 if (!xfs_inode_hasattr(dp))
1389 * Shortform or leaf formats don't require transaction rolls and
1390 * thus state transitions. Call the right helper and return.
1392 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
1393 return xfs_attr_sf_removename(args);
1395 if (xfs_attr_is_leaf(dp))
1396 return xfs_attr_leaf_removename(args);
1399 * Node format may require transaction rolls. Set up the
1400 * state context and fall into the state machine.
1402 if (!dac->da_state) {
1403 error = xfs_attr_node_removename_setup(dac);
1406 state = dac->da_state;
1410 case XFS_DAS_RMTBLK:
1411 dac->dela_state = XFS_DAS_RMTBLK;
1414 * If there is an out-of-line value, de-allocate the blocks.
1415 * This is done before we remove the attribute so that we don't
1416 * overflow the maximum size of a transaction and/or hit a
1419 if (args->rmtblkno > 0) {
1421 * May return -EAGAIN. Roll and repeat until all remote
1422 * blocks are removed.
1424 error = __xfs_attr_rmtval_remove(dac);
1425 if (error == -EAGAIN)
1431 * Refill the state structure with buffers (the prior
1432 * calls released our buffers) and close out this
1433 * transaction before proceeding.
1435 ASSERT(args->rmtblkno == 0);
1436 error = xfs_attr_refillstate(state);
1439 dac->dela_state = XFS_DAS_RM_NAME;
1440 dac->flags |= XFS_DAC_DEFER_FINISH;
1445 case XFS_DAS_RM_NAME:
1447 * If we came here fresh from a transaction roll, reattach all
1448 * the buffers to the current transaction.
1450 if (dac->dela_state == XFS_DAS_RM_NAME) {
1451 error = xfs_attr_refillstate(state);
1456 retval = xfs_attr_node_removename(args, state);
1459 * Check to see if the tree needs to be collapsed. If so, roll
1460 * the transacton and fall into the shrink state.
1462 if (retval && (state->path.active > 1)) {
1463 error = xfs_da3_join(state);
1467 dac->flags |= XFS_DAC_DEFER_FINISH;
1468 dac->dela_state = XFS_DAS_RM_SHRINK;
1473 case XFS_DAS_RM_SHRINK:
1475 * If the result is small enough, push it all into the inode.
1476 * This is our final state so it's safe to return a dirty
1479 if (xfs_attr_is_leaf(dp))
1480 error = xfs_attr_node_shrink(args, state);
1481 ASSERT(error != -EAGAIN);
1490 xfs_da_state_free(state);
1495 * Fill in the disk block numbers in the state structure for the buffers
1496 * that are attached to the state structure.
1497 * This is done so that we can quickly reattach ourselves to those buffers
1498 * after some set of transaction commits have released these buffers.
1501 xfs_attr_fillstate(xfs_da_state_t *state)
1503 xfs_da_state_path_t *path;
1504 xfs_da_state_blk_t *blk;
1507 trace_xfs_attr_fillstate(state->args);
1510 * Roll down the "path" in the state structure, storing the on-disk
1511 * block number for those buffers in the "path".
1513 path = &state->path;
1514 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1515 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1517 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1520 blk->disk_blkno = 0;
1525 * Roll down the "altpath" in the state structure, storing the on-disk
1526 * block number for those buffers in the "altpath".
1528 path = &state->altpath;
1529 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1530 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1532 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1535 blk->disk_blkno = 0;
1543 * Reattach the buffers to the state structure based on the disk block
1544 * numbers stored in the state structure.
1545 * This is done after some set of transaction commits have released those
1546 * buffers from our grip.
1549 xfs_attr_refillstate(xfs_da_state_t *state)
1551 xfs_da_state_path_t *path;
1552 xfs_da_state_blk_t *blk;
1555 trace_xfs_attr_refillstate(state->args);
1558 * Roll down the "path" in the state structure, storing the on-disk
1559 * block number for those buffers in the "path".
1561 path = &state->path;
1562 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1563 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1564 if (blk->disk_blkno) {
1565 error = xfs_da3_node_read_mapped(state->args->trans,
1566 state->args->dp, blk->disk_blkno,
1567 &blk->bp, XFS_ATTR_FORK);
1576 * Roll down the "altpath" in the state structure, storing the on-disk
1577 * block number for those buffers in the "altpath".
1579 path = &state->altpath;
1580 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1581 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1582 if (blk->disk_blkno) {
1583 error = xfs_da3_node_read_mapped(state->args->trans,
1584 state->args->dp, blk->disk_blkno,
1585 &blk->bp, XFS_ATTR_FORK);
1597 * Retrieve the attribute data from a node attribute list.
1599 * This routine gets called for any attribute fork that has more than one
1600 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1601 * "remote" values taking up more blocks.
1603 * Returns 0 on successful retrieval, otherwise an error.
1607 struct xfs_da_args *args)
1609 struct xfs_da_state *state;
1610 struct xfs_da_state_blk *blk;
1614 trace_xfs_attr_node_get(args);
1617 * Search to see if name exists, and get back a pointer to it.
1619 error = xfs_attr_node_hasname(args, &state);
1620 if (error != -EEXIST)
1624 * Get the value, local or "remote"
1626 blk = &state->path.blk[state->path.active - 1];
1627 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1630 * If not in a transaction, we have to release all the buffers.
1633 for (i = 0; state != NULL && i < state->path.active; i++) {
1634 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1635 state->path.blk[i].bp = NULL;
1639 xfs_da_state_free(state);
1643 /* Returns true if the attribute entry name is valid. */
1650 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1651 * out, so use >= for the length check.
1653 if (length >= MAXNAMELEN)
1656 /* There shouldn't be any nulls here */
1657 return !memchr(name, 0, length);