1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_da_format.h"
15 #include "xfs_da_btree.h"
16 #include "xfs_attr_sf.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
20 #include "xfs_bmap_btree.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_quota.h"
25 #include "xfs_trans_space.h"
26 #include "xfs_trace.h"
31 * Provide the external interfaces to manage attribute lists.
34 /*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
39 * Internal routines when attribute list fits inside the inode.
41 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
44 * Internal routines when attribute list is one block.
46 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
47 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
48 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
49 STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
52 * Internal routines when attribute list is more than one block.
54 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
55 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
56 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
57 STATIC int xfs_attr_node_hasname(xfs_da_args_t *args,
58 struct xfs_da_state **state);
59 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
60 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
66 if (!XFS_IFORK_Q(ip) ||
67 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
68 ip->i_afp->if_nextents == 0))
74 * Returns true if the there is exactly only block in the attr fork, in which
75 * case the attribute fork consists of a single leaf block entry.
81 struct xfs_ifork *ifp = ip->i_afp;
82 struct xfs_iext_cursor icur;
83 struct xfs_bmbt_irec imap;
85 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
88 xfs_iext_first(ifp, &icur);
89 xfs_iext_get_extent(ifp, &icur, &imap);
90 return imap.br_startoff == 0 && imap.br_blockcount == 1;
93 /*========================================================================
94 * Overall external interface routines.
95 *========================================================================*/
98 * Retrieve an extended attribute and its value. Must have ilock.
99 * Returns 0 on successful retrieval, otherwise an error.
102 xfs_attr_get_ilocked(
103 struct xfs_da_args *args)
105 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
107 if (!xfs_inode_hasattr(args->dp))
110 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
111 return xfs_attr_shortform_getvalue(args);
112 if (xfs_attr_is_leaf(args->dp))
113 return xfs_attr_leaf_get(args);
114 return xfs_attr_node_get(args);
118 * Retrieve an extended attribute by name, and its value if requested.
120 * If args->valuelen is zero, then the caller does not want the value, just an
121 * indication whether the attribute exists and the size of the value if it
122 * exists. The size is returned in args.valuelen.
124 * If args->value is NULL but args->valuelen is non-zero, allocate the buffer
125 * for the value after existence of the attribute has been determined. The
126 * caller always has to free args->value if it is set, no matter if this
127 * function was successful or not.
129 * If the attribute is found, but exceeds the size limit set by the caller in
130 * args->valuelen, return -ERANGE with the size of the attribute that was found
135 struct xfs_da_args *args)
140 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
142 if (XFS_FORCED_SHUTDOWN(args->dp->i_mount))
145 args->geo = args->dp->i_mount->m_attr_geo;
146 args->whichfork = XFS_ATTR_FORK;
147 args->hashval = xfs_da_hashname(args->name, args->namelen);
149 /* Entirely possible to look up a name which doesn't exist */
150 args->op_flags = XFS_DA_OP_OKNOENT;
152 lock_mode = xfs_ilock_attr_map_shared(args->dp);
153 error = xfs_attr_get_ilocked(args);
154 xfs_iunlock(args->dp, lock_mode);
160 * Calculate how many blocks we need for the new attribute,
164 struct xfs_da_args *args,
167 struct xfs_mount *mp = args->dp->i_mount;
172 * Determine space new attribute will use, and if it would be
173 * "local" or "remote" (note: local != inline).
175 size = xfs_attr_leaf_newentsize(args, local);
176 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
178 if (size > (args->geo->blksize / 2)) {
179 /* Double split possible */
184 * Out of line attribute, cannot double split, but
185 * make room for the attribute value itself.
187 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
189 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
196 xfs_attr_try_sf_addname(
197 struct xfs_inode *dp,
198 struct xfs_da_args *args)
204 * Build initial attribute list (if required).
206 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
207 xfs_attr_shortform_create(args);
209 error = xfs_attr_shortform_addname(args);
210 if (error == -ENOSPC)
214 * Commit the shortform mods, and we're done.
215 * NOTE: this is also the error path (EEXIST, etc).
217 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
218 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
220 if (dp->i_mount->m_flags & XFS_MOUNT_WSYNC)
221 xfs_trans_set_sync(args->trans);
227 * Check to see if the attr should be upgraded from non-existent or shortform to
228 * single-leaf-block attribute list.
231 xfs_attr_is_shortform(
232 struct xfs_inode *ip)
234 return ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL ||
235 (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
236 ip->i_afp->if_nextents == 0);
240 * Attempts to set an attr in shortform, or converts short form to leaf form if
241 * there is not enough room. If the attr is set, the transaction is committed
245 xfs_attr_set_shortform(
246 struct xfs_da_args *args,
247 struct xfs_buf **leaf_bp)
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;
262 * It won't fit in the shortform, transform to a leaf block. GROT:
263 * another possible req'mt for a double-split btree op.
265 error = xfs_attr_shortform_to_leaf(args, leaf_bp);
270 * Prevent the leaf buffer from being unlocked so that a concurrent AIL
271 * push cannot grab the half-baked leaf buffer and run into problems
272 * with the write verifier. Once we're done rolling the transaction we
273 * can release the hold and add the attr to the leaf.
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 *leaf_bp = NULL;
298 * If the attribute list is already in leaf format, jump straight to
299 * leaf handling. Otherwise, try to add the attribute to the shortform
300 * list; if there's no room then convert the list to leaf format and try
303 if (xfs_attr_is_shortform(dp)) {
306 * If the attr was successfully set in shortform, the
307 * transaction is committed and set to NULL. Otherwise, is it
308 * converted from shortform to leaf, and the transaction is
311 error = xfs_attr_set_shortform(args, &leaf_bp);
312 if (error || !args->trans)
316 if (xfs_attr_is_leaf(dp)) {
317 error = xfs_attr_leaf_addname(args);
318 if (error != -ENOSPC)
322 * Promote the attribute list to the Btree format.
324 error = xfs_attr3_leaf_to_node(args);
329 * Finish any deferred work items and roll the transaction once
330 * more. The goal here is to call node_addname with the inode
331 * and transaction in the same state (inode locked and joined,
332 * transaction clean) no matter how we got to this step.
334 error = xfs_defer_finish(&args->trans);
339 * Commit the current trans (including the inode) and
342 error = xfs_trans_roll_inode(&args->trans, dp);
347 error = xfs_attr_node_addname(args);
352 * Return EEXIST if attr is found, or ENOATTR if not
356 struct xfs_da_args *args)
358 struct xfs_inode *dp = args->dp;
359 struct xfs_buf *bp = NULL;
362 if (!xfs_inode_hasattr(dp))
365 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
366 return xfs_attr_sf_findname(args, NULL, NULL);
368 if (xfs_attr_is_leaf(dp)) {
369 error = xfs_attr_leaf_hasname(args, &bp);
372 xfs_trans_brelse(args->trans, bp);
377 return xfs_attr_node_hasname(args, NULL);
381 * Remove the attribute specified in @args.
384 xfs_attr_remove_args(
385 struct xfs_da_args *args)
387 if (!xfs_inode_hasattr(args->dp))
390 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
391 return xfs_attr_shortform_remove(args);
392 if (xfs_attr_is_leaf(args->dp))
393 return xfs_attr_leaf_removename(args);
394 return xfs_attr_node_removename(args);
398 * Note: If args->value is NULL the attribute will be removed, just like the
399 * Linux ->setattr API.
403 struct xfs_da_args *args)
405 struct xfs_inode *dp = args->dp;
406 struct xfs_mount *mp = dp->i_mount;
407 struct xfs_trans_res tres;
408 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
413 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
416 error = xfs_qm_dqattach(dp);
420 args->geo = mp->m_attr_geo;
421 args->whichfork = XFS_ATTR_FORK;
422 args->hashval = xfs_da_hashname(args->name, args->namelen);
425 * We have no control over the attribute names that userspace passes us
426 * to remove, so we have to allow the name lookup prior to attribute
427 * removal to fail as well.
429 args->op_flags = XFS_DA_OP_OKNOENT;
432 XFS_STATS_INC(mp, xs_attr_set);
434 args->op_flags |= XFS_DA_OP_ADDNAME;
435 args->total = xfs_attr_calc_size(args, &local);
438 * If the inode doesn't have an attribute fork, add one.
439 * (inode must not be locked when we call this routine)
441 if (XFS_IFORK_Q(dp) == 0) {
442 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
443 xfs_attr_sf_entsize_byname(args->namelen,
446 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
451 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
452 M_RES(mp)->tr_attrsetrt.tr_logres *
454 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
455 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
459 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
461 XFS_STATS_INC(mp, xs_attr_remove);
463 tres = M_RES(mp)->tr_attrrm;
464 total = XFS_ATTRRM_SPACE_RES(mp);
465 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
469 * Root fork attributes can use reserved data blocks for this
470 * operation if necessary
472 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
476 if (args->value || xfs_inode_hasattr(dp)) {
477 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
478 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
480 goto out_trans_cancel;
484 error = xfs_has_attr(args);
485 if (error == -EEXIST && (args->attr_flags & XATTR_CREATE))
486 goto out_trans_cancel;
487 if (error == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
488 goto out_trans_cancel;
489 if (error != -ENOATTR && error != -EEXIST)
490 goto out_trans_cancel;
492 error = xfs_attr_set_args(args);
494 goto out_trans_cancel;
495 /* shortform attribute has already been committed */
499 error = xfs_has_attr(args);
500 if (error != -EEXIST)
501 goto out_trans_cancel;
503 error = xfs_attr_remove_args(args);
505 goto out_trans_cancel;
509 * If this is a synchronous mount, make sure that the
510 * transaction goes to disk before returning to the user.
512 if (mp->m_flags & XFS_MOUNT_WSYNC)
513 xfs_trans_set_sync(args->trans);
515 if (!(args->op_flags & XFS_DA_OP_NOTIME))
516 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
519 * Commit the last in the sequence of transactions.
521 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
522 error = xfs_trans_commit(args->trans);
524 xfs_iunlock(dp, XFS_ILOCK_EXCL);
529 xfs_trans_cancel(args->trans);
533 /*========================================================================
534 * External routines when attribute list is inside the inode
535 *========================================================================*/
537 static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
539 struct xfs_attr_shortform *sf;
541 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
542 return be16_to_cpu(sf->hdr.totsize);
546 * Add a name to the shortform attribute list structure
547 * This is the external routine.
550 xfs_attr_shortform_addname(xfs_da_args_t *args)
552 int newsize, forkoff, retval;
554 trace_xfs_attr_sf_addname(args);
556 retval = xfs_attr_shortform_lookup(args);
557 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
559 if (retval == -EEXIST) {
560 if (args->attr_flags & XATTR_CREATE)
562 retval = xfs_attr_shortform_remove(args);
566 * Since we have removed the old attr, clear ATTR_REPLACE so
567 * that the leaf format add routine won't trip over the attr
570 args->attr_flags &= ~XATTR_REPLACE;
573 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
574 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
577 newsize = xfs_attr_sf_totsize(args->dp);
578 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
580 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
584 xfs_attr_shortform_add(args, forkoff);
589 /*========================================================================
590 * External routines when attribute list is one block
591 *========================================================================*/
593 /* Store info about a remote block */
595 xfs_attr_save_rmt_blk(
596 struct xfs_da_args *args)
598 args->blkno2 = args->blkno;
599 args->index2 = args->index;
600 args->rmtblkno2 = args->rmtblkno;
601 args->rmtblkcnt2 = args->rmtblkcnt;
602 args->rmtvaluelen2 = args->rmtvaluelen;
605 /* Set stored info about a remote block */
607 xfs_attr_restore_rmt_blk(
608 struct xfs_da_args *args)
610 args->blkno = args->blkno2;
611 args->index = args->index2;
612 args->rmtblkno = args->rmtblkno2;
613 args->rmtblkcnt = args->rmtblkcnt2;
614 args->rmtvaluelen = args->rmtvaluelen2;
618 * Tries to add an attribute to an inode in leaf form
620 * This function is meant to execute as part of a delayed operation and leaves
621 * the transaction handling to the caller. On success the attribute is added
622 * and the inode and transaction are left dirty. If there is not enough space,
623 * the attr data is converted to node format and -ENOSPC is returned. Caller is
624 * responsible for handling the dirty inode and transaction or adding the attr
628 xfs_attr_leaf_try_add(
629 struct xfs_da_args *args,
635 * Look up the given attribute in the leaf block. Figure out if
636 * the given flags produce an error or call for an atomic rename.
638 retval = xfs_attr_leaf_hasname(args, &bp);
639 if (retval != -ENOATTR && retval != -EEXIST)
641 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
643 if (retval == -EEXIST) {
644 if (args->attr_flags & XATTR_CREATE)
647 trace_xfs_attr_leaf_replace(args);
649 /* save the attribute state for later removal*/
650 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
651 xfs_attr_save_rmt_blk(args);
654 * clear the remote attr state now that it is saved so that the
655 * values reflect the state of the attribute we are about to
656 * add, not the attribute we just found and will remove later.
660 args->rmtvaluelen = 0;
664 * Add the attribute to the leaf block
666 return xfs_attr3_leaf_add(bp, args);
669 xfs_trans_brelse(args->trans, bp);
675 * Add a name to the leaf attribute list structure
677 * This leaf block cannot have a "remote" value, we only call this routine
678 * if bmap_one_block() says there is only one block (ie: no remote blks).
681 xfs_attr_leaf_addname(
682 struct xfs_da_args *args)
685 struct xfs_buf *bp = NULL;
686 struct xfs_inode *dp = args->dp;
688 trace_xfs_attr_leaf_addname(args);
690 error = xfs_attr_leaf_try_add(args, bp);
695 * Commit the transaction that added the attr name so that
696 * later routines can manage their own transactions.
698 error = xfs_trans_roll_inode(&args->trans, dp);
703 * If there was an out-of-line value, allocate the blocks we
704 * identified for its storage and copy the value. This is done
705 * after we create the attribute so that we don't overflow the
706 * maximum size of a transaction and/or hit a deadlock.
708 if (args->rmtblkno > 0) {
709 error = xfs_attr_rmtval_set(args);
714 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
716 * Added a "remote" value, just clear the incomplete flag.
718 if (args->rmtblkno > 0)
719 error = xfs_attr3_leaf_clearflag(args);
725 * If this is an atomic rename operation, we must "flip" the incomplete
726 * flags on the "new" and "old" attribute/value pairs so that one
727 * disappears and one appears atomically. Then we must remove the "old"
728 * attribute/value pair.
730 * In a separate transaction, set the incomplete flag on the "old" attr
731 * and clear the incomplete flag on the "new" attr.
734 error = xfs_attr3_leaf_flipflags(args);
738 * Commit the flag value change and start the next trans in series.
740 error = xfs_trans_roll_inode(&args->trans, args->dp);
745 * Dismantle the "old" attribute/value pair by removing a "remote" value
748 xfs_attr_restore_rmt_blk(args);
750 if (args->rmtblkno) {
751 error = xfs_attr_rmtval_invalidate(args);
755 error = xfs_attr_rmtval_remove(args);
761 * Read in the block containing the "old" attr, then remove the "old"
762 * attr from that block (neat, huh!)
764 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
769 xfs_attr3_leaf_remove(bp, args);
772 * If the result is small enough, shrink it all into the inode.
774 forkoff = xfs_attr_shortform_allfit(bp, dp);
776 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
777 /* bp is gone due to xfs_da_shrink_inode */
783 * Return EEXIST if attr is found, or ENOATTR if not
786 xfs_attr_leaf_hasname(
787 struct xfs_da_args *args,
792 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
796 error = xfs_attr3_leaf_lookup_int(*bp, args);
797 if (error != -ENOATTR && error != -EEXIST)
798 xfs_trans_brelse(args->trans, *bp);
804 * Remove a name from the leaf attribute list structure
806 * This leaf block cannot have a "remote" value, we only call this routine
807 * if bmap_one_block() says there is only one block (ie: no remote blks).
810 xfs_attr_leaf_removename(
811 struct xfs_da_args *args)
813 struct xfs_inode *dp;
817 trace_xfs_attr_leaf_removename(args);
820 * Remove the attribute.
824 error = xfs_attr_leaf_hasname(args, &bp);
826 if (error == -ENOATTR) {
827 xfs_trans_brelse(args->trans, bp);
829 } else if (error != -EEXIST)
832 xfs_attr3_leaf_remove(bp, args);
835 * If the result is small enough, shrink it all into the inode.
837 forkoff = xfs_attr_shortform_allfit(bp, dp);
839 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
840 /* bp is gone due to xfs_da_shrink_inode */
846 * Look up a name in a leaf attribute list structure.
848 * This leaf block cannot have a "remote" value, we only call this routine
849 * if bmap_one_block() says there is only one block (ie: no remote blks).
851 * Returns 0 on successful retrieval, otherwise an error.
854 xfs_attr_leaf_get(xfs_da_args_t *args)
859 trace_xfs_attr_leaf_get(args);
861 error = xfs_attr_leaf_hasname(args, &bp);
863 if (error == -ENOATTR) {
864 xfs_trans_brelse(args->trans, bp);
866 } else if (error != -EEXIST)
870 error = xfs_attr3_leaf_getvalue(bp, args);
871 xfs_trans_brelse(args->trans, bp);
876 * Return EEXIST if attr is found, or ENOATTR if not
877 * statep: If not null is set to point at the found state. Caller will
878 * be responsible for freeing the state in this case.
881 xfs_attr_node_hasname(
882 struct xfs_da_args *args,
883 struct xfs_da_state **statep)
885 struct xfs_da_state *state;
888 state = xfs_da_state_alloc(args);
893 * Search to see if name exists, and get back a pointer to it.
895 error = xfs_da3_node_lookup_int(state, &retval);
897 xfs_da_state_free(state);
904 xfs_da_state_free(state);
908 /*========================================================================
909 * External routines when attribute list size > geo->blksize
910 *========================================================================*/
913 * Add a name to a Btree-format attribute list.
915 * This will involve walking down the Btree, and may involve splitting
916 * leaf nodes and even splitting intermediate nodes up to and including
917 * the root node (a special case of an intermediate node).
919 * "Remote" attribute values confuse the issue and atomic rename operations
920 * add a whole extra layer of confusion on top of that.
923 xfs_attr_node_addname(
924 struct xfs_da_args *args)
926 struct xfs_da_state *state;
927 struct xfs_da_state_blk *blk;
928 struct xfs_inode *dp;
931 trace_xfs_attr_node_addname(args);
934 * Fill in bucket of arguments/results/context to carry around.
939 * Search to see if name already exists, and get back a pointer
940 * to where it should go.
943 retval = xfs_attr_node_hasname(args, &state);
944 if (retval != -ENOATTR && retval != -EEXIST)
947 blk = &state->path.blk[ state->path.active-1 ];
948 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
949 if (retval == -ENOATTR && (args->attr_flags & XATTR_REPLACE))
951 if (retval == -EEXIST) {
952 if (args->attr_flags & XATTR_CREATE)
955 trace_xfs_attr_node_replace(args);
957 /* save the attribute state for later removal*/
958 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
959 xfs_attr_save_rmt_blk(args);
962 * clear the remote attr state now that it is saved so that the
963 * values reflect the state of the attribute we are about to
964 * add, not the attribute we just found and will remove later.
968 args->rmtvaluelen = 0;
971 retval = xfs_attr3_leaf_add(blk->bp, state->args);
972 if (retval == -ENOSPC) {
973 if (state->path.active == 1) {
975 * Its really a single leaf node, but it had
976 * out-of-line values so it looked like it *might*
977 * have been a b-tree.
979 xfs_da_state_free(state);
981 error = xfs_attr3_leaf_to_node(args);
984 error = xfs_defer_finish(&args->trans);
989 * Commit the node conversion and start the next
990 * trans in the chain.
992 error = xfs_trans_roll_inode(&args->trans, dp);
1000 * Split as many Btree elements as required.
1001 * This code tracks the new and old attr's location
1002 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1003 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1005 error = xfs_da3_split(state);
1008 error = xfs_defer_finish(&args->trans);
1013 * Addition succeeded, update Btree hashvals.
1015 xfs_da3_fixhashpath(state, &state->path);
1019 * Kill the state structure, we're done with it and need to
1020 * allow the buffers to come back later.
1022 xfs_da_state_free(state);
1026 * Commit the leaf addition or btree split and start the next
1027 * trans in the chain.
1029 error = xfs_trans_roll_inode(&args->trans, dp);
1034 * If there was an out-of-line value, allocate the blocks we
1035 * identified for its storage and copy the value. This is done
1036 * after we create the attribute so that we don't overflow the
1037 * maximum size of a transaction and/or hit a deadlock.
1039 if (args->rmtblkno > 0) {
1040 error = xfs_attr_rmtval_set(args);
1045 if (!(args->op_flags & XFS_DA_OP_RENAME)) {
1047 * Added a "remote" value, just clear the incomplete flag.
1049 if (args->rmtblkno > 0)
1050 error = xfs_attr3_leaf_clearflag(args);
1056 * If this is an atomic rename operation, we must "flip" the incomplete
1057 * flags on the "new" and "old" attribute/value pairs so that one
1058 * disappears and one appears atomically. Then we must remove the "old"
1059 * attribute/value pair.
1061 * In a separate transaction, set the incomplete flag on the "old" attr
1062 * and clear the incomplete flag on the "new" attr.
1064 error = xfs_attr3_leaf_flipflags(args);
1068 * Commit the flag value change and start the next trans in series
1070 error = xfs_trans_roll_inode(&args->trans, args->dp);
1075 * Dismantle the "old" attribute/value pair by removing a "remote" value
1078 xfs_attr_restore_rmt_blk(args);
1080 if (args->rmtblkno) {
1081 error = xfs_attr_rmtval_invalidate(args);
1085 error = xfs_attr_rmtval_remove(args);
1091 * Re-find the "old" attribute entry after any split ops. The INCOMPLETE
1092 * flag means that we will find the "old" attr, not the "new" one.
1094 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1095 state = xfs_da_state_alloc(args);
1097 error = xfs_da3_node_lookup_int(state, &retval);
1102 * Remove the name and update the hashvals in the tree.
1104 blk = &state->path.blk[state->path.active-1];
1105 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1106 error = xfs_attr3_leaf_remove(blk->bp, args);
1107 xfs_da3_fixhashpath(state, &state->path);
1110 * Check to see if the tree needs to be collapsed.
1112 if (retval && (state->path.active > 1)) {
1113 error = xfs_da3_join(state);
1121 xfs_da_state_free(state);
1128 * Shrink an attribute from leaf to shortform
1131 xfs_attr_node_shrink(
1132 struct xfs_da_args *args,
1133 struct xfs_da_state *state)
1135 struct xfs_inode *dp = args->dp;
1140 * Have to get rid of the copy of this dabuf in the state.
1142 ASSERT(state->path.active == 1);
1143 ASSERT(state->path.blk[0].bp);
1144 state->path.blk[0].bp = NULL;
1146 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1150 forkoff = xfs_attr_shortform_allfit(bp, dp);
1152 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1153 /* bp is gone due to xfs_da_shrink_inode */
1155 xfs_trans_brelse(args->trans, bp);
1161 * Mark an attribute entry INCOMPLETE and save pointers to the relevant buffers
1162 * for later deletion of the entry.
1165 xfs_attr_leaf_mark_incomplete(
1166 struct xfs_da_args *args,
1167 struct xfs_da_state *state)
1172 * Fill in disk block numbers in the state structure
1173 * so that we can get the buffers back after we commit
1174 * several transactions in the following calls.
1176 error = xfs_attr_fillstate(state);
1181 * Mark the attribute as INCOMPLETE
1183 return xfs_attr3_leaf_setflag(args);
1187 * Initial setup for xfs_attr_node_removename. Make sure the attr is there and
1188 * the blocks are valid. Attr keys with remote blocks will be marked
1192 int xfs_attr_node_removename_setup(
1193 struct xfs_da_args *args,
1194 struct xfs_da_state **state)
1198 error = xfs_attr_node_hasname(args, state);
1199 if (error != -EEXIST)
1202 ASSERT((*state)->path.blk[(*state)->path.active - 1].bp != NULL);
1203 ASSERT((*state)->path.blk[(*state)->path.active - 1].magic ==
1204 XFS_ATTR_LEAF_MAGIC);
1206 if (args->rmtblkno > 0) {
1207 error = xfs_attr_leaf_mark_incomplete(args, *state);
1211 return xfs_attr_rmtval_invalidate(args);
1218 xfs_attr_node_remove_name(
1219 struct xfs_da_args *args,
1220 struct xfs_da_state *state)
1222 struct xfs_da_state_blk *blk;
1226 * Remove the name and update the hashvals in the tree.
1228 blk = &state->path.blk[state->path.active-1];
1229 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1230 retval = xfs_attr3_leaf_remove(blk->bp, args);
1231 xfs_da3_fixhashpath(state, &state->path);
1237 * Remove a name from a B-tree attribute list.
1239 * This will involve walking down the Btree, and may involve joining
1240 * leaf nodes and even joining intermediate nodes up to and including
1241 * the root node (a special case of an intermediate node).
1244 xfs_attr_node_removename(
1245 struct xfs_da_args *args)
1247 struct xfs_da_state *state;
1249 struct xfs_inode *dp = args->dp;
1251 trace_xfs_attr_node_removename(args);
1253 error = xfs_attr_node_removename_setup(args, &state);
1258 * If there is an out-of-line value, de-allocate the blocks.
1259 * This is done before we remove the attribute so that we don't
1260 * overflow the maximum size of a transaction and/or hit a deadlock.
1262 if (args->rmtblkno > 0) {
1263 error = xfs_attr_rmtval_remove(args);
1268 * Refill the state structure with buffers, the prior calls
1269 * released our buffers.
1271 error = xfs_attr_refillstate(state);
1275 retval = xfs_attr_node_remove_name(args, state);
1278 * Check to see if the tree needs to be collapsed.
1280 if (retval && (state->path.active > 1)) {
1281 error = xfs_da3_join(state);
1284 error = xfs_defer_finish(&args->trans);
1288 * Commit the Btree join operation and start a new trans.
1290 error = xfs_trans_roll_inode(&args->trans, dp);
1296 * If the result is small enough, push it all into the inode.
1298 if (xfs_attr_is_leaf(dp))
1299 error = xfs_attr_node_shrink(args, state);
1303 xfs_da_state_free(state);
1308 * Fill in the disk block numbers in the state structure for the buffers
1309 * that are attached to the state structure.
1310 * This is done so that we can quickly reattach ourselves to those buffers
1311 * after some set of transaction commits have released these buffers.
1314 xfs_attr_fillstate(xfs_da_state_t *state)
1316 xfs_da_state_path_t *path;
1317 xfs_da_state_blk_t *blk;
1320 trace_xfs_attr_fillstate(state->args);
1323 * Roll down the "path" in the state structure, storing the on-disk
1324 * block number for those buffers in the "path".
1326 path = &state->path;
1327 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1328 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1330 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1333 blk->disk_blkno = 0;
1338 * Roll down the "altpath" in the state structure, storing the on-disk
1339 * block number for those buffers in the "altpath".
1341 path = &state->altpath;
1342 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1343 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1345 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1348 blk->disk_blkno = 0;
1356 * Reattach the buffers to the state structure based on the disk block
1357 * numbers stored in the state structure.
1358 * This is done after some set of transaction commits have released those
1359 * buffers from our grip.
1362 xfs_attr_refillstate(xfs_da_state_t *state)
1364 xfs_da_state_path_t *path;
1365 xfs_da_state_blk_t *blk;
1368 trace_xfs_attr_refillstate(state->args);
1371 * Roll down the "path" in the state structure, storing the on-disk
1372 * block number for those buffers in the "path".
1374 path = &state->path;
1375 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1376 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1377 if (blk->disk_blkno) {
1378 error = xfs_da3_node_read_mapped(state->args->trans,
1379 state->args->dp, blk->disk_blkno,
1380 &blk->bp, XFS_ATTR_FORK);
1389 * Roll down the "altpath" in the state structure, storing the on-disk
1390 * block number for those buffers in the "altpath".
1392 path = &state->altpath;
1393 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1394 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1395 if (blk->disk_blkno) {
1396 error = xfs_da3_node_read_mapped(state->args->trans,
1397 state->args->dp, blk->disk_blkno,
1398 &blk->bp, XFS_ATTR_FORK);
1410 * Retrieve the attribute data from a node attribute list.
1412 * This routine gets called for any attribute fork that has more than one
1413 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1414 * "remote" values taking up more blocks.
1416 * Returns 0 on successful retrieval, otherwise an error.
1420 struct xfs_da_args *args)
1422 struct xfs_da_state *state;
1423 struct xfs_da_state_blk *blk;
1427 trace_xfs_attr_node_get(args);
1430 * Search to see if name exists, and get back a pointer to it.
1432 error = xfs_attr_node_hasname(args, &state);
1433 if (error != -EEXIST)
1437 * Get the value, local or "remote"
1439 blk = &state->path.blk[state->path.active - 1];
1440 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1443 * If not in a transaction, we have to release all the buffers.
1446 for (i = 0; state != NULL && i < state->path.active; i++) {
1447 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1448 state->path.blk[i].bp = NULL;
1452 xfs_da_state_free(state);
1456 /* Returns true if the attribute entry name is valid. */
1463 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1464 * out, so use >= for the length check.
1466 if (length >= MAXNAMELEN)
1469 /* There shouldn't be any nulls here */
1470 return !memchr(name, 0, length);