2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_alloc.h"
35 #include "xfs_btree.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_inode_item.h"
43 #include "xfs_attr_leaf.h"
44 #include "xfs_error.h"
49 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
52 /*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
57 * Routines used for growing the Btree.
59 STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
61 STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
63 STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
64 STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
65 xfs_da_state_blk_t *blk1,
66 xfs_da_state_blk_t *blk2);
67 STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
68 xfs_da_state_blk_t *leaf_blk_1,
69 xfs_da_state_blk_t *leaf_blk_2,
70 int *number_entries_in_blk1,
71 int *number_usedbytes_in_blk1);
74 * Routines used for shrinking the Btree.
76 STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
77 xfs_dabuf_t *bp, int level);
78 STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
80 STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
81 xfs_dablk_t blkno, int blkcnt);
86 STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
88 xfs_attr_leafblock_t *dst_leaf,
89 int dst_start, int move_count,
91 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
93 /*========================================================================
94 * Namespace helper routines
95 *========================================================================*/
98 * If namespace bits don't match return 0.
99 * If all match then return 1.
102 xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
104 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
108 /*========================================================================
109 * External routines when attribute fork size < XFS_LITINO(mp).
110 *========================================================================*/
113 * Query whether the requested number of additional bytes of extended
114 * attribute space will be able to fit inline.
115 * Returns zero if not, else the di_forkoff fork offset to be used in the
116 * literal area for attribute data once the new bytes have been added.
118 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
119 * special case for dev/uuid inodes, they have fixed size data forks.
122 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
125 int minforkoff; /* lower limit on valid forkoff locations */
126 int maxforkoff; /* upper limit on valid forkoff locations */
128 xfs_mount_t *mp = dp->i_mount;
130 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
132 switch (dp->i_d.di_format) {
133 case XFS_DINODE_FMT_DEV:
134 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
135 return (offset >= minforkoff) ? minforkoff : 0;
136 case XFS_DINODE_FMT_UUID:
137 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
138 return (offset >= minforkoff) ? minforkoff : 0;
141 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
142 if (bytes <= XFS_IFORK_ASIZE(dp))
143 return dp->i_d.di_forkoff;
147 dsize = dp->i_df.if_bytes;
149 switch (dp->i_d.di_format) {
150 case XFS_DINODE_FMT_EXTENTS:
152 * If there is no attr fork and the data fork is extents,
153 * determine if creating the default attr fork will result
154 * in the extents form migrating to btree. If so, the
155 * minimum offset only needs to be the space required for
158 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes > mp->m_attroffset)
159 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
162 case XFS_DINODE_FMT_BTREE:
164 * If have data btree then keep forkoff if we have one,
165 * otherwise we are adding a new attr, so then we set
166 * minforkoff to where the btree root can finish so we have
167 * plenty of room for attrs
169 if (dp->i_d.di_forkoff) {
170 if (offset < dp->i_d.di_forkoff)
173 return dp->i_d.di_forkoff;
175 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
180 * A data fork btree root must have space for at least
181 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
183 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
184 minforkoff = roundup(minforkoff, 8) >> 3;
186 /* attr fork btree root can have at least this many key/ptr pairs */
187 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
188 maxforkoff = maxforkoff >> 3; /* rounded down */
190 if (offset >= minforkoff && offset < maxforkoff)
192 if (offset >= maxforkoff)
198 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
201 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
203 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
204 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
205 spin_lock(&mp->m_sb_lock);
206 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
207 xfs_sb_version_addattr2(&mp->m_sb);
208 spin_unlock(&mp->m_sb_lock);
209 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
211 spin_unlock(&mp->m_sb_lock);
216 * Create the initial contents of a shortform attribute list.
219 xfs_attr_shortform_create(xfs_da_args_t *args)
221 xfs_attr_sf_hdr_t *hdr;
229 ASSERT(ifp->if_bytes == 0);
230 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
231 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
232 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
233 ifp->if_flags |= XFS_IFINLINE;
235 ASSERT(ifp->if_flags & XFS_IFINLINE);
237 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
238 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
240 hdr->totsize = cpu_to_be16(sizeof(*hdr));
241 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
245 * Add a name/value pair to the shortform attribute list.
246 * Overflow from the inode has already been checked for.
249 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
251 xfs_attr_shortform_t *sf;
252 xfs_attr_sf_entry_t *sfe;
260 dp->i_d.di_forkoff = forkoff;
261 dp->i_df.if_ext_max =
262 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
263 dp->i_afp->if_ext_max =
264 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
267 ASSERT(ifp->if_flags & XFS_IFINLINE);
268 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
270 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
272 if (sfe->namelen != args->namelen)
274 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
276 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
282 offset = (char *)sfe - (char *)sf;
283 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
284 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
285 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
286 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
288 sfe->namelen = args->namelen;
289 sfe->valuelen = args->valuelen;
290 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
291 memcpy(sfe->nameval, args->name, args->namelen);
292 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
294 be16_add_cpu(&sf->hdr.totsize, size);
295 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
297 xfs_sbversion_add_attr2(mp, args->trans);
301 * After the last attribute is removed revert to original inode format,
302 * making all literal area available to the data fork once more.
306 struct xfs_inode *ip,
307 struct xfs_trans *tp)
309 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
310 ip->i_d.di_forkoff = 0;
311 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
313 ASSERT(ip->i_d.di_anextents == 0);
314 ASSERT(ip->i_afp == NULL);
316 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
317 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
321 * Remove an attribute from the shortform attribute list structure.
324 xfs_attr_shortform_remove(xfs_da_args_t *args)
326 xfs_attr_shortform_t *sf;
327 xfs_attr_sf_entry_t *sfe;
328 int base, size=0, end, totsize, i;
334 base = sizeof(xfs_attr_sf_hdr_t);
335 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
338 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
340 size = XFS_ATTR_SF_ENTSIZE(sfe);
341 if (sfe->namelen != args->namelen)
343 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
345 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
350 return(XFS_ERROR(ENOATTR));
353 * Fix up the attribute fork data, covering the hole
356 totsize = be16_to_cpu(sf->hdr.totsize);
358 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
360 be16_add_cpu(&sf->hdr.totsize, -size);
363 * Fix up the start offset of the attribute fork
366 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
367 (mp->m_flags & XFS_MOUNT_ATTR2) &&
368 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
369 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
370 xfs_attr_fork_reset(dp, args->trans);
372 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
373 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
374 ASSERT(dp->i_d.di_forkoff);
375 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
376 (args->op_flags & XFS_DA_OP_ADDNAME) ||
377 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
378 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
379 dp->i_afp->if_ext_max =
380 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
381 dp->i_df.if_ext_max =
382 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
383 xfs_trans_log_inode(args->trans, dp,
384 XFS_ILOG_CORE | XFS_ILOG_ADATA);
387 xfs_sbversion_add_attr2(mp, args->trans);
393 * Look up a name in a shortform attribute list structure.
397 xfs_attr_shortform_lookup(xfs_da_args_t *args)
399 xfs_attr_shortform_t *sf;
400 xfs_attr_sf_entry_t *sfe;
404 ifp = args->dp->i_afp;
405 ASSERT(ifp->if_flags & XFS_IFINLINE);
406 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
408 for (i = 0; i < sf->hdr.count;
409 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
410 if (sfe->namelen != args->namelen)
412 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
414 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
416 return(XFS_ERROR(EEXIST));
418 return(XFS_ERROR(ENOATTR));
422 * Look up a name in a shortform attribute list structure.
426 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
428 xfs_attr_shortform_t *sf;
429 xfs_attr_sf_entry_t *sfe;
432 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
433 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
435 for (i = 0; i < sf->hdr.count;
436 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
437 if (sfe->namelen != args->namelen)
439 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
441 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
443 if (args->flags & ATTR_KERNOVAL) {
444 args->valuelen = sfe->valuelen;
445 return(XFS_ERROR(EEXIST));
447 if (args->valuelen < sfe->valuelen) {
448 args->valuelen = sfe->valuelen;
449 return(XFS_ERROR(ERANGE));
451 args->valuelen = sfe->valuelen;
452 memcpy(args->value, &sfe->nameval[args->namelen],
454 return(XFS_ERROR(EEXIST));
456 return(XFS_ERROR(ENOATTR));
460 * Convert from using the shortform to the leaf.
463 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
466 xfs_attr_shortform_t *sf;
467 xfs_attr_sf_entry_t *sfe;
477 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
478 size = be16_to_cpu(sf->hdr.totsize);
479 tmpbuffer = kmem_alloc(size, KM_SLEEP);
480 ASSERT(tmpbuffer != NULL);
481 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
482 sf = (xfs_attr_shortform_t *)tmpbuffer;
484 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
486 error = xfs_da_grow_inode(args, &blkno);
489 * If we hit an IO error middle of the transaction inside
490 * grow_inode(), we may have inconsistent data. Bail out.
494 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
495 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
500 error = xfs_attr_leaf_create(args, blkno, &bp);
502 error = xfs_da_shrink_inode(args, 0, bp);
506 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
507 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
511 memset((char *)&nargs, 0, sizeof(nargs));
513 nargs.firstblock = args->firstblock;
514 nargs.flist = args->flist;
515 nargs.total = args->total;
516 nargs.whichfork = XFS_ATTR_FORK;
517 nargs.trans = args->trans;
518 nargs.op_flags = XFS_DA_OP_OKNOENT;
521 for (i = 0; i < sf->hdr.count; i++) {
522 nargs.name = (char *)sfe->nameval;
523 nargs.namelen = sfe->namelen;
524 nargs.value = (char *)&sfe->nameval[nargs.namelen];
525 nargs.valuelen = sfe->valuelen;
526 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
528 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
529 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
530 ASSERT(error == ENOATTR);
531 error = xfs_attr_leaf_add(bp, &nargs);
532 ASSERT(error != ENOSPC);
535 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
542 kmem_free(tmpbuffer);
547 xfs_attr_shortform_compare(const void *a, const void *b)
549 xfs_attr_sf_sort_t *sa, *sb;
551 sa = (xfs_attr_sf_sort_t *)a;
552 sb = (xfs_attr_sf_sort_t *)b;
553 if (sa->hash < sb->hash) {
555 } else if (sa->hash > sb->hash) {
558 return(sa->entno - sb->entno);
563 #define XFS_ISRESET_CURSOR(cursor) \
564 (!((cursor)->initted) && !((cursor)->hashval) && \
565 !((cursor)->blkno) && !((cursor)->offset))
567 * Copy out entries of shortform attribute lists for attr_list().
568 * Shortform attribute lists are not stored in hashval sorted order.
569 * If the output buffer is not large enough to hold them all, then we
570 * we have to calculate each entries' hashvalue and sort them before
571 * we can begin returning them to the user.
575 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
577 attrlist_cursor_kern_t *cursor;
578 xfs_attr_sf_sort_t *sbuf, *sbp;
579 xfs_attr_shortform_t *sf;
580 xfs_attr_sf_entry_t *sfe;
582 int sbsize, nsbuf, count, i;
585 ASSERT(context != NULL);
588 ASSERT(dp->i_afp != NULL);
589 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
593 cursor = context->cursor;
594 ASSERT(cursor != NULL);
596 xfs_attr_trace_l_c("sf start", context);
599 * If the buffer is large enough and the cursor is at the start,
600 * do not bother with sorting since we will return everything in
601 * one buffer and another call using the cursor won't need to be
603 * Note the generous fudge factor of 16 overhead bytes per entry.
604 * If bufsize is zero then put_listent must be a search function
605 * and can just scan through what we have.
607 if (context->bufsize == 0 ||
608 (XFS_ISRESET_CURSOR(cursor) &&
609 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
610 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
611 error = context->put_listent(context,
613 (char *)sfe->nameval,
616 (char*)&sfe->nameval[sfe->namelen]);
619 * Either search callback finished early or
620 * didn't fit it all in the buffer after all.
622 if (context->seen_enough)
627 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
629 xfs_attr_trace_l_c("sf big-gulp", context);
633 /* do no more for a search callback */
634 if (context->bufsize == 0)
638 * It didn't all fit, so we have to sort everything on hashval.
640 sbsize = sf->hdr.count * sizeof(*sbuf);
641 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
644 * Scan the attribute list for the rest of the entries, storing
645 * the relevant info from only those that match into a buffer.
648 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
650 ((char *)sfe < (char *)sf) ||
651 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
652 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
654 context->dp->i_mount, sfe);
655 xfs_attr_trace_l_c("sf corrupted", context);
657 return XFS_ERROR(EFSCORRUPTED);
661 sbp->hash = xfs_da_hashname((char *)sfe->nameval, sfe->namelen);
662 sbp->name = (char *)sfe->nameval;
663 sbp->namelen = sfe->namelen;
664 /* These are bytes, and both on-disk, don't endian-flip */
665 sbp->valuelen = sfe->valuelen;
666 sbp->flags = sfe->flags;
667 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
673 * Sort the entries on hash then entno.
675 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
678 * Re-find our place IN THE SORTED LIST.
683 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
684 if (sbp->hash == cursor->hashval) {
685 if (cursor->offset == count) {
689 } else if (sbp->hash > cursor->hashval) {
695 xfs_attr_trace_l_c("blk end", context);
700 * Loop putting entries into the user buffer.
702 for ( ; i < nsbuf; i++, sbp++) {
703 if (cursor->hashval != sbp->hash) {
704 cursor->hashval = sbp->hash;
707 error = context->put_listent(context,
712 &sbp->name[sbp->namelen]);
715 if (context->seen_enough)
721 xfs_attr_trace_l_c("sf E-O-F", context);
726 * Check a leaf attribute block to see if all the entries would fit into
727 * a shortform attribute list.
730 xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
732 xfs_attr_leafblock_t *leaf;
733 xfs_attr_leaf_entry_t *entry;
734 xfs_attr_leaf_name_local_t *name_loc;
738 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
740 entry = &leaf->entries[0];
741 bytes = sizeof(struct xfs_attr_sf_hdr);
742 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
743 if (entry->flags & XFS_ATTR_INCOMPLETE)
744 continue; /* don't copy partial entries */
745 if (!(entry->flags & XFS_ATTR_LOCAL))
747 name_loc = xfs_attr_leaf_name_local(leaf, i);
748 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
750 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
752 bytes += sizeof(struct xfs_attr_sf_entry)-1
754 + be16_to_cpu(name_loc->valuelen);
756 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
757 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
758 (bytes == sizeof(struct xfs_attr_sf_hdr)))
760 return(xfs_attr_shortform_bytesfit(dp, bytes));
764 * Convert a leaf attribute list to shortform attribute list
767 xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
769 xfs_attr_leafblock_t *leaf;
770 xfs_attr_leaf_entry_t *entry;
771 xfs_attr_leaf_name_local_t *name_loc;
778 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
779 ASSERT(tmpbuffer != NULL);
782 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
783 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
784 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
785 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
788 * Clean out the prior contents of the attribute list.
790 error = xfs_da_shrink_inode(args, 0, bp);
795 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
796 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
797 xfs_attr_fork_reset(dp, args->trans);
801 xfs_attr_shortform_create(args);
804 * Copy the attributes
806 memset((char *)&nargs, 0, sizeof(nargs));
808 nargs.firstblock = args->firstblock;
809 nargs.flist = args->flist;
810 nargs.total = args->total;
811 nargs.whichfork = XFS_ATTR_FORK;
812 nargs.trans = args->trans;
813 nargs.op_flags = XFS_DA_OP_OKNOENT;
814 entry = &leaf->entries[0];
815 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
816 if (entry->flags & XFS_ATTR_INCOMPLETE)
817 continue; /* don't copy partial entries */
820 ASSERT(entry->flags & XFS_ATTR_LOCAL);
821 name_loc = xfs_attr_leaf_name_local(leaf, i);
822 nargs.name = (char *)name_loc->nameval;
823 nargs.namelen = name_loc->namelen;
824 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
825 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
826 nargs.hashval = be32_to_cpu(entry->hashval);
827 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
828 xfs_attr_shortform_add(&nargs, forkoff);
833 kmem_free(tmpbuffer);
838 * Convert from using a single leaf to a root node and a leaf.
841 xfs_attr_leaf_to_node(xfs_da_args_t *args)
843 xfs_attr_leafblock_t *leaf;
844 xfs_da_intnode_t *node;
846 xfs_dabuf_t *bp1, *bp2;
852 error = xfs_da_grow_inode(args, &blkno);
855 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
861 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
866 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
867 xfs_da_buf_done(bp1);
869 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
872 * Set up the new root node.
874 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
879 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
880 /* both on-disk, don't endian-flip twice */
881 node->btree[0].hashval =
882 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
883 node->btree[0].before = cpu_to_be32(blkno);
884 node->hdr.count = cpu_to_be16(1);
885 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
889 xfs_da_buf_done(bp1);
891 xfs_da_buf_done(bp2);
896 /*========================================================================
897 * Routines used for growing the Btree.
898 *========================================================================*/
901 * Create the initial contents of a leaf attribute list
902 * or a leaf in a node attribute list.
905 xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
907 xfs_attr_leafblock_t *leaf;
908 xfs_attr_leaf_hdr_t *hdr;
915 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
921 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
923 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
924 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
925 if (!hdr->firstused) {
926 hdr->firstused = cpu_to_be16(
927 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
930 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
931 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
932 sizeof(xfs_attr_leaf_hdr_t));
934 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
941 * Split the leaf node, rebalance, then add the new entry.
944 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
945 xfs_da_state_blk_t *newblk)
951 * Allocate space for a new leaf node.
953 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
954 error = xfs_da_grow_inode(state->args, &blkno);
957 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
960 newblk->blkno = blkno;
961 newblk->magic = XFS_ATTR_LEAF_MAGIC;
964 * Rebalance the entries across the two leaves.
965 * NOTE: rebalance() currently depends on the 2nd block being empty.
967 xfs_attr_leaf_rebalance(state, oldblk, newblk);
968 error = xfs_da_blk_link(state, oldblk, newblk);
973 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
974 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
975 * "new" attrs info. Will need the "old" info to remove it later.
977 * Insert the "new" entry in the correct block.
980 error = xfs_attr_leaf_add(oldblk->bp, state->args);
982 error = xfs_attr_leaf_add(newblk->bp, state->args);
985 * Update last hashval in each block since we added the name.
987 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
988 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
993 * Add a name to the leaf attribute list structure.
996 xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
998 xfs_attr_leafblock_t *leaf;
999 xfs_attr_leaf_hdr_t *hdr;
1000 xfs_attr_leaf_map_t *map;
1001 int tablesize, entsize, sum, tmp, i;
1004 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1005 ASSERT((args->index >= 0)
1006 && (args->index <= be16_to_cpu(leaf->hdr.count)));
1008 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1009 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1012 * Search through freemap for first-fit on new name length.
1013 * (may need to figure in size of entry struct too)
1015 tablesize = (be16_to_cpu(hdr->count) + 1)
1016 * sizeof(xfs_attr_leaf_entry_t)
1017 + sizeof(xfs_attr_leaf_hdr_t);
1018 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1019 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1020 if (tablesize > be16_to_cpu(hdr->firstused)) {
1021 sum += be16_to_cpu(map->size);
1025 continue; /* no space in this map */
1027 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
1028 tmp += sizeof(xfs_attr_leaf_entry_t);
1029 if (be16_to_cpu(map->size) >= tmp) {
1030 tmp = xfs_attr_leaf_add_work(bp, args, i);
1033 sum += be16_to_cpu(map->size);
1037 * If there are no holes in the address space of the block,
1038 * and we don't have enough freespace, then compaction will do us
1039 * no good and we should just give up.
1041 if (!hdr->holes && (sum < entsize))
1042 return(XFS_ERROR(ENOSPC));
1045 * Compact the entries to coalesce free space.
1046 * This may change the hdr->count via dropping INCOMPLETE entries.
1048 xfs_attr_leaf_compact(args->trans, bp);
1051 * After compaction, the block is guaranteed to have only one
1052 * free region, in freemap[0]. If it is not big enough, give up.
1054 if (be16_to_cpu(hdr->freemap[0].size)
1055 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1056 return(XFS_ERROR(ENOSPC));
1058 return(xfs_attr_leaf_add_work(bp, args, 0));
1062 * Add a name to a leaf attribute list structure.
1065 xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1067 xfs_attr_leafblock_t *leaf;
1068 xfs_attr_leaf_hdr_t *hdr;
1069 xfs_attr_leaf_entry_t *entry;
1070 xfs_attr_leaf_name_local_t *name_loc;
1071 xfs_attr_leaf_name_remote_t *name_rmt;
1072 xfs_attr_leaf_map_t *map;
1077 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1079 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1080 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
1083 * Force open some space in the entry array and fill it in.
1085 entry = &leaf->entries[args->index];
1086 if (args->index < be16_to_cpu(hdr->count)) {
1087 tmp = be16_to_cpu(hdr->count) - args->index;
1088 tmp *= sizeof(xfs_attr_leaf_entry_t);
1089 memmove((char *)(entry+1), (char *)entry, tmp);
1090 xfs_da_log_buf(args->trans, bp,
1091 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1093 be16_add_cpu(&hdr->count, 1);
1096 * Allocate space for the new string (at the end of the run).
1098 map = &hdr->freemap[mapindex];
1099 mp = args->trans->t_mountp;
1100 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1101 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1102 ASSERT(be16_to_cpu(map->size) >=
1103 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1104 mp->m_sb.sb_blocksize, NULL));
1105 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1106 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1107 be16_add_cpu(&map->size,
1108 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1109 mp->m_sb.sb_blocksize, &tmp));
1110 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1111 be16_to_cpu(map->size));
1112 entry->hashval = cpu_to_be32(args->hashval);
1113 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1114 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1115 if (args->op_flags & XFS_DA_OP_RENAME) {
1116 entry->flags |= XFS_ATTR_INCOMPLETE;
1117 if ((args->blkno2 == args->blkno) &&
1118 (args->index2 <= args->index)) {
1122 xfs_da_log_buf(args->trans, bp,
1123 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1124 ASSERT((args->index == 0) ||
1125 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1126 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
1127 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1130 * Copy the attribute name and value into the new space.
1132 * For "remote" attribute values, simply note that we need to
1133 * allocate space for the "remote" value. We can't actually
1134 * allocate the extents in this transaction, and we can't decide
1135 * which blocks they should be as we might allocate more blocks
1136 * as part of this transaction (a split operation for example).
1138 if (entry->flags & XFS_ATTR_LOCAL) {
1139 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
1140 name_loc->namelen = args->namelen;
1141 name_loc->valuelen = cpu_to_be16(args->valuelen);
1142 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1143 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1144 be16_to_cpu(name_loc->valuelen));
1146 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
1147 name_rmt->namelen = args->namelen;
1148 memcpy((char *)name_rmt->name, args->name, args->namelen);
1149 entry->flags |= XFS_ATTR_INCOMPLETE;
1151 name_rmt->valuelen = 0;
1152 name_rmt->valueblk = 0;
1154 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1156 xfs_da_log_buf(args->trans, bp,
1157 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1158 xfs_attr_leaf_entsize(leaf, args->index)));
1161 * Update the control info for this leaf node
1163 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
1164 /* both on-disk, don't endian-flip twice */
1165 hdr->firstused = entry->nameidx;
1167 ASSERT(be16_to_cpu(hdr->firstused) >=
1168 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1169 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
1170 + sizeof(xfs_attr_leaf_hdr_t);
1171 map = &hdr->freemap[0];
1172 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1173 if (be16_to_cpu(map->base) == tmp) {
1174 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1175 be16_add_cpu(&map->size,
1176 -((int)sizeof(xfs_attr_leaf_entry_t)));
1179 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
1180 xfs_da_log_buf(args->trans, bp,
1181 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1186 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1189 xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1191 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1192 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1196 mp = trans->t_mountp;
1197 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1198 ASSERT(tmpbuffer != NULL);
1199 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1200 memset(bp->data, 0, XFS_LBSIZE(mp));
1203 * Copy basic information
1205 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1207 hdr_s = &leaf_s->hdr;
1208 hdr_d = &leaf_d->hdr;
1209 hdr_d->info = hdr_s->info; /* struct copy */
1210 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
1211 /* handle truncation gracefully */
1212 if (!hdr_d->firstused) {
1213 hdr_d->firstused = cpu_to_be16(
1214 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1216 hdr_d->usedbytes = 0;
1219 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1220 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1221 sizeof(xfs_attr_leaf_hdr_t));
1224 * Copy all entry's in the same (sorted) order,
1225 * but allocate name/value pairs packed and in sequence.
1227 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1228 be16_to_cpu(hdr_s->count), mp);
1229 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1231 kmem_free(tmpbuffer);
1235 * Redistribute the attribute list entries between two leaf nodes,
1236 * taking into account the size of the new entry.
1238 * NOTE: if new block is empty, then it will get the upper half of the
1239 * old block. At present, all (one) callers pass in an empty second block.
1241 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1242 * to match what it is doing in splitting the attribute leaf block. Those
1243 * values are used in "atomic rename" operations on attributes. Note that
1244 * the "new" and "old" values can end up in different blocks.
1247 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1248 xfs_da_state_blk_t *blk2)
1250 xfs_da_args_t *args;
1251 xfs_da_state_blk_t *tmp_blk;
1252 xfs_attr_leafblock_t *leaf1, *leaf2;
1253 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1254 int count, totallen, max, space, swap;
1257 * Set up environment.
1259 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1260 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1261 leaf1 = blk1->bp->data;
1262 leaf2 = blk2->bp->data;
1263 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1264 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1268 * Check ordering of blocks, reverse if it makes things simpler.
1270 * NOTE: Given that all (current) callers pass in an empty
1271 * second block, this code should never set "swap".
1274 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1278 leaf1 = blk1->bp->data;
1279 leaf2 = blk2->bp->data;
1286 * Examine entries until we reduce the absolute difference in
1287 * byte usage between the two blocks to a minimum. Then get
1288 * the direction to copy and the number of elements to move.
1290 * "inleaf" is true if the new entry should be inserted into blk1.
1291 * If "swap" is also true, then reverse the sense of "inleaf".
1293 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1296 state->inleaf = !state->inleaf;
1299 * Move any entries required from leaf to leaf:
1301 if (count < be16_to_cpu(hdr1->count)) {
1303 * Figure the total bytes to be added to the destination leaf.
1305 /* number entries being moved */
1306 count = be16_to_cpu(hdr1->count) - count;
1307 space = be16_to_cpu(hdr1->usedbytes) - totallen;
1308 space += count * sizeof(xfs_attr_leaf_entry_t);
1311 * leaf2 is the destination, compact it if it looks tight.
1313 max = be16_to_cpu(hdr2->firstused)
1314 - sizeof(xfs_attr_leaf_hdr_t);
1315 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
1317 xfs_attr_leaf_compact(args->trans, blk2->bp);
1321 * Move high entries from leaf1 to low end of leaf2.
1323 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
1324 leaf2, 0, count, state->mp);
1326 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1327 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1328 } else if (count > be16_to_cpu(hdr1->count)) {
1330 * I assert that since all callers pass in an empty
1331 * second buffer, this code should never execute.
1335 * Figure the total bytes to be added to the destination leaf.
1337 /* number entries being moved */
1338 count -= be16_to_cpu(hdr1->count);
1339 space = totallen - be16_to_cpu(hdr1->usedbytes);
1340 space += count * sizeof(xfs_attr_leaf_entry_t);
1343 * leaf1 is the destination, compact it if it looks tight.
1345 max = be16_to_cpu(hdr1->firstused)
1346 - sizeof(xfs_attr_leaf_hdr_t);
1347 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
1349 xfs_attr_leaf_compact(args->trans, blk1->bp);
1353 * Move low entries from leaf2 to high end of leaf1.
1355 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1356 be16_to_cpu(hdr1->count), 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);
1363 * Copy out last hashval in each block for B-tree code.
1365 blk1->hashval = be32_to_cpu(
1366 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1367 blk2->hashval = be32_to_cpu(
1368 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
1371 * Adjust the expected index for insertion.
1372 * NOTE: this code depends on the (current) situation that the
1373 * second block was originally empty.
1375 * If the insertion point moved to the 2nd block, we must adjust
1376 * the index. We must also track the entry just following the
1377 * new entry for use in an "atomic rename" operation, that entry
1378 * is always the "old" entry and the "new" entry is what we are
1379 * inserting. The index/blkno fields refer to the "old" entry,
1380 * while the index2/blkno2 fields refer to the "new" entry.
1382 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
1383 ASSERT(state->inleaf == 0);
1384 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
1385 args->index = args->index2 = blk2->index;
1386 args->blkno = args->blkno2 = blk2->blkno;
1387 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
1388 if (state->inleaf) {
1389 args->index = blk1->index;
1390 args->blkno = blk1->blkno;
1392 args->blkno2 = blk2->blkno;
1394 blk2->index = blk1->index
1395 - be16_to_cpu(leaf1->hdr.count);
1396 args->index = args->index2 = blk2->index;
1397 args->blkno = args->blkno2 = blk2->blkno;
1400 ASSERT(state->inleaf == 1);
1401 args->index = args->index2 = blk1->index;
1402 args->blkno = args->blkno2 = blk1->blkno;
1407 * Examine entries until we reduce the absolute difference in
1408 * byte usage between the two blocks to a minimum.
1409 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1410 * GROT: there will always be enough room in either block for a new entry.
1411 * GROT: Do a double-split for this case?
1414 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1415 xfs_da_state_blk_t *blk1,
1416 xfs_da_state_blk_t *blk2,
1417 int *countarg, int *usedbytesarg)
1419 xfs_attr_leafblock_t *leaf1, *leaf2;
1420 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1421 xfs_attr_leaf_entry_t *entry;
1422 int count, max, index, totallen, half;
1423 int lastdelta, foundit, tmp;
1426 * Set up environment.
1428 leaf1 = blk1->bp->data;
1429 leaf2 = blk2->bp->data;
1436 * Examine entries until we reduce the absolute difference in
1437 * byte usage between the two blocks to a minimum.
1439 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
1440 half = (max+1) * sizeof(*entry);
1441 half += be16_to_cpu(hdr1->usedbytes) +
1442 be16_to_cpu(hdr2->usedbytes) +
1443 xfs_attr_leaf_newentsize(
1444 state->args->namelen,
1445 state->args->valuelen,
1446 state->blocksize, NULL);
1448 lastdelta = state->blocksize;
1449 entry = &leaf1->entries[0];
1450 for (count = index = 0; count < max; entry++, index++, count++) {
1452 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1454 * The new entry is in the first block, account for it.
1456 if (count == blk1->index) {
1457 tmp = totallen + sizeof(*entry) +
1458 xfs_attr_leaf_newentsize(
1459 state->args->namelen,
1460 state->args->valuelen,
1461 state->blocksize, NULL);
1462 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1464 lastdelta = XFS_ATTR_ABS(half - tmp);
1470 * Wrap around into the second block if necessary.
1472 if (count == be16_to_cpu(hdr1->count)) {
1474 entry = &leaf1->entries[0];
1479 * Figure out if next leaf entry would be too much.
1481 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1483 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1485 lastdelta = XFS_ATTR_ABS(half - tmp);
1491 * Calculate the number of usedbytes that will end up in lower block.
1492 * If new entry not in lower block, fix up the count.
1494 totallen -= count * sizeof(*entry);
1496 totallen -= sizeof(*entry) +
1497 xfs_attr_leaf_newentsize(
1498 state->args->namelen,
1499 state->args->valuelen,
1500 state->blocksize, NULL);
1504 *usedbytesarg = totallen;
1508 /*========================================================================
1509 * Routines used for shrinking the Btree.
1510 *========================================================================*/
1513 * Check a leaf block and its neighbors to see if the block should be
1514 * collapsed into one or the other neighbor. Always keep the block
1515 * with the smaller block number.
1516 * If the current block is over 50% full, don't try to join it, return 0.
1517 * If the block is empty, fill in the state structure and return 2.
1518 * If it can be collapsed, fill in the state structure and return 1.
1519 * If nothing can be done, return 0.
1521 * GROT: allow for INCOMPLETE entries in calculation.
1524 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1526 xfs_attr_leafblock_t *leaf;
1527 xfs_da_state_blk_t *blk;
1528 xfs_da_blkinfo_t *info;
1529 int count, bytes, forward, error, retval, i;
1534 * Check for the degenerate case of the block being over 50% full.
1535 * If so, it's not worth even looking to see if we might be able
1536 * to coalesce with a sibling.
1538 blk = &state->path.blk[ state->path.active-1 ];
1539 info = blk->bp->data;
1540 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
1541 leaf = (xfs_attr_leafblock_t *)info;
1542 count = be16_to_cpu(leaf->hdr.count);
1543 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1544 count * sizeof(xfs_attr_leaf_entry_t) +
1545 be16_to_cpu(leaf->hdr.usedbytes);
1546 if (bytes > (state->blocksize >> 1)) {
1547 *action = 0; /* blk over 50%, don't try to join */
1552 * Check for the degenerate case of the block being empty.
1553 * If the block is empty, we'll simply delete it, no need to
1554 * coalesce it with a sibling block. We choose (arbitrarily)
1555 * to merge with the forward block unless it is NULL.
1559 * Make altpath point to the block we want to keep and
1560 * path point to the block we want to drop (this one).
1562 forward = (info->forw != 0);
1563 memcpy(&state->altpath, &state->path, sizeof(state->path));
1564 error = xfs_da_path_shift(state, &state->altpath, forward,
1577 * Examine each sibling block to see if we can coalesce with
1578 * at least 25% free space to spare. We need to figure out
1579 * whether to merge with the forward or the backward block.
1580 * We prefer coalescing with the lower numbered sibling so as
1581 * to shrink an attribute list over time.
1583 /* start with smaller blk num */
1584 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
1585 for (i = 0; i < 2; forward = !forward, i++) {
1587 blkno = be32_to_cpu(info->forw);
1589 blkno = be32_to_cpu(info->back);
1592 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1593 blkno, -1, &bp, XFS_ATTR_FORK);
1598 leaf = (xfs_attr_leafblock_t *)info;
1599 count = be16_to_cpu(leaf->hdr.count);
1600 bytes = state->blocksize - (state->blocksize>>2);
1601 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1603 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1604 count += be16_to_cpu(leaf->hdr.count);
1605 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1606 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1607 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1608 xfs_da_brelse(state->args->trans, bp);
1610 break; /* fits with at least 25% to spare */
1618 * Make altpath point to the block we want to keep (the lower
1619 * numbered block) and path point to the block we want to drop.
1621 memcpy(&state->altpath, &state->path, sizeof(state->path));
1622 if (blkno < blk->blkno) {
1623 error = xfs_da_path_shift(state, &state->altpath, forward,
1626 error = xfs_da_path_shift(state, &state->path, forward,
1640 * Remove a name from the leaf attribute list structure.
1642 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1643 * If two leaves are 37% full, when combined they will leave 25% free.
1646 xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1648 xfs_attr_leafblock_t *leaf;
1649 xfs_attr_leaf_hdr_t *hdr;
1650 xfs_attr_leaf_map_t *map;
1651 xfs_attr_leaf_entry_t *entry;
1652 int before, after, smallest, entsize;
1653 int tablesize, tmp, i;
1657 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1659 mp = args->trans->t_mountp;
1660 ASSERT((be16_to_cpu(hdr->count) > 0)
1661 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
1662 ASSERT((args->index >= 0)
1663 && (args->index < be16_to_cpu(hdr->count)));
1664 ASSERT(be16_to_cpu(hdr->firstused) >=
1665 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1666 entry = &leaf->entries[args->index];
1667 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1668 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1671 * Scan through free region table:
1672 * check for adjacency of free'd entry with an existing one,
1673 * find smallest free region in case we need to replace it,
1674 * adjust any map that borders the entry table,
1676 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
1677 + sizeof(xfs_attr_leaf_hdr_t);
1678 map = &hdr->freemap[0];
1679 tmp = be16_to_cpu(map->size);
1680 before = after = -1;
1681 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1682 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1683 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1684 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1685 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1686 if (be16_to_cpu(map->base) == tablesize) {
1687 be16_add_cpu(&map->base,
1688 -((int)sizeof(xfs_attr_leaf_entry_t)));
1689 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
1692 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
1693 == be16_to_cpu(entry->nameidx)) {
1695 } else if (be16_to_cpu(map->base)
1696 == (be16_to_cpu(entry->nameidx) + entsize)) {
1698 } else if (be16_to_cpu(map->size) < tmp) {
1699 tmp = be16_to_cpu(map->size);
1705 * Coalesce adjacent freemap regions,
1706 * or replace the smallest region.
1708 if ((before >= 0) || (after >= 0)) {
1709 if ((before >= 0) && (after >= 0)) {
1710 map = &hdr->freemap[before];
1711 be16_add_cpu(&map->size, entsize);
1712 be16_add_cpu(&map->size,
1713 be16_to_cpu(hdr->freemap[after].size));
1714 hdr->freemap[after].base = 0;
1715 hdr->freemap[after].size = 0;
1716 } else if (before >= 0) {
1717 map = &hdr->freemap[before];
1718 be16_add_cpu(&map->size, entsize);
1720 map = &hdr->freemap[after];
1721 /* both on-disk, don't endian flip twice */
1722 map->base = entry->nameidx;
1723 be16_add_cpu(&map->size, entsize);
1727 * Replace smallest region (if it is smaller than free'd entry)
1729 map = &hdr->freemap[smallest];
1730 if (be16_to_cpu(map->size) < entsize) {
1731 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
1732 map->size = cpu_to_be16(entsize);
1737 * Did we remove the first entry?
1739 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
1745 * Compress the remaining entries and zero out the removed stuff.
1747 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
1748 be16_add_cpu(&hdr->usedbytes, -entsize);
1749 xfs_da_log_buf(args->trans, bp,
1750 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1753 tmp = (be16_to_cpu(hdr->count) - args->index)
1754 * sizeof(xfs_attr_leaf_entry_t);
1755 memmove((char *)entry, (char *)(entry+1), tmp);
1756 be16_add_cpu(&hdr->count, -1);
1757 xfs_da_log_buf(args->trans, bp,
1758 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1759 entry = &leaf->entries[be16_to_cpu(hdr->count)];
1760 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1763 * If we removed the first entry, re-find the first used byte
1764 * in the name area. Note that if the entry was the "firstused",
1765 * then we don't have a "hole" in our block resulting from
1766 * removing the name.
1769 tmp = XFS_LBSIZE(mp);
1770 entry = &leaf->entries[0];
1771 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
1772 ASSERT(be16_to_cpu(entry->nameidx) >=
1773 be16_to_cpu(hdr->firstused));
1774 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1776 if (be16_to_cpu(entry->nameidx) < tmp)
1777 tmp = be16_to_cpu(entry->nameidx);
1779 hdr->firstused = cpu_to_be16(tmp);
1780 if (!hdr->firstused) {
1781 hdr->firstused = cpu_to_be16(
1782 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1785 hdr->holes = 1; /* mark as needing compaction */
1787 xfs_da_log_buf(args->trans, bp,
1788 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1791 * Check if leaf is less than 50% full, caller may want to
1792 * "join" the leaf with a sibling if so.
1794 tmp = sizeof(xfs_attr_leaf_hdr_t);
1795 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1796 tmp += be16_to_cpu(leaf->hdr.usedbytes);
1797 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1801 * Move all the attribute list entries from drop_leaf into save_leaf.
1804 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1805 xfs_da_state_blk_t *save_blk)
1807 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1808 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1813 * Set up environment.
1816 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1817 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1818 drop_leaf = drop_blk->bp->data;
1819 save_leaf = save_blk->bp->data;
1820 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1821 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1822 drop_hdr = &drop_leaf->hdr;
1823 save_hdr = &save_leaf->hdr;
1826 * Save last hashval from dying block for later Btree fixup.
1828 drop_blk->hashval = be32_to_cpu(
1829 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
1832 * Check if we need a temp buffer, or can we do it in place.
1833 * Note that we don't check "leaf" for holes because we will
1834 * always be dropping it, toosmall() decided that for us already.
1836 if (save_hdr->holes == 0) {
1838 * dest leaf has no holes, so we add there. May need
1839 * to make some room in the entry array.
1841 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1842 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1843 be16_to_cpu(drop_hdr->count), mp);
1845 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1846 be16_to_cpu(save_hdr->count),
1847 be16_to_cpu(drop_hdr->count), mp);
1851 * Destination has holes, so we make a temporary copy
1852 * of the leaf and add them both to that.
1854 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1855 ASSERT(tmpbuffer != NULL);
1856 memset(tmpbuffer, 0, state->blocksize);
1857 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1858 tmp_hdr = &tmp_leaf->hdr;
1859 tmp_hdr->info = save_hdr->info; /* struct copy */
1861 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
1862 if (!tmp_hdr->firstused) {
1863 tmp_hdr->firstused = cpu_to_be16(
1864 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1866 tmp_hdr->usedbytes = 0;
1867 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1868 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1869 be16_to_cpu(drop_hdr->count), mp);
1870 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1871 be16_to_cpu(tmp_leaf->hdr.count),
1872 be16_to_cpu(save_hdr->count), mp);
1874 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1875 be16_to_cpu(save_hdr->count), mp);
1876 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1877 be16_to_cpu(tmp_leaf->hdr.count),
1878 be16_to_cpu(drop_hdr->count), mp);
1880 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1881 kmem_free(tmpbuffer);
1884 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1885 state->blocksize - 1);
1888 * Copy out last hashval in each block for B-tree code.
1890 save_blk->hashval = be32_to_cpu(
1891 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
1894 /*========================================================================
1895 * Routines used for finding things in the Btree.
1896 *========================================================================*/
1899 * Look up a name in a leaf attribute list structure.
1900 * This is the internal routine, it uses the caller's buffer.
1902 * Note that duplicate keys are allowed, but only check within the
1903 * current leaf node. The Btree code must check in adjacent leaf nodes.
1905 * Return in args->index the index into the entry[] array of either
1906 * the found entry, or where the entry should have been (insert before
1909 * Don't change the args->value unless we find the attribute.
1912 xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1914 xfs_attr_leafblock_t *leaf;
1915 xfs_attr_leaf_entry_t *entry;
1916 xfs_attr_leaf_name_local_t *name_loc;
1917 xfs_attr_leaf_name_remote_t *name_rmt;
1919 xfs_dahash_t hashval;
1922 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1923 ASSERT(be16_to_cpu(leaf->hdr.count)
1924 < (XFS_LBSIZE(args->dp->i_mount)/8));
1927 * Binary search. (note: small blocks will skip this loop)
1929 hashval = args->hashval;
1930 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
1931 for (entry = &leaf->entries[probe]; span > 4;
1932 entry = &leaf->entries[probe]) {
1934 if (be32_to_cpu(entry->hashval) < hashval)
1936 else if (be32_to_cpu(entry->hashval) > hashval)
1941 ASSERT((probe >= 0) &&
1943 || (probe < be16_to_cpu(leaf->hdr.count))));
1944 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
1947 * Since we may have duplicate hashval's, find the first matching
1948 * hashval in the leaf.
1950 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
1954 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1955 (be32_to_cpu(entry->hashval) < hashval)) {
1959 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
1960 (be32_to_cpu(entry->hashval) != hashval)) {
1961 args->index = probe;
1962 return(XFS_ERROR(ENOATTR));
1966 * Duplicate keys may be present, so search all of them for a match.
1968 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
1969 (be32_to_cpu(entry->hashval) == hashval);
1972 * GROT: Add code to remove incomplete entries.
1975 * If we are looking for INCOMPLETE entries, show only those.
1976 * If we are looking for complete entries, show only those.
1978 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
1979 (entry->flags & XFS_ATTR_INCOMPLETE)) {
1982 if (entry->flags & XFS_ATTR_LOCAL) {
1983 name_loc = xfs_attr_leaf_name_local(leaf, probe);
1984 if (name_loc->namelen != args->namelen)
1986 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
1988 if (!xfs_attr_namesp_match(args->flags, entry->flags))
1990 args->index = probe;
1991 return(XFS_ERROR(EEXIST));
1993 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
1994 if (name_rmt->namelen != args->namelen)
1996 if (memcmp(args->name, (char *)name_rmt->name,
1997 args->namelen) != 0)
1999 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2001 args->index = probe;
2002 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2003 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2004 be32_to_cpu(name_rmt->valuelen));
2005 return(XFS_ERROR(EEXIST));
2008 args->index = probe;
2009 return(XFS_ERROR(ENOATTR));
2013 * Get the value associated with an attribute name from a leaf attribute
2017 xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2020 xfs_attr_leafblock_t *leaf;
2021 xfs_attr_leaf_entry_t *entry;
2022 xfs_attr_leaf_name_local_t *name_loc;
2023 xfs_attr_leaf_name_remote_t *name_rmt;
2026 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2027 ASSERT(be16_to_cpu(leaf->hdr.count)
2028 < (XFS_LBSIZE(args->dp->i_mount)/8));
2029 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2031 entry = &leaf->entries[args->index];
2032 if (entry->flags & XFS_ATTR_LOCAL) {
2033 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2034 ASSERT(name_loc->namelen == args->namelen);
2035 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2036 valuelen = be16_to_cpu(name_loc->valuelen);
2037 if (args->flags & ATTR_KERNOVAL) {
2038 args->valuelen = valuelen;
2041 if (args->valuelen < valuelen) {
2042 args->valuelen = valuelen;
2043 return(XFS_ERROR(ERANGE));
2045 args->valuelen = valuelen;
2046 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2048 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2049 ASSERT(name_rmt->namelen == args->namelen);
2050 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2051 valuelen = be32_to_cpu(name_rmt->valuelen);
2052 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2053 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2054 if (args->flags & ATTR_KERNOVAL) {
2055 args->valuelen = valuelen;
2058 if (args->valuelen < valuelen) {
2059 args->valuelen = valuelen;
2060 return(XFS_ERROR(ERANGE));
2062 args->valuelen = valuelen;
2067 /*========================================================================
2069 *========================================================================*/
2072 * Move the indicated entries from one leaf to another.
2073 * NOTE: this routine modifies both source and destination leaves.
2077 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2078 xfs_attr_leafblock_t *leaf_d, int start_d,
2079 int count, xfs_mount_t *mp)
2081 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2082 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2086 * Check for nothing to do.
2092 * Set up environment.
2094 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2095 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2096 hdr_s = &leaf_s->hdr;
2097 hdr_d = &leaf_d->hdr;
2098 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2099 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2100 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2101 ((be16_to_cpu(hdr_s->count)
2102 * sizeof(*entry_s))+sizeof(*hdr_s)));
2103 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2104 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2105 ((be16_to_cpu(hdr_d->count)
2106 * sizeof(*entry_d))+sizeof(*hdr_d)));
2108 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2109 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2110 ASSERT(count <= be16_to_cpu(hdr_s->count));
2113 * Move the entries in the destination leaf up to make a hole?
2115 if (start_d < be16_to_cpu(hdr_d->count)) {
2116 tmp = be16_to_cpu(hdr_d->count) - start_d;
2117 tmp *= sizeof(xfs_attr_leaf_entry_t);
2118 entry_s = &leaf_d->entries[start_d];
2119 entry_d = &leaf_d->entries[start_d + count];
2120 memmove((char *)entry_d, (char *)entry_s, tmp);
2124 * Copy all entry's in the same (sorted) order,
2125 * but allocate attribute info packed and in sequence.
2127 entry_s = &leaf_s->entries[start_s];
2128 entry_d = &leaf_d->entries[start_d];
2130 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2131 ASSERT(be16_to_cpu(entry_s->nameidx)
2132 >= be16_to_cpu(hdr_s->firstused));
2133 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2136 * Code to drop INCOMPLETE entries. Difficult to use as we
2137 * may also need to change the insertion index. Code turned
2138 * off for 6.2, should be revisited later.
2140 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2141 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2142 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2143 be16_add_cpu(&hdr_s->count, -1);
2144 entry_d--; /* to compensate for ++ in loop hdr */
2146 if ((start_s + i) < offset)
2147 result++; /* insertion index adjustment */
2150 be16_add_cpu(&hdr_d->firstused, -tmp);
2151 /* both on-disk, don't endian flip twice */
2152 entry_d->hashval = entry_s->hashval;
2153 /* both on-disk, don't endian flip twice */
2154 entry_d->nameidx = hdr_d->firstused;
2155 entry_d->flags = entry_s->flags;
2156 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2158 memmove(xfs_attr_leaf_name(leaf_d, desti),
2159 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
2160 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2162 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2163 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2164 be16_add_cpu(&hdr_d->usedbytes, tmp);
2165 be16_add_cpu(&hdr_s->count, -1);
2166 be16_add_cpu(&hdr_d->count, 1);
2167 tmp = be16_to_cpu(hdr_d->count)
2168 * sizeof(xfs_attr_leaf_entry_t)
2169 + sizeof(xfs_attr_leaf_hdr_t);
2170 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
2177 * Zero out the entries we just copied.
2179 if (start_s == be16_to_cpu(hdr_s->count)) {
2180 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2181 entry_s = &leaf_s->entries[start_s];
2182 ASSERT(((char *)entry_s + tmp) <=
2183 ((char *)leaf_s + XFS_LBSIZE(mp)));
2184 memset((char *)entry_s, 0, tmp);
2187 * Move the remaining entries down to fill the hole,
2188 * then zero the entries at the top.
2190 tmp = be16_to_cpu(hdr_s->count) - count;
2191 tmp *= sizeof(xfs_attr_leaf_entry_t);
2192 entry_s = &leaf_s->entries[start_s + count];
2193 entry_d = &leaf_s->entries[start_s];
2194 memmove((char *)entry_d, (char *)entry_s, tmp);
2196 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2197 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
2198 ASSERT(((char *)entry_s + tmp) <=
2199 ((char *)leaf_s + XFS_LBSIZE(mp)));
2200 memset((char *)entry_s, 0, tmp);
2204 * Fill in the freemap information
2206 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2207 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2208 sizeof(xfs_attr_leaf_entry_t));
2209 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2210 - be16_to_cpu(hdr_d->freemap[0].base));
2211 hdr_d->freemap[1].base = 0;
2212 hdr_d->freemap[2].base = 0;
2213 hdr_d->freemap[1].size = 0;
2214 hdr_d->freemap[2].size = 0;
2215 hdr_s->holes = 1; /* leaf may not be compact */
2219 * Compare two leaf blocks "order".
2220 * Return 0 unless leaf2 should go before leaf1.
2223 xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2225 xfs_attr_leafblock_t *leaf1, *leaf2;
2227 leaf1 = leaf1_bp->data;
2228 leaf2 = leaf2_bp->data;
2229 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2230 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
2231 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2232 (be16_to_cpu(leaf2->hdr.count) > 0) &&
2233 ((be32_to_cpu(leaf2->entries[0].hashval) <
2234 be32_to_cpu(leaf1->entries[0].hashval)) ||
2235 (be32_to_cpu(leaf2->entries[
2236 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2237 be32_to_cpu(leaf1->entries[
2238 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
2245 * Pick up the last hashvalue from a leaf block.
2248 xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2250 xfs_attr_leafblock_t *leaf;
2253 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2255 *count = be16_to_cpu(leaf->hdr.count);
2256 if (!leaf->hdr.count)
2258 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
2262 * Calculate the number of bytes used to store the indicated attribute
2263 * (whether local or remote only calculate bytes in this block).
2266 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2268 xfs_attr_leaf_name_local_t *name_loc;
2269 xfs_attr_leaf_name_remote_t *name_rmt;
2272 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2273 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2274 name_loc = xfs_attr_leaf_name_local(leaf, index);
2275 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2276 be16_to_cpu(name_loc->valuelen));
2278 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2279 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2285 * Calculate the number of bytes that would be required to store the new
2286 * attribute (whether local or remote only calculate bytes in this block).
2287 * This routine decides as a side effect whether the attribute will be
2288 * a "local" or a "remote" attribute.
2291 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2295 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2296 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
2301 size = xfs_attr_leaf_entsize_remote(namelen);
2310 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2313 xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2315 attrlist_cursor_kern_t *cursor;
2316 xfs_attr_leafblock_t *leaf;
2317 xfs_attr_leaf_entry_t *entry;
2322 cursor = context->cursor;
2323 cursor->initted = 1;
2325 xfs_attr_trace_l_cl("blk start", context, leaf);
2328 * Re-find our place in the leaf block if this is a new syscall.
2330 if (context->resynch) {
2331 entry = &leaf->entries[0];
2332 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2333 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
2334 if (cursor->offset == context->dupcnt) {
2335 context->dupcnt = 0;
2339 } else if (be32_to_cpu(entry->hashval) >
2341 context->dupcnt = 0;
2345 if (i == be16_to_cpu(leaf->hdr.count)) {
2346 xfs_attr_trace_l_c("not found", context);
2350 entry = &leaf->entries[0];
2353 context->resynch = 0;
2356 * We have found our place, start copying out the new attributes.
2359 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
2360 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2361 cursor->hashval = be32_to_cpu(entry->hashval);
2365 if (entry->flags & XFS_ATTR_INCOMPLETE)
2366 continue; /* skip incomplete entries */
2368 if (entry->flags & XFS_ATTR_LOCAL) {
2369 xfs_attr_leaf_name_local_t *name_loc =
2370 xfs_attr_leaf_name_local(leaf, i);
2372 retval = context->put_listent(context,
2374 (char *)name_loc->nameval,
2375 (int)name_loc->namelen,
2376 be16_to_cpu(name_loc->valuelen),
2377 (char *)&name_loc->nameval[name_loc->namelen]);
2381 xfs_attr_leaf_name_remote_t *name_rmt =
2382 xfs_attr_leaf_name_remote(leaf, i);
2384 int valuelen = be32_to_cpu(name_rmt->valuelen);
2386 if (context->put_value) {
2389 memset((char *)&args, 0, sizeof(args));
2390 args.dp = context->dp;
2391 args.whichfork = XFS_ATTR_FORK;
2392 args.valuelen = valuelen;
2393 args.value = kmem_alloc(valuelen, KM_SLEEP);
2394 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2395 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2396 retval = xfs_attr_rmtval_get(&args);
2399 retval = context->put_listent(context,
2401 (char *)name_rmt->name,
2402 (int)name_rmt->namelen,
2405 kmem_free(args.value);
2407 retval = context->put_listent(context,
2409 (char *)name_rmt->name,
2410 (int)name_rmt->namelen,
2417 if (context->seen_enough)
2421 xfs_attr_trace_l_cl("blk end", context, leaf);
2426 /*========================================================================
2427 * Manage the INCOMPLETE flag in a leaf entry
2428 *========================================================================*/
2431 * Clear the INCOMPLETE flag on an entry in a leaf block.
2434 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2436 xfs_attr_leafblock_t *leaf;
2437 xfs_attr_leaf_entry_t *entry;
2438 xfs_attr_leaf_name_remote_t *name_rmt;
2442 xfs_attr_leaf_name_local_t *name_loc;
2448 * Set up the operation.
2450 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2458 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2459 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2460 ASSERT(args->index >= 0);
2461 entry = &leaf->entries[ args->index ];
2462 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2465 if (entry->flags & XFS_ATTR_LOCAL) {
2466 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2467 namelen = name_loc->namelen;
2468 name = (char *)name_loc->nameval;
2470 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2471 namelen = name_rmt->namelen;
2472 name = (char *)name_rmt->name;
2474 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2475 ASSERT(namelen == args->namelen);
2476 ASSERT(memcmp(name, args->name, namelen) == 0);
2479 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2480 xfs_da_log_buf(args->trans, bp,
2481 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2483 if (args->rmtblkno) {
2484 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2485 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2486 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2487 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2488 xfs_da_log_buf(args->trans, bp,
2489 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2491 xfs_da_buf_done(bp);
2494 * Commit the flag value change and start the next trans in series.
2496 return xfs_trans_roll(&args->trans, args->dp);
2500 * Set the INCOMPLETE flag on an entry in a leaf block.
2503 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2505 xfs_attr_leafblock_t *leaf;
2506 xfs_attr_leaf_entry_t *entry;
2507 xfs_attr_leaf_name_remote_t *name_rmt;
2512 * Set up the operation.
2514 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2522 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2523 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2524 ASSERT(args->index >= 0);
2525 entry = &leaf->entries[ args->index ];
2527 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2528 entry->flags |= XFS_ATTR_INCOMPLETE;
2529 xfs_da_log_buf(args->trans, bp,
2530 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2531 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2532 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2533 name_rmt->valueblk = 0;
2534 name_rmt->valuelen = 0;
2535 xfs_da_log_buf(args->trans, bp,
2536 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2538 xfs_da_buf_done(bp);
2541 * Commit the flag value change and start the next trans in series.
2543 return xfs_trans_roll(&args->trans, args->dp);
2547 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2548 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2549 * entry given by args->blkno2/index2.
2551 * Note that they could be in different blocks, or in the same block.
2554 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2556 xfs_attr_leafblock_t *leaf1, *leaf2;
2557 xfs_attr_leaf_entry_t *entry1, *entry2;
2558 xfs_attr_leaf_name_remote_t *name_rmt;
2559 xfs_dabuf_t *bp1, *bp2;
2562 xfs_attr_leaf_name_local_t *name_loc;
2563 int namelen1, namelen2;
2564 char *name1, *name2;
2568 * Read the block containing the "old" attr
2570 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2575 ASSERT(bp1 != NULL);
2578 * Read the block containing the "new" attr, if it is different
2580 if (args->blkno2 != args->blkno) {
2581 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2582 -1, &bp2, XFS_ATTR_FORK);
2586 ASSERT(bp2 != NULL);
2592 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2593 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
2594 ASSERT(args->index >= 0);
2595 entry1 = &leaf1->entries[ args->index ];
2598 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2599 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
2600 ASSERT(args->index2 >= 0);
2601 entry2 = &leaf2->entries[ args->index2 ];
2604 if (entry1->flags & XFS_ATTR_LOCAL) {
2605 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
2606 namelen1 = name_loc->namelen;
2607 name1 = (char *)name_loc->nameval;
2609 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2610 namelen1 = name_rmt->namelen;
2611 name1 = (char *)name_rmt->name;
2613 if (entry2->flags & XFS_ATTR_LOCAL) {
2614 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
2615 namelen2 = name_loc->namelen;
2616 name2 = (char *)name_loc->nameval;
2618 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2619 namelen2 = name_rmt->namelen;
2620 name2 = (char *)name_rmt->name;
2622 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2623 ASSERT(namelen1 == namelen2);
2624 ASSERT(memcmp(name1, name2, namelen1) == 0);
2627 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2628 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2630 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2631 xfs_da_log_buf(args->trans, bp1,
2632 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2633 if (args->rmtblkno) {
2634 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2635 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2636 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2637 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2638 xfs_da_log_buf(args->trans, bp1,
2639 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2642 entry2->flags |= XFS_ATTR_INCOMPLETE;
2643 xfs_da_log_buf(args->trans, bp2,
2644 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2645 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2646 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2647 name_rmt->valueblk = 0;
2648 name_rmt->valuelen = 0;
2649 xfs_da_log_buf(args->trans, bp2,
2650 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2652 xfs_da_buf_done(bp1);
2654 xfs_da_buf_done(bp2);
2657 * Commit the flag value change and start the next trans in series.
2659 error = xfs_trans_roll(&args->trans, args->dp);
2664 /*========================================================================
2665 * Indiscriminately delete the entire attribute fork
2666 *========================================================================*/
2669 * Recurse (gasp!) through the attribute nodes until we find leaves.
2670 * We're doing a depth-first traversal in order to invalidate everything.
2673 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2675 xfs_da_blkinfo_t *info;
2681 * Read block 0 to see what we have to work with.
2682 * We only get here if we have extents, since we remove
2683 * the extents in reverse order the extent containing
2684 * block 0 must still be there.
2686 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2689 blkno = xfs_da_blkno(bp);
2692 * Invalidate the tree, even if the "tree" is only a single leaf block.
2693 * This is a depth-first traversal!
2696 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2697 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2698 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2699 error = xfs_attr_leaf_inactive(trans, dp, bp);
2701 error = XFS_ERROR(EIO);
2702 xfs_da_brelse(*trans, bp);
2708 * Invalidate the incore copy of the root block.
2710 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2713 xfs_da_binval(*trans, bp); /* remove from cache */
2715 * Commit the invalidate and start the next transaction.
2717 error = xfs_trans_roll(trans, dp);
2723 * Recurse (gasp!) through the attribute nodes until we find leaves.
2724 * We're doing a depth-first traversal in order to invalidate everything.
2727 xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2730 xfs_da_blkinfo_t *info;
2731 xfs_da_intnode_t *node;
2732 xfs_dablk_t child_fsb;
2733 xfs_daddr_t parent_blkno, child_blkno;
2734 int error, count, i;
2735 xfs_dabuf_t *child_bp;
2738 * Since this code is recursive (gasp!) we must protect ourselves.
2740 if (level > XFS_DA_NODE_MAXDEPTH) {
2741 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2742 return(XFS_ERROR(EIO));
2746 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
2747 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
2748 count = be16_to_cpu(node->hdr.count);
2750 xfs_da_brelse(*trans, bp);
2753 child_fsb = be32_to_cpu(node->btree[0].before);
2754 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2757 * If this is the node level just above the leaves, simply loop
2758 * over the leaves removing all of them. If this is higher up
2759 * in the tree, recurse downward.
2761 for (i = 0; i < count; i++) {
2763 * Read the subsidiary block to see what we have to work with.
2764 * Don't do this in a transaction. This is a depth-first
2765 * traversal of the tree so we may deal with many blocks
2766 * before we come back to this one.
2768 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2773 /* save for re-read later */
2774 child_blkno = xfs_da_blkno(child_bp);
2777 * Invalidate the subtree, however we have to.
2779 info = child_bp->data;
2780 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2781 error = xfs_attr_node_inactive(trans, dp,
2783 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2784 error = xfs_attr_leaf_inactive(trans, dp,
2787 error = XFS_ERROR(EIO);
2788 xfs_da_brelse(*trans, child_bp);
2794 * Remove the subsidiary block from the cache
2797 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2798 &child_bp, XFS_ATTR_FORK);
2801 xfs_da_binval(*trans, child_bp);
2805 * If we're not done, re-read the parent to get the next
2806 * child block number.
2808 if ((i+1) < count) {
2809 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2810 &bp, XFS_ATTR_FORK);
2813 child_fsb = be32_to_cpu(node->btree[i+1].before);
2814 xfs_da_brelse(*trans, bp);
2817 * Atomically commit the whole invalidate stuff.
2819 error = xfs_trans_roll(trans, dp);
2828 * Invalidate all of the "remote" value regions pointed to by a particular
2830 * Note that we must release the lock on the buffer so that we are not
2831 * caught holding something that the logging code wants to flush to disk.
2834 xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2836 xfs_attr_leafblock_t *leaf;
2837 xfs_attr_leaf_entry_t *entry;
2838 xfs_attr_leaf_name_remote_t *name_rmt;
2839 xfs_attr_inactive_list_t *list, *lp;
2840 int error, count, size, tmp, i;
2843 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2846 * Count the number of "remote" value extents.
2849 entry = &leaf->entries[0];
2850 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2851 if (be16_to_cpu(entry->nameidx) &&
2852 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2853 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2854 if (name_rmt->valueblk)
2860 * If there are no "remote" values, we're done.
2863 xfs_da_brelse(*trans, bp);
2868 * Allocate storage for a list of all the "remote" value extents.
2870 size = count * sizeof(xfs_attr_inactive_list_t);
2871 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2874 * Identify each of the "remote" value extents.
2877 entry = &leaf->entries[0];
2878 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2879 if (be16_to_cpu(entry->nameidx) &&
2880 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2881 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2882 if (name_rmt->valueblk) {
2883 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2884 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2885 be32_to_cpu(name_rmt->valuelen));
2890 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2893 * Invalidate each of the "remote" value extents.
2896 for (lp = list, i = 0; i < count; i++, lp++) {
2897 tmp = xfs_attr_leaf_freextent(trans, dp,
2898 lp->valueblk, lp->valuelen);
2901 error = tmp; /* save only the 1st errno */
2904 kmem_free((xfs_caddr_t)list);
2909 * Look at all the extents for this logical region,
2910 * invalidate any buffers that are incore/in transactions.
2913 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2914 xfs_dablk_t blkno, int blkcnt)
2916 xfs_bmbt_irec_t map;
2918 int tblkcnt, dblkcnt, nmap, error;
2923 * Roll through the "value", invalidating the attribute value's
2928 while (tblkcnt > 0) {
2930 * Try to remember where we decided to put the value.
2933 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2934 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2935 NULL, 0, &map, &nmap, NULL, NULL);
2940 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2943 * If it's a hole, these are already unmapped
2944 * so there's nothing to invalidate.
2946 if (map.br_startblock != HOLESTARTBLOCK) {
2948 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
2950 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
2952 bp = xfs_trans_get_buf(*trans,
2953 dp->i_mount->m_ddev_targp,
2954 dblkno, dblkcnt, XFS_BUF_LOCK);
2955 xfs_trans_binval(*trans, bp);
2957 * Roll to next transaction.
2959 error = xfs_trans_roll(trans, dp);
2964 tblkno += map.br_blockcount;
2965 tblkcnt -= map.br_blockcount;