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_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_attr_sf.h"
29 #include "xfs_inode.h"
30 #include "xfs_alloc.h"
31 #include "xfs_trans.h"
32 #include "xfs_inode_item.h"
34 #include "xfs_bmap_util.h"
35 #include "xfs_bmap_btree.h"
37 #include "xfs_attr_leaf.h"
38 #include "xfs_attr_remote.h"
39 #include "xfs_error.h"
40 #include "xfs_quota.h"
41 #include "xfs_trans_space.h"
42 #include "xfs_trace.h"
47 * Provide the external interfaces to manage attribute lists.
50 /*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
55 * Internal routines when attribute list fits inside the inode.
57 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
60 * Internal routines when attribute list is one block.
62 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
63 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
64 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
67 * Internal routines when attribute list is more than one block.
69 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
70 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
71 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
73 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
78 struct xfs_da_args
*args
,
80 const unsigned char *name
,
87 memset(args
, 0, sizeof(*args
));
88 args
->geo
= dp
->i_mount
->m_attr_geo
;
89 args
->whichfork
= XFS_ATTR_FORK
;
93 args
->namelen
= strlen((const char *)name
);
94 if (args
->namelen
>= MAXNAMELEN
)
95 return -EFAULT
; /* match IRIX behaviour */
97 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
103 struct xfs_inode
*ip
)
105 if (!XFS_IFORK_Q(ip
) ||
106 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
107 ip
->i_d
.di_anextents
== 0))
112 /*========================================================================
113 * Overall external interface routines.
114 *========================================================================*/
118 struct xfs_inode
*ip
,
119 const unsigned char *name
,
120 unsigned char *value
,
124 struct xfs_da_args args
;
128 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
130 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
133 if (!xfs_inode_hasattr(ip
))
136 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
141 args
.valuelen
= *valuelenp
;
142 /* Entirely possible to look up a name which doesn't exist */
143 args
.op_flags
= XFS_DA_OP_OKNOENT
;
145 lock_mode
= xfs_ilock_attr_map_shared(ip
);
146 if (!xfs_inode_hasattr(ip
))
148 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
149 error
= xfs_attr_shortform_getvalue(&args
);
150 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
151 error
= xfs_attr_leaf_get(&args
);
153 error
= xfs_attr_node_get(&args
);
154 xfs_iunlock(ip
, lock_mode
);
156 *valuelenp
= args
.valuelen
;
157 return error
== -EEXIST
? 0 : error
;
161 * Calculate how many blocks we need for the new attribute,
165 struct xfs_da_args
*args
,
168 struct xfs_mount
*mp
= args
->dp
->i_mount
;
173 * Determine space new attribute will use, and if it would be
174 * "local" or "remote" (note: local != inline).
176 size
= xfs_attr_leaf_newentsize(args
, local
);
177 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
179 if (size
> (args
->geo
->blksize
/ 2)) {
180 /* Double split possible */
185 * Out of line attribute, cannot double split, but
186 * make room for the attribute value itself.
188 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
190 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
198 struct xfs_inode
*dp
,
199 const unsigned char *name
,
200 unsigned char *value
,
204 struct xfs_mount
*mp
= dp
->i_mount
;
205 struct xfs_da_args args
;
206 struct xfs_bmap_free flist
;
207 struct xfs_trans_res tres
;
208 xfs_fsblock_t firstblock
;
209 int rsvd
= (flags
& ATTR_ROOT
) != 0;
210 int error
, err2
, local
;
212 XFS_STATS_INC(mp
, xs_attr_set
);
214 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
217 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
222 args
.valuelen
= valuelen
;
223 args
.firstblock
= &firstblock
;
225 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
226 args
.total
= xfs_attr_calc_size(&args
, &local
);
228 error
= xfs_qm_dqattach(dp
, 0);
233 * If the inode doesn't have an attribute fork, add one.
234 * (inode must not be locked when we call this routine)
236 if (XFS_IFORK_Q(dp
) == 0) {
237 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
238 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
240 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
245 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
246 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
247 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
248 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
251 * Root fork attributes can use reserved data blocks for this
252 * operation if necessary
254 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
255 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
259 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
260 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
261 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
262 XFS_QMOPT_RES_REGBLKS
);
264 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
265 xfs_trans_cancel(args
.trans
);
269 xfs_trans_ijoin(args
.trans
, dp
, 0);
272 * If the attribute list is non-existent or a shortform list,
273 * upgrade it to a single-leaf-block attribute list.
275 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
276 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
277 dp
->i_d
.di_anextents
== 0)) {
280 * Build initial attribute list (if required).
282 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
283 xfs_attr_shortform_create(&args
);
286 * Try to add the attr to the attribute list in
289 error
= xfs_attr_shortform_addname(&args
);
290 if (error
!= -ENOSPC
) {
292 * Commit the shortform mods, and we're done.
293 * NOTE: this is also the error path (EEXIST, etc).
295 ASSERT(args
.trans
!= NULL
);
298 * If this is a synchronous mount, make sure that
299 * the transaction goes to disk before returning
302 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
303 xfs_trans_set_sync(args
.trans
);
305 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
306 xfs_trans_ichgtime(args
.trans
, dp
,
309 err2
= xfs_trans_commit(args
.trans
);
310 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
312 return error
? error
: err2
;
316 * It won't fit in the shortform, transform to a leaf block.
317 * GROT: another possible req'mt for a double-split btree op.
319 xfs_bmap_init(args
.flist
, args
.firstblock
);
320 error
= xfs_attr_shortform_to_leaf(&args
);
322 error
= xfs_bmap_finish(&args
.trans
, args
.flist
, dp
);
325 xfs_bmap_cancel(&flist
);
330 * Commit the leaf transformation. We'll need another (linked)
331 * transaction to add the new attribute to the leaf.
334 error
= xfs_trans_roll(&args
.trans
, dp
);
340 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
341 error
= xfs_attr_leaf_addname(&args
);
343 error
= xfs_attr_node_addname(&args
);
348 * If this is a synchronous mount, make sure that the
349 * transaction goes to disk before returning to the user.
351 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
352 xfs_trans_set_sync(args
.trans
);
354 if ((flags
& ATTR_KERNOTIME
) == 0)
355 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
358 * Commit the last in the sequence of transactions.
360 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
361 error
= xfs_trans_commit(args
.trans
);
362 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
368 xfs_trans_cancel(args
.trans
);
369 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
374 * Generic handler routine to remove a name from an attribute list.
375 * Transitions attribute list from Btree to shortform as necessary.
379 struct xfs_inode
*dp
,
380 const unsigned char *name
,
383 struct xfs_mount
*mp
= dp
->i_mount
;
384 struct xfs_da_args args
;
385 struct xfs_bmap_free flist
;
386 xfs_fsblock_t firstblock
;
389 XFS_STATS_INC(mp
, xs_attr_remove
);
391 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
394 if (!xfs_inode_hasattr(dp
))
397 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
401 args
.firstblock
= &firstblock
;
405 * we have no control over the attribute names that userspace passes us
406 * to remove, so we have to allow the name lookup prior to attribute
409 args
.op_flags
= XFS_DA_OP_OKNOENT
;
411 error
= xfs_qm_dqattach(dp
, 0);
416 * Root fork attributes can use reserved data blocks for this
417 * operation if necessary
419 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
420 XFS_ATTRRM_SPACE_RES(mp
), 0,
421 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
426 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
428 * No need to make quota reservations here. We expect to release some
429 * blocks not allocate in the common case.
431 xfs_trans_ijoin(args
.trans
, dp
, 0);
433 if (!xfs_inode_hasattr(dp
)) {
435 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
436 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
437 error
= xfs_attr_shortform_remove(&args
);
438 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
439 error
= xfs_attr_leaf_removename(&args
);
441 error
= xfs_attr_node_removename(&args
);
448 * If this is a synchronous mount, make sure that the
449 * transaction goes to disk before returning to the user.
451 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
452 xfs_trans_set_sync(args
.trans
);
454 if ((flags
& ATTR_KERNOTIME
) == 0)
455 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
458 * Commit the last in the sequence of transactions.
460 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
461 error
= xfs_trans_commit(args
.trans
);
462 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
468 xfs_trans_cancel(args
.trans
);
469 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
473 /*========================================================================
474 * External routines when attribute list is inside the inode
475 *========================================================================*/
478 * Add a name to the shortform attribute list structure
479 * This is the external routine.
482 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
484 int newsize
, forkoff
, retval
;
486 trace_xfs_attr_sf_addname(args
);
488 retval
= xfs_attr_shortform_lookup(args
);
489 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
491 } else if (retval
== -EEXIST
) {
492 if (args
->flags
& ATTR_CREATE
)
494 retval
= xfs_attr_shortform_remove(args
);
498 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
499 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
502 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
503 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
505 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
509 xfs_attr_shortform_add(args
, forkoff
);
514 /*========================================================================
515 * External routines when attribute list is one block
516 *========================================================================*/
519 * Add a name to the leaf attribute list structure
521 * This leaf block cannot have a "remote" value, we only call this routine
522 * if bmap_one_block() says there is only one block (ie: no remote blks).
525 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
529 int retval
, error
, forkoff
;
531 trace_xfs_attr_leaf_addname(args
);
534 * Read the (only) block in the attribute list in.
538 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
543 * Look up the given attribute in the leaf block. Figure out if
544 * the given flags produce an error or call for an atomic rename.
546 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
547 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
548 xfs_trans_brelse(args
->trans
, bp
);
550 } else if (retval
== -EEXIST
) {
551 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
552 xfs_trans_brelse(args
->trans
, bp
);
556 trace_xfs_attr_leaf_replace(args
);
558 /* save the attribute state for later removal*/
559 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
560 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
561 args
->index2
= args
->index
;
562 args
->rmtblkno2
= args
->rmtblkno
;
563 args
->rmtblkcnt2
= args
->rmtblkcnt
;
564 args
->rmtvaluelen2
= args
->rmtvaluelen
;
567 * clear the remote attr state now that it is saved so that the
568 * values reflect the state of the attribute we are about to
569 * add, not the attribute we just found and will remove later.
573 args
->rmtvaluelen
= 0;
577 * Add the attribute to the leaf block, transitioning to a Btree
580 retval
= xfs_attr3_leaf_add(bp
, args
);
581 if (retval
== -ENOSPC
) {
583 * Promote the attribute list to the Btree format, then
584 * Commit that transaction so that the node_addname() call
585 * can manage its own transactions.
587 xfs_bmap_init(args
->flist
, args
->firstblock
);
588 error
= xfs_attr3_leaf_to_node(args
);
590 error
= xfs_bmap_finish(&args
->trans
, args
->flist
, dp
);
593 xfs_bmap_cancel(args
->flist
);
598 * Commit the current trans (including the inode) and start
601 error
= xfs_trans_roll(&args
->trans
, dp
);
606 * Fob the whole rest of the problem off on the Btree code.
608 error
= xfs_attr_node_addname(args
);
613 * Commit the transaction that added the attr name so that
614 * later routines can manage their own transactions.
616 error
= xfs_trans_roll(&args
->trans
, dp
);
621 * If there was an out-of-line value, allocate the blocks we
622 * identified for its storage and copy the value. This is done
623 * after we create the attribute so that we don't overflow the
624 * maximum size of a transaction and/or hit a deadlock.
626 if (args
->rmtblkno
> 0) {
627 error
= xfs_attr_rmtval_set(args
);
633 * If this is an atomic rename operation, we must "flip" the
634 * incomplete flags on the "new" and "old" attribute/value pairs
635 * so that one disappears and one appears atomically. Then we
636 * must remove the "old" attribute/value pair.
638 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
640 * In a separate transaction, set the incomplete flag on the
641 * "old" attr and clear the incomplete flag on the "new" attr.
643 error
= xfs_attr3_leaf_flipflags(args
);
648 * Dismantle the "old" attribute/value pair by removing
649 * a "remote" value (if it exists).
651 args
->index
= args
->index2
;
652 args
->blkno
= args
->blkno2
;
653 args
->rmtblkno
= args
->rmtblkno2
;
654 args
->rmtblkcnt
= args
->rmtblkcnt2
;
655 args
->rmtvaluelen
= args
->rmtvaluelen2
;
656 if (args
->rmtblkno
) {
657 error
= xfs_attr_rmtval_remove(args
);
663 * Read in the block containing the "old" attr, then
664 * remove the "old" attr from that block (neat, huh!)
666 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
671 xfs_attr3_leaf_remove(bp
, args
);
674 * If the result is small enough, shrink it all into the inode.
676 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
677 xfs_bmap_init(args
->flist
, args
->firstblock
);
678 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
679 /* bp is gone due to xfs_da_shrink_inode */
681 error
= xfs_bmap_finish(&args
->trans
,
685 xfs_bmap_cancel(args
->flist
);
691 * Commit the remove and start the next trans in series.
693 error
= xfs_trans_roll(&args
->trans
, dp
);
695 } else if (args
->rmtblkno
> 0) {
697 * Added a "remote" value, just clear the incomplete flag.
699 error
= xfs_attr3_leaf_clearflag(args
);
705 * Remove a name from the leaf attribute list structure
707 * This leaf block cannot have a "remote" value, we only call this routine
708 * if bmap_one_block() says there is only one block (ie: no remote blks).
711 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
717 trace_xfs_attr_leaf_removename(args
);
720 * Remove the attribute.
724 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
728 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
729 if (error
== -ENOATTR
) {
730 xfs_trans_brelse(args
->trans
, bp
);
734 xfs_attr3_leaf_remove(bp
, args
);
737 * If the result is small enough, shrink it all into the inode.
739 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
740 xfs_bmap_init(args
->flist
, args
->firstblock
);
741 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
742 /* bp is gone due to xfs_da_shrink_inode */
744 error
= xfs_bmap_finish(&args
->trans
, args
->flist
, dp
);
747 xfs_bmap_cancel(args
->flist
);
755 * Look up a name in a leaf attribute list structure.
757 * This leaf block cannot have a "remote" value, we only call this routine
758 * if bmap_one_block() says there is only one block (ie: no remote blks).
761 xfs_attr_leaf_get(xfs_da_args_t
*args
)
766 trace_xfs_attr_leaf_get(args
);
769 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
773 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
774 if (error
!= -EEXIST
) {
775 xfs_trans_brelse(args
->trans
, bp
);
778 error
= xfs_attr3_leaf_getvalue(bp
, args
);
779 xfs_trans_brelse(args
->trans
, bp
);
780 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
781 error
= xfs_attr_rmtval_get(args
);
786 /*========================================================================
787 * External routines when attribute list size > geo->blksize
788 *========================================================================*/
791 * Add a name to a Btree-format attribute list.
793 * This will involve walking down the Btree, and may involve splitting
794 * leaf nodes and even splitting intermediate nodes up to and including
795 * the root node (a special case of an intermediate node).
797 * "Remote" attribute values confuse the issue and atomic rename operations
798 * add a whole extra layer of confusion on top of that.
801 xfs_attr_node_addname(xfs_da_args_t
*args
)
803 xfs_da_state_t
*state
;
804 xfs_da_state_blk_t
*blk
;
809 trace_xfs_attr_node_addname(args
);
812 * Fill in bucket of arguments/results/context to carry around.
817 state
= xfs_da_state_alloc();
822 * Search to see if name already exists, and get back a pointer
823 * to where it should go.
825 error
= xfs_da3_node_lookup_int(state
, &retval
);
828 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
829 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
830 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
832 } else if (retval
== -EEXIST
) {
833 if (args
->flags
& ATTR_CREATE
)
836 trace_xfs_attr_node_replace(args
);
838 /* save the attribute state for later removal*/
839 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
840 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
841 args
->index2
= args
->index
;
842 args
->rmtblkno2
= args
->rmtblkno
;
843 args
->rmtblkcnt2
= args
->rmtblkcnt
;
844 args
->rmtvaluelen2
= args
->rmtvaluelen
;
847 * clear the remote attr state now that it is saved so that the
848 * values reflect the state of the attribute we are about to
849 * add, not the attribute we just found and will remove later.
853 args
->rmtvaluelen
= 0;
856 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
857 if (retval
== -ENOSPC
) {
858 if (state
->path
.active
== 1) {
860 * Its really a single leaf node, but it had
861 * out-of-line values so it looked like it *might*
862 * have been a b-tree.
864 xfs_da_state_free(state
);
866 xfs_bmap_init(args
->flist
, args
->firstblock
);
867 error
= xfs_attr3_leaf_to_node(args
);
869 error
= xfs_bmap_finish(&args
->trans
,
873 xfs_bmap_cancel(args
->flist
);
878 * Commit the node conversion and start the next
879 * trans in the chain.
881 error
= xfs_trans_roll(&args
->trans
, dp
);
889 * Split as many Btree elements as required.
890 * This code tracks the new and old attr's location
891 * in the index/blkno/rmtblkno/rmtblkcnt fields and
892 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
894 xfs_bmap_init(args
->flist
, args
->firstblock
);
895 error
= xfs_da3_split(state
);
897 error
= xfs_bmap_finish(&args
->trans
, args
->flist
, dp
);
900 xfs_bmap_cancel(args
->flist
);
905 * Addition succeeded, update Btree hashvals.
907 xfs_da3_fixhashpath(state
, &state
->path
);
911 * Kill the state structure, we're done with it and need to
912 * allow the buffers to come back later.
914 xfs_da_state_free(state
);
918 * Commit the leaf addition or btree split and start the next
919 * trans in the chain.
921 error
= xfs_trans_roll(&args
->trans
, dp
);
926 * If there was an out-of-line value, allocate the blocks we
927 * identified for its storage and copy the value. This is done
928 * after we create the attribute so that we don't overflow the
929 * maximum size of a transaction and/or hit a deadlock.
931 if (args
->rmtblkno
> 0) {
932 error
= xfs_attr_rmtval_set(args
);
938 * If this is an atomic rename operation, we must "flip" the
939 * incomplete flags on the "new" and "old" attribute/value pairs
940 * so that one disappears and one appears atomically. Then we
941 * must remove the "old" attribute/value pair.
943 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
945 * In a separate transaction, set the incomplete flag on the
946 * "old" attr and clear the incomplete flag on the "new" attr.
948 error
= xfs_attr3_leaf_flipflags(args
);
953 * Dismantle the "old" attribute/value pair by removing
954 * a "remote" value (if it exists).
956 args
->index
= args
->index2
;
957 args
->blkno
= args
->blkno2
;
958 args
->rmtblkno
= args
->rmtblkno2
;
959 args
->rmtblkcnt
= args
->rmtblkcnt2
;
960 args
->rmtvaluelen
= args
->rmtvaluelen2
;
961 if (args
->rmtblkno
) {
962 error
= xfs_attr_rmtval_remove(args
);
968 * Re-find the "old" attribute entry after any split ops.
969 * The INCOMPLETE flag means that we will find the "old"
970 * attr, not the "new" one.
972 args
->flags
|= XFS_ATTR_INCOMPLETE
;
973 state
= xfs_da_state_alloc();
977 error
= xfs_da3_node_lookup_int(state
, &retval
);
982 * Remove the name and update the hashvals in the tree.
984 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
985 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
986 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
987 xfs_da3_fixhashpath(state
, &state
->path
);
990 * Check to see if the tree needs to be collapsed.
992 if (retval
&& (state
->path
.active
> 1)) {
993 xfs_bmap_init(args
->flist
, args
->firstblock
);
994 error
= xfs_da3_join(state
);
996 error
= xfs_bmap_finish(&args
->trans
,
1000 xfs_bmap_cancel(args
->flist
);
1006 * Commit and start the next trans in the chain.
1008 error
= xfs_trans_roll(&args
->trans
, dp
);
1012 } else if (args
->rmtblkno
> 0) {
1014 * Added a "remote" value, just clear the incomplete flag.
1016 error
= xfs_attr3_leaf_clearflag(args
);
1024 xfs_da_state_free(state
);
1031 * Remove a name from a B-tree attribute list.
1033 * This will involve walking down the Btree, and may involve joining
1034 * leaf nodes and even joining intermediate nodes up to and including
1035 * the root node (a special case of an intermediate node).
1038 xfs_attr_node_removename(xfs_da_args_t
*args
)
1040 xfs_da_state_t
*state
;
1041 xfs_da_state_blk_t
*blk
;
1044 int retval
, error
, forkoff
;
1046 trace_xfs_attr_node_removename(args
);
1049 * Tie a string around our finger to remind us where we are.
1052 state
= xfs_da_state_alloc();
1054 state
->mp
= dp
->i_mount
;
1057 * Search to see if name exists, and get back a pointer to it.
1059 error
= xfs_da3_node_lookup_int(state
, &retval
);
1060 if (error
|| (retval
!= -EEXIST
)) {
1067 * If there is an out-of-line value, de-allocate the blocks.
1068 * This is done before we remove the attribute so that we don't
1069 * overflow the maximum size of a transaction and/or hit a deadlock.
1071 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1072 ASSERT(blk
->bp
!= NULL
);
1073 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1074 if (args
->rmtblkno
> 0) {
1076 * Fill in disk block numbers in the state structure
1077 * so that we can get the buffers back after we commit
1078 * several transactions in the following calls.
1080 error
= xfs_attr_fillstate(state
);
1085 * Mark the attribute as INCOMPLETE, then bunmapi() the
1088 error
= xfs_attr3_leaf_setflag(args
);
1091 error
= xfs_attr_rmtval_remove(args
);
1096 * Refill the state structure with buffers, the prior calls
1097 * released our buffers.
1099 error
= xfs_attr_refillstate(state
);
1105 * Remove the name and update the hashvals in the tree.
1107 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1108 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1109 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1110 xfs_da3_fixhashpath(state
, &state
->path
);
1113 * Check to see if the tree needs to be collapsed.
1115 if (retval
&& (state
->path
.active
> 1)) {
1116 xfs_bmap_init(args
->flist
, args
->firstblock
);
1117 error
= xfs_da3_join(state
);
1119 error
= xfs_bmap_finish(&args
->trans
, args
->flist
, dp
);
1122 xfs_bmap_cancel(args
->flist
);
1126 * Commit the Btree join operation and start a new trans.
1128 error
= xfs_trans_roll(&args
->trans
, dp
);
1134 * If the result is small enough, push it all into the inode.
1136 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1138 * Have to get rid of the copy of this dabuf in the state.
1140 ASSERT(state
->path
.active
== 1);
1141 ASSERT(state
->path
.blk
[0].bp
);
1142 state
->path
.blk
[0].bp
= NULL
;
1144 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1148 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1149 xfs_bmap_init(args
->flist
, args
->firstblock
);
1150 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1151 /* bp is gone due to xfs_da_shrink_inode */
1153 error
= xfs_bmap_finish(&args
->trans
,
1157 xfs_bmap_cancel(args
->flist
);
1161 xfs_trans_brelse(args
->trans
, bp
);
1166 xfs_da_state_free(state
);
1171 * Fill in the disk block numbers in the state structure for the buffers
1172 * that are attached to the state structure.
1173 * This is done so that we can quickly reattach ourselves to those buffers
1174 * after some set of transaction commits have released these buffers.
1177 xfs_attr_fillstate(xfs_da_state_t
*state
)
1179 xfs_da_state_path_t
*path
;
1180 xfs_da_state_blk_t
*blk
;
1183 trace_xfs_attr_fillstate(state
->args
);
1186 * Roll down the "path" in the state structure, storing the on-disk
1187 * block number for those buffers in the "path".
1189 path
= &state
->path
;
1190 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1191 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1193 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1196 blk
->disk_blkno
= 0;
1201 * Roll down the "altpath" in the state structure, storing the on-disk
1202 * block number for those buffers in the "altpath".
1204 path
= &state
->altpath
;
1205 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1206 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1208 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1211 blk
->disk_blkno
= 0;
1219 * Reattach the buffers to the state structure based on the disk block
1220 * numbers stored in the state structure.
1221 * This is done after some set of transaction commits have released those
1222 * buffers from our grip.
1225 xfs_attr_refillstate(xfs_da_state_t
*state
)
1227 xfs_da_state_path_t
*path
;
1228 xfs_da_state_blk_t
*blk
;
1231 trace_xfs_attr_refillstate(state
->args
);
1234 * Roll down the "path" in the state structure, storing the on-disk
1235 * block number for those buffers in the "path".
1237 path
= &state
->path
;
1238 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1239 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1240 if (blk
->disk_blkno
) {
1241 error
= xfs_da3_node_read(state
->args
->trans
,
1243 blk
->blkno
, blk
->disk_blkno
,
1244 &blk
->bp
, XFS_ATTR_FORK
);
1253 * Roll down the "altpath" in the state structure, storing the on-disk
1254 * block number for those buffers in the "altpath".
1256 path
= &state
->altpath
;
1257 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1258 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1259 if (blk
->disk_blkno
) {
1260 error
= xfs_da3_node_read(state
->args
->trans
,
1262 blk
->blkno
, blk
->disk_blkno
,
1263 &blk
->bp
, XFS_ATTR_FORK
);
1275 * Look up a filename in a node attribute list.
1277 * This routine gets called for any attribute fork that has more than one
1278 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1279 * "remote" values taking up more blocks.
1282 xfs_attr_node_get(xfs_da_args_t
*args
)
1284 xfs_da_state_t
*state
;
1285 xfs_da_state_blk_t
*blk
;
1289 trace_xfs_attr_node_get(args
);
1291 state
= xfs_da_state_alloc();
1293 state
->mp
= args
->dp
->i_mount
;
1296 * Search to see if name exists, and get back a pointer to it.
1298 error
= xfs_da3_node_lookup_int(state
, &retval
);
1301 } else if (retval
== -EEXIST
) {
1302 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1303 ASSERT(blk
->bp
!= NULL
);
1304 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1307 * Get the value, local or "remote"
1309 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1310 if (!retval
&& (args
->rmtblkno
> 0)
1311 && !(args
->flags
& ATTR_KERNOVAL
)) {
1312 retval
= xfs_attr_rmtval_get(args
);
1317 * If not in a transaction, we have to release all the buffers.
1319 for (i
= 0; i
< state
->path
.active
; i
++) {
1320 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1321 state
->path
.blk
[i
].bp
= NULL
;
1324 xfs_da_state_free(state
);
This page took 0.074014 seconds and 5 git commands to generate.