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_is_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 (xfs_has_wsync(dp->i_mount))
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 trace_xfs_attr_sf_addname_return(XFS_DAS_UNINIT, args->dp);
339 dac->flags |= XFS_DAC_DEFER_FINISH;
344 * Set the attribute specified in @args.
345 * This routine is meant to function as a delayed operation, and may return
346 * -EAGAIN when the transaction needs to be rolled. Calling functions will need
347 * to handle this, and recall the function until a successful error code is
352 struct xfs_delattr_context *dac,
353 struct xfs_buf **leaf_bp)
355 struct xfs_da_args *args = dac->da_args;
356 struct xfs_inode *dp = args->dp;
357 struct xfs_buf *bp = NULL;
358 int forkoff, error = 0;
360 /* State machine switch */
361 switch (dac->dela_state) {
364 * If the fork is shortform, attempt to add the attr. If there
365 * is no space, this converts to leaf format and returns
366 * -EAGAIN with the leaf buffer held across the roll. The caller
367 * will deal with a transaction roll error, but otherwise
368 * release the hold once we return with a clean transaction.
370 if (xfs_attr_is_shortform(dp))
371 return xfs_attr_sf_addname(dac, leaf_bp);
372 if (*leaf_bp != NULL) {
373 xfs_trans_bhold_release(args->trans, *leaf_bp);
377 if (xfs_attr_is_leaf(dp)) {
378 error = xfs_attr_leaf_try_add(args, *leaf_bp);
379 if (error == -ENOSPC) {
380 error = xfs_attr3_leaf_to_node(args);
385 * Finish any deferred work items and roll the
386 * transaction once more. The goal here is to
387 * call node_addname with the inode and
388 * transaction in the same state (inode locked
389 * and joined, transaction clean) no matter how
390 * we got to this step.
392 * At this point, we are still in
393 * XFS_DAS_UNINIT, but when we come back, we'll
394 * be a node, so we'll fall down into the node
395 * handling code below
397 dac->flags |= XFS_DAC_DEFER_FINISH;
398 trace_xfs_attr_set_iter_return(
399 dac->dela_state, args->dp);
405 dac->dela_state = XFS_DAS_FOUND_LBLK;
407 error = xfs_attr_node_addname_find_attr(dac);
411 error = xfs_attr_node_addname(dac);
415 dac->dela_state = XFS_DAS_FOUND_NBLK;
417 trace_xfs_attr_set_iter_return(dac->dela_state, args->dp);
419 case XFS_DAS_FOUND_LBLK:
421 * If there was an out-of-line value, allocate the blocks we
422 * identified for its storage and copy the value. This is done
423 * after we create the attribute so that we don't overflow the
424 * maximum size of a transaction and/or hit a deadlock.
427 /* Open coded xfs_attr_rmtval_set without trans handling */
428 if ((dac->flags & XFS_DAC_LEAF_ADDNAME_INIT) == 0) {
429 dac->flags |= XFS_DAC_LEAF_ADDNAME_INIT;
430 if (args->rmtblkno > 0) {
431 error = xfs_attr_rmtval_find_space(dac);
438 * Repeat allocating remote blocks for the attr value until
439 * blkcnt drops to zero.
441 if (dac->blkcnt > 0) {
442 error = xfs_attr_rmtval_set_blk(dac);
445 trace_xfs_attr_set_iter_return(dac->dela_state,
450 error = xfs_attr_rmtval_set_value(args);
455 * If this is not a rename, clear the incomplete flag and we're
458 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
459 if (args->rmtblkno > 0)
460 error = xfs_attr3_leaf_clearflag(args);
465 * If this is an atomic rename operation, we must "flip" the
466 * incomplete flags on the "new" and "old" attribute/value pairs
467 * so that one disappears and one appears atomically. Then we
468 * must remove the "old" attribute/value pair.
470 * In a separate transaction, set the incomplete flag on the
471 * "old" attr and clear the incomplete flag on the "new" attr.
473 error = xfs_attr3_leaf_flipflags(args);
477 * Commit the flag value change and start the next trans in
480 dac->dela_state = XFS_DAS_FLIP_LFLAG;
481 trace_xfs_attr_set_iter_return(dac->dela_state, args->dp);
483 case XFS_DAS_FLIP_LFLAG:
485 * Dismantle the "old" attribute/value pair by removing a
486 * "remote" value (if it exists).
488 xfs_attr_restore_rmt_blk(args);
489 error = xfs_attr_rmtval_invalidate(args);
494 case XFS_DAS_RM_LBLK:
495 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
496 dac->dela_state = XFS_DAS_RM_LBLK;
497 if (args->rmtblkno) {
498 error = xfs_attr_rmtval_remove(dac);
499 if (error == -EAGAIN)
500 trace_xfs_attr_set_iter_return(
501 dac->dela_state, args->dp);
505 dac->dela_state = XFS_DAS_RD_LEAF;
506 trace_xfs_attr_set_iter_return(dac->dela_state, args->dp);
511 case XFS_DAS_RD_LEAF:
513 * This is the last step for leaf format. Read the block with
514 * the old attr, remove the old attr, check for shortform
515 * conversion and return.
517 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
522 xfs_attr3_leaf_remove(bp, args);
524 forkoff = xfs_attr_shortform_allfit(bp, dp);
526 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
527 /* bp is gone due to xfs_da_shrink_inode */
531 case XFS_DAS_FOUND_NBLK:
533 * Find space for remote blocks and fall into the allocation
536 if (args->rmtblkno > 0) {
537 error = xfs_attr_rmtval_find_space(dac);
543 case XFS_DAS_ALLOC_NODE:
545 * If there was an out-of-line value, allocate the blocks we
546 * identified for its storage and copy the value. This is done
547 * after we create the attribute so that we don't overflow the
548 * maximum size of a transaction and/or hit a deadlock.
550 dac->dela_state = XFS_DAS_ALLOC_NODE;
551 if (args->rmtblkno > 0) {
552 if (dac->blkcnt > 0) {
553 error = xfs_attr_rmtval_set_blk(dac);
556 trace_xfs_attr_set_iter_return(
557 dac->dela_state, args->dp);
561 error = xfs_attr_rmtval_set_value(args);
567 * If this was not a rename, clear the incomplete flag and we're
570 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
571 if (args->rmtblkno > 0)
572 error = xfs_attr3_leaf_clearflag(args);
577 * If this is an atomic rename operation, we must "flip" the
578 * incomplete flags on the "new" and "old" attribute/value pairs
579 * so that one disappears and one appears atomically. Then we
580 * must remove the "old" attribute/value pair.
582 * In a separate transaction, set the incomplete flag on the
583 * "old" attr and clear the incomplete flag on the "new" attr.
585 error = xfs_attr3_leaf_flipflags(args);
589 * Commit the flag value change and start the next trans in
592 dac->dela_state = XFS_DAS_FLIP_NFLAG;
593 trace_xfs_attr_set_iter_return(dac->dela_state, args->dp);
596 case XFS_DAS_FLIP_NFLAG:
598 * Dismantle the "old" attribute/value pair by removing a
599 * "remote" value (if it exists).
601 xfs_attr_restore_rmt_blk(args);
603 error = xfs_attr_rmtval_invalidate(args);
608 case XFS_DAS_RM_NBLK:
609 /* Set state in case xfs_attr_rmtval_remove returns -EAGAIN */
610 dac->dela_state = XFS_DAS_RM_NBLK;
611 if (args->rmtblkno) {
612 error = xfs_attr_rmtval_remove(dac);
613 if (error == -EAGAIN)
614 trace_xfs_attr_set_iter_return(
615 dac->dela_state, args->dp);
620 dac->dela_state = XFS_DAS_CLR_FLAG;
621 trace_xfs_attr_set_iter_return(dac->dela_state, args->dp);
626 case XFS_DAS_CLR_FLAG:
628 * The last state for node format. Look up the old attr and
631 error = xfs_attr_node_addname_clear_incomplete(dac);
643 * Return EEXIST if attr is found, or ENOATTR if not
647 struct xfs_da_args *args)
649 struct xfs_inode *dp = args->dp;
650 struct xfs_buf *bp = NULL;
653 if (!xfs_inode_hasattr(dp))
656 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
657 return xfs_attr_sf_findname(args, NULL, NULL);
659 if (xfs_attr_is_leaf(dp)) {
660 error = xfs_attr_leaf_hasname(args, &bp);
663 xfs_trans_brelse(args->trans, bp);
668 return xfs_attr_node_hasname(args, NULL);
672 * Remove the attribute specified in @args.
675 xfs_attr_remove_args(
676 struct xfs_da_args *args)
679 struct xfs_delattr_context dac = {
684 error = xfs_attr_remove_iter(&dac);
685 if (error != -EAGAIN)
688 error = xfs_attr_trans_roll(&dac);
698 * Note: If args->value is NULL the attribute will be removed, just like the
699 * Linux ->setattr API.
703 struct xfs_da_args *args)
705 struct xfs_inode *dp = args->dp;
706 struct xfs_mount *mp = dp->i_mount;
707 struct xfs_trans_res tres;
708 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
713 if (xfs_is_shutdown(dp->i_mount))
716 error = xfs_qm_dqattach(dp);
720 args->geo = mp->m_attr_geo;
721 args->whichfork = XFS_ATTR_FORK;
722 args->hashval = xfs_da_hashname(args->name, args->namelen);
725 * We have no control over the attribute names that userspace passes us
726 * to remove, so we have to allow the name lookup prior to attribute
727 * removal to fail as well.
729 args->op_flags = XFS_DA_OP_OKNOENT;
732 XFS_STATS_INC(mp, xs_attr_set);
734 args->op_flags |= XFS_DA_OP_ADDNAME;
735 args->total = xfs_attr_calc_size(args, &local);
738 * If the inode doesn't have an attribute fork, add one.
739 * (inode must not be locked when we call this routine)
741 if (XFS_IFORK_Q(dp) == 0) {
742 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
743 xfs_attr_sf_entsize_byname(args->namelen,
746 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
751 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
752 M_RES(mp)->tr_attrsetrt.tr_logres *
754 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
755 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
759 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
761 XFS_STATS_INC(mp, xs_attr_remove);
763 tres = M_RES(mp)->tr_attrrm;
764 total = XFS_ATTRRM_SPACE_RES(mp);
765 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
769 * Root fork attributes can use reserved data blocks for this
770 * operation if necessary
772 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
776 if (args->value || xfs_inode_hasattr(dp)) {
777 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
778 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
780 goto out_trans_cancel;
783 error = xfs_attr_lookup(args);
785 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
786 goto out_trans_cancel;
787 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
788 goto out_trans_cancel;
789 if (error != -ENOATTR && error != -EEXIST)
790 goto out_trans_cancel;
792 error = xfs_attr_set_args(args);
794 goto out_trans_cancel;
795 /* shortform attribute has already been committed */
799 if (error != -EEXIST)
800 goto out_trans_cancel;
802 error = xfs_attr_remove_args(args);
804 goto out_trans_cancel;
808 * If this is a synchronous mount, make sure that the
809 * transaction goes to disk before returning to the user.
811 if (xfs_has_wsync(mp))
812 xfs_trans_set_sync(args->trans);
814 if (!(args->op_flags & XFS_DA_OP_NOTIME))
815 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
818 * Commit the last in the sequence of transactions.
820 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
821 error = xfs_trans_commit(args->trans);
823 xfs_iunlock(dp, XFS_ILOCK_EXCL);
828 xfs_trans_cancel(args->trans);
832 /*========================================================================
833 * External routines when attribute list is inside the inode
834 *========================================================================*/
836 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
838 struct xfs_attr_shortform *sf;
840 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
841 return be16_to_cpu(sf->hdr.totsize);
845 * Add a name to the shortform attribute list structure
846 * This is the external routine.
849 xfs_attr_shortform_addname(xfs_da_args_t *args)
851 int newsize, forkoff, retval;
853 trace_xfs_attr_sf_addname(args);
855 retval = xfs_attr_shortform_lookup(args);
856 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
858 if (retval == -EEXIST) {
859 if (args->attr_flags & XATTR_CREATE)
861 retval = xfs_attr_sf_removename(args);
865 * Since we have removed the old attr, clear ATTR_REPLACE so
866 * that the leaf format add routine won't trip over the attr
869 args->attr_flags &= ~XATTR_REPLACE;
872 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
873 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
876 newsize = xfs_attr_sf_totsize(args->dp);
877 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
879 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
883 xfs_attr_shortform_add(args, forkoff);
888 /*========================================================================
889 * External routines when attribute list is one block
890 *========================================================================*/
892 /* Store info about a remote block */
894 xfs_attr_save_rmt_blk(
895 struct xfs_da_args *args)
897 args->blkno2 = args->blkno;
898 args->index2 = args->index;
899 args->rmtblkno2 = args->rmtblkno;
900 args->rmtblkcnt2 = args->rmtblkcnt;
901 args->rmtvaluelen2 = args->rmtvaluelen;
904 /* Set stored info about a remote block */
906 xfs_attr_restore_rmt_blk(
907 struct xfs_da_args *args)
909 args->blkno = args->blkno2;
910 args->index = args->index2;
911 args->rmtblkno = args->rmtblkno2;
912 args->rmtblkcnt = args->rmtblkcnt2;
913 args->rmtvaluelen = args->rmtvaluelen2;
917 * Tries to add an attribute to an inode in leaf form
919 * This function is meant to execute as part of a delayed operation and leaves
920 * the transaction handling to the caller. On success the attribute is added
921 * and the inode and transaction are left dirty. If there is not enough space,
922 * the attr data is converted to node format and -ENOSPC is returned. Caller is
923 * responsible for handling the dirty inode and transaction or adding the attr
927 xfs_attr_leaf_try_add(
928 struct xfs_da_args *args,
934 * Look up the given attribute in the leaf block. Figure out if
935 * the given flags produce an error or call for an atomic rename.
937 retval = xfs_attr_leaf_hasname(args, &bp);
938 if (retval != -ENOATTR && retval != -EEXIST)
940 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
942 if (retval == -EEXIST) {
943 if (args->attr_flags & XATTR_CREATE)
946 trace_xfs_attr_leaf_replace(args);
948 /* save the attribute state for later removal*/
949 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
950 xfs_attr_save_rmt_blk(args);
953 * clear the remote attr state now that it is saved so that the
954 * values reflect the state of the attribute we are about to
955 * add, not the attribute we just found and will remove later.
959 args->rmtvaluelen = 0;
963 * Add the attribute to the leaf block
965 return xfs_attr3_leaf_add(bp, args);
968 xfs_trans_brelse(args->trans, bp);
973 * Return EEXIST if attr is found, or ENOATTR if not
976 xfs_attr_leaf_hasname(
977 struct xfs_da_args *args,
982 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
986 error = xfs_attr3_leaf_lookup_int(*bp, args);
987 if (error != -ENOATTR && error != -EEXIST)
988 xfs_trans_brelse(args->trans, *bp);
994 * Remove a name from the leaf attribute list structure
996 * This leaf block cannot have a "remote" value, we only call this routine
997 * if bmap_one_block() says there is only one block (ie: no remote blks).
1000 xfs_attr_leaf_removename(
1001 struct xfs_da_args *args)
1003 struct xfs_inode *dp;
1007 trace_xfs_attr_leaf_removename(args);
1010 * Remove the attribute.
1014 error = xfs_attr_leaf_hasname(args, &bp);
1016 if (error == -ENOATTR) {
1017 xfs_trans_brelse(args->trans, bp);
1019 } else if (error != -EEXIST)
1022 xfs_attr3_leaf_remove(bp, args);
1025 * If the result is small enough, shrink it all into the inode.
1027 forkoff = xfs_attr_shortform_allfit(bp, dp);
1029 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1030 /* bp is gone due to xfs_da_shrink_inode */
1036 * Look up a name in a leaf attribute list structure.
1038 * This leaf block cannot have a "remote" value, we only call this routine
1039 * if bmap_one_block() says there is only one block (ie: no remote blks).
1041 * Returns 0 on successful retrieval, otherwise an error.
1044 xfs_attr_leaf_get(xfs_da_args_t *args)
1049 trace_xfs_attr_leaf_get(args);
1051 error = xfs_attr_leaf_hasname(args, &bp);
1053 if (error == -ENOATTR) {
1054 xfs_trans_brelse(args->trans, bp);
1056 } else if (error != -EEXIST)
1060 error = xfs_attr3_leaf_getvalue(bp, args);
1061 xfs_trans_brelse(args->trans, bp);
1066 * Return EEXIST if attr is found, or ENOATTR if not
1067 * statep: If not null is set to point at the found state. Caller will
1068 * be responsible for freeing the state in this case.
1071 xfs_attr_node_hasname(
1072 struct xfs_da_args *args,
1073 struct xfs_da_state **statep)
1075 struct xfs_da_state *state;
1078 state = xfs_da_state_alloc(args);
1083 * Search to see if name exists, and get back a pointer to it.
1085 error = xfs_da3_node_lookup_int(state, &retval);
1090 xfs_da_state_free(state);
1095 /*========================================================================
1096 * External routines when attribute list size > geo->blksize
1097 *========================================================================*/
1100 xfs_attr_node_addname_find_attr(
1101 struct xfs_delattr_context *dac)
1103 struct xfs_da_args *args = dac->da_args;
1107 * Search to see if name already exists, and get back a pointer
1108 * to where it should go.
1110 retval = xfs_attr_node_hasname(args, &dac->da_state);
1111 if (retval != -ENOATTR && retval != -EEXIST)
1114 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
1116 if (retval == -EEXIST) {
1117 if (args->attr_flags & XATTR_CREATE)
1120 trace_xfs_attr_node_replace(args);
1122 /* save the attribute state for later removal*/
1123 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1124 xfs_attr_save_rmt_blk(args);
1127 * clear the remote attr state now that it is saved so that the
1128 * values reflect the state of the attribute we are about to
1129 * add, not the attribute we just found and will remove later.
1132 args->rmtblkcnt = 0;
1133 args->rmtvaluelen = 0;
1139 xfs_da_state_free(dac->da_state);
1144 * Add a name to a Btree-format attribute list.
1146 * This will involve walking down the Btree, and may involve splitting
1147 * leaf nodes and even splitting intermediate nodes up to and including
1148 * the root node (a special case of an intermediate node).
1150 * "Remote" attribute values confuse the issue and atomic rename operations
1151 * add a whole extra layer of confusion on top of that.
1153 * This routine is meant to function as a delayed operation, and may return
1154 * -EAGAIN when the transaction needs to be rolled. Calling functions will need
1155 * to handle this, and recall the function until a successful error code is
1159 xfs_attr_node_addname(
1160 struct xfs_delattr_context *dac)
1162 struct xfs_da_args *args = dac->da_args;
1163 struct xfs_da_state *state = dac->da_state;
1164 struct xfs_da_state_blk *blk;
1167 trace_xfs_attr_node_addname(args);
1169 blk = &state->path.blk[state->path.active-1];
1170 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1172 error = xfs_attr3_leaf_add(blk->bp, state->args);
1173 if (error == -ENOSPC) {
1174 if (state->path.active == 1) {
1176 * Its really a single leaf node, but it had
1177 * out-of-line values so it looked like it *might*
1178 * have been a b-tree.
1180 xfs_da_state_free(state);
1182 error = xfs_attr3_leaf_to_node(args);
1187 * Now that we have converted the leaf to a node, we can
1188 * roll the transaction, and try xfs_attr3_leaf_add
1189 * again on re-entry. No need to set dela_state to do
1190 * this. dela_state is still unset by this function at
1193 dac->flags |= XFS_DAC_DEFER_FINISH;
1194 trace_xfs_attr_node_addname_return(
1195 dac->dela_state, args->dp);
1200 * Split as many Btree elements as required.
1201 * This code tracks the new and old attr's location
1202 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1203 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1205 error = xfs_da3_split(state);
1208 dac->flags |= XFS_DAC_DEFER_FINISH;
1211 * Addition succeeded, update Btree hashvals.
1213 xfs_da3_fixhashpath(state, &state->path);
1218 xfs_da_state_free(state);
1224 xfs_attr_node_addname_clear_incomplete(
1225 struct xfs_delattr_context *dac)
1227 struct xfs_da_args *args = dac->da_args;
1228 struct xfs_da_state *state = NULL;
1233 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1234 * flag means that we will find the "old" attr, not the "new" one.
1236 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1237 state = xfs_da_state_alloc(args);
1239 error = xfs_da3_node_lookup_int(state, &retval);
1243 error = xfs_attr_node_removename(args, state);
1246 * Check to see if the tree needs to be collapsed.
1248 if (retval && (state->path.active > 1)) {
1249 error = xfs_da3_join(state);
1257 xfs_da_state_free(state);
1264 * Shrink an attribute from leaf to shortform
1267 xfs_attr_node_shrink(
1268 struct xfs_da_args *args,
1269 struct xfs_da_state *state)
1271 struct xfs_inode *dp = args->dp;
1276 * Have to get rid of the copy of this dabuf in the state.
1278 ASSERT(state->path.active == 1);
1279 ASSERT(state->path.blk[0].bp);
1280 state->path.blk[0].bp = NULL;
1282 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1286 forkoff = xfs_attr_shortform_allfit(bp, dp);
1288 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1289 /* bp is gone due to xfs_da_shrink_inode */
1291 xfs_trans_brelse(args->trans, bp);
1297 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1298 * for later deletion of the entry.
1301 xfs_attr_leaf_mark_incomplete(
1302 struct xfs_da_args *args,
1303 struct xfs_da_state *state)
1308 * Fill in disk block numbers in the state structure
1309 * so that we can get the buffers back after we commit
1310 * several transactions in the following calls.
1312 error = xfs_attr_fillstate(state);
1317 * Mark the attribute as INCOMPLETE
1319 return xfs_attr3_leaf_setflag(args);
1323 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1324 * the blocks are valid. Attr keys with remote blocks will be marked
1328 int xfs_attr_node_removename_setup(
1329 struct xfs_delattr_context *dac)
1331 struct xfs_da_args *args = dac->da_args;
1332 struct xfs_da_state **state = &dac->da_state;
1335 error = xfs_attr_node_hasname(args, state);
1336 if (error != -EEXIST)
1340 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1341 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1342 XFS_ATTR_LEAF_MAGIC);
1344 if (args->rmtblkno > 0) {
1345 error = xfs_attr_leaf_mark_incomplete(args, *state);
1349 error = xfs_attr_rmtval_invalidate(args);
1353 xfs_da_state_free(*state);
1359 xfs_attr_node_removename(
1360 struct xfs_da_args *args,
1361 struct xfs_da_state *state)
1363 struct xfs_da_state_blk *blk;
1367 * Remove the name and update the hashvals in the tree.
1369 blk = &state->path.blk[state->path.active-1];
1370 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1371 retval = xfs_attr3_leaf_remove(blk->bp, args);
1372 xfs_da3_fixhashpath(state, &state->path);
1378 * Remove the attribute specified in @args.
1380 * This will involve walking down the Btree, and may involve joining
1381 * leaf nodes and even joining intermediate nodes up to and including
1382 * the root node (a special case of an intermediate node).
1384 * This routine is meant to function as either an in-line or delayed operation,
1385 * and may return -EAGAIN when the transaction needs to be rolled. Calling
1386 * functions will need to handle this, and call the function until a
1387 * successful error code is returned.
1390 xfs_attr_remove_iter(
1391 struct xfs_delattr_context *dac)
1393 struct xfs_da_args *args = dac->da_args;
1394 struct xfs_da_state *state = dac->da_state;
1395 int retval, error = 0;
1396 struct xfs_inode *dp = args->dp;
1398 trace_xfs_attr_node_removename(args);
1400 switch (dac->dela_state) {
1401 case XFS_DAS_UNINIT:
1402 if (!xfs_inode_hasattr(dp))
1406 * Shortform or leaf formats don't require transaction rolls and
1407 * thus state transitions. Call the right helper and return.
1409 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
1410 return xfs_attr_sf_removename(args);
1412 if (xfs_attr_is_leaf(dp))
1413 return xfs_attr_leaf_removename(args);
1416 * Node format may require transaction rolls. Set up the
1417 * state context and fall into the state machine.
1419 if (!dac->da_state) {
1420 error = xfs_attr_node_removename_setup(dac);
1423 state = dac->da_state;
1427 case XFS_DAS_RMTBLK:
1428 dac->dela_state = XFS_DAS_RMTBLK;
1431 * If there is an out-of-line value, de-allocate the blocks.
1432 * This is done before we remove the attribute so that we don't
1433 * overflow the maximum size of a transaction and/or hit a
1436 if (args->rmtblkno > 0) {
1438 * May return -EAGAIN. Roll and repeat until all remote
1439 * blocks are removed.
1441 error = xfs_attr_rmtval_remove(dac);
1442 if (error == -EAGAIN) {
1443 trace_xfs_attr_remove_iter_return(
1444 dac->dela_state, args->dp);
1451 * Refill the state structure with buffers (the prior
1452 * calls released our buffers) and close out this
1453 * transaction before proceeding.
1455 ASSERT(args->rmtblkno == 0);
1456 error = xfs_attr_refillstate(state);
1459 dac->dela_state = XFS_DAS_RM_NAME;
1460 dac->flags |= XFS_DAC_DEFER_FINISH;
1461 trace_xfs_attr_remove_iter_return(dac->dela_state, args->dp);
1466 case XFS_DAS_RM_NAME:
1468 * If we came here fresh from a transaction roll, reattach all
1469 * the buffers to the current transaction.
1471 if (dac->dela_state == XFS_DAS_RM_NAME) {
1472 error = xfs_attr_refillstate(state);
1477 retval = xfs_attr_node_removename(args, state);
1480 * Check to see if the tree needs to be collapsed. If so, roll
1481 * the transacton and fall into the shrink state.
1483 if (retval && (state->path.active > 1)) {
1484 error = xfs_da3_join(state);
1488 dac->flags |= XFS_DAC_DEFER_FINISH;
1489 dac->dela_state = XFS_DAS_RM_SHRINK;
1490 trace_xfs_attr_remove_iter_return(
1491 dac->dela_state, args->dp);
1496 case XFS_DAS_RM_SHRINK:
1498 * If the result is small enough, push it all into the inode.
1499 * This is our final state so it's safe to return a dirty
1502 if (xfs_attr_is_leaf(dp))
1503 error = xfs_attr_node_shrink(args, state);
1504 ASSERT(error != -EAGAIN);
1513 xfs_da_state_free(state);
1518 * Fill in the disk block numbers in the state structure for the buffers
1519 * that are attached to the state structure.
1520 * This is done so that we can quickly reattach ourselves to those buffers
1521 * after some set of transaction commits have released these buffers.
1524 xfs_attr_fillstate(xfs_da_state_t *state)
1526 xfs_da_state_path_t *path;
1527 xfs_da_state_blk_t *blk;
1530 trace_xfs_attr_fillstate(state->args);
1533 * Roll down the "path" in the state structure, storing the on-disk
1534 * block number for those buffers in the "path".
1536 path = &state->path;
1537 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1538 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1540 blk->disk_blkno = xfs_buf_daddr(blk->bp);
1543 blk->disk_blkno = 0;
1548 * Roll down the "altpath" in the state structure, storing the on-disk
1549 * block number for those buffers in the "altpath".
1551 path = &state->altpath;
1552 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1553 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1555 blk->disk_blkno = xfs_buf_daddr(blk->bp);
1558 blk->disk_blkno = 0;
1566 * Reattach the buffers to the state structure based on the disk block
1567 * numbers stored in the state structure.
1568 * This is done after some set of transaction commits have released those
1569 * buffers from our grip.
1572 xfs_attr_refillstate(xfs_da_state_t *state)
1574 xfs_da_state_path_t *path;
1575 xfs_da_state_blk_t *blk;
1578 trace_xfs_attr_refillstate(state->args);
1581 * Roll down the "path" in the state structure, storing the on-disk
1582 * block number for those buffers in the "path".
1584 path = &state->path;
1585 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1586 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1587 if (blk->disk_blkno) {
1588 error = xfs_da3_node_read_mapped(state->args->trans,
1589 state->args->dp, blk->disk_blkno,
1590 &blk->bp, XFS_ATTR_FORK);
1599 * Roll down the "altpath" in the state structure, storing the on-disk
1600 * block number for those buffers in the "altpath".
1602 path = &state->altpath;
1603 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1604 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1605 if (blk->disk_blkno) {
1606 error = xfs_da3_node_read_mapped(state->args->trans,
1607 state->args->dp, blk->disk_blkno,
1608 &blk->bp, XFS_ATTR_FORK);
1620 * Retrieve the attribute data from a node attribute list.
1622 * This routine gets called for any attribute fork that has more than one
1623 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1624 * "remote" values taking up more blocks.
1626 * Returns 0 on successful retrieval, otherwise an error.
1630 struct xfs_da_args *args)
1632 struct xfs_da_state *state;
1633 struct xfs_da_state_blk *blk;
1637 trace_xfs_attr_node_get(args);
1640 * Search to see if name exists, and get back a pointer to it.
1642 error = xfs_attr_node_hasname(args, &state);
1643 if (error != -EEXIST)
1647 * Get the value, local or "remote"
1649 blk = &state->path.blk[state->path.active - 1];
1650 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1653 * If not in a transaction, we have to release all the buffers.
1656 for (i = 0; state != NULL && i < state->path.active; i++) {
1657 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1658 state->path.blk[i].bp = NULL;
1662 xfs_da_state_free(state);
1666 /* Returns true if the attribute entry name is valid. */
1673 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1674 * out, so use >= for the length check.
1676 if (length >= MAXNAMELEN)
1679 /* There shouldn't be any nulls here */
1680 return !memchr(name, 0, length);