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
);
826 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
829 error
= xfs_da_grow_inode(args
, &blkno
);
832 * If we hit an IO error middle of the transaction inside
833 * grow_inode(), we may have inconsistent data. Bail out.
837 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
838 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
843 error
= xfs_attr3_leaf_create(args
, blkno
, &bp
);
845 /* xfs_attr3_leaf_create may not have instantiated a block */
846 if (bp
&& (xfs_da_shrink_inode(args
, 0, bp
) != 0))
848 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
849 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
853 memset((char *)&nargs
, 0, sizeof(nargs
));
855 nargs
.geo
= args
->geo
;
856 nargs
.total
= args
->total
;
857 nargs
.whichfork
= XFS_ATTR_FORK
;
858 nargs
.trans
= args
->trans
;
859 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
862 for (i
= 0; i
< sf
->hdr
.count
; i
++) {
863 nargs
.name
= sfe
->nameval
;
864 nargs
.namelen
= sfe
->namelen
;
865 nargs
.value
= &sfe
->nameval
[nargs
.namelen
];
866 nargs
.valuelen
= sfe
->valuelen
;
867 nargs
.hashval
= xfs_da_hashname(sfe
->nameval
,
869 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe
->flags
);
870 error
= xfs_attr3_leaf_lookup_int(bp
, &nargs
); /* set a->index */
871 ASSERT(error
== -ENOATTR
);
872 error
= xfs_attr3_leaf_add(bp
, &nargs
);
873 ASSERT(error
!= -ENOSPC
);
876 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
881 kmem_free(tmpbuffer
);
886 * Check a leaf attribute block to see if all the entries would fit into
887 * a shortform attribute list.
890 xfs_attr_shortform_allfit(
892 struct xfs_inode
*dp
)
894 struct xfs_attr_leafblock
*leaf
;
895 struct xfs_attr_leaf_entry
*entry
;
896 xfs_attr_leaf_name_local_t
*name_loc
;
897 struct xfs_attr3_icleaf_hdr leafhdr
;
900 struct xfs_mount
*mp
= bp
->b_mount
;
903 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &leafhdr
, leaf
);
904 entry
= xfs_attr3_leaf_entryp(leaf
);
906 bytes
= sizeof(struct xfs_attr_sf_hdr
);
907 for (i
= 0; i
< leafhdr
.count
; entry
++, i
++) {
908 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
909 continue; /* don't copy partial entries */
910 if (!(entry
->flags
& XFS_ATTR_LOCAL
))
912 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
913 if (name_loc
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
915 if (be16_to_cpu(name_loc
->valuelen
) >= XFS_ATTR_SF_ENTSIZE_MAX
)
917 bytes
+= sizeof(struct xfs_attr_sf_entry
) - 1
919 + be16_to_cpu(name_loc
->valuelen
);
921 if ((dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) &&
922 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
923 (bytes
== sizeof(struct xfs_attr_sf_hdr
)))
925 return xfs_attr_shortform_bytesfit(dp
, bytes
);
928 /* Verify the consistency of an inline attribute fork. */
930 xfs_attr_shortform_verify(
931 struct xfs_inode
*ip
)
933 struct xfs_attr_shortform
*sfp
;
934 struct xfs_attr_sf_entry
*sfep
;
935 struct xfs_attr_sf_entry
*next_sfep
;
937 struct xfs_ifork
*ifp
;
941 ASSERT(ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
);
942 ifp
= XFS_IFORK_PTR(ip
, XFS_ATTR_FORK
);
943 sfp
= (struct xfs_attr_shortform
*)ifp
->if_u1
.if_data
;
944 size
= ifp
->if_bytes
;
947 * Give up if the attribute is way too short.
949 if (size
< sizeof(struct xfs_attr_sf_hdr
))
950 return __this_address
;
952 endp
= (char *)sfp
+ size
;
954 /* Check all reported entries */
955 sfep
= &sfp
->list
[0];
956 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
958 * struct xfs_attr_sf_entry has a variable length.
959 * Check the fixed-offset parts of the structure are
960 * within the data buffer.
962 if (((char *)sfep
+ sizeof(*sfep
)) >= endp
)
963 return __this_address
;
965 /* Don't allow names with known bad length. */
966 if (sfep
->namelen
== 0)
967 return __this_address
;
970 * Check that the variable-length part of the structure is
971 * within the data buffer. The next entry starts after the
972 * name component, so nextentry is an acceptable test.
974 next_sfep
= XFS_ATTR_SF_NEXTENTRY(sfep
);
975 if ((char *)next_sfep
> endp
)
976 return __this_address
;
979 * Check for unknown flags. Short form doesn't support
980 * the incomplete or local bits, so we can use the namespace
983 if (sfep
->flags
& ~XFS_ATTR_NSP_ONDISK_MASK
)
984 return __this_address
;
987 * Check for invalid namespace combinations. We only allow
988 * one namespace flag per xattr, so we can just count the
989 * bits (i.e. hweight) here.
991 if (hweight8(sfep
->flags
& XFS_ATTR_NSP_ONDISK_MASK
) > 1)
992 return __this_address
;
996 if ((void *)sfep
!= (void *)endp
)
997 return __this_address
;
1003 * Convert a leaf attribute list to shortform attribute list
1006 xfs_attr3_leaf_to_shortform(
1008 struct xfs_da_args
*args
,
1011 struct xfs_attr_leafblock
*leaf
;
1012 struct xfs_attr3_icleaf_hdr ichdr
;
1013 struct xfs_attr_leaf_entry
*entry
;
1014 struct xfs_attr_leaf_name_local
*name_loc
;
1015 struct xfs_da_args nargs
;
1016 struct xfs_inode
*dp
= args
->dp
;
1021 trace_xfs_attr_leaf_to_sf(args
);
1023 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, 0);
1027 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1029 leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1030 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1031 entry
= xfs_attr3_leaf_entryp(leaf
);
1033 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1034 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1037 * Clean out the prior contents of the attribute list.
1039 error
= xfs_da_shrink_inode(args
, 0, bp
);
1043 if (forkoff
== -1) {
1044 ASSERT(dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
);
1045 ASSERT(dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
);
1046 xfs_attr_fork_remove(dp
, args
->trans
);
1050 xfs_attr_shortform_create(args
);
1053 * Copy the attributes
1055 memset((char *)&nargs
, 0, sizeof(nargs
));
1056 nargs
.geo
= args
->geo
;
1058 nargs
.total
= args
->total
;
1059 nargs
.whichfork
= XFS_ATTR_FORK
;
1060 nargs
.trans
= args
->trans
;
1061 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
1063 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
1064 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
1065 continue; /* don't copy partial entries */
1066 if (!entry
->nameidx
)
1068 ASSERT(entry
->flags
& XFS_ATTR_LOCAL
);
1069 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
1070 nargs
.name
= name_loc
->nameval
;
1071 nargs
.namelen
= name_loc
->namelen
;
1072 nargs
.value
= &name_loc
->nameval
[nargs
.namelen
];
1073 nargs
.valuelen
= be16_to_cpu(name_loc
->valuelen
);
1074 nargs
.hashval
= be32_to_cpu(entry
->hashval
);
1075 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(entry
->flags
);
1076 xfs_attr_shortform_add(&nargs
, forkoff
);
1081 kmem_free(tmpbuffer
);
1086 * Convert from using a single leaf to a root node and a leaf.
1089 xfs_attr3_leaf_to_node(
1090 struct xfs_da_args
*args
)
1092 struct xfs_attr_leafblock
*leaf
;
1093 struct xfs_attr3_icleaf_hdr icleafhdr
;
1094 struct xfs_attr_leaf_entry
*entries
;
1095 struct xfs_da_node_entry
*btree
;
1096 struct xfs_da3_icnode_hdr icnodehdr
;
1097 struct xfs_da_intnode
*node
;
1098 struct xfs_inode
*dp
= args
->dp
;
1099 struct xfs_mount
*mp
= dp
->i_mount
;
1100 struct xfs_buf
*bp1
= NULL
;
1101 struct xfs_buf
*bp2
= NULL
;
1105 trace_xfs_attr_leaf_to_node(args
);
1107 error
= xfs_da_grow_inode(args
, &blkno
);
1110 error
= xfs_attr3_leaf_read(args
->trans
, dp
, 0, -1, &bp1
);
1114 error
= xfs_da_get_buf(args
->trans
, dp
, blkno
, -1, &bp2
, XFS_ATTR_FORK
);
1118 /* copy leaf to new buffer, update identifiers */
1119 xfs_trans_buf_set_type(args
->trans
, bp2
, XFS_BLFT_ATTR_LEAF_BUF
);
1120 bp2
->b_ops
= bp1
->b_ops
;
1121 memcpy(bp2
->b_addr
, bp1
->b_addr
, args
->geo
->blksize
);
1122 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1123 struct xfs_da3_blkinfo
*hdr3
= bp2
->b_addr
;
1124 hdr3
->blkno
= cpu_to_be64(bp2
->b_bn
);
1126 xfs_trans_log_buf(args
->trans
, bp2
, 0, args
->geo
->blksize
- 1);
1129 * Set up the new root node.
1131 error
= xfs_da3_node_create(args
, 0, 1, &bp1
, XFS_ATTR_FORK
);
1135 dp
->d_ops
->node_hdr_from_disk(&icnodehdr
, node
);
1136 btree
= dp
->d_ops
->node_tree_p(node
);
1139 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &icleafhdr
, leaf
);
1140 entries
= xfs_attr3_leaf_entryp(leaf
);
1142 /* both on-disk, don't endian-flip twice */
1143 btree
[0].hashval
= entries
[icleafhdr
.count
- 1].hashval
;
1144 btree
[0].before
= cpu_to_be32(blkno
);
1145 icnodehdr
.count
= 1;
1146 dp
->d_ops
->node_hdr_to_disk(node
, &icnodehdr
);
1147 xfs_trans_log_buf(args
->trans
, bp1
, 0, args
->geo
->blksize
- 1);
1153 /*========================================================================
1154 * Routines used for growing the Btree.
1155 *========================================================================*/
1158 * Create the initial contents of a leaf attribute list
1159 * or a leaf in a node attribute list.
1162 xfs_attr3_leaf_create(
1163 struct xfs_da_args
*args
,
1165 struct xfs_buf
**bpp
)
1167 struct xfs_attr_leafblock
*leaf
;
1168 struct xfs_attr3_icleaf_hdr ichdr
;
1169 struct xfs_inode
*dp
= args
->dp
;
1170 struct xfs_mount
*mp
= dp
->i_mount
;
1174 trace_xfs_attr_leaf_create(args
);
1176 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp
,
1180 bp
->b_ops
= &xfs_attr3_leaf_buf_ops
;
1181 xfs_trans_buf_set_type(args
->trans
, bp
, XFS_BLFT_ATTR_LEAF_BUF
);
1183 memset(leaf
, 0, args
->geo
->blksize
);
1185 memset(&ichdr
, 0, sizeof(ichdr
));
1186 ichdr
.firstused
= args
->geo
->blksize
;
1188 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1189 struct xfs_da3_blkinfo
*hdr3
= bp
->b_addr
;
1191 ichdr
.magic
= XFS_ATTR3_LEAF_MAGIC
;
1193 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
1194 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
1195 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
1197 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr3_leaf_hdr
);
1199 ichdr
.magic
= XFS_ATTR_LEAF_MAGIC
;
1200 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr_leaf_hdr
);
1202 ichdr
.freemap
[0].size
= ichdr
.firstused
- ichdr
.freemap
[0].base
;
1204 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1205 xfs_trans_log_buf(args
->trans
, bp
, 0, args
->geo
->blksize
- 1);
1212 * Split the leaf node, rebalance, then add the new entry.
1215 xfs_attr3_leaf_split(
1216 struct xfs_da_state
*state
,
1217 struct xfs_da_state_blk
*oldblk
,
1218 struct xfs_da_state_blk
*newblk
)
1223 trace_xfs_attr_leaf_split(state
->args
);
1226 * Allocate space for a new leaf node.
1228 ASSERT(oldblk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1229 error
= xfs_da_grow_inode(state
->args
, &blkno
);
1232 error
= xfs_attr3_leaf_create(state
->args
, blkno
, &newblk
->bp
);
1235 newblk
->blkno
= blkno
;
1236 newblk
->magic
= XFS_ATTR_LEAF_MAGIC
;
1239 * Rebalance the entries across the two leaves.
1240 * NOTE: rebalance() currently depends on the 2nd block being empty.
1242 xfs_attr3_leaf_rebalance(state
, oldblk
, newblk
);
1243 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1248 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1249 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1250 * "new" attrs info. Will need the "old" info to remove it later.
1252 * Insert the "new" entry in the correct block.
1254 if (state
->inleaf
) {
1255 trace_xfs_attr_leaf_add_old(state
->args
);
1256 error
= xfs_attr3_leaf_add(oldblk
->bp
, state
->args
);
1258 trace_xfs_attr_leaf_add_new(state
->args
);
1259 error
= xfs_attr3_leaf_add(newblk
->bp
, state
->args
);
1263 * Update last hashval in each block since we added the name.
1265 oldblk
->hashval
= xfs_attr_leaf_lasthash(oldblk
->bp
, NULL
);
1266 newblk
->hashval
= xfs_attr_leaf_lasthash(newblk
->bp
, NULL
);
1271 * Add a name to the leaf attribute list structure.
1276 struct xfs_da_args
*args
)
1278 struct xfs_attr_leafblock
*leaf
;
1279 struct xfs_attr3_icleaf_hdr ichdr
;
1286 trace_xfs_attr_leaf_add(args
);
1289 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1290 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
.count
);
1291 entsize
= xfs_attr_leaf_newentsize(args
, NULL
);
1294 * Search through freemap for first-fit on new name length.
1295 * (may need to figure in size of entry struct too)
1297 tablesize
= (ichdr
.count
+ 1) * sizeof(xfs_attr_leaf_entry_t
)
1298 + xfs_attr3_leaf_hdr_size(leaf
);
1299 for (sum
= 0, i
= XFS_ATTR_LEAF_MAPSIZE
- 1; i
>= 0; i
--) {
1300 if (tablesize
> ichdr
.firstused
) {
1301 sum
+= ichdr
.freemap
[i
].size
;
1304 if (!ichdr
.freemap
[i
].size
)
1305 continue; /* no space in this map */
1307 if (ichdr
.freemap
[i
].base
< ichdr
.firstused
)
1308 tmp
+= sizeof(xfs_attr_leaf_entry_t
);
1309 if (ichdr
.freemap
[i
].size
>= tmp
) {
1310 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, i
);
1313 sum
+= ichdr
.freemap
[i
].size
;
1317 * If there are no holes in the address space of the block,
1318 * and we don't have enough freespace, then compaction will do us
1319 * no good and we should just give up.
1321 if (!ichdr
.holes
&& sum
< entsize
)
1325 * Compact the entries to coalesce free space.
1326 * This may change the hdr->count via dropping INCOMPLETE entries.
1328 xfs_attr3_leaf_compact(args
, &ichdr
, bp
);
1331 * After compaction, the block is guaranteed to have only one
1332 * free region, in freemap[0]. If it is not big enough, give up.
1334 if (ichdr
.freemap
[0].size
< (entsize
+ sizeof(xfs_attr_leaf_entry_t
))) {
1339 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, 0);
1342 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1343 xfs_trans_log_buf(args
->trans
, bp
,
1344 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
1345 xfs_attr3_leaf_hdr_size(leaf
)));
1350 * Add a name to a leaf attribute list structure.
1353 xfs_attr3_leaf_add_work(
1355 struct xfs_attr3_icleaf_hdr
*ichdr
,
1356 struct xfs_da_args
*args
,
1359 struct xfs_attr_leafblock
*leaf
;
1360 struct xfs_attr_leaf_entry
*entry
;
1361 struct xfs_attr_leaf_name_local
*name_loc
;
1362 struct xfs_attr_leaf_name_remote
*name_rmt
;
1363 struct xfs_mount
*mp
;
1367 trace_xfs_attr_leaf_add_work(args
);
1370 ASSERT(mapindex
>= 0 && mapindex
< XFS_ATTR_LEAF_MAPSIZE
);
1371 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
->count
);
1374 * Force open some space in the entry array and fill it in.
1376 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
1377 if (args
->index
< ichdr
->count
) {
1378 tmp
= ichdr
->count
- args
->index
;
1379 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
1380 memmove(entry
+ 1, entry
, tmp
);
1381 xfs_trans_log_buf(args
->trans
, bp
,
1382 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1387 * Allocate space for the new string (at the end of the run).
1389 mp
= args
->trans
->t_mountp
;
1390 ASSERT(ichdr
->freemap
[mapindex
].base
< args
->geo
->blksize
);
1391 ASSERT((ichdr
->freemap
[mapindex
].base
& 0x3) == 0);
1392 ASSERT(ichdr
->freemap
[mapindex
].size
>=
1393 xfs_attr_leaf_newentsize(args
, NULL
));
1394 ASSERT(ichdr
->freemap
[mapindex
].size
< args
->geo
->blksize
);
1395 ASSERT((ichdr
->freemap
[mapindex
].size
& 0x3) == 0);
1397 ichdr
->freemap
[mapindex
].size
-= xfs_attr_leaf_newentsize(args
, &tmp
);
1399 entry
->nameidx
= cpu_to_be16(ichdr
->freemap
[mapindex
].base
+
1400 ichdr
->freemap
[mapindex
].size
);
1401 entry
->hashval
= cpu_to_be32(args
->hashval
);
1402 entry
->flags
= tmp
? XFS_ATTR_LOCAL
: 0;
1403 entry
->flags
|= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
1404 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1405 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1406 if ((args
->blkno2
== args
->blkno
) &&
1407 (args
->index2
<= args
->index
)) {
1411 xfs_trans_log_buf(args
->trans
, bp
,
1412 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
1413 ASSERT((args
->index
== 0) ||
1414 (be32_to_cpu(entry
->hashval
) >= be32_to_cpu((entry
-1)->hashval
)));
1415 ASSERT((args
->index
== ichdr
->count
- 1) ||
1416 (be32_to_cpu(entry
->hashval
) <= be32_to_cpu((entry
+1)->hashval
)));
1419 * For "remote" attribute values, simply note that we need to
1420 * allocate space for the "remote" value. We can't actually
1421 * allocate the extents in this transaction, and we can't decide
1422 * which blocks they should be as we might allocate more blocks
1423 * as part of this transaction (a split operation for example).
1425 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1426 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
1427 name_loc
->namelen
= args
->namelen
;
1428 name_loc
->valuelen
= cpu_to_be16(args
->valuelen
);
1429 memcpy((char *)name_loc
->nameval
, args
->name
, args
->namelen
);
1430 memcpy((char *)&name_loc
->nameval
[args
->namelen
], args
->value
,
1431 be16_to_cpu(name_loc
->valuelen
));
1433 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
1434 name_rmt
->namelen
= args
->namelen
;
1435 memcpy((char *)name_rmt
->name
, args
->name
, args
->namelen
);
1436 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1438 name_rmt
->valuelen
= 0;
1439 name_rmt
->valueblk
= 0;
1441 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
1442 args
->rmtvaluelen
= args
->valuelen
;
1444 xfs_trans_log_buf(args
->trans
, bp
,
1445 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
1446 xfs_attr_leaf_entsize(leaf
, args
->index
)));
1449 * Update the control info for this leaf node
1451 if (be16_to_cpu(entry
->nameidx
) < ichdr
->firstused
)
1452 ichdr
->firstused
= be16_to_cpu(entry
->nameidx
);
1454 ASSERT(ichdr
->firstused
>= ichdr
->count
* sizeof(xfs_attr_leaf_entry_t
)
1455 + xfs_attr3_leaf_hdr_size(leaf
));
1456 tmp
= (ichdr
->count
- 1) * sizeof(xfs_attr_leaf_entry_t
)
1457 + xfs_attr3_leaf_hdr_size(leaf
);
1459 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
1460 if (ichdr
->freemap
[i
].base
== tmp
) {
1461 ichdr
->freemap
[i
].base
+= sizeof(xfs_attr_leaf_entry_t
);
1462 ichdr
->freemap
[i
].size
-= sizeof(xfs_attr_leaf_entry_t
);
1465 ichdr
->usedbytes
+= xfs_attr_leaf_entsize(leaf
, args
->index
);
1470 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1473 xfs_attr3_leaf_compact(
1474 struct xfs_da_args
*args
,
1475 struct xfs_attr3_icleaf_hdr
*ichdr_dst
,
1478 struct xfs_attr_leafblock
*leaf_src
;
1479 struct xfs_attr_leafblock
*leaf_dst
;
1480 struct xfs_attr3_icleaf_hdr ichdr_src
;
1481 struct xfs_trans
*trans
= args
->trans
;
1484 trace_xfs_attr_leaf_compact(args
);
1486 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, 0);
1487 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1488 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1489 leaf_src
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1490 leaf_dst
= bp
->b_addr
;
1493 * Copy the on-disk header back into the destination buffer to ensure
1494 * all the information in the header that is not part of the incore
1495 * header structure is preserved.
1497 memcpy(bp
->b_addr
, tmpbuffer
, xfs_attr3_leaf_hdr_size(leaf_src
));
1499 /* Initialise the incore headers */
1500 ichdr_src
= *ichdr_dst
; /* struct copy */
1501 ichdr_dst
->firstused
= args
->geo
->blksize
;
1502 ichdr_dst
->usedbytes
= 0;
1503 ichdr_dst
->count
= 0;
1504 ichdr_dst
->holes
= 0;
1505 ichdr_dst
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_src
);
1506 ichdr_dst
->freemap
[0].size
= ichdr_dst
->firstused
-
1507 ichdr_dst
->freemap
[0].base
;
1509 /* write the header back to initialise the underlying buffer */
1510 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf_dst
, ichdr_dst
);
1513 * Copy all entry's in the same (sorted) order,
1514 * but allocate name/value pairs packed and in sequence.
1516 xfs_attr3_leaf_moveents(args
, leaf_src
, &ichdr_src
, 0,
1517 leaf_dst
, ichdr_dst
, 0, ichdr_src
.count
);
1519 * this logs the entire buffer, but the caller must write the header
1520 * back to the buffer when it is finished modifying it.
1522 xfs_trans_log_buf(trans
, bp
, 0, args
->geo
->blksize
- 1);
1524 kmem_free(tmpbuffer
);
1528 * Compare two leaf blocks "order".
1529 * Return 0 unless leaf2 should go before leaf1.
1532 xfs_attr3_leaf_order(
1533 struct xfs_buf
*leaf1_bp
,
1534 struct xfs_attr3_icleaf_hdr
*leaf1hdr
,
1535 struct xfs_buf
*leaf2_bp
,
1536 struct xfs_attr3_icleaf_hdr
*leaf2hdr
)
1538 struct xfs_attr_leaf_entry
*entries1
;
1539 struct xfs_attr_leaf_entry
*entries2
;
1541 entries1
= xfs_attr3_leaf_entryp(leaf1_bp
->b_addr
);
1542 entries2
= xfs_attr3_leaf_entryp(leaf2_bp
->b_addr
);
1543 if (leaf1hdr
->count
> 0 && leaf2hdr
->count
> 0 &&
1544 ((be32_to_cpu(entries2
[0].hashval
) <
1545 be32_to_cpu(entries1
[0].hashval
)) ||
1546 (be32_to_cpu(entries2
[leaf2hdr
->count
- 1].hashval
) <
1547 be32_to_cpu(entries1
[leaf1hdr
->count
- 1].hashval
)))) {
1554 xfs_attr_leaf_order(
1555 struct xfs_buf
*leaf1_bp
,
1556 struct xfs_buf
*leaf2_bp
)
1558 struct xfs_attr3_icleaf_hdr ichdr1
;
1559 struct xfs_attr3_icleaf_hdr ichdr2
;
1560 struct xfs_mount
*mp
= leaf1_bp
->b_mount
;
1562 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr1
, leaf1_bp
->b_addr
);
1563 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr2
, leaf2_bp
->b_addr
);
1564 return xfs_attr3_leaf_order(leaf1_bp
, &ichdr1
, leaf2_bp
, &ichdr2
);
1568 * Redistribute the attribute list entries between two leaf nodes,
1569 * taking into account the size of the new entry.
1571 * NOTE: if new block is empty, then it will get the upper half of the
1572 * old block. At present, all (one) callers pass in an empty second block.
1574 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1575 * to match what it is doing in splitting the attribute leaf block. Those
1576 * values are used in "atomic rename" operations on attributes. Note that
1577 * the "new" and "old" values can end up in different blocks.
1580 xfs_attr3_leaf_rebalance(
1581 struct xfs_da_state
*state
,
1582 struct xfs_da_state_blk
*blk1
,
1583 struct xfs_da_state_blk
*blk2
)
1585 struct xfs_da_args
*args
;
1586 struct xfs_attr_leafblock
*leaf1
;
1587 struct xfs_attr_leafblock
*leaf2
;
1588 struct xfs_attr3_icleaf_hdr ichdr1
;
1589 struct xfs_attr3_icleaf_hdr ichdr2
;
1590 struct xfs_attr_leaf_entry
*entries1
;
1591 struct xfs_attr_leaf_entry
*entries2
;
1599 * Set up environment.
1601 ASSERT(blk1
->magic
== XFS_ATTR_LEAF_MAGIC
);
1602 ASSERT(blk2
->magic
== XFS_ATTR_LEAF_MAGIC
);
1603 leaf1
= blk1
->bp
->b_addr
;
1604 leaf2
= blk2
->bp
->b_addr
;
1605 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr1
, leaf1
);
1606 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, leaf2
);
1607 ASSERT(ichdr2
.count
== 0);
1610 trace_xfs_attr_leaf_rebalance(args
);
1613 * Check ordering of blocks, reverse if it makes things simpler.
1615 * NOTE: Given that all (current) callers pass in an empty
1616 * second block, this code should never set "swap".
1619 if (xfs_attr3_leaf_order(blk1
->bp
, &ichdr1
, blk2
->bp
, &ichdr2
)) {
1622 /* swap structures rather than reconverting them */
1623 swap(ichdr1
, ichdr2
);
1625 leaf1
= blk1
->bp
->b_addr
;
1626 leaf2
= blk2
->bp
->b_addr
;
1631 * Examine entries until we reduce the absolute difference in
1632 * byte usage between the two blocks to a minimum. Then get
1633 * the direction to copy and the number of elements to move.
1635 * "inleaf" is true if the new entry should be inserted into blk1.
1636 * If "swap" is also true, then reverse the sense of "inleaf".
1638 state
->inleaf
= xfs_attr3_leaf_figure_balance(state
, blk1
, &ichdr1
,
1642 state
->inleaf
= !state
->inleaf
;
1645 * Move any entries required from leaf to leaf:
1647 if (count
< ichdr1
.count
) {
1649 * Figure the total bytes to be added to the destination leaf.
1651 /* number entries being moved */
1652 count
= ichdr1
.count
- count
;
1653 space
= ichdr1
.usedbytes
- totallen
;
1654 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1657 * leaf2 is the destination, compact it if it looks tight.
1659 max
= ichdr2
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1660 max
-= ichdr2
.count
* sizeof(xfs_attr_leaf_entry_t
);
1662 xfs_attr3_leaf_compact(args
, &ichdr2
, blk2
->bp
);
1665 * Move high entries from leaf1 to low end of leaf2.
1667 xfs_attr3_leaf_moveents(args
, leaf1
, &ichdr1
,
1668 ichdr1
.count
- count
, leaf2
, &ichdr2
, 0, count
);
1670 } else if (count
> ichdr1
.count
) {
1672 * I assert that since all callers pass in an empty
1673 * second buffer, this code should never execute.
1678 * Figure the total bytes to be added to the destination leaf.
1680 /* number entries being moved */
1681 count
-= ichdr1
.count
;
1682 space
= totallen
- ichdr1
.usedbytes
;
1683 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1686 * leaf1 is the destination, compact it if it looks tight.
1688 max
= ichdr1
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1689 max
-= ichdr1
.count
* sizeof(xfs_attr_leaf_entry_t
);
1691 xfs_attr3_leaf_compact(args
, &ichdr1
, blk1
->bp
);
1694 * Move low entries from leaf2 to high end of leaf1.
1696 xfs_attr3_leaf_moveents(args
, leaf2
, &ichdr2
, 0, leaf1
, &ichdr1
,
1697 ichdr1
.count
, count
);
1700 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf1
, &ichdr1
);
1701 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf2
, &ichdr2
);
1702 xfs_trans_log_buf(args
->trans
, blk1
->bp
, 0, args
->geo
->blksize
- 1);
1703 xfs_trans_log_buf(args
->trans
, blk2
->bp
, 0, args
->geo
->blksize
- 1);
1706 * Copy out last hashval in each block for B-tree code.
1708 entries1
= xfs_attr3_leaf_entryp(leaf1
);
1709 entries2
= xfs_attr3_leaf_entryp(leaf2
);
1710 blk1
->hashval
= be32_to_cpu(entries1
[ichdr1
.count
- 1].hashval
);
1711 blk2
->hashval
= be32_to_cpu(entries2
[ichdr2
.count
- 1].hashval
);
1714 * Adjust the expected index for insertion.
1715 * NOTE: this code depends on the (current) situation that the
1716 * second block was originally empty.
1718 * If the insertion point moved to the 2nd block, we must adjust
1719 * the index. We must also track the entry just following the
1720 * new entry for use in an "atomic rename" operation, that entry
1721 * is always the "old" entry and the "new" entry is what we are
1722 * inserting. The index/blkno fields refer to the "old" entry,
1723 * while the index2/blkno2 fields refer to the "new" entry.
1725 if (blk1
->index
> ichdr1
.count
) {
1726 ASSERT(state
->inleaf
== 0);
1727 blk2
->index
= blk1
->index
- ichdr1
.count
;
1728 args
->index
= args
->index2
= blk2
->index
;
1729 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1730 } else if (blk1
->index
== ichdr1
.count
) {
1731 if (state
->inleaf
) {
1732 args
->index
= blk1
->index
;
1733 args
->blkno
= blk1
->blkno
;
1735 args
->blkno2
= blk2
->blkno
;
1738 * On a double leaf split, the original attr location
1739 * is already stored in blkno2/index2, so don't
1740 * overwrite it overwise we corrupt the tree.
1742 blk2
->index
= blk1
->index
- ichdr1
.count
;
1743 args
->index
= blk2
->index
;
1744 args
->blkno
= blk2
->blkno
;
1745 if (!state
->extravalid
) {
1747 * set the new attr location to match the old
1748 * one and let the higher level split code
1749 * decide where in the leaf to place it.
1751 args
->index2
= blk2
->index
;
1752 args
->blkno2
= blk2
->blkno
;
1756 ASSERT(state
->inleaf
== 1);
1757 args
->index
= args
->index2
= blk1
->index
;
1758 args
->blkno
= args
->blkno2
= blk1
->blkno
;
1763 * Examine entries until we reduce the absolute difference in
1764 * byte usage between the two blocks to a minimum.
1765 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1766 * GROT: there will always be enough room in either block for a new entry.
1767 * GROT: Do a double-split for this case?
1770 xfs_attr3_leaf_figure_balance(
1771 struct xfs_da_state
*state
,
1772 struct xfs_da_state_blk
*blk1
,
1773 struct xfs_attr3_icleaf_hdr
*ichdr1
,
1774 struct xfs_da_state_blk
*blk2
,
1775 struct xfs_attr3_icleaf_hdr
*ichdr2
,
1779 struct xfs_attr_leafblock
*leaf1
= blk1
->bp
->b_addr
;
1780 struct xfs_attr_leafblock
*leaf2
= blk2
->bp
->b_addr
;
1781 struct xfs_attr_leaf_entry
*entry
;
1792 * Examine entries until we reduce the absolute difference in
1793 * byte usage between the two blocks to a minimum.
1795 max
= ichdr1
->count
+ ichdr2
->count
;
1796 half
= (max
+ 1) * sizeof(*entry
);
1797 half
+= ichdr1
->usedbytes
+ ichdr2
->usedbytes
+
1798 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1800 lastdelta
= state
->args
->geo
->blksize
;
1801 entry
= xfs_attr3_leaf_entryp(leaf1
);
1802 for (count
= index
= 0; count
< max
; entry
++, index
++, count
++) {
1804 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1806 * The new entry is in the first block, account for it.
1808 if (count
== blk1
->index
) {
1809 tmp
= totallen
+ sizeof(*entry
) +
1810 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1811 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1813 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1819 * Wrap around into the second block if necessary.
1821 if (count
== ichdr1
->count
) {
1823 entry
= xfs_attr3_leaf_entryp(leaf1
);
1828 * Figure out if next leaf entry would be too much.
1830 tmp
= totallen
+ sizeof(*entry
) + xfs_attr_leaf_entsize(leaf1
,
1832 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1834 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1840 * Calculate the number of usedbytes that will end up in lower block.
1841 * If new entry not in lower block, fix up the count.
1843 totallen
-= count
* sizeof(*entry
);
1845 totallen
-= sizeof(*entry
) +
1846 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1850 *usedbytesarg
= totallen
;
1854 /*========================================================================
1855 * Routines used for shrinking the Btree.
1856 *========================================================================*/
1859 * Check a leaf block and its neighbors to see if the block should be
1860 * collapsed into one or the other neighbor. Always keep the block
1861 * with the smaller block number.
1862 * If the current block is over 50% full, don't try to join it, return 0.
1863 * If the block is empty, fill in the state structure and return 2.
1864 * If it can be collapsed, fill in the state structure and return 1.
1865 * If nothing can be done, return 0.
1867 * GROT: allow for INCOMPLETE entries in calculation.
1870 xfs_attr3_leaf_toosmall(
1871 struct xfs_da_state
*state
,
1874 struct xfs_attr_leafblock
*leaf
;
1875 struct xfs_da_state_blk
*blk
;
1876 struct xfs_attr3_icleaf_hdr ichdr
;
1885 trace_xfs_attr_leaf_toosmall(state
->args
);
1888 * Check for the degenerate case of the block being over 50% full.
1889 * If so, it's not worth even looking to see if we might be able
1890 * to coalesce with a sibling.
1892 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1893 leaf
= blk
->bp
->b_addr
;
1894 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr
, leaf
);
1895 bytes
= xfs_attr3_leaf_hdr_size(leaf
) +
1896 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
) +
1898 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1899 *action
= 0; /* blk over 50%, don't try to join */
1904 * Check for the degenerate case of the block being empty.
1905 * If the block is empty, we'll simply delete it, no need to
1906 * coalesce it with a sibling block. We choose (arbitrarily)
1907 * to merge with the forward block unless it is NULL.
1909 if (ichdr
.count
== 0) {
1911 * Make altpath point to the block we want to keep and
1912 * path point to the block we want to drop (this one).
1914 forward
= (ichdr
.forw
!= 0);
1915 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1916 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1929 * Examine each sibling block to see if we can coalesce with
1930 * at least 25% free space to spare. We need to figure out
1931 * whether to merge with the forward or the backward block.
1932 * We prefer coalescing with the lower numbered sibling so as
1933 * to shrink an attribute list over time.
1935 /* start with smaller blk num */
1936 forward
= ichdr
.forw
< ichdr
.back
;
1937 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1938 struct xfs_attr3_icleaf_hdr ichdr2
;
1945 error
= xfs_attr3_leaf_read(state
->args
->trans
, state
->args
->dp
,
1950 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, bp
->b_addr
);
1952 bytes
= state
->args
->geo
->blksize
-
1953 (state
->args
->geo
->blksize
>> 2) -
1954 ichdr
.usedbytes
- ichdr2
.usedbytes
-
1955 ((ichdr
.count
+ ichdr2
.count
) *
1956 sizeof(xfs_attr_leaf_entry_t
)) -
1957 xfs_attr3_leaf_hdr_size(leaf
);
1959 xfs_trans_brelse(state
->args
->trans
, bp
);
1961 break; /* fits with at least 25% to spare */
1969 * Make altpath point to the block we want to keep (the lower
1970 * numbered block) and path point to the block we want to drop.
1972 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1973 if (blkno
< blk
->blkno
) {
1974 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1977 error
= xfs_da3_path_shift(state
, &state
->path
, forward
,
1991 * Remove a name from the leaf attribute list structure.
1993 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1994 * If two leaves are 37% full, when combined they will leave 25% free.
1997 xfs_attr3_leaf_remove(
1999 struct xfs_da_args
*args
)
2001 struct xfs_attr_leafblock
*leaf
;
2002 struct xfs_attr3_icleaf_hdr ichdr
;
2003 struct xfs_attr_leaf_entry
*entry
;
2012 trace_xfs_attr_leaf_remove(args
);
2015 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2017 ASSERT(ichdr
.count
> 0 && ichdr
.count
< args
->geo
->blksize
/ 8);
2018 ASSERT(args
->index
>= 0 && args
->index
< ichdr
.count
);
2019 ASSERT(ichdr
.firstused
>= ichdr
.count
* sizeof(*entry
) +
2020 xfs_attr3_leaf_hdr_size(leaf
));
2022 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2024 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2025 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2028 * Scan through free region table:
2029 * check for adjacency of free'd entry with an existing one,
2030 * find smallest free region in case we need to replace it,
2031 * adjust any map that borders the entry table,
2033 tablesize
= ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
)
2034 + xfs_attr3_leaf_hdr_size(leaf
);
2035 tmp
= ichdr
.freemap
[0].size
;
2036 before
= after
= -1;
2037 smallest
= XFS_ATTR_LEAF_MAPSIZE
- 1;
2038 entsize
= xfs_attr_leaf_entsize(leaf
, args
->index
);
2039 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
2040 ASSERT(ichdr
.freemap
[i
].base
< args
->geo
->blksize
);
2041 ASSERT(ichdr
.freemap
[i
].size
< args
->geo
->blksize
);
2042 if (ichdr
.freemap
[i
].base
== tablesize
) {
2043 ichdr
.freemap
[i
].base
-= sizeof(xfs_attr_leaf_entry_t
);
2044 ichdr
.freemap
[i
].size
+= sizeof(xfs_attr_leaf_entry_t
);
2047 if (ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
==
2048 be16_to_cpu(entry
->nameidx
)) {
2050 } else if (ichdr
.freemap
[i
].base
==
2051 (be16_to_cpu(entry
->nameidx
) + entsize
)) {
2053 } else if (ichdr
.freemap
[i
].size
< tmp
) {
2054 tmp
= ichdr
.freemap
[i
].size
;
2060 * Coalesce adjacent freemap regions,
2061 * or replace the smallest region.
2063 if ((before
>= 0) || (after
>= 0)) {
2064 if ((before
>= 0) && (after
>= 0)) {
2065 ichdr
.freemap
[before
].size
+= entsize
;
2066 ichdr
.freemap
[before
].size
+= ichdr
.freemap
[after
].size
;
2067 ichdr
.freemap
[after
].base
= 0;
2068 ichdr
.freemap
[after
].size
= 0;
2069 } else if (before
>= 0) {
2070 ichdr
.freemap
[before
].size
+= entsize
;
2072 ichdr
.freemap
[after
].base
= be16_to_cpu(entry
->nameidx
);
2073 ichdr
.freemap
[after
].size
+= entsize
;
2077 * Replace smallest region (if it is smaller than free'd entry)
2079 if (ichdr
.freemap
[smallest
].size
< entsize
) {
2080 ichdr
.freemap
[smallest
].base
= be16_to_cpu(entry
->nameidx
);
2081 ichdr
.freemap
[smallest
].size
= entsize
;
2086 * Did we remove the first entry?
2088 if (be16_to_cpu(entry
->nameidx
) == ichdr
.firstused
)
2094 * Compress the remaining entries and zero out the removed stuff.
2096 memset(xfs_attr3_leaf_name(leaf
, args
->index
), 0, entsize
);
2097 ichdr
.usedbytes
-= entsize
;
2098 xfs_trans_log_buf(args
->trans
, bp
,
2099 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
2102 tmp
= (ichdr
.count
- args
->index
) * sizeof(xfs_attr_leaf_entry_t
);
2103 memmove(entry
, entry
+ 1, tmp
);
2105 xfs_trans_log_buf(args
->trans
, bp
,
2106 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(xfs_attr_leaf_entry_t
)));
2108 entry
= &xfs_attr3_leaf_entryp(leaf
)[ichdr
.count
];
2109 memset(entry
, 0, sizeof(xfs_attr_leaf_entry_t
));
2112 * If we removed the first entry, re-find the first used byte
2113 * in the name area. Note that if the entry was the "firstused",
2114 * then we don't have a "hole" in our block resulting from
2115 * removing the name.
2118 tmp
= args
->geo
->blksize
;
2119 entry
= xfs_attr3_leaf_entryp(leaf
);
2120 for (i
= ichdr
.count
- 1; i
>= 0; entry
++, i
--) {
2121 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2122 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2124 if (be16_to_cpu(entry
->nameidx
) < tmp
)
2125 tmp
= be16_to_cpu(entry
->nameidx
);
2127 ichdr
.firstused
= tmp
;
2128 ASSERT(ichdr
.firstused
!= 0);
2130 ichdr
.holes
= 1; /* mark as needing compaction */
2132 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
2133 xfs_trans_log_buf(args
->trans
, bp
,
2134 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
2135 xfs_attr3_leaf_hdr_size(leaf
)));
2138 * Check if leaf is less than 50% full, caller may want to
2139 * "join" the leaf with a sibling if so.
2141 tmp
= ichdr
.usedbytes
+ xfs_attr3_leaf_hdr_size(leaf
) +
2142 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
);
2144 return tmp
< args
->geo
->magicpct
; /* leaf is < 37% full */
2148 * Move all the attribute list entries from drop_leaf into save_leaf.
2151 xfs_attr3_leaf_unbalance(
2152 struct xfs_da_state
*state
,
2153 struct xfs_da_state_blk
*drop_blk
,
2154 struct xfs_da_state_blk
*save_blk
)
2156 struct xfs_attr_leafblock
*drop_leaf
= drop_blk
->bp
->b_addr
;
2157 struct xfs_attr_leafblock
*save_leaf
= save_blk
->bp
->b_addr
;
2158 struct xfs_attr3_icleaf_hdr drophdr
;
2159 struct xfs_attr3_icleaf_hdr savehdr
;
2160 struct xfs_attr_leaf_entry
*entry
;
2162 trace_xfs_attr_leaf_unbalance(state
->args
);
2164 drop_leaf
= drop_blk
->bp
->b_addr
;
2165 save_leaf
= save_blk
->bp
->b_addr
;
2166 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &drophdr
, drop_leaf
);
2167 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &savehdr
, save_leaf
);
2168 entry
= xfs_attr3_leaf_entryp(drop_leaf
);
2171 * Save last hashval from dying block for later Btree fixup.
2173 drop_blk
->hashval
= be32_to_cpu(entry
[drophdr
.count
- 1].hashval
);
2176 * Check if we need a temp buffer, or can we do it in place.
2177 * Note that we don't check "leaf" for holes because we will
2178 * always be dropping it, toosmall() decided that for us already.
2180 if (savehdr
.holes
== 0) {
2182 * dest leaf has no holes, so we add there. May need
2183 * to make some room in the entry array.
2185 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2186 drop_blk
->bp
, &drophdr
)) {
2187 xfs_attr3_leaf_moveents(state
->args
,
2188 drop_leaf
, &drophdr
, 0,
2189 save_leaf
, &savehdr
, 0,
2192 xfs_attr3_leaf_moveents(state
->args
,
2193 drop_leaf
, &drophdr
, 0,
2194 save_leaf
, &savehdr
,
2195 savehdr
.count
, drophdr
.count
);
2199 * Destination has holes, so we make a temporary copy
2200 * of the leaf and add them both to that.
2202 struct xfs_attr_leafblock
*tmp_leaf
;
2203 struct xfs_attr3_icleaf_hdr tmphdr
;
2205 tmp_leaf
= kmem_zalloc(state
->args
->geo
->blksize
, 0);
2208 * Copy the header into the temp leaf so that all the stuff
2209 * not in the incore header is present and gets copied back in
2210 * once we've moved all the entries.
2212 memcpy(tmp_leaf
, save_leaf
, xfs_attr3_leaf_hdr_size(save_leaf
));
2214 memset(&tmphdr
, 0, sizeof(tmphdr
));
2215 tmphdr
.magic
= savehdr
.magic
;
2216 tmphdr
.forw
= savehdr
.forw
;
2217 tmphdr
.back
= savehdr
.back
;
2218 tmphdr
.firstused
= state
->args
->geo
->blksize
;
2220 /* write the header to the temp buffer to initialise it */
2221 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, tmp_leaf
, &tmphdr
);
2223 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2224 drop_blk
->bp
, &drophdr
)) {
2225 xfs_attr3_leaf_moveents(state
->args
,
2226 drop_leaf
, &drophdr
, 0,
2227 tmp_leaf
, &tmphdr
, 0,
2229 xfs_attr3_leaf_moveents(state
->args
,
2230 save_leaf
, &savehdr
, 0,
2231 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2234 xfs_attr3_leaf_moveents(state
->args
,
2235 save_leaf
, &savehdr
, 0,
2236 tmp_leaf
, &tmphdr
, 0,
2238 xfs_attr3_leaf_moveents(state
->args
,
2239 drop_leaf
, &drophdr
, 0,
2240 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2243 memcpy(save_leaf
, tmp_leaf
, state
->args
->geo
->blksize
);
2244 savehdr
= tmphdr
; /* struct copy */
2245 kmem_free(tmp_leaf
);
2248 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, save_leaf
, &savehdr
);
2249 xfs_trans_log_buf(state
->args
->trans
, save_blk
->bp
, 0,
2250 state
->args
->geo
->blksize
- 1);
2253 * Copy out last hashval in each block for B-tree code.
2255 entry
= xfs_attr3_leaf_entryp(save_leaf
);
2256 save_blk
->hashval
= be32_to_cpu(entry
[savehdr
.count
- 1].hashval
);
2259 /*========================================================================
2260 * Routines used for finding things in the Btree.
2261 *========================================================================*/
2264 * Look up a name in a leaf attribute list structure.
2265 * This is the internal routine, it uses the caller's buffer.
2267 * Note that duplicate keys are allowed, but only check within the
2268 * current leaf node. The Btree code must check in adjacent leaf nodes.
2270 * Return in args->index the index into the entry[] array of either
2271 * the found entry, or where the entry should have been (insert before
2274 * Don't change the args->value unless we find the attribute.
2277 xfs_attr3_leaf_lookup_int(
2279 struct xfs_da_args
*args
)
2281 struct xfs_attr_leafblock
*leaf
;
2282 struct xfs_attr3_icleaf_hdr ichdr
;
2283 struct xfs_attr_leaf_entry
*entry
;
2284 struct xfs_attr_leaf_entry
*entries
;
2285 struct xfs_attr_leaf_name_local
*name_loc
;
2286 struct xfs_attr_leaf_name_remote
*name_rmt
;
2287 xfs_dahash_t hashval
;
2291 trace_xfs_attr_leaf_lookup(args
);
2294 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2295 entries
= xfs_attr3_leaf_entryp(leaf
);
2296 if (ichdr
.count
>= args
->geo
->blksize
/ 8)
2297 return -EFSCORRUPTED
;
2300 * Binary search. (note: small blocks will skip this loop)
2302 hashval
= args
->hashval
;
2303 probe
= span
= ichdr
.count
/ 2;
2304 for (entry
= &entries
[probe
]; span
> 4; entry
= &entries
[probe
]) {
2306 if (be32_to_cpu(entry
->hashval
) < hashval
)
2308 else if (be32_to_cpu(entry
->hashval
) > hashval
)
2313 if (!(probe
>= 0 && (!ichdr
.count
|| probe
< ichdr
.count
)))
2314 return -EFSCORRUPTED
;
2315 if (!(span
<= 4 || be32_to_cpu(entry
->hashval
) == hashval
))
2316 return -EFSCORRUPTED
;
2319 * Since we may have duplicate hashval's, find the first matching
2320 * hashval in the leaf.
2322 while (probe
> 0 && be32_to_cpu(entry
->hashval
) >= hashval
) {
2326 while (probe
< ichdr
.count
&&
2327 be32_to_cpu(entry
->hashval
) < hashval
) {
2331 if (probe
== ichdr
.count
|| be32_to_cpu(entry
->hashval
) != hashval
) {
2332 args
->index
= probe
;
2337 * Duplicate keys may be present, so search all of them for a match.
2339 for (; probe
< ichdr
.count
&& (be32_to_cpu(entry
->hashval
) == hashval
);
2342 * GROT: Add code to remove incomplete entries.
2345 * If we are looking for INCOMPLETE entries, show only those.
2346 * If we are looking for complete entries, show only those.
2348 if ((args
->flags
& XFS_ATTR_INCOMPLETE
) !=
2349 (entry
->flags
& XFS_ATTR_INCOMPLETE
)) {
2352 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2353 name_loc
= xfs_attr3_leaf_name_local(leaf
, probe
);
2354 if (name_loc
->namelen
!= args
->namelen
)
2356 if (memcmp(args
->name
, name_loc
->nameval
,
2357 args
->namelen
) != 0)
2359 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2361 args
->index
= probe
;
2364 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, probe
);
2365 if (name_rmt
->namelen
!= args
->namelen
)
2367 if (memcmp(args
->name
, name_rmt
->name
,
2368 args
->namelen
) != 0)
2370 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2372 args
->index
= probe
;
2373 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2374 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2375 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(
2381 args
->index
= probe
;
2386 * Get the value associated with an attribute name from a leaf attribute
2389 * If ATTR_KERNOVAL is specified, only the length needs to be returned.
2390 * Unlike a lookup, we only return an error if the attribute does not
2391 * exist or we can't retrieve the value.
2394 xfs_attr3_leaf_getvalue(
2396 struct xfs_da_args
*args
)
2398 struct xfs_attr_leafblock
*leaf
;
2399 struct xfs_attr3_icleaf_hdr ichdr
;
2400 struct xfs_attr_leaf_entry
*entry
;
2401 struct xfs_attr_leaf_name_local
*name_loc
;
2402 struct xfs_attr_leaf_name_remote
*name_rmt
;
2405 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2406 ASSERT(ichdr
.count
< args
->geo
->blksize
/ 8);
2407 ASSERT(args
->index
< ichdr
.count
);
2409 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2410 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2411 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2412 ASSERT(name_loc
->namelen
== args
->namelen
);
2413 ASSERT(memcmp(args
->name
, name_loc
->nameval
, args
->namelen
) == 0);
2414 return xfs_attr_copy_value(args
,
2415 &name_loc
->nameval
[args
->namelen
],
2416 be16_to_cpu(name_loc
->valuelen
));
2419 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2420 ASSERT(name_rmt
->namelen
== args
->namelen
);
2421 ASSERT(memcmp(args
->name
, name_rmt
->name
, args
->namelen
) == 0);
2422 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2423 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2424 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(args
->dp
->i_mount
,
2426 return xfs_attr_copy_value(args
, NULL
, args
->rmtvaluelen
);
2429 /*========================================================================
2431 *========================================================================*/
2434 * Move the indicated entries from one leaf to another.
2435 * NOTE: this routine modifies both source and destination leaves.
2439 xfs_attr3_leaf_moveents(
2440 struct xfs_da_args
*args
,
2441 struct xfs_attr_leafblock
*leaf_s
,
2442 struct xfs_attr3_icleaf_hdr
*ichdr_s
,
2444 struct xfs_attr_leafblock
*leaf_d
,
2445 struct xfs_attr3_icleaf_hdr
*ichdr_d
,
2449 struct xfs_attr_leaf_entry
*entry_s
;
2450 struct xfs_attr_leaf_entry
*entry_d
;
2456 * Check for nothing to do.
2462 * Set up environment.
2464 ASSERT(ichdr_s
->magic
== XFS_ATTR_LEAF_MAGIC
||
2465 ichdr_s
->magic
== XFS_ATTR3_LEAF_MAGIC
);
2466 ASSERT(ichdr_s
->magic
== ichdr_d
->magic
);
2467 ASSERT(ichdr_s
->count
> 0 && ichdr_s
->count
< args
->geo
->blksize
/ 8);
2468 ASSERT(ichdr_s
->firstused
>= (ichdr_s
->count
* sizeof(*entry_s
))
2469 + xfs_attr3_leaf_hdr_size(leaf_s
));
2470 ASSERT(ichdr_d
->count
< args
->geo
->blksize
/ 8);
2471 ASSERT(ichdr_d
->firstused
>= (ichdr_d
->count
* sizeof(*entry_d
))
2472 + xfs_attr3_leaf_hdr_size(leaf_d
));
2474 ASSERT(start_s
< ichdr_s
->count
);
2475 ASSERT(start_d
<= ichdr_d
->count
);
2476 ASSERT(count
<= ichdr_s
->count
);
2480 * Move the entries in the destination leaf up to make a hole?
2482 if (start_d
< ichdr_d
->count
) {
2483 tmp
= ichdr_d
->count
- start_d
;
2484 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2485 entry_s
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2486 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
+ count
];
2487 memmove(entry_d
, entry_s
, tmp
);
2491 * Copy all entry's in the same (sorted) order,
2492 * but allocate attribute info packed and in sequence.
2494 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2495 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2497 for (i
= 0; i
< count
; entry_s
++, entry_d
++, desti
++, i
++) {
2498 ASSERT(be16_to_cpu(entry_s
->nameidx
) >= ichdr_s
->firstused
);
2499 tmp
= xfs_attr_leaf_entsize(leaf_s
, start_s
+ i
);
2502 * Code to drop INCOMPLETE entries. Difficult to use as we
2503 * may also need to change the insertion index. Code turned
2504 * off for 6.2, should be revisited later.
2506 if (entry_s
->flags
& XFS_ATTR_INCOMPLETE
) { /* skip partials? */
2507 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2508 ichdr_s
->usedbytes
-= tmp
;
2509 ichdr_s
->count
-= 1;
2510 entry_d
--; /* to compensate for ++ in loop hdr */
2512 if ((start_s
+ i
) < offset
)
2513 result
++; /* insertion index adjustment */
2516 ichdr_d
->firstused
-= tmp
;
2517 /* both on-disk, don't endian flip twice */
2518 entry_d
->hashval
= entry_s
->hashval
;
2519 entry_d
->nameidx
= cpu_to_be16(ichdr_d
->firstused
);
2520 entry_d
->flags
= entry_s
->flags
;
2521 ASSERT(be16_to_cpu(entry_d
->nameidx
) + tmp
2522 <= args
->geo
->blksize
);
2523 memmove(xfs_attr3_leaf_name(leaf_d
, desti
),
2524 xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), tmp
);
2525 ASSERT(be16_to_cpu(entry_s
->nameidx
) + tmp
2526 <= args
->geo
->blksize
);
2527 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2528 ichdr_s
->usedbytes
-= tmp
;
2529 ichdr_d
->usedbytes
+= tmp
;
2530 ichdr_s
->count
-= 1;
2531 ichdr_d
->count
+= 1;
2532 tmp
= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
)
2533 + xfs_attr3_leaf_hdr_size(leaf_d
);
2534 ASSERT(ichdr_d
->firstused
>= tmp
);
2541 * Zero out the entries we just copied.
2543 if (start_s
== ichdr_s
->count
) {
2544 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2545 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2546 ASSERT(((char *)entry_s
+ tmp
) <=
2547 ((char *)leaf_s
+ args
->geo
->blksize
));
2548 memset(entry_s
, 0, tmp
);
2551 * Move the remaining entries down to fill the hole,
2552 * then zero the entries at the top.
2554 tmp
= (ichdr_s
->count
- count
) * sizeof(xfs_attr_leaf_entry_t
);
2555 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
+ count
];
2556 entry_d
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2557 memmove(entry_d
, entry_s
, tmp
);
2559 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2560 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[ichdr_s
->count
];
2561 ASSERT(((char *)entry_s
+ tmp
) <=
2562 ((char *)leaf_s
+ args
->geo
->blksize
));
2563 memset(entry_s
, 0, tmp
);
2567 * Fill in the freemap information
2569 ichdr_d
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_d
);
2570 ichdr_d
->freemap
[0].base
+= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
);
2571 ichdr_d
->freemap
[0].size
= ichdr_d
->firstused
- ichdr_d
->freemap
[0].base
;
2572 ichdr_d
->freemap
[1].base
= 0;
2573 ichdr_d
->freemap
[2].base
= 0;
2574 ichdr_d
->freemap
[1].size
= 0;
2575 ichdr_d
->freemap
[2].size
= 0;
2576 ichdr_s
->holes
= 1; /* leaf may not be compact */
2580 * Pick up the last hashvalue from a leaf block.
2583 xfs_attr_leaf_lasthash(
2587 struct xfs_attr3_icleaf_hdr ichdr
;
2588 struct xfs_attr_leaf_entry
*entries
;
2589 struct xfs_mount
*mp
= bp
->b_mount
;
2591 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, bp
->b_addr
);
2592 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
2594 *count
= ichdr
.count
;
2597 return be32_to_cpu(entries
[ichdr
.count
- 1].hashval
);
2601 * Calculate the number of bytes used to store the indicated attribute
2602 * (whether local or remote only calculate bytes in this block).
2605 xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
)
2607 struct xfs_attr_leaf_entry
*entries
;
2608 xfs_attr_leaf_name_local_t
*name_loc
;
2609 xfs_attr_leaf_name_remote_t
*name_rmt
;
2612 entries
= xfs_attr3_leaf_entryp(leaf
);
2613 if (entries
[index
].flags
& XFS_ATTR_LOCAL
) {
2614 name_loc
= xfs_attr3_leaf_name_local(leaf
, index
);
2615 size
= xfs_attr_leaf_entsize_local(name_loc
->namelen
,
2616 be16_to_cpu(name_loc
->valuelen
));
2618 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, index
);
2619 size
= xfs_attr_leaf_entsize_remote(name_rmt
->namelen
);
2625 * Calculate the number of bytes that would be required to store the new
2626 * attribute (whether local or remote only calculate bytes in this block).
2627 * This routine decides as a side effect whether the attribute will be
2628 * a "local" or a "remote" attribute.
2631 xfs_attr_leaf_newentsize(
2632 struct xfs_da_args
*args
,
2637 size
= xfs_attr_leaf_entsize_local(args
->namelen
, args
->valuelen
);
2638 if (size
< xfs_attr_leaf_entsize_local_max(args
->geo
->blksize
)) {
2645 return xfs_attr_leaf_entsize_remote(args
->namelen
);
2649 /*========================================================================
2650 * Manage the INCOMPLETE flag in a leaf entry
2651 *========================================================================*/
2654 * Clear the INCOMPLETE flag on an entry in a leaf block.
2657 xfs_attr3_leaf_clearflag(
2658 struct xfs_da_args
*args
)
2660 struct xfs_attr_leafblock
*leaf
;
2661 struct xfs_attr_leaf_entry
*entry
;
2662 struct xfs_attr_leaf_name_remote
*name_rmt
;
2666 struct xfs_attr3_icleaf_hdr ichdr
;
2667 xfs_attr_leaf_name_local_t
*name_loc
;
2672 trace_xfs_attr_leaf_clearflag(args
);
2674 * Set up the operation.
2676 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2681 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2682 ASSERT(entry
->flags
& XFS_ATTR_INCOMPLETE
);
2685 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2686 ASSERT(args
->index
< ichdr
.count
);
2687 ASSERT(args
->index
>= 0);
2689 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2690 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2691 namelen
= name_loc
->namelen
;
2692 name
= (char *)name_loc
->nameval
;
2694 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2695 namelen
= name_rmt
->namelen
;
2696 name
= (char *)name_rmt
->name
;
2698 ASSERT(be32_to_cpu(entry
->hashval
) == args
->hashval
);
2699 ASSERT(namelen
== args
->namelen
);
2700 ASSERT(memcmp(name
, args
->name
, namelen
) == 0);
2703 entry
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2704 xfs_trans_log_buf(args
->trans
, bp
,
2705 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2707 if (args
->rmtblkno
) {
2708 ASSERT((entry
->flags
& XFS_ATTR_LOCAL
) == 0);
2709 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2710 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2711 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2712 xfs_trans_log_buf(args
->trans
, bp
,
2713 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2717 * Commit the flag value change and start the next trans in series.
2719 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2723 * Set the INCOMPLETE flag on an entry in a leaf block.
2726 xfs_attr3_leaf_setflag(
2727 struct xfs_da_args
*args
)
2729 struct xfs_attr_leafblock
*leaf
;
2730 struct xfs_attr_leaf_entry
*entry
;
2731 struct xfs_attr_leaf_name_remote
*name_rmt
;
2735 struct xfs_attr3_icleaf_hdr ichdr
;
2738 trace_xfs_attr_leaf_setflag(args
);
2741 * Set up the operation.
2743 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2749 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2750 ASSERT(args
->index
< ichdr
.count
);
2751 ASSERT(args
->index
>= 0);
2753 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2755 ASSERT((entry
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2756 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
2757 xfs_trans_log_buf(args
->trans
, bp
,
2758 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2759 if ((entry
->flags
& XFS_ATTR_LOCAL
) == 0) {
2760 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2761 name_rmt
->valueblk
= 0;
2762 name_rmt
->valuelen
= 0;
2763 xfs_trans_log_buf(args
->trans
, bp
,
2764 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2768 * Commit the flag value change and start the next trans in series.
2770 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2774 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2775 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2776 * entry given by args->blkno2/index2.
2778 * Note that they could be in different blocks, or in the same block.
2781 xfs_attr3_leaf_flipflags(
2782 struct xfs_da_args
*args
)
2784 struct xfs_attr_leafblock
*leaf1
;
2785 struct xfs_attr_leafblock
*leaf2
;
2786 struct xfs_attr_leaf_entry
*entry1
;
2787 struct xfs_attr_leaf_entry
*entry2
;
2788 struct xfs_attr_leaf_name_remote
*name_rmt
;
2789 struct xfs_buf
*bp1
;
2790 struct xfs_buf
*bp2
;
2793 struct xfs_attr3_icleaf_hdr ichdr1
;
2794 struct xfs_attr3_icleaf_hdr ichdr2
;
2795 xfs_attr_leaf_name_local_t
*name_loc
;
2796 int namelen1
, namelen2
;
2797 char *name1
, *name2
;
2800 trace_xfs_attr_leaf_flipflags(args
);
2803 * Read the block containing the "old" attr
2805 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp1
);
2810 * Read the block containing the "new" attr, if it is different
2812 if (args
->blkno2
!= args
->blkno
) {
2813 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno2
,
2821 leaf1
= bp1
->b_addr
;
2822 entry1
= &xfs_attr3_leaf_entryp(leaf1
)[args
->index
];
2824 leaf2
= bp2
->b_addr
;
2825 entry2
= &xfs_attr3_leaf_entryp(leaf2
)[args
->index2
];
2828 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr1
, leaf1
);
2829 ASSERT(args
->index
< ichdr1
.count
);
2830 ASSERT(args
->index
>= 0);
2832 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr2
, leaf2
);
2833 ASSERT(args
->index2
< ichdr2
.count
);
2834 ASSERT(args
->index2
>= 0);
2836 if (entry1
->flags
& XFS_ATTR_LOCAL
) {
2837 name_loc
= xfs_attr3_leaf_name_local(leaf1
, args
->index
);
2838 namelen1
= name_loc
->namelen
;
2839 name1
= (char *)name_loc
->nameval
;
2841 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2842 namelen1
= name_rmt
->namelen
;
2843 name1
= (char *)name_rmt
->name
;
2845 if (entry2
->flags
& XFS_ATTR_LOCAL
) {
2846 name_loc
= xfs_attr3_leaf_name_local(leaf2
, args
->index2
);
2847 namelen2
= name_loc
->namelen
;
2848 name2
= (char *)name_loc
->nameval
;
2850 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2851 namelen2
= name_rmt
->namelen
;
2852 name2
= (char *)name_rmt
->name
;
2854 ASSERT(be32_to_cpu(entry1
->hashval
) == be32_to_cpu(entry2
->hashval
));
2855 ASSERT(namelen1
== namelen2
);
2856 ASSERT(memcmp(name1
, name2
, namelen1
) == 0);
2859 ASSERT(entry1
->flags
& XFS_ATTR_INCOMPLETE
);
2860 ASSERT((entry2
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2862 entry1
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2863 xfs_trans_log_buf(args
->trans
, bp1
,
2864 XFS_DA_LOGRANGE(leaf1
, entry1
, sizeof(*entry1
)));
2865 if (args
->rmtblkno
) {
2866 ASSERT((entry1
->flags
& XFS_ATTR_LOCAL
) == 0);
2867 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2868 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2869 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2870 xfs_trans_log_buf(args
->trans
, bp1
,
2871 XFS_DA_LOGRANGE(leaf1
, name_rmt
, sizeof(*name_rmt
)));
2874 entry2
->flags
|= XFS_ATTR_INCOMPLETE
;
2875 xfs_trans_log_buf(args
->trans
, bp2
,
2876 XFS_DA_LOGRANGE(leaf2
, entry2
, sizeof(*entry2
)));
2877 if ((entry2
->flags
& XFS_ATTR_LOCAL
) == 0) {
2878 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2879 name_rmt
->valueblk
= 0;
2880 name_rmt
->valuelen
= 0;
2881 xfs_trans_log_buf(args
->trans
, bp2
,
2882 XFS_DA_LOGRANGE(leaf2
, name_rmt
, sizeof(*name_rmt
)));
2886 * Commit the flag value change and start the next trans in series.
2888 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);