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"
23 #include "xfs_trans.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_alloc.h"
32 #include "xfs_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_inode_item.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_error.h"
41 #include "xfs_trace.h"
46 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
49 /*========================================================================
50 * Function prototypes for the kernel.
51 *========================================================================*/
54 * Routines used for growing the Btree.
56 STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
57 struct xfs_buf **bpp);
58 STATIC int xfs_attr_leaf_add_work(struct xfs_buf *leaf_buffer,
59 xfs_da_args_t *args, int freemap_index);
60 STATIC void xfs_attr_leaf_compact(struct xfs_da_args *args,
61 struct xfs_buf *leaf_buffer);
62 STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
63 xfs_da_state_blk_t *blk1,
64 xfs_da_state_blk_t *blk2);
65 STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
66 xfs_da_state_blk_t *leaf_blk_1,
67 xfs_da_state_blk_t *leaf_blk_2,
68 int *number_entries_in_blk1,
69 int *number_usedbytes_in_blk1);
72 * Routines used for shrinking the Btree.
74 STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
75 struct xfs_buf *bp, int level);
76 STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
78 STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
79 xfs_dablk_t blkno, int blkcnt);
84 STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
86 xfs_attr_leafblock_t *dst_leaf,
87 int dst_start, int move_count,
89 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
91 /*========================================================================
92 * Namespace helper routines
93 *========================================================================*/
96 * If namespace bits don't match return 0.
97 * If all match then return 1.
100 xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
102 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
106 /*========================================================================
107 * External routines when attribute fork size < XFS_LITINO(mp).
108 *========================================================================*/
111 * Query whether the requested number of additional bytes of extended
112 * attribute space will be able to fit inline.
114 * Returns zero if not, else the di_forkoff fork offset to be used in the
115 * literal area for attribute data once the new bytes have been added.
117 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
118 * special case for dev/uuid inodes, they have fixed size data forks.
121 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
124 int minforkoff; /* lower limit on valid forkoff locations */
125 int maxforkoff; /* upper limit on valid forkoff locations */
127 xfs_mount_t *mp = dp->i_mount;
129 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
131 switch (dp->i_d.di_format) {
132 case XFS_DINODE_FMT_DEV:
133 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
134 return (offset >= minforkoff) ? minforkoff : 0;
135 case XFS_DINODE_FMT_UUID:
136 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
137 return (offset >= minforkoff) ? minforkoff : 0;
141 * If the requested numbers of bytes is smaller or equal to the
142 * current attribute fork size we can always proceed.
144 * Note that if_bytes in the data fork might actually be larger than
145 * the current data fork size is due to delalloc extents. In that
146 * case either the extent count will go down when they are converted
147 * to real extents, or the delalloc conversion will take care of the
148 * literal area rebalancing.
150 if (bytes <= XFS_IFORK_ASIZE(dp))
151 return dp->i_d.di_forkoff;
154 * For attr2 we can try to move the forkoff if there is space in the
155 * literal area, but for the old format we are done if there is no
156 * space in the fixed attribute fork.
158 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
161 dsize = dp->i_df.if_bytes;
163 switch (dp->i_d.di_format) {
164 case XFS_DINODE_FMT_EXTENTS:
166 * If there is no attr fork and the data fork is extents,
167 * determine if creating the default attr fork will result
168 * in the extents form migrating to btree. If so, the
169 * minimum offset only needs to be the space required for
172 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
173 xfs_default_attroffset(dp))
174 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
176 case XFS_DINODE_FMT_BTREE:
178 * If we have a data btree then keep forkoff if we have one,
179 * otherwise we are adding a new attr, so then we set
180 * minforkoff to where the btree root can finish so we have
181 * plenty of room for attrs
183 if (dp->i_d.di_forkoff) {
184 if (offset < dp->i_d.di_forkoff)
186 return dp->i_d.di_forkoff;
188 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
193 * A data fork btree root must have space for at least
194 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
196 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
197 minforkoff = roundup(minforkoff, 8) >> 3;
199 /* attr fork btree root can have at least this many key/ptr pairs */
200 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
201 maxforkoff = maxforkoff >> 3; /* rounded down */
203 if (offset >= maxforkoff)
205 if (offset >= minforkoff)
211 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
214 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
216 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
217 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
218 spin_lock(&mp->m_sb_lock);
219 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
220 xfs_sb_version_addattr2(&mp->m_sb);
221 spin_unlock(&mp->m_sb_lock);
222 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
224 spin_unlock(&mp->m_sb_lock);
229 * Create the initial contents of a shortform attribute list.
232 xfs_attr_shortform_create(xfs_da_args_t *args)
234 xfs_attr_sf_hdr_t *hdr;
238 trace_xfs_attr_sf_create(args);
244 ASSERT(ifp->if_bytes == 0);
245 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
246 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
247 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
248 ifp->if_flags |= XFS_IFINLINE;
250 ASSERT(ifp->if_flags & XFS_IFINLINE);
252 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
253 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
255 hdr->totsize = cpu_to_be16(sizeof(*hdr));
256 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
260 * Add a name/value pair to the shortform attribute list.
261 * Overflow from the inode has already been checked for.
264 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
266 xfs_attr_shortform_t *sf;
267 xfs_attr_sf_entry_t *sfe;
273 trace_xfs_attr_sf_add(args);
277 dp->i_d.di_forkoff = forkoff;
280 ASSERT(ifp->if_flags & XFS_IFINLINE);
281 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
283 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
285 if (sfe->namelen != args->namelen)
287 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
289 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
295 offset = (char *)sfe - (char *)sf;
296 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
297 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
298 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
299 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
301 sfe->namelen = args->namelen;
302 sfe->valuelen = args->valuelen;
303 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
304 memcpy(sfe->nameval, args->name, args->namelen);
305 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
307 be16_add_cpu(&sf->hdr.totsize, size);
308 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
310 xfs_sbversion_add_attr2(mp, args->trans);
314 * After the last attribute is removed revert to original inode format,
315 * making all literal area available to the data fork once more.
319 struct xfs_inode *ip,
320 struct xfs_trans *tp)
322 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
323 ip->i_d.di_forkoff = 0;
324 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
326 ASSERT(ip->i_d.di_anextents == 0);
327 ASSERT(ip->i_afp == NULL);
329 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
333 * Remove an attribute from the shortform attribute list structure.
336 xfs_attr_shortform_remove(xfs_da_args_t *args)
338 xfs_attr_shortform_t *sf;
339 xfs_attr_sf_entry_t *sfe;
340 int base, size=0, end, totsize, i;
344 trace_xfs_attr_sf_remove(args);
348 base = sizeof(xfs_attr_sf_hdr_t);
349 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
352 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
354 size = XFS_ATTR_SF_ENTSIZE(sfe);
355 if (sfe->namelen != args->namelen)
357 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
359 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
364 return(XFS_ERROR(ENOATTR));
367 * Fix up the attribute fork data, covering the hole
370 totsize = be16_to_cpu(sf->hdr.totsize);
372 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
374 be16_add_cpu(&sf->hdr.totsize, -size);
377 * Fix up the start offset of the attribute fork
380 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
381 (mp->m_flags & XFS_MOUNT_ATTR2) &&
382 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
383 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
384 xfs_attr_fork_reset(dp, args->trans);
386 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
387 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
388 ASSERT(dp->i_d.di_forkoff);
389 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
390 (args->op_flags & XFS_DA_OP_ADDNAME) ||
391 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
392 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
393 xfs_trans_log_inode(args->trans, dp,
394 XFS_ILOG_CORE | XFS_ILOG_ADATA);
397 xfs_sbversion_add_attr2(mp, args->trans);
403 * Look up a name in a shortform attribute list structure.
407 xfs_attr_shortform_lookup(xfs_da_args_t *args)
409 xfs_attr_shortform_t *sf;
410 xfs_attr_sf_entry_t *sfe;
414 trace_xfs_attr_sf_lookup(args);
416 ifp = args->dp->i_afp;
417 ASSERT(ifp->if_flags & XFS_IFINLINE);
418 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
420 for (i = 0; i < sf->hdr.count;
421 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
422 if (sfe->namelen != args->namelen)
424 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
426 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
428 return(XFS_ERROR(EEXIST));
430 return(XFS_ERROR(ENOATTR));
434 * Look up a name in a shortform attribute list structure.
438 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
440 xfs_attr_shortform_t *sf;
441 xfs_attr_sf_entry_t *sfe;
444 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
445 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
447 for (i = 0; i < sf->hdr.count;
448 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
449 if (sfe->namelen != args->namelen)
451 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
453 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
455 if (args->flags & ATTR_KERNOVAL) {
456 args->valuelen = sfe->valuelen;
457 return(XFS_ERROR(EEXIST));
459 if (args->valuelen < sfe->valuelen) {
460 args->valuelen = sfe->valuelen;
461 return(XFS_ERROR(ERANGE));
463 args->valuelen = sfe->valuelen;
464 memcpy(args->value, &sfe->nameval[args->namelen],
466 return(XFS_ERROR(EEXIST));
468 return(XFS_ERROR(ENOATTR));
472 * Convert from using the shortform to the leaf.
475 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
478 xfs_attr_shortform_t *sf;
479 xfs_attr_sf_entry_t *sfe;
487 trace_xfs_attr_sf_to_leaf(args);
491 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
492 size = be16_to_cpu(sf->hdr.totsize);
493 tmpbuffer = kmem_alloc(size, KM_SLEEP);
494 ASSERT(tmpbuffer != NULL);
495 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
496 sf = (xfs_attr_shortform_t *)tmpbuffer;
498 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
500 error = xfs_da_grow_inode(args, &blkno);
503 * If we hit an IO error middle of the transaction inside
504 * grow_inode(), we may have inconsistent data. Bail out.
508 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
509 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
514 error = xfs_attr_leaf_create(args, blkno, &bp);
516 error = xfs_da_shrink_inode(args, 0, bp);
520 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
521 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
525 memset((char *)&nargs, 0, sizeof(nargs));
527 nargs.firstblock = args->firstblock;
528 nargs.flist = args->flist;
529 nargs.total = args->total;
530 nargs.whichfork = XFS_ATTR_FORK;
531 nargs.trans = args->trans;
532 nargs.op_flags = XFS_DA_OP_OKNOENT;
535 for (i = 0; i < sf->hdr.count; i++) {
536 nargs.name = sfe->nameval;
537 nargs.namelen = sfe->namelen;
538 nargs.value = &sfe->nameval[nargs.namelen];
539 nargs.valuelen = sfe->valuelen;
540 nargs.hashval = xfs_da_hashname(sfe->nameval,
542 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
543 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
544 ASSERT(error == ENOATTR);
545 error = xfs_attr_leaf_add(bp, &nargs);
546 ASSERT(error != ENOSPC);
549 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
554 kmem_free(tmpbuffer);
559 xfs_attr_shortform_compare(const void *a, const void *b)
561 xfs_attr_sf_sort_t *sa, *sb;
563 sa = (xfs_attr_sf_sort_t *)a;
564 sb = (xfs_attr_sf_sort_t *)b;
565 if (sa->hash < sb->hash) {
567 } else if (sa->hash > sb->hash) {
570 return(sa->entno - sb->entno);
575 #define XFS_ISRESET_CURSOR(cursor) \
576 (!((cursor)->initted) && !((cursor)->hashval) && \
577 !((cursor)->blkno) && !((cursor)->offset))
579 * Copy out entries of shortform attribute lists for attr_list().
580 * Shortform attribute lists are not stored in hashval sorted order.
581 * If the output buffer is not large enough to hold them all, then we
582 * we have to calculate each entries' hashvalue and sort them before
583 * we can begin returning them to the user.
587 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
589 attrlist_cursor_kern_t *cursor;
590 xfs_attr_sf_sort_t *sbuf, *sbp;
591 xfs_attr_shortform_t *sf;
592 xfs_attr_sf_entry_t *sfe;
594 int sbsize, nsbuf, count, i;
597 ASSERT(context != NULL);
600 ASSERT(dp->i_afp != NULL);
601 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
605 cursor = context->cursor;
606 ASSERT(cursor != NULL);
608 trace_xfs_attr_list_sf(context);
611 * If the buffer is large enough and the cursor is at the start,
612 * do not bother with sorting since we will return everything in
613 * one buffer and another call using the cursor won't need to be
615 * Note the generous fudge factor of 16 overhead bytes per entry.
616 * If bufsize is zero then put_listent must be a search function
617 * and can just scan through what we have.
619 if (context->bufsize == 0 ||
620 (XFS_ISRESET_CURSOR(cursor) &&
621 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
622 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
623 error = context->put_listent(context,
628 &sfe->nameval[sfe->namelen]);
631 * Either search callback finished early or
632 * didn't fit it all in the buffer after all.
634 if (context->seen_enough)
639 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
641 trace_xfs_attr_list_sf_all(context);
645 /* do no more for a search callback */
646 if (context->bufsize == 0)
650 * It didn't all fit, so we have to sort everything on hashval.
652 sbsize = sf->hdr.count * sizeof(*sbuf);
653 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
656 * Scan the attribute list for the rest of the entries, storing
657 * the relevant info from only those that match into a buffer.
660 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
662 ((char *)sfe < (char *)sf) ||
663 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
664 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
666 context->dp->i_mount, sfe);
668 return XFS_ERROR(EFSCORRUPTED);
672 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
673 sbp->name = sfe->nameval;
674 sbp->namelen = sfe->namelen;
675 /* These are bytes, and both on-disk, don't endian-flip */
676 sbp->valuelen = sfe->valuelen;
677 sbp->flags = sfe->flags;
678 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
684 * Sort the entries on hash then entno.
686 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
689 * Re-find our place IN THE SORTED LIST.
694 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
695 if (sbp->hash == cursor->hashval) {
696 if (cursor->offset == count) {
700 } else if (sbp->hash > cursor->hashval) {
710 * Loop putting entries into the user buffer.
712 for ( ; i < nsbuf; i++, sbp++) {
713 if (cursor->hashval != sbp->hash) {
714 cursor->hashval = sbp->hash;
717 error = context->put_listent(context,
722 &sbp->name[sbp->namelen]);
725 if (context->seen_enough)
735 * Check a leaf attribute block to see if all the entries would fit into
736 * a shortform attribute list.
739 xfs_attr_shortform_allfit(
741 struct xfs_inode *dp)
743 xfs_attr_leafblock_t *leaf;
744 xfs_attr_leaf_entry_t *entry;
745 xfs_attr_leaf_name_local_t *name_loc;
749 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
751 entry = &leaf->entries[0];
752 bytes = sizeof(struct xfs_attr_sf_hdr);
753 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
754 if (entry->flags & XFS_ATTR_INCOMPLETE)
755 continue; /* don't copy partial entries */
756 if (!(entry->flags & XFS_ATTR_LOCAL))
758 name_loc = xfs_attr_leaf_name_local(leaf, i);
759 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
761 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
763 bytes += sizeof(struct xfs_attr_sf_entry)-1
765 + be16_to_cpu(name_loc->valuelen);
767 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
768 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
769 (bytes == sizeof(struct xfs_attr_sf_hdr)))
771 return(xfs_attr_shortform_bytesfit(dp, bytes));
775 * Convert a leaf attribute list to shortform attribute list
778 xfs_attr_leaf_to_shortform(
783 xfs_attr_leafblock_t *leaf;
784 xfs_attr_leaf_entry_t *entry;
785 xfs_attr_leaf_name_local_t *name_loc;
791 trace_xfs_attr_leaf_to_sf(args);
794 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
795 ASSERT(tmpbuffer != NULL);
798 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
799 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
800 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
801 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
804 * Clean out the prior contents of the attribute list.
806 error = xfs_da_shrink_inode(args, 0, bp);
811 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
812 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
813 xfs_attr_fork_reset(dp, args->trans);
817 xfs_attr_shortform_create(args);
820 * Copy the attributes
822 memset((char *)&nargs, 0, sizeof(nargs));
824 nargs.firstblock = args->firstblock;
825 nargs.flist = args->flist;
826 nargs.total = args->total;
827 nargs.whichfork = XFS_ATTR_FORK;
828 nargs.trans = args->trans;
829 nargs.op_flags = XFS_DA_OP_OKNOENT;
830 entry = &leaf->entries[0];
831 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
832 if (entry->flags & XFS_ATTR_INCOMPLETE)
833 continue; /* don't copy partial entries */
836 ASSERT(entry->flags & XFS_ATTR_LOCAL);
837 name_loc = xfs_attr_leaf_name_local(leaf, i);
838 nargs.name = name_loc->nameval;
839 nargs.namelen = name_loc->namelen;
840 nargs.value = &name_loc->nameval[nargs.namelen];
841 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
842 nargs.hashval = be32_to_cpu(entry->hashval);
843 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
844 xfs_attr_shortform_add(&nargs, forkoff);
849 kmem_free(tmpbuffer);
854 * Convert from using a single leaf to a root node and a leaf.
857 xfs_attr_leaf_to_node(xfs_da_args_t *args)
859 xfs_attr_leafblock_t *leaf;
860 xfs_da_intnode_t *node;
862 struct xfs_buf *bp1, *bp2;
866 trace_xfs_attr_leaf_to_node(args);
870 error = xfs_da_grow_inode(args, &blkno);
873 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
879 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
884 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(dp->i_mount));
886 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
889 * Set up the new root node.
891 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
896 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
897 /* both on-disk, don't endian-flip twice */
898 node->btree[0].hashval =
899 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
900 node->btree[0].before = cpu_to_be32(blkno);
901 node->hdr.count = cpu_to_be16(1);
902 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
909 /*========================================================================
910 * Routines used for growing the Btree.
911 *========================================================================*/
914 * Create the initial contents of a leaf attribute list
915 * or a leaf in a node attribute list.
918 xfs_attr_leaf_create(
921 struct xfs_buf **bpp)
923 xfs_attr_leafblock_t *leaf;
924 xfs_attr_leaf_hdr_t *hdr;
929 trace_xfs_attr_leaf_create(args);
933 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
939 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
941 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
942 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
943 if (!hdr->firstused) {
944 hdr->firstused = cpu_to_be16(
945 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
948 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
949 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
950 sizeof(xfs_attr_leaf_hdr_t));
952 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
959 * Split the leaf node, rebalance, then add the new entry.
962 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
963 xfs_da_state_blk_t *newblk)
968 trace_xfs_attr_leaf_split(state->args);
971 * Allocate space for a new leaf node.
973 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
974 error = xfs_da_grow_inode(state->args, &blkno);
977 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
980 newblk->blkno = blkno;
981 newblk->magic = XFS_ATTR_LEAF_MAGIC;
984 * Rebalance the entries across the two leaves.
985 * NOTE: rebalance() currently depends on the 2nd block being empty.
987 xfs_attr_leaf_rebalance(state, oldblk, newblk);
988 error = xfs_da_blk_link(state, oldblk, newblk);
993 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
994 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
995 * "new" attrs info. Will need the "old" info to remove it later.
997 * Insert the "new" entry in the correct block.
1000 trace_xfs_attr_leaf_add_old(state->args);
1001 error = xfs_attr_leaf_add(oldblk->bp, state->args);
1003 trace_xfs_attr_leaf_add_new(state->args);
1004 error = xfs_attr_leaf_add(newblk->bp, state->args);
1008 * Update last hashval in each block since we added the name.
1010 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1011 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1016 * Add a name to the leaf attribute list structure.
1021 struct xfs_da_args *args)
1023 xfs_attr_leafblock_t *leaf;
1024 xfs_attr_leaf_hdr_t *hdr;
1025 xfs_attr_leaf_map_t *map;
1026 int tablesize, entsize, sum, tmp, i;
1028 trace_xfs_attr_leaf_add(args);
1031 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1032 ASSERT((args->index >= 0)
1033 && (args->index <= be16_to_cpu(leaf->hdr.count)));
1035 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1036 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1039 * Search through freemap for first-fit on new name length.
1040 * (may need to figure in size of entry struct too)
1042 tablesize = (be16_to_cpu(hdr->count) + 1)
1043 * sizeof(xfs_attr_leaf_entry_t)
1044 + sizeof(xfs_attr_leaf_hdr_t);
1045 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1046 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1047 if (tablesize > be16_to_cpu(hdr->firstused)) {
1048 sum += be16_to_cpu(map->size);
1052 continue; /* no space in this map */
1054 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
1055 tmp += sizeof(xfs_attr_leaf_entry_t);
1056 if (be16_to_cpu(map->size) >= tmp) {
1057 tmp = xfs_attr_leaf_add_work(bp, args, i);
1060 sum += be16_to_cpu(map->size);
1064 * If there are no holes in the address space of the block,
1065 * and we don't have enough freespace, then compaction will do us
1066 * no good and we should just give up.
1068 if (!hdr->holes && (sum < entsize))
1069 return(XFS_ERROR(ENOSPC));
1072 * Compact the entries to coalesce free space.
1073 * This may change the hdr->count via dropping INCOMPLETE entries.
1075 xfs_attr_leaf_compact(args, bp);
1078 * After compaction, the block is guaranteed to have only one
1079 * free region, in freemap[0]. If it is not big enough, give up.
1081 if (be16_to_cpu(hdr->freemap[0].size)
1082 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1083 return(XFS_ERROR(ENOSPC));
1085 return(xfs_attr_leaf_add_work(bp, args, 0));
1089 * Add a name to a leaf attribute list structure.
1092 xfs_attr_leaf_add_work(
1094 xfs_da_args_t *args,
1097 xfs_attr_leafblock_t *leaf;
1098 xfs_attr_leaf_hdr_t *hdr;
1099 xfs_attr_leaf_entry_t *entry;
1100 xfs_attr_leaf_name_local_t *name_loc;
1101 xfs_attr_leaf_name_remote_t *name_rmt;
1102 xfs_attr_leaf_map_t *map;
1106 trace_xfs_attr_leaf_add_work(args);
1109 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1111 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1112 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
1115 * Force open some space in the entry array and fill it in.
1117 entry = &leaf->entries[args->index];
1118 if (args->index < be16_to_cpu(hdr->count)) {
1119 tmp = be16_to_cpu(hdr->count) - args->index;
1120 tmp *= sizeof(xfs_attr_leaf_entry_t);
1121 memmove((char *)(entry+1), (char *)entry, tmp);
1122 xfs_trans_log_buf(args->trans, bp,
1123 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1125 be16_add_cpu(&hdr->count, 1);
1128 * Allocate space for the new string (at the end of the run).
1130 map = &hdr->freemap[mapindex];
1131 mp = args->trans->t_mountp;
1132 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1133 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1134 ASSERT(be16_to_cpu(map->size) >=
1135 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1136 mp->m_sb.sb_blocksize, NULL));
1137 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1138 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1139 be16_add_cpu(&map->size,
1140 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1141 mp->m_sb.sb_blocksize, &tmp));
1142 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1143 be16_to_cpu(map->size));
1144 entry->hashval = cpu_to_be32(args->hashval);
1145 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1146 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1147 if (args->op_flags & XFS_DA_OP_RENAME) {
1148 entry->flags |= XFS_ATTR_INCOMPLETE;
1149 if ((args->blkno2 == args->blkno) &&
1150 (args->index2 <= args->index)) {
1154 xfs_trans_log_buf(args->trans, bp,
1155 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1156 ASSERT((args->index == 0) ||
1157 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1158 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
1159 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1162 * For "remote" attribute values, simply note that we need to
1163 * allocate space for the "remote" value. We can't actually
1164 * allocate the extents in this transaction, and we can't decide
1165 * which blocks they should be as we might allocate more blocks
1166 * as part of this transaction (a split operation for example).
1168 if (entry->flags & XFS_ATTR_LOCAL) {
1169 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
1170 name_loc->namelen = args->namelen;
1171 name_loc->valuelen = cpu_to_be16(args->valuelen);
1172 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1173 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1174 be16_to_cpu(name_loc->valuelen));
1176 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
1177 name_rmt->namelen = args->namelen;
1178 memcpy((char *)name_rmt->name, args->name, args->namelen);
1179 entry->flags |= XFS_ATTR_INCOMPLETE;
1181 name_rmt->valuelen = 0;
1182 name_rmt->valueblk = 0;
1184 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1186 xfs_trans_log_buf(args->trans, bp,
1187 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1188 xfs_attr_leaf_entsize(leaf, args->index)));
1191 * Update the control info for this leaf node
1193 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
1194 /* both on-disk, don't endian-flip twice */
1195 hdr->firstused = entry->nameidx;
1197 ASSERT(be16_to_cpu(hdr->firstused) >=
1198 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1199 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
1200 + sizeof(xfs_attr_leaf_hdr_t);
1201 map = &hdr->freemap[0];
1202 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1203 if (be16_to_cpu(map->base) == tmp) {
1204 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1205 be16_add_cpu(&map->size,
1206 -((int)sizeof(xfs_attr_leaf_entry_t)));
1209 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
1210 xfs_trans_log_buf(args->trans, bp,
1211 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1216 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1219 xfs_attr_leaf_compact(
1220 struct xfs_da_args *args,
1223 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1224 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1225 struct xfs_trans *trans = args->trans;
1226 struct xfs_mount *mp = trans->t_mountp;
1229 trace_xfs_attr_leaf_compact(args);
1231 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1232 ASSERT(tmpbuffer != NULL);
1233 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1234 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
1237 * Copy basic information
1239 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1240 leaf_d = bp->b_addr;
1241 hdr_s = &leaf_s->hdr;
1242 hdr_d = &leaf_d->hdr;
1243 hdr_d->info = hdr_s->info; /* struct copy */
1244 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
1245 /* handle truncation gracefully */
1246 if (!hdr_d->firstused) {
1247 hdr_d->firstused = cpu_to_be16(
1248 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1250 hdr_d->usedbytes = 0;
1253 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1254 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1255 sizeof(xfs_attr_leaf_hdr_t));
1258 * Copy all entry's in the same (sorted) order,
1259 * but allocate name/value pairs packed and in sequence.
1261 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1262 be16_to_cpu(hdr_s->count), mp);
1263 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1265 kmem_free(tmpbuffer);
1269 * Redistribute the attribute list entries between two leaf nodes,
1270 * taking into account the size of the new entry.
1272 * NOTE: if new block is empty, then it will get the upper half of the
1273 * old block. At present, all (one) callers pass in an empty second block.
1275 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1276 * to match what it is doing in splitting the attribute leaf block. Those
1277 * values are used in "atomic rename" operations on attributes. Note that
1278 * the "new" and "old" values can end up in different blocks.
1281 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1282 xfs_da_state_blk_t *blk2)
1284 xfs_da_args_t *args;
1285 xfs_da_state_blk_t *tmp_blk;
1286 xfs_attr_leafblock_t *leaf1, *leaf2;
1287 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1288 int count, totallen, max, space, swap;
1291 * Set up environment.
1293 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1294 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1295 leaf1 = blk1->bp->b_addr;
1296 leaf2 = blk2->bp->b_addr;
1297 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1298 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1299 ASSERT(leaf2->hdr.count == 0);
1302 trace_xfs_attr_leaf_rebalance(args);
1305 * Check ordering of blocks, reverse if it makes things simpler.
1307 * NOTE: Given that all (current) callers pass in an empty
1308 * second block, this code should never set "swap".
1311 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1315 leaf1 = blk1->bp->b_addr;
1316 leaf2 = blk2->bp->b_addr;
1323 * Examine entries until we reduce the absolute difference in
1324 * byte usage between the two blocks to a minimum. Then get
1325 * the direction to copy and the number of elements to move.
1327 * "inleaf" is true if the new entry should be inserted into blk1.
1328 * If "swap" is also true, then reverse the sense of "inleaf".
1330 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1333 state->inleaf = !state->inleaf;
1336 * Move any entries required from leaf to leaf:
1338 if (count < be16_to_cpu(hdr1->count)) {
1340 * Figure the total bytes to be added to the destination leaf.
1342 /* number entries being moved */
1343 count = be16_to_cpu(hdr1->count) - count;
1344 space = be16_to_cpu(hdr1->usedbytes) - totallen;
1345 space += count * sizeof(xfs_attr_leaf_entry_t);
1348 * leaf2 is the destination, compact it if it looks tight.
1350 max = be16_to_cpu(hdr2->firstused)
1351 - sizeof(xfs_attr_leaf_hdr_t);
1352 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
1354 xfs_attr_leaf_compact(args, blk2->bp);
1357 * Move high entries from leaf1 to low end of leaf2.
1359 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
1360 leaf2, 0, count, state->mp);
1362 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1363 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1364 } else if (count > be16_to_cpu(hdr1->count)) {
1366 * I assert that since all callers pass in an empty
1367 * second buffer, this code should never execute.
1372 * Figure the total bytes to be added to the destination leaf.
1374 /* number entries being moved */
1375 count -= be16_to_cpu(hdr1->count);
1376 space = totallen - be16_to_cpu(hdr1->usedbytes);
1377 space += count * sizeof(xfs_attr_leaf_entry_t);
1380 * leaf1 is the destination, compact it if it looks tight.
1382 max = be16_to_cpu(hdr1->firstused)
1383 - sizeof(xfs_attr_leaf_hdr_t);
1384 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
1386 xfs_attr_leaf_compact(args, blk1->bp);
1389 * Move low entries from leaf2 to high end of leaf1.
1391 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1392 be16_to_cpu(hdr1->count), count, state->mp);
1394 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1395 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1399 * Copy out last hashval in each block for B-tree code.
1401 blk1->hashval = be32_to_cpu(
1402 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1403 blk2->hashval = be32_to_cpu(
1404 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
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 > be16_to_cpu(leaf1->hdr.count)) {
1419 ASSERT(state->inleaf == 0);
1420 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
1421 args->index = args->index2 = blk2->index;
1422 args->blkno = args->blkno2 = blk2->blkno;
1423 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
1424 if (state->inleaf) {
1425 args->index = blk1->index;
1426 args->blkno = blk1->blkno;
1428 args->blkno2 = blk2->blkno;
1431 * On a double leaf split, the original attr location
1432 * is already stored in blkno2/index2, so don't
1433 * overwrite it overwise we corrupt the tree.
1435 blk2->index = blk1->index
1436 - be16_to_cpu(leaf1->hdr.count);
1437 args->index = blk2->index;
1438 args->blkno = blk2->blkno;
1439 if (!state->extravalid) {
1441 * set the new attr location to match the old
1442 * one and let the higher level split code
1443 * decide where in the leaf to place it.
1445 args->index2 = blk2->index;
1446 args->blkno2 = blk2->blkno;
1450 ASSERT(state->inleaf == 1);
1451 args->index = args->index2 = blk1->index;
1452 args->blkno = args->blkno2 = blk1->blkno;
1457 * Examine entries until we reduce the absolute difference in
1458 * byte usage between the two blocks to a minimum.
1459 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1460 * GROT: there will always be enough room in either block for a new entry.
1461 * GROT: Do a double-split for this case?
1464 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1465 xfs_da_state_blk_t *blk1,
1466 xfs_da_state_blk_t *blk2,
1467 int *countarg, int *usedbytesarg)
1469 xfs_attr_leafblock_t *leaf1, *leaf2;
1470 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1471 xfs_attr_leaf_entry_t *entry;
1472 int count, max, index, totallen, half;
1473 int lastdelta, foundit, tmp;
1476 * Set up environment.
1478 leaf1 = blk1->bp->b_addr;
1479 leaf2 = blk2->bp->b_addr;
1486 * Examine entries until we reduce the absolute difference in
1487 * byte usage between the two blocks to a minimum.
1489 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
1490 half = (max+1) * sizeof(*entry);
1491 half += be16_to_cpu(hdr1->usedbytes) +
1492 be16_to_cpu(hdr2->usedbytes) +
1493 xfs_attr_leaf_newentsize(
1494 state->args->namelen,
1495 state->args->valuelen,
1496 state->blocksize, NULL);
1498 lastdelta = state->blocksize;
1499 entry = &leaf1->entries[0];
1500 for (count = index = 0; count < max; entry++, index++, count++) {
1502 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1504 * The new entry is in the first block, account for it.
1506 if (count == blk1->index) {
1507 tmp = totallen + sizeof(*entry) +
1508 xfs_attr_leaf_newentsize(
1509 state->args->namelen,
1510 state->args->valuelen,
1511 state->blocksize, NULL);
1512 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1514 lastdelta = XFS_ATTR_ABS(half - tmp);
1520 * Wrap around into the second block if necessary.
1522 if (count == be16_to_cpu(hdr1->count)) {
1524 entry = &leaf1->entries[0];
1529 * Figure out if next leaf entry would be too much.
1531 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1533 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1535 lastdelta = XFS_ATTR_ABS(half - tmp);
1541 * Calculate the number of usedbytes that will end up in lower block.
1542 * If new entry not in lower block, fix up the count.
1544 totallen -= count * sizeof(*entry);
1546 totallen -= sizeof(*entry) +
1547 xfs_attr_leaf_newentsize(
1548 state->args->namelen,
1549 state->args->valuelen,
1550 state->blocksize, NULL);
1554 *usedbytesarg = totallen;
1558 /*========================================================================
1559 * Routines used for shrinking the Btree.
1560 *========================================================================*/
1563 * Check a leaf block and its neighbors to see if the block should be
1564 * collapsed into one or the other neighbor. Always keep the block
1565 * with the smaller block number.
1566 * If the current block is over 50% full, don't try to join it, return 0.
1567 * If the block is empty, fill in the state structure and return 2.
1568 * If it can be collapsed, fill in the state structure and return 1.
1569 * If nothing can be done, return 0.
1571 * GROT: allow for INCOMPLETE entries in calculation.
1574 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1576 xfs_attr_leafblock_t *leaf;
1577 xfs_da_state_blk_t *blk;
1578 xfs_da_blkinfo_t *info;
1579 int count, bytes, forward, error, retval, i;
1583 trace_xfs_attr_leaf_toosmall(state->args);
1586 * Check for the degenerate case of the block being over 50% full.
1587 * If so, it's not worth even looking to see if we might be able
1588 * to coalesce with a sibling.
1590 blk = &state->path.blk[ state->path.active-1 ];
1591 info = blk->bp->b_addr;
1592 ASSERT(info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1593 leaf = (xfs_attr_leafblock_t *)info;
1594 count = be16_to_cpu(leaf->hdr.count);
1595 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1596 count * sizeof(xfs_attr_leaf_entry_t) +
1597 be16_to_cpu(leaf->hdr.usedbytes);
1598 if (bytes > (state->blocksize >> 1)) {
1599 *action = 0; /* blk over 50%, don't try to join */
1604 * Check for the degenerate case of the block being empty.
1605 * If the block is empty, we'll simply delete it, no need to
1606 * coalesce it with a sibling block. We choose (arbitrarily)
1607 * to merge with the forward block unless it is NULL.
1611 * Make altpath point to the block we want to keep and
1612 * path point to the block we want to drop (this one).
1614 forward = (info->forw != 0);
1615 memcpy(&state->altpath, &state->path, sizeof(state->path));
1616 error = xfs_da_path_shift(state, &state->altpath, forward,
1629 * Examine each sibling block to see if we can coalesce with
1630 * at least 25% free space to spare. We need to figure out
1631 * whether to merge with the forward or the backward block.
1632 * We prefer coalescing with the lower numbered sibling so as
1633 * to shrink an attribute list over time.
1635 /* start with smaller blk num */
1636 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
1637 for (i = 0; i < 2; forward = !forward, i++) {
1639 blkno = be32_to_cpu(info->forw);
1641 blkno = be32_to_cpu(info->back);
1644 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1645 blkno, -1, &bp, XFS_ATTR_FORK);
1650 leaf = (xfs_attr_leafblock_t *)info;
1651 count = be16_to_cpu(leaf->hdr.count);
1652 bytes = state->blocksize - (state->blocksize>>2);
1653 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1655 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1656 count += be16_to_cpu(leaf->hdr.count);
1657 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1658 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1659 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1660 xfs_trans_brelse(state->args->trans, bp);
1662 break; /* fits with at least 25% to spare */
1670 * Make altpath point to the block we want to keep (the lower
1671 * numbered block) and path point to the block we want to drop.
1673 memcpy(&state->altpath, &state->path, sizeof(state->path));
1674 if (blkno < blk->blkno) {
1675 error = xfs_da_path_shift(state, &state->altpath, forward,
1678 error = xfs_da_path_shift(state, &state->path, forward,
1692 * Remove a name from the leaf attribute list structure.
1694 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1695 * If two leaves are 37% full, when combined they will leave 25% free.
1698 xfs_attr_leaf_remove(
1700 xfs_da_args_t *args)
1702 xfs_attr_leafblock_t *leaf;
1703 xfs_attr_leaf_hdr_t *hdr;
1704 xfs_attr_leaf_map_t *map;
1705 xfs_attr_leaf_entry_t *entry;
1706 int before, after, smallest, entsize;
1707 int tablesize, tmp, i;
1710 trace_xfs_attr_leaf_remove(args);
1713 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1715 mp = args->trans->t_mountp;
1716 ASSERT((be16_to_cpu(hdr->count) > 0)
1717 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
1718 ASSERT((args->index >= 0)
1719 && (args->index < be16_to_cpu(hdr->count)));
1720 ASSERT(be16_to_cpu(hdr->firstused) >=
1721 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1722 entry = &leaf->entries[args->index];
1723 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1724 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1727 * Scan through free region table:
1728 * check for adjacency of free'd entry with an existing one,
1729 * find smallest free region in case we need to replace it,
1730 * adjust any map that borders the entry table,
1732 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
1733 + sizeof(xfs_attr_leaf_hdr_t);
1734 map = &hdr->freemap[0];
1735 tmp = be16_to_cpu(map->size);
1736 before = after = -1;
1737 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1738 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1739 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1740 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1741 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1742 if (be16_to_cpu(map->base) == tablesize) {
1743 be16_add_cpu(&map->base,
1744 -((int)sizeof(xfs_attr_leaf_entry_t)));
1745 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
1748 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
1749 == be16_to_cpu(entry->nameidx)) {
1751 } else if (be16_to_cpu(map->base)
1752 == (be16_to_cpu(entry->nameidx) + entsize)) {
1754 } else if (be16_to_cpu(map->size) < tmp) {
1755 tmp = be16_to_cpu(map->size);
1761 * Coalesce adjacent freemap regions,
1762 * or replace the smallest region.
1764 if ((before >= 0) || (after >= 0)) {
1765 if ((before >= 0) && (after >= 0)) {
1766 map = &hdr->freemap[before];
1767 be16_add_cpu(&map->size, entsize);
1768 be16_add_cpu(&map->size,
1769 be16_to_cpu(hdr->freemap[after].size));
1770 hdr->freemap[after].base = 0;
1771 hdr->freemap[after].size = 0;
1772 } else if (before >= 0) {
1773 map = &hdr->freemap[before];
1774 be16_add_cpu(&map->size, entsize);
1776 map = &hdr->freemap[after];
1777 /* both on-disk, don't endian flip twice */
1778 map->base = entry->nameidx;
1779 be16_add_cpu(&map->size, entsize);
1783 * Replace smallest region (if it is smaller than free'd entry)
1785 map = &hdr->freemap[smallest];
1786 if (be16_to_cpu(map->size) < entsize) {
1787 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
1788 map->size = cpu_to_be16(entsize);
1793 * Did we remove the first entry?
1795 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
1801 * Compress the remaining entries and zero out the removed stuff.
1803 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
1804 be16_add_cpu(&hdr->usedbytes, -entsize);
1805 xfs_trans_log_buf(args->trans, bp,
1806 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1809 tmp = (be16_to_cpu(hdr->count) - args->index)
1810 * sizeof(xfs_attr_leaf_entry_t);
1811 memmove((char *)entry, (char *)(entry+1), tmp);
1812 be16_add_cpu(&hdr->count, -1);
1813 xfs_trans_log_buf(args->trans, bp,
1814 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1815 entry = &leaf->entries[be16_to_cpu(hdr->count)];
1816 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1819 * If we removed the first entry, re-find the first used byte
1820 * in the name area. Note that if the entry was the "firstused",
1821 * then we don't have a "hole" in our block resulting from
1822 * removing the name.
1825 tmp = XFS_LBSIZE(mp);
1826 entry = &leaf->entries[0];
1827 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
1828 ASSERT(be16_to_cpu(entry->nameidx) >=
1829 be16_to_cpu(hdr->firstused));
1830 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1832 if (be16_to_cpu(entry->nameidx) < tmp)
1833 tmp = be16_to_cpu(entry->nameidx);
1835 hdr->firstused = cpu_to_be16(tmp);
1836 if (!hdr->firstused) {
1837 hdr->firstused = cpu_to_be16(
1838 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1841 hdr->holes = 1; /* mark as needing compaction */
1843 xfs_trans_log_buf(args->trans, bp,
1844 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1847 * Check if leaf is less than 50% full, caller may want to
1848 * "join" the leaf with a sibling if so.
1850 tmp = sizeof(xfs_attr_leaf_hdr_t);
1851 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1852 tmp += be16_to_cpu(leaf->hdr.usedbytes);
1853 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1857 * Move all the attribute list entries from drop_leaf into save_leaf.
1860 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1861 xfs_da_state_blk_t *save_blk)
1863 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1864 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1868 trace_xfs_attr_leaf_unbalance(state->args);
1871 * Set up environment.
1874 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1875 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1876 drop_leaf = drop_blk->bp->b_addr;
1877 save_leaf = save_blk->bp->b_addr;
1878 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1879 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1880 drop_hdr = &drop_leaf->hdr;
1881 save_hdr = &save_leaf->hdr;
1884 * Save last hashval from dying block for later Btree fixup.
1886 drop_blk->hashval = be32_to_cpu(
1887 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
1890 * Check if we need a temp buffer, or can we do it in place.
1891 * Note that we don't check "leaf" for holes because we will
1892 * always be dropping it, toosmall() decided that for us already.
1894 if (save_hdr->holes == 0) {
1896 * dest leaf has no holes, so we add there. May need
1897 * to make some room in the entry array.
1899 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1900 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1901 be16_to_cpu(drop_hdr->count), mp);
1903 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1904 be16_to_cpu(save_hdr->count),
1905 be16_to_cpu(drop_hdr->count), mp);
1909 * Destination has holes, so we make a temporary copy
1910 * of the leaf and add them both to that.
1912 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1913 ASSERT(tmpbuffer != NULL);
1914 memset(tmpbuffer, 0, state->blocksize);
1915 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1916 tmp_hdr = &tmp_leaf->hdr;
1917 tmp_hdr->info = save_hdr->info; /* struct copy */
1919 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
1920 if (!tmp_hdr->firstused) {
1921 tmp_hdr->firstused = cpu_to_be16(
1922 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1924 tmp_hdr->usedbytes = 0;
1925 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1926 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1927 be16_to_cpu(drop_hdr->count), mp);
1928 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1929 be16_to_cpu(tmp_leaf->hdr.count),
1930 be16_to_cpu(save_hdr->count), mp);
1932 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1933 be16_to_cpu(save_hdr->count), mp);
1934 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1935 be16_to_cpu(tmp_leaf->hdr.count),
1936 be16_to_cpu(drop_hdr->count), mp);
1938 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1939 kmem_free(tmpbuffer);
1942 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
1943 state->blocksize - 1);
1946 * Copy out last hashval in each block for B-tree code.
1948 save_blk->hashval = be32_to_cpu(
1949 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
1952 /*========================================================================
1953 * Routines used for finding things in the Btree.
1954 *========================================================================*/
1957 * Look up a name in a leaf attribute list structure.
1958 * This is the internal routine, it uses the caller's buffer.
1960 * Note that duplicate keys are allowed, but only check within the
1961 * current leaf node. The Btree code must check in adjacent leaf nodes.
1963 * Return in args->index the index into the entry[] array of either
1964 * the found entry, or where the entry should have been (insert before
1967 * Don't change the args->value unless we find the attribute.
1970 xfs_attr_leaf_lookup_int(
1972 xfs_da_args_t *args)
1974 xfs_attr_leafblock_t *leaf;
1975 xfs_attr_leaf_entry_t *entry;
1976 xfs_attr_leaf_name_local_t *name_loc;
1977 xfs_attr_leaf_name_remote_t *name_rmt;
1979 xfs_dahash_t hashval;
1981 trace_xfs_attr_leaf_lookup(args);
1984 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1985 ASSERT(be16_to_cpu(leaf->hdr.count)
1986 < (XFS_LBSIZE(args->dp->i_mount)/8));
1989 * Binary search. (note: small blocks will skip this loop)
1991 hashval = args->hashval;
1992 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
1993 for (entry = &leaf->entries[probe]; span > 4;
1994 entry = &leaf->entries[probe]) {
1996 if (be32_to_cpu(entry->hashval) < hashval)
1998 else if (be32_to_cpu(entry->hashval) > hashval)
2003 ASSERT((probe >= 0) &&
2005 || (probe < be16_to_cpu(leaf->hdr.count))));
2006 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
2009 * Since we may have duplicate hashval's, find the first matching
2010 * hashval in the leaf.
2012 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
2016 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
2017 (be32_to_cpu(entry->hashval) < hashval)) {
2021 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2022 (be32_to_cpu(entry->hashval) != hashval)) {
2023 args->index = probe;
2024 return(XFS_ERROR(ENOATTR));
2028 * Duplicate keys may be present, so search all of them for a match.
2030 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2031 (be32_to_cpu(entry->hashval) == hashval);
2034 * GROT: Add code to remove incomplete entries.
2037 * If we are looking for INCOMPLETE entries, show only those.
2038 * If we are looking for complete entries, show only those.
2040 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2041 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2044 if (entry->flags & XFS_ATTR_LOCAL) {
2045 name_loc = xfs_attr_leaf_name_local(leaf, probe);
2046 if (name_loc->namelen != args->namelen)
2048 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
2050 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2052 args->index = probe;
2053 return(XFS_ERROR(EEXIST));
2055 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
2056 if (name_rmt->namelen != args->namelen)
2058 if (memcmp(args->name, (char *)name_rmt->name,
2059 args->namelen) != 0)
2061 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2063 args->index = probe;
2064 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2065 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2066 be32_to_cpu(name_rmt->valuelen));
2067 return(XFS_ERROR(EEXIST));
2070 args->index = probe;
2071 return(XFS_ERROR(ENOATTR));
2075 * Get the value associated with an attribute name from a leaf attribute
2079 xfs_attr_leaf_getvalue(
2081 xfs_da_args_t *args)
2084 xfs_attr_leafblock_t *leaf;
2085 xfs_attr_leaf_entry_t *entry;
2086 xfs_attr_leaf_name_local_t *name_loc;
2087 xfs_attr_leaf_name_remote_t *name_rmt;
2090 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2091 ASSERT(be16_to_cpu(leaf->hdr.count)
2092 < (XFS_LBSIZE(args->dp->i_mount)/8));
2093 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2095 entry = &leaf->entries[args->index];
2096 if (entry->flags & XFS_ATTR_LOCAL) {
2097 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2098 ASSERT(name_loc->namelen == args->namelen);
2099 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2100 valuelen = be16_to_cpu(name_loc->valuelen);
2101 if (args->flags & ATTR_KERNOVAL) {
2102 args->valuelen = valuelen;
2105 if (args->valuelen < valuelen) {
2106 args->valuelen = valuelen;
2107 return(XFS_ERROR(ERANGE));
2109 args->valuelen = valuelen;
2110 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2112 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2113 ASSERT(name_rmt->namelen == args->namelen);
2114 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2115 valuelen = be32_to_cpu(name_rmt->valuelen);
2116 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2117 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2118 if (args->flags & ATTR_KERNOVAL) {
2119 args->valuelen = valuelen;
2122 if (args->valuelen < valuelen) {
2123 args->valuelen = valuelen;
2124 return(XFS_ERROR(ERANGE));
2126 args->valuelen = valuelen;
2131 /*========================================================================
2133 *========================================================================*/
2136 * Move the indicated entries from one leaf to another.
2137 * NOTE: this routine modifies both source and destination leaves.
2141 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2142 xfs_attr_leafblock_t *leaf_d, int start_d,
2143 int count, xfs_mount_t *mp)
2145 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2146 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2150 * Check for nothing to do.
2156 * Set up environment.
2158 ASSERT(leaf_s->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2159 ASSERT(leaf_d->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2160 hdr_s = &leaf_s->hdr;
2161 hdr_d = &leaf_d->hdr;
2162 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2163 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2164 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2165 ((be16_to_cpu(hdr_s->count)
2166 * sizeof(*entry_s))+sizeof(*hdr_s)));
2167 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2168 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2169 ((be16_to_cpu(hdr_d->count)
2170 * sizeof(*entry_d))+sizeof(*hdr_d)));
2172 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2173 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2174 ASSERT(count <= be16_to_cpu(hdr_s->count));
2177 * Move the entries in the destination leaf up to make a hole?
2179 if (start_d < be16_to_cpu(hdr_d->count)) {
2180 tmp = be16_to_cpu(hdr_d->count) - start_d;
2181 tmp *= sizeof(xfs_attr_leaf_entry_t);
2182 entry_s = &leaf_d->entries[start_d];
2183 entry_d = &leaf_d->entries[start_d + count];
2184 memmove((char *)entry_d, (char *)entry_s, tmp);
2188 * Copy all entry's in the same (sorted) order,
2189 * but allocate attribute info packed and in sequence.
2191 entry_s = &leaf_s->entries[start_s];
2192 entry_d = &leaf_d->entries[start_d];
2194 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2195 ASSERT(be16_to_cpu(entry_s->nameidx)
2196 >= be16_to_cpu(hdr_s->firstused));
2197 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2200 * Code to drop INCOMPLETE entries. Difficult to use as we
2201 * may also need to change the insertion index. Code turned
2202 * off for 6.2, should be revisited later.
2204 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2205 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2206 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2207 be16_add_cpu(&hdr_s->count, -1);
2208 entry_d--; /* to compensate for ++ in loop hdr */
2210 if ((start_s + i) < offset)
2211 result++; /* insertion index adjustment */
2214 be16_add_cpu(&hdr_d->firstused, -tmp);
2215 /* both on-disk, don't endian flip twice */
2216 entry_d->hashval = entry_s->hashval;
2217 /* both on-disk, don't endian flip twice */
2218 entry_d->nameidx = hdr_d->firstused;
2219 entry_d->flags = entry_s->flags;
2220 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2222 memmove(xfs_attr_leaf_name(leaf_d, desti),
2223 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
2224 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2226 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2227 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2228 be16_add_cpu(&hdr_d->usedbytes, tmp);
2229 be16_add_cpu(&hdr_s->count, -1);
2230 be16_add_cpu(&hdr_d->count, 1);
2231 tmp = be16_to_cpu(hdr_d->count)
2232 * sizeof(xfs_attr_leaf_entry_t)
2233 + sizeof(xfs_attr_leaf_hdr_t);
2234 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
2241 * Zero out the entries we just copied.
2243 if (start_s == be16_to_cpu(hdr_s->count)) {
2244 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2245 entry_s = &leaf_s->entries[start_s];
2246 ASSERT(((char *)entry_s + tmp) <=
2247 ((char *)leaf_s + XFS_LBSIZE(mp)));
2248 memset((char *)entry_s, 0, tmp);
2251 * Move the remaining entries down to fill the hole,
2252 * then zero the entries at the top.
2254 tmp = be16_to_cpu(hdr_s->count) - count;
2255 tmp *= sizeof(xfs_attr_leaf_entry_t);
2256 entry_s = &leaf_s->entries[start_s + count];
2257 entry_d = &leaf_s->entries[start_s];
2258 memmove((char *)entry_d, (char *)entry_s, tmp);
2260 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2261 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
2262 ASSERT(((char *)entry_s + tmp) <=
2263 ((char *)leaf_s + XFS_LBSIZE(mp)));
2264 memset((char *)entry_s, 0, tmp);
2268 * Fill in the freemap information
2270 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2271 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2272 sizeof(xfs_attr_leaf_entry_t));
2273 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2274 - be16_to_cpu(hdr_d->freemap[0].base));
2275 hdr_d->freemap[1].base = 0;
2276 hdr_d->freemap[2].base = 0;
2277 hdr_d->freemap[1].size = 0;
2278 hdr_d->freemap[2].size = 0;
2279 hdr_s->holes = 1; /* leaf may not be compact */
2283 * Compare two leaf blocks "order".
2284 * Return 0 unless leaf2 should go before leaf1.
2287 xfs_attr_leaf_order(
2288 struct xfs_buf *leaf1_bp,
2289 struct xfs_buf *leaf2_bp)
2291 xfs_attr_leafblock_t *leaf1, *leaf2;
2293 leaf1 = leaf1_bp->b_addr;
2294 leaf2 = leaf2_bp->b_addr;
2295 ASSERT((leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) &&
2296 (leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)));
2297 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2298 (be16_to_cpu(leaf2->hdr.count) > 0) &&
2299 ((be32_to_cpu(leaf2->entries[0].hashval) <
2300 be32_to_cpu(leaf1->entries[0].hashval)) ||
2301 (be32_to_cpu(leaf2->entries[
2302 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2303 be32_to_cpu(leaf1->entries[
2304 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
2311 * Pick up the last hashvalue from a leaf block.
2314 xfs_attr_leaf_lasthash(
2318 xfs_attr_leafblock_t *leaf;
2321 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2323 *count = be16_to_cpu(leaf->hdr.count);
2324 if (!leaf->hdr.count)
2326 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
2330 * Calculate the number of bytes used to store the indicated attribute
2331 * (whether local or remote only calculate bytes in this block).
2334 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2336 xfs_attr_leaf_name_local_t *name_loc;
2337 xfs_attr_leaf_name_remote_t *name_rmt;
2340 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2341 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2342 name_loc = xfs_attr_leaf_name_local(leaf, index);
2343 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2344 be16_to_cpu(name_loc->valuelen));
2346 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2347 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2353 * Calculate the number of bytes that would be required to store the new
2354 * attribute (whether local or remote only calculate bytes in this block).
2355 * This routine decides as a side effect whether the attribute will be
2356 * a "local" or a "remote" attribute.
2359 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2363 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2364 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
2369 size = xfs_attr_leaf_entsize_remote(namelen);
2378 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2381 xfs_attr_leaf_list_int(
2383 xfs_attr_list_context_t *context)
2385 attrlist_cursor_kern_t *cursor;
2386 xfs_attr_leafblock_t *leaf;
2387 xfs_attr_leaf_entry_t *entry;
2392 cursor = context->cursor;
2393 cursor->initted = 1;
2395 trace_xfs_attr_list_leaf(context);
2398 * Re-find our place in the leaf block if this is a new syscall.
2400 if (context->resynch) {
2401 entry = &leaf->entries[0];
2402 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2403 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
2404 if (cursor->offset == context->dupcnt) {
2405 context->dupcnt = 0;
2409 } else if (be32_to_cpu(entry->hashval) >
2411 context->dupcnt = 0;
2415 if (i == be16_to_cpu(leaf->hdr.count)) {
2416 trace_xfs_attr_list_notfound(context);
2420 entry = &leaf->entries[0];
2423 context->resynch = 0;
2426 * We have found our place, start copying out the new attributes.
2429 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
2430 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2431 cursor->hashval = be32_to_cpu(entry->hashval);
2435 if (entry->flags & XFS_ATTR_INCOMPLETE)
2436 continue; /* skip incomplete entries */
2438 if (entry->flags & XFS_ATTR_LOCAL) {
2439 xfs_attr_leaf_name_local_t *name_loc =
2440 xfs_attr_leaf_name_local(leaf, i);
2442 retval = context->put_listent(context,
2445 (int)name_loc->namelen,
2446 be16_to_cpu(name_loc->valuelen),
2447 &name_loc->nameval[name_loc->namelen]);
2451 xfs_attr_leaf_name_remote_t *name_rmt =
2452 xfs_attr_leaf_name_remote(leaf, i);
2454 int valuelen = be32_to_cpu(name_rmt->valuelen);
2456 if (context->put_value) {
2459 memset((char *)&args, 0, sizeof(args));
2460 args.dp = context->dp;
2461 args.whichfork = XFS_ATTR_FORK;
2462 args.valuelen = valuelen;
2463 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
2464 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2465 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2466 retval = xfs_attr_rmtval_get(&args);
2469 retval = context->put_listent(context,
2472 (int)name_rmt->namelen,
2475 kmem_free(args.value);
2477 retval = context->put_listent(context,
2480 (int)name_rmt->namelen,
2487 if (context->seen_enough)
2491 trace_xfs_attr_list_leaf_end(context);
2496 /*========================================================================
2497 * Manage the INCOMPLETE flag in a leaf entry
2498 *========================================================================*/
2501 * Clear the INCOMPLETE flag on an entry in a leaf block.
2504 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2506 xfs_attr_leafblock_t *leaf;
2507 xfs_attr_leaf_entry_t *entry;
2508 xfs_attr_leaf_name_remote_t *name_rmt;
2512 xfs_attr_leaf_name_local_t *name_loc;
2517 trace_xfs_attr_leaf_clearflag(args);
2519 * Set up the operation.
2521 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2529 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2530 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2531 ASSERT(args->index >= 0);
2532 entry = &leaf->entries[ args->index ];
2533 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2536 if (entry->flags & XFS_ATTR_LOCAL) {
2537 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2538 namelen = name_loc->namelen;
2539 name = (char *)name_loc->nameval;
2541 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2542 namelen = name_rmt->namelen;
2543 name = (char *)name_rmt->name;
2545 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2546 ASSERT(namelen == args->namelen);
2547 ASSERT(memcmp(name, args->name, namelen) == 0);
2550 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2551 xfs_trans_log_buf(args->trans, bp,
2552 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2554 if (args->rmtblkno) {
2555 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2556 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2557 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2558 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2559 xfs_trans_log_buf(args->trans, bp,
2560 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2564 * Commit the flag value change and start the next trans in series.
2566 return xfs_trans_roll(&args->trans, args->dp);
2570 * Set the INCOMPLETE flag on an entry in a leaf block.
2573 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2575 xfs_attr_leafblock_t *leaf;
2576 xfs_attr_leaf_entry_t *entry;
2577 xfs_attr_leaf_name_remote_t *name_rmt;
2581 trace_xfs_attr_leaf_setflag(args);
2584 * Set up the operation.
2586 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2594 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2595 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2596 ASSERT(args->index >= 0);
2597 entry = &leaf->entries[ args->index ];
2599 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2600 entry->flags |= XFS_ATTR_INCOMPLETE;
2601 xfs_trans_log_buf(args->trans, bp,
2602 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2603 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2604 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2605 name_rmt->valueblk = 0;
2606 name_rmt->valuelen = 0;
2607 xfs_trans_log_buf(args->trans, bp,
2608 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2612 * Commit the flag value change and start the next trans in series.
2614 return xfs_trans_roll(&args->trans, args->dp);
2618 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2619 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2620 * entry given by args->blkno2/index2.
2622 * Note that they could be in different blocks, or in the same block.
2625 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2627 xfs_attr_leafblock_t *leaf1, *leaf2;
2628 xfs_attr_leaf_entry_t *entry1, *entry2;
2629 xfs_attr_leaf_name_remote_t *name_rmt;
2630 struct xfs_buf *bp1, *bp2;
2633 xfs_attr_leaf_name_local_t *name_loc;
2634 int namelen1, namelen2;
2635 char *name1, *name2;
2638 trace_xfs_attr_leaf_flipflags(args);
2641 * Read the block containing the "old" attr
2643 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2648 ASSERT(bp1 != NULL);
2651 * Read the block containing the "new" attr, if it is different
2653 if (args->blkno2 != args->blkno) {
2654 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2655 -1, &bp2, XFS_ATTR_FORK);
2659 ASSERT(bp2 != NULL);
2664 leaf1 = bp1->b_addr;
2665 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2666 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
2667 ASSERT(args->index >= 0);
2668 entry1 = &leaf1->entries[ args->index ];
2670 leaf2 = bp2->b_addr;
2671 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2672 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
2673 ASSERT(args->index2 >= 0);
2674 entry2 = &leaf2->entries[ args->index2 ];
2677 if (entry1->flags & XFS_ATTR_LOCAL) {
2678 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
2679 namelen1 = name_loc->namelen;
2680 name1 = (char *)name_loc->nameval;
2682 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2683 namelen1 = name_rmt->namelen;
2684 name1 = (char *)name_rmt->name;
2686 if (entry2->flags & XFS_ATTR_LOCAL) {
2687 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
2688 namelen2 = name_loc->namelen;
2689 name2 = (char *)name_loc->nameval;
2691 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2692 namelen2 = name_rmt->namelen;
2693 name2 = (char *)name_rmt->name;
2695 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2696 ASSERT(namelen1 == namelen2);
2697 ASSERT(memcmp(name1, name2, namelen1) == 0);
2700 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2701 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2703 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2704 xfs_trans_log_buf(args->trans, bp1,
2705 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2706 if (args->rmtblkno) {
2707 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2708 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2709 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2710 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2711 xfs_trans_log_buf(args->trans, bp1,
2712 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2715 entry2->flags |= XFS_ATTR_INCOMPLETE;
2716 xfs_trans_log_buf(args->trans, bp2,
2717 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2718 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2719 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2720 name_rmt->valueblk = 0;
2721 name_rmt->valuelen = 0;
2722 xfs_trans_log_buf(args->trans, bp2,
2723 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2727 * Commit the flag value change and start the next trans in series.
2729 error = xfs_trans_roll(&args->trans, args->dp);
2734 /*========================================================================
2735 * Indiscriminately delete the entire attribute fork
2736 *========================================================================*/
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_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2745 xfs_da_blkinfo_t *info;
2751 * Read block 0 to see what we have to work with.
2752 * We only get here if we have extents, since we remove
2753 * the extents in reverse order the extent containing
2754 * block 0 must still be there.
2756 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2759 blkno = XFS_BUF_ADDR(bp);
2762 * Invalidate the tree, even if the "tree" is only a single leaf block.
2763 * This is a depth-first traversal!
2766 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
2767 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2768 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
2769 error = xfs_attr_leaf_inactive(trans, dp, bp);
2771 error = XFS_ERROR(EIO);
2772 xfs_trans_brelse(*trans, bp);
2778 * Invalidate the incore copy of the root block.
2780 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2783 xfs_trans_binval(*trans, bp); /* remove from cache */
2785 * Commit the invalidate and start the next transaction.
2787 error = xfs_trans_roll(trans, dp);
2793 * Recurse (gasp!) through the attribute nodes until we find leaves.
2794 * We're doing a depth-first traversal in order to invalidate everything.
2797 xfs_attr_node_inactive(
2798 struct xfs_trans **trans,
2799 struct xfs_inode *dp,
2803 xfs_da_blkinfo_t *info;
2804 xfs_da_intnode_t *node;
2805 xfs_dablk_t child_fsb;
2806 xfs_daddr_t parent_blkno, child_blkno;
2807 int error, count, i;
2808 struct xfs_buf *child_bp;
2811 * Since this code is recursive (gasp!) we must protect ourselves.
2813 if (level > XFS_DA_NODE_MAXDEPTH) {
2814 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
2815 return(XFS_ERROR(EIO));
2819 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
2820 parent_blkno = XFS_BUF_ADDR(bp); /* save for re-read later */
2821 count = be16_to_cpu(node->hdr.count);
2823 xfs_trans_brelse(*trans, bp);
2826 child_fsb = be32_to_cpu(node->btree[0].before);
2827 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
2830 * If this is the node level just above the leaves, simply loop
2831 * over the leaves removing all of them. If this is higher up
2832 * in the tree, recurse downward.
2834 for (i = 0; i < count; i++) {
2836 * Read the subsidiary block to see what we have to work with.
2837 * Don't do this in a transaction. This is a depth-first
2838 * traversal of the tree so we may deal with many blocks
2839 * before we come back to this one.
2841 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2846 /* save for re-read later */
2847 child_blkno = XFS_BUF_ADDR(child_bp);
2850 * Invalidate the subtree, however we have to.
2852 info = child_bp->b_addr;
2853 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
2854 error = xfs_attr_node_inactive(trans, dp,
2856 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
2857 error = xfs_attr_leaf_inactive(trans, dp,
2860 error = XFS_ERROR(EIO);
2861 xfs_trans_brelse(*trans, child_bp);
2867 * Remove the subsidiary block from the cache
2870 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2871 &child_bp, XFS_ATTR_FORK);
2874 xfs_trans_binval(*trans, child_bp);
2878 * If we're not done, re-read the parent to get the next
2879 * child block number.
2881 if ((i+1) < count) {
2882 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2883 &bp, XFS_ATTR_FORK);
2886 child_fsb = be32_to_cpu(node->btree[i+1].before);
2887 xfs_trans_brelse(*trans, bp);
2890 * Atomically commit the whole invalidate stuff.
2892 error = xfs_trans_roll(trans, dp);
2901 * Invalidate all of the "remote" value regions pointed to by a particular
2903 * Note that we must release the lock on the buffer so that we are not
2904 * caught holding something that the logging code wants to flush to disk.
2907 xfs_attr_leaf_inactive(
2908 struct xfs_trans **trans,
2909 struct xfs_inode *dp,
2912 xfs_attr_leafblock_t *leaf;
2913 xfs_attr_leaf_entry_t *entry;
2914 xfs_attr_leaf_name_remote_t *name_rmt;
2915 xfs_attr_inactive_list_t *list, *lp;
2916 int error, count, size, tmp, i;
2919 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2922 * Count the number of "remote" value extents.
2925 entry = &leaf->entries[0];
2926 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2927 if (be16_to_cpu(entry->nameidx) &&
2928 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2929 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2930 if (name_rmt->valueblk)
2936 * If there are no "remote" values, we're done.
2939 xfs_trans_brelse(*trans, bp);
2944 * Allocate storage for a list of all the "remote" value extents.
2946 size = count * sizeof(xfs_attr_inactive_list_t);
2947 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2950 * Identify each of the "remote" value extents.
2953 entry = &leaf->entries[0];
2954 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2955 if (be16_to_cpu(entry->nameidx) &&
2956 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2957 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2958 if (name_rmt->valueblk) {
2959 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2960 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2961 be32_to_cpu(name_rmt->valuelen));
2966 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
2969 * Invalidate each of the "remote" value extents.
2972 for (lp = list, i = 0; i < count; i++, lp++) {
2973 tmp = xfs_attr_leaf_freextent(trans, dp,
2974 lp->valueblk, lp->valuelen);
2977 error = tmp; /* save only the 1st errno */
2980 kmem_free((xfs_caddr_t)list);
2985 * Look at all the extents for this logical region,
2986 * invalidate any buffers that are incore/in transactions.
2989 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2990 xfs_dablk_t blkno, int blkcnt)
2992 xfs_bmbt_irec_t map;
2994 int tblkcnt, dblkcnt, nmap, error;
2999 * Roll through the "value", invalidating the attribute value's
3004 while (tblkcnt > 0) {
3006 * Try to remember where we decided to put the value.
3009 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3010 &map, &nmap, XFS_BMAPI_ATTRFORK);
3015 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3018 * If it's a hole, these are already unmapped
3019 * so there's nothing to invalidate.
3021 if (map.br_startblock != HOLESTARTBLOCK) {
3023 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3025 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3027 bp = xfs_trans_get_buf(*trans,
3028 dp->i_mount->m_ddev_targp,
3029 dblkno, dblkcnt, 0);
3032 xfs_trans_binval(*trans, bp);
3034 * Roll to next transaction.
3036 error = xfs_trans_roll(trans, dp);
3041 tblkno += map.br_blockcount;
3042 tblkcnt -= map.br_blockcount;