]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/xfs/libxfs/xfs_attr.c
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_inode.h"
31 #include "xfs_alloc.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
35 #include "xfs_bmap_util.h"
36 #include "xfs_bmap_btree.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_trace.h"
48 * Provide the external interfaces to manage attribute lists.
51 /*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
56 * Internal routines when attribute list fits inside the inode.
58 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
61 * Internal routines when attribute list is one block.
63 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
64 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
65 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
68 * Internal routines when attribute list is more than one block.
70 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
71 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
73 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
74 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
79 struct xfs_da_args
*args
,
81 const unsigned char *name
,
88 memset(args
, 0, sizeof(*args
));
89 args
->geo
= dp
->i_mount
->m_attr_geo
;
90 args
->whichfork
= XFS_ATTR_FORK
;
94 args
->namelen
= strlen((const char *)name
);
95 if (args
->namelen
>= MAXNAMELEN
)
96 return -EFAULT
; /* match IRIX behaviour */
98 args
->hashval
= xfs_da_hashname(args
->name
, args
->namelen
);
104 struct xfs_inode
*ip
)
106 if (!XFS_IFORK_Q(ip
) ||
107 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
108 ip
->i_d
.di_anextents
== 0))
113 /*========================================================================
114 * Overall external interface routines.
115 *========================================================================*/
117 /* Retrieve an extended attribute and its value. Must have ilock. */
119 xfs_attr_get_ilocked(
120 struct xfs_inode
*ip
,
121 struct xfs_da_args
*args
)
123 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_SHARED
| XFS_ILOCK_EXCL
));
125 if (!xfs_inode_hasattr(ip
))
127 else if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
)
128 return xfs_attr_shortform_getvalue(args
);
129 else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
))
130 return xfs_attr_leaf_get(args
);
132 return xfs_attr_node_get(args
);
135 /* Retrieve an extended attribute by name, and its value. */
138 struct xfs_inode
*ip
,
139 const unsigned char *name
,
140 unsigned char *value
,
144 struct xfs_da_args args
;
148 XFS_STATS_INC(ip
->i_mount
, xs_attr_get
);
150 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
153 error
= xfs_attr_args_init(&args
, ip
, name
, flags
);
158 args
.valuelen
= *valuelenp
;
159 /* Entirely possible to look up a name which doesn't exist */
160 args
.op_flags
= XFS_DA_OP_OKNOENT
;
162 lock_mode
= xfs_ilock_attr_map_shared(ip
);
163 error
= xfs_attr_get_ilocked(ip
, &args
);
164 xfs_iunlock(ip
, lock_mode
);
166 *valuelenp
= args
.valuelen
;
167 return error
== -EEXIST
? 0 : error
;
171 * Calculate how many blocks we need for the new attribute,
175 struct xfs_da_args
*args
,
178 struct xfs_mount
*mp
= args
->dp
->i_mount
;
183 * Determine space new attribute will use, and if it would be
184 * "local" or "remote" (note: local != inline).
186 size
= xfs_attr_leaf_newentsize(args
, local
);
187 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
189 if (size
> (args
->geo
->blksize
/ 2)) {
190 /* Double split possible */
195 * Out of line attribute, cannot double split, but
196 * make room for the attribute value itself.
198 uint dblocks
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
200 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
207 xfs_attr_try_sf_addname(
208 struct xfs_inode
*dp
,
209 struct xfs_da_args
*args
)
212 struct xfs_mount
*mp
= dp
->i_mount
;
215 error
= xfs_attr_shortform_addname(args
);
216 if (error
== -ENOSPC
)
220 * Commit the shortform mods, and we're done.
221 * NOTE: this is also the error path (EEXIST, etc).
223 if (!error
&& (args
->flags
& ATTR_KERNOTIME
) == 0)
224 xfs_trans_ichgtime(args
->trans
, dp
, XFS_ICHGTIME_CHG
);
226 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
227 xfs_trans_set_sync(args
->trans
);
229 error2
= xfs_trans_commit(args
->trans
);
230 return error
? error
: error2
;
234 * Remove the attribute specified in @args.
237 xfs_attr_remove_args(
238 struct xfs_da_args
*args
)
240 struct xfs_inode
*dp
= args
->dp
;
243 if (!xfs_inode_hasattr(dp
)) {
245 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
246 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
247 error
= xfs_attr_shortform_remove(args
);
248 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
249 error
= xfs_attr_leaf_removename(args
);
251 error
= xfs_attr_node_removename(args
);
259 struct xfs_inode
*dp
,
260 const unsigned char *name
,
261 unsigned char *value
,
265 struct xfs_mount
*mp
= dp
->i_mount
;
266 struct xfs_buf
*leaf_bp
= NULL
;
267 struct xfs_da_args args
;
268 struct xfs_defer_ops dfops
;
269 struct xfs_trans_res tres
;
270 xfs_fsblock_t firstblock
;
271 int rsvd
= (flags
& ATTR_ROOT
) != 0;
274 XFS_STATS_INC(mp
, xs_attr_set
);
276 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
279 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
284 args
.valuelen
= valuelen
;
285 args
.firstblock
= &firstblock
;
287 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
288 args
.total
= xfs_attr_calc_size(&args
, &local
);
290 error
= xfs_qm_dqattach(dp
, 0);
295 * If the inode doesn't have an attribute fork, add one.
296 * (inode must not be locked when we call this routine)
298 if (XFS_IFORK_Q(dp
) == 0) {
299 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
300 XFS_ATTR_SF_ENTSIZE_BYNAME(args
.namelen
, valuelen
);
302 error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
);
307 tres
.tr_logres
= M_RES(mp
)->tr_attrsetm
.tr_logres
+
308 M_RES(mp
)->tr_attrsetrt
.tr_logres
* args
.total
;
309 tres
.tr_logcount
= XFS_ATTRSET_LOG_COUNT
;
310 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
313 * Root fork attributes can use reserved data blocks for this
314 * operation if necessary
316 error
= xfs_trans_alloc(mp
, &tres
, args
.total
, 0,
317 rsvd
? XFS_TRANS_RESERVE
: 0, &args
.trans
);
321 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
322 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
323 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
324 XFS_QMOPT_RES_REGBLKS
);
326 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
327 xfs_trans_cancel(args
.trans
);
331 xfs_trans_ijoin(args
.trans
, dp
, 0);
334 * If the attribute list is non-existent or a shortform list,
335 * upgrade it to a single-leaf-block attribute list.
337 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
||
338 (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
339 dp
->i_d
.di_anextents
== 0)) {
342 * Build initial attribute list (if required).
344 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
345 xfs_attr_shortform_create(&args
);
348 * Try to add the attr to the attribute list in
351 error
= xfs_attr_try_sf_addname(dp
, &args
);
352 if (error
!= -ENOSPC
) {
353 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
358 * It won't fit in the shortform, transform to a leaf block.
359 * GROT: another possible req'mt for a double-split btree op.
361 xfs_defer_init(args
.dfops
, args
.firstblock
);
362 error
= xfs_attr_shortform_to_leaf(&args
, &leaf_bp
);
364 goto out_defer_cancel
;
366 * Prevent the leaf buffer from being unlocked so that a
367 * concurrent AIL push cannot grab the half-baked leaf
368 * buffer and run into problems with the write verifier.
370 xfs_trans_bhold(args
.trans
, leaf_bp
);
371 xfs_defer_bjoin(args
.dfops
, leaf_bp
);
372 xfs_defer_ijoin(args
.dfops
, dp
);
373 error
= xfs_defer_finish(&args
.trans
, args
.dfops
);
375 goto out_defer_cancel
;
378 * Commit the leaf transformation. We'll need another (linked)
379 * transaction to add the new attribute to the leaf, which
380 * means that we have to hold & join the leaf buffer here too.
382 error
= xfs_trans_roll_inode(&args
.trans
, dp
);
385 xfs_trans_bjoin(args
.trans
, leaf_bp
);
389 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
))
390 error
= xfs_attr_leaf_addname(&args
);
392 error
= xfs_attr_node_addname(&args
);
397 * If this is a synchronous mount, make sure that the
398 * transaction goes to disk before returning to the user.
400 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
401 xfs_trans_set_sync(args
.trans
);
403 if ((flags
& ATTR_KERNOTIME
) == 0)
404 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
407 * Commit the last in the sequence of transactions.
409 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
410 error
= xfs_trans_commit(args
.trans
);
411 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
416 xfs_defer_cancel(&dfops
);
419 xfs_trans_brelse(args
.trans
, leaf_bp
);
421 xfs_trans_cancel(args
.trans
);
422 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
427 * Generic handler routine to remove a name from an attribute list.
428 * Transitions attribute list from Btree to shortform as necessary.
432 struct xfs_inode
*dp
,
433 const unsigned char *name
,
436 struct xfs_mount
*mp
= dp
->i_mount
;
437 struct xfs_da_args args
;
438 struct xfs_defer_ops dfops
;
439 xfs_fsblock_t firstblock
;
442 XFS_STATS_INC(mp
, xs_attr_remove
);
444 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
447 error
= xfs_attr_args_init(&args
, dp
, name
, flags
);
451 args
.firstblock
= &firstblock
;
455 * we have no control over the attribute names that userspace passes us
456 * to remove, so we have to allow the name lookup prior to attribute
459 args
.op_flags
= XFS_DA_OP_OKNOENT
;
461 error
= xfs_qm_dqattach(dp
, 0);
466 * Root fork attributes can use reserved data blocks for this
467 * operation if necessary
469 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_attrrm
,
470 XFS_ATTRRM_SPACE_RES(mp
), 0,
471 (flags
& ATTR_ROOT
) ? XFS_TRANS_RESERVE
: 0,
476 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
478 * No need to make quota reservations here. We expect to release some
479 * blocks not allocate in the common case.
481 xfs_trans_ijoin(args
.trans
, dp
, 0);
483 error
= xfs_attr_remove_args(&args
);
488 * If this is a synchronous mount, make sure that the
489 * transaction goes to disk before returning to the user.
491 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
492 xfs_trans_set_sync(args
.trans
);
494 if ((flags
& ATTR_KERNOTIME
) == 0)
495 xfs_trans_ichgtime(args
.trans
, dp
, XFS_ICHGTIME_CHG
);
498 * Commit the last in the sequence of transactions.
500 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
501 error
= xfs_trans_commit(args
.trans
);
502 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
508 xfs_trans_cancel(args
.trans
);
509 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
513 /*========================================================================
514 * External routines when attribute list is inside the inode
515 *========================================================================*/
518 * Add a name to the shortform attribute list structure
519 * This is the external routine.
522 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
524 int newsize
, forkoff
, retval
;
526 trace_xfs_attr_sf_addname(args
);
528 retval
= xfs_attr_shortform_lookup(args
);
529 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
531 } else if (retval
== -EEXIST
) {
532 if (args
->flags
& ATTR_CREATE
)
534 retval
= xfs_attr_shortform_remove(args
);
538 * Since we have removed the old attr, clear ATTR_REPLACE so
539 * that the leaf format add routine won't trip over the attr
542 args
->flags
&= ~ATTR_REPLACE
;
545 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
546 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
549 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
550 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
552 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
556 xfs_attr_shortform_add(args
, forkoff
);
561 /*========================================================================
562 * External routines when attribute list is one block
563 *========================================================================*/
566 * Add a name to the leaf attribute list structure
568 * This leaf block cannot have a "remote" value, we only call this routine
569 * if bmap_one_block() says there is only one block (ie: no remote blks).
572 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
576 int retval
, error
, forkoff
;
578 trace_xfs_attr_leaf_addname(args
);
581 * Read the (only) block in the attribute list in.
585 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
590 * Look up the given attribute in the leaf block. Figure out if
591 * the given flags produce an error or call for an atomic rename.
593 retval
= xfs_attr3_leaf_lookup_int(bp
, args
);
594 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
595 xfs_trans_brelse(args
->trans
, bp
);
597 } else if (retval
== -EEXIST
) {
598 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
599 xfs_trans_brelse(args
->trans
, bp
);
603 trace_xfs_attr_leaf_replace(args
);
605 /* save the attribute state for later removal*/
606 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
607 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
608 args
->index2
= args
->index
;
609 args
->rmtblkno2
= args
->rmtblkno
;
610 args
->rmtblkcnt2
= args
->rmtblkcnt
;
611 args
->rmtvaluelen2
= args
->rmtvaluelen
;
614 * clear the remote attr state now that it is saved so that the
615 * values reflect the state of the attribute we are about to
616 * add, not the attribute we just found and will remove later.
620 args
->rmtvaluelen
= 0;
624 * Add the attribute to the leaf block, transitioning to a Btree
627 retval
= xfs_attr3_leaf_add(bp
, args
);
628 if (retval
== -ENOSPC
) {
630 * Promote the attribute list to the Btree format, then
631 * Commit that transaction so that the node_addname() call
632 * can manage its own transactions.
634 xfs_defer_init(args
->dfops
, args
->firstblock
);
635 error
= xfs_attr3_leaf_to_node(args
);
637 goto out_defer_cancel
;
638 xfs_defer_ijoin(args
->dfops
, dp
);
639 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
641 goto out_defer_cancel
;
644 * Commit the current trans (including the inode) and start
647 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
652 * Fob the whole rest of the problem off on the Btree code.
654 error
= xfs_attr_node_addname(args
);
659 * Commit the transaction that added the attr name so that
660 * later routines can manage their own transactions.
662 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
667 * If there was an out-of-line value, allocate the blocks we
668 * identified for its storage and copy the value. This is done
669 * after we create the attribute so that we don't overflow the
670 * maximum size of a transaction and/or hit a deadlock.
672 if (args
->rmtblkno
> 0) {
673 error
= xfs_attr_rmtval_set(args
);
679 * If this is an atomic rename operation, we must "flip" the
680 * incomplete flags on the "new" and "old" attribute/value pairs
681 * so that one disappears and one appears atomically. Then we
682 * must remove the "old" attribute/value pair.
684 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
686 * In a separate transaction, set the incomplete flag on the
687 * "old" attr and clear the incomplete flag on the "new" attr.
689 error
= xfs_attr3_leaf_flipflags(args
);
694 * Dismantle the "old" attribute/value pair by removing
695 * a "remote" value (if it exists).
697 args
->index
= args
->index2
;
698 args
->blkno
= args
->blkno2
;
699 args
->rmtblkno
= args
->rmtblkno2
;
700 args
->rmtblkcnt
= args
->rmtblkcnt2
;
701 args
->rmtvaluelen
= args
->rmtvaluelen2
;
702 if (args
->rmtblkno
) {
703 error
= xfs_attr_rmtval_remove(args
);
709 * Read in the block containing the "old" attr, then
710 * remove the "old" attr from that block (neat, huh!)
712 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
,
717 xfs_attr3_leaf_remove(bp
, args
);
720 * If the result is small enough, shrink it all into the inode.
722 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
723 xfs_defer_init(args
->dfops
, args
->firstblock
);
724 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
725 /* bp is gone due to xfs_da_shrink_inode */
727 goto out_defer_cancel
;
728 xfs_defer_ijoin(args
->dfops
, dp
);
729 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
731 goto out_defer_cancel
;
735 * Commit the remove and start the next trans in series.
737 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
739 } else if (args
->rmtblkno
> 0) {
741 * Added a "remote" value, just clear the incomplete flag.
743 error
= xfs_attr3_leaf_clearflag(args
);
747 xfs_defer_cancel(args
->dfops
);
753 * Remove a name from the leaf attribute list structure
755 * This leaf block cannot have a "remote" value, we only call this routine
756 * if bmap_one_block() says there is only one block (ie: no remote blks).
759 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
765 trace_xfs_attr_leaf_removename(args
);
768 * Remove the attribute.
772 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
776 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
777 if (error
== -ENOATTR
) {
778 xfs_trans_brelse(args
->trans
, bp
);
782 xfs_attr3_leaf_remove(bp
, args
);
785 * If the result is small enough, shrink it all into the inode.
787 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
788 xfs_defer_init(args
->dfops
, args
->firstblock
);
789 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
790 /* bp is gone due to xfs_da_shrink_inode */
792 goto out_defer_cancel
;
793 xfs_defer_ijoin(args
->dfops
, dp
);
794 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
796 goto out_defer_cancel
;
800 xfs_defer_cancel(args
->dfops
);
806 * Look up a name in a leaf attribute list structure.
808 * This leaf block cannot have a "remote" value, we only call this routine
809 * if bmap_one_block() says there is only one block (ie: no remote blks).
812 xfs_attr_leaf_get(xfs_da_args_t
*args
)
817 trace_xfs_attr_leaf_get(args
);
820 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
824 error
= xfs_attr3_leaf_lookup_int(bp
, args
);
825 if (error
!= -EEXIST
) {
826 xfs_trans_brelse(args
->trans
, bp
);
829 error
= xfs_attr3_leaf_getvalue(bp
, args
);
830 xfs_trans_brelse(args
->trans
, bp
);
831 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
832 error
= xfs_attr_rmtval_get(args
);
837 /*========================================================================
838 * External routines when attribute list size > geo->blksize
839 *========================================================================*/
842 * Add a name to a Btree-format attribute list.
844 * This will involve walking down the Btree, and may involve splitting
845 * leaf nodes and even splitting intermediate nodes up to and including
846 * the root node (a special case of an intermediate node).
848 * "Remote" attribute values confuse the issue and atomic rename operations
849 * add a whole extra layer of confusion on top of that.
852 xfs_attr_node_addname(xfs_da_args_t
*args
)
854 xfs_da_state_t
*state
;
855 xfs_da_state_blk_t
*blk
;
860 trace_xfs_attr_node_addname(args
);
863 * Fill in bucket of arguments/results/context to carry around.
868 state
= xfs_da_state_alloc();
873 * Search to see if name already exists, and get back a pointer
874 * to where it should go.
876 error
= xfs_da3_node_lookup_int(state
, &retval
);
879 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
880 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
881 if ((args
->flags
& ATTR_REPLACE
) && (retval
== -ENOATTR
)) {
883 } else if (retval
== -EEXIST
) {
884 if (args
->flags
& ATTR_CREATE
)
887 trace_xfs_attr_node_replace(args
);
889 /* save the attribute state for later removal*/
890 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
891 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
892 args
->index2
= args
->index
;
893 args
->rmtblkno2
= args
->rmtblkno
;
894 args
->rmtblkcnt2
= args
->rmtblkcnt
;
895 args
->rmtvaluelen2
= args
->rmtvaluelen
;
898 * clear the remote attr state now that it is saved so that the
899 * values reflect the state of the attribute we are about to
900 * add, not the attribute we just found and will remove later.
904 args
->rmtvaluelen
= 0;
907 retval
= xfs_attr3_leaf_add(blk
->bp
, state
->args
);
908 if (retval
== -ENOSPC
) {
909 if (state
->path
.active
== 1) {
911 * Its really a single leaf node, but it had
912 * out-of-line values so it looked like it *might*
913 * have been a b-tree.
915 xfs_da_state_free(state
);
917 xfs_defer_init(args
->dfops
, args
->firstblock
);
918 error
= xfs_attr3_leaf_to_node(args
);
920 goto out_defer_cancel
;
921 xfs_defer_ijoin(args
->dfops
, dp
);
922 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
924 goto out_defer_cancel
;
927 * Commit the node conversion and start the next
928 * trans in the chain.
930 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
938 * Split as many Btree elements as required.
939 * This code tracks the new and old attr's location
940 * in the index/blkno/rmtblkno/rmtblkcnt fields and
941 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
943 xfs_defer_init(args
->dfops
, args
->firstblock
);
944 error
= xfs_da3_split(state
);
946 goto out_defer_cancel
;
947 xfs_defer_ijoin(args
->dfops
, dp
);
948 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
950 goto out_defer_cancel
;
953 * Addition succeeded, update Btree hashvals.
955 xfs_da3_fixhashpath(state
, &state
->path
);
959 * Kill the state structure, we're done with it and need to
960 * allow the buffers to come back later.
962 xfs_da_state_free(state
);
966 * Commit the leaf addition or btree split and start the next
967 * trans in the chain.
969 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
974 * If there was an out-of-line value, allocate the blocks we
975 * identified for its storage and copy the value. This is done
976 * after we create the attribute so that we don't overflow the
977 * maximum size of a transaction and/or hit a deadlock.
979 if (args
->rmtblkno
> 0) {
980 error
= xfs_attr_rmtval_set(args
);
986 * If this is an atomic rename operation, we must "flip" the
987 * incomplete flags on the "new" and "old" attribute/value pairs
988 * so that one disappears and one appears atomically. Then we
989 * must remove the "old" attribute/value pair.
991 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
993 * In a separate transaction, set the incomplete flag on the
994 * "old" attr and clear the incomplete flag on the "new" attr.
996 error
= xfs_attr3_leaf_flipflags(args
);
1001 * Dismantle the "old" attribute/value pair by removing
1002 * a "remote" value (if it exists).
1004 args
->index
= args
->index2
;
1005 args
->blkno
= args
->blkno2
;
1006 args
->rmtblkno
= args
->rmtblkno2
;
1007 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1008 args
->rmtvaluelen
= args
->rmtvaluelen2
;
1009 if (args
->rmtblkno
) {
1010 error
= xfs_attr_rmtval_remove(args
);
1016 * Re-find the "old" attribute entry after any split ops.
1017 * The INCOMPLETE flag means that we will find the "old"
1018 * attr, not the "new" one.
1020 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1021 state
= xfs_da_state_alloc();
1025 error
= xfs_da3_node_lookup_int(state
, &retval
);
1030 * Remove the name and update the hashvals in the tree.
1032 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1033 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1034 error
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1035 xfs_da3_fixhashpath(state
, &state
->path
);
1038 * Check to see if the tree needs to be collapsed.
1040 if (retval
&& (state
->path
.active
> 1)) {
1041 xfs_defer_init(args
->dfops
, args
->firstblock
);
1042 error
= xfs_da3_join(state
);
1044 goto out_defer_cancel
;
1045 xfs_defer_ijoin(args
->dfops
, dp
);
1046 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
1048 goto out_defer_cancel
;
1052 * Commit and start the next trans in the chain.
1054 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1058 } else if (args
->rmtblkno
> 0) {
1060 * Added a "remote" value, just clear the incomplete flag.
1062 error
= xfs_attr3_leaf_clearflag(args
);
1070 xfs_da_state_free(state
);
1075 xfs_defer_cancel(args
->dfops
);
1081 * Remove a name from a B-tree attribute list.
1083 * This will involve walking down the Btree, and may involve joining
1084 * leaf nodes and even joining intermediate nodes up to and including
1085 * the root node (a special case of an intermediate node).
1088 xfs_attr_node_removename(xfs_da_args_t
*args
)
1090 xfs_da_state_t
*state
;
1091 xfs_da_state_blk_t
*blk
;
1094 int retval
, error
, forkoff
;
1096 trace_xfs_attr_node_removename(args
);
1099 * Tie a string around our finger to remind us where we are.
1102 state
= xfs_da_state_alloc();
1104 state
->mp
= dp
->i_mount
;
1107 * Search to see if name exists, and get back a pointer to it.
1109 error
= xfs_da3_node_lookup_int(state
, &retval
);
1110 if (error
|| (retval
!= -EEXIST
)) {
1117 * If there is an out-of-line value, de-allocate the blocks.
1118 * This is done before we remove the attribute so that we don't
1119 * overflow the maximum size of a transaction and/or hit a deadlock.
1121 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1122 ASSERT(blk
->bp
!= NULL
);
1123 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1124 if (args
->rmtblkno
> 0) {
1126 * Fill in disk block numbers in the state structure
1127 * so that we can get the buffers back after we commit
1128 * several transactions in the following calls.
1130 error
= xfs_attr_fillstate(state
);
1135 * Mark the attribute as INCOMPLETE, then bunmapi() the
1138 error
= xfs_attr3_leaf_setflag(args
);
1141 error
= xfs_attr_rmtval_remove(args
);
1146 * Refill the state structure with buffers, the prior calls
1147 * released our buffers.
1149 error
= xfs_attr_refillstate(state
);
1155 * Remove the name and update the hashvals in the tree.
1157 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1158 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1159 retval
= xfs_attr3_leaf_remove(blk
->bp
, args
);
1160 xfs_da3_fixhashpath(state
, &state
->path
);
1163 * Check to see if the tree needs to be collapsed.
1165 if (retval
&& (state
->path
.active
> 1)) {
1166 xfs_defer_init(args
->dfops
, args
->firstblock
);
1167 error
= xfs_da3_join(state
);
1169 goto out_defer_cancel
;
1170 xfs_defer_ijoin(args
->dfops
, dp
);
1171 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
1173 goto out_defer_cancel
;
1175 * Commit the Btree join operation and start a new trans.
1177 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
1183 * If the result is small enough, push it all into the inode.
1185 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1187 * Have to get rid of the copy of this dabuf in the state.
1189 ASSERT(state
->path
.active
== 1);
1190 ASSERT(state
->path
.blk
[0].bp
);
1191 state
->path
.blk
[0].bp
= NULL
;
1193 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, 0, -1, &bp
);
1197 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1198 xfs_defer_init(args
->dfops
, args
->firstblock
);
1199 error
= xfs_attr3_leaf_to_shortform(bp
, args
, forkoff
);
1200 /* bp is gone due to xfs_da_shrink_inode */
1202 goto out_defer_cancel
;
1203 xfs_defer_ijoin(args
->dfops
, dp
);
1204 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
1206 goto out_defer_cancel
;
1208 xfs_trans_brelse(args
->trans
, bp
);
1213 xfs_da_state_free(state
);
1216 xfs_defer_cancel(args
->dfops
);
1222 * Fill in the disk block numbers in the state structure for the buffers
1223 * that are attached to the state structure.
1224 * This is done so that we can quickly reattach ourselves to those buffers
1225 * after some set of transaction commits have released these buffers.
1228 xfs_attr_fillstate(xfs_da_state_t
*state
)
1230 xfs_da_state_path_t
*path
;
1231 xfs_da_state_blk_t
*blk
;
1234 trace_xfs_attr_fillstate(state
->args
);
1237 * Roll down the "path" in the state structure, storing the on-disk
1238 * block number for those buffers in the "path".
1240 path
= &state
->path
;
1241 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1242 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1244 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1247 blk
->disk_blkno
= 0;
1252 * Roll down the "altpath" in the state structure, storing the on-disk
1253 * block number for those buffers in the "altpath".
1255 path
= &state
->altpath
;
1256 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1257 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1259 blk
->disk_blkno
= XFS_BUF_ADDR(blk
->bp
);
1262 blk
->disk_blkno
= 0;
1270 * Reattach the buffers to the state structure based on the disk block
1271 * numbers stored in the state structure.
1272 * This is done after some set of transaction commits have released those
1273 * buffers from our grip.
1276 xfs_attr_refillstate(xfs_da_state_t
*state
)
1278 xfs_da_state_path_t
*path
;
1279 xfs_da_state_blk_t
*blk
;
1282 trace_xfs_attr_refillstate(state
->args
);
1285 * Roll down the "path" in the state structure, storing the on-disk
1286 * block number for those buffers in the "path".
1288 path
= &state
->path
;
1289 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1290 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1291 if (blk
->disk_blkno
) {
1292 error
= xfs_da3_node_read(state
->args
->trans
,
1294 blk
->blkno
, blk
->disk_blkno
,
1295 &blk
->bp
, XFS_ATTR_FORK
);
1304 * Roll down the "altpath" in the state structure, storing the on-disk
1305 * block number for those buffers in the "altpath".
1307 path
= &state
->altpath
;
1308 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1309 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1310 if (blk
->disk_blkno
) {
1311 error
= xfs_da3_node_read(state
->args
->trans
,
1313 blk
->blkno
, blk
->disk_blkno
,
1314 &blk
->bp
, XFS_ATTR_FORK
);
1326 * Look up a filename in a node attribute list.
1328 * This routine gets called for any attribute fork that has more than one
1329 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1330 * "remote" values taking up more blocks.
1333 xfs_attr_node_get(xfs_da_args_t
*args
)
1335 xfs_da_state_t
*state
;
1336 xfs_da_state_blk_t
*blk
;
1340 trace_xfs_attr_node_get(args
);
1342 state
= xfs_da_state_alloc();
1344 state
->mp
= args
->dp
->i_mount
;
1347 * Search to see if name exists, and get back a pointer to it.
1349 error
= xfs_da3_node_lookup_int(state
, &retval
);
1352 } else if (retval
== -EEXIST
) {
1353 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1354 ASSERT(blk
->bp
!= NULL
);
1355 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1358 * Get the value, local or "remote"
1360 retval
= xfs_attr3_leaf_getvalue(blk
->bp
, args
);
1361 if (!retval
&& (args
->rmtblkno
> 0)
1362 && !(args
->flags
& ATTR_KERNOVAL
)) {
1363 retval
= xfs_attr_rmtval_get(args
);
1368 * If not in a transaction, we have to release all the buffers.
1370 for (i
= 0; i
< state
->path
.active
; i
++) {
1371 xfs_trans_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1372 state
->path
.blk
[i
].bp
= NULL
;
1375 xfs_da_state_free(state
);