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"
15 #include "xfs_mount.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
18 #include "xfs_inode.h"
19 #include "xfs_trans.h"
20 #include "xfs_bmap_btree.h"
22 #include "xfs_attr_sf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_attr_leaf.h"
25 #include "xfs_trace.h"
26 #include "xfs_cksum.h"
33 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
36 /*========================================================================
37 * Function prototypes for the kernel.
38 *========================================================================*/
41 * Routines used for growing the Btree.
43 STATIC
int xfs_attr3_leaf_create(struct xfs_da_args
*args
,
44 xfs_dablk_t which_block
, struct xfs_buf
**bpp
);
45 STATIC
int xfs_attr3_leaf_add_work(struct xfs_buf
*leaf_buffer
,
46 struct xfs_attr3_icleaf_hdr
*ichdr
,
47 struct xfs_da_args
*args
, int freemap_index
);
48 STATIC
void xfs_attr3_leaf_compact(struct xfs_da_args
*args
,
49 struct xfs_attr3_icleaf_hdr
*ichdr
,
50 struct xfs_buf
*leaf_buffer
);
51 STATIC
void xfs_attr3_leaf_rebalance(xfs_da_state_t
*state
,
52 xfs_da_state_blk_t
*blk1
,
53 xfs_da_state_blk_t
*blk2
);
54 STATIC
int xfs_attr3_leaf_figure_balance(xfs_da_state_t
*state
,
55 xfs_da_state_blk_t
*leaf_blk_1
,
56 struct xfs_attr3_icleaf_hdr
*ichdr1
,
57 xfs_da_state_blk_t
*leaf_blk_2
,
58 struct xfs_attr3_icleaf_hdr
*ichdr2
,
59 int *number_entries_in_blk1
,
60 int *number_usedbytes_in_blk1
);
65 STATIC
void xfs_attr3_leaf_moveents(struct xfs_da_args
*args
,
66 struct xfs_attr_leafblock
*src_leaf
,
67 struct xfs_attr3_icleaf_hdr
*src_ichdr
, int src_start
,
68 struct xfs_attr_leafblock
*dst_leaf
,
69 struct xfs_attr3_icleaf_hdr
*dst_ichdr
, int dst_start
,
71 STATIC
int xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
);
74 * attr3 block 'firstused' conversion helpers.
76 * firstused refers to the offset of the first used byte of the nameval region
77 * of an attr leaf block. The region starts at the tail of the block and expands
78 * backwards towards the middle. As such, firstused is initialized to the block
79 * size for an empty leaf block and is reduced from there.
81 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
82 * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
83 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
84 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
85 * the attr block size. The following helpers manage the conversion between the
86 * in-core and on-disk formats.
90 xfs_attr3_leaf_firstused_from_disk(
91 struct xfs_da_geometry
*geo
,
92 struct xfs_attr3_icleaf_hdr
*to
,
93 struct xfs_attr_leafblock
*from
)
95 struct xfs_attr3_leaf_hdr
*hdr3
;
97 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
98 hdr3
= (struct xfs_attr3_leaf_hdr
*) from
;
99 to
->firstused
= be16_to_cpu(hdr3
->firstused
);
101 to
->firstused
= be16_to_cpu(from
->hdr
.firstused
);
105 * Convert from the magic fsb size value to actual blocksize. This
106 * should only occur for empty blocks when the block size overflows
109 if (to
->firstused
== XFS_ATTR3_LEAF_NULLOFF
) {
110 ASSERT(!to
->count
&& !to
->usedbytes
);
111 ASSERT(geo
->blksize
> USHRT_MAX
);
112 to
->firstused
= geo
->blksize
;
117 xfs_attr3_leaf_firstused_to_disk(
118 struct xfs_da_geometry
*geo
,
119 struct xfs_attr_leafblock
*to
,
120 struct xfs_attr3_icleaf_hdr
*from
)
122 struct xfs_attr3_leaf_hdr
*hdr3
;
125 /* magic value should only be seen on disk */
126 ASSERT(from
->firstused
!= XFS_ATTR3_LEAF_NULLOFF
);
129 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
130 * value. This only overflows at the max supported value of 64k. Use the
131 * magic on-disk value to represent block size in this case.
133 firstused
= from
->firstused
;
134 if (firstused
> USHRT_MAX
) {
135 ASSERT(from
->firstused
== geo
->blksize
);
136 firstused
= XFS_ATTR3_LEAF_NULLOFF
;
139 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
140 hdr3
= (struct xfs_attr3_leaf_hdr
*) to
;
141 hdr3
->firstused
= cpu_to_be16(firstused
);
143 to
->hdr
.firstused
= cpu_to_be16(firstused
);
148 xfs_attr3_leaf_hdr_from_disk(
149 struct xfs_da_geometry
*geo
,
150 struct xfs_attr3_icleaf_hdr
*to
,
151 struct xfs_attr_leafblock
*from
)
155 ASSERT(from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR_LEAF_MAGIC
) ||
156 from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
));
158 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
159 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)from
;
161 to
->forw
= be32_to_cpu(hdr3
->info
.hdr
.forw
);
162 to
->back
= be32_to_cpu(hdr3
->info
.hdr
.back
);
163 to
->magic
= be16_to_cpu(hdr3
->info
.hdr
.magic
);
164 to
->count
= be16_to_cpu(hdr3
->count
);
165 to
->usedbytes
= be16_to_cpu(hdr3
->usedbytes
);
166 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
167 to
->holes
= hdr3
->holes
;
169 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
170 to
->freemap
[i
].base
= be16_to_cpu(hdr3
->freemap
[i
].base
);
171 to
->freemap
[i
].size
= be16_to_cpu(hdr3
->freemap
[i
].size
);
175 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
176 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
177 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
178 to
->count
= be16_to_cpu(from
->hdr
.count
);
179 to
->usedbytes
= be16_to_cpu(from
->hdr
.usedbytes
);
180 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
181 to
->holes
= from
->hdr
.holes
;
183 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
184 to
->freemap
[i
].base
= be16_to_cpu(from
->hdr
.freemap
[i
].base
);
185 to
->freemap
[i
].size
= be16_to_cpu(from
->hdr
.freemap
[i
].size
);
190 xfs_attr3_leaf_hdr_to_disk(
191 struct xfs_da_geometry
*geo
,
192 struct xfs_attr_leafblock
*to
,
193 struct xfs_attr3_icleaf_hdr
*from
)
197 ASSERT(from
->magic
== XFS_ATTR_LEAF_MAGIC
||
198 from
->magic
== XFS_ATTR3_LEAF_MAGIC
);
200 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
201 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)to
;
203 hdr3
->info
.hdr
.forw
= cpu_to_be32(from
->forw
);
204 hdr3
->info
.hdr
.back
= cpu_to_be32(from
->back
);
205 hdr3
->info
.hdr
.magic
= cpu_to_be16(from
->magic
);
206 hdr3
->count
= cpu_to_be16(from
->count
);
207 hdr3
->usedbytes
= cpu_to_be16(from
->usedbytes
);
208 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
209 hdr3
->holes
= from
->holes
;
212 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
213 hdr3
->freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
214 hdr3
->freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
218 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
219 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
220 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
221 to
->hdr
.count
= cpu_to_be16(from
->count
);
222 to
->hdr
.usedbytes
= cpu_to_be16(from
->usedbytes
);
223 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
224 to
->hdr
.holes
= from
->holes
;
227 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
228 to
->hdr
.freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
229 to
->hdr
.freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
233 static xfs_failaddr_t
234 xfs_attr3_leaf_verify(
237 struct xfs_attr3_icleaf_hdr ichdr
;
238 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
239 struct xfs_attr_leafblock
*leaf
= bp
->b_addr
;
240 struct xfs_perag
*pag
= bp
->b_pag
;
241 struct xfs_attr_leaf_entry
*entries
;
245 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, leaf
);
247 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
248 struct xfs_da3_node_hdr
*hdr3
= bp
->b_addr
;
250 if (ichdr
.magic
!= XFS_ATTR3_LEAF_MAGIC
)
251 return __this_address
;
253 if (!uuid_equal(&hdr3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
))
254 return __this_address
;
255 if (be64_to_cpu(hdr3
->info
.blkno
) != bp
->b_bn
)
256 return __this_address
;
257 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->info
.lsn
)))
258 return __this_address
;
260 if (ichdr
.magic
!= XFS_ATTR_LEAF_MAGIC
)
261 return __this_address
;
264 * In recovery there is a transient state where count == 0 is valid
265 * because we may have transitioned an empty shortform attr to a leaf
266 * if the attr didn't fit in shortform.
268 if (pag
&& pag
->pagf_init
&& ichdr
.count
== 0)
269 return __this_address
;
272 * firstused is the block offset of the first name info structure.
273 * Make sure it doesn't go off the block or crash into the header.
275 if (ichdr
.firstused
> mp
->m_attr_geo
->blksize
)
276 return __this_address
;
277 if (ichdr
.firstused
< xfs_attr3_leaf_hdr_size(leaf
))
278 return __this_address
;
280 /* Make sure the entries array doesn't crash into the name info. */
281 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
282 if ((char *)&entries
[ichdr
.count
] >
283 (char *)bp
->b_addr
+ ichdr
.firstused
)
284 return __this_address
;
286 /* XXX: need to range check rest of attr header values */
287 /* XXX: hash order check? */
290 * Quickly check the freemap information. Attribute data has to be
291 * aligned to 4-byte boundaries, and likewise for the free space.
293 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
294 if (ichdr
.freemap
[i
].base
> mp
->m_attr_geo
->blksize
)
295 return __this_address
;
296 if (ichdr
.freemap
[i
].base
& 0x3)
297 return __this_address
;
298 if (ichdr
.freemap
[i
].size
> mp
->m_attr_geo
->blksize
)
299 return __this_address
;
300 if (ichdr
.freemap
[i
].size
& 0x3)
301 return __this_address
;
302 end
= ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
;
303 if (end
< ichdr
.freemap
[i
].base
)
304 return __this_address
;
305 if (end
> mp
->m_attr_geo
->blksize
)
306 return __this_address
;
313 xfs_attr3_leaf_write_verify(
316 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
317 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
318 struct xfs_attr3_leaf_hdr
*hdr3
= bp
->b_addr
;
321 fa
= xfs_attr3_leaf_verify(bp
);
323 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
327 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
331 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
333 xfs_buf_update_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
);
337 * leaf/node format detection on trees is sketchy, so a node read can be done on
338 * leaf level blocks when detection identifies the tree as a node format tree
339 * incorrectly. In this case, we need to swap the verifier to match the correct
340 * format of the block being read.
343 xfs_attr3_leaf_read_verify(
346 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
349 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
350 !xfs_buf_verify_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
))
351 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
353 fa
= xfs_attr3_leaf_verify(bp
);
355 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
359 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops
= {
360 .name
= "xfs_attr3_leaf",
361 .verify_read
= xfs_attr3_leaf_read_verify
,
362 .verify_write
= xfs_attr3_leaf_write_verify
,
363 .verify_struct
= xfs_attr3_leaf_verify
,
368 struct xfs_trans
*tp
,
369 struct xfs_inode
*dp
,
371 xfs_daddr_t mappedbno
,
372 struct xfs_buf
**bpp
)
376 err
= xfs_da_read_buf(tp
, dp
, bno
, mappedbno
, bpp
,
377 XFS_ATTR_FORK
, &xfs_attr3_leaf_buf_ops
);
378 if (!err
&& tp
&& *bpp
)
379 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_ATTR_LEAF_BUF
);
383 /*========================================================================
384 * Namespace helper routines
385 *========================================================================*/
388 * If namespace bits don't match return 0.
389 * If all match then return 1.
392 xfs_attr_namesp_match(int arg_flags
, int ondisk_flags
)
394 return XFS_ATTR_NSP_ONDISK(ondisk_flags
) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags
);
398 /*========================================================================
399 * External routines when attribute fork size < XFS_LITINO(mp).
400 *========================================================================*/
403 * Query whether the requested number of additional bytes of extended
404 * attribute space will be able to fit inline.
406 * Returns zero if not, else the di_forkoff fork offset to be used in the
407 * literal area for attribute data once the new bytes have been added.
409 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
410 * special case for dev/uuid inodes, they have fixed size data forks.
413 xfs_attr_shortform_bytesfit(xfs_inode_t
*dp
, int bytes
)
416 int minforkoff
; /* lower limit on valid forkoff locations */
417 int maxforkoff
; /* upper limit on valid forkoff locations */
419 xfs_mount_t
*mp
= dp
->i_mount
;
422 offset
= (XFS_LITINO(mp
, dp
->i_d
.di_version
) - bytes
) >> 3;
424 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_DEV
) {
425 minforkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
426 return (offset
>= minforkoff
) ? minforkoff
: 0;
430 * If the requested numbers of bytes is smaller or equal to the
431 * current attribute fork size we can always proceed.
433 * Note that if_bytes in the data fork might actually be larger than
434 * the current data fork size is due to delalloc extents. In that
435 * case either the extent count will go down when they are converted
436 * to real extents, or the delalloc conversion will take care of the
437 * literal area rebalancing.
439 if (bytes
<= XFS_IFORK_ASIZE(dp
))
440 return dp
->i_d
.di_forkoff
;
443 * For attr2 we can try to move the forkoff if there is space in the
444 * literal area, but for the old format we are done if there is no
445 * space in the fixed attribute fork.
447 if (!(mp
->m_flags
& XFS_MOUNT_ATTR2
))
450 dsize
= dp
->i_df
.if_bytes
;
452 switch (dp
->i_d
.di_format
) {
453 case XFS_DINODE_FMT_EXTENTS
:
455 * If there is no attr fork and the data fork is extents,
456 * determine if creating the default attr fork will result
457 * in the extents form migrating to btree. If so, the
458 * minimum offset only needs to be the space required for
461 if (!dp
->i_d
.di_forkoff
&& dp
->i_df
.if_bytes
>
462 xfs_default_attroffset(dp
))
463 dsize
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
465 case XFS_DINODE_FMT_BTREE
:
467 * If we have a data btree then keep forkoff if we have one,
468 * otherwise we are adding a new attr, so then we set
469 * minforkoff to where the btree root can finish so we have
470 * plenty of room for attrs
472 if (dp
->i_d
.di_forkoff
) {
473 if (offset
< dp
->i_d
.di_forkoff
)
475 return dp
->i_d
.di_forkoff
;
477 dsize
= XFS_BMAP_BROOT_SPACE(mp
, dp
->i_df
.if_broot
);
482 * A data fork btree root must have space for at least
483 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
485 minforkoff
= max(dsize
, XFS_BMDR_SPACE_CALC(MINDBTPTRS
));
486 minforkoff
= roundup(minforkoff
, 8) >> 3;
488 /* attr fork btree root can have at least this many key/ptr pairs */
489 maxforkoff
= XFS_LITINO(mp
, dp
->i_d
.di_version
) -
490 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
491 maxforkoff
= maxforkoff
>> 3; /* rounded down */
493 if (offset
>= maxforkoff
)
495 if (offset
>= minforkoff
)
501 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
504 xfs_sbversion_add_attr2(xfs_mount_t
*mp
, xfs_trans_t
*tp
)
506 if ((mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
507 !(xfs_sb_version_hasattr2(&mp
->m_sb
))) {
508 spin_lock(&mp
->m_sb_lock
);
509 if (!xfs_sb_version_hasattr2(&mp
->m_sb
)) {
510 xfs_sb_version_addattr2(&mp
->m_sb
);
511 spin_unlock(&mp
->m_sb_lock
);
514 spin_unlock(&mp
->m_sb_lock
);
519 * Create the initial contents of a shortform attribute list.
522 xfs_attr_shortform_create(xfs_da_args_t
*args
)
524 xfs_attr_sf_hdr_t
*hdr
;
526 struct xfs_ifork
*ifp
;
528 trace_xfs_attr_sf_create(args
);
534 ASSERT(ifp
->if_bytes
== 0);
535 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) {
536 ifp
->if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
537 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_LOCAL
;
538 ifp
->if_flags
|= XFS_IFINLINE
;
540 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
542 xfs_idata_realloc(dp
, sizeof(*hdr
), XFS_ATTR_FORK
);
543 hdr
= (xfs_attr_sf_hdr_t
*)ifp
->if_u1
.if_data
;
545 hdr
->totsize
= cpu_to_be16(sizeof(*hdr
));
546 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
550 * Add a name/value pair to the shortform attribute list.
551 * Overflow from the inode has already been checked for.
554 xfs_attr_shortform_add(xfs_da_args_t
*args
, int forkoff
)
556 xfs_attr_shortform_t
*sf
;
557 xfs_attr_sf_entry_t
*sfe
;
561 struct xfs_ifork
*ifp
;
563 trace_xfs_attr_sf_add(args
);
567 dp
->i_d
.di_forkoff
= forkoff
;
570 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
571 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
573 for (i
= 0; i
< sf
->hdr
.count
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
575 if (sfe
->namelen
!= args
->namelen
)
577 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
579 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
585 offset
= (char *)sfe
- (char *)sf
;
586 size
= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
587 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
);
588 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
589 sfe
= (xfs_attr_sf_entry_t
*)((char *)sf
+ offset
);
591 sfe
->namelen
= args
->namelen
;
592 sfe
->valuelen
= args
->valuelen
;
593 sfe
->flags
= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
594 memcpy(sfe
->nameval
, args
->name
, args
->namelen
);
595 memcpy(&sfe
->nameval
[args
->namelen
], args
->value
, args
->valuelen
);
597 be16_add_cpu(&sf
->hdr
.totsize
, size
);
598 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
600 xfs_sbversion_add_attr2(mp
, args
->trans
);
604 * After the last attribute is removed revert to original inode format,
605 * making all literal area available to the data fork once more.
608 xfs_attr_fork_remove(
609 struct xfs_inode
*ip
,
610 struct xfs_trans
*tp
)
612 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
613 ip
->i_d
.di_forkoff
= 0;
614 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
616 ASSERT(ip
->i_d
.di_anextents
== 0);
617 ASSERT(ip
->i_afp
== NULL
);
619 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
623 * Remove an attribute from the shortform attribute list structure.
626 xfs_attr_shortform_remove(xfs_da_args_t
*args
)
628 xfs_attr_shortform_t
*sf
;
629 xfs_attr_sf_entry_t
*sfe
;
630 int base
, size
=0, end
, totsize
, i
;
634 trace_xfs_attr_sf_remove(args
);
638 base
= sizeof(xfs_attr_sf_hdr_t
);
639 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
642 for (i
= 0; i
< end
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
),
644 size
= XFS_ATTR_SF_ENTSIZE(sfe
);
645 if (sfe
->namelen
!= args
->namelen
)
647 if (memcmp(sfe
->nameval
, args
->name
, args
->namelen
) != 0)
649 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
657 * Fix up the attribute fork data, covering the hole
660 totsize
= be16_to_cpu(sf
->hdr
.totsize
);
662 memmove(&((char *)sf
)[base
], &((char *)sf
)[end
], totsize
- end
);
664 be16_add_cpu(&sf
->hdr
.totsize
, -size
);
667 * Fix up the start offset of the attribute fork
670 if (totsize
== sizeof(xfs_attr_sf_hdr_t
) &&
671 (mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
672 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
673 !(args
->op_flags
& XFS_DA_OP_ADDNAME
)) {
674 xfs_attr_fork_remove(dp
, args
->trans
);
676 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
677 dp
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(dp
, totsize
);
678 ASSERT(dp
->i_d
.di_forkoff
);
679 ASSERT(totsize
> sizeof(xfs_attr_sf_hdr_t
) ||
680 (args
->op_flags
& XFS_DA_OP_ADDNAME
) ||
681 !(mp
->m_flags
& XFS_MOUNT_ATTR2
) ||
682 dp
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
);
683 xfs_trans_log_inode(args
->trans
, dp
,
684 XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
687 xfs_sbversion_add_attr2(mp
, args
->trans
);
693 * Look up a name in a shortform attribute list structure.
697 xfs_attr_shortform_lookup(xfs_da_args_t
*args
)
699 xfs_attr_shortform_t
*sf
;
700 xfs_attr_sf_entry_t
*sfe
;
702 struct xfs_ifork
*ifp
;
704 trace_xfs_attr_sf_lookup(args
);
706 ifp
= args
->dp
->i_afp
;
707 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
708 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
710 for (i
= 0; i
< sf
->hdr
.count
;
711 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
712 if (sfe
->namelen
!= args
->namelen
)
714 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
716 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
724 * Look up a name in a shortform attribute list structure.
728 xfs_attr_shortform_getvalue(xfs_da_args_t
*args
)
730 xfs_attr_shortform_t
*sf
;
731 xfs_attr_sf_entry_t
*sfe
;
734 ASSERT(args
->dp
->i_afp
->if_flags
== XFS_IFINLINE
);
735 sf
= (xfs_attr_shortform_t
*)args
->dp
->i_afp
->if_u1
.if_data
;
737 for (i
= 0; i
< sf
->hdr
.count
;
738 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
739 if (sfe
->namelen
!= args
->namelen
)
741 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
743 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
745 if (args
->flags
& ATTR_KERNOVAL
) {
746 args
->valuelen
= sfe
->valuelen
;
749 if (args
->valuelen
< sfe
->valuelen
) {
750 args
->valuelen
= sfe
->valuelen
;
753 args
->valuelen
= sfe
->valuelen
;
754 memcpy(args
->value
, &sfe
->nameval
[args
->namelen
],
762 * Convert from using the shortform to the leaf. On success, return the
763 * buffer so that we can keep it locked until we're totally done with it.
766 xfs_attr_shortform_to_leaf(
767 struct xfs_da_args
*args
,
768 struct xfs_buf
**leaf_bp
)
770 struct xfs_inode
*dp
;
771 struct xfs_attr_shortform
*sf
;
772 struct xfs_attr_sf_entry
*sfe
;
773 struct xfs_da_args nargs
;
778 struct xfs_ifork
*ifp
;
780 trace_xfs_attr_sf_to_leaf(args
);
784 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
785 size
= be16_to_cpu(sf
->hdr
.totsize
);
786 tmpbuffer
= kmem_alloc(size
, KM_SLEEP
);
787 ASSERT(tmpbuffer
!= NULL
);
788 memcpy(tmpbuffer
, ifp
->if_u1
.if_data
, size
);
789 sf
= (xfs_attr_shortform_t
*)tmpbuffer
;
791 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
792 xfs_bmap_local_to_extents_empty(dp
, XFS_ATTR_FORK
);
795 error
= xfs_da_grow_inode(args
, &blkno
);
798 * If we hit an IO error middle of the transaction inside
799 * grow_inode(), we may have inconsistent data. Bail out.
803 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
804 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
809 error
= xfs_attr3_leaf_create(args
, blkno
, &bp
);
811 /* xfs_attr3_leaf_create may not have instantiated a block */
812 if (bp
&& (xfs_da_shrink_inode(args
, 0, bp
) != 0))
814 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
815 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
819 memset((char *)&nargs
, 0, sizeof(nargs
));
821 nargs
.geo
= args
->geo
;
822 nargs
.total
= args
->total
;
823 nargs
.whichfork
= XFS_ATTR_FORK
;
824 nargs
.trans
= args
->trans
;
825 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
828 for (i
= 0; i
< sf
->hdr
.count
; i
++) {
829 nargs
.name
= sfe
->nameval
;
830 nargs
.namelen
= sfe
->namelen
;
831 nargs
.value
= &sfe
->nameval
[nargs
.namelen
];
832 nargs
.valuelen
= sfe
->valuelen
;
833 nargs
.hashval
= xfs_da_hashname(sfe
->nameval
,
835 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe
->flags
);
836 error
= xfs_attr3_leaf_lookup_int(bp
, &nargs
); /* set a->index */
837 ASSERT(error
== -ENOATTR
);
838 error
= xfs_attr3_leaf_add(bp
, &nargs
);
839 ASSERT(error
!= -ENOSPC
);
842 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
847 kmem_free(tmpbuffer
);
852 * Check a leaf attribute block to see if all the entries would fit into
853 * a shortform attribute list.
856 xfs_attr_shortform_allfit(
858 struct xfs_inode
*dp
)
860 struct xfs_attr_leafblock
*leaf
;
861 struct xfs_attr_leaf_entry
*entry
;
862 xfs_attr_leaf_name_local_t
*name_loc
;
863 struct xfs_attr3_icleaf_hdr leafhdr
;
866 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
869 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &leafhdr
, leaf
);
870 entry
= xfs_attr3_leaf_entryp(leaf
);
872 bytes
= sizeof(struct xfs_attr_sf_hdr
);
873 for (i
= 0; i
< leafhdr
.count
; entry
++, i
++) {
874 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
875 continue; /* don't copy partial entries */
876 if (!(entry
->flags
& XFS_ATTR_LOCAL
))
878 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
879 if (name_loc
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
881 if (be16_to_cpu(name_loc
->valuelen
) >= XFS_ATTR_SF_ENTSIZE_MAX
)
883 bytes
+= sizeof(struct xfs_attr_sf_entry
) - 1
885 + be16_to_cpu(name_loc
->valuelen
);
887 if ((dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) &&
888 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
889 (bytes
== sizeof(struct xfs_attr_sf_hdr
)))
891 return xfs_attr_shortform_bytesfit(dp
, bytes
);
894 /* Verify the consistency of an inline attribute fork. */
896 xfs_attr_shortform_verify(
897 struct xfs_inode
*ip
)
899 struct xfs_attr_shortform
*sfp
;
900 struct xfs_attr_sf_entry
*sfep
;
901 struct xfs_attr_sf_entry
*next_sfep
;
903 struct xfs_ifork
*ifp
;
907 ASSERT(ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
);
908 ifp
= XFS_IFORK_PTR(ip
, XFS_ATTR_FORK
);
909 sfp
= (struct xfs_attr_shortform
*)ifp
->if_u1
.if_data
;
910 size
= ifp
->if_bytes
;
913 * Give up if the attribute is way too short.
915 if (size
< sizeof(struct xfs_attr_sf_hdr
))
916 return __this_address
;
918 endp
= (char *)sfp
+ size
;
920 /* Check all reported entries */
921 sfep
= &sfp
->list
[0];
922 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
924 * struct xfs_attr_sf_entry has a variable length.
925 * Check the fixed-offset parts of the structure are
926 * within the data buffer.
928 if (((char *)sfep
+ sizeof(*sfep
)) >= endp
)
929 return __this_address
;
931 /* Don't allow names with known bad length. */
932 if (sfep
->namelen
== 0)
933 return __this_address
;
936 * Check that the variable-length part of the structure is
937 * within the data buffer. The next entry starts after the
938 * name component, so nextentry is an acceptable test.
940 next_sfep
= XFS_ATTR_SF_NEXTENTRY(sfep
);
941 if ((char *)next_sfep
> endp
)
942 return __this_address
;
945 * Check for unknown flags. Short form doesn't support
946 * the incomplete or local bits, so we can use the namespace
949 if (sfep
->flags
& ~XFS_ATTR_NSP_ONDISK_MASK
)
950 return __this_address
;
953 * Check for invalid namespace combinations. We only allow
954 * one namespace flag per xattr, so we can just count the
955 * bits (i.e. hweight) here.
957 if (hweight8(sfep
->flags
& XFS_ATTR_NSP_ONDISK_MASK
) > 1)
958 return __this_address
;
962 if ((void *)sfep
!= (void *)endp
)
963 return __this_address
;
969 * Convert a leaf attribute list to shortform attribute list
972 xfs_attr3_leaf_to_shortform(
974 struct xfs_da_args
*args
,
977 struct xfs_attr_leafblock
*leaf
;
978 struct xfs_attr3_icleaf_hdr ichdr
;
979 struct xfs_attr_leaf_entry
*entry
;
980 struct xfs_attr_leaf_name_local
*name_loc
;
981 struct xfs_da_args nargs
;
982 struct xfs_inode
*dp
= args
->dp
;
987 trace_xfs_attr_leaf_to_sf(args
);
989 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, KM_SLEEP
);
993 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
995 leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
996 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
997 entry
= xfs_attr3_leaf_entryp(leaf
);
999 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1000 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1003 * Clean out the prior contents of the attribute list.
1005 error
= xfs_da_shrink_inode(args
, 0, bp
);
1009 if (forkoff
== -1) {
1010 ASSERT(dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
);
1011 ASSERT(dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
);
1012 xfs_attr_fork_remove(dp
, args
->trans
);
1016 xfs_attr_shortform_create(args
);
1019 * Copy the attributes
1021 memset((char *)&nargs
, 0, sizeof(nargs
));
1022 nargs
.geo
= args
->geo
;
1024 nargs
.total
= args
->total
;
1025 nargs
.whichfork
= XFS_ATTR_FORK
;
1026 nargs
.trans
= args
->trans
;
1027 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
1029 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
1030 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
1031 continue; /* don't copy partial entries */
1032 if (!entry
->nameidx
)
1034 ASSERT(entry
->flags
& XFS_ATTR_LOCAL
);
1035 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
1036 nargs
.name
= name_loc
->nameval
;
1037 nargs
.namelen
= name_loc
->namelen
;
1038 nargs
.value
= &name_loc
->nameval
[nargs
.namelen
];
1039 nargs
.valuelen
= be16_to_cpu(name_loc
->valuelen
);
1040 nargs
.hashval
= be32_to_cpu(entry
->hashval
);
1041 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(entry
->flags
);
1042 xfs_attr_shortform_add(&nargs
, forkoff
);
1047 kmem_free(tmpbuffer
);
1052 * Convert from using a single leaf to a root node and a leaf.
1055 xfs_attr3_leaf_to_node(
1056 struct xfs_da_args
*args
)
1058 struct xfs_attr_leafblock
*leaf
;
1059 struct xfs_attr3_icleaf_hdr icleafhdr
;
1060 struct xfs_attr_leaf_entry
*entries
;
1061 struct xfs_da_node_entry
*btree
;
1062 struct xfs_da3_icnode_hdr icnodehdr
;
1063 struct xfs_da_intnode
*node
;
1064 struct xfs_inode
*dp
= args
->dp
;
1065 struct xfs_mount
*mp
= dp
->i_mount
;
1066 struct xfs_buf
*bp1
= NULL
;
1067 struct xfs_buf
*bp2
= NULL
;
1071 trace_xfs_attr_leaf_to_node(args
);
1073 error
= xfs_da_grow_inode(args
, &blkno
);
1076 error
= xfs_attr3_leaf_read(args
->trans
, dp
, 0, -1, &bp1
);
1080 error
= xfs_da_get_buf(args
->trans
, dp
, blkno
, -1, &bp2
, XFS_ATTR_FORK
);
1084 /* copy leaf to new buffer, update identifiers */
1085 xfs_trans_buf_set_type(args
->trans
, bp2
, XFS_BLFT_ATTR_LEAF_BUF
);
1086 bp2
->b_ops
= bp1
->b_ops
;
1087 memcpy(bp2
->b_addr
, bp1
->b_addr
, args
->geo
->blksize
);
1088 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1089 struct xfs_da3_blkinfo
*hdr3
= bp2
->b_addr
;
1090 hdr3
->blkno
= cpu_to_be64(bp2
->b_bn
);
1092 xfs_trans_log_buf(args
->trans
, bp2
, 0, args
->geo
->blksize
- 1);
1095 * Set up the new root node.
1097 error
= xfs_da3_node_create(args
, 0, 1, &bp1
, XFS_ATTR_FORK
);
1101 dp
->d_ops
->node_hdr_from_disk(&icnodehdr
, node
);
1102 btree
= dp
->d_ops
->node_tree_p(node
);
1105 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &icleafhdr
, leaf
);
1106 entries
= xfs_attr3_leaf_entryp(leaf
);
1108 /* both on-disk, don't endian-flip twice */
1109 btree
[0].hashval
= entries
[icleafhdr
.count
- 1].hashval
;
1110 btree
[0].before
= cpu_to_be32(blkno
);
1111 icnodehdr
.count
= 1;
1112 dp
->d_ops
->node_hdr_to_disk(node
, &icnodehdr
);
1113 xfs_trans_log_buf(args
->trans
, bp1
, 0, args
->geo
->blksize
- 1);
1119 /*========================================================================
1120 * Routines used for growing the Btree.
1121 *========================================================================*/
1124 * Create the initial contents of a leaf attribute list
1125 * or a leaf in a node attribute list.
1128 xfs_attr3_leaf_create(
1129 struct xfs_da_args
*args
,
1131 struct xfs_buf
**bpp
)
1133 struct xfs_attr_leafblock
*leaf
;
1134 struct xfs_attr3_icleaf_hdr ichdr
;
1135 struct xfs_inode
*dp
= args
->dp
;
1136 struct xfs_mount
*mp
= dp
->i_mount
;
1140 trace_xfs_attr_leaf_create(args
);
1142 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp
,
1146 bp
->b_ops
= &xfs_attr3_leaf_buf_ops
;
1147 xfs_trans_buf_set_type(args
->trans
, bp
, XFS_BLFT_ATTR_LEAF_BUF
);
1149 memset(leaf
, 0, args
->geo
->blksize
);
1151 memset(&ichdr
, 0, sizeof(ichdr
));
1152 ichdr
.firstused
= args
->geo
->blksize
;
1154 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1155 struct xfs_da3_blkinfo
*hdr3
= bp
->b_addr
;
1157 ichdr
.magic
= XFS_ATTR3_LEAF_MAGIC
;
1159 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
1160 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
1161 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
1163 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr3_leaf_hdr
);
1165 ichdr
.magic
= XFS_ATTR_LEAF_MAGIC
;
1166 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr_leaf_hdr
);
1168 ichdr
.freemap
[0].size
= ichdr
.firstused
- ichdr
.freemap
[0].base
;
1170 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1171 xfs_trans_log_buf(args
->trans
, bp
, 0, args
->geo
->blksize
- 1);
1178 * Split the leaf node, rebalance, then add the new entry.
1181 xfs_attr3_leaf_split(
1182 struct xfs_da_state
*state
,
1183 struct xfs_da_state_blk
*oldblk
,
1184 struct xfs_da_state_blk
*newblk
)
1189 trace_xfs_attr_leaf_split(state
->args
);
1192 * Allocate space for a new leaf node.
1194 ASSERT(oldblk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1195 error
= xfs_da_grow_inode(state
->args
, &blkno
);
1198 error
= xfs_attr3_leaf_create(state
->args
, blkno
, &newblk
->bp
);
1201 newblk
->blkno
= blkno
;
1202 newblk
->magic
= XFS_ATTR_LEAF_MAGIC
;
1205 * Rebalance the entries across the two leaves.
1206 * NOTE: rebalance() currently depends on the 2nd block being empty.
1208 xfs_attr3_leaf_rebalance(state
, oldblk
, newblk
);
1209 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1214 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1215 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1216 * "new" attrs info. Will need the "old" info to remove it later.
1218 * Insert the "new" entry in the correct block.
1220 if (state
->inleaf
) {
1221 trace_xfs_attr_leaf_add_old(state
->args
);
1222 error
= xfs_attr3_leaf_add(oldblk
->bp
, state
->args
);
1224 trace_xfs_attr_leaf_add_new(state
->args
);
1225 error
= xfs_attr3_leaf_add(newblk
->bp
, state
->args
);
1229 * Update last hashval in each block since we added the name.
1231 oldblk
->hashval
= xfs_attr_leaf_lasthash(oldblk
->bp
, NULL
);
1232 newblk
->hashval
= xfs_attr_leaf_lasthash(newblk
->bp
, NULL
);
1237 * Add a name to the leaf attribute list structure.
1242 struct xfs_da_args
*args
)
1244 struct xfs_attr_leafblock
*leaf
;
1245 struct xfs_attr3_icleaf_hdr ichdr
;
1252 trace_xfs_attr_leaf_add(args
);
1255 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1256 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
.count
);
1257 entsize
= xfs_attr_leaf_newentsize(args
, NULL
);
1260 * Search through freemap for first-fit on new name length.
1261 * (may need to figure in size of entry struct too)
1263 tablesize
= (ichdr
.count
+ 1) * sizeof(xfs_attr_leaf_entry_t
)
1264 + xfs_attr3_leaf_hdr_size(leaf
);
1265 for (sum
= 0, i
= XFS_ATTR_LEAF_MAPSIZE
- 1; i
>= 0; i
--) {
1266 if (tablesize
> ichdr
.firstused
) {
1267 sum
+= ichdr
.freemap
[i
].size
;
1270 if (!ichdr
.freemap
[i
].size
)
1271 continue; /* no space in this map */
1273 if (ichdr
.freemap
[i
].base
< ichdr
.firstused
)
1274 tmp
+= sizeof(xfs_attr_leaf_entry_t
);
1275 if (ichdr
.freemap
[i
].size
>= tmp
) {
1276 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, i
);
1279 sum
+= ichdr
.freemap
[i
].size
;
1283 * If there are no holes in the address space of the block,
1284 * and we don't have enough freespace, then compaction will do us
1285 * no good and we should just give up.
1287 if (!ichdr
.holes
&& sum
< entsize
)
1291 * Compact the entries to coalesce free space.
1292 * This may change the hdr->count via dropping INCOMPLETE entries.
1294 xfs_attr3_leaf_compact(args
, &ichdr
, bp
);
1297 * After compaction, the block is guaranteed to have only one
1298 * free region, in freemap[0]. If it is not big enough, give up.
1300 if (ichdr
.freemap
[0].size
< (entsize
+ sizeof(xfs_attr_leaf_entry_t
))) {
1305 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, 0);
1308 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1309 xfs_trans_log_buf(args
->trans
, bp
,
1310 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
1311 xfs_attr3_leaf_hdr_size(leaf
)));
1316 * Add a name to a leaf attribute list structure.
1319 xfs_attr3_leaf_add_work(
1321 struct xfs_attr3_icleaf_hdr
*ichdr
,
1322 struct xfs_da_args
*args
,
1325 struct xfs_attr_leafblock
*leaf
;
1326 struct xfs_attr_leaf_entry
*entry
;
1327 struct xfs_attr_leaf_name_local
*name_loc
;
1328 struct xfs_attr_leaf_name_remote
*name_rmt
;
1329 struct xfs_mount
*mp
;
1333 trace_xfs_attr_leaf_add_work(args
);
1336 ASSERT(mapindex
>= 0 && mapindex
< XFS_ATTR_LEAF_MAPSIZE
);
1337 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
->count
);
1340 * Force open some space in the entry array and fill it in.
1342 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
1343 if (args
->index
< ichdr
->count
) {
1344 tmp
= ichdr
->count
- args
->index
;
1345 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
1346 memmove(entry
+ 1, entry
, tmp
);
1347 xfs_trans_log_buf(args
->trans
, bp
,
1348 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1353 * Allocate space for the new string (at the end of the run).
1355 mp
= args
->trans
->t_mountp
;
1356 ASSERT(ichdr
->freemap
[mapindex
].base
< args
->geo
->blksize
);
1357 ASSERT((ichdr
->freemap
[mapindex
].base
& 0x3) == 0);
1358 ASSERT(ichdr
->freemap
[mapindex
].size
>=
1359 xfs_attr_leaf_newentsize(args
, NULL
));
1360 ASSERT(ichdr
->freemap
[mapindex
].size
< args
->geo
->blksize
);
1361 ASSERT((ichdr
->freemap
[mapindex
].size
& 0x3) == 0);
1363 ichdr
->freemap
[mapindex
].size
-= xfs_attr_leaf_newentsize(args
, &tmp
);
1365 entry
->nameidx
= cpu_to_be16(ichdr
->freemap
[mapindex
].base
+
1366 ichdr
->freemap
[mapindex
].size
);
1367 entry
->hashval
= cpu_to_be32(args
->hashval
);
1368 entry
->flags
= tmp
? XFS_ATTR_LOCAL
: 0;
1369 entry
->flags
|= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
1370 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1371 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1372 if ((args
->blkno2
== args
->blkno
) &&
1373 (args
->index2
<= args
->index
)) {
1377 xfs_trans_log_buf(args
->trans
, bp
,
1378 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
1379 ASSERT((args
->index
== 0) ||
1380 (be32_to_cpu(entry
->hashval
) >= be32_to_cpu((entry
-1)->hashval
)));
1381 ASSERT((args
->index
== ichdr
->count
- 1) ||
1382 (be32_to_cpu(entry
->hashval
) <= be32_to_cpu((entry
+1)->hashval
)));
1385 * For "remote" attribute values, simply note that we need to
1386 * allocate space for the "remote" value. We can't actually
1387 * allocate the extents in this transaction, and we can't decide
1388 * which blocks they should be as we might allocate more blocks
1389 * as part of this transaction (a split operation for example).
1391 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1392 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
1393 name_loc
->namelen
= args
->namelen
;
1394 name_loc
->valuelen
= cpu_to_be16(args
->valuelen
);
1395 memcpy((char *)name_loc
->nameval
, args
->name
, args
->namelen
);
1396 memcpy((char *)&name_loc
->nameval
[args
->namelen
], args
->value
,
1397 be16_to_cpu(name_loc
->valuelen
));
1399 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
1400 name_rmt
->namelen
= args
->namelen
;
1401 memcpy((char *)name_rmt
->name
, args
->name
, args
->namelen
);
1402 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1404 name_rmt
->valuelen
= 0;
1405 name_rmt
->valueblk
= 0;
1407 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
1408 args
->rmtvaluelen
= args
->valuelen
;
1410 xfs_trans_log_buf(args
->trans
, bp
,
1411 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
1412 xfs_attr_leaf_entsize(leaf
, args
->index
)));
1415 * Update the control info for this leaf node
1417 if (be16_to_cpu(entry
->nameidx
) < ichdr
->firstused
)
1418 ichdr
->firstused
= be16_to_cpu(entry
->nameidx
);
1420 ASSERT(ichdr
->firstused
>= ichdr
->count
* sizeof(xfs_attr_leaf_entry_t
)
1421 + xfs_attr3_leaf_hdr_size(leaf
));
1422 tmp
= (ichdr
->count
- 1) * sizeof(xfs_attr_leaf_entry_t
)
1423 + xfs_attr3_leaf_hdr_size(leaf
);
1425 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
1426 if (ichdr
->freemap
[i
].base
== tmp
) {
1427 ichdr
->freemap
[i
].base
+= sizeof(xfs_attr_leaf_entry_t
);
1428 ichdr
->freemap
[i
].size
-= sizeof(xfs_attr_leaf_entry_t
);
1431 ichdr
->usedbytes
+= xfs_attr_leaf_entsize(leaf
, args
->index
);
1436 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1439 xfs_attr3_leaf_compact(
1440 struct xfs_da_args
*args
,
1441 struct xfs_attr3_icleaf_hdr
*ichdr_dst
,
1444 struct xfs_attr_leafblock
*leaf_src
;
1445 struct xfs_attr_leafblock
*leaf_dst
;
1446 struct xfs_attr3_icleaf_hdr ichdr_src
;
1447 struct xfs_trans
*trans
= args
->trans
;
1450 trace_xfs_attr_leaf_compact(args
);
1452 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, KM_SLEEP
);
1453 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1454 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1455 leaf_src
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1456 leaf_dst
= bp
->b_addr
;
1459 * Copy the on-disk header back into the destination buffer to ensure
1460 * all the information in the header that is not part of the incore
1461 * header structure is preserved.
1463 memcpy(bp
->b_addr
, tmpbuffer
, xfs_attr3_leaf_hdr_size(leaf_src
));
1465 /* Initialise the incore headers */
1466 ichdr_src
= *ichdr_dst
; /* struct copy */
1467 ichdr_dst
->firstused
= args
->geo
->blksize
;
1468 ichdr_dst
->usedbytes
= 0;
1469 ichdr_dst
->count
= 0;
1470 ichdr_dst
->holes
= 0;
1471 ichdr_dst
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_src
);
1472 ichdr_dst
->freemap
[0].size
= ichdr_dst
->firstused
-
1473 ichdr_dst
->freemap
[0].base
;
1475 /* write the header back to initialise the underlying buffer */
1476 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf_dst
, ichdr_dst
);
1479 * Copy all entry's in the same (sorted) order,
1480 * but allocate name/value pairs packed and in sequence.
1482 xfs_attr3_leaf_moveents(args
, leaf_src
, &ichdr_src
, 0,
1483 leaf_dst
, ichdr_dst
, 0, ichdr_src
.count
);
1485 * this logs the entire buffer, but the caller must write the header
1486 * back to the buffer when it is finished modifying it.
1488 xfs_trans_log_buf(trans
, bp
, 0, args
->geo
->blksize
- 1);
1490 kmem_free(tmpbuffer
);
1494 * Compare two leaf blocks "order".
1495 * Return 0 unless leaf2 should go before leaf1.
1498 xfs_attr3_leaf_order(
1499 struct xfs_buf
*leaf1_bp
,
1500 struct xfs_attr3_icleaf_hdr
*leaf1hdr
,
1501 struct xfs_buf
*leaf2_bp
,
1502 struct xfs_attr3_icleaf_hdr
*leaf2hdr
)
1504 struct xfs_attr_leaf_entry
*entries1
;
1505 struct xfs_attr_leaf_entry
*entries2
;
1507 entries1
= xfs_attr3_leaf_entryp(leaf1_bp
->b_addr
);
1508 entries2
= xfs_attr3_leaf_entryp(leaf2_bp
->b_addr
);
1509 if (leaf1hdr
->count
> 0 && leaf2hdr
->count
> 0 &&
1510 ((be32_to_cpu(entries2
[0].hashval
) <
1511 be32_to_cpu(entries1
[0].hashval
)) ||
1512 (be32_to_cpu(entries2
[leaf2hdr
->count
- 1].hashval
) <
1513 be32_to_cpu(entries1
[leaf1hdr
->count
- 1].hashval
)))) {
1520 xfs_attr_leaf_order(
1521 struct xfs_buf
*leaf1_bp
,
1522 struct xfs_buf
*leaf2_bp
)
1524 struct xfs_attr3_icleaf_hdr ichdr1
;
1525 struct xfs_attr3_icleaf_hdr ichdr2
;
1526 struct xfs_mount
*mp
= leaf1_bp
->b_target
->bt_mount
;
1528 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr1
, leaf1_bp
->b_addr
);
1529 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr2
, leaf2_bp
->b_addr
);
1530 return xfs_attr3_leaf_order(leaf1_bp
, &ichdr1
, leaf2_bp
, &ichdr2
);
1534 * Redistribute the attribute list entries between two leaf nodes,
1535 * taking into account the size of the new entry.
1537 * NOTE: if new block is empty, then it will get the upper half of the
1538 * old block. At present, all (one) callers pass in an empty second block.
1540 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1541 * to match what it is doing in splitting the attribute leaf block. Those
1542 * values are used in "atomic rename" operations on attributes. Note that
1543 * the "new" and "old" values can end up in different blocks.
1546 xfs_attr3_leaf_rebalance(
1547 struct xfs_da_state
*state
,
1548 struct xfs_da_state_blk
*blk1
,
1549 struct xfs_da_state_blk
*blk2
)
1551 struct xfs_da_args
*args
;
1552 struct xfs_attr_leafblock
*leaf1
;
1553 struct xfs_attr_leafblock
*leaf2
;
1554 struct xfs_attr3_icleaf_hdr ichdr1
;
1555 struct xfs_attr3_icleaf_hdr ichdr2
;
1556 struct xfs_attr_leaf_entry
*entries1
;
1557 struct xfs_attr_leaf_entry
*entries2
;
1565 * Set up environment.
1567 ASSERT(blk1
->magic
== XFS_ATTR_LEAF_MAGIC
);
1568 ASSERT(blk2
->magic
== XFS_ATTR_LEAF_MAGIC
);
1569 leaf1
= blk1
->bp
->b_addr
;
1570 leaf2
= blk2
->bp
->b_addr
;
1571 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr1
, leaf1
);
1572 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, leaf2
);
1573 ASSERT(ichdr2
.count
== 0);
1576 trace_xfs_attr_leaf_rebalance(args
);
1579 * Check ordering of blocks, reverse if it makes things simpler.
1581 * NOTE: Given that all (current) callers pass in an empty
1582 * second block, this code should never set "swap".
1585 if (xfs_attr3_leaf_order(blk1
->bp
, &ichdr1
, blk2
->bp
, &ichdr2
)) {
1588 /* swap structures rather than reconverting them */
1589 swap(ichdr1
, ichdr2
);
1591 leaf1
= blk1
->bp
->b_addr
;
1592 leaf2
= blk2
->bp
->b_addr
;
1597 * Examine entries until we reduce the absolute difference in
1598 * byte usage between the two blocks to a minimum. Then get
1599 * the direction to copy and the number of elements to move.
1601 * "inleaf" is true if the new entry should be inserted into blk1.
1602 * If "swap" is also true, then reverse the sense of "inleaf".
1604 state
->inleaf
= xfs_attr3_leaf_figure_balance(state
, blk1
, &ichdr1
,
1608 state
->inleaf
= !state
->inleaf
;
1611 * Move any entries required from leaf to leaf:
1613 if (count
< ichdr1
.count
) {
1615 * Figure the total bytes to be added to the destination leaf.
1617 /* number entries being moved */
1618 count
= ichdr1
.count
- count
;
1619 space
= ichdr1
.usedbytes
- totallen
;
1620 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1623 * leaf2 is the destination, compact it if it looks tight.
1625 max
= ichdr2
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1626 max
-= ichdr2
.count
* sizeof(xfs_attr_leaf_entry_t
);
1628 xfs_attr3_leaf_compact(args
, &ichdr2
, blk2
->bp
);
1631 * Move high entries from leaf1 to low end of leaf2.
1633 xfs_attr3_leaf_moveents(args
, leaf1
, &ichdr1
,
1634 ichdr1
.count
- count
, leaf2
, &ichdr2
, 0, count
);
1636 } else if (count
> ichdr1
.count
) {
1638 * I assert that since all callers pass in an empty
1639 * second buffer, this code should never execute.
1644 * Figure the total bytes to be added to the destination leaf.
1646 /* number entries being moved */
1647 count
-= ichdr1
.count
;
1648 space
= totallen
- ichdr1
.usedbytes
;
1649 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1652 * leaf1 is the destination, compact it if it looks tight.
1654 max
= ichdr1
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1655 max
-= ichdr1
.count
* sizeof(xfs_attr_leaf_entry_t
);
1657 xfs_attr3_leaf_compact(args
, &ichdr1
, blk1
->bp
);
1660 * Move low entries from leaf2 to high end of leaf1.
1662 xfs_attr3_leaf_moveents(args
, leaf2
, &ichdr2
, 0, leaf1
, &ichdr1
,
1663 ichdr1
.count
, count
);
1666 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf1
, &ichdr1
);
1667 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf2
, &ichdr2
);
1668 xfs_trans_log_buf(args
->trans
, blk1
->bp
, 0, args
->geo
->blksize
- 1);
1669 xfs_trans_log_buf(args
->trans
, blk2
->bp
, 0, args
->geo
->blksize
- 1);
1672 * Copy out last hashval in each block for B-tree code.
1674 entries1
= xfs_attr3_leaf_entryp(leaf1
);
1675 entries2
= xfs_attr3_leaf_entryp(leaf2
);
1676 blk1
->hashval
= be32_to_cpu(entries1
[ichdr1
.count
- 1].hashval
);
1677 blk2
->hashval
= be32_to_cpu(entries2
[ichdr2
.count
- 1].hashval
);
1680 * Adjust the expected index for insertion.
1681 * NOTE: this code depends on the (current) situation that the
1682 * second block was originally empty.
1684 * If the insertion point moved to the 2nd block, we must adjust
1685 * the index. We must also track the entry just following the
1686 * new entry for use in an "atomic rename" operation, that entry
1687 * is always the "old" entry and the "new" entry is what we are
1688 * inserting. The index/blkno fields refer to the "old" entry,
1689 * while the index2/blkno2 fields refer to the "new" entry.
1691 if (blk1
->index
> ichdr1
.count
) {
1692 ASSERT(state
->inleaf
== 0);
1693 blk2
->index
= blk1
->index
- ichdr1
.count
;
1694 args
->index
= args
->index2
= blk2
->index
;
1695 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1696 } else if (blk1
->index
== ichdr1
.count
) {
1697 if (state
->inleaf
) {
1698 args
->index
= blk1
->index
;
1699 args
->blkno
= blk1
->blkno
;
1701 args
->blkno2
= blk2
->blkno
;
1704 * On a double leaf split, the original attr location
1705 * is already stored in blkno2/index2, so don't
1706 * overwrite it overwise we corrupt the tree.
1708 blk2
->index
= blk1
->index
- ichdr1
.count
;
1709 args
->index
= blk2
->index
;
1710 args
->blkno
= blk2
->blkno
;
1711 if (!state
->extravalid
) {
1713 * set the new attr location to match the old
1714 * one and let the higher level split code
1715 * decide where in the leaf to place it.
1717 args
->index2
= blk2
->index
;
1718 args
->blkno2
= blk2
->blkno
;
1722 ASSERT(state
->inleaf
== 1);
1723 args
->index
= args
->index2
= blk1
->index
;
1724 args
->blkno
= args
->blkno2
= blk1
->blkno
;
1729 * Examine entries until we reduce the absolute difference in
1730 * byte usage between the two blocks to a minimum.
1731 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1732 * GROT: there will always be enough room in either block for a new entry.
1733 * GROT: Do a double-split for this case?
1736 xfs_attr3_leaf_figure_balance(
1737 struct xfs_da_state
*state
,
1738 struct xfs_da_state_blk
*blk1
,
1739 struct xfs_attr3_icleaf_hdr
*ichdr1
,
1740 struct xfs_da_state_blk
*blk2
,
1741 struct xfs_attr3_icleaf_hdr
*ichdr2
,
1745 struct xfs_attr_leafblock
*leaf1
= blk1
->bp
->b_addr
;
1746 struct xfs_attr_leafblock
*leaf2
= blk2
->bp
->b_addr
;
1747 struct xfs_attr_leaf_entry
*entry
;
1758 * Examine entries until we reduce the absolute difference in
1759 * byte usage between the two blocks to a minimum.
1761 max
= ichdr1
->count
+ ichdr2
->count
;
1762 half
= (max
+ 1) * sizeof(*entry
);
1763 half
+= ichdr1
->usedbytes
+ ichdr2
->usedbytes
+
1764 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1766 lastdelta
= state
->args
->geo
->blksize
;
1767 entry
= xfs_attr3_leaf_entryp(leaf1
);
1768 for (count
= index
= 0; count
< max
; entry
++, index
++, count
++) {
1770 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1772 * The new entry is in the first block, account for it.
1774 if (count
== blk1
->index
) {
1775 tmp
= totallen
+ sizeof(*entry
) +
1776 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1777 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1779 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1785 * Wrap around into the second block if necessary.
1787 if (count
== ichdr1
->count
) {
1789 entry
= xfs_attr3_leaf_entryp(leaf1
);
1794 * Figure out if next leaf entry would be too much.
1796 tmp
= totallen
+ sizeof(*entry
) + xfs_attr_leaf_entsize(leaf1
,
1798 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1800 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1806 * Calculate the number of usedbytes that will end up in lower block.
1807 * If new entry not in lower block, fix up the count.
1809 totallen
-= count
* sizeof(*entry
);
1811 totallen
-= sizeof(*entry
) +
1812 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1816 *usedbytesarg
= totallen
;
1820 /*========================================================================
1821 * Routines used for shrinking the Btree.
1822 *========================================================================*/
1825 * Check a leaf block and its neighbors to see if the block should be
1826 * collapsed into one or the other neighbor. Always keep the block
1827 * with the smaller block number.
1828 * If the current block is over 50% full, don't try to join it, return 0.
1829 * If the block is empty, fill in the state structure and return 2.
1830 * If it can be collapsed, fill in the state structure and return 1.
1831 * If nothing can be done, return 0.
1833 * GROT: allow for INCOMPLETE entries in calculation.
1836 xfs_attr3_leaf_toosmall(
1837 struct xfs_da_state
*state
,
1840 struct xfs_attr_leafblock
*leaf
;
1841 struct xfs_da_state_blk
*blk
;
1842 struct xfs_attr3_icleaf_hdr ichdr
;
1851 trace_xfs_attr_leaf_toosmall(state
->args
);
1854 * Check for the degenerate case of the block being over 50% full.
1855 * If so, it's not worth even looking to see if we might be able
1856 * to coalesce with a sibling.
1858 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1859 leaf
= blk
->bp
->b_addr
;
1860 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr
, leaf
);
1861 bytes
= xfs_attr3_leaf_hdr_size(leaf
) +
1862 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
) +
1864 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1865 *action
= 0; /* blk over 50%, don't try to join */
1870 * Check for the degenerate case of the block being empty.
1871 * If the block is empty, we'll simply delete it, no need to
1872 * coalesce it with a sibling block. We choose (arbitrarily)
1873 * to merge with the forward block unless it is NULL.
1875 if (ichdr
.count
== 0) {
1877 * Make altpath point to the block we want to keep and
1878 * path point to the block we want to drop (this one).
1880 forward
= (ichdr
.forw
!= 0);
1881 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1882 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1895 * Examine each sibling block to see if we can coalesce with
1896 * at least 25% free space to spare. We need to figure out
1897 * whether to merge with the forward or the backward block.
1898 * We prefer coalescing with the lower numbered sibling so as
1899 * to shrink an attribute list over time.
1901 /* start with smaller blk num */
1902 forward
= ichdr
.forw
< ichdr
.back
;
1903 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1904 struct xfs_attr3_icleaf_hdr ichdr2
;
1911 error
= xfs_attr3_leaf_read(state
->args
->trans
, state
->args
->dp
,
1916 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, bp
->b_addr
);
1918 bytes
= state
->args
->geo
->blksize
-
1919 (state
->args
->geo
->blksize
>> 2) -
1920 ichdr
.usedbytes
- ichdr2
.usedbytes
-
1921 ((ichdr
.count
+ ichdr2
.count
) *
1922 sizeof(xfs_attr_leaf_entry_t
)) -
1923 xfs_attr3_leaf_hdr_size(leaf
);
1925 xfs_trans_brelse(state
->args
->trans
, bp
);
1927 break; /* fits with at least 25% to spare */
1935 * Make altpath point to the block we want to keep (the lower
1936 * numbered block) and path point to the block we want to drop.
1938 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1939 if (blkno
< blk
->blkno
) {
1940 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1943 error
= xfs_da3_path_shift(state
, &state
->path
, forward
,
1957 * Remove a name from the leaf attribute list structure.
1959 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1960 * If two leaves are 37% full, when combined they will leave 25% free.
1963 xfs_attr3_leaf_remove(
1965 struct xfs_da_args
*args
)
1967 struct xfs_attr_leafblock
*leaf
;
1968 struct xfs_attr3_icleaf_hdr ichdr
;
1969 struct xfs_attr_leaf_entry
*entry
;
1978 trace_xfs_attr_leaf_remove(args
);
1981 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1983 ASSERT(ichdr
.count
> 0 && ichdr
.count
< args
->geo
->blksize
/ 8);
1984 ASSERT(args
->index
>= 0 && args
->index
< ichdr
.count
);
1985 ASSERT(ichdr
.firstused
>= ichdr
.count
* sizeof(*entry
) +
1986 xfs_attr3_leaf_hdr_size(leaf
));
1988 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
1990 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
1991 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
1994 * Scan through free region table:
1995 * check for adjacency of free'd entry with an existing one,
1996 * find smallest free region in case we need to replace it,
1997 * adjust any map that borders the entry table,
1999 tablesize
= ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
)
2000 + xfs_attr3_leaf_hdr_size(leaf
);
2001 tmp
= ichdr
.freemap
[0].size
;
2002 before
= after
= -1;
2003 smallest
= XFS_ATTR_LEAF_MAPSIZE
- 1;
2004 entsize
= xfs_attr_leaf_entsize(leaf
, args
->index
);
2005 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
2006 ASSERT(ichdr
.freemap
[i
].base
< args
->geo
->blksize
);
2007 ASSERT(ichdr
.freemap
[i
].size
< args
->geo
->blksize
);
2008 if (ichdr
.freemap
[i
].base
== tablesize
) {
2009 ichdr
.freemap
[i
].base
-= sizeof(xfs_attr_leaf_entry_t
);
2010 ichdr
.freemap
[i
].size
+= sizeof(xfs_attr_leaf_entry_t
);
2013 if (ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
==
2014 be16_to_cpu(entry
->nameidx
)) {
2016 } else if (ichdr
.freemap
[i
].base
==
2017 (be16_to_cpu(entry
->nameidx
) + entsize
)) {
2019 } else if (ichdr
.freemap
[i
].size
< tmp
) {
2020 tmp
= ichdr
.freemap
[i
].size
;
2026 * Coalesce adjacent freemap regions,
2027 * or replace the smallest region.
2029 if ((before
>= 0) || (after
>= 0)) {
2030 if ((before
>= 0) && (after
>= 0)) {
2031 ichdr
.freemap
[before
].size
+= entsize
;
2032 ichdr
.freemap
[before
].size
+= ichdr
.freemap
[after
].size
;
2033 ichdr
.freemap
[after
].base
= 0;
2034 ichdr
.freemap
[after
].size
= 0;
2035 } else if (before
>= 0) {
2036 ichdr
.freemap
[before
].size
+= entsize
;
2038 ichdr
.freemap
[after
].base
= be16_to_cpu(entry
->nameidx
);
2039 ichdr
.freemap
[after
].size
+= entsize
;
2043 * Replace smallest region (if it is smaller than free'd entry)
2045 if (ichdr
.freemap
[smallest
].size
< entsize
) {
2046 ichdr
.freemap
[smallest
].base
= be16_to_cpu(entry
->nameidx
);
2047 ichdr
.freemap
[smallest
].size
= entsize
;
2052 * Did we remove the first entry?
2054 if (be16_to_cpu(entry
->nameidx
) == ichdr
.firstused
)
2060 * Compress the remaining entries and zero out the removed stuff.
2062 memset(xfs_attr3_leaf_name(leaf
, args
->index
), 0, entsize
);
2063 ichdr
.usedbytes
-= entsize
;
2064 xfs_trans_log_buf(args
->trans
, bp
,
2065 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
2068 tmp
= (ichdr
.count
- args
->index
) * sizeof(xfs_attr_leaf_entry_t
);
2069 memmove(entry
, entry
+ 1, tmp
);
2071 xfs_trans_log_buf(args
->trans
, bp
,
2072 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(xfs_attr_leaf_entry_t
)));
2074 entry
= &xfs_attr3_leaf_entryp(leaf
)[ichdr
.count
];
2075 memset(entry
, 0, sizeof(xfs_attr_leaf_entry_t
));
2078 * If we removed the first entry, re-find the first used byte
2079 * in the name area. Note that if the entry was the "firstused",
2080 * then we don't have a "hole" in our block resulting from
2081 * removing the name.
2084 tmp
= args
->geo
->blksize
;
2085 entry
= xfs_attr3_leaf_entryp(leaf
);
2086 for (i
= ichdr
.count
- 1; i
>= 0; entry
++, i
--) {
2087 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2088 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2090 if (be16_to_cpu(entry
->nameidx
) < tmp
)
2091 tmp
= be16_to_cpu(entry
->nameidx
);
2093 ichdr
.firstused
= tmp
;
2094 ASSERT(ichdr
.firstused
!= 0);
2096 ichdr
.holes
= 1; /* mark as needing compaction */
2098 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
2099 xfs_trans_log_buf(args
->trans
, bp
,
2100 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
2101 xfs_attr3_leaf_hdr_size(leaf
)));
2104 * Check if leaf is less than 50% full, caller may want to
2105 * "join" the leaf with a sibling if so.
2107 tmp
= ichdr
.usedbytes
+ xfs_attr3_leaf_hdr_size(leaf
) +
2108 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
);
2110 return tmp
< args
->geo
->magicpct
; /* leaf is < 37% full */
2114 * Move all the attribute list entries from drop_leaf into save_leaf.
2117 xfs_attr3_leaf_unbalance(
2118 struct xfs_da_state
*state
,
2119 struct xfs_da_state_blk
*drop_blk
,
2120 struct xfs_da_state_blk
*save_blk
)
2122 struct xfs_attr_leafblock
*drop_leaf
= drop_blk
->bp
->b_addr
;
2123 struct xfs_attr_leafblock
*save_leaf
= save_blk
->bp
->b_addr
;
2124 struct xfs_attr3_icleaf_hdr drophdr
;
2125 struct xfs_attr3_icleaf_hdr savehdr
;
2126 struct xfs_attr_leaf_entry
*entry
;
2128 trace_xfs_attr_leaf_unbalance(state
->args
);
2130 drop_leaf
= drop_blk
->bp
->b_addr
;
2131 save_leaf
= save_blk
->bp
->b_addr
;
2132 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &drophdr
, drop_leaf
);
2133 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &savehdr
, save_leaf
);
2134 entry
= xfs_attr3_leaf_entryp(drop_leaf
);
2137 * Save last hashval from dying block for later Btree fixup.
2139 drop_blk
->hashval
= be32_to_cpu(entry
[drophdr
.count
- 1].hashval
);
2142 * Check if we need a temp buffer, or can we do it in place.
2143 * Note that we don't check "leaf" for holes because we will
2144 * always be dropping it, toosmall() decided that for us already.
2146 if (savehdr
.holes
== 0) {
2148 * dest leaf has no holes, so we add there. May need
2149 * to make some room in the entry array.
2151 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2152 drop_blk
->bp
, &drophdr
)) {
2153 xfs_attr3_leaf_moveents(state
->args
,
2154 drop_leaf
, &drophdr
, 0,
2155 save_leaf
, &savehdr
, 0,
2158 xfs_attr3_leaf_moveents(state
->args
,
2159 drop_leaf
, &drophdr
, 0,
2160 save_leaf
, &savehdr
,
2161 savehdr
.count
, drophdr
.count
);
2165 * Destination has holes, so we make a temporary copy
2166 * of the leaf and add them both to that.
2168 struct xfs_attr_leafblock
*tmp_leaf
;
2169 struct xfs_attr3_icleaf_hdr tmphdr
;
2171 tmp_leaf
= kmem_zalloc(state
->args
->geo
->blksize
, KM_SLEEP
);
2174 * Copy the header into the temp leaf so that all the stuff
2175 * not in the incore header is present and gets copied back in
2176 * once we've moved all the entries.
2178 memcpy(tmp_leaf
, save_leaf
, xfs_attr3_leaf_hdr_size(save_leaf
));
2180 memset(&tmphdr
, 0, sizeof(tmphdr
));
2181 tmphdr
.magic
= savehdr
.magic
;
2182 tmphdr
.forw
= savehdr
.forw
;
2183 tmphdr
.back
= savehdr
.back
;
2184 tmphdr
.firstused
= state
->args
->geo
->blksize
;
2186 /* write the header to the temp buffer to initialise it */
2187 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, tmp_leaf
, &tmphdr
);
2189 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2190 drop_blk
->bp
, &drophdr
)) {
2191 xfs_attr3_leaf_moveents(state
->args
,
2192 drop_leaf
, &drophdr
, 0,
2193 tmp_leaf
, &tmphdr
, 0,
2195 xfs_attr3_leaf_moveents(state
->args
,
2196 save_leaf
, &savehdr
, 0,
2197 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2200 xfs_attr3_leaf_moveents(state
->args
,
2201 save_leaf
, &savehdr
, 0,
2202 tmp_leaf
, &tmphdr
, 0,
2204 xfs_attr3_leaf_moveents(state
->args
,
2205 drop_leaf
, &drophdr
, 0,
2206 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2209 memcpy(save_leaf
, tmp_leaf
, state
->args
->geo
->blksize
);
2210 savehdr
= tmphdr
; /* struct copy */
2211 kmem_free(tmp_leaf
);
2214 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, save_leaf
, &savehdr
);
2215 xfs_trans_log_buf(state
->args
->trans
, save_blk
->bp
, 0,
2216 state
->args
->geo
->blksize
- 1);
2219 * Copy out last hashval in each block for B-tree code.
2221 entry
= xfs_attr3_leaf_entryp(save_leaf
);
2222 save_blk
->hashval
= be32_to_cpu(entry
[savehdr
.count
- 1].hashval
);
2225 /*========================================================================
2226 * Routines used for finding things in the Btree.
2227 *========================================================================*/
2230 * Look up a name in a leaf attribute list structure.
2231 * This is the internal routine, it uses the caller's buffer.
2233 * Note that duplicate keys are allowed, but only check within the
2234 * current leaf node. The Btree code must check in adjacent leaf nodes.
2236 * Return in args->index the index into the entry[] array of either
2237 * the found entry, or where the entry should have been (insert before
2240 * Don't change the args->value unless we find the attribute.
2243 xfs_attr3_leaf_lookup_int(
2245 struct xfs_da_args
*args
)
2247 struct xfs_attr_leafblock
*leaf
;
2248 struct xfs_attr3_icleaf_hdr ichdr
;
2249 struct xfs_attr_leaf_entry
*entry
;
2250 struct xfs_attr_leaf_entry
*entries
;
2251 struct xfs_attr_leaf_name_local
*name_loc
;
2252 struct xfs_attr_leaf_name_remote
*name_rmt
;
2253 xfs_dahash_t hashval
;
2257 trace_xfs_attr_leaf_lookup(args
);
2260 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2261 entries
= xfs_attr3_leaf_entryp(leaf
);
2262 if (ichdr
.count
>= args
->geo
->blksize
/ 8)
2263 return -EFSCORRUPTED
;
2266 * Binary search. (note: small blocks will skip this loop)
2268 hashval
= args
->hashval
;
2269 probe
= span
= ichdr
.count
/ 2;
2270 for (entry
= &entries
[probe
]; span
> 4; entry
= &entries
[probe
]) {
2272 if (be32_to_cpu(entry
->hashval
) < hashval
)
2274 else if (be32_to_cpu(entry
->hashval
) > hashval
)
2279 if (!(probe
>= 0 && (!ichdr
.count
|| probe
< ichdr
.count
)))
2280 return -EFSCORRUPTED
;
2281 if (!(span
<= 4 || be32_to_cpu(entry
->hashval
) == hashval
))
2282 return -EFSCORRUPTED
;
2285 * Since we may have duplicate hashval's, find the first matching
2286 * hashval in the leaf.
2288 while (probe
> 0 && be32_to_cpu(entry
->hashval
) >= hashval
) {
2292 while (probe
< ichdr
.count
&&
2293 be32_to_cpu(entry
->hashval
) < hashval
) {
2297 if (probe
== ichdr
.count
|| be32_to_cpu(entry
->hashval
) != hashval
) {
2298 args
->index
= probe
;
2303 * Duplicate keys may be present, so search all of them for a match.
2305 for (; probe
< ichdr
.count
&& (be32_to_cpu(entry
->hashval
) == hashval
);
2308 * GROT: Add code to remove incomplete entries.
2311 * If we are looking for INCOMPLETE entries, show only those.
2312 * If we are looking for complete entries, show only those.
2314 if ((args
->flags
& XFS_ATTR_INCOMPLETE
) !=
2315 (entry
->flags
& XFS_ATTR_INCOMPLETE
)) {
2318 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2319 name_loc
= xfs_attr3_leaf_name_local(leaf
, probe
);
2320 if (name_loc
->namelen
!= args
->namelen
)
2322 if (memcmp(args
->name
, name_loc
->nameval
,
2323 args
->namelen
) != 0)
2325 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2327 args
->index
= probe
;
2330 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, probe
);
2331 if (name_rmt
->namelen
!= args
->namelen
)
2333 if (memcmp(args
->name
, name_rmt
->name
,
2334 args
->namelen
) != 0)
2336 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2338 args
->index
= probe
;
2339 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2340 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2341 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(
2347 args
->index
= probe
;
2352 * Get the value associated with an attribute name from a leaf attribute
2356 xfs_attr3_leaf_getvalue(
2358 struct xfs_da_args
*args
)
2360 struct xfs_attr_leafblock
*leaf
;
2361 struct xfs_attr3_icleaf_hdr ichdr
;
2362 struct xfs_attr_leaf_entry
*entry
;
2363 struct xfs_attr_leaf_name_local
*name_loc
;
2364 struct xfs_attr_leaf_name_remote
*name_rmt
;
2368 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2369 ASSERT(ichdr
.count
< args
->geo
->blksize
/ 8);
2370 ASSERT(args
->index
< ichdr
.count
);
2372 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2373 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2374 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2375 ASSERT(name_loc
->namelen
== args
->namelen
);
2376 ASSERT(memcmp(args
->name
, name_loc
->nameval
, args
->namelen
) == 0);
2377 valuelen
= be16_to_cpu(name_loc
->valuelen
);
2378 if (args
->flags
& ATTR_KERNOVAL
) {
2379 args
->valuelen
= valuelen
;
2382 if (args
->valuelen
< valuelen
) {
2383 args
->valuelen
= valuelen
;
2386 args
->valuelen
= valuelen
;
2387 memcpy(args
->value
, &name_loc
->nameval
[args
->namelen
], valuelen
);
2389 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2390 ASSERT(name_rmt
->namelen
== args
->namelen
);
2391 ASSERT(memcmp(args
->name
, name_rmt
->name
, args
->namelen
) == 0);
2392 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2393 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2394 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(args
->dp
->i_mount
,
2396 if (args
->flags
& ATTR_KERNOVAL
) {
2397 args
->valuelen
= args
->rmtvaluelen
;
2400 if (args
->valuelen
< args
->rmtvaluelen
) {
2401 args
->valuelen
= args
->rmtvaluelen
;
2404 args
->valuelen
= args
->rmtvaluelen
;
2409 /*========================================================================
2411 *========================================================================*/
2414 * Move the indicated entries from one leaf to another.
2415 * NOTE: this routine modifies both source and destination leaves.
2419 xfs_attr3_leaf_moveents(
2420 struct xfs_da_args
*args
,
2421 struct xfs_attr_leafblock
*leaf_s
,
2422 struct xfs_attr3_icleaf_hdr
*ichdr_s
,
2424 struct xfs_attr_leafblock
*leaf_d
,
2425 struct xfs_attr3_icleaf_hdr
*ichdr_d
,
2429 struct xfs_attr_leaf_entry
*entry_s
;
2430 struct xfs_attr_leaf_entry
*entry_d
;
2436 * Check for nothing to do.
2442 * Set up environment.
2444 ASSERT(ichdr_s
->magic
== XFS_ATTR_LEAF_MAGIC
||
2445 ichdr_s
->magic
== XFS_ATTR3_LEAF_MAGIC
);
2446 ASSERT(ichdr_s
->magic
== ichdr_d
->magic
);
2447 ASSERT(ichdr_s
->count
> 0 && ichdr_s
->count
< args
->geo
->blksize
/ 8);
2448 ASSERT(ichdr_s
->firstused
>= (ichdr_s
->count
* sizeof(*entry_s
))
2449 + xfs_attr3_leaf_hdr_size(leaf_s
));
2450 ASSERT(ichdr_d
->count
< args
->geo
->blksize
/ 8);
2451 ASSERT(ichdr_d
->firstused
>= (ichdr_d
->count
* sizeof(*entry_d
))
2452 + xfs_attr3_leaf_hdr_size(leaf_d
));
2454 ASSERT(start_s
< ichdr_s
->count
);
2455 ASSERT(start_d
<= ichdr_d
->count
);
2456 ASSERT(count
<= ichdr_s
->count
);
2460 * Move the entries in the destination leaf up to make a hole?
2462 if (start_d
< ichdr_d
->count
) {
2463 tmp
= ichdr_d
->count
- start_d
;
2464 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2465 entry_s
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2466 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
+ count
];
2467 memmove(entry_d
, entry_s
, tmp
);
2471 * Copy all entry's in the same (sorted) order,
2472 * but allocate attribute info packed and in sequence.
2474 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2475 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2477 for (i
= 0; i
< count
; entry_s
++, entry_d
++, desti
++, i
++) {
2478 ASSERT(be16_to_cpu(entry_s
->nameidx
) >= ichdr_s
->firstused
);
2479 tmp
= xfs_attr_leaf_entsize(leaf_s
, start_s
+ i
);
2482 * Code to drop INCOMPLETE entries. Difficult to use as we
2483 * may also need to change the insertion index. Code turned
2484 * off for 6.2, should be revisited later.
2486 if (entry_s
->flags
& XFS_ATTR_INCOMPLETE
) { /* skip partials? */
2487 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2488 ichdr_s
->usedbytes
-= tmp
;
2489 ichdr_s
->count
-= 1;
2490 entry_d
--; /* to compensate for ++ in loop hdr */
2492 if ((start_s
+ i
) < offset
)
2493 result
++; /* insertion index adjustment */
2496 ichdr_d
->firstused
-= tmp
;
2497 /* both on-disk, don't endian flip twice */
2498 entry_d
->hashval
= entry_s
->hashval
;
2499 entry_d
->nameidx
= cpu_to_be16(ichdr_d
->firstused
);
2500 entry_d
->flags
= entry_s
->flags
;
2501 ASSERT(be16_to_cpu(entry_d
->nameidx
) + tmp
2502 <= args
->geo
->blksize
);
2503 memmove(xfs_attr3_leaf_name(leaf_d
, desti
),
2504 xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), tmp
);
2505 ASSERT(be16_to_cpu(entry_s
->nameidx
) + tmp
2506 <= args
->geo
->blksize
);
2507 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2508 ichdr_s
->usedbytes
-= tmp
;
2509 ichdr_d
->usedbytes
+= tmp
;
2510 ichdr_s
->count
-= 1;
2511 ichdr_d
->count
+= 1;
2512 tmp
= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
)
2513 + xfs_attr3_leaf_hdr_size(leaf_d
);
2514 ASSERT(ichdr_d
->firstused
>= tmp
);
2521 * Zero out the entries we just copied.
2523 if (start_s
== ichdr_s
->count
) {
2524 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2525 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2526 ASSERT(((char *)entry_s
+ tmp
) <=
2527 ((char *)leaf_s
+ args
->geo
->blksize
));
2528 memset(entry_s
, 0, tmp
);
2531 * Move the remaining entries down to fill the hole,
2532 * then zero the entries at the top.
2534 tmp
= (ichdr_s
->count
- count
) * sizeof(xfs_attr_leaf_entry_t
);
2535 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
+ count
];
2536 entry_d
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2537 memmove(entry_d
, entry_s
, tmp
);
2539 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2540 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[ichdr_s
->count
];
2541 ASSERT(((char *)entry_s
+ tmp
) <=
2542 ((char *)leaf_s
+ args
->geo
->blksize
));
2543 memset(entry_s
, 0, tmp
);
2547 * Fill in the freemap information
2549 ichdr_d
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_d
);
2550 ichdr_d
->freemap
[0].base
+= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
);
2551 ichdr_d
->freemap
[0].size
= ichdr_d
->firstused
- ichdr_d
->freemap
[0].base
;
2552 ichdr_d
->freemap
[1].base
= 0;
2553 ichdr_d
->freemap
[2].base
= 0;
2554 ichdr_d
->freemap
[1].size
= 0;
2555 ichdr_d
->freemap
[2].size
= 0;
2556 ichdr_s
->holes
= 1; /* leaf may not be compact */
2560 * Pick up the last hashvalue from a leaf block.
2563 xfs_attr_leaf_lasthash(
2567 struct xfs_attr3_icleaf_hdr ichdr
;
2568 struct xfs_attr_leaf_entry
*entries
;
2569 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2571 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, bp
->b_addr
);
2572 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
2574 *count
= ichdr
.count
;
2577 return be32_to_cpu(entries
[ichdr
.count
- 1].hashval
);
2581 * Calculate the number of bytes used to store the indicated attribute
2582 * (whether local or remote only calculate bytes in this block).
2585 xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
)
2587 struct xfs_attr_leaf_entry
*entries
;
2588 xfs_attr_leaf_name_local_t
*name_loc
;
2589 xfs_attr_leaf_name_remote_t
*name_rmt
;
2592 entries
= xfs_attr3_leaf_entryp(leaf
);
2593 if (entries
[index
].flags
& XFS_ATTR_LOCAL
) {
2594 name_loc
= xfs_attr3_leaf_name_local(leaf
, index
);
2595 size
= xfs_attr_leaf_entsize_local(name_loc
->namelen
,
2596 be16_to_cpu(name_loc
->valuelen
));
2598 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, index
);
2599 size
= xfs_attr_leaf_entsize_remote(name_rmt
->namelen
);
2605 * Calculate the number of bytes that would be required to store the new
2606 * attribute (whether local or remote only calculate bytes in this block).
2607 * This routine decides as a side effect whether the attribute will be
2608 * a "local" or a "remote" attribute.
2611 xfs_attr_leaf_newentsize(
2612 struct xfs_da_args
*args
,
2617 size
= xfs_attr_leaf_entsize_local(args
->namelen
, args
->valuelen
);
2618 if (size
< xfs_attr_leaf_entsize_local_max(args
->geo
->blksize
)) {
2625 return xfs_attr_leaf_entsize_remote(args
->namelen
);
2629 /*========================================================================
2630 * Manage the INCOMPLETE flag in a leaf entry
2631 *========================================================================*/
2634 * Clear the INCOMPLETE flag on an entry in a leaf block.
2637 xfs_attr3_leaf_clearflag(
2638 struct xfs_da_args
*args
)
2640 struct xfs_attr_leafblock
*leaf
;
2641 struct xfs_attr_leaf_entry
*entry
;
2642 struct xfs_attr_leaf_name_remote
*name_rmt
;
2646 struct xfs_attr3_icleaf_hdr ichdr
;
2647 xfs_attr_leaf_name_local_t
*name_loc
;
2652 trace_xfs_attr_leaf_clearflag(args
);
2654 * Set up the operation.
2656 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2661 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2662 ASSERT(entry
->flags
& XFS_ATTR_INCOMPLETE
);
2665 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2666 ASSERT(args
->index
< ichdr
.count
);
2667 ASSERT(args
->index
>= 0);
2669 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2670 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2671 namelen
= name_loc
->namelen
;
2672 name
= (char *)name_loc
->nameval
;
2674 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2675 namelen
= name_rmt
->namelen
;
2676 name
= (char *)name_rmt
->name
;
2678 ASSERT(be32_to_cpu(entry
->hashval
) == args
->hashval
);
2679 ASSERT(namelen
== args
->namelen
);
2680 ASSERT(memcmp(name
, args
->name
, namelen
) == 0);
2683 entry
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2684 xfs_trans_log_buf(args
->trans
, bp
,
2685 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2687 if (args
->rmtblkno
) {
2688 ASSERT((entry
->flags
& XFS_ATTR_LOCAL
) == 0);
2689 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2690 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2691 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2692 xfs_trans_log_buf(args
->trans
, bp
,
2693 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2697 * Commit the flag value change and start the next trans in series.
2699 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2703 * Set the INCOMPLETE flag on an entry in a leaf block.
2706 xfs_attr3_leaf_setflag(
2707 struct xfs_da_args
*args
)
2709 struct xfs_attr_leafblock
*leaf
;
2710 struct xfs_attr_leaf_entry
*entry
;
2711 struct xfs_attr_leaf_name_remote
*name_rmt
;
2715 struct xfs_attr3_icleaf_hdr ichdr
;
2718 trace_xfs_attr_leaf_setflag(args
);
2721 * Set up the operation.
2723 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2729 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2730 ASSERT(args
->index
< ichdr
.count
);
2731 ASSERT(args
->index
>= 0);
2733 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2735 ASSERT((entry
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2736 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
2737 xfs_trans_log_buf(args
->trans
, bp
,
2738 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2739 if ((entry
->flags
& XFS_ATTR_LOCAL
) == 0) {
2740 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2741 name_rmt
->valueblk
= 0;
2742 name_rmt
->valuelen
= 0;
2743 xfs_trans_log_buf(args
->trans
, bp
,
2744 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2748 * Commit the flag value change and start the next trans in series.
2750 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2754 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2755 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2756 * entry given by args->blkno2/index2.
2758 * Note that they could be in different blocks, or in the same block.
2761 xfs_attr3_leaf_flipflags(
2762 struct xfs_da_args
*args
)
2764 struct xfs_attr_leafblock
*leaf1
;
2765 struct xfs_attr_leafblock
*leaf2
;
2766 struct xfs_attr_leaf_entry
*entry1
;
2767 struct xfs_attr_leaf_entry
*entry2
;
2768 struct xfs_attr_leaf_name_remote
*name_rmt
;
2769 struct xfs_buf
*bp1
;
2770 struct xfs_buf
*bp2
;
2773 struct xfs_attr3_icleaf_hdr ichdr1
;
2774 struct xfs_attr3_icleaf_hdr ichdr2
;
2775 xfs_attr_leaf_name_local_t
*name_loc
;
2776 int namelen1
, namelen2
;
2777 char *name1
, *name2
;
2780 trace_xfs_attr_leaf_flipflags(args
);
2783 * Read the block containing the "old" attr
2785 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp1
);
2790 * Read the block containing the "new" attr, if it is different
2792 if (args
->blkno2
!= args
->blkno
) {
2793 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno2
,
2801 leaf1
= bp1
->b_addr
;
2802 entry1
= &xfs_attr3_leaf_entryp(leaf1
)[args
->index
];
2804 leaf2
= bp2
->b_addr
;
2805 entry2
= &xfs_attr3_leaf_entryp(leaf2
)[args
->index2
];
2808 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr1
, leaf1
);
2809 ASSERT(args
->index
< ichdr1
.count
);
2810 ASSERT(args
->index
>= 0);
2812 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr2
, leaf2
);
2813 ASSERT(args
->index2
< ichdr2
.count
);
2814 ASSERT(args
->index2
>= 0);
2816 if (entry1
->flags
& XFS_ATTR_LOCAL
) {
2817 name_loc
= xfs_attr3_leaf_name_local(leaf1
, args
->index
);
2818 namelen1
= name_loc
->namelen
;
2819 name1
= (char *)name_loc
->nameval
;
2821 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2822 namelen1
= name_rmt
->namelen
;
2823 name1
= (char *)name_rmt
->name
;
2825 if (entry2
->flags
& XFS_ATTR_LOCAL
) {
2826 name_loc
= xfs_attr3_leaf_name_local(leaf2
, args
->index2
);
2827 namelen2
= name_loc
->namelen
;
2828 name2
= (char *)name_loc
->nameval
;
2830 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2831 namelen2
= name_rmt
->namelen
;
2832 name2
= (char *)name_rmt
->name
;
2834 ASSERT(be32_to_cpu(entry1
->hashval
) == be32_to_cpu(entry2
->hashval
));
2835 ASSERT(namelen1
== namelen2
);
2836 ASSERT(memcmp(name1
, name2
, namelen1
) == 0);
2839 ASSERT(entry1
->flags
& XFS_ATTR_INCOMPLETE
);
2840 ASSERT((entry2
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2842 entry1
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2843 xfs_trans_log_buf(args
->trans
, bp1
,
2844 XFS_DA_LOGRANGE(leaf1
, entry1
, sizeof(*entry1
)));
2845 if (args
->rmtblkno
) {
2846 ASSERT((entry1
->flags
& XFS_ATTR_LOCAL
) == 0);
2847 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2848 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2849 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2850 xfs_trans_log_buf(args
->trans
, bp1
,
2851 XFS_DA_LOGRANGE(leaf1
, name_rmt
, sizeof(*name_rmt
)));
2854 entry2
->flags
|= XFS_ATTR_INCOMPLETE
;
2855 xfs_trans_log_buf(args
->trans
, bp2
,
2856 XFS_DA_LOGRANGE(leaf2
, entry2
, sizeof(*entry2
)));
2857 if ((entry2
->flags
& XFS_ATTR_LOCAL
) == 0) {
2858 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2859 name_rmt
->valueblk
= 0;
2860 name_rmt
->valuelen
= 0;
2861 xfs_trans_log_buf(args
->trans
, bp2
,
2862 XFS_DA_LOGRANGE(leaf2
, name_rmt
, sizeof(*name_rmt
)));
2866 * Commit the flag value change and start the next trans in series.
2868 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);