2 * Copyright (c) 2000-2003 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/
34 #include "xfs_types.h"
37 #include "xfs_trans.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_da_btree.h"
44 #include "xfs_bmap_btree.h"
45 #include "xfs_alloc_btree.h"
46 #include "xfs_ialloc_btree.h"
47 #include "xfs_dir_sf.h"
48 #include "xfs_dir2_sf.h"
49 #include "xfs_attr_sf.h"
50 #include "xfs_dinode.h"
51 #include "xfs_inode.h"
52 #include "xfs_inode_item.h"
53 #include "xfs_alloc.h"
54 #include "xfs_btree.h"
56 #include "xfs_dir_leaf.h"
57 #include "xfs_error.h"
62 * Routines to implement leaf blocks of directories as Btrees of hashed names.
65 /*========================================================================
66 * Function prototypes for the kernel.
67 *========================================================================*/
70 * Routines used for growing the Btree.
72 STATIC void xfs_dir_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
75 STATIC int xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer,
76 int musthave, int justcheck);
77 STATIC void xfs_dir_leaf_rebalance(xfs_da_state_t *state,
78 xfs_da_state_blk_t *blk1,
79 xfs_da_state_blk_t *blk2);
80 STATIC int xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
81 xfs_da_state_blk_t *leaf_blk_1,
82 xfs_da_state_blk_t *leaf_blk_2,
83 int *number_entries_in_blk1,
84 int *number_namebytes_in_blk1);
86 STATIC int xfs_dir_leaf_create(struct xfs_da_args *args,
87 xfs_dablk_t which_block,
88 struct xfs_dabuf **bpp);
93 STATIC void xfs_dir_leaf_moveents(xfs_dir_leafblock_t *src_leaf,
95 xfs_dir_leafblock_t *dst_leaf,
96 int dst_start, int move_count,
100 /*========================================================================
101 * External routines when dirsize < XFS_IFORK_DSIZE(dp).
102 *========================================================================*/
106 * Validate a given inode number.
109 xfs_dir_ino_validate(xfs_mount_t *mp, xfs_ino_t ino)
111 xfs_agblock_t agblkno;
117 agno = XFS_INO_TO_AGNO(mp, ino);
118 agblkno = XFS_INO_TO_AGBNO(mp, ino);
119 ioff = XFS_INO_TO_OFFSET(mp, ino);
120 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
122 agno < mp->m_sb.sb_agcount &&
123 agblkno < mp->m_sb.sb_agblocks &&
125 ioff < (1 << mp->m_sb.sb_inopblog) &&
126 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
127 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
128 XFS_RANDOM_DIR_INO_VALIDATE))) {
129 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
130 (unsigned long long) ino);
131 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
132 return XFS_ERROR(EFSCORRUPTED);
138 * Create the initial contents of a shortform directory.
141 xfs_dir_shortform_create(xfs_da_args_t *args, xfs_ino_t parent)
143 xfs_dir_sf_hdr_t *hdr;
148 ASSERT(dp->i_d.di_size == 0);
149 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
150 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
151 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
152 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
153 dp->i_df.if_flags |= XFS_IFINLINE;
155 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
156 ASSERT(dp->i_df.if_bytes == 0);
157 xfs_idata_realloc(dp, sizeof(*hdr), XFS_DATA_FORK);
158 hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
159 XFS_DIR_SF_PUT_DIRINO(&parent, &hdr->parent);
162 dp->i_d.di_size = sizeof(*hdr);
163 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
168 * Add a name to the shortform directory structure.
169 * Overflow from the inode has already been checked for.
172 xfs_dir_shortform_addname(xfs_da_args_t *args)
174 xfs_dir_shortform_t *sf;
175 xfs_dir_sf_entry_t *sfe;
180 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
182 * Catch the case where the conversion from shortform to leaf
183 * failed part way through.
185 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
186 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
187 return XFS_ERROR(EIO);
189 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
190 ASSERT(dp->i_df.if_u1.if_data != NULL);
191 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
193 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
194 if (sfe->namelen == args->namelen &&
195 args->name[0] == sfe->name[0] &&
196 memcmp(args->name, sfe->name, args->namelen) == 0)
197 return(XFS_ERROR(EEXIST));
198 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
201 offset = (int)((char *)sfe - (char *)sf);
202 size = XFS_DIR_SF_ENTSIZE_BYNAME(args->namelen);
203 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
204 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
205 sfe = (xfs_dir_sf_entry_t *)((char *)sf + offset);
207 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
208 sfe->namelen = args->namelen;
209 memcpy(sfe->name, args->name, sfe->namelen);
210 INT_MOD(sf->hdr.count, ARCH_CONVERT, +1);
212 dp->i_d.di_size += size;
213 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
219 * Remove a name from the shortform directory structure.
222 xfs_dir_shortform_removename(xfs_da_args_t *args)
224 xfs_dir_shortform_t *sf;
225 xfs_dir_sf_entry_t *sfe;
226 int base, size = 0, i;
230 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
232 * Catch the case where the conversion from shortform to leaf
233 * failed part way through.
235 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
236 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
237 return XFS_ERROR(EIO);
239 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
240 ASSERT(dp->i_df.if_u1.if_data != NULL);
241 base = sizeof(xfs_dir_sf_hdr_t);
242 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
244 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
245 size = XFS_DIR_SF_ENTSIZE_BYENTRY(sfe);
246 if (sfe->namelen == args->namelen &&
247 sfe->name[0] == args->name[0] &&
248 memcmp(sfe->name, args->name, args->namelen) == 0)
251 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
254 ASSERT(args->oknoent);
255 return(XFS_ERROR(ENOENT));
258 if ((base + size) != dp->i_d.di_size) {
259 memmove(&((char *)sf)[base], &((char *)sf)[base+size],
260 dp->i_d.di_size - (base+size));
262 INT_MOD(sf->hdr.count, ARCH_CONVERT, -1);
264 xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
265 dp->i_d.di_size -= size;
266 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
272 * Look up a name in a shortform directory structure.
275 xfs_dir_shortform_lookup(xfs_da_args_t *args)
277 xfs_dir_shortform_t *sf;
278 xfs_dir_sf_entry_t *sfe;
283 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
285 * Catch the case where the conversion from shortform to leaf
286 * failed part way through.
288 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
289 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
290 return XFS_ERROR(EIO);
292 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
293 ASSERT(dp->i_df.if_u1.if_data != NULL);
294 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
295 if (args->namelen == 2 &&
296 args->name[0] == '.' && args->name[1] == '.') {
297 XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &args->inumber);
298 return(XFS_ERROR(EEXIST));
300 if (args->namelen == 1 && args->name[0] == '.') {
301 args->inumber = dp->i_ino;
302 return(XFS_ERROR(EEXIST));
305 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
306 if (sfe->namelen == args->namelen &&
307 sfe->name[0] == args->name[0] &&
308 memcmp(args->name, sfe->name, args->namelen) == 0) {
309 XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args->inumber);
310 return(XFS_ERROR(EEXIST));
312 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
314 ASSERT(args->oknoent);
315 return(XFS_ERROR(ENOENT));
319 * Convert from using the shortform to the leaf.
322 xfs_dir_shortform_to_leaf(xfs_da_args_t *iargs)
325 xfs_dir_shortform_t *sf;
326 xfs_dir_sf_entry_t *sfe;
336 * Catch the case where the conversion from shortform to leaf
337 * failed part way through.
339 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
340 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
341 return XFS_ERROR(EIO);
343 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
344 ASSERT(dp->i_df.if_u1.if_data != NULL);
345 size = dp->i_df.if_bytes;
346 tmpbuffer = kmem_alloc(size, KM_SLEEP);
347 ASSERT(tmpbuffer != NULL);
349 memcpy(tmpbuffer, dp->i_df.if_u1.if_data, size);
351 sf = (xfs_dir_shortform_t *)tmpbuffer;
352 XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &inumber);
354 xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
356 xfs_trans_log_inode(iargs->trans, dp, XFS_ILOG_CORE);
357 retval = xfs_da_grow_inode(iargs, &blkno);
362 retval = xfs_dir_leaf_create(iargs, blkno, &bp);
369 args.hashval = xfs_dir_hash_dot;
370 args.inumber = dp->i_ino;
372 args.firstblock = iargs->firstblock;
373 args.flist = iargs->flist;
374 args.total = iargs->total;
375 args.whichfork = XFS_DATA_FORK;
376 args.trans = iargs->trans;
378 args.addname = args.oknoent = 1;
379 retval = xfs_dir_leaf_addname(&args);
385 args.hashval = xfs_dir_hash_dotdot;
386 args.inumber = inumber;
387 retval = xfs_dir_leaf_addname(&args);
392 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
393 args.name = (char *)(sfe->name);
394 args.namelen = sfe->namelen;
395 args.hashval = xfs_da_hashname((char *)(sfe->name),
397 XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args.inumber);
398 retval = xfs_dir_leaf_addname(&args);
401 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
406 kmem_free(tmpbuffer, size);
411 xfs_dir_shortform_compare(const void *a, const void *b)
413 xfs_dir_sf_sort_t *sa, *sb;
415 sa = (xfs_dir_sf_sort_t *)a;
416 sb = (xfs_dir_sf_sort_t *)b;
417 if (sa->hash < sb->hash)
419 else if (sa->hash > sb->hash)
422 return sa->entno - sb->entno;
426 * Copy out directory entries for getdents(), for shortform directories.
430 xfs_dir_shortform_getdents(xfs_inode_t *dp, uio_t *uio, int *eofp,
431 xfs_dirent_t *dbp, xfs_dir_put_t put)
433 xfs_dir_shortform_t *sf;
434 xfs_dir_sf_entry_t *sfe;
435 int retval, i, sbsize, nsbuf, lastresid=0, want_entno;
437 xfs_dahash_t cookhash, hash;
438 xfs_dir_put_args_t p;
439 xfs_dir_sf_sort_t *sbuf, *sbp;
442 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
443 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
444 want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
445 nsbuf = INT_GET(sf->hdr.count, ARCH_CONVERT) + 2;
446 sbsize = (nsbuf + 1) * sizeof(*sbuf);
447 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
449 xfs_dir_trace_g_du("sf: start", dp, uio);
452 * Collect all the entries into the buffer.
457 sbp->hash = xfs_dir_hash_dot;
458 sbp->ino = dp->i_ino;
468 sbp->hash = xfs_dir_hash_dotdot;
469 sbp->ino = XFS_GET_DIR_INO8(sf->hdr.parent);
475 * Scan the directory data for the rest of the entries.
477 for (i = 0, sfe = &sf->list[0];
478 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
481 ((char *)sfe < (char *)sf) ||
482 ((char *)sfe >= ((char *)sf + dp->i_df.if_bytes)))) {
483 xfs_dir_trace_g_du("sf: corrupted", dp, uio);
484 XFS_CORRUPTION_ERROR("xfs_dir_shortform_getdents",
485 XFS_ERRLEVEL_LOW, mp, sfe);
486 kmem_free(sbuf, sbsize);
487 return XFS_ERROR(EFSCORRUPTED);
492 sbp->hash = xfs_da_hashname((char *)sfe->name, sfe->namelen);
493 sbp->ino = XFS_GET_DIR_INO8(sfe->inumber);
494 sbp->name = (char *)sfe->name;
495 sbp->namelen = sfe->namelen;
496 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
501 * Sort the entries on hash then entno.
503 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_dir_shortform_compare);
505 * Stuff in last entry.
508 sbp->hash = XFS_DA_MAXHASH;
511 * Figure out the sequence numbers in case there's a hash duplicate.
513 for (hash = sbuf->hash, sbp = sbuf + 1;
514 sbp < &sbuf[nsbuf + 1]; sbp++) {
515 if (sbp->hash == hash)
516 sbp->seqno = sbp[-1].seqno + 1;
522 * Set up put routine.
531 for (sbp = sbuf; sbp < &sbuf[nsbuf + 1]; sbp++) {
532 if (sbp->hash > cookhash ||
533 (sbp->hash == cookhash && sbp->seqno >= want_entno))
538 * Did we fail to find anything? We stop at the last entry,
539 * the one we put maxhash into.
541 if (sbp == &sbuf[nsbuf]) {
542 kmem_free(sbuf, sbsize);
543 xfs_dir_trace_g_du("sf: hash beyond end", dp, uio);
544 uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
550 * Loop putting entries into the user buffer.
552 while (sbp < &sbuf[nsbuf]) {
554 * Save the first resid in a run of equal-hashval entries
555 * so that we can back them out if they don't all fit.
557 if (sbp->seqno == 0 || sbp == sbuf)
558 lastresid = uio->uio_resid;
559 XFS_PUT_COOKIE(p.cook, mp, 0, sbp[1].seqno, sbp[1].hash);
562 p.ino += mp->m_inoadd;
565 p.namelen = sbp->namelen;
569 XFS_DA_MAKE_COOKIE(mp, 0, 0, sbp->hash);
570 kmem_free(sbuf, sbsize);
571 uio->uio_resid = lastresid;
572 xfs_dir_trace_g_du("sf: E-O-B", dp, uio);
577 kmem_free(sbuf, sbsize);
578 uio->uio_offset = p.cook.o;
580 xfs_dir_trace_g_du("sf: E-O-F", dp, uio);
585 * Look up a name in a shortform directory structure, replace the inode number.
588 xfs_dir_shortform_replace(xfs_da_args_t *args)
590 xfs_dir_shortform_t *sf;
591 xfs_dir_sf_entry_t *sfe;
596 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
598 * Catch the case where the conversion from shortform to leaf
599 * failed part way through.
601 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
602 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
603 return XFS_ERROR(EIO);
605 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
606 ASSERT(dp->i_df.if_u1.if_data != NULL);
607 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
608 if (args->namelen == 2 &&
609 args->name[0] == '.' && args->name[1] == '.') {
610 /* XXX - replace assert? */
611 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sf->hdr.parent);
612 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
615 ASSERT(args->namelen != 1 || args->name[0] != '.');
617 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
618 if (sfe->namelen == args->namelen &&
619 sfe->name[0] == args->name[0] &&
620 memcmp(args->name, sfe->name, args->namelen) == 0) {
621 ASSERT(memcmp((char *)&args->inumber,
622 (char *)&sfe->inumber, sizeof(xfs_ino_t)));
623 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
624 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
627 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
629 ASSERT(args->oknoent);
630 return(XFS_ERROR(ENOENT));
634 * Convert a leaf directory to shortform structure
637 xfs_dir_leaf_to_shortform(xfs_da_args_t *iargs)
639 xfs_dir_leafblock_t *leaf;
640 xfs_dir_leaf_hdr_t *hdr;
641 xfs_dir_leaf_entry_t *entry;
642 xfs_dir_leaf_name_t *namest;
651 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
652 ASSERT(tmpbuffer != NULL);
654 retval = xfs_da_read_buf(iargs->trans, iargs->dp, 0, -1, &bp,
659 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
660 leaf = (xfs_dir_leafblock_t *)tmpbuffer;
661 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
662 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
665 * Find and special case the parent inode number
668 entry = &leaf->entries[0];
669 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
670 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
671 if ((entry->namelen == 2) &&
672 (namest->name[0] == '.') &&
673 (namest->name[1] == '.')) {
674 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &parent);
676 } else if ((entry->namelen == 1) && (namest->name[0] == '.')) {
680 retval = xfs_da_shrink_inode(iargs, 0, bp);
683 retval = xfs_dir_shortform_create(iargs, parent);
688 * Copy the rest of the filenames
690 entry = &leaf->entries[0];
692 args.firstblock = iargs->firstblock;
693 args.flist = iargs->flist;
694 args.total = iargs->total;
695 args.whichfork = XFS_DATA_FORK;
696 args.trans = iargs->trans;
698 args.addname = args.oknoent = 1;
699 for (i = 0; i < INT_GET(hdr->count, ARCH_CONVERT); entry++, i++) {
702 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
703 args.name = (char *)(namest->name);
704 args.namelen = entry->namelen;
705 args.hashval = INT_GET(entry->hashval, ARCH_CONVERT);
706 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args.inumber);
707 xfs_dir_shortform_addname(&args);
711 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
716 * Convert from using a single leaf to a root node and a leaf.
719 xfs_dir_leaf_to_node(xfs_da_args_t *args)
721 xfs_dir_leafblock_t *leaf;
722 xfs_da_intnode_t *node;
724 xfs_dabuf_t *bp1, *bp2;
729 retval = xfs_da_grow_inode(args, &blkno);
733 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
738 retval = xfs_da_get_buf(args->trans, args->dp, 1, -1, &bp2,
741 xfs_da_buf_done(bp1);
745 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
746 xfs_da_buf_done(bp1);
747 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
750 * Set up the new root node.
752 retval = xfs_da_node_create(args, 0, 1, &bp1, XFS_DATA_FORK);
754 xfs_da_buf_done(bp2);
759 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
760 INT_SET(node->btree[0].hashval, ARCH_CONVERT, INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
761 xfs_da_buf_done(bp2);
762 INT_SET(node->btree[0].before, ARCH_CONVERT, blkno);
763 INT_SET(node->hdr.count, ARCH_CONVERT, 1);
764 xfs_da_log_buf(args->trans, bp1,
765 XFS_DA_LOGRANGE(node, &node->btree[0], sizeof(node->btree[0])));
766 xfs_da_buf_done(bp1);
772 /*========================================================================
773 * Routines used for growing the Btree.
774 *========================================================================*/
777 * Create the initial contents of a leaf directory
778 * or a leaf in a node directory.
781 xfs_dir_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
783 xfs_dir_leafblock_t *leaf;
784 xfs_dir_leaf_hdr_t *hdr;
791 retval = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp, XFS_DATA_FORK);
796 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
798 INT_SET(hdr->info.magic, ARCH_CONVERT, XFS_DIR_LEAF_MAGIC);
799 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
801 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount) - 1);
802 INT_SET(hdr->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
803 INT_SET(hdr->freemap[0].size, ARCH_CONVERT, INT_GET(hdr->firstused, ARCH_CONVERT) - INT_GET(hdr->freemap[0].base, ARCH_CONVERT));
805 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
812 * Split the leaf node, rebalance, then add the new entry.
815 xfs_dir_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
816 xfs_da_state_blk_t *newblk)
823 * Allocate space for a new leaf node.
826 ASSERT(args != NULL);
827 ASSERT(oldblk->magic == XFS_DIR_LEAF_MAGIC);
828 error = xfs_da_grow_inode(args, &blkno);
831 error = xfs_dir_leaf_create(args, blkno, &newblk->bp);
834 newblk->blkno = blkno;
835 newblk->magic = XFS_DIR_LEAF_MAGIC;
838 * Rebalance the entries across the two leaves.
840 xfs_dir_leaf_rebalance(state, oldblk, newblk);
841 error = xfs_da_blk_link(state, oldblk, newblk);
846 * Insert the new entry in the correct block.
849 error = xfs_dir_leaf_add(oldblk->bp, args, oldblk->index);
851 error = xfs_dir_leaf_add(newblk->bp, args, newblk->index);
855 * Update last hashval in each block since we added the name.
857 oldblk->hashval = xfs_dir_leaf_lasthash(oldblk->bp, NULL);
858 newblk->hashval = xfs_dir_leaf_lasthash(newblk->bp, NULL);
863 * Add a name to the leaf directory structure.
865 * Must take into account fragmented leaves and leaves where spacemap has
866 * lost some freespace information (ie: holes).
869 xfs_dir_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index)
871 xfs_dir_leafblock_t *leaf;
872 xfs_dir_leaf_hdr_t *hdr;
873 xfs_dir_leaf_map_t *map;
874 int tablesize, entsize, sum, i, tmp, error;
877 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
878 ASSERT((index >= 0) && (index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
880 entsize = XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen);
883 * Search through freemap for first-fit on new name length.
884 * (may need to figure in size of entry struct too)
886 tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1) * (uint)sizeof(xfs_dir_leaf_entry_t)
887 + (uint)sizeof(xfs_dir_leaf_hdr_t);
888 map = &hdr->freemap[XFS_DIR_LEAF_MAPSIZE-1];
889 for (sum = 0, i = XFS_DIR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
890 if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
891 sum += INT_GET(map->size, ARCH_CONVERT);
895 continue; /* no space in this map */
897 if (INT_GET(map->base, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
898 tmp += (uint)sizeof(xfs_dir_leaf_entry_t);
899 if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
900 if (!args->justcheck)
901 xfs_dir_leaf_add_work(bp, args, index, i);
904 sum += INT_GET(map->size, ARCH_CONVERT);
908 * If there are no holes in the address space of the block,
909 * and we don't have enough freespace, then compaction will do us
910 * no good and we should just give up.
912 if (!hdr->holes && (sum < entsize))
913 return(XFS_ERROR(ENOSPC));
916 * Compact the entries to coalesce free space.
917 * Pass the justcheck flag so the checking pass can return
918 * an error, without changing anything, if it won't fit.
920 error = xfs_dir_leaf_compact(args->trans, bp,
923 (uint)sizeof(xfs_dir_leaf_entry_t) : 0,
928 * After compaction, the block is guaranteed to have only one
929 * free region, in freemap[0]. If it is not big enough, give up.
931 if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT) <
932 (entsize + (uint)sizeof(xfs_dir_leaf_entry_t)))
933 return(XFS_ERROR(ENOSPC));
935 if (!args->justcheck)
936 xfs_dir_leaf_add_work(bp, args, index, 0);
941 * Add a name to a leaf directory structure.
944 xfs_dir_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int index,
947 xfs_dir_leafblock_t *leaf;
948 xfs_dir_leaf_hdr_t *hdr;
949 xfs_dir_leaf_entry_t *entry;
950 xfs_dir_leaf_name_t *namest;
951 xfs_dir_leaf_map_t *map;
957 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
959 ASSERT((mapindex >= 0) && (mapindex < XFS_DIR_LEAF_MAPSIZE));
960 ASSERT((index >= 0) && (index <= INT_GET(hdr->count, ARCH_CONVERT)));
963 * Force open some space in the entry array and fill it in.
965 entry = &leaf->entries[index];
966 if (index < INT_GET(hdr->count, ARCH_CONVERT)) {
967 tmp = INT_GET(hdr->count, ARCH_CONVERT) - index;
968 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
969 memmove(entry + 1, entry, tmp);
970 xfs_da_log_buf(args->trans, bp,
971 XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
973 INT_MOD(hdr->count, ARCH_CONVERT, +1);
976 * Allocate space for the new string (at the end of the run).
978 map = &hdr->freemap[mapindex];
979 mp = args->trans->t_mountp;
980 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
981 ASSERT(INT_GET(map->size, ARCH_CONVERT) >= XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen));
982 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
983 INT_MOD(map->size, ARCH_CONVERT, -(XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen)));
984 INT_SET(entry->nameidx, ARCH_CONVERT, INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT));
985 INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
986 entry->namelen = args->namelen;
987 xfs_da_log_buf(args->trans, bp,
988 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
991 * Copy the string and inode number into the new space.
993 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
994 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &namest->inumber);
995 memcpy(namest->name, args->name, args->namelen);
996 xfs_da_log_buf(args->trans, bp,
997 XFS_DA_LOGRANGE(leaf, namest, XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry)));
1000 * Update the control info for this leaf node
1002 if (INT_GET(entry->nameidx, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
1003 INT_COPY(hdr->firstused, entry->nameidx, ARCH_CONVERT);
1004 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
1005 tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1) * (uint)sizeof(xfs_dir_leaf_entry_t)
1006 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1007 map = &hdr->freemap[0];
1008 for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
1009 if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
1010 INT_MOD(map->base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
1011 INT_MOD(map->size, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1014 INT_MOD(hdr->namebytes, ARCH_CONVERT, args->namelen);
1015 xfs_da_log_buf(args->trans, bp,
1016 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1020 * Garbage collect a leaf directory block by copying it to a new buffer.
1023 xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp, int musthave,
1026 xfs_dir_leafblock_t *leaf_s, *leaf_d;
1027 xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1030 char *tmpbuffer2=NULL;
1034 mp = trans->t_mountp;
1035 lbsize = XFS_LBSIZE(mp);
1036 tmpbuffer = kmem_alloc(lbsize, KM_SLEEP);
1037 ASSERT(tmpbuffer != NULL);
1038 memcpy(tmpbuffer, bp->data, lbsize);
1041 * Make a second copy in case xfs_dir_leaf_moveents()
1042 * below destroys the original.
1044 if (musthave || justcheck) {
1045 tmpbuffer2 = kmem_alloc(lbsize, KM_SLEEP);
1046 memcpy(tmpbuffer2, bp->data, lbsize);
1048 memset(bp->data, 0, lbsize);
1051 * Copy basic information
1053 leaf_s = (xfs_dir_leafblock_t *)tmpbuffer;
1055 hdr_s = &leaf_s->hdr;
1056 hdr_d = &leaf_d->hdr;
1057 hdr_d->info = hdr_s->info; /* struct copy */
1058 INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize);
1059 if (!hdr_d->firstused)
1060 INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize - 1);
1061 hdr_d->namebytes = 0;
1064 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
1065 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT, INT_GET(hdr_d->firstused, ARCH_CONVERT) - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
1068 * Copy all entry's in the same (sorted) order,
1069 * but allocate filenames packed and in sequence.
1070 * This changes the source (leaf_s) as well.
1072 xfs_dir_leaf_moveents(leaf_s, 0, leaf_d, 0, (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
1074 if (musthave && INT_GET(hdr_d->freemap[0].size, ARCH_CONVERT) < musthave)
1075 rval = XFS_ERROR(ENOSPC);
1079 if (justcheck || rval == ENOSPC) {
1081 memcpy(bp->data, tmpbuffer2, lbsize);
1083 xfs_da_log_buf(trans, bp, 0, lbsize - 1);
1086 kmem_free(tmpbuffer, lbsize);
1087 if (musthave || justcheck)
1088 kmem_free(tmpbuffer2, lbsize);
1093 * Redistribute the directory entries between two leaf nodes,
1094 * taking into account the size of the new entry.
1096 * NOTE: if new block is empty, then it will get the upper half of old block.
1099 xfs_dir_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1100 xfs_da_state_blk_t *blk2)
1102 xfs_da_state_blk_t *tmp_blk;
1103 xfs_dir_leafblock_t *leaf1, *leaf2;
1104 xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1105 int count, totallen, max, space, swap;
1108 * Set up environment.
1110 ASSERT(blk1->magic == XFS_DIR_LEAF_MAGIC);
1111 ASSERT(blk2->magic == XFS_DIR_LEAF_MAGIC);
1112 leaf1 = blk1->bp->data;
1113 leaf2 = blk2->bp->data;
1114 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1115 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1118 * Check ordering of blocks, reverse if it makes things simpler.
1121 if (xfs_dir_leaf_order(blk1->bp, blk2->bp)) {
1125 leaf1 = blk1->bp->data;
1126 leaf2 = blk2->bp->data;
1133 * Examine entries until we reduce the absolute difference in
1134 * byte usage between the two blocks to a minimum. Then get
1135 * the direction to copy and the number of elements to move.
1137 state->inleaf = xfs_dir_leaf_figure_balance(state, blk1, blk2,
1140 state->inleaf = !state->inleaf;
1143 * Move any entries required from leaf to leaf:
1145 if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
1147 * Figure the total bytes to be added to the destination leaf.
1149 count = INT_GET(hdr1->count, ARCH_CONVERT) - count; /* number entries being moved */
1150 space = INT_GET(hdr1->namebytes, ARCH_CONVERT) - totallen;
1151 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1152 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1155 * leaf2 is the destination, compact it if it looks tight.
1157 max = INT_GET(hdr2->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1158 max -= INT_GET(hdr2->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1160 xfs_dir_leaf_compact(state->args->trans, blk2->bp,
1165 * Move high entries from leaf1 to low end of leaf2.
1167 xfs_dir_leaf_moveents(leaf1, INT_GET(hdr1->count, ARCH_CONVERT) - count,
1168 leaf2, 0, count, state->mp);
1170 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1171 state->blocksize-1);
1172 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1173 state->blocksize-1);
1175 } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
1177 * Figure the total bytes to be added to the destination leaf.
1179 count -= INT_GET(hdr1->count, ARCH_CONVERT); /* number entries being moved */
1180 space = totallen - INT_GET(hdr1->namebytes, ARCH_CONVERT);
1181 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1182 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1185 * leaf1 is the destination, compact it if it looks tight.
1187 max = INT_GET(hdr1->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1188 max -= INT_GET(hdr1->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1190 xfs_dir_leaf_compact(state->args->trans, blk1->bp,
1195 * Move low entries from leaf2 to high end of leaf1.
1197 xfs_dir_leaf_moveents(leaf2, 0, leaf1, (int)INT_GET(hdr1->count, ARCH_CONVERT),
1200 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1201 state->blocksize-1);
1202 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1203 state->blocksize-1);
1207 * Copy out last hashval in each block for B-tree code.
1209 blk1->hashval = INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1210 blk2->hashval = INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1213 * Adjust the expected index for insertion.
1214 * GROT: this doesn't work unless blk2 was originally empty.
1216 if (!state->inleaf) {
1217 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1222 * Examine entries until we reduce the absolute difference in
1223 * byte usage between the two blocks to a minimum.
1224 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1225 * GROT: there will always be enough room in either block for a new entry.
1226 * GROT: Do a double-split for this case?
1229 xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
1230 xfs_da_state_blk_t *blk1,
1231 xfs_da_state_blk_t *blk2,
1232 int *countarg, int *namebytesarg)
1234 xfs_dir_leafblock_t *leaf1, *leaf2;
1235 xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1236 xfs_dir_leaf_entry_t *entry;
1237 int count, max, totallen, half;
1238 int lastdelta, foundit, tmp;
1241 * Set up environment.
1243 leaf1 = blk1->bp->data;
1244 leaf2 = blk2->bp->data;
1251 * Examine entries until we reduce the absolute difference in
1252 * byte usage between the two blocks to a minimum.
1254 max = INT_GET(hdr1->count, ARCH_CONVERT) + INT_GET(hdr2->count, ARCH_CONVERT);
1255 half = (max+1) * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1256 half += INT_GET(hdr1->namebytes, ARCH_CONVERT) + INT_GET(hdr2->namebytes, ARCH_CONVERT) + state->args->namelen;
1258 lastdelta = state->blocksize;
1259 entry = &leaf1->entries[0];
1260 for (count = 0; count < max; entry++, count++) {
1262 #define XFS_DIR_ABS(A) (((A) < 0) ? -(A) : (A))
1264 * The new entry is in the first block, account for it.
1266 if (count == blk1->index) {
1267 tmp = totallen + (uint)sizeof(*entry)
1268 + XFS_DIR_LEAF_ENTSIZE_BYNAME(state->args->namelen);
1269 if (XFS_DIR_ABS(half - tmp) > lastdelta)
1271 lastdelta = XFS_DIR_ABS(half - tmp);
1277 * Wrap around into the second block if necessary.
1279 if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
1281 entry = &leaf1->entries[0];
1285 * Figure out if next leaf entry would be too much.
1287 tmp = totallen + (uint)sizeof(*entry)
1288 + XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1289 if (XFS_DIR_ABS(half - tmp) > lastdelta)
1291 lastdelta = XFS_DIR_ABS(half - tmp);
1297 * Calculate the number of namebytes that will end up in lower block.
1298 * If new entry not in lower block, fix up the count.
1301 count * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1303 totallen -= (sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1) +
1304 state->args->namelen;
1308 *namebytesarg = totallen;
1312 /*========================================================================
1313 * Routines used for shrinking the Btree.
1314 *========================================================================*/
1317 * Check a leaf block and its neighbors to see if the block should be
1318 * collapsed into one or the other neighbor. Always keep the block
1319 * with the smaller block number.
1320 * If the current block is over 50% full, don't try to join it, return 0.
1321 * If the block is empty, fill in the state structure and return 2.
1322 * If it can be collapsed, fill in the state structure and return 1.
1323 * If nothing can be done, return 0.
1326 xfs_dir_leaf_toosmall(xfs_da_state_t *state, int *action)
1328 xfs_dir_leafblock_t *leaf;
1329 xfs_da_state_blk_t *blk;
1330 xfs_da_blkinfo_t *info;
1331 int count, bytes, forward, error, retval, i;
1336 * Check for the degenerate case of the block being over 50% full.
1337 * If so, it's not worth even looking to see if we might be able
1338 * to coalesce with a sibling.
1340 blk = &state->path.blk[ state->path.active-1 ];
1341 info = blk->bp->data;
1342 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1343 leaf = (xfs_dir_leafblock_t *)info;
1344 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1345 bytes = (uint)sizeof(xfs_dir_leaf_hdr_t) +
1346 count * (uint)sizeof(xfs_dir_leaf_entry_t) +
1347 count * ((uint)sizeof(xfs_dir_leaf_name_t)-1) +
1348 INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1349 if (bytes > (state->blocksize >> 1)) {
1350 *action = 0; /* blk over 50%, don't try to join */
1355 * Check for the degenerate case of the block being empty.
1356 * If the block is empty, we'll simply delete it, no need to
1357 * coalesce it with a sibling block. We choose (aribtrarily)
1358 * to merge with the forward block unless it is NULL.
1362 * Make altpath point to the block we want to keep and
1363 * path point to the block we want to drop (this one).
1365 forward = info->forw;
1366 memcpy(&state->altpath, &state->path, sizeof(state->path));
1367 error = xfs_da_path_shift(state, &state->altpath, forward,
1380 * Examine each sibling block to see if we can coalesce with
1381 * at least 25% free space to spare. We need to figure out
1382 * whether to merge with the forward or the backward block.
1383 * We prefer coalescing with the lower numbered sibling so as
1384 * to shrink a directory over time.
1386 forward = (INT_GET(info->forw, ARCH_CONVERT) < INT_GET(info->back, ARCH_CONVERT)); /* start with smaller blk num */
1387 for (i = 0; i < 2; forward = !forward, i++) {
1389 blkno = INT_GET(info->forw, ARCH_CONVERT);
1391 blkno = INT_GET(info->back, ARCH_CONVERT);
1394 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1401 leaf = (xfs_dir_leafblock_t *)info;
1402 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1403 bytes = state->blocksize - (state->blocksize>>2);
1404 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1406 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1407 count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
1408 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1409 bytes -= count * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1410 bytes -= count * (uint)sizeof(xfs_dir_leaf_entry_t);
1411 bytes -= (uint)sizeof(xfs_dir_leaf_hdr_t);
1413 break; /* fits with at least 25% to spare */
1415 xfs_da_brelse(state->args->trans, bp);
1421 xfs_da_buf_done(bp);
1424 * Make altpath point to the block we want to keep (the lower
1425 * numbered block) and path point to the block we want to drop.
1427 memcpy(&state->altpath, &state->path, sizeof(state->path));
1428 if (blkno < blk->blkno) {
1429 error = xfs_da_path_shift(state, &state->altpath, forward,
1432 error = xfs_da_path_shift(state, &state->path, forward,
1446 * Remove a name from the leaf directory structure.
1448 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1449 * If two leaves are 37% full, when combined they will leave 25% free.
1452 xfs_dir_leaf_remove(xfs_trans_t *trans, xfs_dabuf_t *bp, int index)
1454 xfs_dir_leafblock_t *leaf;
1455 xfs_dir_leaf_hdr_t *hdr;
1456 xfs_dir_leaf_map_t *map;
1457 xfs_dir_leaf_entry_t *entry;
1458 xfs_dir_leaf_name_t *namest;
1459 int before, after, smallest, entsize;
1460 int tablesize, tmp, i;
1464 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1466 mp = trans->t_mountp;
1467 ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0) && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1468 ASSERT((index >= 0) && (index < INT_GET(hdr->count, ARCH_CONVERT)));
1469 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
1470 entry = &leaf->entries[index];
1471 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1472 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1475 * Scan through free region table:
1476 * check for adjacency of free'd entry with an existing one,
1477 * find smallest free region in case we need to replace it,
1478 * adjust any map that borders the entry table,
1480 tablesize = INT_GET(hdr->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1481 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1482 map = &hdr->freemap[0];
1483 tmp = INT_GET(map->size, ARCH_CONVERT);
1484 before = after = -1;
1485 smallest = XFS_DIR_LEAF_MAPSIZE - 1;
1486 entsize = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1487 for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
1488 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1489 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1490 if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
1491 INT_MOD(map->base, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1492 INT_MOD(map->size, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
1495 if ((INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT)) == INT_GET(entry->nameidx, ARCH_CONVERT)) {
1497 } else if (INT_GET(map->base, ARCH_CONVERT) == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
1499 } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
1500 tmp = INT_GET(map->size, ARCH_CONVERT);
1506 * Coalesce adjacent freemap regions,
1507 * or replace the smallest region.
1509 if ((before >= 0) || (after >= 0)) {
1510 if ((before >= 0) && (after >= 0)) {
1511 map = &hdr->freemap[before];
1512 INT_MOD(map->size, ARCH_CONVERT, entsize);
1513 INT_MOD(map->size, ARCH_CONVERT, INT_GET(hdr->freemap[after].size, ARCH_CONVERT));
1514 hdr->freemap[after].base = 0;
1515 hdr->freemap[after].size = 0;
1516 } else if (before >= 0) {
1517 map = &hdr->freemap[before];
1518 INT_MOD(map->size, ARCH_CONVERT, entsize);
1520 map = &hdr->freemap[after];
1521 INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1522 INT_MOD(map->size, ARCH_CONVERT, entsize);
1526 * Replace smallest region (if it is smaller than free'd entry)
1528 map = &hdr->freemap[smallest];
1529 if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
1530 INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1531 INT_SET(map->size, ARCH_CONVERT, entsize);
1536 * Did we remove the first entry?
1538 if (INT_GET(entry->nameidx, ARCH_CONVERT) == INT_GET(hdr->firstused, ARCH_CONVERT))
1544 * Compress the remaining entries and zero out the removed stuff.
1546 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1547 memset((char *)namest, 0, entsize);
1548 xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, namest, entsize));
1550 INT_MOD(hdr->namebytes, ARCH_CONVERT, -(entry->namelen));
1551 tmp = (INT_GET(hdr->count, ARCH_CONVERT) - index) * (uint)sizeof(xfs_dir_leaf_entry_t);
1552 memmove(entry, entry + 1, tmp);
1553 INT_MOD(hdr->count, ARCH_CONVERT, -1);
1554 xfs_da_log_buf(trans, bp,
1555 XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
1556 entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
1557 memset((char *)entry, 0, sizeof(xfs_dir_leaf_entry_t));
1560 * If we removed the first entry, re-find the first used byte
1561 * in the name area. Note that if the entry was the "firstused",
1562 * then we don't have a "hole" in our block resulting from
1563 * removing the name.
1566 tmp = XFS_LBSIZE(mp);
1567 entry = &leaf->entries[0];
1568 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
1569 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1570 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1571 if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
1572 tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
1574 INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
1575 if (!hdr->firstused)
1576 INT_SET(hdr->firstused, ARCH_CONVERT, tmp - 1);
1578 hdr->holes = 1; /* mark as needing compaction */
1581 xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1584 * Check if leaf is less than 50% full, caller may want to
1585 * "join" the leaf with a sibling if so.
1587 tmp = (uint)sizeof(xfs_dir_leaf_hdr_t);
1588 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1589 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1590 tmp += INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1591 if (tmp < mp->m_dir_magicpct)
1592 return(1); /* leaf is < 37% full */
1597 * Move all the directory entries from drop_leaf into save_leaf.
1600 xfs_dir_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1601 xfs_da_state_blk_t *save_blk)
1603 xfs_dir_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1604 xfs_dir_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1609 * Set up environment.
1612 ASSERT(drop_blk->magic == XFS_DIR_LEAF_MAGIC);
1613 ASSERT(save_blk->magic == XFS_DIR_LEAF_MAGIC);
1614 drop_leaf = drop_blk->bp->data;
1615 save_leaf = save_blk->bp->data;
1616 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1617 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1618 drop_hdr = &drop_leaf->hdr;
1619 save_hdr = &save_leaf->hdr;
1622 * Save last hashval from dying block for later Btree fixup.
1624 drop_blk->hashval = INT_GET(drop_leaf->entries[ drop_leaf->hdr.count-1 ].hashval, ARCH_CONVERT);
1627 * Check if we need a temp buffer, or can we do it in place.
1628 * Note that we don't check "leaf" for holes because we will
1629 * always be dropping it, toosmall() decided that for us already.
1631 if (save_hdr->holes == 0) {
1633 * dest leaf has no holes, so we add there. May need
1634 * to make some room in the entry array.
1636 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1637 xfs_dir_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1638 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1640 xfs_dir_leaf_moveents(drop_leaf, 0,
1641 save_leaf, INT_GET(save_hdr->count, ARCH_CONVERT),
1642 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1646 * Destination has holes, so we make a temporary copy
1647 * of the leaf and add them both to that.
1649 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1650 ASSERT(tmpbuffer != NULL);
1651 memset(tmpbuffer, 0, state->blocksize);
1652 tmp_leaf = (xfs_dir_leafblock_t *)tmpbuffer;
1653 tmp_hdr = &tmp_leaf->hdr;
1654 tmp_hdr->info = save_hdr->info; /* struct copy */
1656 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
1657 if (!tmp_hdr->firstused)
1658 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize - 1);
1659 tmp_hdr->namebytes = 0;
1660 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1661 xfs_dir_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1662 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1663 xfs_dir_leaf_moveents(save_leaf, 0,
1664 tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1665 (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1667 xfs_dir_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1668 (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1669 xfs_dir_leaf_moveents(drop_leaf, 0,
1670 tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1671 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1673 memcpy(save_leaf, tmp_leaf, state->blocksize);
1674 kmem_free(tmpbuffer, state->blocksize);
1677 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1678 state->blocksize - 1);
1681 * Copy out last hashval in each block for B-tree code.
1683 save_blk->hashval = INT_GET(save_leaf->entries[ INT_GET(save_leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1686 /*========================================================================
1687 * Routines used for finding things in the Btree.
1688 *========================================================================*/
1691 * Look up a name in a leaf directory structure.
1692 * This is the internal routine, it uses the caller's buffer.
1694 * Note that duplicate keys are allowed, but only check within the
1695 * current leaf node. The Btree code must check in adjacent leaf nodes.
1697 * Return in *index the index into the entry[] array of either the found
1698 * entry, or where the entry should have been (insert before that entry).
1700 * Don't change the args->inumber unless we find the filename.
1703 xfs_dir_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args, int *index)
1705 xfs_dir_leafblock_t *leaf;
1706 xfs_dir_leaf_entry_t *entry;
1707 xfs_dir_leaf_name_t *namest;
1709 xfs_dahash_t hashval;
1712 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1713 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) < (XFS_LBSIZE(args->dp->i_mount)/8));
1716 * Binary search. (note: small blocks will skip this loop)
1718 hashval = args->hashval;
1719 probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
1720 for (entry = &leaf->entries[probe]; span > 4;
1721 entry = &leaf->entries[probe]) {
1723 if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
1725 else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
1730 ASSERT((probe >= 0) && \
1731 ((!leaf->hdr.count) || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
1732 ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT) == hashval));
1735 * Since we may have duplicate hashval's, find the first matching
1736 * hashval in the leaf.
1738 while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT) >= hashval)) {
1742 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
1746 if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT)) || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
1748 ASSERT(args->oknoent);
1749 return(XFS_ERROR(ENOENT));
1753 * Duplicate keys may be present, so search all of them for a match.
1755 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval)) {
1756 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1757 if (entry->namelen == args->namelen &&
1758 namest->name[0] == args->name[0] &&
1759 memcmp(args->name, namest->name, args->namelen) == 0) {
1760 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args->inumber);
1762 return(XFS_ERROR(EEXIST));
1768 ASSERT(probe == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
1769 return(XFS_ERROR(ENOENT));
1772 /*========================================================================
1774 *========================================================================*/
1777 * Move the indicated entries from one leaf to another.
1778 * NOTE: this routine modifies both source and destination leaves.
1782 xfs_dir_leaf_moveents(xfs_dir_leafblock_t *leaf_s, int start_s,
1783 xfs_dir_leafblock_t *leaf_d, int start_d,
1784 int count, xfs_mount_t *mp)
1786 xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1787 xfs_dir_leaf_entry_t *entry_s, *entry_d;
1791 * Check for nothing to do.
1797 * Set up environment.
1799 ASSERT(INT_GET(leaf_s->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1800 ASSERT(INT_GET(leaf_d->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1801 hdr_s = &leaf_s->hdr;
1802 hdr_d = &leaf_d->hdr;
1803 ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0) && (INT_GET(hdr_s->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1804 ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
1805 ((INT_GET(hdr_s->count, ARCH_CONVERT)*sizeof(*entry_s))+sizeof(*hdr_s)));
1806 ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
1807 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
1808 ((INT_GET(hdr_d->count, ARCH_CONVERT)*sizeof(*entry_d))+sizeof(*hdr_d)));
1810 ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
1811 ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
1812 ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
1815 * Move the entries in the destination leaf up to make a hole?
1817 if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
1818 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
1819 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1820 entry_s = &leaf_d->entries[start_d];
1821 entry_d = &leaf_d->entries[start_d + count];
1822 memcpy(entry_d, entry_s, tmp);
1826 * Copy all entry's in the same (sorted) order,
1827 * but allocate filenames packed and in sequence.
1829 entry_s = &leaf_s->entries[start_s];
1830 entry_d = &leaf_d->entries[start_d];
1831 for (i = 0; i < count; entry_s++, entry_d++, i++) {
1832 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
1833 tmp = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry_s);
1834 INT_MOD(hdr_d->firstused, ARCH_CONVERT, -(tmp));
1835 entry_d->hashval = entry_s->hashval; /* INT_: direct copy */
1836 INT_COPY(entry_d->nameidx, hdr_d->firstused, ARCH_CONVERT);
1837 entry_d->namelen = entry_s->namelen;
1838 ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1839 memcpy(XFS_DIR_LEAF_NAMESTRUCT(leaf_d, INT_GET(entry_d->nameidx, ARCH_CONVERT)),
1840 XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)), tmp);
1841 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1842 memset((char *)XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)),
1844 INT_MOD(hdr_s->namebytes, ARCH_CONVERT, -(entry_d->namelen));
1845 INT_MOD(hdr_d->namebytes, ARCH_CONVERT, entry_d->namelen);
1846 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
1847 INT_MOD(hdr_d->count, ARCH_CONVERT, +1);
1848 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1849 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1850 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
1855 * Zero out the entries we just copied.
1857 if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
1858 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1859 entry_s = &leaf_s->entries[start_s];
1860 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1861 memset((char *)entry_s, 0, tmp);
1864 * Move the remaining entries down to fill the hole,
1865 * then zero the entries at the top.
1867 tmp = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
1868 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1869 entry_s = &leaf_s->entries[start_s + count];
1870 entry_d = &leaf_s->entries[start_s];
1871 memcpy(entry_d, entry_s, tmp);
1873 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1874 entry_s = &leaf_s->entries[INT_GET(hdr_s->count, ARCH_CONVERT)];
1875 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1876 memset((char *)entry_s, 0, tmp);
1880 * Fill in the freemap information
1882 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_hdr_t));
1883 INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT, INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t));
1884 INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT, INT_GET(hdr_d->firstused, ARCH_CONVERT) - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
1885 INT_SET(hdr_d->freemap[1].base, ARCH_CONVERT, (hdr_d->freemap[2].base = 0));
1886 INT_SET(hdr_d->freemap[1].size, ARCH_CONVERT, (hdr_d->freemap[2].size = 0));
1887 hdr_s->holes = 1; /* leaf may not be compact */
1891 * Compare two leaf blocks "order".
1894 xfs_dir_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
1896 xfs_dir_leafblock_t *leaf1, *leaf2;
1898 leaf1 = leaf1_bp->data;
1899 leaf2 = leaf2_bp->data;
1900 ASSERT((INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC) &&
1901 (INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC));
1902 if ((INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0) &&
1903 ((INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
1904 INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT)) ||
1905 (INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
1906 INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
1913 * Pick up the last hashvalue from a leaf block.
1916 xfs_dir_leaf_lasthash(xfs_dabuf_t *bp, int *count)
1918 xfs_dir_leafblock_t *leaf;
1921 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1923 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1924 if (!leaf->hdr.count)
1926 return(INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
1930 * Copy out directory entries for getdents(), for leaf directories.
1933 xfs_dir_leaf_getdents_int(
1943 xfs_dir_leafblock_t *leaf;
1944 xfs_dir_leaf_entry_t *entry;
1945 xfs_dir_leaf_name_t *namest;
1946 int entno, want_entno, i, nextentno;
1948 xfs_dahash_t cookhash;
1949 xfs_dahash_t nexthash = 0;
1950 #if (BITS_PER_LONG == 32)
1951 xfs_dahash_t lasthash = XFS_DA_MAXHASH;
1953 xfs_dir_put_args_t p;
1957 if (INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) != XFS_DIR_LEAF_MAGIC) {
1959 return(XFS_ERROR(ENOENT)); /* XXX wrong code */
1962 want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
1964 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
1966 xfs_dir_trace_g_dul("leaf: start", dp, uio, leaf);
1969 * Re-find our place.
1971 for (i = entno = 0, entry = &leaf->entries[0];
1972 i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
1975 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
1976 INT_GET(entry->nameidx, ARCH_CONVERT));
1979 ((char *)namest < (char *)leaf) ||
1980 ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
1981 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(1)",
1982 XFS_ERRLEVEL_LOW, mp, leaf);
1983 xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
1984 return XFS_ERROR(EFSCORRUPTED);
1986 if (INT_GET(entry->hashval, ARCH_CONVERT) >= cookhash) {
1987 if ( entno < want_entno
1988 && INT_GET(entry->hashval, ARCH_CONVERT)
1991 * Trying to get to a particular offset in a
1992 * run of equal-hashval entries.
1995 } else if ( want_entno > 0
1996 && entno == want_entno
1997 && INT_GET(entry->hashval, ARCH_CONVERT)
2007 if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
2008 xfs_dir_trace_g_du("leaf: hash not found", dp, uio);
2009 if (!INT_GET(leaf->hdr.info.forw, ARCH_CONVERT))
2011 XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
2013 * Don't set uio_offset if there's another block:
2014 * the node code will be setting uio_offset anyway.
2019 xfs_dir_trace_g_due("leaf: hash found", dp, uio, entry);
2026 * We're synchronized, start copying entries out to the user.
2028 for (; entno >= 0 && i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
2029 entry++, i++, (entno = nextentno)) {
2030 int lastresid=0, retval;
2031 xfs_dircook_t lastoffset;
2032 xfs_dahash_t thishash;
2035 * Check for a damaged directory leaf block and pick up
2036 * the inode number from this entry.
2038 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
2039 INT_GET(entry->nameidx, ARCH_CONVERT));
2042 ((char *)namest < (char *)leaf) ||
2043 ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
2044 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(2)",
2045 XFS_ERRLEVEL_LOW, mp, leaf);
2046 xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
2047 return XFS_ERROR(EFSCORRUPTED);
2050 xfs_dir_trace_g_duc("leaf: middle cookie ",
2053 if (i < (INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1)) {
2054 nexthash = INT_GET(entry[1].hashval, ARCH_CONVERT);
2056 if (nexthash == INT_GET(entry->hashval, ARCH_CONVERT))
2057 nextentno = entno + 1;
2060 XFS_PUT_COOKIE(p.cook, mp, bno, nextentno, nexthash);
2061 xfs_dir_trace_g_duc("leaf: middle cookie ",
2064 } else if ((thishash = INT_GET(leaf->hdr.info.forw,
2067 xfs_dir_leafblock_t *leaf2;
2069 ASSERT(nextda != -1);
2071 retval = xfs_da_read_buf(dp->i_transp, dp, thishash,
2072 nextda, &bp2, XFS_DATA_FORK);
2076 ASSERT(bp2 != NULL);
2081 (INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2082 != XFS_DIR_LEAF_MAGIC)
2083 || (INT_GET(leaf2->hdr.info.back, ARCH_CONVERT)
2084 != bno))) { /* GROT */
2085 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(3)",
2086 XFS_ERRLEVEL_LOW, mp,
2088 xfs_da_brelse(dp->i_transp, bp2);
2090 return(XFS_ERROR(EFSCORRUPTED));
2093 nexthash = INT_GET(leaf2->entries[0].hashval,
2096 XFS_PUT_COOKIE(p.cook, mp, thishash, 0, nexthash);
2097 xfs_da_brelse(dp->i_transp, bp2);
2098 xfs_dir_trace_g_duc("leaf: next blk cookie",
2102 XFS_PUT_COOKIE(p.cook, mp, 0, 0, XFS_DA_MAXHASH);
2106 * Save off the cookie so we can fall back should the
2107 * 'put' into the outgoing buffer fails. To handle a run
2108 * of equal-hashvals, the off_t structure on 64bit
2109 * builds has entno built into the cookie to ID the
2110 * entry. On 32bit builds, we only have space for the
2111 * hashval so we can't ID specific entries within a group
2112 * of same hashval entries. For this, lastoffset is set
2113 * to the first in the run of equal hashvals so we don't
2114 * include any entries unless we can include all entries
2115 * that share the same hashval. Hopefully the buffer
2116 * provided is big enough to handle it (see pv763517).
2118 #if (BITS_PER_LONG == 32)
2119 if ((thishash = INT_GET(entry->hashval, ARCH_CONVERT))
2121 XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2122 lastresid = uio->uio_resid;
2123 lasthash = thishash;
2125 xfs_dir_trace_g_duc("leaf: DUP COOKIES, skipped",
2129 thishash = INT_GET(entry->hashval, ARCH_CONVERT);
2130 XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2131 lastresid = uio->uio_resid;
2132 #endif /* BITS_PER_LONG == 32 */
2135 * Put the current entry into the outgoing buffer. If we fail
2136 * then restore the UIO to the first entry in the current
2137 * run of equal-hashval entries (probably one 1 entry long).
2139 p.ino = XFS_GET_DIR_INO8(namest->inumber);
2141 p.ino += mp->m_inoadd;
2143 p.name = (char *)namest->name;
2144 p.namelen = entry->namelen;
2149 uio->uio_offset = lastoffset.o;
2150 uio->uio_resid = lastresid;
2154 xfs_dir_trace_g_du("leaf: E-O-B", dp, uio);
2160 uio->uio_offset = p.cook.o;
2164 xfs_dir_trace_g_du("leaf: E-O-F", dp, uio);
2170 * Format a dirent64 structure and copy it out the the user's buffer.
2173 xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa)
2176 int reclen, namelen;
2180 namelen = pa->namelen;
2181 reclen = DIRENTSIZE(namelen);
2183 if (reclen > uio->uio_resid) {
2187 iovp = uio->uio_iov;
2188 idbp = (xfs_dirent_t *)iovp->iov_base;
2189 iovp->iov_base = (char *)idbp + reclen;
2190 iovp->iov_len -= reclen;
2191 uio->uio_resid -= reclen;
2192 idbp->d_reclen = reclen;
2193 idbp->d_ino = pa->ino;
2194 idbp->d_off = pa->cook.o;
2195 idbp->d_name[namelen] = '\0';
2197 memcpy(idbp->d_name, pa->name, namelen);
2202 * Format a dirent64 structure and copy it out the the user's buffer.
2205 xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa)
2207 int retval, reclen, namelen;
2211 namelen = pa->namelen;
2212 reclen = DIRENTSIZE(namelen);
2214 if (reclen > uio->uio_resid) {
2219 idbp->d_reclen = reclen;
2220 idbp->d_ino = pa->ino;
2221 idbp->d_off = pa->cook.o;
2222 idbp->d_name[namelen] = '\0';
2223 memcpy(idbp->d_name, pa->name, namelen);
2224 retval = uio_read((caddr_t)idbp, reclen, uio);
2225 pa->done = (retval == 0);