1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
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_inode_item.h"
21 #include "xfs_bmap_btree.h"
23 #include "xfs_attr_sf.h"
24 #include "xfs_attr_remote.h"
26 #include "xfs_attr_leaf.h"
27 #include "xfs_error.h"
28 #include "xfs_trace.h"
29 #include "xfs_buf_item.h"
30 #include "xfs_cksum.h"
38 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
41 /*========================================================================
42 * Function prototypes for the kernel.
43 *========================================================================*/
46 * Routines used for growing the Btree.
48 STATIC
int xfs_attr3_leaf_create(struct xfs_da_args
*args
,
49 xfs_dablk_t which_block
, struct xfs_buf
**bpp
);
50 STATIC
int xfs_attr3_leaf_add_work(struct xfs_buf
*leaf_buffer
,
51 struct xfs_attr3_icleaf_hdr
*ichdr
,
52 struct xfs_da_args
*args
, int freemap_index
);
53 STATIC
void xfs_attr3_leaf_compact(struct xfs_da_args
*args
,
54 struct xfs_attr3_icleaf_hdr
*ichdr
,
55 struct xfs_buf
*leaf_buffer
);
56 STATIC
void xfs_attr3_leaf_rebalance(xfs_da_state_t
*state
,
57 xfs_da_state_blk_t
*blk1
,
58 xfs_da_state_blk_t
*blk2
);
59 STATIC
int xfs_attr3_leaf_figure_balance(xfs_da_state_t
*state
,
60 xfs_da_state_blk_t
*leaf_blk_1
,
61 struct xfs_attr3_icleaf_hdr
*ichdr1
,
62 xfs_da_state_blk_t
*leaf_blk_2
,
63 struct xfs_attr3_icleaf_hdr
*ichdr2
,
64 int *number_entries_in_blk1
,
65 int *number_usedbytes_in_blk1
);
70 STATIC
void xfs_attr3_leaf_moveents(struct xfs_da_args
*args
,
71 struct xfs_attr_leafblock
*src_leaf
,
72 struct xfs_attr3_icleaf_hdr
*src_ichdr
, int src_start
,
73 struct xfs_attr_leafblock
*dst_leaf
,
74 struct xfs_attr3_icleaf_hdr
*dst_ichdr
, int dst_start
,
76 STATIC
int xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
);
79 * attr3 block 'firstused' conversion helpers.
81 * firstused refers to the offset of the first used byte of the nameval region
82 * of an attr leaf block. The region starts at the tail of the block and expands
83 * backwards towards the middle. As such, firstused is initialized to the block
84 * size for an empty leaf block and is reduced from there.
86 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
87 * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
88 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
89 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
90 * the attr block size. The following helpers manage the conversion between the
91 * in-core and on-disk formats.
95 xfs_attr3_leaf_firstused_from_disk(
96 struct xfs_da_geometry
*geo
,
97 struct xfs_attr3_icleaf_hdr
*to
,
98 struct xfs_attr_leafblock
*from
)
100 struct xfs_attr3_leaf_hdr
*hdr3
;
102 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
103 hdr3
= (struct xfs_attr3_leaf_hdr
*) from
;
104 to
->firstused
= be16_to_cpu(hdr3
->firstused
);
106 to
->firstused
= be16_to_cpu(from
->hdr
.firstused
);
110 * Convert from the magic fsb size value to actual blocksize. This
111 * should only occur for empty blocks when the block size overflows
114 if (to
->firstused
== XFS_ATTR3_LEAF_NULLOFF
) {
115 ASSERT(!to
->count
&& !to
->usedbytes
);
116 ASSERT(geo
->blksize
> USHRT_MAX
);
117 to
->firstused
= geo
->blksize
;
122 xfs_attr3_leaf_firstused_to_disk(
123 struct xfs_da_geometry
*geo
,
124 struct xfs_attr_leafblock
*to
,
125 struct xfs_attr3_icleaf_hdr
*from
)
127 struct xfs_attr3_leaf_hdr
*hdr3
;
130 /* magic value should only be seen on disk */
131 ASSERT(from
->firstused
!= XFS_ATTR3_LEAF_NULLOFF
);
134 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
135 * value. This only overflows at the max supported value of 64k. Use the
136 * magic on-disk value to represent block size in this case.
138 firstused
= from
->firstused
;
139 if (firstused
> USHRT_MAX
) {
140 ASSERT(from
->firstused
== geo
->blksize
);
141 firstused
= XFS_ATTR3_LEAF_NULLOFF
;
144 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
145 hdr3
= (struct xfs_attr3_leaf_hdr
*) to
;
146 hdr3
->firstused
= cpu_to_be16(firstused
);
148 to
->hdr
.firstused
= cpu_to_be16(firstused
);
153 xfs_attr3_leaf_hdr_from_disk(
154 struct xfs_da_geometry
*geo
,
155 struct xfs_attr3_icleaf_hdr
*to
,
156 struct xfs_attr_leafblock
*from
)
160 ASSERT(from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR_LEAF_MAGIC
) ||
161 from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
));
163 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
164 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)from
;
166 to
->forw
= be32_to_cpu(hdr3
->info
.hdr
.forw
);
167 to
->back
= be32_to_cpu(hdr3
->info
.hdr
.back
);
168 to
->magic
= be16_to_cpu(hdr3
->info
.hdr
.magic
);
169 to
->count
= be16_to_cpu(hdr3
->count
);
170 to
->usedbytes
= be16_to_cpu(hdr3
->usedbytes
);
171 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
172 to
->holes
= hdr3
->holes
;
174 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
175 to
->freemap
[i
].base
= be16_to_cpu(hdr3
->freemap
[i
].base
);
176 to
->freemap
[i
].size
= be16_to_cpu(hdr3
->freemap
[i
].size
);
180 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
181 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
182 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
183 to
->count
= be16_to_cpu(from
->hdr
.count
);
184 to
->usedbytes
= be16_to_cpu(from
->hdr
.usedbytes
);
185 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
186 to
->holes
= from
->hdr
.holes
;
188 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
189 to
->freemap
[i
].base
= be16_to_cpu(from
->hdr
.freemap
[i
].base
);
190 to
->freemap
[i
].size
= be16_to_cpu(from
->hdr
.freemap
[i
].size
);
195 xfs_attr3_leaf_hdr_to_disk(
196 struct xfs_da_geometry
*geo
,
197 struct xfs_attr_leafblock
*to
,
198 struct xfs_attr3_icleaf_hdr
*from
)
202 ASSERT(from
->magic
== XFS_ATTR_LEAF_MAGIC
||
203 from
->magic
== XFS_ATTR3_LEAF_MAGIC
);
205 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
206 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)to
;
208 hdr3
->info
.hdr
.forw
= cpu_to_be32(from
->forw
);
209 hdr3
->info
.hdr
.back
= cpu_to_be32(from
->back
);
210 hdr3
->info
.hdr
.magic
= cpu_to_be16(from
->magic
);
211 hdr3
->count
= cpu_to_be16(from
->count
);
212 hdr3
->usedbytes
= cpu_to_be16(from
->usedbytes
);
213 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
214 hdr3
->holes
= from
->holes
;
217 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
218 hdr3
->freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
219 hdr3
->freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
223 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
224 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
225 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
226 to
->hdr
.count
= cpu_to_be16(from
->count
);
227 to
->hdr
.usedbytes
= cpu_to_be16(from
->usedbytes
);
228 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
229 to
->hdr
.holes
= from
->holes
;
232 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
233 to
->hdr
.freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
234 to
->hdr
.freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
238 static xfs_failaddr_t
239 xfs_attr3_leaf_verify(
242 struct xfs_attr3_icleaf_hdr ichdr
;
243 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
244 struct xfs_attr_leafblock
*leaf
= bp
->b_addr
;
245 struct xfs_attr_leaf_entry
*entries
;
249 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, leaf
);
251 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
252 struct xfs_da3_node_hdr
*hdr3
= bp
->b_addr
;
254 if (ichdr
.magic
!= XFS_ATTR3_LEAF_MAGIC
)
255 return __this_address
;
257 if (!uuid_equal(&hdr3
->info
.uuid
, &mp
->m_sb
.sb_meta_uuid
))
258 return __this_address
;
259 if (be64_to_cpu(hdr3
->info
.blkno
) != bp
->b_bn
)
260 return __this_address
;
261 if (!xfs_log_check_lsn(mp
, be64_to_cpu(hdr3
->info
.lsn
)))
262 return __this_address
;
264 if (ichdr
.magic
!= XFS_ATTR_LEAF_MAGIC
)
265 return __this_address
;
268 * In recovery there is a transient state where count == 0 is valid
269 * because we may have transitioned an empty shortform attr to a leaf
270 * if the attr didn't fit in shortform.
272 if (!xfs_log_in_recovery(mp
) && ichdr
.count
== 0)
273 return __this_address
;
276 * firstused is the block offset of the first name info structure.
277 * Make sure it doesn't go off the block or crash into the header.
279 if (ichdr
.firstused
> mp
->m_attr_geo
->blksize
)
280 return __this_address
;
281 if (ichdr
.firstused
< xfs_attr3_leaf_hdr_size(leaf
))
282 return __this_address
;
284 /* Make sure the entries array doesn't crash into the name info. */
285 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
286 if ((char *)&entries
[ichdr
.count
] >
287 (char *)bp
->b_addr
+ ichdr
.firstused
)
288 return __this_address
;
290 /* XXX: need to range check rest of attr header values */
291 /* XXX: hash order check? */
294 * Quickly check the freemap information. Attribute data has to be
295 * aligned to 4-byte boundaries, and likewise for the free space.
297 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
298 if (ichdr
.freemap
[i
].base
> mp
->m_attr_geo
->blksize
)
299 return __this_address
;
300 if (ichdr
.freemap
[i
].base
& 0x3)
301 return __this_address
;
302 if (ichdr
.freemap
[i
].size
> mp
->m_attr_geo
->blksize
)
303 return __this_address
;
304 if (ichdr
.freemap
[i
].size
& 0x3)
305 return __this_address
;
306 end
= ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
;
307 if (end
< ichdr
.freemap
[i
].base
)
308 return __this_address
;
309 if (end
> mp
->m_attr_geo
->blksize
)
310 return __this_address
;
317 xfs_attr3_leaf_write_verify(
320 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
321 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
322 struct xfs_attr3_leaf_hdr
*hdr3
= bp
->b_addr
;
325 fa
= xfs_attr3_leaf_verify(bp
);
327 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
331 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
335 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
337 xfs_buf_update_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
);
341 * leaf/node format detection on trees is sketchy, so a node read can be done on
342 * leaf level blocks when detection identifies the tree as a node format tree
343 * incorrectly. In this case, we need to swap the verifier to match the correct
344 * format of the block being read.
347 xfs_attr3_leaf_read_verify(
350 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
353 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
354 !xfs_buf_verify_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
))
355 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
357 fa
= xfs_attr3_leaf_verify(bp
);
359 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
363 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops
= {
364 .name
= "xfs_attr3_leaf",
365 .verify_read
= xfs_attr3_leaf_read_verify
,
366 .verify_write
= xfs_attr3_leaf_write_verify
,
367 .verify_struct
= xfs_attr3_leaf_verify
,
372 struct xfs_trans
*tp
,
373 struct xfs_inode
*dp
,
375 xfs_daddr_t mappedbno
,
376 struct xfs_buf
**bpp
)
380 err
= xfs_da_read_buf(tp
, dp
, bno
, mappedbno
, bpp
,
381 XFS_ATTR_FORK
, &xfs_attr3_leaf_buf_ops
);
382 if (!err
&& tp
&& *bpp
)
383 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_ATTR_LEAF_BUF
);
387 /*========================================================================
388 * Namespace helper routines
389 *========================================================================*/
392 * If namespace bits don't match return 0.
393 * If all match then return 1.
396 xfs_attr_namesp_match(int arg_flags
, int ondisk_flags
)
398 return XFS_ATTR_NSP_ONDISK(ondisk_flags
) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags
);
402 /*========================================================================
403 * External routines when attribute fork size < XFS_LITINO(mp).
404 *========================================================================*/
407 * Query whether the requested number of additional bytes of extended
408 * attribute space will be able to fit inline.
410 * Returns zero if not, else the di_forkoff fork offset to be used in the
411 * literal area for attribute data once the new bytes have been added.
413 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
414 * special case for dev/uuid inodes, they have fixed size data forks.
417 xfs_attr_shortform_bytesfit(xfs_inode_t
*dp
, int bytes
)
420 int minforkoff
; /* lower limit on valid forkoff locations */
421 int maxforkoff
; /* upper limit on valid forkoff locations */
423 xfs_mount_t
*mp
= dp
->i_mount
;
426 offset
= (XFS_LITINO(mp
, dp
->i_d
.di_version
) - bytes
) >> 3;
428 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_DEV
) {
429 minforkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
430 return (offset
>= minforkoff
) ? minforkoff
: 0;
434 * If the requested numbers of bytes is smaller or equal to the
435 * current attribute fork size we can always proceed.
437 * Note that if_bytes in the data fork might actually be larger than
438 * the current data fork size is due to delalloc extents. In that
439 * case either the extent count will go down when they are converted
440 * to real extents, or the delalloc conversion will take care of the
441 * literal area rebalancing.
443 if (bytes
<= XFS_IFORK_ASIZE(dp
))
444 return dp
->i_d
.di_forkoff
;
447 * For attr2 we can try to move the forkoff if there is space in the
448 * literal area, but for the old format we are done if there is no
449 * space in the fixed attribute fork.
451 if (!(mp
->m_flags
& XFS_MOUNT_ATTR2
))
454 dsize
= dp
->i_df
.if_bytes
;
456 switch (dp
->i_d
.di_format
) {
457 case XFS_DINODE_FMT_EXTENTS
:
459 * If there is no attr fork and the data fork is extents,
460 * determine if creating the default attr fork will result
461 * in the extents form migrating to btree. If so, the
462 * minimum offset only needs to be the space required for
465 if (!dp
->i_d
.di_forkoff
&& dp
->i_df
.if_bytes
>
466 xfs_default_attroffset(dp
))
467 dsize
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
469 case XFS_DINODE_FMT_BTREE
:
471 * If we have a data btree then keep forkoff if we have one,
472 * otherwise we are adding a new attr, so then we set
473 * minforkoff to where the btree root can finish so we have
474 * plenty of room for attrs
476 if (dp
->i_d
.di_forkoff
) {
477 if (offset
< dp
->i_d
.di_forkoff
)
479 return dp
->i_d
.di_forkoff
;
481 dsize
= XFS_BMAP_BROOT_SPACE(mp
, dp
->i_df
.if_broot
);
486 * A data fork btree root must have space for at least
487 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
489 minforkoff
= max(dsize
, XFS_BMDR_SPACE_CALC(MINDBTPTRS
));
490 minforkoff
= roundup(minforkoff
, 8) >> 3;
492 /* attr fork btree root can have at least this many key/ptr pairs */
493 maxforkoff
= XFS_LITINO(mp
, dp
->i_d
.di_version
) -
494 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
495 maxforkoff
= maxforkoff
>> 3; /* rounded down */
497 if (offset
>= maxforkoff
)
499 if (offset
>= minforkoff
)
505 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
508 xfs_sbversion_add_attr2(xfs_mount_t
*mp
, xfs_trans_t
*tp
)
510 if ((mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
511 !(xfs_sb_version_hasattr2(&mp
->m_sb
))) {
512 spin_lock(&mp
->m_sb_lock
);
513 if (!xfs_sb_version_hasattr2(&mp
->m_sb
)) {
514 xfs_sb_version_addattr2(&mp
->m_sb
);
515 spin_unlock(&mp
->m_sb_lock
);
518 spin_unlock(&mp
->m_sb_lock
);
523 * Create the initial contents of a shortform attribute list.
526 xfs_attr_shortform_create(xfs_da_args_t
*args
)
528 xfs_attr_sf_hdr_t
*hdr
;
530 struct xfs_ifork
*ifp
;
532 trace_xfs_attr_sf_create(args
);
538 ASSERT(ifp
->if_bytes
== 0);
539 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) {
540 ifp
->if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
541 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_LOCAL
;
542 ifp
->if_flags
|= XFS_IFINLINE
;
544 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
546 xfs_idata_realloc(dp
, sizeof(*hdr
), XFS_ATTR_FORK
);
547 hdr
= (xfs_attr_sf_hdr_t
*)ifp
->if_u1
.if_data
;
549 hdr
->totsize
= cpu_to_be16(sizeof(*hdr
));
550 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
554 * Add a name/value pair to the shortform attribute list.
555 * Overflow from the inode has already been checked for.
558 xfs_attr_shortform_add(xfs_da_args_t
*args
, int forkoff
)
560 xfs_attr_shortform_t
*sf
;
561 xfs_attr_sf_entry_t
*sfe
;
565 struct xfs_ifork
*ifp
;
567 trace_xfs_attr_sf_add(args
);
571 dp
->i_d
.di_forkoff
= forkoff
;
574 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
575 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
577 for (i
= 0; i
< sf
->hdr
.count
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
579 if (sfe
->namelen
!= args
->namelen
)
581 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
583 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
589 offset
= (char *)sfe
- (char *)sf
;
590 size
= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
591 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
);
592 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
593 sfe
= (xfs_attr_sf_entry_t
*)((char *)sf
+ offset
);
595 sfe
->namelen
= args
->namelen
;
596 sfe
->valuelen
= args
->valuelen
;
597 sfe
->flags
= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
598 memcpy(sfe
->nameval
, args
->name
, args
->namelen
);
599 memcpy(&sfe
->nameval
[args
->namelen
], args
->value
, args
->valuelen
);
601 be16_add_cpu(&sf
->hdr
.totsize
, size
);
602 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
604 xfs_sbversion_add_attr2(mp
, args
->trans
);
608 * After the last attribute is removed revert to original inode format,
609 * making all literal area available to the data fork once more.
612 xfs_attr_fork_remove(
613 struct xfs_inode
*ip
,
614 struct xfs_trans
*tp
)
616 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
617 ip
->i_d
.di_forkoff
= 0;
618 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
620 ASSERT(ip
->i_d
.di_anextents
== 0);
621 ASSERT(ip
->i_afp
== NULL
);
623 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
627 * Remove an attribute from the shortform attribute list structure.
630 xfs_attr_shortform_remove(xfs_da_args_t
*args
)
632 xfs_attr_shortform_t
*sf
;
633 xfs_attr_sf_entry_t
*sfe
;
634 int base
, size
=0, end
, totsize
, i
;
638 trace_xfs_attr_sf_remove(args
);
642 base
= sizeof(xfs_attr_sf_hdr_t
);
643 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
646 for (i
= 0; i
< end
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
),
648 size
= XFS_ATTR_SF_ENTSIZE(sfe
);
649 if (sfe
->namelen
!= args
->namelen
)
651 if (memcmp(sfe
->nameval
, args
->name
, args
->namelen
) != 0)
653 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
661 * Fix up the attribute fork data, covering the hole
664 totsize
= be16_to_cpu(sf
->hdr
.totsize
);
666 memmove(&((char *)sf
)[base
], &((char *)sf
)[end
], totsize
- end
);
668 be16_add_cpu(&sf
->hdr
.totsize
, -size
);
671 * Fix up the start offset of the attribute fork
674 if (totsize
== sizeof(xfs_attr_sf_hdr_t
) &&
675 (mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
676 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
677 !(args
->op_flags
& XFS_DA_OP_ADDNAME
)) {
678 xfs_attr_fork_remove(dp
, args
->trans
);
680 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
681 dp
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(dp
, totsize
);
682 ASSERT(dp
->i_d
.di_forkoff
);
683 ASSERT(totsize
> sizeof(xfs_attr_sf_hdr_t
) ||
684 (args
->op_flags
& XFS_DA_OP_ADDNAME
) ||
685 !(mp
->m_flags
& XFS_MOUNT_ATTR2
) ||
686 dp
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
);
687 xfs_trans_log_inode(args
->trans
, dp
,
688 XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
691 xfs_sbversion_add_attr2(mp
, args
->trans
);
697 * Look up a name in a shortform attribute list structure.
701 xfs_attr_shortform_lookup(xfs_da_args_t
*args
)
703 xfs_attr_shortform_t
*sf
;
704 xfs_attr_sf_entry_t
*sfe
;
706 struct xfs_ifork
*ifp
;
708 trace_xfs_attr_sf_lookup(args
);
710 ifp
= args
->dp
->i_afp
;
711 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
712 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
714 for (i
= 0; i
< sf
->hdr
.count
;
715 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
716 if (sfe
->namelen
!= args
->namelen
)
718 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
720 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
728 * Look up a name in a shortform attribute list structure.
732 xfs_attr_shortform_getvalue(xfs_da_args_t
*args
)
734 xfs_attr_shortform_t
*sf
;
735 xfs_attr_sf_entry_t
*sfe
;
738 ASSERT(args
->dp
->i_afp
->if_flags
== XFS_IFINLINE
);
739 sf
= (xfs_attr_shortform_t
*)args
->dp
->i_afp
->if_u1
.if_data
;
741 for (i
= 0; i
< sf
->hdr
.count
;
742 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
743 if (sfe
->namelen
!= args
->namelen
)
745 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
747 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
749 if (args
->flags
& ATTR_KERNOVAL
) {
750 args
->valuelen
= sfe
->valuelen
;
753 if (args
->valuelen
< sfe
->valuelen
) {
754 args
->valuelen
= sfe
->valuelen
;
757 args
->valuelen
= sfe
->valuelen
;
758 memcpy(args
->value
, &sfe
->nameval
[args
->namelen
],
766 * Convert from using the shortform to the leaf. On success, return the
767 * buffer so that we can keep it locked until we're totally done with it.
770 xfs_attr_shortform_to_leaf(
771 struct xfs_da_args
*args
,
772 struct xfs_buf
**leaf_bp
)
774 struct xfs_inode
*dp
;
775 struct xfs_attr_shortform
*sf
;
776 struct xfs_attr_sf_entry
*sfe
;
777 struct xfs_da_args nargs
;
782 struct xfs_ifork
*ifp
;
784 trace_xfs_attr_sf_to_leaf(args
);
788 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
789 size
= be16_to_cpu(sf
->hdr
.totsize
);
790 tmpbuffer
= kmem_alloc(size
, KM_SLEEP
);
791 ASSERT(tmpbuffer
!= NULL
);
792 memcpy(tmpbuffer
, ifp
->if_u1
.if_data
, size
);
793 sf
= (xfs_attr_shortform_t
*)tmpbuffer
;
795 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
796 xfs_bmap_local_to_extents_empty(dp
, XFS_ATTR_FORK
);
799 error
= xfs_da_grow_inode(args
, &blkno
);
802 * If we hit an IO error middle of the transaction inside
803 * grow_inode(), we may have inconsistent data. Bail out.
807 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
808 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
813 error
= xfs_attr3_leaf_create(args
, blkno
, &bp
);
815 /* xfs_attr3_leaf_create may not have instantiated a block */
816 if (bp
&& (xfs_da_shrink_inode(args
, 0, bp
) != 0))
818 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
819 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
823 memset((char *)&nargs
, 0, sizeof(nargs
));
825 nargs
.geo
= args
->geo
;
826 nargs
.total
= args
->total
;
827 nargs
.whichfork
= XFS_ATTR_FORK
;
828 nargs
.trans
= args
->trans
;
829 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
832 for (i
= 0; i
< sf
->hdr
.count
; i
++) {
833 nargs
.name
= sfe
->nameval
;
834 nargs
.namelen
= sfe
->namelen
;
835 nargs
.value
= &sfe
->nameval
[nargs
.namelen
];
836 nargs
.valuelen
= sfe
->valuelen
;
837 nargs
.hashval
= xfs_da_hashname(sfe
->nameval
,
839 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe
->flags
);
840 error
= xfs_attr3_leaf_lookup_int(bp
, &nargs
); /* set a->index */
841 ASSERT(error
== -ENOATTR
);
842 error
= xfs_attr3_leaf_add(bp
, &nargs
);
843 ASSERT(error
!= -ENOSPC
);
846 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
851 kmem_free(tmpbuffer
);
856 * Check a leaf attribute block to see if all the entries would fit into
857 * a shortform attribute list.
860 xfs_attr_shortform_allfit(
862 struct xfs_inode
*dp
)
864 struct xfs_attr_leafblock
*leaf
;
865 struct xfs_attr_leaf_entry
*entry
;
866 xfs_attr_leaf_name_local_t
*name_loc
;
867 struct xfs_attr3_icleaf_hdr leafhdr
;
870 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
873 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &leafhdr
, leaf
);
874 entry
= xfs_attr3_leaf_entryp(leaf
);
876 bytes
= sizeof(struct xfs_attr_sf_hdr
);
877 for (i
= 0; i
< leafhdr
.count
; entry
++, i
++) {
878 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
879 continue; /* don't copy partial entries */
880 if (!(entry
->flags
& XFS_ATTR_LOCAL
))
882 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
883 if (name_loc
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
885 if (be16_to_cpu(name_loc
->valuelen
) >= XFS_ATTR_SF_ENTSIZE_MAX
)
887 bytes
+= sizeof(struct xfs_attr_sf_entry
) - 1
889 + be16_to_cpu(name_loc
->valuelen
);
891 if ((dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) &&
892 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
893 (bytes
== sizeof(struct xfs_attr_sf_hdr
)))
895 return xfs_attr_shortform_bytesfit(dp
, bytes
);
898 /* Verify the consistency of an inline attribute fork. */
900 xfs_attr_shortform_verify(
901 struct xfs_inode
*ip
)
903 struct xfs_attr_shortform
*sfp
;
904 struct xfs_attr_sf_entry
*sfep
;
905 struct xfs_attr_sf_entry
*next_sfep
;
907 struct xfs_ifork
*ifp
;
911 ASSERT(ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
);
912 ifp
= XFS_IFORK_PTR(ip
, XFS_ATTR_FORK
);
913 sfp
= (struct xfs_attr_shortform
*)ifp
->if_u1
.if_data
;
914 size
= ifp
->if_bytes
;
917 * Give up if the attribute is way too short.
919 if (size
< sizeof(struct xfs_attr_sf_hdr
))
920 return __this_address
;
922 endp
= (char *)sfp
+ size
;
924 /* Check all reported entries */
925 sfep
= &sfp
->list
[0];
926 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
928 * struct xfs_attr_sf_entry has a variable length.
929 * Check the fixed-offset parts of the structure are
930 * within the data buffer.
932 if (((char *)sfep
+ sizeof(*sfep
)) >= endp
)
933 return __this_address
;
935 /* Don't allow names with known bad length. */
936 if (sfep
->namelen
== 0)
937 return __this_address
;
940 * Check that the variable-length part of the structure is
941 * within the data buffer. The next entry starts after the
942 * name component, so nextentry is an acceptable test.
944 next_sfep
= XFS_ATTR_SF_NEXTENTRY(sfep
);
945 if ((char *)next_sfep
> endp
)
946 return __this_address
;
949 * Check for unknown flags. Short form doesn't support
950 * the incomplete or local bits, so we can use the namespace
953 if (sfep
->flags
& ~XFS_ATTR_NSP_ONDISK_MASK
)
954 return __this_address
;
957 * Check for invalid namespace combinations. We only allow
958 * one namespace flag per xattr, so we can just count the
959 * bits (i.e. hweight) here.
961 if (hweight8(sfep
->flags
& XFS_ATTR_NSP_ONDISK_MASK
) > 1)
962 return __this_address
;
966 if ((void *)sfep
!= (void *)endp
)
967 return __this_address
;
973 * Convert a leaf attribute list to shortform attribute list
976 xfs_attr3_leaf_to_shortform(
978 struct xfs_da_args
*args
,
981 struct xfs_attr_leafblock
*leaf
;
982 struct xfs_attr3_icleaf_hdr ichdr
;
983 struct xfs_attr_leaf_entry
*entry
;
984 struct xfs_attr_leaf_name_local
*name_loc
;
985 struct xfs_da_args nargs
;
986 struct xfs_inode
*dp
= args
->dp
;
991 trace_xfs_attr_leaf_to_sf(args
);
993 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, KM_SLEEP
);
997 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
999 leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1000 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1001 entry
= xfs_attr3_leaf_entryp(leaf
);
1003 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1004 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1007 * Clean out the prior contents of the attribute list.
1009 error
= xfs_da_shrink_inode(args
, 0, bp
);
1013 if (forkoff
== -1) {
1014 ASSERT(dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
);
1015 ASSERT(dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
);
1016 xfs_attr_fork_remove(dp
, args
->trans
);
1020 xfs_attr_shortform_create(args
);
1023 * Copy the attributes
1025 memset((char *)&nargs
, 0, sizeof(nargs
));
1026 nargs
.geo
= args
->geo
;
1028 nargs
.total
= args
->total
;
1029 nargs
.whichfork
= XFS_ATTR_FORK
;
1030 nargs
.trans
= args
->trans
;
1031 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
1033 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
1034 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
1035 continue; /* don't copy partial entries */
1036 if (!entry
->nameidx
)
1038 ASSERT(entry
->flags
& XFS_ATTR_LOCAL
);
1039 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
1040 nargs
.name
= name_loc
->nameval
;
1041 nargs
.namelen
= name_loc
->namelen
;
1042 nargs
.value
= &name_loc
->nameval
[nargs
.namelen
];
1043 nargs
.valuelen
= be16_to_cpu(name_loc
->valuelen
);
1044 nargs
.hashval
= be32_to_cpu(entry
->hashval
);
1045 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(entry
->flags
);
1046 xfs_attr_shortform_add(&nargs
, forkoff
);
1051 kmem_free(tmpbuffer
);
1056 * Convert from using a single leaf to a root node and a leaf.
1059 xfs_attr3_leaf_to_node(
1060 struct xfs_da_args
*args
)
1062 struct xfs_attr_leafblock
*leaf
;
1063 struct xfs_attr3_icleaf_hdr icleafhdr
;
1064 struct xfs_attr_leaf_entry
*entries
;
1065 struct xfs_da_node_entry
*btree
;
1066 struct xfs_da3_icnode_hdr icnodehdr
;
1067 struct xfs_da_intnode
*node
;
1068 struct xfs_inode
*dp
= args
->dp
;
1069 struct xfs_mount
*mp
= dp
->i_mount
;
1070 struct xfs_buf
*bp1
= NULL
;
1071 struct xfs_buf
*bp2
= NULL
;
1075 trace_xfs_attr_leaf_to_node(args
);
1077 error
= xfs_da_grow_inode(args
, &blkno
);
1080 error
= xfs_attr3_leaf_read(args
->trans
, dp
, 0, -1, &bp1
);
1084 error
= xfs_da_get_buf(args
->trans
, dp
, blkno
, -1, &bp2
, XFS_ATTR_FORK
);
1088 /* copy leaf to new buffer, update identifiers */
1089 xfs_trans_buf_set_type(args
->trans
, bp2
, XFS_BLFT_ATTR_LEAF_BUF
);
1090 bp2
->b_ops
= bp1
->b_ops
;
1091 memcpy(bp2
->b_addr
, bp1
->b_addr
, args
->geo
->blksize
);
1092 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1093 struct xfs_da3_blkinfo
*hdr3
= bp2
->b_addr
;
1094 hdr3
->blkno
= cpu_to_be64(bp2
->b_bn
);
1096 xfs_trans_log_buf(args
->trans
, bp2
, 0, args
->geo
->blksize
- 1);
1099 * Set up the new root node.
1101 error
= xfs_da3_node_create(args
, 0, 1, &bp1
, XFS_ATTR_FORK
);
1105 dp
->d_ops
->node_hdr_from_disk(&icnodehdr
, node
);
1106 btree
= dp
->d_ops
->node_tree_p(node
);
1109 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &icleafhdr
, leaf
);
1110 entries
= xfs_attr3_leaf_entryp(leaf
);
1112 /* both on-disk, don't endian-flip twice */
1113 btree
[0].hashval
= entries
[icleafhdr
.count
- 1].hashval
;
1114 btree
[0].before
= cpu_to_be32(blkno
);
1115 icnodehdr
.count
= 1;
1116 dp
->d_ops
->node_hdr_to_disk(node
, &icnodehdr
);
1117 xfs_trans_log_buf(args
->trans
, bp1
, 0, args
->geo
->blksize
- 1);
1123 /*========================================================================
1124 * Routines used for growing the Btree.
1125 *========================================================================*/
1128 * Create the initial contents of a leaf attribute list
1129 * or a leaf in a node attribute list.
1132 xfs_attr3_leaf_create(
1133 struct xfs_da_args
*args
,
1135 struct xfs_buf
**bpp
)
1137 struct xfs_attr_leafblock
*leaf
;
1138 struct xfs_attr3_icleaf_hdr ichdr
;
1139 struct xfs_inode
*dp
= args
->dp
;
1140 struct xfs_mount
*mp
= dp
->i_mount
;
1144 trace_xfs_attr_leaf_create(args
);
1146 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp
,
1150 bp
->b_ops
= &xfs_attr3_leaf_buf_ops
;
1151 xfs_trans_buf_set_type(args
->trans
, bp
, XFS_BLFT_ATTR_LEAF_BUF
);
1153 memset(leaf
, 0, args
->geo
->blksize
);
1155 memset(&ichdr
, 0, sizeof(ichdr
));
1156 ichdr
.firstused
= args
->geo
->blksize
;
1158 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1159 struct xfs_da3_blkinfo
*hdr3
= bp
->b_addr
;
1161 ichdr
.magic
= XFS_ATTR3_LEAF_MAGIC
;
1163 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
1164 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
1165 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
1167 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr3_leaf_hdr
);
1169 ichdr
.magic
= XFS_ATTR_LEAF_MAGIC
;
1170 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr_leaf_hdr
);
1172 ichdr
.freemap
[0].size
= ichdr
.firstused
- ichdr
.freemap
[0].base
;
1174 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1175 xfs_trans_log_buf(args
->trans
, bp
, 0, args
->geo
->blksize
- 1);
1182 * Split the leaf node, rebalance, then add the new entry.
1185 xfs_attr3_leaf_split(
1186 struct xfs_da_state
*state
,
1187 struct xfs_da_state_blk
*oldblk
,
1188 struct xfs_da_state_blk
*newblk
)
1193 trace_xfs_attr_leaf_split(state
->args
);
1196 * Allocate space for a new leaf node.
1198 ASSERT(oldblk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1199 error
= xfs_da_grow_inode(state
->args
, &blkno
);
1202 error
= xfs_attr3_leaf_create(state
->args
, blkno
, &newblk
->bp
);
1205 newblk
->blkno
= blkno
;
1206 newblk
->magic
= XFS_ATTR_LEAF_MAGIC
;
1209 * Rebalance the entries across the two leaves.
1210 * NOTE: rebalance() currently depends on the 2nd block being empty.
1212 xfs_attr3_leaf_rebalance(state
, oldblk
, newblk
);
1213 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1218 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1219 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1220 * "new" attrs info. Will need the "old" info to remove it later.
1222 * Insert the "new" entry in the correct block.
1224 if (state
->inleaf
) {
1225 trace_xfs_attr_leaf_add_old(state
->args
);
1226 error
= xfs_attr3_leaf_add(oldblk
->bp
, state
->args
);
1228 trace_xfs_attr_leaf_add_new(state
->args
);
1229 error
= xfs_attr3_leaf_add(newblk
->bp
, state
->args
);
1233 * Update last hashval in each block since we added the name.
1235 oldblk
->hashval
= xfs_attr_leaf_lasthash(oldblk
->bp
, NULL
);
1236 newblk
->hashval
= xfs_attr_leaf_lasthash(newblk
->bp
, NULL
);
1241 * Add a name to the leaf attribute list structure.
1246 struct xfs_da_args
*args
)
1248 struct xfs_attr_leafblock
*leaf
;
1249 struct xfs_attr3_icleaf_hdr ichdr
;
1256 trace_xfs_attr_leaf_add(args
);
1259 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1260 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
.count
);
1261 entsize
= xfs_attr_leaf_newentsize(args
, NULL
);
1264 * Search through freemap for first-fit on new name length.
1265 * (may need to figure in size of entry struct too)
1267 tablesize
= (ichdr
.count
+ 1) * sizeof(xfs_attr_leaf_entry_t
)
1268 + xfs_attr3_leaf_hdr_size(leaf
);
1269 for (sum
= 0, i
= XFS_ATTR_LEAF_MAPSIZE
- 1; i
>= 0; i
--) {
1270 if (tablesize
> ichdr
.firstused
) {
1271 sum
+= ichdr
.freemap
[i
].size
;
1274 if (!ichdr
.freemap
[i
].size
)
1275 continue; /* no space in this map */
1277 if (ichdr
.freemap
[i
].base
< ichdr
.firstused
)
1278 tmp
+= sizeof(xfs_attr_leaf_entry_t
);
1279 if (ichdr
.freemap
[i
].size
>= tmp
) {
1280 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, i
);
1283 sum
+= ichdr
.freemap
[i
].size
;
1287 * If there are no holes in the address space of the block,
1288 * and we don't have enough freespace, then compaction will do us
1289 * no good and we should just give up.
1291 if (!ichdr
.holes
&& sum
< entsize
)
1295 * Compact the entries to coalesce free space.
1296 * This may change the hdr->count via dropping INCOMPLETE entries.
1298 xfs_attr3_leaf_compact(args
, &ichdr
, bp
);
1301 * After compaction, the block is guaranteed to have only one
1302 * free region, in freemap[0]. If it is not big enough, give up.
1304 if (ichdr
.freemap
[0].size
< (entsize
+ sizeof(xfs_attr_leaf_entry_t
))) {
1309 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, 0);
1312 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1313 xfs_trans_log_buf(args
->trans
, bp
,
1314 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
1315 xfs_attr3_leaf_hdr_size(leaf
)));
1320 * Add a name to a leaf attribute list structure.
1323 xfs_attr3_leaf_add_work(
1325 struct xfs_attr3_icleaf_hdr
*ichdr
,
1326 struct xfs_da_args
*args
,
1329 struct xfs_attr_leafblock
*leaf
;
1330 struct xfs_attr_leaf_entry
*entry
;
1331 struct xfs_attr_leaf_name_local
*name_loc
;
1332 struct xfs_attr_leaf_name_remote
*name_rmt
;
1333 struct xfs_mount
*mp
;
1337 trace_xfs_attr_leaf_add_work(args
);
1340 ASSERT(mapindex
>= 0 && mapindex
< XFS_ATTR_LEAF_MAPSIZE
);
1341 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
->count
);
1344 * Force open some space in the entry array and fill it in.
1346 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
1347 if (args
->index
< ichdr
->count
) {
1348 tmp
= ichdr
->count
- args
->index
;
1349 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
1350 memmove(entry
+ 1, entry
, tmp
);
1351 xfs_trans_log_buf(args
->trans
, bp
,
1352 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1357 * Allocate space for the new string (at the end of the run).
1359 mp
= args
->trans
->t_mountp
;
1360 ASSERT(ichdr
->freemap
[mapindex
].base
< args
->geo
->blksize
);
1361 ASSERT((ichdr
->freemap
[mapindex
].base
& 0x3) == 0);
1362 ASSERT(ichdr
->freemap
[mapindex
].size
>=
1363 xfs_attr_leaf_newentsize(args
, NULL
));
1364 ASSERT(ichdr
->freemap
[mapindex
].size
< args
->geo
->blksize
);
1365 ASSERT((ichdr
->freemap
[mapindex
].size
& 0x3) == 0);
1367 ichdr
->freemap
[mapindex
].size
-= xfs_attr_leaf_newentsize(args
, &tmp
);
1369 entry
->nameidx
= cpu_to_be16(ichdr
->freemap
[mapindex
].base
+
1370 ichdr
->freemap
[mapindex
].size
);
1371 entry
->hashval
= cpu_to_be32(args
->hashval
);
1372 entry
->flags
= tmp
? XFS_ATTR_LOCAL
: 0;
1373 entry
->flags
|= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
1374 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1375 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1376 if ((args
->blkno2
== args
->blkno
) &&
1377 (args
->index2
<= args
->index
)) {
1381 xfs_trans_log_buf(args
->trans
, bp
,
1382 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
1383 ASSERT((args
->index
== 0) ||
1384 (be32_to_cpu(entry
->hashval
) >= be32_to_cpu((entry
-1)->hashval
)));
1385 ASSERT((args
->index
== ichdr
->count
- 1) ||
1386 (be32_to_cpu(entry
->hashval
) <= be32_to_cpu((entry
+1)->hashval
)));
1389 * For "remote" attribute values, simply note that we need to
1390 * allocate space for the "remote" value. We can't actually
1391 * allocate the extents in this transaction, and we can't decide
1392 * which blocks they should be as we might allocate more blocks
1393 * as part of this transaction (a split operation for example).
1395 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1396 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
1397 name_loc
->namelen
= args
->namelen
;
1398 name_loc
->valuelen
= cpu_to_be16(args
->valuelen
);
1399 memcpy((char *)name_loc
->nameval
, args
->name
, args
->namelen
);
1400 memcpy((char *)&name_loc
->nameval
[args
->namelen
], args
->value
,
1401 be16_to_cpu(name_loc
->valuelen
));
1403 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
1404 name_rmt
->namelen
= args
->namelen
;
1405 memcpy((char *)name_rmt
->name
, args
->name
, args
->namelen
);
1406 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1408 name_rmt
->valuelen
= 0;
1409 name_rmt
->valueblk
= 0;
1411 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
1412 args
->rmtvaluelen
= args
->valuelen
;
1414 xfs_trans_log_buf(args
->trans
, bp
,
1415 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
1416 xfs_attr_leaf_entsize(leaf
, args
->index
)));
1419 * Update the control info for this leaf node
1421 if (be16_to_cpu(entry
->nameidx
) < ichdr
->firstused
)
1422 ichdr
->firstused
= be16_to_cpu(entry
->nameidx
);
1424 ASSERT(ichdr
->firstused
>= ichdr
->count
* sizeof(xfs_attr_leaf_entry_t
)
1425 + xfs_attr3_leaf_hdr_size(leaf
));
1426 tmp
= (ichdr
->count
- 1) * sizeof(xfs_attr_leaf_entry_t
)
1427 + xfs_attr3_leaf_hdr_size(leaf
);
1429 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
1430 if (ichdr
->freemap
[i
].base
== tmp
) {
1431 ichdr
->freemap
[i
].base
+= sizeof(xfs_attr_leaf_entry_t
);
1432 ichdr
->freemap
[i
].size
-= sizeof(xfs_attr_leaf_entry_t
);
1435 ichdr
->usedbytes
+= xfs_attr_leaf_entsize(leaf
, args
->index
);
1440 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1443 xfs_attr3_leaf_compact(
1444 struct xfs_da_args
*args
,
1445 struct xfs_attr3_icleaf_hdr
*ichdr_dst
,
1448 struct xfs_attr_leafblock
*leaf_src
;
1449 struct xfs_attr_leafblock
*leaf_dst
;
1450 struct xfs_attr3_icleaf_hdr ichdr_src
;
1451 struct xfs_trans
*trans
= args
->trans
;
1454 trace_xfs_attr_leaf_compact(args
);
1456 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, KM_SLEEP
);
1457 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1458 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1459 leaf_src
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1460 leaf_dst
= bp
->b_addr
;
1463 * Copy the on-disk header back into the destination buffer to ensure
1464 * all the information in the header that is not part of the incore
1465 * header structure is preserved.
1467 memcpy(bp
->b_addr
, tmpbuffer
, xfs_attr3_leaf_hdr_size(leaf_src
));
1469 /* Initialise the incore headers */
1470 ichdr_src
= *ichdr_dst
; /* struct copy */
1471 ichdr_dst
->firstused
= args
->geo
->blksize
;
1472 ichdr_dst
->usedbytes
= 0;
1473 ichdr_dst
->count
= 0;
1474 ichdr_dst
->holes
= 0;
1475 ichdr_dst
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_src
);
1476 ichdr_dst
->freemap
[0].size
= ichdr_dst
->firstused
-
1477 ichdr_dst
->freemap
[0].base
;
1479 /* write the header back to initialise the underlying buffer */
1480 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf_dst
, ichdr_dst
);
1483 * Copy all entry's in the same (sorted) order,
1484 * but allocate name/value pairs packed and in sequence.
1486 xfs_attr3_leaf_moveents(args
, leaf_src
, &ichdr_src
, 0,
1487 leaf_dst
, ichdr_dst
, 0, ichdr_src
.count
);
1489 * this logs the entire buffer, but the caller must write the header
1490 * back to the buffer when it is finished modifying it.
1492 xfs_trans_log_buf(trans
, bp
, 0, args
->geo
->blksize
- 1);
1494 kmem_free(tmpbuffer
);
1498 * Compare two leaf blocks "order".
1499 * Return 0 unless leaf2 should go before leaf1.
1502 xfs_attr3_leaf_order(
1503 struct xfs_buf
*leaf1_bp
,
1504 struct xfs_attr3_icleaf_hdr
*leaf1hdr
,
1505 struct xfs_buf
*leaf2_bp
,
1506 struct xfs_attr3_icleaf_hdr
*leaf2hdr
)
1508 struct xfs_attr_leaf_entry
*entries1
;
1509 struct xfs_attr_leaf_entry
*entries2
;
1511 entries1
= xfs_attr3_leaf_entryp(leaf1_bp
->b_addr
);
1512 entries2
= xfs_attr3_leaf_entryp(leaf2_bp
->b_addr
);
1513 if (leaf1hdr
->count
> 0 && leaf2hdr
->count
> 0 &&
1514 ((be32_to_cpu(entries2
[0].hashval
) <
1515 be32_to_cpu(entries1
[0].hashval
)) ||
1516 (be32_to_cpu(entries2
[leaf2hdr
->count
- 1].hashval
) <
1517 be32_to_cpu(entries1
[leaf1hdr
->count
- 1].hashval
)))) {
1524 xfs_attr_leaf_order(
1525 struct xfs_buf
*leaf1_bp
,
1526 struct xfs_buf
*leaf2_bp
)
1528 struct xfs_attr3_icleaf_hdr ichdr1
;
1529 struct xfs_attr3_icleaf_hdr ichdr2
;
1530 struct xfs_mount
*mp
= leaf1_bp
->b_target
->bt_mount
;
1532 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr1
, leaf1_bp
->b_addr
);
1533 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr2
, leaf2_bp
->b_addr
);
1534 return xfs_attr3_leaf_order(leaf1_bp
, &ichdr1
, leaf2_bp
, &ichdr2
);
1538 * Redistribute the attribute list entries between two leaf nodes,
1539 * taking into account the size of the new entry.
1541 * NOTE: if new block is empty, then it will get the upper half of the
1542 * old block. At present, all (one) callers pass in an empty second block.
1544 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1545 * to match what it is doing in splitting the attribute leaf block. Those
1546 * values are used in "atomic rename" operations on attributes. Note that
1547 * the "new" and "old" values can end up in different blocks.
1550 xfs_attr3_leaf_rebalance(
1551 struct xfs_da_state
*state
,
1552 struct xfs_da_state_blk
*blk1
,
1553 struct xfs_da_state_blk
*blk2
)
1555 struct xfs_da_args
*args
;
1556 struct xfs_attr_leafblock
*leaf1
;
1557 struct xfs_attr_leafblock
*leaf2
;
1558 struct xfs_attr3_icleaf_hdr ichdr1
;
1559 struct xfs_attr3_icleaf_hdr ichdr2
;
1560 struct xfs_attr_leaf_entry
*entries1
;
1561 struct xfs_attr_leaf_entry
*entries2
;
1569 * Set up environment.
1571 ASSERT(blk1
->magic
== XFS_ATTR_LEAF_MAGIC
);
1572 ASSERT(blk2
->magic
== XFS_ATTR_LEAF_MAGIC
);
1573 leaf1
= blk1
->bp
->b_addr
;
1574 leaf2
= blk2
->bp
->b_addr
;
1575 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr1
, leaf1
);
1576 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, leaf2
);
1577 ASSERT(ichdr2
.count
== 0);
1580 trace_xfs_attr_leaf_rebalance(args
);
1583 * Check ordering of blocks, reverse if it makes things simpler.
1585 * NOTE: Given that all (current) callers pass in an empty
1586 * second block, this code should never set "swap".
1589 if (xfs_attr3_leaf_order(blk1
->bp
, &ichdr1
, blk2
->bp
, &ichdr2
)) {
1592 /* swap structures rather than reconverting them */
1593 swap(ichdr1
, ichdr2
);
1595 leaf1
= blk1
->bp
->b_addr
;
1596 leaf2
= blk2
->bp
->b_addr
;
1601 * Examine entries until we reduce the absolute difference in
1602 * byte usage between the two blocks to a minimum. Then get
1603 * the direction to copy and the number of elements to move.
1605 * "inleaf" is true if the new entry should be inserted into blk1.
1606 * If "swap" is also true, then reverse the sense of "inleaf".
1608 state
->inleaf
= xfs_attr3_leaf_figure_balance(state
, blk1
, &ichdr1
,
1612 state
->inleaf
= !state
->inleaf
;
1615 * Move any entries required from leaf to leaf:
1617 if (count
< ichdr1
.count
) {
1619 * Figure the total bytes to be added to the destination leaf.
1621 /* number entries being moved */
1622 count
= ichdr1
.count
- count
;
1623 space
= ichdr1
.usedbytes
- totallen
;
1624 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1627 * leaf2 is the destination, compact it if it looks tight.
1629 max
= ichdr2
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1630 max
-= ichdr2
.count
* sizeof(xfs_attr_leaf_entry_t
);
1632 xfs_attr3_leaf_compact(args
, &ichdr2
, blk2
->bp
);
1635 * Move high entries from leaf1 to low end of leaf2.
1637 xfs_attr3_leaf_moveents(args
, leaf1
, &ichdr1
,
1638 ichdr1
.count
- count
, leaf2
, &ichdr2
, 0, count
);
1640 } else if (count
> ichdr1
.count
) {
1642 * I assert that since all callers pass in an empty
1643 * second buffer, this code should never execute.
1648 * Figure the total bytes to be added to the destination leaf.
1650 /* number entries being moved */
1651 count
-= ichdr1
.count
;
1652 space
= totallen
- ichdr1
.usedbytes
;
1653 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1656 * leaf1 is the destination, compact it if it looks tight.
1658 max
= ichdr1
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1659 max
-= ichdr1
.count
* sizeof(xfs_attr_leaf_entry_t
);
1661 xfs_attr3_leaf_compact(args
, &ichdr1
, blk1
->bp
);
1664 * Move low entries from leaf2 to high end of leaf1.
1666 xfs_attr3_leaf_moveents(args
, leaf2
, &ichdr2
, 0, leaf1
, &ichdr1
,
1667 ichdr1
.count
, count
);
1670 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf1
, &ichdr1
);
1671 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf2
, &ichdr2
);
1672 xfs_trans_log_buf(args
->trans
, blk1
->bp
, 0, args
->geo
->blksize
- 1);
1673 xfs_trans_log_buf(args
->trans
, blk2
->bp
, 0, args
->geo
->blksize
- 1);
1676 * Copy out last hashval in each block for B-tree code.
1678 entries1
= xfs_attr3_leaf_entryp(leaf1
);
1679 entries2
= xfs_attr3_leaf_entryp(leaf2
);
1680 blk1
->hashval
= be32_to_cpu(entries1
[ichdr1
.count
- 1].hashval
);
1681 blk2
->hashval
= be32_to_cpu(entries2
[ichdr2
.count
- 1].hashval
);
1684 * Adjust the expected index for insertion.
1685 * NOTE: this code depends on the (current) situation that the
1686 * second block was originally empty.
1688 * If the insertion point moved to the 2nd block, we must adjust
1689 * the index. We must also track the entry just following the
1690 * new entry for use in an "atomic rename" operation, that entry
1691 * is always the "old" entry and the "new" entry is what we are
1692 * inserting. The index/blkno fields refer to the "old" entry,
1693 * while the index2/blkno2 fields refer to the "new" entry.
1695 if (blk1
->index
> ichdr1
.count
) {
1696 ASSERT(state
->inleaf
== 0);
1697 blk2
->index
= blk1
->index
- ichdr1
.count
;
1698 args
->index
= args
->index2
= blk2
->index
;
1699 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1700 } else if (blk1
->index
== ichdr1
.count
) {
1701 if (state
->inleaf
) {
1702 args
->index
= blk1
->index
;
1703 args
->blkno
= blk1
->blkno
;
1705 args
->blkno2
= blk2
->blkno
;
1708 * On a double leaf split, the original attr location
1709 * is already stored in blkno2/index2, so don't
1710 * overwrite it overwise we corrupt the tree.
1712 blk2
->index
= blk1
->index
- ichdr1
.count
;
1713 args
->index
= blk2
->index
;
1714 args
->blkno
= blk2
->blkno
;
1715 if (!state
->extravalid
) {
1717 * set the new attr location to match the old
1718 * one and let the higher level split code
1719 * decide where in the leaf to place it.
1721 args
->index2
= blk2
->index
;
1722 args
->blkno2
= blk2
->blkno
;
1726 ASSERT(state
->inleaf
== 1);
1727 args
->index
= args
->index2
= blk1
->index
;
1728 args
->blkno
= args
->blkno2
= blk1
->blkno
;
1733 * Examine entries until we reduce the absolute difference in
1734 * byte usage between the two blocks to a minimum.
1735 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1736 * GROT: there will always be enough room in either block for a new entry.
1737 * GROT: Do a double-split for this case?
1740 xfs_attr3_leaf_figure_balance(
1741 struct xfs_da_state
*state
,
1742 struct xfs_da_state_blk
*blk1
,
1743 struct xfs_attr3_icleaf_hdr
*ichdr1
,
1744 struct xfs_da_state_blk
*blk2
,
1745 struct xfs_attr3_icleaf_hdr
*ichdr2
,
1749 struct xfs_attr_leafblock
*leaf1
= blk1
->bp
->b_addr
;
1750 struct xfs_attr_leafblock
*leaf2
= blk2
->bp
->b_addr
;
1751 struct xfs_attr_leaf_entry
*entry
;
1762 * Examine entries until we reduce the absolute difference in
1763 * byte usage between the two blocks to a minimum.
1765 max
= ichdr1
->count
+ ichdr2
->count
;
1766 half
= (max
+ 1) * sizeof(*entry
);
1767 half
+= ichdr1
->usedbytes
+ ichdr2
->usedbytes
+
1768 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1770 lastdelta
= state
->args
->geo
->blksize
;
1771 entry
= xfs_attr3_leaf_entryp(leaf1
);
1772 for (count
= index
= 0; count
< max
; entry
++, index
++, count
++) {
1774 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1776 * The new entry is in the first block, account for it.
1778 if (count
== blk1
->index
) {
1779 tmp
= totallen
+ sizeof(*entry
) +
1780 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1781 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1783 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1789 * Wrap around into the second block if necessary.
1791 if (count
== ichdr1
->count
) {
1793 entry
= xfs_attr3_leaf_entryp(leaf1
);
1798 * Figure out if next leaf entry would be too much.
1800 tmp
= totallen
+ sizeof(*entry
) + xfs_attr_leaf_entsize(leaf1
,
1802 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1804 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1810 * Calculate the number of usedbytes that will end up in lower block.
1811 * If new entry not in lower block, fix up the count.
1813 totallen
-= count
* sizeof(*entry
);
1815 totallen
-= sizeof(*entry
) +
1816 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1820 *usedbytesarg
= totallen
;
1824 /*========================================================================
1825 * Routines used for shrinking the Btree.
1826 *========================================================================*/
1829 * Check a leaf block and its neighbors to see if the block should be
1830 * collapsed into one or the other neighbor. Always keep the block
1831 * with the smaller block number.
1832 * If the current block is over 50% full, don't try to join it, return 0.
1833 * If the block is empty, fill in the state structure and return 2.
1834 * If it can be collapsed, fill in the state structure and return 1.
1835 * If nothing can be done, return 0.
1837 * GROT: allow for INCOMPLETE entries in calculation.
1840 xfs_attr3_leaf_toosmall(
1841 struct xfs_da_state
*state
,
1844 struct xfs_attr_leafblock
*leaf
;
1845 struct xfs_da_state_blk
*blk
;
1846 struct xfs_attr3_icleaf_hdr ichdr
;
1855 trace_xfs_attr_leaf_toosmall(state
->args
);
1858 * Check for the degenerate case of the block being over 50% full.
1859 * If so, it's not worth even looking to see if we might be able
1860 * to coalesce with a sibling.
1862 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1863 leaf
= blk
->bp
->b_addr
;
1864 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr
, leaf
);
1865 bytes
= xfs_attr3_leaf_hdr_size(leaf
) +
1866 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
) +
1868 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1869 *action
= 0; /* blk over 50%, don't try to join */
1874 * Check for the degenerate case of the block being empty.
1875 * If the block is empty, we'll simply delete it, no need to
1876 * coalesce it with a sibling block. We choose (arbitrarily)
1877 * to merge with the forward block unless it is NULL.
1879 if (ichdr
.count
== 0) {
1881 * Make altpath point to the block we want to keep and
1882 * path point to the block we want to drop (this one).
1884 forward
= (ichdr
.forw
!= 0);
1885 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1886 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1899 * Examine each sibling block to see if we can coalesce with
1900 * at least 25% free space to spare. We need to figure out
1901 * whether to merge with the forward or the backward block.
1902 * We prefer coalescing with the lower numbered sibling so as
1903 * to shrink an attribute list over time.
1905 /* start with smaller blk num */
1906 forward
= ichdr
.forw
< ichdr
.back
;
1907 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1908 struct xfs_attr3_icleaf_hdr ichdr2
;
1915 error
= xfs_attr3_leaf_read(state
->args
->trans
, state
->args
->dp
,
1920 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, bp
->b_addr
);
1922 bytes
= state
->args
->geo
->blksize
-
1923 (state
->args
->geo
->blksize
>> 2) -
1924 ichdr
.usedbytes
- ichdr2
.usedbytes
-
1925 ((ichdr
.count
+ ichdr2
.count
) *
1926 sizeof(xfs_attr_leaf_entry_t
)) -
1927 xfs_attr3_leaf_hdr_size(leaf
);
1929 xfs_trans_brelse(state
->args
->trans
, bp
);
1931 break; /* fits with at least 25% to spare */
1939 * Make altpath point to the block we want to keep (the lower
1940 * numbered block) and path point to the block we want to drop.
1942 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1943 if (blkno
< blk
->blkno
) {
1944 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1947 error
= xfs_da3_path_shift(state
, &state
->path
, forward
,
1961 * Remove a name from the leaf attribute list structure.
1963 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1964 * If two leaves are 37% full, when combined they will leave 25% free.
1967 xfs_attr3_leaf_remove(
1969 struct xfs_da_args
*args
)
1971 struct xfs_attr_leafblock
*leaf
;
1972 struct xfs_attr3_icleaf_hdr ichdr
;
1973 struct xfs_attr_leaf_entry
*entry
;
1982 trace_xfs_attr_leaf_remove(args
);
1985 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1987 ASSERT(ichdr
.count
> 0 && ichdr
.count
< args
->geo
->blksize
/ 8);
1988 ASSERT(args
->index
>= 0 && args
->index
< ichdr
.count
);
1989 ASSERT(ichdr
.firstused
>= ichdr
.count
* sizeof(*entry
) +
1990 xfs_attr3_leaf_hdr_size(leaf
));
1992 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
1994 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
1995 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
1998 * Scan through free region table:
1999 * check for adjacency of free'd entry with an existing one,
2000 * find smallest free region in case we need to replace it,
2001 * adjust any map that borders the entry table,
2003 tablesize
= ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
)
2004 + xfs_attr3_leaf_hdr_size(leaf
);
2005 tmp
= ichdr
.freemap
[0].size
;
2006 before
= after
= -1;
2007 smallest
= XFS_ATTR_LEAF_MAPSIZE
- 1;
2008 entsize
= xfs_attr_leaf_entsize(leaf
, args
->index
);
2009 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
2010 ASSERT(ichdr
.freemap
[i
].base
< args
->geo
->blksize
);
2011 ASSERT(ichdr
.freemap
[i
].size
< args
->geo
->blksize
);
2012 if (ichdr
.freemap
[i
].base
== tablesize
) {
2013 ichdr
.freemap
[i
].base
-= sizeof(xfs_attr_leaf_entry_t
);
2014 ichdr
.freemap
[i
].size
+= sizeof(xfs_attr_leaf_entry_t
);
2017 if (ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
==
2018 be16_to_cpu(entry
->nameidx
)) {
2020 } else if (ichdr
.freemap
[i
].base
==
2021 (be16_to_cpu(entry
->nameidx
) + entsize
)) {
2023 } else if (ichdr
.freemap
[i
].size
< tmp
) {
2024 tmp
= ichdr
.freemap
[i
].size
;
2030 * Coalesce adjacent freemap regions,
2031 * or replace the smallest region.
2033 if ((before
>= 0) || (after
>= 0)) {
2034 if ((before
>= 0) && (after
>= 0)) {
2035 ichdr
.freemap
[before
].size
+= entsize
;
2036 ichdr
.freemap
[before
].size
+= ichdr
.freemap
[after
].size
;
2037 ichdr
.freemap
[after
].base
= 0;
2038 ichdr
.freemap
[after
].size
= 0;
2039 } else if (before
>= 0) {
2040 ichdr
.freemap
[before
].size
+= entsize
;
2042 ichdr
.freemap
[after
].base
= be16_to_cpu(entry
->nameidx
);
2043 ichdr
.freemap
[after
].size
+= entsize
;
2047 * Replace smallest region (if it is smaller than free'd entry)
2049 if (ichdr
.freemap
[smallest
].size
< entsize
) {
2050 ichdr
.freemap
[smallest
].base
= be16_to_cpu(entry
->nameidx
);
2051 ichdr
.freemap
[smallest
].size
= entsize
;
2056 * Did we remove the first entry?
2058 if (be16_to_cpu(entry
->nameidx
) == ichdr
.firstused
)
2064 * Compress the remaining entries and zero out the removed stuff.
2066 memset(xfs_attr3_leaf_name(leaf
, args
->index
), 0, entsize
);
2067 ichdr
.usedbytes
-= entsize
;
2068 xfs_trans_log_buf(args
->trans
, bp
,
2069 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
2072 tmp
= (ichdr
.count
- args
->index
) * sizeof(xfs_attr_leaf_entry_t
);
2073 memmove(entry
, entry
+ 1, tmp
);
2075 xfs_trans_log_buf(args
->trans
, bp
,
2076 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(xfs_attr_leaf_entry_t
)));
2078 entry
= &xfs_attr3_leaf_entryp(leaf
)[ichdr
.count
];
2079 memset(entry
, 0, sizeof(xfs_attr_leaf_entry_t
));
2082 * If we removed the first entry, re-find the first used byte
2083 * in the name area. Note that if the entry was the "firstused",
2084 * then we don't have a "hole" in our block resulting from
2085 * removing the name.
2088 tmp
= args
->geo
->blksize
;
2089 entry
= xfs_attr3_leaf_entryp(leaf
);
2090 for (i
= ichdr
.count
- 1; i
>= 0; entry
++, i
--) {
2091 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2092 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2094 if (be16_to_cpu(entry
->nameidx
) < tmp
)
2095 tmp
= be16_to_cpu(entry
->nameidx
);
2097 ichdr
.firstused
= tmp
;
2098 ASSERT(ichdr
.firstused
!= 0);
2100 ichdr
.holes
= 1; /* mark as needing compaction */
2102 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
2103 xfs_trans_log_buf(args
->trans
, bp
,
2104 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
2105 xfs_attr3_leaf_hdr_size(leaf
)));
2108 * Check if leaf is less than 50% full, caller may want to
2109 * "join" the leaf with a sibling if so.
2111 tmp
= ichdr
.usedbytes
+ xfs_attr3_leaf_hdr_size(leaf
) +
2112 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
);
2114 return tmp
< args
->geo
->magicpct
; /* leaf is < 37% full */
2118 * Move all the attribute list entries from drop_leaf into save_leaf.
2121 xfs_attr3_leaf_unbalance(
2122 struct xfs_da_state
*state
,
2123 struct xfs_da_state_blk
*drop_blk
,
2124 struct xfs_da_state_blk
*save_blk
)
2126 struct xfs_attr_leafblock
*drop_leaf
= drop_blk
->bp
->b_addr
;
2127 struct xfs_attr_leafblock
*save_leaf
= save_blk
->bp
->b_addr
;
2128 struct xfs_attr3_icleaf_hdr drophdr
;
2129 struct xfs_attr3_icleaf_hdr savehdr
;
2130 struct xfs_attr_leaf_entry
*entry
;
2132 trace_xfs_attr_leaf_unbalance(state
->args
);
2134 drop_leaf
= drop_blk
->bp
->b_addr
;
2135 save_leaf
= save_blk
->bp
->b_addr
;
2136 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &drophdr
, drop_leaf
);
2137 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &savehdr
, save_leaf
);
2138 entry
= xfs_attr3_leaf_entryp(drop_leaf
);
2141 * Save last hashval from dying block for later Btree fixup.
2143 drop_blk
->hashval
= be32_to_cpu(entry
[drophdr
.count
- 1].hashval
);
2146 * Check if we need a temp buffer, or can we do it in place.
2147 * Note that we don't check "leaf" for holes because we will
2148 * always be dropping it, toosmall() decided that for us already.
2150 if (savehdr
.holes
== 0) {
2152 * dest leaf has no holes, so we add there. May need
2153 * to make some room in the entry array.
2155 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2156 drop_blk
->bp
, &drophdr
)) {
2157 xfs_attr3_leaf_moveents(state
->args
,
2158 drop_leaf
, &drophdr
, 0,
2159 save_leaf
, &savehdr
, 0,
2162 xfs_attr3_leaf_moveents(state
->args
,
2163 drop_leaf
, &drophdr
, 0,
2164 save_leaf
, &savehdr
,
2165 savehdr
.count
, drophdr
.count
);
2169 * Destination has holes, so we make a temporary copy
2170 * of the leaf and add them both to that.
2172 struct xfs_attr_leafblock
*tmp_leaf
;
2173 struct xfs_attr3_icleaf_hdr tmphdr
;
2175 tmp_leaf
= kmem_zalloc(state
->args
->geo
->blksize
, KM_SLEEP
);
2178 * Copy the header into the temp leaf so that all the stuff
2179 * not in the incore header is present and gets copied back in
2180 * once we've moved all the entries.
2182 memcpy(tmp_leaf
, save_leaf
, xfs_attr3_leaf_hdr_size(save_leaf
));
2184 memset(&tmphdr
, 0, sizeof(tmphdr
));
2185 tmphdr
.magic
= savehdr
.magic
;
2186 tmphdr
.forw
= savehdr
.forw
;
2187 tmphdr
.back
= savehdr
.back
;
2188 tmphdr
.firstused
= state
->args
->geo
->blksize
;
2190 /* write the header to the temp buffer to initialise it */
2191 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, tmp_leaf
, &tmphdr
);
2193 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2194 drop_blk
->bp
, &drophdr
)) {
2195 xfs_attr3_leaf_moveents(state
->args
,
2196 drop_leaf
, &drophdr
, 0,
2197 tmp_leaf
, &tmphdr
, 0,
2199 xfs_attr3_leaf_moveents(state
->args
,
2200 save_leaf
, &savehdr
, 0,
2201 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2204 xfs_attr3_leaf_moveents(state
->args
,
2205 save_leaf
, &savehdr
, 0,
2206 tmp_leaf
, &tmphdr
, 0,
2208 xfs_attr3_leaf_moveents(state
->args
,
2209 drop_leaf
, &drophdr
, 0,
2210 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2213 memcpy(save_leaf
, tmp_leaf
, state
->args
->geo
->blksize
);
2214 savehdr
= tmphdr
; /* struct copy */
2215 kmem_free(tmp_leaf
);
2218 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, save_leaf
, &savehdr
);
2219 xfs_trans_log_buf(state
->args
->trans
, save_blk
->bp
, 0,
2220 state
->args
->geo
->blksize
- 1);
2223 * Copy out last hashval in each block for B-tree code.
2225 entry
= xfs_attr3_leaf_entryp(save_leaf
);
2226 save_blk
->hashval
= be32_to_cpu(entry
[savehdr
.count
- 1].hashval
);
2229 /*========================================================================
2230 * Routines used for finding things in the Btree.
2231 *========================================================================*/
2234 * Look up a name in a leaf attribute list structure.
2235 * This is the internal routine, it uses the caller's buffer.
2237 * Note that duplicate keys are allowed, but only check within the
2238 * current leaf node. The Btree code must check in adjacent leaf nodes.
2240 * Return in args->index the index into the entry[] array of either
2241 * the found entry, or where the entry should have been (insert before
2244 * Don't change the args->value unless we find the attribute.
2247 xfs_attr3_leaf_lookup_int(
2249 struct xfs_da_args
*args
)
2251 struct xfs_attr_leafblock
*leaf
;
2252 struct xfs_attr3_icleaf_hdr ichdr
;
2253 struct xfs_attr_leaf_entry
*entry
;
2254 struct xfs_attr_leaf_entry
*entries
;
2255 struct xfs_attr_leaf_name_local
*name_loc
;
2256 struct xfs_attr_leaf_name_remote
*name_rmt
;
2257 xfs_dahash_t hashval
;
2261 trace_xfs_attr_leaf_lookup(args
);
2264 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2265 entries
= xfs_attr3_leaf_entryp(leaf
);
2266 if (ichdr
.count
>= args
->geo
->blksize
/ 8)
2267 return -EFSCORRUPTED
;
2270 * Binary search. (note: small blocks will skip this loop)
2272 hashval
= args
->hashval
;
2273 probe
= span
= ichdr
.count
/ 2;
2274 for (entry
= &entries
[probe
]; span
> 4; entry
= &entries
[probe
]) {
2276 if (be32_to_cpu(entry
->hashval
) < hashval
)
2278 else if (be32_to_cpu(entry
->hashval
) > hashval
)
2283 if (!(probe
>= 0 && (!ichdr
.count
|| probe
< ichdr
.count
)))
2284 return -EFSCORRUPTED
;
2285 if (!(span
<= 4 || be32_to_cpu(entry
->hashval
) == hashval
))
2286 return -EFSCORRUPTED
;
2289 * Since we may have duplicate hashval's, find the first matching
2290 * hashval in the leaf.
2292 while (probe
> 0 && be32_to_cpu(entry
->hashval
) >= hashval
) {
2296 while (probe
< ichdr
.count
&&
2297 be32_to_cpu(entry
->hashval
) < hashval
) {
2301 if (probe
== ichdr
.count
|| be32_to_cpu(entry
->hashval
) != hashval
) {
2302 args
->index
= probe
;
2307 * Duplicate keys may be present, so search all of them for a match.
2309 for (; probe
< ichdr
.count
&& (be32_to_cpu(entry
->hashval
) == hashval
);
2312 * GROT: Add code to remove incomplete entries.
2315 * If we are looking for INCOMPLETE entries, show only those.
2316 * If we are looking for complete entries, show only those.
2318 if ((args
->flags
& XFS_ATTR_INCOMPLETE
) !=
2319 (entry
->flags
& XFS_ATTR_INCOMPLETE
)) {
2322 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2323 name_loc
= xfs_attr3_leaf_name_local(leaf
, probe
);
2324 if (name_loc
->namelen
!= args
->namelen
)
2326 if (memcmp(args
->name
, name_loc
->nameval
,
2327 args
->namelen
) != 0)
2329 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2331 args
->index
= probe
;
2334 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, probe
);
2335 if (name_rmt
->namelen
!= args
->namelen
)
2337 if (memcmp(args
->name
, name_rmt
->name
,
2338 args
->namelen
) != 0)
2340 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2342 args
->index
= probe
;
2343 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2344 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2345 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(
2351 args
->index
= probe
;
2356 * Get the value associated with an attribute name from a leaf attribute
2360 xfs_attr3_leaf_getvalue(
2362 struct xfs_da_args
*args
)
2364 struct xfs_attr_leafblock
*leaf
;
2365 struct xfs_attr3_icleaf_hdr ichdr
;
2366 struct xfs_attr_leaf_entry
*entry
;
2367 struct xfs_attr_leaf_name_local
*name_loc
;
2368 struct xfs_attr_leaf_name_remote
*name_rmt
;
2372 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2373 ASSERT(ichdr
.count
< args
->geo
->blksize
/ 8);
2374 ASSERT(args
->index
< ichdr
.count
);
2376 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2377 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2378 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2379 ASSERT(name_loc
->namelen
== args
->namelen
);
2380 ASSERT(memcmp(args
->name
, name_loc
->nameval
, args
->namelen
) == 0);
2381 valuelen
= be16_to_cpu(name_loc
->valuelen
);
2382 if (args
->flags
& ATTR_KERNOVAL
) {
2383 args
->valuelen
= valuelen
;
2386 if (args
->valuelen
< valuelen
) {
2387 args
->valuelen
= valuelen
;
2390 args
->valuelen
= valuelen
;
2391 memcpy(args
->value
, &name_loc
->nameval
[args
->namelen
], valuelen
);
2393 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2394 ASSERT(name_rmt
->namelen
== args
->namelen
);
2395 ASSERT(memcmp(args
->name
, name_rmt
->name
, args
->namelen
) == 0);
2396 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2397 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2398 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(args
->dp
->i_mount
,
2400 if (args
->flags
& ATTR_KERNOVAL
) {
2401 args
->valuelen
= args
->rmtvaluelen
;
2404 if (args
->valuelen
< args
->rmtvaluelen
) {
2405 args
->valuelen
= args
->rmtvaluelen
;
2408 args
->valuelen
= args
->rmtvaluelen
;
2413 /*========================================================================
2415 *========================================================================*/
2418 * Move the indicated entries from one leaf to another.
2419 * NOTE: this routine modifies both source and destination leaves.
2423 xfs_attr3_leaf_moveents(
2424 struct xfs_da_args
*args
,
2425 struct xfs_attr_leafblock
*leaf_s
,
2426 struct xfs_attr3_icleaf_hdr
*ichdr_s
,
2428 struct xfs_attr_leafblock
*leaf_d
,
2429 struct xfs_attr3_icleaf_hdr
*ichdr_d
,
2433 struct xfs_attr_leaf_entry
*entry_s
;
2434 struct xfs_attr_leaf_entry
*entry_d
;
2440 * Check for nothing to do.
2446 * Set up environment.
2448 ASSERT(ichdr_s
->magic
== XFS_ATTR_LEAF_MAGIC
||
2449 ichdr_s
->magic
== XFS_ATTR3_LEAF_MAGIC
);
2450 ASSERT(ichdr_s
->magic
== ichdr_d
->magic
);
2451 ASSERT(ichdr_s
->count
> 0 && ichdr_s
->count
< args
->geo
->blksize
/ 8);
2452 ASSERT(ichdr_s
->firstused
>= (ichdr_s
->count
* sizeof(*entry_s
))
2453 + xfs_attr3_leaf_hdr_size(leaf_s
));
2454 ASSERT(ichdr_d
->count
< args
->geo
->blksize
/ 8);
2455 ASSERT(ichdr_d
->firstused
>= (ichdr_d
->count
* sizeof(*entry_d
))
2456 + xfs_attr3_leaf_hdr_size(leaf_d
));
2458 ASSERT(start_s
< ichdr_s
->count
);
2459 ASSERT(start_d
<= ichdr_d
->count
);
2460 ASSERT(count
<= ichdr_s
->count
);
2464 * Move the entries in the destination leaf up to make a hole?
2466 if (start_d
< ichdr_d
->count
) {
2467 tmp
= ichdr_d
->count
- start_d
;
2468 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2469 entry_s
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2470 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
+ count
];
2471 memmove(entry_d
, entry_s
, tmp
);
2475 * Copy all entry's in the same (sorted) order,
2476 * but allocate attribute info packed and in sequence.
2478 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2479 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2481 for (i
= 0; i
< count
; entry_s
++, entry_d
++, desti
++, i
++) {
2482 ASSERT(be16_to_cpu(entry_s
->nameidx
) >= ichdr_s
->firstused
);
2483 tmp
= xfs_attr_leaf_entsize(leaf_s
, start_s
+ i
);
2486 * Code to drop INCOMPLETE entries. Difficult to use as we
2487 * may also need to change the insertion index. Code turned
2488 * off for 6.2, should be revisited later.
2490 if (entry_s
->flags
& XFS_ATTR_INCOMPLETE
) { /* skip partials? */
2491 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2492 ichdr_s
->usedbytes
-= tmp
;
2493 ichdr_s
->count
-= 1;
2494 entry_d
--; /* to compensate for ++ in loop hdr */
2496 if ((start_s
+ i
) < offset
)
2497 result
++; /* insertion index adjustment */
2500 ichdr_d
->firstused
-= tmp
;
2501 /* both on-disk, don't endian flip twice */
2502 entry_d
->hashval
= entry_s
->hashval
;
2503 entry_d
->nameidx
= cpu_to_be16(ichdr_d
->firstused
);
2504 entry_d
->flags
= entry_s
->flags
;
2505 ASSERT(be16_to_cpu(entry_d
->nameidx
) + tmp
2506 <= args
->geo
->blksize
);
2507 memmove(xfs_attr3_leaf_name(leaf_d
, desti
),
2508 xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), tmp
);
2509 ASSERT(be16_to_cpu(entry_s
->nameidx
) + tmp
2510 <= args
->geo
->blksize
);
2511 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2512 ichdr_s
->usedbytes
-= tmp
;
2513 ichdr_d
->usedbytes
+= tmp
;
2514 ichdr_s
->count
-= 1;
2515 ichdr_d
->count
+= 1;
2516 tmp
= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
)
2517 + xfs_attr3_leaf_hdr_size(leaf_d
);
2518 ASSERT(ichdr_d
->firstused
>= tmp
);
2525 * Zero out the entries we just copied.
2527 if (start_s
== ichdr_s
->count
) {
2528 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2529 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2530 ASSERT(((char *)entry_s
+ tmp
) <=
2531 ((char *)leaf_s
+ args
->geo
->blksize
));
2532 memset(entry_s
, 0, tmp
);
2535 * Move the remaining entries down to fill the hole,
2536 * then zero the entries at the top.
2538 tmp
= (ichdr_s
->count
- count
) * sizeof(xfs_attr_leaf_entry_t
);
2539 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
+ count
];
2540 entry_d
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2541 memmove(entry_d
, entry_s
, tmp
);
2543 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2544 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[ichdr_s
->count
];
2545 ASSERT(((char *)entry_s
+ tmp
) <=
2546 ((char *)leaf_s
+ args
->geo
->blksize
));
2547 memset(entry_s
, 0, tmp
);
2551 * Fill in the freemap information
2553 ichdr_d
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_d
);
2554 ichdr_d
->freemap
[0].base
+= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
);
2555 ichdr_d
->freemap
[0].size
= ichdr_d
->firstused
- ichdr_d
->freemap
[0].base
;
2556 ichdr_d
->freemap
[1].base
= 0;
2557 ichdr_d
->freemap
[2].base
= 0;
2558 ichdr_d
->freemap
[1].size
= 0;
2559 ichdr_d
->freemap
[2].size
= 0;
2560 ichdr_s
->holes
= 1; /* leaf may not be compact */
2564 * Pick up the last hashvalue from a leaf block.
2567 xfs_attr_leaf_lasthash(
2571 struct xfs_attr3_icleaf_hdr ichdr
;
2572 struct xfs_attr_leaf_entry
*entries
;
2573 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2575 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, bp
->b_addr
);
2576 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
2578 *count
= ichdr
.count
;
2581 return be32_to_cpu(entries
[ichdr
.count
- 1].hashval
);
2585 * Calculate the number of bytes used to store the indicated attribute
2586 * (whether local or remote only calculate bytes in this block).
2589 xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
)
2591 struct xfs_attr_leaf_entry
*entries
;
2592 xfs_attr_leaf_name_local_t
*name_loc
;
2593 xfs_attr_leaf_name_remote_t
*name_rmt
;
2596 entries
= xfs_attr3_leaf_entryp(leaf
);
2597 if (entries
[index
].flags
& XFS_ATTR_LOCAL
) {
2598 name_loc
= xfs_attr3_leaf_name_local(leaf
, index
);
2599 size
= xfs_attr_leaf_entsize_local(name_loc
->namelen
,
2600 be16_to_cpu(name_loc
->valuelen
));
2602 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, index
);
2603 size
= xfs_attr_leaf_entsize_remote(name_rmt
->namelen
);
2609 * Calculate the number of bytes that would be required to store the new
2610 * attribute (whether local or remote only calculate bytes in this block).
2611 * This routine decides as a side effect whether the attribute will be
2612 * a "local" or a "remote" attribute.
2615 xfs_attr_leaf_newentsize(
2616 struct xfs_da_args
*args
,
2621 size
= xfs_attr_leaf_entsize_local(args
->namelen
, args
->valuelen
);
2622 if (size
< xfs_attr_leaf_entsize_local_max(args
->geo
->blksize
)) {
2629 return xfs_attr_leaf_entsize_remote(args
->namelen
);
2633 /*========================================================================
2634 * Manage the INCOMPLETE flag in a leaf entry
2635 *========================================================================*/
2638 * Clear the INCOMPLETE flag on an entry in a leaf block.
2641 xfs_attr3_leaf_clearflag(
2642 struct xfs_da_args
*args
)
2644 struct xfs_attr_leafblock
*leaf
;
2645 struct xfs_attr_leaf_entry
*entry
;
2646 struct xfs_attr_leaf_name_remote
*name_rmt
;
2650 struct xfs_attr3_icleaf_hdr ichdr
;
2651 xfs_attr_leaf_name_local_t
*name_loc
;
2656 trace_xfs_attr_leaf_clearflag(args
);
2658 * Set up the operation.
2660 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2665 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2666 ASSERT(entry
->flags
& XFS_ATTR_INCOMPLETE
);
2669 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2670 ASSERT(args
->index
< ichdr
.count
);
2671 ASSERT(args
->index
>= 0);
2673 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2674 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2675 namelen
= name_loc
->namelen
;
2676 name
= (char *)name_loc
->nameval
;
2678 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2679 namelen
= name_rmt
->namelen
;
2680 name
= (char *)name_rmt
->name
;
2682 ASSERT(be32_to_cpu(entry
->hashval
) == args
->hashval
);
2683 ASSERT(namelen
== args
->namelen
);
2684 ASSERT(memcmp(name
, args
->name
, namelen
) == 0);
2687 entry
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2688 xfs_trans_log_buf(args
->trans
, bp
,
2689 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2691 if (args
->rmtblkno
) {
2692 ASSERT((entry
->flags
& XFS_ATTR_LOCAL
) == 0);
2693 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2694 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2695 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2696 xfs_trans_log_buf(args
->trans
, bp
,
2697 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2701 * Commit the flag value change and start the next trans in series.
2703 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2707 * Set the INCOMPLETE flag on an entry in a leaf block.
2710 xfs_attr3_leaf_setflag(
2711 struct xfs_da_args
*args
)
2713 struct xfs_attr_leafblock
*leaf
;
2714 struct xfs_attr_leaf_entry
*entry
;
2715 struct xfs_attr_leaf_name_remote
*name_rmt
;
2719 struct xfs_attr3_icleaf_hdr ichdr
;
2722 trace_xfs_attr_leaf_setflag(args
);
2725 * Set up the operation.
2727 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2733 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2734 ASSERT(args
->index
< ichdr
.count
);
2735 ASSERT(args
->index
>= 0);
2737 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2739 ASSERT((entry
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2740 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
2741 xfs_trans_log_buf(args
->trans
, bp
,
2742 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2743 if ((entry
->flags
& XFS_ATTR_LOCAL
) == 0) {
2744 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2745 name_rmt
->valueblk
= 0;
2746 name_rmt
->valuelen
= 0;
2747 xfs_trans_log_buf(args
->trans
, bp
,
2748 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2752 * Commit the flag value change and start the next trans in series.
2754 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2758 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2759 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2760 * entry given by args->blkno2/index2.
2762 * Note that they could be in different blocks, or in the same block.
2765 xfs_attr3_leaf_flipflags(
2766 struct xfs_da_args
*args
)
2768 struct xfs_attr_leafblock
*leaf1
;
2769 struct xfs_attr_leafblock
*leaf2
;
2770 struct xfs_attr_leaf_entry
*entry1
;
2771 struct xfs_attr_leaf_entry
*entry2
;
2772 struct xfs_attr_leaf_name_remote
*name_rmt
;
2773 struct xfs_buf
*bp1
;
2774 struct xfs_buf
*bp2
;
2777 struct xfs_attr3_icleaf_hdr ichdr1
;
2778 struct xfs_attr3_icleaf_hdr ichdr2
;
2779 xfs_attr_leaf_name_local_t
*name_loc
;
2780 int namelen1
, namelen2
;
2781 char *name1
, *name2
;
2784 trace_xfs_attr_leaf_flipflags(args
);
2787 * Read the block containing the "old" attr
2789 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp1
);
2794 * Read the block containing the "new" attr, if it is different
2796 if (args
->blkno2
!= args
->blkno
) {
2797 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno2
,
2805 leaf1
= bp1
->b_addr
;
2806 entry1
= &xfs_attr3_leaf_entryp(leaf1
)[args
->index
];
2808 leaf2
= bp2
->b_addr
;
2809 entry2
= &xfs_attr3_leaf_entryp(leaf2
)[args
->index2
];
2812 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr1
, leaf1
);
2813 ASSERT(args
->index
< ichdr1
.count
);
2814 ASSERT(args
->index
>= 0);
2816 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr2
, leaf2
);
2817 ASSERT(args
->index2
< ichdr2
.count
);
2818 ASSERT(args
->index2
>= 0);
2820 if (entry1
->flags
& XFS_ATTR_LOCAL
) {
2821 name_loc
= xfs_attr3_leaf_name_local(leaf1
, args
->index
);
2822 namelen1
= name_loc
->namelen
;
2823 name1
= (char *)name_loc
->nameval
;
2825 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2826 namelen1
= name_rmt
->namelen
;
2827 name1
= (char *)name_rmt
->name
;
2829 if (entry2
->flags
& XFS_ATTR_LOCAL
) {
2830 name_loc
= xfs_attr3_leaf_name_local(leaf2
, args
->index2
);
2831 namelen2
= name_loc
->namelen
;
2832 name2
= (char *)name_loc
->nameval
;
2834 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2835 namelen2
= name_rmt
->namelen
;
2836 name2
= (char *)name_rmt
->name
;
2838 ASSERT(be32_to_cpu(entry1
->hashval
) == be32_to_cpu(entry2
->hashval
));
2839 ASSERT(namelen1
== namelen2
);
2840 ASSERT(memcmp(name1
, name2
, namelen1
) == 0);
2843 ASSERT(entry1
->flags
& XFS_ATTR_INCOMPLETE
);
2844 ASSERT((entry2
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2846 entry1
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2847 xfs_trans_log_buf(args
->trans
, bp1
,
2848 XFS_DA_LOGRANGE(leaf1
, entry1
, sizeof(*entry1
)));
2849 if (args
->rmtblkno
) {
2850 ASSERT((entry1
->flags
& XFS_ATTR_LOCAL
) == 0);
2851 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2852 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2853 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2854 xfs_trans_log_buf(args
->trans
, bp1
,
2855 XFS_DA_LOGRANGE(leaf1
, name_rmt
, sizeof(*name_rmt
)));
2858 entry2
->flags
|= XFS_ATTR_INCOMPLETE
;
2859 xfs_trans_log_buf(args
->trans
, bp2
,
2860 XFS_DA_LOGRANGE(leaf2
, entry2
, sizeof(*entry2
)));
2861 if ((entry2
->flags
& XFS_ATTR_LOCAL
) == 0) {
2862 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2863 name_rmt
->valueblk
= 0;
2864 name_rmt
->valuelen
= 0;
2865 xfs_trans_log_buf(args
->trans
, bp2
,
2866 XFS_DA_LOGRANGE(leaf2
, name_rmt
, sizeof(*name_rmt
)));
2870 * Commit the flag value change and start the next trans in series.
2872 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);