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_da_args *args,
57 struct xfs_da_state *state);
58 STATIC int xfs_attr_node_addname_find_attr(struct xfs_da_args *args,
59 struct xfs_da_state **state);
60 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
61 STATIC int xfs_attr_node_addname_clear_incomplete(struct xfs_da_args *args);
62 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
63 struct xfs_da_state **state);
64 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
65 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
71 if (!XFS_IFORK_Q(ip) ||
72 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
73 ip->i_afp->if_nextents == 0))
79 * Returns true if the there is exactly only block in the attr fork, in which
80 * case the attribute fork consists of a single leaf block entry.
86 struct xfs_ifork *ifp = ip->i_afp;
87 struct xfs_iext_cursor icur;
88 struct xfs_bmbt_irec imap;
90 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
93 xfs_iext_first(ifp, &icur);
94 xfs_iext_get_extent(ifp, &icur, &imap);
95 return imap.br_startoff == 0 && imap.br_blockcount == 1;
98 /*========================================================================
99 * Overall external interface routines.
100 *========================================================================*/
103 * Retrieve an extended attribute and its value. Must have ilock.
104 * Returns 0 on successful retrieval, otherwise an error.
107 xfs_attr_get_ilocked(
108 struct xfs_da_args *args)
110 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
112 if (!xfs_inode_hasattr(args->dp))
115 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
116 return xfs_attr_shortform_getvalue(args);
117 if (xfs_attr_is_leaf(args->dp))
118 return xfs_attr_leaf_get(args);
119 return xfs_attr_node_get(args);
123 * Retrieve an extended attribute by name, and its value if requested.
125 * If args->valuelen is zero, then the caller does not want the value, just an
126 * indication whether the attribute exists and the size of the value if it
127 * exists. The size is returned in args.valuelen.
129 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
130 * for the value after existence of the attribute has been determined. The
131 * caller always has to free args->value if it is set, no matter if this
132 * function was successful or not.
134 * If the attribute is found, but exceeds the size limit set by the caller in
135 * args->valuelen, return -ERANGE with the size of the attribute that was found
140 struct xfs_da_args *args)
145 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
147 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
150 args->geo = args->dp->i_mount->m_attr_geo;
151 args->whichfork = XFS_ATTR_FORK;
152 args->hashval = xfs_da_hashname(args->name, args->namelen);
154 /* Entirely possible to look up a name which doesn't exist */
155 args->op_flags = XFS_DA_OP_OKNOENT;
157 lock_mode = xfs_ilock_attr_map_shared(args->dp);
158 error = xfs_attr_get_ilocked(args);
159 xfs_iunlock(args->dp, lock_mode);
165 * Calculate how many blocks we need for the new attribute,
169 struct xfs_da_args *args,
172 struct xfs_mount *mp = args->dp->i_mount;
177 * Determine space new attribute will use, and if it would be
178 * "local" or "remote" (note: local != inline).
180 size = xfs_attr_leaf_newentsize(args, local);
181 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
183 if (size > (args->geo->blksize / 2)) {
184 /* Double split possible */
189 * Out of line attribute, cannot double split, but
190 * make room for the attribute value itself.
192 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
194 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
201 xfs_attr_try_sf_addname(
202 struct xfs_inode *dp,
203 struct xfs_da_args *args)
209 * Build initial attribute list (if required).
211 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
212 xfs_attr_shortform_create(args);
214 error = xfs_attr_shortform_addname(args);
215 if (error == -ENOSPC)
219 * Commit the shortform mods, and we're done.
220 * NOTE: this is also the error path (EEXIST, etc).
222 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
223 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
225 if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
226 xfs_trans_set_sync(args->trans);
232 * Check to see if the attr should be upgraded from non-existent or shortform to
233 * single-leaf-block attribute list.
236 xfs_attr_is_shortform(
237 struct xfs_inode *ip)
239 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
240 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
241 ip->i_afp->if_nextents == 0);
246 struct xfs_da_args *args)
248 struct xfs_buf *leaf_bp = NULL;
249 struct xfs_inode *dp = args->dp;
250 int error, error2 = 0;
253 * Try to add the attr to the attribute list in the inode.
255 error = xfs_attr_try_sf_addname(dp, args);
256 if (error != -ENOSPC) {
257 error2 = xfs_trans_commit(args->trans);
259 return error ? error : error2;
263 * It won't fit in the shortform, transform to a leaf block. GROT:
264 * another possible req'mt for a double-split btree op.
266 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
271 * Prevent the leaf buffer from being unlocked so that a concurrent AIL
272 * push cannot grab the half-baked leaf buffer and run into problems
273 * with the write verifier.
275 xfs_trans_bhold(args->trans, leaf_bp);
276 error = xfs_defer_finish(&args->trans);
277 xfs_trans_bhold_release(args->trans, leaf_bp);
279 xfs_trans_brelse(args->trans, leaf_bp);
287 * Set the attribute specified in @args.
291 struct xfs_da_args *args)
293 struct xfs_inode *dp = args->dp;
294 struct xfs_buf *bp = NULL;
295 struct xfs_da_state *state = NULL;
296 int forkoff, error = 0;
299 * If the attribute list is already in leaf format, jump straight to
300 * leaf handling. Otherwise, try to add the attribute to the shortform
301 * list; if there's no room then convert the list to leaf format and try
304 if (xfs_attr_is_shortform(dp)) {
305 error = xfs_attr_set_fmt(args);
306 if (error != -EAGAIN)
310 if (xfs_attr_is_leaf(dp)) {
311 error = xfs_attr_leaf_try_add(args, bp);
312 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);
344 * Commit the transaction that added the attr name so that
345 * later routines can manage their own transactions.
347 error = xfs_trans_roll_inode(&args->trans, dp);
352 * If there was an out-of-line value, allocate the blocks we
353 * identified for its storage and copy the value. This is done
354 * after we create the attribute so that we don't overflow the
355 * maximum size of a transaction and/or hit a deadlock.
357 if (args->rmtblkno > 0) {
358 error = xfs_attr_rmtval_set(args);
363 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
365 * Added a "remote" value, just clear the incomplete
368 if (args->rmtblkno > 0)
369 error = xfs_attr3_leaf_clearflag(args);
375 * If this is an atomic rename operation, we must "flip" the
376 * incomplete flags on the "new" and "old" attribute/value pairs
377 * so that one disappears and one appears atomically. Then we
378 * must remove the "old" attribute/value pair.
380 * In a separate transaction, set the incomplete flag on the
381 * "old" attr and clear the incomplete flag on the "new" attr.
384 error = xfs_attr3_leaf_flipflags(args);
388 * Commit the flag value change and start the next trans in
391 error = xfs_trans_roll_inode(&args->trans, args->dp);
396 * Dismantle the "old" attribute/value pair by removing a
397 * "remote" value (if it exists).
399 xfs_attr_restore_rmt_blk(args);
401 if (args->rmtblkno) {
402 error = xfs_attr_rmtval_invalidate(args);
406 error = xfs_attr_rmtval_remove(args);
412 * Read in the block containing the "old" attr, then remove the
413 * "old" attr from that block (neat, huh!)
415 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
420 xfs_attr3_leaf_remove(bp, args);
423 * If the result is small enough, shrink it all into the inode.
425 forkoff = xfs_attr_shortform_allfit(bp, dp);
427 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
428 /* bp is gone due to xfs_da_shrink_inode */
436 error = xfs_attr_node_addname_find_attr(args, &state);
439 error = xfs_attr_node_addname(args, state);
440 } while (error == -EAGAIN);
445 * Commit the leaf addition or btree split and start the next
446 * trans in the chain.
448 error = xfs_trans_roll_inode(&args->trans, dp);
453 * If there was an out-of-line value, allocate the blocks we
454 * identified for its storage and copy the value. This is done
455 * after we create the attribute so that we don't overflow the
456 * maximum size of a transaction and/or hit a deadlock.
458 if (args->rmtblkno > 0) {
459 error = xfs_attr_rmtval_set(args);
464 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
466 * Added a "remote" value, just clear the incomplete flag.
468 if (args->rmtblkno > 0)
469 error = xfs_attr3_leaf_clearflag(args);
474 * If this is an atomic rename operation, we must "flip" the incomplete
475 * flags on the "new" and "old" attribute/value pairs so that one
476 * disappears and one appears atomically. Then we must remove the "old"
477 * attribute/value pair.
479 * In a separate transaction, set the incomplete flag on the "old" attr
480 * and clear the incomplete flag on the "new" attr.
482 error = xfs_attr3_leaf_flipflags(args);
486 * Commit the flag value change and start the next trans in series
488 error = xfs_trans_roll_inode(&args->trans, args->dp);
493 * Dismantle the "old" attribute/value pair by removing a "remote" value
496 xfs_attr_restore_rmt_blk(args);
498 if (args->rmtblkno) {
499 error = xfs_attr_rmtval_invalidate(args);
503 error = xfs_attr_rmtval_remove(args);
508 error = xfs_attr_node_addname_clear_incomplete(args);
514 * Return EEXIST if attr is found, or ENOATTR if not
518 struct xfs_da_args *args)
520 struct xfs_inode *dp = args->dp;
521 struct xfs_buf *bp = NULL;
524 if (!xfs_inode_hasattr(dp))
527 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
528 return xfs_attr_sf_findname(args, NULL, NULL);
530 if (xfs_attr_is_leaf(dp)) {
531 error = xfs_attr_leaf_hasname(args, &bp);
534 xfs_trans_brelse(args->trans, bp);
539 return xfs_attr_node_hasname(args, NULL);
543 * Remove the attribute specified in @args.
546 xfs_attr_remove_args(
547 struct xfs_da_args *args)
549 if (!xfs_inode_hasattr(args->dp))
552 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
553 return xfs_attr_shortform_remove(args);
554 if (xfs_attr_is_leaf(args->dp))
555 return xfs_attr_leaf_removename(args);
556 return xfs_attr_node_removename(args);
560 * Note: If args->value is NULL the attribute will be removed, just like the
561 * Linux ->setattr API.
565 struct xfs_da_args *args)
567 struct xfs_inode *dp = args->dp;
568 struct xfs_mount *mp = dp->i_mount;
569 struct xfs_trans_res tres;
570 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
575 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
578 error = xfs_qm_dqattach(dp);
582 args->geo = mp->m_attr_geo;
583 args->whichfork = XFS_ATTR_FORK;
584 args->hashval = xfs_da_hashname(args->name, args->namelen);
587 * We have no control over the attribute names that userspace passes us
588 * to remove, so we have to allow the name lookup prior to attribute
589 * removal to fail as well.
591 args->op_flags = XFS_DA_OP_OKNOENT;
594 XFS_STATS_INC(mp, xs_attr_set);
596 args->op_flags |= XFS_DA_OP_ADDNAME;
597 args->total = xfs_attr_calc_size(args, &local);
600 * If the inode doesn't have an attribute fork, add one.
601 * (inode must not be locked when we call this routine)
603 if (XFS_IFORK_Q(dp) == 0) {
604 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
605 xfs_attr_sf_entsize_byname(args->namelen,
608 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
613 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
614 M_RES(mp)->tr_attrsetrt.tr_logres *
616 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
617 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
621 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
623 XFS_STATS_INC(mp, xs_attr_remove);
625 tres = M_RES(mp)->tr_attrrm;
626 total = XFS_ATTRRM_SPACE_RES(mp);
627 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
631 * Root fork attributes can use reserved data blocks for this
632 * operation if necessary
634 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
638 if (args->value || xfs_inode_hasattr(dp)) {
639 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
640 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
642 goto out_trans_cancel;
646 error = xfs_has_attr(args);
647 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
648 goto out_trans_cancel;
649 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
650 goto out_trans_cancel;
651 if (error != -ENOATTR && error != -EEXIST)
652 goto out_trans_cancel;
654 error = xfs_attr_set_args(args);
656 goto out_trans_cancel;
657 /* shortform attribute has already been committed */
661 error = xfs_has_attr(args);
662 if (error != -EEXIST)
663 goto out_trans_cancel;
665 error = xfs_attr_remove_args(args);
667 goto out_trans_cancel;
671 * If this is a synchronous mount, make sure that the
672 * transaction goes to disk before returning to the user.
674 if (mp->m_flags & XFS_MOUNT_WSYNC)
675 xfs_trans_set_sync(args->trans);
677 if (!(args->op_flags & XFS_DA_OP_NOTIME))
678 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
681 * Commit the last in the sequence of transactions.
683 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
684 error = xfs_trans_commit(args->trans);
686 xfs_iunlock(dp, XFS_ILOCK_EXCL);
691 xfs_trans_cancel(args->trans);
695 /*========================================================================
696 * External routines when attribute list is inside the inode
697 *========================================================================*/
699 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
701 struct xfs_attr_shortform *sf;
703 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
704 return be16_to_cpu(sf->hdr.totsize);
708 * Add a name to the shortform attribute list structure
709 * This is the external routine.
712 xfs_attr_shortform_addname(xfs_da_args_t *args)
714 int newsize, forkoff, retval;
716 trace_xfs_attr_sf_addname(args);
718 retval = xfs_attr_shortform_lookup(args);
719 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
721 if (retval == -EEXIST) {
722 if (args->attr_flags & XATTR_CREATE)
724 retval = xfs_attr_shortform_remove(args);
728 * Since we have removed the old attr, clear ATTR_REPLACE so
729 * that the leaf format add routine won't trip over the attr
732 args->attr_flags &= ~XATTR_REPLACE;
735 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
736 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
739 newsize = xfs_attr_sf_totsize(args->dp);
740 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
742 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
746 xfs_attr_shortform_add(args, forkoff);
751 /*========================================================================
752 * External routines when attribute list is one block
753 *========================================================================*/
755 /* Store info about a remote block */
757 xfs_attr_save_rmt_blk(
758 struct xfs_da_args *args)
760 args->blkno2 = args->blkno;
761 args->index2 = args->index;
762 args->rmtblkno2 = args->rmtblkno;
763 args->rmtblkcnt2 = args->rmtblkcnt;
764 args->rmtvaluelen2 = args->rmtvaluelen;
767 /* Set stored info about a remote block */
769 xfs_attr_restore_rmt_blk(
770 struct xfs_da_args *args)
772 args->blkno = args->blkno2;
773 args->index = args->index2;
774 args->rmtblkno = args->rmtblkno2;
775 args->rmtblkcnt = args->rmtblkcnt2;
776 args->rmtvaluelen = args->rmtvaluelen2;
780 * Tries to add an attribute to an inode in leaf form
782 * This function is meant to execute as part of a delayed operation and leaves
783 * the transaction handling to the caller. On success the attribute is added
784 * and the inode and transaction are left dirty. If there is not enough space,
785 * the attr data is converted to node format and -ENOSPC is returned. Caller is
786 * responsible for handling the dirty inode and transaction or adding the attr
790 xfs_attr_leaf_try_add(
791 struct xfs_da_args *args,
797 * Look up the given attribute in the leaf block. Figure out if
798 * the given flags produce an error or call for an atomic rename.
800 retval = xfs_attr_leaf_hasname(args, &bp);
801 if (retval != -ENOATTR && retval != -EEXIST)
803 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
805 if (retval == -EEXIST) {
806 if (args->attr_flags & XATTR_CREATE)
809 trace_xfs_attr_leaf_replace(args);
811 /* save the attribute state for later removal*/
812 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
813 xfs_attr_save_rmt_blk(args);
816 * clear the remote attr state now that it is saved so that the
817 * values reflect the state of the attribute we are about to
818 * add, not the attribute we just found and will remove later.
822 args->rmtvaluelen = 0;
826 * Add the attribute to the leaf block
828 return xfs_attr3_leaf_add(bp, args);
831 xfs_trans_brelse(args->trans, bp);
836 * Return EEXIST if attr is found, or ENOATTR if not
839 xfs_attr_leaf_hasname(
840 struct xfs_da_args *args,
845 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
849 error = xfs_attr3_leaf_lookup_int(*bp, args);
850 if (error != -ENOATTR && error != -EEXIST)
851 xfs_trans_brelse(args->trans, *bp);
857 * Remove a name from the leaf attribute list structure
859 * This leaf block cannot have a "remote" value, we only call this routine
860 * if bmap_one_block() says there is only one block (ie: no remote blks).
863 xfs_attr_leaf_removename(
864 struct xfs_da_args *args)
866 struct xfs_inode *dp;
870 trace_xfs_attr_leaf_removename(args);
873 * Remove the attribute.
877 error = xfs_attr_leaf_hasname(args, &bp);
879 if (error == -ENOATTR) {
880 xfs_trans_brelse(args->trans, bp);
882 } else if (error != -EEXIST)
885 xfs_attr3_leaf_remove(bp, args);
888 * If the result is small enough, shrink it all into the inode.
890 forkoff = xfs_attr_shortform_allfit(bp, dp);
892 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
893 /* bp is gone due to xfs_da_shrink_inode */
899 * Look up a name in a leaf attribute list structure.
901 * This leaf block cannot have a "remote" value, we only call this routine
902 * if bmap_one_block() says there is only one block (ie: no remote blks).
904 * Returns 0 on successful retrieval, otherwise an error.
907 xfs_attr_leaf_get(xfs_da_args_t *args)
912 trace_xfs_attr_leaf_get(args);
914 error = xfs_attr_leaf_hasname(args, &bp);
916 if (error == -ENOATTR) {
917 xfs_trans_brelse(args->trans, bp);
919 } else if (error != -EEXIST)
923 error = xfs_attr3_leaf_getvalue(bp, args);
924 xfs_trans_brelse(args->trans, bp);
929 * Return EEXIST if attr is found, or ENOATTR if not
930 * statep: If not null is set to point at the found state. Caller will
931 * be responsible for freeing the state in this case.
934 xfs_attr_node_hasname(
935 struct xfs_da_args *args,
936 struct xfs_da_state **statep)
938 struct xfs_da_state *state;
941 state = xfs_da_state_alloc(args);
946 * Search to see if name exists, and get back a pointer to it.
948 error = xfs_da3_node_lookup_int(state, &retval);
950 xfs_da_state_free(state);
957 xfs_da_state_free(state);
961 /*========================================================================
962 * External routines when attribute list size > geo->blksize
963 *========================================================================*/
966 xfs_attr_node_addname_find_attr(
967 struct xfs_da_args *args,
968 struct xfs_da_state **state)
973 * Search to see if name already exists, and get back a pointer
974 * to where it should go.
976 retval = xfs_attr_node_hasname(args, state);
977 if (retval != -ENOATTR && retval != -EEXIST)
980 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
982 if (retval == -EEXIST) {
983 if (args->attr_flags & XATTR_CREATE)
986 trace_xfs_attr_node_replace(args);
988 /* save the attribute state for later removal*/
989 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
990 xfs_attr_save_rmt_blk(args);
993 * clear the remote attr state now that it is saved so that the
994 * values reflect the state of the attribute we are about to
995 * add, not the attribute we just found and will remove later.
999 args->rmtvaluelen = 0;
1005 xfs_da_state_free(*state);
1010 * Add a name to a Btree-format attribute list.
1012 * This will involve walking down the Btree, and may involve splitting
1013 * leaf nodes and even splitting intermediate nodes up to and including
1014 * the root node (a special case of an intermediate node).
1016 * "Remote" attribute values confuse the issue and atomic rename operations
1017 * add a whole extra layer of confusion on top of that.
1020 xfs_attr_node_addname(
1021 struct xfs_da_args *args,
1022 struct xfs_da_state *state)
1024 struct xfs_da_state_blk *blk;
1025 struct xfs_inode *dp;
1028 trace_xfs_attr_node_addname(args);
1031 blk = &state->path.blk[state->path.active-1];
1032 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1034 error = xfs_attr3_leaf_add(blk->bp, state->args);
1035 if (error == -ENOSPC) {
1036 if (state->path.active == 1) {
1038 * Its really a single leaf node, but it had
1039 * out-of-line values so it looked like it *might*
1040 * have been a b-tree.
1042 xfs_da_state_free(state);
1044 error = xfs_attr3_leaf_to_node(args);
1047 error = xfs_defer_finish(&args->trans);
1052 * Commit the node conversion and start the next
1053 * trans in the chain.
1055 error = xfs_trans_roll_inode(&args->trans, dp);
1063 * Split as many Btree elements as required.
1064 * This code tracks the new and old attr's location
1065 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1066 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1068 error = xfs_da3_split(state);
1071 error = xfs_defer_finish(&args->trans);
1076 * Addition succeeded, update Btree hashvals.
1078 xfs_da3_fixhashpath(state, &state->path);
1083 xfs_da_state_free(state);
1089 xfs_attr_node_addname_clear_incomplete(
1090 struct xfs_da_args *args)
1092 struct xfs_da_state *state = NULL;
1093 struct xfs_da_state_blk *blk;
1098 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1099 * flag means that we will find the "old" attr, not the "new" one.
1101 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1102 state = xfs_da_state_alloc(args);
1104 error = xfs_da3_node_lookup_int(state, &retval);
1109 * Remove the name and update the hashvals in the tree.
1111 blk = &state->path.blk[state->path.active-1];
1112 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1113 error = xfs_attr3_leaf_remove(blk->bp, args);
1114 xfs_da3_fixhashpath(state, &state->path);
1117 * Check to see if the tree needs to be collapsed.
1119 if (retval && (state->path.active > 1)) {
1120 error = xfs_da3_join(state);
1128 xfs_da_state_free(state);
1135 * Shrink an attribute from leaf to shortform
1138 xfs_attr_node_shrink(
1139 struct xfs_da_args *args,
1140 struct xfs_da_state *state)
1142 struct xfs_inode *dp = args->dp;
1147 * Have to get rid of the copy of this dabuf in the state.
1149 ASSERT(state->path.active == 1);
1150 ASSERT(state->path.blk[0].bp);
1151 state->path.blk[0].bp = NULL;
1153 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1157 forkoff = xfs_attr_shortform_allfit(bp, dp);
1159 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1160 /* bp is gone due to xfs_da_shrink_inode */
1162 xfs_trans_brelse(args->trans, bp);
1168 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1169 * for later deletion of the entry.
1172 xfs_attr_leaf_mark_incomplete(
1173 struct xfs_da_args *args,
1174 struct xfs_da_state *state)
1179 * Fill in disk block numbers in the state structure
1180 * so that we can get the buffers back after we commit
1181 * several transactions in the following calls.
1183 error = xfs_attr_fillstate(state);
1188 * Mark the attribute as INCOMPLETE
1190 return xfs_attr3_leaf_setflag(args);
1194 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1195 * the blocks are valid. Attr keys with remote blocks will be marked
1199 int xfs_attr_node_removename_setup(
1200 struct xfs_da_args *args,
1201 struct xfs_da_state **state)
1205 error = xfs_attr_node_hasname(args, state);
1206 if (error != -EEXIST)
1209 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1210 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1211 XFS_ATTR_LEAF_MAGIC);
1213 if (args->rmtblkno > 0) {
1214 error = xfs_attr_leaf_mark_incomplete(args, *state);
1218 return xfs_attr_rmtval_invalidate(args);
1225 xfs_attr_node_remove_name(
1226 struct xfs_da_args *args,
1227 struct xfs_da_state *state)
1229 struct xfs_da_state_blk *blk;
1233 * Remove the name and update the hashvals in the tree.
1235 blk = &state->path.blk[state->path.active-1];
1236 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1237 retval = xfs_attr3_leaf_remove(blk->bp, args);
1238 xfs_da3_fixhashpath(state, &state->path);
1244 * Remove a name from a B-tree attribute list.
1246 * This will involve walking down the Btree, and may involve joining
1247 * leaf nodes and even joining intermediate nodes up to and including
1248 * the root node (a special case of an intermediate node).
1251 xfs_attr_node_removename(
1252 struct xfs_da_args *args)
1254 struct xfs_da_state *state;
1256 struct xfs_inode *dp = args->dp;
1258 trace_xfs_attr_node_removename(args);
1260 error = xfs_attr_node_removename_setup(args, &state);
1265 * If there is an out-of-line value, de-allocate the blocks.
1266 * This is done before we remove the attribute so that we don't
1267 * overflow the maximum size of a transaction and/or hit a deadlock.
1269 if (args->rmtblkno > 0) {
1270 error = xfs_attr_rmtval_remove(args);
1275 * Refill the state structure with buffers, the prior calls
1276 * released our buffers.
1278 error = xfs_attr_refillstate(state);
1282 retval = xfs_attr_node_remove_name(args, state);
1285 * Check to see if the tree needs to be collapsed.
1287 if (retval && (state->path.active > 1)) {
1288 error = xfs_da3_join(state);
1291 error = xfs_defer_finish(&args->trans);
1295 * Commit the Btree join operation and start a new trans.
1297 error = xfs_trans_roll_inode(&args->trans, dp);
1303 * If the result is small enough, push it all into the inode.
1305 if (xfs_attr_is_leaf(dp))
1306 error = xfs_attr_node_shrink(args, state);
1310 xfs_da_state_free(state);
1315 * Fill in the disk block numbers in the state structure for the buffers
1316 * that are attached to the state structure.
1317 * This is done so that we can quickly reattach ourselves to those buffers
1318 * after some set of transaction commits have released these buffers.
1321 xfs_attr_fillstate(xfs_da_state_t *state)
1323 xfs_da_state_path_t *path;
1324 xfs_da_state_blk_t *blk;
1327 trace_xfs_attr_fillstate(state->args);
1330 * Roll down the "path" in the state structure, storing the on-disk
1331 * block number for those buffers in the "path".
1333 path = &state->path;
1334 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1335 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1337 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1340 blk->disk_blkno = 0;
1345 * Roll down the "altpath" in the state structure, storing the on-disk
1346 * block number for those buffers in the "altpath".
1348 path = &state->altpath;
1349 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1350 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1352 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1355 blk->disk_blkno = 0;
1363 * Reattach the buffers to the state structure based on the disk block
1364 * numbers stored in the state structure.
1365 * This is done after some set of transaction commits have released those
1366 * buffers from our grip.
1369 xfs_attr_refillstate(xfs_da_state_t *state)
1371 xfs_da_state_path_t *path;
1372 xfs_da_state_blk_t *blk;
1375 trace_xfs_attr_refillstate(state->args);
1378 * Roll down the "path" in the state structure, storing the on-disk
1379 * block number for those buffers in the "path".
1381 path = &state->path;
1382 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1383 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1384 if (blk->disk_blkno) {
1385 error = xfs_da3_node_read_mapped(state->args->trans,
1386 state->args->dp, blk->disk_blkno,
1387 &blk->bp, XFS_ATTR_FORK);
1396 * Roll down the "altpath" in the state structure, storing the on-disk
1397 * block number for those buffers in the "altpath".
1399 path = &state->altpath;
1400 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1401 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1402 if (blk->disk_blkno) {
1403 error = xfs_da3_node_read_mapped(state->args->trans,
1404 state->args->dp, blk->disk_blkno,
1405 &blk->bp, XFS_ATTR_FORK);
1417 * Retrieve the attribute data from a node attribute list.
1419 * This routine gets called for any attribute fork that has more than one
1420 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1421 * "remote" values taking up more blocks.
1423 * Returns 0 on successful retrieval, otherwise an error.
1427 struct xfs_da_args *args)
1429 struct xfs_da_state *state;
1430 struct xfs_da_state_blk *blk;
1434 trace_xfs_attr_node_get(args);
1437 * Search to see if name exists, and get back a pointer to it.
1439 error = xfs_attr_node_hasname(args, &state);
1440 if (error != -EEXIST)
1444 * Get the value, local or "remote"
1446 blk = &state->path.blk[state->path.active - 1];
1447 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1450 * If not in a transaction, we have to release all the buffers.
1453 for (i = 0; state != NULL && i < state->path.active; i++) {
1454 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1455 state->path.blk[i].bp = NULL;
1459 xfs_da_state_free(state);
1463 /* Returns true if the attribute entry name is valid. */
1470 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1471 * out, so use >= for the length check.
1473 if (length >= MAXNAMELEN)
1476 /* There shouldn't be any nulls here */
1477 return !memchr(name, 0, length);