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;
631 xfs_ino_t parent = 0;
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(be16_to_cpu(leaf->hdr.info.magic) == 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(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
746 node->btree[0].hashval = cpu_to_be32(
747 INT_GET(leaf->entries[
748 INT_GET(leaf->hdr.count, ARCH_CONVERT)-1].hashval, ARCH_CONVERT));
749 xfs_da_buf_done(bp2);
750 node->btree[0].before = cpu_to_be32(blkno);
751 node->hdr.count = cpu_to_be16(1);
752 xfs_da_log_buf(args->trans, bp1,
753 XFS_DA_LOGRANGE(node, &node->btree[0], sizeof(node->btree[0])));
754 xfs_da_buf_done(bp1);
760 /*========================================================================
761 * Routines used for growing the Btree.
762 *========================================================================*/
765 * Create the initial contents of a leaf directory
766 * or a leaf in a node directory.
769 xfs_dir_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
771 xfs_dir_leafblock_t *leaf;
772 xfs_dir_leaf_hdr_t *hdr;
779 retval = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp, XFS_DATA_FORK);
784 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
786 hdr->info.magic = cpu_to_be16(XFS_DIR_LEAF_MAGIC);
787 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
789 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount) - 1);
790 INT_SET(hdr->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
791 INT_SET(hdr->freemap[0].size, ARCH_CONVERT, INT_GET(hdr->firstused, ARCH_CONVERT) - INT_GET(hdr->freemap[0].base, ARCH_CONVERT));
793 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
800 * Split the leaf node, rebalance, then add the new entry.
803 xfs_dir_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
804 xfs_da_state_blk_t *newblk)
811 * Allocate space for a new leaf node.
814 ASSERT(args != NULL);
815 ASSERT(oldblk->magic == XFS_DIR_LEAF_MAGIC);
816 error = xfs_da_grow_inode(args, &blkno);
819 error = xfs_dir_leaf_create(args, blkno, &newblk->bp);
822 newblk->blkno = blkno;
823 newblk->magic = XFS_DIR_LEAF_MAGIC;
826 * Rebalance the entries across the two leaves.
828 xfs_dir_leaf_rebalance(state, oldblk, newblk);
829 error = xfs_da_blk_link(state, oldblk, newblk);
834 * Insert the new entry in the correct block.
837 error = xfs_dir_leaf_add(oldblk->bp, args, oldblk->index);
839 error = xfs_dir_leaf_add(newblk->bp, args, newblk->index);
843 * Update last hashval in each block since we added the name.
845 oldblk->hashval = xfs_dir_leaf_lasthash(oldblk->bp, NULL);
846 newblk->hashval = xfs_dir_leaf_lasthash(newblk->bp, NULL);
851 * Add a name to the leaf directory structure.
853 * Must take into account fragmented leaves and leaves where spacemap has
854 * lost some freespace information (ie: holes).
857 xfs_dir_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index)
859 xfs_dir_leafblock_t *leaf;
860 xfs_dir_leaf_hdr_t *hdr;
861 xfs_dir_leaf_map_t *map;
862 int tablesize, entsize, sum, i, tmp, error;
865 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
866 ASSERT((index >= 0) && (index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
868 entsize = XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen);
871 * Search through freemap for first-fit on new name length.
872 * (may need to figure in size of entry struct too)
874 tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1) * (uint)sizeof(xfs_dir_leaf_entry_t)
875 + (uint)sizeof(xfs_dir_leaf_hdr_t);
876 map = &hdr->freemap[XFS_DIR_LEAF_MAPSIZE-1];
877 for (sum = 0, i = XFS_DIR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
878 if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
879 sum += INT_GET(map->size, ARCH_CONVERT);
883 continue; /* no space in this map */
885 if (INT_GET(map->base, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
886 tmp += (uint)sizeof(xfs_dir_leaf_entry_t);
887 if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
888 if (!args->justcheck)
889 xfs_dir_leaf_add_work(bp, args, index, i);
892 sum += INT_GET(map->size, ARCH_CONVERT);
896 * If there are no holes in the address space of the block,
897 * and we don't have enough freespace, then compaction will do us
898 * no good and we should just give up.
900 if (!hdr->holes && (sum < entsize))
901 return XFS_ERROR(ENOSPC);
904 * Compact the entries to coalesce free space.
905 * Pass the justcheck flag so the checking pass can return
906 * an error, without changing anything, if it won't fit.
908 error = xfs_dir_leaf_compact(args->trans, bp,
911 (uint)sizeof(xfs_dir_leaf_entry_t) : 0,
916 * After compaction, the block is guaranteed to have only one
917 * free region, in freemap[0]. If it is not big enough, give up.
919 if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT) <
920 (entsize + (uint)sizeof(xfs_dir_leaf_entry_t)))
921 return XFS_ERROR(ENOSPC);
923 if (!args->justcheck)
924 xfs_dir_leaf_add_work(bp, args, index, 0);
929 * Add a name to a leaf directory structure.
932 xfs_dir_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int index,
935 xfs_dir_leafblock_t *leaf;
936 xfs_dir_leaf_hdr_t *hdr;
937 xfs_dir_leaf_entry_t *entry;
938 xfs_dir_leaf_name_t *namest;
939 xfs_dir_leaf_map_t *map;
945 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
947 ASSERT((mapindex >= 0) && (mapindex < XFS_DIR_LEAF_MAPSIZE));
948 ASSERT((index >= 0) && (index <= INT_GET(hdr->count, ARCH_CONVERT)));
951 * Force open some space in the entry array and fill it in.
953 entry = &leaf->entries[index];
954 if (index < INT_GET(hdr->count, ARCH_CONVERT)) {
955 tmp = INT_GET(hdr->count, ARCH_CONVERT) - index;
956 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
957 memmove(entry + 1, entry, tmp);
958 xfs_da_log_buf(args->trans, bp,
959 XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
961 INT_MOD(hdr->count, ARCH_CONVERT, +1);
964 * Allocate space for the new string (at the end of the run).
966 map = &hdr->freemap[mapindex];
967 mp = args->trans->t_mountp;
968 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
969 ASSERT(INT_GET(map->size, ARCH_CONVERT) >= XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen));
970 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
971 INT_MOD(map->size, ARCH_CONVERT, -(XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen)));
972 INT_SET(entry->nameidx, ARCH_CONVERT, INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT));
973 INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
974 entry->namelen = args->namelen;
975 xfs_da_log_buf(args->trans, bp,
976 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
979 * Copy the string and inode number into the new space.
981 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
982 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &namest->inumber);
983 memcpy(namest->name, args->name, args->namelen);
984 xfs_da_log_buf(args->trans, bp,
985 XFS_DA_LOGRANGE(leaf, namest, XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry)));
988 * Update the control info for this leaf node
990 if (INT_GET(entry->nameidx, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
991 INT_COPY(hdr->firstused, entry->nameidx, ARCH_CONVERT);
992 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
993 tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1) * (uint)sizeof(xfs_dir_leaf_entry_t)
994 + (uint)sizeof(xfs_dir_leaf_hdr_t);
995 map = &hdr->freemap[0];
996 for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
997 if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
998 INT_MOD(map->base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
999 INT_MOD(map->size, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1002 INT_MOD(hdr->namebytes, ARCH_CONVERT, args->namelen);
1003 xfs_da_log_buf(args->trans, bp,
1004 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1008 * Garbage collect a leaf directory block by copying it to a new buffer.
1011 xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp, int musthave,
1014 xfs_dir_leafblock_t *leaf_s, *leaf_d;
1015 xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1018 char *tmpbuffer2=NULL;
1022 mp = trans->t_mountp;
1023 lbsize = XFS_LBSIZE(mp);
1024 tmpbuffer = kmem_alloc(lbsize, KM_SLEEP);
1025 ASSERT(tmpbuffer != NULL);
1026 memcpy(tmpbuffer, bp->data, lbsize);
1029 * Make a second copy in case xfs_dir_leaf_moveents()
1030 * below destroys the original.
1032 if (musthave || justcheck) {
1033 tmpbuffer2 = kmem_alloc(lbsize, KM_SLEEP);
1034 memcpy(tmpbuffer2, bp->data, lbsize);
1036 memset(bp->data, 0, lbsize);
1039 * Copy basic information
1041 leaf_s = (xfs_dir_leafblock_t *)tmpbuffer;
1043 hdr_s = &leaf_s->hdr;
1044 hdr_d = &leaf_d->hdr;
1045 hdr_d->info = hdr_s->info; /* struct copy */
1046 INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize);
1047 if (!hdr_d->firstused)
1048 INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize - 1);
1049 hdr_d->namebytes = 0;
1052 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
1053 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));
1056 * Copy all entry's in the same (sorted) order,
1057 * but allocate filenames packed and in sequence.
1058 * This changes the source (leaf_s) as well.
1060 xfs_dir_leaf_moveents(leaf_s, 0, leaf_d, 0, (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
1062 if (musthave && INT_GET(hdr_d->freemap[0].size, ARCH_CONVERT) < musthave)
1063 rval = XFS_ERROR(ENOSPC);
1067 if (justcheck || rval == ENOSPC) {
1069 memcpy(bp->data, tmpbuffer2, lbsize);
1071 xfs_da_log_buf(trans, bp, 0, lbsize - 1);
1074 kmem_free(tmpbuffer, lbsize);
1075 if (musthave || justcheck)
1076 kmem_free(tmpbuffer2, lbsize);
1081 * Redistribute the directory entries between two leaf nodes,
1082 * taking into account the size of the new entry.
1084 * NOTE: if new block is empty, then it will get the upper half of old block.
1087 xfs_dir_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1088 xfs_da_state_blk_t *blk2)
1090 xfs_da_state_blk_t *tmp_blk;
1091 xfs_dir_leafblock_t *leaf1, *leaf2;
1092 xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1093 int count, totallen, max, space, swap;
1096 * Set up environment.
1098 ASSERT(blk1->magic == XFS_DIR_LEAF_MAGIC);
1099 ASSERT(blk2->magic == XFS_DIR_LEAF_MAGIC);
1100 leaf1 = blk1->bp->data;
1101 leaf2 = blk2->bp->data;
1102 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1103 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1106 * Check ordering of blocks, reverse if it makes things simpler.
1109 if (xfs_dir_leaf_order(blk1->bp, blk2->bp)) {
1113 leaf1 = blk1->bp->data;
1114 leaf2 = blk2->bp->data;
1121 * Examine entries until we reduce the absolute difference in
1122 * byte usage between the two blocks to a minimum. Then get
1123 * the direction to copy and the number of elements to move.
1125 state->inleaf = xfs_dir_leaf_figure_balance(state, blk1, blk2,
1128 state->inleaf = !state->inleaf;
1131 * Move any entries required from leaf to leaf:
1133 if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
1135 * Figure the total bytes to be added to the destination leaf.
1137 count = INT_GET(hdr1->count, ARCH_CONVERT) - count; /* number entries being moved */
1138 space = INT_GET(hdr1->namebytes, ARCH_CONVERT) - totallen;
1139 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1140 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1143 * leaf2 is the destination, compact it if it looks tight.
1145 max = INT_GET(hdr2->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1146 max -= INT_GET(hdr2->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1148 xfs_dir_leaf_compact(state->args->trans, blk2->bp,
1153 * Move high entries from leaf1 to low end of leaf2.
1155 xfs_dir_leaf_moveents(leaf1, INT_GET(hdr1->count, ARCH_CONVERT) - count,
1156 leaf2, 0, count, state->mp);
1158 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1159 state->blocksize-1);
1160 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1161 state->blocksize-1);
1163 } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
1165 * Figure the total bytes to be added to the destination leaf.
1167 count -= INT_GET(hdr1->count, ARCH_CONVERT); /* number entries being moved */
1168 space = totallen - INT_GET(hdr1->namebytes, ARCH_CONVERT);
1169 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1170 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1173 * leaf1 is the destination, compact it if it looks tight.
1175 max = INT_GET(hdr1->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1176 max -= INT_GET(hdr1->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1178 xfs_dir_leaf_compact(state->args->trans, blk1->bp,
1183 * Move low entries from leaf2 to high end of leaf1.
1185 xfs_dir_leaf_moveents(leaf2, 0, leaf1, (int)INT_GET(hdr1->count, ARCH_CONVERT),
1188 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1189 state->blocksize-1);
1190 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1191 state->blocksize-1);
1195 * Copy out last hashval in each block for B-tree code.
1197 blk1->hashval = INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1198 blk2->hashval = INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1201 * Adjust the expected index for insertion.
1202 * GROT: this doesn't work unless blk2 was originally empty.
1204 if (!state->inleaf) {
1205 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1210 * Examine entries until we reduce the absolute difference in
1211 * byte usage between the two blocks to a minimum.
1212 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1213 * GROT: there will always be enough room in either block for a new entry.
1214 * GROT: Do a double-split for this case?
1217 xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
1218 xfs_da_state_blk_t *blk1,
1219 xfs_da_state_blk_t *blk2,
1220 int *countarg, int *namebytesarg)
1222 xfs_dir_leafblock_t *leaf1, *leaf2;
1223 xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1224 xfs_dir_leaf_entry_t *entry;
1225 int count, max, totallen, half;
1226 int lastdelta, foundit, tmp;
1229 * Set up environment.
1231 leaf1 = blk1->bp->data;
1232 leaf2 = blk2->bp->data;
1239 * Examine entries until we reduce the absolute difference in
1240 * byte usage between the two blocks to a minimum.
1242 max = INT_GET(hdr1->count, ARCH_CONVERT) + INT_GET(hdr2->count, ARCH_CONVERT);
1243 half = (max+1) * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1244 half += INT_GET(hdr1->namebytes, ARCH_CONVERT) + INT_GET(hdr2->namebytes, ARCH_CONVERT) + state->args->namelen;
1246 lastdelta = state->blocksize;
1247 entry = &leaf1->entries[0];
1248 for (count = 0; count < max; entry++, count++) {
1250 #define XFS_DIR_ABS(A) (((A) < 0) ? -(A) : (A))
1252 * The new entry is in the first block, account for it.
1254 if (count == blk1->index) {
1255 tmp = totallen + (uint)sizeof(*entry)
1256 + XFS_DIR_LEAF_ENTSIZE_BYNAME(state->args->namelen);
1257 if (XFS_DIR_ABS(half - tmp) > lastdelta)
1259 lastdelta = XFS_DIR_ABS(half - tmp);
1265 * Wrap around into the second block if necessary.
1267 if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
1269 entry = &leaf1->entries[0];
1273 * Figure out if next leaf entry would be too much.
1275 tmp = totallen + (uint)sizeof(*entry)
1276 + XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1277 if (XFS_DIR_ABS(half - tmp) > lastdelta)
1279 lastdelta = XFS_DIR_ABS(half - tmp);
1285 * Calculate the number of namebytes that will end up in lower block.
1286 * If new entry not in lower block, fix up the count.
1289 count * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1291 totallen -= (sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1) +
1292 state->args->namelen;
1296 *namebytesarg = totallen;
1300 /*========================================================================
1301 * Routines used for shrinking the Btree.
1302 *========================================================================*/
1305 * Check a leaf block and its neighbors to see if the block should be
1306 * collapsed into one or the other neighbor. Always keep the block
1307 * with the smaller block number.
1308 * If the current block is over 50% full, don't try to join it, return 0.
1309 * If the block is empty, fill in the state structure and return 2.
1310 * If it can be collapsed, fill in the state structure and return 1.
1311 * If nothing can be done, return 0.
1314 xfs_dir_leaf_toosmall(xfs_da_state_t *state, int *action)
1316 xfs_dir_leafblock_t *leaf;
1317 xfs_da_state_blk_t *blk;
1318 xfs_da_blkinfo_t *info;
1319 int count, bytes, forward, error, retval, i;
1324 * Check for the degenerate case of the block being over 50% full.
1325 * If so, it's not worth even looking to see if we might be able
1326 * to coalesce with a sibling.
1328 blk = &state->path.blk[ state->path.active-1 ];
1329 info = blk->bp->data;
1330 ASSERT(be16_to_cpu(info->magic) == XFS_DIR_LEAF_MAGIC);
1331 leaf = (xfs_dir_leafblock_t *)info;
1332 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1333 bytes = (uint)sizeof(xfs_dir_leaf_hdr_t) +
1334 count * (uint)sizeof(xfs_dir_leaf_entry_t) +
1335 count * ((uint)sizeof(xfs_dir_leaf_name_t)-1) +
1336 INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1337 if (bytes > (state->blocksize >> 1)) {
1338 *action = 0; /* blk over 50%, don't try to join */
1343 * Check for the degenerate case of the block being empty.
1344 * If the block is empty, we'll simply delete it, no need to
1345 * coalesce it with a sibling block. We choose (aribtrarily)
1346 * to merge with the forward block unless it is NULL.
1350 * Make altpath point to the block we want to keep and
1351 * path point to the block we want to drop (this one).
1353 forward = (info->forw != 0);
1354 memcpy(&state->altpath, &state->path, sizeof(state->path));
1355 error = xfs_da_path_shift(state, &state->altpath, forward,
1368 * Examine each sibling block to see if we can coalesce with
1369 * at least 25% free space to spare. We need to figure out
1370 * whether to merge with the forward or the backward block.
1371 * We prefer coalescing with the lower numbered sibling so as
1372 * to shrink a directory over time.
1374 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back)); /* start with smaller blk num */
1375 for (i = 0; i < 2; forward = !forward, i++) {
1377 blkno = be32_to_cpu(info->forw);
1379 blkno = be32_to_cpu(info->back);
1382 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1389 leaf = (xfs_dir_leafblock_t *)info;
1390 count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1391 bytes = state->blocksize - (state->blocksize>>2);
1392 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1394 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1395 count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
1396 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1397 bytes -= count * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1398 bytes -= count * (uint)sizeof(xfs_dir_leaf_entry_t);
1399 bytes -= (uint)sizeof(xfs_dir_leaf_hdr_t);
1401 break; /* fits with at least 25% to spare */
1403 xfs_da_brelse(state->args->trans, bp);
1409 xfs_da_buf_done(bp);
1412 * Make altpath point to the block we want to keep (the lower
1413 * numbered block) and path point to the block we want to drop.
1415 memcpy(&state->altpath, &state->path, sizeof(state->path));
1416 if (blkno < blk->blkno) {
1417 error = xfs_da_path_shift(state, &state->altpath, forward,
1420 error = xfs_da_path_shift(state, &state->path, forward,
1434 * Remove a name from the leaf directory structure.
1436 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1437 * If two leaves are 37% full, when combined they will leave 25% free.
1440 xfs_dir_leaf_remove(xfs_trans_t *trans, xfs_dabuf_t *bp, int index)
1442 xfs_dir_leafblock_t *leaf;
1443 xfs_dir_leaf_hdr_t *hdr;
1444 xfs_dir_leaf_map_t *map;
1445 xfs_dir_leaf_entry_t *entry;
1446 xfs_dir_leaf_name_t *namest;
1447 int before, after, smallest, entsize;
1448 int tablesize, tmp, i;
1452 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1454 mp = trans->t_mountp;
1455 ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0) && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1456 ASSERT((index >= 0) && (index < INT_GET(hdr->count, ARCH_CONVERT)));
1457 ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
1458 entry = &leaf->entries[index];
1459 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1460 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1463 * Scan through free region table:
1464 * check for adjacency of free'd entry with an existing one,
1465 * find smallest free region in case we need to replace it,
1466 * adjust any map that borders the entry table,
1468 tablesize = INT_GET(hdr->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1469 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1470 map = &hdr->freemap[0];
1471 tmp = INT_GET(map->size, ARCH_CONVERT);
1472 before = after = -1;
1473 smallest = XFS_DIR_LEAF_MAPSIZE - 1;
1474 entsize = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1475 for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
1476 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1477 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1478 if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
1479 INT_MOD(map->base, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1480 INT_MOD(map->size, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
1483 if ((INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT)) == INT_GET(entry->nameidx, ARCH_CONVERT)) {
1485 } else if (INT_GET(map->base, ARCH_CONVERT) == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
1487 } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
1488 tmp = INT_GET(map->size, ARCH_CONVERT);
1494 * Coalesce adjacent freemap regions,
1495 * or replace the smallest region.
1497 if ((before >= 0) || (after >= 0)) {
1498 if ((before >= 0) && (after >= 0)) {
1499 map = &hdr->freemap[before];
1500 INT_MOD(map->size, ARCH_CONVERT, entsize);
1501 INT_MOD(map->size, ARCH_CONVERT, INT_GET(hdr->freemap[after].size, ARCH_CONVERT));
1502 hdr->freemap[after].base = 0;
1503 hdr->freemap[after].size = 0;
1504 } else if (before >= 0) {
1505 map = &hdr->freemap[before];
1506 INT_MOD(map->size, ARCH_CONVERT, entsize);
1508 map = &hdr->freemap[after];
1509 INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1510 INT_MOD(map->size, ARCH_CONVERT, entsize);
1514 * Replace smallest region (if it is smaller than free'd entry)
1516 map = &hdr->freemap[smallest];
1517 if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
1518 INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1519 INT_SET(map->size, ARCH_CONVERT, entsize);
1524 * Did we remove the first entry?
1526 if (INT_GET(entry->nameidx, ARCH_CONVERT) == INT_GET(hdr->firstused, ARCH_CONVERT))
1532 * Compress the remaining entries and zero out the removed stuff.
1534 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1535 memset((char *)namest, 0, entsize);
1536 xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, namest, entsize));
1538 INT_MOD(hdr->namebytes, ARCH_CONVERT, -(entry->namelen));
1539 tmp = (INT_GET(hdr->count, ARCH_CONVERT) - index) * (uint)sizeof(xfs_dir_leaf_entry_t);
1540 memmove(entry, entry + 1, tmp);
1541 INT_MOD(hdr->count, ARCH_CONVERT, -1);
1542 xfs_da_log_buf(trans, bp,
1543 XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
1544 entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
1545 memset((char *)entry, 0, sizeof(xfs_dir_leaf_entry_t));
1548 * If we removed the first entry, re-find the first used byte
1549 * in the name area. Note that if the entry was the "firstused",
1550 * then we don't have a "hole" in our block resulting from
1551 * removing the name.
1554 tmp = XFS_LBSIZE(mp);
1555 entry = &leaf->entries[0];
1556 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
1557 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1558 ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1559 if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
1560 tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
1562 INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
1563 if (!hdr->firstused)
1564 INT_SET(hdr->firstused, ARCH_CONVERT, tmp - 1);
1566 hdr->holes = 1; /* mark as needing compaction */
1569 xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1572 * Check if leaf is less than 50% full, caller may want to
1573 * "join" the leaf with a sibling if so.
1575 tmp = (uint)sizeof(xfs_dir_leaf_hdr_t);
1576 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1577 tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1578 tmp += INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1579 if (tmp < mp->m_dir_magicpct)
1580 return 1; /* leaf is < 37% full */
1585 * Move all the directory entries from drop_leaf into save_leaf.
1588 xfs_dir_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1589 xfs_da_state_blk_t *save_blk)
1591 xfs_dir_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1592 xfs_dir_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1597 * Set up environment.
1600 ASSERT(drop_blk->magic == XFS_DIR_LEAF_MAGIC);
1601 ASSERT(save_blk->magic == XFS_DIR_LEAF_MAGIC);
1602 drop_leaf = drop_blk->bp->data;
1603 save_leaf = save_blk->bp->data;
1604 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1605 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1606 drop_hdr = &drop_leaf->hdr;
1607 save_hdr = &save_leaf->hdr;
1610 * Save last hashval from dying block for later Btree fixup.
1612 drop_blk->hashval = INT_GET(drop_leaf->entries[ drop_leaf->hdr.count-1 ].hashval, ARCH_CONVERT);
1615 * Check if we need a temp buffer, or can we do it in place.
1616 * Note that we don't check "leaf" for holes because we will
1617 * always be dropping it, toosmall() decided that for us already.
1619 if (save_hdr->holes == 0) {
1621 * dest leaf has no holes, so we add there. May need
1622 * to make some room in the entry array.
1624 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1625 xfs_dir_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1626 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1628 xfs_dir_leaf_moveents(drop_leaf, 0,
1629 save_leaf, INT_GET(save_hdr->count, ARCH_CONVERT),
1630 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1634 * Destination has holes, so we make a temporary copy
1635 * of the leaf and add them both to that.
1637 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1638 ASSERT(tmpbuffer != NULL);
1639 memset(tmpbuffer, 0, state->blocksize);
1640 tmp_leaf = (xfs_dir_leafblock_t *)tmpbuffer;
1641 tmp_hdr = &tmp_leaf->hdr;
1642 tmp_hdr->info = save_hdr->info; /* struct copy */
1644 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
1645 if (!tmp_hdr->firstused)
1646 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize - 1);
1647 tmp_hdr->namebytes = 0;
1648 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1649 xfs_dir_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1650 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1651 xfs_dir_leaf_moveents(save_leaf, 0,
1652 tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1653 (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1655 xfs_dir_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1656 (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1657 xfs_dir_leaf_moveents(drop_leaf, 0,
1658 tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1659 (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1661 memcpy(save_leaf, tmp_leaf, state->blocksize);
1662 kmem_free(tmpbuffer, state->blocksize);
1665 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1666 state->blocksize - 1);
1669 * Copy out last hashval in each block for B-tree code.
1671 save_blk->hashval = INT_GET(save_leaf->entries[ INT_GET(save_leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1674 /*========================================================================
1675 * Routines used for finding things in the Btree.
1676 *========================================================================*/
1679 * Look up a name in a leaf directory structure.
1680 * This is the internal routine, it uses the caller's buffer.
1682 * Note that duplicate keys are allowed, but only check within the
1683 * current leaf node. The Btree code must check in adjacent leaf nodes.
1685 * Return in *index the index into the entry[] array of either the found
1686 * entry, or where the entry should have been (insert before that entry).
1688 * Don't change the args->inumber unless we find the filename.
1691 xfs_dir_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args, int *index)
1693 xfs_dir_leafblock_t *leaf;
1694 xfs_dir_leaf_entry_t *entry;
1695 xfs_dir_leaf_name_t *namest;
1697 xfs_dahash_t hashval;
1700 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1701 ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) < (XFS_LBSIZE(args->dp->i_mount)/8));
1704 * Binary search. (note: small blocks will skip this loop)
1706 hashval = args->hashval;
1707 probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
1708 for (entry = &leaf->entries[probe]; span > 4;
1709 entry = &leaf->entries[probe]) {
1711 if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
1713 else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
1718 ASSERT((probe >= 0) && \
1719 ((!leaf->hdr.count) || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
1720 ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT) == hashval));
1723 * Since we may have duplicate hashval's, find the first matching
1724 * hashval in the leaf.
1726 while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT) >= hashval)) {
1730 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
1734 if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT)) || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
1736 ASSERT(args->oknoent);
1737 return XFS_ERROR(ENOENT);
1741 * Duplicate keys may be present, so search all of them for a match.
1743 while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval)) {
1744 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1745 if (entry->namelen == args->namelen &&
1746 namest->name[0] == args->name[0] &&
1747 memcmp(args->name, namest->name, args->namelen) == 0) {
1748 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args->inumber);
1750 return XFS_ERROR(EEXIST);
1756 ASSERT(probe == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
1757 return XFS_ERROR(ENOENT);
1760 /*========================================================================
1762 *========================================================================*/
1765 * Move the indicated entries from one leaf to another.
1766 * NOTE: this routine modifies both source and destination leaves.
1770 xfs_dir_leaf_moveents(xfs_dir_leafblock_t *leaf_s, int start_s,
1771 xfs_dir_leafblock_t *leaf_d, int start_d,
1772 int count, xfs_mount_t *mp)
1774 xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1775 xfs_dir_leaf_entry_t *entry_s, *entry_d;
1779 * Check for nothing to do.
1785 * Set up environment.
1787 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1788 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1789 hdr_s = &leaf_s->hdr;
1790 hdr_d = &leaf_d->hdr;
1791 ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0) && (INT_GET(hdr_s->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1792 ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
1793 ((INT_GET(hdr_s->count, ARCH_CONVERT)*sizeof(*entry_s))+sizeof(*hdr_s)));
1794 ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
1795 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
1796 ((INT_GET(hdr_d->count, ARCH_CONVERT)*sizeof(*entry_d))+sizeof(*hdr_d)));
1798 ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
1799 ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
1800 ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
1803 * Move the entries in the destination leaf up to make a hole?
1805 if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
1806 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
1807 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1808 entry_s = &leaf_d->entries[start_d];
1809 entry_d = &leaf_d->entries[start_d + count];
1810 memcpy(entry_d, entry_s, tmp);
1814 * Copy all entry's in the same (sorted) order,
1815 * but allocate filenames packed and in sequence.
1817 entry_s = &leaf_s->entries[start_s];
1818 entry_d = &leaf_d->entries[start_d];
1819 for (i = 0; i < count; entry_s++, entry_d++, i++) {
1820 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
1821 tmp = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry_s);
1822 INT_MOD(hdr_d->firstused, ARCH_CONVERT, -(tmp));
1823 entry_d->hashval = entry_s->hashval; /* INT_: direct copy */
1824 INT_COPY(entry_d->nameidx, hdr_d->firstused, ARCH_CONVERT);
1825 entry_d->namelen = entry_s->namelen;
1826 ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1827 memcpy(XFS_DIR_LEAF_NAMESTRUCT(leaf_d, INT_GET(entry_d->nameidx, ARCH_CONVERT)),
1828 XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)), tmp);
1829 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1830 memset((char *)XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)),
1832 INT_MOD(hdr_s->namebytes, ARCH_CONVERT, -(entry_d->namelen));
1833 INT_MOD(hdr_d->namebytes, ARCH_CONVERT, entry_d->namelen);
1834 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
1835 INT_MOD(hdr_d->count, ARCH_CONVERT, +1);
1836 tmp = INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1837 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1838 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
1843 * Zero out the entries we just copied.
1845 if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
1846 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1847 entry_s = &leaf_s->entries[start_s];
1848 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1849 memset((char *)entry_s, 0, tmp);
1852 * Move the remaining entries down to fill the hole,
1853 * then zero the entries at the top.
1855 tmp = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
1856 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1857 entry_s = &leaf_s->entries[start_s + count];
1858 entry_d = &leaf_s->entries[start_s];
1859 memcpy(entry_d, entry_s, tmp);
1861 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1862 entry_s = &leaf_s->entries[INT_GET(hdr_s->count, ARCH_CONVERT)];
1863 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1864 memset((char *)entry_s, 0, tmp);
1868 * Fill in the freemap information
1870 INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_hdr_t));
1871 INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT, INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t));
1872 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));
1873 INT_SET(hdr_d->freemap[1].base, ARCH_CONVERT, (hdr_d->freemap[2].base = 0));
1874 INT_SET(hdr_d->freemap[1].size, ARCH_CONVERT, (hdr_d->freemap[2].size = 0));
1875 hdr_s->holes = 1; /* leaf may not be compact */
1879 * Compare two leaf blocks "order".
1882 xfs_dir_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
1884 xfs_dir_leafblock_t *leaf1, *leaf2;
1886 leaf1 = leaf1_bp->data;
1887 leaf2 = leaf2_bp->data;
1888 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC) &&
1889 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC));
1890 if ((INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0) &&
1891 ((INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
1892 INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT)) ||
1893 (INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
1894 INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
1901 * Pick up the last hashvalue from a leaf block.
1904 xfs_dir_leaf_lasthash(xfs_dabuf_t *bp, int *count)
1906 xfs_dir_leafblock_t *leaf;
1909 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1911 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1912 if (!leaf->hdr.count)
1914 return(INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
1918 * Copy out directory entries for getdents(), for leaf directories.
1921 xfs_dir_leaf_getdents_int(
1931 xfs_dir_leafblock_t *leaf;
1932 xfs_dir_leaf_entry_t *entry;
1933 xfs_dir_leaf_name_t *namest;
1934 int entno, want_entno, i, nextentno;
1936 xfs_dahash_t cookhash;
1937 xfs_dahash_t nexthash = 0;
1938 #if (BITS_PER_LONG == 32)
1939 xfs_dahash_t lasthash = XFS_DA_MAXHASH;
1941 xfs_dir_put_args_t p;
1945 if (be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR_LEAF_MAGIC) {
1947 return XFS_ERROR(ENOENT); /* XXX wrong code */
1950 want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
1952 cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
1954 xfs_dir_trace_g_dul("leaf: start", dp, uio, leaf);
1957 * Re-find our place.
1959 for (i = entno = 0, entry = &leaf->entries[0];
1960 i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
1963 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
1964 INT_GET(entry->nameidx, ARCH_CONVERT));
1967 ((char *)namest < (char *)leaf) ||
1968 ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
1969 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(1)",
1970 XFS_ERRLEVEL_LOW, mp, leaf);
1971 xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
1972 return XFS_ERROR(EFSCORRUPTED);
1974 if (INT_GET(entry->hashval, ARCH_CONVERT) >= cookhash) {
1975 if ( entno < want_entno
1976 && INT_GET(entry->hashval, ARCH_CONVERT)
1979 * Trying to get to a particular offset in a
1980 * run of equal-hashval entries.
1983 } else if ( want_entno > 0
1984 && entno == want_entno
1985 && INT_GET(entry->hashval, ARCH_CONVERT)
1995 if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
1996 xfs_dir_trace_g_du("leaf: hash not found", dp, uio);
1997 if (!leaf->hdr.info.forw)
1999 XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
2001 * Don't set uio_offset if there's another block:
2002 * the node code will be setting uio_offset anyway.
2007 xfs_dir_trace_g_due("leaf: hash found", dp, uio, entry);
2014 * We're synchronized, start copying entries out to the user.
2016 for (; entno >= 0 && i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
2017 entry++, i++, (entno = nextentno)) {
2018 int lastresid=0, retval;
2019 xfs_dircook_t lastoffset;
2020 xfs_dahash_t thishash;
2023 * Check for a damaged directory leaf block and pick up
2024 * the inode number from this entry.
2026 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
2027 INT_GET(entry->nameidx, ARCH_CONVERT));
2030 ((char *)namest < (char *)leaf) ||
2031 ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
2032 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(2)",
2033 XFS_ERRLEVEL_LOW, mp, leaf);
2034 xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
2035 return XFS_ERROR(EFSCORRUPTED);
2038 xfs_dir_trace_g_duc("leaf: middle cookie ",
2041 if (i < (INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1)) {
2042 nexthash = INT_GET(entry[1].hashval, ARCH_CONVERT);
2044 if (nexthash == INT_GET(entry->hashval, ARCH_CONVERT))
2045 nextentno = entno + 1;
2048 XFS_PUT_COOKIE(p.cook, mp, bno, nextentno, nexthash);
2049 xfs_dir_trace_g_duc("leaf: middle cookie ",
2052 } else if ((thishash = be32_to_cpu(leaf->hdr.info.forw))) {
2054 xfs_dir_leafblock_t *leaf2;
2056 ASSERT(nextda != -1);
2058 retval = xfs_da_read_buf(dp->i_transp, dp, thishash,
2059 nextda, &bp2, XFS_DATA_FORK);
2063 ASSERT(bp2 != NULL);
2068 (be16_to_cpu(leaf2->hdr.info.magic)
2069 != XFS_DIR_LEAF_MAGIC)
2070 || (be32_to_cpu(leaf2->hdr.info.back)
2071 != bno))) { /* GROT */
2072 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(3)",
2073 XFS_ERRLEVEL_LOW, mp,
2075 xfs_da_brelse(dp->i_transp, bp2);
2077 return XFS_ERROR(EFSCORRUPTED);
2080 nexthash = INT_GET(leaf2->entries[0].hashval,
2083 XFS_PUT_COOKIE(p.cook, mp, thishash, 0, nexthash);
2084 xfs_da_brelse(dp->i_transp, bp2);
2085 xfs_dir_trace_g_duc("leaf: next blk cookie",
2089 XFS_PUT_COOKIE(p.cook, mp, 0, 0, XFS_DA_MAXHASH);
2093 * Save off the cookie so we can fall back should the
2094 * 'put' into the outgoing buffer fails. To handle a run
2095 * of equal-hashvals, the off_t structure on 64bit
2096 * builds has entno built into the cookie to ID the
2097 * entry. On 32bit builds, we only have space for the
2098 * hashval so we can't ID specific entries within a group
2099 * of same hashval entries. For this, lastoffset is set
2100 * to the first in the run of equal hashvals so we don't
2101 * include any entries unless we can include all entries
2102 * that share the same hashval. Hopefully the buffer
2103 * provided is big enough to handle it (see pv763517).
2105 #if (BITS_PER_LONG == 32)
2106 if ((thishash = INT_GET(entry->hashval, ARCH_CONVERT))
2108 XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2109 lastresid = uio->uio_resid;
2110 lasthash = thishash;
2112 xfs_dir_trace_g_duc("leaf: DUP COOKIES, skipped",
2116 thishash = INT_GET(entry->hashval, ARCH_CONVERT);
2117 XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2118 lastresid = uio->uio_resid;
2119 #endif /* BITS_PER_LONG == 32 */
2122 * Put the current entry into the outgoing buffer. If we fail
2123 * then restore the UIO to the first entry in the current
2124 * run of equal-hashval entries (probably one 1 entry long).
2126 p.ino = XFS_GET_DIR_INO8(namest->inumber);
2128 p.ino += mp->m_inoadd;
2130 p.name = (char *)namest->name;
2131 p.namelen = entry->namelen;
2136 uio->uio_offset = lastoffset.o;
2137 uio->uio_resid = lastresid;
2141 xfs_dir_trace_g_du("leaf: E-O-B", dp, uio);
2147 uio->uio_offset = p.cook.o;
2151 xfs_dir_trace_g_du("leaf: E-O-F", dp, uio);
2157 * Format a dirent64 structure and copy it out the the user's buffer.
2160 xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa)
2163 int reclen, namelen;
2167 namelen = pa->namelen;
2168 reclen = DIRENTSIZE(namelen);
2170 if (reclen > uio->uio_resid) {
2174 iovp = uio->uio_iov;
2175 idbp = (xfs_dirent_t *)iovp->iov_base;
2176 iovp->iov_base = (char *)idbp + reclen;
2177 iovp->iov_len -= reclen;
2178 uio->uio_resid -= reclen;
2179 idbp->d_reclen = reclen;
2180 idbp->d_ino = pa->ino;
2181 idbp->d_off = pa->cook.o;
2182 idbp->d_name[namelen] = '\0';
2184 memcpy(idbp->d_name, pa->name, namelen);
2189 * Format a dirent64 structure and copy it out the the user's buffer.
2192 xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa)
2194 int retval, reclen, namelen;
2198 namelen = pa->namelen;
2199 reclen = DIRENTSIZE(namelen);
2201 if (reclen > uio->uio_resid) {
2206 idbp->d_reclen = reclen;
2207 idbp->d_ino = pa->ino;
2208 idbp->d_off = pa->cook.o;
2209 idbp->d_name[namelen] = '\0';
2210 memcpy(idbp->d_name, pa->name, namelen);
2211 retval = uio_read((caddr_t)idbp, reclen, uio);
2212 pa->done = (retval == 0);