2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 * GROT: figure out how to recover gracefully when bmap returns ENOSPC.
40 #include "xfs_macros.h"
41 #include "xfs_types.h"
44 #include "xfs_trans.h"
49 #include "xfs_dmapi.h"
50 #include "xfs_mount.h"
51 #include "xfs_alloc_btree.h"
52 #include "xfs_bmap_btree.h"
53 #include "xfs_ialloc_btree.h"
54 #include "xfs_alloc.h"
55 #include "xfs_btree.h"
56 #include "xfs_attr_sf.h"
57 #include "xfs_dir_sf.h"
58 #include "xfs_dir2_sf.h"
59 #include "xfs_dinode.h"
60 #include "xfs_inode_item.h"
61 #include "xfs_inode.h"
63 #include "xfs_da_btree.h"
65 #include "xfs_attr_leaf.h"
66 #include "xfs_error.h"
72 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
75 /*========================================================================
76 * Function prototypes for the kernel.
77 *========================================================================*/
80 * Routines used for growing the Btree.
82 STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
84 STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
86 STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
87 STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
88 xfs_da_state_blk_t *blk1,
89 xfs_da_state_blk_t *blk2);
90 STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
91 xfs_da_state_blk_t *leaf_blk_1,
92 xfs_da_state_blk_t *leaf_blk_2,
93 int *number_entries_in_blk1,
94 int *number_usedbytes_in_blk1);
97 * Routines used for shrinking the Btree.
99 STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
100 xfs_dabuf_t *bp, int level);
101 STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
103 STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
104 xfs_dablk_t blkno, int blkcnt);
109 STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
111 xfs_attr_leafblock_t *dst_leaf,
112 int dst_start, int move_count,
114 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
115 STATIC int xfs_attr_put_listent(xfs_attr_list_context_t *context,
116 attrnames_t *, char *name, int namelen,
120 /*========================================================================
121 * External routines when dirsize < XFS_LITINO(mp).
122 *========================================================================*/
125 * Create the initial contents of a shortform attribute list.
128 xfs_attr_shortform_create(xfs_da_args_t *args)
130 xfs_attr_sf_hdr_t *hdr;
138 ASSERT(ifp->if_bytes == 0);
139 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
140 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
141 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
142 ifp->if_flags |= XFS_IFINLINE;
144 ASSERT(ifp->if_flags & XFS_IFINLINE);
146 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
147 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
149 INT_SET(hdr->totsize, ARCH_CONVERT, sizeof(*hdr));
150 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
155 * Add a name/value pair to the shortform attribute list.
156 * Overflow from the inode has already been checked for.
159 xfs_attr_shortform_add(xfs_da_args_t *args)
161 xfs_attr_shortform_t *sf;
162 xfs_attr_sf_entry_t *sfe;
169 ASSERT(ifp->if_flags & XFS_IFINLINE);
170 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
172 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
173 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
174 if (sfe->namelen != args->namelen)
176 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
178 if (((args->flags & ATTR_SECURE) != 0) !=
179 ((sfe->flags & XFS_ATTR_SECURE) != 0))
181 if (((args->flags & ATTR_ROOT) != 0) !=
182 ((sfe->flags & XFS_ATTR_ROOT) != 0))
184 return(XFS_ERROR(EEXIST));
187 offset = (char *)sfe - (char *)sf;
188 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
189 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
190 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
191 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
193 sfe->namelen = args->namelen;
194 INT_SET(sfe->valuelen, ARCH_CONVERT, args->valuelen);
195 sfe->flags = (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
196 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
197 memcpy(sfe->nameval, args->name, args->namelen);
198 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
199 INT_MOD(sf->hdr.count, ARCH_CONVERT, 1);
200 INT_MOD(sf->hdr.totsize, ARCH_CONVERT, size);
201 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
207 * Remove a name from the shortform attribute list structure.
210 xfs_attr_shortform_remove(xfs_da_args_t *args)
212 xfs_attr_shortform_t *sf;
213 xfs_attr_sf_entry_t *sfe;
214 int base, size=0, end, totsize, i;
218 * Remove the attribute.
221 base = sizeof(xfs_attr_sf_hdr_t);
222 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
224 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
225 sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
227 size = XFS_ATTR_SF_ENTSIZE(sfe);
228 if (sfe->namelen != args->namelen)
230 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
232 if (((args->flags & ATTR_SECURE) != 0) !=
233 ((sfe->flags & XFS_ATTR_SECURE) != 0))
235 if (((args->flags & ATTR_ROOT) != 0) !=
236 ((sfe->flags & XFS_ATTR_ROOT) != 0))
240 if (i == INT_GET(sf->hdr.count, ARCH_CONVERT))
241 return(XFS_ERROR(ENOATTR));
244 totsize = INT_GET(sf->hdr.totsize, ARCH_CONVERT);
245 if (end != totsize) {
246 memmove(&((char *)sf)[base], &((char *)sf)[end],
249 INT_MOD(sf->hdr.count, ARCH_CONVERT, -1);
250 INT_MOD(sf->hdr.totsize, ARCH_CONVERT, -size);
251 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
252 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
258 * Look up a name in a shortform attribute list structure.
262 xfs_attr_shortform_lookup(xfs_da_args_t *args)
264 xfs_attr_shortform_t *sf;
265 xfs_attr_sf_entry_t *sfe;
269 ifp = args->dp->i_afp;
270 ASSERT(ifp->if_flags & XFS_IFINLINE);
271 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
273 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
274 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
275 if (sfe->namelen != args->namelen)
277 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
279 if (((args->flags & ATTR_SECURE) != 0) !=
280 ((sfe->flags & XFS_ATTR_SECURE) != 0))
282 if (((args->flags & ATTR_ROOT) != 0) !=
283 ((sfe->flags & XFS_ATTR_ROOT) != 0))
285 return(XFS_ERROR(EEXIST));
287 return(XFS_ERROR(ENOATTR));
291 * Look up a name in a shortform attribute list structure.
295 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
297 xfs_attr_shortform_t *sf;
298 xfs_attr_sf_entry_t *sfe;
301 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
302 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
304 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT);
305 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
306 if (sfe->namelen != args->namelen)
308 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
310 if (((args->flags & ATTR_SECURE) != 0) !=
311 ((sfe->flags & XFS_ATTR_SECURE) != 0))
313 if (((args->flags & ATTR_ROOT) != 0) !=
314 ((sfe->flags & XFS_ATTR_ROOT) != 0))
316 if (args->flags & ATTR_KERNOVAL) {
317 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
318 return(XFS_ERROR(EEXIST));
320 if (args->valuelen < INT_GET(sfe->valuelen, ARCH_CONVERT)) {
321 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
322 return(XFS_ERROR(ERANGE));
324 args->valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
325 memcpy(args->value, &sfe->nameval[args->namelen],
327 return(XFS_ERROR(EEXIST));
329 return(XFS_ERROR(ENOATTR));
333 * Convert from using the shortform to the leaf.
336 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
339 xfs_attr_shortform_t *sf;
340 xfs_attr_sf_entry_t *sfe;
350 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
351 size = INT_GET(sf->hdr.totsize, ARCH_CONVERT);
352 tmpbuffer = kmem_alloc(size, KM_SLEEP);
353 ASSERT(tmpbuffer != NULL);
354 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
355 sf = (xfs_attr_shortform_t *)tmpbuffer;
357 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
359 error = xfs_da_grow_inode(args, &blkno);
362 * If we hit an IO error middle of the transaction inside
363 * grow_inode(), we may have inconsistent data. Bail out.
367 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
368 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
373 error = xfs_attr_leaf_create(args, blkno, &bp);
375 error = xfs_da_shrink_inode(args, 0, bp);
379 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
380 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
384 memset((char *)&nargs, 0, sizeof(nargs));
386 nargs.firstblock = args->firstblock;
387 nargs.flist = args->flist;
388 nargs.total = args->total;
389 nargs.whichfork = XFS_ATTR_FORK;
390 nargs.trans = args->trans;
394 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
395 nargs.name = (char *)sfe->nameval;
396 nargs.namelen = sfe->namelen;
397 nargs.value = (char *)&sfe->nameval[nargs.namelen];
398 nargs.valuelen = INT_GET(sfe->valuelen, ARCH_CONVERT);
399 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
401 nargs.flags = (sfe->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
402 ((sfe->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
403 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
404 ASSERT(error == ENOATTR);
405 error = xfs_attr_leaf_add(bp, &nargs);
406 ASSERT(error != ENOSPC);
409 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
416 kmem_free(tmpbuffer, size);
421 xfs_attr_shortform_compare(const void *a, const void *b)
423 xfs_attr_sf_sort_t *sa, *sb;
425 sa = (xfs_attr_sf_sort_t *)a;
426 sb = (xfs_attr_sf_sort_t *)b;
427 if (INT_GET(sa->hash, ARCH_CONVERT)
428 < INT_GET(sb->hash, ARCH_CONVERT)) {
430 } else if (INT_GET(sa->hash, ARCH_CONVERT)
431 > INT_GET(sb->hash, ARCH_CONVERT)) {
434 return(sa->entno - sb->entno);
439 * Copy out entries of shortform attribute lists for attr_list().
440 * Shortform atrtribute lists are not stored in hashval sorted order.
441 * If the output buffer is not large enough to hold them all, then we
442 * we have to calculate each entries' hashvalue and sort them before
443 * we can begin returning them to the user.
447 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
449 attrlist_cursor_kern_t *cursor;
450 xfs_attr_sf_sort_t *sbuf, *sbp;
451 xfs_attr_shortform_t *sf;
452 xfs_attr_sf_entry_t *sfe;
454 int sbsize, nsbuf, count, i;
456 ASSERT(context != NULL);
459 ASSERT(dp->i_afp != NULL);
460 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
464 cursor = context->cursor;
465 ASSERT(cursor != NULL);
467 xfs_attr_trace_l_c("sf start", context);
470 * If the buffer is large enough, do not bother with sorting.
471 * Note the generous fudge factor of 16 overhead bytes per entry.
473 if ((dp->i_afp->if_bytes + INT_GET(sf->hdr.count, ARCH_CONVERT) * 16)
474 < context->bufsize) {
475 for (i = 0, sfe = &sf->list[0];
476 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
479 if (((context->flags & ATTR_SECURE) != 0) !=
480 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
481 !(context->flags & ATTR_KERNORMALS)) {
482 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
485 if (((context->flags & ATTR_ROOT) != 0) !=
486 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
487 !(context->flags & ATTR_KERNROOTLS)) {
488 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
491 namesp = (sfe->flags & XFS_ATTR_SECURE) ? &attr_secure:
492 ((sfe->flags & XFS_ATTR_ROOT) ? &attr_trusted :
494 if (context->flags & ATTR_KERNOVAL) {
495 ASSERT(context->flags & ATTR_KERNAMELS);
496 context->count += namesp->attr_namelen +
497 INT_GET(sfe->namelen, ARCH_CONVERT) + 1;
500 if (xfs_attr_put_listent(context, namesp,
501 (char *)sfe->nameval,
503 (int)INT_GET(sfe->valuelen,
507 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
509 xfs_attr_trace_l_c("sf big-gulp", context);
514 * It didn't all fit, so we have to sort everything on hashval.
516 sbsize = INT_GET(sf->hdr.count, ARCH_CONVERT) * sizeof(*sbuf);
517 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
520 * Scan the attribute list for the rest of the entries, storing
521 * the relevant info from only those that match into a buffer.
524 for (i = 0, sfe = &sf->list[0];
525 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
527 ((char *)sfe < (char *)sf) ||
528 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
529 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
531 context->dp->i_mount, sfe);
532 xfs_attr_trace_l_c("sf corrupted", context);
533 kmem_free(sbuf, sbsize);
534 return XFS_ERROR(EFSCORRUPTED);
536 if (((context->flags & ATTR_SECURE) != 0) !=
537 ((sfe->flags & XFS_ATTR_SECURE) != 0) &&
538 !(context->flags & ATTR_KERNORMALS)) {
539 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
542 if (((context->flags & ATTR_ROOT) != 0) !=
543 ((sfe->flags & XFS_ATTR_ROOT) != 0) &&
544 !(context->flags & ATTR_KERNROOTLS)) {
545 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
549 INT_SET(sbp->hash, ARCH_CONVERT,
550 xfs_da_hashname((char *)sfe->nameval, sfe->namelen));
551 sbp->name = (char *)sfe->nameval;
552 sbp->namelen = sfe->namelen;
553 /* These are bytes, and both on-disk, don't endian-flip */
554 sbp->valuelen = sfe->valuelen;
555 sbp->flags = sfe->flags;
556 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
562 * Sort the entries on hash then entno.
564 qsort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
567 * Re-find our place IN THE SORTED LIST.
572 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
573 if (INT_GET(sbp->hash, ARCH_CONVERT) == cursor->hashval) {
574 if (cursor->offset == count) {
578 } else if (INT_GET(sbp->hash, ARCH_CONVERT) > cursor->hashval) {
583 kmem_free(sbuf, sbsize);
584 xfs_attr_trace_l_c("blk end", context);
589 * Loop putting entries into the user buffer.
591 for ( ; i < nsbuf; i++, sbp++) {
594 namesp = (sbp->flags & XFS_ATTR_SECURE) ? &attr_secure :
595 ((sbp->flags & XFS_ATTR_ROOT) ? &attr_trusted :
598 if (cursor->hashval != INT_GET(sbp->hash, ARCH_CONVERT)) {
599 cursor->hashval = INT_GET(sbp->hash, ARCH_CONVERT);
602 if (context->flags & ATTR_KERNOVAL) {
603 ASSERT(context->flags & ATTR_KERNAMELS);
604 context->count += namesp->attr_namelen +
607 if (xfs_attr_put_listent(context, namesp,
608 sbp->name, sbp->namelen,
609 INT_GET(sbp->valuelen, ARCH_CONVERT)))
615 kmem_free(sbuf, sbsize);
616 xfs_attr_trace_l_c("sf E-O-F", context);
621 * Check a leaf attribute block to see if all the entries would fit into
622 * a shortform attribute list.
625 xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
627 xfs_attr_leafblock_t *leaf;
628 xfs_attr_leaf_entry_t *entry;
629 xfs_attr_leaf_name_local_t *name_loc;
633 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
634 == XFS_ATTR_LEAF_MAGIC);
636 entry = &leaf->entries[0];
637 bytes = sizeof(struct xfs_attr_sf_hdr);
638 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
639 if (entry->flags & XFS_ATTR_INCOMPLETE)
640 continue; /* don't copy partial entries */
641 if (!(entry->flags & XFS_ATTR_LOCAL))
643 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
644 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
646 if (INT_GET(name_loc->valuelen, ARCH_CONVERT) >= XFS_ATTR_SF_ENTSIZE_MAX)
648 bytes += sizeof(struct xfs_attr_sf_entry)-1
650 + INT_GET(name_loc->valuelen, ARCH_CONVERT);
652 return( bytes < XFS_IFORK_ASIZE(dp) );
656 * Convert a leaf attribute list to shortform attribute list
659 xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args)
661 xfs_attr_leafblock_t *leaf;
662 xfs_attr_leaf_entry_t *entry;
663 xfs_attr_leaf_name_local_t *name_loc;
670 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
671 ASSERT(tmpbuffer != NULL);
674 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
675 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
676 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
677 == XFS_ATTR_LEAF_MAGIC);
678 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
681 * Clean out the prior contents of the attribute list.
683 error = xfs_da_shrink_inode(args, 0, bp);
686 error = xfs_attr_shortform_create(args);
691 * Copy the attributes
693 memset((char *)&nargs, 0, sizeof(nargs));
695 nargs.firstblock = args->firstblock;
696 nargs.flist = args->flist;
697 nargs.total = args->total;
698 nargs.whichfork = XFS_ATTR_FORK;
699 nargs.trans = args->trans;
701 entry = &leaf->entries[0];
702 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
703 if (entry->flags & XFS_ATTR_INCOMPLETE)
704 continue; /* don't copy partial entries */
707 ASSERT(entry->flags & XFS_ATTR_LOCAL);
708 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
709 nargs.name = (char *)name_loc->nameval;
710 nargs.namelen = name_loc->namelen;
711 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
712 nargs.valuelen = INT_GET(name_loc->valuelen, ARCH_CONVERT);
713 nargs.hashval = INT_GET(entry->hashval, ARCH_CONVERT);
714 nargs.flags = (entry->flags & XFS_ATTR_SECURE) ? ATTR_SECURE :
715 ((entry->flags & XFS_ATTR_ROOT) ? ATTR_ROOT : 0);
716 xfs_attr_shortform_add(&nargs);
721 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
726 * Convert from using a single leaf to a root node and a leaf.
729 xfs_attr_leaf_to_node(xfs_da_args_t *args)
731 xfs_attr_leafblock_t *leaf;
732 xfs_da_intnode_t *node;
734 xfs_dabuf_t *bp1, *bp2;
740 error = xfs_da_grow_inode(args, &blkno);
743 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
749 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
754 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
755 xfs_da_buf_done(bp1);
757 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
760 * Set up the new root node.
762 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
767 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
768 == XFS_ATTR_LEAF_MAGIC);
769 /* both on-disk, don't endian-flip twice */
770 node->btree[0].hashval =
771 leaf->entries[INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval;
772 INT_SET(node->btree[0].before, ARCH_CONVERT, blkno);
773 INT_SET(node->hdr.count, ARCH_CONVERT, 1);
774 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
778 xfs_da_buf_done(bp1);
780 xfs_da_buf_done(bp2);
785 /*========================================================================
786 * Routines used for growing the Btree.
787 *========================================================================*/
790 * Create the initial contents of a leaf attribute list
791 * or a leaf in a node attribute list.
794 xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
796 xfs_attr_leafblock_t *leaf;
797 xfs_attr_leaf_hdr_t *hdr;
804 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
810 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
812 INT_SET(hdr->info.magic, ARCH_CONVERT, XFS_ATTR_LEAF_MAGIC);
813 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
814 if (!hdr->firstused) {
815 INT_SET(hdr->firstused, ARCH_CONVERT,
816 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
819 INT_SET(hdr->freemap[0].base, ARCH_CONVERT,
820 sizeof(xfs_attr_leaf_hdr_t));
821 INT_SET(hdr->freemap[0].size, ARCH_CONVERT,
822 INT_GET(hdr->firstused, ARCH_CONVERT)
823 - INT_GET(hdr->freemap[0].base,
826 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
833 * Split the leaf node, rebalance, then add the new entry.
836 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
837 xfs_da_state_blk_t *newblk)
843 * Allocate space for a new leaf node.
845 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
846 error = xfs_da_grow_inode(state->args, &blkno);
849 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
852 newblk->blkno = blkno;
853 newblk->magic = XFS_ATTR_LEAF_MAGIC;
856 * Rebalance the entries across the two leaves.
857 * NOTE: rebalance() currently depends on the 2nd block being empty.
859 xfs_attr_leaf_rebalance(state, oldblk, newblk);
860 error = xfs_da_blk_link(state, oldblk, newblk);
865 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
866 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
867 * "new" attrs info. Will need the "old" info to remove it later.
869 * Insert the "new" entry in the correct block.
872 error = xfs_attr_leaf_add(oldblk->bp, state->args);
874 error = xfs_attr_leaf_add(newblk->bp, state->args);
877 * Update last hashval in each block since we added the name.
879 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
880 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
885 * Add a name to the leaf attribute list structure.
888 xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
890 xfs_attr_leafblock_t *leaf;
891 xfs_attr_leaf_hdr_t *hdr;
892 xfs_attr_leaf_map_t *map;
893 int tablesize, entsize, sum, tmp, i;
896 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
897 == XFS_ATTR_LEAF_MAGIC);
898 ASSERT((args->index >= 0)
899 && (args->index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
901 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
902 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
905 * Search through freemap for first-fit on new name length.
906 * (may need to figure in size of entry struct too)
908 tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1)
909 * sizeof(xfs_attr_leaf_entry_t)
910 + sizeof(xfs_attr_leaf_hdr_t);
911 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
912 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
913 if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
914 sum += INT_GET(map->size, ARCH_CONVERT);
918 continue; /* no space in this map */
920 if (INT_GET(map->base, ARCH_CONVERT)
921 < INT_GET(hdr->firstused, ARCH_CONVERT))
922 tmp += sizeof(xfs_attr_leaf_entry_t);
923 if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
924 tmp = xfs_attr_leaf_add_work(bp, args, i);
927 sum += INT_GET(map->size, ARCH_CONVERT);
931 * If there are no holes in the address space of the block,
932 * and we don't have enough freespace, then compaction will do us
933 * no good and we should just give up.
935 if (!hdr->holes && (sum < entsize))
936 return(XFS_ERROR(ENOSPC));
939 * Compact the entries to coalesce free space.
940 * This may change the hdr->count via dropping INCOMPLETE entries.
942 xfs_attr_leaf_compact(args->trans, bp);
945 * After compaction, the block is guaranteed to have only one
946 * free region, in freemap[0]. If it is not big enough, give up.
948 if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT)
949 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
950 return(XFS_ERROR(ENOSPC));
952 return(xfs_attr_leaf_add_work(bp, args, 0));
956 * Add a name to a leaf attribute list structure.
959 xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
961 xfs_attr_leafblock_t *leaf;
962 xfs_attr_leaf_hdr_t *hdr;
963 xfs_attr_leaf_entry_t *entry;
964 xfs_attr_leaf_name_local_t *name_loc;
965 xfs_attr_leaf_name_remote_t *name_rmt;
966 xfs_attr_leaf_map_t *map;
971 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
972 == XFS_ATTR_LEAF_MAGIC);
974 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
975 ASSERT((args->index >= 0)
976 && (args->index <= INT_GET(hdr->count, ARCH_CONVERT)));
979 * Force open some space in the entry array and fill it in.
981 entry = &leaf->entries[args->index];
982 if (args->index < INT_GET(hdr->count, ARCH_CONVERT)) {
983 tmp = INT_GET(hdr->count, ARCH_CONVERT) - args->index;
984 tmp *= sizeof(xfs_attr_leaf_entry_t);
985 memmove((char *)(entry+1), (char *)entry, tmp);
986 xfs_da_log_buf(args->trans, bp,
987 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
989 INT_MOD(hdr->count, ARCH_CONVERT, 1);
992 * Allocate space for the new string (at the end of the run).
994 map = &hdr->freemap[mapindex];
995 mp = args->trans->t_mountp;
996 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
997 ASSERT((INT_GET(map->base, ARCH_CONVERT) & 0x3) == 0);
998 ASSERT(INT_GET(map->size, ARCH_CONVERT) >=
999 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1000 mp->m_sb.sb_blocksize, NULL));
1001 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1002 ASSERT((INT_GET(map->size, ARCH_CONVERT) & 0x3) == 0);
1003 INT_MOD(map->size, ARCH_CONVERT,
1004 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1005 mp->m_sb.sb_blocksize, &tmp));
1006 INT_SET(entry->nameidx, ARCH_CONVERT,
1007 INT_GET(map->base, ARCH_CONVERT)
1008 + INT_GET(map->size, ARCH_CONVERT));
1009 INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
1010 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1011 entry->flags |= (args->flags & ATTR_SECURE) ? XFS_ATTR_SECURE :
1012 ((args->flags & ATTR_ROOT) ? XFS_ATTR_ROOT : 0);
1014 entry->flags |= XFS_ATTR_INCOMPLETE;
1015 if ((args->blkno2 == args->blkno) &&
1016 (args->index2 <= args->index)) {
1020 xfs_da_log_buf(args->trans, bp,
1021 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1022 ASSERT((args->index == 0) || (INT_GET(entry->hashval, ARCH_CONVERT)
1023 >= INT_GET((entry-1)->hashval,
1025 ASSERT((args->index == INT_GET(hdr->count, ARCH_CONVERT)-1) ||
1026 (INT_GET(entry->hashval, ARCH_CONVERT)
1027 <= (INT_GET((entry+1)->hashval, ARCH_CONVERT))));
1030 * Copy the attribute name and value into the new space.
1032 * For "remote" attribute values, simply note that we need to
1033 * allocate space for the "remote" value. We can't actually
1034 * allocate the extents in this transaction, and we can't decide
1035 * which blocks they should be as we might allocate more blocks
1036 * as part of this transaction (a split operation for example).
1038 if (entry->flags & XFS_ATTR_LOCAL) {
1039 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1040 name_loc->namelen = args->namelen;
1041 INT_SET(name_loc->valuelen, ARCH_CONVERT, args->valuelen);
1042 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1043 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1044 INT_GET(name_loc->valuelen, ARCH_CONVERT));
1046 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
1047 name_rmt->namelen = args->namelen;
1048 memcpy((char *)name_rmt->name, args->name, args->namelen);
1049 entry->flags |= XFS_ATTR_INCOMPLETE;
1051 name_rmt->valuelen = 0;
1052 name_rmt->valueblk = 0;
1054 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1056 xfs_da_log_buf(args->trans, bp,
1057 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1058 xfs_attr_leaf_entsize(leaf, args->index)));
1061 * Update the control info for this leaf node
1063 if (INT_GET(entry->nameidx, ARCH_CONVERT)
1064 < INT_GET(hdr->firstused, ARCH_CONVERT)) {
1065 /* both on-disk, don't endian-flip twice */
1066 hdr->firstused = entry->nameidx;
1068 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT)
1069 >= ((INT_GET(hdr->count, ARCH_CONVERT)
1070 * sizeof(*entry))+sizeof(*hdr)));
1071 tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1)
1072 * sizeof(xfs_attr_leaf_entry_t)
1073 + sizeof(xfs_attr_leaf_hdr_t);
1074 map = &hdr->freemap[0];
1075 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1076 if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
1077 INT_MOD(map->base, ARCH_CONVERT,
1078 sizeof(xfs_attr_leaf_entry_t));
1079 INT_MOD(map->size, ARCH_CONVERT,
1080 -sizeof(xfs_attr_leaf_entry_t));
1083 INT_MOD(hdr->usedbytes, ARCH_CONVERT,
1084 xfs_attr_leaf_entsize(leaf, args->index));
1085 xfs_da_log_buf(args->trans, bp,
1086 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1091 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1094 xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1096 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1097 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1101 mp = trans->t_mountp;
1102 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1103 ASSERT(tmpbuffer != NULL);
1104 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1105 memset(bp->data, 0, XFS_LBSIZE(mp));
1108 * Copy basic information
1110 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1112 hdr_s = &leaf_s->hdr;
1113 hdr_d = &leaf_d->hdr;
1114 hdr_d->info = hdr_s->info; /* struct copy */
1115 INT_SET(hdr_d->firstused, ARCH_CONVERT, XFS_LBSIZE(mp));
1116 /* handle truncation gracefully */
1117 if (!hdr_d->firstused) {
1118 INT_SET(hdr_d->firstused, ARCH_CONVERT,
1119 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1121 hdr_d->usedbytes = 0;
1124 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT,
1125 sizeof(xfs_attr_leaf_hdr_t));
1126 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT,
1127 INT_GET(hdr_d->firstused, ARCH_CONVERT)
1128 - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
1131 * Copy all entry's in the same (sorted) order,
1132 * but allocate name/value pairs packed and in sequence.
1134 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1135 (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
1137 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1139 kmem_free(tmpbuffer, XFS_LBSIZE(mp));
1143 * Redistribute the attribute list entries between two leaf nodes,
1144 * taking into account the size of the new entry.
1146 * NOTE: if new block is empty, then it will get the upper half of the
1147 * old block. At present, all (one) callers pass in an empty second block.
1149 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1150 * to match what it is doing in splitting the attribute leaf block. Those
1151 * values are used in "atomic rename" operations on attributes. Note that
1152 * the "new" and "old" values can end up in different blocks.
1155 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1156 xfs_da_state_blk_t *blk2)
1158 xfs_da_args_t *args;
1159 xfs_da_state_blk_t *tmp_blk;
1160 xfs_attr_leafblock_t *leaf1, *leaf2;
1161 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1162 int count, totallen, max, space, swap;
1165 * Set up environment.
1167 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1168 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1169 leaf1 = blk1->bp->data;
1170 leaf2 = blk2->bp->data;
1171 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
1172 == XFS_ATTR_LEAF_MAGIC);
1173 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
1174 == XFS_ATTR_LEAF_MAGIC);
1178 * Check ordering of blocks, reverse if it makes things simpler.
1180 * NOTE: Given that all (current) callers pass in an empty
1181 * second block, this code should never set "swap".
1184 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1188 leaf1 = blk1->bp->data;
1189 leaf2 = blk2->bp->data;
1196 * Examine entries until we reduce the absolute difference in
1197 * byte usage between the two blocks to a minimum. Then get
1198 * the direction to copy and the number of elements to move.
1200 * "inleaf" is true if the new entry should be inserted into blk1.
1201 * If "swap" is also true, then reverse the sense of "inleaf".
1203 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1206 state->inleaf = !state->inleaf;
1209 * Move any entries required from leaf to leaf:
1211 if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
1213 * Figure the total bytes to be added to the destination leaf.
1215 /* number entries being moved */
1216 count = INT_GET(hdr1->count, ARCH_CONVERT) - count;
1217 space = INT_GET(hdr1->usedbytes, ARCH_CONVERT) - totallen;
1218 space += count * sizeof(xfs_attr_leaf_entry_t);
1221 * leaf2 is the destination, compact it if it looks tight.
1223 max = INT_GET(hdr2->firstused, ARCH_CONVERT)
1224 - sizeof(xfs_attr_leaf_hdr_t);
1225 max -= INT_GET(hdr2->count, ARCH_CONVERT)
1226 * sizeof(xfs_attr_leaf_entry_t);
1228 xfs_attr_leaf_compact(args->trans, blk2->bp);
1232 * Move high entries from leaf1 to low end of leaf2.
1234 xfs_attr_leaf_moveents(leaf1,
1235 INT_GET(hdr1->count, ARCH_CONVERT)-count,
1236 leaf2, 0, count, state->mp);
1238 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1239 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1240 } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
1242 * I assert that since all callers pass in an empty
1243 * second buffer, this code should never execute.
1247 * Figure the total bytes to be added to the destination leaf.
1249 /* number entries being moved */
1250 count -= INT_GET(hdr1->count, ARCH_CONVERT);
1251 space = totallen - INT_GET(hdr1->usedbytes, ARCH_CONVERT);
1252 space += count * sizeof(xfs_attr_leaf_entry_t);
1255 * leaf1 is the destination, compact it if it looks tight.
1257 max = INT_GET(hdr1->firstused, ARCH_CONVERT)
1258 - sizeof(xfs_attr_leaf_hdr_t);
1259 max -= INT_GET(hdr1->count, ARCH_CONVERT)
1260 * sizeof(xfs_attr_leaf_entry_t);
1262 xfs_attr_leaf_compact(args->trans, blk1->bp);
1266 * Move low entries from leaf2 to high end of leaf1.
1268 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1269 (int)INT_GET(hdr1->count, ARCH_CONVERT), count,
1272 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1273 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1277 * Copy out last hashval in each block for B-tree code.
1280 INT_GET(leaf1->entries[INT_GET(leaf1->hdr.count,
1281 ARCH_CONVERT)-1].hashval, ARCH_CONVERT);
1283 INT_GET(leaf2->entries[INT_GET(leaf2->hdr.count,
1284 ARCH_CONVERT)-1].hashval, ARCH_CONVERT);
1287 * Adjust the expected index for insertion.
1288 * NOTE: this code depends on the (current) situation that the
1289 * second block was originally empty.
1291 * If the insertion point moved to the 2nd block, we must adjust
1292 * the index. We must also track the entry just following the
1293 * new entry for use in an "atomic rename" operation, that entry
1294 * is always the "old" entry and the "new" entry is what we are
1295 * inserting. The index/blkno fields refer to the "old" entry,
1296 * while the index2/blkno2 fields refer to the "new" entry.
1298 if (blk1->index > INT_GET(leaf1->hdr.count, ARCH_CONVERT)) {
1299 ASSERT(state->inleaf == 0);
1300 blk2->index = blk1->index
1301 - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1302 args->index = args->index2 = blk2->index;
1303 args->blkno = args->blkno2 = blk2->blkno;
1304 } else if (blk1->index == INT_GET(leaf1->hdr.count, ARCH_CONVERT)) {
1305 if (state->inleaf) {
1306 args->index = blk1->index;
1307 args->blkno = blk1->blkno;
1309 args->blkno2 = blk2->blkno;
1311 blk2->index = blk1->index
1312 - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1313 args->index = args->index2 = blk2->index;
1314 args->blkno = args->blkno2 = blk2->blkno;
1317 ASSERT(state->inleaf == 1);
1318 args->index = args->index2 = blk1->index;
1319 args->blkno = args->blkno2 = blk1->blkno;
1324 * Examine entries until we reduce the absolute difference in
1325 * byte usage between the two blocks to a minimum.
1326 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1327 * GROT: there will always be enough room in either block for a new entry.
1328 * GROT: Do a double-split for this case?
1331 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1332 xfs_da_state_blk_t *blk1,
1333 xfs_da_state_blk_t *blk2,
1334 int *countarg, int *usedbytesarg)
1336 xfs_attr_leafblock_t *leaf1, *leaf2;
1337 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1338 xfs_attr_leaf_entry_t *entry;
1339 int count, max, index, totallen, half;
1340 int lastdelta, foundit, tmp;
1343 * Set up environment.
1345 leaf1 = blk1->bp->data;
1346 leaf2 = blk2->bp->data;
1353 * Examine entries until we reduce the absolute difference in
1354 * byte usage between the two blocks to a minimum.
1356 max = INT_GET(hdr1->count, ARCH_CONVERT)
1357 + INT_GET(hdr2->count, ARCH_CONVERT);
1358 half = (max+1) * sizeof(*entry);
1359 half += INT_GET(hdr1->usedbytes, ARCH_CONVERT)
1360 + INT_GET(hdr2->usedbytes, ARCH_CONVERT)
1361 + xfs_attr_leaf_newentsize(
1362 state->args->namelen,
1363 state->args->valuelen,
1364 state->blocksize, NULL);
1366 lastdelta = state->blocksize;
1367 entry = &leaf1->entries[0];
1368 for (count = index = 0; count < max; entry++, index++, count++) {
1370 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1372 * The new entry is in the first block, account for it.
1374 if (count == blk1->index) {
1375 tmp = totallen + sizeof(*entry) +
1376 xfs_attr_leaf_newentsize(
1377 state->args->namelen,
1378 state->args->valuelen,
1379 state->blocksize, NULL);
1380 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1382 lastdelta = XFS_ATTR_ABS(half - tmp);
1388 * Wrap around into the second block if necessary.
1390 if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
1392 entry = &leaf1->entries[0];
1397 * Figure out if next leaf entry would be too much.
1399 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1401 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1403 lastdelta = XFS_ATTR_ABS(half - tmp);
1409 * Calculate the number of usedbytes that will end up in lower block.
1410 * If new entry not in lower block, fix up the count.
1412 totallen -= count * sizeof(*entry);
1414 totallen -= sizeof(*entry) +
1415 xfs_attr_leaf_newentsize(
1416 state->args->namelen,
1417 state->args->valuelen,
1418 state->blocksize, NULL);
1422 *usedbytesarg = totallen;
1426 /*========================================================================
1427 * Routines used for shrinking the Btree.
1428 *========================================================================*/
1431 * Check a leaf block and its neighbors to see if the block should be
1432 * collapsed into one or the other neighbor. Always keep the block
1433 * with the smaller block number.
1434 * If the current block is over 50% full, don't try to join it, return 0.
1435 * If the block is empty, fill in the state structure and return 2.
1436 * If it can be collapsed, fill in the state structure and return 1.
1437 * If nothing can be done, return 0.
1439 * GROT: allow for INCOMPLETE entries in calculation.
1442 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1444 xfs_attr_leafblock_t *leaf;
1445 xfs_da_state_blk_t *blk;
1446 xfs_da_blkinfo_t *info;
1447 int count, bytes, forward, error, retval, i;
1452 * Check for the degenerate case of the block being over 50% full.
1453 * If so, it's not worth even looking to see if we might be able
1454 * to coalesce with a sibling.
1456 blk = &state->path.blk[ state->path.active-1 ];
1457 info = blk->bp->data;
1458 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
1459 leaf = (xfs_attr_leafblock_t *)info;
1460 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1461 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1462 count * sizeof(xfs_attr_leaf_entry_t) +
1463 INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1464 if (bytes > (state->blocksize >> 1)) {
1465 *action = 0; /* blk over 50%, don't try to join */
1470 * Check for the degenerate case of the block being empty.
1471 * If the block is empty, we'll simply delete it, no need to
1472 * coalesce it with a sibling block. We choose (aribtrarily)
1473 * to merge with the forward block unless it is NULL.
1477 * Make altpath point to the block we want to keep and
1478 * path point to the block we want to drop (this one).
1480 forward = info->forw;
1481 memcpy(&state->altpath, &state->path, sizeof(state->path));
1482 error = xfs_da_path_shift(state, &state->altpath, forward,
1495 * Examine each sibling block to see if we can coalesce with
1496 * at least 25% free space to spare. We need to figure out
1497 * whether to merge with the forward or the backward block.
1498 * We prefer coalescing with the lower numbered sibling so as
1499 * to shrink an attribute list over time.
1501 /* start with smaller blk num */
1502 forward = (INT_GET(info->forw, ARCH_CONVERT)
1503 < INT_GET(info->back, ARCH_CONVERT));
1504 for (i = 0; i < 2; forward = !forward, i++) {
1506 blkno = INT_GET(info->forw, ARCH_CONVERT);
1508 blkno = INT_GET(info->back, ARCH_CONVERT);
1511 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1512 blkno, -1, &bp, XFS_ATTR_FORK);
1517 leaf = (xfs_attr_leafblock_t *)info;
1518 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1519 bytes = state->blocksize - (state->blocksize>>2);
1520 bytes -= INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1522 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1523 == XFS_ATTR_LEAF_MAGIC);
1524 count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
1525 bytes -= INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1526 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1527 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1528 xfs_da_brelse(state->args->trans, bp);
1530 break; /* fits with at least 25% to spare */
1538 * Make altpath point to the block we want to keep (the lower
1539 * numbered block) and path point to the block we want to drop.
1541 memcpy(&state->altpath, &state->path, sizeof(state->path));
1542 if (blkno < blk->blkno) {
1543 error = xfs_da_path_shift(state, &state->altpath, forward,
1546 error = xfs_da_path_shift(state, &state->path, forward,
1560 * Remove a name from the leaf attribute list structure.
1562 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1563 * If two leaves are 37% full, when combined they will leave 25% free.
1566 xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1568 xfs_attr_leafblock_t *leaf;
1569 xfs_attr_leaf_hdr_t *hdr;
1570 xfs_attr_leaf_map_t *map;
1571 xfs_attr_leaf_entry_t *entry;
1572 int before, after, smallest, entsize;
1573 int tablesize, tmp, i;
1577 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1578 == XFS_ATTR_LEAF_MAGIC);
1580 mp = args->trans->t_mountp;
1581 ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0)
1582 && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1583 ASSERT((args->index >= 0)
1584 && (args->index < INT_GET(hdr->count, ARCH_CONVERT)));
1585 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT)
1586 >= ((INT_GET(hdr->count, ARCH_CONVERT)
1587 * sizeof(*entry))+sizeof(*hdr)));
1588 entry = &leaf->entries[args->index];
1589 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1590 >= INT_GET(hdr->firstused, ARCH_CONVERT));
1591 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1594 * Scan through free region table:
1595 * check for adjacency of free'd entry with an existing one,
1596 * find smallest free region in case we need to replace it,
1597 * adjust any map that borders the entry table,
1599 tablesize = INT_GET(hdr->count, ARCH_CONVERT)
1600 * sizeof(xfs_attr_leaf_entry_t)
1601 + sizeof(xfs_attr_leaf_hdr_t);
1602 map = &hdr->freemap[0];
1603 tmp = INT_GET(map->size, ARCH_CONVERT);
1604 before = after = -1;
1605 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1606 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1607 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1608 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1609 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1610 if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
1611 INT_MOD(map->base, ARCH_CONVERT,
1612 -sizeof(xfs_attr_leaf_entry_t));
1613 INT_MOD(map->size, ARCH_CONVERT,
1614 sizeof(xfs_attr_leaf_entry_t));
1617 if ((INT_GET(map->base, ARCH_CONVERT)
1618 + INT_GET(map->size, ARCH_CONVERT))
1619 == INT_GET(entry->nameidx, ARCH_CONVERT)) {
1621 } else if (INT_GET(map->base, ARCH_CONVERT)
1622 == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
1624 } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
1625 tmp = INT_GET(map->size, ARCH_CONVERT);
1631 * Coalesce adjacent freemap regions,
1632 * or replace the smallest region.
1634 if ((before >= 0) || (after >= 0)) {
1635 if ((before >= 0) && (after >= 0)) {
1636 map = &hdr->freemap[before];
1637 INT_MOD(map->size, ARCH_CONVERT, entsize);
1638 INT_MOD(map->size, ARCH_CONVERT,
1639 INT_GET(hdr->freemap[after].size,
1641 hdr->freemap[after].base = 0;
1642 hdr->freemap[after].size = 0;
1643 } else if (before >= 0) {
1644 map = &hdr->freemap[before];
1645 INT_MOD(map->size, ARCH_CONVERT, entsize);
1647 map = &hdr->freemap[after];
1648 /* both on-disk, don't endian flip twice */
1649 map->base = entry->nameidx;
1650 INT_MOD(map->size, ARCH_CONVERT, entsize);
1654 * Replace smallest region (if it is smaller than free'd entry)
1656 map = &hdr->freemap[smallest];
1657 if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
1658 INT_SET(map->base, ARCH_CONVERT,
1659 INT_GET(entry->nameidx, ARCH_CONVERT));
1660 INT_SET(map->size, ARCH_CONVERT, entsize);
1665 * Did we remove the first entry?
1667 if (INT_GET(entry->nameidx, ARCH_CONVERT)
1668 == INT_GET(hdr->firstused, ARCH_CONVERT))
1674 * Compress the remaining entries and zero out the removed stuff.
1676 memset(XFS_ATTR_LEAF_NAME(leaf, args->index), 0, entsize);
1677 INT_MOD(hdr->usedbytes, ARCH_CONVERT, -entsize);
1678 xfs_da_log_buf(args->trans, bp,
1679 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1682 tmp = (INT_GET(hdr->count, ARCH_CONVERT) - args->index)
1683 * sizeof(xfs_attr_leaf_entry_t);
1684 memmove((char *)entry, (char *)(entry+1), tmp);
1685 INT_MOD(hdr->count, ARCH_CONVERT, -1);
1686 xfs_da_log_buf(args->trans, bp,
1687 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1688 entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
1689 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1692 * If we removed the first entry, re-find the first used byte
1693 * in the name area. Note that if the entry was the "firstused",
1694 * then we don't have a "hole" in our block resulting from
1695 * removing the name.
1698 tmp = XFS_LBSIZE(mp);
1699 entry = &leaf->entries[0];
1700 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1;
1701 i >= 0; entry++, i--) {
1702 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1703 >= INT_GET(hdr->firstused, ARCH_CONVERT));
1704 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT)
1706 if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
1707 tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
1709 INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
1710 if (!hdr->firstused) {
1711 INT_SET(hdr->firstused, ARCH_CONVERT,
1712 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1715 hdr->holes = 1; /* mark as needing compaction */
1717 xfs_da_log_buf(args->trans, bp,
1718 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1721 * Check if leaf is less than 50% full, caller may want to
1722 * "join" the leaf with a sibling if so.
1724 tmp = sizeof(xfs_attr_leaf_hdr_t);
1725 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT)
1726 * sizeof(xfs_attr_leaf_entry_t);
1727 tmp += INT_GET(leaf->hdr.usedbytes, ARCH_CONVERT);
1728 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1732 * Move all the attribute list entries from drop_leaf into save_leaf.
1735 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1736 xfs_da_state_blk_t *save_blk)
1738 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1739 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1744 * Set up environment.
1747 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1748 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1749 drop_leaf = drop_blk->bp->data;
1750 save_leaf = save_blk->bp->data;
1751 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT)
1752 == XFS_ATTR_LEAF_MAGIC);
1753 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT)
1754 == XFS_ATTR_LEAF_MAGIC);
1755 drop_hdr = &drop_leaf->hdr;
1756 save_hdr = &save_leaf->hdr;
1759 * Save last hashval from dying block for later Btree fixup.
1762 INT_GET(drop_leaf->entries[INT_GET(drop_leaf->hdr.count,
1763 ARCH_CONVERT)-1].hashval,
1767 * Check if we need a temp buffer, or can we do it in place.
1768 * Note that we don't check "leaf" for holes because we will
1769 * always be dropping it, toosmall() decided that for us already.
1771 if (save_hdr->holes == 0) {
1773 * dest leaf has no holes, so we add there. May need
1774 * to make some room in the entry array.
1776 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1777 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1778 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1780 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1781 INT_GET(save_hdr->count, ARCH_CONVERT),
1782 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1787 * Destination has holes, so we make a temporary copy
1788 * of the leaf and add them both to that.
1790 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1791 ASSERT(tmpbuffer != NULL);
1792 memset(tmpbuffer, 0, state->blocksize);
1793 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1794 tmp_hdr = &tmp_leaf->hdr;
1795 tmp_hdr->info = save_hdr->info; /* struct copy */
1797 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
1798 if (!tmp_hdr->firstused) {
1799 INT_SET(tmp_hdr->firstused, ARCH_CONVERT,
1800 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1802 tmp_hdr->usedbytes = 0;
1803 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1804 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1805 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1807 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1808 INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1809 (int)INT_GET(save_hdr->count, ARCH_CONVERT),
1812 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1813 (int)INT_GET(save_hdr->count, ARCH_CONVERT),
1815 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1816 INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1817 (int)INT_GET(drop_hdr->count, ARCH_CONVERT),
1820 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1821 kmem_free(tmpbuffer, state->blocksize);
1824 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1825 state->blocksize - 1);
1828 * Copy out last hashval in each block for B-tree code.
1831 INT_GET(save_leaf->entries[INT_GET(save_leaf->hdr.count,
1832 ARCH_CONVERT)-1].hashval,
1836 /*========================================================================
1837 * Routines used for finding things in the Btree.
1838 *========================================================================*/
1841 * Look up a name in a leaf attribute list structure.
1842 * This is the internal routine, it uses the caller's buffer.
1844 * Note that duplicate keys are allowed, but only check within the
1845 * current leaf node. The Btree code must check in adjacent leaf nodes.
1847 * Return in args->index the index into the entry[] array of either
1848 * the found entry, or where the entry should have been (insert before
1851 * Don't change the args->value unless we find the attribute.
1854 xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1856 xfs_attr_leafblock_t *leaf;
1857 xfs_attr_leaf_entry_t *entry;
1858 xfs_attr_leaf_name_local_t *name_loc;
1859 xfs_attr_leaf_name_remote_t *name_rmt;
1861 xfs_dahash_t hashval;
1864 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1865 == XFS_ATTR_LEAF_MAGIC);
1866 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT)
1867 < (XFS_LBSIZE(args->dp->i_mount)/8));
1870 * Binary search. (note: small blocks will skip this loop)
1872 hashval = args->hashval;
1873 probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
1874 for (entry = &leaf->entries[probe]; span > 4;
1875 entry = &leaf->entries[probe]) {
1877 if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
1879 else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
1884 ASSERT((probe >= 0) &&
1886 || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
1887 ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT)
1891 * Since we may have duplicate hashval's, find the first matching
1892 * hashval in the leaf.
1894 while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT)
1899 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))
1900 && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
1904 if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT))
1905 || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
1906 args->index = probe;
1907 return(XFS_ERROR(ENOATTR));
1911 * Duplicate keys may be present, so search all of them for a match.
1913 for ( ; (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))
1914 && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval);
1917 * GROT: Add code to remove incomplete entries.
1920 * If we are looking for INCOMPLETE entries, show only those.
1921 * If we are looking for complete entries, show only those.
1923 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
1924 (entry->flags & XFS_ATTR_INCOMPLETE)) {
1927 if (entry->flags & XFS_ATTR_LOCAL) {
1928 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, probe);
1929 if (name_loc->namelen != args->namelen)
1931 if (memcmp(args->name, (char *)name_loc->nameval,
1932 args->namelen) != 0)
1934 if (((args->flags & ATTR_SECURE) != 0) !=
1935 ((entry->flags & XFS_ATTR_SECURE) != 0))
1937 if (((args->flags & ATTR_ROOT) != 0) !=
1938 ((entry->flags & XFS_ATTR_ROOT) != 0))
1940 args->index = probe;
1941 return(XFS_ERROR(EEXIST));
1943 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, probe);
1944 if (name_rmt->namelen != args->namelen)
1946 if (memcmp(args->name, (char *)name_rmt->name,
1947 args->namelen) != 0)
1949 if (((args->flags & ATTR_SECURE) != 0) !=
1950 ((entry->flags & XFS_ATTR_SECURE) != 0))
1952 if (((args->flags & ATTR_ROOT) != 0) !=
1953 ((entry->flags & XFS_ATTR_ROOT) != 0))
1955 args->index = probe;
1957 = INT_GET(name_rmt->valueblk, ARCH_CONVERT);
1958 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
1959 INT_GET(name_rmt->valuelen,
1961 return(XFS_ERROR(EEXIST));
1964 args->index = probe;
1965 return(XFS_ERROR(ENOATTR));
1969 * Get the value associated with an attribute name from a leaf attribute
1973 xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
1976 xfs_attr_leafblock_t *leaf;
1977 xfs_attr_leaf_entry_t *entry;
1978 xfs_attr_leaf_name_local_t *name_loc;
1979 xfs_attr_leaf_name_remote_t *name_rmt;
1982 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1983 == XFS_ATTR_LEAF_MAGIC);
1984 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT)
1985 < (XFS_LBSIZE(args->dp->i_mount)/8));
1986 ASSERT(args->index < ((int)INT_GET(leaf->hdr.count, ARCH_CONVERT)));
1988 entry = &leaf->entries[args->index];
1989 if (entry->flags & XFS_ATTR_LOCAL) {
1990 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1991 ASSERT(name_loc->namelen == args->namelen);
1992 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
1993 valuelen = INT_GET(name_loc->valuelen, ARCH_CONVERT);
1994 if (args->flags & ATTR_KERNOVAL) {
1995 args->valuelen = valuelen;
1998 if (args->valuelen < valuelen) {
1999 args->valuelen = valuelen;
2000 return(XFS_ERROR(ERANGE));
2002 args->valuelen = valuelen;
2003 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2005 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2006 ASSERT(name_rmt->namelen == args->namelen);
2007 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2008 valuelen = INT_GET(name_rmt->valuelen, ARCH_CONVERT);
2009 args->rmtblkno = INT_GET(name_rmt->valueblk, ARCH_CONVERT);
2010 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2011 if (args->flags & ATTR_KERNOVAL) {
2012 args->valuelen = valuelen;
2015 if (args->valuelen < valuelen) {
2016 args->valuelen = valuelen;
2017 return(XFS_ERROR(ERANGE));
2019 args->valuelen = valuelen;
2024 /*========================================================================
2026 *========================================================================*/
2029 * Move the indicated entries from one leaf to another.
2030 * NOTE: this routine modifies both source and destination leaves.
2034 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2035 xfs_attr_leafblock_t *leaf_d, int start_d,
2036 int count, xfs_mount_t *mp)
2038 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2039 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2043 * Check for nothing to do.
2049 * Set up environment.
2051 ASSERT(INT_GET(leaf_s->hdr.info.magic, ARCH_CONVERT)
2052 == XFS_ATTR_LEAF_MAGIC);
2053 ASSERT(INT_GET(leaf_d->hdr.info.magic, ARCH_CONVERT)
2054 == XFS_ATTR_LEAF_MAGIC);
2055 hdr_s = &leaf_s->hdr;
2056 hdr_d = &leaf_d->hdr;
2057 ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0)
2058 && (INT_GET(hdr_s->count, ARCH_CONVERT)
2059 < (XFS_LBSIZE(mp)/8)));
2060 ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
2061 ((INT_GET(hdr_s->count, ARCH_CONVERT)
2062 * sizeof(*entry_s))+sizeof(*hdr_s)));
2063 ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
2064 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
2065 ((INT_GET(hdr_d->count, ARCH_CONVERT)
2066 * sizeof(*entry_d))+sizeof(*hdr_d)));
2068 ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
2069 ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
2070 ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
2073 * Move the entries in the destination leaf up to make a hole?
2075 if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
2076 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
2077 tmp *= sizeof(xfs_attr_leaf_entry_t);
2078 entry_s = &leaf_d->entries[start_d];
2079 entry_d = &leaf_d->entries[start_d + count];
2080 memmove((char *)entry_d, (char *)entry_s, tmp);
2084 * Copy all entry's in the same (sorted) order,
2085 * but allocate attribute info packed and in sequence.
2087 entry_s = &leaf_s->entries[start_s];
2088 entry_d = &leaf_d->entries[start_d];
2090 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2091 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT)
2092 >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
2093 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2096 * Code to drop INCOMPLETE entries. Difficult to use as we
2097 * may also need to change the insertion index. Code turned
2098 * off for 6.2, should be revisited later.
2100 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2101 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2102 INT_MOD(hdr_s->usedbytes, ARCH_CONVERT, -tmp);
2103 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
2104 entry_d--; /* to compensate for ++ in loop hdr */
2106 if ((start_s + i) < offset)
2107 result++; /* insertion index adjustment */
2110 INT_MOD(hdr_d->firstused, ARCH_CONVERT, -tmp);
2111 /* both on-disk, don't endian flip twice */
2112 entry_d->hashval = entry_s->hashval;
2113 /* both on-disk, don't endian flip twice */
2114 entry_d->nameidx = hdr_d->firstused;
2115 entry_d->flags = entry_s->flags;
2116 ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp
2118 memmove(XFS_ATTR_LEAF_NAME(leaf_d, desti),
2119 XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), tmp);
2120 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp
2122 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
2123 INT_MOD(hdr_s->usedbytes, ARCH_CONVERT, -tmp);
2124 INT_MOD(hdr_d->usedbytes, ARCH_CONVERT, tmp);
2125 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
2126 INT_MOD(hdr_d->count, ARCH_CONVERT, 1);
2127 tmp = INT_GET(hdr_d->count, ARCH_CONVERT)
2128 * sizeof(xfs_attr_leaf_entry_t)
2129 + sizeof(xfs_attr_leaf_hdr_t);
2130 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
2137 * Zero out the entries we just copied.
2139 if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
2140 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2141 entry_s = &leaf_s->entries[start_s];
2142 ASSERT(((char *)entry_s + tmp) <=
2143 ((char *)leaf_s + XFS_LBSIZE(mp)));
2144 memset((char *)entry_s, 0, tmp);
2147 * Move the remaining entries down to fill the hole,
2148 * then zero the entries at the top.
2150 tmp = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
2151 tmp *= sizeof(xfs_attr_leaf_entry_t);
2152 entry_s = &leaf_s->entries[start_s + count];
2153 entry_d = &leaf_s->entries[start_s];
2154 memmove((char *)entry_d, (char *)entry_s, tmp);
2156 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2157 entry_s = &leaf_s->entries[INT_GET(hdr_s->count,
2159 ASSERT(((char *)entry_s + tmp) <=
2160 ((char *)leaf_s + XFS_LBSIZE(mp)));
2161 memset((char *)entry_s, 0, tmp);
2165 * Fill in the freemap information
2167 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT,
2168 sizeof(xfs_attr_leaf_hdr_t));
2169 INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT,
2170 INT_GET(hdr_d->count, ARCH_CONVERT)
2171 * sizeof(xfs_attr_leaf_entry_t));
2172 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT,
2173 INT_GET(hdr_d->firstused, ARCH_CONVERT)
2174 - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
2175 hdr_d->freemap[1].base = 0;
2176 hdr_d->freemap[2].base = 0;
2177 hdr_d->freemap[1].size = 0;
2178 hdr_d->freemap[2].size = 0;
2179 hdr_s->holes = 1; /* leaf may not be compact */
2183 * Compare two leaf blocks "order".
2184 * Return 0 unless leaf2 should go before leaf1.
2187 xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2189 xfs_attr_leafblock_t *leaf1, *leaf2;
2191 leaf1 = leaf1_bp->data;
2192 leaf2 = leaf2_bp->data;
2193 ASSERT((INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
2194 == XFS_ATTR_LEAF_MAGIC) &&
2195 (INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2196 == XFS_ATTR_LEAF_MAGIC));
2197 if ( (INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0)
2198 && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0)
2199 && ( (INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
2200 INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT))
2201 || (INT_GET(leaf2->entries[INT_GET(leaf2->hdr.count,
2202 ARCH_CONVERT)-1].hashval, ARCH_CONVERT) <
2203 INT_GET(leaf1->entries[INT_GET(leaf1->hdr.count,
2204 ARCH_CONVERT)-1].hashval, ARCH_CONVERT))) ) {
2211 * Pick up the last hashvalue from a leaf block.
2214 xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2216 xfs_attr_leafblock_t *leaf;
2219 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2220 == XFS_ATTR_LEAF_MAGIC);
2222 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
2223 if (!leaf->hdr.count)
2225 return(INT_GET(leaf->entries[INT_GET(leaf->hdr.count,
2226 ARCH_CONVERT)-1].hashval, ARCH_CONVERT));
2230 * Calculate the number of bytes used to store the indicated attribute
2231 * (whether local or remote only calculate bytes in this block).
2234 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2236 xfs_attr_leaf_name_local_t *name_loc;
2237 xfs_attr_leaf_name_remote_t *name_rmt;
2240 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2241 == XFS_ATTR_LEAF_MAGIC);
2242 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2243 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, index);
2244 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc->namelen,
2245 INT_GET(name_loc->valuelen,
2248 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, index);
2249 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt->namelen);
2255 * Calculate the number of bytes that would be required to store the new
2256 * attribute (whether local or remote only calculate bytes in this block).
2257 * This routine decides as a side effect whether the attribute will be
2258 * a "local" or a "remote" attribute.
2261 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2265 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen, valuelen);
2266 if (size < XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize)) {
2271 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen);
2280 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2283 xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2285 attrlist_cursor_kern_t *cursor;
2286 xfs_attr_leafblock_t *leaf;
2287 xfs_attr_leaf_entry_t *entry;
2288 xfs_attr_leaf_name_local_t *name_loc;
2289 xfs_attr_leaf_name_remote_t *name_rmt;
2294 cursor = context->cursor;
2295 cursor->initted = 1;
2297 xfs_attr_trace_l_cl("blk start", context, leaf);
2300 * Re-find our place in the leaf block if this is a new syscall.
2302 if (context->resynch) {
2303 entry = &leaf->entries[0];
2304 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
2306 if (INT_GET(entry->hashval, ARCH_CONVERT)
2307 == cursor->hashval) {
2308 if (cursor->offset == context->dupcnt) {
2309 context->dupcnt = 0;
2313 } else if (INT_GET(entry->hashval, ARCH_CONVERT)
2314 > cursor->hashval) {
2315 context->dupcnt = 0;
2319 if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
2320 xfs_attr_trace_l_c("not found", context);
2324 entry = &leaf->entries[0];
2327 context->resynch = 0;
2330 * We have found our place, start copying out the new attributes.
2333 for ( ; (i < INT_GET(leaf->hdr.count, ARCH_CONVERT))
2334 && (retval == 0); entry++, i++) {
2335 attrnames_t *namesp;
2337 if (INT_GET(entry->hashval, ARCH_CONVERT) != cursor->hashval) {
2338 cursor->hashval = INT_GET(entry->hashval, ARCH_CONVERT);
2342 if (entry->flags & XFS_ATTR_INCOMPLETE)
2343 continue; /* skip incomplete entries */
2344 if (((context->flags & ATTR_SECURE) != 0) !=
2345 ((entry->flags & XFS_ATTR_SECURE) != 0) &&
2346 !(context->flags & ATTR_KERNORMALS))
2347 continue; /* skip non-matching entries */
2348 if (((context->flags & ATTR_ROOT) != 0) !=
2349 ((entry->flags & XFS_ATTR_ROOT) != 0) &&
2350 !(context->flags & ATTR_KERNROOTLS))
2351 continue; /* skip non-matching entries */
2353 namesp = (entry->flags & XFS_ATTR_SECURE) ? &attr_secure :
2354 ((entry->flags & XFS_ATTR_ROOT) ? &attr_trusted :
2357 if (entry->flags & XFS_ATTR_LOCAL) {
2358 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
2359 if (context->flags & ATTR_KERNOVAL) {
2360 ASSERT(context->flags & ATTR_KERNAMELS);
2361 context->count += namesp->attr_namelen +
2362 (int)name_loc->namelen + 1;
2364 retval = xfs_attr_put_listent(context, namesp,
2365 (char *)name_loc->nameval,
2366 (int)name_loc->namelen,
2367 (int)INT_GET(name_loc->valuelen,
2371 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2372 if (context->flags & ATTR_KERNOVAL) {
2373 ASSERT(context->flags & ATTR_KERNAMELS);
2374 context->count += namesp->attr_namelen +
2375 (int)name_rmt->namelen + 1;
2377 retval = xfs_attr_put_listent(context, namesp,
2378 (char *)name_rmt->name,
2379 (int)name_rmt->namelen,
2380 (int)INT_GET(name_rmt->valuelen,
2388 xfs_attr_trace_l_cl("blk end", context, leaf);
2392 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
2393 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
2394 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
2395 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
2396 & ~(sizeof(u_int32_t)-1))
2399 * Format an attribute and copy it out to the user's buffer.
2400 * Take care to check values and protect against them changing later,
2401 * we may be reading them directly out of a user buffer.
2405 xfs_attr_put_listent(xfs_attr_list_context_t *context,
2406 attrnames_t *namesp, char *name, int namelen, int valuelen)
2408 attrlist_ent_t *aep;
2411 ASSERT(!(context->flags & ATTR_KERNOVAL));
2412 if (context->flags & ATTR_KERNAMELS) {
2415 ASSERT(context->count >= 0);
2417 arraytop = context->count + namesp->attr_namelen + namelen + 1;
2418 if (arraytop > context->firstu) {
2419 context->count = -1; /* insufficient space */
2422 offset = (char *)context->alist + context->count;
2423 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
2424 offset += namesp->attr_namelen;
2425 strncpy(offset, name, namelen); /* real name */
2428 context->count += namesp->attr_namelen + namelen + 1;
2432 ASSERT(context->count >= 0);
2433 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
2434 ASSERT(context->firstu >= sizeof(*context->alist));
2435 ASSERT(context->firstu <= context->bufsize);
2437 arraytop = sizeof(*context->alist) +
2438 context->count * sizeof(context->alist->al_offset[0]);
2439 context->firstu -= ATTR_ENTSIZE(namelen);
2440 if (context->firstu < arraytop) {
2441 xfs_attr_trace_l_c("buffer full", context);
2442 context->alist->al_more = 1;
2446 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
2447 aep->a_valuelen = valuelen;
2448 memcpy(aep->a_name, name, namelen);
2449 aep->a_name[ namelen ] = 0;
2450 context->alist->al_offset[ context->count++ ] = context->firstu;
2451 context->alist->al_count = context->count;
2452 xfs_attr_trace_l_c("add", context);
2456 /*========================================================================
2457 * Manage the INCOMPLETE flag in a leaf entry
2458 *========================================================================*/
2461 * Clear the INCOMPLETE flag on an entry in a leaf block.
2464 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2466 xfs_attr_leafblock_t *leaf;
2467 xfs_attr_leaf_entry_t *entry;
2468 xfs_attr_leaf_name_remote_t *name_rmt;
2472 xfs_attr_leaf_name_local_t *name_loc;
2478 * Set up the operation.
2480 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2488 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2489 == XFS_ATTR_LEAF_MAGIC);
2490 ASSERT(args->index < INT_GET(leaf->hdr.count, ARCH_CONVERT));
2491 ASSERT(args->index >= 0);
2492 entry = &leaf->entries[ args->index ];
2493 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2496 if (entry->flags & XFS_ATTR_LOCAL) {
2497 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2498 namelen = name_loc->namelen;
2499 name = (char *)name_loc->nameval;
2501 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2502 namelen = name_rmt->namelen;
2503 name = (char *)name_rmt->name;
2505 ASSERT(INT_GET(entry->hashval, ARCH_CONVERT) == args->hashval);
2506 ASSERT(namelen == args->namelen);
2507 ASSERT(memcmp(name, args->name, namelen) == 0);
2510 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2511 xfs_da_log_buf(args->trans, bp,
2512 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2514 if (args->rmtblkno) {
2515 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2516 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2517 INT_SET(name_rmt->valueblk, ARCH_CONVERT, args->rmtblkno);
2518 INT_SET(name_rmt->valuelen, ARCH_CONVERT, args->valuelen);
2519 xfs_da_log_buf(args->trans, bp,
2520 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2522 xfs_da_buf_done(bp);
2525 * Commit the flag value change and start the next trans in series.
2527 error = xfs_attr_rolltrans(&args->trans, args->dp);
2533 * Set the INCOMPLETE flag on an entry in a leaf block.
2536 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2538 xfs_attr_leafblock_t *leaf;
2539 xfs_attr_leaf_entry_t *entry;
2540 xfs_attr_leaf_name_remote_t *name_rmt;
2545 * Set up the operation.
2547 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2555 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2556 == XFS_ATTR_LEAF_MAGIC);
2557 ASSERT(args->index < INT_GET(leaf->hdr.count, ARCH_CONVERT));
2558 ASSERT(args->index >= 0);
2559 entry = &leaf->entries[ args->index ];
2561 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2562 entry->flags |= XFS_ATTR_INCOMPLETE;
2563 xfs_da_log_buf(args->trans, bp,
2564 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2565 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2566 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2567 name_rmt->valueblk = 0;
2568 name_rmt->valuelen = 0;
2569 xfs_da_log_buf(args->trans, bp,
2570 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2572 xfs_da_buf_done(bp);
2575 * Commit the flag value change and start the next trans in series.
2577 error = xfs_attr_rolltrans(&args->trans, args->dp);
2583 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2584 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2585 * entry given by args->blkno2/index2.
2587 * Note that they could be in different blocks, or in the same block.
2590 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2592 xfs_attr_leafblock_t *leaf1, *leaf2;
2593 xfs_attr_leaf_entry_t *entry1, *entry2;
2594 xfs_attr_leaf_name_remote_t *name_rmt;
2595 xfs_dabuf_t *bp1, *bp2;
2598 xfs_attr_leaf_name_local_t *name_loc;
2599 int namelen1, namelen2;
2600 char *name1, *name2;
2604 * Read the block containing the "old" attr
2606 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2611 ASSERT(bp1 != NULL);
2614 * Read the block containing the "new" attr, if it is different
2616 if (args->blkno2 != args->blkno) {
2617 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2618 -1, &bp2, XFS_ATTR_FORK);
2622 ASSERT(bp2 != NULL);
2628 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT)
2629 == XFS_ATTR_LEAF_MAGIC);
2630 ASSERT(args->index < INT_GET(leaf1->hdr.count, ARCH_CONVERT));
2631 ASSERT(args->index >= 0);
2632 entry1 = &leaf1->entries[ args->index ];
2635 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2636 == XFS_ATTR_LEAF_MAGIC);
2637 ASSERT(args->index2 < INT_GET(leaf2->hdr.count, ARCH_CONVERT));
2638 ASSERT(args->index2 >= 0);
2639 entry2 = &leaf2->entries[ args->index2 ];
2642 if (entry1->flags & XFS_ATTR_LOCAL) {
2643 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf1, args->index);
2644 namelen1 = name_loc->namelen;
2645 name1 = (char *)name_loc->nameval;
2647 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2648 namelen1 = name_rmt->namelen;
2649 name1 = (char *)name_rmt->name;
2651 if (entry2->flags & XFS_ATTR_LOCAL) {
2652 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf2, args->index2);
2653 namelen2 = name_loc->namelen;
2654 name2 = (char *)name_loc->nameval;
2656 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2657 namelen2 = name_rmt->namelen;
2658 name2 = (char *)name_rmt->name;
2660 ASSERT(INT_GET(entry1->hashval, ARCH_CONVERT) == INT_GET(entry2->hashval, ARCH_CONVERT));
2661 ASSERT(namelen1 == namelen2);
2662 ASSERT(memcmp(name1, name2, namelen1) == 0);
2665 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2666 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2668 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2669 xfs_da_log_buf(args->trans, bp1,
2670 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2671 if (args->rmtblkno) {
2672 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2673 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2674 INT_SET(name_rmt->valueblk, ARCH_CONVERT, args->rmtblkno);
2675 INT_SET(name_rmt->valuelen, ARCH_CONVERT, args->valuelen);
2676 xfs_da_log_buf(args->trans, bp1,
2677 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2680 entry2->flags |= XFS_ATTR_INCOMPLETE;
2681 xfs_da_log_buf(args->trans, bp2,
2682 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2683 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2684 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2685 name_rmt->valueblk = 0;
2686 name_rmt->valuelen = 0;
2687 xfs_da_log_buf(args->trans, bp2,
2688 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2690 xfs_da_buf_done(bp1);
2692 xfs_da_buf_done(bp2);
2695 * Commit the flag value change and start the next trans in series.
2697 error = xfs_attr_rolltrans(&args->trans, args->dp);
2702 /*========================================================================
2703 * Indiscriminately delete the entire attribute fork
2704 *========================================================================*/
2707 * Recurse (gasp!) through the attribute nodes until we find leaves.
2708 * We're doing a depth-first traversal in order to invalidate everything.
2711 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2713 xfs_da_blkinfo_t *info;
2719 * Read block 0 to see what we have to work with.
2720 * We only get here if we have extents, since we remove
2721 * the extents in reverse order the extent containing
2722 * block 0 must still be there.
2724 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2727 blkno = xfs_da_blkno(bp);
2730 * Invalidate the tree, even if the "tree" is only a single leaf block.
2731 * This is a depth-first traversal!
2734 if (INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
2735 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2736 } else if (INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC) {
2737 error = xfs_attr_leaf_inactive(trans, dp, bp);
2739 error = XFS_ERROR(EIO);
2740 xfs_da_brelse(*trans, bp);
2746 * Invalidate the incore copy of the root block.
2748 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2751 xfs_da_binval(*trans, bp); /* remove from cache */
2753 * Commit the invalidate and start the next transaction.
2755 error = xfs_attr_rolltrans(trans, dp);
2761 * Recurse (gasp!) through the attribute nodes until we find leaves.
2762 * We're doing a depth-first traversal in order to invalidate everything.
2765 xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2768 xfs_da_blkinfo_t *info;
2769 xfs_da_intnode_t *node;
2770 xfs_dablk_t child_fsb;
2771 xfs_daddr_t parent_blkno, child_blkno;
2772 int error, count, i;
2773 xfs_dabuf_t *child_bp;
2776 * Since this code is recursive (gasp!) we must protect ourselves.
2778 if (level > XFS_DA_NODE_MAXDEPTH) {
2779 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2780 return(XFS_ERROR(EIO));
2784 ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT)
2785 == XFS_DA_NODE_MAGIC);
2786 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
2787 count = INT_GET(node->hdr.count, ARCH_CONVERT);
2789 xfs_da_brelse(*trans, bp);
2792 child_fsb = INT_GET(node->btree[0].before, ARCH_CONVERT);
2793 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2796 * If this is the node level just above the leaves, simply loop
2797 * over the leaves removing all of them. If this is higher up
2798 * in the tree, recurse downward.
2800 for (i = 0; i < count; i++) {
2802 * Read the subsidiary block to see what we have to work with.
2803 * Don't do this in a transaction. This is a depth-first
2804 * traversal of the tree so we may deal with many blocks
2805 * before we come back to this one.
2807 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2812 /* save for re-read later */
2813 child_blkno = xfs_da_blkno(child_bp);
2816 * Invalidate the subtree, however we have to.
2818 info = child_bp->data;
2819 if (INT_GET(info->magic, ARCH_CONVERT)
2820 == XFS_DA_NODE_MAGIC) {
2821 error = xfs_attr_node_inactive(trans, dp,
2823 } else if (INT_GET(info->magic, ARCH_CONVERT)
2824 == XFS_ATTR_LEAF_MAGIC) {
2825 error = xfs_attr_leaf_inactive(trans, dp,
2828 error = XFS_ERROR(EIO);
2829 xfs_da_brelse(*trans, child_bp);
2835 * Remove the subsidiary block from the cache
2838 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2839 &child_bp, XFS_ATTR_FORK);
2842 xfs_da_binval(*trans, child_bp);
2846 * If we're not done, re-read the parent to get the next
2847 * child block number.
2849 if ((i+1) < count) {
2850 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2851 &bp, XFS_ATTR_FORK);
2854 child_fsb = INT_GET(node->btree[i+1].before, ARCH_CONVERT);
2855 xfs_da_brelse(*trans, bp);
2858 * Atomically commit the whole invalidate stuff.
2860 if ((error = xfs_attr_rolltrans(trans, dp)))
2868 * Invalidate all of the "remote" value regions pointed to by a particular
2870 * Note that we must release the lock on the buffer so that we are not
2871 * caught holding something that the logging code wants to flush to disk.
2874 xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2876 xfs_attr_leafblock_t *leaf;
2877 xfs_attr_leaf_entry_t *entry;
2878 xfs_attr_leaf_name_remote_t *name_rmt;
2879 xfs_attr_inactive_list_t *list, *lp;
2880 int error, count, size, tmp, i;
2883 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
2884 == XFS_ATTR_LEAF_MAGIC);
2887 * Count the number of "remote" value extents.
2890 entry = &leaf->entries[0];
2891 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
2892 if ( INT_GET(entry->nameidx, ARCH_CONVERT)
2893 && ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2894 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2895 if (name_rmt->valueblk)
2901 * If there are no "remote" values, we're done.
2904 xfs_da_brelse(*trans, bp);
2909 * Allocate storage for a list of all the "remote" value extents.
2911 size = count * sizeof(xfs_attr_inactive_list_t);
2912 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2915 * Identify each of the "remote" value extents.
2918 entry = &leaf->entries[0];
2919 for (i = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); entry++, i++) {
2920 if ( INT_GET(entry->nameidx, ARCH_CONVERT)
2921 && ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2922 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2923 if (name_rmt->valueblk) {
2924 /* both on-disk, don't endian flip twice */
2925 lp->valueblk = name_rmt->valueblk;
2926 INT_SET(lp->valuelen, ARCH_CONVERT,
2927 XFS_B_TO_FSB(dp->i_mount,
2928 INT_GET(name_rmt->valuelen,
2934 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2937 * Invalidate each of the "remote" value extents.
2940 for (lp = list, i = 0; i < count; i++, lp++) {
2941 tmp = xfs_attr_leaf_freextent(trans, dp,
2942 INT_GET(lp->valueblk,
2944 INT_GET(lp->valuelen,
2947 error = tmp; /* save only the 1st errno */
2950 kmem_free((xfs_caddr_t)list, size);
2955 * Look at all the extents for this logical region,
2956 * invalidate any buffers that are incore/in transactions.
2959 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2960 xfs_dablk_t blkno, int blkcnt)
2962 xfs_bmbt_irec_t map;
2964 int tblkcnt, dblkcnt, nmap, error;
2969 * Roll through the "value", invalidating the attribute value's
2974 while (tblkcnt > 0) {
2976 * Try to remember where we decided to put the value.
2979 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2980 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2981 NULL, 0, &map, &nmap, NULL);
2986 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2989 * If it's a hole, these are already unmapped
2990 * so there's nothing to invalidate.
2992 if (map.br_startblock != HOLESTARTBLOCK) {
2994 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
2996 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
2998 bp = xfs_trans_get_buf(*trans,
2999 dp->i_mount->m_ddev_targp,
3000 dblkno, dblkcnt, XFS_BUF_LOCK);
3001 xfs_trans_binval(*trans, bp);
3003 * Roll to next transaction.
3005 if ((error = xfs_attr_rolltrans(trans, dp)))
3009 tblkno += map.br_blockcount;
3010 tblkcnt -= map.br_blockcount;
3018 * Roll from one trans in the sequence of PERMANENT transactions to the next.
3021 xfs_attr_rolltrans(xfs_trans_t **transp, xfs_inode_t *dp)
3024 unsigned int logres, count;
3028 * Ensure that the inode is always logged.
3031 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
3034 * Copy the critical parameters from one trans to the next.
3036 logres = trans->t_log_res;
3037 count = trans->t_log_count;
3038 *transp = xfs_trans_dup(trans);
3041 * Commit the current transaction.
3042 * If this commit failed, then it'd just unlock those items that
3043 * are not marked ihold. That also means that a filesystem shutdown
3044 * is in progress. The caller takes the responsibility to cancel
3045 * the duplicate transaction that gets returned.
3047 if ((error = xfs_trans_commit(trans, 0, NULL)))
3053 * Reserve space in the log for th next transaction.
3054 * This also pushes items in the "AIL", the list of logged items,
3055 * out to disk if they are taking up space at the tail of the log
3056 * that we want to use. This requires that either nothing be locked
3057 * across this call, or that anything that is locked be logged in
3058 * the prior and the next transactions.
3060 error = xfs_trans_reserve(trans, 0, logres, 0,
3061 XFS_TRANS_PERM_LOG_RES, count);
3063 * Ensure that the inode is in the new transaction and locked.
3066 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
3067 xfs_trans_ihold(trans, dp);