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_da_args args;
216 struct xfs_defer_ops dfops;
217 struct xfs_trans_res tres;
218 xfs_fsblock_t firstblock;
219 int rsvd = (flags & ATTR_ROOT) != 0;
220 int error, err2, local;
222 XFS_STATS_INC(mp, xs_attr_set);
224 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
227 error = xfs_attr_args_init(&args, dp, name, flags);
232 args.valuelen = valuelen;
233 args.firstblock = &firstblock;
235 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
236 args.total = xfs_attr_calc_size(&args, &local);
238 error = xfs_qm_dqattach(dp, 0);
243 * If the inode doesn't have an attribute fork, add one.
244 * (inode must not be locked when we call this routine)
246 if (XFS_IFORK_Q(dp) == 0) {
247 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
248 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
250 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
255 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
256 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
257 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
258 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
261 * Root fork attributes can use reserved data blocks for this
262 * operation if necessary
264 error = xfs_trans_alloc(mp, &tres, args.total, 0,
265 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
269 xfs_ilock(dp, XFS_ILOCK_EXCL);
270 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
271 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
272 XFS_QMOPT_RES_REGBLKS);
274 xfs_iunlock(dp, XFS_ILOCK_EXCL);
275 xfs_trans_cancel(args.trans);
279 xfs_trans_ijoin(args.trans, dp, 0);
282 * If the attribute list is non-existent or a shortform list,
283 * upgrade it to a single-leaf-block attribute list.
285 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
286 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
287 dp->i_d.di_anextents == 0)) {
290 * Build initial attribute list (if required).
292 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
293 xfs_attr_shortform_create(&args);
296 * Try to add the attr to the attribute list in
299 error = xfs_attr_shortform_addname(&args);
300 if (error != -ENOSPC) {
302 * Commit the shortform mods, and we're done.
303 * NOTE: this is also the error path (EEXIST, etc).
305 ASSERT(args.trans != NULL);
308 * If this is a synchronous mount, make sure that
309 * the transaction goes to disk before returning
312 if (mp->m_flags & XFS_MOUNT_WSYNC)
313 xfs_trans_set_sync(args.trans);
315 if (!error && (flags & ATTR_KERNOTIME) == 0) {
316 xfs_trans_ichgtime(args.trans, dp,
319 err2 = xfs_trans_commit(args.trans);
320 xfs_iunlock(dp, XFS_ILOCK_EXCL);
322 return error ? error : err2;
326 * It won't fit in the shortform, transform to a leaf block.
327 * GROT: another possible req'mt for a double-split btree op.
329 xfs_defer_init(args.dfops, args.firstblock);
330 error = xfs_attr_shortform_to_leaf(&args);
332 goto out_defer_cancel;
333 xfs_defer_ijoin(args.dfops, dp);
334 error = xfs_defer_finish(&args.trans, args.dfops);
336 goto out_defer_cancel;
339 * Commit the leaf transformation. We'll need another (linked)
340 * transaction to add the new attribute to the leaf.
343 error = xfs_trans_roll_inode(&args.trans, dp);
349 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
350 error = xfs_attr_leaf_addname(&args);
352 error = xfs_attr_node_addname(&args);
357 * If this is a synchronous mount, make sure that the
358 * transaction goes to disk before returning to the user.
360 if (mp->m_flags & XFS_MOUNT_WSYNC)
361 xfs_trans_set_sync(args.trans);
363 if ((flags & ATTR_KERNOTIME) == 0)
364 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
367 * Commit the last in the sequence of transactions.
369 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
370 error = xfs_trans_commit(args.trans);
371 xfs_iunlock(dp, XFS_ILOCK_EXCL);
376 xfs_defer_cancel(&dfops);
380 xfs_trans_cancel(args.trans);
381 xfs_iunlock(dp, XFS_ILOCK_EXCL);
386 * Generic handler routine to remove a name from an attribute list.
387 * Transitions attribute list from Btree to shortform as necessary.
391 struct xfs_inode *dp,
392 const unsigned char *name,
395 struct xfs_mount *mp = dp->i_mount;
396 struct xfs_da_args args;
397 struct xfs_defer_ops dfops;
398 xfs_fsblock_t firstblock;
401 XFS_STATS_INC(mp, xs_attr_remove);
403 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
406 error = xfs_attr_args_init(&args, dp, name, flags);
410 args.firstblock = &firstblock;
414 * we have no control over the attribute names that userspace passes us
415 * to remove, so we have to allow the name lookup prior to attribute
418 args.op_flags = XFS_DA_OP_OKNOENT;
420 error = xfs_qm_dqattach(dp, 0);
425 * Root fork attributes can use reserved data blocks for this
426 * operation if necessary
428 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
429 XFS_ATTRRM_SPACE_RES(mp), 0,
430 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
435 xfs_ilock(dp, XFS_ILOCK_EXCL);
437 * No need to make quota reservations here. We expect to release some
438 * blocks not allocate in the common case.
440 xfs_trans_ijoin(args.trans, dp, 0);
442 if (!xfs_inode_hasattr(dp)) {
444 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
445 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
446 error = xfs_attr_shortform_remove(&args);
447 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
448 error = xfs_attr_leaf_removename(&args);
450 error = xfs_attr_node_removename(&args);
457 * If this is a synchronous mount, make sure that the
458 * transaction goes to disk before returning to the user.
460 if (mp->m_flags & XFS_MOUNT_WSYNC)
461 xfs_trans_set_sync(args.trans);
463 if ((flags & ATTR_KERNOTIME) == 0)
464 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
467 * Commit the last in the sequence of transactions.
469 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
470 error = xfs_trans_commit(args.trans);
471 xfs_iunlock(dp, XFS_ILOCK_EXCL);
477 xfs_trans_cancel(args.trans);
478 xfs_iunlock(dp, XFS_ILOCK_EXCL);
482 /*========================================================================
483 * External routines when attribute list is inside the inode
484 *========================================================================*/
487 * Add a name to the shortform attribute list structure
488 * This is the external routine.
491 xfs_attr_shortform_addname(xfs_da_args_t *args)
493 int newsize, forkoff, retval;
495 trace_xfs_attr_sf_addname(args);
497 retval = xfs_attr_shortform_lookup(args);
498 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
500 } else if (retval == -EEXIST) {
501 if (args->flags & ATTR_CREATE)
503 retval = xfs_attr_shortform_remove(args);
507 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
508 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
511 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
512 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
514 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
518 xfs_attr_shortform_add(args, forkoff);
523 /*========================================================================
524 * External routines when attribute list is one block
525 *========================================================================*/
528 * Add a name to the leaf attribute list structure
530 * This leaf block cannot have a "remote" value, we only call this routine
531 * if bmap_one_block() says there is only one block (ie: no remote blks).
534 xfs_attr_leaf_addname(xfs_da_args_t *args)
538 int retval, error, forkoff;
540 trace_xfs_attr_leaf_addname(args);
543 * Read the (only) block in the attribute list in.
547 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
552 * Look up the given attribute in the leaf block. Figure out if
553 * the given flags produce an error or call for an atomic rename.
555 retval = xfs_attr3_leaf_lookup_int(bp, args);
556 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
557 xfs_trans_brelse(args->trans, bp);
559 } else if (retval == -EEXIST) {
560 if (args->flags & ATTR_CREATE) { /* pure create op */
561 xfs_trans_brelse(args->trans, bp);
565 trace_xfs_attr_leaf_replace(args);
567 /* save the attribute state for later removal*/
568 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
569 args->blkno2 = args->blkno; /* set 2nd entry info*/
570 args->index2 = args->index;
571 args->rmtblkno2 = args->rmtblkno;
572 args->rmtblkcnt2 = args->rmtblkcnt;
573 args->rmtvaluelen2 = args->rmtvaluelen;
576 * clear the remote attr state now that it is saved so that the
577 * values reflect the state of the attribute we are about to
578 * add, not the attribute we just found and will remove later.
582 args->rmtvaluelen = 0;
586 * Add the attribute to the leaf block, transitioning to a Btree
589 retval = xfs_attr3_leaf_add(bp, args);
590 if (retval == -ENOSPC) {
592 * Promote the attribute list to the Btree format, then
593 * Commit that transaction so that the node_addname() call
594 * can manage its own transactions.
596 xfs_defer_init(args->dfops, args->firstblock);
597 error = xfs_attr3_leaf_to_node(args);
599 goto out_defer_cancel;
600 xfs_defer_ijoin(args->dfops, dp);
601 error = xfs_defer_finish(&args->trans, args->dfops);
603 goto out_defer_cancel;
606 * Commit the current trans (including the inode) and start
609 error = xfs_trans_roll_inode(&args->trans, dp);
614 * Fob the whole rest of the problem off on the Btree code.
616 error = xfs_attr_node_addname(args);
621 * Commit the transaction that added the attr name so that
622 * later routines can manage their own transactions.
624 error = xfs_trans_roll_inode(&args->trans, dp);
629 * If there was an out-of-line value, allocate the blocks we
630 * identified for its storage and copy the value. This is done
631 * after we create the attribute so that we don't overflow the
632 * maximum size of a transaction and/or hit a deadlock.
634 if (args->rmtblkno > 0) {
635 error = xfs_attr_rmtval_set(args);
641 * If this is an atomic rename operation, we must "flip" the
642 * incomplete flags on the "new" and "old" attribute/value pairs
643 * so that one disappears and one appears atomically. Then we
644 * must remove the "old" attribute/value pair.
646 if (args->op_flags & XFS_DA_OP_RENAME) {
648 * In a separate transaction, set the incomplete flag on the
649 * "old" attr and clear the incomplete flag on the "new" attr.
651 error = xfs_attr3_leaf_flipflags(args);
656 * Dismantle the "old" attribute/value pair by removing
657 * a "remote" value (if it exists).
659 args->index = args->index2;
660 args->blkno = args->blkno2;
661 args->rmtblkno = args->rmtblkno2;
662 args->rmtblkcnt = args->rmtblkcnt2;
663 args->rmtvaluelen = args->rmtvaluelen2;
664 if (args->rmtblkno) {
665 error = xfs_attr_rmtval_remove(args);
671 * Read in the block containing the "old" attr, then
672 * remove the "old" attr from that block (neat, huh!)
674 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
679 xfs_attr3_leaf_remove(bp, args);
682 * If the result is small enough, shrink it all into the inode.
684 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
685 xfs_defer_init(args->dfops, args->firstblock);
686 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
687 /* bp is gone due to xfs_da_shrink_inode */
689 goto out_defer_cancel;
690 xfs_defer_ijoin(args->dfops, dp);
691 error = xfs_defer_finish(&args->trans, args->dfops);
693 goto out_defer_cancel;
697 * Commit the remove and start the next trans in series.
699 error = xfs_trans_roll_inode(&args->trans, dp);
701 } else if (args->rmtblkno > 0) {
703 * Added a "remote" value, just clear the incomplete flag.
705 error = xfs_attr3_leaf_clearflag(args);
709 xfs_defer_cancel(args->dfops);
715 * Remove a name from the leaf attribute list structure
717 * This leaf block cannot have a "remote" value, we only call this routine
718 * if bmap_one_block() says there is only one block (ie: no remote blks).
721 xfs_attr_leaf_removename(xfs_da_args_t *args)
727 trace_xfs_attr_leaf_removename(args);
730 * Remove the attribute.
734 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
738 error = xfs_attr3_leaf_lookup_int(bp, args);
739 if (error == -ENOATTR) {
740 xfs_trans_brelse(args->trans, bp);
744 xfs_attr3_leaf_remove(bp, args);
747 * If the result is small enough, shrink it all into the inode.
749 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
750 xfs_defer_init(args->dfops, args->firstblock);
751 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
752 /* bp is gone due to xfs_da_shrink_inode */
754 goto out_defer_cancel;
755 xfs_defer_ijoin(args->dfops, dp);
756 error = xfs_defer_finish(&args->trans, args->dfops);
758 goto out_defer_cancel;
762 xfs_defer_cancel(args->dfops);
768 * Look up a name in a leaf attribute list structure.
770 * This leaf block cannot have a "remote" value, we only call this routine
771 * if bmap_one_block() says there is only one block (ie: no remote blks).
774 xfs_attr_leaf_get(xfs_da_args_t *args)
779 trace_xfs_attr_leaf_get(args);
782 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
786 error = xfs_attr3_leaf_lookup_int(bp, args);
787 if (error != -EEXIST) {
788 xfs_trans_brelse(args->trans, bp);
791 error = xfs_attr3_leaf_getvalue(bp, args);
792 xfs_trans_brelse(args->trans, bp);
793 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
794 error = xfs_attr_rmtval_get(args);
799 /*========================================================================
800 * External routines when attribute list size > geo->blksize
801 *========================================================================*/
804 * Add a name to a Btree-format attribute list.
806 * This will involve walking down the Btree, and may involve splitting
807 * leaf nodes and even splitting intermediate nodes up to and including
808 * the root node (a special case of an intermediate node).
810 * "Remote" attribute values confuse the issue and atomic rename operations
811 * add a whole extra layer of confusion on top of that.
814 xfs_attr_node_addname(xfs_da_args_t *args)
816 xfs_da_state_t *state;
817 xfs_da_state_blk_t *blk;
822 trace_xfs_attr_node_addname(args);
825 * Fill in bucket of arguments/results/context to carry around.
830 state = xfs_da_state_alloc();
835 * Search to see if name already exists, and get back a pointer
836 * to where it should go.
838 error = xfs_da3_node_lookup_int(state, &retval);
841 blk = &state->path.blk[ state->path.active-1 ];
842 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
843 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
845 } else if (retval == -EEXIST) {
846 if (args->flags & ATTR_CREATE)
849 trace_xfs_attr_node_replace(args);
851 /* save the attribute state for later removal*/
852 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
853 args->blkno2 = args->blkno; /* set 2nd entry info*/
854 args->index2 = args->index;
855 args->rmtblkno2 = args->rmtblkno;
856 args->rmtblkcnt2 = args->rmtblkcnt;
857 args->rmtvaluelen2 = args->rmtvaluelen;
860 * clear the remote attr state now that it is saved so that the
861 * values reflect the state of the attribute we are about to
862 * add, not the attribute we just found and will remove later.
866 args->rmtvaluelen = 0;
869 retval = xfs_attr3_leaf_add(blk->bp, state->args);
870 if (retval == -ENOSPC) {
871 if (state->path.active == 1) {
873 * Its really a single leaf node, but it had
874 * out-of-line values so it looked like it *might*
875 * have been a b-tree.
877 xfs_da_state_free(state);
879 xfs_defer_init(args->dfops, args->firstblock);
880 error = xfs_attr3_leaf_to_node(args);
882 goto out_defer_cancel;
883 xfs_defer_ijoin(args->dfops, dp);
884 error = xfs_defer_finish(&args->trans, args->dfops);
886 goto out_defer_cancel;
889 * Commit the node conversion and start the next
890 * trans in the chain.
892 error = xfs_trans_roll_inode(&args->trans, dp);
900 * Split as many Btree elements as required.
901 * This code tracks the new and old attr's location
902 * in the index/blkno/rmtblkno/rmtblkcnt fields and
903 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
905 xfs_defer_init(args->dfops, args->firstblock);
906 error = xfs_da3_split(state);
908 goto out_defer_cancel;
909 xfs_defer_ijoin(args->dfops, dp);
910 error = xfs_defer_finish(&args->trans, args->dfops);
912 goto out_defer_cancel;
915 * Addition succeeded, update Btree hashvals.
917 xfs_da3_fixhashpath(state, &state->path);
921 * Kill the state structure, we're done with it and need to
922 * allow the buffers to come back later.
924 xfs_da_state_free(state);
928 * Commit the leaf addition or btree split and start the next
929 * trans in the chain.
931 error = xfs_trans_roll_inode(&args->trans, dp);
936 * If there was an out-of-line value, allocate the blocks we
937 * identified for its storage and copy the value. This is done
938 * after we create the attribute so that we don't overflow the
939 * maximum size of a transaction and/or hit a deadlock.
941 if (args->rmtblkno > 0) {
942 error = xfs_attr_rmtval_set(args);
948 * If this is an atomic rename operation, we must "flip" the
949 * incomplete flags on the "new" and "old" attribute/value pairs
950 * so that one disappears and one appears atomically. Then we
951 * must remove the "old" attribute/value pair.
953 if (args->op_flags & XFS_DA_OP_RENAME) {
955 * In a separate transaction, set the incomplete flag on the
956 * "old" attr and clear the incomplete flag on the "new" attr.
958 error = xfs_attr3_leaf_flipflags(args);
963 * Dismantle the "old" attribute/value pair by removing
964 * a "remote" value (if it exists).
966 args->index = args->index2;
967 args->blkno = args->blkno2;
968 args->rmtblkno = args->rmtblkno2;
969 args->rmtblkcnt = args->rmtblkcnt2;
970 args->rmtvaluelen = args->rmtvaluelen2;
971 if (args->rmtblkno) {
972 error = xfs_attr_rmtval_remove(args);
978 * Re-find the "old" attribute entry after any split ops.
979 * The INCOMPLETE flag means that we will find the "old"
980 * attr, not the "new" one.
982 args->flags |= XFS_ATTR_INCOMPLETE;
983 state = xfs_da_state_alloc();
987 error = xfs_da3_node_lookup_int(state, &retval);
992 * Remove the name and update the hashvals in the tree.
994 blk = &state->path.blk[ state->path.active-1 ];
995 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
996 error = xfs_attr3_leaf_remove(blk->bp, args);
997 xfs_da3_fixhashpath(state, &state->path);
1000 * Check to see if the tree needs to be collapsed.
1002 if (retval && (state->path.active > 1)) {
1003 xfs_defer_init(args->dfops, args->firstblock);
1004 error = xfs_da3_join(state);
1006 goto out_defer_cancel;
1007 xfs_defer_ijoin(args->dfops, dp);
1008 error = xfs_defer_finish(&args->trans, args->dfops);
1010 goto out_defer_cancel;
1014 * Commit and start the next trans in the chain.
1016 error = xfs_trans_roll_inode(&args->trans, dp);
1020 } else if (args->rmtblkno > 0) {
1022 * Added a "remote" value, just clear the incomplete flag.
1024 error = xfs_attr3_leaf_clearflag(args);
1032 xfs_da_state_free(state);
1037 xfs_defer_cancel(args->dfops);
1043 * Remove a name from a B-tree attribute list.
1045 * This will involve walking down the Btree, and may involve joining
1046 * leaf nodes and even joining intermediate nodes up to and including
1047 * the root node (a special case of an intermediate node).
1050 xfs_attr_node_removename(xfs_da_args_t *args)
1052 xfs_da_state_t *state;
1053 xfs_da_state_blk_t *blk;
1056 int retval, error, forkoff;
1058 trace_xfs_attr_node_removename(args);
1061 * Tie a string around our finger to remind us where we are.
1064 state = xfs_da_state_alloc();
1066 state->mp = dp->i_mount;
1069 * Search to see if name exists, and get back a pointer to it.
1071 error = xfs_da3_node_lookup_int(state, &retval);
1072 if (error || (retval != -EEXIST)) {
1079 * If there is an out-of-line value, de-allocate the blocks.
1080 * This is done before we remove the attribute so that we don't
1081 * overflow the maximum size of a transaction and/or hit a deadlock.
1083 blk = &state->path.blk[ state->path.active-1 ];
1084 ASSERT(blk->bp != NULL);
1085 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1086 if (args->rmtblkno > 0) {
1088 * Fill in disk block numbers in the state structure
1089 * so that we can get the buffers back after we commit
1090 * several transactions in the following calls.
1092 error = xfs_attr_fillstate(state);
1097 * Mark the attribute as INCOMPLETE, then bunmapi() the
1100 error = xfs_attr3_leaf_setflag(args);
1103 error = xfs_attr_rmtval_remove(args);
1108 * Refill the state structure with buffers, the prior calls
1109 * released our buffers.
1111 error = xfs_attr_refillstate(state);
1117 * Remove the name and update the hashvals in the tree.
1119 blk = &state->path.blk[ state->path.active-1 ];
1120 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1121 retval = xfs_attr3_leaf_remove(blk->bp, args);
1122 xfs_da3_fixhashpath(state, &state->path);
1125 * Check to see if the tree needs to be collapsed.
1127 if (retval && (state->path.active > 1)) {
1128 xfs_defer_init(args->dfops, args->firstblock);
1129 error = xfs_da3_join(state);
1131 goto out_defer_cancel;
1132 xfs_defer_ijoin(args->dfops, dp);
1133 error = xfs_defer_finish(&args->trans, args->dfops);
1135 goto out_defer_cancel;
1137 * Commit the Btree join operation and start a new trans.
1139 error = xfs_trans_roll_inode(&args->trans, dp);
1145 * If the result is small enough, push it all into the inode.
1147 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1149 * Have to get rid of the copy of this dabuf in the state.
1151 ASSERT(state->path.active == 1);
1152 ASSERT(state->path.blk[0].bp);
1153 state->path.blk[0].bp = NULL;
1155 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1159 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1160 xfs_defer_init(args->dfops, args->firstblock);
1161 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1162 /* bp is gone due to xfs_da_shrink_inode */
1164 goto out_defer_cancel;
1165 xfs_defer_ijoin(args->dfops, dp);
1166 error = xfs_defer_finish(&args->trans, args->dfops);
1168 goto out_defer_cancel;
1170 xfs_trans_brelse(args->trans, bp);
1175 xfs_da_state_free(state);
1178 xfs_defer_cancel(args->dfops);
1184 * Fill in the disk block numbers in the state structure for the buffers
1185 * that are attached to the state structure.
1186 * This is done so that we can quickly reattach ourselves to those buffers
1187 * after some set of transaction commits have released these buffers.
1190 xfs_attr_fillstate(xfs_da_state_t *state)
1192 xfs_da_state_path_t *path;
1193 xfs_da_state_blk_t *blk;
1196 trace_xfs_attr_fillstate(state->args);
1199 * Roll down the "path" in the state structure, storing the on-disk
1200 * block number for those buffers in the "path".
1202 path = &state->path;
1203 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1204 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1206 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1209 blk->disk_blkno = 0;
1214 * Roll down the "altpath" in the state structure, storing the on-disk
1215 * block number for those buffers in the "altpath".
1217 path = &state->altpath;
1218 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1219 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1221 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1224 blk->disk_blkno = 0;
1232 * Reattach the buffers to the state structure based on the disk block
1233 * numbers stored in the state structure.
1234 * This is done after some set of transaction commits have released those
1235 * buffers from our grip.
1238 xfs_attr_refillstate(xfs_da_state_t *state)
1240 xfs_da_state_path_t *path;
1241 xfs_da_state_blk_t *blk;
1244 trace_xfs_attr_refillstate(state->args);
1247 * Roll down the "path" in the state structure, storing the on-disk
1248 * block number for those buffers in the "path".
1250 path = &state->path;
1251 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1252 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1253 if (blk->disk_blkno) {
1254 error = xfs_da3_node_read(state->args->trans,
1256 blk->blkno, blk->disk_blkno,
1257 &blk->bp, XFS_ATTR_FORK);
1266 * Roll down the "altpath" in the state structure, storing the on-disk
1267 * block number for those buffers in the "altpath".
1269 path = &state->altpath;
1270 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1271 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1272 if (blk->disk_blkno) {
1273 error = xfs_da3_node_read(state->args->trans,
1275 blk->blkno, blk->disk_blkno,
1276 &blk->bp, XFS_ATTR_FORK);
1288 * Look up a filename in a node attribute list.
1290 * This routine gets called for any attribute fork that has more than one
1291 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1292 * "remote" values taking up more blocks.
1295 xfs_attr_node_get(xfs_da_args_t *args)
1297 xfs_da_state_t *state;
1298 xfs_da_state_blk_t *blk;
1302 trace_xfs_attr_node_get(args);
1304 state = xfs_da_state_alloc();
1306 state->mp = args->dp->i_mount;
1309 * Search to see if name exists, and get back a pointer to it.
1311 error = xfs_da3_node_lookup_int(state, &retval);
1314 } else if (retval == -EEXIST) {
1315 blk = &state->path.blk[ state->path.active-1 ];
1316 ASSERT(blk->bp != NULL);
1317 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1320 * Get the value, local or "remote"
1322 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1323 if (!retval && (args->rmtblkno > 0)
1324 && !(args->flags & ATTR_KERNOVAL)) {
1325 retval = xfs_attr_rmtval_get(args);
1330 * If not in a transaction, we have to release all the buffers.
1332 for (i = 0; i < state->path.active; i++) {
1333 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1334 state->path.blk[i].bp = NULL;
1337 xfs_da_state_free(state);