2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
23 #include "xfs_trans.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_inode_item.h"
39 #include "xfs_alloc.h"
40 #include "xfs_btree.h"
42 #include "xfs_dir_leaf.h"
43 #include "xfs_error.h"
48 * Routines to implement leaf blocks of directories as Btrees of hashed names.
51 /*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
56 * Routines used for growing the Btree.
58 STATIC void xfs_dir_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
61 STATIC int xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer,
62 int musthave, int justcheck);
63 STATIC void xfs_dir_leaf_rebalance(xfs_da_state_t *state,
64 xfs_da_state_blk_t *blk1,
65 xfs_da_state_blk_t *blk2);
66 STATIC int xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
67 xfs_da_state_blk_t *leaf_blk_1,
68 xfs_da_state_blk_t *leaf_blk_2,
69 int *number_entries_in_blk1,
70 int *number_namebytes_in_blk1);
72 STATIC int xfs_dir_leaf_create(struct xfs_da_args *args,
73 xfs_dablk_t which_block,
74 struct xfs_dabuf **bpp);
79 STATIC void xfs_dir_leaf_moveents(xfs_dir_leafblock_t *src_leaf,
81 xfs_dir_leafblock_t *dst_leaf,
82 int dst_start, int move_count,
86 /*========================================================================
87 * External routines when dirsize < XFS_IFORK_DSIZE(dp).
88 *========================================================================*/
92 * Validate a given inode number.
95 xfs_dir_ino_validate(xfs_mount_t *mp, xfs_ino_t ino)
97 xfs_agblock_t agblkno;
103 agno = XFS_INO_TO_AGNO(mp, ino);
104 agblkno = XFS_INO_TO_AGBNO(mp, ino);
105 ioff = XFS_INO_TO_OFFSET(mp, ino);
106 agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
108 agno < mp->m_sb.sb_agcount &&
109 agblkno < mp->m_sb.sb_agblocks &&
111 ioff < (1 << mp->m_sb.sb_inopblog) &&
112 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
113 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
114 XFS_RANDOM_DIR_INO_VALIDATE))) {
115 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
116 (unsigned long long) ino);
117 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
118 return XFS_ERROR(EFSCORRUPTED);
124 * Create the initial contents of a shortform directory.
127 xfs_dir_shortform_create(xfs_da_args_t *args, xfs_ino_t parent)
129 xfs_dir_sf_hdr_t *hdr;
134 ASSERT(dp->i_d.di_size == 0);
135 if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
136 dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
137 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
138 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
139 dp->i_df.if_flags |= XFS_IFINLINE;
141 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
142 ASSERT(dp->i_df.if_bytes == 0);
143 xfs_idata_realloc(dp, sizeof(*hdr), XFS_DATA_FORK);
144 hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
145 XFS_DIR_SF_PUT_DIRINO(&parent, &hdr->parent);
148 dp->i_d.di_size = sizeof(*hdr);
149 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
154 * Add a name to the shortform directory structure.
155 * Overflow from the inode has already been checked for.
158 xfs_dir_shortform_addname(xfs_da_args_t *args)
160 xfs_dir_shortform_t *sf;
161 xfs_dir_sf_entry_t *sfe;
166 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
168 * Catch the case where the conversion from shortform to leaf
169 * failed part way through.
171 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
172 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
173 return XFS_ERROR(EIO);
175 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
176 ASSERT(dp->i_df.if_u1.if_data != NULL);
177 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
179 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
180 if (sfe->namelen == args->namelen &&
181 args->name[0] == sfe->name[0] &&
182 memcmp(args->name, sfe->name, args->namelen) == 0)
183 return(XFS_ERROR(EEXIST));
184 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
187 offset = (int)((char *)sfe - (char *)sf);
188 size = XFS_DIR_SF_ENTSIZE_BYNAME(args->namelen);
189 xfs_idata_realloc(dp, size, XFS_DATA_FORK);
190 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
191 sfe = (xfs_dir_sf_entry_t *)((char *)sf + offset);
193 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
194 sfe->namelen = args->namelen;
195 memcpy(sfe->name, args->name, sfe->namelen);
196 INT_MOD(sf->hdr.count, ARCH_CONVERT, +1);
198 dp->i_d.di_size += size;
199 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
205 * Remove a name from the shortform directory structure.
208 xfs_dir_shortform_removename(xfs_da_args_t *args)
210 xfs_dir_shortform_t *sf;
211 xfs_dir_sf_entry_t *sfe;
212 int base, size = 0, i;
216 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
218 * Catch the case where the conversion from shortform to leaf
219 * failed part way through.
221 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
222 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
223 return XFS_ERROR(EIO);
225 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
226 ASSERT(dp->i_df.if_u1.if_data != NULL);
227 base = sizeof(xfs_dir_sf_hdr_t);
228 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
230 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
231 size = XFS_DIR_SF_ENTSIZE_BYENTRY(sfe);
232 if (sfe->namelen == args->namelen &&
233 sfe->name[0] == args->name[0] &&
234 memcmp(sfe->name, args->name, args->namelen) == 0)
237 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
240 ASSERT(args->oknoent);
241 return(XFS_ERROR(ENOENT));
244 if ((base + size) != dp->i_d.di_size) {
245 memmove(&((char *)sf)[base], &((char *)sf)[base+size],
246 dp->i_d.di_size - (base+size));
248 INT_MOD(sf->hdr.count, ARCH_CONVERT, -1);
250 xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
251 dp->i_d.di_size -= size;
252 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
258 * Look up a name in a shortform directory structure.
261 xfs_dir_shortform_lookup(xfs_da_args_t *args)
263 xfs_dir_shortform_t *sf;
264 xfs_dir_sf_entry_t *sfe;
269 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
271 * Catch the case where the conversion from shortform to leaf
272 * failed part way through.
274 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
275 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
276 return XFS_ERROR(EIO);
278 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
279 ASSERT(dp->i_df.if_u1.if_data != NULL);
280 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
281 if (args->namelen == 2 &&
282 args->name[0] == '.' && args->name[1] == '.') {
283 XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &args->inumber);
284 return(XFS_ERROR(EEXIST));
286 if (args->namelen == 1 && args->name[0] == '.') {
287 args->inumber = dp->i_ino;
288 return(XFS_ERROR(EEXIST));
291 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
292 if (sfe->namelen == args->namelen &&
293 sfe->name[0] == args->name[0] &&
294 memcmp(args->name, sfe->name, args->namelen) == 0) {
295 XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args->inumber);
296 return(XFS_ERROR(EEXIST));
298 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
300 ASSERT(args->oknoent);
301 return(XFS_ERROR(ENOENT));
305 * Convert from using the shortform to the leaf.
308 xfs_dir_shortform_to_leaf(xfs_da_args_t *iargs)
311 xfs_dir_shortform_t *sf;
312 xfs_dir_sf_entry_t *sfe;
322 * Catch the case where the conversion from shortform to leaf
323 * failed part way through.
325 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
326 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
327 return XFS_ERROR(EIO);
329 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
330 ASSERT(dp->i_df.if_u1.if_data != NULL);
331 size = dp->i_df.if_bytes;
332 tmpbuffer = kmem_alloc(size, KM_SLEEP);
333 ASSERT(tmpbuffer != NULL);
335 memcpy(tmpbuffer, dp->i_df.if_u1.if_data, size);
337 sf = (xfs_dir_shortform_t *)tmpbuffer;
338 XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &inumber);
340 xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
342 xfs_trans_log_inode(iargs->trans, dp, XFS_ILOG_CORE);
343 retval = xfs_da_grow_inode(iargs, &blkno);
348 retval = xfs_dir_leaf_create(iargs, blkno, &bp);
355 args.hashval = xfs_dir_hash_dot;
356 args.inumber = dp->i_ino;
358 args.firstblock = iargs->firstblock;
359 args.flist = iargs->flist;
360 args.total = iargs->total;
361 args.whichfork = XFS_DATA_FORK;
362 args.trans = iargs->trans;
364 args.addname = args.oknoent = 1;
365 retval = xfs_dir_leaf_addname(&args);
371 args.hashval = xfs_dir_hash_dotdot;
372 args.inumber = inumber;
373 retval = xfs_dir_leaf_addname(&args);
378 for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
379 args.name = (char *)(sfe->name);
380 args.namelen = sfe->namelen;
381 args.hashval = xfs_da_hashname((char *)(sfe->name),
383 XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args.inumber);
384 retval = xfs_dir_leaf_addname(&args);
387 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
392 kmem_free(tmpbuffer, size);
397 xfs_dir_shortform_compare(const void *a, const void *b)
399 xfs_dir_sf_sort_t *sa, *sb;
401 sa = (xfs_dir_sf_sort_t *)a;
402 sb = (xfs_dir_sf_sort_t *)b;
403 if (sa->hash < sb->hash)
405 else if (sa->hash > sb->hash)
408 return sa->entno - sb->entno;
412 * Copy out directory entries for getdents(), for shortform directories.
416 xfs_dir_shortform_getdents(xfs_inode_t *dp, uio_t *uio, int *eofp,
417 xfs_dirent_t *dbp, xfs_dir_put_t put)
419 xfs_dir_shortform_t *sf;
420 xfs_dir_sf_entry_t *sfe;
421 int retval, i, sbsize, nsbuf, lastresid=0, want_entno;
423 xfs_dahash_t cookhash, hash;
424 xfs_dir_put_args_t p;
425 xfs_dir_sf_sort_t *sbuf, *sbp;
428 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
429 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
430 want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
431 nsbuf = INT_GET(sf->hdr.count, ARCH_CONVERT) + 2;
432 sbsize = (nsbuf + 1) * sizeof(*sbuf);
433 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
435 xfs_dir_trace_g_du("sf: start", dp, uio);
438 * Collect all the entries into the buffer.
443 sbp->hash = xfs_dir_hash_dot;
444 sbp->ino = dp->i_ino;
454 sbp->hash = xfs_dir_hash_dotdot;
455 sbp->ino = XFS_GET_DIR_INO8(sf->hdr.parent);
461 * Scan the directory data for the rest of the entries.
463 for (i = 0, sfe = &sf->list[0];
464 i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
467 ((char *)sfe < (char *)sf) ||
468 ((char *)sfe >= ((char *)sf + dp->i_df.if_bytes)))) {
469 xfs_dir_trace_g_du("sf: corrupted", dp, uio);
470 XFS_CORRUPTION_ERROR("xfs_dir_shortform_getdents",
471 XFS_ERRLEVEL_LOW, mp, sfe);
472 kmem_free(sbuf, sbsize);
473 return XFS_ERROR(EFSCORRUPTED);
478 sbp->hash = xfs_da_hashname((char *)sfe->name, sfe->namelen);
479 sbp->ino = XFS_GET_DIR_INO8(sfe->inumber);
480 sbp->name = (char *)sfe->name;
481 sbp->namelen = sfe->namelen;
482 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
487 * Sort the entries on hash then entno.
489 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_dir_shortform_compare);
491 * Stuff in last entry.
494 sbp->hash = XFS_DA_MAXHASH;
497 * Figure out the sequence numbers in case there's a hash duplicate.
499 for (hash = sbuf->hash, sbp = sbuf + 1;
500 sbp < &sbuf[nsbuf + 1]; sbp++) {
501 if (sbp->hash == hash)
502 sbp->seqno = sbp[-1].seqno + 1;
508 * Set up put routine.
517 for (sbp = sbuf; sbp < &sbuf[nsbuf + 1]; sbp++) {
518 if (sbp->hash > cookhash ||
519 (sbp->hash == cookhash && sbp->seqno >= want_entno))
524 * Did we fail to find anything? We stop at the last entry,
525 * the one we put maxhash into.
527 if (sbp == &sbuf[nsbuf]) {
528 kmem_free(sbuf, sbsize);
529 xfs_dir_trace_g_du("sf: hash beyond end", dp, uio);
530 uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
536 * Loop putting entries into the user buffer.
538 while (sbp < &sbuf[nsbuf]) {
540 * Save the first resid in a run of equal-hashval entries
541 * so that we can back them out if they don't all fit.
543 if (sbp->seqno == 0 || sbp == sbuf)
544 lastresid = uio->uio_resid;
545 XFS_PUT_COOKIE(p.cook, mp, 0, sbp[1].seqno, sbp[1].hash);
548 p.ino += mp->m_inoadd;
551 p.namelen = sbp->namelen;
555 XFS_DA_MAKE_COOKIE(mp, 0, 0, sbp->hash);
556 kmem_free(sbuf, sbsize);
557 uio->uio_resid = lastresid;
558 xfs_dir_trace_g_du("sf: E-O-B", dp, uio);
563 kmem_free(sbuf, sbsize);
564 uio->uio_offset = p.cook.o;
566 xfs_dir_trace_g_du("sf: E-O-F", dp, uio);
571 * Look up a name in a shortform directory structure, replace the inode number.
574 xfs_dir_shortform_replace(xfs_da_args_t *args)
576 xfs_dir_shortform_t *sf;
577 xfs_dir_sf_entry_t *sfe;
582 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
584 * Catch the case where the conversion from shortform to leaf
585 * failed part way through.
587 if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
588 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
589 return XFS_ERROR(EIO);
591 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
592 ASSERT(dp->i_df.if_u1.if_data != NULL);
593 sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
594 if (args->namelen == 2 &&
595 args->name[0] == '.' && args->name[1] == '.') {
596 /* XXX - replace assert? */
597 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sf->hdr.parent);
598 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
601 ASSERT(args->namelen != 1 || args->name[0] != '.');
603 for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
604 if (sfe->namelen == args->namelen &&
605 sfe->name[0] == args->name[0] &&
606 memcmp(args->name, sfe->name, args->namelen) == 0) {
607 ASSERT(memcmp((char *)&args->inumber,
608 (char *)&sfe->inumber, sizeof(xfs_ino_t)));
609 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
610 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
613 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
615 ASSERT(args->oknoent);
616 return(XFS_ERROR(ENOENT));
620 * Convert a leaf directory to shortform structure
623 xfs_dir_leaf_to_shortform(xfs_da_args_t *iargs)
625 xfs_dir_leafblock_t *leaf;
626 xfs_dir_leaf_hdr_t *hdr;
627 xfs_dir_leaf_entry_t *entry;
628 xfs_dir_leaf_name_t *namest;
637 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
638 ASSERT(tmpbuffer != NULL);
640 retval = xfs_da_read_buf(iargs->trans, iargs->dp, 0, -1, &bp,
645 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
646 leaf = (xfs_dir_leafblock_t *)tmpbuffer;
647 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
648 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
651 * Find and special case the parent inode number
654 entry = &leaf->entries[0];
655 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
656 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
657 if ((entry->namelen == 2) &&
658 (namest->name[0] == '.') &&
659 (namest->name[1] == '.')) {
660 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &parent);
662 } else if ((entry->namelen == 1) && (namest->name[0] == '.')) {
666 retval = xfs_da_shrink_inode(iargs, 0, bp);
669 retval = xfs_dir_shortform_create(iargs, parent);
674 * Copy the rest of the filenames
676 entry = &leaf->entries[0];
678 args.firstblock = iargs->firstblock;
679 args.flist = iargs->flist;
680 args.total = iargs->total;
681 args.whichfork = XFS_DATA_FORK;
682 args.trans = iargs->trans;
684 args.addname = args.oknoent = 1;
685 for (i = 0; i < INT_GET(hdr->count, ARCH_CONVERT); entry++, i++) {
688 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
689 args.name = (char *)(namest->name);
690 args.namelen = entry->namelen;
691 args.hashval = INT_GET(entry->hashval, ARCH_CONVERT);
692 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args.inumber);
693 xfs_dir_shortform_addname(&args);
697 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
702 * Convert from using a single leaf to a root node and a leaf.
705 xfs_dir_leaf_to_node(xfs_da_args_t *args)
707 xfs_dir_leafblock_t *leaf;
708 xfs_da_intnode_t *node;
710 xfs_dabuf_t *bp1, *bp2;
715 retval = xfs_da_grow_inode(args, &blkno);
719 retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
724 retval = xfs_da_get_buf(args->trans, args->dp, 1, -1, &bp2,
727 xfs_da_buf_done(bp1);
731 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
732 xfs_da_buf_done(bp1);
733 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
736 * Set up the new root node.
738 retval = xfs_da_node_create(args, 0, 1, &bp1, XFS_DATA_FORK);
740 xfs_da_buf_done(bp2);
745 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
746 INT_SET(node->btree[0].hashval, ARCH_CONVERT, INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
747 xfs_da_buf_done(bp2);
748 INT_SET(node->btree[0].before, ARCH_CONVERT, blkno);
749 INT_SET(node->hdr.count, ARCH_CONVERT, 1);
750 xfs_da_log_buf(args->trans, bp1,
751 XFS_DA_LOGRANGE(node, &node->btree[0], sizeof(node->btree[0])));
752 xfs_da_buf_done(bp1);
758 /*========================================================================
759 * Routines used for growing the Btree.
760 *========================================================================*/
763 * Create the initial contents of a leaf directory
764 * or a leaf in a node directory.
767 xfs_dir_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
769 xfs_dir_leafblock_t *leaf;
770 xfs_dir_leaf_hdr_t *hdr;
777 retval = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp, XFS_DATA_FORK);
782 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
784 INT_SET(hdr->info.magic, ARCH_CONVERT, XFS_DIR_LEAF_MAGIC);
785 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
787 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount) - 1);
788 INT_SET(hdr->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
789 INT_SET(hdr->freemap[0].size, ARCH_CONVERT, INT_GET(hdr->firstused, ARCH_CONVERT) - INT_GET(hdr->freemap[0].base, ARCH_CONVERT));
791 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
798 * Split the leaf node, rebalance, then add the new entry.
801 xfs_dir_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
802 xfs_da_state_blk_t *newblk)
809 * Allocate space for a new leaf node.
812 ASSERT(args != NULL);
813 ASSERT(oldblk->magic == XFS_DIR_LEAF_MAGIC);
814 error = xfs_da_grow_inode(args, &blkno);
817 error = xfs_dir_leaf_create(args, blkno, &newblk->bp);
820 newblk->blkno = blkno;
821 newblk->magic = XFS_DIR_LEAF_MAGIC;
824 * Rebalance the entries across the two leaves.
826 xfs_dir_leaf_rebalance(state, oldblk, newblk);
827 error = xfs_da_blk_link(state, oldblk, newblk);
832 * Insert the new entry in the correct block.
835 error = xfs_dir_leaf_add(oldblk->bp, args, oldblk->index);
837 error = xfs_dir_leaf_add(newblk->bp, args, newblk->index);
841 * Update last hashval in each block since we added the name.
843 oldblk->hashval = xfs_dir_leaf_lasthash(oldblk->bp, NULL);
844 newblk->hashval = xfs_dir_leaf_lasthash(newblk->bp, NULL);
849 * Add a name to the leaf directory structure.
851 * Must take into account fragmented leaves and leaves where spacemap has
852 * lost some freespace information (ie: holes).
855 xfs_dir_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index)
857 xfs_dir_leafblock_t *leaf;
858 xfs_dir_leaf_hdr_t *hdr;
859 xfs_dir_leaf_map_t *map;
860 int tablesize, entsize, sum, i, tmp, error;
863 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
864 ASSERT((index >= 0) && (index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
866 entsize = XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen);
869 * Search through freemap for first-fit on new name length.
870 * (may need to figure in size of entry struct too)
872 tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1) * (uint)sizeof(xfs_dir_leaf_entry_t)
873 + (uint)sizeof(xfs_dir_leaf_hdr_t);
874 map = &hdr->freemap[XFS_DIR_LEAF_MAPSIZE-1];
875 for (sum = 0, i = XFS_DIR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
876 if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
877 sum += INT_GET(map->size, ARCH_CONVERT);
881 continue; /* no space in this map */
883 if (INT_GET(map->base, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
884 tmp += (uint)sizeof(xfs_dir_leaf_entry_t);
885 if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
886 if (!args->justcheck)
887 xfs_dir_leaf_add_work(bp, args, index, i);
890 sum += INT_GET(map->size, ARCH_CONVERT);
894 * If there are no holes in the address space of the block,
895 * and we don't have enough freespace, then compaction will do us
896 * no good and we should just give up.
898 if (!hdr->holes && (sum < entsize))
899 return(XFS_ERROR(ENOSPC));
902 * Compact the entries to coalesce free space.
903 * Pass the justcheck flag so the checking pass can return
904 * an error, without changing anything, if it won't fit.
906 error = xfs_dir_leaf_compact(args->trans, bp,
909 (uint)sizeof(xfs_dir_leaf_entry_t) : 0,
914 * After compaction, the block is guaranteed to have only one
915 * free region, in freemap[0]. If it is not big enough, give up.
917 if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT) <
918 (entsize + (uint)sizeof(xfs_dir_leaf_entry_t)))
919 return(XFS_ERROR(ENOSPC));
921 if (!args->justcheck)
922 xfs_dir_leaf_add_work(bp, args, index, 0);
927 * Add a name to a leaf directory structure.
930 xfs_dir_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int index,
933 xfs_dir_leafblock_t *leaf;
934 xfs_dir_leaf_hdr_t *hdr;
935 xfs_dir_leaf_entry_t *entry;
936 xfs_dir_leaf_name_t *namest;
937 xfs_dir_leaf_map_t *map;
943 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
945 ASSERT((mapindex >= 0) && (mapindex < XFS_DIR_LEAF_MAPSIZE));
946 ASSERT((index >= 0) && (index <= INT_GET(hdr->count, ARCH_CONVERT)));
949 * Force open some space in the entry array and fill it in.
951 entry = &leaf->entries[index];
952 if (index < INT_GET(hdr->count, ARCH_CONVERT)) {
953 tmp = INT_GET(hdr->count, ARCH_CONVERT) - index;
954 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
955 memmove(entry + 1, entry, tmp);
956 xfs_da_log_buf(args->trans, bp,
957 XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
959 INT_MOD(hdr->count, ARCH_CONVERT, +1);
962 * Allocate space for the new string (at the end of the run).
964 map = &hdr->freemap[mapindex];
965 mp = args->trans->t_mountp;
966 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
967 ASSERT(INT_GET(map->size, ARCH_CONVERT) >= XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen));
968 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
969 INT_MOD(map->size, ARCH_CONVERT, -(XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen)));
970 INT_SET(entry->nameidx, ARCH_CONVERT, INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT));
971 INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
972 entry->namelen = args->namelen;
973 xfs_da_log_buf(args->trans, bp,
974 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
977 * Copy the string and inode number into the new space.
979 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
980 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &namest->inumber);
981 memcpy(namest->name, args->name, args->namelen);
982 xfs_da_log_buf(args->trans, bp,
983 XFS_DA_LOGRANGE(leaf, namest, XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry)));
986 * Update the control info for this leaf node
988 if (INT_GET(entry->nameidx, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
989 INT_COPY(hdr->firstused, entry->nameidx, ARCH_CONVERT);
990 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
991 tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1) * (uint)sizeof(xfs_dir_leaf_entry_t)
992 + (uint)sizeof(xfs_dir_leaf_hdr_t);
993 map = &hdr->freemap[0];
994 for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
995 if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
996 INT_MOD(map->base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
997 INT_MOD(map->size, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1000 INT_MOD(hdr->namebytes, ARCH_CONVERT, args->namelen);
1001 xfs_da_log_buf(args->trans, bp,
1002 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1006 * Garbage collect a leaf directory block by copying it to a new buffer.
1009 xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp, int musthave,
1012 xfs_dir_leafblock_t *leaf_s, *leaf_d;
1013 xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1016 char *tmpbuffer2=NULL;
1020 mp = trans->t_mountp;
1021 lbsize = XFS_LBSIZE(mp);
1022 tmpbuffer = kmem_alloc(lbsize, KM_SLEEP);
1023 ASSERT(tmpbuffer != NULL);
1024 memcpy(tmpbuffer, bp->data, lbsize);
1027 * Make a second copy in case xfs_dir_leaf_moveents()
1028 * below destroys the original.
1030 if (musthave || justcheck) {
1031 tmpbuffer2 = kmem_alloc(lbsize, KM_SLEEP);
1032 memcpy(tmpbuffer2, bp->data, lbsize);
1034 memset(bp->data, 0, lbsize);
1037 * Copy basic information
1039 leaf_s = (xfs_dir_leafblock_t *)tmpbuffer;
1041 hdr_s = &leaf_s->hdr;
1042 hdr_d = &leaf_d->hdr;
1043 hdr_d->info = hdr_s->info; /* struct copy */
1044 INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize);
1045 if (!hdr_d->firstused)
1046 INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize - 1);
1047 hdr_d->namebytes = 0;
1050 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
1051 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));
1054 * Copy all entry's in the same (sorted) order,
1055 * but allocate filenames packed and in sequence.
1056 * This changes the source (leaf_s) as well.
1058 xfs_dir_leaf_moveents(leaf_s, 0, leaf_d, 0, (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
1060 if (musthave && INT_GET(hdr_d->freemap[0].size, ARCH_CONVERT) < musthave)
1061 rval = XFS_ERROR(ENOSPC);
1065 if (justcheck || rval == ENOSPC) {
1067 memcpy(bp->data, tmpbuffer2, lbsize);
1069 xfs_da_log_buf(trans, bp, 0, lbsize - 1);
1072 kmem_free(tmpbuffer, lbsize);
1073 if (musthave || justcheck)
1074 kmem_free(tmpbuffer2, lbsize);
1079 * Redistribute the directory entries between two leaf nodes,
1080 * taking into account the size of the new entry.
1082 * NOTE: if new block is empty, then it will get the upper half of old block.
1085 xfs_dir_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1086 xfs_da_state_blk_t *blk2)
1088 xfs_da_state_blk_t *tmp_blk;
1089 xfs_dir_leafblock_t *leaf1, *leaf2;
1090 xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1091 int count, totallen, max, space, swap;
1094 * Set up environment.
1096 ASSERT(blk1->magic == XFS_DIR_LEAF_MAGIC);
1097 ASSERT(blk2->magic == XFS_DIR_LEAF_MAGIC);
1098 leaf1 = blk1->bp->data;
1099 leaf2 = blk2->bp->data;
1100 ASSERT(INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1101 ASSERT(INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1104 * Check ordering of blocks, reverse if it makes things simpler.
1107 if (xfs_dir_leaf_order(blk1->bp, blk2->bp)) {
1111 leaf1 = blk1->bp->data;
1112 leaf2 = blk2->bp->data;
1119 * Examine entries until we reduce the absolute difference in
1120 * byte usage between the two blocks to a minimum. Then get
1121 * the direction to copy and the number of elements to move.
1123 state->inleaf = xfs_dir_leaf_figure_balance(state, blk1, blk2,
1126 state->inleaf = !state->inleaf;
1129 * Move any entries required from leaf to leaf:
1131 if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
1133 * Figure the total bytes to be added to the destination leaf.
1135 count = INT_GET(hdr1->count, ARCH_CONVERT) - count; /* number entries being moved */
1136 space = INT_GET(hdr1->namebytes, ARCH_CONVERT) - totallen;
1137 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1138 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1141 * leaf2 is the destination, compact it if it looks tight.
1143 max = INT_GET(hdr2->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1144 max -= INT_GET(hdr2->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1146 xfs_dir_leaf_compact(state->args->trans, blk2->bp,
1151 * Move high entries from leaf1 to low end of leaf2.
1153 xfs_dir_leaf_moveents(leaf1, INT_GET(hdr1->count, ARCH_CONVERT) - count,
1154 leaf2, 0, count, state->mp);
1156 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1157 state->blocksize-1);
1158 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1159 state->blocksize-1);
1161 } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
1163 * Figure the total bytes to be added to the destination leaf.
1165 count -= INT_GET(hdr1->count, ARCH_CONVERT); /* number entries being moved */
1166 space = totallen - INT_GET(hdr1->namebytes, ARCH_CONVERT);
1167 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1168 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1171 * leaf1 is the destination, compact it if it looks tight.
1173 max = INT_GET(hdr1->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1174 max -= INT_GET(hdr1->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1176 xfs_dir_leaf_compact(state->args->trans, blk1->bp,
1181 * Move low entries from leaf2 to high end of leaf1.
1183 xfs_dir_leaf_moveents(leaf2, 0, leaf1, (int)INT_GET(hdr1->count, ARCH_CONVERT),
1186 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1187 state->blocksize-1);
1188 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1189 state->blocksize-1);
1193 * Copy out last hashval in each block for B-tree code.
1195 blk1->hashval = INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1196 blk2->hashval = INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1199 * Adjust the expected index for insertion.
1200 * GROT: this doesn't work unless blk2 was originally empty.
1202 if (!state->inleaf) {
1203 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1208 * Examine entries until we reduce the absolute difference in
1209 * byte usage between the two blocks to a minimum.
1210 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1211 * GROT: there will always be enough room in either block for a new entry.
1212 * GROT: Do a double-split for this case?
1215 xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
1216 xfs_da_state_blk_t *blk1,
1217 xfs_da_state_blk_t *blk2,
1218 int *countarg, int *namebytesarg)
1220 xfs_dir_leafblock_t *leaf1, *leaf2;
1221 xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1222 xfs_dir_leaf_entry_t *entry;
1223 int count, max, totallen, half;
1224 int lastdelta, foundit, tmp;
1227 * Set up environment.
1229 leaf1 = blk1->bp->data;
1230 leaf2 = blk2->bp->data;
1237 * Examine entries until we reduce the absolute difference in
1238 * byte usage between the two blocks to a minimum.
1240 max = INT_GET(hdr1->count, ARCH_CONVERT) + INT_GET(hdr2->count, ARCH_CONVERT);
1241 half = (max+1) * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1242 half += INT_GET(hdr1->namebytes, ARCH_CONVERT) + INT_GET(hdr2->namebytes, ARCH_CONVERT) + state->args->namelen;
1244 lastdelta = state->blocksize;
1245 entry = &leaf1->entries[0];
1246 for (count = 0; count < max; entry++, count++) {
1248 #define XFS_DIR_ABS(A) (((A) < 0) ? -(A) : (A))
1250 * The new entry is in the first block, account for it.
1252 if (count == blk1->index) {
1253 tmp = totallen + (uint)sizeof(*entry)
1254 + XFS_DIR_LEAF_ENTSIZE_BYNAME(state->args->namelen);
1255 if (XFS_DIR_ABS(half - tmp) > lastdelta)
1257 lastdelta = XFS_DIR_ABS(half - tmp);
1263 * Wrap around into the second block if necessary.
1265 if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
1267 entry = &leaf1->entries[0];
1271 * Figure out if next leaf entry would be too much.
1273 tmp = totallen + (uint)sizeof(*entry)
1274 + XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1275 if (XFS_DIR_ABS(half - tmp) > lastdelta)
1277 lastdelta = XFS_DIR_ABS(half - tmp);
1283 * Calculate the number of namebytes that will end up in lower block.
1284 * If new entry not in lower block, fix up the count.
1287 count * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1289 totallen -= (sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1) +
1290 state->args->namelen;
1294 *namebytesarg = totallen;
1298 /*========================================================================
1299 * Routines used for shrinking the Btree.
1300 *========================================================================*/
1303 * Check a leaf block and its neighbors to see if the block should be
1304 * collapsed into one or the other neighbor. Always keep the block
1305 * with the smaller block number.
1306 * If the current block is over 50% full, don't try to join it, return 0.
1307 * If the block is empty, fill in the state structure and return 2.
1308 * If it can be collapsed, fill in the state structure and return 1.
1309 * If nothing can be done, return 0.
1312 xfs_dir_leaf_toosmall(xfs_da_state_t *state, int *action)
1314 xfs_dir_leafblock_t *leaf;
1315 xfs_da_state_blk_t *blk;
1316 xfs_da_blkinfo_t *info;
1317 int count, bytes, forward, error, retval, i;
1322 * Check for the degenerate case of the block being over 50% full.
1323 * If so, it's not worth even looking to see if we might be able
1324 * to coalesce with a sibling.
1326 blk = &state->path.blk[ state->path.active-1 ];
1327 info = blk->bp->data;
1328 ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1329 leaf = (xfs_dir_leafblock_t *)info;
1330 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1331 bytes = (uint)sizeof(xfs_dir_leaf_hdr_t) +
1332 count * (uint)sizeof(xfs_dir_leaf_entry_t) +
1333 count * ((uint)sizeof(xfs_dir_leaf_name_t)-1) +
1334 INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1335 if (bytes > (state->blocksize >> 1)) {
1336 *action = 0; /* blk over 50%, don't try to join */
1341 * Check for the degenerate case of the block being empty.
1342 * If the block is empty, we'll simply delete it, no need to
1343 * coalesce it with a sibling block. We choose (aribtrarily)
1344 * to merge with the forward block unless it is NULL.
1348 * Make altpath point to the block we want to keep and
1349 * path point to the block we want to drop (this one).
1351 forward = info->forw;
1352 memcpy(&state->altpath, &state->path, sizeof(state->path));
1353 error = xfs_da_path_shift(state, &state->altpath, forward,
1366 * Examine each sibling block to see if we can coalesce with
1367 * at least 25% free space to spare. We need to figure out
1368 * whether to merge with the forward or the backward block.
1369 * We prefer coalescing with the lower numbered sibling so as
1370 * to shrink a directory over time.
1372 forward = (INT_GET(info->forw, ARCH_CONVERT) < INT_GET(info->back, ARCH_CONVERT)); /* start with smaller blk num */
1373 for (i = 0; i < 2; forward = !forward, i++) {
1375 blkno = INT_GET(info->forw, ARCH_CONVERT);
1377 blkno = INT_GET(info->back, ARCH_CONVERT);
1380 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1387 leaf = (xfs_dir_leafblock_t *)info;
1388 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1389 bytes = state->blocksize - (state->blocksize>>2);
1390 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1392 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1393 count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
1394 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1395 bytes -= count * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1396 bytes -= count * (uint)sizeof(xfs_dir_leaf_entry_t);
1397 bytes -= (uint)sizeof(xfs_dir_leaf_hdr_t);
1399 break; /* fits with at least 25% to spare */
1401 xfs_da_brelse(state->args->trans, bp);
1407 xfs_da_buf_done(bp);
1410 * Make altpath point to the block we want to keep (the lower
1411 * numbered block) and path point to the block we want to drop.
1413 memcpy(&state->altpath, &state->path, sizeof(state->path));
1414 if (blkno < blk->blkno) {
1415 error = xfs_da_path_shift(state, &state->altpath, forward,
1418 error = xfs_da_path_shift(state, &state->path, forward,
1432 * Remove a name from the leaf directory structure.
1434 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1435 * If two leaves are 37% full, when combined they will leave 25% free.
1438 xfs_dir_leaf_remove(xfs_trans_t *trans, xfs_dabuf_t *bp, int index)
1440 xfs_dir_leafblock_t *leaf;
1441 xfs_dir_leaf_hdr_t *hdr;
1442 xfs_dir_leaf_map_t *map;
1443 xfs_dir_leaf_entry_t *entry;
1444 xfs_dir_leaf_name_t *namest;
1445 int before, after, smallest, entsize;
1446 int tablesize, tmp, i;
1450 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1452 mp = trans->t_mountp;
1453 ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0) && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1454 ASSERT((index >= 0) && (index < INT_GET(hdr->count, ARCH_CONVERT)));
1455 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
1456 entry = &leaf->entries[index];
1457 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1458 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1461 * Scan through free region table:
1462 * check for adjacency of free'd entry with an existing one,
1463 * find smallest free region in case we need to replace it,
1464 * adjust any map that borders the entry table,
1466 tablesize = INT_GET(hdr->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1467 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1468 map = &hdr->freemap[0];
1469 tmp = INT_GET(map->size, ARCH_CONVERT);
1470 before = after = -1;
1471 smallest = XFS_DIR_LEAF_MAPSIZE - 1;
1472 entsize = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1473 for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
1474 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1475 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1476 if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
1477 INT_MOD(map->base, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1478 INT_MOD(map->size, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
1481 if ((INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT)) == INT_GET(entry->nameidx, ARCH_CONVERT)) {
1483 } else if (INT_GET(map->base, ARCH_CONVERT) == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
1485 } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
1486 tmp = INT_GET(map->size, ARCH_CONVERT);
1492 * Coalesce adjacent freemap regions,
1493 * or replace the smallest region.
1495 if ((before >= 0) || (after >= 0)) {
1496 if ((before >= 0) && (after >= 0)) {
1497 map = &hdr->freemap[before];
1498 INT_MOD(map->size, ARCH_CONVERT, entsize);
1499 INT_MOD(map->size, ARCH_CONVERT, INT_GET(hdr->freemap[after].size, ARCH_CONVERT));
1500 hdr->freemap[after].base = 0;
1501 hdr->freemap[after].size = 0;
1502 } else if (before >= 0) {
1503 map = &hdr->freemap[before];
1504 INT_MOD(map->size, ARCH_CONVERT, entsize);
1506 map = &hdr->freemap[after];
1507 INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1508 INT_MOD(map->size, ARCH_CONVERT, entsize);
1512 * Replace smallest region (if it is smaller than free'd entry)
1514 map = &hdr->freemap[smallest];
1515 if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
1516 INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1517 INT_SET(map->size, ARCH_CONVERT, entsize);
1522 * Did we remove the first entry?
1524 if (INT_GET(entry->nameidx, ARCH_CONVERT) == INT_GET(hdr->firstused, ARCH_CONVERT))
1530 * Compress the remaining entries and zero out the removed stuff.
1532 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1533 memset((char *)namest, 0, entsize);
1534 xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, namest, entsize));
1536 INT_MOD(hdr->namebytes, ARCH_CONVERT, -(entry->namelen));
1537 tmp = (INT_GET(hdr->count, ARCH_CONVERT) - index) * (uint)sizeof(xfs_dir_leaf_entry_t);
1538 memmove(entry, entry + 1, tmp);
1539 INT_MOD(hdr->count, ARCH_CONVERT, -1);
1540 xfs_da_log_buf(trans, bp,
1541 XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
1542 entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
1543 memset((char *)entry, 0, sizeof(xfs_dir_leaf_entry_t));
1546 * If we removed the first entry, re-find the first used byte
1547 * in the name area. Note that if the entry was the "firstused",
1548 * then we don't have a "hole" in our block resulting from
1549 * removing the name.
1552 tmp = XFS_LBSIZE(mp);
1553 entry = &leaf->entries[0];
1554 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
1555 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1556 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1557 if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
1558 tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
1560 INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
1561 if (!hdr->firstused)
1562 INT_SET(hdr->firstused, ARCH_CONVERT, tmp - 1);
1564 hdr->holes = 1; /* mark as needing compaction */
1567 xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1570 * Check if leaf is less than 50% full, caller may want to
1571 * "join" the leaf with a sibling if so.
1573 tmp = (uint)sizeof(xfs_dir_leaf_hdr_t);
1574 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1575 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1576 tmp += INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1577 if (tmp < mp->m_dir_magicpct)
1578 return(1); /* leaf is < 37% full */
1583 * Move all the directory entries from drop_leaf into save_leaf.
1586 xfs_dir_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1587 xfs_da_state_blk_t *save_blk)
1589 xfs_dir_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1590 xfs_dir_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1595 * Set up environment.
1598 ASSERT(drop_blk->magic == XFS_DIR_LEAF_MAGIC);
1599 ASSERT(save_blk->magic == XFS_DIR_LEAF_MAGIC);
1600 drop_leaf = drop_blk->bp->data;
1601 save_leaf = save_blk->bp->data;
1602 ASSERT(INT_GET(drop_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1603 ASSERT(INT_GET(save_leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1604 drop_hdr = &drop_leaf->hdr;
1605 save_hdr = &save_leaf->hdr;
1608 * Save last hashval from dying block for later Btree fixup.
1610 drop_blk->hashval = INT_GET(drop_leaf->entries[ drop_leaf->hdr.count-1 ].hashval, ARCH_CONVERT);
1613 * Check if we need a temp buffer, or can we do it in place.
1614 * Note that we don't check "leaf" for holes because we will
1615 * always be dropping it, toosmall() decided that for us already.
1617 if (save_hdr->holes == 0) {
1619 * dest leaf has no holes, so we add there. May need
1620 * to make some room in the entry array.
1622 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1623 xfs_dir_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1624 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1626 xfs_dir_leaf_moveents(drop_leaf, 0,
1627 save_leaf, INT_GET(save_hdr->count, ARCH_CONVERT),
1628 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1632 * Destination has holes, so we make a temporary copy
1633 * of the leaf and add them both to that.
1635 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1636 ASSERT(tmpbuffer != NULL);
1637 memset(tmpbuffer, 0, state->blocksize);
1638 tmp_leaf = (xfs_dir_leafblock_t *)tmpbuffer;
1639 tmp_hdr = &tmp_leaf->hdr;
1640 tmp_hdr->info = save_hdr->info; /* struct copy */
1642 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
1643 if (!tmp_hdr->firstused)
1644 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize - 1);
1645 tmp_hdr->namebytes = 0;
1646 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1647 xfs_dir_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1648 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1649 xfs_dir_leaf_moveents(save_leaf, 0,
1650 tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1651 (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1653 xfs_dir_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1654 (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1655 xfs_dir_leaf_moveents(drop_leaf, 0,
1656 tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1657 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1659 memcpy(save_leaf, tmp_leaf, state->blocksize);
1660 kmem_free(tmpbuffer, state->blocksize);
1663 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1664 state->blocksize - 1);
1667 * Copy out last hashval in each block for B-tree code.
1669 save_blk->hashval = INT_GET(save_leaf->entries[ INT_GET(save_leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1672 /*========================================================================
1673 * Routines used for finding things in the Btree.
1674 *========================================================================*/
1677 * Look up a name in a leaf directory structure.
1678 * This is the internal routine, it uses the caller's buffer.
1680 * Note that duplicate keys are allowed, but only check within the
1681 * current leaf node. The Btree code must check in adjacent leaf nodes.
1683 * Return in *index the index into the entry[] array of either the found
1684 * entry, or where the entry should have been (insert before that entry).
1686 * Don't change the args->inumber unless we find the filename.
1689 xfs_dir_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args, int *index)
1691 xfs_dir_leafblock_t *leaf;
1692 xfs_dir_leaf_entry_t *entry;
1693 xfs_dir_leaf_name_t *namest;
1695 xfs_dahash_t hashval;
1698 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1699 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) < (XFS_LBSIZE(args->dp->i_mount)/8));
1702 * Binary search. (note: small blocks will skip this loop)
1704 hashval = args->hashval;
1705 probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
1706 for (entry = &leaf->entries[probe]; span > 4;
1707 entry = &leaf->entries[probe]) {
1709 if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
1711 else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
1716 ASSERT((probe >= 0) && \
1717 ((!leaf->hdr.count) || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
1718 ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT) == hashval));
1721 * Since we may have duplicate hashval's, find the first matching
1722 * hashval in the leaf.
1724 while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT) >= hashval)) {
1728 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
1732 if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT)) || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
1734 ASSERT(args->oknoent);
1735 return(XFS_ERROR(ENOENT));
1739 * Duplicate keys may be present, so search all of them for a match.
1741 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval)) {
1742 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1743 if (entry->namelen == args->namelen &&
1744 namest->name[0] == args->name[0] &&
1745 memcmp(args->name, namest->name, args->namelen) == 0) {
1746 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args->inumber);
1748 return(XFS_ERROR(EEXIST));
1754 ASSERT(probe == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
1755 return(XFS_ERROR(ENOENT));
1758 /*========================================================================
1760 *========================================================================*/
1763 * Move the indicated entries from one leaf to another.
1764 * NOTE: this routine modifies both source and destination leaves.
1768 xfs_dir_leaf_moveents(xfs_dir_leafblock_t *leaf_s, int start_s,
1769 xfs_dir_leafblock_t *leaf_d, int start_d,
1770 int count, xfs_mount_t *mp)
1772 xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1773 xfs_dir_leaf_entry_t *entry_s, *entry_d;
1777 * Check for nothing to do.
1783 * Set up environment.
1785 ASSERT(INT_GET(leaf_s->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1786 ASSERT(INT_GET(leaf_d->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1787 hdr_s = &leaf_s->hdr;
1788 hdr_d = &leaf_d->hdr;
1789 ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0) && (INT_GET(hdr_s->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1790 ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
1791 ((INT_GET(hdr_s->count, ARCH_CONVERT)*sizeof(*entry_s))+sizeof(*hdr_s)));
1792 ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
1793 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
1794 ((INT_GET(hdr_d->count, ARCH_CONVERT)*sizeof(*entry_d))+sizeof(*hdr_d)));
1796 ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
1797 ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
1798 ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
1801 * Move the entries in the destination leaf up to make a hole?
1803 if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
1804 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
1805 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1806 entry_s = &leaf_d->entries[start_d];
1807 entry_d = &leaf_d->entries[start_d + count];
1808 memcpy(entry_d, entry_s, tmp);
1812 * Copy all entry's in the same (sorted) order,
1813 * but allocate filenames packed and in sequence.
1815 entry_s = &leaf_s->entries[start_s];
1816 entry_d = &leaf_d->entries[start_d];
1817 for (i = 0; i < count; entry_s++, entry_d++, i++) {
1818 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
1819 tmp = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry_s);
1820 INT_MOD(hdr_d->firstused, ARCH_CONVERT, -(tmp));
1821 entry_d->hashval = entry_s->hashval; /* INT_: direct copy */
1822 INT_COPY(entry_d->nameidx, hdr_d->firstused, ARCH_CONVERT);
1823 entry_d->namelen = entry_s->namelen;
1824 ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1825 memcpy(XFS_DIR_LEAF_NAMESTRUCT(leaf_d, INT_GET(entry_d->nameidx, ARCH_CONVERT)),
1826 XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)), tmp);
1827 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1828 memset((char *)XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)),
1830 INT_MOD(hdr_s->namebytes, ARCH_CONVERT, -(entry_d->namelen));
1831 INT_MOD(hdr_d->namebytes, ARCH_CONVERT, entry_d->namelen);
1832 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
1833 INT_MOD(hdr_d->count, ARCH_CONVERT, +1);
1834 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1835 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1836 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
1841 * Zero out the entries we just copied.
1843 if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
1844 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1845 entry_s = &leaf_s->entries[start_s];
1846 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1847 memset((char *)entry_s, 0, tmp);
1850 * Move the remaining entries down to fill the hole,
1851 * then zero the entries at the top.
1853 tmp = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
1854 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1855 entry_s = &leaf_s->entries[start_s + count];
1856 entry_d = &leaf_s->entries[start_s];
1857 memcpy(entry_d, entry_s, tmp);
1859 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1860 entry_s = &leaf_s->entries[INT_GET(hdr_s->count, ARCH_CONVERT)];
1861 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1862 memset((char *)entry_s, 0, tmp);
1866 * Fill in the freemap information
1868 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_hdr_t));
1869 INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT, INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t));
1870 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));
1871 INT_SET(hdr_d->freemap[1].base, ARCH_CONVERT, (hdr_d->freemap[2].base = 0));
1872 INT_SET(hdr_d->freemap[1].size, ARCH_CONVERT, (hdr_d->freemap[2].size = 0));
1873 hdr_s->holes = 1; /* leaf may not be compact */
1877 * Compare two leaf blocks "order".
1880 xfs_dir_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
1882 xfs_dir_leafblock_t *leaf1, *leaf2;
1884 leaf1 = leaf1_bp->data;
1885 leaf2 = leaf2_bp->data;
1886 ASSERT((INT_GET(leaf1->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC) &&
1887 (INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC));
1888 if ((INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0) &&
1889 ((INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
1890 INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT)) ||
1891 (INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
1892 INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
1899 * Pick up the last hashvalue from a leaf block.
1902 xfs_dir_leaf_lasthash(xfs_dabuf_t *bp, int *count)
1904 xfs_dir_leafblock_t *leaf;
1907 ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC);
1909 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1910 if (!leaf->hdr.count)
1912 return(INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
1916 * Copy out directory entries for getdents(), for leaf directories.
1919 xfs_dir_leaf_getdents_int(
1929 xfs_dir_leafblock_t *leaf;
1930 xfs_dir_leaf_entry_t *entry;
1931 xfs_dir_leaf_name_t *namest;
1932 int entno, want_entno, i, nextentno;
1934 xfs_dahash_t cookhash;
1935 xfs_dahash_t nexthash = 0;
1936 #if (BITS_PER_LONG == 32)
1937 xfs_dahash_t lasthash = XFS_DA_MAXHASH;
1939 xfs_dir_put_args_t p;
1943 if (INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) != XFS_DIR_LEAF_MAGIC) {
1945 return(XFS_ERROR(ENOENT)); /* XXX wrong code */
1948 want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
1950 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
1952 xfs_dir_trace_g_dul("leaf: start", dp, uio, leaf);
1955 * Re-find our place.
1957 for (i = entno = 0, entry = &leaf->entries[0];
1958 i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
1961 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
1962 INT_GET(entry->nameidx, ARCH_CONVERT));
1965 ((char *)namest < (char *)leaf) ||
1966 ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
1967 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(1)",
1968 XFS_ERRLEVEL_LOW, mp, leaf);
1969 xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
1970 return XFS_ERROR(EFSCORRUPTED);
1972 if (INT_GET(entry->hashval, ARCH_CONVERT) >= cookhash) {
1973 if ( entno < want_entno
1974 && INT_GET(entry->hashval, ARCH_CONVERT)
1977 * Trying to get to a particular offset in a
1978 * run of equal-hashval entries.
1981 } else if ( want_entno > 0
1982 && entno == want_entno
1983 && INT_GET(entry->hashval, ARCH_CONVERT)
1993 if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
1994 xfs_dir_trace_g_du("leaf: hash not found", dp, uio);
1995 if (!INT_GET(leaf->hdr.info.forw, ARCH_CONVERT))
1997 XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
1999 * Don't set uio_offset if there's another block:
2000 * the node code will be setting uio_offset anyway.
2005 xfs_dir_trace_g_due("leaf: hash found", dp, uio, entry);
2012 * We're synchronized, start copying entries out to the user.
2014 for (; entno >= 0 && i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
2015 entry++, i++, (entno = nextentno)) {
2016 int lastresid=0, retval;
2017 xfs_dircook_t lastoffset;
2018 xfs_dahash_t thishash;
2021 * Check for a damaged directory leaf block and pick up
2022 * the inode number from this entry.
2024 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
2025 INT_GET(entry->nameidx, ARCH_CONVERT));
2028 ((char *)namest < (char *)leaf) ||
2029 ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
2030 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(2)",
2031 XFS_ERRLEVEL_LOW, mp, leaf);
2032 xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
2033 return XFS_ERROR(EFSCORRUPTED);
2036 xfs_dir_trace_g_duc("leaf: middle cookie ",
2039 if (i < (INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1)) {
2040 nexthash = INT_GET(entry[1].hashval, ARCH_CONVERT);
2042 if (nexthash == INT_GET(entry->hashval, ARCH_CONVERT))
2043 nextentno = entno + 1;
2046 XFS_PUT_COOKIE(p.cook, mp, bno, nextentno, nexthash);
2047 xfs_dir_trace_g_duc("leaf: middle cookie ",
2050 } else if ((thishash = INT_GET(leaf->hdr.info.forw,
2053 xfs_dir_leafblock_t *leaf2;
2055 ASSERT(nextda != -1);
2057 retval = xfs_da_read_buf(dp->i_transp, dp, thishash,
2058 nextda, &bp2, XFS_DATA_FORK);
2062 ASSERT(bp2 != NULL);
2067 (INT_GET(leaf2->hdr.info.magic, ARCH_CONVERT)
2068 != XFS_DIR_LEAF_MAGIC)
2069 || (INT_GET(leaf2->hdr.info.back, ARCH_CONVERT)
2070 != bno))) { /* GROT */
2071 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(3)",
2072 XFS_ERRLEVEL_LOW, mp,
2074 xfs_da_brelse(dp->i_transp, bp2);
2076 return(XFS_ERROR(EFSCORRUPTED));
2079 nexthash = INT_GET(leaf2->entries[0].hashval,
2082 XFS_PUT_COOKIE(p.cook, mp, thishash, 0, nexthash);
2083 xfs_da_brelse(dp->i_transp, bp2);
2084 xfs_dir_trace_g_duc("leaf: next blk cookie",
2088 XFS_PUT_COOKIE(p.cook, mp, 0, 0, XFS_DA_MAXHASH);
2092 * Save off the cookie so we can fall back should the
2093 * 'put' into the outgoing buffer fails. To handle a run
2094 * of equal-hashvals, the off_t structure on 64bit
2095 * builds has entno built into the cookie to ID the
2096 * entry. On 32bit builds, we only have space for the
2097 * hashval so we can't ID specific entries within a group
2098 * of same hashval entries. For this, lastoffset is set
2099 * to the first in the run of equal hashvals so we don't
2100 * include any entries unless we can include all entries
2101 * that share the same hashval. Hopefully the buffer
2102 * provided is big enough to handle it (see pv763517).
2104 #if (BITS_PER_LONG == 32)
2105 if ((thishash = INT_GET(entry->hashval, ARCH_CONVERT))
2107 XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2108 lastresid = uio->uio_resid;
2109 lasthash = thishash;
2111 xfs_dir_trace_g_duc("leaf: DUP COOKIES, skipped",
2115 thishash = INT_GET(entry->hashval, ARCH_CONVERT);
2116 XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2117 lastresid = uio->uio_resid;
2118 #endif /* BITS_PER_LONG == 32 */
2121 * Put the current entry into the outgoing buffer. If we fail
2122 * then restore the UIO to the first entry in the current
2123 * run of equal-hashval entries (probably one 1 entry long).
2125 p.ino = XFS_GET_DIR_INO8(namest->inumber);
2127 p.ino += mp->m_inoadd;
2129 p.name = (char *)namest->name;
2130 p.namelen = entry->namelen;
2135 uio->uio_offset = lastoffset.o;
2136 uio->uio_resid = lastresid;
2140 xfs_dir_trace_g_du("leaf: E-O-B", dp, uio);
2146 uio->uio_offset = p.cook.o;
2150 xfs_dir_trace_g_du("leaf: E-O-F", dp, uio);
2156 * Format a dirent64 structure and copy it out the the user's buffer.
2159 xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa)
2162 int reclen, namelen;
2166 namelen = pa->namelen;
2167 reclen = DIRENTSIZE(namelen);
2169 if (reclen > uio->uio_resid) {
2173 iovp = uio->uio_iov;
2174 idbp = (xfs_dirent_t *)iovp->iov_base;
2175 iovp->iov_base = (char *)idbp + reclen;
2176 iovp->iov_len -= reclen;
2177 uio->uio_resid -= reclen;
2178 idbp->d_reclen = reclen;
2179 idbp->d_ino = pa->ino;
2180 idbp->d_off = pa->cook.o;
2181 idbp->d_name[namelen] = '\0';
2183 memcpy(idbp->d_name, pa->name, namelen);
2188 * Format a dirent64 structure and copy it out the the user's buffer.
2191 xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa)
2193 int retval, reclen, namelen;
2197 namelen = pa->namelen;
2198 reclen = DIRENTSIZE(namelen);
2200 if (reclen > uio->uio_resid) {
2205 idbp->d_reclen = reclen;
2206 idbp->d_ino = pa->ino;
2207 idbp->d_off = pa->cook.o;
2208 idbp->d_name[namelen] = '\0';
2209 memcpy(idbp->d_name, pa->name, namelen);
2210 retval = uio_read((caddr_t)idbp, reclen, uio);
2211 pa->done = (retval == 0);