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);
81 struct xfs_da_args *args,
83 const unsigned char *name,
90 memset(args, 0, sizeof(*args));
91 args->geo = dp->i_mount->m_attr_geo;
92 args->whichfork = XFS_ATTR_FORK;
96 args->namelen = strlen((const char *)name);
97 if (args->namelen >= MAXNAMELEN)
98 return EFAULT; /* match IRIX behaviour */
100 args->hashval = xfs_da_hashname(args->name, args->namelen);
106 struct xfs_inode *ip)
108 if (!XFS_IFORK_Q(ip) ||
109 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
110 ip->i_d.di_anextents == 0))
115 /*========================================================================
116 * Overall external interface routines.
117 *========================================================================*/
121 struct xfs_inode *ip,
122 const unsigned char *name,
123 unsigned char *value,
127 struct xfs_da_args args;
131 XFS_STATS_INC(xs_attr_get);
133 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
136 if (!xfs_inode_hasattr(ip))
139 error = xfs_attr_args_init(&args, ip, name, flags);
144 args.valuelen = *valuelenp;
146 lock_mode = xfs_ilock_attr_map_shared(ip);
147 if (!xfs_inode_hasattr(ip))
149 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
150 error = xfs_attr_shortform_getvalue(&args);
151 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
152 error = xfs_attr_leaf_get(&args);
154 error = xfs_attr_node_get(&args);
155 xfs_iunlock(ip, lock_mode);
157 *valuelenp = args.valuelen;
158 return error == EEXIST ? 0 : error;
162 * Calculate how many blocks we need for the new attribute,
166 struct xfs_da_args *args,
169 struct xfs_mount *mp = args->dp->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(args->namelen, args->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_attr3_rmt_blocks(mp, args->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 xfs_fsblock_t firstblock;
212 int rsvd = (flags & ATTR_ROOT) != 0;
213 int error, err2, committed, local;
215 XFS_STATS_INC(xs_attr_set);
217 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
220 error = xfs_attr_args_init(&args, dp, name, flags);
225 args.valuelen = valuelen;
226 args.firstblock = &firstblock;
228 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
229 args.total = xfs_attr_calc_size(&args, &local);
231 error = xfs_qm_dqattach(dp, 0);
236 * If the inode doesn't have an attribute fork, add one.
237 * (inode must not be locked when we call this routine)
239 if (XFS_IFORK_Q(dp) == 0) {
240 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
241 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
243 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
249 * Start our first transaction of the day.
251 * All future transactions during this code must be "chained" off
252 * this one via the trans_dup() call. All transactions will contain
253 * the inode, and the inode will always be marked with trans_ihold().
254 * Since the inode will be locked in all transactions, we must log
255 * the inode in every transaction to let it float upward through
258 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
261 * Root fork attributes can use reserved data blocks for this
262 * operation if necessary
266 args.trans->t_flags |= XFS_TRANS_RESERVE;
268 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
269 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
270 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
271 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
272 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
274 xfs_trans_cancel(args.trans, 0);
277 xfs_ilock(dp, XFS_ILOCK_EXCL);
279 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
280 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
281 XFS_QMOPT_RES_REGBLKS);
283 xfs_iunlock(dp, XFS_ILOCK_EXCL);
284 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
288 xfs_trans_ijoin(args.trans, dp, 0);
291 * If the attribute list is non-existent or a shortform list,
292 * upgrade it to a single-leaf-block attribute list.
294 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
295 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
296 dp->i_d.di_anextents == 0)) {
299 * Build initial attribute list (if required).
301 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
302 xfs_attr_shortform_create(&args);
305 * Try to add the attr to the attribute list in
308 error = xfs_attr_shortform_addname(&args);
309 if (error != ENOSPC) {
311 * Commit the shortform mods, and we're done.
312 * NOTE: this is also the error path (EEXIST, etc).
314 ASSERT(args.trans != NULL);
317 * If this is a synchronous mount, make sure that
318 * the transaction goes to disk before returning
321 if (mp->m_flags & XFS_MOUNT_WSYNC)
322 xfs_trans_set_sync(args.trans);
324 if (!error && (flags & ATTR_KERNOTIME) == 0) {
325 xfs_trans_ichgtime(args.trans, dp,
328 err2 = xfs_trans_commit(args.trans,
329 XFS_TRANS_RELEASE_LOG_RES);
330 xfs_iunlock(dp, XFS_ILOCK_EXCL);
332 return error ? error : err2;
336 * It won't fit in the shortform, transform to a leaf block.
337 * GROT: another possible req'mt for a double-split btree op.
339 xfs_bmap_init(args.flist, args.firstblock);
340 error = xfs_attr_shortform_to_leaf(&args);
342 error = xfs_bmap_finish(&args.trans, args.flist,
348 xfs_bmap_cancel(&flist);
353 * bmap_finish() may have committed the last trans and started
354 * a new one. We need the inode to be in all transactions.
357 xfs_trans_ijoin(args.trans, dp, 0);
360 * Commit the leaf transformation. We'll need another (linked)
361 * transaction to add the new attribute to the leaf.
364 error = xfs_trans_roll(&args.trans, dp);
370 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
371 error = xfs_attr_leaf_addname(&args);
373 error = xfs_attr_node_addname(&args);
378 * If this is a synchronous mount, make sure that the
379 * transaction goes to disk before returning to the user.
381 if (mp->m_flags & XFS_MOUNT_WSYNC)
382 xfs_trans_set_sync(args.trans);
384 if ((flags & ATTR_KERNOTIME) == 0)
385 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
388 * Commit the last in the sequence of transactions.
390 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
391 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
392 xfs_iunlock(dp, XFS_ILOCK_EXCL);
398 xfs_trans_cancel(args.trans,
399 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
401 xfs_iunlock(dp, XFS_ILOCK_EXCL);
406 * Generic handler routine to remove a name from an attribute list.
407 * Transitions attribute list from Btree to shortform as necessary.
411 struct xfs_inode *dp,
412 const unsigned char *name,
415 struct xfs_mount *mp = dp->i_mount;
416 struct xfs_da_args args;
417 struct xfs_bmap_free flist;
418 xfs_fsblock_t firstblock;
421 XFS_STATS_INC(xs_attr_remove);
423 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
426 if (!xfs_inode_hasattr(dp))
429 error = xfs_attr_args_init(&args, dp, name, flags);
433 args.firstblock = &firstblock;
437 * we have no control over the attribute names that userspace passes us
438 * to remove, so we have to allow the name lookup prior to attribute
441 args.op_flags = XFS_DA_OP_OKNOENT;
443 error = xfs_qm_dqattach(dp, 0);
448 * Start our first transaction of the day.
450 * All future transactions during this code must be "chained" off
451 * this one via the trans_dup() call. All transactions will contain
452 * the inode, and the inode will always be marked with trans_ihold().
453 * Since the inode will be locked in all transactions, we must log
454 * the inode in every transaction to let it float upward through
457 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
460 * Root fork attributes can use reserved data blocks for this
461 * operation if necessary
464 if (flags & ATTR_ROOT)
465 args.trans->t_flags |= XFS_TRANS_RESERVE;
467 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
468 XFS_ATTRRM_SPACE_RES(mp), 0);
470 xfs_trans_cancel(args.trans, 0);
474 xfs_ilock(dp, XFS_ILOCK_EXCL);
476 * No need to make quota reservations here. We expect to release some
477 * blocks not allocate in the common case.
479 xfs_trans_ijoin(args.trans, dp, 0);
481 if (!xfs_inode_hasattr(dp)) {
482 error = XFS_ERROR(ENOATTR);
483 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
484 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
485 error = xfs_attr_shortform_remove(&args);
486 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
487 error = xfs_attr_leaf_removename(&args);
489 error = xfs_attr_node_removename(&args);
496 * If this is a synchronous mount, make sure that the
497 * transaction goes to disk before returning to the user.
499 if (mp->m_flags & XFS_MOUNT_WSYNC)
500 xfs_trans_set_sync(args.trans);
502 if ((flags & ATTR_KERNOTIME) == 0)
503 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
506 * Commit the last in the sequence of transactions.
508 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
509 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
510 xfs_iunlock(dp, XFS_ILOCK_EXCL);
516 xfs_trans_cancel(args.trans,
517 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
519 xfs_iunlock(dp, XFS_ILOCK_EXCL);
523 /*========================================================================
524 * External routines when attribute list is inside the inode
525 *========================================================================*/
528 * Add a name to the shortform attribute list structure
529 * This is the external routine.
532 xfs_attr_shortform_addname(xfs_da_args_t *args)
534 int newsize, forkoff, retval;
536 trace_xfs_attr_sf_addname(args);
538 retval = xfs_attr_shortform_lookup(args);
539 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
541 } else if (retval == EEXIST) {
542 if (args->flags & ATTR_CREATE)
544 retval = xfs_attr_shortform_remove(args);
548 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
549 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
550 return(XFS_ERROR(ENOSPC));
552 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
553 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
555 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
557 return(XFS_ERROR(ENOSPC));
559 xfs_attr_shortform_add(args, forkoff);
564 /*========================================================================
565 * External routines when attribute list is one block
566 *========================================================================*/
569 * Add a name to the leaf attribute list structure
571 * This leaf block cannot have a "remote" value, we only call this routine
572 * if bmap_one_block() says there is only one block (ie: no remote blks).
575 xfs_attr_leaf_addname(xfs_da_args_t *args)
579 int retval, error, committed, forkoff;
581 trace_xfs_attr_leaf_addname(args);
584 * Read the (only) block in the attribute list in.
588 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
593 * Look up the given attribute in the leaf block. Figure out if
594 * the given flags produce an error or call for an atomic rename.
596 retval = xfs_attr3_leaf_lookup_int(bp, args);
597 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
598 xfs_trans_brelse(args->trans, bp);
600 } else if (retval == EEXIST) {
601 if (args->flags & ATTR_CREATE) { /* pure create op */
602 xfs_trans_brelse(args->trans, bp);
606 trace_xfs_attr_leaf_replace(args);
608 /* save the attribute state for later removal*/
609 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
610 args->blkno2 = args->blkno; /* set 2nd entry info*/
611 args->index2 = args->index;
612 args->rmtblkno2 = args->rmtblkno;
613 args->rmtblkcnt2 = args->rmtblkcnt;
614 args->rmtvaluelen2 = args->rmtvaluelen;
617 * clear the remote attr state now that it is saved so that the
618 * values reflect the state of the attribute we are about to
619 * add, not the attribute we just found and will remove later.
623 args->rmtvaluelen = 0;
627 * Add the attribute to the leaf block, transitioning to a Btree
630 retval = xfs_attr3_leaf_add(bp, args);
631 if (retval == ENOSPC) {
633 * Promote the attribute list to the Btree format, then
634 * Commit that transaction so that the node_addname() call
635 * can manage its own transactions.
637 xfs_bmap_init(args->flist, args->firstblock);
638 error = xfs_attr3_leaf_to_node(args);
640 error = xfs_bmap_finish(&args->trans, args->flist,
646 xfs_bmap_cancel(args->flist);
651 * bmap_finish() may have committed the last trans and started
652 * a new one. We need the inode to be in all transactions.
655 xfs_trans_ijoin(args->trans, dp, 0);
658 * Commit the current trans (including the inode) and start
661 error = xfs_trans_roll(&args->trans, dp);
666 * Fob the whole rest of the problem off on the Btree code.
668 error = xfs_attr_node_addname(args);
673 * Commit the transaction that added the attr name so that
674 * later routines can manage their own transactions.
676 error = xfs_trans_roll(&args->trans, dp);
681 * If there was an out-of-line value, allocate the blocks we
682 * identified for its storage and copy the value. This is done
683 * after we create the attribute so that we don't overflow the
684 * maximum size of a transaction and/or hit a deadlock.
686 if (args->rmtblkno > 0) {
687 error = xfs_attr_rmtval_set(args);
693 * If this is an atomic rename operation, we must "flip" the
694 * incomplete flags on the "new" and "old" attribute/value pairs
695 * so that one disappears and one appears atomically. Then we
696 * must remove the "old" attribute/value pair.
698 if (args->op_flags & XFS_DA_OP_RENAME) {
700 * In a separate transaction, set the incomplete flag on the
701 * "old" attr and clear the incomplete flag on the "new" attr.
703 error = xfs_attr3_leaf_flipflags(args);
708 * Dismantle the "old" attribute/value pair by removing
709 * a "remote" value (if it exists).
711 args->index = args->index2;
712 args->blkno = args->blkno2;
713 args->rmtblkno = args->rmtblkno2;
714 args->rmtblkcnt = args->rmtblkcnt2;
715 args->rmtvaluelen = args->rmtvaluelen2;
716 if (args->rmtblkno) {
717 error = xfs_attr_rmtval_remove(args);
723 * Read in the block containing the "old" attr, then
724 * remove the "old" attr from that block (neat, huh!)
726 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
731 xfs_attr3_leaf_remove(bp, args);
734 * If the result is small enough, shrink it all into the inode.
736 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
737 xfs_bmap_init(args->flist, args->firstblock);
738 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
739 /* bp is gone due to xfs_da_shrink_inode */
741 error = xfs_bmap_finish(&args->trans,
748 xfs_bmap_cancel(args->flist);
753 * bmap_finish() may have committed the last trans
754 * and started a new one. We need the inode to be
755 * in all transactions.
758 xfs_trans_ijoin(args->trans, dp, 0);
762 * Commit the remove and start the next trans in series.
764 error = xfs_trans_roll(&args->trans, dp);
766 } else if (args->rmtblkno > 0) {
768 * Added a "remote" value, just clear the incomplete flag.
770 error = xfs_attr3_leaf_clearflag(args);
776 * Remove a name from the leaf attribute list structure
778 * This leaf block cannot have a "remote" value, we only call this routine
779 * if bmap_one_block() says there is only one block (ie: no remote blks).
782 xfs_attr_leaf_removename(xfs_da_args_t *args)
786 int error, committed, forkoff;
788 trace_xfs_attr_leaf_removename(args);
791 * Remove the attribute.
795 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
799 error = xfs_attr3_leaf_lookup_int(bp, args);
800 if (error == ENOATTR) {
801 xfs_trans_brelse(args->trans, bp);
805 xfs_attr3_leaf_remove(bp, args);
808 * If the result is small enough, shrink it all into the inode.
810 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
811 xfs_bmap_init(args->flist, args->firstblock);
812 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
813 /* bp is gone due to xfs_da_shrink_inode */
815 error = xfs_bmap_finish(&args->trans, args->flist,
821 xfs_bmap_cancel(args->flist);
826 * bmap_finish() may have committed the last trans and started
827 * a new one. We need the inode to be in all transactions.
830 xfs_trans_ijoin(args->trans, dp, 0);
836 * Look up a name in a leaf attribute list structure.
838 * This leaf block cannot have a "remote" value, we only call this routine
839 * if bmap_one_block() says there is only one block (ie: no remote blks).
842 xfs_attr_leaf_get(xfs_da_args_t *args)
847 trace_xfs_attr_leaf_get(args);
850 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
854 error = xfs_attr3_leaf_lookup_int(bp, args);
855 if (error != EEXIST) {
856 xfs_trans_brelse(args->trans, bp);
859 error = xfs_attr3_leaf_getvalue(bp, args);
860 xfs_trans_brelse(args->trans, bp);
861 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
862 error = xfs_attr_rmtval_get(args);
867 /*========================================================================
868 * External routines when attribute list size > XFS_LBSIZE(mp).
869 *========================================================================*/
872 * Add a name to a Btree-format attribute list.
874 * This will involve walking down the Btree, and may involve splitting
875 * leaf nodes and even splitting intermediate nodes up to and including
876 * the root node (a special case of an intermediate node).
878 * "Remote" attribute values confuse the issue and atomic rename operations
879 * add a whole extra layer of confusion on top of that.
882 xfs_attr_node_addname(xfs_da_args_t *args)
884 xfs_da_state_t *state;
885 xfs_da_state_blk_t *blk;
888 int committed, retval, error;
890 trace_xfs_attr_node_addname(args);
893 * Fill in bucket of arguments/results/context to carry around.
898 state = xfs_da_state_alloc();
901 state->blocksize = state->mp->m_sb.sb_blocksize;
902 state->node_ents = args->geo->node_ents;
905 * Search to see if name already exists, and get back a pointer
906 * to where it should go.
908 error = xfs_da3_node_lookup_int(state, &retval);
911 blk = &state->path.blk[ state->path.active-1 ];
912 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
913 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
915 } else if (retval == EEXIST) {
916 if (args->flags & ATTR_CREATE)
919 trace_xfs_attr_node_replace(args);
921 /* save the attribute state for later removal*/
922 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
923 args->blkno2 = args->blkno; /* set 2nd entry info*/
924 args->index2 = args->index;
925 args->rmtblkno2 = args->rmtblkno;
926 args->rmtblkcnt2 = args->rmtblkcnt;
927 args->rmtvaluelen2 = args->rmtvaluelen;
930 * clear the remote attr state now that it is saved so that the
931 * values reflect the state of the attribute we are about to
932 * add, not the attribute we just found and will remove later.
936 args->rmtvaluelen = 0;
939 retval = xfs_attr3_leaf_add(blk->bp, state->args);
940 if (retval == ENOSPC) {
941 if (state->path.active == 1) {
943 * Its really a single leaf node, but it had
944 * out-of-line values so it looked like it *might*
945 * have been a b-tree.
947 xfs_da_state_free(state);
949 xfs_bmap_init(args->flist, args->firstblock);
950 error = xfs_attr3_leaf_to_node(args);
952 error = xfs_bmap_finish(&args->trans,
959 xfs_bmap_cancel(args->flist);
964 * bmap_finish() may have committed the last trans
965 * and started a new one. We need the inode to be
966 * in all transactions.
969 xfs_trans_ijoin(args->trans, dp, 0);
972 * Commit the node conversion and start the next
973 * trans in the chain.
975 error = xfs_trans_roll(&args->trans, dp);
983 * Split as many Btree elements as required.
984 * This code tracks the new and old attr's location
985 * in the index/blkno/rmtblkno/rmtblkcnt fields and
986 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
988 xfs_bmap_init(args->flist, args->firstblock);
989 error = xfs_da3_split(state);
991 error = xfs_bmap_finish(&args->trans, args->flist,
997 xfs_bmap_cancel(args->flist);
1002 * bmap_finish() may have committed the last trans and started
1003 * a new one. We need the inode to be in all transactions.
1006 xfs_trans_ijoin(args->trans, dp, 0);
1009 * Addition succeeded, update Btree hashvals.
1011 xfs_da3_fixhashpath(state, &state->path);
1015 * Kill the state structure, we're done with it and need to
1016 * allow the buffers to come back later.
1018 xfs_da_state_free(state);
1022 * Commit the leaf addition or btree split and start the next
1023 * trans in the chain.
1025 error = xfs_trans_roll(&args->trans, dp);
1030 * If there was an out-of-line value, allocate the blocks we
1031 * identified for its storage and copy the value. This is done
1032 * after we create the attribute so that we don't overflow the
1033 * maximum size of a transaction and/or hit a deadlock.
1035 if (args->rmtblkno > 0) {
1036 error = xfs_attr_rmtval_set(args);
1042 * If this is an atomic rename operation, we must "flip" the
1043 * incomplete flags on the "new" and "old" attribute/value pairs
1044 * so that one disappears and one appears atomically. Then we
1045 * must remove the "old" attribute/value pair.
1047 if (args->op_flags & XFS_DA_OP_RENAME) {
1049 * In a separate transaction, set the incomplete flag on the
1050 * "old" attr and clear the incomplete flag on the "new" attr.
1052 error = xfs_attr3_leaf_flipflags(args);
1057 * Dismantle the "old" attribute/value pair by removing
1058 * a "remote" value (if it exists).
1060 args->index = args->index2;
1061 args->blkno = args->blkno2;
1062 args->rmtblkno = args->rmtblkno2;
1063 args->rmtblkcnt = args->rmtblkcnt2;
1064 args->rmtvaluelen = args->rmtvaluelen2;
1065 if (args->rmtblkno) {
1066 error = xfs_attr_rmtval_remove(args);
1072 * Re-find the "old" attribute entry after any split ops.
1073 * The INCOMPLETE flag means that we will find the "old"
1074 * attr, not the "new" one.
1076 args->flags |= XFS_ATTR_INCOMPLETE;
1077 state = xfs_da_state_alloc();
1080 state->blocksize = state->mp->m_sb.sb_blocksize;
1081 state->node_ents = args->geo->node_ents;
1083 error = xfs_da3_node_lookup_int(state, &retval);
1088 * Remove the name and update the hashvals in the tree.
1090 blk = &state->path.blk[ state->path.active-1 ];
1091 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1092 error = xfs_attr3_leaf_remove(blk->bp, args);
1093 xfs_da3_fixhashpath(state, &state->path);
1096 * Check to see if the tree needs to be collapsed.
1098 if (retval && (state->path.active > 1)) {
1099 xfs_bmap_init(args->flist, args->firstblock);
1100 error = xfs_da3_join(state);
1102 error = xfs_bmap_finish(&args->trans,
1109 xfs_bmap_cancel(args->flist);
1114 * bmap_finish() may have committed the last trans
1115 * and started a new one. We need the inode to be
1116 * in all transactions.
1119 xfs_trans_ijoin(args->trans, dp, 0);
1123 * Commit and start the next trans in the chain.
1125 error = xfs_trans_roll(&args->trans, dp);
1129 } else if (args->rmtblkno > 0) {
1131 * Added a "remote" value, just clear the incomplete flag.
1133 error = xfs_attr3_leaf_clearflag(args);
1141 xfs_da_state_free(state);
1148 * Remove a name from a B-tree attribute list.
1150 * This will involve walking down the Btree, and may involve joining
1151 * leaf nodes and even joining intermediate nodes up to and including
1152 * the root node (a special case of an intermediate node).
1155 xfs_attr_node_removename(xfs_da_args_t *args)
1157 xfs_da_state_t *state;
1158 xfs_da_state_blk_t *blk;
1161 int retval, error, committed, forkoff;
1163 trace_xfs_attr_node_removename(args);
1166 * Tie a string around our finger to remind us where we are.
1169 state = xfs_da_state_alloc();
1171 state->mp = dp->i_mount;
1172 state->blocksize = state->mp->m_sb.sb_blocksize;
1173 state->node_ents = args->geo->node_ents;
1176 * Search to see if name exists, and get back a pointer to it.
1178 error = xfs_da3_node_lookup_int(state, &retval);
1179 if (error || (retval != EEXIST)) {
1186 * If there is an out-of-line value, de-allocate the blocks.
1187 * This is done before we remove the attribute so that we don't
1188 * overflow the maximum size of a transaction and/or hit a deadlock.
1190 blk = &state->path.blk[ state->path.active-1 ];
1191 ASSERT(blk->bp != NULL);
1192 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1193 if (args->rmtblkno > 0) {
1195 * Fill in disk block numbers in the state structure
1196 * so that we can get the buffers back after we commit
1197 * several transactions in the following calls.
1199 error = xfs_attr_fillstate(state);
1204 * Mark the attribute as INCOMPLETE, then bunmapi() the
1207 error = xfs_attr3_leaf_setflag(args);
1210 error = xfs_attr_rmtval_remove(args);
1215 * Refill the state structure with buffers, the prior calls
1216 * released our buffers.
1218 error = xfs_attr_refillstate(state);
1224 * Remove the name and update the hashvals in the tree.
1226 blk = &state->path.blk[ state->path.active-1 ];
1227 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1228 retval = xfs_attr3_leaf_remove(blk->bp, args);
1229 xfs_da3_fixhashpath(state, &state->path);
1232 * Check to see if the tree needs to be collapsed.
1234 if (retval && (state->path.active > 1)) {
1235 xfs_bmap_init(args->flist, args->firstblock);
1236 error = xfs_da3_join(state);
1238 error = xfs_bmap_finish(&args->trans, args->flist,
1244 xfs_bmap_cancel(args->flist);
1249 * bmap_finish() may have committed the last trans and started
1250 * a new one. We need the inode to be in all transactions.
1253 xfs_trans_ijoin(args->trans, dp, 0);
1256 * Commit the Btree join operation and start a new trans.
1258 error = xfs_trans_roll(&args->trans, dp);
1264 * If the result is small enough, push it all into the inode.
1266 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1268 * Have to get rid of the copy of this dabuf in the state.
1270 ASSERT(state->path.active == 1);
1271 ASSERT(state->path.blk[0].bp);
1272 state->path.blk[0].bp = NULL;
1274 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1278 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1279 xfs_bmap_init(args->flist, args->firstblock);
1280 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1281 /* bp is gone due to xfs_da_shrink_inode */
1283 error = xfs_bmap_finish(&args->trans,
1290 xfs_bmap_cancel(args->flist);
1295 * bmap_finish() may have committed the last trans
1296 * and started a new one. We need the inode to be
1297 * in all transactions.
1300 xfs_trans_ijoin(args->trans, dp, 0);
1302 xfs_trans_brelse(args->trans, bp);
1307 xfs_da_state_free(state);
1312 * Fill in the disk block numbers in the state structure for the buffers
1313 * that are attached to the state structure.
1314 * This is done so that we can quickly reattach ourselves to those buffers
1315 * after some set of transaction commits have released these buffers.
1318 xfs_attr_fillstate(xfs_da_state_t *state)
1320 xfs_da_state_path_t *path;
1321 xfs_da_state_blk_t *blk;
1324 trace_xfs_attr_fillstate(state->args);
1327 * Roll down the "path" in the state structure, storing the on-disk
1328 * block number for those buffers in the "path".
1330 path = &state->path;
1331 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1332 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1334 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1337 blk->disk_blkno = 0;
1342 * Roll down the "altpath" in the state structure, storing the on-disk
1343 * block number for those buffers in the "altpath".
1345 path = &state->altpath;
1346 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1347 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1349 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1352 blk->disk_blkno = 0;
1360 * Reattach the buffers to the state structure based on the disk block
1361 * numbers stored in the state structure.
1362 * This is done after some set of transaction commits have released those
1363 * buffers from our grip.
1366 xfs_attr_refillstate(xfs_da_state_t *state)
1368 xfs_da_state_path_t *path;
1369 xfs_da_state_blk_t *blk;
1372 trace_xfs_attr_refillstate(state->args);
1375 * Roll down the "path" in the state structure, storing the on-disk
1376 * block number for those buffers in the "path".
1378 path = &state->path;
1379 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1380 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1381 if (blk->disk_blkno) {
1382 error = xfs_da3_node_read(state->args->trans,
1384 blk->blkno, blk->disk_blkno,
1385 &blk->bp, XFS_ATTR_FORK);
1394 * Roll down the "altpath" in the state structure, storing the on-disk
1395 * block number for those buffers in the "altpath".
1397 path = &state->altpath;
1398 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1399 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1400 if (blk->disk_blkno) {
1401 error = xfs_da3_node_read(state->args->trans,
1403 blk->blkno, blk->disk_blkno,
1404 &blk->bp, XFS_ATTR_FORK);
1416 * Look up a filename in a node attribute list.
1418 * This routine gets called for any attribute fork that has more than one
1419 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1420 * "remote" values taking up more blocks.
1423 xfs_attr_node_get(xfs_da_args_t *args)
1425 xfs_da_state_t *state;
1426 xfs_da_state_blk_t *blk;
1430 trace_xfs_attr_node_get(args);
1432 state = xfs_da_state_alloc();
1434 state->mp = args->dp->i_mount;
1435 state->blocksize = state->mp->m_sb.sb_blocksize;
1436 state->node_ents = args->geo->node_ents;
1439 * Search to see if name exists, and get back a pointer to it.
1441 error = xfs_da3_node_lookup_int(state, &retval);
1444 } else if (retval == EEXIST) {
1445 blk = &state->path.blk[ state->path.active-1 ];
1446 ASSERT(blk->bp != NULL);
1447 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1450 * Get the value, local or "remote"
1452 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1453 if (!retval && (args->rmtblkno > 0)
1454 && !(args->flags & ATTR_KERNOVAL)) {
1455 retval = xfs_attr_rmtval_get(args);
1460 * If not in a transaction, we have to release all the buffers.
1462 for (i = 0; i < state->path.active; i++) {
1463 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1464 state->path.blk[i].bp = NULL;
1467 xfs_da_state_free(state);