2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_alloc.h"
35 #include "xfs_btree.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_inode_item.h"
43 #include "xfs_attr_leaf.h"
44 #include "xfs_error.h"
49 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
52 /*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
57 * Routines used for growing the Btree.
59 STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
61 STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
63 STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
64 STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
65 xfs_da_state_blk_t *blk1,
66 xfs_da_state_blk_t *blk2);
67 STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
68 xfs_da_state_blk_t *leaf_blk_1,
69 xfs_da_state_blk_t *leaf_blk_2,
70 int *number_entries_in_blk1,
71 int *number_usedbytes_in_blk1);
74 * Routines used for shrinking the Btree.
76 STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
77 xfs_dabuf_t *bp, int level);
78 STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
80 STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
81 xfs_dablk_t blkno, int blkcnt);
86 STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
88 xfs_attr_leafblock_t *dst_leaf,
89 int dst_start, int move_count,
91 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
93 /*========================================================================
94 * Namespace helper routines
95 *========================================================================*/
97 STATIC inline attrnames_t *
98 xfs_attr_flags_namesp(int flags)
100 return ((flags & XFS_ATTR_SECURE) ? &attr_secure:
101 ((flags & XFS_ATTR_ROOT) ? &attr_trusted : &attr_user));
105 * If namespace bits don't match return 0.
106 * If all match then return 1.
109 xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
111 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
115 * If namespace bits don't match and we don't have an override for it
117 * If all match or are overridable then return 1.
120 xfs_attr_namesp_match_overrides(int arg_flags, int ondisk_flags)
122 if (((arg_flags & ATTR_SECURE) == 0) !=
123 ((ondisk_flags & XFS_ATTR_SECURE) == 0) &&
124 !(arg_flags & ATTR_KERNORMALS))
126 if (((arg_flags & ATTR_ROOT) == 0) !=
127 ((ondisk_flags & XFS_ATTR_ROOT) == 0) &&
128 !(arg_flags & ATTR_KERNROOTLS))
134 /*========================================================================
135 * External routines when attribute fork size < XFS_LITINO(mp).
136 *========================================================================*/
139 * Query whether the requested number of additional bytes of extended
140 * attribute space will be able to fit inline.
141 * Returns zero if not, else the di_forkoff fork offset to be used in the
142 * literal area for attribute data once the new bytes have been added.
144 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
145 * special case for dev/uuid inodes, they have fixed size data forks.
148 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
151 int minforkoff; /* lower limit on valid forkoff locations */
152 int maxforkoff; /* upper limit on valid forkoff locations */
154 xfs_mount_t *mp = dp->i_mount;
156 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
158 switch (dp->i_d.di_format) {
159 case XFS_DINODE_FMT_DEV:
160 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
161 return (offset >= minforkoff) ? minforkoff : 0;
162 case XFS_DINODE_FMT_UUID:
163 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
164 return (offset >= minforkoff) ? minforkoff : 0;
167 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
168 if (bytes <= XFS_IFORK_ASIZE(dp))
169 return mp->m_attroffset >> 3;
173 dsize = dp->i_df.if_bytes;
175 switch (dp->i_d.di_format) {
176 case XFS_DINODE_FMT_EXTENTS:
178 * If there is no attr fork and the data fork is extents,
179 * determine if creating the default attr fork will result
180 * in the extents form migrating to btree. If so, the
181 * minimum offset only needs to be the space required for
184 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes > mp->m_attroffset)
185 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
188 case XFS_DINODE_FMT_BTREE:
190 * If have data btree then keep forkoff if we have one,
191 * otherwise we are adding a new attr, so then we set
192 * minforkoff to where the btree root can finish so we have
193 * plenty of room for attrs
195 if (dp->i_d.di_forkoff) {
196 if (offset < dp->i_d.di_forkoff)
199 return dp->i_d.di_forkoff;
201 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
206 * A data fork btree root must have space for at least
207 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
209 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
210 minforkoff = roundup(minforkoff, 8) >> 3;
212 /* attr fork btree root can have at least this many key/ptr pairs */
213 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
214 maxforkoff = maxforkoff >> 3; /* rounded down */
216 if (offset >= minforkoff && offset < maxforkoff)
218 if (offset >= maxforkoff)
224 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
227 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
231 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
232 !(XFS_SB_VERSION_HASATTR2(&mp->m_sb))) {
234 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb)) {
235 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
236 XFS_SB_UNLOCK(mp, s);
237 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
239 XFS_SB_UNLOCK(mp, s);
244 * Create the initial contents of a shortform attribute list.
247 xfs_attr_shortform_create(xfs_da_args_t *args)
249 xfs_attr_sf_hdr_t *hdr;
257 ASSERT(ifp->if_bytes == 0);
258 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
259 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
260 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
261 ifp->if_flags |= XFS_IFINLINE;
263 ASSERT(ifp->if_flags & XFS_IFINLINE);
265 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
266 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
268 hdr->totsize = cpu_to_be16(sizeof(*hdr));
269 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
273 * Add a name/value pair to the shortform attribute list.
274 * Overflow from the inode has already been checked for.
277 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
279 xfs_attr_shortform_t *sf;
280 xfs_attr_sf_entry_t *sfe;
288 dp->i_d.di_forkoff = forkoff;
289 dp->i_df.if_ext_max =
290 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
291 dp->i_afp->if_ext_max =
292 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
295 ASSERT(ifp->if_flags & XFS_IFINLINE);
296 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
298 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
300 if (sfe->namelen != args->namelen)
302 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
304 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
310 offset = (char *)sfe - (char *)sf;
311 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
312 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
313 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
314 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
316 sfe->namelen = args->namelen;
317 sfe->valuelen = args->valuelen;
318 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
319 memcpy(sfe->nameval, args->name, args->namelen);
320 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
322 be16_add(&sf->hdr.totsize, size);
323 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
325 xfs_sbversion_add_attr2(mp, args->trans);
329 * Remove an attribute from the shortform attribute list structure.
332 xfs_attr_shortform_remove(xfs_da_args_t *args)
334 xfs_attr_shortform_t *sf;
335 xfs_attr_sf_entry_t *sfe;
336 int base, size=0, end, totsize, i;
342 base = sizeof(xfs_attr_sf_hdr_t);
343 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
346 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
348 size = XFS_ATTR_SF_ENTSIZE(sfe);
349 if (sfe->namelen != args->namelen)
351 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
353 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
358 return(XFS_ERROR(ENOATTR));
361 * Fix up the attribute fork data, covering the hole
364 totsize = be16_to_cpu(sf->hdr.totsize);
366 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
368 be16_add(&sf->hdr.totsize, -size);
371 * Fix up the start offset of the attribute fork
374 if (totsize == sizeof(xfs_attr_sf_hdr_t) && !args->addname &&
375 (mp->m_flags & XFS_MOUNT_ATTR2) &&
376 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE)) {
378 * Last attribute now removed, revert to original
379 * inode format making all literal area available
380 * to the data fork once more.
382 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
383 dp->i_d.di_forkoff = 0;
384 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
385 ASSERT(dp->i_d.di_anextents == 0);
386 ASSERT(dp->i_afp == NULL);
387 dp->i_df.if_ext_max =
388 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
389 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
391 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
392 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
393 ASSERT(dp->i_d.di_forkoff);
394 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || args->addname ||
395 !(mp->m_flags & XFS_MOUNT_ATTR2));
396 dp->i_afp->if_ext_max =
397 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
398 dp->i_df.if_ext_max =
399 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
400 xfs_trans_log_inode(args->trans, dp,
401 XFS_ILOG_CORE | XFS_ILOG_ADATA);
404 xfs_sbversion_add_attr2(mp, args->trans);
410 * Look up a name in a shortform attribute list structure.
414 xfs_attr_shortform_lookup(xfs_da_args_t *args)
416 xfs_attr_shortform_t *sf;
417 xfs_attr_sf_entry_t *sfe;
421 ifp = args->dp->i_afp;
422 ASSERT(ifp->if_flags & XFS_IFINLINE);
423 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
425 for (i = 0; i < sf->hdr.count;
426 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
427 if (sfe->namelen != args->namelen)
429 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
431 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
433 return(XFS_ERROR(EEXIST));
435 return(XFS_ERROR(ENOATTR));
439 * Look up a name in a shortform attribute list structure.
443 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
445 xfs_attr_shortform_t *sf;
446 xfs_attr_sf_entry_t *sfe;
449 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
450 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
452 for (i = 0; i < sf->hdr.count;
453 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
454 if (sfe->namelen != args->namelen)
456 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
458 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
460 if (args->flags & ATTR_KERNOVAL) {
461 args->valuelen = sfe->valuelen;
462 return(XFS_ERROR(EEXIST));
464 if (args->valuelen < sfe->valuelen) {
465 args->valuelen = sfe->valuelen;
466 return(XFS_ERROR(ERANGE));
468 args->valuelen = sfe->valuelen;
469 memcpy(args->value, &sfe->nameval[args->namelen],
471 return(XFS_ERROR(EEXIST));
473 return(XFS_ERROR(ENOATTR));
477 * Convert from using the shortform to the leaf.
480 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
483 xfs_attr_shortform_t *sf;
484 xfs_attr_sf_entry_t *sfe;
494 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
495 size = be16_to_cpu(sf->hdr.totsize);
496 tmpbuffer = kmem_alloc(size, KM_SLEEP);
497 ASSERT(tmpbuffer != NULL);
498 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
499 sf = (xfs_attr_shortform_t *)tmpbuffer;
501 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
503 error = xfs_da_grow_inode(args, &blkno);
506 * If we hit an IO error middle of the transaction inside
507 * grow_inode(), we may have inconsistent data. Bail out.
511 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
512 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
517 error = xfs_attr_leaf_create(args, blkno, &bp);
519 error = xfs_da_shrink_inode(args, 0, bp);
523 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
524 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
528 memset((char *)&nargs, 0, sizeof(nargs));
530 nargs.firstblock = args->firstblock;
531 nargs.flist = args->flist;
532 nargs.total = args->total;
533 nargs.whichfork = XFS_ATTR_FORK;
534 nargs.trans = args->trans;
538 for (i = 0; i < sf->hdr.count; i++) {
539 nargs.name = (char *)sfe->nameval;
540 nargs.namelen = sfe->namelen;
541 nargs.value = (char *)&sfe->nameval[nargs.namelen];
542 nargs.valuelen = sfe->valuelen;
543 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
545 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
546 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
547 ASSERT(error == ENOATTR);
548 error = xfs_attr_leaf_add(bp, &nargs);
549 ASSERT(error != ENOSPC);
552 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
559 kmem_free(tmpbuffer, size);
564 xfs_attr_shortform_compare(const void *a, const void *b)
566 xfs_attr_sf_sort_t *sa, *sb;
568 sa = (xfs_attr_sf_sort_t *)a;
569 sb = (xfs_attr_sf_sort_t *)b;
570 if (sa->hash < sb->hash) {
572 } else if (sa->hash > sb->hash) {
575 return(sa->entno - sb->entno);
580 #define XFS_ISRESET_CURSOR(cursor) \
581 (!((cursor)->initted) && !((cursor)->hashval) && \
582 !((cursor)->blkno) && !((cursor)->offset))
584 * Copy out entries of shortform attribute lists for attr_list().
585 * Shortform attribute lists are not stored in hashval sorted order.
586 * If the output buffer is not large enough to hold them all, then we
587 * we have to calculate each entries' hashvalue and sort them before
588 * we can begin returning them to the user.
592 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
594 attrlist_cursor_kern_t *cursor;
595 xfs_attr_sf_sort_t *sbuf, *sbp;
596 xfs_attr_shortform_t *sf;
597 xfs_attr_sf_entry_t *sfe;
599 int sbsize, nsbuf, count, i;
602 ASSERT(context != NULL);
605 ASSERT(dp->i_afp != NULL);
606 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
610 cursor = context->cursor;
611 ASSERT(cursor != NULL);
613 xfs_attr_trace_l_c("sf start", context);
616 * If the buffer is large enough and the cursor is at the start,
617 * do not bother with sorting since we will return everything in
618 * one buffer and another call using the cursor won't need to be
620 * Note the generous fudge factor of 16 overhead bytes per entry.
621 * If bufsize is zero then put_listent must be a search function
622 * and can just scan through what we have.
624 if (context->bufsize == 0 ||
625 (XFS_ISRESET_CURSOR(cursor) &&
626 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
627 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
630 if (!xfs_attr_namesp_match_overrides(context->flags, sfe->flags)) {
631 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
634 namesp = xfs_attr_flags_namesp(sfe->flags);
635 error = context->put_listent(context,
637 (char *)sfe->nameval,
640 (char*)&sfe->nameval[sfe->namelen]);
643 * Either search callback finished early or
644 * didn't fit it all in the buffer after all.
646 if (context->seen_enough)
651 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
653 xfs_attr_trace_l_c("sf big-gulp", context);
657 /* do no more for a search callback */
658 if (context->bufsize == 0)
662 * It didn't all fit, so we have to sort everything on hashval.
664 sbsize = sf->hdr.count * sizeof(*sbuf);
665 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
668 * Scan the attribute list for the rest of the entries, storing
669 * the relevant info from only those that match into a buffer.
672 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
674 ((char *)sfe < (char *)sf) ||
675 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
676 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
678 context->dp->i_mount, sfe);
679 xfs_attr_trace_l_c("sf corrupted", context);
680 kmem_free(sbuf, sbsize);
681 return XFS_ERROR(EFSCORRUPTED);
683 if (!xfs_attr_namesp_match_overrides(context->flags, sfe->flags)) {
684 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
688 sbp->hash = xfs_da_hashname((char *)sfe->nameval, sfe->namelen);
689 sbp->name = (char *)sfe->nameval;
690 sbp->namelen = sfe->namelen;
691 /* These are bytes, and both on-disk, don't endian-flip */
692 sbp->valuelen = sfe->valuelen;
693 sbp->flags = sfe->flags;
694 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
700 * Sort the entries on hash then entno.
702 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
705 * Re-find our place IN THE SORTED LIST.
710 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
711 if (sbp->hash == cursor->hashval) {
712 if (cursor->offset == count) {
716 } else if (sbp->hash > cursor->hashval) {
721 kmem_free(sbuf, sbsize);
722 xfs_attr_trace_l_c("blk end", context);
727 * Loop putting entries into the user buffer.
729 for ( ; i < nsbuf; i++, sbp++) {
732 namesp = xfs_attr_flags_namesp(sbp->flags);
734 if (cursor->hashval != sbp->hash) {
735 cursor->hashval = sbp->hash;
738 error = context->put_listent(context,
743 &sbp->name[sbp->namelen]);
746 if (context->seen_enough)
751 kmem_free(sbuf, sbsize);
752 xfs_attr_trace_l_c("sf E-O-F", context);
757 * Check a leaf attribute block to see if all the entries would fit into
758 * a shortform attribute list.
761 xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
763 xfs_attr_leafblock_t *leaf;
764 xfs_attr_leaf_entry_t *entry;
765 xfs_attr_leaf_name_local_t *name_loc;
769 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
771 entry = &leaf->entries[0];
772 bytes = sizeof(struct xfs_attr_sf_hdr);
773 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
774 if (entry->flags & XFS_ATTR_INCOMPLETE)
775 continue; /* don't copy partial entries */
776 if (!(entry->flags & XFS_ATTR_LOCAL))
778 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
779 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
781 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
783 bytes += sizeof(struct xfs_attr_sf_entry)-1
785 + be16_to_cpu(name_loc->valuelen);
787 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
788 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
789 (bytes == sizeof(struct xfs_attr_sf_hdr)))
791 return(xfs_attr_shortform_bytesfit(dp, bytes));
795 * Convert a leaf attribute list to shortform attribute list
798 xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
800 xfs_attr_leafblock_t *leaf;
801 xfs_attr_leaf_entry_t *entry;
802 xfs_attr_leaf_name_local_t *name_loc;
809 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
810 ASSERT(tmpbuffer != NULL);
813 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
814 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
815 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
816 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
819 * Clean out the prior contents of the attribute list.
821 error = xfs_da_shrink_inode(args, 0, bp);
826 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
827 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
830 * Last attribute was removed, revert to original
831 * inode format making all literal area available
832 * to the data fork once more.
834 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
835 dp->i_d.di_forkoff = 0;
836 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
837 ASSERT(dp->i_d.di_anextents == 0);
838 ASSERT(dp->i_afp == NULL);
839 dp->i_df.if_ext_max =
840 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
841 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
845 xfs_attr_shortform_create(args);
848 * Copy the attributes
850 memset((char *)&nargs, 0, sizeof(nargs));
852 nargs.firstblock = args->firstblock;
853 nargs.flist = args->flist;
854 nargs.total = args->total;
855 nargs.whichfork = XFS_ATTR_FORK;
856 nargs.trans = args->trans;
858 entry = &leaf->entries[0];
859 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
860 if (entry->flags & XFS_ATTR_INCOMPLETE)
861 continue; /* don't copy partial entries */
864 ASSERT(entry->flags & XFS_ATTR_LOCAL);
865 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
866 nargs.name = (char *)name_loc->nameval;
867 nargs.namelen = name_loc->namelen;
868 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
869 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
870 nargs.hashval = be32_to_cpu(entry->hashval);
871 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
872 xfs_attr_shortform_add(&nargs, forkoff);
877 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
882 * Convert from using a single leaf to a root node and a leaf.
885 xfs_attr_leaf_to_node(xfs_da_args_t *args)
887 xfs_attr_leafblock_t *leaf;
888 xfs_da_intnode_t *node;
890 xfs_dabuf_t *bp1, *bp2;
896 error = xfs_da_grow_inode(args, &blkno);
899 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
905 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
910 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
911 xfs_da_buf_done(bp1);
913 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
916 * Set up the new root node.
918 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
923 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
924 /* both on-disk, don't endian-flip twice */
925 node->btree[0].hashval =
926 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
927 node->btree[0].before = cpu_to_be32(blkno);
928 node->hdr.count = cpu_to_be16(1);
929 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
933 xfs_da_buf_done(bp1);
935 xfs_da_buf_done(bp2);
940 /*========================================================================
941 * Routines used for growing the Btree.
942 *========================================================================*/
945 * Create the initial contents of a leaf attribute list
946 * or a leaf in a node attribute list.
949 xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
951 xfs_attr_leafblock_t *leaf;
952 xfs_attr_leaf_hdr_t *hdr;
959 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
965 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
967 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
968 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
969 if (!hdr->firstused) {
970 hdr->firstused = cpu_to_be16(
971 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
974 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
975 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
976 sizeof(xfs_attr_leaf_hdr_t));
978 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
985 * Split the leaf node, rebalance, then add the new entry.
988 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
989 xfs_da_state_blk_t *newblk)
995 * Allocate space for a new leaf node.
997 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
998 error = xfs_da_grow_inode(state->args, &blkno);
1001 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
1004 newblk->blkno = blkno;
1005 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1008 * Rebalance the entries across the two leaves.
1009 * NOTE: rebalance() currently depends on the 2nd block being empty.
1011 xfs_attr_leaf_rebalance(state, oldblk, newblk);
1012 error = xfs_da_blk_link(state, oldblk, newblk);
1017 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1018 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1019 * "new" attrs info. Will need the "old" info to remove it later.
1021 * Insert the "new" entry in the correct block.
1024 error = xfs_attr_leaf_add(oldblk->bp, state->args);
1026 error = xfs_attr_leaf_add(newblk->bp, state->args);
1029 * Update last hashval in each block since we added the name.
1031 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1032 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1037 * Add a name to the leaf attribute list structure.
1040 xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
1042 xfs_attr_leafblock_t *leaf;
1043 xfs_attr_leaf_hdr_t *hdr;
1044 xfs_attr_leaf_map_t *map;
1045 int tablesize, entsize, sum, tmp, i;
1048 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1049 ASSERT((args->index >= 0)
1050 && (args->index <= be16_to_cpu(leaf->hdr.count)));
1052 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1053 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1056 * Search through freemap for first-fit on new name length.
1057 * (may need to figure in size of entry struct too)
1059 tablesize = (be16_to_cpu(hdr->count) + 1)
1060 * sizeof(xfs_attr_leaf_entry_t)
1061 + sizeof(xfs_attr_leaf_hdr_t);
1062 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1063 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1064 if (tablesize > be16_to_cpu(hdr->firstused)) {
1065 sum += be16_to_cpu(map->size);
1069 continue; /* no space in this map */
1071 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
1072 tmp += sizeof(xfs_attr_leaf_entry_t);
1073 if (be16_to_cpu(map->size) >= tmp) {
1074 tmp = xfs_attr_leaf_add_work(bp, args, i);
1077 sum += be16_to_cpu(map->size);
1081 * If there are no holes in the address space of the block,
1082 * and we don't have enough freespace, then compaction will do us
1083 * no good and we should just give up.
1085 if (!hdr->holes && (sum < entsize))
1086 return(XFS_ERROR(ENOSPC));
1089 * Compact the entries to coalesce free space.
1090 * This may change the hdr->count via dropping INCOMPLETE entries.
1092 xfs_attr_leaf_compact(args->trans, bp);
1095 * After compaction, the block is guaranteed to have only one
1096 * free region, in freemap[0]. If it is not big enough, give up.
1098 if (be16_to_cpu(hdr->freemap[0].size)
1099 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1100 return(XFS_ERROR(ENOSPC));
1102 return(xfs_attr_leaf_add_work(bp, args, 0));
1106 * Add a name to a leaf attribute list structure.
1109 xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1111 xfs_attr_leafblock_t *leaf;
1112 xfs_attr_leaf_hdr_t *hdr;
1113 xfs_attr_leaf_entry_t *entry;
1114 xfs_attr_leaf_name_local_t *name_loc;
1115 xfs_attr_leaf_name_remote_t *name_rmt;
1116 xfs_attr_leaf_map_t *map;
1121 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1123 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1124 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
1127 * Force open some space in the entry array and fill it in.
1129 entry = &leaf->entries[args->index];
1130 if (args->index < be16_to_cpu(hdr->count)) {
1131 tmp = be16_to_cpu(hdr->count) - args->index;
1132 tmp *= sizeof(xfs_attr_leaf_entry_t);
1133 memmove((char *)(entry+1), (char *)entry, tmp);
1134 xfs_da_log_buf(args->trans, bp,
1135 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1137 be16_add(&hdr->count, 1);
1140 * Allocate space for the new string (at the end of the run).
1142 map = &hdr->freemap[mapindex];
1143 mp = args->trans->t_mountp;
1144 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1145 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1146 ASSERT(be16_to_cpu(map->size) >=
1147 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1148 mp->m_sb.sb_blocksize, NULL));
1149 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1150 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1151 be16_add(&map->size,
1152 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1153 mp->m_sb.sb_blocksize, &tmp));
1154 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1155 be16_to_cpu(map->size));
1156 entry->hashval = cpu_to_be32(args->hashval);
1157 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1158 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1160 entry->flags |= XFS_ATTR_INCOMPLETE;
1161 if ((args->blkno2 == args->blkno) &&
1162 (args->index2 <= args->index)) {
1166 xfs_da_log_buf(args->trans, bp,
1167 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1168 ASSERT((args->index == 0) ||
1169 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1170 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
1171 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1174 * Copy the attribute name and value into the new space.
1176 * For "remote" attribute values, simply note that we need to
1177 * allocate space for the "remote" value. We can't actually
1178 * allocate the extents in this transaction, and we can't decide
1179 * which blocks they should be as we might allocate more blocks
1180 * as part of this transaction (a split operation for example).
1182 if (entry->flags & XFS_ATTR_LOCAL) {
1183 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1184 name_loc->namelen = args->namelen;
1185 name_loc->valuelen = cpu_to_be16(args->valuelen);
1186 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1187 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1188 be16_to_cpu(name_loc->valuelen));
1190 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
1191 name_rmt->namelen = args->namelen;
1192 memcpy((char *)name_rmt->name, args->name, args->namelen);
1193 entry->flags |= XFS_ATTR_INCOMPLETE;
1195 name_rmt->valuelen = 0;
1196 name_rmt->valueblk = 0;
1198 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1200 xfs_da_log_buf(args->trans, bp,
1201 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1202 xfs_attr_leaf_entsize(leaf, args->index)));
1205 * Update the control info for this leaf node
1207 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
1208 /* both on-disk, don't endian-flip twice */
1209 hdr->firstused = entry->nameidx;
1211 ASSERT(be16_to_cpu(hdr->firstused) >=
1212 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1213 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
1214 + sizeof(xfs_attr_leaf_hdr_t);
1215 map = &hdr->freemap[0];
1216 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1217 if (be16_to_cpu(map->base) == tmp) {
1218 be16_add(&map->base, sizeof(xfs_attr_leaf_entry_t));
1219 be16_add(&map->size,
1220 -((int)sizeof(xfs_attr_leaf_entry_t)));
1223 be16_add(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
1224 xfs_da_log_buf(args->trans, bp,
1225 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1230 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1233 xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1235 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1236 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1240 mp = trans->t_mountp;
1241 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1242 ASSERT(tmpbuffer != NULL);
1243 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1244 memset(bp->data, 0, XFS_LBSIZE(mp));
1247 * Copy basic information
1249 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1251 hdr_s = &leaf_s->hdr;
1252 hdr_d = &leaf_d->hdr;
1253 hdr_d->info = hdr_s->info; /* struct copy */
1254 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
1255 /* handle truncation gracefully */
1256 if (!hdr_d->firstused) {
1257 hdr_d->firstused = cpu_to_be16(
1258 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1260 hdr_d->usedbytes = 0;
1263 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1264 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1265 sizeof(xfs_attr_leaf_hdr_t));
1268 * Copy all entry's in the same (sorted) order,
1269 * but allocate name/value pairs packed and in sequence.
1271 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1272 be16_to_cpu(hdr_s->count), mp);
1273 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1275 kmem_free(tmpbuffer, XFS_LBSIZE(mp));
1279 * Redistribute the attribute list entries between two leaf nodes,
1280 * taking into account the size of the new entry.
1282 * NOTE: if new block is empty, then it will get the upper half of the
1283 * old block. At present, all (one) callers pass in an empty second block.
1285 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1286 * to match what it is doing in splitting the attribute leaf block. Those
1287 * values are used in "atomic rename" operations on attributes. Note that
1288 * the "new" and "old" values can end up in different blocks.
1291 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1292 xfs_da_state_blk_t *blk2)
1294 xfs_da_args_t *args;
1295 xfs_da_state_blk_t *tmp_blk;
1296 xfs_attr_leafblock_t *leaf1, *leaf2;
1297 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1298 int count, totallen, max, space, swap;
1301 * Set up environment.
1303 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1304 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1305 leaf1 = blk1->bp->data;
1306 leaf2 = blk2->bp->data;
1307 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1308 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1312 * Check ordering of blocks, reverse if it makes things simpler.
1314 * NOTE: Given that all (current) callers pass in an empty
1315 * second block, this code should never set "swap".
1318 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1322 leaf1 = blk1->bp->data;
1323 leaf2 = blk2->bp->data;
1330 * Examine entries until we reduce the absolute difference in
1331 * byte usage between the two blocks to a minimum. Then get
1332 * the direction to copy and the number of elements to move.
1334 * "inleaf" is true if the new entry should be inserted into blk1.
1335 * If "swap" is also true, then reverse the sense of "inleaf".
1337 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1340 state->inleaf = !state->inleaf;
1343 * Move any entries required from leaf to leaf:
1345 if (count < be16_to_cpu(hdr1->count)) {
1347 * Figure the total bytes to be added to the destination leaf.
1349 /* number entries being moved */
1350 count = be16_to_cpu(hdr1->count) - count;
1351 space = be16_to_cpu(hdr1->usedbytes) - totallen;
1352 space += count * sizeof(xfs_attr_leaf_entry_t);
1355 * leaf2 is the destination, compact it if it looks tight.
1357 max = be16_to_cpu(hdr2->firstused)
1358 - sizeof(xfs_attr_leaf_hdr_t);
1359 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
1361 xfs_attr_leaf_compact(args->trans, blk2->bp);
1365 * Move high entries from leaf1 to low end of leaf2.
1367 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
1368 leaf2, 0, count, state->mp);
1370 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1371 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1372 } else if (count > be16_to_cpu(hdr1->count)) {
1374 * I assert that since all callers pass in an empty
1375 * second buffer, this code should never execute.
1379 * Figure the total bytes to be added to the destination leaf.
1381 /* number entries being moved */
1382 count -= be16_to_cpu(hdr1->count);
1383 space = totallen - be16_to_cpu(hdr1->usedbytes);
1384 space += count * sizeof(xfs_attr_leaf_entry_t);
1387 * leaf1 is the destination, compact it if it looks tight.
1389 max = be16_to_cpu(hdr1->firstused)
1390 - sizeof(xfs_attr_leaf_hdr_t);
1391 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
1393 xfs_attr_leaf_compact(args->trans, blk1->bp);
1397 * Move low entries from leaf2 to high end of leaf1.
1399 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1400 be16_to_cpu(hdr1->count), count, state->mp);
1402 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1403 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1407 * Copy out last hashval in each block for B-tree code.
1409 blk1->hashval = be32_to_cpu(
1410 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1411 blk2->hashval = be32_to_cpu(
1412 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
1415 * Adjust the expected index for insertion.
1416 * NOTE: this code depends on the (current) situation that the
1417 * second block was originally empty.
1419 * If the insertion point moved to the 2nd block, we must adjust
1420 * the index. We must also track the entry just following the
1421 * new entry for use in an "atomic rename" operation, that entry
1422 * is always the "old" entry and the "new" entry is what we are
1423 * inserting. The index/blkno fields refer to the "old" entry,
1424 * while the index2/blkno2 fields refer to the "new" entry.
1426 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
1427 ASSERT(state->inleaf == 0);
1428 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
1429 args->index = args->index2 = blk2->index;
1430 args->blkno = args->blkno2 = blk2->blkno;
1431 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
1432 if (state->inleaf) {
1433 args->index = blk1->index;
1434 args->blkno = blk1->blkno;
1436 args->blkno2 = blk2->blkno;
1438 blk2->index = blk1->index
1439 - be16_to_cpu(leaf1->hdr.count);
1440 args->index = args->index2 = blk2->index;
1441 args->blkno = args->blkno2 = blk2->blkno;
1444 ASSERT(state->inleaf == 1);
1445 args->index = args->index2 = blk1->index;
1446 args->blkno = args->blkno2 = blk1->blkno;
1451 * Examine entries until we reduce the absolute difference in
1452 * byte usage between the two blocks to a minimum.
1453 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1454 * GROT: there will always be enough room in either block for a new entry.
1455 * GROT: Do a double-split for this case?
1458 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1459 xfs_da_state_blk_t *blk1,
1460 xfs_da_state_blk_t *blk2,
1461 int *countarg, int *usedbytesarg)
1463 xfs_attr_leafblock_t *leaf1, *leaf2;
1464 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1465 xfs_attr_leaf_entry_t *entry;
1466 int count, max, index, totallen, half;
1467 int lastdelta, foundit, tmp;
1470 * Set up environment.
1472 leaf1 = blk1->bp->data;
1473 leaf2 = blk2->bp->data;
1480 * Examine entries until we reduce the absolute difference in
1481 * byte usage between the two blocks to a minimum.
1483 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
1484 half = (max+1) * sizeof(*entry);
1485 half += be16_to_cpu(hdr1->usedbytes) +
1486 be16_to_cpu(hdr2->usedbytes) +
1487 xfs_attr_leaf_newentsize(
1488 state->args->namelen,
1489 state->args->valuelen,
1490 state->blocksize, NULL);
1492 lastdelta = state->blocksize;
1493 entry = &leaf1->entries[0];
1494 for (count = index = 0; count < max; entry++, index++, count++) {
1496 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1498 * The new entry is in the first block, account for it.
1500 if (count == blk1->index) {
1501 tmp = totallen + sizeof(*entry) +
1502 xfs_attr_leaf_newentsize(
1503 state->args->namelen,
1504 state->args->valuelen,
1505 state->blocksize, NULL);
1506 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1508 lastdelta = XFS_ATTR_ABS(half - tmp);
1514 * Wrap around into the second block if necessary.
1516 if (count == be16_to_cpu(hdr1->count)) {
1518 entry = &leaf1->entries[0];
1523 * Figure out if next leaf entry would be too much.
1525 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1527 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1529 lastdelta = XFS_ATTR_ABS(half - tmp);
1535 * Calculate the number of usedbytes that will end up in lower block.
1536 * If new entry not in lower block, fix up the count.
1538 totallen -= count * sizeof(*entry);
1540 totallen -= sizeof(*entry) +
1541 xfs_attr_leaf_newentsize(
1542 state->args->namelen,
1543 state->args->valuelen,
1544 state->blocksize, NULL);
1548 *usedbytesarg = totallen;
1552 /*========================================================================
1553 * Routines used for shrinking the Btree.
1554 *========================================================================*/
1557 * Check a leaf block and its neighbors to see if the block should be
1558 * collapsed into one or the other neighbor. Always keep the block
1559 * with the smaller block number.
1560 * If the current block is over 50% full, don't try to join it, return 0.
1561 * If the block is empty, fill in the state structure and return 2.
1562 * If it can be collapsed, fill in the state structure and return 1.
1563 * If nothing can be done, return 0.
1565 * GROT: allow for INCOMPLETE entries in calculation.
1568 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1570 xfs_attr_leafblock_t *leaf;
1571 xfs_da_state_blk_t *blk;
1572 xfs_da_blkinfo_t *info;
1573 int count, bytes, forward, error, retval, i;
1578 * Check for the degenerate case of the block being over 50% full.
1579 * If so, it's not worth even looking to see if we might be able
1580 * to coalesce with a sibling.
1582 blk = &state->path.blk[ state->path.active-1 ];
1583 info = blk->bp->data;
1584 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
1585 leaf = (xfs_attr_leafblock_t *)info;
1586 count = be16_to_cpu(leaf->hdr.count);
1587 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1588 count * sizeof(xfs_attr_leaf_entry_t) +
1589 be16_to_cpu(leaf->hdr.usedbytes);
1590 if (bytes > (state->blocksize >> 1)) {
1591 *action = 0; /* blk over 50%, don't try to join */
1596 * Check for the degenerate case of the block being empty.
1597 * If the block is empty, we'll simply delete it, no need to
1598 * coalesce it with a sibling block. We choose (arbitrarily)
1599 * to merge with the forward block unless it is NULL.
1603 * Make altpath point to the block we want to keep and
1604 * path point to the block we want to drop (this one).
1606 forward = (info->forw != 0);
1607 memcpy(&state->altpath, &state->path, sizeof(state->path));
1608 error = xfs_da_path_shift(state, &state->altpath, forward,
1621 * Examine each sibling block to see if we can coalesce with
1622 * at least 25% free space to spare. We need to figure out
1623 * whether to merge with the forward or the backward block.
1624 * We prefer coalescing with the lower numbered sibling so as
1625 * to shrink an attribute list over time.
1627 /* start with smaller blk num */
1628 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
1629 for (i = 0; i < 2; forward = !forward, i++) {
1631 blkno = be32_to_cpu(info->forw);
1633 blkno = be32_to_cpu(info->back);
1636 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1637 blkno, -1, &bp, XFS_ATTR_FORK);
1642 leaf = (xfs_attr_leafblock_t *)info;
1643 count = be16_to_cpu(leaf->hdr.count);
1644 bytes = state->blocksize - (state->blocksize>>2);
1645 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1647 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1648 count += be16_to_cpu(leaf->hdr.count);
1649 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1650 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1651 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1652 xfs_da_brelse(state->args->trans, bp);
1654 break; /* fits with at least 25% to spare */
1662 * Make altpath point to the block we want to keep (the lower
1663 * numbered block) and path point to the block we want to drop.
1665 memcpy(&state->altpath, &state->path, sizeof(state->path));
1666 if (blkno < blk->blkno) {
1667 error = xfs_da_path_shift(state, &state->altpath, forward,
1670 error = xfs_da_path_shift(state, &state->path, forward,
1684 * Remove a name from the leaf attribute list structure.
1686 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1687 * If two leaves are 37% full, when combined they will leave 25% free.
1690 xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1692 xfs_attr_leafblock_t *leaf;
1693 xfs_attr_leaf_hdr_t *hdr;
1694 xfs_attr_leaf_map_t *map;
1695 xfs_attr_leaf_entry_t *entry;
1696 int before, after, smallest, entsize;
1697 int tablesize, tmp, i;
1701 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1703 mp = args->trans->t_mountp;
1704 ASSERT((be16_to_cpu(hdr->count) > 0)
1705 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
1706 ASSERT((args->index >= 0)
1707 && (args->index < be16_to_cpu(hdr->count)));
1708 ASSERT(be16_to_cpu(hdr->firstused) >=
1709 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1710 entry = &leaf->entries[args->index];
1711 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1712 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1715 * Scan through free region table:
1716 * check for adjacency of free'd entry with an existing one,
1717 * find smallest free region in case we need to replace it,
1718 * adjust any map that borders the entry table,
1720 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
1721 + sizeof(xfs_attr_leaf_hdr_t);
1722 map = &hdr->freemap[0];
1723 tmp = be16_to_cpu(map->size);
1724 before = after = -1;
1725 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1726 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1727 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1728 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1729 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1730 if (be16_to_cpu(map->base) == tablesize) {
1731 be16_add(&map->base,
1732 -((int)sizeof(xfs_attr_leaf_entry_t)));
1733 be16_add(&map->size, sizeof(xfs_attr_leaf_entry_t));
1736 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
1737 == be16_to_cpu(entry->nameidx)) {
1739 } else if (be16_to_cpu(map->base)
1740 == (be16_to_cpu(entry->nameidx) + entsize)) {
1742 } else if (be16_to_cpu(map->size) < tmp) {
1743 tmp = be16_to_cpu(map->size);
1749 * Coalesce adjacent freemap regions,
1750 * or replace the smallest region.
1752 if ((before >= 0) || (after >= 0)) {
1753 if ((before >= 0) && (after >= 0)) {
1754 map = &hdr->freemap[before];
1755 be16_add(&map->size, entsize);
1756 be16_add(&map->size,
1757 be16_to_cpu(hdr->freemap[after].size));
1758 hdr->freemap[after].base = 0;
1759 hdr->freemap[after].size = 0;
1760 } else if (before >= 0) {
1761 map = &hdr->freemap[before];
1762 be16_add(&map->size, entsize);
1764 map = &hdr->freemap[after];
1765 /* both on-disk, don't endian flip twice */
1766 map->base = entry->nameidx;
1767 be16_add(&map->size, entsize);
1771 * Replace smallest region (if it is smaller than free'd entry)
1773 map = &hdr->freemap[smallest];
1774 if (be16_to_cpu(map->size) < entsize) {
1775 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
1776 map->size = cpu_to_be16(entsize);
1781 * Did we remove the first entry?
1783 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
1789 * Compress the remaining entries and zero out the removed stuff.
1791 memset(XFS_ATTR_LEAF_NAME(leaf, args->index), 0, entsize);
1792 be16_add(&hdr->usedbytes, -entsize);
1793 xfs_da_log_buf(args->trans, bp,
1794 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1797 tmp = (be16_to_cpu(hdr->count) - args->index)
1798 * sizeof(xfs_attr_leaf_entry_t);
1799 memmove((char *)entry, (char *)(entry+1), tmp);
1800 be16_add(&hdr->count, -1);
1801 xfs_da_log_buf(args->trans, bp,
1802 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1803 entry = &leaf->entries[be16_to_cpu(hdr->count)];
1804 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1807 * If we removed the first entry, re-find the first used byte
1808 * in the name area. Note that if the entry was the "firstused",
1809 * then we don't have a "hole" in our block resulting from
1810 * removing the name.
1813 tmp = XFS_LBSIZE(mp);
1814 entry = &leaf->entries[0];
1815 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
1816 ASSERT(be16_to_cpu(entry->nameidx) >=
1817 be16_to_cpu(hdr->firstused));
1818 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1820 if (be16_to_cpu(entry->nameidx) < tmp)
1821 tmp = be16_to_cpu(entry->nameidx);
1823 hdr->firstused = cpu_to_be16(tmp);
1824 if (!hdr->firstused) {
1825 hdr->firstused = cpu_to_be16(
1826 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1829 hdr->holes = 1; /* mark as needing compaction */
1831 xfs_da_log_buf(args->trans, bp,
1832 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1835 * Check if leaf is less than 50% full, caller may want to
1836 * "join" the leaf with a sibling if so.
1838 tmp = sizeof(xfs_attr_leaf_hdr_t);
1839 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1840 tmp += be16_to_cpu(leaf->hdr.usedbytes);
1841 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1845 * Move all the attribute list entries from drop_leaf into save_leaf.
1848 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1849 xfs_da_state_blk_t *save_blk)
1851 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1852 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1857 * Set up environment.
1860 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1861 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1862 drop_leaf = drop_blk->bp->data;
1863 save_leaf = save_blk->bp->data;
1864 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1865 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1866 drop_hdr = &drop_leaf->hdr;
1867 save_hdr = &save_leaf->hdr;
1870 * Save last hashval from dying block for later Btree fixup.
1872 drop_blk->hashval = be32_to_cpu(
1873 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
1876 * Check if we need a temp buffer, or can we do it in place.
1877 * Note that we don't check "leaf" for holes because we will
1878 * always be dropping it, toosmall() decided that for us already.
1880 if (save_hdr->holes == 0) {
1882 * dest leaf has no holes, so we add there. May need
1883 * to make some room in the entry array.
1885 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1886 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1887 be16_to_cpu(drop_hdr->count), mp);
1889 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1890 be16_to_cpu(save_hdr->count),
1891 be16_to_cpu(drop_hdr->count), mp);
1895 * Destination has holes, so we make a temporary copy
1896 * of the leaf and add them both to that.
1898 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1899 ASSERT(tmpbuffer != NULL);
1900 memset(tmpbuffer, 0, state->blocksize);
1901 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1902 tmp_hdr = &tmp_leaf->hdr;
1903 tmp_hdr->info = save_hdr->info; /* struct copy */
1905 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
1906 if (!tmp_hdr->firstused) {
1907 tmp_hdr->firstused = cpu_to_be16(
1908 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1910 tmp_hdr->usedbytes = 0;
1911 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1912 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1913 be16_to_cpu(drop_hdr->count), mp);
1914 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1915 be16_to_cpu(tmp_leaf->hdr.count),
1916 be16_to_cpu(save_hdr->count), mp);
1918 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1919 be16_to_cpu(save_hdr->count), mp);
1920 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1921 be16_to_cpu(tmp_leaf->hdr.count),
1922 be16_to_cpu(drop_hdr->count), mp);
1924 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1925 kmem_free(tmpbuffer, state->blocksize);
1928 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1929 state->blocksize - 1);
1932 * Copy out last hashval in each block for B-tree code.
1934 save_blk->hashval = be32_to_cpu(
1935 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
1938 /*========================================================================
1939 * Routines used for finding things in the Btree.
1940 *========================================================================*/
1943 * Look up a name in a leaf attribute list structure.
1944 * This is the internal routine, it uses the caller's buffer.
1946 * Note that duplicate keys are allowed, but only check within the
1947 * current leaf node. The Btree code must check in adjacent leaf nodes.
1949 * Return in args->index the index into the entry[] array of either
1950 * the found entry, or where the entry should have been (insert before
1953 * Don't change the args->value unless we find the attribute.
1956 xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1958 xfs_attr_leafblock_t *leaf;
1959 xfs_attr_leaf_entry_t *entry;
1960 xfs_attr_leaf_name_local_t *name_loc;
1961 xfs_attr_leaf_name_remote_t *name_rmt;
1963 xfs_dahash_t hashval;
1966 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1967 ASSERT(be16_to_cpu(leaf->hdr.count)
1968 < (XFS_LBSIZE(args->dp->i_mount)/8));
1971 * Binary search. (note: small blocks will skip this loop)
1973 hashval = args->hashval;
1974 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
1975 for (entry = &leaf->entries[probe]; span > 4;
1976 entry = &leaf->entries[probe]) {
1978 if (be32_to_cpu(entry->hashval) < hashval)
1980 else if (be32_to_cpu(entry->hashval) > hashval)
1985 ASSERT((probe >= 0) &&
1987 || (probe < be16_to_cpu(leaf->hdr.count))));
1988 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
1991 * Since we may have duplicate hashval's, find the first matching
1992 * hashval in the leaf.
1994 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
1998 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1999 (be32_to_cpu(entry->hashval) < hashval)) {
2003 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2004 (be32_to_cpu(entry->hashval) != hashval)) {
2005 args->index = probe;
2006 return(XFS_ERROR(ENOATTR));
2010 * Duplicate keys may be present, so search all of them for a match.
2012 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2013 (be32_to_cpu(entry->hashval) == hashval);
2016 * GROT: Add code to remove incomplete entries.
2019 * If we are looking for INCOMPLETE entries, show only those.
2020 * If we are looking for complete entries, show only those.
2022 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2023 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2026 if (entry->flags & XFS_ATTR_LOCAL) {
2027 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, probe);
2028 if (name_loc->namelen != args->namelen)
2030 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
2032 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2034 args->index = probe;
2035 return(XFS_ERROR(EEXIST));
2037 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, probe);
2038 if (name_rmt->namelen != args->namelen)
2040 if (memcmp(args->name, (char *)name_rmt->name,
2041 args->namelen) != 0)
2043 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2045 args->index = probe;
2046 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2047 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2048 be32_to_cpu(name_rmt->valuelen));
2049 return(XFS_ERROR(EEXIST));
2052 args->index = probe;
2053 return(XFS_ERROR(ENOATTR));
2057 * Get the value associated with an attribute name from a leaf attribute
2061 xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2064 xfs_attr_leafblock_t *leaf;
2065 xfs_attr_leaf_entry_t *entry;
2066 xfs_attr_leaf_name_local_t *name_loc;
2067 xfs_attr_leaf_name_remote_t *name_rmt;
2070 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2071 ASSERT(be16_to_cpu(leaf->hdr.count)
2072 < (XFS_LBSIZE(args->dp->i_mount)/8));
2073 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2075 entry = &leaf->entries[args->index];
2076 if (entry->flags & XFS_ATTR_LOCAL) {
2077 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2078 ASSERT(name_loc->namelen == args->namelen);
2079 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2080 valuelen = be16_to_cpu(name_loc->valuelen);
2081 if (args->flags & ATTR_KERNOVAL) {
2082 args->valuelen = valuelen;
2085 if (args->valuelen < valuelen) {
2086 args->valuelen = valuelen;
2087 return(XFS_ERROR(ERANGE));
2089 args->valuelen = valuelen;
2090 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2092 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2093 ASSERT(name_rmt->namelen == args->namelen);
2094 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2095 valuelen = be32_to_cpu(name_rmt->valuelen);
2096 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2097 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2098 if (args->flags & ATTR_KERNOVAL) {
2099 args->valuelen = valuelen;
2102 if (args->valuelen < valuelen) {
2103 args->valuelen = valuelen;
2104 return(XFS_ERROR(ERANGE));
2106 args->valuelen = valuelen;
2111 /*========================================================================
2113 *========================================================================*/
2116 * Move the indicated entries from one leaf to another.
2117 * NOTE: this routine modifies both source and destination leaves.
2121 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2122 xfs_attr_leafblock_t *leaf_d, int start_d,
2123 int count, xfs_mount_t *mp)
2125 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2126 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2130 * Check for nothing to do.
2136 * Set up environment.
2138 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2139 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2140 hdr_s = &leaf_s->hdr;
2141 hdr_d = &leaf_d->hdr;
2142 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2143 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2144 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2145 ((be16_to_cpu(hdr_s->count)
2146 * sizeof(*entry_s))+sizeof(*hdr_s)));
2147 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2148 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2149 ((be16_to_cpu(hdr_d->count)
2150 * sizeof(*entry_d))+sizeof(*hdr_d)));
2152 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2153 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2154 ASSERT(count <= be16_to_cpu(hdr_s->count));
2157 * Move the entries in the destination leaf up to make a hole?
2159 if (start_d < be16_to_cpu(hdr_d->count)) {
2160 tmp = be16_to_cpu(hdr_d->count) - start_d;
2161 tmp *= sizeof(xfs_attr_leaf_entry_t);
2162 entry_s = &leaf_d->entries[start_d];
2163 entry_d = &leaf_d->entries[start_d + count];
2164 memmove((char *)entry_d, (char *)entry_s, tmp);
2168 * Copy all entry's in the same (sorted) order,
2169 * but allocate attribute info packed and in sequence.
2171 entry_s = &leaf_s->entries[start_s];
2172 entry_d = &leaf_d->entries[start_d];
2174 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2175 ASSERT(be16_to_cpu(entry_s->nameidx)
2176 >= be16_to_cpu(hdr_s->firstused));
2177 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2180 * Code to drop INCOMPLETE entries. Difficult to use as we
2181 * may also need to change the insertion index. Code turned
2182 * off for 6.2, should be revisited later.
2184 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2185 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2186 be16_add(&hdr_s->usedbytes, -tmp);
2187 be16_add(&hdr_s->count, -1);
2188 entry_d--; /* to compensate for ++ in loop hdr */
2190 if ((start_s + i) < offset)
2191 result++; /* insertion index adjustment */
2194 be16_add(&hdr_d->firstused, -tmp);
2195 /* both on-disk, don't endian flip twice */
2196 entry_d->hashval = entry_s->hashval;
2197 /* both on-disk, don't endian flip twice */
2198 entry_d->nameidx = hdr_d->firstused;
2199 entry_d->flags = entry_s->flags;
2200 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2202 memmove(XFS_ATTR_LEAF_NAME(leaf_d, desti),
2203 XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), tmp);
2204 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2206 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2207 be16_add(&hdr_s->usedbytes, -tmp);
2208 be16_add(&hdr_d->usedbytes, tmp);
2209 be16_add(&hdr_s->count, -1);
2210 be16_add(&hdr_d->count, 1);
2211 tmp = be16_to_cpu(hdr_d->count)
2212 * sizeof(xfs_attr_leaf_entry_t)
2213 + sizeof(xfs_attr_leaf_hdr_t);
2214 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
2221 * Zero out the entries we just copied.
2223 if (start_s == be16_to_cpu(hdr_s->count)) {
2224 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2225 entry_s = &leaf_s->entries[start_s];
2226 ASSERT(((char *)entry_s + tmp) <=
2227 ((char *)leaf_s + XFS_LBSIZE(mp)));
2228 memset((char *)entry_s, 0, tmp);
2231 * Move the remaining entries down to fill the hole,
2232 * then zero the entries at the top.
2234 tmp = be16_to_cpu(hdr_s->count) - count;
2235 tmp *= sizeof(xfs_attr_leaf_entry_t);
2236 entry_s = &leaf_s->entries[start_s + count];
2237 entry_d = &leaf_s->entries[start_s];
2238 memmove((char *)entry_d, (char *)entry_s, tmp);
2240 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2241 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
2242 ASSERT(((char *)entry_s + tmp) <=
2243 ((char *)leaf_s + XFS_LBSIZE(mp)));
2244 memset((char *)entry_s, 0, tmp);
2248 * Fill in the freemap information
2250 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2251 be16_add(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2252 sizeof(xfs_attr_leaf_entry_t));
2253 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2254 - be16_to_cpu(hdr_d->freemap[0].base));
2255 hdr_d->freemap[1].base = 0;
2256 hdr_d->freemap[2].base = 0;
2257 hdr_d->freemap[1].size = 0;
2258 hdr_d->freemap[2].size = 0;
2259 hdr_s->holes = 1; /* leaf may not be compact */
2263 * Compare two leaf blocks "order".
2264 * Return 0 unless leaf2 should go before leaf1.
2267 xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2269 xfs_attr_leafblock_t *leaf1, *leaf2;
2271 leaf1 = leaf1_bp->data;
2272 leaf2 = leaf2_bp->data;
2273 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2274 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
2275 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2276 (be16_to_cpu(leaf2->hdr.count) > 0) &&
2277 ((be32_to_cpu(leaf2->entries[0].hashval) <
2278 be32_to_cpu(leaf1->entries[0].hashval)) ||
2279 (be32_to_cpu(leaf2->entries[
2280 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2281 be32_to_cpu(leaf1->entries[
2282 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
2289 * Pick up the last hashvalue from a leaf block.
2292 xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2294 xfs_attr_leafblock_t *leaf;
2297 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2299 *count = be16_to_cpu(leaf->hdr.count);
2300 if (!leaf->hdr.count)
2302 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
2306 * Calculate the number of bytes used to store the indicated attribute
2307 * (whether local or remote only calculate bytes in this block).
2310 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2312 xfs_attr_leaf_name_local_t *name_loc;
2313 xfs_attr_leaf_name_remote_t *name_rmt;
2316 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2317 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2318 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, index);
2319 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc->namelen,
2320 be16_to_cpu(name_loc->valuelen));
2322 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, index);
2323 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt->namelen);
2329 * Calculate the number of bytes that would be required to store the new
2330 * attribute (whether local or remote only calculate bytes in this block).
2331 * This routine decides as a side effect whether the attribute will be
2332 * a "local" or a "remote" attribute.
2335 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2339 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen, valuelen);
2340 if (size < XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize)) {
2345 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen);
2354 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2357 xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2359 attrlist_cursor_kern_t *cursor;
2360 xfs_attr_leafblock_t *leaf;
2361 xfs_attr_leaf_entry_t *entry;
2366 cursor = context->cursor;
2367 cursor->initted = 1;
2369 xfs_attr_trace_l_cl("blk start", context, leaf);
2372 * Re-find our place in the leaf block if this is a new syscall.
2374 if (context->resynch) {
2375 entry = &leaf->entries[0];
2376 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2377 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
2378 if (cursor->offset == context->dupcnt) {
2379 context->dupcnt = 0;
2383 } else if (be32_to_cpu(entry->hashval) >
2385 context->dupcnt = 0;
2389 if (i == be16_to_cpu(leaf->hdr.count)) {
2390 xfs_attr_trace_l_c("not found", context);
2394 entry = &leaf->entries[0];
2397 context->resynch = 0;
2400 * We have found our place, start copying out the new attributes.
2403 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
2404 attrnames_t *namesp;
2406 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2407 cursor->hashval = be32_to_cpu(entry->hashval);
2411 if (entry->flags & XFS_ATTR_INCOMPLETE)
2412 continue; /* skip incomplete entries */
2413 if (!xfs_attr_namesp_match_overrides(context->flags, entry->flags))
2416 namesp = xfs_attr_flags_namesp(entry->flags);
2418 if (entry->flags & XFS_ATTR_LOCAL) {
2419 xfs_attr_leaf_name_local_t *name_loc =
2420 XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
2422 retval = context->put_listent(context,
2424 (char *)name_loc->nameval,
2425 (int)name_loc->namelen,
2426 be16_to_cpu(name_loc->valuelen),
2427 (char *)&name_loc->nameval[name_loc->namelen]);
2431 xfs_attr_leaf_name_remote_t *name_rmt =
2432 XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2434 int valuelen = be32_to_cpu(name_rmt->valuelen);
2436 if (context->put_value) {
2439 memset((char *)&args, 0, sizeof(args));
2440 args.dp = context->dp;
2441 args.whichfork = XFS_ATTR_FORK;
2442 args.valuelen = valuelen;
2443 args.value = kmem_alloc(valuelen, KM_SLEEP);
2444 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2445 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2446 retval = xfs_attr_rmtval_get(&args);
2449 retval = context->put_listent(context,
2451 (char *)name_rmt->name,
2452 (int)name_rmt->namelen,
2455 kmem_free(args.value, valuelen);
2458 retval = context->put_listent(context,
2460 (char *)name_rmt->name,
2461 (int)name_rmt->namelen,
2468 if (context->seen_enough)
2472 xfs_attr_trace_l_cl("blk end", context, leaf);
2477 /*========================================================================
2478 * Manage the INCOMPLETE flag in a leaf entry
2479 *========================================================================*/
2482 * Clear the INCOMPLETE flag on an entry in a leaf block.
2485 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2487 xfs_attr_leafblock_t *leaf;
2488 xfs_attr_leaf_entry_t *entry;
2489 xfs_attr_leaf_name_remote_t *name_rmt;
2493 xfs_attr_leaf_name_local_t *name_loc;
2499 * Set up the operation.
2501 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2509 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2510 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2511 ASSERT(args->index >= 0);
2512 entry = &leaf->entries[ args->index ];
2513 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2516 if (entry->flags & XFS_ATTR_LOCAL) {
2517 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2518 namelen = name_loc->namelen;
2519 name = (char *)name_loc->nameval;
2521 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2522 namelen = name_rmt->namelen;
2523 name = (char *)name_rmt->name;
2525 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2526 ASSERT(namelen == args->namelen);
2527 ASSERT(memcmp(name, args->name, namelen) == 0);
2530 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2531 xfs_da_log_buf(args->trans, bp,
2532 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2534 if (args->rmtblkno) {
2535 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2536 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2537 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2538 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2539 xfs_da_log_buf(args->trans, bp,
2540 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2542 xfs_da_buf_done(bp);
2545 * Commit the flag value change and start the next trans in series.
2547 error = xfs_attr_rolltrans(&args->trans, args->dp);
2553 * Set the INCOMPLETE flag on an entry in a leaf block.
2556 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2558 xfs_attr_leafblock_t *leaf;
2559 xfs_attr_leaf_entry_t *entry;
2560 xfs_attr_leaf_name_remote_t *name_rmt;
2565 * Set up the operation.
2567 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2575 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2576 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2577 ASSERT(args->index >= 0);
2578 entry = &leaf->entries[ args->index ];
2580 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2581 entry->flags |= XFS_ATTR_INCOMPLETE;
2582 xfs_da_log_buf(args->trans, bp,
2583 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2584 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2585 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2586 name_rmt->valueblk = 0;
2587 name_rmt->valuelen = 0;
2588 xfs_da_log_buf(args->trans, bp,
2589 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2591 xfs_da_buf_done(bp);
2594 * Commit the flag value change and start the next trans in series.
2596 error = xfs_attr_rolltrans(&args->trans, args->dp);
2602 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2603 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2604 * entry given by args->blkno2/index2.
2606 * Note that they could be in different blocks, or in the same block.
2609 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2611 xfs_attr_leafblock_t *leaf1, *leaf2;
2612 xfs_attr_leaf_entry_t *entry1, *entry2;
2613 xfs_attr_leaf_name_remote_t *name_rmt;
2614 xfs_dabuf_t *bp1, *bp2;
2617 xfs_attr_leaf_name_local_t *name_loc;
2618 int namelen1, namelen2;
2619 char *name1, *name2;
2623 * Read the block containing the "old" attr
2625 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2630 ASSERT(bp1 != NULL);
2633 * Read the block containing the "new" attr, if it is different
2635 if (args->blkno2 != args->blkno) {
2636 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2637 -1, &bp2, XFS_ATTR_FORK);
2641 ASSERT(bp2 != NULL);
2647 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2648 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
2649 ASSERT(args->index >= 0);
2650 entry1 = &leaf1->entries[ args->index ];
2653 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2654 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
2655 ASSERT(args->index2 >= 0);
2656 entry2 = &leaf2->entries[ args->index2 ];
2659 if (entry1->flags & XFS_ATTR_LOCAL) {
2660 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf1, args->index);
2661 namelen1 = name_loc->namelen;
2662 name1 = (char *)name_loc->nameval;
2664 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2665 namelen1 = name_rmt->namelen;
2666 name1 = (char *)name_rmt->name;
2668 if (entry2->flags & XFS_ATTR_LOCAL) {
2669 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf2, args->index2);
2670 namelen2 = name_loc->namelen;
2671 name2 = (char *)name_loc->nameval;
2673 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2674 namelen2 = name_rmt->namelen;
2675 name2 = (char *)name_rmt->name;
2677 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2678 ASSERT(namelen1 == namelen2);
2679 ASSERT(memcmp(name1, name2, namelen1) == 0);
2682 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2683 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2685 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2686 xfs_da_log_buf(args->trans, bp1,
2687 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2688 if (args->rmtblkno) {
2689 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2690 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2691 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2692 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2693 xfs_da_log_buf(args->trans, bp1,
2694 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2697 entry2->flags |= XFS_ATTR_INCOMPLETE;
2698 xfs_da_log_buf(args->trans, bp2,
2699 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2700 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2701 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2702 name_rmt->valueblk = 0;
2703 name_rmt->valuelen = 0;
2704 xfs_da_log_buf(args->trans, bp2,
2705 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2707 xfs_da_buf_done(bp1);
2709 xfs_da_buf_done(bp2);
2712 * Commit the flag value change and start the next trans in series.
2714 error = xfs_attr_rolltrans(&args->trans, args->dp);
2719 /*========================================================================
2720 * Indiscriminately delete the entire attribute fork
2721 *========================================================================*/
2724 * Recurse (gasp!) through the attribute nodes until we find leaves.
2725 * We're doing a depth-first traversal in order to invalidate everything.
2728 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2730 xfs_da_blkinfo_t *info;
2736 * Read block 0 to see what we have to work with.
2737 * We only get here if we have extents, since we remove
2738 * the extents in reverse order the extent containing
2739 * block 0 must still be there.
2741 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2744 blkno = xfs_da_blkno(bp);
2747 * Invalidate the tree, even if the "tree" is only a single leaf block.
2748 * This is a depth-first traversal!
2751 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2752 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2753 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2754 error = xfs_attr_leaf_inactive(trans, dp, bp);
2756 error = XFS_ERROR(EIO);
2757 xfs_da_brelse(*trans, bp);
2763 * Invalidate the incore copy of the root block.
2765 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2768 xfs_da_binval(*trans, bp); /* remove from cache */
2770 * Commit the invalidate and start the next transaction.
2772 error = xfs_attr_rolltrans(trans, dp);
2778 * Recurse (gasp!) through the attribute nodes until we find leaves.
2779 * We're doing a depth-first traversal in order to invalidate everything.
2782 xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2785 xfs_da_blkinfo_t *info;
2786 xfs_da_intnode_t *node;
2787 xfs_dablk_t child_fsb;
2788 xfs_daddr_t parent_blkno, child_blkno;
2789 int error, count, i;
2790 xfs_dabuf_t *child_bp;
2793 * Since this code is recursive (gasp!) we must protect ourselves.
2795 if (level > XFS_DA_NODE_MAXDEPTH) {
2796 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2797 return(XFS_ERROR(EIO));
2801 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
2802 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
2803 count = be16_to_cpu(node->hdr.count);
2805 xfs_da_brelse(*trans, bp);
2808 child_fsb = be32_to_cpu(node->btree[0].before);
2809 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2812 * If this is the node level just above the leaves, simply loop
2813 * over the leaves removing all of them. If this is higher up
2814 * in the tree, recurse downward.
2816 for (i = 0; i < count; i++) {
2818 * Read the subsidiary block to see what we have to work with.
2819 * Don't do this in a transaction. This is a depth-first
2820 * traversal of the tree so we may deal with many blocks
2821 * before we come back to this one.
2823 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2828 /* save for re-read later */
2829 child_blkno = xfs_da_blkno(child_bp);
2832 * Invalidate the subtree, however we have to.
2834 info = child_bp->data;
2835 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2836 error = xfs_attr_node_inactive(trans, dp,
2838 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2839 error = xfs_attr_leaf_inactive(trans, dp,
2842 error = XFS_ERROR(EIO);
2843 xfs_da_brelse(*trans, child_bp);
2849 * Remove the subsidiary block from the cache
2852 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2853 &child_bp, XFS_ATTR_FORK);
2856 xfs_da_binval(*trans, child_bp);
2860 * If we're not done, re-read the parent to get the next
2861 * child block number.
2863 if ((i+1) < count) {
2864 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2865 &bp, XFS_ATTR_FORK);
2868 child_fsb = be32_to_cpu(node->btree[i+1].before);
2869 xfs_da_brelse(*trans, bp);
2872 * Atomically commit the whole invalidate stuff.
2874 if ((error = xfs_attr_rolltrans(trans, dp)))
2882 * Invalidate all of the "remote" value regions pointed to by a particular
2884 * Note that we must release the lock on the buffer so that we are not
2885 * caught holding something that the logging code wants to flush to disk.
2888 xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2890 xfs_attr_leafblock_t *leaf;
2891 xfs_attr_leaf_entry_t *entry;
2892 xfs_attr_leaf_name_remote_t *name_rmt;
2893 xfs_attr_inactive_list_t *list, *lp;
2894 int error, count, size, tmp, i;
2897 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2900 * Count the number of "remote" value extents.
2903 entry = &leaf->entries[0];
2904 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2905 if (be16_to_cpu(entry->nameidx) &&
2906 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2907 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2908 if (name_rmt->valueblk)
2914 * If there are no "remote" values, we're done.
2917 xfs_da_brelse(*trans, bp);
2922 * Allocate storage for a list of all the "remote" value extents.
2924 size = count * sizeof(xfs_attr_inactive_list_t);
2925 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2928 * Identify each of the "remote" value extents.
2931 entry = &leaf->entries[0];
2932 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2933 if (be16_to_cpu(entry->nameidx) &&
2934 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2935 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2936 if (name_rmt->valueblk) {
2937 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2938 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2939 be32_to_cpu(name_rmt->valuelen));
2944 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2947 * Invalidate each of the "remote" value extents.
2950 for (lp = list, i = 0; i < count; i++, lp++) {
2951 tmp = xfs_attr_leaf_freextent(trans, dp,
2952 lp->valueblk, lp->valuelen);
2955 error = tmp; /* save only the 1st errno */
2958 kmem_free((xfs_caddr_t)list, size);
2963 * Look at all the extents for this logical region,
2964 * invalidate any buffers that are incore/in transactions.
2967 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2968 xfs_dablk_t blkno, int blkcnt)
2970 xfs_bmbt_irec_t map;
2972 int tblkcnt, dblkcnt, nmap, error;
2977 * Roll through the "value", invalidating the attribute value's
2982 while (tblkcnt > 0) {
2984 * Try to remember where we decided to put the value.
2987 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2988 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2989 NULL, 0, &map, &nmap, NULL, NULL);
2994 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2997 * If it's a hole, these are already unmapped
2998 * so there's nothing to invalidate.
3000 if (map.br_startblock != HOLESTARTBLOCK) {
3002 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3004 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3006 bp = xfs_trans_get_buf(*trans,
3007 dp->i_mount->m_ddev_targp,
3008 dblkno, dblkcnt, XFS_BUF_LOCK);
3009 xfs_trans_binval(*trans, bp);
3011 * Roll to next transaction.
3013 if ((error = xfs_attr_rolltrans(trans, dp)))
3017 tblkno += map.br_blockcount;
3018 tblkcnt -= map.br_blockcount;
3026 * Roll from one trans in the sequence of PERMANENT transactions to the next.
3029 xfs_attr_rolltrans(xfs_trans_t **transp, xfs_inode_t *dp)
3032 unsigned int logres, count;
3036 * Ensure that the inode is always logged.
3039 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
3042 * Copy the critical parameters from one trans to the next.
3044 logres = trans->t_log_res;
3045 count = trans->t_log_count;
3046 *transp = xfs_trans_dup(trans);
3049 * Commit the current transaction.
3050 * If this commit failed, then it'd just unlock those items that
3051 * are not marked ihold. That also means that a filesystem shutdown
3052 * is in progress. The caller takes the responsibility to cancel
3053 * the duplicate transaction that gets returned.
3055 if ((error = xfs_trans_commit(trans, 0, NULL)))
3061 * Reserve space in the log for th next transaction.
3062 * This also pushes items in the "AIL", the list of logged items,
3063 * out to disk if they are taking up space at the tail of the log
3064 * that we want to use. This requires that either nothing be locked
3065 * across this call, or that anything that is locked be logged in
3066 * the prior and the next transactions.
3068 error = xfs_trans_reserve(trans, 0, logres, 0,
3069 XFS_TRANS_PERM_LOG_RES, count);
3071 * Ensure that the inode is in the new transaction and locked.
3074 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
3075 xfs_trans_ihold(trans, dp);