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 (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
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_ATTR2) &&
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 hdr->totsize = cpu_to_be16(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 < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
229 if (sfe->namelen != args->namelen)
231 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
233 if (((args->flags & ATTR_SECURE) != 0) !=
234 ((sfe->flags & XFS_ATTR_SECURE) != 0))
236 if (((args->flags & ATTR_ROOT) != 0) !=
237 ((sfe->flags & XFS_ATTR_ROOT) != 0))
243 offset = (char *)sfe - (char *)sf;
244 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
245 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
246 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
247 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
249 sfe->namelen = args->namelen;
250 sfe->valuelen = args->valuelen;
251 sfe->flags = (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
252 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
253 memcpy(sfe->nameval, args->name, args->namelen);
254 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
256 be16_add(&sf->hdr.totsize, size);
257 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
259 xfs_sbversion_add_attr2(mp, args->trans);
263 * Remove an attribute from the shortform attribute list structure.
266 xfs_attr_shortform_remove(xfs_da_args_t *args)
268 xfs_attr_shortform_t *sf;
269 xfs_attr_sf_entry_t *sfe;
270 int base, size=0, end, totsize, i;
276 base = sizeof(xfs_attr_sf_hdr_t);
277 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
280 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
282 size = XFS_ATTR_SF_ENTSIZE(sfe);
283 if (sfe->namelen != args->namelen)
285 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
287 if (((args->flags & ATTR_SECURE) != 0) !=
288 ((sfe->flags & XFS_ATTR_SECURE) != 0))
290 if (((args->flags & ATTR_ROOT) != 0) !=
291 ((sfe->flags & XFS_ATTR_ROOT) != 0))
296 return(XFS_ERROR(ENOATTR));
299 * Fix up the attribute fork data, covering the hole
302 totsize = be16_to_cpu(sf->hdr.totsize);
304 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
306 be16_add(&sf->hdr.totsize, -size);
309 * Fix up the start offset of the attribute fork
312 if (totsize == sizeof(xfs_attr_sf_hdr_t) && !args->addname &&
313 (mp->m_flags & XFS_MOUNT_ATTR2)) {
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 !(mp->m_flags & XFS_MOUNT_ATTR2));
333 dp->i_afp->if_ext_max =
334 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
335 dp->i_df.if_ext_max =
336 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
337 xfs_trans_log_inode(args->trans, dp,
338 XFS_ILOG_CORE | XFS_ILOG_ADATA);
341 xfs_sbversion_add_attr2(mp, args->trans);
347 * Look up a name in a shortform attribute list structure.
351 xfs_attr_shortform_lookup(xfs_da_args_t *args)
353 xfs_attr_shortform_t *sf;
354 xfs_attr_sf_entry_t *sfe;
358 ifp = args->dp->i_afp;
359 ASSERT(ifp->if_flags & XFS_IFINLINE);
360 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
362 for (i = 0; i < sf->hdr.count;
363 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
364 if (sfe->namelen != args->namelen)
366 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
368 if (((args->flags & ATTR_SECURE) != 0) !=
369 ((sfe->flags & XFS_ATTR_SECURE) != 0))
371 if (((args->flags & ATTR_ROOT) != 0) !=
372 ((sfe->flags & XFS_ATTR_ROOT) != 0))
374 return(XFS_ERROR(EEXIST));
376 return(XFS_ERROR(ENOATTR));
380 * Look up a name in a shortform attribute list structure.
384 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
386 xfs_attr_shortform_t *sf;
387 xfs_attr_sf_entry_t *sfe;
390 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
391 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
393 for (i = 0; i < sf->hdr.count;
394 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
395 if (sfe->namelen != args->namelen)
397 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
399 if (((args->flags & ATTR_SECURE) != 0) !=
400 ((sfe->flags & XFS_ATTR_SECURE) != 0))
402 if (((args->flags & ATTR_ROOT) != 0) !=
403 ((sfe->flags & XFS_ATTR_ROOT) != 0))
405 if (args->flags & ATTR_KERNOVAL) {
406 args->valuelen = sfe->valuelen;
407 return(XFS_ERROR(EEXIST));
409 if (args->valuelen < sfe->valuelen) {
410 args->valuelen = sfe->valuelen;
411 return(XFS_ERROR(ERANGE));
413 args->valuelen = sfe->valuelen;
414 memcpy(args->value, &sfe->nameval[args->namelen],
416 return(XFS_ERROR(EEXIST));
418 return(XFS_ERROR(ENOATTR));
422 * Convert from using the shortform to the leaf.
425 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
428 xfs_attr_shortform_t *sf;
429 xfs_attr_sf_entry_t *sfe;
439 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
440 size = be16_to_cpu(sf->hdr.totsize);
441 tmpbuffer = kmem_alloc(size, KM_SLEEP);
442 ASSERT(tmpbuffer != NULL);
443 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
444 sf = (xfs_attr_shortform_t *)tmpbuffer;
446 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
448 error = xfs_da_grow_inode(args, &blkno);
451 * If we hit an IO error middle of the transaction inside
452 * grow_inode(), we may have inconsistent data. Bail out.
456 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
457 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
462 error = xfs_attr_leaf_create(args, blkno, &bp);
464 error = xfs_da_shrink_inode(args, 0, bp);
468 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
469 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
473 memset((char *)&nargs, 0, sizeof(nargs));
475 nargs.firstblock = args->firstblock;
476 nargs.flist = args->flist;
477 nargs.total = args->total;
478 nargs.whichfork = XFS_ATTR_FORK;
479 nargs.trans = args->trans;
483 for (i = 0; i < sf->hdr.count; i++) {
484 nargs.name = (char *)sfe->nameval;
485 nargs.namelen = sfe->namelen;
486 nargs.value = (char *)&sfe->nameval[nargs.namelen];
487 nargs.valuelen = sfe->valuelen;
488 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
490 nargs.flags = (sfe->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
491 ((sfe->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
492 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
493 ASSERT(error == ENOATTR);
494 error = xfs_attr_leaf_add(bp, &nargs);
495 ASSERT(error != ENOSPC);
498 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
505 kmem_free(tmpbuffer, size);
510 xfs_attr_shortform_compare(const void *a, const void *b)
512 xfs_attr_sf_sort_t *sa, *sb;
514 sa = (xfs_attr_sf_sort_t *)a;
515 sb = (xfs_attr_sf_sort_t *)b;
516 if (INT_GET(sa->hash, ARCH_CONVERT)
517 < INT_GET(sb->hash, ARCH_CONVERT)) {
519 } else if (INT_GET(sa->hash, ARCH_CONVERT)
520 > INT_GET(sb->hash, ARCH_CONVERT)) {
523 return(sa->entno - sb->entno);
528 * Copy out entries of shortform attribute lists for attr_list().
529 * Shortform atrtribute lists are not stored in hashval sorted order.
530 * If the output buffer is not large enough to hold them all, then we
531 * we have to calculate each entries' hashvalue and sort them before
532 * we can begin returning them to the user.
536 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
538 attrlist_cursor_kern_t *cursor;
539 xfs_attr_sf_sort_t *sbuf, *sbp;
540 xfs_attr_shortform_t *sf;
541 xfs_attr_sf_entry_t *sfe;
543 int sbsize, nsbuf, count, i;
545 ASSERT(context != NULL);
548 ASSERT(dp->i_afp != NULL);
549 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
553 cursor = context->cursor;
554 ASSERT(cursor != NULL);
556 xfs_attr_trace_l_c("sf start", context);
559 * If the buffer is large enough, do not bother with sorting.
560 * Note the generous fudge factor of 16 overhead bytes per entry.
562 if ((dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize) {
563 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
566 if (((context->flags & ATTR_SECURE) != 0) !=
567 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
568 !(context->flags & ATTR_KERNORMALS)) {
569 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
572 if (((context->flags & ATTR_ROOT) != 0) !=
573 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
574 !(context->flags & ATTR_KERNROOTLS)) {
575 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
578 namesp = (sfe->flags & XFS_ATTR_SECURE) ? &attr_secure:
579 ((sfe->flags & XFS_ATTR_ROOT) ? &attr_trusted :
581 if (context->flags & ATTR_KERNOVAL) {
582 ASSERT(context->flags & ATTR_KERNAMELS);
583 context->count += namesp->attr_namelen +
587 if (xfs_attr_put_listent(context, namesp,
588 (char *)sfe->nameval,
593 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
595 xfs_attr_trace_l_c("sf big-gulp", context);
600 * It didn't all fit, so we have to sort everything on hashval.
602 sbsize = sf->hdr.count * sizeof(*sbuf);
603 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
606 * Scan the attribute list for the rest of the entries, storing
607 * the relevant info from only those that match into a buffer.
610 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
612 ((char *)sfe < (char *)sf) ||
613 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
614 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
616 context->dp->i_mount, sfe);
617 xfs_attr_trace_l_c("sf corrupted", context);
618 kmem_free(sbuf, sbsize);
619 return XFS_ERROR(EFSCORRUPTED);
621 if (((context->flags & ATTR_SECURE) != 0) !=
622 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
623 !(context->flags & ATTR_KERNORMALS)) {
624 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
627 if (((context->flags & ATTR_ROOT) != 0) !=
628 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
629 !(context->flags & ATTR_KERNROOTLS)) {
630 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
634 INT_SET(sbp->hash, ARCH_CONVERT,
635 xfs_da_hashname((char *)sfe->nameval, sfe->namelen));
636 sbp->name = (char *)sfe->nameval;
637 sbp->namelen = sfe->namelen;
638 /* These are bytes, and both on-disk, don't endian-flip */
639 sbp->valuelen = sfe->valuelen;
640 sbp->flags = sfe->flags;
641 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
647 * Sort the entries on hash then entno.
649 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
652 * Re-find our place IN THE SORTED LIST.
657 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
658 if (INT_GET(sbp->hash, ARCH_CONVERT) == cursor->hashval) {
659 if (cursor->offset == count) {
663 } else if (INT_GET(sbp->hash, ARCH_CONVERT) > cursor->hashval) {
668 kmem_free(sbuf, sbsize);
669 xfs_attr_trace_l_c("blk end", context);
674 * Loop putting entries into the user buffer.
676 for ( ; i < nsbuf; i++, sbp++) {
679 namesp = (sbp->flags & XFS_ATTR_SECURE) ? &attr_secure :
680 ((sbp->flags & XFS_ATTR_ROOT) ? &attr_trusted :
683 if (cursor->hashval != INT_GET(sbp->hash, ARCH_CONVERT)) {
684 cursor->hashval = INT_GET(sbp->hash, ARCH_CONVERT);
687 if (context->flags & ATTR_KERNOVAL) {
688 ASSERT(context->flags & ATTR_KERNAMELS);
689 context->count += namesp->attr_namelen +
692 if (xfs_attr_put_listent(context, namesp,
693 sbp->name, sbp->namelen,
700 kmem_free(sbuf, sbsize);
701 xfs_attr_trace_l_c("sf E-O-F", context);
706 * Check a leaf attribute block to see if all the entries would fit into
707 * a shortform attribute list.
710 xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
712 xfs_attr_leafblock_t *leaf;
713 xfs_attr_leaf_entry_t *entry;
714 xfs_attr_leaf_name_local_t *name_loc;
718 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
720 entry = &leaf->entries[0];
721 bytes = sizeof(struct xfs_attr_sf_hdr);
722 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
723 if (entry->flags & XFS_ATTR_INCOMPLETE)
724 continue; /* don't copy partial entries */
725 if (!(entry->flags & XFS_ATTR_LOCAL))
727 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
728 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
730 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
732 bytes += sizeof(struct xfs_attr_sf_entry)-1
734 + be16_to_cpu(name_loc->valuelen);
736 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
737 (bytes == sizeof(struct xfs_attr_sf_hdr)))
739 return(xfs_attr_shortform_bytesfit(dp, bytes));
743 * Convert a leaf attribute list to shortform attribute list
746 xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
748 xfs_attr_leafblock_t *leaf;
749 xfs_attr_leaf_entry_t *entry;
750 xfs_attr_leaf_name_local_t *name_loc;
757 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
758 ASSERT(tmpbuffer != NULL);
761 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
762 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
763 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
764 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
767 * Clean out the prior contents of the attribute list.
769 error = xfs_da_shrink_inode(args, 0, bp);
774 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
777 * Last attribute was removed, revert to original
778 * inode format making all literal area available
779 * to the data fork once more.
781 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
782 dp->i_d.di_forkoff = 0;
783 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
784 ASSERT(dp->i_d.di_anextents == 0);
785 ASSERT(dp->i_afp == NULL);
786 dp->i_df.if_ext_max =
787 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
788 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
792 xfs_attr_shortform_create(args);
795 * Copy the attributes
797 memset((char *)&nargs, 0, sizeof(nargs));
799 nargs.firstblock = args->firstblock;
800 nargs.flist = args->flist;
801 nargs.total = args->total;
802 nargs.whichfork = XFS_ATTR_FORK;
803 nargs.trans = args->trans;
805 entry = &leaf->entries[0];
806 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
807 if (entry->flags & XFS_ATTR_INCOMPLETE)
808 continue; /* don't copy partial entries */
811 ASSERT(entry->flags & XFS_ATTR_LOCAL);
812 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
813 nargs.name = (char *)name_loc->nameval;
814 nargs.namelen = name_loc->namelen;
815 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
816 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
817 nargs.hashval = be32_to_cpu(entry->hashval);
818 nargs.flags = (entry->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
819 ((entry->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
820 xfs_attr_shortform_add(&nargs, forkoff);
825 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
830 * Convert from using a single leaf to a root node and a leaf.
833 xfs_attr_leaf_to_node(xfs_da_args_t *args)
835 xfs_attr_leafblock_t *leaf;
836 xfs_da_intnode_t *node;
838 xfs_dabuf_t *bp1, *bp2;
844 error = xfs_da_grow_inode(args, &blkno);
847 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
853 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
858 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
859 xfs_da_buf_done(bp1);
861 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
864 * Set up the new root node.
866 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
871 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
872 /* both on-disk, don't endian-flip twice */
873 node->btree[0].hashval =
874 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
875 INT_SET(node->btree[0].before, ARCH_CONVERT, blkno);
876 INT_SET(node->hdr.count, ARCH_CONVERT, 1);
877 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
881 xfs_da_buf_done(bp1);
883 xfs_da_buf_done(bp2);
888 /*========================================================================
889 * Routines used for growing the Btree.
890 *========================================================================*/
893 * Create the initial contents of a leaf attribute list
894 * or a leaf in a node attribute list.
897 xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
899 xfs_attr_leafblock_t *leaf;
900 xfs_attr_leaf_hdr_t *hdr;
907 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
913 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
915 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
916 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
917 if (!hdr->firstused) {
918 hdr->firstused = cpu_to_be16(
919 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
922 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
923 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
924 sizeof(xfs_attr_leaf_hdr_t));
926 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
933 * Split the leaf node, rebalance, then add the new entry.
936 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
937 xfs_da_state_blk_t *newblk)
943 * Allocate space for a new leaf node.
945 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
946 error = xfs_da_grow_inode(state->args, &blkno);
949 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
952 newblk->blkno = blkno;
953 newblk->magic = XFS_ATTR_LEAF_MAGIC;
956 * Rebalance the entries across the two leaves.
957 * NOTE: rebalance() currently depends on the 2nd block being empty.
959 xfs_attr_leaf_rebalance(state, oldblk, newblk);
960 error = xfs_da_blk_link(state, oldblk, newblk);
965 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
966 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
967 * "new" attrs info. Will need the "old" info to remove it later.
969 * Insert the "new" entry in the correct block.
972 error = xfs_attr_leaf_add(oldblk->bp, state->args);
974 error = xfs_attr_leaf_add(newblk->bp, state->args);
977 * Update last hashval in each block since we added the name.
979 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
980 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
985 * Add a name to the leaf attribute list structure.
988 xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
990 xfs_attr_leafblock_t *leaf;
991 xfs_attr_leaf_hdr_t *hdr;
992 xfs_attr_leaf_map_t *map;
993 int tablesize, entsize, sum, tmp, i;
996 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
997 ASSERT((args->index >= 0)
998 && (args->index <= be16_to_cpu(leaf->hdr.count)));
1000 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1001 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1004 * Search through freemap for first-fit on new name length.
1005 * (may need to figure in size of entry struct too)
1007 tablesize = (be16_to_cpu(hdr->count) + 1)
1008 * sizeof(xfs_attr_leaf_entry_t)
1009 + sizeof(xfs_attr_leaf_hdr_t);
1010 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1011 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1012 if (tablesize > be16_to_cpu(hdr->firstused)) {
1013 sum += be16_to_cpu(map->size);
1017 continue; /* no space in this map */
1019 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
1020 tmp += sizeof(xfs_attr_leaf_entry_t);
1021 if (be16_to_cpu(map->size) >= tmp) {
1022 tmp = xfs_attr_leaf_add_work(bp, args, i);
1025 sum += be16_to_cpu(map->size);
1029 * If there are no holes in the address space of the block,
1030 * and we don't have enough freespace, then compaction will do us
1031 * no good and we should just give up.
1033 if (!hdr->holes && (sum < entsize))
1034 return(XFS_ERROR(ENOSPC));
1037 * Compact the entries to coalesce free space.
1038 * This may change the hdr->count via dropping INCOMPLETE entries.
1040 xfs_attr_leaf_compact(args->trans, bp);
1043 * After compaction, the block is guaranteed to have only one
1044 * free region, in freemap[0]. If it is not big enough, give up.
1046 if (be16_to_cpu(hdr->freemap[0].size)
1047 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1048 return(XFS_ERROR(ENOSPC));
1050 return(xfs_attr_leaf_add_work(bp, args, 0));
1054 * Add a name to a leaf attribute list structure.
1057 xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1059 xfs_attr_leafblock_t *leaf;
1060 xfs_attr_leaf_hdr_t *hdr;
1061 xfs_attr_leaf_entry_t *entry;
1062 xfs_attr_leaf_name_local_t *name_loc;
1063 xfs_attr_leaf_name_remote_t *name_rmt;
1064 xfs_attr_leaf_map_t *map;
1069 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1071 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1072 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
1075 * Force open some space in the entry array and fill it in.
1077 entry = &leaf->entries[args->index];
1078 if (args->index < be16_to_cpu(hdr->count)) {
1079 tmp = be16_to_cpu(hdr->count) - args->index;
1080 tmp *= sizeof(xfs_attr_leaf_entry_t);
1081 memmove((char *)(entry+1), (char *)entry, tmp);
1082 xfs_da_log_buf(args->trans, bp,
1083 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1085 be16_add(&hdr->count, 1);
1088 * Allocate space for the new string (at the end of the run).
1090 map = &hdr->freemap[mapindex];
1091 mp = args->trans->t_mountp;
1092 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1093 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1094 ASSERT(be16_to_cpu(map->size) >=
1095 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1096 mp->m_sb.sb_blocksize, NULL));
1097 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1098 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1099 be16_add(&map->size,
1100 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1101 mp->m_sb.sb_blocksize, &tmp));
1102 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1103 be16_to_cpu(map->size));
1104 entry->hashval = cpu_to_be32(args->hashval);
1105 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1106 entry->flags |= (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
1107 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
1109 entry->flags |= XFS_ATTR_INCOMPLETE;
1110 if ((args->blkno2 == args->blkno) &&
1111 (args->index2 <= args->index)) {
1115 xfs_da_log_buf(args->trans, bp,
1116 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1117 ASSERT((args->index == 0) ||
1118 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1119 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
1120 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1123 * Copy the attribute name and value into the new space.
1125 * For "remote" attribute values, simply note that we need to
1126 * allocate space for the "remote" value. We can't actually
1127 * allocate the extents in this transaction, and we can't decide
1128 * which blocks they should be as we might allocate more blocks
1129 * as part of this transaction (a split operation for example).
1131 if (entry->flags & XFS_ATTR_LOCAL) {
1132 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1133 name_loc->namelen = args->namelen;
1134 name_loc->valuelen = cpu_to_be16(args->valuelen);
1135 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1136 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1137 be16_to_cpu(name_loc->valuelen));
1139 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
1140 name_rmt->namelen = args->namelen;
1141 memcpy((char *)name_rmt->name, args->name, args->namelen);
1142 entry->flags |= XFS_ATTR_INCOMPLETE;
1144 name_rmt->valuelen = 0;
1145 name_rmt->valueblk = 0;
1147 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1149 xfs_da_log_buf(args->trans, bp,
1150 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1151 xfs_attr_leaf_entsize(leaf, args->index)));
1154 * Update the control info for this leaf node
1156 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
1157 /* both on-disk, don't endian-flip twice */
1158 hdr->firstused = entry->nameidx;
1160 ASSERT(be16_to_cpu(hdr->firstused) >=
1161 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1162 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
1163 + sizeof(xfs_attr_leaf_hdr_t);
1164 map = &hdr->freemap[0];
1165 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1166 if (be16_to_cpu(map->base) == tmp) {
1167 be16_add(&map->base, sizeof(xfs_attr_leaf_entry_t));
1168 be16_add(&map->size,
1169 -((int)sizeof(xfs_attr_leaf_entry_t)));
1172 be16_add(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
1173 xfs_da_log_buf(args->trans, bp,
1174 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1179 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1182 xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1184 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1185 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1189 mp = trans->t_mountp;
1190 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1191 ASSERT(tmpbuffer != NULL);
1192 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1193 memset(bp->data, 0, XFS_LBSIZE(mp));
1196 * Copy basic information
1198 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1200 hdr_s = &leaf_s->hdr;
1201 hdr_d = &leaf_d->hdr;
1202 hdr_d->info = hdr_s->info; /* struct copy */
1203 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
1204 /* handle truncation gracefully */
1205 if (!hdr_d->firstused) {
1206 hdr_d->firstused = cpu_to_be16(
1207 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1209 hdr_d->usedbytes = 0;
1212 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1213 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1214 sizeof(xfs_attr_leaf_hdr_t));
1217 * Copy all entry's in the same (sorted) order,
1218 * but allocate name/value pairs packed and in sequence.
1220 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1221 be16_to_cpu(hdr_s->count), mp);
1222 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1224 kmem_free(tmpbuffer, XFS_LBSIZE(mp));
1228 * Redistribute the attribute list entries between two leaf nodes,
1229 * taking into account the size of the new entry.
1231 * NOTE: if new block is empty, then it will get the upper half of the
1232 * old block. At present, all (one) callers pass in an empty second block.
1234 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1235 * to match what it is doing in splitting the attribute leaf block. Those
1236 * values are used in "atomic rename" operations on attributes. Note that
1237 * the "new" and "old" values can end up in different blocks.
1240 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1241 xfs_da_state_blk_t *blk2)
1243 xfs_da_args_t *args;
1244 xfs_da_state_blk_t *tmp_blk;
1245 xfs_attr_leafblock_t *leaf1, *leaf2;
1246 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1247 int count, totallen, max, space, swap;
1250 * Set up environment.
1252 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1253 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1254 leaf1 = blk1->bp->data;
1255 leaf2 = blk2->bp->data;
1256 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1257 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1261 * Check ordering of blocks, reverse if it makes things simpler.
1263 * NOTE: Given that all (current) callers pass in an empty
1264 * second block, this code should never set "swap".
1267 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1271 leaf1 = blk1->bp->data;
1272 leaf2 = blk2->bp->data;
1279 * Examine entries until we reduce the absolute difference in
1280 * byte usage between the two blocks to a minimum. Then get
1281 * the direction to copy and the number of elements to move.
1283 * "inleaf" is true if the new entry should be inserted into blk1.
1284 * If "swap" is also true, then reverse the sense of "inleaf".
1286 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1289 state->inleaf = !state->inleaf;
1292 * Move any entries required from leaf to leaf:
1294 if (count < be16_to_cpu(hdr1->count)) {
1296 * Figure the total bytes to be added to the destination leaf.
1298 /* number entries being moved */
1299 count = be16_to_cpu(hdr1->count) - count;
1300 space = be16_to_cpu(hdr1->usedbytes) - totallen;
1301 space += count * sizeof(xfs_attr_leaf_entry_t);
1304 * leaf2 is the destination, compact it if it looks tight.
1306 max = be16_to_cpu(hdr2->firstused)
1307 - sizeof(xfs_attr_leaf_hdr_t);
1308 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
1310 xfs_attr_leaf_compact(args->trans, blk2->bp);
1314 * Move high entries from leaf1 to low end of leaf2.
1316 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
1317 leaf2, 0, count, state->mp);
1319 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1320 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1321 } else if (count > be16_to_cpu(hdr1->count)) {
1323 * I assert that since all callers pass in an empty
1324 * second buffer, this code should never execute.
1328 * Figure the total bytes to be added to the destination leaf.
1330 /* number entries being moved */
1331 count -= be16_to_cpu(hdr1->count);
1332 space = totallen - be16_to_cpu(hdr1->usedbytes);
1333 space += count * sizeof(xfs_attr_leaf_entry_t);
1336 * leaf1 is the destination, compact it if it looks tight.
1338 max = be16_to_cpu(hdr1->firstused)
1339 - sizeof(xfs_attr_leaf_hdr_t);
1340 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
1342 xfs_attr_leaf_compact(args->trans, blk1->bp);
1346 * Move low entries from leaf2 to high end of leaf1.
1348 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1349 be16_to_cpu(hdr1->count), count, state->mp);
1351 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1352 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1356 * Copy out last hashval in each block for B-tree code.
1358 blk1->hashval = be32_to_cpu(
1359 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1360 blk2->hashval = be32_to_cpu(
1361 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
1364 * Adjust the expected index for insertion.
1365 * NOTE: this code depends on the (current) situation that the
1366 * second block was originally empty.
1368 * If the insertion point moved to the 2nd block, we must adjust
1369 * the index. We must also track the entry just following the
1370 * new entry for use in an "atomic rename" operation, that entry
1371 * is always the "old" entry and the "new" entry is what we are
1372 * inserting. The index/blkno fields refer to the "old" entry,
1373 * while the index2/blkno2 fields refer to the "new" entry.
1375 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
1376 ASSERT(state->inleaf == 0);
1377 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
1378 args->index = args->index2 = blk2->index;
1379 args->blkno = args->blkno2 = blk2->blkno;
1380 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
1381 if (state->inleaf) {
1382 args->index = blk1->index;
1383 args->blkno = blk1->blkno;
1385 args->blkno2 = blk2->blkno;
1387 blk2->index = blk1->index
1388 - be16_to_cpu(leaf1->hdr.count);
1389 args->index = args->index2 = blk2->index;
1390 args->blkno = args->blkno2 = blk2->blkno;
1393 ASSERT(state->inleaf == 1);
1394 args->index = args->index2 = blk1->index;
1395 args->blkno = args->blkno2 = blk1->blkno;
1400 * Examine entries until we reduce the absolute difference in
1401 * byte usage between the two blocks to a minimum.
1402 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1403 * GROT: there will always be enough room in either block for a new entry.
1404 * GROT: Do a double-split for this case?
1407 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1408 xfs_da_state_blk_t *blk1,
1409 xfs_da_state_blk_t *blk2,
1410 int *countarg, int *usedbytesarg)
1412 xfs_attr_leafblock_t *leaf1, *leaf2;
1413 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1414 xfs_attr_leaf_entry_t *entry;
1415 int count, max, index, totallen, half;
1416 int lastdelta, foundit, tmp;
1419 * Set up environment.
1421 leaf1 = blk1->bp->data;
1422 leaf2 = blk2->bp->data;
1429 * Examine entries until we reduce the absolute difference in
1430 * byte usage between the two blocks to a minimum.
1432 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
1433 half = (max+1) * sizeof(*entry);
1434 half += be16_to_cpu(hdr1->usedbytes) +
1435 be16_to_cpu(hdr2->usedbytes) +
1436 xfs_attr_leaf_newentsize(
1437 state->args->namelen,
1438 state->args->valuelen,
1439 state->blocksize, NULL);
1441 lastdelta = state->blocksize;
1442 entry = &leaf1->entries[0];
1443 for (count = index = 0; count < max; entry++, index++, count++) {
1445 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1447 * The new entry is in the first block, account for it.
1449 if (count == blk1->index) {
1450 tmp = totallen + sizeof(*entry) +
1451 xfs_attr_leaf_newentsize(
1452 state->args->namelen,
1453 state->args->valuelen,
1454 state->blocksize, NULL);
1455 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1457 lastdelta = XFS_ATTR_ABS(half - tmp);
1463 * Wrap around into the second block if necessary.
1465 if (count == be16_to_cpu(hdr1->count)) {
1467 entry = &leaf1->entries[0];
1472 * Figure out if next leaf entry would be too much.
1474 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1476 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1478 lastdelta = XFS_ATTR_ABS(half - tmp);
1484 * Calculate the number of usedbytes that will end up in lower block.
1485 * If new entry not in lower block, fix up the count.
1487 totallen -= count * sizeof(*entry);
1489 totallen -= sizeof(*entry) +
1490 xfs_attr_leaf_newentsize(
1491 state->args->namelen,
1492 state->args->valuelen,
1493 state->blocksize, NULL);
1497 *usedbytesarg = totallen;
1501 /*========================================================================
1502 * Routines used for shrinking the Btree.
1503 *========================================================================*/
1506 * Check a leaf block and its neighbors to see if the block should be
1507 * collapsed into one or the other neighbor. Always keep the block
1508 * with the smaller block number.
1509 * If the current block is over 50% full, don't try to join it, return 0.
1510 * If the block is empty, fill in the state structure and return 2.
1511 * If it can be collapsed, fill in the state structure and return 1.
1512 * If nothing can be done, return 0.
1514 * GROT: allow for INCOMPLETE entries in calculation.
1517 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1519 xfs_attr_leafblock_t *leaf;
1520 xfs_da_state_blk_t *blk;
1521 xfs_da_blkinfo_t *info;
1522 int count, bytes, forward, error, retval, i;
1527 * Check for the degenerate case of the block being over 50% full.
1528 * If so, it's not worth even looking to see if we might be able
1529 * to coalesce with a sibling.
1531 blk = &state->path.blk[ state->path.active-1 ];
1532 info = blk->bp->data;
1533 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
1534 leaf = (xfs_attr_leafblock_t *)info;
1535 count = be16_to_cpu(leaf->hdr.count);
1536 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1537 count * sizeof(xfs_attr_leaf_entry_t) +
1538 be16_to_cpu(leaf->hdr.usedbytes);
1539 if (bytes > (state->blocksize >> 1)) {
1540 *action = 0; /* blk over 50%, don't try to join */
1545 * Check for the degenerate case of the block being empty.
1546 * If the block is empty, we'll simply delete it, no need to
1547 * coalesce it with a sibling block. We choose (aribtrarily)
1548 * to merge with the forward block unless it is NULL.
1552 * Make altpath point to the block we want to keep and
1553 * path point to the block we want to drop (this one).
1555 forward = (info->forw != 0);
1556 memcpy(&state->altpath, &state->path, sizeof(state->path));
1557 error = xfs_da_path_shift(state, &state->altpath, forward,
1570 * Examine each sibling block to see if we can coalesce with
1571 * at least 25% free space to spare. We need to figure out
1572 * whether to merge with the forward or the backward block.
1573 * We prefer coalescing with the lower numbered sibling so as
1574 * to shrink an attribute list over time.
1576 /* start with smaller blk num */
1577 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
1578 for (i = 0; i < 2; forward = !forward, i++) {
1580 blkno = be32_to_cpu(info->forw);
1582 blkno = be32_to_cpu(info->back);
1585 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1586 blkno, -1, &bp, XFS_ATTR_FORK);
1591 leaf = (xfs_attr_leafblock_t *)info;
1592 count = be16_to_cpu(leaf->hdr.count);
1593 bytes = state->blocksize - (state->blocksize>>2);
1594 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1596 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1597 count += be16_to_cpu(leaf->hdr.count);
1598 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1599 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1600 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1601 xfs_da_brelse(state->args->trans, bp);
1603 break; /* fits with at least 25% to spare */
1611 * Make altpath point to the block we want to keep (the lower
1612 * numbered block) and path point to the block we want to drop.
1614 memcpy(&state->altpath, &state->path, sizeof(state->path));
1615 if (blkno < blk->blkno) {
1616 error = xfs_da_path_shift(state, &state->altpath, forward,
1619 error = xfs_da_path_shift(state, &state->path, forward,
1633 * Remove a name from the leaf attribute list structure.
1635 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1636 * If two leaves are 37% full, when combined they will leave 25% free.
1639 xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1641 xfs_attr_leafblock_t *leaf;
1642 xfs_attr_leaf_hdr_t *hdr;
1643 xfs_attr_leaf_map_t *map;
1644 xfs_attr_leaf_entry_t *entry;
1645 int before, after, smallest, entsize;
1646 int tablesize, tmp, i;
1650 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1652 mp = args->trans->t_mountp;
1653 ASSERT((be16_to_cpu(hdr->count) > 0)
1654 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
1655 ASSERT((args->index >= 0)
1656 && (args->index < be16_to_cpu(hdr->count)));
1657 ASSERT(be16_to_cpu(hdr->firstused) >=
1658 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1659 entry = &leaf->entries[args->index];
1660 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1661 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1664 * Scan through free region table:
1665 * check for adjacency of free'd entry with an existing one,
1666 * find smallest free region in case we need to replace it,
1667 * adjust any map that borders the entry table,
1669 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
1670 + sizeof(xfs_attr_leaf_hdr_t);
1671 map = &hdr->freemap[0];
1672 tmp = be16_to_cpu(map->size);
1673 before = after = -1;
1674 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1675 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1676 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1677 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1678 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1679 if (be16_to_cpu(map->base) == tablesize) {
1680 be16_add(&map->base,
1681 -((int)sizeof(xfs_attr_leaf_entry_t)));
1682 be16_add(&map->size, sizeof(xfs_attr_leaf_entry_t));
1685 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
1686 == be16_to_cpu(entry->nameidx)) {
1688 } else if (be16_to_cpu(map->base)
1689 == (be16_to_cpu(entry->nameidx) + entsize)) {
1691 } else if (be16_to_cpu(map->size) < tmp) {
1692 tmp = be16_to_cpu(map->size);
1698 * Coalesce adjacent freemap regions,
1699 * or replace the smallest region.
1701 if ((before >= 0) || (after >= 0)) {
1702 if ((before >= 0) && (after >= 0)) {
1703 map = &hdr->freemap[before];
1704 be16_add(&map->size, entsize);
1705 be16_add(&map->size,
1706 be16_to_cpu(hdr->freemap[after].size));
1707 hdr->freemap[after].base = 0;
1708 hdr->freemap[after].size = 0;
1709 } else if (before >= 0) {
1710 map = &hdr->freemap[before];
1711 be16_add(&map->size, entsize);
1713 map = &hdr->freemap[after];
1714 /* both on-disk, don't endian flip twice */
1715 map->base = entry->nameidx;
1716 be16_add(&map->size, entsize);
1720 * Replace smallest region (if it is smaller than free'd entry)
1722 map = &hdr->freemap[smallest];
1723 if (be16_to_cpu(map->size) < entsize) {
1724 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
1725 map->size = cpu_to_be16(entsize);
1730 * Did we remove the first entry?
1732 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
1738 * Compress the remaining entries and zero out the removed stuff.
1740 memset(XFS_ATTR_LEAF_NAME(leaf, args->index), 0, entsize);
1741 be16_add(&hdr->usedbytes, -entsize);
1742 xfs_da_log_buf(args->trans, bp,
1743 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1746 tmp = (be16_to_cpu(hdr->count) - args->index)
1747 * sizeof(xfs_attr_leaf_entry_t);
1748 memmove((char *)entry, (char *)(entry+1), tmp);
1749 be16_add(&hdr->count, -1);
1750 xfs_da_log_buf(args->trans, bp,
1751 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1752 entry = &leaf->entries[be16_to_cpu(hdr->count)];
1753 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1756 * If we removed the first entry, re-find the first used byte
1757 * in the name area. Note that if the entry was the "firstused",
1758 * then we don't have a "hole" in our block resulting from
1759 * removing the name.
1762 tmp = XFS_LBSIZE(mp);
1763 entry = &leaf->entries[0];
1764 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
1765 ASSERT(be16_to_cpu(entry->nameidx) >=
1766 be16_to_cpu(hdr->firstused));
1767 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1769 if (be16_to_cpu(entry->nameidx) < tmp)
1770 tmp = be16_to_cpu(entry->nameidx);
1772 hdr->firstused = cpu_to_be16(tmp);
1773 if (!hdr->firstused) {
1774 hdr->firstused = cpu_to_be16(
1775 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1778 hdr->holes = 1; /* mark as needing compaction */
1780 xfs_da_log_buf(args->trans, bp,
1781 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1784 * Check if leaf is less than 50% full, caller may want to
1785 * "join" the leaf with a sibling if so.
1787 tmp = sizeof(xfs_attr_leaf_hdr_t);
1788 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1789 tmp += be16_to_cpu(leaf->hdr.usedbytes);
1790 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1794 * Move all the attribute list entries from drop_leaf into save_leaf.
1797 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1798 xfs_da_state_blk_t *save_blk)
1800 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1801 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1806 * Set up environment.
1809 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1810 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1811 drop_leaf = drop_blk->bp->data;
1812 save_leaf = save_blk->bp->data;
1813 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1814 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1815 drop_hdr = &drop_leaf->hdr;
1816 save_hdr = &save_leaf->hdr;
1819 * Save last hashval from dying block for later Btree fixup.
1821 drop_blk->hashval = be32_to_cpu(
1822 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
1825 * Check if we need a temp buffer, or can we do it in place.
1826 * Note that we don't check "leaf" for holes because we will
1827 * always be dropping it, toosmall() decided that for us already.
1829 if (save_hdr->holes == 0) {
1831 * dest leaf has no holes, so we add there. May need
1832 * to make some room in the entry array.
1834 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1835 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1836 be16_to_cpu(drop_hdr->count), mp);
1838 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1839 be16_to_cpu(save_hdr->count),
1840 be16_to_cpu(drop_hdr->count), mp);
1844 * Destination has holes, so we make a temporary copy
1845 * of the leaf and add them both to that.
1847 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1848 ASSERT(tmpbuffer != NULL);
1849 memset(tmpbuffer, 0, state->blocksize);
1850 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1851 tmp_hdr = &tmp_leaf->hdr;
1852 tmp_hdr->info = save_hdr->info; /* struct copy */
1854 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
1855 if (!tmp_hdr->firstused) {
1856 tmp_hdr->firstused = cpu_to_be16(
1857 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1859 tmp_hdr->usedbytes = 0;
1860 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1861 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1862 be16_to_cpu(drop_hdr->count), mp);
1863 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1864 be16_to_cpu(tmp_leaf->hdr.count),
1865 be16_to_cpu(save_hdr->count), mp);
1867 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1868 be16_to_cpu(save_hdr->count), mp);
1869 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1870 be16_to_cpu(tmp_leaf->hdr.count),
1871 be16_to_cpu(drop_hdr->count), mp);
1873 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1874 kmem_free(tmpbuffer, state->blocksize);
1877 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1878 state->blocksize - 1);
1881 * Copy out last hashval in each block for B-tree code.
1883 save_blk->hashval = be32_to_cpu(
1884 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
1887 /*========================================================================
1888 * Routines used for finding things in the Btree.
1889 *========================================================================*/
1892 * Look up a name in a leaf attribute list structure.
1893 * This is the internal routine, it uses the caller's buffer.
1895 * Note that duplicate keys are allowed, but only check within the
1896 * current leaf node. The Btree code must check in adjacent leaf nodes.
1898 * Return in args->index the index into the entry[] array of either
1899 * the found entry, or where the entry should have been (insert before
1902 * Don't change the args->value unless we find the attribute.
1905 xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1907 xfs_attr_leafblock_t *leaf;
1908 xfs_attr_leaf_entry_t *entry;
1909 xfs_attr_leaf_name_local_t *name_loc;
1910 xfs_attr_leaf_name_remote_t *name_rmt;
1912 xfs_dahash_t hashval;
1915 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1916 ASSERT(be16_to_cpu(leaf->hdr.count)
1917 < (XFS_LBSIZE(args->dp->i_mount)/8));
1920 * Binary search. (note: small blocks will skip this loop)
1922 hashval = args->hashval;
1923 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
1924 for (entry = &leaf->entries[probe]; span > 4;
1925 entry = &leaf->entries[probe]) {
1927 if (be32_to_cpu(entry->hashval) < hashval)
1929 else if (be32_to_cpu(entry->hashval) > hashval)
1934 ASSERT((probe >= 0) &&
1936 || (probe < be16_to_cpu(leaf->hdr.count))));
1937 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
1940 * Since we may have duplicate hashval's, find the first matching
1941 * hashval in the leaf.
1943 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
1947 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1948 (be32_to_cpu(entry->hashval) < hashval)) {
1952 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
1953 (be32_to_cpu(entry->hashval) != hashval)) {
1954 args->index = probe;
1955 return(XFS_ERROR(ENOATTR));
1959 * Duplicate keys may be present, so search all of them for a match.
1961 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
1962 (be32_to_cpu(entry->hashval) == hashval);
1965 * GROT: Add code to remove incomplete entries.
1968 * If we are looking for INCOMPLETE entries, show only those.
1969 * If we are looking for complete entries, show only those.
1971 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
1972 (entry->flags & XFS_ATTR_INCOMPLETE)) {
1975 if (entry->flags & XFS_ATTR_LOCAL) {
1976 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, probe);
1977 if (name_loc->namelen != args->namelen)
1979 if (memcmp(args->name, (char *)name_loc->nameval,
1980 args->namelen) != 0)
1982 if (((args->flags & ATTR_SECURE) != 0) !=
1983 ((entry->flags & XFS_ATTR_SECURE) != 0))
1985 if (((args->flags & ATTR_ROOT) != 0) !=
1986 ((entry->flags & XFS_ATTR_ROOT) != 0))
1988 args->index = probe;
1989 return(XFS_ERROR(EEXIST));
1991 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, probe);
1992 if (name_rmt->namelen != args->namelen)
1994 if (memcmp(args->name, (char *)name_rmt->name,
1995 args->namelen) != 0)
1997 if (((args->flags & ATTR_SECURE) != 0) !=
1998 ((entry->flags & XFS_ATTR_SECURE) != 0))
2000 if (((args->flags & ATTR_ROOT) != 0) !=
2001 ((entry->flags & XFS_ATTR_ROOT) != 0))
2003 args->index = probe;
2004 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2005 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2006 be32_to_cpu(name_rmt->valuelen));
2007 return(XFS_ERROR(EEXIST));
2010 args->index = probe;
2011 return(XFS_ERROR(ENOATTR));
2015 * Get the value associated with an attribute name from a leaf attribute
2019 xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2022 xfs_attr_leafblock_t *leaf;
2023 xfs_attr_leaf_entry_t *entry;
2024 xfs_attr_leaf_name_local_t *name_loc;
2025 xfs_attr_leaf_name_remote_t *name_rmt;
2028 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2029 ASSERT(be16_to_cpu(leaf->hdr.count)
2030 < (XFS_LBSIZE(args->dp->i_mount)/8));
2031 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2033 entry = &leaf->entries[args->index];
2034 if (entry->flags & XFS_ATTR_LOCAL) {
2035 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2036 ASSERT(name_loc->namelen == args->namelen);
2037 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2038 valuelen = be16_to_cpu(name_loc->valuelen);
2039 if (args->flags & ATTR_KERNOVAL) {
2040 args->valuelen = valuelen;
2043 if (args->valuelen < valuelen) {
2044 args->valuelen = valuelen;
2045 return(XFS_ERROR(ERANGE));
2047 args->valuelen = valuelen;
2048 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2050 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2051 ASSERT(name_rmt->namelen == args->namelen);
2052 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2053 valuelen = be32_to_cpu(name_rmt->valuelen);
2054 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2055 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2056 if (args->flags & ATTR_KERNOVAL) {
2057 args->valuelen = valuelen;
2060 if (args->valuelen < valuelen) {
2061 args->valuelen = valuelen;
2062 return(XFS_ERROR(ERANGE));
2064 args->valuelen = valuelen;
2069 /*========================================================================
2071 *========================================================================*/
2074 * Move the indicated entries from one leaf to another.
2075 * NOTE: this routine modifies both source and destination leaves.
2079 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2080 xfs_attr_leafblock_t *leaf_d, int start_d,
2081 int count, xfs_mount_t *mp)
2083 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2084 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2088 * Check for nothing to do.
2094 * Set up environment.
2096 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2097 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2098 hdr_s = &leaf_s->hdr;
2099 hdr_d = &leaf_d->hdr;
2100 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2101 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2102 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2103 ((be16_to_cpu(hdr_s->count)
2104 * sizeof(*entry_s))+sizeof(*hdr_s)));
2105 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2106 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2107 ((be16_to_cpu(hdr_d->count)
2108 * sizeof(*entry_d))+sizeof(*hdr_d)));
2110 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2111 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2112 ASSERT(count <= be16_to_cpu(hdr_s->count));
2115 * Move the entries in the destination leaf up to make a hole?
2117 if (start_d < be16_to_cpu(hdr_d->count)) {
2118 tmp = be16_to_cpu(hdr_d->count) - start_d;
2119 tmp *= sizeof(xfs_attr_leaf_entry_t);
2120 entry_s = &leaf_d->entries[start_d];
2121 entry_d = &leaf_d->entries[start_d + count];
2122 memmove((char *)entry_d, (char *)entry_s, tmp);
2126 * Copy all entry's in the same (sorted) order,
2127 * but allocate attribute info packed and in sequence.
2129 entry_s = &leaf_s->entries[start_s];
2130 entry_d = &leaf_d->entries[start_d];
2132 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2133 ASSERT(be16_to_cpu(entry_s->nameidx)
2134 >= be16_to_cpu(hdr_s->firstused));
2135 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2138 * Code to drop INCOMPLETE entries. Difficult to use as we
2139 * may also need to change the insertion index. Code turned
2140 * off for 6.2, should be revisited later.
2142 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2143 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2144 be16_add(&hdr_s->usedbytes, -tmp);
2145 be16_add(&hdr_s->count, -1);
2146 entry_d--; /* to compensate for ++ in loop hdr */
2148 if ((start_s + i) < offset)
2149 result++; /* insertion index adjustment */
2152 be16_add(&hdr_d->firstused, -tmp);
2153 /* both on-disk, don't endian flip twice */
2154 entry_d->hashval = entry_s->hashval;
2155 /* both on-disk, don't endian flip twice */
2156 entry_d->nameidx = hdr_d->firstused;
2157 entry_d->flags = entry_s->flags;
2158 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2160 memmove(XFS_ATTR_LEAF_NAME(leaf_d, desti),
2161 XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), tmp);
2162 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2164 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2165 be16_add(&hdr_s->usedbytes, -tmp);
2166 be16_add(&hdr_d->usedbytes, tmp);
2167 be16_add(&hdr_s->count, -1);
2168 be16_add(&hdr_d->count, 1);
2169 tmp = be16_to_cpu(hdr_d->count)
2170 * sizeof(xfs_attr_leaf_entry_t)
2171 + sizeof(xfs_attr_leaf_hdr_t);
2172 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
2179 * Zero out the entries we just copied.
2181 if (start_s == be16_to_cpu(hdr_s->count)) {
2182 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2183 entry_s = &leaf_s->entries[start_s];
2184 ASSERT(((char *)entry_s + tmp) <=
2185 ((char *)leaf_s + XFS_LBSIZE(mp)));
2186 memset((char *)entry_s, 0, tmp);
2189 * Move the remaining entries down to fill the hole,
2190 * then zero the entries at the top.
2192 tmp = be16_to_cpu(hdr_s->count) - count;
2193 tmp *= sizeof(xfs_attr_leaf_entry_t);
2194 entry_s = &leaf_s->entries[start_s + count];
2195 entry_d = &leaf_s->entries[start_s];
2196 memmove((char *)entry_d, (char *)entry_s, tmp);
2198 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2199 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
2200 ASSERT(((char *)entry_s + tmp) <=
2201 ((char *)leaf_s + XFS_LBSIZE(mp)));
2202 memset((char *)entry_s, 0, tmp);
2206 * Fill in the freemap information
2208 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2209 be16_add(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2210 sizeof(xfs_attr_leaf_entry_t));
2211 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2212 - be16_to_cpu(hdr_d->freemap[0].base));
2213 hdr_d->freemap[1].base = 0;
2214 hdr_d->freemap[2].base = 0;
2215 hdr_d->freemap[1].size = 0;
2216 hdr_d->freemap[2].size = 0;
2217 hdr_s->holes = 1; /* leaf may not be compact */
2221 * Compare two leaf blocks "order".
2222 * Return 0 unless leaf2 should go before leaf1.
2225 xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2227 xfs_attr_leafblock_t *leaf1, *leaf2;
2229 leaf1 = leaf1_bp->data;
2230 leaf2 = leaf2_bp->data;
2231 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2232 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
2233 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2234 (be16_to_cpu(leaf2->hdr.count) > 0) &&
2235 ((be32_to_cpu(leaf2->entries[0].hashval) <
2236 be32_to_cpu(leaf1->entries[0].hashval)) ||
2237 (be32_to_cpu(leaf2->entries[
2238 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2239 be32_to_cpu(leaf1->entries[
2240 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
2247 * Pick up the last hashvalue from a leaf block.
2250 xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2252 xfs_attr_leafblock_t *leaf;
2255 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2257 *count = be16_to_cpu(leaf->hdr.count);
2258 if (!leaf->hdr.count)
2260 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
2264 * Calculate the number of bytes used to store the indicated attribute
2265 * (whether local or remote only calculate bytes in this block).
2268 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2270 xfs_attr_leaf_name_local_t *name_loc;
2271 xfs_attr_leaf_name_remote_t *name_rmt;
2274 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2275 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2276 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, index);
2277 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc->namelen,
2278 be16_to_cpu(name_loc->valuelen));
2280 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, index);
2281 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt->namelen);
2287 * Calculate the number of bytes that would be required to store the new
2288 * attribute (whether local or remote only calculate bytes in this block).
2289 * This routine decides as a side effect whether the attribute will be
2290 * a "local" or a "remote" attribute.
2293 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2297 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen, valuelen);
2298 if (size < XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize)) {
2303 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen);
2312 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2315 xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2317 attrlist_cursor_kern_t *cursor;
2318 xfs_attr_leafblock_t *leaf;
2319 xfs_attr_leaf_entry_t *entry;
2320 xfs_attr_leaf_name_local_t *name_loc;
2321 xfs_attr_leaf_name_remote_t *name_rmt;
2326 cursor = context->cursor;
2327 cursor->initted = 1;
2329 xfs_attr_trace_l_cl("blk start", context, leaf);
2332 * Re-find our place in the leaf block if this is a new syscall.
2334 if (context->resynch) {
2335 entry = &leaf->entries[0];
2336 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2337 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
2338 if (cursor->offset == context->dupcnt) {
2339 context->dupcnt = 0;
2343 } else if (be32_to_cpu(entry->hashval) >
2345 context->dupcnt = 0;
2349 if (i == be16_to_cpu(leaf->hdr.count)) {
2350 xfs_attr_trace_l_c("not found", context);
2354 entry = &leaf->entries[0];
2357 context->resynch = 0;
2360 * We have found our place, start copying out the new attributes.
2363 for ( ; (i < be16_to_cpu(leaf->hdr.count))
2364 && (retval == 0); entry++, i++) {
2365 attrnames_t *namesp;
2367 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2368 cursor->hashval = be32_to_cpu(entry->hashval);
2372 if (entry->flags & XFS_ATTR_INCOMPLETE)
2373 continue; /* skip incomplete entries */
2374 if (((context->flags & ATTR_SECURE) != 0) !=
2375 ((entry->flags & XFS_ATTR_SECURE) != 0) &&
2376 !(context->flags & ATTR_KERNORMALS))
2377 continue; /* skip non-matching entries */
2378 if (((context->flags & ATTR_ROOT) != 0) !=
2379 ((entry->flags & XFS_ATTR_ROOT) != 0) &&
2380 !(context->flags & ATTR_KERNROOTLS))
2381 continue; /* skip non-matching entries */
2383 namesp = (entry->flags & XFS_ATTR_SECURE) ? &attr_secure :
2384 ((entry->flags & XFS_ATTR_ROOT) ? &attr_trusted :
2387 if (entry->flags & XFS_ATTR_LOCAL) {
2388 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
2389 if (context->flags & ATTR_KERNOVAL) {
2390 ASSERT(context->flags & ATTR_KERNAMELS);
2391 context->count += namesp->attr_namelen +
2392 (int)name_loc->namelen + 1;
2394 retval = xfs_attr_put_listent(context, namesp,
2395 (char *)name_loc->nameval,
2396 (int)name_loc->namelen,
2397 be16_to_cpu(name_loc->valuelen));
2400 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2401 if (context->flags & ATTR_KERNOVAL) {
2402 ASSERT(context->flags & ATTR_KERNAMELS);
2403 context->count += namesp->attr_namelen +
2404 (int)name_rmt->namelen + 1;
2406 retval = xfs_attr_put_listent(context, namesp,
2407 (char *)name_rmt->name,
2408 (int)name_rmt->namelen,
2409 be32_to_cpu(name_rmt->valuelen));
2416 xfs_attr_trace_l_cl("blk end", context, leaf);
2420 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
2421 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
2422 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
2423 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
2424 & ~(sizeof(u_int32_t)-1))
2427 * Format an attribute and copy it out to the user's buffer.
2428 * Take care to check values and protect against them changing later,
2429 * we may be reading them directly out of a user buffer.
2433 xfs_attr_put_listent(xfs_attr_list_context_t *context,
2434 attrnames_t *namesp, char *name, int namelen, int valuelen)
2436 attrlist_ent_t *aep;
2439 ASSERT(!(context->flags & ATTR_KERNOVAL));
2440 if (context->flags & ATTR_KERNAMELS) {
2443 ASSERT(context->count >= 0);
2445 arraytop = context->count + namesp->attr_namelen + namelen + 1;
2446 if (arraytop > context->firstu) {
2447 context->count = -1; /* insufficient space */
2450 offset = (char *)context->alist + context->count;
2451 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
2452 offset += namesp->attr_namelen;
2453 strncpy(offset, name, namelen); /* real name */
2456 context->count += namesp->attr_namelen + namelen + 1;
2460 ASSERT(context->count >= 0);
2461 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
2462 ASSERT(context->firstu >= sizeof(*context->alist));
2463 ASSERT(context->firstu <= context->bufsize);
2465 arraytop = sizeof(*context->alist) +
2466 context->count * sizeof(context->alist->al_offset[0]);
2467 context->firstu -= ATTR_ENTSIZE(namelen);
2468 if (context->firstu < arraytop) {
2469 xfs_attr_trace_l_c("buffer full", context);
2470 context->alist->al_more = 1;
2474 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
2475 aep->a_valuelen = valuelen;
2476 memcpy(aep->a_name, name, namelen);
2477 aep->a_name[ namelen ] = 0;
2478 context->alist->al_offset[ context->count++ ] = context->firstu;
2479 context->alist->al_count = context->count;
2480 xfs_attr_trace_l_c("add", context);
2484 /*========================================================================
2485 * Manage the INCOMPLETE flag in a leaf entry
2486 *========================================================================*/
2489 * Clear the INCOMPLETE flag on an entry in a leaf block.
2492 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2494 xfs_attr_leafblock_t *leaf;
2495 xfs_attr_leaf_entry_t *entry;
2496 xfs_attr_leaf_name_remote_t *name_rmt;
2500 xfs_attr_leaf_name_local_t *name_loc;
2506 * Set up the operation.
2508 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2516 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2517 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2518 ASSERT(args->index >= 0);
2519 entry = &leaf->entries[ args->index ];
2520 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2523 if (entry->flags & XFS_ATTR_LOCAL) {
2524 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2525 namelen = name_loc->namelen;
2526 name = (char *)name_loc->nameval;
2528 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2529 namelen = name_rmt->namelen;
2530 name = (char *)name_rmt->name;
2532 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2533 ASSERT(namelen == args->namelen);
2534 ASSERT(memcmp(name, args->name, namelen) == 0);
2537 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2538 xfs_da_log_buf(args->trans, bp,
2539 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2541 if (args->rmtblkno) {
2542 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2543 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2544 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2545 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2546 xfs_da_log_buf(args->trans, bp,
2547 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2549 xfs_da_buf_done(bp);
2552 * Commit the flag value change and start the next trans in series.
2554 error = xfs_attr_rolltrans(&args->trans, args->dp);
2560 * Set the INCOMPLETE flag on an entry in a leaf block.
2563 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2565 xfs_attr_leafblock_t *leaf;
2566 xfs_attr_leaf_entry_t *entry;
2567 xfs_attr_leaf_name_remote_t *name_rmt;
2572 * Set up the operation.
2574 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2582 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2583 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2584 ASSERT(args->index >= 0);
2585 entry = &leaf->entries[ args->index ];
2587 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2588 entry->flags |= XFS_ATTR_INCOMPLETE;
2589 xfs_da_log_buf(args->trans, bp,
2590 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2591 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2592 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2593 name_rmt->valueblk = 0;
2594 name_rmt->valuelen = 0;
2595 xfs_da_log_buf(args->trans, bp,
2596 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2598 xfs_da_buf_done(bp);
2601 * Commit the flag value change and start the next trans in series.
2603 error = xfs_attr_rolltrans(&args->trans, args->dp);
2609 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2610 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2611 * entry given by args->blkno2/index2.
2613 * Note that they could be in different blocks, or in the same block.
2616 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2618 xfs_attr_leafblock_t *leaf1, *leaf2;
2619 xfs_attr_leaf_entry_t *entry1, *entry2;
2620 xfs_attr_leaf_name_remote_t *name_rmt;
2621 xfs_dabuf_t *bp1, *bp2;
2624 xfs_attr_leaf_name_local_t *name_loc;
2625 int namelen1, namelen2;
2626 char *name1, *name2;
2630 * Read the block containing the "old" attr
2632 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2637 ASSERT(bp1 != NULL);
2640 * Read the block containing the "new" attr, if it is different
2642 if (args->blkno2 != args->blkno) {
2643 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2644 -1, &bp2, XFS_ATTR_FORK);
2648 ASSERT(bp2 != NULL);
2654 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2655 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
2656 ASSERT(args->index >= 0);
2657 entry1 = &leaf1->entries[ args->index ];
2660 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2661 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
2662 ASSERT(args->index2 >= 0);
2663 entry2 = &leaf2->entries[ args->index2 ];
2666 if (entry1->flags & XFS_ATTR_LOCAL) {
2667 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf1, args->index);
2668 namelen1 = name_loc->namelen;
2669 name1 = (char *)name_loc->nameval;
2671 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2672 namelen1 = name_rmt->namelen;
2673 name1 = (char *)name_rmt->name;
2675 if (entry2->flags & XFS_ATTR_LOCAL) {
2676 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf2, args->index2);
2677 namelen2 = name_loc->namelen;
2678 name2 = (char *)name_loc->nameval;
2680 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2681 namelen2 = name_rmt->namelen;
2682 name2 = (char *)name_rmt->name;
2684 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2685 ASSERT(namelen1 == namelen2);
2686 ASSERT(memcmp(name1, name2, namelen1) == 0);
2689 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2690 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2692 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2693 xfs_da_log_buf(args->trans, bp1,
2694 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2695 if (args->rmtblkno) {
2696 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2697 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2698 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2699 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2700 xfs_da_log_buf(args->trans, bp1,
2701 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2704 entry2->flags |= XFS_ATTR_INCOMPLETE;
2705 xfs_da_log_buf(args->trans, bp2,
2706 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2707 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2708 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2709 name_rmt->valueblk = 0;
2710 name_rmt->valuelen = 0;
2711 xfs_da_log_buf(args->trans, bp2,
2712 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2714 xfs_da_buf_done(bp1);
2716 xfs_da_buf_done(bp2);
2719 * Commit the flag value change and start the next trans in series.
2721 error = xfs_attr_rolltrans(&args->trans, args->dp);
2726 /*========================================================================
2727 * Indiscriminately delete the entire attribute fork
2728 *========================================================================*/
2731 * Recurse (gasp!) through the attribute nodes until we find leaves.
2732 * We're doing a depth-first traversal in order to invalidate everything.
2735 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2737 xfs_da_blkinfo_t *info;
2743 * Read block 0 to see what we have to work with.
2744 * We only get here if we have extents, since we remove
2745 * the extents in reverse order the extent containing
2746 * block 0 must still be there.
2748 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2751 blkno = xfs_da_blkno(bp);
2754 * Invalidate the tree, even if the "tree" is only a single leaf block.
2755 * This is a depth-first traversal!
2758 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2759 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2760 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2761 error = xfs_attr_leaf_inactive(trans, dp, bp);
2763 error = XFS_ERROR(EIO);
2764 xfs_da_brelse(*trans, bp);
2770 * Invalidate the incore copy of the root block.
2772 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2775 xfs_da_binval(*trans, bp); /* remove from cache */
2777 * Commit the invalidate and start the next transaction.
2779 error = xfs_attr_rolltrans(trans, dp);
2785 * Recurse (gasp!) through the attribute nodes until we find leaves.
2786 * We're doing a depth-first traversal in order to invalidate everything.
2789 xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2792 xfs_da_blkinfo_t *info;
2793 xfs_da_intnode_t *node;
2794 xfs_dablk_t child_fsb;
2795 xfs_daddr_t parent_blkno, child_blkno;
2796 int error, count, i;
2797 xfs_dabuf_t *child_bp;
2800 * Since this code is recursive (gasp!) we must protect ourselves.
2802 if (level > XFS_DA_NODE_MAXDEPTH) {
2803 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2804 return(XFS_ERROR(EIO));
2808 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
2809 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
2810 count = INT_GET(node->hdr.count, ARCH_CONVERT);
2812 xfs_da_brelse(*trans, bp);
2815 child_fsb = INT_GET(node->btree[0].before, ARCH_CONVERT);
2816 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2819 * If this is the node level just above the leaves, simply loop
2820 * over the leaves removing all of them. If this is higher up
2821 * in the tree, recurse downward.
2823 for (i = 0; i < count; i++) {
2825 * Read the subsidiary block to see what we have to work with.
2826 * Don't do this in a transaction. This is a depth-first
2827 * traversal of the tree so we may deal with many blocks
2828 * before we come back to this one.
2830 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2835 /* save for re-read later */
2836 child_blkno = xfs_da_blkno(child_bp);
2839 * Invalidate the subtree, however we have to.
2841 info = child_bp->data;
2842 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2843 error = xfs_attr_node_inactive(trans, dp,
2845 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2846 error = xfs_attr_leaf_inactive(trans, dp,
2849 error = XFS_ERROR(EIO);
2850 xfs_da_brelse(*trans, child_bp);
2856 * Remove the subsidiary block from the cache
2859 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2860 &child_bp, XFS_ATTR_FORK);
2863 xfs_da_binval(*trans, child_bp);
2867 * If we're not done, re-read the parent to get the next
2868 * child block number.
2870 if ((i+1) < count) {
2871 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2872 &bp, XFS_ATTR_FORK);
2875 child_fsb = INT_GET(node->btree[i+1].before, ARCH_CONVERT);
2876 xfs_da_brelse(*trans, bp);
2879 * Atomically commit the whole invalidate stuff.
2881 if ((error = xfs_attr_rolltrans(trans, dp)))
2889 * Invalidate all of the "remote" value regions pointed to by a particular
2891 * Note that we must release the lock on the buffer so that we are not
2892 * caught holding something that the logging code wants to flush to disk.
2895 xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2897 xfs_attr_leafblock_t *leaf;
2898 xfs_attr_leaf_entry_t *entry;
2899 xfs_attr_leaf_name_remote_t *name_rmt;
2900 xfs_attr_inactive_list_t *list, *lp;
2901 int error, count, size, tmp, i;
2904 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2907 * Count the number of "remote" value extents.
2910 entry = &leaf->entries[0];
2911 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2912 if (be16_to_cpu(entry->nameidx) &&
2913 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2914 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2915 if (name_rmt->valueblk)
2921 * If there are no "remote" values, we're done.
2924 xfs_da_brelse(*trans, bp);
2929 * Allocate storage for a list of all the "remote" value extents.
2931 size = count * sizeof(xfs_attr_inactive_list_t);
2932 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2935 * Identify each of the "remote" value extents.
2938 entry = &leaf->entries[0];
2939 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2940 if (be16_to_cpu(entry->nameidx) &&
2941 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2942 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2943 if (name_rmt->valueblk) {
2944 /* both on-disk, don't endian flip twice */
2945 lp->valueblk = name_rmt->valueblk;
2946 INT_SET(lp->valuelen, ARCH_CONVERT,
2947 XFS_B_TO_FSB(dp->i_mount,
2948 be32_to_cpu(name_rmt->valuelen)));
2953 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2956 * Invalidate each of the "remote" value extents.
2959 for (lp = list, i = 0; i < count; i++, lp++) {
2960 tmp = xfs_attr_leaf_freextent(trans, dp,
2961 INT_GET(lp->valueblk,
2963 INT_GET(lp->valuelen,
2966 error = tmp; /* save only the 1st errno */
2969 kmem_free((xfs_caddr_t)list, size);
2974 * Look at all the extents for this logical region,
2975 * invalidate any buffers that are incore/in transactions.
2978 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2979 xfs_dablk_t blkno, int blkcnt)
2981 xfs_bmbt_irec_t map;
2983 int tblkcnt, dblkcnt, nmap, error;
2988 * Roll through the "value", invalidating the attribute value's
2993 while (tblkcnt > 0) {
2995 * Try to remember where we decided to put the value.
2998 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2999 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
3000 NULL, 0, &map, &nmap, NULL);
3005 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3008 * If it's a hole, these are already unmapped
3009 * so there's nothing to invalidate.
3011 if (map.br_startblock != HOLESTARTBLOCK) {
3013 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3015 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3017 bp = xfs_trans_get_buf(*trans,
3018 dp->i_mount->m_ddev_targp,
3019 dblkno, dblkcnt, XFS_BUF_LOCK);
3020 xfs_trans_binval(*trans, bp);
3022 * Roll to next transaction.
3024 if ((error = xfs_attr_rolltrans(trans, dp)))
3028 tblkno += map.br_blockcount;
3029 tblkcnt -= map.br_blockcount;
3037 * Roll from one trans in the sequence of PERMANENT transactions to the next.
3040 xfs_attr_rolltrans(xfs_trans_t **transp, xfs_inode_t *dp)
3043 unsigned int logres, count;
3047 * Ensure that the inode is always logged.
3050 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
3053 * Copy the critical parameters from one trans to the next.
3055 logres = trans->t_log_res;
3056 count = trans->t_log_count;
3057 *transp = xfs_trans_dup(trans);
3060 * Commit the current transaction.
3061 * If this commit failed, then it'd just unlock those items that
3062 * are not marked ihold. That also means that a filesystem shutdown
3063 * is in progress. The caller takes the responsibility to cancel
3064 * the duplicate transaction that gets returned.
3066 if ((error = xfs_trans_commit(trans, 0, NULL)))
3072 * Reserve space in the log for th next transaction.
3073 * This also pushes items in the "AIL", the list of logged items,
3074 * out to disk if they are taking up space at the tail of the log
3075 * that we want to use. This requires that either nothing be locked
3076 * across this call, or that anything that is locked be logged in
3077 * the prior and the next transactions.
3079 error = xfs_trans_reserve(trans, 0, logres, 0,
3080 XFS_TRANS_PERM_LOG_RES, count);
3082 * Ensure that the inode is in the new transaction and locked.
3085 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
3086 xfs_trans_ihold(trans, dp);