1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_da_format.h"
16 #include "xfs_inode.h"
17 #include "xfs_trans.h"
18 #include "xfs_bmap_btree.h"
20 #include "xfs_attr_sf.h"
21 #include "xfs_attr_remote.h"
23 #include "xfs_attr_leaf.h"
24 #include "xfs_trace.h"
31 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
34 /*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
39 * Routines used for growing the Btree.
41 STATIC
int xfs_attr3_leaf_create(struct xfs_da_args
*args
,
42 xfs_dablk_t which_block
, struct xfs_buf
**bpp
);
43 STATIC
int xfs_attr3_leaf_add_work(struct xfs_buf
*leaf_buffer
,
44 struct xfs_attr3_icleaf_hdr
*ichdr
,
45 struct xfs_da_args
*args
, int freemap_index
);
46 STATIC
void xfs_attr3_leaf_compact(struct xfs_da_args
*args
,
47 struct xfs_attr3_icleaf_hdr
*ichdr
,
48 struct xfs_buf
*leaf_buffer
);
49 STATIC
void xfs_attr3_leaf_rebalance(xfs_da_state_t
*state
,
50 xfs_da_state_blk_t
*blk1
,
51 xfs_da_state_blk_t
*blk2
);
52 STATIC
int xfs_attr3_leaf_figure_balance(xfs_da_state_t
*state
,
53 xfs_da_state_blk_t
*leaf_blk_1
,
54 struct xfs_attr3_icleaf_hdr
*ichdr1
,
55 xfs_da_state_blk_t
*leaf_blk_2
,
56 struct xfs_attr3_icleaf_hdr
*ichdr2
,
57 int *number_entries_in_blk1
,
58 int *number_usedbytes_in_blk1
);
63 STATIC
void xfs_attr3_leaf_moveents(struct xfs_da_args
*args
,
64 struct xfs_attr_leafblock
*src_leaf
,
65 struct xfs_attr3_icleaf_hdr
*src_ichdr
, int src_start
,
66 struct xfs_attr_leafblock
*dst_leaf
,
67 struct xfs_attr3_icleaf_hdr
*dst_ichdr
, int dst_start
,
69 STATIC
int xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
);
72 * attr3 block 'firstused' conversion helpers.
74 * firstused refers to the offset of the first used byte of the nameval region
75 * of an attr leaf block. The region starts at the tail of the block and expands
76 * backwards towards the middle. As such, firstused is initialized to the block
77 * size for an empty leaf block and is reduced from there.
79 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
80 * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
81 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
82 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
83 * the attr block size. The following helpers manage the conversion between the
84 * in-core and on-disk formats.
88 xfs_attr3_leaf_firstused_from_disk(
89 struct xfs_da_geometry
*geo
,
90 struct xfs_attr3_icleaf_hdr
*to
,
91 struct xfs_attr_leafblock
*from
)
93 struct xfs_attr3_leaf_hdr
*hdr3
;
95 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
96 hdr3
= (struct xfs_attr3_leaf_hdr
*) from
;
97 to
->firstused
= be16_to_cpu(hdr3
->firstused
);
99 to
->firstused
= be16_to_cpu(from
->hdr
.firstused
);
103 * Convert from the magic fsb size value to actual blocksize. This
104 * should only occur for empty blocks when the block size overflows
107 if (to
->firstused
== XFS_ATTR3_LEAF_NULLOFF
) {
108 ASSERT(!to
->count
&& !to
->usedbytes
);
109 ASSERT(geo
->blksize
> USHRT_MAX
);
110 to
->firstused
= geo
->blksize
;
115 xfs_attr3_leaf_firstused_to_disk(
116 struct xfs_da_geometry
*geo
,
117 struct xfs_attr_leafblock
*to
,
118 struct xfs_attr3_icleaf_hdr
*from
)
120 struct xfs_attr3_leaf_hdr
*hdr3
;
123 /* magic value should only be seen on disk */
124 ASSERT(from
->firstused
!= XFS_ATTR3_LEAF_NULLOFF
);
127 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
128 * value. This only overflows at the max supported value of 64k. Use the
129 * magic on-disk value to represent block size in this case.
131 firstused
= from
->firstused
;
132 if (firstused
> USHRT_MAX
) {
133 ASSERT(from
->firstused
== geo
->blksize
);
134 firstused
= XFS_ATTR3_LEAF_NULLOFF
;
137 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
138 hdr3
= (struct xfs_attr3_leaf_hdr
*) to
;
139 hdr3
->firstused
= cpu_to_be16(firstused
);
141 to
->hdr
.firstused
= cpu_to_be16(firstused
);
146 xfs_attr3_leaf_hdr_from_disk(
147 struct xfs_da_geometry
*geo
,
148 struct xfs_attr3_icleaf_hdr
*to
,
149 struct xfs_attr_leafblock
*from
)
153 ASSERT(from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR_LEAF_MAGIC
) ||
154 from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
));
156 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
157 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)from
;
159 to
->forw
= be32_to_cpu(hdr3
->info
.hdr
.forw
);
160 to
->back
= be32_to_cpu(hdr3
->info
.hdr
.back
);
161 to
->magic
= be16_to_cpu(hdr3
->info
.hdr
.magic
);
162 to
->count
= be16_to_cpu(hdr3
->count
);
163 to
->usedbytes
= be16_to_cpu(hdr3
->usedbytes
);
164 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
165 to
->holes
= hdr3
->holes
;
167 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
168 to
->freemap
[i
].base
= be16_to_cpu(hdr3
->freemap
[i
].base
);
169 to
->freemap
[i
].size
= be16_to_cpu(hdr3
->freemap
[i
].size
);
173 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
174 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
175 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
176 to
->count
= be16_to_cpu(from
->hdr
.count
);
177 to
->usedbytes
= be16_to_cpu(from
->hdr
.usedbytes
);
178 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
179 to
->holes
= from
->hdr
.holes
;
181 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
182 to
->freemap
[i
].base
= be16_to_cpu(from
->hdr
.freemap
[i
].base
);
183 to
->freemap
[i
].size
= be16_to_cpu(from
->hdr
.freemap
[i
].size
);
188 xfs_attr3_leaf_hdr_to_disk(
189 struct xfs_da_geometry
*geo
,
190 struct xfs_attr_leafblock
*to
,
191 struct xfs_attr3_icleaf_hdr
*from
)
195 ASSERT(from
->magic
== XFS_ATTR_LEAF_MAGIC
||
196 from
->magic
== XFS_ATTR3_LEAF_MAGIC
);
198 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
199 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)to
;
201 hdr3
->info
.hdr
.forw
= cpu_to_be32(from
->forw
);
202 hdr3
->info
.hdr
.back
= cpu_to_be32(from
->back
);
203 hdr3
->info
.hdr
.magic
= cpu_to_be16(from
->magic
);
204 hdr3
->count
= cpu_to_be16(from
->count
);
205 hdr3
->usedbytes
= cpu_to_be16(from
->usedbytes
);
206 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
207 hdr3
->holes
= from
->holes
;
210 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
211 hdr3
->freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
212 hdr3
->freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
216 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
217 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
218 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
219 to
->hdr
.count
= cpu_to_be16(from
->count
);
220 to
->hdr
.usedbytes
= cpu_to_be16(from
->usedbytes
);
221 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
222 to
->hdr
.holes
= from
->holes
;
225 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
226 to
->hdr
.freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
227 to
->hdr
.freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
231 static xfs_failaddr_t
232 xfs_attr3_leaf_verify(
235 struct xfs_attr3_icleaf_hdr ichdr
;
236 struct xfs_mount
*mp
= bp
->b_mount
;
237 struct xfs_attr_leafblock
*leaf
= bp
->b_addr
;
238 struct xfs_attr_leaf_entry
*entries
;
239 uint32_t end
; /* must be 32bit - see below */
243 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, leaf
);
245 fa
= xfs_da3_blkinfo_verify(bp
, bp
->b_addr
);
250 * In recovery there is a transient state where count == 0 is valid
251 * because we may have transitioned an empty shortform attr to a leaf
252 * if the attr didn't fit in shortform.
254 if (!xfs_log_in_recovery(mp
) && ichdr
.count
== 0)
255 return __this_address
;
258 * firstused is the block offset of the first name info structure.
259 * Make sure it doesn't go off the block or crash into the header.
261 if (ichdr
.firstused
> mp
->m_attr_geo
->blksize
)
262 return __this_address
;
263 if (ichdr
.firstused
< xfs_attr3_leaf_hdr_size(leaf
))
264 return __this_address
;
266 /* Make sure the entries array doesn't crash into the name info. */
267 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
268 if ((char *)&entries
[ichdr
.count
] >
269 (char *)bp
->b_addr
+ ichdr
.firstused
)
270 return __this_address
;
272 /* XXX: need to range check rest of attr header values */
273 /* XXX: hash order check? */
276 * Quickly check the freemap information. Attribute data has to be
277 * aligned to 4-byte boundaries, and likewise for the free space.
279 * Note that for 64k block size filesystems, the freemap entries cannot
280 * overflow as they are only be16 fields. However, when checking end
281 * pointer of the freemap, we have to be careful to detect overflows and
282 * so use uint32_t for those checks.
284 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
285 if (ichdr
.freemap
[i
].base
> mp
->m_attr_geo
->blksize
)
286 return __this_address
;
287 if (ichdr
.freemap
[i
].base
& 0x3)
288 return __this_address
;
289 if (ichdr
.freemap
[i
].size
> mp
->m_attr_geo
->blksize
)
290 return __this_address
;
291 if (ichdr
.freemap
[i
].size
& 0x3)
292 return __this_address
;
294 /* be care of 16 bit overflows here */
295 end
= (uint32_t)ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
;
296 if (end
< ichdr
.freemap
[i
].base
)
297 return __this_address
;
298 if (end
> mp
->m_attr_geo
->blksize
)
299 return __this_address
;
306 xfs_attr3_leaf_write_verify(
309 struct xfs_mount
*mp
= bp
->b_mount
;
310 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
311 struct xfs_attr3_leaf_hdr
*hdr3
= bp
->b_addr
;
314 fa
= xfs_attr3_leaf_verify(bp
);
316 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
320 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
324 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
326 xfs_buf_update_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
);
330 * leaf/node format detection on trees is sketchy, so a node read can be done on
331 * leaf level blocks when detection identifies the tree as a node format tree
332 * incorrectly. In this case, we need to swap the verifier to match the correct
333 * format of the block being read.
336 xfs_attr3_leaf_read_verify(
339 struct xfs_mount
*mp
= bp
->b_mount
;
342 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
343 !xfs_buf_verify_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
))
344 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
346 fa
= xfs_attr3_leaf_verify(bp
);
348 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
352 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops
= {
353 .name
= "xfs_attr3_leaf",
354 .magic16
= { cpu_to_be16(XFS_ATTR_LEAF_MAGIC
),
355 cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
) },
356 .verify_read
= xfs_attr3_leaf_read_verify
,
357 .verify_write
= xfs_attr3_leaf_write_verify
,
358 .verify_struct
= xfs_attr3_leaf_verify
,
363 struct xfs_trans
*tp
,
364 struct xfs_inode
*dp
,
366 xfs_daddr_t mappedbno
,
367 struct xfs_buf
**bpp
)
371 err
= xfs_da_read_buf(tp
, dp
, bno
, mappedbno
, bpp
,
372 XFS_ATTR_FORK
, &xfs_attr3_leaf_buf_ops
);
373 if (!err
&& tp
&& *bpp
)
374 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_ATTR_LEAF_BUF
);
378 /*========================================================================
379 * Namespace helper routines
380 *========================================================================*/
383 * If namespace bits don't match return 0.
384 * If all match then return 1.
387 xfs_attr_namesp_match(int arg_flags
, int ondisk_flags
)
389 return XFS_ATTR_NSP_ONDISK(ondisk_flags
) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags
);
394 struct xfs_da_args
*args
,
395 unsigned char *value
,
399 * No copy if all we have to do is get the length
401 if (args
->flags
& ATTR_KERNOVAL
) {
402 args
->valuelen
= valuelen
;
407 * No copy if the length of the existing buffer is too small
409 if (args
->valuelen
< valuelen
) {
410 args
->valuelen
= valuelen
;
414 if (args
->op_flags
& XFS_DA_OP_ALLOCVAL
) {
415 args
->value
= kmem_alloc_large(valuelen
, 0);
419 args
->valuelen
= valuelen
;
421 /* remote block xattr requires IO for copy-in */
423 return xfs_attr_rmtval_get(args
);
426 * This is to prevent a GCC warning because the remote xattr case
427 * doesn't have a value to pass in. In that case, we never reach here,
428 * but GCC can't work that out and so throws a "passing NULL to
433 memcpy(args
->value
, value
, valuelen
);
437 /*========================================================================
438 * External routines when attribute fork size < XFS_LITINO(mp).
439 *========================================================================*/
442 * Query whether the requested number of additional bytes of extended
443 * attribute space will be able to fit inline.
445 * Returns zero if not, else the di_forkoff fork offset to be used in the
446 * literal area for attribute data once the new bytes have been added.
448 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
449 * special case for dev/uuid inodes, they have fixed size data forks.
452 xfs_attr_shortform_bytesfit(xfs_inode_t
*dp
, int bytes
)
455 int minforkoff
; /* lower limit on valid forkoff locations */
456 int maxforkoff
; /* upper limit on valid forkoff locations */
458 xfs_mount_t
*mp
= dp
->i_mount
;
461 offset
= (XFS_LITINO(mp
, dp
->i_d
.di_version
) - bytes
) >> 3;
463 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_DEV
) {
464 minforkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
465 return (offset
>= minforkoff
) ? minforkoff
: 0;
469 * If the requested numbers of bytes is smaller or equal to the
470 * current attribute fork size we can always proceed.
472 * Note that if_bytes in the data fork might actually be larger than
473 * the current data fork size is due to delalloc extents. In that
474 * case either the extent count will go down when they are converted
475 * to real extents, or the delalloc conversion will take care of the
476 * literal area rebalancing.
478 if (bytes
<= XFS_IFORK_ASIZE(dp
))
479 return dp
->i_d
.di_forkoff
;
482 * For attr2 we can try to move the forkoff if there is space in the
483 * literal area, but for the old format we are done if there is no
484 * space in the fixed attribute fork.
486 if (!(mp
->m_flags
& XFS_MOUNT_ATTR2
))
489 dsize
= dp
->i_df
.if_bytes
;
491 switch (dp
->i_d
.di_format
) {
492 case XFS_DINODE_FMT_EXTENTS
:
494 * If there is no attr fork and the data fork is extents,
495 * determine if creating the default attr fork will result
496 * in the extents form migrating to btree. If so, the
497 * minimum offset only needs to be the space required for
500 if (!dp
->i_d
.di_forkoff
&& dp
->i_df
.if_bytes
>
501 xfs_default_attroffset(dp
))
502 dsize
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
504 case XFS_DINODE_FMT_BTREE
:
506 * If we have a data btree then keep forkoff if we have one,
507 * otherwise we are adding a new attr, so then we set
508 * minforkoff to where the btree root can finish so we have
509 * plenty of room for attrs
511 if (dp
->i_d
.di_forkoff
) {
512 if (offset
< dp
->i_d
.di_forkoff
)
514 return dp
->i_d
.di_forkoff
;
516 dsize
= XFS_BMAP_BROOT_SPACE(mp
, dp
->i_df
.if_broot
);
521 * A data fork btree root must have space for at least
522 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
524 minforkoff
= max(dsize
, XFS_BMDR_SPACE_CALC(MINDBTPTRS
));
525 minforkoff
= roundup(minforkoff
, 8) >> 3;
527 /* attr fork btree root can have at least this many key/ptr pairs */
528 maxforkoff
= XFS_LITINO(mp
, dp
->i_d
.di_version
) -
529 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
530 maxforkoff
= maxforkoff
>> 3; /* rounded down */
532 if (offset
>= maxforkoff
)
534 if (offset
>= minforkoff
)
540 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
543 xfs_sbversion_add_attr2(xfs_mount_t
*mp
, xfs_trans_t
*tp
)
545 if ((mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
546 !(xfs_sb_version_hasattr2(&mp
->m_sb
))) {
547 spin_lock(&mp
->m_sb_lock
);
548 if (!xfs_sb_version_hasattr2(&mp
->m_sb
)) {
549 xfs_sb_version_addattr2(&mp
->m_sb
);
550 spin_unlock(&mp
->m_sb_lock
);
553 spin_unlock(&mp
->m_sb_lock
);
558 * Create the initial contents of a shortform attribute list.
561 xfs_attr_shortform_create(xfs_da_args_t
*args
)
563 xfs_attr_sf_hdr_t
*hdr
;
565 struct xfs_ifork
*ifp
;
567 trace_xfs_attr_sf_create(args
);
573 ASSERT(ifp
->if_bytes
== 0);
574 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) {
575 ifp
->if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
576 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_LOCAL
;
577 ifp
->if_flags
|= XFS_IFINLINE
;
579 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
581 xfs_idata_realloc(dp
, sizeof(*hdr
), XFS_ATTR_FORK
);
582 hdr
= (xfs_attr_sf_hdr_t
*)ifp
->if_u1
.if_data
;
584 hdr
->totsize
= cpu_to_be16(sizeof(*hdr
));
585 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
589 * Add a name/value pair to the shortform attribute list.
590 * Overflow from the inode has already been checked for.
593 xfs_attr_shortform_add(xfs_da_args_t
*args
, int forkoff
)
595 xfs_attr_shortform_t
*sf
;
596 xfs_attr_sf_entry_t
*sfe
;
600 struct xfs_ifork
*ifp
;
602 trace_xfs_attr_sf_add(args
);
606 dp
->i_d
.di_forkoff
= forkoff
;
609 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
610 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
612 for (i
= 0; i
< sf
->hdr
.count
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
614 if (sfe
->namelen
!= args
->namelen
)
616 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
618 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
624 offset
= (char *)sfe
- (char *)sf
;
625 size
= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
626 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
);
627 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
628 sfe
= (xfs_attr_sf_entry_t
*)((char *)sf
+ offset
);
630 sfe
->namelen
= args
->namelen
;
631 sfe
->valuelen
= args
->valuelen
;
632 sfe
->flags
= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
633 memcpy(sfe
->nameval
, args
->name
, args
->namelen
);
634 memcpy(&sfe
->nameval
[args
->namelen
], args
->value
, args
->valuelen
);
636 be16_add_cpu(&sf
->hdr
.totsize
, size
);
637 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
639 xfs_sbversion_add_attr2(mp
, args
->trans
);
643 * After the last attribute is removed revert to original inode format,
644 * making all literal area available to the data fork once more.
647 xfs_attr_fork_remove(
648 struct xfs_inode
*ip
,
649 struct xfs_trans
*tp
)
651 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
652 ip
->i_d
.di_forkoff
= 0;
653 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
655 ASSERT(ip
->i_d
.di_anextents
== 0);
656 ASSERT(ip
->i_afp
== NULL
);
658 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
662 * Remove an attribute from the shortform attribute list structure.
665 xfs_attr_shortform_remove(xfs_da_args_t
*args
)
667 xfs_attr_shortform_t
*sf
;
668 xfs_attr_sf_entry_t
*sfe
;
669 int base
, size
=0, end
, totsize
, i
;
673 trace_xfs_attr_sf_remove(args
);
677 base
= sizeof(xfs_attr_sf_hdr_t
);
678 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
681 for (i
= 0; i
< end
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
),
683 size
= XFS_ATTR_SF_ENTSIZE(sfe
);
684 if (sfe
->namelen
!= args
->namelen
)
686 if (memcmp(sfe
->nameval
, args
->name
, args
->namelen
) != 0)
688 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
696 * Fix up the attribute fork data, covering the hole
699 totsize
= be16_to_cpu(sf
->hdr
.totsize
);
701 memmove(&((char *)sf
)[base
], &((char *)sf
)[end
], totsize
- end
);
703 be16_add_cpu(&sf
->hdr
.totsize
, -size
);
706 * Fix up the start offset of the attribute fork
709 if (totsize
== sizeof(xfs_attr_sf_hdr_t
) &&
710 (mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
711 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
712 !(args
->op_flags
& XFS_DA_OP_ADDNAME
)) {
713 xfs_attr_fork_remove(dp
, args
->trans
);
715 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
716 dp
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(dp
, totsize
);
717 ASSERT(dp
->i_d
.di_forkoff
);
718 ASSERT(totsize
> sizeof(xfs_attr_sf_hdr_t
) ||
719 (args
->op_flags
& XFS_DA_OP_ADDNAME
) ||
720 !(mp
->m_flags
& XFS_MOUNT_ATTR2
) ||
721 dp
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
);
722 xfs_trans_log_inode(args
->trans
, dp
,
723 XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
726 xfs_sbversion_add_attr2(mp
, args
->trans
);
732 * Look up a name in a shortform attribute list structure.
736 xfs_attr_shortform_lookup(xfs_da_args_t
*args
)
738 xfs_attr_shortform_t
*sf
;
739 xfs_attr_sf_entry_t
*sfe
;
741 struct xfs_ifork
*ifp
;
743 trace_xfs_attr_sf_lookup(args
);
745 ifp
= args
->dp
->i_afp
;
746 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
747 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
749 for (i
= 0; i
< sf
->hdr
.count
;
750 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
751 if (sfe
->namelen
!= args
->namelen
)
753 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
755 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
763 * Retreive the attribute value and length.
765 * If ATTR_KERNOVAL is specified, only the length needs to be returned.
766 * Unlike a lookup, we only return an error if the attribute does not
767 * exist or we can't retrieve the value.
770 xfs_attr_shortform_getvalue(
771 struct xfs_da_args
*args
)
773 struct xfs_attr_shortform
*sf
;
774 struct xfs_attr_sf_entry
*sfe
;
777 ASSERT(args
->dp
->i_afp
->if_flags
== XFS_IFINLINE
);
778 sf
= (xfs_attr_shortform_t
*)args
->dp
->i_afp
->if_u1
.if_data
;
780 for (i
= 0; i
< sf
->hdr
.count
;
781 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
782 if (sfe
->namelen
!= args
->namelen
)
784 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
786 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
788 return xfs_attr_copy_value(args
, &sfe
->nameval
[args
->namelen
],
795 * Convert from using the shortform to the leaf. On success, return the
796 * buffer so that we can keep it locked until we're totally done with it.
799 xfs_attr_shortform_to_leaf(
800 struct xfs_da_args
*args
,
801 struct xfs_buf
**leaf_bp
)
803 struct xfs_inode
*dp
;
804 struct xfs_attr_shortform
*sf
;
805 struct xfs_attr_sf_entry
*sfe
;
806 struct xfs_da_args nargs
;
811 struct xfs_ifork
*ifp
;
813 trace_xfs_attr_sf_to_leaf(args
);
817 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
818 size
= be16_to_cpu(sf
->hdr
.totsize
);
819 tmpbuffer
= kmem_alloc(size
, 0);
820 ASSERT(tmpbuffer
!= NULL
);
821 memcpy(tmpbuffer
, ifp
->if_u1
.if_data
, size
);
822 sf
= (xfs_attr_shortform_t
*)tmpbuffer
;
824 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
825 xfs_bmap_local_to_extents_empty(dp
, XFS_ATTR_FORK
);
828 error
= xfs_da_grow_inode(args
, &blkno
);
831 * If we hit an IO error middle of the transaction inside
832 * grow_inode(), we may have inconsistent data. Bail out.
836 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
837 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
842 error
= xfs_attr3_leaf_create(args
, blkno
, &bp
);
844 /* xfs_attr3_leaf_create may not have instantiated a block */
845 if (bp
&& (xfs_da_shrink_inode(args
, 0, bp
) != 0))
847 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
848 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
852 memset((char *)&nargs
, 0, sizeof(nargs
));
854 nargs
.geo
= args
->geo
;
855 nargs
.total
= args
->total
;
856 nargs
.whichfork
= XFS_ATTR_FORK
;
857 nargs
.trans
= args
->trans
;
858 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
861 for (i
= 0; i
< sf
->hdr
.count
; i
++) {
862 nargs
.name
= sfe
->nameval
;
863 nargs
.namelen
= sfe
->namelen
;
864 nargs
.value
= &sfe
->nameval
[nargs
.namelen
];
865 nargs
.valuelen
= sfe
->valuelen
;
866 nargs
.hashval
= xfs_da_hashname(sfe
->nameval
,
868 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe
->flags
);
869 error
= xfs_attr3_leaf_lookup_int(bp
, &nargs
); /* set a->index */
870 ASSERT(error
== -ENOATTR
);
871 error
= xfs_attr3_leaf_add(bp
, &nargs
);
872 ASSERT(error
!= -ENOSPC
);
875 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
880 kmem_free(tmpbuffer
);
885 * Check a leaf attribute block to see if all the entries would fit into
886 * a shortform attribute list.
889 xfs_attr_shortform_allfit(
891 struct xfs_inode
*dp
)
893 struct xfs_attr_leafblock
*leaf
;
894 struct xfs_attr_leaf_entry
*entry
;
895 xfs_attr_leaf_name_local_t
*name_loc
;
896 struct xfs_attr3_icleaf_hdr leafhdr
;
899 struct xfs_mount
*mp
= bp
->b_mount
;
902 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &leafhdr
, leaf
);
903 entry
= xfs_attr3_leaf_entryp(leaf
);
905 bytes
= sizeof(struct xfs_attr_sf_hdr
);
906 for (i
= 0; i
< leafhdr
.count
; entry
++, i
++) {
907 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
908 continue; /* don't copy partial entries */
909 if (!(entry
->flags
& XFS_ATTR_LOCAL
))
911 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
912 if (name_loc
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
914 if (be16_to_cpu(name_loc
->valuelen
) >= XFS_ATTR_SF_ENTSIZE_MAX
)
916 bytes
+= sizeof(struct xfs_attr_sf_entry
) - 1
918 + be16_to_cpu(name_loc
->valuelen
);
920 if ((dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) &&
921 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
922 (bytes
== sizeof(struct xfs_attr_sf_hdr
)))
924 return xfs_attr_shortform_bytesfit(dp
, bytes
);
927 /* Verify the consistency of an inline attribute fork. */
929 xfs_attr_shortform_verify(
930 struct xfs_inode
*ip
)
932 struct xfs_attr_shortform
*sfp
;
933 struct xfs_attr_sf_entry
*sfep
;
934 struct xfs_attr_sf_entry
*next_sfep
;
936 struct xfs_ifork
*ifp
;
940 ASSERT(ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
);
941 ifp
= XFS_IFORK_PTR(ip
, XFS_ATTR_FORK
);
942 sfp
= (struct xfs_attr_shortform
*)ifp
->if_u1
.if_data
;
943 size
= ifp
->if_bytes
;
946 * Give up if the attribute is way too short.
948 if (size
< sizeof(struct xfs_attr_sf_hdr
))
949 return __this_address
;
951 endp
= (char *)sfp
+ size
;
953 /* Check all reported entries */
954 sfep
= &sfp
->list
[0];
955 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
957 * struct xfs_attr_sf_entry has a variable length.
958 * Check the fixed-offset parts of the structure are
959 * within the data buffer.
961 if (((char *)sfep
+ sizeof(*sfep
)) >= endp
)
962 return __this_address
;
964 /* Don't allow names with known bad length. */
965 if (sfep
->namelen
== 0)
966 return __this_address
;
969 * Check that the variable-length part of the structure is
970 * within the data buffer. The next entry starts after the
971 * name component, so nextentry is an acceptable test.
973 next_sfep
= XFS_ATTR_SF_NEXTENTRY(sfep
);
974 if ((char *)next_sfep
> endp
)
975 return __this_address
;
978 * Check for unknown flags. Short form doesn't support
979 * the incomplete or local bits, so we can use the namespace
982 if (sfep
->flags
& ~XFS_ATTR_NSP_ONDISK_MASK
)
983 return __this_address
;
986 * Check for invalid namespace combinations. We only allow
987 * one namespace flag per xattr, so we can just count the
988 * bits (i.e. hweight) here.
990 if (hweight8(sfep
->flags
& XFS_ATTR_NSP_ONDISK_MASK
) > 1)
991 return __this_address
;
995 if ((void *)sfep
!= (void *)endp
)
996 return __this_address
;
1002 * Convert a leaf attribute list to shortform attribute list
1005 xfs_attr3_leaf_to_shortform(
1007 struct xfs_da_args
*args
,
1010 struct xfs_attr_leafblock
*leaf
;
1011 struct xfs_attr3_icleaf_hdr ichdr
;
1012 struct xfs_attr_leaf_entry
*entry
;
1013 struct xfs_attr_leaf_name_local
*name_loc
;
1014 struct xfs_da_args nargs
;
1015 struct xfs_inode
*dp
= args
->dp
;
1020 trace_xfs_attr_leaf_to_sf(args
);
1022 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, 0);
1026 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1028 leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1029 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1030 entry
= xfs_attr3_leaf_entryp(leaf
);
1032 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1033 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1036 * Clean out the prior contents of the attribute list.
1038 error
= xfs_da_shrink_inode(args
, 0, bp
);
1042 if (forkoff
== -1) {
1043 ASSERT(dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
);
1044 ASSERT(dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
);
1045 xfs_attr_fork_remove(dp
, args
->trans
);
1049 xfs_attr_shortform_create(args
);
1052 * Copy the attributes
1054 memset((char *)&nargs
, 0, sizeof(nargs
));
1055 nargs
.geo
= args
->geo
;
1057 nargs
.total
= args
->total
;
1058 nargs
.whichfork
= XFS_ATTR_FORK
;
1059 nargs
.trans
= args
->trans
;
1060 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
1062 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
1063 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
1064 continue; /* don't copy partial entries */
1065 if (!entry
->nameidx
)
1067 ASSERT(entry
->flags
& XFS_ATTR_LOCAL
);
1068 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
1069 nargs
.name
= name_loc
->nameval
;
1070 nargs
.namelen
= name_loc
->namelen
;
1071 nargs
.value
= &name_loc
->nameval
[nargs
.namelen
];
1072 nargs
.valuelen
= be16_to_cpu(name_loc
->valuelen
);
1073 nargs
.hashval
= be32_to_cpu(entry
->hashval
);
1074 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(entry
->flags
);
1075 xfs_attr_shortform_add(&nargs
, forkoff
);
1080 kmem_free(tmpbuffer
);
1085 * Convert from using a single leaf to a root node and a leaf.
1088 xfs_attr3_leaf_to_node(
1089 struct xfs_da_args
*args
)
1091 struct xfs_attr_leafblock
*leaf
;
1092 struct xfs_attr3_icleaf_hdr icleafhdr
;
1093 struct xfs_attr_leaf_entry
*entries
;
1094 struct xfs_da_node_entry
*btree
;
1095 struct xfs_da3_icnode_hdr icnodehdr
;
1096 struct xfs_da_intnode
*node
;
1097 struct xfs_inode
*dp
= args
->dp
;
1098 struct xfs_mount
*mp
= dp
->i_mount
;
1099 struct xfs_buf
*bp1
= NULL
;
1100 struct xfs_buf
*bp2
= NULL
;
1104 trace_xfs_attr_leaf_to_node(args
);
1106 error
= xfs_da_grow_inode(args
, &blkno
);
1109 error
= xfs_attr3_leaf_read(args
->trans
, dp
, 0, -1, &bp1
);
1113 error
= xfs_da_get_buf(args
->trans
, dp
, blkno
, -1, &bp2
, XFS_ATTR_FORK
);
1117 /* copy leaf to new buffer, update identifiers */
1118 xfs_trans_buf_set_type(args
->trans
, bp2
, XFS_BLFT_ATTR_LEAF_BUF
);
1119 bp2
->b_ops
= bp1
->b_ops
;
1120 memcpy(bp2
->b_addr
, bp1
->b_addr
, args
->geo
->blksize
);
1121 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1122 struct xfs_da3_blkinfo
*hdr3
= bp2
->b_addr
;
1123 hdr3
->blkno
= cpu_to_be64(bp2
->b_bn
);
1125 xfs_trans_log_buf(args
->trans
, bp2
, 0, args
->geo
->blksize
- 1);
1128 * Set up the new root node.
1130 error
= xfs_da3_node_create(args
, 0, 1, &bp1
, XFS_ATTR_FORK
);
1134 dp
->d_ops
->node_hdr_from_disk(&icnodehdr
, node
);
1135 btree
= dp
->d_ops
->node_tree_p(node
);
1138 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &icleafhdr
, leaf
);
1139 entries
= xfs_attr3_leaf_entryp(leaf
);
1141 /* both on-disk, don't endian-flip twice */
1142 btree
[0].hashval
= entries
[icleafhdr
.count
- 1].hashval
;
1143 btree
[0].before
= cpu_to_be32(blkno
);
1144 icnodehdr
.count
= 1;
1145 dp
->d_ops
->node_hdr_to_disk(node
, &icnodehdr
);
1146 xfs_trans_log_buf(args
->trans
, bp1
, 0, args
->geo
->blksize
- 1);
1152 /*========================================================================
1153 * Routines used for growing the Btree.
1154 *========================================================================*/
1157 * Create the initial contents of a leaf attribute list
1158 * or a leaf in a node attribute list.
1161 xfs_attr3_leaf_create(
1162 struct xfs_da_args
*args
,
1164 struct xfs_buf
**bpp
)
1166 struct xfs_attr_leafblock
*leaf
;
1167 struct xfs_attr3_icleaf_hdr ichdr
;
1168 struct xfs_inode
*dp
= args
->dp
;
1169 struct xfs_mount
*mp
= dp
->i_mount
;
1173 trace_xfs_attr_leaf_create(args
);
1175 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp
,
1179 bp
->b_ops
= &xfs_attr3_leaf_buf_ops
;
1180 xfs_trans_buf_set_type(args
->trans
, bp
, XFS_BLFT_ATTR_LEAF_BUF
);
1182 memset(leaf
, 0, args
->geo
->blksize
);
1184 memset(&ichdr
, 0, sizeof(ichdr
));
1185 ichdr
.firstused
= args
->geo
->blksize
;
1187 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1188 struct xfs_da3_blkinfo
*hdr3
= bp
->b_addr
;
1190 ichdr
.magic
= XFS_ATTR3_LEAF_MAGIC
;
1192 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
1193 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
1194 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
1196 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr3_leaf_hdr
);
1198 ichdr
.magic
= XFS_ATTR_LEAF_MAGIC
;
1199 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr_leaf_hdr
);
1201 ichdr
.freemap
[0].size
= ichdr
.firstused
- ichdr
.freemap
[0].base
;
1203 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1204 xfs_trans_log_buf(args
->trans
, bp
, 0, args
->geo
->blksize
- 1);
1211 * Split the leaf node, rebalance, then add the new entry.
1214 xfs_attr3_leaf_split(
1215 struct xfs_da_state
*state
,
1216 struct xfs_da_state_blk
*oldblk
,
1217 struct xfs_da_state_blk
*newblk
)
1222 trace_xfs_attr_leaf_split(state
->args
);
1225 * Allocate space for a new leaf node.
1227 ASSERT(oldblk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1228 error
= xfs_da_grow_inode(state
->args
, &blkno
);
1231 error
= xfs_attr3_leaf_create(state
->args
, blkno
, &newblk
->bp
);
1234 newblk
->blkno
= blkno
;
1235 newblk
->magic
= XFS_ATTR_LEAF_MAGIC
;
1238 * Rebalance the entries across the two leaves.
1239 * NOTE: rebalance() currently depends on the 2nd block being empty.
1241 xfs_attr3_leaf_rebalance(state
, oldblk
, newblk
);
1242 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1247 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1248 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1249 * "new" attrs info. Will need the "old" info to remove it later.
1251 * Insert the "new" entry in the correct block.
1253 if (state
->inleaf
) {
1254 trace_xfs_attr_leaf_add_old(state
->args
);
1255 error
= xfs_attr3_leaf_add(oldblk
->bp
, state
->args
);
1257 trace_xfs_attr_leaf_add_new(state
->args
);
1258 error
= xfs_attr3_leaf_add(newblk
->bp
, state
->args
);
1262 * Update last hashval in each block since we added the name.
1264 oldblk
->hashval
= xfs_attr_leaf_lasthash(oldblk
->bp
, NULL
);
1265 newblk
->hashval
= xfs_attr_leaf_lasthash(newblk
->bp
, NULL
);
1270 * Add a name to the leaf attribute list structure.
1275 struct xfs_da_args
*args
)
1277 struct xfs_attr_leafblock
*leaf
;
1278 struct xfs_attr3_icleaf_hdr ichdr
;
1285 trace_xfs_attr_leaf_add(args
);
1288 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1289 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
.count
);
1290 entsize
= xfs_attr_leaf_newentsize(args
, NULL
);
1293 * Search through freemap for first-fit on new name length.
1294 * (may need to figure in size of entry struct too)
1296 tablesize
= (ichdr
.count
+ 1) * sizeof(xfs_attr_leaf_entry_t
)
1297 + xfs_attr3_leaf_hdr_size(leaf
);
1298 for (sum
= 0, i
= XFS_ATTR_LEAF_MAPSIZE
- 1; i
>= 0; i
--) {
1299 if (tablesize
> ichdr
.firstused
) {
1300 sum
+= ichdr
.freemap
[i
].size
;
1303 if (!ichdr
.freemap
[i
].size
)
1304 continue; /* no space in this map */
1306 if (ichdr
.freemap
[i
].base
< ichdr
.firstused
)
1307 tmp
+= sizeof(xfs_attr_leaf_entry_t
);
1308 if (ichdr
.freemap
[i
].size
>= tmp
) {
1309 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, i
);
1312 sum
+= ichdr
.freemap
[i
].size
;
1316 * If there are no holes in the address space of the block,
1317 * and we don't have enough freespace, then compaction will do us
1318 * no good and we should just give up.
1320 if (!ichdr
.holes
&& sum
< entsize
)
1324 * Compact the entries to coalesce free space.
1325 * This may change the hdr->count via dropping INCOMPLETE entries.
1327 xfs_attr3_leaf_compact(args
, &ichdr
, bp
);
1330 * After compaction, the block is guaranteed to have only one
1331 * free region, in freemap[0]. If it is not big enough, give up.
1333 if (ichdr
.freemap
[0].size
< (entsize
+ sizeof(xfs_attr_leaf_entry_t
))) {
1338 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, 0);
1341 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1342 xfs_trans_log_buf(args
->trans
, bp
,
1343 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
1344 xfs_attr3_leaf_hdr_size(leaf
)));
1349 * Add a name to a leaf attribute list structure.
1352 xfs_attr3_leaf_add_work(
1354 struct xfs_attr3_icleaf_hdr
*ichdr
,
1355 struct xfs_da_args
*args
,
1358 struct xfs_attr_leafblock
*leaf
;
1359 struct xfs_attr_leaf_entry
*entry
;
1360 struct xfs_attr_leaf_name_local
*name_loc
;
1361 struct xfs_attr_leaf_name_remote
*name_rmt
;
1362 struct xfs_mount
*mp
;
1366 trace_xfs_attr_leaf_add_work(args
);
1369 ASSERT(mapindex
>= 0 && mapindex
< XFS_ATTR_LEAF_MAPSIZE
);
1370 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
->count
);
1373 * Force open some space in the entry array and fill it in.
1375 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
1376 if (args
->index
< ichdr
->count
) {
1377 tmp
= ichdr
->count
- args
->index
;
1378 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
1379 memmove(entry
+ 1, entry
, tmp
);
1380 xfs_trans_log_buf(args
->trans
, bp
,
1381 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1386 * Allocate space for the new string (at the end of the run).
1388 mp
= args
->trans
->t_mountp
;
1389 ASSERT(ichdr
->freemap
[mapindex
].base
< args
->geo
->blksize
);
1390 ASSERT((ichdr
->freemap
[mapindex
].base
& 0x3) == 0);
1391 ASSERT(ichdr
->freemap
[mapindex
].size
>=
1392 xfs_attr_leaf_newentsize(args
, NULL
));
1393 ASSERT(ichdr
->freemap
[mapindex
].size
< args
->geo
->blksize
);
1394 ASSERT((ichdr
->freemap
[mapindex
].size
& 0x3) == 0);
1396 ichdr
->freemap
[mapindex
].size
-= xfs_attr_leaf_newentsize(args
, &tmp
);
1398 entry
->nameidx
= cpu_to_be16(ichdr
->freemap
[mapindex
].base
+
1399 ichdr
->freemap
[mapindex
].size
);
1400 entry
->hashval
= cpu_to_be32(args
->hashval
);
1401 entry
->flags
= tmp
? XFS_ATTR_LOCAL
: 0;
1402 entry
->flags
|= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
1403 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1404 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1405 if ((args
->blkno2
== args
->blkno
) &&
1406 (args
->index2
<= args
->index
)) {
1410 xfs_trans_log_buf(args
->trans
, bp
,
1411 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
1412 ASSERT((args
->index
== 0) ||
1413 (be32_to_cpu(entry
->hashval
) >= be32_to_cpu((entry
-1)->hashval
)));
1414 ASSERT((args
->index
== ichdr
->count
- 1) ||
1415 (be32_to_cpu(entry
->hashval
) <= be32_to_cpu((entry
+1)->hashval
)));
1418 * For "remote" attribute values, simply note that we need to
1419 * allocate space for the "remote" value. We can't actually
1420 * allocate the extents in this transaction, and we can't decide
1421 * which blocks they should be as we might allocate more blocks
1422 * as part of this transaction (a split operation for example).
1424 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1425 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
1426 name_loc
->namelen
= args
->namelen
;
1427 name_loc
->valuelen
= cpu_to_be16(args
->valuelen
);
1428 memcpy((char *)name_loc
->nameval
, args
->name
, args
->namelen
);
1429 memcpy((char *)&name_loc
->nameval
[args
->namelen
], args
->value
,
1430 be16_to_cpu(name_loc
->valuelen
));
1432 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
1433 name_rmt
->namelen
= args
->namelen
;
1434 memcpy((char *)name_rmt
->name
, args
->name
, args
->namelen
);
1435 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1437 name_rmt
->valuelen
= 0;
1438 name_rmt
->valueblk
= 0;
1440 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
1441 args
->rmtvaluelen
= args
->valuelen
;
1443 xfs_trans_log_buf(args
->trans
, bp
,
1444 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
1445 xfs_attr_leaf_entsize(leaf
, args
->index
)));
1448 * Update the control info for this leaf node
1450 if (be16_to_cpu(entry
->nameidx
) < ichdr
->firstused
)
1451 ichdr
->firstused
= be16_to_cpu(entry
->nameidx
);
1453 ASSERT(ichdr
->firstused
>= ichdr
->count
* sizeof(xfs_attr_leaf_entry_t
)
1454 + xfs_attr3_leaf_hdr_size(leaf
));
1455 tmp
= (ichdr
->count
- 1) * sizeof(xfs_attr_leaf_entry_t
)
1456 + xfs_attr3_leaf_hdr_size(leaf
);
1458 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
1459 if (ichdr
->freemap
[i
].base
== tmp
) {
1460 ichdr
->freemap
[i
].base
+= sizeof(xfs_attr_leaf_entry_t
);
1461 ichdr
->freemap
[i
].size
-= sizeof(xfs_attr_leaf_entry_t
);
1464 ichdr
->usedbytes
+= xfs_attr_leaf_entsize(leaf
, args
->index
);
1469 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1472 xfs_attr3_leaf_compact(
1473 struct xfs_da_args
*args
,
1474 struct xfs_attr3_icleaf_hdr
*ichdr_dst
,
1477 struct xfs_attr_leafblock
*leaf_src
;
1478 struct xfs_attr_leafblock
*leaf_dst
;
1479 struct xfs_attr3_icleaf_hdr ichdr_src
;
1480 struct xfs_trans
*trans
= args
->trans
;
1483 trace_xfs_attr_leaf_compact(args
);
1485 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, 0);
1486 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1487 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1488 leaf_src
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1489 leaf_dst
= bp
->b_addr
;
1492 * Copy the on-disk header back into the destination buffer to ensure
1493 * all the information in the header that is not part of the incore
1494 * header structure is preserved.
1496 memcpy(bp
->b_addr
, tmpbuffer
, xfs_attr3_leaf_hdr_size(leaf_src
));
1498 /* Initialise the incore headers */
1499 ichdr_src
= *ichdr_dst
; /* struct copy */
1500 ichdr_dst
->firstused
= args
->geo
->blksize
;
1501 ichdr_dst
->usedbytes
= 0;
1502 ichdr_dst
->count
= 0;
1503 ichdr_dst
->holes
= 0;
1504 ichdr_dst
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_src
);
1505 ichdr_dst
->freemap
[0].size
= ichdr_dst
->firstused
-
1506 ichdr_dst
->freemap
[0].base
;
1508 /* write the header back to initialise the underlying buffer */
1509 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf_dst
, ichdr_dst
);
1512 * Copy all entry's in the same (sorted) order,
1513 * but allocate name/value pairs packed and in sequence.
1515 xfs_attr3_leaf_moveents(args
, leaf_src
, &ichdr_src
, 0,
1516 leaf_dst
, ichdr_dst
, 0, ichdr_src
.count
);
1518 * this logs the entire buffer, but the caller must write the header
1519 * back to the buffer when it is finished modifying it.
1521 xfs_trans_log_buf(trans
, bp
, 0, args
->geo
->blksize
- 1);
1523 kmem_free(tmpbuffer
);
1527 * Compare two leaf blocks "order".
1528 * Return 0 unless leaf2 should go before leaf1.
1531 xfs_attr3_leaf_order(
1532 struct xfs_buf
*leaf1_bp
,
1533 struct xfs_attr3_icleaf_hdr
*leaf1hdr
,
1534 struct xfs_buf
*leaf2_bp
,
1535 struct xfs_attr3_icleaf_hdr
*leaf2hdr
)
1537 struct xfs_attr_leaf_entry
*entries1
;
1538 struct xfs_attr_leaf_entry
*entries2
;
1540 entries1
= xfs_attr3_leaf_entryp(leaf1_bp
->b_addr
);
1541 entries2
= xfs_attr3_leaf_entryp(leaf2_bp
->b_addr
);
1542 if (leaf1hdr
->count
> 0 && leaf2hdr
->count
> 0 &&
1543 ((be32_to_cpu(entries2
[0].hashval
) <
1544 be32_to_cpu(entries1
[0].hashval
)) ||
1545 (be32_to_cpu(entries2
[leaf2hdr
->count
- 1].hashval
) <
1546 be32_to_cpu(entries1
[leaf1hdr
->count
- 1].hashval
)))) {
1553 xfs_attr_leaf_order(
1554 struct xfs_buf
*leaf1_bp
,
1555 struct xfs_buf
*leaf2_bp
)
1557 struct xfs_attr3_icleaf_hdr ichdr1
;
1558 struct xfs_attr3_icleaf_hdr ichdr2
;
1559 struct xfs_mount
*mp
= leaf1_bp
->b_mount
;
1561 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr1
, leaf1_bp
->b_addr
);
1562 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr2
, leaf2_bp
->b_addr
);
1563 return xfs_attr3_leaf_order(leaf1_bp
, &ichdr1
, leaf2_bp
, &ichdr2
);
1567 * Redistribute the attribute list entries between two leaf nodes,
1568 * taking into account the size of the new entry.
1570 * NOTE: if new block is empty, then it will get the upper half of the
1571 * old block. At present, all (one) callers pass in an empty second block.
1573 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1574 * to match what it is doing in splitting the attribute leaf block. Those
1575 * values are used in "atomic rename" operations on attributes. Note that
1576 * the "new" and "old" values can end up in different blocks.
1579 xfs_attr3_leaf_rebalance(
1580 struct xfs_da_state
*state
,
1581 struct xfs_da_state_blk
*blk1
,
1582 struct xfs_da_state_blk
*blk2
)
1584 struct xfs_da_args
*args
;
1585 struct xfs_attr_leafblock
*leaf1
;
1586 struct xfs_attr_leafblock
*leaf2
;
1587 struct xfs_attr3_icleaf_hdr ichdr1
;
1588 struct xfs_attr3_icleaf_hdr ichdr2
;
1589 struct xfs_attr_leaf_entry
*entries1
;
1590 struct xfs_attr_leaf_entry
*entries2
;
1598 * Set up environment.
1600 ASSERT(blk1
->magic
== XFS_ATTR_LEAF_MAGIC
);
1601 ASSERT(blk2
->magic
== XFS_ATTR_LEAF_MAGIC
);
1602 leaf1
= blk1
->bp
->b_addr
;
1603 leaf2
= blk2
->bp
->b_addr
;
1604 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr1
, leaf1
);
1605 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, leaf2
);
1606 ASSERT(ichdr2
.count
== 0);
1609 trace_xfs_attr_leaf_rebalance(args
);
1612 * Check ordering of blocks, reverse if it makes things simpler.
1614 * NOTE: Given that all (current) callers pass in an empty
1615 * second block, this code should never set "swap".
1618 if (xfs_attr3_leaf_order(blk1
->bp
, &ichdr1
, blk2
->bp
, &ichdr2
)) {
1621 /* swap structures rather than reconverting them */
1622 swap(ichdr1
, ichdr2
);
1624 leaf1
= blk1
->bp
->b_addr
;
1625 leaf2
= blk2
->bp
->b_addr
;
1630 * Examine entries until we reduce the absolute difference in
1631 * byte usage between the two blocks to a minimum. Then get
1632 * the direction to copy and the number of elements to move.
1634 * "inleaf" is true if the new entry should be inserted into blk1.
1635 * If "swap" is also true, then reverse the sense of "inleaf".
1637 state
->inleaf
= xfs_attr3_leaf_figure_balance(state
, blk1
, &ichdr1
,
1641 state
->inleaf
= !state
->inleaf
;
1644 * Move any entries required from leaf to leaf:
1646 if (count
< ichdr1
.count
) {
1648 * Figure the total bytes to be added to the destination leaf.
1650 /* number entries being moved */
1651 count
= ichdr1
.count
- count
;
1652 space
= ichdr1
.usedbytes
- totallen
;
1653 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1656 * leaf2 is the destination, compact it if it looks tight.
1658 max
= ichdr2
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1659 max
-= ichdr2
.count
* sizeof(xfs_attr_leaf_entry_t
);
1661 xfs_attr3_leaf_compact(args
, &ichdr2
, blk2
->bp
);
1664 * Move high entries from leaf1 to low end of leaf2.
1666 xfs_attr3_leaf_moveents(args
, leaf1
, &ichdr1
,
1667 ichdr1
.count
- count
, leaf2
, &ichdr2
, 0, count
);
1669 } else if (count
> ichdr1
.count
) {
1671 * I assert that since all callers pass in an empty
1672 * second buffer, this code should never execute.
1677 * Figure the total bytes to be added to the destination leaf.
1679 /* number entries being moved */
1680 count
-= ichdr1
.count
;
1681 space
= totallen
- ichdr1
.usedbytes
;
1682 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1685 * leaf1 is the destination, compact it if it looks tight.
1687 max
= ichdr1
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1688 max
-= ichdr1
.count
* sizeof(xfs_attr_leaf_entry_t
);
1690 xfs_attr3_leaf_compact(args
, &ichdr1
, blk1
->bp
);
1693 * Move low entries from leaf2 to high end of leaf1.
1695 xfs_attr3_leaf_moveents(args
, leaf2
, &ichdr2
, 0, leaf1
, &ichdr1
,
1696 ichdr1
.count
, count
);
1699 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf1
, &ichdr1
);
1700 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf2
, &ichdr2
);
1701 xfs_trans_log_buf(args
->trans
, blk1
->bp
, 0, args
->geo
->blksize
- 1);
1702 xfs_trans_log_buf(args
->trans
, blk2
->bp
, 0, args
->geo
->blksize
- 1);
1705 * Copy out last hashval in each block for B-tree code.
1707 entries1
= xfs_attr3_leaf_entryp(leaf1
);
1708 entries2
= xfs_attr3_leaf_entryp(leaf2
);
1709 blk1
->hashval
= be32_to_cpu(entries1
[ichdr1
.count
- 1].hashval
);
1710 blk2
->hashval
= be32_to_cpu(entries2
[ichdr2
.count
- 1].hashval
);
1713 * Adjust the expected index for insertion.
1714 * NOTE: this code depends on the (current) situation that the
1715 * second block was originally empty.
1717 * If the insertion point moved to the 2nd block, we must adjust
1718 * the index. We must also track the entry just following the
1719 * new entry for use in an "atomic rename" operation, that entry
1720 * is always the "old" entry and the "new" entry is what we are
1721 * inserting. The index/blkno fields refer to the "old" entry,
1722 * while the index2/blkno2 fields refer to the "new" entry.
1724 if (blk1
->index
> ichdr1
.count
) {
1725 ASSERT(state
->inleaf
== 0);
1726 blk2
->index
= blk1
->index
- ichdr1
.count
;
1727 args
->index
= args
->index2
= blk2
->index
;
1728 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1729 } else if (blk1
->index
== ichdr1
.count
) {
1730 if (state
->inleaf
) {
1731 args
->index
= blk1
->index
;
1732 args
->blkno
= blk1
->blkno
;
1734 args
->blkno2
= blk2
->blkno
;
1737 * On a double leaf split, the original attr location
1738 * is already stored in blkno2/index2, so don't
1739 * overwrite it overwise we corrupt the tree.
1741 blk2
->index
= blk1
->index
- ichdr1
.count
;
1742 args
->index
= blk2
->index
;
1743 args
->blkno
= blk2
->blkno
;
1744 if (!state
->extravalid
) {
1746 * set the new attr location to match the old
1747 * one and let the higher level split code
1748 * decide where in the leaf to place it.
1750 args
->index2
= blk2
->index
;
1751 args
->blkno2
= blk2
->blkno
;
1755 ASSERT(state
->inleaf
== 1);
1756 args
->index
= args
->index2
= blk1
->index
;
1757 args
->blkno
= args
->blkno2
= blk1
->blkno
;
1762 * Examine entries until we reduce the absolute difference in
1763 * byte usage between the two blocks to a minimum.
1764 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1765 * GROT: there will always be enough room in either block for a new entry.
1766 * GROT: Do a double-split for this case?
1769 xfs_attr3_leaf_figure_balance(
1770 struct xfs_da_state
*state
,
1771 struct xfs_da_state_blk
*blk1
,
1772 struct xfs_attr3_icleaf_hdr
*ichdr1
,
1773 struct xfs_da_state_blk
*blk2
,
1774 struct xfs_attr3_icleaf_hdr
*ichdr2
,
1778 struct xfs_attr_leafblock
*leaf1
= blk1
->bp
->b_addr
;
1779 struct xfs_attr_leafblock
*leaf2
= blk2
->bp
->b_addr
;
1780 struct xfs_attr_leaf_entry
*entry
;
1791 * Examine entries until we reduce the absolute difference in
1792 * byte usage between the two blocks to a minimum.
1794 max
= ichdr1
->count
+ ichdr2
->count
;
1795 half
= (max
+ 1) * sizeof(*entry
);
1796 half
+= ichdr1
->usedbytes
+ ichdr2
->usedbytes
+
1797 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1799 lastdelta
= state
->args
->geo
->blksize
;
1800 entry
= xfs_attr3_leaf_entryp(leaf1
);
1801 for (count
= index
= 0; count
< max
; entry
++, index
++, count
++) {
1803 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1805 * The new entry is in the first block, account for it.
1807 if (count
== blk1
->index
) {
1808 tmp
= totallen
+ sizeof(*entry
) +
1809 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1810 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1812 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1818 * Wrap around into the second block if necessary.
1820 if (count
== ichdr1
->count
) {
1822 entry
= xfs_attr3_leaf_entryp(leaf1
);
1827 * Figure out if next leaf entry would be too much.
1829 tmp
= totallen
+ sizeof(*entry
) + xfs_attr_leaf_entsize(leaf1
,
1831 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1833 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1839 * Calculate the number of usedbytes that will end up in lower block.
1840 * If new entry not in lower block, fix up the count.
1842 totallen
-= count
* sizeof(*entry
);
1844 totallen
-= sizeof(*entry
) +
1845 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1849 *usedbytesarg
= totallen
;
1853 /*========================================================================
1854 * Routines used for shrinking the Btree.
1855 *========================================================================*/
1858 * Check a leaf block and its neighbors to see if the block should be
1859 * collapsed into one or the other neighbor. Always keep the block
1860 * with the smaller block number.
1861 * If the current block is over 50% full, don't try to join it, return 0.
1862 * If the block is empty, fill in the state structure and return 2.
1863 * If it can be collapsed, fill in the state structure and return 1.
1864 * If nothing can be done, return 0.
1866 * GROT: allow for INCOMPLETE entries in calculation.
1869 xfs_attr3_leaf_toosmall(
1870 struct xfs_da_state
*state
,
1873 struct xfs_attr_leafblock
*leaf
;
1874 struct xfs_da_state_blk
*blk
;
1875 struct xfs_attr3_icleaf_hdr ichdr
;
1884 trace_xfs_attr_leaf_toosmall(state
->args
);
1887 * Check for the degenerate case of the block being over 50% full.
1888 * If so, it's not worth even looking to see if we might be able
1889 * to coalesce with a sibling.
1891 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1892 leaf
= blk
->bp
->b_addr
;
1893 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr
, leaf
);
1894 bytes
= xfs_attr3_leaf_hdr_size(leaf
) +
1895 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
) +
1897 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1898 *action
= 0; /* blk over 50%, don't try to join */
1903 * Check for the degenerate case of the block being empty.
1904 * If the block is empty, we'll simply delete it, no need to
1905 * coalesce it with a sibling block. We choose (arbitrarily)
1906 * to merge with the forward block unless it is NULL.
1908 if (ichdr
.count
== 0) {
1910 * Make altpath point to the block we want to keep and
1911 * path point to the block we want to drop (this one).
1913 forward
= (ichdr
.forw
!= 0);
1914 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1915 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1928 * Examine each sibling block to see if we can coalesce with
1929 * at least 25% free space to spare. We need to figure out
1930 * whether to merge with the forward or the backward block.
1931 * We prefer coalescing with the lower numbered sibling so as
1932 * to shrink an attribute list over time.
1934 /* start with smaller blk num */
1935 forward
= ichdr
.forw
< ichdr
.back
;
1936 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1937 struct xfs_attr3_icleaf_hdr ichdr2
;
1944 error
= xfs_attr3_leaf_read(state
->args
->trans
, state
->args
->dp
,
1949 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, bp
->b_addr
);
1951 bytes
= state
->args
->geo
->blksize
-
1952 (state
->args
->geo
->blksize
>> 2) -
1953 ichdr
.usedbytes
- ichdr2
.usedbytes
-
1954 ((ichdr
.count
+ ichdr2
.count
) *
1955 sizeof(xfs_attr_leaf_entry_t
)) -
1956 xfs_attr3_leaf_hdr_size(leaf
);
1958 xfs_trans_brelse(state
->args
->trans
, bp
);
1960 break; /* fits with at least 25% to spare */
1968 * Make altpath point to the block we want to keep (the lower
1969 * numbered block) and path point to the block we want to drop.
1971 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1972 if (blkno
< blk
->blkno
) {
1973 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1976 error
= xfs_da3_path_shift(state
, &state
->path
, forward
,
1990 * Remove a name from the leaf attribute list structure.
1992 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1993 * If two leaves are 37% full, when combined they will leave 25% free.
1996 xfs_attr3_leaf_remove(
1998 struct xfs_da_args
*args
)
2000 struct xfs_attr_leafblock
*leaf
;
2001 struct xfs_attr3_icleaf_hdr ichdr
;
2002 struct xfs_attr_leaf_entry
*entry
;
2011 trace_xfs_attr_leaf_remove(args
);
2014 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2016 ASSERT(ichdr
.count
> 0 && ichdr
.count
< args
->geo
->blksize
/ 8);
2017 ASSERT(args
->index
>= 0 && args
->index
< ichdr
.count
);
2018 ASSERT(ichdr
.firstused
>= ichdr
.count
* sizeof(*entry
) +
2019 xfs_attr3_leaf_hdr_size(leaf
));
2021 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2023 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2024 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2027 * Scan through free region table:
2028 * check for adjacency of free'd entry with an existing one,
2029 * find smallest free region in case we need to replace it,
2030 * adjust any map that borders the entry table,
2032 tablesize
= ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
)
2033 + xfs_attr3_leaf_hdr_size(leaf
);
2034 tmp
= ichdr
.freemap
[0].size
;
2035 before
= after
= -1;
2036 smallest
= XFS_ATTR_LEAF_MAPSIZE
- 1;
2037 entsize
= xfs_attr_leaf_entsize(leaf
, args
->index
);
2038 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
2039 ASSERT(ichdr
.freemap
[i
].base
< args
->geo
->blksize
);
2040 ASSERT(ichdr
.freemap
[i
].size
< args
->geo
->blksize
);
2041 if (ichdr
.freemap
[i
].base
== tablesize
) {
2042 ichdr
.freemap
[i
].base
-= sizeof(xfs_attr_leaf_entry_t
);
2043 ichdr
.freemap
[i
].size
+= sizeof(xfs_attr_leaf_entry_t
);
2046 if (ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
==
2047 be16_to_cpu(entry
->nameidx
)) {
2049 } else if (ichdr
.freemap
[i
].base
==
2050 (be16_to_cpu(entry
->nameidx
) + entsize
)) {
2052 } else if (ichdr
.freemap
[i
].size
< tmp
) {
2053 tmp
= ichdr
.freemap
[i
].size
;
2059 * Coalesce adjacent freemap regions,
2060 * or replace the smallest region.
2062 if ((before
>= 0) || (after
>= 0)) {
2063 if ((before
>= 0) && (after
>= 0)) {
2064 ichdr
.freemap
[before
].size
+= entsize
;
2065 ichdr
.freemap
[before
].size
+= ichdr
.freemap
[after
].size
;
2066 ichdr
.freemap
[after
].base
= 0;
2067 ichdr
.freemap
[after
].size
= 0;
2068 } else if (before
>= 0) {
2069 ichdr
.freemap
[before
].size
+= entsize
;
2071 ichdr
.freemap
[after
].base
= be16_to_cpu(entry
->nameidx
);
2072 ichdr
.freemap
[after
].size
+= entsize
;
2076 * Replace smallest region (if it is smaller than free'd entry)
2078 if (ichdr
.freemap
[smallest
].size
< entsize
) {
2079 ichdr
.freemap
[smallest
].base
= be16_to_cpu(entry
->nameidx
);
2080 ichdr
.freemap
[smallest
].size
= entsize
;
2085 * Did we remove the first entry?
2087 if (be16_to_cpu(entry
->nameidx
) == ichdr
.firstused
)
2093 * Compress the remaining entries and zero out the removed stuff.
2095 memset(xfs_attr3_leaf_name(leaf
, args
->index
), 0, entsize
);
2096 ichdr
.usedbytes
-= entsize
;
2097 xfs_trans_log_buf(args
->trans
, bp
,
2098 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
2101 tmp
= (ichdr
.count
- args
->index
) * sizeof(xfs_attr_leaf_entry_t
);
2102 memmove(entry
, entry
+ 1, tmp
);
2104 xfs_trans_log_buf(args
->trans
, bp
,
2105 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(xfs_attr_leaf_entry_t
)));
2107 entry
= &xfs_attr3_leaf_entryp(leaf
)[ichdr
.count
];
2108 memset(entry
, 0, sizeof(xfs_attr_leaf_entry_t
));
2111 * If we removed the first entry, re-find the first used byte
2112 * in the name area. Note that if the entry was the "firstused",
2113 * then we don't have a "hole" in our block resulting from
2114 * removing the name.
2117 tmp
= args
->geo
->blksize
;
2118 entry
= xfs_attr3_leaf_entryp(leaf
);
2119 for (i
= ichdr
.count
- 1; i
>= 0; entry
++, i
--) {
2120 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2121 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2123 if (be16_to_cpu(entry
->nameidx
) < tmp
)
2124 tmp
= be16_to_cpu(entry
->nameidx
);
2126 ichdr
.firstused
= tmp
;
2127 ASSERT(ichdr
.firstused
!= 0);
2129 ichdr
.holes
= 1; /* mark as needing compaction */
2131 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
2132 xfs_trans_log_buf(args
->trans
, bp
,
2133 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
2134 xfs_attr3_leaf_hdr_size(leaf
)));
2137 * Check if leaf is less than 50% full, caller may want to
2138 * "join" the leaf with a sibling if so.
2140 tmp
= ichdr
.usedbytes
+ xfs_attr3_leaf_hdr_size(leaf
) +
2141 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
);
2143 return tmp
< args
->geo
->magicpct
; /* leaf is < 37% full */
2147 * Move all the attribute list entries from drop_leaf into save_leaf.
2150 xfs_attr3_leaf_unbalance(
2151 struct xfs_da_state
*state
,
2152 struct xfs_da_state_blk
*drop_blk
,
2153 struct xfs_da_state_blk
*save_blk
)
2155 struct xfs_attr_leafblock
*drop_leaf
= drop_blk
->bp
->b_addr
;
2156 struct xfs_attr_leafblock
*save_leaf
= save_blk
->bp
->b_addr
;
2157 struct xfs_attr3_icleaf_hdr drophdr
;
2158 struct xfs_attr3_icleaf_hdr savehdr
;
2159 struct xfs_attr_leaf_entry
*entry
;
2161 trace_xfs_attr_leaf_unbalance(state
->args
);
2163 drop_leaf
= drop_blk
->bp
->b_addr
;
2164 save_leaf
= save_blk
->bp
->b_addr
;
2165 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &drophdr
, drop_leaf
);
2166 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &savehdr
, save_leaf
);
2167 entry
= xfs_attr3_leaf_entryp(drop_leaf
);
2170 * Save last hashval from dying block for later Btree fixup.
2172 drop_blk
->hashval
= be32_to_cpu(entry
[drophdr
.count
- 1].hashval
);
2175 * Check if we need a temp buffer, or can we do it in place.
2176 * Note that we don't check "leaf" for holes because we will
2177 * always be dropping it, toosmall() decided that for us already.
2179 if (savehdr
.holes
== 0) {
2181 * dest leaf has no holes, so we add there. May need
2182 * to make some room in the entry array.
2184 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2185 drop_blk
->bp
, &drophdr
)) {
2186 xfs_attr3_leaf_moveents(state
->args
,
2187 drop_leaf
, &drophdr
, 0,
2188 save_leaf
, &savehdr
, 0,
2191 xfs_attr3_leaf_moveents(state
->args
,
2192 drop_leaf
, &drophdr
, 0,
2193 save_leaf
, &savehdr
,
2194 savehdr
.count
, drophdr
.count
);
2198 * Destination has holes, so we make a temporary copy
2199 * of the leaf and add them both to that.
2201 struct xfs_attr_leafblock
*tmp_leaf
;
2202 struct xfs_attr3_icleaf_hdr tmphdr
;
2204 tmp_leaf
= kmem_zalloc(state
->args
->geo
->blksize
, 0);
2207 * Copy the header into the temp leaf so that all the stuff
2208 * not in the incore header is present and gets copied back in
2209 * once we've moved all the entries.
2211 memcpy(tmp_leaf
, save_leaf
, xfs_attr3_leaf_hdr_size(save_leaf
));
2213 memset(&tmphdr
, 0, sizeof(tmphdr
));
2214 tmphdr
.magic
= savehdr
.magic
;
2215 tmphdr
.forw
= savehdr
.forw
;
2216 tmphdr
.back
= savehdr
.back
;
2217 tmphdr
.firstused
= state
->args
->geo
->blksize
;
2219 /* write the header to the temp buffer to initialise it */
2220 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, tmp_leaf
, &tmphdr
);
2222 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2223 drop_blk
->bp
, &drophdr
)) {
2224 xfs_attr3_leaf_moveents(state
->args
,
2225 drop_leaf
, &drophdr
, 0,
2226 tmp_leaf
, &tmphdr
, 0,
2228 xfs_attr3_leaf_moveents(state
->args
,
2229 save_leaf
, &savehdr
, 0,
2230 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2233 xfs_attr3_leaf_moveents(state
->args
,
2234 save_leaf
, &savehdr
, 0,
2235 tmp_leaf
, &tmphdr
, 0,
2237 xfs_attr3_leaf_moveents(state
->args
,
2238 drop_leaf
, &drophdr
, 0,
2239 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2242 memcpy(save_leaf
, tmp_leaf
, state
->args
->geo
->blksize
);
2243 savehdr
= tmphdr
; /* struct copy */
2244 kmem_free(tmp_leaf
);
2247 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, save_leaf
, &savehdr
);
2248 xfs_trans_log_buf(state
->args
->trans
, save_blk
->bp
, 0,
2249 state
->args
->geo
->blksize
- 1);
2252 * Copy out last hashval in each block for B-tree code.
2254 entry
= xfs_attr3_leaf_entryp(save_leaf
);
2255 save_blk
->hashval
= be32_to_cpu(entry
[savehdr
.count
- 1].hashval
);
2258 /*========================================================================
2259 * Routines used for finding things in the Btree.
2260 *========================================================================*/
2263 * Look up a name in a leaf attribute list structure.
2264 * This is the internal routine, it uses the caller's buffer.
2266 * Note that duplicate keys are allowed, but only check within the
2267 * current leaf node. The Btree code must check in adjacent leaf nodes.
2269 * Return in args->index the index into the entry[] array of either
2270 * the found entry, or where the entry should have been (insert before
2273 * Don't change the args->value unless we find the attribute.
2276 xfs_attr3_leaf_lookup_int(
2278 struct xfs_da_args
*args
)
2280 struct xfs_attr_leafblock
*leaf
;
2281 struct xfs_attr3_icleaf_hdr ichdr
;
2282 struct xfs_attr_leaf_entry
*entry
;
2283 struct xfs_attr_leaf_entry
*entries
;
2284 struct xfs_attr_leaf_name_local
*name_loc
;
2285 struct xfs_attr_leaf_name_remote
*name_rmt
;
2286 xfs_dahash_t hashval
;
2290 trace_xfs_attr_leaf_lookup(args
);
2293 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2294 entries
= xfs_attr3_leaf_entryp(leaf
);
2295 if (ichdr
.count
>= args
->geo
->blksize
/ 8)
2296 return -EFSCORRUPTED
;
2299 * Binary search. (note: small blocks will skip this loop)
2301 hashval
= args
->hashval
;
2302 probe
= span
= ichdr
.count
/ 2;
2303 for (entry
= &entries
[probe
]; span
> 4; entry
= &entries
[probe
]) {
2305 if (be32_to_cpu(entry
->hashval
) < hashval
)
2307 else if (be32_to_cpu(entry
->hashval
) > hashval
)
2312 if (!(probe
>= 0 && (!ichdr
.count
|| probe
< ichdr
.count
)))
2313 return -EFSCORRUPTED
;
2314 if (!(span
<= 4 || be32_to_cpu(entry
->hashval
) == hashval
))
2315 return -EFSCORRUPTED
;
2318 * Since we may have duplicate hashval's, find the first matching
2319 * hashval in the leaf.
2321 while (probe
> 0 && be32_to_cpu(entry
->hashval
) >= hashval
) {
2325 while (probe
< ichdr
.count
&&
2326 be32_to_cpu(entry
->hashval
) < hashval
) {
2330 if (probe
== ichdr
.count
|| be32_to_cpu(entry
->hashval
) != hashval
) {
2331 args
->index
= probe
;
2336 * Duplicate keys may be present, so search all of them for a match.
2338 for (; probe
< ichdr
.count
&& (be32_to_cpu(entry
->hashval
) == hashval
);
2341 * GROT: Add code to remove incomplete entries.
2344 * If we are looking for INCOMPLETE entries, show only those.
2345 * If we are looking for complete entries, show only those.
2347 if ((args
->flags
& XFS_ATTR_INCOMPLETE
) !=
2348 (entry
->flags
& XFS_ATTR_INCOMPLETE
)) {
2351 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2352 name_loc
= xfs_attr3_leaf_name_local(leaf
, probe
);
2353 if (name_loc
->namelen
!= args
->namelen
)
2355 if (memcmp(args
->name
, name_loc
->nameval
,
2356 args
->namelen
) != 0)
2358 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2360 args
->index
= probe
;
2363 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, probe
);
2364 if (name_rmt
->namelen
!= args
->namelen
)
2366 if (memcmp(args
->name
, name_rmt
->name
,
2367 args
->namelen
) != 0)
2369 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2371 args
->index
= probe
;
2372 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2373 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2374 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(
2380 args
->index
= probe
;
2385 * Get the value associated with an attribute name from a leaf attribute
2388 * If ATTR_KERNOVAL is specified, only the length needs to be returned.
2389 * Unlike a lookup, we only return an error if the attribute does not
2390 * exist or we can't retrieve the value.
2393 xfs_attr3_leaf_getvalue(
2395 struct xfs_da_args
*args
)
2397 struct xfs_attr_leafblock
*leaf
;
2398 struct xfs_attr3_icleaf_hdr ichdr
;
2399 struct xfs_attr_leaf_entry
*entry
;
2400 struct xfs_attr_leaf_name_local
*name_loc
;
2401 struct xfs_attr_leaf_name_remote
*name_rmt
;
2404 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2405 ASSERT(ichdr
.count
< args
->geo
->blksize
/ 8);
2406 ASSERT(args
->index
< ichdr
.count
);
2408 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2409 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2410 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2411 ASSERT(name_loc
->namelen
== args
->namelen
);
2412 ASSERT(memcmp(args
->name
, name_loc
->nameval
, args
->namelen
) == 0);
2413 return xfs_attr_copy_value(args
,
2414 &name_loc
->nameval
[args
->namelen
],
2415 be16_to_cpu(name_loc
->valuelen
));
2418 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2419 ASSERT(name_rmt
->namelen
== args
->namelen
);
2420 ASSERT(memcmp(args
->name
, name_rmt
->name
, args
->namelen
) == 0);
2421 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2422 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2423 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(args
->dp
->i_mount
,
2425 return xfs_attr_copy_value(args
, NULL
, args
->rmtvaluelen
);
2428 /*========================================================================
2430 *========================================================================*/
2433 * Move the indicated entries from one leaf to another.
2434 * NOTE: this routine modifies both source and destination leaves.
2438 xfs_attr3_leaf_moveents(
2439 struct xfs_da_args
*args
,
2440 struct xfs_attr_leafblock
*leaf_s
,
2441 struct xfs_attr3_icleaf_hdr
*ichdr_s
,
2443 struct xfs_attr_leafblock
*leaf_d
,
2444 struct xfs_attr3_icleaf_hdr
*ichdr_d
,
2448 struct xfs_attr_leaf_entry
*entry_s
;
2449 struct xfs_attr_leaf_entry
*entry_d
;
2455 * Check for nothing to do.
2461 * Set up environment.
2463 ASSERT(ichdr_s
->magic
== XFS_ATTR_LEAF_MAGIC
||
2464 ichdr_s
->magic
== XFS_ATTR3_LEAF_MAGIC
);
2465 ASSERT(ichdr_s
->magic
== ichdr_d
->magic
);
2466 ASSERT(ichdr_s
->count
> 0 && ichdr_s
->count
< args
->geo
->blksize
/ 8);
2467 ASSERT(ichdr_s
->firstused
>= (ichdr_s
->count
* sizeof(*entry_s
))
2468 + xfs_attr3_leaf_hdr_size(leaf_s
));
2469 ASSERT(ichdr_d
->count
< args
->geo
->blksize
/ 8);
2470 ASSERT(ichdr_d
->firstused
>= (ichdr_d
->count
* sizeof(*entry_d
))
2471 + xfs_attr3_leaf_hdr_size(leaf_d
));
2473 ASSERT(start_s
< ichdr_s
->count
);
2474 ASSERT(start_d
<= ichdr_d
->count
);
2475 ASSERT(count
<= ichdr_s
->count
);
2479 * Move the entries in the destination leaf up to make a hole?
2481 if (start_d
< ichdr_d
->count
) {
2482 tmp
= ichdr_d
->count
- start_d
;
2483 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2484 entry_s
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2485 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
+ count
];
2486 memmove(entry_d
, entry_s
, tmp
);
2490 * Copy all entry's in the same (sorted) order,
2491 * but allocate attribute info packed and in sequence.
2493 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2494 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2496 for (i
= 0; i
< count
; entry_s
++, entry_d
++, desti
++, i
++) {
2497 ASSERT(be16_to_cpu(entry_s
->nameidx
) >= ichdr_s
->firstused
);
2498 tmp
= xfs_attr_leaf_entsize(leaf_s
, start_s
+ i
);
2501 * Code to drop INCOMPLETE entries. Difficult to use as we
2502 * may also need to change the insertion index. Code turned
2503 * off for 6.2, should be revisited later.
2505 if (entry_s
->flags
& XFS_ATTR_INCOMPLETE
) { /* skip partials? */
2506 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2507 ichdr_s
->usedbytes
-= tmp
;
2508 ichdr_s
->count
-= 1;
2509 entry_d
--; /* to compensate for ++ in loop hdr */
2511 if ((start_s
+ i
) < offset
)
2512 result
++; /* insertion index adjustment */
2515 ichdr_d
->firstused
-= tmp
;
2516 /* both on-disk, don't endian flip twice */
2517 entry_d
->hashval
= entry_s
->hashval
;
2518 entry_d
->nameidx
= cpu_to_be16(ichdr_d
->firstused
);
2519 entry_d
->flags
= entry_s
->flags
;
2520 ASSERT(be16_to_cpu(entry_d
->nameidx
) + tmp
2521 <= args
->geo
->blksize
);
2522 memmove(xfs_attr3_leaf_name(leaf_d
, desti
),
2523 xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), tmp
);
2524 ASSERT(be16_to_cpu(entry_s
->nameidx
) + tmp
2525 <= args
->geo
->blksize
);
2526 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2527 ichdr_s
->usedbytes
-= tmp
;
2528 ichdr_d
->usedbytes
+= tmp
;
2529 ichdr_s
->count
-= 1;
2530 ichdr_d
->count
+= 1;
2531 tmp
= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
)
2532 + xfs_attr3_leaf_hdr_size(leaf_d
);
2533 ASSERT(ichdr_d
->firstused
>= tmp
);
2540 * Zero out the entries we just copied.
2542 if (start_s
== ichdr_s
->count
) {
2543 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2544 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2545 ASSERT(((char *)entry_s
+ tmp
) <=
2546 ((char *)leaf_s
+ args
->geo
->blksize
));
2547 memset(entry_s
, 0, tmp
);
2550 * Move the remaining entries down to fill the hole,
2551 * then zero the entries at the top.
2553 tmp
= (ichdr_s
->count
- count
) * sizeof(xfs_attr_leaf_entry_t
);
2554 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
+ count
];
2555 entry_d
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2556 memmove(entry_d
, entry_s
, tmp
);
2558 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2559 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[ichdr_s
->count
];
2560 ASSERT(((char *)entry_s
+ tmp
) <=
2561 ((char *)leaf_s
+ args
->geo
->blksize
));
2562 memset(entry_s
, 0, tmp
);
2566 * Fill in the freemap information
2568 ichdr_d
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_d
);
2569 ichdr_d
->freemap
[0].base
+= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
);
2570 ichdr_d
->freemap
[0].size
= ichdr_d
->firstused
- ichdr_d
->freemap
[0].base
;
2571 ichdr_d
->freemap
[1].base
= 0;
2572 ichdr_d
->freemap
[2].base
= 0;
2573 ichdr_d
->freemap
[1].size
= 0;
2574 ichdr_d
->freemap
[2].size
= 0;
2575 ichdr_s
->holes
= 1; /* leaf may not be compact */
2579 * Pick up the last hashvalue from a leaf block.
2582 xfs_attr_leaf_lasthash(
2586 struct xfs_attr3_icleaf_hdr ichdr
;
2587 struct xfs_attr_leaf_entry
*entries
;
2588 struct xfs_mount
*mp
= bp
->b_mount
;
2590 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, bp
->b_addr
);
2591 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
2593 *count
= ichdr
.count
;
2596 return be32_to_cpu(entries
[ichdr
.count
- 1].hashval
);
2600 * Calculate the number of bytes used to store the indicated attribute
2601 * (whether local or remote only calculate bytes in this block).
2604 xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
)
2606 struct xfs_attr_leaf_entry
*entries
;
2607 xfs_attr_leaf_name_local_t
*name_loc
;
2608 xfs_attr_leaf_name_remote_t
*name_rmt
;
2611 entries
= xfs_attr3_leaf_entryp(leaf
);
2612 if (entries
[index
].flags
& XFS_ATTR_LOCAL
) {
2613 name_loc
= xfs_attr3_leaf_name_local(leaf
, index
);
2614 size
= xfs_attr_leaf_entsize_local(name_loc
->namelen
,
2615 be16_to_cpu(name_loc
->valuelen
));
2617 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, index
);
2618 size
= xfs_attr_leaf_entsize_remote(name_rmt
->namelen
);
2624 * Calculate the number of bytes that would be required to store the new
2625 * attribute (whether local or remote only calculate bytes in this block).
2626 * This routine decides as a side effect whether the attribute will be
2627 * a "local" or a "remote" attribute.
2630 xfs_attr_leaf_newentsize(
2631 struct xfs_da_args
*args
,
2636 size
= xfs_attr_leaf_entsize_local(args
->namelen
, args
->valuelen
);
2637 if (size
< xfs_attr_leaf_entsize_local_max(args
->geo
->blksize
)) {
2644 return xfs_attr_leaf_entsize_remote(args
->namelen
);
2648 /*========================================================================
2649 * Manage the INCOMPLETE flag in a leaf entry
2650 *========================================================================*/
2653 * Clear the INCOMPLETE flag on an entry in a leaf block.
2656 xfs_attr3_leaf_clearflag(
2657 struct xfs_da_args
*args
)
2659 struct xfs_attr_leafblock
*leaf
;
2660 struct xfs_attr_leaf_entry
*entry
;
2661 struct xfs_attr_leaf_name_remote
*name_rmt
;
2665 struct xfs_attr3_icleaf_hdr ichdr
;
2666 xfs_attr_leaf_name_local_t
*name_loc
;
2671 trace_xfs_attr_leaf_clearflag(args
);
2673 * Set up the operation.
2675 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2680 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2681 ASSERT(entry
->flags
& XFS_ATTR_INCOMPLETE
);
2684 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2685 ASSERT(args
->index
< ichdr
.count
);
2686 ASSERT(args
->index
>= 0);
2688 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2689 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2690 namelen
= name_loc
->namelen
;
2691 name
= (char *)name_loc
->nameval
;
2693 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2694 namelen
= name_rmt
->namelen
;
2695 name
= (char *)name_rmt
->name
;
2697 ASSERT(be32_to_cpu(entry
->hashval
) == args
->hashval
);
2698 ASSERT(namelen
== args
->namelen
);
2699 ASSERT(memcmp(name
, args
->name
, namelen
) == 0);
2702 entry
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2703 xfs_trans_log_buf(args
->trans
, bp
,
2704 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2706 if (args
->rmtblkno
) {
2707 ASSERT((entry
->flags
& XFS_ATTR_LOCAL
) == 0);
2708 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2709 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2710 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2711 xfs_trans_log_buf(args
->trans
, bp
,
2712 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2716 * Commit the flag value change and start the next trans in series.
2718 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2722 * Set the INCOMPLETE flag on an entry in a leaf block.
2725 xfs_attr3_leaf_setflag(
2726 struct xfs_da_args
*args
)
2728 struct xfs_attr_leafblock
*leaf
;
2729 struct xfs_attr_leaf_entry
*entry
;
2730 struct xfs_attr_leaf_name_remote
*name_rmt
;
2734 struct xfs_attr3_icleaf_hdr ichdr
;
2737 trace_xfs_attr_leaf_setflag(args
);
2740 * Set up the operation.
2742 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2748 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2749 ASSERT(args
->index
< ichdr
.count
);
2750 ASSERT(args
->index
>= 0);
2752 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2754 ASSERT((entry
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2755 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
2756 xfs_trans_log_buf(args
->trans
, bp
,
2757 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2758 if ((entry
->flags
& XFS_ATTR_LOCAL
) == 0) {
2759 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2760 name_rmt
->valueblk
= 0;
2761 name_rmt
->valuelen
= 0;
2762 xfs_trans_log_buf(args
->trans
, bp
,
2763 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2767 * Commit the flag value change and start the next trans in series.
2769 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2773 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2774 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2775 * entry given by args->blkno2/index2.
2777 * Note that they could be in different blocks, or in the same block.
2780 xfs_attr3_leaf_flipflags(
2781 struct xfs_da_args
*args
)
2783 struct xfs_attr_leafblock
*leaf1
;
2784 struct xfs_attr_leafblock
*leaf2
;
2785 struct xfs_attr_leaf_entry
*entry1
;
2786 struct xfs_attr_leaf_entry
*entry2
;
2787 struct xfs_attr_leaf_name_remote
*name_rmt
;
2788 struct xfs_buf
*bp1
;
2789 struct xfs_buf
*bp2
;
2792 struct xfs_attr3_icleaf_hdr ichdr1
;
2793 struct xfs_attr3_icleaf_hdr ichdr2
;
2794 xfs_attr_leaf_name_local_t
*name_loc
;
2795 int namelen1
, namelen2
;
2796 char *name1
, *name2
;
2799 trace_xfs_attr_leaf_flipflags(args
);
2802 * Read the block containing the "old" attr
2804 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp1
);
2809 * Read the block containing the "new" attr, if it is different
2811 if (args
->blkno2
!= args
->blkno
) {
2812 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno2
,
2820 leaf1
= bp1
->b_addr
;
2821 entry1
= &xfs_attr3_leaf_entryp(leaf1
)[args
->index
];
2823 leaf2
= bp2
->b_addr
;
2824 entry2
= &xfs_attr3_leaf_entryp(leaf2
)[args
->index2
];
2827 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr1
, leaf1
);
2828 ASSERT(args
->index
< ichdr1
.count
);
2829 ASSERT(args
->index
>= 0);
2831 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr2
, leaf2
);
2832 ASSERT(args
->index2
< ichdr2
.count
);
2833 ASSERT(args
->index2
>= 0);
2835 if (entry1
->flags
& XFS_ATTR_LOCAL
) {
2836 name_loc
= xfs_attr3_leaf_name_local(leaf1
, args
->index
);
2837 namelen1
= name_loc
->namelen
;
2838 name1
= (char *)name_loc
->nameval
;
2840 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2841 namelen1
= name_rmt
->namelen
;
2842 name1
= (char *)name_rmt
->name
;
2844 if (entry2
->flags
& XFS_ATTR_LOCAL
) {
2845 name_loc
= xfs_attr3_leaf_name_local(leaf2
, args
->index2
);
2846 namelen2
= name_loc
->namelen
;
2847 name2
= (char *)name_loc
->nameval
;
2849 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2850 namelen2
= name_rmt
->namelen
;
2851 name2
= (char *)name_rmt
->name
;
2853 ASSERT(be32_to_cpu(entry1
->hashval
) == be32_to_cpu(entry2
->hashval
));
2854 ASSERT(namelen1
== namelen2
);
2855 ASSERT(memcmp(name1
, name2
, namelen1
) == 0);
2858 ASSERT(entry1
->flags
& XFS_ATTR_INCOMPLETE
);
2859 ASSERT((entry2
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2861 entry1
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2862 xfs_trans_log_buf(args
->trans
, bp1
,
2863 XFS_DA_LOGRANGE(leaf1
, entry1
, sizeof(*entry1
)));
2864 if (args
->rmtblkno
) {
2865 ASSERT((entry1
->flags
& XFS_ATTR_LOCAL
) == 0);
2866 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2867 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2868 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2869 xfs_trans_log_buf(args
->trans
, bp1
,
2870 XFS_DA_LOGRANGE(leaf1
, name_rmt
, sizeof(*name_rmt
)));
2873 entry2
->flags
|= XFS_ATTR_INCOMPLETE
;
2874 xfs_trans_log_buf(args
->trans
, bp2
,
2875 XFS_DA_LOGRANGE(leaf2
, entry2
, sizeof(*entry2
)));
2876 if ((entry2
->flags
& XFS_ATTR_LOCAL
) == 0) {
2877 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2878 name_rmt
->valueblk
= 0;
2879 name_rmt
->valuelen
= 0;
2880 xfs_trans_log_buf(args
->trans
, bp2
,
2881 XFS_DA_LOGRANGE(leaf2
, name_rmt
, sizeof(*name_rmt
)));
2885 * Commit the flag value change and start the next trans in series.
2887 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);