MIPS: Alchemy: devboards: factor out PB1200 IRQ cascade code.
[linux-2.6/linux-mips.git] / fs / xfs / xfs_attr_leaf.c
bloba90ce74fc256904837381f61e49c0e569818e89f
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_alloc.h"
35 #include "xfs_btree.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_inode_item.h"
41 #include "xfs_bmap.h"
42 #include "xfs_attr.h"
43 #include "xfs_attr_leaf.h"
44 #include "xfs_error.h"
45 #include "xfs_trace.h"
48 * xfs_attr_leaf.c
50 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
53 /*========================================================================
54 * Function prototypes for the kernel.
55 *========================================================================*/
58 * Routines used for growing the Btree.
60 STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
61 xfs_dabuf_t **bpp);
62 STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
63 int freemap_index);
64 STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
65 STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
66 xfs_da_state_blk_t *blk1,
67 xfs_da_state_blk_t *blk2);
68 STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
69 xfs_da_state_blk_t *leaf_blk_1,
70 xfs_da_state_blk_t *leaf_blk_2,
71 int *number_entries_in_blk1,
72 int *number_usedbytes_in_blk1);
75 * Routines used for shrinking the Btree.
77 STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
78 xfs_dabuf_t *bp, int level);
79 STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
80 xfs_dabuf_t *bp);
81 STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
82 xfs_dablk_t blkno, int blkcnt);
85 * Utility routines.
87 STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
88 int src_start,
89 xfs_attr_leafblock_t *dst_leaf,
90 int dst_start, int move_count,
91 xfs_mount_t *mp);
92 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
94 /*========================================================================
95 * Namespace helper routines
96 *========================================================================*/
99 * If namespace bits don't match return 0.
100 * If all match then return 1.
102 STATIC int
103 xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
105 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
109 /*========================================================================
110 * External routines when attribute fork size < XFS_LITINO(mp).
111 *========================================================================*/
114 * Query whether the requested number of additional bytes of extended
115 * attribute space will be able to fit inline.
116 * Returns zero if not, else the di_forkoff fork offset to be used in the
117 * literal area for attribute data once the new bytes have been added.
119 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
120 * special case for dev/uuid inodes, they have fixed size data forks.
123 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
125 int offset;
126 int minforkoff; /* lower limit on valid forkoff locations */
127 int maxforkoff; /* upper limit on valid forkoff locations */
128 int dsize;
129 xfs_mount_t *mp = dp->i_mount;
131 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
133 switch (dp->i_d.di_format) {
134 case XFS_DINODE_FMT_DEV:
135 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
136 return (offset >= minforkoff) ? minforkoff : 0;
137 case XFS_DINODE_FMT_UUID:
138 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
139 return (offset >= minforkoff) ? minforkoff : 0;
142 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
143 if (bytes <= XFS_IFORK_ASIZE(dp))
144 return dp->i_d.di_forkoff;
145 return 0;
148 dsize = dp->i_df.if_bytes;
150 switch (dp->i_d.di_format) {
151 case XFS_DINODE_FMT_EXTENTS:
153 * If there is no attr fork and the data fork is extents,
154 * determine if creating the default attr fork will result
155 * in the extents form migrating to btree. If so, the
156 * minimum offset only needs to be the space required for
157 * the btree root.
159 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
160 xfs_default_attroffset(dp))
161 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
162 break;
164 case XFS_DINODE_FMT_BTREE:
166 * If have data btree then keep forkoff if we have one,
167 * otherwise we are adding a new attr, so then we set
168 * minforkoff to where the btree root can finish so we have
169 * plenty of room for attrs
171 if (dp->i_d.di_forkoff) {
172 if (offset < dp->i_d.di_forkoff)
173 return 0;
174 else
175 return dp->i_d.di_forkoff;
176 } else
177 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
178 break;
182 * A data fork btree root must have space for at least
183 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
185 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
186 minforkoff = roundup(minforkoff, 8) >> 3;
188 /* attr fork btree root can have at least this many key/ptr pairs */
189 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
190 maxforkoff = maxforkoff >> 3; /* rounded down */
192 if (offset >= minforkoff && offset < maxforkoff)
193 return offset;
194 if (offset >= maxforkoff)
195 return maxforkoff;
196 return 0;
200 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
202 STATIC void
203 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
205 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
206 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
207 spin_lock(&mp->m_sb_lock);
208 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
209 xfs_sb_version_addattr2(&mp->m_sb);
210 spin_unlock(&mp->m_sb_lock);
211 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
212 } else
213 spin_unlock(&mp->m_sb_lock);
218 * Create the initial contents of a shortform attribute list.
220 void
221 xfs_attr_shortform_create(xfs_da_args_t *args)
223 xfs_attr_sf_hdr_t *hdr;
224 xfs_inode_t *dp;
225 xfs_ifork_t *ifp;
227 dp = args->dp;
228 ASSERT(dp != NULL);
229 ifp = dp->i_afp;
230 ASSERT(ifp != NULL);
231 ASSERT(ifp->if_bytes == 0);
232 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
233 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
234 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
235 ifp->if_flags |= XFS_IFINLINE;
236 } else {
237 ASSERT(ifp->if_flags & XFS_IFINLINE);
239 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
240 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
241 hdr->count = 0;
242 hdr->totsize = cpu_to_be16(sizeof(*hdr));
243 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
247 * Add a name/value pair to the shortform attribute list.
248 * Overflow from the inode has already been checked for.
250 void
251 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
253 xfs_attr_shortform_t *sf;
254 xfs_attr_sf_entry_t *sfe;
255 int i, offset, size;
256 xfs_mount_t *mp;
257 xfs_inode_t *dp;
258 xfs_ifork_t *ifp;
260 dp = args->dp;
261 mp = dp->i_mount;
262 dp->i_d.di_forkoff = forkoff;
263 dp->i_df.if_ext_max =
264 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
265 dp->i_afp->if_ext_max =
266 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
268 ifp = dp->i_afp;
269 ASSERT(ifp->if_flags & XFS_IFINLINE);
270 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
271 sfe = &sf->list[0];
272 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
273 #ifdef DEBUG
274 if (sfe->namelen != args->namelen)
275 continue;
276 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
277 continue;
278 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
279 continue;
280 ASSERT(0);
281 #endif
284 offset = (char *)sfe - (char *)sf;
285 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
286 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
287 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
288 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
290 sfe->namelen = args->namelen;
291 sfe->valuelen = args->valuelen;
292 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
293 memcpy(sfe->nameval, args->name, args->namelen);
294 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
295 sf->hdr.count++;
296 be16_add_cpu(&sf->hdr.totsize, size);
297 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
299 xfs_sbversion_add_attr2(mp, args->trans);
303 * After the last attribute is removed revert to original inode format,
304 * making all literal area available to the data fork once more.
306 STATIC void
307 xfs_attr_fork_reset(
308 struct xfs_inode *ip,
309 struct xfs_trans *tp)
311 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
312 ip->i_d.di_forkoff = 0;
313 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
315 ASSERT(ip->i_d.di_anextents == 0);
316 ASSERT(ip->i_afp == NULL);
318 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
319 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
323 * Remove an attribute from the shortform attribute list structure.
326 xfs_attr_shortform_remove(xfs_da_args_t *args)
328 xfs_attr_shortform_t *sf;
329 xfs_attr_sf_entry_t *sfe;
330 int base, size=0, end, totsize, i;
331 xfs_mount_t *mp;
332 xfs_inode_t *dp;
334 dp = args->dp;
335 mp = dp->i_mount;
336 base = sizeof(xfs_attr_sf_hdr_t);
337 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
338 sfe = &sf->list[0];
339 end = sf->hdr.count;
340 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
341 base += size, i++) {
342 size = XFS_ATTR_SF_ENTSIZE(sfe);
343 if (sfe->namelen != args->namelen)
344 continue;
345 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
346 continue;
347 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
348 continue;
349 break;
351 if (i == end)
352 return(XFS_ERROR(ENOATTR));
355 * Fix up the attribute fork data, covering the hole
357 end = base + size;
358 totsize = be16_to_cpu(sf->hdr.totsize);
359 if (end != totsize)
360 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
361 sf->hdr.count--;
362 be16_add_cpu(&sf->hdr.totsize, -size);
365 * Fix up the start offset of the attribute fork
367 totsize -= size;
368 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
369 (mp->m_flags & XFS_MOUNT_ATTR2) &&
370 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
371 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
372 xfs_attr_fork_reset(dp, args->trans);
373 } else {
374 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
375 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
376 ASSERT(dp->i_d.di_forkoff);
377 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
378 (args->op_flags & XFS_DA_OP_ADDNAME) ||
379 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
380 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
381 dp->i_afp->if_ext_max =
382 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
383 dp->i_df.if_ext_max =
384 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
385 xfs_trans_log_inode(args->trans, dp,
386 XFS_ILOG_CORE | XFS_ILOG_ADATA);
389 xfs_sbversion_add_attr2(mp, args->trans);
391 return(0);
395 * Look up a name in a shortform attribute list structure.
397 /*ARGSUSED*/
399 xfs_attr_shortform_lookup(xfs_da_args_t *args)
401 xfs_attr_shortform_t *sf;
402 xfs_attr_sf_entry_t *sfe;
403 int i;
404 xfs_ifork_t *ifp;
406 ifp = args->dp->i_afp;
407 ASSERT(ifp->if_flags & XFS_IFINLINE);
408 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
409 sfe = &sf->list[0];
410 for (i = 0; i < sf->hdr.count;
411 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
412 if (sfe->namelen != args->namelen)
413 continue;
414 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
415 continue;
416 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
417 continue;
418 return(XFS_ERROR(EEXIST));
420 return(XFS_ERROR(ENOATTR));
424 * Look up a name in a shortform attribute list structure.
426 /*ARGSUSED*/
428 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
430 xfs_attr_shortform_t *sf;
431 xfs_attr_sf_entry_t *sfe;
432 int i;
434 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
435 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
436 sfe = &sf->list[0];
437 for (i = 0; i < sf->hdr.count;
438 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
439 if (sfe->namelen != args->namelen)
440 continue;
441 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
442 continue;
443 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
444 continue;
445 if (args->flags & ATTR_KERNOVAL) {
446 args->valuelen = sfe->valuelen;
447 return(XFS_ERROR(EEXIST));
449 if (args->valuelen < sfe->valuelen) {
450 args->valuelen = sfe->valuelen;
451 return(XFS_ERROR(ERANGE));
453 args->valuelen = sfe->valuelen;
454 memcpy(args->value, &sfe->nameval[args->namelen],
455 args->valuelen);
456 return(XFS_ERROR(EEXIST));
458 return(XFS_ERROR(ENOATTR));
462 * Convert from using the shortform to the leaf.
465 xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
467 xfs_inode_t *dp;
468 xfs_attr_shortform_t *sf;
469 xfs_attr_sf_entry_t *sfe;
470 xfs_da_args_t nargs;
471 char *tmpbuffer;
472 int error, i, size;
473 xfs_dablk_t blkno;
474 xfs_dabuf_t *bp;
475 xfs_ifork_t *ifp;
477 dp = args->dp;
478 ifp = dp->i_afp;
479 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
480 size = be16_to_cpu(sf->hdr.totsize);
481 tmpbuffer = kmem_alloc(size, KM_SLEEP);
482 ASSERT(tmpbuffer != NULL);
483 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
484 sf = (xfs_attr_shortform_t *)tmpbuffer;
486 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
487 bp = NULL;
488 error = xfs_da_grow_inode(args, &blkno);
489 if (error) {
491 * If we hit an IO error middle of the transaction inside
492 * grow_inode(), we may have inconsistent data. Bail out.
494 if (error == EIO)
495 goto out;
496 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
497 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
498 goto out;
501 ASSERT(blkno == 0);
502 error = xfs_attr_leaf_create(args, blkno, &bp);
503 if (error) {
504 error = xfs_da_shrink_inode(args, 0, bp);
505 bp = NULL;
506 if (error)
507 goto out;
508 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
509 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
510 goto out;
513 memset((char *)&nargs, 0, sizeof(nargs));
514 nargs.dp = dp;
515 nargs.firstblock = args->firstblock;
516 nargs.flist = args->flist;
517 nargs.total = args->total;
518 nargs.whichfork = XFS_ATTR_FORK;
519 nargs.trans = args->trans;
520 nargs.op_flags = XFS_DA_OP_OKNOENT;
522 sfe = &sf->list[0];
523 for (i = 0; i < sf->hdr.count; i++) {
524 nargs.name = sfe->nameval;
525 nargs.namelen = sfe->namelen;
526 nargs.value = &sfe->nameval[nargs.namelen];
527 nargs.valuelen = sfe->valuelen;
528 nargs.hashval = xfs_da_hashname(sfe->nameval,
529 sfe->namelen);
530 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
531 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
532 ASSERT(error == ENOATTR);
533 error = xfs_attr_leaf_add(bp, &nargs);
534 ASSERT(error != ENOSPC);
535 if (error)
536 goto out;
537 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
539 error = 0;
541 out:
542 if(bp)
543 xfs_da_buf_done(bp);
544 kmem_free(tmpbuffer);
545 return(error);
548 STATIC int
549 xfs_attr_shortform_compare(const void *a, const void *b)
551 xfs_attr_sf_sort_t *sa, *sb;
553 sa = (xfs_attr_sf_sort_t *)a;
554 sb = (xfs_attr_sf_sort_t *)b;
555 if (sa->hash < sb->hash) {
556 return(-1);
557 } else if (sa->hash > sb->hash) {
558 return(1);
559 } else {
560 return(sa->entno - sb->entno);
565 #define XFS_ISRESET_CURSOR(cursor) \
566 (!((cursor)->initted) && !((cursor)->hashval) && \
567 !((cursor)->blkno) && !((cursor)->offset))
569 * Copy out entries of shortform attribute lists for attr_list().
570 * Shortform attribute lists are not stored in hashval sorted order.
571 * If the output buffer is not large enough to hold them all, then we
572 * we have to calculate each entries' hashvalue and sort them before
573 * we can begin returning them to the user.
575 /*ARGSUSED*/
577 xfs_attr_shortform_list(xfs_attr_list_context_t *context)
579 attrlist_cursor_kern_t *cursor;
580 xfs_attr_sf_sort_t *sbuf, *sbp;
581 xfs_attr_shortform_t *sf;
582 xfs_attr_sf_entry_t *sfe;
583 xfs_inode_t *dp;
584 int sbsize, nsbuf, count, i;
585 int error;
587 ASSERT(context != NULL);
588 dp = context->dp;
589 ASSERT(dp != NULL);
590 ASSERT(dp->i_afp != NULL);
591 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
592 ASSERT(sf != NULL);
593 if (!sf->hdr.count)
594 return(0);
595 cursor = context->cursor;
596 ASSERT(cursor != NULL);
598 trace_xfs_attr_list_sf(context);
601 * If the buffer is large enough and the cursor is at the start,
602 * do not bother with sorting since we will return everything in
603 * one buffer and another call using the cursor won't need to be
604 * made.
605 * Note the generous fudge factor of 16 overhead bytes per entry.
606 * If bufsize is zero then put_listent must be a search function
607 * and can just scan through what we have.
609 if (context->bufsize == 0 ||
610 (XFS_ISRESET_CURSOR(cursor) &&
611 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
612 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
613 error = context->put_listent(context,
614 sfe->flags,
615 sfe->nameval,
616 (int)sfe->namelen,
617 (int)sfe->valuelen,
618 &sfe->nameval[sfe->namelen]);
621 * Either search callback finished early or
622 * didn't fit it all in the buffer after all.
624 if (context->seen_enough)
625 break;
627 if (error)
628 return error;
629 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
631 trace_xfs_attr_list_sf_all(context);
632 return(0);
635 /* do no more for a search callback */
636 if (context->bufsize == 0)
637 return 0;
640 * It didn't all fit, so we have to sort everything on hashval.
642 sbsize = sf->hdr.count * sizeof(*sbuf);
643 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
646 * Scan the attribute list for the rest of the entries, storing
647 * the relevant info from only those that match into a buffer.
649 nsbuf = 0;
650 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
651 if (unlikely(
652 ((char *)sfe < (char *)sf) ||
653 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
654 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
655 XFS_ERRLEVEL_LOW,
656 context->dp->i_mount, sfe);
657 kmem_free(sbuf);
658 return XFS_ERROR(EFSCORRUPTED);
661 sbp->entno = i;
662 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
663 sbp->name = sfe->nameval;
664 sbp->namelen = sfe->namelen;
665 /* These are bytes, and both on-disk, don't endian-flip */
666 sbp->valuelen = sfe->valuelen;
667 sbp->flags = sfe->flags;
668 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
669 sbp++;
670 nsbuf++;
674 * Sort the entries on hash then entno.
676 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
679 * Re-find our place IN THE SORTED LIST.
681 count = 0;
682 cursor->initted = 1;
683 cursor->blkno = 0;
684 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
685 if (sbp->hash == cursor->hashval) {
686 if (cursor->offset == count) {
687 break;
689 count++;
690 } else if (sbp->hash > cursor->hashval) {
691 break;
694 if (i == nsbuf) {
695 kmem_free(sbuf);
696 return(0);
700 * Loop putting entries into the user buffer.
702 for ( ; i < nsbuf; i++, sbp++) {
703 if (cursor->hashval != sbp->hash) {
704 cursor->hashval = sbp->hash;
705 cursor->offset = 0;
707 error = context->put_listent(context,
708 sbp->flags,
709 sbp->name,
710 sbp->namelen,
711 sbp->valuelen,
712 &sbp->name[sbp->namelen]);
713 if (error)
714 return error;
715 if (context->seen_enough)
716 break;
717 cursor->offset++;
720 kmem_free(sbuf);
721 return(0);
725 * Check a leaf attribute block to see if all the entries would fit into
726 * a shortform attribute list.
729 xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
731 xfs_attr_leafblock_t *leaf;
732 xfs_attr_leaf_entry_t *entry;
733 xfs_attr_leaf_name_local_t *name_loc;
734 int bytes, i;
736 leaf = bp->data;
737 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
739 entry = &leaf->entries[0];
740 bytes = sizeof(struct xfs_attr_sf_hdr);
741 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
742 if (entry->flags & XFS_ATTR_INCOMPLETE)
743 continue; /* don't copy partial entries */
744 if (!(entry->flags & XFS_ATTR_LOCAL))
745 return(0);
746 name_loc = xfs_attr_leaf_name_local(leaf, i);
747 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
748 return(0);
749 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
750 return(0);
751 bytes += sizeof(struct xfs_attr_sf_entry)-1
752 + name_loc->namelen
753 + be16_to_cpu(name_loc->valuelen);
755 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
756 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
757 (bytes == sizeof(struct xfs_attr_sf_hdr)))
758 return(-1);
759 return(xfs_attr_shortform_bytesfit(dp, bytes));
763 * Convert a leaf attribute list to shortform attribute list
766 xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
768 xfs_attr_leafblock_t *leaf;
769 xfs_attr_leaf_entry_t *entry;
770 xfs_attr_leaf_name_local_t *name_loc;
771 xfs_da_args_t nargs;
772 xfs_inode_t *dp;
773 char *tmpbuffer;
774 int error, i;
776 dp = args->dp;
777 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
778 ASSERT(tmpbuffer != NULL);
780 ASSERT(bp != NULL);
781 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
782 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
783 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
784 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
787 * Clean out the prior contents of the attribute list.
789 error = xfs_da_shrink_inode(args, 0, bp);
790 if (error)
791 goto out;
793 if (forkoff == -1) {
794 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
795 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
796 xfs_attr_fork_reset(dp, args->trans);
797 goto out;
800 xfs_attr_shortform_create(args);
803 * Copy the attributes
805 memset((char *)&nargs, 0, sizeof(nargs));
806 nargs.dp = dp;
807 nargs.firstblock = args->firstblock;
808 nargs.flist = args->flist;
809 nargs.total = args->total;
810 nargs.whichfork = XFS_ATTR_FORK;
811 nargs.trans = args->trans;
812 nargs.op_flags = XFS_DA_OP_OKNOENT;
813 entry = &leaf->entries[0];
814 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
815 if (entry->flags & XFS_ATTR_INCOMPLETE)
816 continue; /* don't copy partial entries */
817 if (!entry->nameidx)
818 continue;
819 ASSERT(entry->flags & XFS_ATTR_LOCAL);
820 name_loc = xfs_attr_leaf_name_local(leaf, i);
821 nargs.name = name_loc->nameval;
822 nargs.namelen = name_loc->namelen;
823 nargs.value = &name_loc->nameval[nargs.namelen];
824 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
825 nargs.hashval = be32_to_cpu(entry->hashval);
826 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
827 xfs_attr_shortform_add(&nargs, forkoff);
829 error = 0;
831 out:
832 kmem_free(tmpbuffer);
833 return(error);
837 * Convert from using a single leaf to a root node and a leaf.
840 xfs_attr_leaf_to_node(xfs_da_args_t *args)
842 xfs_attr_leafblock_t *leaf;
843 xfs_da_intnode_t *node;
844 xfs_inode_t *dp;
845 xfs_dabuf_t *bp1, *bp2;
846 xfs_dablk_t blkno;
847 int error;
849 dp = args->dp;
850 bp1 = bp2 = NULL;
851 error = xfs_da_grow_inode(args, &blkno);
852 if (error)
853 goto out;
854 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
855 XFS_ATTR_FORK);
856 if (error)
857 goto out;
858 ASSERT(bp1 != NULL);
859 bp2 = NULL;
860 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
861 XFS_ATTR_FORK);
862 if (error)
863 goto out;
864 ASSERT(bp2 != NULL);
865 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
866 xfs_da_buf_done(bp1);
867 bp1 = NULL;
868 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
871 * Set up the new root node.
873 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
874 if (error)
875 goto out;
876 node = bp1->data;
877 leaf = bp2->data;
878 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
879 /* both on-disk, don't endian-flip twice */
880 node->btree[0].hashval =
881 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
882 node->btree[0].before = cpu_to_be32(blkno);
883 node->hdr.count = cpu_to_be16(1);
884 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
885 error = 0;
886 out:
887 if (bp1)
888 xfs_da_buf_done(bp1);
889 if (bp2)
890 xfs_da_buf_done(bp2);
891 return(error);
895 /*========================================================================
896 * Routines used for growing the Btree.
897 *========================================================================*/
900 * Create the initial contents of a leaf attribute list
901 * or a leaf in a node attribute list.
903 STATIC int
904 xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
906 xfs_attr_leafblock_t *leaf;
907 xfs_attr_leaf_hdr_t *hdr;
908 xfs_inode_t *dp;
909 xfs_dabuf_t *bp;
910 int error;
912 dp = args->dp;
913 ASSERT(dp != NULL);
914 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
915 XFS_ATTR_FORK);
916 if (error)
917 return(error);
918 ASSERT(bp != NULL);
919 leaf = bp->data;
920 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
921 hdr = &leaf->hdr;
922 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
923 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
924 if (!hdr->firstused) {
925 hdr->firstused = cpu_to_be16(
926 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
929 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
930 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
931 sizeof(xfs_attr_leaf_hdr_t));
933 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
935 *bpp = bp;
936 return(0);
940 * Split the leaf node, rebalance, then add the new entry.
943 xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
944 xfs_da_state_blk_t *newblk)
946 xfs_dablk_t blkno;
947 int error;
950 * Allocate space for a new leaf node.
952 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
953 error = xfs_da_grow_inode(state->args, &blkno);
954 if (error)
955 return(error);
956 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
957 if (error)
958 return(error);
959 newblk->blkno = blkno;
960 newblk->magic = XFS_ATTR_LEAF_MAGIC;
963 * Rebalance the entries across the two leaves.
964 * NOTE: rebalance() currently depends on the 2nd block being empty.
966 xfs_attr_leaf_rebalance(state, oldblk, newblk);
967 error = xfs_da_blk_link(state, oldblk, newblk);
968 if (error)
969 return(error);
972 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
973 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
974 * "new" attrs info. Will need the "old" info to remove it later.
976 * Insert the "new" entry in the correct block.
978 if (state->inleaf)
979 error = xfs_attr_leaf_add(oldblk->bp, state->args);
980 else
981 error = xfs_attr_leaf_add(newblk->bp, state->args);
984 * Update last hashval in each block since we added the name.
986 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
987 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
988 return(error);
992 * Add a name to the leaf attribute list structure.
995 xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
997 xfs_attr_leafblock_t *leaf;
998 xfs_attr_leaf_hdr_t *hdr;
999 xfs_attr_leaf_map_t *map;
1000 int tablesize, entsize, sum, tmp, i;
1002 leaf = bp->data;
1003 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1004 ASSERT((args->index >= 0)
1005 && (args->index <= be16_to_cpu(leaf->hdr.count)));
1006 hdr = &leaf->hdr;
1007 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1008 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1011 * Search through freemap for first-fit on new name length.
1012 * (may need to figure in size of entry struct too)
1014 tablesize = (be16_to_cpu(hdr->count) + 1)
1015 * sizeof(xfs_attr_leaf_entry_t)
1016 + sizeof(xfs_attr_leaf_hdr_t);
1017 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1018 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
1019 if (tablesize > be16_to_cpu(hdr->firstused)) {
1020 sum += be16_to_cpu(map->size);
1021 continue;
1023 if (!map->size)
1024 continue; /* no space in this map */
1025 tmp = entsize;
1026 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
1027 tmp += sizeof(xfs_attr_leaf_entry_t);
1028 if (be16_to_cpu(map->size) >= tmp) {
1029 tmp = xfs_attr_leaf_add_work(bp, args, i);
1030 return(tmp);
1032 sum += be16_to_cpu(map->size);
1036 * If there are no holes in the address space of the block,
1037 * and we don't have enough freespace, then compaction will do us
1038 * no good and we should just give up.
1040 if (!hdr->holes && (sum < entsize))
1041 return(XFS_ERROR(ENOSPC));
1044 * Compact the entries to coalesce free space.
1045 * This may change the hdr->count via dropping INCOMPLETE entries.
1047 xfs_attr_leaf_compact(args->trans, bp);
1050 * After compaction, the block is guaranteed to have only one
1051 * free region, in freemap[0]. If it is not big enough, give up.
1053 if (be16_to_cpu(hdr->freemap[0].size)
1054 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1055 return(XFS_ERROR(ENOSPC));
1057 return(xfs_attr_leaf_add_work(bp, args, 0));
1061 * Add a name to a leaf attribute list structure.
1063 STATIC int
1064 xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1066 xfs_attr_leafblock_t *leaf;
1067 xfs_attr_leaf_hdr_t *hdr;
1068 xfs_attr_leaf_entry_t *entry;
1069 xfs_attr_leaf_name_local_t *name_loc;
1070 xfs_attr_leaf_name_remote_t *name_rmt;
1071 xfs_attr_leaf_map_t *map;
1072 xfs_mount_t *mp;
1073 int tmp, i;
1075 leaf = bp->data;
1076 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1077 hdr = &leaf->hdr;
1078 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
1079 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
1082 * Force open some space in the entry array and fill it in.
1084 entry = &leaf->entries[args->index];
1085 if (args->index < be16_to_cpu(hdr->count)) {
1086 tmp = be16_to_cpu(hdr->count) - args->index;
1087 tmp *= sizeof(xfs_attr_leaf_entry_t);
1088 memmove((char *)(entry+1), (char *)entry, tmp);
1089 xfs_da_log_buf(args->trans, bp,
1090 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1092 be16_add_cpu(&hdr->count, 1);
1095 * Allocate space for the new string (at the end of the run).
1097 map = &hdr->freemap[mapindex];
1098 mp = args->trans->t_mountp;
1099 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1100 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1101 ASSERT(be16_to_cpu(map->size) >=
1102 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1103 mp->m_sb.sb_blocksize, NULL));
1104 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1105 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1106 be16_add_cpu(&map->size,
1107 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1108 mp->m_sb.sb_blocksize, &tmp));
1109 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1110 be16_to_cpu(map->size));
1111 entry->hashval = cpu_to_be32(args->hashval);
1112 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1113 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1114 if (args->op_flags & XFS_DA_OP_RENAME) {
1115 entry->flags |= XFS_ATTR_INCOMPLETE;
1116 if ((args->blkno2 == args->blkno) &&
1117 (args->index2 <= args->index)) {
1118 args->index2++;
1121 xfs_da_log_buf(args->trans, bp,
1122 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1123 ASSERT((args->index == 0) ||
1124 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1125 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
1126 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1129 * Copy the attribute name and value into the new space.
1131 * For "remote" attribute values, simply note that we need to
1132 * allocate space for the "remote" value. We can't actually
1133 * allocate the extents in this transaction, and we can't decide
1134 * which blocks they should be as we might allocate more blocks
1135 * as part of this transaction (a split operation for example).
1137 if (entry->flags & XFS_ATTR_LOCAL) {
1138 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
1139 name_loc->namelen = args->namelen;
1140 name_loc->valuelen = cpu_to_be16(args->valuelen);
1141 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1142 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1143 be16_to_cpu(name_loc->valuelen));
1144 } else {
1145 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
1146 name_rmt->namelen = args->namelen;
1147 memcpy((char *)name_rmt->name, args->name, args->namelen);
1148 entry->flags |= XFS_ATTR_INCOMPLETE;
1149 /* just in case */
1150 name_rmt->valuelen = 0;
1151 name_rmt->valueblk = 0;
1152 args->rmtblkno = 1;
1153 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1155 xfs_da_log_buf(args->trans, bp,
1156 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1157 xfs_attr_leaf_entsize(leaf, args->index)));
1160 * Update the control info for this leaf node
1162 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
1163 /* both on-disk, don't endian-flip twice */
1164 hdr->firstused = entry->nameidx;
1166 ASSERT(be16_to_cpu(hdr->firstused) >=
1167 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1168 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
1169 + sizeof(xfs_attr_leaf_hdr_t);
1170 map = &hdr->freemap[0];
1171 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1172 if (be16_to_cpu(map->base) == tmp) {
1173 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1174 be16_add_cpu(&map->size,
1175 -((int)sizeof(xfs_attr_leaf_entry_t)));
1178 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
1179 xfs_da_log_buf(args->trans, bp,
1180 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1181 return(0);
1185 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1187 STATIC void
1188 xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1190 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1191 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1192 xfs_mount_t *mp;
1193 char *tmpbuffer;
1195 mp = trans->t_mountp;
1196 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1197 ASSERT(tmpbuffer != NULL);
1198 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1199 memset(bp->data, 0, XFS_LBSIZE(mp));
1202 * Copy basic information
1204 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1205 leaf_d = bp->data;
1206 hdr_s = &leaf_s->hdr;
1207 hdr_d = &leaf_d->hdr;
1208 hdr_d->info = hdr_s->info; /* struct copy */
1209 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
1210 /* handle truncation gracefully */
1211 if (!hdr_d->firstused) {
1212 hdr_d->firstused = cpu_to_be16(
1213 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1215 hdr_d->usedbytes = 0;
1216 hdr_d->count = 0;
1217 hdr_d->holes = 0;
1218 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1219 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1220 sizeof(xfs_attr_leaf_hdr_t));
1223 * Copy all entry's in the same (sorted) order,
1224 * but allocate name/value pairs packed and in sequence.
1226 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
1227 be16_to_cpu(hdr_s->count), mp);
1228 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1230 kmem_free(tmpbuffer);
1234 * Redistribute the attribute list entries between two leaf nodes,
1235 * taking into account the size of the new entry.
1237 * NOTE: if new block is empty, then it will get the upper half of the
1238 * old block. At present, all (one) callers pass in an empty second block.
1240 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1241 * to match what it is doing in splitting the attribute leaf block. Those
1242 * values are used in "atomic rename" operations on attributes. Note that
1243 * the "new" and "old" values can end up in different blocks.
1245 STATIC void
1246 xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1247 xfs_da_state_blk_t *blk2)
1249 xfs_da_args_t *args;
1250 xfs_da_state_blk_t *tmp_blk;
1251 xfs_attr_leafblock_t *leaf1, *leaf2;
1252 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1253 int count, totallen, max, space, swap;
1256 * Set up environment.
1258 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1259 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1260 leaf1 = blk1->bp->data;
1261 leaf2 = blk2->bp->data;
1262 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1263 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1264 args = state->args;
1267 * Check ordering of blocks, reverse if it makes things simpler.
1269 * NOTE: Given that all (current) callers pass in an empty
1270 * second block, this code should never set "swap".
1272 swap = 0;
1273 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1274 tmp_blk = blk1;
1275 blk1 = blk2;
1276 blk2 = tmp_blk;
1277 leaf1 = blk1->bp->data;
1278 leaf2 = blk2->bp->data;
1279 swap = 1;
1281 hdr1 = &leaf1->hdr;
1282 hdr2 = &leaf2->hdr;
1285 * Examine entries until we reduce the absolute difference in
1286 * byte usage between the two blocks to a minimum. Then get
1287 * the direction to copy and the number of elements to move.
1289 * "inleaf" is true if the new entry should be inserted into blk1.
1290 * If "swap" is also true, then reverse the sense of "inleaf".
1292 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1293 &count, &totallen);
1294 if (swap)
1295 state->inleaf = !state->inleaf;
1298 * Move any entries required from leaf to leaf:
1300 if (count < be16_to_cpu(hdr1->count)) {
1302 * Figure the total bytes to be added to the destination leaf.
1304 /* number entries being moved */
1305 count = be16_to_cpu(hdr1->count) - count;
1306 space = be16_to_cpu(hdr1->usedbytes) - totallen;
1307 space += count * sizeof(xfs_attr_leaf_entry_t);
1310 * leaf2 is the destination, compact it if it looks tight.
1312 max = be16_to_cpu(hdr2->firstused)
1313 - sizeof(xfs_attr_leaf_hdr_t);
1314 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
1315 if (space > max) {
1316 xfs_attr_leaf_compact(args->trans, blk2->bp);
1320 * Move high entries from leaf1 to low end of leaf2.
1322 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
1323 leaf2, 0, count, state->mp);
1325 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1326 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1327 } else if (count > be16_to_cpu(hdr1->count)) {
1329 * I assert that since all callers pass in an empty
1330 * second buffer, this code should never execute.
1334 * Figure the total bytes to be added to the destination leaf.
1336 /* number entries being moved */
1337 count -= be16_to_cpu(hdr1->count);
1338 space = totallen - be16_to_cpu(hdr1->usedbytes);
1339 space += count * sizeof(xfs_attr_leaf_entry_t);
1342 * leaf1 is the destination, compact it if it looks tight.
1344 max = be16_to_cpu(hdr1->firstused)
1345 - sizeof(xfs_attr_leaf_hdr_t);
1346 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
1347 if (space > max) {
1348 xfs_attr_leaf_compact(args->trans, blk1->bp);
1352 * Move low entries from leaf2 to high end of leaf1.
1354 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
1355 be16_to_cpu(hdr1->count), count, state->mp);
1357 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1358 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1362 * Copy out last hashval in each block for B-tree code.
1364 blk1->hashval = be32_to_cpu(
1365 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1366 blk2->hashval = be32_to_cpu(
1367 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
1370 * Adjust the expected index for insertion.
1371 * NOTE: this code depends on the (current) situation that the
1372 * second block was originally empty.
1374 * If the insertion point moved to the 2nd block, we must adjust
1375 * the index. We must also track the entry just following the
1376 * new entry for use in an "atomic rename" operation, that entry
1377 * is always the "old" entry and the "new" entry is what we are
1378 * inserting. The index/blkno fields refer to the "old" entry,
1379 * while the index2/blkno2 fields refer to the "new" entry.
1381 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
1382 ASSERT(state->inleaf == 0);
1383 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
1384 args->index = args->index2 = blk2->index;
1385 args->blkno = args->blkno2 = blk2->blkno;
1386 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
1387 if (state->inleaf) {
1388 args->index = blk1->index;
1389 args->blkno = blk1->blkno;
1390 args->index2 = 0;
1391 args->blkno2 = blk2->blkno;
1392 } else {
1393 blk2->index = blk1->index
1394 - be16_to_cpu(leaf1->hdr.count);
1395 args->index = args->index2 = blk2->index;
1396 args->blkno = args->blkno2 = blk2->blkno;
1398 } else {
1399 ASSERT(state->inleaf == 1);
1400 args->index = args->index2 = blk1->index;
1401 args->blkno = args->blkno2 = blk1->blkno;
1406 * Examine entries until we reduce the absolute difference in
1407 * byte usage between the two blocks to a minimum.
1408 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1409 * GROT: there will always be enough room in either block for a new entry.
1410 * GROT: Do a double-split for this case?
1412 STATIC int
1413 xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1414 xfs_da_state_blk_t *blk1,
1415 xfs_da_state_blk_t *blk2,
1416 int *countarg, int *usedbytesarg)
1418 xfs_attr_leafblock_t *leaf1, *leaf2;
1419 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1420 xfs_attr_leaf_entry_t *entry;
1421 int count, max, index, totallen, half;
1422 int lastdelta, foundit, tmp;
1425 * Set up environment.
1427 leaf1 = blk1->bp->data;
1428 leaf2 = blk2->bp->data;
1429 hdr1 = &leaf1->hdr;
1430 hdr2 = &leaf2->hdr;
1431 foundit = 0;
1432 totallen = 0;
1435 * Examine entries until we reduce the absolute difference in
1436 * byte usage between the two blocks to a minimum.
1438 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
1439 half = (max+1) * sizeof(*entry);
1440 half += be16_to_cpu(hdr1->usedbytes) +
1441 be16_to_cpu(hdr2->usedbytes) +
1442 xfs_attr_leaf_newentsize(
1443 state->args->namelen,
1444 state->args->valuelen,
1445 state->blocksize, NULL);
1446 half /= 2;
1447 lastdelta = state->blocksize;
1448 entry = &leaf1->entries[0];
1449 for (count = index = 0; count < max; entry++, index++, count++) {
1451 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1453 * The new entry is in the first block, account for it.
1455 if (count == blk1->index) {
1456 tmp = totallen + sizeof(*entry) +
1457 xfs_attr_leaf_newentsize(
1458 state->args->namelen,
1459 state->args->valuelen,
1460 state->blocksize, NULL);
1461 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1462 break;
1463 lastdelta = XFS_ATTR_ABS(half - tmp);
1464 totallen = tmp;
1465 foundit = 1;
1469 * Wrap around into the second block if necessary.
1471 if (count == be16_to_cpu(hdr1->count)) {
1472 leaf1 = leaf2;
1473 entry = &leaf1->entries[0];
1474 index = 0;
1478 * Figure out if next leaf entry would be too much.
1480 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1481 index);
1482 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1483 break;
1484 lastdelta = XFS_ATTR_ABS(half - tmp);
1485 totallen = tmp;
1486 #undef XFS_ATTR_ABS
1490 * Calculate the number of usedbytes that will end up in lower block.
1491 * If new entry not in lower block, fix up the count.
1493 totallen -= count * sizeof(*entry);
1494 if (foundit) {
1495 totallen -= sizeof(*entry) +
1496 xfs_attr_leaf_newentsize(
1497 state->args->namelen,
1498 state->args->valuelen,
1499 state->blocksize, NULL);
1502 *countarg = count;
1503 *usedbytesarg = totallen;
1504 return(foundit);
1507 /*========================================================================
1508 * Routines used for shrinking the Btree.
1509 *========================================================================*/
1512 * Check a leaf block and its neighbors to see if the block should be
1513 * collapsed into one or the other neighbor. Always keep the block
1514 * with the smaller block number.
1515 * If the current block is over 50% full, don't try to join it, return 0.
1516 * If the block is empty, fill in the state structure and return 2.
1517 * If it can be collapsed, fill in the state structure and return 1.
1518 * If nothing can be done, return 0.
1520 * GROT: allow for INCOMPLETE entries in calculation.
1523 xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1525 xfs_attr_leafblock_t *leaf;
1526 xfs_da_state_blk_t *blk;
1527 xfs_da_blkinfo_t *info;
1528 int count, bytes, forward, error, retval, i;
1529 xfs_dablk_t blkno;
1530 xfs_dabuf_t *bp;
1533 * Check for the degenerate case of the block being over 50% full.
1534 * If so, it's not worth even looking to see if we might be able
1535 * to coalesce with a sibling.
1537 blk = &state->path.blk[ state->path.active-1 ];
1538 info = blk->bp->data;
1539 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
1540 leaf = (xfs_attr_leafblock_t *)info;
1541 count = be16_to_cpu(leaf->hdr.count);
1542 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1543 count * sizeof(xfs_attr_leaf_entry_t) +
1544 be16_to_cpu(leaf->hdr.usedbytes);
1545 if (bytes > (state->blocksize >> 1)) {
1546 *action = 0; /* blk over 50%, don't try to join */
1547 return(0);
1551 * Check for the degenerate case of the block being empty.
1552 * If the block is empty, we'll simply delete it, no need to
1553 * coalesce it with a sibling block. We choose (arbitrarily)
1554 * to merge with the forward block unless it is NULL.
1556 if (count == 0) {
1558 * Make altpath point to the block we want to keep and
1559 * path point to the block we want to drop (this one).
1561 forward = (info->forw != 0);
1562 memcpy(&state->altpath, &state->path, sizeof(state->path));
1563 error = xfs_da_path_shift(state, &state->altpath, forward,
1564 0, &retval);
1565 if (error)
1566 return(error);
1567 if (retval) {
1568 *action = 0;
1569 } else {
1570 *action = 2;
1572 return(0);
1576 * Examine each sibling block to see if we can coalesce with
1577 * at least 25% free space to spare. We need to figure out
1578 * whether to merge with the forward or the backward block.
1579 * We prefer coalescing with the lower numbered sibling so as
1580 * to shrink an attribute list over time.
1582 /* start with smaller blk num */
1583 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
1584 for (i = 0; i < 2; forward = !forward, i++) {
1585 if (forward)
1586 blkno = be32_to_cpu(info->forw);
1587 else
1588 blkno = be32_to_cpu(info->back);
1589 if (blkno == 0)
1590 continue;
1591 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1592 blkno, -1, &bp, XFS_ATTR_FORK);
1593 if (error)
1594 return(error);
1595 ASSERT(bp != NULL);
1597 leaf = (xfs_attr_leafblock_t *)info;
1598 count = be16_to_cpu(leaf->hdr.count);
1599 bytes = state->blocksize - (state->blocksize>>2);
1600 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1601 leaf = bp->data;
1602 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1603 count += be16_to_cpu(leaf->hdr.count);
1604 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
1605 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1606 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1607 xfs_da_brelse(state->args->trans, bp);
1608 if (bytes >= 0)
1609 break; /* fits with at least 25% to spare */
1611 if (i >= 2) {
1612 *action = 0;
1613 return(0);
1617 * Make altpath point to the block we want to keep (the lower
1618 * numbered block) and path point to the block we want to drop.
1620 memcpy(&state->altpath, &state->path, sizeof(state->path));
1621 if (blkno < blk->blkno) {
1622 error = xfs_da_path_shift(state, &state->altpath, forward,
1623 0, &retval);
1624 } else {
1625 error = xfs_da_path_shift(state, &state->path, forward,
1626 0, &retval);
1628 if (error)
1629 return(error);
1630 if (retval) {
1631 *action = 0;
1632 } else {
1633 *action = 1;
1635 return(0);
1639 * Remove a name from the leaf attribute list structure.
1641 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1642 * If two leaves are 37% full, when combined they will leave 25% free.
1645 xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1647 xfs_attr_leafblock_t *leaf;
1648 xfs_attr_leaf_hdr_t *hdr;
1649 xfs_attr_leaf_map_t *map;
1650 xfs_attr_leaf_entry_t *entry;
1651 int before, after, smallest, entsize;
1652 int tablesize, tmp, i;
1653 xfs_mount_t *mp;
1655 leaf = bp->data;
1656 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1657 hdr = &leaf->hdr;
1658 mp = args->trans->t_mountp;
1659 ASSERT((be16_to_cpu(hdr->count) > 0)
1660 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
1661 ASSERT((args->index >= 0)
1662 && (args->index < be16_to_cpu(hdr->count)));
1663 ASSERT(be16_to_cpu(hdr->firstused) >=
1664 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1665 entry = &leaf->entries[args->index];
1666 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1667 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1670 * Scan through free region table:
1671 * check for adjacency of free'd entry with an existing one,
1672 * find smallest free region in case we need to replace it,
1673 * adjust any map that borders the entry table,
1675 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
1676 + sizeof(xfs_attr_leaf_hdr_t);
1677 map = &hdr->freemap[0];
1678 tmp = be16_to_cpu(map->size);
1679 before = after = -1;
1680 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1681 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1682 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
1683 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1684 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1685 if (be16_to_cpu(map->base) == tablesize) {
1686 be16_add_cpu(&map->base,
1687 -((int)sizeof(xfs_attr_leaf_entry_t)));
1688 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
1691 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
1692 == be16_to_cpu(entry->nameidx)) {
1693 before = i;
1694 } else if (be16_to_cpu(map->base)
1695 == (be16_to_cpu(entry->nameidx) + entsize)) {
1696 after = i;
1697 } else if (be16_to_cpu(map->size) < tmp) {
1698 tmp = be16_to_cpu(map->size);
1699 smallest = i;
1704 * Coalesce adjacent freemap regions,
1705 * or replace the smallest region.
1707 if ((before >= 0) || (after >= 0)) {
1708 if ((before >= 0) && (after >= 0)) {
1709 map = &hdr->freemap[before];
1710 be16_add_cpu(&map->size, entsize);
1711 be16_add_cpu(&map->size,
1712 be16_to_cpu(hdr->freemap[after].size));
1713 hdr->freemap[after].base = 0;
1714 hdr->freemap[after].size = 0;
1715 } else if (before >= 0) {
1716 map = &hdr->freemap[before];
1717 be16_add_cpu(&map->size, entsize);
1718 } else {
1719 map = &hdr->freemap[after];
1720 /* both on-disk, don't endian flip twice */
1721 map->base = entry->nameidx;
1722 be16_add_cpu(&map->size, entsize);
1724 } else {
1726 * Replace smallest region (if it is smaller than free'd entry)
1728 map = &hdr->freemap[smallest];
1729 if (be16_to_cpu(map->size) < entsize) {
1730 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
1731 map->size = cpu_to_be16(entsize);
1736 * Did we remove the first entry?
1738 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
1739 smallest = 1;
1740 else
1741 smallest = 0;
1744 * Compress the remaining entries and zero out the removed stuff.
1746 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
1747 be16_add_cpu(&hdr->usedbytes, -entsize);
1748 xfs_da_log_buf(args->trans, bp,
1749 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
1750 entsize));
1752 tmp = (be16_to_cpu(hdr->count) - args->index)
1753 * sizeof(xfs_attr_leaf_entry_t);
1754 memmove((char *)entry, (char *)(entry+1), tmp);
1755 be16_add_cpu(&hdr->count, -1);
1756 xfs_da_log_buf(args->trans, bp,
1757 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1758 entry = &leaf->entries[be16_to_cpu(hdr->count)];
1759 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1762 * If we removed the first entry, re-find the first used byte
1763 * in the name area. Note that if the entry was the "firstused",
1764 * then we don't have a "hole" in our block resulting from
1765 * removing the name.
1767 if (smallest) {
1768 tmp = XFS_LBSIZE(mp);
1769 entry = &leaf->entries[0];
1770 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
1771 ASSERT(be16_to_cpu(entry->nameidx) >=
1772 be16_to_cpu(hdr->firstused));
1773 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1775 if (be16_to_cpu(entry->nameidx) < tmp)
1776 tmp = be16_to_cpu(entry->nameidx);
1778 hdr->firstused = cpu_to_be16(tmp);
1779 if (!hdr->firstused) {
1780 hdr->firstused = cpu_to_be16(
1781 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1783 } else {
1784 hdr->holes = 1; /* mark as needing compaction */
1786 xfs_da_log_buf(args->trans, bp,
1787 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1790 * Check if leaf is less than 50% full, caller may want to
1791 * "join" the leaf with a sibling if so.
1793 tmp = sizeof(xfs_attr_leaf_hdr_t);
1794 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1795 tmp += be16_to_cpu(leaf->hdr.usedbytes);
1796 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1800 * Move all the attribute list entries from drop_leaf into save_leaf.
1802 void
1803 xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1804 xfs_da_state_blk_t *save_blk)
1806 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1807 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1808 xfs_mount_t *mp;
1809 char *tmpbuffer;
1812 * Set up environment.
1814 mp = state->mp;
1815 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1816 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1817 drop_leaf = drop_blk->bp->data;
1818 save_leaf = save_blk->bp->data;
1819 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1820 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1821 drop_hdr = &drop_leaf->hdr;
1822 save_hdr = &save_leaf->hdr;
1825 * Save last hashval from dying block for later Btree fixup.
1827 drop_blk->hashval = be32_to_cpu(
1828 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
1831 * Check if we need a temp buffer, or can we do it in place.
1832 * Note that we don't check "leaf" for holes because we will
1833 * always be dropping it, toosmall() decided that for us already.
1835 if (save_hdr->holes == 0) {
1837 * dest leaf has no holes, so we add there. May need
1838 * to make some room in the entry array.
1840 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1841 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1842 be16_to_cpu(drop_hdr->count), mp);
1843 } else {
1844 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
1845 be16_to_cpu(save_hdr->count),
1846 be16_to_cpu(drop_hdr->count), mp);
1848 } else {
1850 * Destination has holes, so we make a temporary copy
1851 * of the leaf and add them both to that.
1853 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1854 ASSERT(tmpbuffer != NULL);
1855 memset(tmpbuffer, 0, state->blocksize);
1856 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1857 tmp_hdr = &tmp_leaf->hdr;
1858 tmp_hdr->info = save_hdr->info; /* struct copy */
1859 tmp_hdr->count = 0;
1860 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
1861 if (!tmp_hdr->firstused) {
1862 tmp_hdr->firstused = cpu_to_be16(
1863 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1865 tmp_hdr->usedbytes = 0;
1866 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1867 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1868 be16_to_cpu(drop_hdr->count), mp);
1869 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
1870 be16_to_cpu(tmp_leaf->hdr.count),
1871 be16_to_cpu(save_hdr->count), mp);
1872 } else {
1873 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1874 be16_to_cpu(save_hdr->count), mp);
1875 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
1876 be16_to_cpu(tmp_leaf->hdr.count),
1877 be16_to_cpu(drop_hdr->count), mp);
1879 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1880 kmem_free(tmpbuffer);
1883 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1884 state->blocksize - 1);
1887 * Copy out last hashval in each block for B-tree code.
1889 save_blk->hashval = be32_to_cpu(
1890 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
1893 /*========================================================================
1894 * Routines used for finding things in the Btree.
1895 *========================================================================*/
1898 * Look up a name in a leaf attribute list structure.
1899 * This is the internal routine, it uses the caller's buffer.
1901 * Note that duplicate keys are allowed, but only check within the
1902 * current leaf node. The Btree code must check in adjacent leaf nodes.
1904 * Return in args->index the index into the entry[] array of either
1905 * the found entry, or where the entry should have been (insert before
1906 * that entry).
1908 * Don't change the args->value unless we find the attribute.
1911 xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1913 xfs_attr_leafblock_t *leaf;
1914 xfs_attr_leaf_entry_t *entry;
1915 xfs_attr_leaf_name_local_t *name_loc;
1916 xfs_attr_leaf_name_remote_t *name_rmt;
1917 int probe, span;
1918 xfs_dahash_t hashval;
1920 leaf = bp->data;
1921 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1922 ASSERT(be16_to_cpu(leaf->hdr.count)
1923 < (XFS_LBSIZE(args->dp->i_mount)/8));
1926 * Binary search. (note: small blocks will skip this loop)
1928 hashval = args->hashval;
1929 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
1930 for (entry = &leaf->entries[probe]; span > 4;
1931 entry = &leaf->entries[probe]) {
1932 span /= 2;
1933 if (be32_to_cpu(entry->hashval) < hashval)
1934 probe += span;
1935 else if (be32_to_cpu(entry->hashval) > hashval)
1936 probe -= span;
1937 else
1938 break;
1940 ASSERT((probe >= 0) &&
1941 (!leaf->hdr.count
1942 || (probe < be16_to_cpu(leaf->hdr.count))));
1943 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
1946 * Since we may have duplicate hashval's, find the first matching
1947 * hashval in the leaf.
1949 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
1950 entry--;
1951 probe--;
1953 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1954 (be32_to_cpu(entry->hashval) < hashval)) {
1955 entry++;
1956 probe++;
1958 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
1959 (be32_to_cpu(entry->hashval) != hashval)) {
1960 args->index = probe;
1961 return(XFS_ERROR(ENOATTR));
1965 * Duplicate keys may be present, so search all of them for a match.
1967 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
1968 (be32_to_cpu(entry->hashval) == hashval);
1969 entry++, probe++) {
1971 * GROT: Add code to remove incomplete entries.
1974 * If we are looking for INCOMPLETE entries, show only those.
1975 * If we are looking for complete entries, show only those.
1977 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
1978 (entry->flags & XFS_ATTR_INCOMPLETE)) {
1979 continue;
1981 if (entry->flags & XFS_ATTR_LOCAL) {
1982 name_loc = xfs_attr_leaf_name_local(leaf, probe);
1983 if (name_loc->namelen != args->namelen)
1984 continue;
1985 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
1986 continue;
1987 if (!xfs_attr_namesp_match(args->flags, entry->flags))
1988 continue;
1989 args->index = probe;
1990 return(XFS_ERROR(EEXIST));
1991 } else {
1992 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
1993 if (name_rmt->namelen != args->namelen)
1994 continue;
1995 if (memcmp(args->name, (char *)name_rmt->name,
1996 args->namelen) != 0)
1997 continue;
1998 if (!xfs_attr_namesp_match(args->flags, entry->flags))
1999 continue;
2000 args->index = probe;
2001 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2002 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
2003 be32_to_cpu(name_rmt->valuelen));
2004 return(XFS_ERROR(EEXIST));
2007 args->index = probe;
2008 return(XFS_ERROR(ENOATTR));
2012 * Get the value associated with an attribute name from a leaf attribute
2013 * list structure.
2016 xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2018 int valuelen;
2019 xfs_attr_leafblock_t *leaf;
2020 xfs_attr_leaf_entry_t *entry;
2021 xfs_attr_leaf_name_local_t *name_loc;
2022 xfs_attr_leaf_name_remote_t *name_rmt;
2024 leaf = bp->data;
2025 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2026 ASSERT(be16_to_cpu(leaf->hdr.count)
2027 < (XFS_LBSIZE(args->dp->i_mount)/8));
2028 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2030 entry = &leaf->entries[args->index];
2031 if (entry->flags & XFS_ATTR_LOCAL) {
2032 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2033 ASSERT(name_loc->namelen == args->namelen);
2034 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2035 valuelen = be16_to_cpu(name_loc->valuelen);
2036 if (args->flags & ATTR_KERNOVAL) {
2037 args->valuelen = valuelen;
2038 return(0);
2040 if (args->valuelen < valuelen) {
2041 args->valuelen = valuelen;
2042 return(XFS_ERROR(ERANGE));
2044 args->valuelen = valuelen;
2045 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2046 } else {
2047 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2048 ASSERT(name_rmt->namelen == args->namelen);
2049 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2050 valuelen = be32_to_cpu(name_rmt->valuelen);
2051 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2052 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2053 if (args->flags & ATTR_KERNOVAL) {
2054 args->valuelen = valuelen;
2055 return(0);
2057 if (args->valuelen < valuelen) {
2058 args->valuelen = valuelen;
2059 return(XFS_ERROR(ERANGE));
2061 args->valuelen = valuelen;
2063 return(0);
2066 /*========================================================================
2067 * Utility routines.
2068 *========================================================================*/
2071 * Move the indicated entries from one leaf to another.
2072 * NOTE: this routine modifies both source and destination leaves.
2074 /*ARGSUSED*/
2075 STATIC void
2076 xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2077 xfs_attr_leafblock_t *leaf_d, int start_d,
2078 int count, xfs_mount_t *mp)
2080 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2081 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2082 int desti, tmp, i;
2085 * Check for nothing to do.
2087 if (count == 0)
2088 return;
2091 * Set up environment.
2093 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2094 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2095 hdr_s = &leaf_s->hdr;
2096 hdr_d = &leaf_d->hdr;
2097 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2098 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2099 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2100 ((be16_to_cpu(hdr_s->count)
2101 * sizeof(*entry_s))+sizeof(*hdr_s)));
2102 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2103 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2104 ((be16_to_cpu(hdr_d->count)
2105 * sizeof(*entry_d))+sizeof(*hdr_d)));
2107 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2108 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2109 ASSERT(count <= be16_to_cpu(hdr_s->count));
2112 * Move the entries in the destination leaf up to make a hole?
2114 if (start_d < be16_to_cpu(hdr_d->count)) {
2115 tmp = be16_to_cpu(hdr_d->count) - start_d;
2116 tmp *= sizeof(xfs_attr_leaf_entry_t);
2117 entry_s = &leaf_d->entries[start_d];
2118 entry_d = &leaf_d->entries[start_d + count];
2119 memmove((char *)entry_d, (char *)entry_s, tmp);
2123 * Copy all entry's in the same (sorted) order,
2124 * but allocate attribute info packed and in sequence.
2126 entry_s = &leaf_s->entries[start_s];
2127 entry_d = &leaf_d->entries[start_d];
2128 desti = start_d;
2129 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2130 ASSERT(be16_to_cpu(entry_s->nameidx)
2131 >= be16_to_cpu(hdr_s->firstused));
2132 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2133 #ifdef GROT
2135 * Code to drop INCOMPLETE entries. Difficult to use as we
2136 * may also need to change the insertion index. Code turned
2137 * off for 6.2, should be revisited later.
2139 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2140 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2141 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2142 be16_add_cpu(&hdr_s->count, -1);
2143 entry_d--; /* to compensate for ++ in loop hdr */
2144 desti--;
2145 if ((start_s + i) < offset)
2146 result++; /* insertion index adjustment */
2147 } else {
2148 #endif /* GROT */
2149 be16_add_cpu(&hdr_d->firstused, -tmp);
2150 /* both on-disk, don't endian flip twice */
2151 entry_d->hashval = entry_s->hashval;
2152 /* both on-disk, don't endian flip twice */
2153 entry_d->nameidx = hdr_d->firstused;
2154 entry_d->flags = entry_s->flags;
2155 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2156 <= XFS_LBSIZE(mp));
2157 memmove(xfs_attr_leaf_name(leaf_d, desti),
2158 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
2159 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2160 <= XFS_LBSIZE(mp));
2161 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
2162 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2163 be16_add_cpu(&hdr_d->usedbytes, tmp);
2164 be16_add_cpu(&hdr_s->count, -1);
2165 be16_add_cpu(&hdr_d->count, 1);
2166 tmp = be16_to_cpu(hdr_d->count)
2167 * sizeof(xfs_attr_leaf_entry_t)
2168 + sizeof(xfs_attr_leaf_hdr_t);
2169 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
2170 #ifdef GROT
2172 #endif /* GROT */
2176 * Zero out the entries we just copied.
2178 if (start_s == be16_to_cpu(hdr_s->count)) {
2179 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2180 entry_s = &leaf_s->entries[start_s];
2181 ASSERT(((char *)entry_s + tmp) <=
2182 ((char *)leaf_s + XFS_LBSIZE(mp)));
2183 memset((char *)entry_s, 0, tmp);
2184 } else {
2186 * Move the remaining entries down to fill the hole,
2187 * then zero the entries at the top.
2189 tmp = be16_to_cpu(hdr_s->count) - count;
2190 tmp *= sizeof(xfs_attr_leaf_entry_t);
2191 entry_s = &leaf_s->entries[start_s + count];
2192 entry_d = &leaf_s->entries[start_s];
2193 memmove((char *)entry_d, (char *)entry_s, tmp);
2195 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2196 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
2197 ASSERT(((char *)entry_s + tmp) <=
2198 ((char *)leaf_s + XFS_LBSIZE(mp)));
2199 memset((char *)entry_s, 0, tmp);
2203 * Fill in the freemap information
2205 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2206 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2207 sizeof(xfs_attr_leaf_entry_t));
2208 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2209 - be16_to_cpu(hdr_d->freemap[0].base));
2210 hdr_d->freemap[1].base = 0;
2211 hdr_d->freemap[2].base = 0;
2212 hdr_d->freemap[1].size = 0;
2213 hdr_d->freemap[2].size = 0;
2214 hdr_s->holes = 1; /* leaf may not be compact */
2218 * Compare two leaf blocks "order".
2219 * Return 0 unless leaf2 should go before leaf1.
2222 xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2224 xfs_attr_leafblock_t *leaf1, *leaf2;
2226 leaf1 = leaf1_bp->data;
2227 leaf2 = leaf2_bp->data;
2228 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2229 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
2230 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2231 (be16_to_cpu(leaf2->hdr.count) > 0) &&
2232 ((be32_to_cpu(leaf2->entries[0].hashval) <
2233 be32_to_cpu(leaf1->entries[0].hashval)) ||
2234 (be32_to_cpu(leaf2->entries[
2235 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2236 be32_to_cpu(leaf1->entries[
2237 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
2238 return(1);
2240 return(0);
2244 * Pick up the last hashvalue from a leaf block.
2246 xfs_dahash_t
2247 xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2249 xfs_attr_leafblock_t *leaf;
2251 leaf = bp->data;
2252 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2253 if (count)
2254 *count = be16_to_cpu(leaf->hdr.count);
2255 if (!leaf->hdr.count)
2256 return(0);
2257 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
2261 * Calculate the number of bytes used to store the indicated attribute
2262 * (whether local or remote only calculate bytes in this block).
2264 STATIC int
2265 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2267 xfs_attr_leaf_name_local_t *name_loc;
2268 xfs_attr_leaf_name_remote_t *name_rmt;
2269 int size;
2271 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2272 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2273 name_loc = xfs_attr_leaf_name_local(leaf, index);
2274 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2275 be16_to_cpu(name_loc->valuelen));
2276 } else {
2277 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2278 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2280 return(size);
2284 * Calculate the number of bytes that would be required to store the new
2285 * attribute (whether local or remote only calculate bytes in this block).
2286 * This routine decides as a side effect whether the attribute will be
2287 * a "local" or a "remote" attribute.
2290 xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
2292 int size;
2294 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2295 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
2296 if (local) {
2297 *local = 1;
2299 } else {
2300 size = xfs_attr_leaf_entsize_remote(namelen);
2301 if (local) {
2302 *local = 0;
2305 return(size);
2309 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2312 xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2314 attrlist_cursor_kern_t *cursor;
2315 xfs_attr_leafblock_t *leaf;
2316 xfs_attr_leaf_entry_t *entry;
2317 int retval, i;
2319 ASSERT(bp != NULL);
2320 leaf = bp->data;
2321 cursor = context->cursor;
2322 cursor->initted = 1;
2324 trace_xfs_attr_list_leaf(context);
2327 * Re-find our place in the leaf block if this is a new syscall.
2329 if (context->resynch) {
2330 entry = &leaf->entries[0];
2331 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2332 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
2333 if (cursor->offset == context->dupcnt) {
2334 context->dupcnt = 0;
2335 break;
2337 context->dupcnt++;
2338 } else if (be32_to_cpu(entry->hashval) >
2339 cursor->hashval) {
2340 context->dupcnt = 0;
2341 break;
2344 if (i == be16_to_cpu(leaf->hdr.count)) {
2345 trace_xfs_attr_list_notfound(context);
2346 return(0);
2348 } else {
2349 entry = &leaf->entries[0];
2350 i = 0;
2352 context->resynch = 0;
2355 * We have found our place, start copying out the new attributes.
2357 retval = 0;
2358 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
2359 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2360 cursor->hashval = be32_to_cpu(entry->hashval);
2361 cursor->offset = 0;
2364 if (entry->flags & XFS_ATTR_INCOMPLETE)
2365 continue; /* skip incomplete entries */
2367 if (entry->flags & XFS_ATTR_LOCAL) {
2368 xfs_attr_leaf_name_local_t *name_loc =
2369 xfs_attr_leaf_name_local(leaf, i);
2371 retval = context->put_listent(context,
2372 entry->flags,
2373 name_loc->nameval,
2374 (int)name_loc->namelen,
2375 be16_to_cpu(name_loc->valuelen),
2376 &name_loc->nameval[name_loc->namelen]);
2377 if (retval)
2378 return retval;
2379 } else {
2380 xfs_attr_leaf_name_remote_t *name_rmt =
2381 xfs_attr_leaf_name_remote(leaf, i);
2383 int valuelen = be32_to_cpu(name_rmt->valuelen);
2385 if (context->put_value) {
2386 xfs_da_args_t args;
2388 memset((char *)&args, 0, sizeof(args));
2389 args.dp = context->dp;
2390 args.whichfork = XFS_ATTR_FORK;
2391 args.valuelen = valuelen;
2392 args.value = kmem_alloc(valuelen, KM_SLEEP);
2393 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2394 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2395 retval = xfs_attr_rmtval_get(&args);
2396 if (retval)
2397 return retval;
2398 retval = context->put_listent(context,
2399 entry->flags,
2400 name_rmt->name,
2401 (int)name_rmt->namelen,
2402 valuelen,
2403 args.value);
2404 kmem_free(args.value);
2405 } else {
2406 retval = context->put_listent(context,
2407 entry->flags,
2408 name_rmt->name,
2409 (int)name_rmt->namelen,
2410 valuelen,
2411 NULL);
2413 if (retval)
2414 return retval;
2416 if (context->seen_enough)
2417 break;
2418 cursor->offset++;
2420 trace_xfs_attr_list_leaf_end(context);
2421 return(retval);
2425 /*========================================================================
2426 * Manage the INCOMPLETE flag in a leaf entry
2427 *========================================================================*/
2430 * Clear the INCOMPLETE flag on an entry in a leaf block.
2433 xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2435 xfs_attr_leafblock_t *leaf;
2436 xfs_attr_leaf_entry_t *entry;
2437 xfs_attr_leaf_name_remote_t *name_rmt;
2438 xfs_dabuf_t *bp;
2439 int error;
2440 #ifdef DEBUG
2441 xfs_attr_leaf_name_local_t *name_loc;
2442 int namelen;
2443 char *name;
2444 #endif /* DEBUG */
2447 * Set up the operation.
2449 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2450 XFS_ATTR_FORK);
2451 if (error) {
2452 return(error);
2454 ASSERT(bp != NULL);
2456 leaf = bp->data;
2457 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2458 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2459 ASSERT(args->index >= 0);
2460 entry = &leaf->entries[ args->index ];
2461 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2463 #ifdef DEBUG
2464 if (entry->flags & XFS_ATTR_LOCAL) {
2465 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
2466 namelen = name_loc->namelen;
2467 name = (char *)name_loc->nameval;
2468 } else {
2469 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2470 namelen = name_rmt->namelen;
2471 name = (char *)name_rmt->name;
2473 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2474 ASSERT(namelen == args->namelen);
2475 ASSERT(memcmp(name, args->name, namelen) == 0);
2476 #endif /* DEBUG */
2478 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2479 xfs_da_log_buf(args->trans, bp,
2480 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2482 if (args->rmtblkno) {
2483 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2484 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2485 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2486 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2487 xfs_da_log_buf(args->trans, bp,
2488 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2490 xfs_da_buf_done(bp);
2493 * Commit the flag value change and start the next trans in series.
2495 return xfs_trans_roll(&args->trans, args->dp);
2499 * Set the INCOMPLETE flag on an entry in a leaf block.
2502 xfs_attr_leaf_setflag(xfs_da_args_t *args)
2504 xfs_attr_leafblock_t *leaf;
2505 xfs_attr_leaf_entry_t *entry;
2506 xfs_attr_leaf_name_remote_t *name_rmt;
2507 xfs_dabuf_t *bp;
2508 int error;
2511 * Set up the operation.
2513 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2514 XFS_ATTR_FORK);
2515 if (error) {
2516 return(error);
2518 ASSERT(bp != NULL);
2520 leaf = bp->data;
2521 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2522 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
2523 ASSERT(args->index >= 0);
2524 entry = &leaf->entries[ args->index ];
2526 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2527 entry->flags |= XFS_ATTR_INCOMPLETE;
2528 xfs_da_log_buf(args->trans, bp,
2529 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2530 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2531 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
2532 name_rmt->valueblk = 0;
2533 name_rmt->valuelen = 0;
2534 xfs_da_log_buf(args->trans, bp,
2535 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2537 xfs_da_buf_done(bp);
2540 * Commit the flag value change and start the next trans in series.
2542 return xfs_trans_roll(&args->trans, args->dp);
2546 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2547 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2548 * entry given by args->blkno2/index2.
2550 * Note that they could be in different blocks, or in the same block.
2553 xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2555 xfs_attr_leafblock_t *leaf1, *leaf2;
2556 xfs_attr_leaf_entry_t *entry1, *entry2;
2557 xfs_attr_leaf_name_remote_t *name_rmt;
2558 xfs_dabuf_t *bp1, *bp2;
2559 int error;
2560 #ifdef DEBUG
2561 xfs_attr_leaf_name_local_t *name_loc;
2562 int namelen1, namelen2;
2563 char *name1, *name2;
2564 #endif /* DEBUG */
2567 * Read the block containing the "old" attr
2569 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2570 XFS_ATTR_FORK);
2571 if (error) {
2572 return(error);
2574 ASSERT(bp1 != NULL);
2577 * Read the block containing the "new" attr, if it is different
2579 if (args->blkno2 != args->blkno) {
2580 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2581 -1, &bp2, XFS_ATTR_FORK);
2582 if (error) {
2583 return(error);
2585 ASSERT(bp2 != NULL);
2586 } else {
2587 bp2 = bp1;
2590 leaf1 = bp1->data;
2591 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2592 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
2593 ASSERT(args->index >= 0);
2594 entry1 = &leaf1->entries[ args->index ];
2596 leaf2 = bp2->data;
2597 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2598 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
2599 ASSERT(args->index2 >= 0);
2600 entry2 = &leaf2->entries[ args->index2 ];
2602 #ifdef DEBUG
2603 if (entry1->flags & XFS_ATTR_LOCAL) {
2604 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
2605 namelen1 = name_loc->namelen;
2606 name1 = (char *)name_loc->nameval;
2607 } else {
2608 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2609 namelen1 = name_rmt->namelen;
2610 name1 = (char *)name_rmt->name;
2612 if (entry2->flags & XFS_ATTR_LOCAL) {
2613 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
2614 namelen2 = name_loc->namelen;
2615 name2 = (char *)name_loc->nameval;
2616 } else {
2617 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2618 namelen2 = name_rmt->namelen;
2619 name2 = (char *)name_rmt->name;
2621 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2622 ASSERT(namelen1 == namelen2);
2623 ASSERT(memcmp(name1, name2, namelen1) == 0);
2624 #endif /* DEBUG */
2626 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2627 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2629 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2630 xfs_da_log_buf(args->trans, bp1,
2631 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2632 if (args->rmtblkno) {
2633 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2634 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
2635 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2636 name_rmt->valuelen = cpu_to_be32(args->valuelen);
2637 xfs_da_log_buf(args->trans, bp1,
2638 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2641 entry2->flags |= XFS_ATTR_INCOMPLETE;
2642 xfs_da_log_buf(args->trans, bp2,
2643 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2644 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2645 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
2646 name_rmt->valueblk = 0;
2647 name_rmt->valuelen = 0;
2648 xfs_da_log_buf(args->trans, bp2,
2649 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2651 xfs_da_buf_done(bp1);
2652 if (bp1 != bp2)
2653 xfs_da_buf_done(bp2);
2656 * Commit the flag value change and start the next trans in series.
2658 error = xfs_trans_roll(&args->trans, args->dp);
2660 return(error);
2663 /*========================================================================
2664 * Indiscriminately delete the entire attribute fork
2665 *========================================================================*/
2668 * Recurse (gasp!) through the attribute nodes until we find leaves.
2669 * We're doing a depth-first traversal in order to invalidate everything.
2672 xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2674 xfs_da_blkinfo_t *info;
2675 xfs_daddr_t blkno;
2676 xfs_dabuf_t *bp;
2677 int error;
2680 * Read block 0 to see what we have to work with.
2681 * We only get here if we have extents, since we remove
2682 * the extents in reverse order the extent containing
2683 * block 0 must still be there.
2685 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2686 if (error)
2687 return(error);
2688 blkno = xfs_da_blkno(bp);
2691 * Invalidate the tree, even if the "tree" is only a single leaf block.
2692 * This is a depth-first traversal!
2694 info = bp->data;
2695 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2696 error = xfs_attr_node_inactive(trans, dp, bp, 1);
2697 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2698 error = xfs_attr_leaf_inactive(trans, dp, bp);
2699 } else {
2700 error = XFS_ERROR(EIO);
2701 xfs_da_brelse(*trans, bp);
2703 if (error)
2704 return(error);
2707 * Invalidate the incore copy of the root block.
2709 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2710 if (error)
2711 return(error);
2712 xfs_da_binval(*trans, bp); /* remove from cache */
2714 * Commit the invalidate and start the next transaction.
2716 error = xfs_trans_roll(trans, dp);
2718 return (error);
2722 * Recurse (gasp!) through the attribute nodes until we find leaves.
2723 * We're doing a depth-first traversal in order to invalidate everything.
2725 STATIC int
2726 xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2727 int level)
2729 xfs_da_blkinfo_t *info;
2730 xfs_da_intnode_t *node;
2731 xfs_dablk_t child_fsb;
2732 xfs_daddr_t parent_blkno, child_blkno;
2733 int error, count, i;
2734 xfs_dabuf_t *child_bp;
2737 * Since this code is recursive (gasp!) we must protect ourselves.
2739 if (level > XFS_DA_NODE_MAXDEPTH) {
2740 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2741 return(XFS_ERROR(EIO));
2744 node = bp->data;
2745 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
2746 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
2747 count = be16_to_cpu(node->hdr.count);
2748 if (!count) {
2749 xfs_da_brelse(*trans, bp);
2750 return(0);
2752 child_fsb = be32_to_cpu(node->btree[0].before);
2753 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2756 * If this is the node level just above the leaves, simply loop
2757 * over the leaves removing all of them. If this is higher up
2758 * in the tree, recurse downward.
2760 for (i = 0; i < count; i++) {
2762 * Read the subsidiary block to see what we have to work with.
2763 * Don't do this in a transaction. This is a depth-first
2764 * traversal of the tree so we may deal with many blocks
2765 * before we come back to this one.
2767 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2768 XFS_ATTR_FORK);
2769 if (error)
2770 return(error);
2771 if (child_bp) {
2772 /* save for re-read later */
2773 child_blkno = xfs_da_blkno(child_bp);
2776 * Invalidate the subtree, however we have to.
2778 info = child_bp->data;
2779 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
2780 error = xfs_attr_node_inactive(trans, dp,
2781 child_bp, level+1);
2782 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
2783 error = xfs_attr_leaf_inactive(trans, dp,
2784 child_bp);
2785 } else {
2786 error = XFS_ERROR(EIO);
2787 xfs_da_brelse(*trans, child_bp);
2789 if (error)
2790 return(error);
2793 * Remove the subsidiary block from the cache
2794 * and from the log.
2796 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2797 &child_bp, XFS_ATTR_FORK);
2798 if (error)
2799 return(error);
2800 xfs_da_binval(*trans, child_bp);
2804 * If we're not done, re-read the parent to get the next
2805 * child block number.
2807 if ((i+1) < count) {
2808 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2809 &bp, XFS_ATTR_FORK);
2810 if (error)
2811 return(error);
2812 child_fsb = be32_to_cpu(node->btree[i+1].before);
2813 xfs_da_brelse(*trans, bp);
2816 * Atomically commit the whole invalidate stuff.
2818 error = xfs_trans_roll(trans, dp);
2819 if (error)
2820 return (error);
2823 return(0);
2827 * Invalidate all of the "remote" value regions pointed to by a particular
2828 * leaf block.
2829 * Note that we must release the lock on the buffer so that we are not
2830 * caught holding something that the logging code wants to flush to disk.
2832 STATIC int
2833 xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2835 xfs_attr_leafblock_t *leaf;
2836 xfs_attr_leaf_entry_t *entry;
2837 xfs_attr_leaf_name_remote_t *name_rmt;
2838 xfs_attr_inactive_list_t *list, *lp;
2839 int error, count, size, tmp, i;
2841 leaf = bp->data;
2842 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2845 * Count the number of "remote" value extents.
2847 count = 0;
2848 entry = &leaf->entries[0];
2849 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2850 if (be16_to_cpu(entry->nameidx) &&
2851 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2852 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2853 if (name_rmt->valueblk)
2854 count++;
2859 * If there are no "remote" values, we're done.
2861 if (count == 0) {
2862 xfs_da_brelse(*trans, bp);
2863 return(0);
2867 * Allocate storage for a list of all the "remote" value extents.
2869 size = count * sizeof(xfs_attr_inactive_list_t);
2870 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2873 * Identify each of the "remote" value extents.
2875 lp = list;
2876 entry = &leaf->entries[0];
2877 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
2878 if (be16_to_cpu(entry->nameidx) &&
2879 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
2880 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
2881 if (name_rmt->valueblk) {
2882 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2883 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2884 be32_to_cpu(name_rmt->valuelen));
2885 lp++;
2889 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2892 * Invalidate each of the "remote" value extents.
2894 error = 0;
2895 for (lp = list, i = 0; i < count; i++, lp++) {
2896 tmp = xfs_attr_leaf_freextent(trans, dp,
2897 lp->valueblk, lp->valuelen);
2899 if (error == 0)
2900 error = tmp; /* save only the 1st errno */
2903 kmem_free((xfs_caddr_t)list);
2904 return(error);
2908 * Look at all the extents for this logical region,
2909 * invalidate any buffers that are incore/in transactions.
2911 STATIC int
2912 xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2913 xfs_dablk_t blkno, int blkcnt)
2915 xfs_bmbt_irec_t map;
2916 xfs_dablk_t tblkno;
2917 int tblkcnt, dblkcnt, nmap, error;
2918 xfs_daddr_t dblkno;
2919 xfs_buf_t *bp;
2922 * Roll through the "value", invalidating the attribute value's
2923 * blocks.
2925 tblkno = blkno;
2926 tblkcnt = blkcnt;
2927 while (tblkcnt > 0) {
2929 * Try to remember where we decided to put the value.
2931 nmap = 1;
2932 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2933 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2934 NULL, 0, &map, &nmap, NULL, NULL);
2935 if (error) {
2936 return(error);
2938 ASSERT(nmap == 1);
2939 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2942 * If it's a hole, these are already unmapped
2943 * so there's nothing to invalidate.
2945 if (map.br_startblock != HOLESTARTBLOCK) {
2947 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
2948 map.br_startblock);
2949 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
2950 map.br_blockcount);
2951 bp = xfs_trans_get_buf(*trans,
2952 dp->i_mount->m_ddev_targp,
2953 dblkno, dblkcnt, XBF_LOCK);
2954 xfs_trans_binval(*trans, bp);
2956 * Roll to next transaction.
2958 error = xfs_trans_roll(trans, dp);
2959 if (error)
2960 return (error);
2963 tblkno += map.br_blockcount;
2964 tblkcnt -= map.br_blockcount;
2967 return(0);