2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_inode.h"
31 #include "xfs_alloc.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
35 #include "xfs_bmap_util.h"
36 #include "xfs_bmap_btree.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_trace.h"
48 * Provide the external interfaces to manage attribute lists.
51 /*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
56 * Internal routines when attribute list fits inside the inode.
58 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
61 * Internal routines when attribute list is one block.
63 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
64 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
65 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
68 * Internal routines when attribute list is more than one block.
70 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
71 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
72 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
73 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
74 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
79 struct xfs_da_args *args,
81 const unsigned char *name,
88 memset(args, 0, sizeof(*args));
89 args->geo = dp->i_mount->m_attr_geo;
90 args->whichfork = XFS_ATTR_FORK;
94 args->namelen = strlen((const char *)name);
95 if (args->namelen >= MAXNAMELEN)
96 return -EFAULT; /* match IRIX behaviour */
98 args->hashval = xfs_da_hashname(args->name, args->namelen);
104 struct xfs_inode *ip)
106 if (!XFS_IFORK_Q(ip) ||
107 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
108 ip->i_d.di_anextents == 0))
113 /*========================================================================
114 * Overall external interface routines.
115 *========================================================================*/
117 /* Retrieve an extended attribute and its value. Must have ilock. */
119 xfs_attr_get_ilocked(
120 struct xfs_inode *ip,
121 struct xfs_da_args *args)
123 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
125 if (!xfs_inode_hasattr(ip))
127 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
128 return xfs_attr_shortform_getvalue(args);
129 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
130 return xfs_attr_leaf_get(args);
132 return xfs_attr_node_get(args);
135 /* Retrieve an extended attribute by name, and its value. */
138 struct xfs_inode *ip,
139 const unsigned char *name,
140 unsigned char *value,
144 struct xfs_da_args args;
148 XFS_STATS_INC(ip->i_mount, xs_attr_get);
150 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
153 error = xfs_attr_args_init(&args, ip, name, flags);
158 args.valuelen = *valuelenp;
159 /* Entirely possible to look up a name which doesn't exist */
160 args.op_flags = XFS_DA_OP_OKNOENT;
162 lock_mode = xfs_ilock_attr_map_shared(ip);
163 error = xfs_attr_get_ilocked(ip, &args);
164 xfs_iunlock(ip, lock_mode);
166 *valuelenp = args.valuelen;
167 return error == -EEXIST ? 0 : error;
171 * Calculate how many blocks we need for the new attribute,
175 struct xfs_da_args *args,
178 struct xfs_mount *mp = args->dp->i_mount;
183 * Determine space new attribute will use, and if it would be
184 * "local" or "remote" (note: local != inline).
186 size = xfs_attr_leaf_newentsize(args, local);
187 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
189 if (size > (args->geo->blksize / 2)) {
190 /* Double split possible */
195 * Out of line attribute, cannot double split, but
196 * make room for the attribute value itself.
198 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
200 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
208 struct xfs_inode *dp,
209 const unsigned char *name,
210 unsigned char *value,
214 struct xfs_mount *mp = dp->i_mount;
215 struct xfs_buf *leaf_bp = NULL;
216 struct xfs_da_args args;
217 struct xfs_defer_ops dfops;
218 struct xfs_trans_res tres;
219 xfs_fsblock_t firstblock;
220 int rsvd = (flags & ATTR_ROOT) != 0;
221 int error, err2, local;
223 XFS_STATS_INC(mp, xs_attr_set);
225 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
228 error = xfs_attr_args_init(&args, dp, name, flags);
233 args.valuelen = valuelen;
234 args.firstblock = &firstblock;
236 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
237 args.total = xfs_attr_calc_size(&args, &local);
239 error = xfs_qm_dqattach(dp, 0);
244 * If the inode doesn't have an attribute fork, add one.
245 * (inode must not be locked when we call this routine)
247 if (XFS_IFORK_Q(dp) == 0) {
248 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
249 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
251 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
256 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
257 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
258 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
259 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
262 * Root fork attributes can use reserved data blocks for this
263 * operation if necessary
265 error = xfs_trans_alloc(mp, &tres, args.total, 0,
266 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
270 xfs_ilock(dp, XFS_ILOCK_EXCL);
271 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
272 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
273 XFS_QMOPT_RES_REGBLKS);
275 xfs_iunlock(dp, XFS_ILOCK_EXCL);
276 xfs_trans_cancel(args.trans);
280 xfs_trans_ijoin(args.trans, dp, 0);
283 * If the attribute list is non-existent or a shortform list,
284 * upgrade it to a single-leaf-block attribute list.
286 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
287 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
288 dp->i_d.di_anextents == 0)) {
291 * Build initial attribute list (if required).
293 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
294 xfs_attr_shortform_create(&args);
297 * Try to add the attr to the attribute list in
300 error = xfs_attr_shortform_addname(&args);
301 if (error != -ENOSPC) {
303 * Commit the shortform mods, and we're done.
304 * NOTE: this is also the error path (EEXIST, etc).
306 ASSERT(args.trans != NULL);
309 * If this is a synchronous mount, make sure that
310 * the transaction goes to disk before returning
313 if (mp->m_flags & XFS_MOUNT_WSYNC)
314 xfs_trans_set_sync(args.trans);
316 if (!error && (flags & ATTR_KERNOTIME) == 0) {
317 xfs_trans_ichgtime(args.trans, dp,
320 err2 = xfs_trans_commit(args.trans);
321 xfs_iunlock(dp, XFS_ILOCK_EXCL);
323 return error ? error : err2;
327 * It won't fit in the shortform, transform to a leaf block.
328 * GROT: another possible req'mt for a double-split btree op.
330 xfs_defer_init(args.dfops, args.firstblock);
331 error = xfs_attr_shortform_to_leaf(&args, &leaf_bp);
333 goto out_defer_cancel;
335 * Prevent the leaf buffer from being unlocked so that a
336 * concurrent AIL push cannot grab the half-baked leaf
337 * buffer and run into problems with the write verifier.
339 xfs_trans_bhold(args.trans, leaf_bp);
340 xfs_defer_bjoin(args.dfops, leaf_bp);
341 xfs_defer_ijoin(args.dfops, dp);
342 error = xfs_defer_finish(&args.trans, args.dfops);
344 goto out_defer_cancel;
347 * Commit the leaf transformation. We'll need another (linked)
348 * transaction to add the new attribute to the leaf, which
349 * means that we have to hold & join the leaf buffer here too.
351 error = xfs_trans_roll_inode(&args.trans, dp);
354 xfs_trans_bjoin(args.trans, leaf_bp);
358 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
359 error = xfs_attr_leaf_addname(&args);
361 error = xfs_attr_node_addname(&args);
366 * If this is a synchronous mount, make sure that the
367 * transaction goes to disk before returning to the user.
369 if (mp->m_flags & XFS_MOUNT_WSYNC)
370 xfs_trans_set_sync(args.trans);
372 if ((flags & ATTR_KERNOTIME) == 0)
373 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
376 * Commit the last in the sequence of transactions.
378 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
379 error = xfs_trans_commit(args.trans);
380 xfs_iunlock(dp, XFS_ILOCK_EXCL);
385 xfs_defer_cancel(&dfops);
388 xfs_trans_brelse(args.trans, leaf_bp);
390 xfs_trans_cancel(args.trans);
391 xfs_iunlock(dp, XFS_ILOCK_EXCL);
396 * Generic handler routine to remove a name from an attribute list.
397 * Transitions attribute list from Btree to shortform as necessary.
401 struct xfs_inode *dp,
402 const unsigned char *name,
405 struct xfs_mount *mp = dp->i_mount;
406 struct xfs_da_args args;
407 struct xfs_defer_ops dfops;
408 xfs_fsblock_t firstblock;
411 XFS_STATS_INC(mp, xs_attr_remove);
413 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
416 error = xfs_attr_args_init(&args, dp, name, flags);
420 args.firstblock = &firstblock;
424 * we have no control over the attribute names that userspace passes us
425 * to remove, so we have to allow the name lookup prior to attribute
428 args.op_flags = XFS_DA_OP_OKNOENT;
430 error = xfs_qm_dqattach(dp, 0);
435 * Root fork attributes can use reserved data blocks for this
436 * operation if necessary
438 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
439 XFS_ATTRRM_SPACE_RES(mp), 0,
440 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
445 xfs_ilock(dp, XFS_ILOCK_EXCL);
447 * No need to make quota reservations here. We expect to release some
448 * blocks not allocate in the common case.
450 xfs_trans_ijoin(args.trans, dp, 0);
452 if (!xfs_inode_hasattr(dp)) {
454 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
455 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
456 error = xfs_attr_shortform_remove(&args);
457 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
458 error = xfs_attr_leaf_removename(&args);
460 error = xfs_attr_node_removename(&args);
467 * If this is a synchronous mount, make sure that the
468 * transaction goes to disk before returning to the user.
470 if (mp->m_flags & XFS_MOUNT_WSYNC)
471 xfs_trans_set_sync(args.trans);
473 if ((flags & ATTR_KERNOTIME) == 0)
474 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
477 * Commit the last in the sequence of transactions.
479 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
480 error = xfs_trans_commit(args.trans);
481 xfs_iunlock(dp, XFS_ILOCK_EXCL);
487 xfs_trans_cancel(args.trans);
488 xfs_iunlock(dp, XFS_ILOCK_EXCL);
492 /*========================================================================
493 * External routines when attribute list is inside the inode
494 *========================================================================*/
497 * Add a name to the shortform attribute list structure
498 * This is the external routine.
501 xfs_attr_shortform_addname(xfs_da_args_t *args)
503 int newsize, forkoff, retval;
505 trace_xfs_attr_sf_addname(args);
507 retval = xfs_attr_shortform_lookup(args);
508 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
510 } else if (retval == -EEXIST) {
511 if (args->flags & ATTR_CREATE)
513 retval = xfs_attr_shortform_remove(args);
517 * Since we have removed the old attr, clear ATTR_REPLACE so
518 * that the leaf format add routine won't trip over the attr
521 args->flags &= ~ATTR_REPLACE;
524 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
525 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
528 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
529 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
531 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
535 xfs_attr_shortform_add(args, forkoff);
540 /*========================================================================
541 * External routines when attribute list is one block
542 *========================================================================*/
545 * Add a name to the leaf attribute list structure
547 * This leaf block cannot have a "remote" value, we only call this routine
548 * if bmap_one_block() says there is only one block (ie: no remote blks).
551 xfs_attr_leaf_addname(xfs_da_args_t *args)
555 int retval, error, forkoff;
557 trace_xfs_attr_leaf_addname(args);
560 * Read the (only) block in the attribute list in.
564 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
569 * Look up the given attribute in the leaf block. Figure out if
570 * the given flags produce an error or call for an atomic rename.
572 retval = xfs_attr3_leaf_lookup_int(bp, args);
573 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
574 xfs_trans_brelse(args->trans, bp);
576 } else if (retval == -EEXIST) {
577 if (args->flags & ATTR_CREATE) { /* pure create op */
578 xfs_trans_brelse(args->trans, bp);
582 trace_xfs_attr_leaf_replace(args);
584 /* save the attribute state for later removal*/
585 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
586 args->blkno2 = args->blkno; /* set 2nd entry info*/
587 args->index2 = args->index;
588 args->rmtblkno2 = args->rmtblkno;
589 args->rmtblkcnt2 = args->rmtblkcnt;
590 args->rmtvaluelen2 = args->rmtvaluelen;
593 * clear the remote attr state now that it is saved so that the
594 * values reflect the state of the attribute we are about to
595 * add, not the attribute we just found and will remove later.
599 args->rmtvaluelen = 0;
603 * Add the attribute to the leaf block, transitioning to a Btree
606 retval = xfs_attr3_leaf_add(bp, args);
607 if (retval == -ENOSPC) {
609 * Promote the attribute list to the Btree format, then
610 * Commit that transaction so that the node_addname() call
611 * can manage its own transactions.
613 xfs_defer_init(args->dfops, args->firstblock);
614 error = xfs_attr3_leaf_to_node(args);
616 goto out_defer_cancel;
617 xfs_defer_ijoin(args->dfops, dp);
618 error = xfs_defer_finish(&args->trans, args->dfops);
620 goto out_defer_cancel;
623 * Commit the current trans (including the inode) and start
626 error = xfs_trans_roll_inode(&args->trans, dp);
631 * Fob the whole rest of the problem off on the Btree code.
633 error = xfs_attr_node_addname(args);
638 * Commit the transaction that added the attr name so that
639 * later routines can manage their own transactions.
641 error = xfs_trans_roll_inode(&args->trans, dp);
646 * If there was an out-of-line value, allocate the blocks we
647 * identified for its storage and copy the value. This is done
648 * after we create the attribute so that we don't overflow the
649 * maximum size of a transaction and/or hit a deadlock.
651 if (args->rmtblkno > 0) {
652 error = xfs_attr_rmtval_set(args);
658 * If this is an atomic rename operation, we must "flip" the
659 * incomplete flags on the "new" and "old" attribute/value pairs
660 * so that one disappears and one appears atomically. Then we
661 * must remove the "old" attribute/value pair.
663 if (args->op_flags & XFS_DA_OP_RENAME) {
665 * In a separate transaction, set the incomplete flag on the
666 * "old" attr and clear the incomplete flag on the "new" attr.
668 error = xfs_attr3_leaf_flipflags(args);
673 * Dismantle the "old" attribute/value pair by removing
674 * a "remote" value (if it exists).
676 args->index = args->index2;
677 args->blkno = args->blkno2;
678 args->rmtblkno = args->rmtblkno2;
679 args->rmtblkcnt = args->rmtblkcnt2;
680 args->rmtvaluelen = args->rmtvaluelen2;
681 if (args->rmtblkno) {
682 error = xfs_attr_rmtval_remove(args);
688 * Read in the block containing the "old" attr, then
689 * remove the "old" attr from that block (neat, huh!)
691 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
696 xfs_attr3_leaf_remove(bp, args);
699 * If the result is small enough, shrink it all into the inode.
701 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
702 xfs_defer_init(args->dfops, args->firstblock);
703 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
704 /* bp is gone due to xfs_da_shrink_inode */
706 goto out_defer_cancel;
707 xfs_defer_ijoin(args->dfops, dp);
708 error = xfs_defer_finish(&args->trans, args->dfops);
710 goto out_defer_cancel;
714 * Commit the remove and start the next trans in series.
716 error = xfs_trans_roll_inode(&args->trans, dp);
718 } else if (args->rmtblkno > 0) {
720 * Added a "remote" value, just clear the incomplete flag.
722 error = xfs_attr3_leaf_clearflag(args);
726 xfs_defer_cancel(args->dfops);
731 * Remove a name from the leaf attribute list structure
733 * This leaf block cannot have a "remote" value, we only call this routine
734 * if bmap_one_block() says there is only one block (ie: no remote blks).
737 xfs_attr_leaf_removename(xfs_da_args_t *args)
743 trace_xfs_attr_leaf_removename(args);
746 * Remove the attribute.
750 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
754 error = xfs_attr3_leaf_lookup_int(bp, args);
755 if (error == -ENOATTR) {
756 xfs_trans_brelse(args->trans, bp);
760 xfs_attr3_leaf_remove(bp, args);
763 * If the result is small enough, shrink it all into the inode.
765 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
766 xfs_defer_init(args->dfops, args->firstblock);
767 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
768 /* bp is gone due to xfs_da_shrink_inode */
770 goto out_defer_cancel;
771 xfs_defer_ijoin(args->dfops, dp);
772 error = xfs_defer_finish(&args->trans, args->dfops);
774 goto out_defer_cancel;
778 xfs_defer_cancel(args->dfops);
783 * Look up a name in a leaf attribute list structure.
785 * This leaf block cannot have a "remote" value, we only call this routine
786 * if bmap_one_block() says there is only one block (ie: no remote blks).
789 xfs_attr_leaf_get(xfs_da_args_t *args)
794 trace_xfs_attr_leaf_get(args);
797 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
801 error = xfs_attr3_leaf_lookup_int(bp, args);
802 if (error != -EEXIST) {
803 xfs_trans_brelse(args->trans, bp);
806 error = xfs_attr3_leaf_getvalue(bp, args);
807 xfs_trans_brelse(args->trans, bp);
808 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
809 error = xfs_attr_rmtval_get(args);
814 /*========================================================================
815 * External routines when attribute list size > geo->blksize
816 *========================================================================*/
819 * Add a name to a Btree-format attribute list.
821 * This will involve walking down the Btree, and may involve splitting
822 * leaf nodes and even splitting intermediate nodes up to and including
823 * the root node (a special case of an intermediate node).
825 * "Remote" attribute values confuse the issue and atomic rename operations
826 * add a whole extra layer of confusion on top of that.
829 xfs_attr_node_addname(xfs_da_args_t *args)
831 xfs_da_state_t *state;
832 xfs_da_state_blk_t *blk;
837 trace_xfs_attr_node_addname(args);
840 * Fill in bucket of arguments/results/context to carry around.
845 state = xfs_da_state_alloc();
850 * Search to see if name already exists, and get back a pointer
851 * to where it should go.
853 error = xfs_da3_node_lookup_int(state, &retval);
856 blk = &state->path.blk[ state->path.active-1 ];
857 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
858 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
860 } else if (retval == -EEXIST) {
861 if (args->flags & ATTR_CREATE)
864 trace_xfs_attr_node_replace(args);
866 /* save the attribute state for later removal*/
867 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
868 args->blkno2 = args->blkno; /* set 2nd entry info*/
869 args->index2 = args->index;
870 args->rmtblkno2 = args->rmtblkno;
871 args->rmtblkcnt2 = args->rmtblkcnt;
872 args->rmtvaluelen2 = args->rmtvaluelen;
875 * clear the remote attr state now that it is saved so that the
876 * values reflect the state of the attribute we are about to
877 * add, not the attribute we just found and will remove later.
881 args->rmtvaluelen = 0;
884 retval = xfs_attr3_leaf_add(blk->bp, state->args);
885 if (retval == -ENOSPC) {
886 if (state->path.active == 1) {
888 * Its really a single leaf node, but it had
889 * out-of-line values so it looked like it *might*
890 * have been a b-tree.
892 xfs_da_state_free(state);
894 xfs_defer_init(args->dfops, args->firstblock);
895 error = xfs_attr3_leaf_to_node(args);
897 goto out_defer_cancel;
898 xfs_defer_ijoin(args->dfops, dp);
899 error = xfs_defer_finish(&args->trans, args->dfops);
901 goto out_defer_cancel;
904 * Commit the node conversion and start the next
905 * trans in the chain.
907 error = xfs_trans_roll_inode(&args->trans, dp);
915 * Split as many Btree elements as required.
916 * This code tracks the new and old attr's location
917 * in the index/blkno/rmtblkno/rmtblkcnt fields and
918 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
920 xfs_defer_init(args->dfops, args->firstblock);
921 error = xfs_da3_split(state);
923 goto out_defer_cancel;
924 xfs_defer_ijoin(args->dfops, dp);
925 error = xfs_defer_finish(&args->trans, args->dfops);
927 goto out_defer_cancel;
930 * Addition succeeded, update Btree hashvals.
932 xfs_da3_fixhashpath(state, &state->path);
936 * Kill the state structure, we're done with it and need to
937 * allow the buffers to come back later.
939 xfs_da_state_free(state);
943 * Commit the leaf addition or btree split and start the next
944 * trans in the chain.
946 error = xfs_trans_roll_inode(&args->trans, dp);
951 * If there was an out-of-line value, allocate the blocks we
952 * identified for its storage and copy the value. This is done
953 * after we create the attribute so that we don't overflow the
954 * maximum size of a transaction and/or hit a deadlock.
956 if (args->rmtblkno > 0) {
957 error = xfs_attr_rmtval_set(args);
963 * If this is an atomic rename operation, we must "flip" the
964 * incomplete flags on the "new" and "old" attribute/value pairs
965 * so that one disappears and one appears atomically. Then we
966 * must remove the "old" attribute/value pair.
968 if (args->op_flags & XFS_DA_OP_RENAME) {
970 * In a separate transaction, set the incomplete flag on the
971 * "old" attr and clear the incomplete flag on the "new" attr.
973 error = xfs_attr3_leaf_flipflags(args);
978 * Dismantle the "old" attribute/value pair by removing
979 * a "remote" value (if it exists).
981 args->index = args->index2;
982 args->blkno = args->blkno2;
983 args->rmtblkno = args->rmtblkno2;
984 args->rmtblkcnt = args->rmtblkcnt2;
985 args->rmtvaluelen = args->rmtvaluelen2;
986 if (args->rmtblkno) {
987 error = xfs_attr_rmtval_remove(args);
993 * Re-find the "old" attribute entry after any split ops.
994 * The INCOMPLETE flag means that we will find the "old"
995 * attr, not the "new" one.
997 args->flags |= XFS_ATTR_INCOMPLETE;
998 state = xfs_da_state_alloc();
1002 error = xfs_da3_node_lookup_int(state, &retval);
1007 * Remove the name and update the hashvals in the tree.
1009 blk = &state->path.blk[ state->path.active-1 ];
1010 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1011 error = xfs_attr3_leaf_remove(blk->bp, args);
1012 xfs_da3_fixhashpath(state, &state->path);
1015 * Check to see if the tree needs to be collapsed.
1017 if (retval && (state->path.active > 1)) {
1018 xfs_defer_init(args->dfops, args->firstblock);
1019 error = xfs_da3_join(state);
1021 goto out_defer_cancel;
1022 xfs_defer_ijoin(args->dfops, dp);
1023 error = xfs_defer_finish(&args->trans, args->dfops);
1025 goto out_defer_cancel;
1029 * Commit and start the next trans in the chain.
1031 error = xfs_trans_roll_inode(&args->trans, dp);
1035 } else if (args->rmtblkno > 0) {
1037 * Added a "remote" value, just clear the incomplete flag.
1039 error = xfs_attr3_leaf_clearflag(args);
1047 xfs_da_state_free(state);
1052 xfs_defer_cancel(args->dfops);
1057 * Remove a name from a B-tree attribute list.
1059 * This will involve walking down the Btree, and may involve joining
1060 * leaf nodes and even joining intermediate nodes up to and including
1061 * the root node (a special case of an intermediate node).
1064 xfs_attr_node_removename(xfs_da_args_t *args)
1066 xfs_da_state_t *state;
1067 xfs_da_state_blk_t *blk;
1070 int retval, error, forkoff;
1072 trace_xfs_attr_node_removename(args);
1075 * Tie a string around our finger to remind us where we are.
1078 state = xfs_da_state_alloc();
1080 state->mp = dp->i_mount;
1083 * Search to see if name exists, and get back a pointer to it.
1085 error = xfs_da3_node_lookup_int(state, &retval);
1086 if (error || (retval != -EEXIST)) {
1093 * If there is an out-of-line value, de-allocate the blocks.
1094 * This is done before we remove the attribute so that we don't
1095 * overflow the maximum size of a transaction and/or hit a deadlock.
1097 blk = &state->path.blk[ state->path.active-1 ];
1098 ASSERT(blk->bp != NULL);
1099 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1100 if (args->rmtblkno > 0) {
1102 * Fill in disk block numbers in the state structure
1103 * so that we can get the buffers back after we commit
1104 * several transactions in the following calls.
1106 error = xfs_attr_fillstate(state);
1111 * Mark the attribute as INCOMPLETE, then bunmapi() the
1114 error = xfs_attr3_leaf_setflag(args);
1117 error = xfs_attr_rmtval_remove(args);
1122 * Refill the state structure with buffers, the prior calls
1123 * released our buffers.
1125 error = xfs_attr_refillstate(state);
1131 * Remove the name and update the hashvals in the tree.
1133 blk = &state->path.blk[ state->path.active-1 ];
1134 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1135 retval = xfs_attr3_leaf_remove(blk->bp, args);
1136 xfs_da3_fixhashpath(state, &state->path);
1139 * Check to see if the tree needs to be collapsed.
1141 if (retval && (state->path.active > 1)) {
1142 xfs_defer_init(args->dfops, args->firstblock);
1143 error = xfs_da3_join(state);
1145 goto out_defer_cancel;
1146 xfs_defer_ijoin(args->dfops, dp);
1147 error = xfs_defer_finish(&args->trans, args->dfops);
1149 goto out_defer_cancel;
1151 * Commit the Btree join operation and start a new trans.
1153 error = xfs_trans_roll_inode(&args->trans, dp);
1159 * If the result is small enough, push it all into the inode.
1161 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1163 * Have to get rid of the copy of this dabuf in the state.
1165 ASSERT(state->path.active == 1);
1166 ASSERT(state->path.blk[0].bp);
1167 state->path.blk[0].bp = NULL;
1169 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1173 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1174 xfs_defer_init(args->dfops, args->firstblock);
1175 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1176 /* bp is gone due to xfs_da_shrink_inode */
1178 goto out_defer_cancel;
1179 xfs_defer_ijoin(args->dfops, dp);
1180 error = xfs_defer_finish(&args->trans, args->dfops);
1182 goto out_defer_cancel;
1184 xfs_trans_brelse(args->trans, bp);
1189 xfs_da_state_free(state);
1192 xfs_defer_cancel(args->dfops);
1197 * Fill in the disk block numbers in the state structure for the buffers
1198 * that are attached to the state structure.
1199 * This is done so that we can quickly reattach ourselves to those buffers
1200 * after some set of transaction commits have released these buffers.
1203 xfs_attr_fillstate(xfs_da_state_t *state)
1205 xfs_da_state_path_t *path;
1206 xfs_da_state_blk_t *blk;
1209 trace_xfs_attr_fillstate(state->args);
1212 * Roll down the "path" in the state structure, storing the on-disk
1213 * block number for those buffers in the "path".
1215 path = &state->path;
1216 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1217 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1219 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1222 blk->disk_blkno = 0;
1227 * Roll down the "altpath" in the state structure, storing the on-disk
1228 * block number for those buffers in the "altpath".
1230 path = &state->altpath;
1231 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1232 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1234 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1237 blk->disk_blkno = 0;
1245 * Reattach the buffers to the state structure based on the disk block
1246 * numbers stored in the state structure.
1247 * This is done after some set of transaction commits have released those
1248 * buffers from our grip.
1251 xfs_attr_refillstate(xfs_da_state_t *state)
1253 xfs_da_state_path_t *path;
1254 xfs_da_state_blk_t *blk;
1257 trace_xfs_attr_refillstate(state->args);
1260 * Roll down the "path" in the state structure, storing the on-disk
1261 * block number for those buffers in the "path".
1263 path = &state->path;
1264 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1265 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1266 if (blk->disk_blkno) {
1267 error = xfs_da3_node_read(state->args->trans,
1269 blk->blkno, blk->disk_blkno,
1270 &blk->bp, XFS_ATTR_FORK);
1279 * Roll down the "altpath" in the state structure, storing the on-disk
1280 * block number for those buffers in the "altpath".
1282 path = &state->altpath;
1283 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1284 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1285 if (blk->disk_blkno) {
1286 error = xfs_da3_node_read(state->args->trans,
1288 blk->blkno, blk->disk_blkno,
1289 &blk->bp, XFS_ATTR_FORK);
1301 * Look up a filename in a node attribute list.
1303 * This routine gets called for any attribute fork that has more than one
1304 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1305 * "remote" values taking up more blocks.
1308 xfs_attr_node_get(xfs_da_args_t *args)
1310 xfs_da_state_t *state;
1311 xfs_da_state_blk_t *blk;
1315 trace_xfs_attr_node_get(args);
1317 state = xfs_da_state_alloc();
1319 state->mp = args->dp->i_mount;
1322 * Search to see if name exists, and get back a pointer to it.
1324 error = xfs_da3_node_lookup_int(state, &retval);
1327 } else if (retval == -EEXIST) {
1328 blk = &state->path.blk[ state->path.active-1 ];
1329 ASSERT(blk->bp != NULL);
1330 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1333 * Get the value, local or "remote"
1335 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1336 if (!retval && (args->rmtblkno > 0)
1337 && !(args->flags & ATTR_KERNOVAL)) {
1338 retval = xfs_attr_rmtval_get(args);
1343 * If not in a transaction, we have to release all the buffers.
1345 for (i = 0; i < state->path.active; i++) {
1346 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1347 state->path.blk[i].bp = NULL;
1350 xfs_da_state_free(state);