1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_da_format.h"
16 #include "xfs_inode.h"
17 #include "xfs_trans.h"
18 #include "xfs_bmap_btree.h"
20 #include "xfs_attr_sf.h"
21 #include "xfs_attr_remote.h"
23 #include "xfs_attr_leaf.h"
24 #include "xfs_trace.h"
31 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
34 /*========================================================================
35 * Function prototypes for the kernel.
36 *========================================================================*/
39 * Routines used for growing the Btree.
41 STATIC
int xfs_attr3_leaf_create(struct xfs_da_args
*args
,
42 xfs_dablk_t which_block
, struct xfs_buf
**bpp
);
43 STATIC
int xfs_attr3_leaf_add_work(struct xfs_buf
*leaf_buffer
,
44 struct xfs_attr3_icleaf_hdr
*ichdr
,
45 struct xfs_da_args
*args
, int freemap_index
);
46 STATIC
void xfs_attr3_leaf_compact(struct xfs_da_args
*args
,
47 struct xfs_attr3_icleaf_hdr
*ichdr
,
48 struct xfs_buf
*leaf_buffer
);
49 STATIC
void xfs_attr3_leaf_rebalance(xfs_da_state_t
*state
,
50 xfs_da_state_blk_t
*blk1
,
51 xfs_da_state_blk_t
*blk2
);
52 STATIC
int xfs_attr3_leaf_figure_balance(xfs_da_state_t
*state
,
53 xfs_da_state_blk_t
*leaf_blk_1
,
54 struct xfs_attr3_icleaf_hdr
*ichdr1
,
55 xfs_da_state_blk_t
*leaf_blk_2
,
56 struct xfs_attr3_icleaf_hdr
*ichdr2
,
57 int *number_entries_in_blk1
,
58 int *number_usedbytes_in_blk1
);
63 STATIC
void xfs_attr3_leaf_moveents(struct xfs_da_args
*args
,
64 struct xfs_attr_leafblock
*src_leaf
,
65 struct xfs_attr3_icleaf_hdr
*src_ichdr
, int src_start
,
66 struct xfs_attr_leafblock
*dst_leaf
,
67 struct xfs_attr3_icleaf_hdr
*dst_ichdr
, int dst_start
,
69 STATIC
int xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
);
72 * attr3 block 'firstused' conversion helpers.
74 * firstused refers to the offset of the first used byte of the nameval region
75 * of an attr leaf block. The region starts at the tail of the block and expands
76 * backwards towards the middle. As such, firstused is initialized to the block
77 * size for an empty leaf block and is reduced from there.
79 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
80 * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
81 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
82 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
83 * the attr block size. The following helpers manage the conversion between the
84 * in-core and on-disk formats.
88 xfs_attr3_leaf_firstused_from_disk(
89 struct xfs_da_geometry
*geo
,
90 struct xfs_attr3_icleaf_hdr
*to
,
91 struct xfs_attr_leafblock
*from
)
93 struct xfs_attr3_leaf_hdr
*hdr3
;
95 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
96 hdr3
= (struct xfs_attr3_leaf_hdr
*) from
;
97 to
->firstused
= be16_to_cpu(hdr3
->firstused
);
99 to
->firstused
= be16_to_cpu(from
->hdr
.firstused
);
103 * Convert from the magic fsb size value to actual blocksize. This
104 * should only occur for empty blocks when the block size overflows
107 if (to
->firstused
== XFS_ATTR3_LEAF_NULLOFF
) {
108 ASSERT(!to
->count
&& !to
->usedbytes
);
109 ASSERT(geo
->blksize
> USHRT_MAX
);
110 to
->firstused
= geo
->blksize
;
115 xfs_attr3_leaf_firstused_to_disk(
116 struct xfs_da_geometry
*geo
,
117 struct xfs_attr_leafblock
*to
,
118 struct xfs_attr3_icleaf_hdr
*from
)
120 struct xfs_attr3_leaf_hdr
*hdr3
;
123 /* magic value should only be seen on disk */
124 ASSERT(from
->firstused
!= XFS_ATTR3_LEAF_NULLOFF
);
127 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
128 * value. This only overflows at the max supported value of 64k. Use the
129 * magic on-disk value to represent block size in this case.
131 firstused
= from
->firstused
;
132 if (firstused
> USHRT_MAX
) {
133 ASSERT(from
->firstused
== geo
->blksize
);
134 firstused
= XFS_ATTR3_LEAF_NULLOFF
;
137 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
138 hdr3
= (struct xfs_attr3_leaf_hdr
*) to
;
139 hdr3
->firstused
= cpu_to_be16(firstused
);
141 to
->hdr
.firstused
= cpu_to_be16(firstused
);
146 xfs_attr3_leaf_hdr_from_disk(
147 struct xfs_da_geometry
*geo
,
148 struct xfs_attr3_icleaf_hdr
*to
,
149 struct xfs_attr_leafblock
*from
)
153 ASSERT(from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR_LEAF_MAGIC
) ||
154 from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
));
156 if (from
->hdr
.info
.magic
== cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
)) {
157 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)from
;
159 to
->forw
= be32_to_cpu(hdr3
->info
.hdr
.forw
);
160 to
->back
= be32_to_cpu(hdr3
->info
.hdr
.back
);
161 to
->magic
= be16_to_cpu(hdr3
->info
.hdr
.magic
);
162 to
->count
= be16_to_cpu(hdr3
->count
);
163 to
->usedbytes
= be16_to_cpu(hdr3
->usedbytes
);
164 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
165 to
->holes
= hdr3
->holes
;
167 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
168 to
->freemap
[i
].base
= be16_to_cpu(hdr3
->freemap
[i
].base
);
169 to
->freemap
[i
].size
= be16_to_cpu(hdr3
->freemap
[i
].size
);
173 to
->forw
= be32_to_cpu(from
->hdr
.info
.forw
);
174 to
->back
= be32_to_cpu(from
->hdr
.info
.back
);
175 to
->magic
= be16_to_cpu(from
->hdr
.info
.magic
);
176 to
->count
= be16_to_cpu(from
->hdr
.count
);
177 to
->usedbytes
= be16_to_cpu(from
->hdr
.usedbytes
);
178 xfs_attr3_leaf_firstused_from_disk(geo
, to
, from
);
179 to
->holes
= from
->hdr
.holes
;
181 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
182 to
->freemap
[i
].base
= be16_to_cpu(from
->hdr
.freemap
[i
].base
);
183 to
->freemap
[i
].size
= be16_to_cpu(from
->hdr
.freemap
[i
].size
);
188 xfs_attr3_leaf_hdr_to_disk(
189 struct xfs_da_geometry
*geo
,
190 struct xfs_attr_leafblock
*to
,
191 struct xfs_attr3_icleaf_hdr
*from
)
195 ASSERT(from
->magic
== XFS_ATTR_LEAF_MAGIC
||
196 from
->magic
== XFS_ATTR3_LEAF_MAGIC
);
198 if (from
->magic
== XFS_ATTR3_LEAF_MAGIC
) {
199 struct xfs_attr3_leaf_hdr
*hdr3
= (struct xfs_attr3_leaf_hdr
*)to
;
201 hdr3
->info
.hdr
.forw
= cpu_to_be32(from
->forw
);
202 hdr3
->info
.hdr
.back
= cpu_to_be32(from
->back
);
203 hdr3
->info
.hdr
.magic
= cpu_to_be16(from
->magic
);
204 hdr3
->count
= cpu_to_be16(from
->count
);
205 hdr3
->usedbytes
= cpu_to_be16(from
->usedbytes
);
206 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
207 hdr3
->holes
= from
->holes
;
210 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
211 hdr3
->freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
212 hdr3
->freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
216 to
->hdr
.info
.forw
= cpu_to_be32(from
->forw
);
217 to
->hdr
.info
.back
= cpu_to_be32(from
->back
);
218 to
->hdr
.info
.magic
= cpu_to_be16(from
->magic
);
219 to
->hdr
.count
= cpu_to_be16(from
->count
);
220 to
->hdr
.usedbytes
= cpu_to_be16(from
->usedbytes
);
221 xfs_attr3_leaf_firstused_to_disk(geo
, to
, from
);
222 to
->hdr
.holes
= from
->holes
;
225 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
226 to
->hdr
.freemap
[i
].base
= cpu_to_be16(from
->freemap
[i
].base
);
227 to
->hdr
.freemap
[i
].size
= cpu_to_be16(from
->freemap
[i
].size
);
231 static xfs_failaddr_t
232 xfs_attr3_leaf_verify(
235 struct xfs_attr3_icleaf_hdr ichdr
;
236 struct xfs_mount
*mp
= bp
->b_mount
;
237 struct xfs_attr_leafblock
*leaf
= bp
->b_addr
;
238 struct xfs_attr_leaf_entry
*entries
;
239 uint32_t end
; /* must be 32bit - see below */
243 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, leaf
);
245 fa
= xfs_da3_blkinfo_verify(bp
, bp
->b_addr
);
250 * In recovery there is a transient state where count == 0 is valid
251 * because we may have transitioned an empty shortform attr to a leaf
252 * if the attr didn't fit in shortform.
254 if (!xfs_log_in_recovery(mp
) && ichdr
.count
== 0)
255 return __this_address
;
258 * firstused is the block offset of the first name info structure.
259 * Make sure it doesn't go off the block or crash into the header.
261 if (ichdr
.firstused
> mp
->m_attr_geo
->blksize
)
262 return __this_address
;
263 if (ichdr
.firstused
< xfs_attr3_leaf_hdr_size(leaf
))
264 return __this_address
;
266 /* Make sure the entries array doesn't crash into the name info. */
267 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
268 if ((char *)&entries
[ichdr
.count
] >
269 (char *)bp
->b_addr
+ ichdr
.firstused
)
270 return __this_address
;
272 /* XXX: need to range check rest of attr header values */
273 /* XXX: hash order check? */
276 * Quickly check the freemap information. Attribute data has to be
277 * aligned to 4-byte boundaries, and likewise for the free space.
279 * Note that for 64k block size filesystems, the freemap entries cannot
280 * overflow as they are only be16 fields. However, when checking end
281 * pointer of the freemap, we have to be careful to detect overflows and
282 * so use uint32_t for those checks.
284 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
285 if (ichdr
.freemap
[i
].base
> mp
->m_attr_geo
->blksize
)
286 return __this_address
;
287 if (ichdr
.freemap
[i
].base
& 0x3)
288 return __this_address
;
289 if (ichdr
.freemap
[i
].size
> mp
->m_attr_geo
->blksize
)
290 return __this_address
;
291 if (ichdr
.freemap
[i
].size
& 0x3)
292 return __this_address
;
294 /* be care of 16 bit overflows here */
295 end
= (uint32_t)ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
;
296 if (end
< ichdr
.freemap
[i
].base
)
297 return __this_address
;
298 if (end
> mp
->m_attr_geo
->blksize
)
299 return __this_address
;
306 xfs_attr3_leaf_write_verify(
309 struct xfs_mount
*mp
= bp
->b_mount
;
310 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
311 struct xfs_attr3_leaf_hdr
*hdr3
= bp
->b_addr
;
314 fa
= xfs_attr3_leaf_verify(bp
);
316 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
320 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
324 hdr3
->info
.lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
326 xfs_buf_update_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
);
330 * leaf/node format detection on trees is sketchy, so a node read can be done on
331 * leaf level blocks when detection identifies the tree as a node format tree
332 * incorrectly. In this case, we need to swap the verifier to match the correct
333 * format of the block being read.
336 xfs_attr3_leaf_read_verify(
339 struct xfs_mount
*mp
= bp
->b_mount
;
342 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
343 !xfs_buf_verify_cksum(bp
, XFS_ATTR3_LEAF_CRC_OFF
))
344 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
346 fa
= xfs_attr3_leaf_verify(bp
);
348 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
352 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops
= {
353 .name
= "xfs_attr3_leaf",
354 .magic16
= { cpu_to_be16(XFS_ATTR_LEAF_MAGIC
),
355 cpu_to_be16(XFS_ATTR3_LEAF_MAGIC
) },
356 .verify_read
= xfs_attr3_leaf_read_verify
,
357 .verify_write
= xfs_attr3_leaf_write_verify
,
358 .verify_struct
= xfs_attr3_leaf_verify
,
363 struct xfs_trans
*tp
,
364 struct xfs_inode
*dp
,
366 xfs_daddr_t mappedbno
,
367 struct xfs_buf
**bpp
)
371 err
= xfs_da_read_buf(tp
, dp
, bno
, mappedbno
, bpp
,
372 XFS_ATTR_FORK
, &xfs_attr3_leaf_buf_ops
);
373 if (!err
&& tp
&& *bpp
)
374 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_ATTR_LEAF_BUF
);
378 /*========================================================================
379 * Namespace helper routines
380 *========================================================================*/
383 * If namespace bits don't match return 0.
384 * If all match then return 1.
387 xfs_attr_namesp_match(int arg_flags
, int ondisk_flags
)
389 return XFS_ATTR_NSP_ONDISK(ondisk_flags
) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags
);
394 struct xfs_da_args
*args
,
395 unsigned char *value
,
399 * No copy if all we have to do is get the length
401 if (args
->flags
& ATTR_KERNOVAL
) {
402 args
->valuelen
= valuelen
;
407 * No copy if the length of the existing buffer is too small
409 if (args
->valuelen
< valuelen
) {
410 args
->valuelen
= valuelen
;
413 args
->valuelen
= valuelen
;
415 /* remote block xattr requires IO for copy-in */
417 return xfs_attr_rmtval_get(args
);
420 * This is to prevent a GCC warning because the remote xattr case
421 * doesn't have a value to pass in. In that case, we never reach here,
422 * but GCC can't work that out and so throws a "passing NULL to
427 memcpy(args
->value
, value
, valuelen
);
431 /*========================================================================
432 * External routines when attribute fork size < XFS_LITINO(mp).
433 *========================================================================*/
436 * Query whether the requested number of additional bytes of extended
437 * attribute space will be able to fit inline.
439 * Returns zero if not, else the di_forkoff fork offset to be used in the
440 * literal area for attribute data once the new bytes have been added.
442 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
443 * special case for dev/uuid inodes, they have fixed size data forks.
446 xfs_attr_shortform_bytesfit(xfs_inode_t
*dp
, int bytes
)
449 int minforkoff
; /* lower limit on valid forkoff locations */
450 int maxforkoff
; /* upper limit on valid forkoff locations */
452 xfs_mount_t
*mp
= dp
->i_mount
;
455 offset
= (XFS_LITINO(mp
, dp
->i_d
.di_version
) - bytes
) >> 3;
457 if (dp
->i_d
.di_format
== XFS_DINODE_FMT_DEV
) {
458 minforkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
459 return (offset
>= minforkoff
) ? minforkoff
: 0;
463 * If the requested numbers of bytes is smaller or equal to the
464 * current attribute fork size we can always proceed.
466 * Note that if_bytes in the data fork might actually be larger than
467 * the current data fork size is due to delalloc extents. In that
468 * case either the extent count will go down when they are converted
469 * to real extents, or the delalloc conversion will take care of the
470 * literal area rebalancing.
472 if (bytes
<= XFS_IFORK_ASIZE(dp
))
473 return dp
->i_d
.di_forkoff
;
476 * For attr2 we can try to move the forkoff if there is space in the
477 * literal area, but for the old format we are done if there is no
478 * space in the fixed attribute fork.
480 if (!(mp
->m_flags
& XFS_MOUNT_ATTR2
))
483 dsize
= dp
->i_df
.if_bytes
;
485 switch (dp
->i_d
.di_format
) {
486 case XFS_DINODE_FMT_EXTENTS
:
488 * If there is no attr fork and the data fork is extents,
489 * determine if creating the default attr fork will result
490 * in the extents form migrating to btree. If so, the
491 * minimum offset only needs to be the space required for
494 if (!dp
->i_d
.di_forkoff
&& dp
->i_df
.if_bytes
>
495 xfs_default_attroffset(dp
))
496 dsize
= XFS_BMDR_SPACE_CALC(MINDBTPTRS
);
498 case XFS_DINODE_FMT_BTREE
:
500 * If we have a data btree then keep forkoff if we have one,
501 * otherwise we are adding a new attr, so then we set
502 * minforkoff to where the btree root can finish so we have
503 * plenty of room for attrs
505 if (dp
->i_d
.di_forkoff
) {
506 if (offset
< dp
->i_d
.di_forkoff
)
508 return dp
->i_d
.di_forkoff
;
510 dsize
= XFS_BMAP_BROOT_SPACE(mp
, dp
->i_df
.if_broot
);
515 * A data fork btree root must have space for at least
516 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
518 minforkoff
= max(dsize
, XFS_BMDR_SPACE_CALC(MINDBTPTRS
));
519 minforkoff
= roundup(minforkoff
, 8) >> 3;
521 /* attr fork btree root can have at least this many key/ptr pairs */
522 maxforkoff
= XFS_LITINO(mp
, dp
->i_d
.di_version
) -
523 XFS_BMDR_SPACE_CALC(MINABTPTRS
);
524 maxforkoff
= maxforkoff
>> 3; /* rounded down */
526 if (offset
>= maxforkoff
)
528 if (offset
>= minforkoff
)
534 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
537 xfs_sbversion_add_attr2(xfs_mount_t
*mp
, xfs_trans_t
*tp
)
539 if ((mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
540 !(xfs_sb_version_hasattr2(&mp
->m_sb
))) {
541 spin_lock(&mp
->m_sb_lock
);
542 if (!xfs_sb_version_hasattr2(&mp
->m_sb
)) {
543 xfs_sb_version_addattr2(&mp
->m_sb
);
544 spin_unlock(&mp
->m_sb_lock
);
547 spin_unlock(&mp
->m_sb_lock
);
552 * Create the initial contents of a shortform attribute list.
555 xfs_attr_shortform_create(xfs_da_args_t
*args
)
557 xfs_attr_sf_hdr_t
*hdr
;
559 struct xfs_ifork
*ifp
;
561 trace_xfs_attr_sf_create(args
);
567 ASSERT(ifp
->if_bytes
== 0);
568 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) {
569 ifp
->if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
570 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_LOCAL
;
571 ifp
->if_flags
|= XFS_IFINLINE
;
573 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
575 xfs_idata_realloc(dp
, sizeof(*hdr
), XFS_ATTR_FORK
);
576 hdr
= (xfs_attr_sf_hdr_t
*)ifp
->if_u1
.if_data
;
578 hdr
->totsize
= cpu_to_be16(sizeof(*hdr
));
579 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
583 * Add a name/value pair to the shortform attribute list.
584 * Overflow from the inode has already been checked for.
587 xfs_attr_shortform_add(xfs_da_args_t
*args
, int forkoff
)
589 xfs_attr_shortform_t
*sf
;
590 xfs_attr_sf_entry_t
*sfe
;
594 struct xfs_ifork
*ifp
;
596 trace_xfs_attr_sf_add(args
);
600 dp
->i_d
.di_forkoff
= forkoff
;
603 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
604 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
606 for (i
= 0; i
< sf
->hdr
.count
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
608 if (sfe
->namelen
!= args
->namelen
)
610 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
612 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
618 offset
= (char *)sfe
- (char *)sf
;
619 size
= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
620 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
);
621 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
622 sfe
= (xfs_attr_sf_entry_t
*)((char *)sf
+ offset
);
624 sfe
->namelen
= args
->namelen
;
625 sfe
->valuelen
= args
->valuelen
;
626 sfe
->flags
= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
627 memcpy(sfe
->nameval
, args
->name
, args
->namelen
);
628 memcpy(&sfe
->nameval
[args
->namelen
], args
->value
, args
->valuelen
);
630 be16_add_cpu(&sf
->hdr
.totsize
, size
);
631 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
633 xfs_sbversion_add_attr2(mp
, args
->trans
);
637 * After the last attribute is removed revert to original inode format,
638 * making all literal area available to the data fork once more.
641 xfs_attr_fork_remove(
642 struct xfs_inode
*ip
,
643 struct xfs_trans
*tp
)
645 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
646 ip
->i_d
.di_forkoff
= 0;
647 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
649 ASSERT(ip
->i_d
.di_anextents
== 0);
650 ASSERT(ip
->i_afp
== NULL
);
652 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
656 * Remove an attribute from the shortform attribute list structure.
659 xfs_attr_shortform_remove(xfs_da_args_t
*args
)
661 xfs_attr_shortform_t
*sf
;
662 xfs_attr_sf_entry_t
*sfe
;
663 int base
, size
=0, end
, totsize
, i
;
667 trace_xfs_attr_sf_remove(args
);
671 base
= sizeof(xfs_attr_sf_hdr_t
);
672 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
675 for (i
= 0; i
< end
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
),
677 size
= XFS_ATTR_SF_ENTSIZE(sfe
);
678 if (sfe
->namelen
!= args
->namelen
)
680 if (memcmp(sfe
->nameval
, args
->name
, args
->namelen
) != 0)
682 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
690 * Fix up the attribute fork data, covering the hole
693 totsize
= be16_to_cpu(sf
->hdr
.totsize
);
695 memmove(&((char *)sf
)[base
], &((char *)sf
)[end
], totsize
- end
);
697 be16_add_cpu(&sf
->hdr
.totsize
, -size
);
700 * Fix up the start offset of the attribute fork
703 if (totsize
== sizeof(xfs_attr_sf_hdr_t
) &&
704 (mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
705 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
706 !(args
->op_flags
& XFS_DA_OP_ADDNAME
)) {
707 xfs_attr_fork_remove(dp
, args
->trans
);
709 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
710 dp
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(dp
, totsize
);
711 ASSERT(dp
->i_d
.di_forkoff
);
712 ASSERT(totsize
> sizeof(xfs_attr_sf_hdr_t
) ||
713 (args
->op_flags
& XFS_DA_OP_ADDNAME
) ||
714 !(mp
->m_flags
& XFS_MOUNT_ATTR2
) ||
715 dp
->i_d
.di_format
== XFS_DINODE_FMT_BTREE
);
716 xfs_trans_log_inode(args
->trans
, dp
,
717 XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
720 xfs_sbversion_add_attr2(mp
, args
->trans
);
726 * Look up a name in a shortform attribute list structure.
730 xfs_attr_shortform_lookup(xfs_da_args_t
*args
)
732 xfs_attr_shortform_t
*sf
;
733 xfs_attr_sf_entry_t
*sfe
;
735 struct xfs_ifork
*ifp
;
737 trace_xfs_attr_sf_lookup(args
);
739 ifp
= args
->dp
->i_afp
;
740 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
741 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
743 for (i
= 0; i
< sf
->hdr
.count
;
744 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
745 if (sfe
->namelen
!= args
->namelen
)
747 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
749 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
757 * Retreive the attribute value and length.
759 * If ATTR_KERNOVAL is specified, only the length needs to be returned.
760 * Unlike a lookup, we only return an error if the attribute does not
761 * exist or we can't retrieve the value.
764 xfs_attr_shortform_getvalue(
765 struct xfs_da_args
*args
)
767 struct xfs_attr_shortform
*sf
;
768 struct xfs_attr_sf_entry
*sfe
;
771 ASSERT(args
->dp
->i_afp
->if_flags
== XFS_IFINLINE
);
772 sf
= (xfs_attr_shortform_t
*)args
->dp
->i_afp
->if_u1
.if_data
;
774 for (i
= 0; i
< sf
->hdr
.count
;
775 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
776 if (sfe
->namelen
!= args
->namelen
)
778 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
780 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
782 return xfs_attr_copy_value(args
, &sfe
->nameval
[args
->namelen
],
789 * Convert from using the shortform to the leaf. On success, return the
790 * buffer so that we can keep it locked until we're totally done with it.
793 xfs_attr_shortform_to_leaf(
794 struct xfs_da_args
*args
,
795 struct xfs_buf
**leaf_bp
)
797 struct xfs_inode
*dp
;
798 struct xfs_attr_shortform
*sf
;
799 struct xfs_attr_sf_entry
*sfe
;
800 struct xfs_da_args nargs
;
805 struct xfs_ifork
*ifp
;
807 trace_xfs_attr_sf_to_leaf(args
);
811 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
812 size
= be16_to_cpu(sf
->hdr
.totsize
);
813 tmpbuffer
= kmem_alloc(size
, 0);
814 ASSERT(tmpbuffer
!= NULL
);
815 memcpy(tmpbuffer
, ifp
->if_u1
.if_data
, size
);
816 sf
= (xfs_attr_shortform_t
*)tmpbuffer
;
818 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
819 xfs_bmap_local_to_extents_empty(dp
, XFS_ATTR_FORK
);
822 error
= xfs_da_grow_inode(args
, &blkno
);
825 * If we hit an IO error middle of the transaction inside
826 * grow_inode(), we may have inconsistent data. Bail out.
830 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
831 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
836 error
= xfs_attr3_leaf_create(args
, blkno
, &bp
);
838 /* xfs_attr3_leaf_create may not have instantiated a block */
839 if (bp
&& (xfs_da_shrink_inode(args
, 0, bp
) != 0))
841 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
842 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
846 memset((char *)&nargs
, 0, sizeof(nargs
));
848 nargs
.geo
= args
->geo
;
849 nargs
.total
= args
->total
;
850 nargs
.whichfork
= XFS_ATTR_FORK
;
851 nargs
.trans
= args
->trans
;
852 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
855 for (i
= 0; i
< sf
->hdr
.count
; i
++) {
856 nargs
.name
= sfe
->nameval
;
857 nargs
.namelen
= sfe
->namelen
;
858 nargs
.value
= &sfe
->nameval
[nargs
.namelen
];
859 nargs
.valuelen
= sfe
->valuelen
;
860 nargs
.hashval
= xfs_da_hashname(sfe
->nameval
,
862 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe
->flags
);
863 error
= xfs_attr3_leaf_lookup_int(bp
, &nargs
); /* set a->index */
864 ASSERT(error
== -ENOATTR
);
865 error
= xfs_attr3_leaf_add(bp
, &nargs
);
866 ASSERT(error
!= -ENOSPC
);
869 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
874 kmem_free(tmpbuffer
);
879 * Check a leaf attribute block to see if all the entries would fit into
880 * a shortform attribute list.
883 xfs_attr_shortform_allfit(
885 struct xfs_inode
*dp
)
887 struct xfs_attr_leafblock
*leaf
;
888 struct xfs_attr_leaf_entry
*entry
;
889 xfs_attr_leaf_name_local_t
*name_loc
;
890 struct xfs_attr3_icleaf_hdr leafhdr
;
893 struct xfs_mount
*mp
= bp
->b_mount
;
896 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &leafhdr
, leaf
);
897 entry
= xfs_attr3_leaf_entryp(leaf
);
899 bytes
= sizeof(struct xfs_attr_sf_hdr
);
900 for (i
= 0; i
< leafhdr
.count
; entry
++, i
++) {
901 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
902 continue; /* don't copy partial entries */
903 if (!(entry
->flags
& XFS_ATTR_LOCAL
))
905 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
906 if (name_loc
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
908 if (be16_to_cpu(name_loc
->valuelen
) >= XFS_ATTR_SF_ENTSIZE_MAX
)
910 bytes
+= sizeof(struct xfs_attr_sf_entry
) - 1
912 + be16_to_cpu(name_loc
->valuelen
);
914 if ((dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) &&
915 (dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
) &&
916 (bytes
== sizeof(struct xfs_attr_sf_hdr
)))
918 return xfs_attr_shortform_bytesfit(dp
, bytes
);
921 /* Verify the consistency of an inline attribute fork. */
923 xfs_attr_shortform_verify(
924 struct xfs_inode
*ip
)
926 struct xfs_attr_shortform
*sfp
;
927 struct xfs_attr_sf_entry
*sfep
;
928 struct xfs_attr_sf_entry
*next_sfep
;
930 struct xfs_ifork
*ifp
;
934 ASSERT(ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
);
935 ifp
= XFS_IFORK_PTR(ip
, XFS_ATTR_FORK
);
936 sfp
= (struct xfs_attr_shortform
*)ifp
->if_u1
.if_data
;
937 size
= ifp
->if_bytes
;
940 * Give up if the attribute is way too short.
942 if (size
< sizeof(struct xfs_attr_sf_hdr
))
943 return __this_address
;
945 endp
= (char *)sfp
+ size
;
947 /* Check all reported entries */
948 sfep
= &sfp
->list
[0];
949 for (i
= 0; i
< sfp
->hdr
.count
; i
++) {
951 * struct xfs_attr_sf_entry has a variable length.
952 * Check the fixed-offset parts of the structure are
953 * within the data buffer.
955 if (((char *)sfep
+ sizeof(*sfep
)) >= endp
)
956 return __this_address
;
958 /* Don't allow names with known bad length. */
959 if (sfep
->namelen
== 0)
960 return __this_address
;
963 * Check that the variable-length part of the structure is
964 * within the data buffer. The next entry starts after the
965 * name component, so nextentry is an acceptable test.
967 next_sfep
= XFS_ATTR_SF_NEXTENTRY(sfep
);
968 if ((char *)next_sfep
> endp
)
969 return __this_address
;
972 * Check for unknown flags. Short form doesn't support
973 * the incomplete or local bits, so we can use the namespace
976 if (sfep
->flags
& ~XFS_ATTR_NSP_ONDISK_MASK
)
977 return __this_address
;
980 * Check for invalid namespace combinations. We only allow
981 * one namespace flag per xattr, so we can just count the
982 * bits (i.e. hweight) here.
984 if (hweight8(sfep
->flags
& XFS_ATTR_NSP_ONDISK_MASK
) > 1)
985 return __this_address
;
989 if ((void *)sfep
!= (void *)endp
)
990 return __this_address
;
996 * Convert a leaf attribute list to shortform attribute list
999 xfs_attr3_leaf_to_shortform(
1001 struct xfs_da_args
*args
,
1004 struct xfs_attr_leafblock
*leaf
;
1005 struct xfs_attr3_icleaf_hdr ichdr
;
1006 struct xfs_attr_leaf_entry
*entry
;
1007 struct xfs_attr_leaf_name_local
*name_loc
;
1008 struct xfs_da_args nargs
;
1009 struct xfs_inode
*dp
= args
->dp
;
1014 trace_xfs_attr_leaf_to_sf(args
);
1016 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, 0);
1020 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1022 leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1023 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1024 entry
= xfs_attr3_leaf_entryp(leaf
);
1026 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1027 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1030 * Clean out the prior contents of the attribute list.
1032 error
= xfs_da_shrink_inode(args
, 0, bp
);
1036 if (forkoff
== -1) {
1037 ASSERT(dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
);
1038 ASSERT(dp
->i_d
.di_format
!= XFS_DINODE_FMT_BTREE
);
1039 xfs_attr_fork_remove(dp
, args
->trans
);
1043 xfs_attr_shortform_create(args
);
1046 * Copy the attributes
1048 memset((char *)&nargs
, 0, sizeof(nargs
));
1049 nargs
.geo
= args
->geo
;
1051 nargs
.total
= args
->total
;
1052 nargs
.whichfork
= XFS_ATTR_FORK
;
1053 nargs
.trans
= args
->trans
;
1054 nargs
.op_flags
= XFS_DA_OP_OKNOENT
;
1056 for (i
= 0; i
< ichdr
.count
; entry
++, i
++) {
1057 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
1058 continue; /* don't copy partial entries */
1059 if (!entry
->nameidx
)
1061 ASSERT(entry
->flags
& XFS_ATTR_LOCAL
);
1062 name_loc
= xfs_attr3_leaf_name_local(leaf
, i
);
1063 nargs
.name
= name_loc
->nameval
;
1064 nargs
.namelen
= name_loc
->namelen
;
1065 nargs
.value
= &name_loc
->nameval
[nargs
.namelen
];
1066 nargs
.valuelen
= be16_to_cpu(name_loc
->valuelen
);
1067 nargs
.hashval
= be32_to_cpu(entry
->hashval
);
1068 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(entry
->flags
);
1069 xfs_attr_shortform_add(&nargs
, forkoff
);
1074 kmem_free(tmpbuffer
);
1079 * Convert from using a single leaf to a root node and a leaf.
1082 xfs_attr3_leaf_to_node(
1083 struct xfs_da_args
*args
)
1085 struct xfs_attr_leafblock
*leaf
;
1086 struct xfs_attr3_icleaf_hdr icleafhdr
;
1087 struct xfs_attr_leaf_entry
*entries
;
1088 struct xfs_da_node_entry
*btree
;
1089 struct xfs_da3_icnode_hdr icnodehdr
;
1090 struct xfs_da_intnode
*node
;
1091 struct xfs_inode
*dp
= args
->dp
;
1092 struct xfs_mount
*mp
= dp
->i_mount
;
1093 struct xfs_buf
*bp1
= NULL
;
1094 struct xfs_buf
*bp2
= NULL
;
1098 trace_xfs_attr_leaf_to_node(args
);
1100 error
= xfs_da_grow_inode(args
, &blkno
);
1103 error
= xfs_attr3_leaf_read(args
->trans
, dp
, 0, -1, &bp1
);
1107 error
= xfs_da_get_buf(args
->trans
, dp
, blkno
, -1, &bp2
, XFS_ATTR_FORK
);
1111 /* copy leaf to new buffer, update identifiers */
1112 xfs_trans_buf_set_type(args
->trans
, bp2
, XFS_BLFT_ATTR_LEAF_BUF
);
1113 bp2
->b_ops
= bp1
->b_ops
;
1114 memcpy(bp2
->b_addr
, bp1
->b_addr
, args
->geo
->blksize
);
1115 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1116 struct xfs_da3_blkinfo
*hdr3
= bp2
->b_addr
;
1117 hdr3
->blkno
= cpu_to_be64(bp2
->b_bn
);
1119 xfs_trans_log_buf(args
->trans
, bp2
, 0, args
->geo
->blksize
- 1);
1122 * Set up the new root node.
1124 error
= xfs_da3_node_create(args
, 0, 1, &bp1
, XFS_ATTR_FORK
);
1128 dp
->d_ops
->node_hdr_from_disk(&icnodehdr
, node
);
1129 btree
= dp
->d_ops
->node_tree_p(node
);
1132 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &icleafhdr
, leaf
);
1133 entries
= xfs_attr3_leaf_entryp(leaf
);
1135 /* both on-disk, don't endian-flip twice */
1136 btree
[0].hashval
= entries
[icleafhdr
.count
- 1].hashval
;
1137 btree
[0].before
= cpu_to_be32(blkno
);
1138 icnodehdr
.count
= 1;
1139 dp
->d_ops
->node_hdr_to_disk(node
, &icnodehdr
);
1140 xfs_trans_log_buf(args
->trans
, bp1
, 0, args
->geo
->blksize
- 1);
1146 /*========================================================================
1147 * Routines used for growing the Btree.
1148 *========================================================================*/
1151 * Create the initial contents of a leaf attribute list
1152 * or a leaf in a node attribute list.
1155 xfs_attr3_leaf_create(
1156 struct xfs_da_args
*args
,
1158 struct xfs_buf
**bpp
)
1160 struct xfs_attr_leafblock
*leaf
;
1161 struct xfs_attr3_icleaf_hdr ichdr
;
1162 struct xfs_inode
*dp
= args
->dp
;
1163 struct xfs_mount
*mp
= dp
->i_mount
;
1167 trace_xfs_attr_leaf_create(args
);
1169 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp
,
1173 bp
->b_ops
= &xfs_attr3_leaf_buf_ops
;
1174 xfs_trans_buf_set_type(args
->trans
, bp
, XFS_BLFT_ATTR_LEAF_BUF
);
1176 memset(leaf
, 0, args
->geo
->blksize
);
1178 memset(&ichdr
, 0, sizeof(ichdr
));
1179 ichdr
.firstused
= args
->geo
->blksize
;
1181 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
1182 struct xfs_da3_blkinfo
*hdr3
= bp
->b_addr
;
1184 ichdr
.magic
= XFS_ATTR3_LEAF_MAGIC
;
1186 hdr3
->blkno
= cpu_to_be64(bp
->b_bn
);
1187 hdr3
->owner
= cpu_to_be64(dp
->i_ino
);
1188 uuid_copy(&hdr3
->uuid
, &mp
->m_sb
.sb_meta_uuid
);
1190 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr3_leaf_hdr
);
1192 ichdr
.magic
= XFS_ATTR_LEAF_MAGIC
;
1193 ichdr
.freemap
[0].base
= sizeof(struct xfs_attr_leaf_hdr
);
1195 ichdr
.freemap
[0].size
= ichdr
.firstused
- ichdr
.freemap
[0].base
;
1197 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1198 xfs_trans_log_buf(args
->trans
, bp
, 0, args
->geo
->blksize
- 1);
1205 * Split the leaf node, rebalance, then add the new entry.
1208 xfs_attr3_leaf_split(
1209 struct xfs_da_state
*state
,
1210 struct xfs_da_state_blk
*oldblk
,
1211 struct xfs_da_state_blk
*newblk
)
1216 trace_xfs_attr_leaf_split(state
->args
);
1219 * Allocate space for a new leaf node.
1221 ASSERT(oldblk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1222 error
= xfs_da_grow_inode(state
->args
, &blkno
);
1225 error
= xfs_attr3_leaf_create(state
->args
, blkno
, &newblk
->bp
);
1228 newblk
->blkno
= blkno
;
1229 newblk
->magic
= XFS_ATTR_LEAF_MAGIC
;
1232 * Rebalance the entries across the two leaves.
1233 * NOTE: rebalance() currently depends on the 2nd block being empty.
1235 xfs_attr3_leaf_rebalance(state
, oldblk
, newblk
);
1236 error
= xfs_da3_blk_link(state
, oldblk
, newblk
);
1241 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1242 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1243 * "new" attrs info. Will need the "old" info to remove it later.
1245 * Insert the "new" entry in the correct block.
1247 if (state
->inleaf
) {
1248 trace_xfs_attr_leaf_add_old(state
->args
);
1249 error
= xfs_attr3_leaf_add(oldblk
->bp
, state
->args
);
1251 trace_xfs_attr_leaf_add_new(state
->args
);
1252 error
= xfs_attr3_leaf_add(newblk
->bp
, state
->args
);
1256 * Update last hashval in each block since we added the name.
1258 oldblk
->hashval
= xfs_attr_leaf_lasthash(oldblk
->bp
, NULL
);
1259 newblk
->hashval
= xfs_attr_leaf_lasthash(newblk
->bp
, NULL
);
1264 * Add a name to the leaf attribute list structure.
1269 struct xfs_da_args
*args
)
1271 struct xfs_attr_leafblock
*leaf
;
1272 struct xfs_attr3_icleaf_hdr ichdr
;
1279 trace_xfs_attr_leaf_add(args
);
1282 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
1283 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
.count
);
1284 entsize
= xfs_attr_leaf_newentsize(args
, NULL
);
1287 * Search through freemap for first-fit on new name length.
1288 * (may need to figure in size of entry struct too)
1290 tablesize
= (ichdr
.count
+ 1) * sizeof(xfs_attr_leaf_entry_t
)
1291 + xfs_attr3_leaf_hdr_size(leaf
);
1292 for (sum
= 0, i
= XFS_ATTR_LEAF_MAPSIZE
- 1; i
>= 0; i
--) {
1293 if (tablesize
> ichdr
.firstused
) {
1294 sum
+= ichdr
.freemap
[i
].size
;
1297 if (!ichdr
.freemap
[i
].size
)
1298 continue; /* no space in this map */
1300 if (ichdr
.freemap
[i
].base
< ichdr
.firstused
)
1301 tmp
+= sizeof(xfs_attr_leaf_entry_t
);
1302 if (ichdr
.freemap
[i
].size
>= tmp
) {
1303 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, i
);
1306 sum
+= ichdr
.freemap
[i
].size
;
1310 * If there are no holes in the address space of the block,
1311 * and we don't have enough freespace, then compaction will do us
1312 * no good and we should just give up.
1314 if (!ichdr
.holes
&& sum
< entsize
)
1318 * Compact the entries to coalesce free space.
1319 * This may change the hdr->count via dropping INCOMPLETE entries.
1321 xfs_attr3_leaf_compact(args
, &ichdr
, bp
);
1324 * After compaction, the block is guaranteed to have only one
1325 * free region, in freemap[0]. If it is not big enough, give up.
1327 if (ichdr
.freemap
[0].size
< (entsize
+ sizeof(xfs_attr_leaf_entry_t
))) {
1332 tmp
= xfs_attr3_leaf_add_work(bp
, &ichdr
, args
, 0);
1335 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
1336 xfs_trans_log_buf(args
->trans
, bp
,
1337 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
1338 xfs_attr3_leaf_hdr_size(leaf
)));
1343 * Add a name to a leaf attribute list structure.
1346 xfs_attr3_leaf_add_work(
1348 struct xfs_attr3_icleaf_hdr
*ichdr
,
1349 struct xfs_da_args
*args
,
1352 struct xfs_attr_leafblock
*leaf
;
1353 struct xfs_attr_leaf_entry
*entry
;
1354 struct xfs_attr_leaf_name_local
*name_loc
;
1355 struct xfs_attr_leaf_name_remote
*name_rmt
;
1356 struct xfs_mount
*mp
;
1360 trace_xfs_attr_leaf_add_work(args
);
1363 ASSERT(mapindex
>= 0 && mapindex
< XFS_ATTR_LEAF_MAPSIZE
);
1364 ASSERT(args
->index
>= 0 && args
->index
<= ichdr
->count
);
1367 * Force open some space in the entry array and fill it in.
1369 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
1370 if (args
->index
< ichdr
->count
) {
1371 tmp
= ichdr
->count
- args
->index
;
1372 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
1373 memmove(entry
+ 1, entry
, tmp
);
1374 xfs_trans_log_buf(args
->trans
, bp
,
1375 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1380 * Allocate space for the new string (at the end of the run).
1382 mp
= args
->trans
->t_mountp
;
1383 ASSERT(ichdr
->freemap
[mapindex
].base
< args
->geo
->blksize
);
1384 ASSERT((ichdr
->freemap
[mapindex
].base
& 0x3) == 0);
1385 ASSERT(ichdr
->freemap
[mapindex
].size
>=
1386 xfs_attr_leaf_newentsize(args
, NULL
));
1387 ASSERT(ichdr
->freemap
[mapindex
].size
< args
->geo
->blksize
);
1388 ASSERT((ichdr
->freemap
[mapindex
].size
& 0x3) == 0);
1390 ichdr
->freemap
[mapindex
].size
-= xfs_attr_leaf_newentsize(args
, &tmp
);
1392 entry
->nameidx
= cpu_to_be16(ichdr
->freemap
[mapindex
].base
+
1393 ichdr
->freemap
[mapindex
].size
);
1394 entry
->hashval
= cpu_to_be32(args
->hashval
);
1395 entry
->flags
= tmp
? XFS_ATTR_LOCAL
: 0;
1396 entry
->flags
|= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
1397 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1398 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1399 if ((args
->blkno2
== args
->blkno
) &&
1400 (args
->index2
<= args
->index
)) {
1404 xfs_trans_log_buf(args
->trans
, bp
,
1405 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
1406 ASSERT((args
->index
== 0) ||
1407 (be32_to_cpu(entry
->hashval
) >= be32_to_cpu((entry
-1)->hashval
)));
1408 ASSERT((args
->index
== ichdr
->count
- 1) ||
1409 (be32_to_cpu(entry
->hashval
) <= be32_to_cpu((entry
+1)->hashval
)));
1412 * For "remote" attribute values, simply note that we need to
1413 * allocate space for the "remote" value. We can't actually
1414 * allocate the extents in this transaction, and we can't decide
1415 * which blocks they should be as we might allocate more blocks
1416 * as part of this transaction (a split operation for example).
1418 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1419 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
1420 name_loc
->namelen
= args
->namelen
;
1421 name_loc
->valuelen
= cpu_to_be16(args
->valuelen
);
1422 memcpy((char *)name_loc
->nameval
, args
->name
, args
->namelen
);
1423 memcpy((char *)&name_loc
->nameval
[args
->namelen
], args
->value
,
1424 be16_to_cpu(name_loc
->valuelen
));
1426 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
1427 name_rmt
->namelen
= args
->namelen
;
1428 memcpy((char *)name_rmt
->name
, args
->name
, args
->namelen
);
1429 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1431 name_rmt
->valuelen
= 0;
1432 name_rmt
->valueblk
= 0;
1434 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
1435 args
->rmtvaluelen
= args
->valuelen
;
1437 xfs_trans_log_buf(args
->trans
, bp
,
1438 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
1439 xfs_attr_leaf_entsize(leaf
, args
->index
)));
1442 * Update the control info for this leaf node
1444 if (be16_to_cpu(entry
->nameidx
) < ichdr
->firstused
)
1445 ichdr
->firstused
= be16_to_cpu(entry
->nameidx
);
1447 ASSERT(ichdr
->firstused
>= ichdr
->count
* sizeof(xfs_attr_leaf_entry_t
)
1448 + xfs_attr3_leaf_hdr_size(leaf
));
1449 tmp
= (ichdr
->count
- 1) * sizeof(xfs_attr_leaf_entry_t
)
1450 + xfs_attr3_leaf_hdr_size(leaf
);
1452 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
1453 if (ichdr
->freemap
[i
].base
== tmp
) {
1454 ichdr
->freemap
[i
].base
+= sizeof(xfs_attr_leaf_entry_t
);
1455 ichdr
->freemap
[i
].size
-= sizeof(xfs_attr_leaf_entry_t
);
1458 ichdr
->usedbytes
+= xfs_attr_leaf_entsize(leaf
, args
->index
);
1463 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1466 xfs_attr3_leaf_compact(
1467 struct xfs_da_args
*args
,
1468 struct xfs_attr3_icleaf_hdr
*ichdr_dst
,
1471 struct xfs_attr_leafblock
*leaf_src
;
1472 struct xfs_attr_leafblock
*leaf_dst
;
1473 struct xfs_attr3_icleaf_hdr ichdr_src
;
1474 struct xfs_trans
*trans
= args
->trans
;
1477 trace_xfs_attr_leaf_compact(args
);
1479 tmpbuffer
= kmem_alloc(args
->geo
->blksize
, 0);
1480 memcpy(tmpbuffer
, bp
->b_addr
, args
->geo
->blksize
);
1481 memset(bp
->b_addr
, 0, args
->geo
->blksize
);
1482 leaf_src
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1483 leaf_dst
= bp
->b_addr
;
1486 * Copy the on-disk header back into the destination buffer to ensure
1487 * all the information in the header that is not part of the incore
1488 * header structure is preserved.
1490 memcpy(bp
->b_addr
, tmpbuffer
, xfs_attr3_leaf_hdr_size(leaf_src
));
1492 /* Initialise the incore headers */
1493 ichdr_src
= *ichdr_dst
; /* struct copy */
1494 ichdr_dst
->firstused
= args
->geo
->blksize
;
1495 ichdr_dst
->usedbytes
= 0;
1496 ichdr_dst
->count
= 0;
1497 ichdr_dst
->holes
= 0;
1498 ichdr_dst
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_src
);
1499 ichdr_dst
->freemap
[0].size
= ichdr_dst
->firstused
-
1500 ichdr_dst
->freemap
[0].base
;
1502 /* write the header back to initialise the underlying buffer */
1503 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf_dst
, ichdr_dst
);
1506 * Copy all entry's in the same (sorted) order,
1507 * but allocate name/value pairs packed and in sequence.
1509 xfs_attr3_leaf_moveents(args
, leaf_src
, &ichdr_src
, 0,
1510 leaf_dst
, ichdr_dst
, 0, ichdr_src
.count
);
1512 * this logs the entire buffer, but the caller must write the header
1513 * back to the buffer when it is finished modifying it.
1515 xfs_trans_log_buf(trans
, bp
, 0, args
->geo
->blksize
- 1);
1517 kmem_free(tmpbuffer
);
1521 * Compare two leaf blocks "order".
1522 * Return 0 unless leaf2 should go before leaf1.
1525 xfs_attr3_leaf_order(
1526 struct xfs_buf
*leaf1_bp
,
1527 struct xfs_attr3_icleaf_hdr
*leaf1hdr
,
1528 struct xfs_buf
*leaf2_bp
,
1529 struct xfs_attr3_icleaf_hdr
*leaf2hdr
)
1531 struct xfs_attr_leaf_entry
*entries1
;
1532 struct xfs_attr_leaf_entry
*entries2
;
1534 entries1
= xfs_attr3_leaf_entryp(leaf1_bp
->b_addr
);
1535 entries2
= xfs_attr3_leaf_entryp(leaf2_bp
->b_addr
);
1536 if (leaf1hdr
->count
> 0 && leaf2hdr
->count
> 0 &&
1537 ((be32_to_cpu(entries2
[0].hashval
) <
1538 be32_to_cpu(entries1
[0].hashval
)) ||
1539 (be32_to_cpu(entries2
[leaf2hdr
->count
- 1].hashval
) <
1540 be32_to_cpu(entries1
[leaf1hdr
->count
- 1].hashval
)))) {
1547 xfs_attr_leaf_order(
1548 struct xfs_buf
*leaf1_bp
,
1549 struct xfs_buf
*leaf2_bp
)
1551 struct xfs_attr3_icleaf_hdr ichdr1
;
1552 struct xfs_attr3_icleaf_hdr ichdr2
;
1553 struct xfs_mount
*mp
= leaf1_bp
->b_mount
;
1555 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr1
, leaf1_bp
->b_addr
);
1556 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr2
, leaf2_bp
->b_addr
);
1557 return xfs_attr3_leaf_order(leaf1_bp
, &ichdr1
, leaf2_bp
, &ichdr2
);
1561 * Redistribute the attribute list entries between two leaf nodes,
1562 * taking into account the size of the new entry.
1564 * NOTE: if new block is empty, then it will get the upper half of the
1565 * old block. At present, all (one) callers pass in an empty second block.
1567 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1568 * to match what it is doing in splitting the attribute leaf block. Those
1569 * values are used in "atomic rename" operations on attributes. Note that
1570 * the "new" and "old" values can end up in different blocks.
1573 xfs_attr3_leaf_rebalance(
1574 struct xfs_da_state
*state
,
1575 struct xfs_da_state_blk
*blk1
,
1576 struct xfs_da_state_blk
*blk2
)
1578 struct xfs_da_args
*args
;
1579 struct xfs_attr_leafblock
*leaf1
;
1580 struct xfs_attr_leafblock
*leaf2
;
1581 struct xfs_attr3_icleaf_hdr ichdr1
;
1582 struct xfs_attr3_icleaf_hdr ichdr2
;
1583 struct xfs_attr_leaf_entry
*entries1
;
1584 struct xfs_attr_leaf_entry
*entries2
;
1592 * Set up environment.
1594 ASSERT(blk1
->magic
== XFS_ATTR_LEAF_MAGIC
);
1595 ASSERT(blk2
->magic
== XFS_ATTR_LEAF_MAGIC
);
1596 leaf1
= blk1
->bp
->b_addr
;
1597 leaf2
= blk2
->bp
->b_addr
;
1598 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr1
, leaf1
);
1599 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, leaf2
);
1600 ASSERT(ichdr2
.count
== 0);
1603 trace_xfs_attr_leaf_rebalance(args
);
1606 * Check ordering of blocks, reverse if it makes things simpler.
1608 * NOTE: Given that all (current) callers pass in an empty
1609 * second block, this code should never set "swap".
1612 if (xfs_attr3_leaf_order(blk1
->bp
, &ichdr1
, blk2
->bp
, &ichdr2
)) {
1615 /* swap structures rather than reconverting them */
1616 swap(ichdr1
, ichdr2
);
1618 leaf1
= blk1
->bp
->b_addr
;
1619 leaf2
= blk2
->bp
->b_addr
;
1624 * Examine entries until we reduce the absolute difference in
1625 * byte usage between the two blocks to a minimum. Then get
1626 * the direction to copy and the number of elements to move.
1628 * "inleaf" is true if the new entry should be inserted into blk1.
1629 * If "swap" is also true, then reverse the sense of "inleaf".
1631 state
->inleaf
= xfs_attr3_leaf_figure_balance(state
, blk1
, &ichdr1
,
1635 state
->inleaf
= !state
->inleaf
;
1638 * Move any entries required from leaf to leaf:
1640 if (count
< ichdr1
.count
) {
1642 * Figure the total bytes to be added to the destination leaf.
1644 /* number entries being moved */
1645 count
= ichdr1
.count
- count
;
1646 space
= ichdr1
.usedbytes
- totallen
;
1647 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1650 * leaf2 is the destination, compact it if it looks tight.
1652 max
= ichdr2
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1653 max
-= ichdr2
.count
* sizeof(xfs_attr_leaf_entry_t
);
1655 xfs_attr3_leaf_compact(args
, &ichdr2
, blk2
->bp
);
1658 * Move high entries from leaf1 to low end of leaf2.
1660 xfs_attr3_leaf_moveents(args
, leaf1
, &ichdr1
,
1661 ichdr1
.count
- count
, leaf2
, &ichdr2
, 0, count
);
1663 } else if (count
> ichdr1
.count
) {
1665 * I assert that since all callers pass in an empty
1666 * second buffer, this code should never execute.
1671 * Figure the total bytes to be added to the destination leaf.
1673 /* number entries being moved */
1674 count
-= ichdr1
.count
;
1675 space
= totallen
- ichdr1
.usedbytes
;
1676 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1679 * leaf1 is the destination, compact it if it looks tight.
1681 max
= ichdr1
.firstused
- xfs_attr3_leaf_hdr_size(leaf1
);
1682 max
-= ichdr1
.count
* sizeof(xfs_attr_leaf_entry_t
);
1684 xfs_attr3_leaf_compact(args
, &ichdr1
, blk1
->bp
);
1687 * Move low entries from leaf2 to high end of leaf1.
1689 xfs_attr3_leaf_moveents(args
, leaf2
, &ichdr2
, 0, leaf1
, &ichdr1
,
1690 ichdr1
.count
, count
);
1693 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf1
, &ichdr1
);
1694 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, leaf2
, &ichdr2
);
1695 xfs_trans_log_buf(args
->trans
, blk1
->bp
, 0, args
->geo
->blksize
- 1);
1696 xfs_trans_log_buf(args
->trans
, blk2
->bp
, 0, args
->geo
->blksize
- 1);
1699 * Copy out last hashval in each block for B-tree code.
1701 entries1
= xfs_attr3_leaf_entryp(leaf1
);
1702 entries2
= xfs_attr3_leaf_entryp(leaf2
);
1703 blk1
->hashval
= be32_to_cpu(entries1
[ichdr1
.count
- 1].hashval
);
1704 blk2
->hashval
= be32_to_cpu(entries2
[ichdr2
.count
- 1].hashval
);
1707 * Adjust the expected index for insertion.
1708 * NOTE: this code depends on the (current) situation that the
1709 * second block was originally empty.
1711 * If the insertion point moved to the 2nd block, we must adjust
1712 * the index. We must also track the entry just following the
1713 * new entry for use in an "atomic rename" operation, that entry
1714 * is always the "old" entry and the "new" entry is what we are
1715 * inserting. The index/blkno fields refer to the "old" entry,
1716 * while the index2/blkno2 fields refer to the "new" entry.
1718 if (blk1
->index
> ichdr1
.count
) {
1719 ASSERT(state
->inleaf
== 0);
1720 blk2
->index
= blk1
->index
- ichdr1
.count
;
1721 args
->index
= args
->index2
= blk2
->index
;
1722 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1723 } else if (blk1
->index
== ichdr1
.count
) {
1724 if (state
->inleaf
) {
1725 args
->index
= blk1
->index
;
1726 args
->blkno
= blk1
->blkno
;
1728 args
->blkno2
= blk2
->blkno
;
1731 * On a double leaf split, the original attr location
1732 * is already stored in blkno2/index2, so don't
1733 * overwrite it overwise we corrupt the tree.
1735 blk2
->index
= blk1
->index
- ichdr1
.count
;
1736 args
->index
= blk2
->index
;
1737 args
->blkno
= blk2
->blkno
;
1738 if (!state
->extravalid
) {
1740 * set the new attr location to match the old
1741 * one and let the higher level split code
1742 * decide where in the leaf to place it.
1744 args
->index2
= blk2
->index
;
1745 args
->blkno2
= blk2
->blkno
;
1749 ASSERT(state
->inleaf
== 1);
1750 args
->index
= args
->index2
= blk1
->index
;
1751 args
->blkno
= args
->blkno2
= blk1
->blkno
;
1756 * Examine entries until we reduce the absolute difference in
1757 * byte usage between the two blocks to a minimum.
1758 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1759 * GROT: there will always be enough room in either block for a new entry.
1760 * GROT: Do a double-split for this case?
1763 xfs_attr3_leaf_figure_balance(
1764 struct xfs_da_state
*state
,
1765 struct xfs_da_state_blk
*blk1
,
1766 struct xfs_attr3_icleaf_hdr
*ichdr1
,
1767 struct xfs_da_state_blk
*blk2
,
1768 struct xfs_attr3_icleaf_hdr
*ichdr2
,
1772 struct xfs_attr_leafblock
*leaf1
= blk1
->bp
->b_addr
;
1773 struct xfs_attr_leafblock
*leaf2
= blk2
->bp
->b_addr
;
1774 struct xfs_attr_leaf_entry
*entry
;
1785 * Examine entries until we reduce the absolute difference in
1786 * byte usage between the two blocks to a minimum.
1788 max
= ichdr1
->count
+ ichdr2
->count
;
1789 half
= (max
+ 1) * sizeof(*entry
);
1790 half
+= ichdr1
->usedbytes
+ ichdr2
->usedbytes
+
1791 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1793 lastdelta
= state
->args
->geo
->blksize
;
1794 entry
= xfs_attr3_leaf_entryp(leaf1
);
1795 for (count
= index
= 0; count
< max
; entry
++, index
++, count
++) {
1797 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1799 * The new entry is in the first block, account for it.
1801 if (count
== blk1
->index
) {
1802 tmp
= totallen
+ sizeof(*entry
) +
1803 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1804 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1806 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1812 * Wrap around into the second block if necessary.
1814 if (count
== ichdr1
->count
) {
1816 entry
= xfs_attr3_leaf_entryp(leaf1
);
1821 * Figure out if next leaf entry would be too much.
1823 tmp
= totallen
+ sizeof(*entry
) + xfs_attr_leaf_entsize(leaf1
,
1825 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1827 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1833 * Calculate the number of usedbytes that will end up in lower block.
1834 * If new entry not in lower block, fix up the count.
1836 totallen
-= count
* sizeof(*entry
);
1838 totallen
-= sizeof(*entry
) +
1839 xfs_attr_leaf_newentsize(state
->args
, NULL
);
1843 *usedbytesarg
= totallen
;
1847 /*========================================================================
1848 * Routines used for shrinking the Btree.
1849 *========================================================================*/
1852 * Check a leaf block and its neighbors to see if the block should be
1853 * collapsed into one or the other neighbor. Always keep the block
1854 * with the smaller block number.
1855 * If the current block is over 50% full, don't try to join it, return 0.
1856 * If the block is empty, fill in the state structure and return 2.
1857 * If it can be collapsed, fill in the state structure and return 1.
1858 * If nothing can be done, return 0.
1860 * GROT: allow for INCOMPLETE entries in calculation.
1863 xfs_attr3_leaf_toosmall(
1864 struct xfs_da_state
*state
,
1867 struct xfs_attr_leafblock
*leaf
;
1868 struct xfs_da_state_blk
*blk
;
1869 struct xfs_attr3_icleaf_hdr ichdr
;
1878 trace_xfs_attr_leaf_toosmall(state
->args
);
1881 * Check for the degenerate case of the block being over 50% full.
1882 * If so, it's not worth even looking to see if we might be able
1883 * to coalesce with a sibling.
1885 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1886 leaf
= blk
->bp
->b_addr
;
1887 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr
, leaf
);
1888 bytes
= xfs_attr3_leaf_hdr_size(leaf
) +
1889 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
) +
1891 if (bytes
> (state
->args
->geo
->blksize
>> 1)) {
1892 *action
= 0; /* blk over 50%, don't try to join */
1897 * Check for the degenerate case of the block being empty.
1898 * If the block is empty, we'll simply delete it, no need to
1899 * coalesce it with a sibling block. We choose (arbitrarily)
1900 * to merge with the forward block unless it is NULL.
1902 if (ichdr
.count
== 0) {
1904 * Make altpath point to the block we want to keep and
1905 * path point to the block we want to drop (this one).
1907 forward
= (ichdr
.forw
!= 0);
1908 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1909 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1922 * Examine each sibling block to see if we can coalesce with
1923 * at least 25% free space to spare. We need to figure out
1924 * whether to merge with the forward or the backward block.
1925 * We prefer coalescing with the lower numbered sibling so as
1926 * to shrink an attribute list over time.
1928 /* start with smaller blk num */
1929 forward
= ichdr
.forw
< ichdr
.back
;
1930 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1931 struct xfs_attr3_icleaf_hdr ichdr2
;
1938 error
= xfs_attr3_leaf_read(state
->args
->trans
, state
->args
->dp
,
1943 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &ichdr2
, bp
->b_addr
);
1945 bytes
= state
->args
->geo
->blksize
-
1946 (state
->args
->geo
->blksize
>> 2) -
1947 ichdr
.usedbytes
- ichdr2
.usedbytes
-
1948 ((ichdr
.count
+ ichdr2
.count
) *
1949 sizeof(xfs_attr_leaf_entry_t
)) -
1950 xfs_attr3_leaf_hdr_size(leaf
);
1952 xfs_trans_brelse(state
->args
->trans
, bp
);
1954 break; /* fits with at least 25% to spare */
1962 * Make altpath point to the block we want to keep (the lower
1963 * numbered block) and path point to the block we want to drop.
1965 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1966 if (blkno
< blk
->blkno
) {
1967 error
= xfs_da3_path_shift(state
, &state
->altpath
, forward
,
1970 error
= xfs_da3_path_shift(state
, &state
->path
, forward
,
1984 * Remove a name from the leaf attribute list structure.
1986 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1987 * If two leaves are 37% full, when combined they will leave 25% free.
1990 xfs_attr3_leaf_remove(
1992 struct xfs_da_args
*args
)
1994 struct xfs_attr_leafblock
*leaf
;
1995 struct xfs_attr3_icleaf_hdr ichdr
;
1996 struct xfs_attr_leaf_entry
*entry
;
2005 trace_xfs_attr_leaf_remove(args
);
2008 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2010 ASSERT(ichdr
.count
> 0 && ichdr
.count
< args
->geo
->blksize
/ 8);
2011 ASSERT(args
->index
>= 0 && args
->index
< ichdr
.count
);
2012 ASSERT(ichdr
.firstused
>= ichdr
.count
* sizeof(*entry
) +
2013 xfs_attr3_leaf_hdr_size(leaf
));
2015 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2017 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2018 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2021 * Scan through free region table:
2022 * check for adjacency of free'd entry with an existing one,
2023 * find smallest free region in case we need to replace it,
2024 * adjust any map that borders the entry table,
2026 tablesize
= ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
)
2027 + xfs_attr3_leaf_hdr_size(leaf
);
2028 tmp
= ichdr
.freemap
[0].size
;
2029 before
= after
= -1;
2030 smallest
= XFS_ATTR_LEAF_MAPSIZE
- 1;
2031 entsize
= xfs_attr_leaf_entsize(leaf
, args
->index
);
2032 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; i
++) {
2033 ASSERT(ichdr
.freemap
[i
].base
< args
->geo
->blksize
);
2034 ASSERT(ichdr
.freemap
[i
].size
< args
->geo
->blksize
);
2035 if (ichdr
.freemap
[i
].base
== tablesize
) {
2036 ichdr
.freemap
[i
].base
-= sizeof(xfs_attr_leaf_entry_t
);
2037 ichdr
.freemap
[i
].size
+= sizeof(xfs_attr_leaf_entry_t
);
2040 if (ichdr
.freemap
[i
].base
+ ichdr
.freemap
[i
].size
==
2041 be16_to_cpu(entry
->nameidx
)) {
2043 } else if (ichdr
.freemap
[i
].base
==
2044 (be16_to_cpu(entry
->nameidx
) + entsize
)) {
2046 } else if (ichdr
.freemap
[i
].size
< tmp
) {
2047 tmp
= ichdr
.freemap
[i
].size
;
2053 * Coalesce adjacent freemap regions,
2054 * or replace the smallest region.
2056 if ((before
>= 0) || (after
>= 0)) {
2057 if ((before
>= 0) && (after
>= 0)) {
2058 ichdr
.freemap
[before
].size
+= entsize
;
2059 ichdr
.freemap
[before
].size
+= ichdr
.freemap
[after
].size
;
2060 ichdr
.freemap
[after
].base
= 0;
2061 ichdr
.freemap
[after
].size
= 0;
2062 } else if (before
>= 0) {
2063 ichdr
.freemap
[before
].size
+= entsize
;
2065 ichdr
.freemap
[after
].base
= be16_to_cpu(entry
->nameidx
);
2066 ichdr
.freemap
[after
].size
+= entsize
;
2070 * Replace smallest region (if it is smaller than free'd entry)
2072 if (ichdr
.freemap
[smallest
].size
< entsize
) {
2073 ichdr
.freemap
[smallest
].base
= be16_to_cpu(entry
->nameidx
);
2074 ichdr
.freemap
[smallest
].size
= entsize
;
2079 * Did we remove the first entry?
2081 if (be16_to_cpu(entry
->nameidx
) == ichdr
.firstused
)
2087 * Compress the remaining entries and zero out the removed stuff.
2089 memset(xfs_attr3_leaf_name(leaf
, args
->index
), 0, entsize
);
2090 ichdr
.usedbytes
-= entsize
;
2091 xfs_trans_log_buf(args
->trans
, bp
,
2092 XFS_DA_LOGRANGE(leaf
, xfs_attr3_leaf_name(leaf
, args
->index
),
2095 tmp
= (ichdr
.count
- args
->index
) * sizeof(xfs_attr_leaf_entry_t
);
2096 memmove(entry
, entry
+ 1, tmp
);
2098 xfs_trans_log_buf(args
->trans
, bp
,
2099 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(xfs_attr_leaf_entry_t
)));
2101 entry
= &xfs_attr3_leaf_entryp(leaf
)[ichdr
.count
];
2102 memset(entry
, 0, sizeof(xfs_attr_leaf_entry_t
));
2105 * If we removed the first entry, re-find the first used byte
2106 * in the name area. Note that if the entry was the "firstused",
2107 * then we don't have a "hole" in our block resulting from
2108 * removing the name.
2111 tmp
= args
->geo
->blksize
;
2112 entry
= xfs_attr3_leaf_entryp(leaf
);
2113 for (i
= ichdr
.count
- 1; i
>= 0; entry
++, i
--) {
2114 ASSERT(be16_to_cpu(entry
->nameidx
) >= ichdr
.firstused
);
2115 ASSERT(be16_to_cpu(entry
->nameidx
) < args
->geo
->blksize
);
2117 if (be16_to_cpu(entry
->nameidx
) < tmp
)
2118 tmp
= be16_to_cpu(entry
->nameidx
);
2120 ichdr
.firstused
= tmp
;
2121 ASSERT(ichdr
.firstused
!= 0);
2123 ichdr
.holes
= 1; /* mark as needing compaction */
2125 xfs_attr3_leaf_hdr_to_disk(args
->geo
, leaf
, &ichdr
);
2126 xfs_trans_log_buf(args
->trans
, bp
,
2127 XFS_DA_LOGRANGE(leaf
, &leaf
->hdr
,
2128 xfs_attr3_leaf_hdr_size(leaf
)));
2131 * Check if leaf is less than 50% full, caller may want to
2132 * "join" the leaf with a sibling if so.
2134 tmp
= ichdr
.usedbytes
+ xfs_attr3_leaf_hdr_size(leaf
) +
2135 ichdr
.count
* sizeof(xfs_attr_leaf_entry_t
);
2137 return tmp
< args
->geo
->magicpct
; /* leaf is < 37% full */
2141 * Move all the attribute list entries from drop_leaf into save_leaf.
2144 xfs_attr3_leaf_unbalance(
2145 struct xfs_da_state
*state
,
2146 struct xfs_da_state_blk
*drop_blk
,
2147 struct xfs_da_state_blk
*save_blk
)
2149 struct xfs_attr_leafblock
*drop_leaf
= drop_blk
->bp
->b_addr
;
2150 struct xfs_attr_leafblock
*save_leaf
= save_blk
->bp
->b_addr
;
2151 struct xfs_attr3_icleaf_hdr drophdr
;
2152 struct xfs_attr3_icleaf_hdr savehdr
;
2153 struct xfs_attr_leaf_entry
*entry
;
2155 trace_xfs_attr_leaf_unbalance(state
->args
);
2157 drop_leaf
= drop_blk
->bp
->b_addr
;
2158 save_leaf
= save_blk
->bp
->b_addr
;
2159 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &drophdr
, drop_leaf
);
2160 xfs_attr3_leaf_hdr_from_disk(state
->args
->geo
, &savehdr
, save_leaf
);
2161 entry
= xfs_attr3_leaf_entryp(drop_leaf
);
2164 * Save last hashval from dying block for later Btree fixup.
2166 drop_blk
->hashval
= be32_to_cpu(entry
[drophdr
.count
- 1].hashval
);
2169 * Check if we need a temp buffer, or can we do it in place.
2170 * Note that we don't check "leaf" for holes because we will
2171 * always be dropping it, toosmall() decided that for us already.
2173 if (savehdr
.holes
== 0) {
2175 * dest leaf has no holes, so we add there. May need
2176 * to make some room in the entry array.
2178 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2179 drop_blk
->bp
, &drophdr
)) {
2180 xfs_attr3_leaf_moveents(state
->args
,
2181 drop_leaf
, &drophdr
, 0,
2182 save_leaf
, &savehdr
, 0,
2185 xfs_attr3_leaf_moveents(state
->args
,
2186 drop_leaf
, &drophdr
, 0,
2187 save_leaf
, &savehdr
,
2188 savehdr
.count
, drophdr
.count
);
2192 * Destination has holes, so we make a temporary copy
2193 * of the leaf and add them both to that.
2195 struct xfs_attr_leafblock
*tmp_leaf
;
2196 struct xfs_attr3_icleaf_hdr tmphdr
;
2198 tmp_leaf
= kmem_zalloc(state
->args
->geo
->blksize
, 0);
2201 * Copy the header into the temp leaf so that all the stuff
2202 * not in the incore header is present and gets copied back in
2203 * once we've moved all the entries.
2205 memcpy(tmp_leaf
, save_leaf
, xfs_attr3_leaf_hdr_size(save_leaf
));
2207 memset(&tmphdr
, 0, sizeof(tmphdr
));
2208 tmphdr
.magic
= savehdr
.magic
;
2209 tmphdr
.forw
= savehdr
.forw
;
2210 tmphdr
.back
= savehdr
.back
;
2211 tmphdr
.firstused
= state
->args
->geo
->blksize
;
2213 /* write the header to the temp buffer to initialise it */
2214 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, tmp_leaf
, &tmphdr
);
2216 if (xfs_attr3_leaf_order(save_blk
->bp
, &savehdr
,
2217 drop_blk
->bp
, &drophdr
)) {
2218 xfs_attr3_leaf_moveents(state
->args
,
2219 drop_leaf
, &drophdr
, 0,
2220 tmp_leaf
, &tmphdr
, 0,
2222 xfs_attr3_leaf_moveents(state
->args
,
2223 save_leaf
, &savehdr
, 0,
2224 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2227 xfs_attr3_leaf_moveents(state
->args
,
2228 save_leaf
, &savehdr
, 0,
2229 tmp_leaf
, &tmphdr
, 0,
2231 xfs_attr3_leaf_moveents(state
->args
,
2232 drop_leaf
, &drophdr
, 0,
2233 tmp_leaf
, &tmphdr
, tmphdr
.count
,
2236 memcpy(save_leaf
, tmp_leaf
, state
->args
->geo
->blksize
);
2237 savehdr
= tmphdr
; /* struct copy */
2238 kmem_free(tmp_leaf
);
2241 xfs_attr3_leaf_hdr_to_disk(state
->args
->geo
, save_leaf
, &savehdr
);
2242 xfs_trans_log_buf(state
->args
->trans
, save_blk
->bp
, 0,
2243 state
->args
->geo
->blksize
- 1);
2246 * Copy out last hashval in each block for B-tree code.
2248 entry
= xfs_attr3_leaf_entryp(save_leaf
);
2249 save_blk
->hashval
= be32_to_cpu(entry
[savehdr
.count
- 1].hashval
);
2252 /*========================================================================
2253 * Routines used for finding things in the Btree.
2254 *========================================================================*/
2257 * Look up a name in a leaf attribute list structure.
2258 * This is the internal routine, it uses the caller's buffer.
2260 * Note that duplicate keys are allowed, but only check within the
2261 * current leaf node. The Btree code must check in adjacent leaf nodes.
2263 * Return in args->index the index into the entry[] array of either
2264 * the found entry, or where the entry should have been (insert before
2267 * Don't change the args->value unless we find the attribute.
2270 xfs_attr3_leaf_lookup_int(
2272 struct xfs_da_args
*args
)
2274 struct xfs_attr_leafblock
*leaf
;
2275 struct xfs_attr3_icleaf_hdr ichdr
;
2276 struct xfs_attr_leaf_entry
*entry
;
2277 struct xfs_attr_leaf_entry
*entries
;
2278 struct xfs_attr_leaf_name_local
*name_loc
;
2279 struct xfs_attr_leaf_name_remote
*name_rmt
;
2280 xfs_dahash_t hashval
;
2284 trace_xfs_attr_leaf_lookup(args
);
2287 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2288 entries
= xfs_attr3_leaf_entryp(leaf
);
2289 if (ichdr
.count
>= args
->geo
->blksize
/ 8)
2290 return -EFSCORRUPTED
;
2293 * Binary search. (note: small blocks will skip this loop)
2295 hashval
= args
->hashval
;
2296 probe
= span
= ichdr
.count
/ 2;
2297 for (entry
= &entries
[probe
]; span
> 4; entry
= &entries
[probe
]) {
2299 if (be32_to_cpu(entry
->hashval
) < hashval
)
2301 else if (be32_to_cpu(entry
->hashval
) > hashval
)
2306 if (!(probe
>= 0 && (!ichdr
.count
|| probe
< ichdr
.count
)))
2307 return -EFSCORRUPTED
;
2308 if (!(span
<= 4 || be32_to_cpu(entry
->hashval
) == hashval
))
2309 return -EFSCORRUPTED
;
2312 * Since we may have duplicate hashval's, find the first matching
2313 * hashval in the leaf.
2315 while (probe
> 0 && be32_to_cpu(entry
->hashval
) >= hashval
) {
2319 while (probe
< ichdr
.count
&&
2320 be32_to_cpu(entry
->hashval
) < hashval
) {
2324 if (probe
== ichdr
.count
|| be32_to_cpu(entry
->hashval
) != hashval
) {
2325 args
->index
= probe
;
2330 * Duplicate keys may be present, so search all of them for a match.
2332 for (; probe
< ichdr
.count
&& (be32_to_cpu(entry
->hashval
) == hashval
);
2335 * GROT: Add code to remove incomplete entries.
2338 * If we are looking for INCOMPLETE entries, show only those.
2339 * If we are looking for complete entries, show only those.
2341 if ((args
->flags
& XFS_ATTR_INCOMPLETE
) !=
2342 (entry
->flags
& XFS_ATTR_INCOMPLETE
)) {
2345 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2346 name_loc
= xfs_attr3_leaf_name_local(leaf
, probe
);
2347 if (name_loc
->namelen
!= args
->namelen
)
2349 if (memcmp(args
->name
, name_loc
->nameval
,
2350 args
->namelen
) != 0)
2352 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2354 args
->index
= probe
;
2357 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, probe
);
2358 if (name_rmt
->namelen
!= args
->namelen
)
2360 if (memcmp(args
->name
, name_rmt
->name
,
2361 args
->namelen
) != 0)
2363 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2365 args
->index
= probe
;
2366 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2367 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2368 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(
2374 args
->index
= probe
;
2379 * Get the value associated with an attribute name from a leaf attribute
2382 * If ATTR_KERNOVAL is specified, only the length needs to be returned.
2383 * Unlike a lookup, we only return an error if the attribute does not
2384 * exist or we can't retrieve the value.
2387 xfs_attr3_leaf_getvalue(
2389 struct xfs_da_args
*args
)
2391 struct xfs_attr_leafblock
*leaf
;
2392 struct xfs_attr3_icleaf_hdr ichdr
;
2393 struct xfs_attr_leaf_entry
*entry
;
2394 struct xfs_attr_leaf_name_local
*name_loc
;
2395 struct xfs_attr_leaf_name_remote
*name_rmt
;
2398 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2399 ASSERT(ichdr
.count
< args
->geo
->blksize
/ 8);
2400 ASSERT(args
->index
< ichdr
.count
);
2402 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2403 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2404 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2405 ASSERT(name_loc
->namelen
== args
->namelen
);
2406 ASSERT(memcmp(args
->name
, name_loc
->nameval
, args
->namelen
) == 0);
2407 return xfs_attr_copy_value(args
,
2408 &name_loc
->nameval
[args
->namelen
],
2409 be16_to_cpu(name_loc
->valuelen
));
2412 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2413 ASSERT(name_rmt
->namelen
== args
->namelen
);
2414 ASSERT(memcmp(args
->name
, name_rmt
->name
, args
->namelen
) == 0);
2415 args
->rmtvaluelen
= be32_to_cpu(name_rmt
->valuelen
);
2416 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2417 args
->rmtblkcnt
= xfs_attr3_rmt_blocks(args
->dp
->i_mount
,
2419 return xfs_attr_copy_value(args
, NULL
, args
->rmtvaluelen
);
2422 /*========================================================================
2424 *========================================================================*/
2427 * Move the indicated entries from one leaf to another.
2428 * NOTE: this routine modifies both source and destination leaves.
2432 xfs_attr3_leaf_moveents(
2433 struct xfs_da_args
*args
,
2434 struct xfs_attr_leafblock
*leaf_s
,
2435 struct xfs_attr3_icleaf_hdr
*ichdr_s
,
2437 struct xfs_attr_leafblock
*leaf_d
,
2438 struct xfs_attr3_icleaf_hdr
*ichdr_d
,
2442 struct xfs_attr_leaf_entry
*entry_s
;
2443 struct xfs_attr_leaf_entry
*entry_d
;
2449 * Check for nothing to do.
2455 * Set up environment.
2457 ASSERT(ichdr_s
->magic
== XFS_ATTR_LEAF_MAGIC
||
2458 ichdr_s
->magic
== XFS_ATTR3_LEAF_MAGIC
);
2459 ASSERT(ichdr_s
->magic
== ichdr_d
->magic
);
2460 ASSERT(ichdr_s
->count
> 0 && ichdr_s
->count
< args
->geo
->blksize
/ 8);
2461 ASSERT(ichdr_s
->firstused
>= (ichdr_s
->count
* sizeof(*entry_s
))
2462 + xfs_attr3_leaf_hdr_size(leaf_s
));
2463 ASSERT(ichdr_d
->count
< args
->geo
->blksize
/ 8);
2464 ASSERT(ichdr_d
->firstused
>= (ichdr_d
->count
* sizeof(*entry_d
))
2465 + xfs_attr3_leaf_hdr_size(leaf_d
));
2467 ASSERT(start_s
< ichdr_s
->count
);
2468 ASSERT(start_d
<= ichdr_d
->count
);
2469 ASSERT(count
<= ichdr_s
->count
);
2473 * Move the entries in the destination leaf up to make a hole?
2475 if (start_d
< ichdr_d
->count
) {
2476 tmp
= ichdr_d
->count
- start_d
;
2477 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2478 entry_s
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2479 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
+ count
];
2480 memmove(entry_d
, entry_s
, tmp
);
2484 * Copy all entry's in the same (sorted) order,
2485 * but allocate attribute info packed and in sequence.
2487 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2488 entry_d
= &xfs_attr3_leaf_entryp(leaf_d
)[start_d
];
2490 for (i
= 0; i
< count
; entry_s
++, entry_d
++, desti
++, i
++) {
2491 ASSERT(be16_to_cpu(entry_s
->nameidx
) >= ichdr_s
->firstused
);
2492 tmp
= xfs_attr_leaf_entsize(leaf_s
, start_s
+ i
);
2495 * Code to drop INCOMPLETE entries. Difficult to use as we
2496 * may also need to change the insertion index. Code turned
2497 * off for 6.2, should be revisited later.
2499 if (entry_s
->flags
& XFS_ATTR_INCOMPLETE
) { /* skip partials? */
2500 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2501 ichdr_s
->usedbytes
-= tmp
;
2502 ichdr_s
->count
-= 1;
2503 entry_d
--; /* to compensate for ++ in loop hdr */
2505 if ((start_s
+ i
) < offset
)
2506 result
++; /* insertion index adjustment */
2509 ichdr_d
->firstused
-= tmp
;
2510 /* both on-disk, don't endian flip twice */
2511 entry_d
->hashval
= entry_s
->hashval
;
2512 entry_d
->nameidx
= cpu_to_be16(ichdr_d
->firstused
);
2513 entry_d
->flags
= entry_s
->flags
;
2514 ASSERT(be16_to_cpu(entry_d
->nameidx
) + tmp
2515 <= args
->geo
->blksize
);
2516 memmove(xfs_attr3_leaf_name(leaf_d
, desti
),
2517 xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), tmp
);
2518 ASSERT(be16_to_cpu(entry_s
->nameidx
) + tmp
2519 <= args
->geo
->blksize
);
2520 memset(xfs_attr3_leaf_name(leaf_s
, start_s
+ i
), 0, tmp
);
2521 ichdr_s
->usedbytes
-= tmp
;
2522 ichdr_d
->usedbytes
+= tmp
;
2523 ichdr_s
->count
-= 1;
2524 ichdr_d
->count
+= 1;
2525 tmp
= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
)
2526 + xfs_attr3_leaf_hdr_size(leaf_d
);
2527 ASSERT(ichdr_d
->firstused
>= tmp
);
2534 * Zero out the entries we just copied.
2536 if (start_s
== ichdr_s
->count
) {
2537 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2538 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2539 ASSERT(((char *)entry_s
+ tmp
) <=
2540 ((char *)leaf_s
+ args
->geo
->blksize
));
2541 memset(entry_s
, 0, tmp
);
2544 * Move the remaining entries down to fill the hole,
2545 * then zero the entries at the top.
2547 tmp
= (ichdr_s
->count
- count
) * sizeof(xfs_attr_leaf_entry_t
);
2548 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
+ count
];
2549 entry_d
= &xfs_attr3_leaf_entryp(leaf_s
)[start_s
];
2550 memmove(entry_d
, entry_s
, tmp
);
2552 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2553 entry_s
= &xfs_attr3_leaf_entryp(leaf_s
)[ichdr_s
->count
];
2554 ASSERT(((char *)entry_s
+ tmp
) <=
2555 ((char *)leaf_s
+ args
->geo
->blksize
));
2556 memset(entry_s
, 0, tmp
);
2560 * Fill in the freemap information
2562 ichdr_d
->freemap
[0].base
= xfs_attr3_leaf_hdr_size(leaf_d
);
2563 ichdr_d
->freemap
[0].base
+= ichdr_d
->count
* sizeof(xfs_attr_leaf_entry_t
);
2564 ichdr_d
->freemap
[0].size
= ichdr_d
->firstused
- ichdr_d
->freemap
[0].base
;
2565 ichdr_d
->freemap
[1].base
= 0;
2566 ichdr_d
->freemap
[2].base
= 0;
2567 ichdr_d
->freemap
[1].size
= 0;
2568 ichdr_d
->freemap
[2].size
= 0;
2569 ichdr_s
->holes
= 1; /* leaf may not be compact */
2573 * Pick up the last hashvalue from a leaf block.
2576 xfs_attr_leaf_lasthash(
2580 struct xfs_attr3_icleaf_hdr ichdr
;
2581 struct xfs_attr_leaf_entry
*entries
;
2582 struct xfs_mount
*mp
= bp
->b_mount
;
2584 xfs_attr3_leaf_hdr_from_disk(mp
->m_attr_geo
, &ichdr
, bp
->b_addr
);
2585 entries
= xfs_attr3_leaf_entryp(bp
->b_addr
);
2587 *count
= ichdr
.count
;
2590 return be32_to_cpu(entries
[ichdr
.count
- 1].hashval
);
2594 * Calculate the number of bytes used to store the indicated attribute
2595 * (whether local or remote only calculate bytes in this block).
2598 xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
)
2600 struct xfs_attr_leaf_entry
*entries
;
2601 xfs_attr_leaf_name_local_t
*name_loc
;
2602 xfs_attr_leaf_name_remote_t
*name_rmt
;
2605 entries
= xfs_attr3_leaf_entryp(leaf
);
2606 if (entries
[index
].flags
& XFS_ATTR_LOCAL
) {
2607 name_loc
= xfs_attr3_leaf_name_local(leaf
, index
);
2608 size
= xfs_attr_leaf_entsize_local(name_loc
->namelen
,
2609 be16_to_cpu(name_loc
->valuelen
));
2611 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, index
);
2612 size
= xfs_attr_leaf_entsize_remote(name_rmt
->namelen
);
2618 * Calculate the number of bytes that would be required to store the new
2619 * attribute (whether local or remote only calculate bytes in this block).
2620 * This routine decides as a side effect whether the attribute will be
2621 * a "local" or a "remote" attribute.
2624 xfs_attr_leaf_newentsize(
2625 struct xfs_da_args
*args
,
2630 size
= xfs_attr_leaf_entsize_local(args
->namelen
, args
->valuelen
);
2631 if (size
< xfs_attr_leaf_entsize_local_max(args
->geo
->blksize
)) {
2638 return xfs_attr_leaf_entsize_remote(args
->namelen
);
2642 /*========================================================================
2643 * Manage the INCOMPLETE flag in a leaf entry
2644 *========================================================================*/
2647 * Clear the INCOMPLETE flag on an entry in a leaf block.
2650 xfs_attr3_leaf_clearflag(
2651 struct xfs_da_args
*args
)
2653 struct xfs_attr_leafblock
*leaf
;
2654 struct xfs_attr_leaf_entry
*entry
;
2655 struct xfs_attr_leaf_name_remote
*name_rmt
;
2659 struct xfs_attr3_icleaf_hdr ichdr
;
2660 xfs_attr_leaf_name_local_t
*name_loc
;
2665 trace_xfs_attr_leaf_clearflag(args
);
2667 * Set up the operation.
2669 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2674 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2675 ASSERT(entry
->flags
& XFS_ATTR_INCOMPLETE
);
2678 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2679 ASSERT(args
->index
< ichdr
.count
);
2680 ASSERT(args
->index
>= 0);
2682 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2683 name_loc
= xfs_attr3_leaf_name_local(leaf
, args
->index
);
2684 namelen
= name_loc
->namelen
;
2685 name
= (char *)name_loc
->nameval
;
2687 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2688 namelen
= name_rmt
->namelen
;
2689 name
= (char *)name_rmt
->name
;
2691 ASSERT(be32_to_cpu(entry
->hashval
) == args
->hashval
);
2692 ASSERT(namelen
== args
->namelen
);
2693 ASSERT(memcmp(name
, args
->name
, namelen
) == 0);
2696 entry
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2697 xfs_trans_log_buf(args
->trans
, bp
,
2698 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2700 if (args
->rmtblkno
) {
2701 ASSERT((entry
->flags
& XFS_ATTR_LOCAL
) == 0);
2702 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2703 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2704 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2705 xfs_trans_log_buf(args
->trans
, bp
,
2706 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2710 * Commit the flag value change and start the next trans in series.
2712 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2716 * Set the INCOMPLETE flag on an entry in a leaf block.
2719 xfs_attr3_leaf_setflag(
2720 struct xfs_da_args
*args
)
2722 struct xfs_attr_leafblock
*leaf
;
2723 struct xfs_attr_leaf_entry
*entry
;
2724 struct xfs_attr_leaf_name_remote
*name_rmt
;
2728 struct xfs_attr3_icleaf_hdr ichdr
;
2731 trace_xfs_attr_leaf_setflag(args
);
2734 * Set up the operation.
2736 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
);
2742 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr
, leaf
);
2743 ASSERT(args
->index
< ichdr
.count
);
2744 ASSERT(args
->index
>= 0);
2746 entry
= &xfs_attr3_leaf_entryp(leaf
)[args
->index
];
2748 ASSERT((entry
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2749 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
2750 xfs_trans_log_buf(args
->trans
, bp
,
2751 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2752 if ((entry
->flags
& XFS_ATTR_LOCAL
) == 0) {
2753 name_rmt
= xfs_attr3_leaf_name_remote(leaf
, args
->index
);
2754 name_rmt
->valueblk
= 0;
2755 name_rmt
->valuelen
= 0;
2756 xfs_trans_log_buf(args
->trans
, bp
,
2757 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2761 * Commit the flag value change and start the next trans in series.
2763 return xfs_trans_roll_inode(&args
->trans
, args
->dp
);
2767 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2768 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2769 * entry given by args->blkno2/index2.
2771 * Note that they could be in different blocks, or in the same block.
2774 xfs_attr3_leaf_flipflags(
2775 struct xfs_da_args
*args
)
2777 struct xfs_attr_leafblock
*leaf1
;
2778 struct xfs_attr_leafblock
*leaf2
;
2779 struct xfs_attr_leaf_entry
*entry1
;
2780 struct xfs_attr_leaf_entry
*entry2
;
2781 struct xfs_attr_leaf_name_remote
*name_rmt
;
2782 struct xfs_buf
*bp1
;
2783 struct xfs_buf
*bp2
;
2786 struct xfs_attr3_icleaf_hdr ichdr1
;
2787 struct xfs_attr3_icleaf_hdr ichdr2
;
2788 xfs_attr_leaf_name_local_t
*name_loc
;
2789 int namelen1
, namelen2
;
2790 char *name1
, *name2
;
2793 trace_xfs_attr_leaf_flipflags(args
);
2796 * Read the block containing the "old" attr
2798 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno
, -1, &bp1
);
2803 * Read the block containing the "new" attr, if it is different
2805 if (args
->blkno2
!= args
->blkno
) {
2806 error
= xfs_attr3_leaf_read(args
->trans
, args
->dp
, args
->blkno2
,
2814 leaf1
= bp1
->b_addr
;
2815 entry1
= &xfs_attr3_leaf_entryp(leaf1
)[args
->index
];
2817 leaf2
= bp2
->b_addr
;
2818 entry2
= &xfs_attr3_leaf_entryp(leaf2
)[args
->index2
];
2821 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr1
, leaf1
);
2822 ASSERT(args
->index
< ichdr1
.count
);
2823 ASSERT(args
->index
>= 0);
2825 xfs_attr3_leaf_hdr_from_disk(args
->geo
, &ichdr2
, leaf2
);
2826 ASSERT(args
->index2
< ichdr2
.count
);
2827 ASSERT(args
->index2
>= 0);
2829 if (entry1
->flags
& XFS_ATTR_LOCAL
) {
2830 name_loc
= xfs_attr3_leaf_name_local(leaf1
, args
->index
);
2831 namelen1
= name_loc
->namelen
;
2832 name1
= (char *)name_loc
->nameval
;
2834 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2835 namelen1
= name_rmt
->namelen
;
2836 name1
= (char *)name_rmt
->name
;
2838 if (entry2
->flags
& XFS_ATTR_LOCAL
) {
2839 name_loc
= xfs_attr3_leaf_name_local(leaf2
, args
->index2
);
2840 namelen2
= name_loc
->namelen
;
2841 name2
= (char *)name_loc
->nameval
;
2843 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2844 namelen2
= name_rmt
->namelen
;
2845 name2
= (char *)name_rmt
->name
;
2847 ASSERT(be32_to_cpu(entry1
->hashval
) == be32_to_cpu(entry2
->hashval
));
2848 ASSERT(namelen1
== namelen2
);
2849 ASSERT(memcmp(name1
, name2
, namelen1
) == 0);
2852 ASSERT(entry1
->flags
& XFS_ATTR_INCOMPLETE
);
2853 ASSERT((entry2
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2855 entry1
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2856 xfs_trans_log_buf(args
->trans
, bp1
,
2857 XFS_DA_LOGRANGE(leaf1
, entry1
, sizeof(*entry1
)));
2858 if (args
->rmtblkno
) {
2859 ASSERT((entry1
->flags
& XFS_ATTR_LOCAL
) == 0);
2860 name_rmt
= xfs_attr3_leaf_name_remote(leaf1
, args
->index
);
2861 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2862 name_rmt
->valuelen
= cpu_to_be32(args
->rmtvaluelen
);
2863 xfs_trans_log_buf(args
->trans
, bp1
,
2864 XFS_DA_LOGRANGE(leaf1
, name_rmt
, sizeof(*name_rmt
)));
2867 entry2
->flags
|= XFS_ATTR_INCOMPLETE
;
2868 xfs_trans_log_buf(args
->trans
, bp2
,
2869 XFS_DA_LOGRANGE(leaf2
, entry2
, sizeof(*entry2
)));
2870 if ((entry2
->flags
& XFS_ATTR_LOCAL
) == 0) {
2871 name_rmt
= xfs_attr3_leaf_name_remote(leaf2
, args
->index2
);
2872 name_rmt
->valueblk
= 0;
2873 name_rmt
->valuelen
= 0;
2874 xfs_trans_log_buf(args
->trans
, bp2
,
2875 XFS_DA_LOGRANGE(leaf2
, name_rmt
, sizeof(*name_rmt
)));
2879 * Commit the flag value change and start the next trans in series.
2881 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);