2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_alloc.h"
35 #include "xfs_btree.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_inode_item.h"
43 #include "xfs_attr_leaf.h"
44 #include "xfs_error.h"
49 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
52 /*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
57 * Routines used for growing the Btree.
59 STATIC
int xfs_attr_leaf_create(xfs_da_args_t
*args
, xfs_dablk_t which_block
,
61 STATIC
int xfs_attr_leaf_add_work(xfs_dabuf_t
*leaf_buffer
, xfs_da_args_t
*args
,
63 STATIC
void xfs_attr_leaf_compact(xfs_trans_t
*trans
, xfs_dabuf_t
*leaf_buffer
);
64 STATIC
void xfs_attr_leaf_rebalance(xfs_da_state_t
*state
,
65 xfs_da_state_blk_t
*blk1
,
66 xfs_da_state_blk_t
*blk2
);
67 STATIC
int xfs_attr_leaf_figure_balance(xfs_da_state_t
*state
,
68 xfs_da_state_blk_t
*leaf_blk_1
,
69 xfs_da_state_blk_t
*leaf_blk_2
,
70 int *number_entries_in_blk1
,
71 int *number_usedbytes_in_blk1
);
74 * Routines used for shrinking the Btree.
76 STATIC
int xfs_attr_node_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
,
77 xfs_dabuf_t
*bp
, int level
);
78 STATIC
int xfs_attr_leaf_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
,
80 STATIC
int xfs_attr_leaf_freextent(xfs_trans_t
**trans
, xfs_inode_t
*dp
,
81 xfs_dablk_t blkno
, int blkcnt
);
86 STATIC
void xfs_attr_leaf_moveents(xfs_attr_leafblock_t
*src_leaf
,
88 xfs_attr_leafblock_t
*dst_leaf
,
89 int dst_start
, int move_count
,
91 STATIC
int xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
);
93 /*========================================================================
94 * Namespace helper routines
95 *========================================================================*/
97 STATIC
inline attrnames_t
*
98 xfs_attr_flags_namesp(int flags
)
100 return ((flags
& XFS_ATTR_SECURE
) ? &attr_secure
:
101 ((flags
& XFS_ATTR_ROOT
) ? &attr_trusted
: &attr_user
));
105 * If namespace bits don't match return 0.
106 * If all match then return 1.
109 xfs_attr_namesp_match(int arg_flags
, int ondisk_flags
)
111 return XFS_ATTR_NSP_ONDISK(ondisk_flags
) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags
);
115 * If namespace bits don't match and we don't have an override for it
117 * If all match or are overridable then return 1.
120 xfs_attr_namesp_match_overrides(int arg_flags
, int ondisk_flags
)
122 if (((arg_flags
& ATTR_SECURE
) == 0) !=
123 ((ondisk_flags
& XFS_ATTR_SECURE
) == 0) &&
124 !(arg_flags
& ATTR_KERNORMALS
))
126 if (((arg_flags
& ATTR_ROOT
) == 0) !=
127 ((ondisk_flags
& XFS_ATTR_ROOT
) == 0) &&
128 !(arg_flags
& ATTR_KERNROOTLS
))
134 /*========================================================================
135 * External routines when attribute fork size < XFS_LITINO(mp).
136 *========================================================================*/
139 * Query whether the requested number of additional bytes of extended
140 * attribute space will be able to fit inline.
141 * Returns zero if not, else the di_forkoff fork offset to be used in the
142 * literal area for attribute data once the new bytes have been added.
144 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
145 * special case for dev/uuid inodes, they have fixed size data forks.
148 xfs_attr_shortform_bytesfit(xfs_inode_t
*dp
, int bytes
)
151 int minforkoff
; /* lower limit on valid forkoff locations */
152 int maxforkoff
; /* upper limit on valid forkoff locations */
153 xfs_mount_t
*mp
= dp
->i_mount
;
155 offset
= (XFS_LITINO(mp
) - bytes
) >> 3; /* rounded down */
157 switch (dp
->i_d
.di_format
) {
158 case XFS_DINODE_FMT_DEV
:
159 minforkoff
= roundup(sizeof(xfs_dev_t
), 8) >> 3;
160 return (offset
>= minforkoff
) ? minforkoff
: 0;
161 case XFS_DINODE_FMT_UUID
:
162 minforkoff
= roundup(sizeof(uuid_t
), 8) >> 3;
163 return (offset
>= minforkoff
) ? minforkoff
: 0;
166 if (!(mp
->m_flags
& XFS_MOUNT_ATTR2
)) {
167 if (bytes
<= XFS_IFORK_ASIZE(dp
))
168 return mp
->m_attroffset
>> 3;
172 /* data fork btree root can have at least this many key/ptr pairs */
173 minforkoff
= MAX(dp
->i_df
.if_bytes
, XFS_BMDR_SPACE_CALC(MINDBTPTRS
));
174 minforkoff
= roundup(minforkoff
, 8) >> 3;
176 /* attr fork btree root can have at least this many key/ptr pairs */
177 maxforkoff
= XFS_LITINO(mp
) - XFS_BMDR_SPACE_CALC(MINABTPTRS
);
178 maxforkoff
= maxforkoff
>> 3; /* rounded down */
180 if (offset
>= minforkoff
&& offset
< maxforkoff
)
182 if (offset
>= maxforkoff
)
188 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
191 xfs_sbversion_add_attr2(xfs_mount_t
*mp
, xfs_trans_t
*tp
)
195 if ((mp
->m_flags
& XFS_MOUNT_ATTR2
) &&
196 !(XFS_SB_VERSION_HASATTR2(&mp
->m_sb
))) {
198 if (!XFS_SB_VERSION_HASATTR2(&mp
->m_sb
)) {
199 XFS_SB_VERSION_ADDATTR2(&mp
->m_sb
);
200 XFS_SB_UNLOCK(mp
, s
);
201 xfs_mod_sb(tp
, XFS_SB_VERSIONNUM
| XFS_SB_FEATURES2
);
203 XFS_SB_UNLOCK(mp
, s
);
208 * Create the initial contents of a shortform attribute list.
211 xfs_attr_shortform_create(xfs_da_args_t
*args
)
213 xfs_attr_sf_hdr_t
*hdr
;
221 ASSERT(ifp
->if_bytes
== 0);
222 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) {
223 ifp
->if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
224 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_LOCAL
;
225 ifp
->if_flags
|= XFS_IFINLINE
;
227 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
229 xfs_idata_realloc(dp
, sizeof(*hdr
), XFS_ATTR_FORK
);
230 hdr
= (xfs_attr_sf_hdr_t
*)ifp
->if_u1
.if_data
;
232 hdr
->totsize
= cpu_to_be16(sizeof(*hdr
));
233 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
237 * Add a name/value pair to the shortform attribute list.
238 * Overflow from the inode has already been checked for.
241 xfs_attr_shortform_add(xfs_da_args_t
*args
, int forkoff
)
243 xfs_attr_shortform_t
*sf
;
244 xfs_attr_sf_entry_t
*sfe
;
252 dp
->i_d
.di_forkoff
= forkoff
;
253 dp
->i_df
.if_ext_max
=
254 XFS_IFORK_DSIZE(dp
) / (uint
)sizeof(xfs_bmbt_rec_t
);
255 dp
->i_afp
->if_ext_max
=
256 XFS_IFORK_ASIZE(dp
) / (uint
)sizeof(xfs_bmbt_rec_t
);
259 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
260 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
262 for (i
= 0; i
< sf
->hdr
.count
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
264 if (sfe
->namelen
!= args
->namelen
)
266 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
268 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
274 offset
= (char *)sfe
- (char *)sf
;
275 size
= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
276 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
);
277 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
278 sfe
= (xfs_attr_sf_entry_t
*)((char *)sf
+ offset
);
280 sfe
->namelen
= args
->namelen
;
281 sfe
->valuelen
= args
->valuelen
;
282 sfe
->flags
= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
283 memcpy(sfe
->nameval
, args
->name
, args
->namelen
);
284 memcpy(&sfe
->nameval
[args
->namelen
], args
->value
, args
->valuelen
);
286 be16_add(&sf
->hdr
.totsize
, size
);
287 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
289 xfs_sbversion_add_attr2(mp
, args
->trans
);
293 * Remove an attribute from the shortform attribute list structure.
296 xfs_attr_shortform_remove(xfs_da_args_t
*args
)
298 xfs_attr_shortform_t
*sf
;
299 xfs_attr_sf_entry_t
*sfe
;
300 int base
, size
=0, end
, totsize
, i
;
306 base
= sizeof(xfs_attr_sf_hdr_t
);
307 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
310 for (i
= 0; i
< end
; sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
),
312 size
= XFS_ATTR_SF_ENTSIZE(sfe
);
313 if (sfe
->namelen
!= args
->namelen
)
315 if (memcmp(sfe
->nameval
, args
->name
, args
->namelen
) != 0)
317 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
322 return(XFS_ERROR(ENOATTR
));
325 * Fix up the attribute fork data, covering the hole
328 totsize
= be16_to_cpu(sf
->hdr
.totsize
);
330 memmove(&((char *)sf
)[base
], &((char *)sf
)[end
], totsize
- end
);
332 be16_add(&sf
->hdr
.totsize
, -size
);
335 * Fix up the start offset of the attribute fork
338 if (totsize
== sizeof(xfs_attr_sf_hdr_t
) && !args
->addname
&&
339 (mp
->m_flags
& XFS_MOUNT_ATTR2
)) {
341 * Last attribute now removed, revert to original
342 * inode format making all literal area available
343 * to the data fork once more.
345 xfs_idestroy_fork(dp
, XFS_ATTR_FORK
);
346 dp
->i_d
.di_forkoff
= 0;
347 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
348 ASSERT(dp
->i_d
.di_anextents
== 0);
349 ASSERT(dp
->i_afp
== NULL
);
350 dp
->i_df
.if_ext_max
=
351 XFS_IFORK_DSIZE(dp
) / (uint
)sizeof(xfs_bmbt_rec_t
);
352 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
354 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
355 dp
->i_d
.di_forkoff
= xfs_attr_shortform_bytesfit(dp
, totsize
);
356 ASSERT(dp
->i_d
.di_forkoff
);
357 ASSERT(totsize
> sizeof(xfs_attr_sf_hdr_t
) || args
->addname
||
358 !(mp
->m_flags
& XFS_MOUNT_ATTR2
));
359 dp
->i_afp
->if_ext_max
=
360 XFS_IFORK_ASIZE(dp
) / (uint
)sizeof(xfs_bmbt_rec_t
);
361 dp
->i_df
.if_ext_max
=
362 XFS_IFORK_DSIZE(dp
) / (uint
)sizeof(xfs_bmbt_rec_t
);
363 xfs_trans_log_inode(args
->trans
, dp
,
364 XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
367 xfs_sbversion_add_attr2(mp
, args
->trans
);
373 * Look up a name in a shortform attribute list structure.
377 xfs_attr_shortform_lookup(xfs_da_args_t
*args
)
379 xfs_attr_shortform_t
*sf
;
380 xfs_attr_sf_entry_t
*sfe
;
384 ifp
= args
->dp
->i_afp
;
385 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
386 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
388 for (i
= 0; i
< sf
->hdr
.count
;
389 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
390 if (sfe
->namelen
!= args
->namelen
)
392 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
394 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
396 return(XFS_ERROR(EEXIST
));
398 return(XFS_ERROR(ENOATTR
));
402 * Look up a name in a shortform attribute list structure.
406 xfs_attr_shortform_getvalue(xfs_da_args_t
*args
)
408 xfs_attr_shortform_t
*sf
;
409 xfs_attr_sf_entry_t
*sfe
;
412 ASSERT(args
->dp
->i_d
.di_aformat
== XFS_IFINLINE
);
413 sf
= (xfs_attr_shortform_t
*)args
->dp
->i_afp
->if_u1
.if_data
;
415 for (i
= 0; i
< sf
->hdr
.count
;
416 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
417 if (sfe
->namelen
!= args
->namelen
)
419 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
421 if (!xfs_attr_namesp_match(args
->flags
, sfe
->flags
))
423 if (args
->flags
& ATTR_KERNOVAL
) {
424 args
->valuelen
= sfe
->valuelen
;
425 return(XFS_ERROR(EEXIST
));
427 if (args
->valuelen
< sfe
->valuelen
) {
428 args
->valuelen
= sfe
->valuelen
;
429 return(XFS_ERROR(ERANGE
));
431 args
->valuelen
= sfe
->valuelen
;
432 memcpy(args
->value
, &sfe
->nameval
[args
->namelen
],
434 return(XFS_ERROR(EEXIST
));
436 return(XFS_ERROR(ENOATTR
));
440 * Convert from using the shortform to the leaf.
443 xfs_attr_shortform_to_leaf(xfs_da_args_t
*args
)
446 xfs_attr_shortform_t
*sf
;
447 xfs_attr_sf_entry_t
*sfe
;
457 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
458 size
= be16_to_cpu(sf
->hdr
.totsize
);
459 tmpbuffer
= kmem_alloc(size
, KM_SLEEP
);
460 ASSERT(tmpbuffer
!= NULL
);
461 memcpy(tmpbuffer
, ifp
->if_u1
.if_data
, size
);
462 sf
= (xfs_attr_shortform_t
*)tmpbuffer
;
464 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
466 error
= xfs_da_grow_inode(args
, &blkno
);
469 * If we hit an IO error middle of the transaction inside
470 * grow_inode(), we may have inconsistent data. Bail out.
474 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
475 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
480 error
= xfs_attr_leaf_create(args
, blkno
, &bp
);
482 error
= xfs_da_shrink_inode(args
, 0, bp
);
486 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
487 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
491 memset((char *)&nargs
, 0, sizeof(nargs
));
493 nargs
.firstblock
= args
->firstblock
;
494 nargs
.flist
= args
->flist
;
495 nargs
.total
= args
->total
;
496 nargs
.whichfork
= XFS_ATTR_FORK
;
497 nargs
.trans
= args
->trans
;
501 for (i
= 0; i
< sf
->hdr
.count
; i
++) {
502 nargs
.name
= (char *)sfe
->nameval
;
503 nargs
.namelen
= sfe
->namelen
;
504 nargs
.value
= (char *)&sfe
->nameval
[nargs
.namelen
];
505 nargs
.valuelen
= sfe
->valuelen
;
506 nargs
.hashval
= xfs_da_hashname((char *)sfe
->nameval
,
508 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe
->flags
);
509 error
= xfs_attr_leaf_lookup_int(bp
, &nargs
); /* set a->index */
510 ASSERT(error
== ENOATTR
);
511 error
= xfs_attr_leaf_add(bp
, &nargs
);
512 ASSERT(error
!= ENOSPC
);
515 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
522 kmem_free(tmpbuffer
, size
);
527 xfs_attr_shortform_compare(const void *a
, const void *b
)
529 xfs_attr_sf_sort_t
*sa
, *sb
;
531 sa
= (xfs_attr_sf_sort_t
*)a
;
532 sb
= (xfs_attr_sf_sort_t
*)b
;
533 if (sa
->hash
< sb
->hash
) {
535 } else if (sa
->hash
> sb
->hash
) {
538 return(sa
->entno
- sb
->entno
);
543 #define XFS_ISRESET_CURSOR(cursor) \
544 (!((cursor)->initted) && !((cursor)->hashval) && \
545 !((cursor)->blkno) && !((cursor)->offset))
547 * Copy out entries of shortform attribute lists for attr_list().
548 * Shortform attribute lists are not stored in hashval sorted order.
549 * If the output buffer is not large enough to hold them all, then we
550 * we have to calculate each entries' hashvalue and sort them before
551 * we can begin returning them to the user.
555 xfs_attr_shortform_list(xfs_attr_list_context_t
*context
)
557 attrlist_cursor_kern_t
*cursor
;
558 xfs_attr_sf_sort_t
*sbuf
, *sbp
;
559 xfs_attr_shortform_t
*sf
;
560 xfs_attr_sf_entry_t
*sfe
;
562 int sbsize
, nsbuf
, count
, i
;
565 ASSERT(context
!= NULL
);
568 ASSERT(dp
->i_afp
!= NULL
);
569 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
573 cursor
= context
->cursor
;
574 ASSERT(cursor
!= NULL
);
576 xfs_attr_trace_l_c("sf start", context
);
579 * If the buffer is large enough and the cursor is at the start,
580 * do not bother with sorting since we will return everything in
581 * one buffer and another call using the cursor won't need to be
583 * Note the generous fudge factor of 16 overhead bytes per entry.
584 * If bufsize is zero then put_listent must be a search function
585 * and can just scan through what we have.
587 if (context
->bufsize
== 0 ||
588 (XFS_ISRESET_CURSOR(cursor
) &&
589 (dp
->i_afp
->if_bytes
+ sf
->hdr
.count
* 16) < context
->bufsize
)) {
590 for (i
= 0, sfe
= &sf
->list
[0]; i
< sf
->hdr
.count
; i
++) {
593 if (!xfs_attr_namesp_match_overrides(context
->flags
, sfe
->flags
)) {
594 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
597 namesp
= xfs_attr_flags_namesp(sfe
->flags
);
598 error
= context
->put_listent(context
,
600 (char *)sfe
->nameval
,
603 (char*)&sfe
->nameval
[sfe
->namelen
]);
606 * Either search callback finished early or
607 * didn't fit it all in the buffer after all.
609 if (context
->seen_enough
)
614 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
616 xfs_attr_trace_l_c("sf big-gulp", context
);
620 /* do no more for a search callback */
621 if (context
->bufsize
== 0)
625 * It didn't all fit, so we have to sort everything on hashval.
627 sbsize
= sf
->hdr
.count
* sizeof(*sbuf
);
628 sbp
= sbuf
= kmem_alloc(sbsize
, KM_SLEEP
);
631 * Scan the attribute list for the rest of the entries, storing
632 * the relevant info from only those that match into a buffer.
635 for (i
= 0, sfe
= &sf
->list
[0]; i
< sf
->hdr
.count
; i
++) {
637 ((char *)sfe
< (char *)sf
) ||
638 ((char *)sfe
>= ((char *)sf
+ dp
->i_afp
->if_bytes
)))) {
639 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
641 context
->dp
->i_mount
, sfe
);
642 xfs_attr_trace_l_c("sf corrupted", context
);
643 kmem_free(sbuf
, sbsize
);
644 return XFS_ERROR(EFSCORRUPTED
);
646 if (!xfs_attr_namesp_match_overrides(context
->flags
, sfe
->flags
)) {
647 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
651 sbp
->hash
= xfs_da_hashname((char *)sfe
->nameval
, sfe
->namelen
);
652 sbp
->name
= (char *)sfe
->nameval
;
653 sbp
->namelen
= sfe
->namelen
;
654 /* These are bytes, and both on-disk, don't endian-flip */
655 sbp
->valuelen
= sfe
->valuelen
;
656 sbp
->flags
= sfe
->flags
;
657 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
663 * Sort the entries on hash then entno.
665 xfs_sort(sbuf
, nsbuf
, sizeof(*sbuf
), xfs_attr_shortform_compare
);
668 * Re-find our place IN THE SORTED LIST.
673 for (sbp
= sbuf
, i
= 0; i
< nsbuf
; i
++, sbp
++) {
674 if (sbp
->hash
== cursor
->hashval
) {
675 if (cursor
->offset
== count
) {
679 } else if (sbp
->hash
> cursor
->hashval
) {
684 kmem_free(sbuf
, sbsize
);
685 xfs_attr_trace_l_c("blk end", context
);
690 * Loop putting entries into the user buffer.
692 for ( ; i
< nsbuf
; i
++, sbp
++) {
695 namesp
= xfs_attr_flags_namesp(sbp
->flags
);
697 if (cursor
->hashval
!= sbp
->hash
) {
698 cursor
->hashval
= sbp
->hash
;
701 error
= context
->put_listent(context
,
706 &sbp
->name
[sbp
->namelen
]);
709 if (context
->seen_enough
)
714 kmem_free(sbuf
, sbsize
);
715 xfs_attr_trace_l_c("sf E-O-F", context
);
720 * Check a leaf attribute block to see if all the entries would fit into
721 * a shortform attribute list.
724 xfs_attr_shortform_allfit(xfs_dabuf_t
*bp
, xfs_inode_t
*dp
)
726 xfs_attr_leafblock_t
*leaf
;
727 xfs_attr_leaf_entry_t
*entry
;
728 xfs_attr_leaf_name_local_t
*name_loc
;
732 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
734 entry
= &leaf
->entries
[0];
735 bytes
= sizeof(struct xfs_attr_sf_hdr
);
736 for (i
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); entry
++, i
++) {
737 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
738 continue; /* don't copy partial entries */
739 if (!(entry
->flags
& XFS_ATTR_LOCAL
))
741 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, i
);
742 if (name_loc
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
744 if (be16_to_cpu(name_loc
->valuelen
) >= XFS_ATTR_SF_ENTSIZE_MAX
)
746 bytes
+= sizeof(struct xfs_attr_sf_entry
)-1
748 + be16_to_cpu(name_loc
->valuelen
);
750 if ((dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
) &&
751 (bytes
== sizeof(struct xfs_attr_sf_hdr
)))
753 return(xfs_attr_shortform_bytesfit(dp
, bytes
));
757 * Convert a leaf attribute list to shortform attribute list
760 xfs_attr_leaf_to_shortform(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
, int forkoff
)
762 xfs_attr_leafblock_t
*leaf
;
763 xfs_attr_leaf_entry_t
*entry
;
764 xfs_attr_leaf_name_local_t
*name_loc
;
771 tmpbuffer
= kmem_alloc(XFS_LBSIZE(dp
->i_mount
), KM_SLEEP
);
772 ASSERT(tmpbuffer
!= NULL
);
775 memcpy(tmpbuffer
, bp
->data
, XFS_LBSIZE(dp
->i_mount
));
776 leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
777 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
778 memset(bp
->data
, 0, XFS_LBSIZE(dp
->i_mount
));
781 * Clean out the prior contents of the attribute list.
783 error
= xfs_da_shrink_inode(args
, 0, bp
);
788 ASSERT(dp
->i_mount
->m_flags
& XFS_MOUNT_ATTR2
);
791 * Last attribute was removed, revert to original
792 * inode format making all literal area available
793 * to the data fork once more.
795 xfs_idestroy_fork(dp
, XFS_ATTR_FORK
);
796 dp
->i_d
.di_forkoff
= 0;
797 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
798 ASSERT(dp
->i_d
.di_anextents
== 0);
799 ASSERT(dp
->i_afp
== NULL
);
800 dp
->i_df
.if_ext_max
=
801 XFS_IFORK_DSIZE(dp
) / (uint
)sizeof(xfs_bmbt_rec_t
);
802 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
);
806 xfs_attr_shortform_create(args
);
809 * Copy the attributes
811 memset((char *)&nargs
, 0, sizeof(nargs
));
813 nargs
.firstblock
= args
->firstblock
;
814 nargs
.flist
= args
->flist
;
815 nargs
.total
= args
->total
;
816 nargs
.whichfork
= XFS_ATTR_FORK
;
817 nargs
.trans
= args
->trans
;
819 entry
= &leaf
->entries
[0];
820 for (i
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); entry
++, i
++) {
821 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
822 continue; /* don't copy partial entries */
825 ASSERT(entry
->flags
& XFS_ATTR_LOCAL
);
826 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, i
);
827 nargs
.name
= (char *)name_loc
->nameval
;
828 nargs
.namelen
= name_loc
->namelen
;
829 nargs
.value
= (char *)&name_loc
->nameval
[nargs
.namelen
];
830 nargs
.valuelen
= be16_to_cpu(name_loc
->valuelen
);
831 nargs
.hashval
= be32_to_cpu(entry
->hashval
);
832 nargs
.flags
= XFS_ATTR_NSP_ONDISK_TO_ARGS(entry
->flags
);
833 xfs_attr_shortform_add(&nargs
, forkoff
);
838 kmem_free(tmpbuffer
, XFS_LBSIZE(dp
->i_mount
));
843 * Convert from using a single leaf to a root node and a leaf.
846 xfs_attr_leaf_to_node(xfs_da_args_t
*args
)
848 xfs_attr_leafblock_t
*leaf
;
849 xfs_da_intnode_t
*node
;
851 xfs_dabuf_t
*bp1
, *bp2
;
857 error
= xfs_da_grow_inode(args
, &blkno
);
860 error
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp1
,
866 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp2
,
871 memcpy(bp2
->data
, bp1
->data
, XFS_LBSIZE(dp
->i_mount
));
872 xfs_da_buf_done(bp1
);
874 xfs_da_log_buf(args
->trans
, bp2
, 0, XFS_LBSIZE(dp
->i_mount
) - 1);
877 * Set up the new root node.
879 error
= xfs_da_node_create(args
, 0, 1, &bp1
, XFS_ATTR_FORK
);
884 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
885 /* both on-disk, don't endian-flip twice */
886 node
->btree
[0].hashval
=
887 leaf
->entries
[be16_to_cpu(leaf
->hdr
.count
)-1 ].hashval
;
888 node
->btree
[0].before
= cpu_to_be32(blkno
);
889 node
->hdr
.count
= cpu_to_be16(1);
890 xfs_da_log_buf(args
->trans
, bp1
, 0, XFS_LBSIZE(dp
->i_mount
) - 1);
894 xfs_da_buf_done(bp1
);
896 xfs_da_buf_done(bp2
);
901 /*========================================================================
902 * Routines used for growing the Btree.
903 *========================================================================*/
906 * Create the initial contents of a leaf attribute list
907 * or a leaf in a node attribute list.
910 xfs_attr_leaf_create(xfs_da_args_t
*args
, xfs_dablk_t blkno
, xfs_dabuf_t
**bpp
)
912 xfs_attr_leafblock_t
*leaf
;
913 xfs_attr_leaf_hdr_t
*hdr
;
920 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp
,
926 memset((char *)leaf
, 0, XFS_LBSIZE(dp
->i_mount
));
928 hdr
->info
.magic
= cpu_to_be16(XFS_ATTR_LEAF_MAGIC
);
929 hdr
->firstused
= cpu_to_be16(XFS_LBSIZE(dp
->i_mount
));
930 if (!hdr
->firstused
) {
931 hdr
->firstused
= cpu_to_be16(
932 XFS_LBSIZE(dp
->i_mount
) - XFS_ATTR_LEAF_NAME_ALIGN
);
935 hdr
->freemap
[0].base
= cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t
));
936 hdr
->freemap
[0].size
= cpu_to_be16(be16_to_cpu(hdr
->firstused
) -
937 sizeof(xfs_attr_leaf_hdr_t
));
939 xfs_da_log_buf(args
->trans
, bp
, 0, XFS_LBSIZE(dp
->i_mount
) - 1);
946 * Split the leaf node, rebalance, then add the new entry.
949 xfs_attr_leaf_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
950 xfs_da_state_blk_t
*newblk
)
956 * Allocate space for a new leaf node.
958 ASSERT(oldblk
->magic
== XFS_ATTR_LEAF_MAGIC
);
959 error
= xfs_da_grow_inode(state
->args
, &blkno
);
962 error
= xfs_attr_leaf_create(state
->args
, blkno
, &newblk
->bp
);
965 newblk
->blkno
= blkno
;
966 newblk
->magic
= XFS_ATTR_LEAF_MAGIC
;
969 * Rebalance the entries across the two leaves.
970 * NOTE: rebalance() currently depends on the 2nd block being empty.
972 xfs_attr_leaf_rebalance(state
, oldblk
, newblk
);
973 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
978 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
979 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
980 * "new" attrs info. Will need the "old" info to remove it later.
982 * Insert the "new" entry in the correct block.
985 error
= xfs_attr_leaf_add(oldblk
->bp
, state
->args
);
987 error
= xfs_attr_leaf_add(newblk
->bp
, state
->args
);
990 * Update last hashval in each block since we added the name.
992 oldblk
->hashval
= xfs_attr_leaf_lasthash(oldblk
->bp
, NULL
);
993 newblk
->hashval
= xfs_attr_leaf_lasthash(newblk
->bp
, NULL
);
998 * Add a name to the leaf attribute list structure.
1001 xfs_attr_leaf_add(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
1003 xfs_attr_leafblock_t
*leaf
;
1004 xfs_attr_leaf_hdr_t
*hdr
;
1005 xfs_attr_leaf_map_t
*map
;
1006 int tablesize
, entsize
, sum
, tmp
, i
;
1009 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1010 ASSERT((args
->index
>= 0)
1011 && (args
->index
<= be16_to_cpu(leaf
->hdr
.count
)));
1013 entsize
= xfs_attr_leaf_newentsize(args
->namelen
, args
->valuelen
,
1014 args
->trans
->t_mountp
->m_sb
.sb_blocksize
, NULL
);
1017 * Search through freemap for first-fit on new name length.
1018 * (may need to figure in size of entry struct too)
1020 tablesize
= (be16_to_cpu(hdr
->count
) + 1)
1021 * sizeof(xfs_attr_leaf_entry_t
)
1022 + sizeof(xfs_attr_leaf_hdr_t
);
1023 map
= &hdr
->freemap
[XFS_ATTR_LEAF_MAPSIZE
-1];
1024 for (sum
= 0, i
= XFS_ATTR_LEAF_MAPSIZE
-1; i
>= 0; map
--, i
--) {
1025 if (tablesize
> be16_to_cpu(hdr
->firstused
)) {
1026 sum
+= be16_to_cpu(map
->size
);
1030 continue; /* no space in this map */
1032 if (be16_to_cpu(map
->base
) < be16_to_cpu(hdr
->firstused
))
1033 tmp
+= sizeof(xfs_attr_leaf_entry_t
);
1034 if (be16_to_cpu(map
->size
) >= tmp
) {
1035 tmp
= xfs_attr_leaf_add_work(bp
, args
, i
);
1038 sum
+= be16_to_cpu(map
->size
);
1042 * If there are no holes in the address space of the block,
1043 * and we don't have enough freespace, then compaction will do us
1044 * no good and we should just give up.
1046 if (!hdr
->holes
&& (sum
< entsize
))
1047 return(XFS_ERROR(ENOSPC
));
1050 * Compact the entries to coalesce free space.
1051 * This may change the hdr->count via dropping INCOMPLETE entries.
1053 xfs_attr_leaf_compact(args
->trans
, bp
);
1056 * After compaction, the block is guaranteed to have only one
1057 * free region, in freemap[0]. If it is not big enough, give up.
1059 if (be16_to_cpu(hdr
->freemap
[0].size
)
1060 < (entsize
+ sizeof(xfs_attr_leaf_entry_t
)))
1061 return(XFS_ERROR(ENOSPC
));
1063 return(xfs_attr_leaf_add_work(bp
, args
, 0));
1067 * Add a name to a leaf attribute list structure.
1070 xfs_attr_leaf_add_work(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
, int mapindex
)
1072 xfs_attr_leafblock_t
*leaf
;
1073 xfs_attr_leaf_hdr_t
*hdr
;
1074 xfs_attr_leaf_entry_t
*entry
;
1075 xfs_attr_leaf_name_local_t
*name_loc
;
1076 xfs_attr_leaf_name_remote_t
*name_rmt
;
1077 xfs_attr_leaf_map_t
*map
;
1082 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1084 ASSERT((mapindex
>= 0) && (mapindex
< XFS_ATTR_LEAF_MAPSIZE
));
1085 ASSERT((args
->index
>= 0) && (args
->index
<= be16_to_cpu(hdr
->count
)));
1088 * Force open some space in the entry array and fill it in.
1090 entry
= &leaf
->entries
[args
->index
];
1091 if (args
->index
< be16_to_cpu(hdr
->count
)) {
1092 tmp
= be16_to_cpu(hdr
->count
) - args
->index
;
1093 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
1094 memmove((char *)(entry
+1), (char *)entry
, tmp
);
1095 xfs_da_log_buf(args
->trans
, bp
,
1096 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1098 be16_add(&hdr
->count
, 1);
1101 * Allocate space for the new string (at the end of the run).
1103 map
= &hdr
->freemap
[mapindex
];
1104 mp
= args
->trans
->t_mountp
;
1105 ASSERT(be16_to_cpu(map
->base
) < XFS_LBSIZE(mp
));
1106 ASSERT((be16_to_cpu(map
->base
) & 0x3) == 0);
1107 ASSERT(be16_to_cpu(map
->size
) >=
1108 xfs_attr_leaf_newentsize(args
->namelen
, args
->valuelen
,
1109 mp
->m_sb
.sb_blocksize
, NULL
));
1110 ASSERT(be16_to_cpu(map
->size
) < XFS_LBSIZE(mp
));
1111 ASSERT((be16_to_cpu(map
->size
) & 0x3) == 0);
1112 be16_add(&map
->size
,
1113 -xfs_attr_leaf_newentsize(args
->namelen
, args
->valuelen
,
1114 mp
->m_sb
.sb_blocksize
, &tmp
));
1115 entry
->nameidx
= cpu_to_be16(be16_to_cpu(map
->base
) +
1116 be16_to_cpu(map
->size
));
1117 entry
->hashval
= cpu_to_be32(args
->hashval
);
1118 entry
->flags
= tmp
? XFS_ATTR_LOCAL
: 0;
1119 entry
->flags
|= XFS_ATTR_NSP_ARGS_TO_ONDISK(args
->flags
);
1121 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1122 if ((args
->blkno2
== args
->blkno
) &&
1123 (args
->index2
<= args
->index
)) {
1127 xfs_da_log_buf(args
->trans
, bp
,
1128 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
1129 ASSERT((args
->index
== 0) ||
1130 (be32_to_cpu(entry
->hashval
) >= be32_to_cpu((entry
-1)->hashval
)));
1131 ASSERT((args
->index
== be16_to_cpu(hdr
->count
)-1) ||
1132 (be32_to_cpu(entry
->hashval
) <= be32_to_cpu((entry
+1)->hashval
)));
1135 * Copy the attribute name and value into the new space.
1137 * For "remote" attribute values, simply note that we need to
1138 * allocate space for the "remote" value. We can't actually
1139 * allocate the extents in this transaction, and we can't decide
1140 * which blocks they should be as we might allocate more blocks
1141 * as part of this transaction (a split operation for example).
1143 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1144 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, args
->index
);
1145 name_loc
->namelen
= args
->namelen
;
1146 name_loc
->valuelen
= cpu_to_be16(args
->valuelen
);
1147 memcpy((char *)name_loc
->nameval
, args
->name
, args
->namelen
);
1148 memcpy((char *)&name_loc
->nameval
[args
->namelen
], args
->value
,
1149 be16_to_cpu(name_loc
->valuelen
));
1151 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
1152 name_rmt
->namelen
= args
->namelen
;
1153 memcpy((char *)name_rmt
->name
, args
->name
, args
->namelen
);
1154 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1156 name_rmt
->valuelen
= 0;
1157 name_rmt
->valueblk
= 0;
1159 args
->rmtblkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
1161 xfs_da_log_buf(args
->trans
, bp
,
1162 XFS_DA_LOGRANGE(leaf
, XFS_ATTR_LEAF_NAME(leaf
, args
->index
),
1163 xfs_attr_leaf_entsize(leaf
, args
->index
)));
1166 * Update the control info for this leaf node
1168 if (be16_to_cpu(entry
->nameidx
) < be16_to_cpu(hdr
->firstused
)) {
1169 /* both on-disk, don't endian-flip twice */
1170 hdr
->firstused
= entry
->nameidx
;
1172 ASSERT(be16_to_cpu(hdr
->firstused
) >=
1173 ((be16_to_cpu(hdr
->count
) * sizeof(*entry
)) + sizeof(*hdr
)));
1174 tmp
= (be16_to_cpu(hdr
->count
)-1) * sizeof(xfs_attr_leaf_entry_t
)
1175 + sizeof(xfs_attr_leaf_hdr_t
);
1176 map
= &hdr
->freemap
[0];
1177 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; map
++, i
++) {
1178 if (be16_to_cpu(map
->base
) == tmp
) {
1179 be16_add(&map
->base
, sizeof(xfs_attr_leaf_entry_t
));
1180 be16_add(&map
->size
,
1181 -((int)sizeof(xfs_attr_leaf_entry_t
)));
1184 be16_add(&hdr
->usedbytes
, xfs_attr_leaf_entsize(leaf
, args
->index
));
1185 xfs_da_log_buf(args
->trans
, bp
,
1186 XFS_DA_LOGRANGE(leaf
, hdr
, sizeof(*hdr
)));
1191 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1194 xfs_attr_leaf_compact(xfs_trans_t
*trans
, xfs_dabuf_t
*bp
)
1196 xfs_attr_leafblock_t
*leaf_s
, *leaf_d
;
1197 xfs_attr_leaf_hdr_t
*hdr_s
, *hdr_d
;
1201 mp
= trans
->t_mountp
;
1202 tmpbuffer
= kmem_alloc(XFS_LBSIZE(mp
), KM_SLEEP
);
1203 ASSERT(tmpbuffer
!= NULL
);
1204 memcpy(tmpbuffer
, bp
->data
, XFS_LBSIZE(mp
));
1205 memset(bp
->data
, 0, XFS_LBSIZE(mp
));
1208 * Copy basic information
1210 leaf_s
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1212 hdr_s
= &leaf_s
->hdr
;
1213 hdr_d
= &leaf_d
->hdr
;
1214 hdr_d
->info
= hdr_s
->info
; /* struct copy */
1215 hdr_d
->firstused
= cpu_to_be16(XFS_LBSIZE(mp
));
1216 /* handle truncation gracefully */
1217 if (!hdr_d
->firstused
) {
1218 hdr_d
->firstused
= cpu_to_be16(
1219 XFS_LBSIZE(mp
) - XFS_ATTR_LEAF_NAME_ALIGN
);
1221 hdr_d
->usedbytes
= 0;
1224 hdr_d
->freemap
[0].base
= cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t
));
1225 hdr_d
->freemap
[0].size
= cpu_to_be16(be16_to_cpu(hdr_d
->firstused
) -
1226 sizeof(xfs_attr_leaf_hdr_t
));
1229 * Copy all entry's in the same (sorted) order,
1230 * but allocate name/value pairs packed and in sequence.
1232 xfs_attr_leaf_moveents(leaf_s
, 0, leaf_d
, 0,
1233 be16_to_cpu(hdr_s
->count
), mp
);
1234 xfs_da_log_buf(trans
, bp
, 0, XFS_LBSIZE(mp
) - 1);
1236 kmem_free(tmpbuffer
, XFS_LBSIZE(mp
));
1240 * Redistribute the attribute list entries between two leaf nodes,
1241 * taking into account the size of the new entry.
1243 * NOTE: if new block is empty, then it will get the upper half of the
1244 * old block. At present, all (one) callers pass in an empty second block.
1246 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1247 * to match what it is doing in splitting the attribute leaf block. Those
1248 * values are used in "atomic rename" operations on attributes. Note that
1249 * the "new" and "old" values can end up in different blocks.
1252 xfs_attr_leaf_rebalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
1253 xfs_da_state_blk_t
*blk2
)
1255 xfs_da_args_t
*args
;
1256 xfs_da_state_blk_t
*tmp_blk
;
1257 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
1258 xfs_attr_leaf_hdr_t
*hdr1
, *hdr2
;
1259 int count
, totallen
, max
, space
, swap
;
1262 * Set up environment.
1264 ASSERT(blk1
->magic
== XFS_ATTR_LEAF_MAGIC
);
1265 ASSERT(blk2
->magic
== XFS_ATTR_LEAF_MAGIC
);
1266 leaf1
= blk1
->bp
->data
;
1267 leaf2
= blk2
->bp
->data
;
1268 ASSERT(be16_to_cpu(leaf1
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1269 ASSERT(be16_to_cpu(leaf2
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1273 * Check ordering of blocks, reverse if it makes things simpler.
1275 * NOTE: Given that all (current) callers pass in an empty
1276 * second block, this code should never set "swap".
1279 if (xfs_attr_leaf_order(blk1
->bp
, blk2
->bp
)) {
1283 leaf1
= blk1
->bp
->data
;
1284 leaf2
= blk2
->bp
->data
;
1291 * Examine entries until we reduce the absolute difference in
1292 * byte usage between the two blocks to a minimum. Then get
1293 * the direction to copy and the number of elements to move.
1295 * "inleaf" is true if the new entry should be inserted into blk1.
1296 * If "swap" is also true, then reverse the sense of "inleaf".
1298 state
->inleaf
= xfs_attr_leaf_figure_balance(state
, blk1
, blk2
,
1301 state
->inleaf
= !state
->inleaf
;
1304 * Move any entries required from leaf to leaf:
1306 if (count
< be16_to_cpu(hdr1
->count
)) {
1308 * Figure the total bytes to be added to the destination leaf.
1310 /* number entries being moved */
1311 count
= be16_to_cpu(hdr1
->count
) - count
;
1312 space
= be16_to_cpu(hdr1
->usedbytes
) - totallen
;
1313 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1316 * leaf2 is the destination, compact it if it looks tight.
1318 max
= be16_to_cpu(hdr2
->firstused
)
1319 - sizeof(xfs_attr_leaf_hdr_t
);
1320 max
-= be16_to_cpu(hdr2
->count
) * sizeof(xfs_attr_leaf_entry_t
);
1322 xfs_attr_leaf_compact(args
->trans
, blk2
->bp
);
1326 * Move high entries from leaf1 to low end of leaf2.
1328 xfs_attr_leaf_moveents(leaf1
, be16_to_cpu(hdr1
->count
) - count
,
1329 leaf2
, 0, count
, state
->mp
);
1331 xfs_da_log_buf(args
->trans
, blk1
->bp
, 0, state
->blocksize
-1);
1332 xfs_da_log_buf(args
->trans
, blk2
->bp
, 0, state
->blocksize
-1);
1333 } else if (count
> be16_to_cpu(hdr1
->count
)) {
1335 * I assert that since all callers pass in an empty
1336 * second buffer, this code should never execute.
1340 * Figure the total bytes to be added to the destination leaf.
1342 /* number entries being moved */
1343 count
-= be16_to_cpu(hdr1
->count
);
1344 space
= totallen
- be16_to_cpu(hdr1
->usedbytes
);
1345 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1348 * leaf1 is the destination, compact it if it looks tight.
1350 max
= be16_to_cpu(hdr1
->firstused
)
1351 - sizeof(xfs_attr_leaf_hdr_t
);
1352 max
-= be16_to_cpu(hdr1
->count
) * sizeof(xfs_attr_leaf_entry_t
);
1354 xfs_attr_leaf_compact(args
->trans
, blk1
->bp
);
1358 * Move low entries from leaf2 to high end of leaf1.
1360 xfs_attr_leaf_moveents(leaf2
, 0, leaf1
,
1361 be16_to_cpu(hdr1
->count
), count
, state
->mp
);
1363 xfs_da_log_buf(args
->trans
, blk1
->bp
, 0, state
->blocksize
-1);
1364 xfs_da_log_buf(args
->trans
, blk2
->bp
, 0, state
->blocksize
-1);
1368 * Copy out last hashval in each block for B-tree code.
1370 blk1
->hashval
= be32_to_cpu(
1371 leaf1
->entries
[be16_to_cpu(leaf1
->hdr
.count
)-1].hashval
);
1372 blk2
->hashval
= be32_to_cpu(
1373 leaf2
->entries
[be16_to_cpu(leaf2
->hdr
.count
)-1].hashval
);
1376 * Adjust the expected index for insertion.
1377 * NOTE: this code depends on the (current) situation that the
1378 * second block was originally empty.
1380 * If the insertion point moved to the 2nd block, we must adjust
1381 * the index. We must also track the entry just following the
1382 * new entry for use in an "atomic rename" operation, that entry
1383 * is always the "old" entry and the "new" entry is what we are
1384 * inserting. The index/blkno fields refer to the "old" entry,
1385 * while the index2/blkno2 fields refer to the "new" entry.
1387 if (blk1
->index
> be16_to_cpu(leaf1
->hdr
.count
)) {
1388 ASSERT(state
->inleaf
== 0);
1389 blk2
->index
= blk1
->index
- be16_to_cpu(leaf1
->hdr
.count
);
1390 args
->index
= args
->index2
= blk2
->index
;
1391 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1392 } else if (blk1
->index
== be16_to_cpu(leaf1
->hdr
.count
)) {
1393 if (state
->inleaf
) {
1394 args
->index
= blk1
->index
;
1395 args
->blkno
= blk1
->blkno
;
1397 args
->blkno2
= blk2
->blkno
;
1399 blk2
->index
= blk1
->index
1400 - be16_to_cpu(leaf1
->hdr
.count
);
1401 args
->index
= args
->index2
= blk2
->index
;
1402 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1405 ASSERT(state
->inleaf
== 1);
1406 args
->index
= args
->index2
= blk1
->index
;
1407 args
->blkno
= args
->blkno2
= blk1
->blkno
;
1412 * Examine entries until we reduce the absolute difference in
1413 * byte usage between the two blocks to a minimum.
1414 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1415 * GROT: there will always be enough room in either block for a new entry.
1416 * GROT: Do a double-split for this case?
1419 xfs_attr_leaf_figure_balance(xfs_da_state_t
*state
,
1420 xfs_da_state_blk_t
*blk1
,
1421 xfs_da_state_blk_t
*blk2
,
1422 int *countarg
, int *usedbytesarg
)
1424 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
1425 xfs_attr_leaf_hdr_t
*hdr1
, *hdr2
;
1426 xfs_attr_leaf_entry_t
*entry
;
1427 int count
, max
, index
, totallen
, half
;
1428 int lastdelta
, foundit
, tmp
;
1431 * Set up environment.
1433 leaf1
= blk1
->bp
->data
;
1434 leaf2
= blk2
->bp
->data
;
1441 * Examine entries until we reduce the absolute difference in
1442 * byte usage between the two blocks to a minimum.
1444 max
= be16_to_cpu(hdr1
->count
) + be16_to_cpu(hdr2
->count
);
1445 half
= (max
+1) * sizeof(*entry
);
1446 half
+= be16_to_cpu(hdr1
->usedbytes
) +
1447 be16_to_cpu(hdr2
->usedbytes
) +
1448 xfs_attr_leaf_newentsize(
1449 state
->args
->namelen
,
1450 state
->args
->valuelen
,
1451 state
->blocksize
, NULL
);
1453 lastdelta
= state
->blocksize
;
1454 entry
= &leaf1
->entries
[0];
1455 for (count
= index
= 0; count
< max
; entry
++, index
++, count
++) {
1457 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1459 * The new entry is in the first block, account for it.
1461 if (count
== blk1
->index
) {
1462 tmp
= totallen
+ sizeof(*entry
) +
1463 xfs_attr_leaf_newentsize(
1464 state
->args
->namelen
,
1465 state
->args
->valuelen
,
1466 state
->blocksize
, NULL
);
1467 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1469 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1475 * Wrap around into the second block if necessary.
1477 if (count
== be16_to_cpu(hdr1
->count
)) {
1479 entry
= &leaf1
->entries
[0];
1484 * Figure out if next leaf entry would be too much.
1486 tmp
= totallen
+ sizeof(*entry
) + xfs_attr_leaf_entsize(leaf1
,
1488 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1490 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1496 * Calculate the number of usedbytes that will end up in lower block.
1497 * If new entry not in lower block, fix up the count.
1499 totallen
-= count
* sizeof(*entry
);
1501 totallen
-= sizeof(*entry
) +
1502 xfs_attr_leaf_newentsize(
1503 state
->args
->namelen
,
1504 state
->args
->valuelen
,
1505 state
->blocksize
, NULL
);
1509 *usedbytesarg
= totallen
;
1513 /*========================================================================
1514 * Routines used for shrinking the Btree.
1515 *========================================================================*/
1518 * Check a leaf block and its neighbors to see if the block should be
1519 * collapsed into one or the other neighbor. Always keep the block
1520 * with the smaller block number.
1521 * If the current block is over 50% full, don't try to join it, return 0.
1522 * If the block is empty, fill in the state structure and return 2.
1523 * If it can be collapsed, fill in the state structure and return 1.
1524 * If nothing can be done, return 0.
1526 * GROT: allow for INCOMPLETE entries in calculation.
1529 xfs_attr_leaf_toosmall(xfs_da_state_t
*state
, int *action
)
1531 xfs_attr_leafblock_t
*leaf
;
1532 xfs_da_state_blk_t
*blk
;
1533 xfs_da_blkinfo_t
*info
;
1534 int count
, bytes
, forward
, error
, retval
, i
;
1539 * Check for the degenerate case of the block being over 50% full.
1540 * If so, it's not worth even looking to see if we might be able
1541 * to coalesce with a sibling.
1543 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1544 info
= blk
->bp
->data
;
1545 ASSERT(be16_to_cpu(info
->magic
) == XFS_ATTR_LEAF_MAGIC
);
1546 leaf
= (xfs_attr_leafblock_t
*)info
;
1547 count
= be16_to_cpu(leaf
->hdr
.count
);
1548 bytes
= sizeof(xfs_attr_leaf_hdr_t
) +
1549 count
* sizeof(xfs_attr_leaf_entry_t
) +
1550 be16_to_cpu(leaf
->hdr
.usedbytes
);
1551 if (bytes
> (state
->blocksize
>> 1)) {
1552 *action
= 0; /* blk over 50%, don't try to join */
1557 * Check for the degenerate case of the block being empty.
1558 * If the block is empty, we'll simply delete it, no need to
1559 * coalesce it with a sibling block. We choose (arbitrarily)
1560 * to merge with the forward block unless it is NULL.
1564 * Make altpath point to the block we want to keep and
1565 * path point to the block we want to drop (this one).
1567 forward
= (info
->forw
!= 0);
1568 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1569 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
1582 * Examine each sibling block to see if we can coalesce with
1583 * at least 25% free space to spare. We need to figure out
1584 * whether to merge with the forward or the backward block.
1585 * We prefer coalescing with the lower numbered sibling so as
1586 * to shrink an attribute list over time.
1588 /* start with smaller blk num */
1589 forward
= (be32_to_cpu(info
->forw
) < be32_to_cpu(info
->back
));
1590 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1592 blkno
= be32_to_cpu(info
->forw
);
1594 blkno
= be32_to_cpu(info
->back
);
1597 error
= xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
,
1598 blkno
, -1, &bp
, XFS_ATTR_FORK
);
1603 leaf
= (xfs_attr_leafblock_t
*)info
;
1604 count
= be16_to_cpu(leaf
->hdr
.count
);
1605 bytes
= state
->blocksize
- (state
->blocksize
>>2);
1606 bytes
-= be16_to_cpu(leaf
->hdr
.usedbytes
);
1608 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1609 count
+= be16_to_cpu(leaf
->hdr
.count
);
1610 bytes
-= be16_to_cpu(leaf
->hdr
.usedbytes
);
1611 bytes
-= count
* sizeof(xfs_attr_leaf_entry_t
);
1612 bytes
-= sizeof(xfs_attr_leaf_hdr_t
);
1613 xfs_da_brelse(state
->args
->trans
, bp
);
1615 break; /* fits with at least 25% to spare */
1623 * Make altpath point to the block we want to keep (the lower
1624 * numbered block) and path point to the block we want to drop.
1626 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1627 if (blkno
< blk
->blkno
) {
1628 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
1631 error
= xfs_da_path_shift(state
, &state
->path
, forward
,
1645 * Remove a name from the leaf attribute list structure.
1647 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1648 * If two leaves are 37% full, when combined they will leave 25% free.
1651 xfs_attr_leaf_remove(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
1653 xfs_attr_leafblock_t
*leaf
;
1654 xfs_attr_leaf_hdr_t
*hdr
;
1655 xfs_attr_leaf_map_t
*map
;
1656 xfs_attr_leaf_entry_t
*entry
;
1657 int before
, after
, smallest
, entsize
;
1658 int tablesize
, tmp
, i
;
1662 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1664 mp
= args
->trans
->t_mountp
;
1665 ASSERT((be16_to_cpu(hdr
->count
) > 0)
1666 && (be16_to_cpu(hdr
->count
) < (XFS_LBSIZE(mp
)/8)));
1667 ASSERT((args
->index
>= 0)
1668 && (args
->index
< be16_to_cpu(hdr
->count
)));
1669 ASSERT(be16_to_cpu(hdr
->firstused
) >=
1670 ((be16_to_cpu(hdr
->count
) * sizeof(*entry
)) + sizeof(*hdr
)));
1671 entry
= &leaf
->entries
[args
->index
];
1672 ASSERT(be16_to_cpu(entry
->nameidx
) >= be16_to_cpu(hdr
->firstused
));
1673 ASSERT(be16_to_cpu(entry
->nameidx
) < XFS_LBSIZE(mp
));
1676 * Scan through free region table:
1677 * check for adjacency of free'd entry with an existing one,
1678 * find smallest free region in case we need to replace it,
1679 * adjust any map that borders the entry table,
1681 tablesize
= be16_to_cpu(hdr
->count
) * sizeof(xfs_attr_leaf_entry_t
)
1682 + sizeof(xfs_attr_leaf_hdr_t
);
1683 map
= &hdr
->freemap
[0];
1684 tmp
= be16_to_cpu(map
->size
);
1685 before
= after
= -1;
1686 smallest
= XFS_ATTR_LEAF_MAPSIZE
- 1;
1687 entsize
= xfs_attr_leaf_entsize(leaf
, args
->index
);
1688 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; map
++, i
++) {
1689 ASSERT(be16_to_cpu(map
->base
) < XFS_LBSIZE(mp
));
1690 ASSERT(be16_to_cpu(map
->size
) < XFS_LBSIZE(mp
));
1691 if (be16_to_cpu(map
->base
) == tablesize
) {
1692 be16_add(&map
->base
,
1693 -((int)sizeof(xfs_attr_leaf_entry_t
)));
1694 be16_add(&map
->size
, sizeof(xfs_attr_leaf_entry_t
));
1697 if ((be16_to_cpu(map
->base
) + be16_to_cpu(map
->size
))
1698 == be16_to_cpu(entry
->nameidx
)) {
1700 } else if (be16_to_cpu(map
->base
)
1701 == (be16_to_cpu(entry
->nameidx
) + entsize
)) {
1703 } else if (be16_to_cpu(map
->size
) < tmp
) {
1704 tmp
= be16_to_cpu(map
->size
);
1710 * Coalesce adjacent freemap regions,
1711 * or replace the smallest region.
1713 if ((before
>= 0) || (after
>= 0)) {
1714 if ((before
>= 0) && (after
>= 0)) {
1715 map
= &hdr
->freemap
[before
];
1716 be16_add(&map
->size
, entsize
);
1717 be16_add(&map
->size
,
1718 be16_to_cpu(hdr
->freemap
[after
].size
));
1719 hdr
->freemap
[after
].base
= 0;
1720 hdr
->freemap
[after
].size
= 0;
1721 } else if (before
>= 0) {
1722 map
= &hdr
->freemap
[before
];
1723 be16_add(&map
->size
, entsize
);
1725 map
= &hdr
->freemap
[after
];
1726 /* both on-disk, don't endian flip twice */
1727 map
->base
= entry
->nameidx
;
1728 be16_add(&map
->size
, entsize
);
1732 * Replace smallest region (if it is smaller than free'd entry)
1734 map
= &hdr
->freemap
[smallest
];
1735 if (be16_to_cpu(map
->size
) < entsize
) {
1736 map
->base
= cpu_to_be16(be16_to_cpu(entry
->nameidx
));
1737 map
->size
= cpu_to_be16(entsize
);
1742 * Did we remove the first entry?
1744 if (be16_to_cpu(entry
->nameidx
) == be16_to_cpu(hdr
->firstused
))
1750 * Compress the remaining entries and zero out the removed stuff.
1752 memset(XFS_ATTR_LEAF_NAME(leaf
, args
->index
), 0, entsize
);
1753 be16_add(&hdr
->usedbytes
, -entsize
);
1754 xfs_da_log_buf(args
->trans
, bp
,
1755 XFS_DA_LOGRANGE(leaf
, XFS_ATTR_LEAF_NAME(leaf
, args
->index
),
1758 tmp
= (be16_to_cpu(hdr
->count
) - args
->index
)
1759 * sizeof(xfs_attr_leaf_entry_t
);
1760 memmove((char *)entry
, (char *)(entry
+1), tmp
);
1761 be16_add(&hdr
->count
, -1);
1762 xfs_da_log_buf(args
->trans
, bp
,
1763 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1764 entry
= &leaf
->entries
[be16_to_cpu(hdr
->count
)];
1765 memset((char *)entry
, 0, sizeof(xfs_attr_leaf_entry_t
));
1768 * If we removed the first entry, re-find the first used byte
1769 * in the name area. Note that if the entry was the "firstused",
1770 * then we don't have a "hole" in our block resulting from
1771 * removing the name.
1774 tmp
= XFS_LBSIZE(mp
);
1775 entry
= &leaf
->entries
[0];
1776 for (i
= be16_to_cpu(hdr
->count
)-1; i
>= 0; entry
++, i
--) {
1777 ASSERT(be16_to_cpu(entry
->nameidx
) >=
1778 be16_to_cpu(hdr
->firstused
));
1779 ASSERT(be16_to_cpu(entry
->nameidx
) < XFS_LBSIZE(mp
));
1781 if (be16_to_cpu(entry
->nameidx
) < tmp
)
1782 tmp
= be16_to_cpu(entry
->nameidx
);
1784 hdr
->firstused
= cpu_to_be16(tmp
);
1785 if (!hdr
->firstused
) {
1786 hdr
->firstused
= cpu_to_be16(
1787 tmp
- XFS_ATTR_LEAF_NAME_ALIGN
);
1790 hdr
->holes
= 1; /* mark as needing compaction */
1792 xfs_da_log_buf(args
->trans
, bp
,
1793 XFS_DA_LOGRANGE(leaf
, hdr
, sizeof(*hdr
)));
1796 * Check if leaf is less than 50% full, caller may want to
1797 * "join" the leaf with a sibling if so.
1799 tmp
= sizeof(xfs_attr_leaf_hdr_t
);
1800 tmp
+= be16_to_cpu(leaf
->hdr
.count
) * sizeof(xfs_attr_leaf_entry_t
);
1801 tmp
+= be16_to_cpu(leaf
->hdr
.usedbytes
);
1802 return(tmp
< mp
->m_attr_magicpct
); /* leaf is < 37% full */
1806 * Move all the attribute list entries from drop_leaf into save_leaf.
1809 xfs_attr_leaf_unbalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
1810 xfs_da_state_blk_t
*save_blk
)
1812 xfs_attr_leafblock_t
*drop_leaf
, *save_leaf
, *tmp_leaf
;
1813 xfs_attr_leaf_hdr_t
*drop_hdr
, *save_hdr
, *tmp_hdr
;
1818 * Set up environment.
1821 ASSERT(drop_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1822 ASSERT(save_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1823 drop_leaf
= drop_blk
->bp
->data
;
1824 save_leaf
= save_blk
->bp
->data
;
1825 ASSERT(be16_to_cpu(drop_leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1826 ASSERT(be16_to_cpu(save_leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1827 drop_hdr
= &drop_leaf
->hdr
;
1828 save_hdr
= &save_leaf
->hdr
;
1831 * Save last hashval from dying block for later Btree fixup.
1833 drop_blk
->hashval
= be32_to_cpu(
1834 drop_leaf
->entries
[be16_to_cpu(drop_leaf
->hdr
.count
)-1].hashval
);
1837 * Check if we need a temp buffer, or can we do it in place.
1838 * Note that we don't check "leaf" for holes because we will
1839 * always be dropping it, toosmall() decided that for us already.
1841 if (save_hdr
->holes
== 0) {
1843 * dest leaf has no holes, so we add there. May need
1844 * to make some room in the entry array.
1846 if (xfs_attr_leaf_order(save_blk
->bp
, drop_blk
->bp
)) {
1847 xfs_attr_leaf_moveents(drop_leaf
, 0, save_leaf
, 0,
1848 be16_to_cpu(drop_hdr
->count
), mp
);
1850 xfs_attr_leaf_moveents(drop_leaf
, 0, save_leaf
,
1851 be16_to_cpu(save_hdr
->count
),
1852 be16_to_cpu(drop_hdr
->count
), mp
);
1856 * Destination has holes, so we make a temporary copy
1857 * of the leaf and add them both to that.
1859 tmpbuffer
= kmem_alloc(state
->blocksize
, KM_SLEEP
);
1860 ASSERT(tmpbuffer
!= NULL
);
1861 memset(tmpbuffer
, 0, state
->blocksize
);
1862 tmp_leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1863 tmp_hdr
= &tmp_leaf
->hdr
;
1864 tmp_hdr
->info
= save_hdr
->info
; /* struct copy */
1866 tmp_hdr
->firstused
= cpu_to_be16(state
->blocksize
);
1867 if (!tmp_hdr
->firstused
) {
1868 tmp_hdr
->firstused
= cpu_to_be16(
1869 state
->blocksize
- XFS_ATTR_LEAF_NAME_ALIGN
);
1871 tmp_hdr
->usedbytes
= 0;
1872 if (xfs_attr_leaf_order(save_blk
->bp
, drop_blk
->bp
)) {
1873 xfs_attr_leaf_moveents(drop_leaf
, 0, tmp_leaf
, 0,
1874 be16_to_cpu(drop_hdr
->count
), mp
);
1875 xfs_attr_leaf_moveents(save_leaf
, 0, tmp_leaf
,
1876 be16_to_cpu(tmp_leaf
->hdr
.count
),
1877 be16_to_cpu(save_hdr
->count
), mp
);
1879 xfs_attr_leaf_moveents(save_leaf
, 0, tmp_leaf
, 0,
1880 be16_to_cpu(save_hdr
->count
), mp
);
1881 xfs_attr_leaf_moveents(drop_leaf
, 0, tmp_leaf
,
1882 be16_to_cpu(tmp_leaf
->hdr
.count
),
1883 be16_to_cpu(drop_hdr
->count
), mp
);
1885 memcpy((char *)save_leaf
, (char *)tmp_leaf
, state
->blocksize
);
1886 kmem_free(tmpbuffer
, state
->blocksize
);
1889 xfs_da_log_buf(state
->args
->trans
, save_blk
->bp
, 0,
1890 state
->blocksize
- 1);
1893 * Copy out last hashval in each block for B-tree code.
1895 save_blk
->hashval
= be32_to_cpu(
1896 save_leaf
->entries
[be16_to_cpu(save_leaf
->hdr
.count
)-1].hashval
);
1899 /*========================================================================
1900 * Routines used for finding things in the Btree.
1901 *========================================================================*/
1904 * Look up a name in a leaf attribute list structure.
1905 * This is the internal routine, it uses the caller's buffer.
1907 * Note that duplicate keys are allowed, but only check within the
1908 * current leaf node. The Btree code must check in adjacent leaf nodes.
1910 * Return in args->index the index into the entry[] array of either
1911 * the found entry, or where the entry should have been (insert before
1914 * Don't change the args->value unless we find the attribute.
1917 xfs_attr_leaf_lookup_int(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
1919 xfs_attr_leafblock_t
*leaf
;
1920 xfs_attr_leaf_entry_t
*entry
;
1921 xfs_attr_leaf_name_local_t
*name_loc
;
1922 xfs_attr_leaf_name_remote_t
*name_rmt
;
1924 xfs_dahash_t hashval
;
1927 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
1928 ASSERT(be16_to_cpu(leaf
->hdr
.count
)
1929 < (XFS_LBSIZE(args
->dp
->i_mount
)/8));
1932 * Binary search. (note: small blocks will skip this loop)
1934 hashval
= args
->hashval
;
1935 probe
= span
= be16_to_cpu(leaf
->hdr
.count
) / 2;
1936 for (entry
= &leaf
->entries
[probe
]; span
> 4;
1937 entry
= &leaf
->entries
[probe
]) {
1939 if (be32_to_cpu(entry
->hashval
) < hashval
)
1941 else if (be32_to_cpu(entry
->hashval
) > hashval
)
1946 ASSERT((probe
>= 0) &&
1948 || (probe
< be16_to_cpu(leaf
->hdr
.count
))));
1949 ASSERT((span
<= 4) || (be32_to_cpu(entry
->hashval
) == hashval
));
1952 * Since we may have duplicate hashval's, find the first matching
1953 * hashval in the leaf.
1955 while ((probe
> 0) && (be32_to_cpu(entry
->hashval
) >= hashval
)) {
1959 while ((probe
< be16_to_cpu(leaf
->hdr
.count
)) &&
1960 (be32_to_cpu(entry
->hashval
) < hashval
)) {
1964 if ((probe
== be16_to_cpu(leaf
->hdr
.count
)) ||
1965 (be32_to_cpu(entry
->hashval
) != hashval
)) {
1966 args
->index
= probe
;
1967 return(XFS_ERROR(ENOATTR
));
1971 * Duplicate keys may be present, so search all of them for a match.
1973 for ( ; (probe
< be16_to_cpu(leaf
->hdr
.count
)) &&
1974 (be32_to_cpu(entry
->hashval
) == hashval
);
1977 * GROT: Add code to remove incomplete entries.
1980 * If we are looking for INCOMPLETE entries, show only those.
1981 * If we are looking for complete entries, show only those.
1983 if ((args
->flags
& XFS_ATTR_INCOMPLETE
) !=
1984 (entry
->flags
& XFS_ATTR_INCOMPLETE
)) {
1987 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1988 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, probe
);
1989 if (name_loc
->namelen
!= args
->namelen
)
1991 if (memcmp(args
->name
, (char *)name_loc
->nameval
, args
->namelen
) != 0)
1993 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
1995 args
->index
= probe
;
1996 return(XFS_ERROR(EEXIST
));
1998 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, probe
);
1999 if (name_rmt
->namelen
!= args
->namelen
)
2001 if (memcmp(args
->name
, (char *)name_rmt
->name
,
2002 args
->namelen
) != 0)
2004 if (!xfs_attr_namesp_match(args
->flags
, entry
->flags
))
2006 args
->index
= probe
;
2007 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2008 args
->rmtblkcnt
= XFS_B_TO_FSB(args
->dp
->i_mount
,
2009 be32_to_cpu(name_rmt
->valuelen
));
2010 return(XFS_ERROR(EEXIST
));
2013 args
->index
= probe
;
2014 return(XFS_ERROR(ENOATTR
));
2018 * Get the value associated with an attribute name from a leaf attribute
2022 xfs_attr_leaf_getvalue(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
2025 xfs_attr_leafblock_t
*leaf
;
2026 xfs_attr_leaf_entry_t
*entry
;
2027 xfs_attr_leaf_name_local_t
*name_loc
;
2028 xfs_attr_leaf_name_remote_t
*name_rmt
;
2031 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2032 ASSERT(be16_to_cpu(leaf
->hdr
.count
)
2033 < (XFS_LBSIZE(args
->dp
->i_mount
)/8));
2034 ASSERT(args
->index
< be16_to_cpu(leaf
->hdr
.count
));
2036 entry
= &leaf
->entries
[args
->index
];
2037 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2038 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, args
->index
);
2039 ASSERT(name_loc
->namelen
== args
->namelen
);
2040 ASSERT(memcmp(args
->name
, name_loc
->nameval
, args
->namelen
) == 0);
2041 valuelen
= be16_to_cpu(name_loc
->valuelen
);
2042 if (args
->flags
& ATTR_KERNOVAL
) {
2043 args
->valuelen
= valuelen
;
2046 if (args
->valuelen
< valuelen
) {
2047 args
->valuelen
= valuelen
;
2048 return(XFS_ERROR(ERANGE
));
2050 args
->valuelen
= valuelen
;
2051 memcpy(args
->value
, &name_loc
->nameval
[args
->namelen
], valuelen
);
2053 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
2054 ASSERT(name_rmt
->namelen
== args
->namelen
);
2055 ASSERT(memcmp(args
->name
, name_rmt
->name
, args
->namelen
) == 0);
2056 valuelen
= be32_to_cpu(name_rmt
->valuelen
);
2057 args
->rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2058 args
->rmtblkcnt
= XFS_B_TO_FSB(args
->dp
->i_mount
, valuelen
);
2059 if (args
->flags
& ATTR_KERNOVAL
) {
2060 args
->valuelen
= valuelen
;
2063 if (args
->valuelen
< valuelen
) {
2064 args
->valuelen
= valuelen
;
2065 return(XFS_ERROR(ERANGE
));
2067 args
->valuelen
= valuelen
;
2072 /*========================================================================
2074 *========================================================================*/
2077 * Move the indicated entries from one leaf to another.
2078 * NOTE: this routine modifies both source and destination leaves.
2082 xfs_attr_leaf_moveents(xfs_attr_leafblock_t
*leaf_s
, int start_s
,
2083 xfs_attr_leafblock_t
*leaf_d
, int start_d
,
2084 int count
, xfs_mount_t
*mp
)
2086 xfs_attr_leaf_hdr_t
*hdr_s
, *hdr_d
;
2087 xfs_attr_leaf_entry_t
*entry_s
, *entry_d
;
2091 * Check for nothing to do.
2097 * Set up environment.
2099 ASSERT(be16_to_cpu(leaf_s
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2100 ASSERT(be16_to_cpu(leaf_d
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2101 hdr_s
= &leaf_s
->hdr
;
2102 hdr_d
= &leaf_d
->hdr
;
2103 ASSERT((be16_to_cpu(hdr_s
->count
) > 0) &&
2104 (be16_to_cpu(hdr_s
->count
) < (XFS_LBSIZE(mp
)/8)));
2105 ASSERT(be16_to_cpu(hdr_s
->firstused
) >=
2106 ((be16_to_cpu(hdr_s
->count
)
2107 * sizeof(*entry_s
))+sizeof(*hdr_s
)));
2108 ASSERT(be16_to_cpu(hdr_d
->count
) < (XFS_LBSIZE(mp
)/8));
2109 ASSERT(be16_to_cpu(hdr_d
->firstused
) >=
2110 ((be16_to_cpu(hdr_d
->count
)
2111 * sizeof(*entry_d
))+sizeof(*hdr_d
)));
2113 ASSERT(start_s
< be16_to_cpu(hdr_s
->count
));
2114 ASSERT(start_d
<= be16_to_cpu(hdr_d
->count
));
2115 ASSERT(count
<= be16_to_cpu(hdr_s
->count
));
2118 * Move the entries in the destination leaf up to make a hole?
2120 if (start_d
< be16_to_cpu(hdr_d
->count
)) {
2121 tmp
= be16_to_cpu(hdr_d
->count
) - start_d
;
2122 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2123 entry_s
= &leaf_d
->entries
[start_d
];
2124 entry_d
= &leaf_d
->entries
[start_d
+ count
];
2125 memmove((char *)entry_d
, (char *)entry_s
, tmp
);
2129 * Copy all entry's in the same (sorted) order,
2130 * but allocate attribute info packed and in sequence.
2132 entry_s
= &leaf_s
->entries
[start_s
];
2133 entry_d
= &leaf_d
->entries
[start_d
];
2135 for (i
= 0; i
< count
; entry_s
++, entry_d
++, desti
++, i
++) {
2136 ASSERT(be16_to_cpu(entry_s
->nameidx
)
2137 >= be16_to_cpu(hdr_s
->firstused
));
2138 tmp
= xfs_attr_leaf_entsize(leaf_s
, start_s
+ i
);
2141 * Code to drop INCOMPLETE entries. Difficult to use as we
2142 * may also need to change the insertion index. Code turned
2143 * off for 6.2, should be revisited later.
2145 if (entry_s
->flags
& XFS_ATTR_INCOMPLETE
) { /* skip partials? */
2146 memset(XFS_ATTR_LEAF_NAME(leaf_s
, start_s
+ i
), 0, tmp
);
2147 be16_add(&hdr_s
->usedbytes
, -tmp
);
2148 be16_add(&hdr_s
->count
, -1);
2149 entry_d
--; /* to compensate for ++ in loop hdr */
2151 if ((start_s
+ i
) < offset
)
2152 result
++; /* insertion index adjustment */
2155 be16_add(&hdr_d
->firstused
, -tmp
);
2156 /* both on-disk, don't endian flip twice */
2157 entry_d
->hashval
= entry_s
->hashval
;
2158 /* both on-disk, don't endian flip twice */
2159 entry_d
->nameidx
= hdr_d
->firstused
;
2160 entry_d
->flags
= entry_s
->flags
;
2161 ASSERT(be16_to_cpu(entry_d
->nameidx
) + tmp
2163 memmove(XFS_ATTR_LEAF_NAME(leaf_d
, desti
),
2164 XFS_ATTR_LEAF_NAME(leaf_s
, start_s
+ i
), tmp
);
2165 ASSERT(be16_to_cpu(entry_s
->nameidx
) + tmp
2167 memset(XFS_ATTR_LEAF_NAME(leaf_s
, start_s
+ i
), 0, tmp
);
2168 be16_add(&hdr_s
->usedbytes
, -tmp
);
2169 be16_add(&hdr_d
->usedbytes
, tmp
);
2170 be16_add(&hdr_s
->count
, -1);
2171 be16_add(&hdr_d
->count
, 1);
2172 tmp
= be16_to_cpu(hdr_d
->count
)
2173 * sizeof(xfs_attr_leaf_entry_t
)
2174 + sizeof(xfs_attr_leaf_hdr_t
);
2175 ASSERT(be16_to_cpu(hdr_d
->firstused
) >= tmp
);
2182 * Zero out the entries we just copied.
2184 if (start_s
== be16_to_cpu(hdr_s
->count
)) {
2185 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2186 entry_s
= &leaf_s
->entries
[start_s
];
2187 ASSERT(((char *)entry_s
+ tmp
) <=
2188 ((char *)leaf_s
+ XFS_LBSIZE(mp
)));
2189 memset((char *)entry_s
, 0, tmp
);
2192 * Move the remaining entries down to fill the hole,
2193 * then zero the entries at the top.
2195 tmp
= be16_to_cpu(hdr_s
->count
) - count
;
2196 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2197 entry_s
= &leaf_s
->entries
[start_s
+ count
];
2198 entry_d
= &leaf_s
->entries
[start_s
];
2199 memmove((char *)entry_d
, (char *)entry_s
, tmp
);
2201 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2202 entry_s
= &leaf_s
->entries
[be16_to_cpu(hdr_s
->count
)];
2203 ASSERT(((char *)entry_s
+ tmp
) <=
2204 ((char *)leaf_s
+ XFS_LBSIZE(mp
)));
2205 memset((char *)entry_s
, 0, tmp
);
2209 * Fill in the freemap information
2211 hdr_d
->freemap
[0].base
= cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t
));
2212 be16_add(&hdr_d
->freemap
[0].base
, be16_to_cpu(hdr_d
->count
) *
2213 sizeof(xfs_attr_leaf_entry_t
));
2214 hdr_d
->freemap
[0].size
= cpu_to_be16(be16_to_cpu(hdr_d
->firstused
)
2215 - be16_to_cpu(hdr_d
->freemap
[0].base
));
2216 hdr_d
->freemap
[1].base
= 0;
2217 hdr_d
->freemap
[2].base
= 0;
2218 hdr_d
->freemap
[1].size
= 0;
2219 hdr_d
->freemap
[2].size
= 0;
2220 hdr_s
->holes
= 1; /* leaf may not be compact */
2224 * Compare two leaf blocks "order".
2225 * Return 0 unless leaf2 should go before leaf1.
2228 xfs_attr_leaf_order(xfs_dabuf_t
*leaf1_bp
, xfs_dabuf_t
*leaf2_bp
)
2230 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
2232 leaf1
= leaf1_bp
->data
;
2233 leaf2
= leaf2_bp
->data
;
2234 ASSERT((be16_to_cpu(leaf1
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
) &&
2235 (be16_to_cpu(leaf2
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
));
2236 if ((be16_to_cpu(leaf1
->hdr
.count
) > 0) &&
2237 (be16_to_cpu(leaf2
->hdr
.count
) > 0) &&
2238 ((be32_to_cpu(leaf2
->entries
[0].hashval
) <
2239 be32_to_cpu(leaf1
->entries
[0].hashval
)) ||
2240 (be32_to_cpu(leaf2
->entries
[
2241 be16_to_cpu(leaf2
->hdr
.count
)-1].hashval
) <
2242 be32_to_cpu(leaf1
->entries
[
2243 be16_to_cpu(leaf1
->hdr
.count
)-1].hashval
)))) {
2250 * Pick up the last hashvalue from a leaf block.
2253 xfs_attr_leaf_lasthash(xfs_dabuf_t
*bp
, int *count
)
2255 xfs_attr_leafblock_t
*leaf
;
2258 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2260 *count
= be16_to_cpu(leaf
->hdr
.count
);
2261 if (!leaf
->hdr
.count
)
2263 return be32_to_cpu(leaf
->entries
[be16_to_cpu(leaf
->hdr
.count
)-1].hashval
);
2267 * Calculate the number of bytes used to store the indicated attribute
2268 * (whether local or remote only calculate bytes in this block).
2271 xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
)
2273 xfs_attr_leaf_name_local_t
*name_loc
;
2274 xfs_attr_leaf_name_remote_t
*name_rmt
;
2277 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2278 if (leaf
->entries
[index
].flags
& XFS_ATTR_LOCAL
) {
2279 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, index
);
2280 size
= XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc
->namelen
,
2281 be16_to_cpu(name_loc
->valuelen
));
2283 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, index
);
2284 size
= XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt
->namelen
);
2290 * Calculate the number of bytes that would be required to store the new
2291 * attribute (whether local or remote only calculate bytes in this block).
2292 * This routine decides as a side effect whether the attribute will be
2293 * a "local" or a "remote" attribute.
2296 xfs_attr_leaf_newentsize(int namelen
, int valuelen
, int blocksize
, int *local
)
2300 size
= XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen
, valuelen
);
2301 if (size
< XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize
)) {
2306 size
= XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen
);
2315 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2318 xfs_attr_leaf_list_int(xfs_dabuf_t
*bp
, xfs_attr_list_context_t
*context
)
2320 attrlist_cursor_kern_t
*cursor
;
2321 xfs_attr_leafblock_t
*leaf
;
2322 xfs_attr_leaf_entry_t
*entry
;
2327 cursor
= context
->cursor
;
2328 cursor
->initted
= 1;
2330 xfs_attr_trace_l_cl("blk start", context
, leaf
);
2333 * Re-find our place in the leaf block if this is a new syscall.
2335 if (context
->resynch
) {
2336 entry
= &leaf
->entries
[0];
2337 for (i
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); entry
++, i
++) {
2338 if (be32_to_cpu(entry
->hashval
) == cursor
->hashval
) {
2339 if (cursor
->offset
== context
->dupcnt
) {
2340 context
->dupcnt
= 0;
2344 } else if (be32_to_cpu(entry
->hashval
) >
2346 context
->dupcnt
= 0;
2350 if (i
== be16_to_cpu(leaf
->hdr
.count
)) {
2351 xfs_attr_trace_l_c("not found", context
);
2355 entry
= &leaf
->entries
[0];
2358 context
->resynch
= 0;
2361 * We have found our place, start copying out the new attributes.
2364 for ( ; (i
< be16_to_cpu(leaf
->hdr
.count
)); entry
++, i
++) {
2365 attrnames_t
*namesp
;
2367 if (be32_to_cpu(entry
->hashval
) != cursor
->hashval
) {
2368 cursor
->hashval
= be32_to_cpu(entry
->hashval
);
2372 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
2373 continue; /* skip incomplete entries */
2374 if (!xfs_attr_namesp_match_overrides(context
->flags
, entry
->flags
))
2377 namesp
= xfs_attr_flags_namesp(entry
->flags
);
2379 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2380 xfs_attr_leaf_name_local_t
*name_loc
=
2381 XFS_ATTR_LEAF_NAME_LOCAL(leaf
, i
);
2383 retval
= context
->put_listent(context
,
2385 (char *)name_loc
->nameval
,
2386 (int)name_loc
->namelen
,
2387 be16_to_cpu(name_loc
->valuelen
),
2388 (char *)&name_loc
->nameval
[name_loc
->namelen
]);
2392 xfs_attr_leaf_name_remote_t
*name_rmt
=
2393 XFS_ATTR_LEAF_NAME_REMOTE(leaf
, i
);
2395 int valuelen
= be32_to_cpu(name_rmt
->valuelen
);
2397 if (context
->put_value
) {
2400 memset((char *)&args
, 0, sizeof(args
));
2401 args
.dp
= context
->dp
;
2402 args
.whichfork
= XFS_ATTR_FORK
;
2403 args
.valuelen
= valuelen
;
2404 args
.value
= kmem_alloc(valuelen
, KM_SLEEP
);
2405 args
.rmtblkno
= be32_to_cpu(name_rmt
->valueblk
);
2406 args
.rmtblkcnt
= XFS_B_TO_FSB(args
.dp
->i_mount
, valuelen
);
2407 retval
= xfs_attr_rmtval_get(&args
);
2410 retval
= context
->put_listent(context
,
2412 (char *)name_rmt
->name
,
2413 (int)name_rmt
->namelen
,
2416 kmem_free(args
.value
, valuelen
);
2419 retval
= context
->put_listent(context
,
2421 (char *)name_rmt
->name
,
2422 (int)name_rmt
->namelen
,
2429 if (context
->seen_enough
)
2433 xfs_attr_trace_l_cl("blk end", context
, leaf
);
2438 /*========================================================================
2439 * Manage the INCOMPLETE flag in a leaf entry
2440 *========================================================================*/
2443 * Clear the INCOMPLETE flag on an entry in a leaf block.
2446 xfs_attr_leaf_clearflag(xfs_da_args_t
*args
)
2448 xfs_attr_leafblock_t
*leaf
;
2449 xfs_attr_leaf_entry_t
*entry
;
2450 xfs_attr_leaf_name_remote_t
*name_rmt
;
2454 xfs_attr_leaf_name_local_t
*name_loc
;
2460 * Set up the operation.
2462 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
2470 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2471 ASSERT(args
->index
< be16_to_cpu(leaf
->hdr
.count
));
2472 ASSERT(args
->index
>= 0);
2473 entry
= &leaf
->entries
[ args
->index
];
2474 ASSERT(entry
->flags
& XFS_ATTR_INCOMPLETE
);
2477 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2478 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, args
->index
);
2479 namelen
= name_loc
->namelen
;
2480 name
= (char *)name_loc
->nameval
;
2482 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
2483 namelen
= name_rmt
->namelen
;
2484 name
= (char *)name_rmt
->name
;
2486 ASSERT(be32_to_cpu(entry
->hashval
) == args
->hashval
);
2487 ASSERT(namelen
== args
->namelen
);
2488 ASSERT(memcmp(name
, args
->name
, namelen
) == 0);
2491 entry
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2492 xfs_da_log_buf(args
->trans
, bp
,
2493 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2495 if (args
->rmtblkno
) {
2496 ASSERT((entry
->flags
& XFS_ATTR_LOCAL
) == 0);
2497 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
2498 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2499 name_rmt
->valuelen
= cpu_to_be32(args
->valuelen
);
2500 xfs_da_log_buf(args
->trans
, bp
,
2501 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2503 xfs_da_buf_done(bp
);
2506 * Commit the flag value change and start the next trans in series.
2508 error
= xfs_attr_rolltrans(&args
->trans
, args
->dp
);
2514 * Set the INCOMPLETE flag on an entry in a leaf block.
2517 xfs_attr_leaf_setflag(xfs_da_args_t
*args
)
2519 xfs_attr_leafblock_t
*leaf
;
2520 xfs_attr_leaf_entry_t
*entry
;
2521 xfs_attr_leaf_name_remote_t
*name_rmt
;
2526 * Set up the operation.
2528 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
2536 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2537 ASSERT(args
->index
< be16_to_cpu(leaf
->hdr
.count
));
2538 ASSERT(args
->index
>= 0);
2539 entry
= &leaf
->entries
[ args
->index
];
2541 ASSERT((entry
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2542 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
2543 xfs_da_log_buf(args
->trans
, bp
,
2544 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2545 if ((entry
->flags
& XFS_ATTR_LOCAL
) == 0) {
2546 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
2547 name_rmt
->valueblk
= 0;
2548 name_rmt
->valuelen
= 0;
2549 xfs_da_log_buf(args
->trans
, bp
,
2550 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2552 xfs_da_buf_done(bp
);
2555 * Commit the flag value change and start the next trans in series.
2557 error
= xfs_attr_rolltrans(&args
->trans
, args
->dp
);
2563 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2564 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2565 * entry given by args->blkno2/index2.
2567 * Note that they could be in different blocks, or in the same block.
2570 xfs_attr_leaf_flipflags(xfs_da_args_t
*args
)
2572 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
2573 xfs_attr_leaf_entry_t
*entry1
, *entry2
;
2574 xfs_attr_leaf_name_remote_t
*name_rmt
;
2575 xfs_dabuf_t
*bp1
, *bp2
;
2578 xfs_attr_leaf_name_local_t
*name_loc
;
2579 int namelen1
, namelen2
;
2580 char *name1
, *name2
;
2584 * Read the block containing the "old" attr
2586 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp1
,
2591 ASSERT(bp1
!= NULL
);
2594 * Read the block containing the "new" attr, if it is different
2596 if (args
->blkno2
!= args
->blkno
) {
2597 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno2
,
2598 -1, &bp2
, XFS_ATTR_FORK
);
2602 ASSERT(bp2
!= NULL
);
2608 ASSERT(be16_to_cpu(leaf1
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2609 ASSERT(args
->index
< be16_to_cpu(leaf1
->hdr
.count
));
2610 ASSERT(args
->index
>= 0);
2611 entry1
= &leaf1
->entries
[ args
->index
];
2614 ASSERT(be16_to_cpu(leaf2
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2615 ASSERT(args
->index2
< be16_to_cpu(leaf2
->hdr
.count
));
2616 ASSERT(args
->index2
>= 0);
2617 entry2
= &leaf2
->entries
[ args
->index2
];
2620 if (entry1
->flags
& XFS_ATTR_LOCAL
) {
2621 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf1
, args
->index
);
2622 namelen1
= name_loc
->namelen
;
2623 name1
= (char *)name_loc
->nameval
;
2625 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf1
, args
->index
);
2626 namelen1
= name_rmt
->namelen
;
2627 name1
= (char *)name_rmt
->name
;
2629 if (entry2
->flags
& XFS_ATTR_LOCAL
) {
2630 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf2
, args
->index2
);
2631 namelen2
= name_loc
->namelen
;
2632 name2
= (char *)name_loc
->nameval
;
2634 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf2
, args
->index2
);
2635 namelen2
= name_rmt
->namelen
;
2636 name2
= (char *)name_rmt
->name
;
2638 ASSERT(be32_to_cpu(entry1
->hashval
) == be32_to_cpu(entry2
->hashval
));
2639 ASSERT(namelen1
== namelen2
);
2640 ASSERT(memcmp(name1
, name2
, namelen1
) == 0);
2643 ASSERT(entry1
->flags
& XFS_ATTR_INCOMPLETE
);
2644 ASSERT((entry2
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2646 entry1
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2647 xfs_da_log_buf(args
->trans
, bp1
,
2648 XFS_DA_LOGRANGE(leaf1
, entry1
, sizeof(*entry1
)));
2649 if (args
->rmtblkno
) {
2650 ASSERT((entry1
->flags
& XFS_ATTR_LOCAL
) == 0);
2651 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf1
, args
->index
);
2652 name_rmt
->valueblk
= cpu_to_be32(args
->rmtblkno
);
2653 name_rmt
->valuelen
= cpu_to_be32(args
->valuelen
);
2654 xfs_da_log_buf(args
->trans
, bp1
,
2655 XFS_DA_LOGRANGE(leaf1
, name_rmt
, sizeof(*name_rmt
)));
2658 entry2
->flags
|= XFS_ATTR_INCOMPLETE
;
2659 xfs_da_log_buf(args
->trans
, bp2
,
2660 XFS_DA_LOGRANGE(leaf2
, entry2
, sizeof(*entry2
)));
2661 if ((entry2
->flags
& XFS_ATTR_LOCAL
) == 0) {
2662 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf2
, args
->index2
);
2663 name_rmt
->valueblk
= 0;
2664 name_rmt
->valuelen
= 0;
2665 xfs_da_log_buf(args
->trans
, bp2
,
2666 XFS_DA_LOGRANGE(leaf2
, name_rmt
, sizeof(*name_rmt
)));
2668 xfs_da_buf_done(bp1
);
2670 xfs_da_buf_done(bp2
);
2673 * Commit the flag value change and start the next trans in series.
2675 error
= xfs_attr_rolltrans(&args
->trans
, args
->dp
);
2680 /*========================================================================
2681 * Indiscriminately delete the entire attribute fork
2682 *========================================================================*/
2685 * Recurse (gasp!) through the attribute nodes until we find leaves.
2686 * We're doing a depth-first traversal in order to invalidate everything.
2689 xfs_attr_root_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
)
2691 xfs_da_blkinfo_t
*info
;
2697 * Read block 0 to see what we have to work with.
2698 * We only get here if we have extents, since we remove
2699 * the extents in reverse order the extent containing
2700 * block 0 must still be there.
2702 error
= xfs_da_read_buf(*trans
, dp
, 0, -1, &bp
, XFS_ATTR_FORK
);
2705 blkno
= xfs_da_blkno(bp
);
2708 * Invalidate the tree, even if the "tree" is only a single leaf block.
2709 * This is a depth-first traversal!
2712 if (be16_to_cpu(info
->magic
) == XFS_DA_NODE_MAGIC
) {
2713 error
= xfs_attr_node_inactive(trans
, dp
, bp
, 1);
2714 } else if (be16_to_cpu(info
->magic
) == XFS_ATTR_LEAF_MAGIC
) {
2715 error
= xfs_attr_leaf_inactive(trans
, dp
, bp
);
2717 error
= XFS_ERROR(EIO
);
2718 xfs_da_brelse(*trans
, bp
);
2724 * Invalidate the incore copy of the root block.
2726 error
= xfs_da_get_buf(*trans
, dp
, 0, blkno
, &bp
, XFS_ATTR_FORK
);
2729 xfs_da_binval(*trans
, bp
); /* remove from cache */
2731 * Commit the invalidate and start the next transaction.
2733 error
= xfs_attr_rolltrans(trans
, dp
);
2739 * Recurse (gasp!) through the attribute nodes until we find leaves.
2740 * We're doing a depth-first traversal in order to invalidate everything.
2743 xfs_attr_node_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
, xfs_dabuf_t
*bp
,
2746 xfs_da_blkinfo_t
*info
;
2747 xfs_da_intnode_t
*node
;
2748 xfs_dablk_t child_fsb
;
2749 xfs_daddr_t parent_blkno
, child_blkno
;
2750 int error
, count
, i
;
2751 xfs_dabuf_t
*child_bp
;
2754 * Since this code is recursive (gasp!) we must protect ourselves.
2756 if (level
> XFS_DA_NODE_MAXDEPTH
) {
2757 xfs_da_brelse(*trans
, bp
); /* no locks for later trans */
2758 return(XFS_ERROR(EIO
));
2762 ASSERT(be16_to_cpu(node
->hdr
.info
.magic
) == XFS_DA_NODE_MAGIC
);
2763 parent_blkno
= xfs_da_blkno(bp
); /* save for re-read later */
2764 count
= be16_to_cpu(node
->hdr
.count
);
2766 xfs_da_brelse(*trans
, bp
);
2769 child_fsb
= be32_to_cpu(node
->btree
[0].before
);
2770 xfs_da_brelse(*trans
, bp
); /* no locks for later trans */
2773 * If this is the node level just above the leaves, simply loop
2774 * over the leaves removing all of them. If this is higher up
2775 * in the tree, recurse downward.
2777 for (i
= 0; i
< count
; i
++) {
2779 * Read the subsidiary block to see what we have to work with.
2780 * Don't do this in a transaction. This is a depth-first
2781 * traversal of the tree so we may deal with many blocks
2782 * before we come back to this one.
2784 error
= xfs_da_read_buf(*trans
, dp
, child_fsb
, -2, &child_bp
,
2789 /* save for re-read later */
2790 child_blkno
= xfs_da_blkno(child_bp
);
2793 * Invalidate the subtree, however we have to.
2795 info
= child_bp
->data
;
2796 if (be16_to_cpu(info
->magic
) == XFS_DA_NODE_MAGIC
) {
2797 error
= xfs_attr_node_inactive(trans
, dp
,
2799 } else if (be16_to_cpu(info
->magic
) == XFS_ATTR_LEAF_MAGIC
) {
2800 error
= xfs_attr_leaf_inactive(trans
, dp
,
2803 error
= XFS_ERROR(EIO
);
2804 xfs_da_brelse(*trans
, child_bp
);
2810 * Remove the subsidiary block from the cache
2813 error
= xfs_da_get_buf(*trans
, dp
, 0, child_blkno
,
2814 &child_bp
, XFS_ATTR_FORK
);
2817 xfs_da_binval(*trans
, child_bp
);
2821 * If we're not done, re-read the parent to get the next
2822 * child block number.
2824 if ((i
+1) < count
) {
2825 error
= xfs_da_read_buf(*trans
, dp
, 0, parent_blkno
,
2826 &bp
, XFS_ATTR_FORK
);
2829 child_fsb
= be32_to_cpu(node
->btree
[i
+1].before
);
2830 xfs_da_brelse(*trans
, bp
);
2833 * Atomically commit the whole invalidate stuff.
2835 if ((error
= xfs_attr_rolltrans(trans
, dp
)))
2843 * Invalidate all of the "remote" value regions pointed to by a particular
2845 * Note that we must release the lock on the buffer so that we are not
2846 * caught holding something that the logging code wants to flush to disk.
2849 xfs_attr_leaf_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
, xfs_dabuf_t
*bp
)
2851 xfs_attr_leafblock_t
*leaf
;
2852 xfs_attr_leaf_entry_t
*entry
;
2853 xfs_attr_leaf_name_remote_t
*name_rmt
;
2854 xfs_attr_inactive_list_t
*list
, *lp
;
2855 int error
, count
, size
, tmp
, i
;
2858 ASSERT(be16_to_cpu(leaf
->hdr
.info
.magic
) == XFS_ATTR_LEAF_MAGIC
);
2861 * Count the number of "remote" value extents.
2864 entry
= &leaf
->entries
[0];
2865 for (i
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); entry
++, i
++) {
2866 if (be16_to_cpu(entry
->nameidx
) &&
2867 ((entry
->flags
& XFS_ATTR_LOCAL
) == 0)) {
2868 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, i
);
2869 if (name_rmt
->valueblk
)
2875 * If there are no "remote" values, we're done.
2878 xfs_da_brelse(*trans
, bp
);
2883 * Allocate storage for a list of all the "remote" value extents.
2885 size
= count
* sizeof(xfs_attr_inactive_list_t
);
2886 list
= (xfs_attr_inactive_list_t
*)kmem_alloc(size
, KM_SLEEP
);
2889 * Identify each of the "remote" value extents.
2892 entry
= &leaf
->entries
[0];
2893 for (i
= 0; i
< be16_to_cpu(leaf
->hdr
.count
); entry
++, i
++) {
2894 if (be16_to_cpu(entry
->nameidx
) &&
2895 ((entry
->flags
& XFS_ATTR_LOCAL
) == 0)) {
2896 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, i
);
2897 if (name_rmt
->valueblk
) {
2898 lp
->valueblk
= be32_to_cpu(name_rmt
->valueblk
);
2899 lp
->valuelen
= XFS_B_TO_FSB(dp
->i_mount
,
2900 be32_to_cpu(name_rmt
->valuelen
));
2905 xfs_da_brelse(*trans
, bp
); /* unlock for trans. in freextent() */
2908 * Invalidate each of the "remote" value extents.
2911 for (lp
= list
, i
= 0; i
< count
; i
++, lp
++) {
2912 tmp
= xfs_attr_leaf_freextent(trans
, dp
,
2913 lp
->valueblk
, lp
->valuelen
);
2916 error
= tmp
; /* save only the 1st errno */
2919 kmem_free((xfs_caddr_t
)list
, size
);
2924 * Look at all the extents for this logical region,
2925 * invalidate any buffers that are incore/in transactions.
2928 xfs_attr_leaf_freextent(xfs_trans_t
**trans
, xfs_inode_t
*dp
,
2929 xfs_dablk_t blkno
, int blkcnt
)
2931 xfs_bmbt_irec_t map
;
2933 int tblkcnt
, dblkcnt
, nmap
, error
;
2938 * Roll through the "value", invalidating the attribute value's
2943 while (tblkcnt
> 0) {
2945 * Try to remember where we decided to put the value.
2948 error
= xfs_bmapi(*trans
, dp
, (xfs_fileoff_t
)tblkno
, tblkcnt
,
2949 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2950 NULL
, 0, &map
, &nmap
, NULL
, NULL
);
2955 ASSERT(map
.br_startblock
!= DELAYSTARTBLOCK
);
2958 * If it's a hole, these are already unmapped
2959 * so there's nothing to invalidate.
2961 if (map
.br_startblock
!= HOLESTARTBLOCK
) {
2963 dblkno
= XFS_FSB_TO_DADDR(dp
->i_mount
,
2965 dblkcnt
= XFS_FSB_TO_BB(dp
->i_mount
,
2967 bp
= xfs_trans_get_buf(*trans
,
2968 dp
->i_mount
->m_ddev_targp
,
2969 dblkno
, dblkcnt
, XFS_BUF_LOCK
);
2970 xfs_trans_binval(*trans
, bp
);
2972 * Roll to next transaction.
2974 if ((error
= xfs_attr_rolltrans(trans
, dp
)))
2978 tblkno
+= map
.br_blockcount
;
2979 tblkcnt
-= map
.br_blockcount
;
2987 * Roll from one trans in the sequence of PERMANENT transactions to the next.
2990 xfs_attr_rolltrans(xfs_trans_t
**transp
, xfs_inode_t
*dp
)
2993 unsigned int logres
, count
;
2997 * Ensure that the inode is always logged.
3000 xfs_trans_log_inode(trans
, dp
, XFS_ILOG_CORE
);
3003 * Copy the critical parameters from one trans to the next.
3005 logres
= trans
->t_log_res
;
3006 count
= trans
->t_log_count
;
3007 *transp
= xfs_trans_dup(trans
);
3010 * Commit the current transaction.
3011 * If this commit failed, then it'd just unlock those items that
3012 * are not marked ihold. That also means that a filesystem shutdown
3013 * is in progress. The caller takes the responsibility to cancel
3014 * the duplicate transaction that gets returned.
3016 if ((error
= xfs_trans_commit(trans
, 0, NULL
)))
3022 * Reserve space in the log for th next transaction.
3023 * This also pushes items in the "AIL", the list of logged items,
3024 * out to disk if they are taking up space at the tail of the log
3025 * that we want to use. This requires that either nothing be locked
3026 * across this call, or that anything that is locked be logged in
3027 * the prior and the next transactions.
3029 error
= xfs_trans_reserve(trans
, 0, logres
, 0,
3030 XFS_TRANS_PERM_LOG_RES
, count
);
3032 * Ensure that the inode is in the new transaction and locked.
3035 xfs_trans_ijoin(trans
, dp
, XFS_ILOCK_EXCL
);
3036 xfs_trans_ihold(trans
, dp
);