]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_attr.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_da_format.h"
15 #include "xfs_da_btree.h"
16 #include "xfs_attr_sf.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
20 #include "xfs_bmap_btree.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_trans_space.h"
25 #include "xfs_trace.h"
30 * Provide the external interfaces to manage attribute lists.
33 /*========================================================================
34 * Function prototypes for the kernel.
35 *========================================================================*/
38 * Internal routines when attribute list fits inside the inode.
40 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
43 * Internal routines when attribute list is one block.
45 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
46 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
47 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
50 * Internal routines when attribute list is more than one block.
52 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
53 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
54 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
55 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
56 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
61 struct xfs_da_args
*args
,
63 const unsigned char *name
,
70 memset(args
, 0, sizeof(*args
));
71 args
->geo
= dp
->i_mount
->m_attr_geo
;
72 args
->whichfork
= XFS_ATTR_FORK
;
76 args
->namelen
= strlen((const char *)name
);
77 if (args
->namelen
>= MAXNAMELEN
)
78 return -EFAULT
; /* match IRIX behaviour */
80 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
88 if (!XFS_IFORK_Q(ip
) ||
89 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
90 ip
->i_d
.di_anextents
== 0))
95 /*========================================================================
96 * Overall external interface routines.
97 *========================================================================*/
100 * Retrieve an extended attribute and its value. Must have ilock.
101 * Returns 0 on successful retrieval, otherwise an error.
104 xfs_attr_get_ilocked(
105 struct xfs_inode
*ip
,
106 struct xfs_da_args
*args
)
108 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
| XFS_ILOCK_EXCL
));
110 if (!xfs_inode_hasattr(ip
))
112 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
113 return xfs_attr_shortform_getvalue(args
);
114 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
115 return xfs_attr_leaf_get(args
);
117 return xfs_attr_node_get(args
);
121 * Retrieve an extended attribute by name, and its value if requested.
123 * If ATTR_KERNOVAL is set in @flags, then the caller does not want the value,
124 * just an indication whether the attribute exists and the size of the value if
125 * it exists. The size is returned in @valuelenp,
127 * If the attribute is found, but exceeds the size limit set by the caller in
128 * @valuelenp, return -ERANGE with the size of the attribute that was found in
131 * If ATTR_ALLOC is set in @flags, allocate the buffer for the value after
132 * existence of the attribute has been determined. On success, return that
133 * buffer to the caller and leave them to free it. On failure, free any
134 * allocated buffer and ensure the buffer pointer returned to the caller is
139 struct xfs_inode
*ip
,
140 const unsigned char *name
,
141 unsigned char **value
,
145 struct xfs_da_args args
;
149 ASSERT((flags
& (ATTR_ALLOC
| ATTR_KERNOVAL
)) || *value
);
151 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
153 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
156 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
160 /* Entirely possible to look up a name which doesn't exist */
161 args
.op_flags
= XFS_DA_OP_OKNOENT
;
162 if (flags
& ATTR_ALLOC
)
163 args
.op_flags
|= XFS_DA_OP_ALLOCVAL
;
166 args
.valuelen
= *valuelenp
;
168 lock_mode
= xfs_ilock_attr_map_shared(ip
);
169 error
= xfs_attr_get_ilocked(ip
, &args
);
170 xfs_iunlock(ip
, lock_mode
);
171 *valuelenp
= args
.valuelen
;
173 /* on error, we have to clean up allocated value buffers */
175 if (flags
& ATTR_ALLOC
) {
176 kmem_free(args
.value
);
186 * Calculate how many blocks we need for the new attribute,
190 struct xfs_da_args
*args
,
193 struct xfs_mount
*mp
= args
->dp
->i_mount
;
198 * Determine space new attribute will use, and if it would be
199 * "local" or "remote" (note: local != inline).
201 size
= xfs_attr_leaf_newentsize(args
, local
);
202 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
204 if (size
> (args
->geo
->blksize
/ 2)) {
205 /* Double split possible */
210 * Out of line attribute, cannot double split, but
211 * make room for the attribute value itself.
213 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
215 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
222 xfs_attr_try_sf_addname(
223 struct xfs_inode
*dp
,
224 struct xfs_da_args
*args
)
227 struct xfs_mount
*mp
= dp
->i_mount
;
230 error
= xfs_attr_shortform_addname(args
);
231 if (error
== -ENOSPC
)
235 * Commit the shortform mods, and we're done.
236 * NOTE: this is also the error path (EEXIST, etc).
238 if (!error
&& (args
->flags
& ATTR_KERNOTIME
) == 0)
239 xfs_trans_ichgtime(args
->trans
, dp
, XFS_ICHGTIME_CHG
);
241 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
242 xfs_trans_set_sync(args
->trans
);
244 error2
= xfs_trans_commit(args
->trans
);
246 return error
? error
: error2
;
250 * Set the attribute specified in @args.
254 struct xfs_da_args
*args
)
256 struct xfs_inode
*dp
= args
->dp
;
257 struct xfs_buf
*leaf_bp
= NULL
;
261 * If the attribute list is non-existent or a shortform list,
262 * upgrade it to a single-leaf-block attribute list.
264 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
265 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
266 dp
->i_d
.di_anextents
== 0)) {
269 * Build initial attribute list (if required).
271 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
272 xfs_attr_shortform_create(args
);
275 * Try to add the attr to the attribute list in the inode.
277 error
= xfs_attr_try_sf_addname(dp
, args
);
278 if (error
!= -ENOSPC
)
282 * It won't fit in the shortform, transform to a leaf block.
283 * GROT: another possible req'mt for a double-split btree op.
285 error
= xfs_attr_shortform_to_leaf(args
, &leaf_bp
);
290 * Prevent the leaf buffer from being unlocked so that a
291 * concurrent AIL push cannot grab the half-baked leaf
292 * buffer and run into problems with the write verifier.
293 * Once we're done rolling the transaction we can release
294 * the hold and add the attr to the leaf.
296 xfs_trans_bhold(args
->trans
, leaf_bp
);
297 error
= xfs_defer_finish(&args
->trans
);
298 xfs_trans_bhold_release(args
->trans
, leaf_bp
);
300 xfs_trans_brelse(args
->trans
, leaf_bp
);
305 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
306 error
= xfs_attr_leaf_addname(args
);
308 error
= xfs_attr_node_addname(args
);
313 * Remove the attribute specified in @args.
316 xfs_attr_remove_args(
317 struct xfs_da_args
*args
)
319 struct xfs_inode
*dp
= args
->dp
;
322 if (!xfs_inode_hasattr(dp
)) {
324 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
325 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
326 error
= xfs_attr_shortform_remove(args
);
327 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
328 error
= xfs_attr_leaf_removename(args
);
330 error
= xfs_attr_node_removename(args
);
338 struct xfs_inode
*dp
,
339 const unsigned char *name
,
340 unsigned char *value
,
344 struct xfs_mount
*mp
= dp
->i_mount
;
345 struct xfs_da_args args
;
346 struct xfs_trans_res tres
;
347 int rsvd
= (flags
& ATTR_ROOT
) != 0;
350 XFS_STATS_INC(mp
, xs_attr_set
);
352 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
355 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
360 args
.valuelen
= valuelen
;
361 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
362 args
.total
= xfs_attr_calc_size(&args
, &local
);
364 error
= xfs_qm_dqattach(dp
);
369 * If the inode doesn't have an attribute fork, add one.
370 * (inode must not be locked when we call this routine)
372 if (XFS_IFORK_Q(dp
) == 0) {
373 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
374 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
376 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
381 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
382 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
383 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
384 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
387 * Root fork attributes can use reserved data blocks for this
388 * operation if necessary
390 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
391 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
395 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
396 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
397 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
398 XFS_QMOPT_RES_REGBLKS
);
400 goto out_trans_cancel
;
402 xfs_trans_ijoin(args
.trans
, dp
, 0);
403 error
= xfs_attr_set_args(&args
);
405 goto out_trans_cancel
;
407 /* shortform attribute has already been committed */
412 * If this is a synchronous mount, make sure that the
413 * transaction goes to disk before returning to the user.
415 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
416 xfs_trans_set_sync(args
.trans
);
418 if ((flags
& ATTR_KERNOTIME
) == 0)
419 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
422 * Commit the last in the sequence of transactions.
424 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
425 error
= xfs_trans_commit(args
.trans
);
427 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
432 xfs_trans_cancel(args
.trans
);
437 * Generic handler routine to remove a name from an attribute list.
438 * Transitions attribute list from Btree to shortform as necessary.
442 struct xfs_inode
*dp
,
443 const unsigned char *name
,
446 struct xfs_mount
*mp
= dp
->i_mount
;
447 struct xfs_da_args args
;
450 XFS_STATS_INC(mp
, xs_attr_remove
);
452 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
455 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
460 * we have no control over the attribute names that userspace passes us
461 * to remove, so we have to allow the name lookup prior to attribute
464 args
.op_flags
= XFS_DA_OP_OKNOENT
;
466 error
= xfs_qm_dqattach(dp
);
471 * Root fork attributes can use reserved data blocks for this
472 * operation if necessary
474 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
475 XFS_ATTRRM_SPACE_RES(mp
), 0,
476 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
481 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
483 * No need to make quota reservations here. We expect to release some
484 * blocks not allocate in the common case.
486 xfs_trans_ijoin(args
.trans
, dp
, 0);
488 error
= xfs_attr_remove_args(&args
);
493 * If this is a synchronous mount, make sure that the
494 * transaction goes to disk before returning to the user.
496 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
497 xfs_trans_set_sync(args
.trans
);
499 if ((flags
& ATTR_KERNOTIME
) == 0)
500 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
503 * Commit the last in the sequence of transactions.
505 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
506 error
= xfs_trans_commit(args
.trans
);
507 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
513 xfs_trans_cancel(args
.trans
);
514 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
518 /*========================================================================
519 * External routines when attribute list is inside the inode
520 *========================================================================*/
523 * Add a name to the shortform attribute list structure
524 * This is the external routine.
527 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
529 int newsize
, forkoff
, retval
;
531 trace_xfs_attr_sf_addname(args
);
533 retval
= xfs_attr_shortform_lookup(args
);
534 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
536 } else if (retval
== -EEXIST
) {
537 if (args
->flags
& ATTR_CREATE
)
539 retval
= xfs_attr_shortform_remove(args
);
543 * Since we have removed the old attr, clear ATTR_REPLACE so
544 * that the leaf format add routine won't trip over the attr
547 args
->flags
&= ~ATTR_REPLACE
;
550 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
551 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
554 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
555 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
557 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
561 xfs_attr_shortform_add(args
, forkoff
);
566 /*========================================================================
567 * External routines when attribute list is one block
568 *========================================================================*/
571 * Add a name to the leaf attribute list structure
573 * This leaf block cannot have a "remote" value, we only call this routine
574 * if bmap_one_block() says there is only one block (ie: no remote blks).
577 xfs_attr_leaf_addname(
578 struct xfs_da_args
*args
)
580 struct xfs_inode
*dp
;
582 int retval
, error
, forkoff
;
584 trace_xfs_attr_leaf_addname(args
);
587 * Read the (only) block in the attribute list in.
591 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
596 * Look up the given attribute in the leaf block. Figure out if
597 * the given flags produce an error or call for an atomic rename.
599 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
600 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
601 xfs_trans_brelse(args
->trans
, bp
);
603 } else if (retval
== -EEXIST
) {
604 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
605 xfs_trans_brelse(args
->trans
, bp
);
609 trace_xfs_attr_leaf_replace(args
);
611 /* save the attribute state for later removal*/
612 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
613 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
614 args
->index2
= args
->index
;
615 args
->rmtblkno2
= args
->rmtblkno
;
616 args
->rmtblkcnt2
= args
->rmtblkcnt
;
617 args
->rmtvaluelen2
= args
->rmtvaluelen
;
620 * clear the remote attr state now that it is saved so that the
621 * values reflect the state of the attribute we are about to
622 * add, not the attribute we just found and will remove later.
626 args
->rmtvaluelen
= 0;
630 * Add the attribute to the leaf block, transitioning to a Btree
633 retval
= xfs_attr3_leaf_add(bp
, args
);
634 if (retval
== -ENOSPC
) {
636 * Promote the attribute list to the Btree format, then
637 * Commit that transaction so that the node_addname() call
638 * can manage its own transactions.
640 error
= xfs_attr3_leaf_to_node(args
);
643 error
= xfs_defer_finish(&args
->trans
);
648 * Commit the current trans (including the inode) and start
651 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
656 * Fob the whole rest of the problem off on the Btree code.
658 error
= xfs_attr_node_addname(args
);
663 * Commit the transaction that added the attr name so that
664 * later routines can manage their own transactions.
666 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
671 * If there was an out-of-line value, allocate the blocks we
672 * identified for its storage and copy the value. This is done
673 * after we create the attribute so that we don't overflow the
674 * maximum size of a transaction and/or hit a deadlock.
676 if (args
->rmtblkno
> 0) {
677 error
= xfs_attr_rmtval_set(args
);
683 * If this is an atomic rename operation, we must "flip" the
684 * incomplete flags on the "new" and "old" attribute/value pairs
685 * so that one disappears and one appears atomically. Then we
686 * must remove the "old" attribute/value pair.
688 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
690 * In a separate transaction, set the incomplete flag on the
691 * "old" attr and clear the incomplete flag on the "new" attr.
693 error
= xfs_attr3_leaf_flipflags(args
);
698 * Dismantle the "old" attribute/value pair by removing
699 * a "remote" value (if it exists).
701 args
->index
= args
->index2
;
702 args
->blkno
= args
->blkno2
;
703 args
->rmtblkno
= args
->rmtblkno2
;
704 args
->rmtblkcnt
= args
->rmtblkcnt2
;
705 args
->rmtvaluelen
= args
->rmtvaluelen2
;
706 if (args
->rmtblkno
) {
707 error
= xfs_attr_rmtval_remove(args
);
713 * Read in the block containing the "old" attr, then
714 * remove the "old" attr from that block (neat, huh!)
716 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
721 xfs_attr3_leaf_remove(bp
, args
);
724 * If the result is small enough, shrink it all into the inode.
726 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
727 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
728 /* bp is gone due to xfs_da_shrink_inode */
731 error
= xfs_defer_finish(&args
->trans
);
737 * Commit the remove and start the next trans in series.
739 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
741 } else if (args
->rmtblkno
> 0) {
743 * Added a "remote" value, just clear the incomplete flag.
745 error
= xfs_attr3_leaf_clearflag(args
);
751 * Remove a name from the leaf attribute list structure
753 * This leaf block cannot have a "remote" value, we only call this routine
754 * if bmap_one_block() says there is only one block (ie: no remote blks).
757 xfs_attr_leaf_removename(
758 struct xfs_da_args
*args
)
760 struct xfs_inode
*dp
;
764 trace_xfs_attr_leaf_removename(args
);
767 * Remove the attribute.
771 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
775 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
776 if (error
== -ENOATTR
) {
777 xfs_trans_brelse(args
->trans
, bp
);
781 xfs_attr3_leaf_remove(bp
, args
);
784 * If the result is small enough, shrink it all into the inode.
786 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
787 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
788 /* bp is gone due to xfs_da_shrink_inode */
791 error
= xfs_defer_finish(&args
->trans
);
799 * Look up a name in a leaf attribute list structure.
801 * This leaf block cannot have a "remote" value, we only call this routine
802 * if bmap_one_block() says there is only one block (ie: no remote blks).
804 * Returns 0 on successful retrieval, otherwise an error.
807 xfs_attr_leaf_get(xfs_da_args_t
*args
)
812 trace_xfs_attr_leaf_get(args
);
815 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
819 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
820 if (error
!= -EEXIST
) {
821 xfs_trans_brelse(args
->trans
, bp
);
824 error
= xfs_attr3_leaf_getvalue(bp
, args
);
825 xfs_trans_brelse(args
->trans
, bp
);
829 /*========================================================================
830 * External routines when attribute list size > geo->blksize
831 *========================================================================*/
834 * Add a name to a Btree-format attribute list.
836 * This will involve walking down the Btree, and may involve splitting
837 * leaf nodes and even splitting intermediate nodes up to and including
838 * the root node (a special case of an intermediate node).
840 * "Remote" attribute values confuse the issue and atomic rename operations
841 * add a whole extra layer of confusion on top of that.
844 xfs_attr_node_addname(
845 struct xfs_da_args
*args
)
847 struct xfs_da_state
*state
;
848 struct xfs_da_state_blk
*blk
;
849 struct xfs_inode
*dp
;
850 struct xfs_mount
*mp
;
853 trace_xfs_attr_node_addname(args
);
856 * Fill in bucket of arguments/results/context to carry around.
861 state
= xfs_da_state_alloc();
866 * Search to see if name already exists, and get back a pointer
867 * to where it should go.
869 error
= xfs_da3_node_lookup_int(state
, &retval
);
872 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
873 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
874 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
876 } else if (retval
== -EEXIST
) {
877 if (args
->flags
& ATTR_CREATE
)
880 trace_xfs_attr_node_replace(args
);
882 /* save the attribute state for later removal*/
883 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
884 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
885 args
->index2
= args
->index
;
886 args
->rmtblkno2
= args
->rmtblkno
;
887 args
->rmtblkcnt2
= args
->rmtblkcnt
;
888 args
->rmtvaluelen2
= args
->rmtvaluelen
;
891 * clear the remote attr state now that it is saved so that the
892 * values reflect the state of the attribute we are about to
893 * add, not the attribute we just found and will remove later.
897 args
->rmtvaluelen
= 0;
900 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
901 if (retval
== -ENOSPC
) {
902 if (state
->path
.active
== 1) {
904 * Its really a single leaf node, but it had
905 * out-of-line values so it looked like it *might*
906 * have been a b-tree.
908 xfs_da_state_free(state
);
910 error
= xfs_attr3_leaf_to_node(args
);
913 error
= xfs_defer_finish(&args
->trans
);
918 * Commit the node conversion and start the next
919 * trans in the chain.
921 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
929 * Split as many Btree elements as required.
930 * This code tracks the new and old attr's location
931 * in the index/blkno/rmtblkno/rmtblkcnt fields and
932 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
934 error
= xfs_da3_split(state
);
937 error
= xfs_defer_finish(&args
->trans
);
942 * Addition succeeded, update Btree hashvals.
944 xfs_da3_fixhashpath(state
, &state
->path
);
948 * Kill the state structure, we're done with it and need to
949 * allow the buffers to come back later.
951 xfs_da_state_free(state
);
955 * Commit the leaf addition or btree split and start the next
956 * trans in the chain.
958 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
963 * If there was an out-of-line value, allocate the blocks we
964 * identified for its storage and copy the value. This is done
965 * after we create the attribute so that we don't overflow the
966 * maximum size of a transaction and/or hit a deadlock.
968 if (args
->rmtblkno
> 0) {
969 error
= xfs_attr_rmtval_set(args
);
975 * If this is an atomic rename operation, we must "flip" the
976 * incomplete flags on the "new" and "old" attribute/value pairs
977 * so that one disappears and one appears atomically. Then we
978 * must remove the "old" attribute/value pair.
980 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
982 * In a separate transaction, set the incomplete flag on the
983 * "old" attr and clear the incomplete flag on the "new" attr.
985 error
= xfs_attr3_leaf_flipflags(args
);
990 * Dismantle the "old" attribute/value pair by removing
991 * a "remote" value (if it exists).
993 args
->index
= args
->index2
;
994 args
->blkno
= args
->blkno2
;
995 args
->rmtblkno
= args
->rmtblkno2
;
996 args
->rmtblkcnt
= args
->rmtblkcnt2
;
997 args
->rmtvaluelen
= args
->rmtvaluelen2
;
998 if (args
->rmtblkno
) {
999 error
= xfs_attr_rmtval_remove(args
);
1005 * Re-find the "old" attribute entry after any split ops.
1006 * The INCOMPLETE flag means that we will find the "old"
1007 * attr, not the "new" one.
1009 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1010 state
= xfs_da_state_alloc();
1014 error
= xfs_da3_node_lookup_int(state
, &retval
);
1019 * Remove the name and update the hashvals in the tree.
1021 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1022 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1023 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1024 xfs_da3_fixhashpath(state
, &state
->path
);
1027 * Check to see if the tree needs to be collapsed.
1029 if (retval
&& (state
->path
.active
> 1)) {
1030 error
= xfs_da3_join(state
);
1033 error
= xfs_defer_finish(&args
->trans
);
1039 * Commit and start the next trans in the chain.
1041 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1045 } else if (args
->rmtblkno
> 0) {
1047 * Added a "remote" value, just clear the incomplete flag.
1049 error
= xfs_attr3_leaf_clearflag(args
);
1057 xfs_da_state_free(state
);
1064 * Remove a name from a B-tree attribute list.
1066 * This will involve walking down the Btree, and may involve joining
1067 * leaf nodes and even joining intermediate nodes up to and including
1068 * the root node (a special case of an intermediate node).
1071 xfs_attr_node_removename(
1072 struct xfs_da_args
*args
)
1074 struct xfs_da_state
*state
;
1075 struct xfs_da_state_blk
*blk
;
1076 struct xfs_inode
*dp
;
1078 int retval
, error
, forkoff
;
1080 trace_xfs_attr_node_removename(args
);
1083 * Tie a string around our finger to remind us where we are.
1086 state
= xfs_da_state_alloc();
1088 state
->mp
= dp
->i_mount
;
1091 * Search to see if name exists, and get back a pointer to it.
1093 error
= xfs_da3_node_lookup_int(state
, &retval
);
1094 if (error
|| (retval
!= -EEXIST
)) {
1101 * If there is an out-of-line value, de-allocate the blocks.
1102 * This is done before we remove the attribute so that we don't
1103 * overflow the maximum size of a transaction and/or hit a deadlock.
1105 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1106 ASSERT(blk
->bp
!= NULL
);
1107 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1108 if (args
->rmtblkno
> 0) {
1110 * Fill in disk block numbers in the state structure
1111 * so that we can get the buffers back after we commit
1112 * several transactions in the following calls.
1114 error
= xfs_attr_fillstate(state
);
1119 * Mark the attribute as INCOMPLETE, then bunmapi() the
1122 error
= xfs_attr3_leaf_setflag(args
);
1125 error
= xfs_attr_rmtval_remove(args
);
1130 * Refill the state structure with buffers, the prior calls
1131 * released our buffers.
1133 error
= xfs_attr_refillstate(state
);
1139 * Remove the name and update the hashvals in the tree.
1141 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1142 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1143 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1144 xfs_da3_fixhashpath(state
, &state
->path
);
1147 * Check to see if the tree needs to be collapsed.
1149 if (retval
&& (state
->path
.active
> 1)) {
1150 error
= xfs_da3_join(state
);
1153 error
= xfs_defer_finish(&args
->trans
);
1157 * Commit the Btree join operation and start a new trans.
1159 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1165 * If the result is small enough, push it all into the inode.
1167 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1169 * Have to get rid of the copy of this dabuf in the state.
1171 ASSERT(state
->path
.active
== 1);
1172 ASSERT(state
->path
.blk
[0].bp
);
1173 state
->path
.blk
[0].bp
= NULL
;
1175 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1179 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1180 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1181 /* bp is gone due to xfs_da_shrink_inode */
1184 error
= xfs_defer_finish(&args
->trans
);
1188 xfs_trans_brelse(args
->trans
, bp
);
1193 xfs_da_state_free(state
);
1198 * Fill in the disk block numbers in the state structure for the buffers
1199 * that are attached to the state structure.
1200 * This is done so that we can quickly reattach ourselves to those buffers
1201 * after some set of transaction commits have released these buffers.
1204 xfs_attr_fillstate(xfs_da_state_t
*state
)
1206 xfs_da_state_path_t
*path
;
1207 xfs_da_state_blk_t
*blk
;
1210 trace_xfs_attr_fillstate(state
->args
);
1213 * Roll down the "path" in the state structure, storing the on-disk
1214 * block number for those buffers in the "path".
1216 path
= &state
->path
;
1217 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1218 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1220 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1223 blk
->disk_blkno
= 0;
1228 * Roll down the "altpath" in the state structure, storing the on-disk
1229 * block number for those buffers in the "altpath".
1231 path
= &state
->altpath
;
1232 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1233 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1235 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1238 blk
->disk_blkno
= 0;
1246 * Reattach the buffers to the state structure based on the disk block
1247 * numbers stored in the state structure.
1248 * This is done after some set of transaction commits have released those
1249 * buffers from our grip.
1252 xfs_attr_refillstate(xfs_da_state_t
*state
)
1254 xfs_da_state_path_t
*path
;
1255 xfs_da_state_blk_t
*blk
;
1258 trace_xfs_attr_refillstate(state
->args
);
1261 * Roll down the "path" in the state structure, storing the on-disk
1262 * block number for those buffers in the "path".
1264 path
= &state
->path
;
1265 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1266 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1267 if (blk
->disk_blkno
) {
1268 error
= xfs_da3_node_read(state
->args
->trans
,
1270 blk
->blkno
, blk
->disk_blkno
,
1271 &blk
->bp
, XFS_ATTR_FORK
);
1280 * Roll down the "altpath" in the state structure, storing the on-disk
1281 * block number for those buffers in the "altpath".
1283 path
= &state
->altpath
;
1284 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1285 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1286 if (blk
->disk_blkno
) {
1287 error
= xfs_da3_node_read(state
->args
->trans
,
1289 blk
->blkno
, blk
->disk_blkno
,
1290 &blk
->bp
, XFS_ATTR_FORK
);
1302 * Retrieve the attribute data from a node attribute list.
1304 * This routine gets called for any attribute fork that has more than one
1305 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1306 * "remote" values taking up more blocks.
1308 * Returns 0 on successful retrieval, otherwise an error.
1311 xfs_attr_node_get(xfs_da_args_t
*args
)
1313 xfs_da_state_t
*state
;
1314 xfs_da_state_blk_t
*blk
;
1318 trace_xfs_attr_node_get(args
);
1320 state
= xfs_da_state_alloc();
1322 state
->mp
= args
->dp
->i_mount
;
1325 * Search to see if name exists, and get back a pointer to it.
1327 error
= xfs_da3_node_lookup_int(state
, &retval
);
1332 if (retval
!= -EEXIST
)
1336 * Get the value, local or "remote"
1338 blk
= &state
->path
.blk
[state
->path
.active
- 1];
1339 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1342 * If not in a transaction, we have to release all the buffers.
1345 for (i
= 0; i
< state
->path
.active
; i
++) {
1346 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1347 state
->path
.blk
[i
].bp
= NULL
;
1350 xfs_da_state_free(state
);
1354 /* Returns true if the attribute entry name is valid. */
1361 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1362 * out, so use >= for the length check.
1364 if (length
>= MAXNAMELEN
)
1367 /* There shouldn't be any nulls here */
1368 return !memchr(name
, 0, length
);