2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_da_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_alloc.h"
50 #include "xfs_btree.h"
51 #include "xfs_dir_sf.h"
52 #include "xfs_dir2_sf.h"
53 #include "xfs_attr_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_inode_item.h"
59 #include "xfs_attr_leaf.h"
60 #include "xfs_error.h"
65 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
68 /*========================================================================
69 * Function prototypes for the kernel.
70 *========================================================================*/
73 * Routines used for growing the Btree.
75 STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
77 STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
79 STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
80 STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
81 xfs_da_state_blk_t *blk1,
82 xfs_da_state_blk_t *blk2);
83 STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
84 xfs_da_state_blk_t *leaf_blk_1,
85 xfs_da_state_blk_t *leaf_blk_2,
86 int *number_entries_in_blk1,
87 int *number_usedbytes_in_blk1);
90 * Routines used for shrinking the Btree.
92 STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
93 xfs_dabuf_t *bp, int level);
94 STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
96 STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
97 xfs_dablk_t blkno, int blkcnt);
102 STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
104 xfs_attr_leafblock_t *dst_leaf,
105 int dst_start, int move_count,
107 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
108 STATIC int xfs_attr_put_listent(xfs_attr_list_context_t *context,
109 attrnames_t *, char *name, int namelen,
113 /*========================================================================
114 * External routines when attribute fork size < XFS_LITINO(mp).
115 *========================================================================*/
118 * Query whether the requested number of additional bytes of extended
119 * attribute space will be able to fit inline.
120 * Returns zero if not, else the di_forkoff fork offset to be used in the
121 * literal area for attribute data once the new bytes have been added.
123 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
124 * special case for dev/uuid inodes, they have fixed size data forks.
127 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
130 int minforkoff; /* lower limit on valid forkoff locations */
131 int maxforkoff; /* upper limit on valid forkoff locations */
132 xfs_mount_t *mp = dp->i_mount;
134 if (unlikely(mp->m_flags & XFS_MOUNT_COMPAT_ATTR)) {
135 if (bytes <= XFS_IFORK_ASIZE(dp))
136 return mp->m_attroffset >> 3;
140 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
142 switch (dp->i_d.di_format) {
143 case XFS_DINODE_FMT_DEV:
144 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
145 return (offset >= minforkoff) ? minforkoff : 0;
146 case XFS_DINODE_FMT_UUID:
147 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
148 return (offset >= minforkoff) ? minforkoff : 0;
151 /* data fork btree root can have at least this many key/ptr pairs */
152 minforkoff = MAX(dp->i_df.if_bytes, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
153 minforkoff = roundup(minforkoff, 8) >> 3;
155 /* attr fork btree root can have at least this many key/ptr pairs */
156 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
157 maxforkoff = maxforkoff >> 3; /* rounded down */
159 if (offset >= minforkoff && offset < maxforkoff)
161 if (offset >= maxforkoff)
167 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
170 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
174 if (!(mp->m_flags & XFS_MOUNT_COMPAT_ATTR) &&
175 !(XFS_SB_VERSION_HASATTR2(&mp->m_sb))) {
177 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb)) {
178 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
179 XFS_SB_UNLOCK(mp, s);
180 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
182 XFS_SB_UNLOCK(mp, s);
187 * Create the initial contents of a shortform attribute list.
190 xfs_attr_shortform_create(xfs_da_args_t *args)
192 xfs_attr_sf_hdr_t *hdr;
200 ASSERT(ifp->if_bytes == 0);
201 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
202 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
203 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
204 ifp->if_flags |= XFS_IFINLINE;
206 ASSERT(ifp->if_flags & XFS_IFINLINE);
208 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
209 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
211 INT_SET(hdr->totsize, ARCH_CONVERT, sizeof(*hdr));
212 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
216 * Add a name/value pair to the shortform attribute list.
217 * Overflow from the inode has already been checked for.
220 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
222 xfs_attr_shortform_t *sf;
223 xfs_attr_sf_entry_t *sfe;
231 dp->i_d.di_forkoff = forkoff;
232 dp->i_df.if_ext_max =
233 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
234 dp->i_afp->if_ext_max =
235 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
238 ASSERT(ifp->if_flags & XFS_IFINLINE);
239 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
241 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
242 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
244 if (sfe->namelen != args->namelen)
246 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
248 if (((args->flags & ATTR_SECURE) != 0) !=
249 ((sfe->flags & XFS_ATTR_SECURE) != 0))
251 if (((args->flags & ATTR_ROOT) != 0) !=
252 ((sfe->flags & XFS_ATTR_ROOT) != 0))
258 offset = (char *)sfe - (char *)sf;
259 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
260 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
261 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
262 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
264 sfe->namelen = args->namelen;
265 INT_SET(sfe->valuelen, ARCH_CONVERT, args->valuelen);
266 sfe->flags = (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
267 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
268 memcpy(sfe->nameval, args->name, args->namelen);
269 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
270 INT_MOD(sf->hdr.count, ARCH_CONVERT, 1);
271 INT_MOD(sf->hdr.totsize, ARCH_CONVERT, size);
272 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
274 xfs_sbversion_add_attr2(mp, args->trans);
278 * Remove an attribute from the shortform attribute list structure.
281 xfs_attr_shortform_remove(xfs_da_args_t *args)
283 xfs_attr_shortform_t *sf;
284 xfs_attr_sf_entry_t *sfe;
285 int base, size=0, end, totsize, i;
291 base = sizeof(xfs_attr_sf_hdr_t);
292 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
294 end = INT_GET(sf->hdr.count, ARCH_CONVERT);
295 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
297 size = XFS_ATTR_SF_ENTSIZE(sfe);
298 if (sfe->namelen != args->namelen)
300 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
302 if (((args->flags & ATTR_SECURE) != 0) !=
303 ((sfe->flags & XFS_ATTR_SECURE) != 0))
305 if (((args->flags & ATTR_ROOT) != 0) !=
306 ((sfe->flags & XFS_ATTR_ROOT) != 0))
311 return(XFS_ERROR(ENOATTR));
314 * Fix up the attribute fork data, covering the hole
317 totsize = INT_GET(sf->hdr.totsize, ARCH_CONVERT);
319 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
320 INT_MOD(sf->hdr.count, ARCH_CONVERT, -1);
321 INT_MOD(sf->hdr.totsize, ARCH_CONVERT, -size);
324 * Fix up the start offset of the attribute fork
327 if (totsize == sizeof(xfs_attr_sf_hdr_t) && !args->addname) {
329 * Last attribute now removed, revert to original
330 * inode format making all literal area available
331 * to the data fork once more.
333 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
334 dp->i_d.di_forkoff = 0;
335 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
336 ASSERT(dp->i_d.di_anextents == 0);
337 ASSERT(dp->i_afp == NULL);
338 dp->i_df.if_ext_max =
339 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
340 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
342 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
343 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
344 ASSERT(dp->i_d.di_forkoff);
345 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || args->addname);
346 dp->i_afp->if_ext_max =
347 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
348 dp->i_df.if_ext_max =
349 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
350 xfs_trans_log_inode(args->trans, dp,
351 XFS_ILOG_CORE | XFS_ILOG_ADATA);
354 xfs_sbversion_add_attr2(mp, args->trans);
360 * Look up a name in a shortform attribute list structure.
364 xfs_attr_shortform_lookup(xfs_da_args_t *args)
366 xfs_attr_shortform_t *sf;
367 xfs_attr_sf_entry_t *sfe;
371 ifp = args->dp->i_afp;
372 ASSERT(ifp->if_flags & XFS_IFINLINE);
373 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
375 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
376 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
377 if (sfe->namelen != args->namelen)
379 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
381 if (((args->flags & ATTR_SECURE) != 0) !=
382 ((sfe->flags & XFS_ATTR_SECURE) != 0))
384 if (((args->flags & ATTR_ROOT) != 0) !=
385 ((sfe->flags & XFS_ATTR_ROOT) != 0))
387 return(XFS_ERROR(EEXIST));
389 return(XFS_ERROR(ENOATTR));
393 * Look up a name in a shortform attribute list structure.
397 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
399 xfs_attr_shortform_t *sf;
400 xfs_attr_sf_entry_t *sfe;
403 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
404 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
406 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
407 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
408 if (sfe->namelen != args->namelen)
410 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
412 if (((args->flags & ATTR_SECURE) != 0) !=
413 ((sfe->flags & XFS_ATTR_SECURE) != 0))
415 if (((args->flags & ATTR_ROOT) != 0) !=
416 ((sfe->flags & XFS_ATTR_ROOT) != 0))
418 if (args->flags & ATTR_KERNOVAL) {
419 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
420 return(XFS_ERROR(EEXIST));
422 if (args->valuelen < INT_GET(sfe->valuelen, ARCH_CONVERT)) {
423 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
424 return(XFS_ERROR(ERANGE));
426 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
427 memcpy(args->value, &sfe->nameval[args->namelen],
429 return(XFS_ERROR(EEXIST));
431 return(XFS_ERROR(ENOATTR));
435 * Convert from using the shortform to the leaf.
438 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
441 xfs_attr_shortform_t *sf;
442 xfs_attr_sf_entry_t *sfe;
452 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
453 size = INT_GET(sf->hdr.totsize, ARCH_CONVERT);
454 tmpbuffer = kmem_alloc(size, KM_SLEEP);
455 ASSERT(tmpbuffer != NULL);
456 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
457 sf = (xfs_attr_shortform_t *)tmpbuffer;
459 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
461 error = xfs_da_grow_inode(args, &blkno);
464 * If we hit an IO error middle of the transaction inside
465 * grow_inode(), we may have inconsistent data. Bail out.
469 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
470 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
475 error = xfs_attr_leaf_create(args, blkno, &bp);
477 error = xfs_da_shrink_inode(args, 0, bp);
481 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
482 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
486 memset((char *)&nargs, 0, sizeof(nargs));
488 nargs.firstblock = args->firstblock;
489 nargs.flist = args->flist;
490 nargs.total = args->total;
491 nargs.whichfork = XFS_ATTR_FORK;
492 nargs.trans = args->trans;
496 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
497 nargs.name = (char *)sfe->nameval;
498 nargs.namelen = sfe->namelen;
499 nargs.value = (char *)&sfe->nameval[nargs.namelen];
500 nargs.valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
501 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
503 nargs.flags = (sfe->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
504 ((sfe->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
505 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
506 ASSERT(error == ENOATTR);
507 error = xfs_attr_leaf_add(bp, &nargs);
508 ASSERT(error != ENOSPC);
511 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
518 kmem_free(tmpbuffer, size);
523 xfs_attr_shortform_compare(const void *a, const void *b)
525 xfs_attr_sf_sort_t *sa, *sb;
527 sa = (xfs_attr_sf_sort_t *)a;
528 sb = (xfs_attr_sf_sort_t *)b;
529 if (INT_GET(sa->hash, ARCH_CONVERT)
530 < INT_GET(sb->hash, ARCH_CONVERT)) {
532 } else if (INT_GET(sa->hash, ARCH_CONVERT)
533 > INT_GET(sb->hash, ARCH_CONVERT)) {
536 return(sa->entno - sb->entno);
541 * Copy out entries of shortform attribute lists for attr_list().
542 * Shortform atrtribute lists are not stored in hashval sorted order.
543 * If the output buffer is not large enough to hold them all, then we
544 * we have to calculate each entries' hashvalue and sort them before
545 * we can begin returning them to the user.
549 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
551 attrlist_cursor_kern_t *cursor;
552 xfs_attr_sf_sort_t *sbuf, *sbp;
553 xfs_attr_shortform_t *sf;
554 xfs_attr_sf_entry_t *sfe;
556 int sbsize, nsbuf, count, i;
558 ASSERT(context != NULL);
561 ASSERT(dp->i_afp != NULL);
562 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
566 cursor = context->cursor;
567 ASSERT(cursor != NULL);
569 xfs_attr_trace_l_c("sf start", context);
572 * If the buffer is large enough, do not bother with sorting.
573 * Note the generous fudge factor of 16 overhead bytes per entry.
575 if ((dp->i_afp->if_bytes + INT_GET(sf->hdr.count, ARCH_CONVERT) * 16)
576 < context->bufsize) {
577 for (i = 0, sfe = &sf->list[0];
578 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
581 if (((context->flags & ATTR_SECURE) != 0) !=
582 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
583 !(context->flags & ATTR_KERNORMALS)) {
584 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
587 if (((context->flags & ATTR_ROOT) != 0) !=
588 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
589 !(context->flags & ATTR_KERNROOTLS)) {
590 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
593 namesp = (sfe->flags & XFS_ATTR_SECURE) ? &attr_secure:
594 ((sfe->flags & XFS_ATTR_ROOT) ? &attr_trusted :
596 if (context->flags & ATTR_KERNOVAL) {
597 ASSERT(context->flags & ATTR_KERNAMELS);
598 context->count += namesp->attr_namelen +
599 INT_GET(sfe->namelen, ARCH_CONVERT) + 1;
602 if (xfs_attr_put_listent(context, namesp,
603 (char *)sfe->nameval,
605 (int)INT_GET(sfe->valuelen,
609 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
611 xfs_attr_trace_l_c("sf big-gulp", context);
616 * It didn't all fit, so we have to sort everything on hashval.
618 sbsize = INT_GET(sf->hdr.count, ARCH_CONVERT) * sizeof(*sbuf);
619 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
622 * Scan the attribute list for the rest of the entries, storing
623 * the relevant info from only those that match into a buffer.
626 for (i = 0, sfe = &sf->list[0];
627 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
629 ((char *)sfe < (char *)sf) ||
630 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
631 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
633 context->dp->i_mount, sfe);
634 xfs_attr_trace_l_c("sf corrupted", context);
635 kmem_free(sbuf, sbsize);
636 return XFS_ERROR(EFSCORRUPTED);
638 if (((context->flags & ATTR_SECURE) != 0) !=
639 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
640 !(context->flags & ATTR_KERNORMALS)) {
641 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
644 if (((context->flags & ATTR_ROOT) != 0) !=
645 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
646 !(context->flags & ATTR_KERNROOTLS)) {
647 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
651 INT_SET(sbp->hash, ARCH_CONVERT,
652 xfs_da_hashname((char *)sfe->nameval, sfe->namelen));
653 sbp->name = (char *)sfe->nameval;
654 sbp->namelen = sfe->namelen;
655 /* These are bytes, and both on-disk, don't endian-flip */
656 sbp->valuelen = sfe->valuelen;
657 sbp->flags = sfe->flags;
658 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
664 * Sort the entries on hash then entno.
666 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
669 * Re-find our place IN THE SORTED LIST.
674 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
675 if (INT_GET(sbp->hash, ARCH_CONVERT) == cursor->hashval) {
676 if (cursor->offset == count) {
680 } else if (INT_GET(sbp->hash, ARCH_CONVERT) > cursor->hashval) {
685 kmem_free(sbuf, sbsize);
686 xfs_attr_trace_l_c("blk end", context);
691 * Loop putting entries into the user buffer.
693 for ( ; i < nsbuf; i++, sbp++) {
696 namesp = (sbp->flags & XFS_ATTR_SECURE) ? &attr_secure :
697 ((sbp->flags & XFS_ATTR_ROOT) ? &attr_trusted :
700 if (cursor->hashval != INT_GET(sbp->hash, ARCH_CONVERT)) {
701 cursor->hashval = INT_GET(sbp->hash, ARCH_CONVERT);
704 if (context->flags & ATTR_KERNOVAL) {
705 ASSERT(context->flags & ATTR_KERNAMELS);
706 context->count += namesp->attr_namelen +
709 if (xfs_attr_put_listent(context, namesp,
710 sbp->name, sbp->namelen,
711 INT_GET(sbp->valuelen, ARCH_CONVERT)))
717 kmem_free(sbuf, sbsize);
718 xfs_attr_trace_l_c("sf E-O-F", context);
723 * Check a leaf attribute block to see if all the entries would fit into
724 * a shortform attribute list.
727 xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
729 xfs_attr_leafblock_t *leaf;
730 xfs_attr_leaf_entry_t *entry;
731 xfs_attr_leaf_name_local_t *name_loc;
735 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
736 == XFS_ATTR_LEAF_MAGIC);
738 entry = &leaf->entries[0];
739 bytes = sizeof(struct xfs_attr_sf_hdr);
740 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
741 if (entry->flags & XFS_ATTR_INCOMPLETE)
742 continue; /* don't copy partial entries */
743 if (!(entry->flags & XFS_ATTR_LOCAL))
745 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
746 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
748 if (INT_GET(name_loc->valuelen, ARCH_CONVERT) >= XFS_ATTR_SF_ENTSIZE_MAX)
750 bytes += sizeof(struct xfs_attr_sf_entry)-1
752 + INT_GET(name_loc->valuelen, ARCH_CONVERT);
754 if (bytes == sizeof(struct xfs_attr_sf_hdr))
756 return(xfs_attr_shortform_bytesfit(dp, bytes));
760 * Convert a leaf attribute list to shortform attribute list
763 xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
765 xfs_attr_leafblock_t *leaf;
766 xfs_attr_leaf_entry_t *entry;
767 xfs_attr_leaf_name_local_t *name_loc;
774 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
775 ASSERT(tmpbuffer != NULL);
778 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
779 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
780 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
781 == XFS_ATTR_LEAF_MAGIC);
782 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
785 * Clean out the prior contents of the attribute list.
787 error = xfs_da_shrink_inode(args, 0, bp);
793 * Last attribute was removed, revert to original
794 * inode format making all literal area available
795 * to the data fork once more.
797 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
798 dp->i_d.di_forkoff = 0;
799 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
800 ASSERT(dp->i_d.di_anextents == 0);
801 ASSERT(dp->i_afp == NULL);
802 dp->i_df.if_ext_max =
803 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
804 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
808 xfs_attr_shortform_create(args);
811 * Copy the attributes
813 memset((char *)&nargs, 0, sizeof(nargs));
815 nargs.firstblock = args->firstblock;
816 nargs.flist = args->flist;
817 nargs.total = args->total;
818 nargs.whichfork = XFS_ATTR_FORK;
819 nargs.trans = args->trans;
821 entry = &leaf->entries[0];
822 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
823 if (entry->flags & XFS_ATTR_INCOMPLETE)
824 continue; /* don't copy partial entries */
827 ASSERT(entry->flags & XFS_ATTR_LOCAL);
828 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
829 nargs.name = (char *)name_loc->nameval;
830 nargs.namelen = name_loc->namelen;
831 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
832 nargs.valuelen = INT_GET(name_loc->valuelen, ARCH_CONVERT);
833 nargs.hashval = INT_GET(entry->hashval, ARCH_CONVERT);
834 nargs.flags = (entry->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
835 ((entry->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
836 xfs_attr_shortform_add(&nargs, forkoff);
841 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
846 * Convert from using a single leaf to a root node and a leaf.
849 xfs_attr_leaf_to_node(xfs_da_args_t *args)
851 xfs_attr_leafblock_t *leaf;
852 xfs_da_intnode_t *node;
854 xfs_dabuf_t *bp1, *bp2;
860 error = xfs_da_grow_inode(args, &blkno);
863 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
869 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
874 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
875 xfs_da_buf_done(bp1);
877 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
880 * Set up the new root node.
882 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
887 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
888 == XFS_ATTR_LEAF_MAGIC);
889 /* both on-disk, don't endian-flip twice */
890 node->btree[0].hashval =
891 leaf->entries[INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval;
892 INT_SET(node->btree[0].before, ARCH_CONVERT, blkno);
893 INT_SET(node->hdr.count, ARCH_CONVERT, 1);
894 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
898 xfs_da_buf_done(bp1);
900 xfs_da_buf_done(bp2);
905 /*========================================================================
906 * Routines used for growing the Btree.
907 *========================================================================*/
910 * Create the initial contents of a leaf attribute list
911 * or a leaf in a node attribute list.
914 xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
916 xfs_attr_leafblock_t *leaf;
917 xfs_attr_leaf_hdr_t *hdr;
924 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
930 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
932 INT_SET(hdr->info.magic, ARCH_CONVERT, XFS_ATTR_LEAF_MAGIC);
933 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
934 if (!hdr->firstused) {
935 INT_SET(hdr->firstused, ARCH_CONVERT,
936 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
939 INT_SET(hdr->freemap[0].base, ARCH_CONVERT,
940 sizeof(xfs_attr_leaf_hdr_t));
941 INT_SET(hdr->freemap[0].size, ARCH_CONVERT,
942 INT_GET(hdr->firstused, ARCH_CONVERT)
943 - INT_GET(hdr->freemap[0].base,
946 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
953 * Split the leaf node, rebalance, then add the new entry.
956 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
957 xfs_da_state_blk_t *newblk)
963 * Allocate space for a new leaf node.
965 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
966 error = xfs_da_grow_inode(state->args, &blkno);
969 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
972 newblk->blkno = blkno;
973 newblk->magic = XFS_ATTR_LEAF_MAGIC;
976 * Rebalance the entries across the two leaves.
977 * NOTE: rebalance() currently depends on the 2nd block being empty.
979 xfs_attr_leaf_rebalance(state, oldblk, newblk);
980 error = xfs_da_blk_link(state, oldblk, newblk);
985 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
986 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
987 * "new" attrs info. Will need the "old" info to remove it later.
989 * Insert the "new" entry in the correct block.
992 error = xfs_attr_leaf_add(oldblk->bp, state->args);
994 error = xfs_attr_leaf_add(newblk->bp, state->args);
997 * Update last hashval in each block since we added the name.
999 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1000 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1005 * Add a name to the leaf attribute list structure.
1008 xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
1010 xfs_attr_leafblock_t *leaf;
1011 xfs_attr_leaf_hdr_t *hdr;
1012 xfs_attr_leaf_map_t *map;
1013 int tablesize, entsize, sum, tmp, i;
1016 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1017 == XFS_ATTR_LEAF_MAGIC);
1018 ASSERT((args->index >= 0)
1019 && (args->index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
1021 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1022 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1025 * Search through freemap for first-fit on new name length.
1026 * (may need to figure in size of entry struct too)
1028 tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1)
1029 * sizeof(xfs_attr_leaf_entry_t)
1030 + sizeof(xfs_attr_leaf_hdr_t);
1031 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1032 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1033 if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
1034 sum += INT_GET(map->size, ARCH_CONVERT);
1038 continue; /* no space in this map */
1040 if (INT_GET(map->base, ARCH_CONVERT)
1041 < INT_GET(hdr->firstused, ARCH_CONVERT))
1042 tmp += sizeof(xfs_attr_leaf_entry_t);
1043 if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
1044 tmp = xfs_attr_leaf_add_work(bp, args, i);
1047 sum += INT_GET(map->size, ARCH_CONVERT);
1051 * If there are no holes in the address space of the block,
1052 * and we don't have enough freespace, then compaction will do us
1053 * no good and we should just give up.
1055 if (!hdr->holes && (sum < entsize))
1056 return(XFS_ERROR(ENOSPC));
1059 * Compact the entries to coalesce free space.
1060 * This may change the hdr->count via dropping INCOMPLETE entries.
1062 xfs_attr_leaf_compact(args->trans, bp);
1065 * After compaction, the block is guaranteed to have only one
1066 * free region, in freemap[0]. If it is not big enough, give up.
1068 if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT)
1069 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1070 return(XFS_ERROR(ENOSPC));
1072 return(xfs_attr_leaf_add_work(bp, args, 0));
1076 * Add a name to a leaf attribute list structure.
1079 xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1081 xfs_attr_leafblock_t *leaf;
1082 xfs_attr_leaf_hdr_t *hdr;
1083 xfs_attr_leaf_entry_t *entry;
1084 xfs_attr_leaf_name_local_t *name_loc;
1085 xfs_attr_leaf_name_remote_t *name_rmt;
1086 xfs_attr_leaf_map_t *map;
1091 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1092 == XFS_ATTR_LEAF_MAGIC);
1094 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1095 ASSERT((args->index >= 0)
1096 && (args->index <= INT_GET(hdr->count, ARCH_CONVERT)));
1099 * Force open some space in the entry array and fill it in.
1101 entry = &leaf->entries[args->index];
1102 if (args->index < INT_GET(hdr->count, ARCH_CONVERT)) {
1103 tmp = INT_GET(hdr->count, ARCH_CONVERT) - args->index;
1104 tmp *= sizeof(xfs_attr_leaf_entry_t);
1105 memmove((char *)(entry+1), (char *)entry, tmp);
1106 xfs_da_log_buf(args->trans, bp,
1107 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1109 INT_MOD(hdr->count, ARCH_CONVERT, 1);
1112 * Allocate space for the new string (at the end of the run).
1114 map = &hdr->freemap[mapindex];
1115 mp = args->trans->t_mountp;
1116 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1117 ASSERT((INT_GET(map->base, ARCH_CONVERT) & 0x3) == 0);
1118 ASSERT(INT_GET(map->size, ARCH_CONVERT) >=
1119 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1120 mp->m_sb.sb_blocksize, NULL));
1121 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1122 ASSERT((INT_GET(map->size, ARCH_CONVERT) & 0x3) == 0);
1123 INT_MOD(map->size, ARCH_CONVERT,
1124 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1125 mp->m_sb.sb_blocksize, &tmp));
1126 INT_SET(entry->nameidx, ARCH_CONVERT,
1127 INT_GET(map->base, ARCH_CONVERT)
1128 + INT_GET(map->size, ARCH_CONVERT));
1129 INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
1130 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1131 entry->flags |= (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
1132 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
1134 entry->flags |= XFS_ATTR_INCOMPLETE;
1135 if ((args->blkno2 == args->blkno) &&
1136 (args->index2 <= args->index)) {
1140 xfs_da_log_buf(args->trans, bp,
1141 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1142 ASSERT((args->index == 0) || (INT_GET(entry->hashval, ARCH_CONVERT)
1143 >= INT_GET((entry-1)->hashval,
1145 ASSERT((args->index == INT_GET(hdr->count, ARCH_CONVERT)-1) ||
1146 (INT_GET(entry->hashval, ARCH_CONVERT)
1147 <= (INT_GET((entry+1)->hashval, ARCH_CONVERT))));
1150 * Copy the attribute name and value into the new space.
1152 * For "remote" attribute values, simply note that we need to
1153 * allocate space for the "remote" value. We can't actually
1154 * allocate the extents in this transaction, and we can't decide
1155 * which blocks they should be as we might allocate more blocks
1156 * as part of this transaction (a split operation for example).
1158 if (entry->flags & XFS_ATTR_LOCAL) {
1159 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1160 name_loc->namelen = args->namelen;
1161 INT_SET(name_loc->valuelen, ARCH_CONVERT, args->valuelen);
1162 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1163 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1164 INT_GET(name_loc->valuelen, ARCH_CONVERT));
1166 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
1167 name_rmt->namelen = args->namelen;
1168 memcpy((char *)name_rmt->name, args->name, args->namelen);
1169 entry->flags |= XFS_ATTR_INCOMPLETE;
1171 name_rmt->valuelen = 0;
1172 name_rmt->valueblk = 0;
1174 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1176 xfs_da_log_buf(args->trans, bp,
1177 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1178 xfs_attr_leaf_entsize(leaf, args->index)));
1181 * Update the control info for this leaf node
1183 if (INT_GET(entry->nameidx, ARCH_CONVERT)
1184 < INT_GET(hdr->firstused, ARCH_CONVERT)) {
1185 /* both on-disk, don't endian-flip twice */
1186 hdr->firstused = entry->nameidx;
1188 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT)
1189 >= ((INT_GET(hdr->count, ARCH_CONVERT)
1190 * sizeof(*entry))+sizeof(*hdr)));
1191 tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1)
1192 * sizeof(xfs_attr_leaf_entry_t)
1193 + sizeof(xfs_attr_leaf_hdr_t);
1194 map = &hdr->freemap[0];
1195 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1196 if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
1197 INT_MOD(map->base, ARCH_CONVERT,
1198 sizeof(xfs_attr_leaf_entry_t));
1199 INT_MOD(map->size, ARCH_CONVERT,
1200 -sizeof(xfs_attr_leaf_entry_t));
1203 INT_MOD(hdr->usedbytes, ARCH_CONVERT,
1204 xfs_attr_leaf_entsize(leaf, args->index));
1205 xfs_da_log_buf(args->trans, bp,
1206 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1211 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1214 xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1216 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1217 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1221 mp = trans->t_mountp;
1222 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1223 ASSERT(tmpbuffer != NULL);
1224 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1225 memset(bp->data, 0, XFS_LBSIZE(mp));
1228 * Copy basic information
1230 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1232 hdr_s = &leaf_s->hdr;
1233 hdr_d = &leaf_d->hdr;
1234 hdr_d->info = hdr_s->info; /* struct copy */
1235 INT_SET(hdr_d->firstused, ARCH_CONVERT, XFS_LBSIZE(mp));
1236 /* handle truncation gracefully */
1237 if (!hdr_d->firstused) {
1238 INT_SET(hdr_d->firstused, ARCH_CONVERT,
1239 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1241 hdr_d->usedbytes = 0;
1244 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT,
1245 sizeof(xfs_attr_leaf_hdr_t));
1246 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT,
1247 INT_GET(hdr_d->firstused, ARCH_CONVERT)
1248 - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
1251 * Copy all entry's in the same (sorted) order,
1252 * but allocate name/value pairs packed and in sequence.
1254 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1255 (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
1257 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1259 kmem_free(tmpbuffer, XFS_LBSIZE(mp));
1263 * Redistribute the attribute list entries between two leaf nodes,
1264 * taking into account the size of the new entry.
1266 * NOTE: if new block is empty, then it will get the upper half of the
1267 * old block. At present, all (one) callers pass in an empty second block.
1269 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1270 * to match what it is doing in splitting the attribute leaf block. Those
1271 * values are used in "atomic rename" operations on attributes. Note that
1272 * the "new" and "old" values can end up in different blocks.
1275 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1276 xfs_da_state_blk_t *blk2)
1278 xfs_da_args_t *args;
1279 xfs_da_state_blk_t *tmp_blk;
1280 xfs_attr_leafblock_t *leaf1, *leaf2;
1281 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1282 int count, totallen, max, space, swap;
1285 * Set up environment.
1287 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1288 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1289 leaf1 = blk1->bp->data;
1290 leaf2 = blk2->bp->data;
1291 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
1292 == XFS_ATTR_LEAF_MAGIC);
1293 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
1294 == XFS_ATTR_LEAF_MAGIC);
1298 * Check ordering of blocks, reverse if it makes things simpler.
1300 * NOTE: Given that all (current) callers pass in an empty
1301 * second block, this code should never set "swap".
1304 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1308 leaf1 = blk1->bp->data;
1309 leaf2 = blk2->bp->data;
1316 * Examine entries until we reduce the absolute difference in
1317 * byte usage between the two blocks to a minimum. Then get
1318 * the direction to copy and the number of elements to move.
1320 * "inleaf" is true if the new entry should be inserted into blk1.
1321 * If "swap" is also true, then reverse the sense of "inleaf".
1323 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1326 state->inleaf = !state->inleaf;
1329 * Move any entries required from leaf to leaf:
1331 if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
1333 * Figure the total bytes to be added to the destination leaf.
1335 /* number entries being moved */
1336 count = INT_GET(hdr1->count, ARCH_CONVERT) - count;
1337 space = INT_GET(hdr1->usedbytes, ARCH_CONVERT) - totallen;
1338 space += count * sizeof(xfs_attr_leaf_entry_t);
1341 * leaf2 is the destination, compact it if it looks tight.
1343 max = INT_GET(hdr2->firstused, ARCH_CONVERT)
1344 - sizeof(xfs_attr_leaf_hdr_t);
1345 max -= INT_GET(hdr2->count, ARCH_CONVERT)
1346 * sizeof(xfs_attr_leaf_entry_t);
1348 xfs_attr_leaf_compact(args->trans, blk2->bp);
1352 * Move high entries from leaf1 to low end of leaf2.
1354 xfs_attr_leaf_moveents(leaf1,
1355 INT_GET(hdr1->count, ARCH_CONVERT)-count,
1356 leaf2, 0, count, state->mp);
1358 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1359 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1360 } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
1362 * I assert that since all callers pass in an empty
1363 * second buffer, this code should never execute.
1367 * Figure the total bytes to be added to the destination leaf.
1369 /* number entries being moved */
1370 count -= INT_GET(hdr1->count, ARCH_CONVERT);
1371 space = totallen - INT_GET(hdr1->usedbytes, ARCH_CONVERT);
1372 space += count * sizeof(xfs_attr_leaf_entry_t);
1375 * leaf1 is the destination, compact it if it looks tight.
1377 max = INT_GET(hdr1->firstused, ARCH_CONVERT)
1378 - sizeof(xfs_attr_leaf_hdr_t);
1379 max -= INT_GET(hdr1->count, ARCH_CONVERT)
1380 * sizeof(xfs_attr_leaf_entry_t);
1382 xfs_attr_leaf_compact(args->trans, blk1->bp);
1386 * Move low entries from leaf2 to high end of leaf1.
1388 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1389 (int)INT_GET(hdr1->count, ARCH_CONVERT), count,
1392 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1393 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1397 * Copy out last hashval in each block for B-tree code.
1400 INT_GET(leaf1->entries[INT_GET(leaf1->hdr.count,
1401 ARCH_CONVERT)-1].hashval, ARCH_CONVERT);
1403 INT_GET(leaf2->entries[INT_GET(leaf2->hdr.count,
1404 ARCH_CONVERT)-1].hashval, ARCH_CONVERT);
1407 * Adjust the expected index for insertion.
1408 * NOTE: this code depends on the (current) situation that the
1409 * second block was originally empty.
1411 * If the insertion point moved to the 2nd block, we must adjust
1412 * the index. We must also track the entry just following the
1413 * new entry for use in an "atomic rename" operation, that entry
1414 * is always the "old" entry and the "new" entry is what we are
1415 * inserting. The index/blkno fields refer to the "old" entry,
1416 * while the index2/blkno2 fields refer to the "new" entry.
1418 if (blk1->index > INT_GET(leaf1->hdr.count, ARCH_CONVERT)) {
1419 ASSERT(state->inleaf == 0);
1420 blk2->index = blk1->index
1421 - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1422 args->index = args->index2 = blk2->index;
1423 args->blkno = args->blkno2 = blk2->blkno;
1424 } else if (blk1->index == INT_GET(leaf1->hdr.count, ARCH_CONVERT)) {
1425 if (state->inleaf) {
1426 args->index = blk1->index;
1427 args->blkno = blk1->blkno;
1429 args->blkno2 = blk2->blkno;
1431 blk2->index = blk1->index
1432 - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1433 args->index = args->index2 = blk2->index;
1434 args->blkno = args->blkno2 = blk2->blkno;
1437 ASSERT(state->inleaf == 1);
1438 args->index = args->index2 = blk1->index;
1439 args->blkno = args->blkno2 = blk1->blkno;
1444 * Examine entries until we reduce the absolute difference in
1445 * byte usage between the two blocks to a minimum.
1446 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1447 * GROT: there will always be enough room in either block for a new entry.
1448 * GROT: Do a double-split for this case?
1451 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1452 xfs_da_state_blk_t *blk1,
1453 xfs_da_state_blk_t *blk2,
1454 int *countarg, int *usedbytesarg)
1456 xfs_attr_leafblock_t *leaf1, *leaf2;
1457 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1458 xfs_attr_leaf_entry_t *entry;
1459 int count, max, index, totallen, half;
1460 int lastdelta, foundit, tmp;
1463 * Set up environment.
1465 leaf1 = blk1->bp->data;
1466 leaf2 = blk2->bp->data;
1473 * Examine entries until we reduce the absolute difference in
1474 * byte usage between the two blocks to a minimum.
1476 max = INT_GET(hdr1->count, ARCH_CONVERT)
1477 + INT_GET(hdr2->count, ARCH_CONVERT);
1478 half = (max+1) * sizeof(*entry);
1479 half += INT_GET(hdr1->usedbytes, ARCH_CONVERT)
1480 + INT_GET(hdr2->usedbytes, ARCH_CONVERT)
1481 + xfs_attr_leaf_newentsize(
1482 state->args->namelen,
1483 state->args->valuelen,
1484 state->blocksize, NULL);
1486 lastdelta = state->blocksize;
1487 entry = &leaf1->entries[0];
1488 for (count = index = 0; count < max; entry++, index++, count++) {
1490 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1492 * The new entry is in the first block, account for it.
1494 if (count == blk1->index) {
1495 tmp = totallen + sizeof(*entry) +
1496 xfs_attr_leaf_newentsize(
1497 state->args->namelen,
1498 state->args->valuelen,
1499 state->blocksize, NULL);
1500 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1502 lastdelta = XFS_ATTR_ABS(half - tmp);
1508 * Wrap around into the second block if necessary.
1510 if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
1512 entry = &leaf1->entries[0];
1517 * Figure out if next leaf entry would be too much.
1519 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1521 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1523 lastdelta = XFS_ATTR_ABS(half - tmp);
1529 * Calculate the number of usedbytes that will end up in lower block.
1530 * If new entry not in lower block, fix up the count.
1532 totallen -= count * sizeof(*entry);
1534 totallen -= sizeof(*entry) +
1535 xfs_attr_leaf_newentsize(
1536 state->args->namelen,
1537 state->args->valuelen,
1538 state->blocksize, NULL);
1542 *usedbytesarg = totallen;
1546 /*========================================================================
1547 * Routines used for shrinking the Btree.
1548 *========================================================================*/
1551 * Check a leaf block and its neighbors to see if the block should be
1552 * collapsed into one or the other neighbor. Always keep the block
1553 * with the smaller block number.
1554 * If the current block is over 50% full, don't try to join it, return 0.
1555 * If the block is empty, fill in the state structure and return 2.
1556 * If it can be collapsed, fill in the state structure and return 1.
1557 * If nothing can be done, return 0.
1559 * GROT: allow for INCOMPLETE entries in calculation.
1562 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1564 xfs_attr_leafblock_t *leaf;
1565 xfs_da_state_blk_t *blk;
1566 xfs_da_blkinfo_t *info;
1567 int count, bytes, forward, error, retval, i;
1572 * Check for the degenerate case of the block being over 50% full.
1573 * If so, it's not worth even looking to see if we might be able
1574 * to coalesce with a sibling.
1576 blk = &state->path.blk[ state->path.active-1 ];
1577 info = blk->bp->data;
1578 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
1579 leaf = (xfs_attr_leafblock_t *)info;
1580 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1581 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1582 count * sizeof(xfs_attr_leaf_entry_t) +
1583 INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1584 if (bytes > (state->blocksize >> 1)) {
1585 *action = 0; /* blk over 50%, don't try to join */
1590 * Check for the degenerate case of the block being empty.
1591 * If the block is empty, we'll simply delete it, no need to
1592 * coalesce it with a sibling block. We choose (aribtrarily)
1593 * to merge with the forward block unless it is NULL.
1597 * Make altpath point to the block we want to keep and
1598 * path point to the block we want to drop (this one).
1600 forward = info->forw;
1601 memcpy(&state->altpath, &state->path, sizeof(state->path));
1602 error = xfs_da_path_shift(state, &state->altpath, forward,
1615 * Examine each sibling block to see if we can coalesce with
1616 * at least 25% free space to spare. We need to figure out
1617 * whether to merge with the forward or the backward block.
1618 * We prefer coalescing with the lower numbered sibling so as
1619 * to shrink an attribute list over time.
1621 /* start with smaller blk num */
1622 forward = (INT_GET(info->forw, ARCH_CONVERT)
1623 < INT_GET(info->back, ARCH_CONVERT));
1624 for (i = 0; i < 2; forward = !forward, i++) {
1626 blkno = INT_GET(info->forw, ARCH_CONVERT);
1628 blkno = INT_GET(info->back, ARCH_CONVERT);
1631 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1632 blkno, -1, &bp, XFS_ATTR_FORK);
1637 leaf = (xfs_attr_leafblock_t *)info;
1638 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1639 bytes = state->blocksize - (state->blocksize>>2);
1640 bytes -= INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1642 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1643 == XFS_ATTR_LEAF_MAGIC);
1644 count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
1645 bytes -= INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1646 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1647 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1648 xfs_da_brelse(state->args->trans, bp);
1650 break; /* fits with at least 25% to spare */
1658 * Make altpath point to the block we want to keep (the lower
1659 * numbered block) and path point to the block we want to drop.
1661 memcpy(&state->altpath, &state->path, sizeof(state->path));
1662 if (blkno < blk->blkno) {
1663 error = xfs_da_path_shift(state, &state->altpath, forward,
1666 error = xfs_da_path_shift(state, &state->path, forward,
1680 * Remove a name from the leaf attribute list structure.
1682 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1683 * If two leaves are 37% full, when combined they will leave 25% free.
1686 xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1688 xfs_attr_leafblock_t *leaf;
1689 xfs_attr_leaf_hdr_t *hdr;
1690 xfs_attr_leaf_map_t *map;
1691 xfs_attr_leaf_entry_t *entry;
1692 int before, after, smallest, entsize;
1693 int tablesize, tmp, i;
1697 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1698 == XFS_ATTR_LEAF_MAGIC);
1700 mp = args->trans->t_mountp;
1701 ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0)
1702 && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1703 ASSERT((args->index >= 0)
1704 && (args->index < INT_GET(hdr->count, ARCH_CONVERT)));
1705 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT)
1706 >= ((INT_GET(hdr->count, ARCH_CONVERT)
1707 * sizeof(*entry))+sizeof(*hdr)));
1708 entry = &leaf->entries[args->index];
1709 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1710 >= INT_GET(hdr->firstused, ARCH_CONVERT));
1711 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1714 * Scan through free region table:
1715 * check for adjacency of free'd entry with an existing one,
1716 * find smallest free region in case we need to replace it,
1717 * adjust any map that borders the entry table,
1719 tablesize = INT_GET(hdr->count, ARCH_CONVERT)
1720 * sizeof(xfs_attr_leaf_entry_t)
1721 + sizeof(xfs_attr_leaf_hdr_t);
1722 map = &hdr->freemap[0];
1723 tmp = INT_GET(map->size, ARCH_CONVERT);
1724 before = after = -1;
1725 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1726 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1727 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1728 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1729 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1730 if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
1731 INT_MOD(map->base, ARCH_CONVERT,
1732 -sizeof(xfs_attr_leaf_entry_t));
1733 INT_MOD(map->size, ARCH_CONVERT,
1734 sizeof(xfs_attr_leaf_entry_t));
1737 if ((INT_GET(map->base, ARCH_CONVERT)
1738 + INT_GET(map->size, ARCH_CONVERT))
1739 == INT_GET(entry->nameidx, ARCH_CONVERT)) {
1741 } else if (INT_GET(map->base, ARCH_CONVERT)
1742 == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
1744 } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
1745 tmp = INT_GET(map->size, ARCH_CONVERT);
1751 * Coalesce adjacent freemap regions,
1752 * or replace the smallest region.
1754 if ((before >= 0) || (after >= 0)) {
1755 if ((before >= 0) && (after >= 0)) {
1756 map = &hdr->freemap[before];
1757 INT_MOD(map->size, ARCH_CONVERT, entsize);
1758 INT_MOD(map->size, ARCH_CONVERT,
1759 INT_GET(hdr->freemap[after].size,
1761 hdr->freemap[after].base = 0;
1762 hdr->freemap[after].size = 0;
1763 } else if (before >= 0) {
1764 map = &hdr->freemap[before];
1765 INT_MOD(map->size, ARCH_CONVERT, entsize);
1767 map = &hdr->freemap[after];
1768 /* both on-disk, don't endian flip twice */
1769 map->base = entry->nameidx;
1770 INT_MOD(map->size, ARCH_CONVERT, entsize);
1774 * Replace smallest region (if it is smaller than free'd entry)
1776 map = &hdr->freemap[smallest];
1777 if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
1778 INT_SET(map->base, ARCH_CONVERT,
1779 INT_GET(entry->nameidx, ARCH_CONVERT));
1780 INT_SET(map->size, ARCH_CONVERT, entsize);
1785 * Did we remove the first entry?
1787 if (INT_GET(entry->nameidx, ARCH_CONVERT)
1788 == INT_GET(hdr->firstused, ARCH_CONVERT))
1794 * Compress the remaining entries and zero out the removed stuff.
1796 memset(XFS_ATTR_LEAF_NAME(leaf, args->index), 0, entsize);
1797 INT_MOD(hdr->usedbytes, ARCH_CONVERT, -entsize);
1798 xfs_da_log_buf(args->trans, bp,
1799 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1802 tmp = (INT_GET(hdr->count, ARCH_CONVERT) - args->index)
1803 * sizeof(xfs_attr_leaf_entry_t);
1804 memmove((char *)entry, (char *)(entry+1), tmp);
1805 INT_MOD(hdr->count, ARCH_CONVERT, -1);
1806 xfs_da_log_buf(args->trans, bp,
1807 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1808 entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
1809 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1812 * If we removed the first entry, re-find the first used byte
1813 * in the name area. Note that if the entry was the "firstused",
1814 * then we don't have a "hole" in our block resulting from
1815 * removing the name.
1818 tmp = XFS_LBSIZE(mp);
1819 entry = &leaf->entries[0];
1820 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1;
1821 i >= 0; entry++, i--) {
1822 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1823 >= INT_GET(hdr->firstused, ARCH_CONVERT));
1824 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1826 if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
1827 tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
1829 INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
1830 if (!hdr->firstused) {
1831 INT_SET(hdr->firstused, ARCH_CONVERT,
1832 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1835 hdr->holes = 1; /* mark as needing compaction */
1837 xfs_da_log_buf(args->trans, bp,
1838 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1841 * Check if leaf is less than 50% full, caller may want to
1842 * "join" the leaf with a sibling if so.
1844 tmp = sizeof(xfs_attr_leaf_hdr_t);
1845 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT)
1846 * sizeof(xfs_attr_leaf_entry_t);
1847 tmp += INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1848 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1852 * Move all the attribute list entries from drop_leaf into save_leaf.
1855 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1856 xfs_da_state_blk_t *save_blk)
1858 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1859 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1864 * Set up environment.
1867 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1868 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1869 drop_leaf = drop_blk->bp->data;
1870 save_leaf = save_blk->bp->data;
1871 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT)
1872 == XFS_ATTR_LEAF_MAGIC);
1873 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT)
1874 == XFS_ATTR_LEAF_MAGIC);
1875 drop_hdr = &drop_leaf->hdr;
1876 save_hdr = &save_leaf->hdr;
1879 * Save last hashval from dying block for later Btree fixup.
1882 INT_GET(drop_leaf->entries[INT_GET(drop_leaf->hdr.count,
1883 ARCH_CONVERT)-1].hashval,
1887 * Check if we need a temp buffer, or can we do it in place.
1888 * Note that we don't check "leaf" for holes because we will
1889 * always be dropping it, toosmall() decided that for us already.
1891 if (save_hdr->holes == 0) {
1893 * dest leaf has no holes, so we add there. May need
1894 * to make some room in the entry array.
1896 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1897 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1898 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1900 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1901 INT_GET(save_hdr->count, ARCH_CONVERT),
1902 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1907 * Destination has holes, so we make a temporary copy
1908 * of the leaf and add them both to that.
1910 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1911 ASSERT(tmpbuffer != NULL);
1912 memset(tmpbuffer, 0, state->blocksize);
1913 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1914 tmp_hdr = &tmp_leaf->hdr;
1915 tmp_hdr->info = save_hdr->info; /* struct copy */
1917 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
1918 if (!tmp_hdr->firstused) {
1919 INT_SET(tmp_hdr->firstused, ARCH_CONVERT,
1920 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1922 tmp_hdr->usedbytes = 0;
1923 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1924 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1925 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1927 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1928 INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1929 (int)INT_GET(save_hdr->count, ARCH_CONVERT),
1932 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1933 (int)INT_GET(save_hdr->count, ARCH_CONVERT),
1935 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1936 INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1937 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1940 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1941 kmem_free(tmpbuffer, state->blocksize);
1944 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1945 state->blocksize - 1);
1948 * Copy out last hashval in each block for B-tree code.
1951 INT_GET(save_leaf->entries[INT_GET(save_leaf->hdr.count,
1952 ARCH_CONVERT)-1].hashval,
1956 /*========================================================================
1957 * Routines used for finding things in the Btree.
1958 *========================================================================*/
1961 * Look up a name in a leaf attribute list structure.
1962 * This is the internal routine, it uses the caller's buffer.
1964 * Note that duplicate keys are allowed, but only check within the
1965 * current leaf node. The Btree code must check in adjacent leaf nodes.
1967 * Return in args->index the index into the entry[] array of either
1968 * the found entry, or where the entry should have been (insert before
1971 * Don't change the args->value unless we find the attribute.
1974 xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1976 xfs_attr_leafblock_t *leaf;
1977 xfs_attr_leaf_entry_t *entry;
1978 xfs_attr_leaf_name_local_t *name_loc;
1979 xfs_attr_leaf_name_remote_t *name_rmt;
1981 xfs_dahash_t hashval;
1984 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1985 == XFS_ATTR_LEAF_MAGIC);
1986 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT)
1987 < (XFS_LBSIZE(args->dp->i_mount)/8));
1990 * Binary search. (note: small blocks will skip this loop)
1992 hashval = args->hashval;
1993 probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
1994 for (entry = &leaf->entries[probe]; span > 4;
1995 entry = &leaf->entries[probe]) {
1997 if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
1999 else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
2004 ASSERT((probe >= 0) &&
2006 || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
2007 ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT)
2011 * Since we may have duplicate hashval's, find the first matching
2012 * hashval in the leaf.
2014 while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT)
2019 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))
2020 && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
2024 if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT))
2025 || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
2026 args->index = probe;
2027 return(XFS_ERROR(ENOATTR));
2031 * Duplicate keys may be present, so search all of them for a match.
2033 for ( ; (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))
2034 && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval);
2037 * GROT: Add code to remove incomplete entries.
2040 * If we are looking for INCOMPLETE entries, show only those.
2041 * If we are looking for complete entries, show only those.
2043 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2044 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2047 if (entry->flags & XFS_ATTR_LOCAL) {
2048 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, probe);
2049 if (name_loc->namelen != args->namelen)
2051 if (memcmp(args->name, (char *)name_loc->nameval,
2052 args->namelen) != 0)
2054 if (((args->flags & ATTR_SECURE) != 0) !=
2055 ((entry->flags & XFS_ATTR_SECURE) != 0))
2057 if (((args->flags & ATTR_ROOT) != 0) !=
2058 ((entry->flags & XFS_ATTR_ROOT) != 0))
2060 args->index = probe;
2061 return(XFS_ERROR(EEXIST));
2063 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, probe);
2064 if (name_rmt->namelen != args->namelen)
2066 if (memcmp(args->name, (char *)name_rmt->name,
2067 args->namelen) != 0)
2069 if (((args->flags & ATTR_SECURE) != 0) !=
2070 ((entry->flags & XFS_ATTR_SECURE) != 0))
2072 if (((args->flags & ATTR_ROOT) != 0) !=
2073 ((entry->flags & XFS_ATTR_ROOT) != 0))
2075 args->index = probe;
2077 = INT_GET(name_rmt->valueblk, ARCH_CONVERT);
2078 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2079 INT_GET(name_rmt->valuelen,
2081 return(XFS_ERROR(EEXIST));
2084 args->index = probe;
2085 return(XFS_ERROR(ENOATTR));
2089 * Get the value associated with an attribute name from a leaf attribute
2093 xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2096 xfs_attr_leafblock_t *leaf;
2097 xfs_attr_leaf_entry_t *entry;
2098 xfs_attr_leaf_name_local_t *name_loc;
2099 xfs_attr_leaf_name_remote_t *name_rmt;
2102 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2103 == XFS_ATTR_LEAF_MAGIC);
2104 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT)
2105 < (XFS_LBSIZE(args->dp->i_mount)/8));
2106 ASSERT(args->index < ((int)INT_GET(leaf->hdr.count, ARCH_CONVERT)));
2108 entry = &leaf->entries[args->index];
2109 if (entry->flags & XFS_ATTR_LOCAL) {
2110 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2111 ASSERT(name_loc->namelen == args->namelen);
2112 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2113 valuelen = INT_GET(name_loc->valuelen, ARCH_CONVERT);
2114 if (args->flags & ATTR_KERNOVAL) {
2115 args->valuelen = valuelen;
2118 if (args->valuelen < valuelen) {
2119 args->valuelen = valuelen;
2120 return(XFS_ERROR(ERANGE));
2122 args->valuelen = valuelen;
2123 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2125 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2126 ASSERT(name_rmt->namelen == args->namelen);
2127 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2128 valuelen = INT_GET(name_rmt->valuelen, ARCH_CONVERT);
2129 args->rmtblkno = INT_GET(name_rmt->valueblk, ARCH_CONVERT);
2130 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2131 if (args->flags & ATTR_KERNOVAL) {
2132 args->valuelen = valuelen;
2135 if (args->valuelen < valuelen) {
2136 args->valuelen = valuelen;
2137 return(XFS_ERROR(ERANGE));
2139 args->valuelen = valuelen;
2144 /*========================================================================
2146 *========================================================================*/
2149 * Move the indicated entries from one leaf to another.
2150 * NOTE: this routine modifies both source and destination leaves.
2154 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2155 xfs_attr_leafblock_t *leaf_d, int start_d,
2156 int count, xfs_mount_t *mp)
2158 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2159 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2163 * Check for nothing to do.
2169 * Set up environment.
2171 ASSERT(INT_GET(leaf_s->hdr.info.magic, ARCH_CONVERT)
2172 == XFS_ATTR_LEAF_MAGIC);
2173 ASSERT(INT_GET(leaf_d->hdr.info.magic, ARCH_CONVERT)
2174 == XFS_ATTR_LEAF_MAGIC);
2175 hdr_s = &leaf_s->hdr;
2176 hdr_d = &leaf_d->hdr;
2177 ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0)
2178 && (INT_GET(hdr_s->count, ARCH_CONVERT)
2179 < (XFS_LBSIZE(mp)/8)));
2180 ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
2181 ((INT_GET(hdr_s->count, ARCH_CONVERT)
2182 * sizeof(*entry_s))+sizeof(*hdr_s)));
2183 ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
2184 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
2185 ((INT_GET(hdr_d->count, ARCH_CONVERT)
2186 * sizeof(*entry_d))+sizeof(*hdr_d)));
2188 ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
2189 ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
2190 ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
2193 * Move the entries in the destination leaf up to make a hole?
2195 if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
2196 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
2197 tmp *= sizeof(xfs_attr_leaf_entry_t);
2198 entry_s = &leaf_d->entries[start_d];
2199 entry_d = &leaf_d->entries[start_d + count];
2200 memmove((char *)entry_d, (char *)entry_s, tmp);
2204 * Copy all entry's in the same (sorted) order,
2205 * but allocate attribute info packed and in sequence.
2207 entry_s = &leaf_s->entries[start_s];
2208 entry_d = &leaf_d->entries[start_d];
2210 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2211 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT)
2212 >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
2213 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2216 * Code to drop INCOMPLETE entries. Difficult to use as we
2217 * may also need to change the insertion index. Code turned
2218 * off for 6.2, should be revisited later.
2220 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2221 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2222 INT_MOD(hdr_s->usedbytes, ARCH_CONVERT, -tmp);
2223 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
2224 entry_d--; /* to compensate for ++ in loop hdr */
2226 if ((start_s + i) < offset)
2227 result++; /* insertion index adjustment */
2230 INT_MOD(hdr_d->firstused, ARCH_CONVERT, -tmp);
2231 /* both on-disk, don't endian flip twice */
2232 entry_d->hashval = entry_s->hashval;
2233 /* both on-disk, don't endian flip twice */
2234 entry_d->nameidx = hdr_d->firstused;
2235 entry_d->flags = entry_s->flags;
2236 ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp
2238 memmove(XFS_ATTR_LEAF_NAME(leaf_d, desti),
2239 XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), tmp);
2240 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp
2242 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2243 INT_MOD(hdr_s->usedbytes, ARCH_CONVERT, -tmp);
2244 INT_MOD(hdr_d->usedbytes, ARCH_CONVERT, tmp);
2245 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
2246 INT_MOD(hdr_d->count, ARCH_CONVERT, 1);
2247 tmp = INT_GET(hdr_d->count, ARCH_CONVERT)
2248 * sizeof(xfs_attr_leaf_entry_t)
2249 + sizeof(xfs_attr_leaf_hdr_t);
2250 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
2257 * Zero out the entries we just copied.
2259 if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
2260 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2261 entry_s = &leaf_s->entries[start_s];
2262 ASSERT(((char *)entry_s + tmp) <=
2263 ((char *)leaf_s + XFS_LBSIZE(mp)));
2264 memset((char *)entry_s, 0, tmp);
2267 * Move the remaining entries down to fill the hole,
2268 * then zero the entries at the top.
2270 tmp = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
2271 tmp *= sizeof(xfs_attr_leaf_entry_t);
2272 entry_s = &leaf_s->entries[start_s + count];
2273 entry_d = &leaf_s->entries[start_s];
2274 memmove((char *)entry_d, (char *)entry_s, tmp);
2276 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2277 entry_s = &leaf_s->entries[INT_GET(hdr_s->count,
2279 ASSERT(((char *)entry_s + tmp) <=
2280 ((char *)leaf_s + XFS_LBSIZE(mp)));
2281 memset((char *)entry_s, 0, tmp);
2285 * Fill in the freemap information
2287 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT,
2288 sizeof(xfs_attr_leaf_hdr_t));
2289 INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT,
2290 INT_GET(hdr_d->count, ARCH_CONVERT)
2291 * sizeof(xfs_attr_leaf_entry_t));
2292 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT,
2293 INT_GET(hdr_d->firstused, ARCH_CONVERT)
2294 - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
2295 hdr_d->freemap[1].base = 0;
2296 hdr_d->freemap[2].base = 0;
2297 hdr_d->freemap[1].size = 0;
2298 hdr_d->freemap[2].size = 0;
2299 hdr_s->holes = 1; /* leaf may not be compact */
2303 * Compare two leaf blocks "order".
2304 * Return 0 unless leaf2 should go before leaf1.
2307 xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2309 xfs_attr_leafblock_t *leaf1, *leaf2;
2311 leaf1 = leaf1_bp->data;
2312 leaf2 = leaf2_bp->data;
2313 ASSERT((INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
2314 == XFS_ATTR_LEAF_MAGIC) &&
2315 (INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2316 == XFS_ATTR_LEAF_MAGIC));
2317 if ( (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0)
2318 && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0)
2319 && ( (INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
2320 INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT))
2321 || (INT_GET(leaf2->entries[INT_GET(leaf2->hdr.count,
2322 ARCH_CONVERT)-1].hashval, ARCH_CONVERT) <
2323 INT_GET(leaf1->entries[INT_GET(leaf1->hdr.count,
2324 ARCH_CONVERT)-1].hashval, ARCH_CONVERT))) ) {
2331 * Pick up the last hashvalue from a leaf block.
2334 xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2336 xfs_attr_leafblock_t *leaf;
2339 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2340 == XFS_ATTR_LEAF_MAGIC);
2342 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
2343 if (!leaf->hdr.count)
2345 return(INT_GET(leaf->entries[INT_GET(leaf->hdr.count,
2346 ARCH_CONVERT)-1].hashval, ARCH_CONVERT));
2350 * Calculate the number of bytes used to store the indicated attribute
2351 * (whether local or remote only calculate bytes in this block).
2354 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2356 xfs_attr_leaf_name_local_t *name_loc;
2357 xfs_attr_leaf_name_remote_t *name_rmt;
2360 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2361 == XFS_ATTR_LEAF_MAGIC);
2362 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2363 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, index);
2364 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc->namelen,
2365 INT_GET(name_loc->valuelen,
2368 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, index);
2369 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt->namelen);
2375 * Calculate the number of bytes that would be required to store the new
2376 * attribute (whether local or remote only calculate bytes in this block).
2377 * This routine decides as a side effect whether the attribute will be
2378 * a "local" or a "remote" attribute.
2381 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2385 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen, valuelen);
2386 if (size < XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize)) {
2391 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen);
2400 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2403 xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2405 attrlist_cursor_kern_t *cursor;
2406 xfs_attr_leafblock_t *leaf;
2407 xfs_attr_leaf_entry_t *entry;
2408 xfs_attr_leaf_name_local_t *name_loc;
2409 xfs_attr_leaf_name_remote_t *name_rmt;
2414 cursor = context->cursor;
2415 cursor->initted = 1;
2417 xfs_attr_trace_l_cl("blk start", context, leaf);
2420 * Re-find our place in the leaf block if this is a new syscall.
2422 if (context->resynch) {
2423 entry = &leaf->entries[0];
2424 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
2426 if (INT_GET(entry->hashval, ARCH_CONVERT)
2427 == cursor->hashval) {
2428 if (cursor->offset == context->dupcnt) {
2429 context->dupcnt = 0;
2433 } else if (INT_GET(entry->hashval, ARCH_CONVERT)
2434 > cursor->hashval) {
2435 context->dupcnt = 0;
2439 if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
2440 xfs_attr_trace_l_c("not found", context);
2444 entry = &leaf->entries[0];
2447 context->resynch = 0;
2450 * We have found our place, start copying out the new attributes.
2453 for ( ; (i < INT_GET(leaf->hdr.count, ARCH_CONVERT))
2454 && (retval == 0); entry++, i++) {
2455 attrnames_t *namesp;
2457 if (INT_GET(entry->hashval, ARCH_CONVERT) != cursor->hashval) {
2458 cursor->hashval = INT_GET(entry->hashval, ARCH_CONVERT);
2462 if (entry->flags & XFS_ATTR_INCOMPLETE)
2463 continue; /* skip incomplete entries */
2464 if (((context->flags & ATTR_SECURE) != 0) !=
2465 ((entry->flags & XFS_ATTR_SECURE) != 0) &&
2466 !(context->flags & ATTR_KERNORMALS))
2467 continue; /* skip non-matching entries */
2468 if (((context->flags & ATTR_ROOT) != 0) !=
2469 ((entry->flags & XFS_ATTR_ROOT) != 0) &&
2470 !(context->flags & ATTR_KERNROOTLS))
2471 continue; /* skip non-matching entries */
2473 namesp = (entry->flags & XFS_ATTR_SECURE) ? &attr_secure :
2474 ((entry->flags & XFS_ATTR_ROOT) ? &attr_trusted :
2477 if (entry->flags & XFS_ATTR_LOCAL) {
2478 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
2479 if (context->flags & ATTR_KERNOVAL) {
2480 ASSERT(context->flags & ATTR_KERNAMELS);
2481 context->count += namesp->attr_namelen +
2482 (int)name_loc->namelen + 1;
2484 retval = xfs_attr_put_listent(context, namesp,
2485 (char *)name_loc->nameval,
2486 (int)name_loc->namelen,
2487 (int)INT_GET(name_loc->valuelen,
2491 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2492 if (context->flags & ATTR_KERNOVAL) {
2493 ASSERT(context->flags & ATTR_KERNAMELS);
2494 context->count += namesp->attr_namelen +
2495 (int)name_rmt->namelen + 1;
2497 retval = xfs_attr_put_listent(context, namesp,
2498 (char *)name_rmt->name,
2499 (int)name_rmt->namelen,
2500 (int)INT_GET(name_rmt->valuelen,
2508 xfs_attr_trace_l_cl("blk end", context, leaf);
2512 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
2513 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
2514 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
2515 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
2516 & ~(sizeof(u_int32_t)-1))
2519 * Format an attribute and copy it out to the user's buffer.
2520 * Take care to check values and protect against them changing later,
2521 * we may be reading them directly out of a user buffer.
2525 xfs_attr_put_listent(xfs_attr_list_context_t *context,
2526 attrnames_t *namesp, char *name, int namelen, int valuelen)
2528 attrlist_ent_t *aep;
2531 ASSERT(!(context->flags & ATTR_KERNOVAL));
2532 if (context->flags & ATTR_KERNAMELS) {
2535 ASSERT(context->count >= 0);
2537 arraytop = context->count + namesp->attr_namelen + namelen + 1;
2538 if (arraytop > context->firstu) {
2539 context->count = -1; /* insufficient space */
2542 offset = (char *)context->alist + context->count;
2543 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
2544 offset += namesp->attr_namelen;
2545 strncpy(offset, name, namelen); /* real name */
2548 context->count += namesp->attr_namelen + namelen + 1;
2552 ASSERT(context->count >= 0);
2553 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
2554 ASSERT(context->firstu >= sizeof(*context->alist));
2555 ASSERT(context->firstu <= context->bufsize);
2557 arraytop = sizeof(*context->alist) +
2558 context->count * sizeof(context->alist->al_offset[0]);
2559 context->firstu -= ATTR_ENTSIZE(namelen);
2560 if (context->firstu < arraytop) {
2561 xfs_attr_trace_l_c("buffer full", context);
2562 context->alist->al_more = 1;
2566 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
2567 aep->a_valuelen = valuelen;
2568 memcpy(aep->a_name, name, namelen);
2569 aep->a_name[ namelen ] = 0;
2570 context->alist->al_offset[ context->count++ ] = context->firstu;
2571 context->alist->al_count = context->count;
2572 xfs_attr_trace_l_c("add", context);
2576 /*========================================================================
2577 * Manage the INCOMPLETE flag in a leaf entry
2578 *========================================================================*/
2581 * Clear the INCOMPLETE flag on an entry in a leaf block.
2584 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2586 xfs_attr_leafblock_t *leaf;
2587 xfs_attr_leaf_entry_t *entry;
2588 xfs_attr_leaf_name_remote_t *name_rmt;
2592 xfs_attr_leaf_name_local_t *name_loc;
2598 * Set up the operation.
2600 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2608 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2609 == XFS_ATTR_LEAF_MAGIC);
2610 ASSERT(args->index < INT_GET(leaf->hdr.count, ARCH_CONVERT));
2611 ASSERT(args->index >= 0);
2612 entry = &leaf->entries[ args->index ];
2613 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2616 if (entry->flags & XFS_ATTR_LOCAL) {
2617 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2618 namelen = name_loc->namelen;
2619 name = (char *)name_loc->nameval;
2621 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2622 namelen = name_rmt->namelen;
2623 name = (char *)name_rmt->name;
2625 ASSERT(INT_GET(entry->hashval, ARCH_CONVERT) == args->hashval);
2626 ASSERT(namelen == args->namelen);
2627 ASSERT(memcmp(name, args->name, namelen) == 0);
2630 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2631 xfs_da_log_buf(args->trans, bp,
2632 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2634 if (args->rmtblkno) {
2635 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2636 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2637 INT_SET(name_rmt->valueblk, ARCH_CONVERT, args->rmtblkno);
2638 INT_SET(name_rmt->valuelen, ARCH_CONVERT, args->valuelen);
2639 xfs_da_log_buf(args->trans, bp,
2640 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2642 xfs_da_buf_done(bp);
2645 * Commit the flag value change and start the next trans in series.
2647 error = xfs_attr_rolltrans(&args->trans, args->dp);
2653 * Set the INCOMPLETE flag on an entry in a leaf block.
2656 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2658 xfs_attr_leafblock_t *leaf;
2659 xfs_attr_leaf_entry_t *entry;
2660 xfs_attr_leaf_name_remote_t *name_rmt;
2665 * Set up the operation.
2667 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2675 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2676 == XFS_ATTR_LEAF_MAGIC);
2677 ASSERT(args->index < INT_GET(leaf->hdr.count, ARCH_CONVERT));
2678 ASSERT(args->index >= 0);
2679 entry = &leaf->entries[ args->index ];
2681 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2682 entry->flags |= XFS_ATTR_INCOMPLETE;
2683 xfs_da_log_buf(args->trans, bp,
2684 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2685 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2686 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2687 name_rmt->valueblk = 0;
2688 name_rmt->valuelen = 0;
2689 xfs_da_log_buf(args->trans, bp,
2690 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2692 xfs_da_buf_done(bp);
2695 * Commit the flag value change and start the next trans in series.
2697 error = xfs_attr_rolltrans(&args->trans, args->dp);
2703 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2704 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2705 * entry given by args->blkno2/index2.
2707 * Note that they could be in different blocks, or in the same block.
2710 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2712 xfs_attr_leafblock_t *leaf1, *leaf2;
2713 xfs_attr_leaf_entry_t *entry1, *entry2;
2714 xfs_attr_leaf_name_remote_t *name_rmt;
2715 xfs_dabuf_t *bp1, *bp2;
2718 xfs_attr_leaf_name_local_t *name_loc;
2719 int namelen1, namelen2;
2720 char *name1, *name2;
2724 * Read the block containing the "old" attr
2726 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2731 ASSERT(bp1 != NULL);
2734 * Read the block containing the "new" attr, if it is different
2736 if (args->blkno2 != args->blkno) {
2737 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2738 -1, &bp2, XFS_ATTR_FORK);
2742 ASSERT(bp2 != NULL);
2748 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
2749 == XFS_ATTR_LEAF_MAGIC);
2750 ASSERT(args->index < INT_GET(leaf1->hdr.count, ARCH_CONVERT));
2751 ASSERT(args->index >= 0);
2752 entry1 = &leaf1->entries[ args->index ];
2755 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2756 == XFS_ATTR_LEAF_MAGIC);
2757 ASSERT(args->index2 < INT_GET(leaf2->hdr.count, ARCH_CONVERT));
2758 ASSERT(args->index2 >= 0);
2759 entry2 = &leaf2->entries[ args->index2 ];
2762 if (entry1->flags & XFS_ATTR_LOCAL) {
2763 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf1, args->index);
2764 namelen1 = name_loc->namelen;
2765 name1 = (char *)name_loc->nameval;
2767 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2768 namelen1 = name_rmt->namelen;
2769 name1 = (char *)name_rmt->name;
2771 if (entry2->flags & XFS_ATTR_LOCAL) {
2772 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf2, args->index2);
2773 namelen2 = name_loc->namelen;
2774 name2 = (char *)name_loc->nameval;
2776 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2777 namelen2 = name_rmt->namelen;
2778 name2 = (char *)name_rmt->name;
2780 ASSERT(INT_GET(entry1->hashval, ARCH_CONVERT) == INT_GET(entry2->hashval, ARCH_CONVERT));
2781 ASSERT(namelen1 == namelen2);
2782 ASSERT(memcmp(name1, name2, namelen1) == 0);
2785 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2786 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2788 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2789 xfs_da_log_buf(args->trans, bp1,
2790 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2791 if (args->rmtblkno) {
2792 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2793 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2794 INT_SET(name_rmt->valueblk, ARCH_CONVERT, args->rmtblkno);
2795 INT_SET(name_rmt->valuelen, ARCH_CONVERT, args->valuelen);
2796 xfs_da_log_buf(args->trans, bp1,
2797 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2800 entry2->flags |= XFS_ATTR_INCOMPLETE;
2801 xfs_da_log_buf(args->trans, bp2,
2802 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2803 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2804 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2805 name_rmt->valueblk = 0;
2806 name_rmt->valuelen = 0;
2807 xfs_da_log_buf(args->trans, bp2,
2808 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2810 xfs_da_buf_done(bp1);
2812 xfs_da_buf_done(bp2);
2815 * Commit the flag value change and start the next trans in series.
2817 error = xfs_attr_rolltrans(&args->trans, args->dp);
2822 /*========================================================================
2823 * Indiscriminately delete the entire attribute fork
2824 *========================================================================*/
2827 * Recurse (gasp!) through the attribute nodes until we find leaves.
2828 * We're doing a depth-first traversal in order to invalidate everything.
2831 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2833 xfs_da_blkinfo_t *info;
2839 * Read block 0 to see what we have to work with.
2840 * We only get here if we have extents, since we remove
2841 * the extents in reverse order the extent containing
2842 * block 0 must still be there.
2844 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2847 blkno = xfs_da_blkno(bp);
2850 * Invalidate the tree, even if the "tree" is only a single leaf block.
2851 * This is a depth-first traversal!
2854 if (INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
2855 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2856 } else if (INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC) {
2857 error = xfs_attr_leaf_inactive(trans, dp, bp);
2859 error = XFS_ERROR(EIO);
2860 xfs_da_brelse(*trans, bp);
2866 * Invalidate the incore copy of the root block.
2868 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2871 xfs_da_binval(*trans, bp); /* remove from cache */
2873 * Commit the invalidate and start the next transaction.
2875 error = xfs_attr_rolltrans(trans, dp);
2881 * Recurse (gasp!) through the attribute nodes until we find leaves.
2882 * We're doing a depth-first traversal in order to invalidate everything.
2885 xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2888 xfs_da_blkinfo_t *info;
2889 xfs_da_intnode_t *node;
2890 xfs_dablk_t child_fsb;
2891 xfs_daddr_t parent_blkno, child_blkno;
2892 int error, count, i;
2893 xfs_dabuf_t *child_bp;
2896 * Since this code is recursive (gasp!) we must protect ourselves.
2898 if (level > XFS_DA_NODE_MAXDEPTH) {
2899 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2900 return(XFS_ERROR(EIO));
2904 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT)
2905 == XFS_DA_NODE_MAGIC);
2906 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
2907 count = INT_GET(node->hdr.count, ARCH_CONVERT);
2909 xfs_da_brelse(*trans, bp);
2912 child_fsb = INT_GET(node->btree[0].before, ARCH_CONVERT);
2913 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2916 * If this is the node level just above the leaves, simply loop
2917 * over the leaves removing all of them. If this is higher up
2918 * in the tree, recurse downward.
2920 for (i = 0; i < count; i++) {
2922 * Read the subsidiary block to see what we have to work with.
2923 * Don't do this in a transaction. This is a depth-first
2924 * traversal of the tree so we may deal with many blocks
2925 * before we come back to this one.
2927 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2932 /* save for re-read later */
2933 child_blkno = xfs_da_blkno(child_bp);
2936 * Invalidate the subtree, however we have to.
2938 info = child_bp->data;
2939 if (INT_GET(info->magic, ARCH_CONVERT)
2940 == XFS_DA_NODE_MAGIC) {
2941 error = xfs_attr_node_inactive(trans, dp,
2943 } else if (INT_GET(info->magic, ARCH_CONVERT)
2944 == XFS_ATTR_LEAF_MAGIC) {
2945 error = xfs_attr_leaf_inactive(trans, dp,
2948 error = XFS_ERROR(EIO);
2949 xfs_da_brelse(*trans, child_bp);
2955 * Remove the subsidiary block from the cache
2958 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2959 &child_bp, XFS_ATTR_FORK);
2962 xfs_da_binval(*trans, child_bp);
2966 * If we're not done, re-read the parent to get the next
2967 * child block number.
2969 if ((i+1) < count) {
2970 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2971 &bp, XFS_ATTR_FORK);
2974 child_fsb = INT_GET(node->btree[i+1].before, ARCH_CONVERT);
2975 xfs_da_brelse(*trans, bp);
2978 * Atomically commit the whole invalidate stuff.
2980 if ((error = xfs_attr_rolltrans(trans, dp)))
2988 * Invalidate all of the "remote" value regions pointed to by a particular
2990 * Note that we must release the lock on the buffer so that we are not
2991 * caught holding something that the logging code wants to flush to disk.
2994 xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2996 xfs_attr_leafblock_t *leaf;
2997 xfs_attr_leaf_entry_t *entry;
2998 xfs_attr_leaf_name_remote_t *name_rmt;
2999 xfs_attr_inactive_list_t *list, *lp;
3000 int error, count, size, tmp, i;
3003 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
3004 == XFS_ATTR_LEAF_MAGIC);
3007 * Count the number of "remote" value extents.
3010 entry = &leaf->entries[0];
3011 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
3012 if ( INT_GET(entry->nameidx, ARCH_CONVERT)
3013 && ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
3014 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
3015 if (name_rmt->valueblk)
3021 * If there are no "remote" values, we're done.
3024 xfs_da_brelse(*trans, bp);
3029 * Allocate storage for a list of all the "remote" value extents.
3031 size = count * sizeof(xfs_attr_inactive_list_t);
3032 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
3035 * Identify each of the "remote" value extents.
3038 entry = &leaf->entries[0];
3039 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
3040 if ( INT_GET(entry->nameidx, ARCH_CONVERT)
3041 && ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
3042 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
3043 if (name_rmt->valueblk) {
3044 /* both on-disk, don't endian flip twice */
3045 lp->valueblk = name_rmt->valueblk;
3046 INT_SET(lp->valuelen, ARCH_CONVERT,
3047 XFS_B_TO_FSB(dp->i_mount,
3048 INT_GET(name_rmt->valuelen,
3054 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
3057 * Invalidate each of the "remote" value extents.
3060 for (lp = list, i = 0; i < count; i++, lp++) {
3061 tmp = xfs_attr_leaf_freextent(trans, dp,
3062 INT_GET(lp->valueblk,
3064 INT_GET(lp->valuelen,
3067 error = tmp; /* save only the 1st errno */
3070 kmem_free((xfs_caddr_t)list, size);
3075 * Look at all the extents for this logical region,
3076 * invalidate any buffers that are incore/in transactions.
3079 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
3080 xfs_dablk_t blkno, int blkcnt)
3082 xfs_bmbt_irec_t map;
3084 int tblkcnt, dblkcnt, nmap, error;
3089 * Roll through the "value", invalidating the attribute value's
3094 while (tblkcnt > 0) {
3096 * Try to remember where we decided to put the value.
3099 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
3100 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3101 NULL, 0, &map, &nmap, NULL);
3106 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3109 * If it's a hole, these are already unmapped
3110 * so there's nothing to invalidate.
3112 if (map.br_startblock != HOLESTARTBLOCK) {
3114 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3116 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3118 bp = xfs_trans_get_buf(*trans,
3119 dp->i_mount->m_ddev_targp,
3120 dblkno, dblkcnt, XFS_BUF_LOCK);
3121 xfs_trans_binval(*trans, bp);
3123 * Roll to next transaction.
3125 if ((error = xfs_attr_rolltrans(trans, dp)))
3129 tblkno += map.br_blockcount;
3130 tblkcnt -= map.br_blockcount;
3138 * Roll from one trans in the sequence of PERMANENT transactions to the next.
3141 xfs_attr_rolltrans(xfs_trans_t **transp, xfs_inode_t *dp)
3144 unsigned int logres, count;
3148 * Ensure that the inode is always logged.
3151 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
3154 * Copy the critical parameters from one trans to the next.
3156 logres = trans->t_log_res;
3157 count = trans->t_log_count;
3158 *transp = xfs_trans_dup(trans);
3161 * Commit the current transaction.
3162 * If this commit failed, then it'd just unlock those items that
3163 * are not marked ihold. That also means that a filesystem shutdown
3164 * is in progress. The caller takes the responsibility to cancel
3165 * the duplicate transaction that gets returned.
3167 if ((error = xfs_trans_commit(trans, 0, NULL)))
3173 * Reserve space in the log for th next transaction.
3174 * This also pushes items in the "AIL", the list of logged items,
3175 * out to disk if they are taking up space at the tail of the log
3176 * that we want to use. This requires that either nothing be locked
3177 * across this call, or that anything that is locked be logged in
3178 * the prior and the next transactions.
3180 error = xfs_trans_reserve(trans, 0, logres, 0,
3181 XFS_TRANS_PERM_LOG_RES, count);
3183 * Ensure that the inode is in the new transaction and locked.
3186 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
3187 xfs_trans_ihold(trans, dp);