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"
27 #include "xfs_mount.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_attr_sf.h"
31 #include "xfs_inode.h"
32 #include "xfs_alloc.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
36 #include "xfs_bmap_util.h"
37 #include "xfs_bmap_btree.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_attr_remote.h"
41 #include "xfs_error.h"
42 #include "xfs_quota.h"
43 #include "xfs_trans_space.h"
44 #include "xfs_trace.h"
45 #include "xfs_dinode.h"
50 * Provide the external interfaces to manage attribute lists.
53 /*========================================================================
54 * Function prototypes for the kernel.
55 *========================================================================*/
58 * Internal routines when attribute list fits inside the inode.
60 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
63 * Internal routines when attribute list is one block.
65 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
66 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
67 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
70 * Internal routines when attribute list is more than one block.
72 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
73 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
74 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
75 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
76 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
80 xfs_attr_name_to_xname(
81 struct xfs_name *xname,
82 const unsigned char *aname)
87 xname->len = strlen((char *)aname);
88 if (xname->len >= MAXNAMELEN)
89 return EFAULT; /* match IRIX behaviour */
98 if (!XFS_IFORK_Q(ip) ||
99 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
100 ip->i_d.di_anextents == 0))
105 /*========================================================================
106 * Overall external interface routines.
107 *========================================================================*/
111 struct xfs_inode *ip,
112 const unsigned char *name,
113 unsigned char *value,
117 struct xfs_da_args args;
118 struct xfs_name xname;
122 XFS_STATS_INC(xs_attr_get);
124 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
127 if (!xfs_inode_hasattr(ip))
130 error = xfs_attr_name_to_xname(&xname, name);
134 memset(&args, 0, sizeof(args));
135 args.name = xname.name;
136 args.namelen = xname.len;
138 args.valuelen = *valuelenp;
140 args.hashval = xfs_da_hashname(args.name, args.namelen);
142 args.whichfork = XFS_ATTR_FORK;
144 lock_mode = xfs_ilock_attr_map_shared(ip);
145 if (!xfs_inode_hasattr(ip))
147 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
148 error = xfs_attr_shortform_getvalue(&args);
149 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
150 error = xfs_attr_leaf_get(&args);
152 error = xfs_attr_node_get(&args);
153 xfs_iunlock(ip, lock_mode);
155 *valuelenp = args.valuelen;
156 return error == EEXIST ? 0 : error;
160 * Calculate how many blocks we need for the new attribute,
164 struct xfs_inode *ip,
169 struct xfs_mount *mp = ip->i_mount;
174 * Determine space new attribute will use, and if it would be
175 * "local" or "remote" (note: local != inline).
177 size = xfs_attr_leaf_newentsize(namelen, valuelen,
178 mp->m_sb.sb_blocksize, local);
180 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
182 if (size > (mp->m_sb.sb_blocksize >> 1)) {
183 /* Double split possible */
188 * Out of line attribute, cannot double split, but
189 * make room for the attribute value itself.
191 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
193 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
201 struct xfs_inode *dp,
202 const unsigned char *name,
203 unsigned char *value,
207 struct xfs_mount *mp = dp->i_mount;
208 struct xfs_da_args args;
209 struct xfs_bmap_free flist;
210 struct xfs_trans_res tres;
211 struct xfs_name xname;
212 xfs_fsblock_t firstblock;
213 int rsvd = (flags & ATTR_ROOT) != 0;
214 int error, err2, committed, local;
216 XFS_STATS_INC(xs_attr_set);
218 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
221 error = xfs_attr_name_to_xname(&xname, name);
225 error = xfs_qm_dqattach(dp, 0);
230 * If the inode doesn't have an attribute fork, add one.
231 * (inode must not be locked when we call this routine)
233 if (XFS_IFORK_Q(dp) == 0) {
234 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
235 XFS_ATTR_SF_ENTSIZE_BYNAME(xname.len, valuelen);
237 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
242 memset(&args, 0, sizeof(args));
243 args.name = xname.name;
244 args.namelen = xname.len;
246 args.valuelen = valuelen;
248 args.hashval = xfs_da_hashname(args.name, args.namelen);
250 args.firstblock = &firstblock;
252 args.whichfork = XFS_ATTR_FORK;
253 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
255 /* Size is now blocks for attribute data */
256 args.total = xfs_attr_calc_size(dp, xname.len, valuelen, &local);
259 * Start our first transaction of the day.
261 * All future transactions during this code must be "chained" off
262 * this one via the trans_dup() call. All transactions will contain
263 * the inode, and the inode will always be marked with trans_ihold().
264 * Since the inode will be locked in all transactions, we must log
265 * the inode in every transaction to let it float upward through
268 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
271 * Root fork attributes can use reserved data blocks for this
272 * operation if necessary
276 args.trans->t_flags |= XFS_TRANS_RESERVE;
278 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
279 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
280 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
281 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
282 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
284 xfs_trans_cancel(args.trans, 0);
287 xfs_ilock(dp, XFS_ILOCK_EXCL);
289 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
290 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
291 XFS_QMOPT_RES_REGBLKS);
293 xfs_iunlock(dp, XFS_ILOCK_EXCL);
294 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
298 xfs_trans_ijoin(args.trans, dp, 0);
301 * If the attribute list is non-existent or a shortform list,
302 * upgrade it to a single-leaf-block attribute list.
304 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
305 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
306 dp->i_d.di_anextents == 0)) {
309 * Build initial attribute list (if required).
311 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
312 xfs_attr_shortform_create(&args);
315 * Try to add the attr to the attribute list in
318 error = xfs_attr_shortform_addname(&args);
319 if (error != ENOSPC) {
321 * Commit the shortform mods, and we're done.
322 * NOTE: this is also the error path (EEXIST, etc).
324 ASSERT(args.trans != NULL);
327 * If this is a synchronous mount, make sure that
328 * the transaction goes to disk before returning
331 if (mp->m_flags & XFS_MOUNT_WSYNC)
332 xfs_trans_set_sync(args.trans);
334 if (!error && (flags & ATTR_KERNOTIME) == 0) {
335 xfs_trans_ichgtime(args.trans, dp,
338 err2 = xfs_trans_commit(args.trans,
339 XFS_TRANS_RELEASE_LOG_RES);
340 xfs_iunlock(dp, XFS_ILOCK_EXCL);
342 return error ? error : err2;
346 * It won't fit in the shortform, transform to a leaf block.
347 * GROT: another possible req'mt for a double-split btree op.
349 xfs_bmap_init(args.flist, args.firstblock);
350 error = xfs_attr_shortform_to_leaf(&args);
352 error = xfs_bmap_finish(&args.trans, args.flist,
358 xfs_bmap_cancel(&flist);
363 * bmap_finish() may have committed the last trans and started
364 * a new one. We need the inode to be in all transactions.
367 xfs_trans_ijoin(args.trans, dp, 0);
370 * Commit the leaf transformation. We'll need another (linked)
371 * transaction to add the new attribute to the leaf.
374 error = xfs_trans_roll(&args.trans, dp);
380 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
381 error = xfs_attr_leaf_addname(&args);
383 error = xfs_attr_node_addname(&args);
388 * If this is a synchronous mount, make sure that the
389 * transaction goes to disk before returning to the user.
391 if (mp->m_flags & XFS_MOUNT_WSYNC)
392 xfs_trans_set_sync(args.trans);
394 if ((flags & ATTR_KERNOTIME) == 0)
395 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
398 * Commit the last in the sequence of transactions.
400 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
401 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
402 xfs_iunlock(dp, XFS_ILOCK_EXCL);
408 xfs_trans_cancel(args.trans,
409 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
411 xfs_iunlock(dp, XFS_ILOCK_EXCL);
416 * Generic handler routine to remove a name from an attribute list.
417 * Transitions attribute list from Btree to shortform as necessary.
420 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
423 xfs_fsblock_t firstblock;
424 xfs_bmap_free_t flist;
426 xfs_mount_t *mp = dp->i_mount;
429 * Fill in the arg structure for this request.
431 memset((char *)&args, 0, sizeof(args));
432 args.name = name->name;
433 args.namelen = name->len;
435 args.hashval = xfs_da_hashname(args.name, args.namelen);
437 args.firstblock = &firstblock;
440 args.whichfork = XFS_ATTR_FORK;
443 * we have no control over the attribute names that userspace passes us
444 * to remove, so we have to allow the name lookup prior to attribute
447 args.op_flags = XFS_DA_OP_OKNOENT;
450 * Attach the dquots to the inode.
452 error = xfs_qm_dqattach(dp, 0);
457 * Start our first transaction of the day.
459 * All future transactions during this code must be "chained" off
460 * this one via the trans_dup() call. All transactions will contain
461 * the inode, and the inode will always be marked with trans_ihold().
462 * Since the inode will be locked in all transactions, we must log
463 * the inode in every transaction to let it float upward through
466 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
469 * Root fork attributes can use reserved data blocks for this
470 * operation if necessary
473 if (flags & ATTR_ROOT)
474 args.trans->t_flags |= XFS_TRANS_RESERVE;
476 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
477 XFS_ATTRRM_SPACE_RES(mp), 0);
479 xfs_trans_cancel(args.trans, 0);
483 xfs_ilock(dp, XFS_ILOCK_EXCL);
485 * No need to make quota reservations here. We expect to release some
486 * blocks not allocate in the common case.
488 xfs_trans_ijoin(args.trans, dp, 0);
491 * Decide on what work routines to call based on the inode size.
493 if (!xfs_inode_hasattr(dp)) {
494 error = XFS_ERROR(ENOATTR);
497 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
498 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
499 error = xfs_attr_shortform_remove(&args);
503 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
504 error = xfs_attr_leaf_removename(&args);
506 error = xfs_attr_node_removename(&args);
513 * If this is a synchronous mount, make sure that the
514 * transaction goes to disk before returning to the user.
516 if (mp->m_flags & XFS_MOUNT_WSYNC) {
517 xfs_trans_set_sync(args.trans);
520 if ((flags & ATTR_KERNOTIME) == 0)
521 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
524 * Commit the last in the sequence of transactions.
526 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
527 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
528 xfs_iunlock(dp, XFS_ILOCK_EXCL);
534 xfs_trans_cancel(args.trans,
535 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
536 xfs_iunlock(dp, XFS_ILOCK_EXCL);
543 const unsigned char *name,
547 struct xfs_name xname;
549 XFS_STATS_INC(xs_attr_remove);
551 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
554 error = xfs_attr_name_to_xname(&xname, name);
558 xfs_ilock(dp, XFS_ILOCK_SHARED);
559 if (!xfs_inode_hasattr(dp)) {
560 xfs_iunlock(dp, XFS_ILOCK_SHARED);
561 return XFS_ERROR(ENOATTR);
563 xfs_iunlock(dp, XFS_ILOCK_SHARED);
565 return xfs_attr_remove_int(dp, &xname, flags);
569 /*========================================================================
570 * External routines when attribute list is inside the inode
571 *========================================================================*/
574 * Add a name to the shortform attribute list structure
575 * This is the external routine.
578 xfs_attr_shortform_addname(xfs_da_args_t *args)
580 int newsize, forkoff, retval;
582 trace_xfs_attr_sf_addname(args);
584 retval = xfs_attr_shortform_lookup(args);
585 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
587 } else if (retval == EEXIST) {
588 if (args->flags & ATTR_CREATE)
590 retval = xfs_attr_shortform_remove(args);
594 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
595 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
596 return(XFS_ERROR(ENOSPC));
598 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
599 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
601 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
603 return(XFS_ERROR(ENOSPC));
605 xfs_attr_shortform_add(args, forkoff);
610 /*========================================================================
611 * External routines when attribute list is one block
612 *========================================================================*/
615 * Add a name to the leaf attribute list structure
617 * This leaf block cannot have a "remote" value, we only call this routine
618 * if bmap_one_block() says there is only one block (ie: no remote blks).
621 xfs_attr_leaf_addname(xfs_da_args_t *args)
625 int retval, error, committed, forkoff;
627 trace_xfs_attr_leaf_addname(args);
630 * Read the (only) block in the attribute list in.
634 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
639 * Look up the given attribute in the leaf block. Figure out if
640 * the given flags produce an error or call for an atomic rename.
642 retval = xfs_attr3_leaf_lookup_int(bp, args);
643 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
644 xfs_trans_brelse(args->trans, bp);
646 } else if (retval == EEXIST) {
647 if (args->flags & ATTR_CREATE) { /* pure create op */
648 xfs_trans_brelse(args->trans, bp);
652 trace_xfs_attr_leaf_replace(args);
654 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
655 args->blkno2 = args->blkno; /* set 2nd entry info*/
656 args->index2 = args->index;
657 args->rmtblkno2 = args->rmtblkno;
658 args->rmtblkcnt2 = args->rmtblkcnt;
662 * Add the attribute to the leaf block, transitioning to a Btree
665 retval = xfs_attr3_leaf_add(bp, args);
666 if (retval == ENOSPC) {
668 * Promote the attribute list to the Btree format, then
669 * Commit that transaction so that the node_addname() call
670 * can manage its own transactions.
672 xfs_bmap_init(args->flist, args->firstblock);
673 error = xfs_attr3_leaf_to_node(args);
675 error = xfs_bmap_finish(&args->trans, args->flist,
681 xfs_bmap_cancel(args->flist);
686 * bmap_finish() may have committed the last trans and started
687 * a new one. We need the inode to be in all transactions.
690 xfs_trans_ijoin(args->trans, dp, 0);
693 * Commit the current trans (including the inode) and start
696 error = xfs_trans_roll(&args->trans, dp);
701 * Fob the whole rest of the problem off on the Btree code.
703 error = xfs_attr_node_addname(args);
708 * Commit the transaction that added the attr name so that
709 * later routines can manage their own transactions.
711 error = xfs_trans_roll(&args->trans, dp);
716 * If there was an out-of-line value, allocate the blocks we
717 * identified for its storage and copy the value. This is done
718 * after we create the attribute so that we don't overflow the
719 * maximum size of a transaction and/or hit a deadlock.
721 if (args->rmtblkno > 0) {
722 error = xfs_attr_rmtval_set(args);
728 * If this is an atomic rename operation, we must "flip" the
729 * incomplete flags on the "new" and "old" attribute/value pairs
730 * so that one disappears and one appears atomically. Then we
731 * must remove the "old" attribute/value pair.
733 if (args->op_flags & XFS_DA_OP_RENAME) {
735 * In a separate transaction, set the incomplete flag on the
736 * "old" attr and clear the incomplete flag on the "new" attr.
738 error = xfs_attr3_leaf_flipflags(args);
743 * Dismantle the "old" attribute/value pair by removing
744 * a "remote" value (if it exists).
746 args->index = args->index2;
747 args->blkno = args->blkno2;
748 args->rmtblkno = args->rmtblkno2;
749 args->rmtblkcnt = args->rmtblkcnt2;
750 if (args->rmtblkno) {
751 error = xfs_attr_rmtval_remove(args);
757 * Read in the block containing the "old" attr, then
758 * remove the "old" attr from that block (neat, huh!)
760 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
765 xfs_attr3_leaf_remove(bp, args);
768 * If the result is small enough, shrink it all into the inode.
770 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
771 xfs_bmap_init(args->flist, args->firstblock);
772 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
773 /* bp is gone due to xfs_da_shrink_inode */
775 error = xfs_bmap_finish(&args->trans,
782 xfs_bmap_cancel(args->flist);
787 * bmap_finish() may have committed the last trans
788 * and started a new one. We need the inode to be
789 * in all transactions.
792 xfs_trans_ijoin(args->trans, dp, 0);
796 * Commit the remove and start the next trans in series.
798 error = xfs_trans_roll(&args->trans, dp);
800 } else if (args->rmtblkno > 0) {
802 * Added a "remote" value, just clear the incomplete flag.
804 error = xfs_attr3_leaf_clearflag(args);
810 * Remove a name from the leaf attribute list structure
812 * This leaf block cannot have a "remote" value, we only call this routine
813 * if bmap_one_block() says there is only one block (ie: no remote blks).
816 xfs_attr_leaf_removename(xfs_da_args_t *args)
820 int error, committed, forkoff;
822 trace_xfs_attr_leaf_removename(args);
825 * Remove the attribute.
829 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
833 error = xfs_attr3_leaf_lookup_int(bp, args);
834 if (error == ENOATTR) {
835 xfs_trans_brelse(args->trans, bp);
839 xfs_attr3_leaf_remove(bp, args);
842 * If the result is small enough, shrink it all into the inode.
844 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
845 xfs_bmap_init(args->flist, args->firstblock);
846 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
847 /* bp is gone due to xfs_da_shrink_inode */
849 error = xfs_bmap_finish(&args->trans, args->flist,
855 xfs_bmap_cancel(args->flist);
860 * bmap_finish() may have committed the last trans and started
861 * a new one. We need the inode to be in all transactions.
864 xfs_trans_ijoin(args->trans, dp, 0);
870 * Look up a name in a leaf attribute list structure.
872 * This leaf block cannot have a "remote" value, we only call this routine
873 * if bmap_one_block() says there is only one block (ie: no remote blks).
876 xfs_attr_leaf_get(xfs_da_args_t *args)
881 trace_xfs_attr_leaf_get(args);
884 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
888 error = xfs_attr3_leaf_lookup_int(bp, args);
889 if (error != EEXIST) {
890 xfs_trans_brelse(args->trans, bp);
893 error = xfs_attr3_leaf_getvalue(bp, args);
894 xfs_trans_brelse(args->trans, bp);
895 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
896 error = xfs_attr_rmtval_get(args);
901 /*========================================================================
902 * External routines when attribute list size > XFS_LBSIZE(mp).
903 *========================================================================*/
906 * Add a name to a Btree-format attribute list.
908 * This will involve walking down the Btree, and may involve splitting
909 * leaf nodes and even splitting intermediate nodes up to and including
910 * the root node (a special case of an intermediate node).
912 * "Remote" attribute values confuse the issue and atomic rename operations
913 * add a whole extra layer of confusion on top of that.
916 xfs_attr_node_addname(xfs_da_args_t *args)
918 xfs_da_state_t *state;
919 xfs_da_state_blk_t *blk;
922 int committed, retval, error;
924 trace_xfs_attr_node_addname(args);
927 * Fill in bucket of arguments/results/context to carry around.
932 state = xfs_da_state_alloc();
935 state->blocksize = state->mp->m_sb.sb_blocksize;
936 state->node_ents = state->mp->m_attr_node_ents;
939 * Search to see if name already exists, and get back a pointer
940 * to where it should go.
942 error = xfs_da3_node_lookup_int(state, &retval);
945 blk = &state->path.blk[ state->path.active-1 ];
946 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
947 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
949 } else if (retval == EEXIST) {
950 if (args->flags & ATTR_CREATE)
953 trace_xfs_attr_node_replace(args);
955 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
956 args->blkno2 = args->blkno; /* set 2nd entry info*/
957 args->index2 = args->index;
958 args->rmtblkno2 = args->rmtblkno;
959 args->rmtblkcnt2 = args->rmtblkcnt;
964 retval = xfs_attr3_leaf_add(blk->bp, state->args);
965 if (retval == ENOSPC) {
966 if (state->path.active == 1) {
968 * Its really a single leaf node, but it had
969 * out-of-line values so it looked like it *might*
970 * have been a b-tree.
972 xfs_da_state_free(state);
974 xfs_bmap_init(args->flist, args->firstblock);
975 error = xfs_attr3_leaf_to_node(args);
977 error = xfs_bmap_finish(&args->trans,
984 xfs_bmap_cancel(args->flist);
989 * bmap_finish() may have committed the last trans
990 * and started a new one. We need the inode to be
991 * in all transactions.
994 xfs_trans_ijoin(args->trans, dp, 0);
997 * Commit the node conversion and start the next
998 * trans in the chain.
1000 error = xfs_trans_roll(&args->trans, dp);
1008 * Split as many Btree elements as required.
1009 * This code tracks the new and old attr's location
1010 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1011 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1013 xfs_bmap_init(args->flist, args->firstblock);
1014 error = xfs_da3_split(state);
1016 error = xfs_bmap_finish(&args->trans, args->flist,
1022 xfs_bmap_cancel(args->flist);
1027 * bmap_finish() may have committed the last trans and started
1028 * a new one. We need the inode to be in all transactions.
1031 xfs_trans_ijoin(args->trans, dp, 0);
1034 * Addition succeeded, update Btree hashvals.
1036 xfs_da3_fixhashpath(state, &state->path);
1040 * Kill the state structure, we're done with it and need to
1041 * allow the buffers to come back later.
1043 xfs_da_state_free(state);
1047 * Commit the leaf addition or btree split and start the next
1048 * trans in the chain.
1050 error = xfs_trans_roll(&args->trans, dp);
1055 * If there was an out-of-line value, allocate the blocks we
1056 * identified for its storage and copy the value. This is done
1057 * after we create the attribute so that we don't overflow the
1058 * maximum size of a transaction and/or hit a deadlock.
1060 if (args->rmtblkno > 0) {
1061 error = xfs_attr_rmtval_set(args);
1067 * If this is an atomic rename operation, we must "flip" the
1068 * incomplete flags on the "new" and "old" attribute/value pairs
1069 * so that one disappears and one appears atomically. Then we
1070 * must remove the "old" attribute/value pair.
1072 if (args->op_flags & XFS_DA_OP_RENAME) {
1074 * In a separate transaction, set the incomplete flag on the
1075 * "old" attr and clear the incomplete flag on the "new" attr.
1077 error = xfs_attr3_leaf_flipflags(args);
1082 * Dismantle the "old" attribute/value pair by removing
1083 * a "remote" value (if it exists).
1085 args->index = args->index2;
1086 args->blkno = args->blkno2;
1087 args->rmtblkno = args->rmtblkno2;
1088 args->rmtblkcnt = args->rmtblkcnt2;
1089 if (args->rmtblkno) {
1090 error = xfs_attr_rmtval_remove(args);
1096 * Re-find the "old" attribute entry after any split ops.
1097 * The INCOMPLETE flag means that we will find the "old"
1098 * attr, not the "new" one.
1100 args->flags |= XFS_ATTR_INCOMPLETE;
1101 state = xfs_da_state_alloc();
1104 state->blocksize = state->mp->m_sb.sb_blocksize;
1105 state->node_ents = state->mp->m_attr_node_ents;
1107 error = xfs_da3_node_lookup_int(state, &retval);
1112 * Remove the name and update the hashvals in the tree.
1114 blk = &state->path.blk[ state->path.active-1 ];
1115 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1116 error = xfs_attr3_leaf_remove(blk->bp, args);
1117 xfs_da3_fixhashpath(state, &state->path);
1120 * Check to see if the tree needs to be collapsed.
1122 if (retval && (state->path.active > 1)) {
1123 xfs_bmap_init(args->flist, args->firstblock);
1124 error = xfs_da3_join(state);
1126 error = xfs_bmap_finish(&args->trans,
1133 xfs_bmap_cancel(args->flist);
1138 * bmap_finish() may have committed the last trans
1139 * and started a new one. We need the inode to be
1140 * in all transactions.
1143 xfs_trans_ijoin(args->trans, dp, 0);
1147 * Commit and start the next trans in the chain.
1149 error = xfs_trans_roll(&args->trans, dp);
1153 } else if (args->rmtblkno > 0) {
1155 * Added a "remote" value, just clear the incomplete flag.
1157 error = xfs_attr3_leaf_clearflag(args);
1165 xfs_da_state_free(state);
1172 * Remove a name from a B-tree attribute list.
1174 * This will involve walking down the Btree, and may involve joining
1175 * leaf nodes and even joining intermediate nodes up to and including
1176 * the root node (a special case of an intermediate node).
1179 xfs_attr_node_removename(xfs_da_args_t *args)
1181 xfs_da_state_t *state;
1182 xfs_da_state_blk_t *blk;
1185 int retval, error, committed, forkoff;
1187 trace_xfs_attr_node_removename(args);
1190 * Tie a string around our finger to remind us where we are.
1193 state = xfs_da_state_alloc();
1195 state->mp = dp->i_mount;
1196 state->blocksize = state->mp->m_sb.sb_blocksize;
1197 state->node_ents = state->mp->m_attr_node_ents;
1200 * Search to see if name exists, and get back a pointer to it.
1202 error = xfs_da3_node_lookup_int(state, &retval);
1203 if (error || (retval != EEXIST)) {
1210 * If there is an out-of-line value, de-allocate the blocks.
1211 * This is done before we remove the attribute so that we don't
1212 * overflow the maximum size of a transaction and/or hit a deadlock.
1214 blk = &state->path.blk[ state->path.active-1 ];
1215 ASSERT(blk->bp != NULL);
1216 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1217 if (args->rmtblkno > 0) {
1219 * Fill in disk block numbers in the state structure
1220 * so that we can get the buffers back after we commit
1221 * several transactions in the following calls.
1223 error = xfs_attr_fillstate(state);
1228 * Mark the attribute as INCOMPLETE, then bunmapi() the
1231 error = xfs_attr3_leaf_setflag(args);
1234 error = xfs_attr_rmtval_remove(args);
1239 * Refill the state structure with buffers, the prior calls
1240 * released our buffers.
1242 error = xfs_attr_refillstate(state);
1248 * Remove the name and update the hashvals in the tree.
1250 blk = &state->path.blk[ state->path.active-1 ];
1251 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1252 retval = xfs_attr3_leaf_remove(blk->bp, args);
1253 xfs_da3_fixhashpath(state, &state->path);
1256 * Check to see if the tree needs to be collapsed.
1258 if (retval && (state->path.active > 1)) {
1259 xfs_bmap_init(args->flist, args->firstblock);
1260 error = xfs_da3_join(state);
1262 error = xfs_bmap_finish(&args->trans, args->flist,
1268 xfs_bmap_cancel(args->flist);
1273 * bmap_finish() may have committed the last trans and started
1274 * a new one. We need the inode to be in all transactions.
1277 xfs_trans_ijoin(args->trans, dp, 0);
1280 * Commit the Btree join operation and start a new trans.
1282 error = xfs_trans_roll(&args->trans, dp);
1288 * If the result is small enough, push it all into the inode.
1290 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1292 * Have to get rid of the copy of this dabuf in the state.
1294 ASSERT(state->path.active == 1);
1295 ASSERT(state->path.blk[0].bp);
1296 state->path.blk[0].bp = NULL;
1298 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1302 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1303 xfs_bmap_init(args->flist, args->firstblock);
1304 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1305 /* bp is gone due to xfs_da_shrink_inode */
1307 error = xfs_bmap_finish(&args->trans,
1314 xfs_bmap_cancel(args->flist);
1319 * bmap_finish() may have committed the last trans
1320 * and started a new one. We need the inode to be
1321 * in all transactions.
1324 xfs_trans_ijoin(args->trans, dp, 0);
1326 xfs_trans_brelse(args->trans, bp);
1331 xfs_da_state_free(state);
1336 * Fill in the disk block numbers in the state structure for the buffers
1337 * that are attached to the state structure.
1338 * This is done so that we can quickly reattach ourselves to those buffers
1339 * after some set of transaction commits have released these buffers.
1342 xfs_attr_fillstate(xfs_da_state_t *state)
1344 xfs_da_state_path_t *path;
1345 xfs_da_state_blk_t *blk;
1348 trace_xfs_attr_fillstate(state->args);
1351 * Roll down the "path" in the state structure, storing the on-disk
1352 * block number for those buffers in the "path".
1354 path = &state->path;
1355 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1356 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1358 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1361 blk->disk_blkno = 0;
1366 * Roll down the "altpath" in the state structure, storing the on-disk
1367 * block number for those buffers in the "altpath".
1369 path = &state->altpath;
1370 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1371 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1373 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1376 blk->disk_blkno = 0;
1384 * Reattach the buffers to the state structure based on the disk block
1385 * numbers stored in the state structure.
1386 * This is done after some set of transaction commits have released those
1387 * buffers from our grip.
1390 xfs_attr_refillstate(xfs_da_state_t *state)
1392 xfs_da_state_path_t *path;
1393 xfs_da_state_blk_t *blk;
1396 trace_xfs_attr_refillstate(state->args);
1399 * Roll down the "path" in the state structure, storing the on-disk
1400 * block number for those buffers in the "path".
1402 path = &state->path;
1403 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1404 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1405 if (blk->disk_blkno) {
1406 error = xfs_da3_node_read(state->args->trans,
1408 blk->blkno, blk->disk_blkno,
1409 &blk->bp, XFS_ATTR_FORK);
1418 * Roll down the "altpath" in the state structure, storing the on-disk
1419 * block number for those buffers in the "altpath".
1421 path = &state->altpath;
1422 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1423 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1424 if (blk->disk_blkno) {
1425 error = xfs_da3_node_read(state->args->trans,
1427 blk->blkno, blk->disk_blkno,
1428 &blk->bp, XFS_ATTR_FORK);
1440 * Look up a filename in a node attribute list.
1442 * This routine gets called for any attribute fork that has more than one
1443 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1444 * "remote" values taking up more blocks.
1447 xfs_attr_node_get(xfs_da_args_t *args)
1449 xfs_da_state_t *state;
1450 xfs_da_state_blk_t *blk;
1454 trace_xfs_attr_node_get(args);
1456 state = xfs_da_state_alloc();
1458 state->mp = args->dp->i_mount;
1459 state->blocksize = state->mp->m_sb.sb_blocksize;
1460 state->node_ents = state->mp->m_attr_node_ents;
1463 * Search to see if name exists, and get back a pointer to it.
1465 error = xfs_da3_node_lookup_int(state, &retval);
1468 } else if (retval == EEXIST) {
1469 blk = &state->path.blk[ state->path.active-1 ];
1470 ASSERT(blk->bp != NULL);
1471 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1474 * Get the value, local or "remote"
1476 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1477 if (!retval && (args->rmtblkno > 0)
1478 && !(args->flags & ATTR_KERNOVAL)) {
1479 retval = xfs_attr_rmtval_get(args);
1484 * If not in a transaction, we have to release all the buffers.
1486 for (i = 0; i < state->path.active; i++) {
1487 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1488 state->path.blk[i].bp = NULL;
1491 xfs_da_state_free(state);