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"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_alloc.h"
36 #include "xfs_btree.h"
37 #include "xfs_dir_sf.h"
38 #include "xfs_dir2_sf.h"
39 #include "xfs_attr_sf.h"
40 #include "xfs_dinode.h"
41 #include "xfs_inode.h"
42 #include "xfs_inode_item.h"
45 #include "xfs_attr_leaf.h"
46 #include "xfs_error.h"
51 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
54 /*========================================================================
55 * Function prototypes for the kernel.
56 *========================================================================*/
59 * Routines used for growing the Btree.
61 STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
63 STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
65 STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
66 STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
67 xfs_da_state_blk_t *blk1,
68 xfs_da_state_blk_t *blk2);
69 STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
70 xfs_da_state_blk_t *leaf_blk_1,
71 xfs_da_state_blk_t *leaf_blk_2,
72 int *number_entries_in_blk1,
73 int *number_usedbytes_in_blk1);
76 * Routines used for shrinking the Btree.
78 STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
79 xfs_dabuf_t *bp, int level);
80 STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
82 STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
83 xfs_dablk_t blkno, int blkcnt);
88 STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
90 xfs_attr_leafblock_t *dst_leaf,
91 int dst_start, int move_count,
93 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
94 STATIC int xfs_attr_put_listent(xfs_attr_list_context_t *context,
95 attrnames_t *, char *name, int namelen,
99 /*========================================================================
100 * External routines when attribute fork size < XFS_LITINO(mp).
101 *========================================================================*/
104 * Query whether the requested number of additional bytes of extended
105 * attribute space will be able to fit inline.
106 * Returns zero if not, else the di_forkoff fork offset to be used in the
107 * literal area for attribute data once the new bytes have been added.
109 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
110 * special case for dev/uuid inodes, they have fixed size data forks.
113 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
116 int minforkoff; /* lower limit on valid forkoff locations */
117 int maxforkoff; /* upper limit on valid forkoff locations */
118 xfs_mount_t *mp = dp->i_mount;
120 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
122 switch (dp->i_d.di_format) {
123 case XFS_DINODE_FMT_DEV:
124 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
125 return (offset >= minforkoff) ? minforkoff : 0;
126 case XFS_DINODE_FMT_UUID:
127 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
128 return (offset >= minforkoff) ? minforkoff : 0;
131 if (unlikely(mp->m_flags & XFS_MOUNT_COMPAT_ATTR)) {
132 if (bytes <= XFS_IFORK_ASIZE(dp))
133 return mp->m_attroffset >> 3;
137 /* data fork btree root can have at least this many key/ptr pairs */
138 minforkoff = MAX(dp->i_df.if_bytes, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
139 minforkoff = roundup(minforkoff, 8) >> 3;
141 /* attr fork btree root can have at least this many key/ptr pairs */
142 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
143 maxforkoff = maxforkoff >> 3; /* rounded down */
145 if (offset >= minforkoff && offset < maxforkoff)
147 if (offset >= maxforkoff)
153 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
156 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
160 if (!(mp->m_flags & XFS_MOUNT_COMPAT_ATTR) &&
161 !(XFS_SB_VERSION_HASATTR2(&mp->m_sb))) {
163 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb)) {
164 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
165 XFS_SB_UNLOCK(mp, s);
166 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
168 XFS_SB_UNLOCK(mp, s);
173 * Create the initial contents of a shortform attribute list.
176 xfs_attr_shortform_create(xfs_da_args_t *args)
178 xfs_attr_sf_hdr_t *hdr;
186 ASSERT(ifp->if_bytes == 0);
187 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
188 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
189 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
190 ifp->if_flags |= XFS_IFINLINE;
192 ASSERT(ifp->if_flags & XFS_IFINLINE);
194 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
195 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
197 INT_SET(hdr->totsize, ARCH_CONVERT, sizeof(*hdr));
198 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
202 * Add a name/value pair to the shortform attribute list.
203 * Overflow from the inode has already been checked for.
206 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
208 xfs_attr_shortform_t *sf;
209 xfs_attr_sf_entry_t *sfe;
217 dp->i_d.di_forkoff = forkoff;
218 dp->i_df.if_ext_max =
219 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
220 dp->i_afp->if_ext_max =
221 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
224 ASSERT(ifp->if_flags & XFS_IFINLINE);
225 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
227 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
228 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
230 if (sfe->namelen != args->namelen)
232 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
234 if (((args->flags & ATTR_SECURE) != 0) !=
235 ((sfe->flags & XFS_ATTR_SECURE) != 0))
237 if (((args->flags & ATTR_ROOT) != 0) !=
238 ((sfe->flags & XFS_ATTR_ROOT) != 0))
244 offset = (char *)sfe - (char *)sf;
245 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
246 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
247 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
248 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
250 sfe->namelen = args->namelen;
251 INT_SET(sfe->valuelen, ARCH_CONVERT, args->valuelen);
252 sfe->flags = (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
253 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
254 memcpy(sfe->nameval, args->name, args->namelen);
255 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
256 INT_MOD(sf->hdr.count, ARCH_CONVERT, 1);
257 INT_MOD(sf->hdr.totsize, ARCH_CONVERT, size);
258 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
260 xfs_sbversion_add_attr2(mp, args->trans);
264 * Remove an attribute from the shortform attribute list structure.
267 xfs_attr_shortform_remove(xfs_da_args_t *args)
269 xfs_attr_shortform_t *sf;
270 xfs_attr_sf_entry_t *sfe;
271 int base, size=0, end, totsize, i;
277 base = sizeof(xfs_attr_sf_hdr_t);
278 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
280 end = INT_GET(sf->hdr.count, ARCH_CONVERT);
281 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
283 size = XFS_ATTR_SF_ENTSIZE(sfe);
284 if (sfe->namelen != args->namelen)
286 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
288 if (((args->flags & ATTR_SECURE) != 0) !=
289 ((sfe->flags & XFS_ATTR_SECURE) != 0))
291 if (((args->flags & ATTR_ROOT) != 0) !=
292 ((sfe->flags & XFS_ATTR_ROOT) != 0))
297 return(XFS_ERROR(ENOATTR));
300 * Fix up the attribute fork data, covering the hole
303 totsize = INT_GET(sf->hdr.totsize, ARCH_CONVERT);
305 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
306 INT_MOD(sf->hdr.count, ARCH_CONVERT, -1);
307 INT_MOD(sf->hdr.totsize, ARCH_CONVERT, -size);
310 * Fix up the start offset of the attribute fork
313 if (totsize == sizeof(xfs_attr_sf_hdr_t) && !args->addname) {
315 * Last attribute now removed, revert to original
316 * inode format making all literal area available
317 * to the data fork once more.
319 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
320 dp->i_d.di_forkoff = 0;
321 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
322 ASSERT(dp->i_d.di_anextents == 0);
323 ASSERT(dp->i_afp == NULL);
324 dp->i_df.if_ext_max =
325 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
326 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
328 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
329 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
330 ASSERT(dp->i_d.di_forkoff);
331 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || args->addname);
332 dp->i_afp->if_ext_max =
333 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
334 dp->i_df.if_ext_max =
335 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
336 xfs_trans_log_inode(args->trans, dp,
337 XFS_ILOG_CORE | XFS_ILOG_ADATA);
340 xfs_sbversion_add_attr2(mp, args->trans);
346 * Look up a name in a shortform attribute list structure.
350 xfs_attr_shortform_lookup(xfs_da_args_t *args)
352 xfs_attr_shortform_t *sf;
353 xfs_attr_sf_entry_t *sfe;
357 ifp = args->dp->i_afp;
358 ASSERT(ifp->if_flags & XFS_IFINLINE);
359 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
361 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
362 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
363 if (sfe->namelen != args->namelen)
365 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
367 if (((args->flags & ATTR_SECURE) != 0) !=
368 ((sfe->flags & XFS_ATTR_SECURE) != 0))
370 if (((args->flags & ATTR_ROOT) != 0) !=
371 ((sfe->flags & XFS_ATTR_ROOT) != 0))
373 return(XFS_ERROR(EEXIST));
375 return(XFS_ERROR(ENOATTR));
379 * Look up a name in a shortform attribute list structure.
383 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
385 xfs_attr_shortform_t *sf;
386 xfs_attr_sf_entry_t *sfe;
389 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
390 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
392 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
393 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
394 if (sfe->namelen != args->namelen)
396 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
398 if (((args->flags & ATTR_SECURE) != 0) !=
399 ((sfe->flags & XFS_ATTR_SECURE) != 0))
401 if (((args->flags & ATTR_ROOT) != 0) !=
402 ((sfe->flags & XFS_ATTR_ROOT) != 0))
404 if (args->flags & ATTR_KERNOVAL) {
405 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
406 return(XFS_ERROR(EEXIST));
408 if (args->valuelen < INT_GET(sfe->valuelen, ARCH_CONVERT)) {
409 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
410 return(XFS_ERROR(ERANGE));
412 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
413 memcpy(args->value, &sfe->nameval[args->namelen],
415 return(XFS_ERROR(EEXIST));
417 return(XFS_ERROR(ENOATTR));
421 * Convert from using the shortform to the leaf.
424 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
427 xfs_attr_shortform_t *sf;
428 xfs_attr_sf_entry_t *sfe;
438 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
439 size = INT_GET(sf->hdr.totsize, ARCH_CONVERT);
440 tmpbuffer = kmem_alloc(size, KM_SLEEP);
441 ASSERT(tmpbuffer != NULL);
442 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
443 sf = (xfs_attr_shortform_t *)tmpbuffer;
445 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
447 error = xfs_da_grow_inode(args, &blkno);
450 * If we hit an IO error middle of the transaction inside
451 * grow_inode(), we may have inconsistent data. Bail out.
455 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
456 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
461 error = xfs_attr_leaf_create(args, blkno, &bp);
463 error = xfs_da_shrink_inode(args, 0, bp);
467 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
468 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
472 memset((char *)&nargs, 0, sizeof(nargs));
474 nargs.firstblock = args->firstblock;
475 nargs.flist = args->flist;
476 nargs.total = args->total;
477 nargs.whichfork = XFS_ATTR_FORK;
478 nargs.trans = args->trans;
482 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
483 nargs.name = (char *)sfe->nameval;
484 nargs.namelen = sfe->namelen;
485 nargs.value = (char *)&sfe->nameval[nargs.namelen];
486 nargs.valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
487 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
489 nargs.flags = (sfe->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
490 ((sfe->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
491 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
492 ASSERT(error == ENOATTR);
493 error = xfs_attr_leaf_add(bp, &nargs);
494 ASSERT(error != ENOSPC);
497 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
504 kmem_free(tmpbuffer, size);
509 xfs_attr_shortform_compare(const void *a, const void *b)
511 xfs_attr_sf_sort_t *sa, *sb;
513 sa = (xfs_attr_sf_sort_t *)a;
514 sb = (xfs_attr_sf_sort_t *)b;
515 if (INT_GET(sa->hash, ARCH_CONVERT)
516 < INT_GET(sb->hash, ARCH_CONVERT)) {
518 } else if (INT_GET(sa->hash, ARCH_CONVERT)
519 > INT_GET(sb->hash, ARCH_CONVERT)) {
522 return(sa->entno - sb->entno);
527 * Copy out entries of shortform attribute lists for attr_list().
528 * Shortform atrtribute lists are not stored in hashval sorted order.
529 * If the output buffer is not large enough to hold them all, then we
530 * we have to calculate each entries' hashvalue and sort them before
531 * we can begin returning them to the user.
535 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
537 attrlist_cursor_kern_t *cursor;
538 xfs_attr_sf_sort_t *sbuf, *sbp;
539 xfs_attr_shortform_t *sf;
540 xfs_attr_sf_entry_t *sfe;
542 int sbsize, nsbuf, count, i;
544 ASSERT(context != NULL);
547 ASSERT(dp->i_afp != NULL);
548 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
552 cursor = context->cursor;
553 ASSERT(cursor != NULL);
555 xfs_attr_trace_l_c("sf start", context);
558 * If the buffer is large enough, do not bother with sorting.
559 * Note the generous fudge factor of 16 overhead bytes per entry.
561 if ((dp->i_afp->if_bytes + INT_GET(sf->hdr.count, ARCH_CONVERT) * 16)
562 < context->bufsize) {
563 for (i = 0, sfe = &sf->list[0];
564 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
567 if (((context->flags & ATTR_SECURE) != 0) !=
568 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
569 !(context->flags & ATTR_KERNORMALS)) {
570 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
573 if (((context->flags & ATTR_ROOT) != 0) !=
574 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
575 !(context->flags & ATTR_KERNROOTLS)) {
576 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
579 namesp = (sfe->flags & XFS_ATTR_SECURE) ? &attr_secure:
580 ((sfe->flags & XFS_ATTR_ROOT) ? &attr_trusted :
582 if (context->flags & ATTR_KERNOVAL) {
583 ASSERT(context->flags & ATTR_KERNAMELS);
584 context->count += namesp->attr_namelen +
585 INT_GET(sfe->namelen, ARCH_CONVERT) + 1;
588 if (xfs_attr_put_listent(context, namesp,
589 (char *)sfe->nameval,
591 (int)INT_GET(sfe->valuelen,
595 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
597 xfs_attr_trace_l_c("sf big-gulp", context);
602 * It didn't all fit, so we have to sort everything on hashval.
604 sbsize = INT_GET(sf->hdr.count, ARCH_CONVERT) * sizeof(*sbuf);
605 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
608 * Scan the attribute list for the rest of the entries, storing
609 * the relevant info from only those that match into a buffer.
612 for (i = 0, sfe = &sf->list[0];
613 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
615 ((char *)sfe < (char *)sf) ||
616 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
617 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
619 context->dp->i_mount, sfe);
620 xfs_attr_trace_l_c("sf corrupted", context);
621 kmem_free(sbuf, sbsize);
622 return XFS_ERROR(EFSCORRUPTED);
624 if (((context->flags & ATTR_SECURE) != 0) !=
625 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
626 !(context->flags & ATTR_KERNORMALS)) {
627 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
630 if (((context->flags & ATTR_ROOT) != 0) !=
631 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
632 !(context->flags & ATTR_KERNROOTLS)) {
633 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
637 INT_SET(sbp->hash, ARCH_CONVERT,
638 xfs_da_hashname((char *)sfe->nameval, sfe->namelen));
639 sbp->name = (char *)sfe->nameval;
640 sbp->namelen = sfe->namelen;
641 /* These are bytes, and both on-disk, don't endian-flip */
642 sbp->valuelen = sfe->valuelen;
643 sbp->flags = sfe->flags;
644 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
650 * Sort the entries on hash then entno.
652 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
655 * Re-find our place IN THE SORTED LIST.
660 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
661 if (INT_GET(sbp->hash, ARCH_CONVERT) == cursor->hashval) {
662 if (cursor->offset == count) {
666 } else if (INT_GET(sbp->hash, ARCH_CONVERT) > cursor->hashval) {
671 kmem_free(sbuf, sbsize);
672 xfs_attr_trace_l_c("blk end", context);
677 * Loop putting entries into the user buffer.
679 for ( ; i < nsbuf; i++, sbp++) {
682 namesp = (sbp->flags & XFS_ATTR_SECURE) ? &attr_secure :
683 ((sbp->flags & XFS_ATTR_ROOT) ? &attr_trusted :
686 if (cursor->hashval != INT_GET(sbp->hash, ARCH_CONVERT)) {
687 cursor->hashval = INT_GET(sbp->hash, ARCH_CONVERT);
690 if (context->flags & ATTR_KERNOVAL) {
691 ASSERT(context->flags & ATTR_KERNAMELS);
692 context->count += namesp->attr_namelen +
695 if (xfs_attr_put_listent(context, namesp,
696 sbp->name, sbp->namelen,
697 INT_GET(sbp->valuelen, ARCH_CONVERT)))
703 kmem_free(sbuf, sbsize);
704 xfs_attr_trace_l_c("sf E-O-F", context);
709 * Check a leaf attribute block to see if all the entries would fit into
710 * a shortform attribute list.
713 xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
715 xfs_attr_leafblock_t *leaf;
716 xfs_attr_leaf_entry_t *entry;
717 xfs_attr_leaf_name_local_t *name_loc;
721 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
722 == XFS_ATTR_LEAF_MAGIC);
724 entry = &leaf->entries[0];
725 bytes = sizeof(struct xfs_attr_sf_hdr);
726 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
727 if (entry->flags & XFS_ATTR_INCOMPLETE)
728 continue; /* don't copy partial entries */
729 if (!(entry->flags & XFS_ATTR_LOCAL))
731 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
732 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
734 if (INT_GET(name_loc->valuelen, ARCH_CONVERT) >= XFS_ATTR_SF_ENTSIZE_MAX)
736 bytes += sizeof(struct xfs_attr_sf_entry)-1
738 + INT_GET(name_loc->valuelen, ARCH_CONVERT);
740 if (bytes == sizeof(struct xfs_attr_sf_hdr))
742 return(xfs_attr_shortform_bytesfit(dp, bytes));
746 * Convert a leaf attribute list to shortform attribute list
749 xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
751 xfs_attr_leafblock_t *leaf;
752 xfs_attr_leaf_entry_t *entry;
753 xfs_attr_leaf_name_local_t *name_loc;
760 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
761 ASSERT(tmpbuffer != NULL);
764 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
765 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
766 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
767 == XFS_ATTR_LEAF_MAGIC);
768 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
771 * Clean out the prior contents of the attribute list.
773 error = xfs_da_shrink_inode(args, 0, bp);
779 * Last attribute was removed, revert to original
780 * inode format making all literal area available
781 * to the data fork once more.
783 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
784 dp->i_d.di_forkoff = 0;
785 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
786 ASSERT(dp->i_d.di_anextents == 0);
787 ASSERT(dp->i_afp == NULL);
788 dp->i_df.if_ext_max =
789 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
790 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
794 xfs_attr_shortform_create(args);
797 * Copy the attributes
799 memset((char *)&nargs, 0, sizeof(nargs));
801 nargs.firstblock = args->firstblock;
802 nargs.flist = args->flist;
803 nargs.total = args->total;
804 nargs.whichfork = XFS_ATTR_FORK;
805 nargs.trans = args->trans;
807 entry = &leaf->entries[0];
808 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
809 if (entry->flags & XFS_ATTR_INCOMPLETE)
810 continue; /* don't copy partial entries */
813 ASSERT(entry->flags & XFS_ATTR_LOCAL);
814 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
815 nargs.name = (char *)name_loc->nameval;
816 nargs.namelen = name_loc->namelen;
817 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
818 nargs.valuelen = INT_GET(name_loc->valuelen, ARCH_CONVERT);
819 nargs.hashval = INT_GET(entry->hashval, ARCH_CONVERT);
820 nargs.flags = (entry->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
821 ((entry->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
822 xfs_attr_shortform_add(&nargs, forkoff);
827 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
832 * Convert from using a single leaf to a root node and a leaf.
835 xfs_attr_leaf_to_node(xfs_da_args_t *args)
837 xfs_attr_leafblock_t *leaf;
838 xfs_da_intnode_t *node;
840 xfs_dabuf_t *bp1, *bp2;
846 error = xfs_da_grow_inode(args, &blkno);
849 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
855 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
860 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
861 xfs_da_buf_done(bp1);
863 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
866 * Set up the new root node.
868 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
873 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
874 == XFS_ATTR_LEAF_MAGIC);
875 /* both on-disk, don't endian-flip twice */
876 node->btree[0].hashval =
877 leaf->entries[INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval;
878 INT_SET(node->btree[0].before, ARCH_CONVERT, blkno);
879 INT_SET(node->hdr.count, ARCH_CONVERT, 1);
880 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
884 xfs_da_buf_done(bp1);
886 xfs_da_buf_done(bp2);
891 /*========================================================================
892 * Routines used for growing the Btree.
893 *========================================================================*/
896 * Create the initial contents of a leaf attribute list
897 * or a leaf in a node attribute list.
900 xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
902 xfs_attr_leafblock_t *leaf;
903 xfs_attr_leaf_hdr_t *hdr;
910 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
916 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
918 INT_SET(hdr->info.magic, ARCH_CONVERT, XFS_ATTR_LEAF_MAGIC);
919 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
920 if (!hdr->firstused) {
921 INT_SET(hdr->firstused, ARCH_CONVERT,
922 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
925 INT_SET(hdr->freemap[0].base, ARCH_CONVERT,
926 sizeof(xfs_attr_leaf_hdr_t));
927 INT_SET(hdr->freemap[0].size, ARCH_CONVERT,
928 INT_GET(hdr->firstused, ARCH_CONVERT)
929 - INT_GET(hdr->freemap[0].base,
932 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
939 * Split the leaf node, rebalance, then add the new entry.
942 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
943 xfs_da_state_blk_t *newblk)
949 * Allocate space for a new leaf node.
951 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
952 error = xfs_da_grow_inode(state->args, &blkno);
955 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
958 newblk->blkno = blkno;
959 newblk->magic = XFS_ATTR_LEAF_MAGIC;
962 * Rebalance the entries across the two leaves.
963 * NOTE: rebalance() currently depends on the 2nd block being empty.
965 xfs_attr_leaf_rebalance(state, oldblk, newblk);
966 error = xfs_da_blk_link(state, oldblk, newblk);
971 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
972 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
973 * "new" attrs info. Will need the "old" info to remove it later.
975 * Insert the "new" entry in the correct block.
978 error = xfs_attr_leaf_add(oldblk->bp, state->args);
980 error = xfs_attr_leaf_add(newblk->bp, state->args);
983 * Update last hashval in each block since we added the name.
985 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
986 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
991 * Add a name to the leaf attribute list structure.
994 xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
996 xfs_attr_leafblock_t *leaf;
997 xfs_attr_leaf_hdr_t *hdr;
998 xfs_attr_leaf_map_t *map;
999 int tablesize, entsize, sum, tmp, i;
1002 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1003 == XFS_ATTR_LEAF_MAGIC);
1004 ASSERT((args->index >= 0)
1005 && (args->index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
1007 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1008 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1011 * Search through freemap for first-fit on new name length.
1012 * (may need to figure in size of entry struct too)
1014 tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1)
1015 * sizeof(xfs_attr_leaf_entry_t)
1016 + sizeof(xfs_attr_leaf_hdr_t);
1017 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1018 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1019 if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
1020 sum += INT_GET(map->size, ARCH_CONVERT);
1024 continue; /* no space in this map */
1026 if (INT_GET(map->base, ARCH_CONVERT)
1027 < INT_GET(hdr->firstused, ARCH_CONVERT))
1028 tmp += sizeof(xfs_attr_leaf_entry_t);
1029 if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
1030 tmp = xfs_attr_leaf_add_work(bp, args, i);
1033 sum += INT_GET(map->size, ARCH_CONVERT);
1037 * If there are no holes in the address space of the block,
1038 * and we don't have enough freespace, then compaction will do us
1039 * no good and we should just give up.
1041 if (!hdr->holes && (sum < entsize))
1042 return(XFS_ERROR(ENOSPC));
1045 * Compact the entries to coalesce free space.
1046 * This may change the hdr->count via dropping INCOMPLETE entries.
1048 xfs_attr_leaf_compact(args->trans, bp);
1051 * After compaction, the block is guaranteed to have only one
1052 * free region, in freemap[0]. If it is not big enough, give up.
1054 if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT)
1055 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1056 return(XFS_ERROR(ENOSPC));
1058 return(xfs_attr_leaf_add_work(bp, args, 0));
1062 * Add a name to a leaf attribute list structure.
1065 xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1067 xfs_attr_leafblock_t *leaf;
1068 xfs_attr_leaf_hdr_t *hdr;
1069 xfs_attr_leaf_entry_t *entry;
1070 xfs_attr_leaf_name_local_t *name_loc;
1071 xfs_attr_leaf_name_remote_t *name_rmt;
1072 xfs_attr_leaf_map_t *map;
1077 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1078 == XFS_ATTR_LEAF_MAGIC);
1080 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1081 ASSERT((args->index >= 0)
1082 && (args->index <= INT_GET(hdr->count, ARCH_CONVERT)));
1085 * Force open some space in the entry array and fill it in.
1087 entry = &leaf->entries[args->index];
1088 if (args->index < INT_GET(hdr->count, ARCH_CONVERT)) {
1089 tmp = INT_GET(hdr->count, ARCH_CONVERT) - args->index;
1090 tmp *= sizeof(xfs_attr_leaf_entry_t);
1091 memmove((char *)(entry+1), (char *)entry, tmp);
1092 xfs_da_log_buf(args->trans, bp,
1093 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1095 INT_MOD(hdr->count, ARCH_CONVERT, 1);
1098 * Allocate space for the new string (at the end of the run).
1100 map = &hdr->freemap[mapindex];
1101 mp = args->trans->t_mountp;
1102 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1103 ASSERT((INT_GET(map->base, ARCH_CONVERT) & 0x3) == 0);
1104 ASSERT(INT_GET(map->size, ARCH_CONVERT) >=
1105 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1106 mp->m_sb.sb_blocksize, NULL));
1107 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1108 ASSERT((INT_GET(map->size, ARCH_CONVERT) & 0x3) == 0);
1109 INT_MOD(map->size, ARCH_CONVERT,
1110 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1111 mp->m_sb.sb_blocksize, &tmp));
1112 INT_SET(entry->nameidx, ARCH_CONVERT,
1113 INT_GET(map->base, ARCH_CONVERT)
1114 + INT_GET(map->size, ARCH_CONVERT));
1115 INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
1116 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1117 entry->flags |= (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
1118 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
1120 entry->flags |= XFS_ATTR_INCOMPLETE;
1121 if ((args->blkno2 == args->blkno) &&
1122 (args->index2 <= args->index)) {
1126 xfs_da_log_buf(args->trans, bp,
1127 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1128 ASSERT((args->index == 0) || (INT_GET(entry->hashval, ARCH_CONVERT)
1129 >= INT_GET((entry-1)->hashval,
1131 ASSERT((args->index == INT_GET(hdr->count, ARCH_CONVERT)-1) ||
1132 (INT_GET(entry->hashval, ARCH_CONVERT)
1133 <= (INT_GET((entry+1)->hashval, ARCH_CONVERT))));
1136 * Copy the attribute name and value into the new space.
1138 * For "remote" attribute values, simply note that we need to
1139 * allocate space for the "remote" value. We can't actually
1140 * allocate the extents in this transaction, and we can't decide
1141 * which blocks they should be as we might allocate more blocks
1142 * as part of this transaction (a split operation for example).
1144 if (entry->flags & XFS_ATTR_LOCAL) {
1145 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1146 name_loc->namelen = args->namelen;
1147 INT_SET(name_loc->valuelen, ARCH_CONVERT, args->valuelen);
1148 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1149 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1150 INT_GET(name_loc->valuelen, ARCH_CONVERT));
1152 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
1153 name_rmt->namelen = args->namelen;
1154 memcpy((char *)name_rmt->name, args->name, args->namelen);
1155 entry->flags |= XFS_ATTR_INCOMPLETE;
1157 name_rmt->valuelen = 0;
1158 name_rmt->valueblk = 0;
1160 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1162 xfs_da_log_buf(args->trans, bp,
1163 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1164 xfs_attr_leaf_entsize(leaf, args->index)));
1167 * Update the control info for this leaf node
1169 if (INT_GET(entry->nameidx, ARCH_CONVERT)
1170 < INT_GET(hdr->firstused, ARCH_CONVERT)) {
1171 /* both on-disk, don't endian-flip twice */
1172 hdr->firstused = entry->nameidx;
1174 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT)
1175 >= ((INT_GET(hdr->count, ARCH_CONVERT)
1176 * sizeof(*entry))+sizeof(*hdr)));
1177 tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1)
1178 * sizeof(xfs_attr_leaf_entry_t)
1179 + sizeof(xfs_attr_leaf_hdr_t);
1180 map = &hdr->freemap[0];
1181 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1182 if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
1183 INT_MOD(map->base, ARCH_CONVERT,
1184 sizeof(xfs_attr_leaf_entry_t));
1185 INT_MOD(map->size, ARCH_CONVERT,
1186 -sizeof(xfs_attr_leaf_entry_t));
1189 INT_MOD(hdr->usedbytes, ARCH_CONVERT,
1190 xfs_attr_leaf_entsize(leaf, args->index));
1191 xfs_da_log_buf(args->trans, bp,
1192 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1197 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1200 xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1202 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1203 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1207 mp = trans->t_mountp;
1208 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1209 ASSERT(tmpbuffer != NULL);
1210 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1211 memset(bp->data, 0, XFS_LBSIZE(mp));
1214 * Copy basic information
1216 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1218 hdr_s = &leaf_s->hdr;
1219 hdr_d = &leaf_d->hdr;
1220 hdr_d->info = hdr_s->info; /* struct copy */
1221 INT_SET(hdr_d->firstused, ARCH_CONVERT, XFS_LBSIZE(mp));
1222 /* handle truncation gracefully */
1223 if (!hdr_d->firstused) {
1224 INT_SET(hdr_d->firstused, ARCH_CONVERT,
1225 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1227 hdr_d->usedbytes = 0;
1230 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT,
1231 sizeof(xfs_attr_leaf_hdr_t));
1232 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT,
1233 INT_GET(hdr_d->firstused, ARCH_CONVERT)
1234 - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
1237 * Copy all entry's in the same (sorted) order,
1238 * but allocate name/value pairs packed and in sequence.
1240 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1241 (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
1243 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1245 kmem_free(tmpbuffer, XFS_LBSIZE(mp));
1249 * Redistribute the attribute list entries between two leaf nodes,
1250 * taking into account the size of the new entry.
1252 * NOTE: if new block is empty, then it will get the upper half of the
1253 * old block. At present, all (one) callers pass in an empty second block.
1255 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1256 * to match what it is doing in splitting the attribute leaf block. Those
1257 * values are used in "atomic rename" operations on attributes. Note that
1258 * the "new" and "old" values can end up in different blocks.
1261 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1262 xfs_da_state_blk_t *blk2)
1264 xfs_da_args_t *args;
1265 xfs_da_state_blk_t *tmp_blk;
1266 xfs_attr_leafblock_t *leaf1, *leaf2;
1267 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1268 int count, totallen, max, space, swap;
1271 * Set up environment.
1273 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1274 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1275 leaf1 = blk1->bp->data;
1276 leaf2 = blk2->bp->data;
1277 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
1278 == XFS_ATTR_LEAF_MAGIC);
1279 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
1280 == XFS_ATTR_LEAF_MAGIC);
1284 * Check ordering of blocks, reverse if it makes things simpler.
1286 * NOTE: Given that all (current) callers pass in an empty
1287 * second block, this code should never set "swap".
1290 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1294 leaf1 = blk1->bp->data;
1295 leaf2 = blk2->bp->data;
1302 * Examine entries until we reduce the absolute difference in
1303 * byte usage between the two blocks to a minimum. Then get
1304 * the direction to copy and the number of elements to move.
1306 * "inleaf" is true if the new entry should be inserted into blk1.
1307 * If "swap" is also true, then reverse the sense of "inleaf".
1309 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1312 state->inleaf = !state->inleaf;
1315 * Move any entries required from leaf to leaf:
1317 if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
1319 * Figure the total bytes to be added to the destination leaf.
1321 /* number entries being moved */
1322 count = INT_GET(hdr1->count, ARCH_CONVERT) - count;
1323 space = INT_GET(hdr1->usedbytes, ARCH_CONVERT) - totallen;
1324 space += count * sizeof(xfs_attr_leaf_entry_t);
1327 * leaf2 is the destination, compact it if it looks tight.
1329 max = INT_GET(hdr2->firstused, ARCH_CONVERT)
1330 - sizeof(xfs_attr_leaf_hdr_t);
1331 max -= INT_GET(hdr2->count, ARCH_CONVERT)
1332 * sizeof(xfs_attr_leaf_entry_t);
1334 xfs_attr_leaf_compact(args->trans, blk2->bp);
1338 * Move high entries from leaf1 to low end of leaf2.
1340 xfs_attr_leaf_moveents(leaf1,
1341 INT_GET(hdr1->count, ARCH_CONVERT)-count,
1342 leaf2, 0, count, state->mp);
1344 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1345 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1346 } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
1348 * I assert that since all callers pass in an empty
1349 * second buffer, this code should never execute.
1353 * Figure the total bytes to be added to the destination leaf.
1355 /* number entries being moved */
1356 count -= INT_GET(hdr1->count, ARCH_CONVERT);
1357 space = totallen - INT_GET(hdr1->usedbytes, ARCH_CONVERT);
1358 space += count * sizeof(xfs_attr_leaf_entry_t);
1361 * leaf1 is the destination, compact it if it looks tight.
1363 max = INT_GET(hdr1->firstused, ARCH_CONVERT)
1364 - sizeof(xfs_attr_leaf_hdr_t);
1365 max -= INT_GET(hdr1->count, ARCH_CONVERT)
1366 * sizeof(xfs_attr_leaf_entry_t);
1368 xfs_attr_leaf_compact(args->trans, blk1->bp);
1372 * Move low entries from leaf2 to high end of leaf1.
1374 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1375 (int)INT_GET(hdr1->count, ARCH_CONVERT), count,
1378 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1379 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1383 * Copy out last hashval in each block for B-tree code.
1386 INT_GET(leaf1->entries[INT_GET(leaf1->hdr.count,
1387 ARCH_CONVERT)-1].hashval, ARCH_CONVERT);
1389 INT_GET(leaf2->entries[INT_GET(leaf2->hdr.count,
1390 ARCH_CONVERT)-1].hashval, ARCH_CONVERT);
1393 * Adjust the expected index for insertion.
1394 * NOTE: this code depends on the (current) situation that the
1395 * second block was originally empty.
1397 * If the insertion point moved to the 2nd block, we must adjust
1398 * the index. We must also track the entry just following the
1399 * new entry for use in an "atomic rename" operation, that entry
1400 * is always the "old" entry and the "new" entry is what we are
1401 * inserting. The index/blkno fields refer to the "old" entry,
1402 * while the index2/blkno2 fields refer to the "new" entry.
1404 if (blk1->index > INT_GET(leaf1->hdr.count, ARCH_CONVERT)) {
1405 ASSERT(state->inleaf == 0);
1406 blk2->index = blk1->index
1407 - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1408 args->index = args->index2 = blk2->index;
1409 args->blkno = args->blkno2 = blk2->blkno;
1410 } else if (blk1->index == INT_GET(leaf1->hdr.count, ARCH_CONVERT)) {
1411 if (state->inleaf) {
1412 args->index = blk1->index;
1413 args->blkno = blk1->blkno;
1415 args->blkno2 = blk2->blkno;
1417 blk2->index = blk1->index
1418 - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1419 args->index = args->index2 = blk2->index;
1420 args->blkno = args->blkno2 = blk2->blkno;
1423 ASSERT(state->inleaf == 1);
1424 args->index = args->index2 = blk1->index;
1425 args->blkno = args->blkno2 = blk1->blkno;
1430 * Examine entries until we reduce the absolute difference in
1431 * byte usage between the two blocks to a minimum.
1432 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1433 * GROT: there will always be enough room in either block for a new entry.
1434 * GROT: Do a double-split for this case?
1437 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1438 xfs_da_state_blk_t *blk1,
1439 xfs_da_state_blk_t *blk2,
1440 int *countarg, int *usedbytesarg)
1442 xfs_attr_leafblock_t *leaf1, *leaf2;
1443 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1444 xfs_attr_leaf_entry_t *entry;
1445 int count, max, index, totallen, half;
1446 int lastdelta, foundit, tmp;
1449 * Set up environment.
1451 leaf1 = blk1->bp->data;
1452 leaf2 = blk2->bp->data;
1459 * Examine entries until we reduce the absolute difference in
1460 * byte usage between the two blocks to a minimum.
1462 max = INT_GET(hdr1->count, ARCH_CONVERT)
1463 + INT_GET(hdr2->count, ARCH_CONVERT);
1464 half = (max+1) * sizeof(*entry);
1465 half += INT_GET(hdr1->usedbytes, ARCH_CONVERT)
1466 + INT_GET(hdr2->usedbytes, ARCH_CONVERT)
1467 + xfs_attr_leaf_newentsize(
1468 state->args->namelen,
1469 state->args->valuelen,
1470 state->blocksize, NULL);
1472 lastdelta = state->blocksize;
1473 entry = &leaf1->entries[0];
1474 for (count = index = 0; count < max; entry++, index++, count++) {
1476 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1478 * The new entry is in the first block, account for it.
1480 if (count == blk1->index) {
1481 tmp = totallen + sizeof(*entry) +
1482 xfs_attr_leaf_newentsize(
1483 state->args->namelen,
1484 state->args->valuelen,
1485 state->blocksize, NULL);
1486 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1488 lastdelta = XFS_ATTR_ABS(half - tmp);
1494 * Wrap around into the second block if necessary.
1496 if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
1498 entry = &leaf1->entries[0];
1503 * Figure out if next leaf entry would be too much.
1505 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1507 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1509 lastdelta = XFS_ATTR_ABS(half - tmp);
1515 * Calculate the number of usedbytes that will end up in lower block.
1516 * If new entry not in lower block, fix up the count.
1518 totallen -= count * sizeof(*entry);
1520 totallen -= sizeof(*entry) +
1521 xfs_attr_leaf_newentsize(
1522 state->args->namelen,
1523 state->args->valuelen,
1524 state->blocksize, NULL);
1528 *usedbytesarg = totallen;
1532 /*========================================================================
1533 * Routines used for shrinking the Btree.
1534 *========================================================================*/
1537 * Check a leaf block and its neighbors to see if the block should be
1538 * collapsed into one or the other neighbor. Always keep the block
1539 * with the smaller block number.
1540 * If the current block is over 50% full, don't try to join it, return 0.
1541 * If the block is empty, fill in the state structure and return 2.
1542 * If it can be collapsed, fill in the state structure and return 1.
1543 * If nothing can be done, return 0.
1545 * GROT: allow for INCOMPLETE entries in calculation.
1548 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1550 xfs_attr_leafblock_t *leaf;
1551 xfs_da_state_blk_t *blk;
1552 xfs_da_blkinfo_t *info;
1553 int count, bytes, forward, error, retval, i;
1558 * Check for the degenerate case of the block being over 50% full.
1559 * If so, it's not worth even looking to see if we might be able
1560 * to coalesce with a sibling.
1562 blk = &state->path.blk[ state->path.active-1 ];
1563 info = blk->bp->data;
1564 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
1565 leaf = (xfs_attr_leafblock_t *)info;
1566 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1567 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1568 count * sizeof(xfs_attr_leaf_entry_t) +
1569 INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1570 if (bytes > (state->blocksize >> 1)) {
1571 *action = 0; /* blk over 50%, don't try to join */
1576 * Check for the degenerate case of the block being empty.
1577 * If the block is empty, we'll simply delete it, no need to
1578 * coalesce it with a sibling block. We choose (aribtrarily)
1579 * to merge with the forward block unless it is NULL.
1583 * Make altpath point to the block we want to keep and
1584 * path point to the block we want to drop (this one).
1586 forward = info->forw;
1587 memcpy(&state->altpath, &state->path, sizeof(state->path));
1588 error = xfs_da_path_shift(state, &state->altpath, forward,
1601 * Examine each sibling block to see if we can coalesce with
1602 * at least 25% free space to spare. We need to figure out
1603 * whether to merge with the forward or the backward block.
1604 * We prefer coalescing with the lower numbered sibling so as
1605 * to shrink an attribute list over time.
1607 /* start with smaller blk num */
1608 forward = (INT_GET(info->forw, ARCH_CONVERT)
1609 < INT_GET(info->back, ARCH_CONVERT));
1610 for (i = 0; i < 2; forward = !forward, i++) {
1612 blkno = INT_GET(info->forw, ARCH_CONVERT);
1614 blkno = INT_GET(info->back, ARCH_CONVERT);
1617 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1618 blkno, -1, &bp, XFS_ATTR_FORK);
1623 leaf = (xfs_attr_leafblock_t *)info;
1624 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1625 bytes = state->blocksize - (state->blocksize>>2);
1626 bytes -= INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1628 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1629 == XFS_ATTR_LEAF_MAGIC);
1630 count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
1631 bytes -= INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1632 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1633 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1634 xfs_da_brelse(state->args->trans, bp);
1636 break; /* fits with at least 25% to spare */
1644 * Make altpath point to the block we want to keep (the lower
1645 * numbered block) and path point to the block we want to drop.
1647 memcpy(&state->altpath, &state->path, sizeof(state->path));
1648 if (blkno < blk->blkno) {
1649 error = xfs_da_path_shift(state, &state->altpath, forward,
1652 error = xfs_da_path_shift(state, &state->path, forward,
1666 * Remove a name from the leaf attribute list structure.
1668 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1669 * If two leaves are 37% full, when combined they will leave 25% free.
1672 xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1674 xfs_attr_leafblock_t *leaf;
1675 xfs_attr_leaf_hdr_t *hdr;
1676 xfs_attr_leaf_map_t *map;
1677 xfs_attr_leaf_entry_t *entry;
1678 int before, after, smallest, entsize;
1679 int tablesize, tmp, i;
1683 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1684 == XFS_ATTR_LEAF_MAGIC);
1686 mp = args->trans->t_mountp;
1687 ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0)
1688 && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1689 ASSERT((args->index >= 0)
1690 && (args->index < INT_GET(hdr->count, ARCH_CONVERT)));
1691 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT)
1692 >= ((INT_GET(hdr->count, ARCH_CONVERT)
1693 * sizeof(*entry))+sizeof(*hdr)));
1694 entry = &leaf->entries[args->index];
1695 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1696 >= INT_GET(hdr->firstused, ARCH_CONVERT));
1697 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1700 * Scan through free region table:
1701 * check for adjacency of free'd entry with an existing one,
1702 * find smallest free region in case we need to replace it,
1703 * adjust any map that borders the entry table,
1705 tablesize = INT_GET(hdr->count, ARCH_CONVERT)
1706 * sizeof(xfs_attr_leaf_entry_t)
1707 + sizeof(xfs_attr_leaf_hdr_t);
1708 map = &hdr->freemap[0];
1709 tmp = INT_GET(map->size, ARCH_CONVERT);
1710 before = after = -1;
1711 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1712 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1713 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1714 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1715 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1716 if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
1717 INT_MOD(map->base, ARCH_CONVERT,
1718 -sizeof(xfs_attr_leaf_entry_t));
1719 INT_MOD(map->size, ARCH_CONVERT,
1720 sizeof(xfs_attr_leaf_entry_t));
1723 if ((INT_GET(map->base, ARCH_CONVERT)
1724 + INT_GET(map->size, ARCH_CONVERT))
1725 == INT_GET(entry->nameidx, ARCH_CONVERT)) {
1727 } else if (INT_GET(map->base, ARCH_CONVERT)
1728 == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
1730 } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
1731 tmp = INT_GET(map->size, ARCH_CONVERT);
1737 * Coalesce adjacent freemap regions,
1738 * or replace the smallest region.
1740 if ((before >= 0) || (after >= 0)) {
1741 if ((before >= 0) && (after >= 0)) {
1742 map = &hdr->freemap[before];
1743 INT_MOD(map->size, ARCH_CONVERT, entsize);
1744 INT_MOD(map->size, ARCH_CONVERT,
1745 INT_GET(hdr->freemap[after].size,
1747 hdr->freemap[after].base = 0;
1748 hdr->freemap[after].size = 0;
1749 } else if (before >= 0) {
1750 map = &hdr->freemap[before];
1751 INT_MOD(map->size, ARCH_CONVERT, entsize);
1753 map = &hdr->freemap[after];
1754 /* both on-disk, don't endian flip twice */
1755 map->base = entry->nameidx;
1756 INT_MOD(map->size, ARCH_CONVERT, entsize);
1760 * Replace smallest region (if it is smaller than free'd entry)
1762 map = &hdr->freemap[smallest];
1763 if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
1764 INT_SET(map->base, ARCH_CONVERT,
1765 INT_GET(entry->nameidx, ARCH_CONVERT));
1766 INT_SET(map->size, ARCH_CONVERT, entsize);
1771 * Did we remove the first entry?
1773 if (INT_GET(entry->nameidx, ARCH_CONVERT)
1774 == INT_GET(hdr->firstused, ARCH_CONVERT))
1780 * Compress the remaining entries and zero out the removed stuff.
1782 memset(XFS_ATTR_LEAF_NAME(leaf, args->index), 0, entsize);
1783 INT_MOD(hdr->usedbytes, ARCH_CONVERT, -entsize);
1784 xfs_da_log_buf(args->trans, bp,
1785 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1788 tmp = (INT_GET(hdr->count, ARCH_CONVERT) - args->index)
1789 * sizeof(xfs_attr_leaf_entry_t);
1790 memmove((char *)entry, (char *)(entry+1), tmp);
1791 INT_MOD(hdr->count, ARCH_CONVERT, -1);
1792 xfs_da_log_buf(args->trans, bp,
1793 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1794 entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
1795 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1798 * If we removed the first entry, re-find the first used byte
1799 * in the name area. Note that if the entry was the "firstused",
1800 * then we don't have a "hole" in our block resulting from
1801 * removing the name.
1804 tmp = XFS_LBSIZE(mp);
1805 entry = &leaf->entries[0];
1806 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1;
1807 i >= 0; entry++, i--) {
1808 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1809 >= INT_GET(hdr->firstused, ARCH_CONVERT));
1810 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1812 if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
1813 tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
1815 INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
1816 if (!hdr->firstused) {
1817 INT_SET(hdr->firstused, ARCH_CONVERT,
1818 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1821 hdr->holes = 1; /* mark as needing compaction */
1823 xfs_da_log_buf(args->trans, bp,
1824 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1827 * Check if leaf is less than 50% full, caller may want to
1828 * "join" the leaf with a sibling if so.
1830 tmp = sizeof(xfs_attr_leaf_hdr_t);
1831 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT)
1832 * sizeof(xfs_attr_leaf_entry_t);
1833 tmp += INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1834 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1838 * Move all the attribute list entries from drop_leaf into save_leaf.
1841 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1842 xfs_da_state_blk_t *save_blk)
1844 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1845 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1850 * Set up environment.
1853 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1854 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1855 drop_leaf = drop_blk->bp->data;
1856 save_leaf = save_blk->bp->data;
1857 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT)
1858 == XFS_ATTR_LEAF_MAGIC);
1859 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT)
1860 == XFS_ATTR_LEAF_MAGIC);
1861 drop_hdr = &drop_leaf->hdr;
1862 save_hdr = &save_leaf->hdr;
1865 * Save last hashval from dying block for later Btree fixup.
1868 INT_GET(drop_leaf->entries[INT_GET(drop_leaf->hdr.count,
1869 ARCH_CONVERT)-1].hashval,
1873 * Check if we need a temp buffer, or can we do it in place.
1874 * Note that we don't check "leaf" for holes because we will
1875 * always be dropping it, toosmall() decided that for us already.
1877 if (save_hdr->holes == 0) {
1879 * dest leaf has no holes, so we add there. May need
1880 * to make some room in the entry array.
1882 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1883 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1884 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1886 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1887 INT_GET(save_hdr->count, ARCH_CONVERT),
1888 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1893 * Destination has holes, so we make a temporary copy
1894 * of the leaf and add them both to that.
1896 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1897 ASSERT(tmpbuffer != NULL);
1898 memset(tmpbuffer, 0, state->blocksize);
1899 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1900 tmp_hdr = &tmp_leaf->hdr;
1901 tmp_hdr->info = save_hdr->info; /* struct copy */
1903 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
1904 if (!tmp_hdr->firstused) {
1905 INT_SET(tmp_hdr->firstused, ARCH_CONVERT,
1906 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1908 tmp_hdr->usedbytes = 0;
1909 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1910 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1911 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1913 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1914 INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1915 (int)INT_GET(save_hdr->count, ARCH_CONVERT),
1918 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1919 (int)INT_GET(save_hdr->count, ARCH_CONVERT),
1921 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1922 INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1923 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1926 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1927 kmem_free(tmpbuffer, state->blocksize);
1930 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1931 state->blocksize - 1);
1934 * Copy out last hashval in each block for B-tree code.
1937 INT_GET(save_leaf->entries[INT_GET(save_leaf->hdr.count,
1938 ARCH_CONVERT)-1].hashval,
1942 /*========================================================================
1943 * Routines used for finding things in the Btree.
1944 *========================================================================*/
1947 * Look up a name in a leaf attribute list structure.
1948 * This is the internal routine, it uses the caller's buffer.
1950 * Note that duplicate keys are allowed, but only check within the
1951 * current leaf node. The Btree code must check in adjacent leaf nodes.
1953 * Return in args->index the index into the entry[] array of either
1954 * the found entry, or where the entry should have been (insert before
1957 * Don't change the args->value unless we find the attribute.
1960 xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1962 xfs_attr_leafblock_t *leaf;
1963 xfs_attr_leaf_entry_t *entry;
1964 xfs_attr_leaf_name_local_t *name_loc;
1965 xfs_attr_leaf_name_remote_t *name_rmt;
1967 xfs_dahash_t hashval;
1970 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1971 == XFS_ATTR_LEAF_MAGIC);
1972 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT)
1973 < (XFS_LBSIZE(args->dp->i_mount)/8));
1976 * Binary search. (note: small blocks will skip this loop)
1978 hashval = args->hashval;
1979 probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
1980 for (entry = &leaf->entries[probe]; span > 4;
1981 entry = &leaf->entries[probe]) {
1983 if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
1985 else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
1990 ASSERT((probe >= 0) &&
1992 || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
1993 ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT)
1997 * Since we may have duplicate hashval's, find the first matching
1998 * hashval in the leaf.
2000 while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT)
2005 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))
2006 && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
2010 if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT))
2011 || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
2012 args->index = probe;
2013 return(XFS_ERROR(ENOATTR));
2017 * Duplicate keys may be present, so search all of them for a match.
2019 for ( ; (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))
2020 && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval);
2023 * GROT: Add code to remove incomplete entries.
2026 * If we are looking for INCOMPLETE entries, show only those.
2027 * If we are looking for complete entries, show only those.
2029 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2030 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2033 if (entry->flags & XFS_ATTR_LOCAL) {
2034 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, probe);
2035 if (name_loc->namelen != args->namelen)
2037 if (memcmp(args->name, (char *)name_loc->nameval,
2038 args->namelen) != 0)
2040 if (((args->flags & ATTR_SECURE) != 0) !=
2041 ((entry->flags & XFS_ATTR_SECURE) != 0))
2043 if (((args->flags & ATTR_ROOT) != 0) !=
2044 ((entry->flags & XFS_ATTR_ROOT) != 0))
2046 args->index = probe;
2047 return(XFS_ERROR(EEXIST));
2049 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, probe);
2050 if (name_rmt->namelen != args->namelen)
2052 if (memcmp(args->name, (char *)name_rmt->name,
2053 args->namelen) != 0)
2055 if (((args->flags & ATTR_SECURE) != 0) !=
2056 ((entry->flags & XFS_ATTR_SECURE) != 0))
2058 if (((args->flags & ATTR_ROOT) != 0) !=
2059 ((entry->flags & XFS_ATTR_ROOT) != 0))
2061 args->index = probe;
2063 = INT_GET(name_rmt->valueblk, ARCH_CONVERT);
2064 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2065 INT_GET(name_rmt->valuelen,
2067 return(XFS_ERROR(EEXIST));
2070 args->index = probe;
2071 return(XFS_ERROR(ENOATTR));
2075 * Get the value associated with an attribute name from a leaf attribute
2079 xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2082 xfs_attr_leafblock_t *leaf;
2083 xfs_attr_leaf_entry_t *entry;
2084 xfs_attr_leaf_name_local_t *name_loc;
2085 xfs_attr_leaf_name_remote_t *name_rmt;
2088 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2089 == XFS_ATTR_LEAF_MAGIC);
2090 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT)
2091 < (XFS_LBSIZE(args->dp->i_mount)/8));
2092 ASSERT(args->index < ((int)INT_GET(leaf->hdr.count, ARCH_CONVERT)));
2094 entry = &leaf->entries[args->index];
2095 if (entry->flags & XFS_ATTR_LOCAL) {
2096 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2097 ASSERT(name_loc->namelen == args->namelen);
2098 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2099 valuelen = INT_GET(name_loc->valuelen, ARCH_CONVERT);
2100 if (args->flags & ATTR_KERNOVAL) {
2101 args->valuelen = valuelen;
2104 if (args->valuelen < valuelen) {
2105 args->valuelen = valuelen;
2106 return(XFS_ERROR(ERANGE));
2108 args->valuelen = valuelen;
2109 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2111 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2112 ASSERT(name_rmt->namelen == args->namelen);
2113 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2114 valuelen = INT_GET(name_rmt->valuelen, ARCH_CONVERT);
2115 args->rmtblkno = INT_GET(name_rmt->valueblk, ARCH_CONVERT);
2116 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2117 if (args->flags & ATTR_KERNOVAL) {
2118 args->valuelen = valuelen;
2121 if (args->valuelen < valuelen) {
2122 args->valuelen = valuelen;
2123 return(XFS_ERROR(ERANGE));
2125 args->valuelen = valuelen;
2130 /*========================================================================
2132 *========================================================================*/
2135 * Move the indicated entries from one leaf to another.
2136 * NOTE: this routine modifies both source and destination leaves.
2140 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2141 xfs_attr_leafblock_t *leaf_d, int start_d,
2142 int count, xfs_mount_t *mp)
2144 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2145 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2149 * Check for nothing to do.
2155 * Set up environment.
2157 ASSERT(INT_GET(leaf_s->hdr.info.magic, ARCH_CONVERT)
2158 == XFS_ATTR_LEAF_MAGIC);
2159 ASSERT(INT_GET(leaf_d->hdr.info.magic, ARCH_CONVERT)
2160 == XFS_ATTR_LEAF_MAGIC);
2161 hdr_s = &leaf_s->hdr;
2162 hdr_d = &leaf_d->hdr;
2163 ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0)
2164 && (INT_GET(hdr_s->count, ARCH_CONVERT)
2165 < (XFS_LBSIZE(mp)/8)));
2166 ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
2167 ((INT_GET(hdr_s->count, ARCH_CONVERT)
2168 * sizeof(*entry_s))+sizeof(*hdr_s)));
2169 ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
2170 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
2171 ((INT_GET(hdr_d->count, ARCH_CONVERT)
2172 * sizeof(*entry_d))+sizeof(*hdr_d)));
2174 ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
2175 ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
2176 ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
2179 * Move the entries in the destination leaf up to make a hole?
2181 if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
2182 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
2183 tmp *= sizeof(xfs_attr_leaf_entry_t);
2184 entry_s = &leaf_d->entries[start_d];
2185 entry_d = &leaf_d->entries[start_d + count];
2186 memmove((char *)entry_d, (char *)entry_s, tmp);
2190 * Copy all entry's in the same (sorted) order,
2191 * but allocate attribute info packed and in sequence.
2193 entry_s = &leaf_s->entries[start_s];
2194 entry_d = &leaf_d->entries[start_d];
2196 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2197 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT)
2198 >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
2199 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2202 * Code to drop INCOMPLETE entries. Difficult to use as we
2203 * may also need to change the insertion index. Code turned
2204 * off for 6.2, should be revisited later.
2206 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2207 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2208 INT_MOD(hdr_s->usedbytes, ARCH_CONVERT, -tmp);
2209 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
2210 entry_d--; /* to compensate for ++ in loop hdr */
2212 if ((start_s + i) < offset)
2213 result++; /* insertion index adjustment */
2216 INT_MOD(hdr_d->firstused, ARCH_CONVERT, -tmp);
2217 /* both on-disk, don't endian flip twice */
2218 entry_d->hashval = entry_s->hashval;
2219 /* both on-disk, don't endian flip twice */
2220 entry_d->nameidx = hdr_d->firstused;
2221 entry_d->flags = entry_s->flags;
2222 ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp
2224 memmove(XFS_ATTR_LEAF_NAME(leaf_d, desti),
2225 XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), tmp);
2226 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp
2228 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2229 INT_MOD(hdr_s->usedbytes, ARCH_CONVERT, -tmp);
2230 INT_MOD(hdr_d->usedbytes, ARCH_CONVERT, tmp);
2231 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
2232 INT_MOD(hdr_d->count, ARCH_CONVERT, 1);
2233 tmp = INT_GET(hdr_d->count, ARCH_CONVERT)
2234 * sizeof(xfs_attr_leaf_entry_t)
2235 + sizeof(xfs_attr_leaf_hdr_t);
2236 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
2243 * Zero out the entries we just copied.
2245 if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
2246 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2247 entry_s = &leaf_s->entries[start_s];
2248 ASSERT(((char *)entry_s + tmp) <=
2249 ((char *)leaf_s + XFS_LBSIZE(mp)));
2250 memset((char *)entry_s, 0, tmp);
2253 * Move the remaining entries down to fill the hole,
2254 * then zero the entries at the top.
2256 tmp = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
2257 tmp *= sizeof(xfs_attr_leaf_entry_t);
2258 entry_s = &leaf_s->entries[start_s + count];
2259 entry_d = &leaf_s->entries[start_s];
2260 memmove((char *)entry_d, (char *)entry_s, tmp);
2262 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2263 entry_s = &leaf_s->entries[INT_GET(hdr_s->count,
2265 ASSERT(((char *)entry_s + tmp) <=
2266 ((char *)leaf_s + XFS_LBSIZE(mp)));
2267 memset((char *)entry_s, 0, tmp);
2271 * Fill in the freemap information
2273 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT,
2274 sizeof(xfs_attr_leaf_hdr_t));
2275 INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT,
2276 INT_GET(hdr_d->count, ARCH_CONVERT)
2277 * sizeof(xfs_attr_leaf_entry_t));
2278 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT,
2279 INT_GET(hdr_d->firstused, ARCH_CONVERT)
2280 - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
2281 hdr_d->freemap[1].base = 0;
2282 hdr_d->freemap[2].base = 0;
2283 hdr_d->freemap[1].size = 0;
2284 hdr_d->freemap[2].size = 0;
2285 hdr_s->holes = 1; /* leaf may not be compact */
2289 * Compare two leaf blocks "order".
2290 * Return 0 unless leaf2 should go before leaf1.
2293 xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2295 xfs_attr_leafblock_t *leaf1, *leaf2;
2297 leaf1 = leaf1_bp->data;
2298 leaf2 = leaf2_bp->data;
2299 ASSERT((INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
2300 == XFS_ATTR_LEAF_MAGIC) &&
2301 (INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2302 == XFS_ATTR_LEAF_MAGIC));
2303 if ( (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0)
2304 && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0)
2305 && ( (INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
2306 INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT))
2307 || (INT_GET(leaf2->entries[INT_GET(leaf2->hdr.count,
2308 ARCH_CONVERT)-1].hashval, ARCH_CONVERT) <
2309 INT_GET(leaf1->entries[INT_GET(leaf1->hdr.count,
2310 ARCH_CONVERT)-1].hashval, ARCH_CONVERT))) ) {
2317 * Pick up the last hashvalue from a leaf block.
2320 xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2322 xfs_attr_leafblock_t *leaf;
2325 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2326 == XFS_ATTR_LEAF_MAGIC);
2328 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
2329 if (!leaf->hdr.count)
2331 return(INT_GET(leaf->entries[INT_GET(leaf->hdr.count,
2332 ARCH_CONVERT)-1].hashval, ARCH_CONVERT));
2336 * Calculate the number of bytes used to store the indicated attribute
2337 * (whether local or remote only calculate bytes in this block).
2340 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2342 xfs_attr_leaf_name_local_t *name_loc;
2343 xfs_attr_leaf_name_remote_t *name_rmt;
2346 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2347 == XFS_ATTR_LEAF_MAGIC);
2348 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2349 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, index);
2350 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc->namelen,
2351 INT_GET(name_loc->valuelen,
2354 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, index);
2355 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt->namelen);
2361 * Calculate the number of bytes that would be required to store the new
2362 * attribute (whether local or remote only calculate bytes in this block).
2363 * This routine decides as a side effect whether the attribute will be
2364 * a "local" or a "remote" attribute.
2367 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2371 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen, valuelen);
2372 if (size < XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize)) {
2377 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen);
2386 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2389 xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2391 attrlist_cursor_kern_t *cursor;
2392 xfs_attr_leafblock_t *leaf;
2393 xfs_attr_leaf_entry_t *entry;
2394 xfs_attr_leaf_name_local_t *name_loc;
2395 xfs_attr_leaf_name_remote_t *name_rmt;
2400 cursor = context->cursor;
2401 cursor->initted = 1;
2403 xfs_attr_trace_l_cl("blk start", context, leaf);
2406 * Re-find our place in the leaf block if this is a new syscall.
2408 if (context->resynch) {
2409 entry = &leaf->entries[0];
2410 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
2412 if (INT_GET(entry->hashval, ARCH_CONVERT)
2413 == cursor->hashval) {
2414 if (cursor->offset == context->dupcnt) {
2415 context->dupcnt = 0;
2419 } else if (INT_GET(entry->hashval, ARCH_CONVERT)
2420 > cursor->hashval) {
2421 context->dupcnt = 0;
2425 if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
2426 xfs_attr_trace_l_c("not found", context);
2430 entry = &leaf->entries[0];
2433 context->resynch = 0;
2436 * We have found our place, start copying out the new attributes.
2439 for ( ; (i < INT_GET(leaf->hdr.count, ARCH_CONVERT))
2440 && (retval == 0); entry++, i++) {
2441 attrnames_t *namesp;
2443 if (INT_GET(entry->hashval, ARCH_CONVERT) != cursor->hashval) {
2444 cursor->hashval = INT_GET(entry->hashval, ARCH_CONVERT);
2448 if (entry->flags & XFS_ATTR_INCOMPLETE)
2449 continue; /* skip incomplete entries */
2450 if (((context->flags & ATTR_SECURE) != 0) !=
2451 ((entry->flags & XFS_ATTR_SECURE) != 0) &&
2452 !(context->flags & ATTR_KERNORMALS))
2453 continue; /* skip non-matching entries */
2454 if (((context->flags & ATTR_ROOT) != 0) !=
2455 ((entry->flags & XFS_ATTR_ROOT) != 0) &&
2456 !(context->flags & ATTR_KERNROOTLS))
2457 continue; /* skip non-matching entries */
2459 namesp = (entry->flags & XFS_ATTR_SECURE) ? &attr_secure :
2460 ((entry->flags & XFS_ATTR_ROOT) ? &attr_trusted :
2463 if (entry->flags & XFS_ATTR_LOCAL) {
2464 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
2465 if (context->flags & ATTR_KERNOVAL) {
2466 ASSERT(context->flags & ATTR_KERNAMELS);
2467 context->count += namesp->attr_namelen +
2468 (int)name_loc->namelen + 1;
2470 retval = xfs_attr_put_listent(context, namesp,
2471 (char *)name_loc->nameval,
2472 (int)name_loc->namelen,
2473 (int)INT_GET(name_loc->valuelen,
2477 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2478 if (context->flags & ATTR_KERNOVAL) {
2479 ASSERT(context->flags & ATTR_KERNAMELS);
2480 context->count += namesp->attr_namelen +
2481 (int)name_rmt->namelen + 1;
2483 retval = xfs_attr_put_listent(context, namesp,
2484 (char *)name_rmt->name,
2485 (int)name_rmt->namelen,
2486 (int)INT_GET(name_rmt->valuelen,
2494 xfs_attr_trace_l_cl("blk end", context, leaf);
2498 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
2499 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
2500 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
2501 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
2502 & ~(sizeof(u_int32_t)-1))
2505 * Format an attribute and copy it out to the user's buffer.
2506 * Take care to check values and protect against them changing later,
2507 * we may be reading them directly out of a user buffer.
2511 xfs_attr_put_listent(xfs_attr_list_context_t *context,
2512 attrnames_t *namesp, char *name, int namelen, int valuelen)
2514 attrlist_ent_t *aep;
2517 ASSERT(!(context->flags & ATTR_KERNOVAL));
2518 if (context->flags & ATTR_KERNAMELS) {
2521 ASSERT(context->count >= 0);
2523 arraytop = context->count + namesp->attr_namelen + namelen + 1;
2524 if (arraytop > context->firstu) {
2525 context->count = -1; /* insufficient space */
2528 offset = (char *)context->alist + context->count;
2529 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
2530 offset += namesp->attr_namelen;
2531 strncpy(offset, name, namelen); /* real name */
2534 context->count += namesp->attr_namelen + namelen + 1;
2538 ASSERT(context->count >= 0);
2539 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
2540 ASSERT(context->firstu >= sizeof(*context->alist));
2541 ASSERT(context->firstu <= context->bufsize);
2543 arraytop = sizeof(*context->alist) +
2544 context->count * sizeof(context->alist->al_offset[0]);
2545 context->firstu -= ATTR_ENTSIZE(namelen);
2546 if (context->firstu < arraytop) {
2547 xfs_attr_trace_l_c("buffer full", context);
2548 context->alist->al_more = 1;
2552 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
2553 aep->a_valuelen = valuelen;
2554 memcpy(aep->a_name, name, namelen);
2555 aep->a_name[ namelen ] = 0;
2556 context->alist->al_offset[ context->count++ ] = context->firstu;
2557 context->alist->al_count = context->count;
2558 xfs_attr_trace_l_c("add", context);
2562 /*========================================================================
2563 * Manage the INCOMPLETE flag in a leaf entry
2564 *========================================================================*/
2567 * Clear the INCOMPLETE flag on an entry in a leaf block.
2570 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2572 xfs_attr_leafblock_t *leaf;
2573 xfs_attr_leaf_entry_t *entry;
2574 xfs_attr_leaf_name_remote_t *name_rmt;
2578 xfs_attr_leaf_name_local_t *name_loc;
2584 * Set up the operation.
2586 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2594 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2595 == XFS_ATTR_LEAF_MAGIC);
2596 ASSERT(args->index < INT_GET(leaf->hdr.count, ARCH_CONVERT));
2597 ASSERT(args->index >= 0);
2598 entry = &leaf->entries[ args->index ];
2599 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2602 if (entry->flags & XFS_ATTR_LOCAL) {
2603 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2604 namelen = name_loc->namelen;
2605 name = (char *)name_loc->nameval;
2607 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2608 namelen = name_rmt->namelen;
2609 name = (char *)name_rmt->name;
2611 ASSERT(INT_GET(entry->hashval, ARCH_CONVERT) == args->hashval);
2612 ASSERT(namelen == args->namelen);
2613 ASSERT(memcmp(name, args->name, namelen) == 0);
2616 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2617 xfs_da_log_buf(args->trans, bp,
2618 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2620 if (args->rmtblkno) {
2621 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2622 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2623 INT_SET(name_rmt->valueblk, ARCH_CONVERT, args->rmtblkno);
2624 INT_SET(name_rmt->valuelen, ARCH_CONVERT, args->valuelen);
2625 xfs_da_log_buf(args->trans, bp,
2626 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2628 xfs_da_buf_done(bp);
2631 * Commit the flag value change and start the next trans in series.
2633 error = xfs_attr_rolltrans(&args->trans, args->dp);
2639 * Set the INCOMPLETE flag on an entry in a leaf block.
2642 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2644 xfs_attr_leafblock_t *leaf;
2645 xfs_attr_leaf_entry_t *entry;
2646 xfs_attr_leaf_name_remote_t *name_rmt;
2651 * Set up the operation.
2653 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2661 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2662 == XFS_ATTR_LEAF_MAGIC);
2663 ASSERT(args->index < INT_GET(leaf->hdr.count, ARCH_CONVERT));
2664 ASSERT(args->index >= 0);
2665 entry = &leaf->entries[ args->index ];
2667 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2668 entry->flags |= XFS_ATTR_INCOMPLETE;
2669 xfs_da_log_buf(args->trans, bp,
2670 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2671 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2672 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2673 name_rmt->valueblk = 0;
2674 name_rmt->valuelen = 0;
2675 xfs_da_log_buf(args->trans, bp,
2676 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2678 xfs_da_buf_done(bp);
2681 * Commit the flag value change and start the next trans in series.
2683 error = xfs_attr_rolltrans(&args->trans, args->dp);
2689 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2690 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2691 * entry given by args->blkno2/index2.
2693 * Note that they could be in different blocks, or in the same block.
2696 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2698 xfs_attr_leafblock_t *leaf1, *leaf2;
2699 xfs_attr_leaf_entry_t *entry1, *entry2;
2700 xfs_attr_leaf_name_remote_t *name_rmt;
2701 xfs_dabuf_t *bp1, *bp2;
2704 xfs_attr_leaf_name_local_t *name_loc;
2705 int namelen1, namelen2;
2706 char *name1, *name2;
2710 * Read the block containing the "old" attr
2712 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2717 ASSERT(bp1 != NULL);
2720 * Read the block containing the "new" attr, if it is different
2722 if (args->blkno2 != args->blkno) {
2723 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2724 -1, &bp2, XFS_ATTR_FORK);
2728 ASSERT(bp2 != NULL);
2734 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
2735 == XFS_ATTR_LEAF_MAGIC);
2736 ASSERT(args->index < INT_GET(leaf1->hdr.count, ARCH_CONVERT));
2737 ASSERT(args->index >= 0);
2738 entry1 = &leaf1->entries[ args->index ];
2741 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2742 == XFS_ATTR_LEAF_MAGIC);
2743 ASSERT(args->index2 < INT_GET(leaf2->hdr.count, ARCH_CONVERT));
2744 ASSERT(args->index2 >= 0);
2745 entry2 = &leaf2->entries[ args->index2 ];
2748 if (entry1->flags & XFS_ATTR_LOCAL) {
2749 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf1, args->index);
2750 namelen1 = name_loc->namelen;
2751 name1 = (char *)name_loc->nameval;
2753 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2754 namelen1 = name_rmt->namelen;
2755 name1 = (char *)name_rmt->name;
2757 if (entry2->flags & XFS_ATTR_LOCAL) {
2758 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf2, args->index2);
2759 namelen2 = name_loc->namelen;
2760 name2 = (char *)name_loc->nameval;
2762 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2763 namelen2 = name_rmt->namelen;
2764 name2 = (char *)name_rmt->name;
2766 ASSERT(INT_GET(entry1->hashval, ARCH_CONVERT) == INT_GET(entry2->hashval, ARCH_CONVERT));
2767 ASSERT(namelen1 == namelen2);
2768 ASSERT(memcmp(name1, name2, namelen1) == 0);
2771 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2772 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2774 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2775 xfs_da_log_buf(args->trans, bp1,
2776 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2777 if (args->rmtblkno) {
2778 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2779 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2780 INT_SET(name_rmt->valueblk, ARCH_CONVERT, args->rmtblkno);
2781 INT_SET(name_rmt->valuelen, ARCH_CONVERT, args->valuelen);
2782 xfs_da_log_buf(args->trans, bp1,
2783 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2786 entry2->flags |= XFS_ATTR_INCOMPLETE;
2787 xfs_da_log_buf(args->trans, bp2,
2788 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2789 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2790 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2791 name_rmt->valueblk = 0;
2792 name_rmt->valuelen = 0;
2793 xfs_da_log_buf(args->trans, bp2,
2794 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2796 xfs_da_buf_done(bp1);
2798 xfs_da_buf_done(bp2);
2801 * Commit the flag value change and start the next trans in series.
2803 error = xfs_attr_rolltrans(&args->trans, args->dp);
2808 /*========================================================================
2809 * Indiscriminately delete the entire attribute fork
2810 *========================================================================*/
2813 * Recurse (gasp!) through the attribute nodes until we find leaves.
2814 * We're doing a depth-first traversal in order to invalidate everything.
2817 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2819 xfs_da_blkinfo_t *info;
2825 * Read block 0 to see what we have to work with.
2826 * We only get here if we have extents, since we remove
2827 * the extents in reverse order the extent containing
2828 * block 0 must still be there.
2830 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2833 blkno = xfs_da_blkno(bp);
2836 * Invalidate the tree, even if the "tree" is only a single leaf block.
2837 * This is a depth-first traversal!
2840 if (INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
2841 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2842 } else if (INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC) {
2843 error = xfs_attr_leaf_inactive(trans, dp, bp);
2845 error = XFS_ERROR(EIO);
2846 xfs_da_brelse(*trans, bp);
2852 * Invalidate the incore copy of the root block.
2854 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2857 xfs_da_binval(*trans, bp); /* remove from cache */
2859 * Commit the invalidate and start the next transaction.
2861 error = xfs_attr_rolltrans(trans, dp);
2867 * Recurse (gasp!) through the attribute nodes until we find leaves.
2868 * We're doing a depth-first traversal in order to invalidate everything.
2871 xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2874 xfs_da_blkinfo_t *info;
2875 xfs_da_intnode_t *node;
2876 xfs_dablk_t child_fsb;
2877 xfs_daddr_t parent_blkno, child_blkno;
2878 int error, count, i;
2879 xfs_dabuf_t *child_bp;
2882 * Since this code is recursive (gasp!) we must protect ourselves.
2884 if (level > XFS_DA_NODE_MAXDEPTH) {
2885 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2886 return(XFS_ERROR(EIO));
2890 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT)
2891 == XFS_DA_NODE_MAGIC);
2892 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
2893 count = INT_GET(node->hdr.count, ARCH_CONVERT);
2895 xfs_da_brelse(*trans, bp);
2898 child_fsb = INT_GET(node->btree[0].before, ARCH_CONVERT);
2899 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2902 * If this is the node level just above the leaves, simply loop
2903 * over the leaves removing all of them. If this is higher up
2904 * in the tree, recurse downward.
2906 for (i = 0; i < count; i++) {
2908 * Read the subsidiary block to see what we have to work with.
2909 * Don't do this in a transaction. This is a depth-first
2910 * traversal of the tree so we may deal with many blocks
2911 * before we come back to this one.
2913 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2918 /* save for re-read later */
2919 child_blkno = xfs_da_blkno(child_bp);
2922 * Invalidate the subtree, however we have to.
2924 info = child_bp->data;
2925 if (INT_GET(info->magic, ARCH_CONVERT)
2926 == XFS_DA_NODE_MAGIC) {
2927 error = xfs_attr_node_inactive(trans, dp,
2929 } else if (INT_GET(info->magic, ARCH_CONVERT)
2930 == XFS_ATTR_LEAF_MAGIC) {
2931 error = xfs_attr_leaf_inactive(trans, dp,
2934 error = XFS_ERROR(EIO);
2935 xfs_da_brelse(*trans, child_bp);
2941 * Remove the subsidiary block from the cache
2944 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2945 &child_bp, XFS_ATTR_FORK);
2948 xfs_da_binval(*trans, child_bp);
2952 * If we're not done, re-read the parent to get the next
2953 * child block number.
2955 if ((i+1) < count) {
2956 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2957 &bp, XFS_ATTR_FORK);
2960 child_fsb = INT_GET(node->btree[i+1].before, ARCH_CONVERT);
2961 xfs_da_brelse(*trans, bp);
2964 * Atomically commit the whole invalidate stuff.
2966 if ((error = xfs_attr_rolltrans(trans, dp)))
2974 * Invalidate all of the "remote" value regions pointed to by a particular
2976 * Note that we must release the lock on the buffer so that we are not
2977 * caught holding something that the logging code wants to flush to disk.
2980 xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2982 xfs_attr_leafblock_t *leaf;
2983 xfs_attr_leaf_entry_t *entry;
2984 xfs_attr_leaf_name_remote_t *name_rmt;
2985 xfs_attr_inactive_list_t *list, *lp;
2986 int error, count, size, tmp, i;
2989 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2990 == XFS_ATTR_LEAF_MAGIC);
2993 * Count the number of "remote" value extents.
2996 entry = &leaf->entries[0];
2997 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
2998 if ( INT_GET(entry->nameidx, ARCH_CONVERT)
2999 && ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
3000 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
3001 if (name_rmt->valueblk)
3007 * If there are no "remote" values, we're done.
3010 xfs_da_brelse(*trans, bp);
3015 * Allocate storage for a list of all the "remote" value extents.
3017 size = count * sizeof(xfs_attr_inactive_list_t);
3018 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
3021 * Identify each of the "remote" value extents.
3024 entry = &leaf->entries[0];
3025 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
3026 if ( INT_GET(entry->nameidx, ARCH_CONVERT)
3027 && ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
3028 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
3029 if (name_rmt->valueblk) {
3030 /* both on-disk, don't endian flip twice */
3031 lp->valueblk = name_rmt->valueblk;
3032 INT_SET(lp->valuelen, ARCH_CONVERT,
3033 XFS_B_TO_FSB(dp->i_mount,
3034 INT_GET(name_rmt->valuelen,
3040 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
3043 * Invalidate each of the "remote" value extents.
3046 for (lp = list, i = 0; i < count; i++, lp++) {
3047 tmp = xfs_attr_leaf_freextent(trans, dp,
3048 INT_GET(lp->valueblk,
3050 INT_GET(lp->valuelen,
3053 error = tmp; /* save only the 1st errno */
3056 kmem_free((xfs_caddr_t)list, size);
3061 * Look at all the extents for this logical region,
3062 * invalidate any buffers that are incore/in transactions.
3065 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
3066 xfs_dablk_t blkno, int blkcnt)
3068 xfs_bmbt_irec_t map;
3070 int tblkcnt, dblkcnt, nmap, error;
3075 * Roll through the "value", invalidating the attribute value's
3080 while (tblkcnt > 0) {
3082 * Try to remember where we decided to put the value.
3085 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
3086 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3087 NULL, 0, &map, &nmap, NULL);
3092 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3095 * If it's a hole, these are already unmapped
3096 * so there's nothing to invalidate.
3098 if (map.br_startblock != HOLESTARTBLOCK) {
3100 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3102 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3104 bp = xfs_trans_get_buf(*trans,
3105 dp->i_mount->m_ddev_targp,
3106 dblkno, dblkcnt, XFS_BUF_LOCK);
3107 xfs_trans_binval(*trans, bp);
3109 * Roll to next transaction.
3111 if ((error = xfs_attr_rolltrans(trans, dp)))
3115 tblkno += map.br_blockcount;
3116 tblkcnt -= map.br_blockcount;
3124 * Roll from one trans in the sequence of PERMANENT transactions to the next.
3127 xfs_attr_rolltrans(xfs_trans_t **transp, xfs_inode_t *dp)
3130 unsigned int logres, count;
3134 * Ensure that the inode is always logged.
3137 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
3140 * Copy the critical parameters from one trans to the next.
3142 logres = trans->t_log_res;
3143 count = trans->t_log_count;
3144 *transp = xfs_trans_dup(trans);
3147 * Commit the current transaction.
3148 * If this commit failed, then it'd just unlock those items that
3149 * are not marked ihold. That also means that a filesystem shutdown
3150 * is in progress. The caller takes the responsibility to cancel
3151 * the duplicate transaction that gets returned.
3153 if ((error = xfs_trans_commit(trans, 0, NULL)))
3159 * Reserve space in the log for th next transaction.
3160 * This also pushes items in the "AIL", the list of logged items,
3161 * out to disk if they are taking up space at the tail of the log
3162 * that we want to use. This requires that either nothing be locked
3163 * across this call, or that anything that is locked be logged in
3164 * the prior and the next transactions.
3166 error = xfs_trans_reserve(trans, 0, logres, 0,
3167 XFS_TRANS_PERM_LOG_RES, count);
3169 * Ensure that the inode is in the new transaction and locked.
3172 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
3173 xfs_trans_ihold(trans, dp);