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);
95 struct xfs_mount *mp = bp->b_target->bt_mount;
96 struct xfs_attr_leaf_hdr *hdr = bp->b_addr;
99 block_ok = hdr->info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
101 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
102 xfs_buf_ioerror(bp, EFSCORRUPTED);
107 xfs_attr_leaf_read_verify(
110 xfs_attr_leaf_verify(bp);
114 xfs_attr_leaf_write_verify(
117 xfs_attr_leaf_verify(bp);
120 const struct xfs_buf_ops xfs_attr_leaf_buf_ops = {
121 .verify_read = xfs_attr_leaf_read_verify,
122 .verify_write = xfs_attr_leaf_write_verify,
127 struct xfs_trans *tp,
128 struct xfs_inode *dp,
130 xfs_daddr_t mappedbno,
131 struct xfs_buf **bpp)
133 return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
134 XFS_ATTR_FORK, &xfs_attr_leaf_buf_ops);
137 /*========================================================================
138 * Namespace helper routines
139 *========================================================================*/
142 * If namespace bits don't match return 0.
143 * If all match then return 1.
146 xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
148 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
152 /*========================================================================
153 * External routines when attribute fork size < XFS_LITINO(mp).
154 *========================================================================*/
157 * Query whether the requested number of additional bytes of extended
158 * attribute space will be able to fit inline.
160 * Returns zero if not, else the di_forkoff fork offset to be used in the
161 * literal area for attribute data once the new bytes have been added.
163 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
164 * special case for dev/uuid inodes, they have fixed size data forks.
167 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
170 int minforkoff; /* lower limit on valid forkoff locations */
171 int maxforkoff; /* upper limit on valid forkoff locations */
173 xfs_mount_t *mp = dp->i_mount;
176 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
178 switch (dp->i_d.di_format) {
179 case XFS_DINODE_FMT_DEV:
180 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
181 return (offset >= minforkoff) ? minforkoff : 0;
182 case XFS_DINODE_FMT_UUID:
183 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
184 return (offset >= minforkoff) ? minforkoff : 0;
188 * If the requested numbers of bytes is smaller or equal to the
189 * current attribute fork size we can always proceed.
191 * Note that if_bytes in the data fork might actually be larger than
192 * the current data fork size is due to delalloc extents. In that
193 * case either the extent count will go down when they are converted
194 * to real extents, or the delalloc conversion will take care of the
195 * literal area rebalancing.
197 if (bytes <= XFS_IFORK_ASIZE(dp))
198 return dp->i_d.di_forkoff;
201 * For attr2 we can try to move the forkoff if there is space in the
202 * literal area, but for the old format we are done if there is no
203 * space in the fixed attribute fork.
205 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
208 dsize = dp->i_df.if_bytes;
210 switch (dp->i_d.di_format) {
211 case XFS_DINODE_FMT_EXTENTS:
213 * If there is no attr fork and the data fork is extents,
214 * determine if creating the default attr fork will result
215 * in the extents form migrating to btree. If so, the
216 * minimum offset only needs to be the space required for
219 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
220 xfs_default_attroffset(dp))
221 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
223 case XFS_DINODE_FMT_BTREE:
225 * If we have a data btree then keep forkoff if we have one,
226 * otherwise we are adding a new attr, so then we set
227 * minforkoff to where the btree root can finish so we have
228 * plenty of room for attrs
230 if (dp->i_d.di_forkoff) {
231 if (offset < dp->i_d.di_forkoff)
233 return dp->i_d.di_forkoff;
235 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
240 * A data fork btree root must have space for at least
241 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
243 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
244 minforkoff = roundup(minforkoff, 8) >> 3;
246 /* attr fork btree root can have at least this many key/ptr pairs */
247 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
248 XFS_BMDR_SPACE_CALC(MINABTPTRS);
249 maxforkoff = maxforkoff >> 3; /* rounded down */
251 if (offset >= maxforkoff)
253 if (offset >= minforkoff)
259 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
262 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
264 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
265 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
266 spin_lock(&mp->m_sb_lock);
267 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
268 xfs_sb_version_addattr2(&mp->m_sb);
269 spin_unlock(&mp->m_sb_lock);
270 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
272 spin_unlock(&mp->m_sb_lock);
277 * Create the initial contents of a shortform attribute list.
280 xfs_attr_shortform_create(xfs_da_args_t *args)
282 xfs_attr_sf_hdr_t *hdr;
286 trace_xfs_attr_sf_create(args);
292 ASSERT(ifp->if_bytes == 0);
293 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
294 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
295 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
296 ifp->if_flags |= XFS_IFINLINE;
298 ASSERT(ifp->if_flags & XFS_IFINLINE);
300 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
301 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
303 hdr->totsize = cpu_to_be16(sizeof(*hdr));
304 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
308 * Add a name/value pair to the shortform attribute list.
309 * Overflow from the inode has already been checked for.
312 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
314 xfs_attr_shortform_t *sf;
315 xfs_attr_sf_entry_t *sfe;
321 trace_xfs_attr_sf_add(args);
325 dp->i_d.di_forkoff = forkoff;
328 ASSERT(ifp->if_flags & XFS_IFINLINE);
329 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
331 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
333 if (sfe->namelen != args->namelen)
335 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
337 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
343 offset = (char *)sfe - (char *)sf;
344 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
345 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
346 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
347 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
349 sfe->namelen = args->namelen;
350 sfe->valuelen = args->valuelen;
351 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
352 memcpy(sfe->nameval, args->name, args->namelen);
353 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
355 be16_add_cpu(&sf->hdr.totsize, size);
356 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
358 xfs_sbversion_add_attr2(mp, args->trans);
362 * After the last attribute is removed revert to original inode format,
363 * making all literal area available to the data fork once more.
367 struct xfs_inode *ip,
368 struct xfs_trans *tp)
370 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
371 ip->i_d.di_forkoff = 0;
372 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
374 ASSERT(ip->i_d.di_anextents == 0);
375 ASSERT(ip->i_afp == NULL);
377 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
381 * Remove an attribute from the shortform attribute list structure.
384 xfs_attr_shortform_remove(xfs_da_args_t *args)
386 xfs_attr_shortform_t *sf;
387 xfs_attr_sf_entry_t *sfe;
388 int base, size=0, end, totsize, i;
392 trace_xfs_attr_sf_remove(args);
396 base = sizeof(xfs_attr_sf_hdr_t);
397 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
400 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
402 size = XFS_ATTR_SF_ENTSIZE(sfe);
403 if (sfe->namelen != args->namelen)
405 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
407 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
412 return(XFS_ERROR(ENOATTR));
415 * Fix up the attribute fork data, covering the hole
418 totsize = be16_to_cpu(sf->hdr.totsize);
420 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
422 be16_add_cpu(&sf->hdr.totsize, -size);
425 * Fix up the start offset of the attribute fork
428 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
429 (mp->m_flags & XFS_MOUNT_ATTR2) &&
430 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
431 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
432 xfs_attr_fork_reset(dp, args->trans);
434 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
435 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
436 ASSERT(dp->i_d.di_forkoff);
437 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
438 (args->op_flags & XFS_DA_OP_ADDNAME) ||
439 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
440 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
441 xfs_trans_log_inode(args->trans, dp,
442 XFS_ILOG_CORE | XFS_ILOG_ADATA);
445 xfs_sbversion_add_attr2(mp, args->trans);
451 * Look up a name in a shortform attribute list structure.
455 xfs_attr_shortform_lookup(xfs_da_args_t *args)
457 xfs_attr_shortform_t *sf;
458 xfs_attr_sf_entry_t *sfe;
462 trace_xfs_attr_sf_lookup(args);
464 ifp = args->dp->i_afp;
465 ASSERT(ifp->if_flags & XFS_IFINLINE);
466 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
468 for (i = 0; i < sf->hdr.count;
469 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
470 if (sfe->namelen != args->namelen)
472 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
474 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
476 return(XFS_ERROR(EEXIST));
478 return(XFS_ERROR(ENOATTR));
482 * Look up a name in a shortform attribute list structure.
486 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
488 xfs_attr_shortform_t *sf;
489 xfs_attr_sf_entry_t *sfe;
492 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
493 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
495 for (i = 0; i < sf->hdr.count;
496 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
497 if (sfe->namelen != args->namelen)
499 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
501 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
503 if (args->flags & ATTR_KERNOVAL) {
504 args->valuelen = sfe->valuelen;
505 return(XFS_ERROR(EEXIST));
507 if (args->valuelen < sfe->valuelen) {
508 args->valuelen = sfe->valuelen;
509 return(XFS_ERROR(ERANGE));
511 args->valuelen = sfe->valuelen;
512 memcpy(args->value, &sfe->nameval[args->namelen],
514 return(XFS_ERROR(EEXIST));
516 return(XFS_ERROR(ENOATTR));
520 * Convert from using the shortform to the leaf.
523 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
526 xfs_attr_shortform_t *sf;
527 xfs_attr_sf_entry_t *sfe;
535 trace_xfs_attr_sf_to_leaf(args);
539 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
540 size = be16_to_cpu(sf->hdr.totsize);
541 tmpbuffer = kmem_alloc(size, KM_SLEEP);
542 ASSERT(tmpbuffer != NULL);
543 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
544 sf = (xfs_attr_shortform_t *)tmpbuffer;
546 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
548 error = xfs_da_grow_inode(args, &blkno);
551 * If we hit an IO error middle of the transaction inside
552 * grow_inode(), we may have inconsistent data. Bail out.
556 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
557 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
562 error = xfs_attr_leaf_create(args, blkno, &bp);
564 error = xfs_da_shrink_inode(args, 0, bp);
568 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
569 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
573 memset((char *)&nargs, 0, sizeof(nargs));
575 nargs.firstblock = args->firstblock;
576 nargs.flist = args->flist;
577 nargs.total = args->total;
578 nargs.whichfork = XFS_ATTR_FORK;
579 nargs.trans = args->trans;
580 nargs.op_flags = XFS_DA_OP_OKNOENT;
583 for (i = 0; i < sf->hdr.count; i++) {
584 nargs.name = sfe->nameval;
585 nargs.namelen = sfe->namelen;
586 nargs.value = &sfe->nameval[nargs.namelen];
587 nargs.valuelen = sfe->valuelen;
588 nargs.hashval = xfs_da_hashname(sfe->nameval,
590 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
591 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
592 ASSERT(error == ENOATTR);
593 error = xfs_attr_leaf_add(bp, &nargs);
594 ASSERT(error != ENOSPC);
597 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
602 kmem_free(tmpbuffer);
607 xfs_attr_shortform_compare(const void *a, const void *b)
609 xfs_attr_sf_sort_t *sa, *sb;
611 sa = (xfs_attr_sf_sort_t *)a;
612 sb = (xfs_attr_sf_sort_t *)b;
613 if (sa->hash < sb->hash) {
615 } else if (sa->hash > sb->hash) {
618 return(sa->entno - sb->entno);
623 #define XFS_ISRESET_CURSOR(cursor) \
624 (!((cursor)->initted) && !((cursor)->hashval) && \
625 !((cursor)->blkno) && !((cursor)->offset))
627 * Copy out entries of shortform attribute lists for attr_list().
628 * Shortform attribute lists are not stored in hashval sorted order.
629 * If the output buffer is not large enough to hold them all, then we
630 * we have to calculate each entries' hashvalue and sort them before
631 * we can begin returning them to the user.
635 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
637 attrlist_cursor_kern_t *cursor;
638 xfs_attr_sf_sort_t *sbuf, *sbp;
639 xfs_attr_shortform_t *sf;
640 xfs_attr_sf_entry_t *sfe;
642 int sbsize, nsbuf, count, i;
645 ASSERT(context != NULL);
648 ASSERT(dp->i_afp != NULL);
649 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
653 cursor = context->cursor;
654 ASSERT(cursor != NULL);
656 trace_xfs_attr_list_sf(context);
659 * If the buffer is large enough and the cursor is at the start,
660 * do not bother with sorting since we will return everything in
661 * one buffer and another call using the cursor won't need to be
663 * Note the generous fudge factor of 16 overhead bytes per entry.
664 * If bufsize is zero then put_listent must be a search function
665 * and can just scan through what we have.
667 if (context->bufsize == 0 ||
668 (XFS_ISRESET_CURSOR(cursor) &&
669 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
670 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
671 error = context->put_listent(context,
676 &sfe->nameval[sfe->namelen]);
679 * Either search callback finished early or
680 * didn't fit it all in the buffer after all.
682 if (context->seen_enough)
687 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
689 trace_xfs_attr_list_sf_all(context);
693 /* do no more for a search callback */
694 if (context->bufsize == 0)
698 * It didn't all fit, so we have to sort everything on hashval.
700 sbsize = sf->hdr.count * sizeof(*sbuf);
701 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
704 * Scan the attribute list for the rest of the entries, storing
705 * the relevant info from only those that match into a buffer.
708 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
710 ((char *)sfe < (char *)sf) ||
711 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
712 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
714 context->dp->i_mount, sfe);
716 return XFS_ERROR(EFSCORRUPTED);
720 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
721 sbp->name = sfe->nameval;
722 sbp->namelen = sfe->namelen;
723 /* These are bytes, and both on-disk, don't endian-flip */
724 sbp->valuelen = sfe->valuelen;
725 sbp->flags = sfe->flags;
726 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
732 * Sort the entries on hash then entno.
734 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
737 * Re-find our place IN THE SORTED LIST.
742 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
743 if (sbp->hash == cursor->hashval) {
744 if (cursor->offset == count) {
748 } else if (sbp->hash > cursor->hashval) {
758 * Loop putting entries into the user buffer.
760 for ( ; i < nsbuf; i++, sbp++) {
761 if (cursor->hashval != sbp->hash) {
762 cursor->hashval = sbp->hash;
765 error = context->put_listent(context,
770 &sbp->name[sbp->namelen]);
773 if (context->seen_enough)
783 * Check a leaf attribute block to see if all the entries would fit into
784 * a shortform attribute list.
787 xfs_attr_shortform_allfit(
789 struct xfs_inode *dp)
791 xfs_attr_leafblock_t *leaf;
792 xfs_attr_leaf_entry_t *entry;
793 xfs_attr_leaf_name_local_t *name_loc;
797 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
799 entry = &leaf->entries[0];
800 bytes = sizeof(struct xfs_attr_sf_hdr);
801 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
802 if (entry->flags & XFS_ATTR_INCOMPLETE)
803 continue; /* don't copy partial entries */
804 if (!(entry->flags & XFS_ATTR_LOCAL))
806 name_loc = xfs_attr_leaf_name_local(leaf, i);
807 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
809 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
811 bytes += sizeof(struct xfs_attr_sf_entry)-1
813 + be16_to_cpu(name_loc->valuelen);
815 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
816 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
817 (bytes == sizeof(struct xfs_attr_sf_hdr)))
819 return(xfs_attr_shortform_bytesfit(dp, bytes));
823 * Convert a leaf attribute list to shortform attribute list
826 xfs_attr_leaf_to_shortform(
831 xfs_attr_leafblock_t *leaf;
832 xfs_attr_leaf_entry_t *entry;
833 xfs_attr_leaf_name_local_t *name_loc;
839 trace_xfs_attr_leaf_to_sf(args);
842 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
843 ASSERT(tmpbuffer != NULL);
846 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
847 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
848 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
849 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
852 * Clean out the prior contents of the attribute list.
854 error = xfs_da_shrink_inode(args, 0, bp);
859 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
860 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
861 xfs_attr_fork_reset(dp, args->trans);
865 xfs_attr_shortform_create(args);
868 * Copy the attributes
870 memset((char *)&nargs, 0, sizeof(nargs));
872 nargs.firstblock = args->firstblock;
873 nargs.flist = args->flist;
874 nargs.total = args->total;
875 nargs.whichfork = XFS_ATTR_FORK;
876 nargs.trans = args->trans;
877 nargs.op_flags = XFS_DA_OP_OKNOENT;
878 entry = &leaf->entries[0];
879 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
880 if (entry->flags & XFS_ATTR_INCOMPLETE)
881 continue; /* don't copy partial entries */
884 ASSERT(entry->flags & XFS_ATTR_LOCAL);
885 name_loc = xfs_attr_leaf_name_local(leaf, i);
886 nargs.name = name_loc->nameval;
887 nargs.namelen = name_loc->namelen;
888 nargs.value = &name_loc->nameval[nargs.namelen];
889 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
890 nargs.hashval = be32_to_cpu(entry->hashval);
891 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
892 xfs_attr_shortform_add(&nargs, forkoff);
897 kmem_free(tmpbuffer);
902 * Convert from using a single leaf to a root node and a leaf.
905 xfs_attr_leaf_to_node(xfs_da_args_t *args)
907 xfs_attr_leafblock_t *leaf;
908 xfs_da_intnode_t *node;
910 struct xfs_buf *bp1, *bp2;
914 trace_xfs_attr_leaf_to_node(args);
918 error = xfs_da_grow_inode(args, &blkno);
921 error = xfs_attr_leaf_read(args->trans, args->dp, 0, -1, &bp1);
926 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
930 bp2->b_ops = bp1->b_ops;
931 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(dp->i_mount));
933 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
936 * Set up the new root node.
938 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
943 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
944 /* both on-disk, don't endian-flip twice */
945 node->btree[0].hashval =
946 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
947 node->btree[0].before = cpu_to_be32(blkno);
948 node->hdr.count = cpu_to_be16(1);
949 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
956 /*========================================================================
957 * Routines used for growing the Btree.
958 *========================================================================*/
961 * Create the initial contents of a leaf attribute list
962 * or a leaf in a node attribute list.
965 xfs_attr_leaf_create(
968 struct xfs_buf **bpp)
970 xfs_attr_leafblock_t *leaf;
971 xfs_attr_leaf_hdr_t *hdr;
976 trace_xfs_attr_leaf_create(args);
980 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
984 bp->b_ops = &xfs_attr_leaf_buf_ops;
986 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
988 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
989 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
990 if (!hdr->firstused) {
991 hdr->firstused = cpu_to_be16(
992 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
995 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
996 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
997 sizeof(xfs_attr_leaf_hdr_t));
999 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
1006 * Split the leaf node, rebalance, then add the new entry.
1009 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
1010 xfs_da_state_blk_t *newblk)
1015 trace_xfs_attr_leaf_split(state->args);
1018 * Allocate space for a new leaf node.
1020 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1021 error = xfs_da_grow_inode(state->args, &blkno);
1024 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
1027 newblk->blkno = blkno;
1028 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1031 * Rebalance the entries across the two leaves.
1032 * NOTE: rebalance() currently depends on the 2nd block being empty.
1034 xfs_attr_leaf_rebalance(state, oldblk, newblk);
1035 error = xfs_da_blk_link(state, oldblk, newblk);
1040 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1041 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1042 * "new" attrs info. Will need the "old" info to remove it later.
1044 * Insert the "new" entry in the correct block.
1046 if (state->inleaf) {
1047 trace_xfs_attr_leaf_add_old(state->args);
1048 error = xfs_attr_leaf_add(oldblk->bp, state->args);
1050 trace_xfs_attr_leaf_add_new(state->args);
1051 error = xfs_attr_leaf_add(newblk->bp, state->args);
1055 * Update last hashval in each block since we added the name.
1057 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1058 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1063 * Add a name to the leaf attribute list structure.
1068 struct xfs_da_args *args)
1070 xfs_attr_leafblock_t *leaf;
1071 xfs_attr_leaf_hdr_t *hdr;
1072 xfs_attr_leaf_map_t *map;
1073 int tablesize, entsize, sum, tmp, i;
1075 trace_xfs_attr_leaf_add(args);
1078 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1079 ASSERT((args->index >= 0)
1080 && (args->index <= be16_to_cpu(leaf->hdr.count)));
1082 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1083 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1086 * Search through freemap for first-fit on new name length.
1087 * (may need to figure in size of entry struct too)
1089 tablesize = (be16_to_cpu(hdr->count) + 1)
1090 * sizeof(xfs_attr_leaf_entry_t)
1091 + sizeof(xfs_attr_leaf_hdr_t);
1092 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1093 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1094 if (tablesize > be16_to_cpu(hdr->firstused)) {
1095 sum += be16_to_cpu(map->size);
1099 continue; /* no space in this map */
1101 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
1102 tmp += sizeof(xfs_attr_leaf_entry_t);
1103 if (be16_to_cpu(map->size) >= tmp) {
1104 tmp = xfs_attr_leaf_add_work(bp, args, i);
1107 sum += be16_to_cpu(map->size);
1111 * If there are no holes in the address space of the block,
1112 * and we don't have enough freespace, then compaction will do us
1113 * no good and we should just give up.
1115 if (!hdr->holes && (sum < entsize))
1116 return(XFS_ERROR(ENOSPC));
1119 * Compact the entries to coalesce free space.
1120 * This may change the hdr->count via dropping INCOMPLETE entries.
1122 xfs_attr_leaf_compact(args, bp);
1125 * After compaction, the block is guaranteed to have only one
1126 * free region, in freemap[0]. If it is not big enough, give up.
1128 if (be16_to_cpu(hdr->freemap[0].size)
1129 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1130 return(XFS_ERROR(ENOSPC));
1132 return(xfs_attr_leaf_add_work(bp, args, 0));
1136 * Add a name to a leaf attribute list structure.
1139 xfs_attr_leaf_add_work(
1141 xfs_da_args_t *args,
1144 xfs_attr_leafblock_t *leaf;
1145 xfs_attr_leaf_hdr_t *hdr;
1146 xfs_attr_leaf_entry_t *entry;
1147 xfs_attr_leaf_name_local_t *name_loc;
1148 xfs_attr_leaf_name_remote_t *name_rmt;
1149 xfs_attr_leaf_map_t *map;
1153 trace_xfs_attr_leaf_add_work(args);
1156 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1158 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1159 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
1162 * Force open some space in the entry array and fill it in.
1164 entry = &leaf->entries[args->index];
1165 if (args->index < be16_to_cpu(hdr->count)) {
1166 tmp = be16_to_cpu(hdr->count) - args->index;
1167 tmp *= sizeof(xfs_attr_leaf_entry_t);
1168 memmove((char *)(entry+1), (char *)entry, tmp);
1169 xfs_trans_log_buf(args->trans, bp,
1170 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1172 be16_add_cpu(&hdr->count, 1);
1175 * Allocate space for the new string (at the end of the run).
1177 map = &hdr->freemap[mapindex];
1178 mp = args->trans->t_mountp;
1179 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1180 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1181 ASSERT(be16_to_cpu(map->size) >=
1182 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1183 mp->m_sb.sb_blocksize, NULL));
1184 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1185 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1186 be16_add_cpu(&map->size,
1187 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1188 mp->m_sb.sb_blocksize, &tmp));
1189 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1190 be16_to_cpu(map->size));
1191 entry->hashval = cpu_to_be32(args->hashval);
1192 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1193 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1194 if (args->op_flags & XFS_DA_OP_RENAME) {
1195 entry->flags |= XFS_ATTR_INCOMPLETE;
1196 if ((args->blkno2 == args->blkno) &&
1197 (args->index2 <= args->index)) {
1201 xfs_trans_log_buf(args->trans, bp,
1202 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1203 ASSERT((args->index == 0) ||
1204 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1205 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
1206 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1209 * For "remote" attribute values, simply note that we need to
1210 * allocate space for the "remote" value. We can't actually
1211 * allocate the extents in this transaction, and we can't decide
1212 * which blocks they should be as we might allocate more blocks
1213 * as part of this transaction (a split operation for example).
1215 if (entry->flags & XFS_ATTR_LOCAL) {
1216 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
1217 name_loc->namelen = args->namelen;
1218 name_loc->valuelen = cpu_to_be16(args->valuelen);
1219 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1220 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1221 be16_to_cpu(name_loc->valuelen));
1223 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
1224 name_rmt->namelen = args->namelen;
1225 memcpy((char *)name_rmt->name, args->name, args->namelen);
1226 entry->flags |= XFS_ATTR_INCOMPLETE;
1228 name_rmt->valuelen = 0;
1229 name_rmt->valueblk = 0;
1231 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1233 xfs_trans_log_buf(args->trans, bp,
1234 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1235 xfs_attr_leaf_entsize(leaf, args->index)));
1238 * Update the control info for this leaf node
1240 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
1241 /* both on-disk, don't endian-flip twice */
1242 hdr->firstused = entry->nameidx;
1244 ASSERT(be16_to_cpu(hdr->firstused) >=
1245 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1246 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
1247 + sizeof(xfs_attr_leaf_hdr_t);
1248 map = &hdr->freemap[0];
1249 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1250 if (be16_to_cpu(map->base) == tmp) {
1251 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1252 be16_add_cpu(&map->size,
1253 -((int)sizeof(xfs_attr_leaf_entry_t)));
1256 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
1257 xfs_trans_log_buf(args->trans, bp,
1258 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1263 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1266 xfs_attr_leaf_compact(
1267 struct xfs_da_args *args,
1270 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1271 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1272 struct xfs_trans *trans = args->trans;
1273 struct xfs_mount *mp = trans->t_mountp;
1276 trace_xfs_attr_leaf_compact(args);
1278 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1279 ASSERT(tmpbuffer != NULL);
1280 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1281 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
1284 * Copy basic information
1286 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1287 leaf_d = bp->b_addr;
1288 hdr_s = &leaf_s->hdr;
1289 hdr_d = &leaf_d->hdr;
1290 hdr_d->info = hdr_s->info; /* struct copy */
1291 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
1292 /* handle truncation gracefully */
1293 if (!hdr_d->firstused) {
1294 hdr_d->firstused = cpu_to_be16(
1295 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1297 hdr_d->usedbytes = 0;
1300 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1301 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1302 sizeof(xfs_attr_leaf_hdr_t));
1305 * Copy all entry's in the same (sorted) order,
1306 * but allocate name/value pairs packed and in sequence.
1308 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1309 be16_to_cpu(hdr_s->count), mp);
1310 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1312 kmem_free(tmpbuffer);
1316 * Redistribute the attribute list entries between two leaf nodes,
1317 * taking into account the size of the new entry.
1319 * NOTE: if new block is empty, then it will get the upper half of the
1320 * old block. At present, all (one) callers pass in an empty second block.
1322 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1323 * to match what it is doing in splitting the attribute leaf block. Those
1324 * values are used in "atomic rename" operations on attributes. Note that
1325 * the "new" and "old" values can end up in different blocks.
1328 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1329 xfs_da_state_blk_t *blk2)
1331 xfs_da_args_t *args;
1332 xfs_da_state_blk_t *tmp_blk;
1333 xfs_attr_leafblock_t *leaf1, *leaf2;
1334 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1335 int count, totallen, max, space, swap;
1338 * Set up environment.
1340 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1341 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1342 leaf1 = blk1->bp->b_addr;
1343 leaf2 = blk2->bp->b_addr;
1344 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1345 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1346 ASSERT(leaf2->hdr.count == 0);
1349 trace_xfs_attr_leaf_rebalance(args);
1352 * Check ordering of blocks, reverse if it makes things simpler.
1354 * NOTE: Given that all (current) callers pass in an empty
1355 * second block, this code should never set "swap".
1358 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1362 leaf1 = blk1->bp->b_addr;
1363 leaf2 = blk2->bp->b_addr;
1370 * Examine entries until we reduce the absolute difference in
1371 * byte usage between the two blocks to a minimum. Then get
1372 * the direction to copy and the number of elements to move.
1374 * "inleaf" is true if the new entry should be inserted into blk1.
1375 * If "swap" is also true, then reverse the sense of "inleaf".
1377 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1380 state->inleaf = !state->inleaf;
1383 * Move any entries required from leaf to leaf:
1385 if (count < be16_to_cpu(hdr1->count)) {
1387 * Figure the total bytes to be added to the destination leaf.
1389 /* number entries being moved */
1390 count = be16_to_cpu(hdr1->count) - count;
1391 space = be16_to_cpu(hdr1->usedbytes) - totallen;
1392 space += count * sizeof(xfs_attr_leaf_entry_t);
1395 * leaf2 is the destination, compact it if it looks tight.
1397 max = be16_to_cpu(hdr2->firstused)
1398 - sizeof(xfs_attr_leaf_hdr_t);
1399 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
1401 xfs_attr_leaf_compact(args, blk2->bp);
1404 * Move high entries from leaf1 to low end of leaf2.
1406 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
1407 leaf2, 0, count, state->mp);
1409 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1410 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1411 } else if (count > be16_to_cpu(hdr1->count)) {
1413 * I assert that since all callers pass in an empty
1414 * second buffer, this code should never execute.
1419 * Figure the total bytes to be added to the destination leaf.
1421 /* number entries being moved */
1422 count -= be16_to_cpu(hdr1->count);
1423 space = totallen - be16_to_cpu(hdr1->usedbytes);
1424 space += count * sizeof(xfs_attr_leaf_entry_t);
1427 * leaf1 is the destination, compact it if it looks tight.
1429 max = be16_to_cpu(hdr1->firstused)
1430 - sizeof(xfs_attr_leaf_hdr_t);
1431 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
1433 xfs_attr_leaf_compact(args, blk1->bp);
1436 * Move low entries from leaf2 to high end of leaf1.
1438 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1439 be16_to_cpu(hdr1->count), count, state->mp);
1441 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1442 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1446 * Copy out last hashval in each block for B-tree code.
1448 blk1->hashval = be32_to_cpu(
1449 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1450 blk2->hashval = be32_to_cpu(
1451 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
1454 * Adjust the expected index for insertion.
1455 * NOTE: this code depends on the (current) situation that the
1456 * second block was originally empty.
1458 * If the insertion point moved to the 2nd block, we must adjust
1459 * the index. We must also track the entry just following the
1460 * new entry for use in an "atomic rename" operation, that entry
1461 * is always the "old" entry and the "new" entry is what we are
1462 * inserting. The index/blkno fields refer to the "old" entry,
1463 * while the index2/blkno2 fields refer to the "new" entry.
1465 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
1466 ASSERT(state->inleaf == 0);
1467 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
1468 args->index = args->index2 = blk2->index;
1469 args->blkno = args->blkno2 = blk2->blkno;
1470 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
1471 if (state->inleaf) {
1472 args->index = blk1->index;
1473 args->blkno = blk1->blkno;
1475 args->blkno2 = blk2->blkno;
1478 * On a double leaf split, the original attr location
1479 * is already stored in blkno2/index2, so don't
1480 * overwrite it overwise we corrupt the tree.
1482 blk2->index = blk1->index
1483 - be16_to_cpu(leaf1->hdr.count);
1484 args->index = blk2->index;
1485 args->blkno = blk2->blkno;
1486 if (!state->extravalid) {
1488 * set the new attr location to match the old
1489 * one and let the higher level split code
1490 * decide where in the leaf to place it.
1492 args->index2 = blk2->index;
1493 args->blkno2 = blk2->blkno;
1497 ASSERT(state->inleaf == 1);
1498 args->index = args->index2 = blk1->index;
1499 args->blkno = args->blkno2 = blk1->blkno;
1504 * Examine entries until we reduce the absolute difference in
1505 * byte usage between the two blocks to a minimum.
1506 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1507 * GROT: there will always be enough room in either block for a new entry.
1508 * GROT: Do a double-split for this case?
1511 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1512 xfs_da_state_blk_t *blk1,
1513 xfs_da_state_blk_t *blk2,
1514 int *countarg, int *usedbytesarg)
1516 xfs_attr_leafblock_t *leaf1, *leaf2;
1517 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1518 xfs_attr_leaf_entry_t *entry;
1519 int count, max, index, totallen, half;
1520 int lastdelta, foundit, tmp;
1523 * Set up environment.
1525 leaf1 = blk1->bp->b_addr;
1526 leaf2 = blk2->bp->b_addr;
1533 * Examine entries until we reduce the absolute difference in
1534 * byte usage between the two blocks to a minimum.
1536 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
1537 half = (max+1) * sizeof(*entry);
1538 half += be16_to_cpu(hdr1->usedbytes) +
1539 be16_to_cpu(hdr2->usedbytes) +
1540 xfs_attr_leaf_newentsize(
1541 state->args->namelen,
1542 state->args->valuelen,
1543 state->blocksize, NULL);
1545 lastdelta = state->blocksize;
1546 entry = &leaf1->entries[0];
1547 for (count = index = 0; count < max; entry++, index++, count++) {
1549 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1551 * The new entry is in the first block, account for it.
1553 if (count == blk1->index) {
1554 tmp = totallen + sizeof(*entry) +
1555 xfs_attr_leaf_newentsize(
1556 state->args->namelen,
1557 state->args->valuelen,
1558 state->blocksize, NULL);
1559 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1561 lastdelta = XFS_ATTR_ABS(half - tmp);
1567 * Wrap around into the second block if necessary.
1569 if (count == be16_to_cpu(hdr1->count)) {
1571 entry = &leaf1->entries[0];
1576 * Figure out if next leaf entry would be too much.
1578 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1580 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1582 lastdelta = XFS_ATTR_ABS(half - tmp);
1588 * Calculate the number of usedbytes that will end up in lower block.
1589 * If new entry not in lower block, fix up the count.
1591 totallen -= count * sizeof(*entry);
1593 totallen -= sizeof(*entry) +
1594 xfs_attr_leaf_newentsize(
1595 state->args->namelen,
1596 state->args->valuelen,
1597 state->blocksize, NULL);
1601 *usedbytesarg = totallen;
1605 /*========================================================================
1606 * Routines used for shrinking the Btree.
1607 *========================================================================*/
1610 * Check a leaf block and its neighbors to see if the block should be
1611 * collapsed into one or the other neighbor. Always keep the block
1612 * with the smaller block number.
1613 * If the current block is over 50% full, don't try to join it, return 0.
1614 * If the block is empty, fill in the state structure and return 2.
1615 * If it can be collapsed, fill in the state structure and return 1.
1616 * If nothing can be done, return 0.
1618 * GROT: allow for INCOMPLETE entries in calculation.
1621 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1623 xfs_attr_leafblock_t *leaf;
1624 xfs_da_state_blk_t *blk;
1625 xfs_da_blkinfo_t *info;
1626 int count, bytes, forward, error, retval, i;
1630 trace_xfs_attr_leaf_toosmall(state->args);
1633 * Check for the degenerate case of the block being over 50% full.
1634 * If so, it's not worth even looking to see if we might be able
1635 * to coalesce with a sibling.
1637 blk = &state->path.blk[ state->path.active-1 ];
1638 info = blk->bp->b_addr;
1639 ASSERT(info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1640 leaf = (xfs_attr_leafblock_t *)info;
1641 count = be16_to_cpu(leaf->hdr.count);
1642 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1643 count * sizeof(xfs_attr_leaf_entry_t) +
1644 be16_to_cpu(leaf->hdr.usedbytes);
1645 if (bytes > (state->blocksize >> 1)) {
1646 *action = 0; /* blk over 50%, don't try to join */
1651 * Check for the degenerate case of the block being empty.
1652 * If the block is empty, we'll simply delete it, no need to
1653 * coalesce it with a sibling block. We choose (arbitrarily)
1654 * to merge with the forward block unless it is NULL.
1658 * Make altpath point to the block we want to keep and
1659 * path point to the block we want to drop (this one).
1661 forward = (info->forw != 0);
1662 memcpy(&state->altpath, &state->path, sizeof(state->path));
1663 error = xfs_da_path_shift(state, &state->altpath, forward,
1676 * Examine each sibling block to see if we can coalesce with
1677 * at least 25% free space to spare. We need to figure out
1678 * whether to merge with the forward or the backward block.
1679 * We prefer coalescing with the lower numbered sibling so as
1680 * to shrink an attribute list over time.
1682 /* start with smaller blk num */
1683 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
1684 for (i = 0; i < 2; forward = !forward, i++) {
1686 blkno = be32_to_cpu(info->forw);
1688 blkno = be32_to_cpu(info->back);
1691 error = xfs_attr_leaf_read(state->args->trans, state->args->dp,
1696 leaf = (xfs_attr_leafblock_t *)info;
1697 count = be16_to_cpu(leaf->hdr.count);
1698 bytes = state->blocksize - (state->blocksize>>2);
1699 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1701 count += be16_to_cpu(leaf->hdr.count);
1702 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1703 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1704 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1705 xfs_trans_brelse(state->args->trans, bp);
1707 break; /* fits with at least 25% to spare */
1715 * Make altpath point to the block we want to keep (the lower
1716 * numbered block) and path point to the block we want to drop.
1718 memcpy(&state->altpath, &state->path, sizeof(state->path));
1719 if (blkno < blk->blkno) {
1720 error = xfs_da_path_shift(state, &state->altpath, forward,
1723 error = xfs_da_path_shift(state, &state->path, forward,
1737 * Remove a name from the leaf attribute list structure.
1739 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1740 * If two leaves are 37% full, when combined they will leave 25% free.
1743 xfs_attr_leaf_remove(
1745 xfs_da_args_t *args)
1747 xfs_attr_leafblock_t *leaf;
1748 xfs_attr_leaf_hdr_t *hdr;
1749 xfs_attr_leaf_map_t *map;
1750 xfs_attr_leaf_entry_t *entry;
1751 int before, after, smallest, entsize;
1752 int tablesize, tmp, i;
1755 trace_xfs_attr_leaf_remove(args);
1758 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1760 mp = args->trans->t_mountp;
1761 ASSERT((be16_to_cpu(hdr->count) > 0)
1762 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
1763 ASSERT((args->index >= 0)
1764 && (args->index < be16_to_cpu(hdr->count)));
1765 ASSERT(be16_to_cpu(hdr->firstused) >=
1766 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1767 entry = &leaf->entries[args->index];
1768 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1769 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1772 * Scan through free region table:
1773 * check for adjacency of free'd entry with an existing one,
1774 * find smallest free region in case we need to replace it,
1775 * adjust any map that borders the entry table,
1777 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
1778 + sizeof(xfs_attr_leaf_hdr_t);
1779 map = &hdr->freemap[0];
1780 tmp = be16_to_cpu(map->size);
1781 before = after = -1;
1782 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1783 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1784 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1785 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1786 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1787 if (be16_to_cpu(map->base) == tablesize) {
1788 be16_add_cpu(&map->base,
1789 -((int)sizeof(xfs_attr_leaf_entry_t)));
1790 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
1793 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
1794 == be16_to_cpu(entry->nameidx)) {
1796 } else if (be16_to_cpu(map->base)
1797 == (be16_to_cpu(entry->nameidx) + entsize)) {
1799 } else if (be16_to_cpu(map->size) < tmp) {
1800 tmp = be16_to_cpu(map->size);
1806 * Coalesce adjacent freemap regions,
1807 * or replace the smallest region.
1809 if ((before >= 0) || (after >= 0)) {
1810 if ((before >= 0) && (after >= 0)) {
1811 map = &hdr->freemap[before];
1812 be16_add_cpu(&map->size, entsize);
1813 be16_add_cpu(&map->size,
1814 be16_to_cpu(hdr->freemap[after].size));
1815 hdr->freemap[after].base = 0;
1816 hdr->freemap[after].size = 0;
1817 } else if (before >= 0) {
1818 map = &hdr->freemap[before];
1819 be16_add_cpu(&map->size, entsize);
1821 map = &hdr->freemap[after];
1822 /* both on-disk, don't endian flip twice */
1823 map->base = entry->nameidx;
1824 be16_add_cpu(&map->size, entsize);
1828 * Replace smallest region (if it is smaller than free'd entry)
1830 map = &hdr->freemap[smallest];
1831 if (be16_to_cpu(map->size) < entsize) {
1832 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
1833 map->size = cpu_to_be16(entsize);
1838 * Did we remove the first entry?
1840 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
1846 * Compress the remaining entries and zero out the removed stuff.
1848 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
1849 be16_add_cpu(&hdr->usedbytes, -entsize);
1850 xfs_trans_log_buf(args->trans, bp,
1851 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1854 tmp = (be16_to_cpu(hdr->count) - args->index)
1855 * sizeof(xfs_attr_leaf_entry_t);
1856 memmove((char *)entry, (char *)(entry+1), tmp);
1857 be16_add_cpu(&hdr->count, -1);
1858 xfs_trans_log_buf(args->trans, bp,
1859 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1860 entry = &leaf->entries[be16_to_cpu(hdr->count)];
1861 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1864 * If we removed the first entry, re-find the first used byte
1865 * in the name area. Note that if the entry was the "firstused",
1866 * then we don't have a "hole" in our block resulting from
1867 * removing the name.
1870 tmp = XFS_LBSIZE(mp);
1871 entry = &leaf->entries[0];
1872 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
1873 ASSERT(be16_to_cpu(entry->nameidx) >=
1874 be16_to_cpu(hdr->firstused));
1875 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1877 if (be16_to_cpu(entry->nameidx) < tmp)
1878 tmp = be16_to_cpu(entry->nameidx);
1880 hdr->firstused = cpu_to_be16(tmp);
1881 if (!hdr->firstused) {
1882 hdr->firstused = cpu_to_be16(
1883 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1886 hdr->holes = 1; /* mark as needing compaction */
1888 xfs_trans_log_buf(args->trans, bp,
1889 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1892 * Check if leaf is less than 50% full, caller may want to
1893 * "join" the leaf with a sibling if so.
1895 tmp = sizeof(xfs_attr_leaf_hdr_t);
1896 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1897 tmp += be16_to_cpu(leaf->hdr.usedbytes);
1898 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1902 * Move all the attribute list entries from drop_leaf into save_leaf.
1905 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1906 xfs_da_state_blk_t *save_blk)
1908 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1909 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1913 trace_xfs_attr_leaf_unbalance(state->args);
1916 * Set up environment.
1919 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1920 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1921 drop_leaf = drop_blk->bp->b_addr;
1922 save_leaf = save_blk->bp->b_addr;
1923 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1924 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1925 drop_hdr = &drop_leaf->hdr;
1926 save_hdr = &save_leaf->hdr;
1929 * Save last hashval from dying block for later Btree fixup.
1931 drop_blk->hashval = be32_to_cpu(
1932 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
1935 * Check if we need a temp buffer, or can we do it in place.
1936 * Note that we don't check "leaf" for holes because we will
1937 * always be dropping it, toosmall() decided that for us already.
1939 if (save_hdr->holes == 0) {
1941 * dest leaf has no holes, so we add there. May need
1942 * to make some room in the entry array.
1944 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1945 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1946 be16_to_cpu(drop_hdr->count), mp);
1948 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1949 be16_to_cpu(save_hdr->count),
1950 be16_to_cpu(drop_hdr->count), mp);
1954 * Destination has holes, so we make a temporary copy
1955 * of the leaf and add them both to that.
1957 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1958 ASSERT(tmpbuffer != NULL);
1959 memset(tmpbuffer, 0, state->blocksize);
1960 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1961 tmp_hdr = &tmp_leaf->hdr;
1962 tmp_hdr->info = save_hdr->info; /* struct copy */
1964 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
1965 if (!tmp_hdr->firstused) {
1966 tmp_hdr->firstused = cpu_to_be16(
1967 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1969 tmp_hdr->usedbytes = 0;
1970 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1971 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1972 be16_to_cpu(drop_hdr->count), mp);
1973 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1974 be16_to_cpu(tmp_leaf->hdr.count),
1975 be16_to_cpu(save_hdr->count), mp);
1977 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1978 be16_to_cpu(save_hdr->count), mp);
1979 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1980 be16_to_cpu(tmp_leaf->hdr.count),
1981 be16_to_cpu(drop_hdr->count), mp);
1983 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1984 kmem_free(tmpbuffer);
1987 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
1988 state->blocksize - 1);
1991 * Copy out last hashval in each block for B-tree code.
1993 save_blk->hashval = be32_to_cpu(
1994 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
1997 /*========================================================================
1998 * Routines used for finding things in the Btree.
1999 *========================================================================*/
2002 * Look up a name in a leaf attribute list structure.
2003 * This is the internal routine, it uses the caller's buffer.
2005 * Note that duplicate keys are allowed, but only check within the
2006 * current leaf node. The Btree code must check in adjacent leaf nodes.
2008 * Return in args->index the index into the entry[] array of either
2009 * the found entry, or where the entry should have been (insert before
2012 * Don't change the args->value unless we find the attribute.
2015 xfs_attr_leaf_lookup_int(
2017 xfs_da_args_t *args)
2019 xfs_attr_leafblock_t *leaf;
2020 xfs_attr_leaf_entry_t *entry;
2021 xfs_attr_leaf_name_local_t *name_loc;
2022 xfs_attr_leaf_name_remote_t *name_rmt;
2024 xfs_dahash_t hashval;
2026 trace_xfs_attr_leaf_lookup(args);
2029 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2030 ASSERT(be16_to_cpu(leaf->hdr.count)
2031 < (XFS_LBSIZE(args->dp->i_mount)/8));
2034 * Binary search. (note: small blocks will skip this loop)
2036 hashval = args->hashval;
2037 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
2038 for (entry = &leaf->entries[probe]; span > 4;
2039 entry = &leaf->entries[probe]) {
2041 if (be32_to_cpu(entry->hashval) < hashval)
2043 else if (be32_to_cpu(entry->hashval) > hashval)
2048 ASSERT((probe >= 0) &&
2050 || (probe < be16_to_cpu(leaf->hdr.count))));
2051 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
2054 * Since we may have duplicate hashval's, find the first matching
2055 * hashval in the leaf.
2057 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
2061 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
2062 (be32_to_cpu(entry->hashval) < hashval)) {
2066 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2067 (be32_to_cpu(entry->hashval) != hashval)) {
2068 args->index = probe;
2069 return(XFS_ERROR(ENOATTR));
2073 * Duplicate keys may be present, so search all of them for a match.
2075 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2076 (be32_to_cpu(entry->hashval) == hashval);
2079 * GROT: Add code to remove incomplete entries.
2082 * If we are looking for INCOMPLETE entries, show only those.
2083 * If we are looking for complete entries, show only those.
2085 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2086 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2089 if (entry->flags & XFS_ATTR_LOCAL) {
2090 name_loc = xfs_attr_leaf_name_local(leaf, probe);
2091 if (name_loc->namelen != args->namelen)
2093 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
2095 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2097 args->index = probe;
2098 return(XFS_ERROR(EEXIST));
2100 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
2101 if (name_rmt->namelen != args->namelen)
2103 if (memcmp(args->name, (char *)name_rmt->name,
2104 args->namelen) != 0)
2106 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2108 args->index = probe;
2109 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2110 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2111 be32_to_cpu(name_rmt->valuelen));
2112 return(XFS_ERROR(EEXIST));
2115 args->index = probe;
2116 return(XFS_ERROR(ENOATTR));
2120 * Get the value associated with an attribute name from a leaf attribute
2124 xfs_attr_leaf_getvalue(
2126 xfs_da_args_t *args)
2129 xfs_attr_leafblock_t *leaf;
2130 xfs_attr_leaf_entry_t *entry;
2131 xfs_attr_leaf_name_local_t *name_loc;
2132 xfs_attr_leaf_name_remote_t *name_rmt;
2135 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2136 ASSERT(be16_to_cpu(leaf->hdr.count)
2137 < (XFS_LBSIZE(args->dp->i_mount)/8));
2138 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2140 entry = &leaf->entries[args->index];
2141 if (entry->flags & XFS_ATTR_LOCAL) {
2142 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2143 ASSERT(name_loc->namelen == args->namelen);
2144 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2145 valuelen = be16_to_cpu(name_loc->valuelen);
2146 if (args->flags & ATTR_KERNOVAL) {
2147 args->valuelen = valuelen;
2150 if (args->valuelen < valuelen) {
2151 args->valuelen = valuelen;
2152 return(XFS_ERROR(ERANGE));
2154 args->valuelen = valuelen;
2155 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2157 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2158 ASSERT(name_rmt->namelen == args->namelen);
2159 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2160 valuelen = be32_to_cpu(name_rmt->valuelen);
2161 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2162 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2163 if (args->flags & ATTR_KERNOVAL) {
2164 args->valuelen = valuelen;
2167 if (args->valuelen < valuelen) {
2168 args->valuelen = valuelen;
2169 return(XFS_ERROR(ERANGE));
2171 args->valuelen = valuelen;
2176 /*========================================================================
2178 *========================================================================*/
2181 * Move the indicated entries from one leaf to another.
2182 * NOTE: this routine modifies both source and destination leaves.
2186 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2187 xfs_attr_leafblock_t *leaf_d, int start_d,
2188 int count, xfs_mount_t *mp)
2190 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2191 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2195 * Check for nothing to do.
2201 * Set up environment.
2203 ASSERT(leaf_s->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2204 ASSERT(leaf_d->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2205 hdr_s = &leaf_s->hdr;
2206 hdr_d = &leaf_d->hdr;
2207 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2208 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2209 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2210 ((be16_to_cpu(hdr_s->count)
2211 * sizeof(*entry_s))+sizeof(*hdr_s)));
2212 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2213 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2214 ((be16_to_cpu(hdr_d->count)
2215 * sizeof(*entry_d))+sizeof(*hdr_d)));
2217 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2218 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2219 ASSERT(count <= be16_to_cpu(hdr_s->count));
2222 * Move the entries in the destination leaf up to make a hole?
2224 if (start_d < be16_to_cpu(hdr_d->count)) {
2225 tmp = be16_to_cpu(hdr_d->count) - start_d;
2226 tmp *= sizeof(xfs_attr_leaf_entry_t);
2227 entry_s = &leaf_d->entries[start_d];
2228 entry_d = &leaf_d->entries[start_d + count];
2229 memmove((char *)entry_d, (char *)entry_s, tmp);
2233 * Copy all entry's in the same (sorted) order,
2234 * but allocate attribute info packed and in sequence.
2236 entry_s = &leaf_s->entries[start_s];
2237 entry_d = &leaf_d->entries[start_d];
2239 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2240 ASSERT(be16_to_cpu(entry_s->nameidx)
2241 >= be16_to_cpu(hdr_s->firstused));
2242 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2245 * Code to drop INCOMPLETE entries. Difficult to use as we
2246 * may also need to change the insertion index. Code turned
2247 * off for 6.2, should be revisited later.
2249 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2250 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2251 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2252 be16_add_cpu(&hdr_s->count, -1);
2253 entry_d--; /* to compensate for ++ in loop hdr */
2255 if ((start_s + i) < offset)
2256 result++; /* insertion index adjustment */
2259 be16_add_cpu(&hdr_d->firstused, -tmp);
2260 /* both on-disk, don't endian flip twice */
2261 entry_d->hashval = entry_s->hashval;
2262 /* both on-disk, don't endian flip twice */
2263 entry_d->nameidx = hdr_d->firstused;
2264 entry_d->flags = entry_s->flags;
2265 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2267 memmove(xfs_attr_leaf_name(leaf_d, desti),
2268 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
2269 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2271 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2272 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2273 be16_add_cpu(&hdr_d->usedbytes, tmp);
2274 be16_add_cpu(&hdr_s->count, -1);
2275 be16_add_cpu(&hdr_d->count, 1);
2276 tmp = be16_to_cpu(hdr_d->count)
2277 * sizeof(xfs_attr_leaf_entry_t)
2278 + sizeof(xfs_attr_leaf_hdr_t);
2279 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
2286 * Zero out the entries we just copied.
2288 if (start_s == be16_to_cpu(hdr_s->count)) {
2289 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2290 entry_s = &leaf_s->entries[start_s];
2291 ASSERT(((char *)entry_s + tmp) <=
2292 ((char *)leaf_s + XFS_LBSIZE(mp)));
2293 memset((char *)entry_s, 0, tmp);
2296 * Move the remaining entries down to fill the hole,
2297 * then zero the entries at the top.
2299 tmp = be16_to_cpu(hdr_s->count) - count;
2300 tmp *= sizeof(xfs_attr_leaf_entry_t);
2301 entry_s = &leaf_s->entries[start_s + count];
2302 entry_d = &leaf_s->entries[start_s];
2303 memmove((char *)entry_d, (char *)entry_s, tmp);
2305 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2306 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
2307 ASSERT(((char *)entry_s + tmp) <=
2308 ((char *)leaf_s + XFS_LBSIZE(mp)));
2309 memset((char *)entry_s, 0, tmp);
2313 * Fill in the freemap information
2315 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2316 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2317 sizeof(xfs_attr_leaf_entry_t));
2318 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2319 - be16_to_cpu(hdr_d->freemap[0].base));
2320 hdr_d->freemap[1].base = 0;
2321 hdr_d->freemap[2].base = 0;
2322 hdr_d->freemap[1].size = 0;
2323 hdr_d->freemap[2].size = 0;
2324 hdr_s->holes = 1; /* leaf may not be compact */
2328 * Compare two leaf blocks "order".
2329 * Return 0 unless leaf2 should go before leaf1.
2332 xfs_attr_leaf_order(
2333 struct xfs_buf *leaf1_bp,
2334 struct xfs_buf *leaf2_bp)
2336 xfs_attr_leafblock_t *leaf1, *leaf2;
2338 leaf1 = leaf1_bp->b_addr;
2339 leaf2 = leaf2_bp->b_addr;
2340 ASSERT((leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) &&
2341 (leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)));
2342 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2343 (be16_to_cpu(leaf2->hdr.count) > 0) &&
2344 ((be32_to_cpu(leaf2->entries[0].hashval) <
2345 be32_to_cpu(leaf1->entries[0].hashval)) ||
2346 (be32_to_cpu(leaf2->entries[
2347 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2348 be32_to_cpu(leaf1->entries[
2349 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
2356 * Pick up the last hashvalue from a leaf block.
2359 xfs_attr_leaf_lasthash(
2363 xfs_attr_leafblock_t *leaf;
2366 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2368 *count = be16_to_cpu(leaf->hdr.count);
2369 if (!leaf->hdr.count)
2371 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
2375 * Calculate the number of bytes used to store the indicated attribute
2376 * (whether local or remote only calculate bytes in this block).
2379 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2381 xfs_attr_leaf_name_local_t *name_loc;
2382 xfs_attr_leaf_name_remote_t *name_rmt;
2385 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2386 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2387 name_loc = xfs_attr_leaf_name_local(leaf, index);
2388 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2389 be16_to_cpu(name_loc->valuelen));
2391 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2392 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2398 * Calculate the number of bytes that would be required to store the new
2399 * attribute (whether local or remote only calculate bytes in this block).
2400 * This routine decides as a side effect whether the attribute will be
2401 * a "local" or a "remote" attribute.
2404 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2408 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2409 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
2414 size = xfs_attr_leaf_entsize_remote(namelen);
2423 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2426 xfs_attr_leaf_list_int(
2428 xfs_attr_list_context_t *context)
2430 attrlist_cursor_kern_t *cursor;
2431 xfs_attr_leafblock_t *leaf;
2432 xfs_attr_leaf_entry_t *entry;
2437 cursor = context->cursor;
2438 cursor->initted = 1;
2440 trace_xfs_attr_list_leaf(context);
2443 * Re-find our place in the leaf block if this is a new syscall.
2445 if (context->resynch) {
2446 entry = &leaf->entries[0];
2447 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2448 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
2449 if (cursor->offset == context->dupcnt) {
2450 context->dupcnt = 0;
2454 } else if (be32_to_cpu(entry->hashval) >
2456 context->dupcnt = 0;
2460 if (i == be16_to_cpu(leaf->hdr.count)) {
2461 trace_xfs_attr_list_notfound(context);
2465 entry = &leaf->entries[0];
2468 context->resynch = 0;
2471 * We have found our place, start copying out the new attributes.
2474 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
2475 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2476 cursor->hashval = be32_to_cpu(entry->hashval);
2480 if (entry->flags & XFS_ATTR_INCOMPLETE)
2481 continue; /* skip incomplete entries */
2483 if (entry->flags & XFS_ATTR_LOCAL) {
2484 xfs_attr_leaf_name_local_t *name_loc =
2485 xfs_attr_leaf_name_local(leaf, i);
2487 retval = context->put_listent(context,
2490 (int)name_loc->namelen,
2491 be16_to_cpu(name_loc->valuelen),
2492 &name_loc->nameval[name_loc->namelen]);
2496 xfs_attr_leaf_name_remote_t *name_rmt =
2497 xfs_attr_leaf_name_remote(leaf, i);
2499 int valuelen = be32_to_cpu(name_rmt->valuelen);
2501 if (context->put_value) {
2504 memset((char *)&args, 0, sizeof(args));
2505 args.dp = context->dp;
2506 args.whichfork = XFS_ATTR_FORK;
2507 args.valuelen = valuelen;
2508 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
2509 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2510 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2511 retval = xfs_attr_rmtval_get(&args);
2514 retval = context->put_listent(context,
2517 (int)name_rmt->namelen,
2520 kmem_free(args.value);
2522 retval = context->put_listent(context,
2525 (int)name_rmt->namelen,
2532 if (context->seen_enough)
2536 trace_xfs_attr_list_leaf_end(context);
2541 /*========================================================================
2542 * Manage the INCOMPLETE flag in a leaf entry
2543 *========================================================================*/
2546 * Clear the INCOMPLETE flag on an entry in a leaf block.
2549 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2551 xfs_attr_leafblock_t *leaf;
2552 xfs_attr_leaf_entry_t *entry;
2553 xfs_attr_leaf_name_remote_t *name_rmt;
2557 xfs_attr_leaf_name_local_t *name_loc;
2562 trace_xfs_attr_leaf_clearflag(args);
2564 * Set up the operation.
2566 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2571 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2572 ASSERT(args->index >= 0);
2573 entry = &leaf->entries[ args->index ];
2574 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2577 if (entry->flags & XFS_ATTR_LOCAL) {
2578 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2579 namelen = name_loc->namelen;
2580 name = (char *)name_loc->nameval;
2582 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2583 namelen = name_rmt->namelen;
2584 name = (char *)name_rmt->name;
2586 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2587 ASSERT(namelen == args->namelen);
2588 ASSERT(memcmp(name, args->name, namelen) == 0);
2591 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2592 xfs_trans_log_buf(args->trans, bp,
2593 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2595 if (args->rmtblkno) {
2596 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2597 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2598 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2599 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2600 xfs_trans_log_buf(args->trans, bp,
2601 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2605 * Commit the flag value change and start the next trans in series.
2607 return xfs_trans_roll(&args->trans, args->dp);
2611 * Set the INCOMPLETE flag on an entry in a leaf block.
2614 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2616 xfs_attr_leafblock_t *leaf;
2617 xfs_attr_leaf_entry_t *entry;
2618 xfs_attr_leaf_name_remote_t *name_rmt;
2622 trace_xfs_attr_leaf_setflag(args);
2625 * Set up the operation.
2627 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2632 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2633 ASSERT(args->index >= 0);
2634 entry = &leaf->entries[ args->index ];
2636 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2637 entry->flags |= XFS_ATTR_INCOMPLETE;
2638 xfs_trans_log_buf(args->trans, bp,
2639 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2640 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2641 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2642 name_rmt->valueblk = 0;
2643 name_rmt->valuelen = 0;
2644 xfs_trans_log_buf(args->trans, bp,
2645 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2649 * Commit the flag value change and start the next trans in series.
2651 return xfs_trans_roll(&args->trans, args->dp);
2655 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2656 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2657 * entry given by args->blkno2/index2.
2659 * Note that they could be in different blocks, or in the same block.
2662 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2664 xfs_attr_leafblock_t *leaf1, *leaf2;
2665 xfs_attr_leaf_entry_t *entry1, *entry2;
2666 xfs_attr_leaf_name_remote_t *name_rmt;
2667 struct xfs_buf *bp1, *bp2;
2670 xfs_attr_leaf_name_local_t *name_loc;
2671 int namelen1, namelen2;
2672 char *name1, *name2;
2675 trace_xfs_attr_leaf_flipflags(args);
2678 * Read the block containing the "old" attr
2680 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2685 * Read the block containing the "new" attr, if it is different
2687 if (args->blkno2 != args->blkno) {
2688 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno2,
2696 leaf1 = bp1->b_addr;
2697 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
2698 ASSERT(args->index >= 0);
2699 entry1 = &leaf1->entries[ args->index ];
2701 leaf2 = bp2->b_addr;
2702 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
2703 ASSERT(args->index2 >= 0);
2704 entry2 = &leaf2->entries[ args->index2 ];
2707 if (entry1->flags & XFS_ATTR_LOCAL) {
2708 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
2709 namelen1 = name_loc->namelen;
2710 name1 = (char *)name_loc->nameval;
2712 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2713 namelen1 = name_rmt->namelen;
2714 name1 = (char *)name_rmt->name;
2716 if (entry2->flags & XFS_ATTR_LOCAL) {
2717 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
2718 namelen2 = name_loc->namelen;
2719 name2 = (char *)name_loc->nameval;
2721 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2722 namelen2 = name_rmt->namelen;
2723 name2 = (char *)name_rmt->name;
2725 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2726 ASSERT(namelen1 == namelen2);
2727 ASSERT(memcmp(name1, name2, namelen1) == 0);
2730 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2731 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2733 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2734 xfs_trans_log_buf(args->trans, bp1,
2735 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2736 if (args->rmtblkno) {
2737 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2738 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2739 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2740 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2741 xfs_trans_log_buf(args->trans, bp1,
2742 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2745 entry2->flags |= XFS_ATTR_INCOMPLETE;
2746 xfs_trans_log_buf(args->trans, bp2,
2747 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2748 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2749 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2750 name_rmt->valueblk = 0;
2751 name_rmt->valuelen = 0;
2752 xfs_trans_log_buf(args->trans, bp2,
2753 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2757 * Commit the flag value change and start the next trans in series.
2759 error = xfs_trans_roll(&args->trans, args->dp);
2764 /*========================================================================
2765 * Indiscriminately delete the entire attribute fork
2766 *========================================================================*/
2769 * Recurse (gasp!) through the attribute nodes until we find leaves.
2770 * We're doing a depth-first traversal in order to invalidate everything.
2773 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2775 xfs_da_blkinfo_t *info;
2781 * Read block 0 to see what we have to work with.
2782 * We only get here if we have extents, since we remove
2783 * the extents in reverse order the extent containing
2784 * block 0 must still be there.
2786 error = xfs_da_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2789 blkno = XFS_BUF_ADDR(bp);
2792 * Invalidate the tree, even if the "tree" is only a single leaf block.
2793 * This is a depth-first traversal!
2796 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
2797 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2798 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
2799 error = xfs_attr_leaf_inactive(trans, dp, bp);
2801 error = XFS_ERROR(EIO);
2802 xfs_trans_brelse(*trans, bp);
2808 * Invalidate the incore copy of the root block.
2810 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2813 xfs_trans_binval(*trans, bp); /* remove from cache */
2815 * Commit the invalidate and start the next transaction.
2817 error = xfs_trans_roll(trans, dp);
2823 * Recurse (gasp!) through the attribute nodes until we find leaves.
2824 * We're doing a depth-first traversal in order to invalidate everything.
2827 xfs_attr_node_inactive(
2828 struct xfs_trans **trans,
2829 struct xfs_inode *dp,
2833 xfs_da_blkinfo_t *info;
2834 xfs_da_intnode_t *node;
2835 xfs_dablk_t child_fsb;
2836 xfs_daddr_t parent_blkno, child_blkno;
2837 int error, count, i;
2838 struct xfs_buf *child_bp;
2841 * Since this code is recursive (gasp!) we must protect ourselves.
2843 if (level > XFS_DA_NODE_MAXDEPTH) {
2844 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
2845 return(XFS_ERROR(EIO));
2849 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
2850 parent_blkno = XFS_BUF_ADDR(bp); /* save for re-read later */
2851 count = be16_to_cpu(node->hdr.count);
2853 xfs_trans_brelse(*trans, bp);
2856 child_fsb = be32_to_cpu(node->btree[0].before);
2857 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
2860 * If this is the node level just above the leaves, simply loop
2861 * over the leaves removing all of them. If this is higher up
2862 * in the tree, recurse downward.
2864 for (i = 0; i < count; i++) {
2866 * Read the subsidiary block to see what we have to work with.
2867 * Don't do this in a transaction. This is a depth-first
2868 * traversal of the tree so we may deal with many blocks
2869 * before we come back to this one.
2871 error = xfs_da_node_read(*trans, dp, child_fsb, -2, &child_bp,
2876 /* save for re-read later */
2877 child_blkno = XFS_BUF_ADDR(child_bp);
2880 * Invalidate the subtree, however we have to.
2882 info = child_bp->b_addr;
2883 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
2884 error = xfs_attr_node_inactive(trans, dp,
2886 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
2887 error = xfs_attr_leaf_inactive(trans, dp,
2890 error = XFS_ERROR(EIO);
2891 xfs_trans_brelse(*trans, child_bp);
2897 * Remove the subsidiary block from the cache
2900 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2901 &child_bp, XFS_ATTR_FORK);
2904 xfs_trans_binval(*trans, child_bp);
2908 * If we're not done, re-read the parent to get the next
2909 * child block number.
2911 if ((i+1) < count) {
2912 error = xfs_da_node_read(*trans, dp, 0, parent_blkno,
2913 &bp, XFS_ATTR_FORK);
2916 child_fsb = be32_to_cpu(node->btree[i+1].before);
2917 xfs_trans_brelse(*trans, bp);
2920 * Atomically commit the whole invalidate stuff.
2922 error = xfs_trans_roll(trans, dp);
2931 * Invalidate all of the "remote" value regions pointed to by a particular
2933 * Note that we must release the lock on the buffer so that we are not
2934 * caught holding something that the logging code wants to flush to disk.
2937 xfs_attr_leaf_inactive(
2938 struct xfs_trans **trans,
2939 struct xfs_inode *dp,
2942 xfs_attr_leafblock_t *leaf;
2943 xfs_attr_leaf_entry_t *entry;
2944 xfs_attr_leaf_name_remote_t *name_rmt;
2945 xfs_attr_inactive_list_t *list, *lp;
2946 int error, count, size, tmp, i;
2949 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2952 * Count the number of "remote" value extents.
2955 entry = &leaf->entries[0];
2956 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2957 if (be16_to_cpu(entry->nameidx) &&
2958 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2959 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2960 if (name_rmt->valueblk)
2966 * If there are no "remote" values, we're done.
2969 xfs_trans_brelse(*trans, bp);
2974 * Allocate storage for a list of all the "remote" value extents.
2976 size = count * sizeof(xfs_attr_inactive_list_t);
2977 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2980 * Identify each of the "remote" value extents.
2983 entry = &leaf->entries[0];
2984 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2985 if (be16_to_cpu(entry->nameidx) &&
2986 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2987 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2988 if (name_rmt->valueblk) {
2989 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2990 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2991 be32_to_cpu(name_rmt->valuelen));
2996 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
2999 * Invalidate each of the "remote" value extents.
3002 for (lp = list, i = 0; i < count; i++, lp++) {
3003 tmp = xfs_attr_leaf_freextent(trans, dp,
3004 lp->valueblk, lp->valuelen);
3007 error = tmp; /* save only the 1st errno */
3010 kmem_free((xfs_caddr_t)list);
3015 * Look at all the extents for this logical region,
3016 * invalidate any buffers that are incore/in transactions.
3019 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
3020 xfs_dablk_t blkno, int blkcnt)
3022 xfs_bmbt_irec_t map;
3024 int tblkcnt, dblkcnt, nmap, error;
3029 * Roll through the "value", invalidating the attribute value's
3034 while (tblkcnt > 0) {
3036 * Try to remember where we decided to put the value.
3039 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3040 &map, &nmap, XFS_BMAPI_ATTRFORK);
3045 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3048 * If it's a hole, these are already unmapped
3049 * so there's nothing to invalidate.
3051 if (map.br_startblock != HOLESTARTBLOCK) {
3053 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3055 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3057 bp = xfs_trans_get_buf(*trans,
3058 dp->i_mount->m_ddev_targp,
3059 dblkno, dblkcnt, 0);
3062 xfs_trans_binval(*trans, bp);
3064 * Roll to next transaction.
3066 error = xfs_trans_roll(trans, dp);
3071 tblkno += map.br_blockcount;
3072 tblkcnt -= map.br_blockcount;