2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
24 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dinode.h"
31 #include "xfs_inode.h"
33 #include "xfs_dir2_format.h"
34 #include "xfs_dir2_priv.h"
35 #include "xfs_error.h"
36 #include "xfs_trace.h"
37 #include "xfs_buf_item.h"
38 #include "xfs_cksum.h"
41 * Local function declarations.
43 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
44 int *indexp, struct xfs_buf **dbpp);
45 static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
47 static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
50 * Check the internal consistency of a leaf1 block.
51 * Pop an assert if something is wrong.
54 #define xfs_dir3_leaf_check(mp, bp) \
56 if (!xfs_dir3_leaf1_check((mp), (bp))) \
65 struct xfs_dir2_leaf *leaf = bp->b_addr;
66 struct xfs_dir3_icleaf_hdr leafhdr;
68 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
70 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
71 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
72 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
74 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
77 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
80 #define xfs_dir3_leaf_check(mp, bp)
84 xfs_dir3_leaf_hdr_from_disk(
85 struct xfs_dir3_icleaf_hdr *to,
86 struct xfs_dir2_leaf *from)
88 if (from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
89 from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
90 to->forw = be32_to_cpu(from->hdr.info.forw);
91 to->back = be32_to_cpu(from->hdr.info.back);
92 to->magic = be16_to_cpu(from->hdr.info.magic);
93 to->count = be16_to_cpu(from->hdr.count);
94 to->stale = be16_to_cpu(from->hdr.stale);
96 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
98 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
99 to->back = be32_to_cpu(hdr3->info.hdr.back);
100 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
101 to->count = be16_to_cpu(hdr3->count);
102 to->stale = be16_to_cpu(hdr3->stale);
105 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
106 to->magic == XFS_DIR3_LEAF1_MAGIC ||
107 to->magic == XFS_DIR2_LEAFN_MAGIC ||
108 to->magic == XFS_DIR3_LEAFN_MAGIC);
112 xfs_dir3_leaf_hdr_to_disk(
113 struct xfs_dir2_leaf *to,
114 struct xfs_dir3_icleaf_hdr *from)
116 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
117 from->magic == XFS_DIR3_LEAF1_MAGIC ||
118 from->magic == XFS_DIR2_LEAFN_MAGIC ||
119 from->magic == XFS_DIR3_LEAFN_MAGIC);
121 if (from->magic == XFS_DIR2_LEAF1_MAGIC ||
122 from->magic == XFS_DIR2_LEAFN_MAGIC) {
123 to->hdr.info.forw = cpu_to_be32(from->forw);
124 to->hdr.info.back = cpu_to_be32(from->back);
125 to->hdr.info.magic = cpu_to_be16(from->magic);
126 to->hdr.count = cpu_to_be16(from->count);
127 to->hdr.stale = cpu_to_be16(from->stale);
129 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
131 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
132 hdr3->info.hdr.back = cpu_to_be32(from->back);
133 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
134 hdr3->count = cpu_to_be16(from->count);
135 hdr3->stale = cpu_to_be16(from->stale);
140 xfs_dir3_leaf_check_int(
141 struct xfs_mount *mp,
142 struct xfs_dir3_icleaf_hdr *hdr,
143 struct xfs_dir2_leaf *leaf)
145 struct xfs_dir2_leaf_entry *ents;
146 xfs_dir2_leaf_tail_t *ltp;
150 ents = xfs_dir3_leaf_ents_p(leaf);
151 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
154 * XXX (dgc): This value is not restrictive enough.
155 * Should factor in the size of the bests table as well.
156 * We can deduce a value for that from di_size.
158 if (hdr->count > xfs_dir3_max_leaf_ents(mp, leaf))
161 /* Leaves and bests don't overlap in leaf format. */
162 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
163 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
164 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
167 /* Check hash value order, count stale entries. */
168 for (i = stale = 0; i < hdr->count; i++) {
169 if (i + 1 < hdr->count) {
170 if (be32_to_cpu(ents[i].hashval) >
171 be32_to_cpu(ents[i + 1].hashval))
174 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
177 if (hdr->stale != stale)
183 xfs_dir3_leaf_verify(
187 struct xfs_mount *mp = bp->b_target->bt_mount;
188 struct xfs_dir2_leaf *leaf = bp->b_addr;
189 struct xfs_dir3_icleaf_hdr leafhdr;
191 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
193 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
194 if (xfs_sb_version_hascrc(&mp->m_sb)) {
195 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
197 if ((magic == XFS_DIR2_LEAF1_MAGIC &&
198 leafhdr.magic != XFS_DIR3_LEAF1_MAGIC) ||
199 (magic == XFS_DIR2_LEAFN_MAGIC &&
200 leafhdr.magic != XFS_DIR3_LEAFN_MAGIC))
203 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
205 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
208 if (leafhdr.magic != magic)
211 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
219 struct xfs_mount *mp = bp->b_target->bt_mount;
221 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
222 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
223 XFS_DIR3_LEAF_CRC_OFF)) ||
224 !xfs_dir3_leaf_verify(bp, magic)) {
225 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
226 xfs_buf_ioerror(bp, EFSCORRUPTED);
235 struct xfs_mount *mp = bp->b_target->bt_mount;
236 struct xfs_buf_log_item *bip = bp->b_fspriv;
237 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
239 if (!xfs_dir3_leaf_verify(bp, magic)) {
240 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
241 xfs_buf_ioerror(bp, EFSCORRUPTED);
245 if (!xfs_sb_version_hascrc(&mp->m_sb))
249 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
251 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
255 xfs_dir3_leaf1_read_verify(
258 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
262 xfs_dir3_leaf1_write_verify(
265 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
269 xfs_dir3_leafn_read_verify(
272 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
276 xfs_dir3_leafn_write_verify(
279 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
282 static const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
283 .verify_read = xfs_dir3_leaf1_read_verify,
284 .verify_write = xfs_dir3_leaf1_write_verify,
287 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
288 .verify_read = xfs_dir3_leafn_read_verify,
289 .verify_write = xfs_dir3_leafn_write_verify,
294 struct xfs_trans *tp,
295 struct xfs_inode *dp,
297 xfs_daddr_t mappedbno,
298 struct xfs_buf **bpp)
300 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
301 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
306 struct xfs_trans *tp,
307 struct xfs_inode *dp,
309 xfs_daddr_t mappedbno,
310 struct xfs_buf **bpp)
312 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
313 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
317 * Initialize a new leaf block, leaf1 or leafn magic accepted.
321 struct xfs_mount *mp,
326 struct xfs_dir2_leaf *leaf = bp->b_addr;
328 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
330 if (xfs_sb_version_hascrc(&mp->m_sb)) {
331 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
333 memset(leaf3, 0, sizeof(*leaf3));
335 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
336 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
337 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
338 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
339 leaf3->info.owner = cpu_to_be64(owner);
340 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
342 memset(leaf, 0, sizeof(*leaf));
343 leaf->hdr.info.magic = cpu_to_be16(type);
347 * If it's a leaf-format directory initialize the tail.
348 * Caller is responsible for initialising the bests table.
350 if (type == XFS_DIR2_LEAF1_MAGIC) {
351 struct xfs_dir2_leaf_tail *ltp;
353 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
355 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
357 bp->b_ops = &xfs_dir3_leafn_buf_ops;
361 xfs_dir3_leaf_get_buf(
364 struct xfs_buf **bpp,
367 struct xfs_inode *dp = args->dp;
368 struct xfs_trans *tp = args->trans;
369 struct xfs_mount *mp = dp->i_mount;
373 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
374 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
375 bno < XFS_DIR2_FREE_FIRSTDB(mp));
377 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
382 xfs_dir3_leaf_init(mp, bp, dp->i_ino, magic);
383 xfs_dir3_leaf_log_header(tp, bp);
384 if (magic == XFS_DIR2_LEAF1_MAGIC)
385 xfs_dir3_leaf_log_tail(tp, bp);
391 * Convert a block form directory to a leaf form directory.
394 xfs_dir2_block_to_leaf(
395 xfs_da_args_t *args, /* operation arguments */
396 struct xfs_buf *dbp) /* input block's buffer */
398 __be16 *bestsp; /* leaf's bestsp entries */
399 xfs_dablk_t blkno; /* leaf block's bno */
400 xfs_dir2_data_hdr_t *hdr; /* block header */
401 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
402 xfs_dir2_block_tail_t *btp; /* block's tail */
403 xfs_inode_t *dp; /* incore directory inode */
404 int error; /* error return code */
405 struct xfs_buf *lbp; /* leaf block's buffer */
406 xfs_dir2_db_t ldb; /* leaf block's bno */
407 xfs_dir2_leaf_t *leaf; /* leaf structure */
408 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
409 xfs_mount_t *mp; /* filesystem mount point */
410 int needlog; /* need to log block header */
411 int needscan; /* need to rescan bestfree */
412 xfs_trans_t *tp; /* transaction pointer */
413 struct xfs_dir2_data_free *bf;
414 struct xfs_dir2_leaf_entry *ents;
415 struct xfs_dir3_icleaf_hdr leafhdr;
417 trace_xfs_dir2_block_to_leaf(args);
423 * Add the leaf block to the inode.
424 * This interface will only put blocks in the leaf/node range.
425 * Since that's empty now, we'll get the root (block 0 in range).
427 if ((error = xfs_da_grow_inode(args, &blkno))) {
430 ldb = xfs_dir2_da_to_db(mp, blkno);
431 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
433 * Initialize the leaf block, get a buffer for it.
435 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
441 xfs_dir3_data_check(dp, dbp);
442 btp = xfs_dir2_block_tail_p(mp, hdr);
443 blp = xfs_dir2_block_leaf_p(btp);
444 bf = xfs_dir3_data_bestfree_p(hdr);
445 ents = xfs_dir3_leaf_ents_p(leaf);
448 * Set the counts in the leaf header.
450 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
451 leafhdr.count = be32_to_cpu(btp->count);
452 leafhdr.stale = be32_to_cpu(btp->stale);
453 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
454 xfs_dir3_leaf_log_header(tp, lbp);
457 * Could compact these but I think we always do the conversion
458 * after squeezing out stale entries.
460 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
461 xfs_dir3_leaf_log_ents(tp, lbp, 0, leafhdr.count - 1);
465 * Make the space formerly occupied by the leaf entries and block
468 xfs_dir2_data_make_free(tp, dbp,
469 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
470 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
472 &needlog, &needscan);
474 * Fix up the block header, make it a data block.
476 dbp->b_ops = &xfs_dir3_data_buf_ops;
477 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
478 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
480 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
483 xfs_dir2_data_freescan(mp, hdr, &needlog);
485 * Set up leaf tail and bests table.
487 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
488 ltp->bestcount = cpu_to_be32(1);
489 bestsp = xfs_dir2_leaf_bests_p(ltp);
490 bestsp[0] = bf[0].length;
492 * Log the data header and leaf bests table.
495 xfs_dir2_data_log_header(tp, dbp);
496 xfs_dir3_leaf_check(mp, lbp);
497 xfs_dir3_data_check(dp, dbp);
498 xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
503 xfs_dir3_leaf_find_stale(
504 struct xfs_dir3_icleaf_hdr *leafhdr,
505 struct xfs_dir2_leaf_entry *ents,
511 * Find the first stale entry before our index, if any.
513 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
514 if (ents[*lowstale].address ==
515 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
520 * Find the first stale entry at or after our index, if any.
521 * Stop if the result would require moving more entries than using
524 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
525 if (ents[*highstale].address ==
526 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
528 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
533 struct xfs_dir2_leaf_entry *
534 xfs_dir3_leaf_find_entry(
535 struct xfs_dir3_icleaf_hdr *leafhdr,
536 struct xfs_dir2_leaf_entry *ents,
537 int index, /* leaf table position */
538 int compact, /* need to compact leaves */
539 int lowstale, /* index of prev stale leaf */
540 int highstale, /* index of next stale leaf */
541 int *lfloglow, /* low leaf logging index */
542 int *lfloghigh) /* high leaf logging index */
544 if (!leafhdr->stale) {
545 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
548 * Now we need to make room to insert the leaf entry.
550 * If there are no stale entries, just insert a hole at index.
553 if (index < leafhdr->count)
554 memmove(lep + 1, lep,
555 (leafhdr->count - index) * sizeof(*lep));
558 * Record low and high logging indices for the leaf.
561 *lfloghigh = leafhdr->count++;
566 * There are stale entries.
568 * We will use one of them for the new entry. It's probably not at
569 * the right location, so we'll have to shift some up or down first.
571 * If we didn't compact before, we need to find the nearest stale
572 * entries before and after our insertion point.
575 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
576 &lowstale, &highstale);
579 * If the low one is better, use it.
582 (highstale == leafhdr->count ||
583 index - lowstale - 1 < highstale - index)) {
584 ASSERT(index - lowstale - 1 >= 0);
585 ASSERT(ents[lowstale].address ==
586 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
589 * Copy entries up to cover the stale entry and make room
592 if (index - lowstale - 1 > 0) {
593 memmove(&ents[lowstale], &ents[lowstale + 1],
594 (index - lowstale - 1) *
595 sizeof(xfs_dir2_leaf_entry_t));
597 *lfloglow = MIN(lowstale, *lfloglow);
598 *lfloghigh = MAX(index - 1, *lfloghigh);
600 return &ents[index - 1];
604 * The high one is better, so use that one.
606 ASSERT(highstale - index >= 0);
607 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
610 * Copy entries down to cover the stale entry and make room for the
613 if (highstale - index > 0) {
614 memmove(&ents[index + 1], &ents[index],
615 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
617 *lfloglow = MIN(index, *lfloglow);
618 *lfloghigh = MAX(highstale, *lfloghigh);
624 * Add an entry to a leaf form directory.
627 xfs_dir2_leaf_addname(
628 xfs_da_args_t *args) /* operation arguments */
630 __be16 *bestsp; /* freespace table in leaf */
631 int compact; /* need to compact leaves */
632 xfs_dir2_data_hdr_t *hdr; /* data block header */
633 struct xfs_buf *dbp; /* data block buffer */
634 xfs_dir2_data_entry_t *dep; /* data block entry */
635 xfs_inode_t *dp; /* incore directory inode */
636 xfs_dir2_data_unused_t *dup; /* data unused entry */
637 int error; /* error return value */
638 int grown; /* allocated new data block */
639 int highstale; /* index of next stale leaf */
640 int i; /* temporary, index */
641 int index; /* leaf table position */
642 struct xfs_buf *lbp; /* leaf's buffer */
643 xfs_dir2_leaf_t *leaf; /* leaf structure */
644 int length; /* length of new entry */
645 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
646 int lfloglow; /* low leaf logging index */
647 int lfloghigh; /* high leaf logging index */
648 int lowstale; /* index of prev stale leaf */
649 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
650 xfs_mount_t *mp; /* filesystem mount point */
651 int needbytes; /* leaf block bytes needed */
652 int needlog; /* need to log data header */
653 int needscan; /* need to rescan data free */
654 __be16 *tagp; /* end of data entry */
655 xfs_trans_t *tp; /* transaction pointer */
656 xfs_dir2_db_t use_block; /* data block number */
657 struct xfs_dir2_data_free *bf; /* bestfree table */
658 struct xfs_dir2_leaf_entry *ents;
659 struct xfs_dir3_icleaf_hdr leafhdr;
661 trace_xfs_dir2_leaf_addname(args);
667 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
672 * Look up the entry by hash value and name.
673 * We know it's not there, our caller has already done a lookup.
674 * So the index is of the entry to insert in front of.
675 * But if there are dup hash values the index is of the first of those.
677 index = xfs_dir2_leaf_search_hash(args, lbp);
679 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
680 ents = xfs_dir3_leaf_ents_p(leaf);
681 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
682 bestsp = xfs_dir2_leaf_bests_p(ltp);
683 length = xfs_dir2_data_entsize(args->namelen);
686 * See if there are any entries with the same hash value
687 * and space in their block for the new entry.
688 * This is good because it puts multiple same-hash value entries
689 * in a data block, improving the lookup of those entries.
691 for (use_block = -1, lep = &ents[index];
692 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
694 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
696 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
697 ASSERT(i < be32_to_cpu(ltp->bestcount));
698 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
699 if (be16_to_cpu(bestsp[i]) >= length) {
705 * Didn't find a block yet, linear search all the data blocks.
707 if (use_block == -1) {
708 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
710 * Remember a block we see that's missing.
712 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
715 else if (be16_to_cpu(bestsp[i]) >= length) {
722 * How many bytes do we need in the leaf block?
726 needbytes += sizeof(xfs_dir2_leaf_entry_t);
728 needbytes += sizeof(xfs_dir2_data_off_t);
731 * Now kill use_block if it refers to a missing block, so we
732 * can use it as an indication of allocation needed.
734 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
737 * If we don't have enough free bytes but we can make enough
738 * by compacting out stale entries, we'll do that.
740 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
745 * Otherwise if we don't have enough free bytes we need to
746 * convert to node form.
748 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
750 * Just checking or no space reservation, give up.
752 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
754 xfs_trans_brelse(tp, lbp);
755 return XFS_ERROR(ENOSPC);
758 * Convert to node form.
760 error = xfs_dir2_leaf_to_node(args, lbp);
764 * Then add the new entry.
766 return xfs_dir2_node_addname(args);
769 * Otherwise it will fit without compaction.
774 * If just checking, then it will fit unless we needed to allocate
777 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
778 xfs_trans_brelse(tp, lbp);
779 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
782 * If no allocations are allowed, return now before we've
785 if (args->total == 0 && use_block == -1) {
786 xfs_trans_brelse(tp, lbp);
787 return XFS_ERROR(ENOSPC);
790 * Need to compact the leaf entries, removing stale ones.
791 * Leave one stale entry behind - the one closest to our
792 * insertion index - and we'll shift that one to our insertion
796 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
797 &highstale, &lfloglow, &lfloghigh);
800 * There are stale entries, so we'll need log-low and log-high
801 * impossibly bad values later.
803 else if (leafhdr.stale) {
804 lfloglow = leafhdr.count;
808 * If there was no data block space found, we need to allocate
811 if (use_block == -1) {
813 * Add the new data block.
815 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
817 xfs_trans_brelse(tp, lbp);
821 * Initialize the block.
823 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
824 xfs_trans_brelse(tp, lbp);
828 * If we're adding a new data block on the end we need to
829 * extend the bests table. Copy it up one entry.
831 if (use_block >= be32_to_cpu(ltp->bestcount)) {
833 memmove(&bestsp[0], &bestsp[1],
834 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
835 be32_add_cpu(<p->bestcount, 1);
836 xfs_dir3_leaf_log_tail(tp, lbp);
837 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
840 * If we're filling in a previously empty block just log it.
843 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
845 bf = xfs_dir3_data_bestfree_p(hdr);
846 bestsp[use_block] = bf[0].length;
850 * Already had space in some data block.
851 * Just read that one in.
853 error = xfs_dir3_data_read(tp, dp,
854 xfs_dir2_db_to_da(mp, use_block),
857 xfs_trans_brelse(tp, lbp);
861 bf = xfs_dir3_data_bestfree_p(hdr);
865 * Point to the biggest freespace in our data block.
867 dup = (xfs_dir2_data_unused_t *)
868 ((char *)hdr + be16_to_cpu(bf[0].offset));
869 ASSERT(be16_to_cpu(dup->length) >= length);
870 needscan = needlog = 0;
872 * Mark the initial part of our freespace in use for the new entry.
874 xfs_dir2_data_use_free(tp, dbp, dup,
875 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
876 &needlog, &needscan);
878 * Initialize our new entry (at last).
880 dep = (xfs_dir2_data_entry_t *)dup;
881 dep->inumber = cpu_to_be64(args->inumber);
882 dep->namelen = args->namelen;
883 memcpy(dep->name, args->name, dep->namelen);
884 tagp = xfs_dir2_data_entry_tag_p(dep);
885 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
887 * Need to scan fix up the bestfree table.
890 xfs_dir2_data_freescan(mp, hdr, &needlog);
892 * Need to log the data block's header.
895 xfs_dir2_data_log_header(tp, dbp);
896 xfs_dir2_data_log_entry(tp, dbp, dep);
898 * If the bests table needs to be changed, do it.
899 * Log the change unless we've already done that.
901 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
902 bestsp[use_block] = bf[0].length;
904 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
907 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
908 highstale, &lfloglow, &lfloghigh);
911 * Fill in the new leaf entry.
913 lep->hashval = cpu_to_be32(args->hashval);
914 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
915 be16_to_cpu(*tagp)));
917 * Log the leaf fields and give up the buffers.
919 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
920 xfs_dir3_leaf_log_header(tp, lbp);
921 xfs_dir3_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
922 xfs_dir3_leaf_check(mp, lbp);
923 xfs_dir3_data_check(dp, dbp);
928 * Compact out any stale entries in the leaf.
929 * Log the header and changed leaf entries, if any.
932 xfs_dir3_leaf_compact(
933 xfs_da_args_t *args, /* operation arguments */
934 struct xfs_dir3_icleaf_hdr *leafhdr,
935 struct xfs_buf *bp) /* leaf buffer */
937 int from; /* source leaf index */
938 xfs_dir2_leaf_t *leaf; /* leaf structure */
939 int loglow; /* first leaf entry to log */
940 int to; /* target leaf index */
941 struct xfs_dir2_leaf_entry *ents;
948 * Compress out the stale entries in place.
950 ents = xfs_dir3_leaf_ents_p(leaf);
951 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
952 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
955 * Only actually copy the entries that are different.
960 ents[to] = ents[from];
965 * Update and log the header, log the leaf entries.
967 ASSERT(leafhdr->stale == from - to);
968 leafhdr->count -= leafhdr->stale;
971 xfs_dir3_leaf_hdr_to_disk(leaf, leafhdr);
972 xfs_dir3_leaf_log_header(args->trans, bp);
974 xfs_dir3_leaf_log_ents(args->trans, bp, loglow, to - 1);
978 * Compact the leaf entries, removing stale ones.
979 * Leave one stale entry behind - the one closest to our
980 * insertion index - and the caller will shift that one to our insertion
982 * Return new insertion index, where the remaining stale entry is,
983 * and leaf logging indices.
986 xfs_dir3_leaf_compact_x1(
987 struct xfs_dir3_icleaf_hdr *leafhdr,
988 struct xfs_dir2_leaf_entry *ents,
989 int *indexp, /* insertion index */
990 int *lowstalep, /* out: stale entry before us */
991 int *highstalep, /* out: stale entry after us */
992 int *lowlogp, /* out: low log index */
993 int *highlogp) /* out: high log index */
995 int from; /* source copy index */
996 int highstale; /* stale entry at/after index */
997 int index; /* insertion index */
998 int keepstale; /* source index of kept stale */
999 int lowstale; /* stale entry before index */
1000 int newindex=0; /* new insertion index */
1001 int to; /* destination copy index */
1003 ASSERT(leafhdr->stale > 1);
1006 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1009 * Pick the better of lowstale and highstale.
1011 if (lowstale >= 0 &&
1012 (highstale == leafhdr->count ||
1013 index - lowstale <= highstale - index))
1014 keepstale = lowstale;
1016 keepstale = highstale;
1018 * Copy the entries in place, removing all the stale entries
1021 for (from = to = 0; from < leafhdr->count; from++) {
1023 * Notice the new value of index.
1027 if (from != keepstale &&
1028 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1034 * Record the new keepstale value for the insertion.
1036 if (from == keepstale)
1037 lowstale = highstale = to;
1039 * Copy only the entries that have moved.
1042 ents[to] = ents[from];
1047 * If the insertion point was past the last entry,
1048 * set the new insertion point accordingly.
1054 * Adjust the leaf header values.
1056 leafhdr->count -= from - to;
1059 * Remember the low/high stale value only in the "right"
1062 if (lowstale >= newindex)
1065 highstale = leafhdr->count;
1066 *highlogp = leafhdr->count - 1;
1067 *lowstalep = lowstale;
1068 *highstalep = highstale;
1071 struct xfs_dir2_leaf_map_info {
1072 xfs_extlen_t map_blocks; /* number of fsbs in map */
1073 xfs_dablk_t map_off; /* last mapped file offset */
1074 int map_size; /* total entries in *map */
1075 int map_valid; /* valid entries in *map */
1076 int nmap; /* mappings to ask xfs_bmapi */
1077 xfs_dir2_db_t curdb; /* db for current block */
1078 int ra_current; /* number of read-ahead blks */
1079 int ra_index; /* *map index for read-ahead */
1080 int ra_offset; /* map entry offset for ra */
1081 int ra_want; /* readahead count wanted */
1082 struct xfs_bmbt_irec map[]; /* map vector for blocks */
1086 xfs_dir2_leaf_readbuf(
1087 struct xfs_inode *dp,
1089 struct xfs_dir2_leaf_map_info *mip,
1090 xfs_dir2_off_t *curoff,
1091 struct xfs_buf **bpp)
1093 struct xfs_mount *mp = dp->i_mount;
1094 struct xfs_buf *bp = *bpp;
1095 struct xfs_bmbt_irec *map = mip->map;
1102 * If we have a buffer, we need to release it and
1103 * take it out of the mapping.
1107 xfs_trans_brelse(NULL, bp);
1109 mip->map_blocks -= mp->m_dirblkfsbs;
1111 * Loop to get rid of the extents for the
1114 for (i = mp->m_dirblkfsbs; i > 0; ) {
1115 j = min_t(int, map->br_blockcount, i);
1116 map->br_blockcount -= j;
1117 map->br_startblock += j;
1118 map->br_startoff += j;
1120 * If mapping is done, pitch it from
1123 if (!map->br_blockcount && --mip->map_valid)
1124 memmove(&map[0], &map[1],
1125 sizeof(map[0]) * mip->map_valid);
1131 * Recalculate the readahead blocks wanted.
1133 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
1134 mp->m_sb.sb_blocksize) - 1;
1135 ASSERT(mip->ra_want >= 0);
1138 * If we don't have as many as we want, and we haven't
1139 * run out of data blocks, get some more mappings.
1141 if (1 + mip->ra_want > mip->map_blocks &&
1142 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
1144 * Get more bmaps, fill in after the ones
1145 * we already have in the table.
1147 mip->nmap = mip->map_size - mip->map_valid;
1148 error = xfs_bmapi_read(dp, mip->map_off,
1149 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
1151 &map[mip->map_valid], &mip->nmap, 0);
1154 * Don't know if we should ignore this or try to return an
1155 * error. The trouble with returning errors is that readdir
1156 * will just stop without actually passing the error through.
1162 * If we got all the mappings we asked for, set the final map
1163 * offset based on the last bmap value received. Otherwise,
1164 * we've reached the end.
1166 if (mip->nmap == mip->map_size - mip->map_valid) {
1167 i = mip->map_valid + mip->nmap - 1;
1168 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
1170 mip->map_off = xfs_dir2_byte_to_da(mp,
1171 XFS_DIR2_LEAF_OFFSET);
1174 * Look for holes in the mapping, and eliminate them. Count up
1177 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
1178 if (map[i].br_startblock == HOLESTARTBLOCK) {
1180 length = mip->map_valid + mip->nmap - i;
1182 memmove(&map[i], &map[i + 1],
1183 sizeof(map[i]) * length);
1185 mip->map_blocks += map[i].br_blockcount;
1189 mip->map_valid += mip->nmap;
1193 * No valid mappings, so no more data blocks.
1195 if (!mip->map_valid) {
1196 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
1201 * Read the directory block starting at the first mapping.
1203 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
1204 error = xfs_dir3_data_read(NULL, dp, map->br_startoff,
1205 map->br_blockcount >= mp->m_dirblkfsbs ?
1206 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
1209 * Should just skip over the data block instead of giving up.
1215 * Adjust the current amount of read-ahead: we just read a block that
1216 * was previously ra.
1218 if (mip->ra_current)
1219 mip->ra_current -= mp->m_dirblkfsbs;
1222 * Do we need more readahead?
1224 for (mip->ra_index = mip->ra_offset = i = 0;
1225 mip->ra_want > mip->ra_current && i < mip->map_blocks;
1226 i += mp->m_dirblkfsbs) {
1227 ASSERT(mip->ra_index < mip->map_valid);
1229 * Read-ahead a contiguous directory block.
1231 if (i > mip->ra_current &&
1232 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
1233 xfs_dir3_data_readahead(NULL, dp,
1234 map[mip->ra_index].br_startoff + mip->ra_offset,
1235 XFS_FSB_TO_DADDR(mp,
1236 map[mip->ra_index].br_startblock +
1238 mip->ra_current = i;
1242 * Read-ahead a non-contiguous directory block. This doesn't
1243 * use our mapping, but this is a very rare case.
1245 else if (i > mip->ra_current) {
1246 xfs_dir3_data_readahead(NULL, dp,
1247 map[mip->ra_index].br_startoff +
1248 mip->ra_offset, -1);
1249 mip->ra_current = i;
1253 * Advance offset through the mapping table.
1255 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1257 * The rest of this extent but not more than a dir
1260 length = min_t(int, mp->m_dirblkfsbs,
1261 map[mip->ra_index].br_blockcount -
1264 mip->ra_offset += length;
1267 * Advance to the next mapping if this one is used up.
1269 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
1282 * Getdents (readdir) for leaf and node directories.
1283 * This reads the data blocks only, so is the same for both forms.
1286 xfs_dir2_leaf_getdents(
1287 xfs_inode_t *dp, /* incore directory inode */
1293 struct xfs_buf *bp = NULL; /* data block buffer */
1294 xfs_dir2_data_hdr_t *hdr; /* data block header */
1295 xfs_dir2_data_entry_t *dep; /* data entry */
1296 xfs_dir2_data_unused_t *dup; /* unused entry */
1297 int error = 0; /* error return value */
1298 int length; /* temporary length value */
1299 xfs_mount_t *mp; /* filesystem mount point */
1300 int byteoff; /* offset in current block */
1301 xfs_dir2_off_t curoff; /* current overall offset */
1302 xfs_dir2_off_t newoff; /* new curoff after new blk */
1303 char *ptr = NULL; /* pointer to current data */
1304 struct xfs_dir2_leaf_map_info *map_info;
1307 * If the offset is at or past the largest allowed value,
1308 * give up right away.
1310 if (*offset >= XFS_DIR2_MAX_DATAPTR)
1316 * Set up to bmap a number of blocks based on the caller's
1317 * buffer size, the directory block size, and the filesystem
1320 length = howmany(bufsize + mp->m_dirblksize,
1321 mp->m_sb.sb_blocksize);
1322 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1323 (length * sizeof(struct xfs_bmbt_irec)),
1325 map_info->map_size = length;
1328 * Inside the loop we keep the main offset value as a byte offset
1329 * in the directory file.
1331 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1334 * Force this conversion through db so we truncate the offset
1335 * down to get the start of the data block.
1337 map_info->map_off = xfs_dir2_db_to_da(mp,
1338 xfs_dir2_byte_to_db(mp, curoff));
1341 * Loop over directory entries until we reach the end offset.
1342 * Get more blocks and readahead as necessary.
1344 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1346 * If we have no buffer, or we're off the end of the
1347 * current buffer, need to get another one.
1349 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
1351 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1353 if (error || !map_info->map_valid)
1357 * Having done a read, we need to set a new offset.
1359 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
1361 * Start of the current block.
1363 if (curoff < newoff)
1366 * Make sure we're in the right block.
1368 else if (curoff > newoff)
1369 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1372 xfs_dir3_data_check(dp, bp);
1374 * Find our position in the block.
1376 ptr = (char *)xfs_dir3_data_entry_p(hdr);
1377 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1379 * Skip past the header.
1382 curoff += xfs_dir3_data_entry_offset(hdr);
1384 * Skip past entries until we reach our offset.
1387 while ((char *)ptr - (char *)hdr < byteoff) {
1388 dup = (xfs_dir2_data_unused_t *)ptr;
1390 if (be16_to_cpu(dup->freetag)
1391 == XFS_DIR2_DATA_FREE_TAG) {
1393 length = be16_to_cpu(dup->length);
1397 dep = (xfs_dir2_data_entry_t *)ptr;
1399 xfs_dir2_data_entsize(dep->namelen);
1403 * Now set our real offset.
1406 xfs_dir2_db_off_to_byte(mp,
1407 xfs_dir2_byte_to_db(mp, curoff),
1408 (char *)ptr - (char *)hdr);
1409 if (ptr >= (char *)hdr + mp->m_dirblksize) {
1415 * We have a pointer to an entry.
1418 dup = (xfs_dir2_data_unused_t *)ptr;
1420 * No, it's unused, skip over it.
1422 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1423 length = be16_to_cpu(dup->length);
1429 dep = (xfs_dir2_data_entry_t *)ptr;
1430 length = xfs_dir2_data_entsize(dep->namelen);
1432 if (filldir(dirent, (char *)dep->name, dep->namelen,
1433 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
1434 be64_to_cpu(dep->inumber), DT_UNKNOWN))
1438 * Advance to next entry in the block.
1442 /* bufsize may have just been a guess; don't go negative */
1443 bufsize = bufsize > length ? bufsize - length : 0;
1447 * All done. Set output offset value to current offset.
1449 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
1450 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
1452 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
1453 kmem_free(map_info);
1455 xfs_trans_brelse(NULL, bp);
1461 * Log the bests entries indicated from a leaf1 block.
1464 xfs_dir3_leaf_log_bests(
1465 xfs_trans_t *tp, /* transaction pointer */
1466 struct xfs_buf *bp, /* leaf buffer */
1467 int first, /* first entry to log */
1468 int last) /* last entry to log */
1470 __be16 *firstb; /* pointer to first entry */
1471 __be16 *lastb; /* pointer to last entry */
1472 struct xfs_dir2_leaf *leaf = bp->b_addr;
1473 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1475 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1476 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1478 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1479 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1480 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1481 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1482 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1486 * Log the leaf entries indicated from a leaf1 or leafn block.
1489 xfs_dir3_leaf_log_ents(
1490 xfs_trans_t *tp, /* transaction pointer */
1491 struct xfs_buf *bp, /* leaf buffer */
1492 int first, /* first entry to log */
1493 int last) /* last entry to log */
1495 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1496 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1497 struct xfs_dir2_leaf *leaf = bp->b_addr;
1498 struct xfs_dir2_leaf_entry *ents;
1500 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1501 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1502 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1503 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1505 ents = xfs_dir3_leaf_ents_p(leaf);
1506 firstlep = &ents[first];
1507 lastlep = &ents[last];
1508 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1509 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1513 * Log the header of the leaf1 or leafn block.
1516 xfs_dir3_leaf_log_header(
1517 struct xfs_trans *tp,
1520 struct xfs_dir2_leaf *leaf = bp->b_addr;
1522 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1523 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1524 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1525 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1527 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1528 xfs_dir3_leaf_hdr_size(leaf) - 1);
1532 * Log the tail of the leaf1 block.
1535 xfs_dir3_leaf_log_tail(
1536 struct xfs_trans *tp,
1539 struct xfs_dir2_leaf *leaf = bp->b_addr;
1540 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1541 struct xfs_mount *mp = tp->t_mountp;
1543 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1544 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1545 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1546 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1548 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1549 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1550 (uint)(mp->m_dirblksize - 1));
1554 * Look up the entry referred to by args in the leaf format directory.
1555 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1556 * is also used by the node-format code.
1559 xfs_dir2_leaf_lookup(
1560 xfs_da_args_t *args) /* operation arguments */
1562 struct xfs_buf *dbp; /* data block buffer */
1563 xfs_dir2_data_entry_t *dep; /* data block entry */
1564 xfs_inode_t *dp; /* incore directory inode */
1565 int error; /* error return code */
1566 int index; /* found entry index */
1567 struct xfs_buf *lbp; /* leaf buffer */
1568 xfs_dir2_leaf_t *leaf; /* leaf structure */
1569 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1570 xfs_trans_t *tp; /* transaction pointer */
1571 struct xfs_dir2_leaf_entry *ents;
1573 trace_xfs_dir2_leaf_lookup(args);
1576 * Look up name in the leaf block, returning both buffers and index.
1578 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1583 xfs_dir3_leaf_check(dp->i_mount, lbp);
1585 ents = xfs_dir3_leaf_ents_p(leaf);
1587 * Get to the leaf entry and contained data entry address.
1592 * Point to the data entry.
1594 dep = (xfs_dir2_data_entry_t *)
1595 ((char *)dbp->b_addr +
1596 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1598 * Return the found inode number & CI name if appropriate
1600 args->inumber = be64_to_cpu(dep->inumber);
1601 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1602 xfs_trans_brelse(tp, dbp);
1603 xfs_trans_brelse(tp, lbp);
1604 return XFS_ERROR(error);
1608 * Look up name/hash in the leaf block.
1609 * Fill in indexp with the found index, and dbpp with the data buffer.
1610 * If not found dbpp will be NULL, and ENOENT comes back.
1611 * lbpp will always be filled in with the leaf buffer unless there's an error.
1613 static int /* error */
1614 xfs_dir2_leaf_lookup_int(
1615 xfs_da_args_t *args, /* operation arguments */
1616 struct xfs_buf **lbpp, /* out: leaf buffer */
1617 int *indexp, /* out: index in leaf block */
1618 struct xfs_buf **dbpp) /* out: data buffer */
1620 xfs_dir2_db_t curdb = -1; /* current data block number */
1621 struct xfs_buf *dbp = NULL; /* data buffer */
1622 xfs_dir2_data_entry_t *dep; /* data entry */
1623 xfs_inode_t *dp; /* incore directory inode */
1624 int error; /* error return code */
1625 int index; /* index in leaf block */
1626 struct xfs_buf *lbp; /* leaf buffer */
1627 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1628 xfs_dir2_leaf_t *leaf; /* leaf structure */
1629 xfs_mount_t *mp; /* filesystem mount point */
1630 xfs_dir2_db_t newdb; /* new data block number */
1631 xfs_trans_t *tp; /* transaction pointer */
1632 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1633 enum xfs_dacmp cmp; /* name compare result */
1634 struct xfs_dir2_leaf_entry *ents;
1635 struct xfs_dir3_icleaf_hdr leafhdr;
1641 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
1647 xfs_dir3_leaf_check(mp, lbp);
1648 ents = xfs_dir3_leaf_ents_p(leaf);
1649 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1652 * Look for the first leaf entry with our hash value.
1654 index = xfs_dir2_leaf_search_hash(args, lbp);
1656 * Loop over all the entries with the right hash value
1657 * looking to match the name.
1659 for (lep = &ents[index];
1660 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1663 * Skip over stale leaf entries.
1665 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1668 * Get the new data block number.
1670 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1672 * If it's not the same as the old data block number,
1673 * need to pitch the old one and read the new one.
1675 if (newdb != curdb) {
1677 xfs_trans_brelse(tp, dbp);
1678 error = xfs_dir3_data_read(tp, dp,
1679 xfs_dir2_db_to_da(mp, newdb),
1682 xfs_trans_brelse(tp, lbp);
1688 * Point to the data entry.
1690 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1691 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1693 * Compare name and if it's an exact match, return the index
1694 * and buffer. If it's the first case-insensitive match, store
1695 * the index and buffer and continue looking for an exact match.
1697 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1698 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1699 args->cmpresult = cmp;
1701 /* case exact match: return the current buffer. */
1702 if (cmp == XFS_CMP_EXACT) {
1709 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1711 * Here, we can only be doing a lookup (not a rename or remove).
1712 * If a case-insensitive match was found earlier, re-read the
1713 * appropriate data block if required and return it.
1715 if (args->cmpresult == XFS_CMP_CASE) {
1717 if (cidb != curdb) {
1718 xfs_trans_brelse(tp, dbp);
1719 error = xfs_dir3_data_read(tp, dp,
1720 xfs_dir2_db_to_da(mp, cidb),
1723 xfs_trans_brelse(tp, lbp);
1731 * No match found, return ENOENT.
1735 xfs_trans_brelse(tp, dbp);
1736 xfs_trans_brelse(tp, lbp);
1737 return XFS_ERROR(ENOENT);
1741 * Remove an entry from a leaf format directory.
1744 xfs_dir2_leaf_removename(
1745 xfs_da_args_t *args) /* operation arguments */
1747 __be16 *bestsp; /* leaf block best freespace */
1748 xfs_dir2_data_hdr_t *hdr; /* data block header */
1749 xfs_dir2_db_t db; /* data block number */
1750 struct xfs_buf *dbp; /* data block buffer */
1751 xfs_dir2_data_entry_t *dep; /* data entry structure */
1752 xfs_inode_t *dp; /* incore directory inode */
1753 int error; /* error return code */
1754 xfs_dir2_db_t i; /* temporary data block # */
1755 int index; /* index into leaf entries */
1756 struct xfs_buf *lbp; /* leaf buffer */
1757 xfs_dir2_leaf_t *leaf; /* leaf structure */
1758 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1759 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1760 xfs_mount_t *mp; /* filesystem mount point */
1761 int needlog; /* need to log data header */
1762 int needscan; /* need to rescan data frees */
1763 xfs_dir2_data_off_t oldbest; /* old value of best free */
1764 xfs_trans_t *tp; /* transaction pointer */
1765 struct xfs_dir2_data_free *bf; /* bestfree table */
1766 struct xfs_dir2_leaf_entry *ents;
1767 struct xfs_dir3_icleaf_hdr leafhdr;
1769 trace_xfs_dir2_leaf_removename(args);
1772 * Lookup the leaf entry, get the leaf and data blocks read in.
1774 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1782 xfs_dir3_data_check(dp, dbp);
1783 bf = xfs_dir3_data_bestfree_p(hdr);
1784 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1785 ents = xfs_dir3_leaf_ents_p(leaf);
1787 * Point to the leaf entry, use that to point to the data entry.
1790 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1791 dep = (xfs_dir2_data_entry_t *)
1792 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1793 needscan = needlog = 0;
1794 oldbest = be16_to_cpu(bf[0].length);
1795 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1796 bestsp = xfs_dir2_leaf_bests_p(ltp);
1797 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1799 * Mark the former data entry unused.
1801 xfs_dir2_data_make_free(tp, dbp,
1802 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1803 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1805 * We just mark the leaf entry stale by putting a null in it.
1808 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1809 xfs_dir3_leaf_log_header(tp, lbp);
1811 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1812 xfs_dir3_leaf_log_ents(tp, lbp, index, index);
1815 * Scan the freespace in the data block again if necessary,
1816 * log the data block header if necessary.
1819 xfs_dir2_data_freescan(mp, hdr, &needlog);
1821 xfs_dir2_data_log_header(tp, dbp);
1823 * If the longest freespace in the data block has changed,
1824 * put the new value in the bests table and log that.
1826 if (be16_to_cpu(bf[0].length) != oldbest) {
1827 bestsp[db] = bf[0].length;
1828 xfs_dir3_leaf_log_bests(tp, lbp, db, db);
1830 xfs_dir3_data_check(dp, dbp);
1832 * If the data block is now empty then get rid of the data block.
1834 if (be16_to_cpu(bf[0].length) ==
1835 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
1836 ASSERT(db != mp->m_dirdatablk);
1837 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1839 * Nope, can't get rid of it because it caused
1840 * allocation of a bmap btree block to do so.
1841 * Just go on, returning success, leaving the
1842 * empty block in place.
1844 if (error == ENOSPC && args->total == 0)
1846 xfs_dir3_leaf_check(mp, lbp);
1851 * If this is the last data block then compact the
1852 * bests table by getting rid of entries.
1854 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1856 * Look for the last active entry (i).
1858 for (i = db - 1; i > 0; i--) {
1859 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1863 * Copy the table down so inactive entries at the
1866 memmove(&bestsp[db - i], bestsp,
1867 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1868 be32_add_cpu(<p->bestcount, -(db - i));
1869 xfs_dir3_leaf_log_tail(tp, lbp);
1870 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1872 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1875 * If the data block was not the first one, drop it.
1877 else if (db != mp->m_dirdatablk)
1880 xfs_dir3_leaf_check(mp, lbp);
1882 * See if we can convert to block form.
1884 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1888 * Replace the inode number in a leaf format directory entry.
1891 xfs_dir2_leaf_replace(
1892 xfs_da_args_t *args) /* operation arguments */
1894 struct xfs_buf *dbp; /* data block buffer */
1895 xfs_dir2_data_entry_t *dep; /* data block entry */
1896 xfs_inode_t *dp; /* incore directory inode */
1897 int error; /* error return code */
1898 int index; /* index of leaf entry */
1899 struct xfs_buf *lbp; /* leaf buffer */
1900 xfs_dir2_leaf_t *leaf; /* leaf structure */
1901 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1902 xfs_trans_t *tp; /* transaction pointer */
1903 struct xfs_dir2_leaf_entry *ents;
1905 trace_xfs_dir2_leaf_replace(args);
1908 * Look up the entry.
1910 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1915 ents = xfs_dir3_leaf_ents_p(leaf);
1917 * Point to the leaf entry, get data address from it.
1921 * Point to the data entry.
1923 dep = (xfs_dir2_data_entry_t *)
1924 ((char *)dbp->b_addr +
1925 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1926 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1928 * Put the new inode number in, log it.
1930 dep->inumber = cpu_to_be64(args->inumber);
1932 xfs_dir2_data_log_entry(tp, dbp, dep);
1933 xfs_dir3_leaf_check(dp->i_mount, lbp);
1934 xfs_trans_brelse(tp, lbp);
1939 * Return index in the leaf block (lbp) which is either the first
1940 * one with this hash value, or if there are none, the insert point
1941 * for that hash value.
1943 int /* index value */
1944 xfs_dir2_leaf_search_hash(
1945 xfs_da_args_t *args, /* operation arguments */
1946 struct xfs_buf *lbp) /* leaf buffer */
1948 xfs_dahash_t hash=0; /* hash from this entry */
1949 xfs_dahash_t hashwant; /* hash value looking for */
1950 int high; /* high leaf index */
1951 int low; /* low leaf index */
1952 xfs_dir2_leaf_t *leaf; /* leaf structure */
1953 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1954 int mid=0; /* current leaf index */
1955 struct xfs_dir2_leaf_entry *ents;
1956 struct xfs_dir3_icleaf_hdr leafhdr;
1959 ents = xfs_dir3_leaf_ents_p(leaf);
1960 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1967 * Note, the table cannot be empty, so we have to go through the loop.
1968 * Binary search the leaf entries looking for our hash value.
1970 for (lep = ents, low = 0, high = leafhdr.count - 1,
1971 hashwant = args->hashval;
1973 mid = (low + high) >> 1;
1974 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1976 if (hash < hashwant)
1982 * Found one, back up through all the equal hash values.
1984 if (hash == hashwant) {
1985 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1990 * Need to point to an entry higher than ours.
1992 else if (hash < hashwant)
1998 * Trim off a trailing data block. We know it's empty since the leaf
1999 * freespace table says so.
2002 xfs_dir2_leaf_trim_data(
2003 xfs_da_args_t *args, /* operation arguments */
2004 struct xfs_buf *lbp, /* leaf buffer */
2005 xfs_dir2_db_t db) /* data block number */
2007 __be16 *bestsp; /* leaf bests table */
2008 struct xfs_buf *dbp; /* data block buffer */
2009 xfs_inode_t *dp; /* incore directory inode */
2010 int error; /* error return value */
2011 xfs_dir2_leaf_t *leaf; /* leaf structure */
2012 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
2013 xfs_mount_t *mp; /* filesystem mount point */
2014 xfs_trans_t *tp; /* transaction pointer */
2020 * Read the offending data block. We need its buffer.
2022 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
2027 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
2031 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
2032 struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
2034 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2035 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2036 ASSERT(be16_to_cpu(bf[0].length) ==
2037 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
2038 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
2043 * Get rid of the data block.
2045 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
2046 ASSERT(error != ENOSPC);
2047 xfs_trans_brelse(tp, dbp);
2051 * Eliminate the last bests entry from the table.
2053 bestsp = xfs_dir2_leaf_bests_p(ltp);
2054 be32_add_cpu(<p->bestcount, -1);
2055 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
2056 xfs_dir3_leaf_log_tail(tp, lbp);
2057 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2061 static inline size_t
2063 struct xfs_dir3_icleaf_hdr *hdr,
2069 entries = hdr->count - hdr->stale;
2070 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
2071 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
2072 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
2074 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
2076 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
2077 + counts * sizeof(xfs_dir2_data_off_t)
2078 + sizeof(xfs_dir2_leaf_tail_t);
2082 * Convert node form directory to leaf form directory.
2083 * The root of the node form dir needs to already be a LEAFN block.
2084 * Just return if we can't do anything.
2087 xfs_dir2_node_to_leaf(
2088 xfs_da_state_t *state) /* directory operation state */
2090 xfs_da_args_t *args; /* operation arguments */
2091 xfs_inode_t *dp; /* incore directory inode */
2092 int error; /* error return code */
2093 struct xfs_buf *fbp; /* buffer for freespace block */
2094 xfs_fileoff_t fo; /* freespace file offset */
2095 xfs_dir2_free_t *free; /* freespace structure */
2096 struct xfs_buf *lbp; /* buffer for leaf block */
2097 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
2098 xfs_dir2_leaf_t *leaf; /* leaf structure */
2099 xfs_mount_t *mp; /* filesystem mount point */
2100 int rval; /* successful free trim? */
2101 xfs_trans_t *tp; /* transaction pointer */
2102 struct xfs_dir3_icleaf_hdr leafhdr;
2103 struct xfs_dir3_icfree_hdr freehdr;
2106 * There's more than a leaf level in the btree, so there must
2107 * be multiple leafn blocks. Give up.
2109 if (state->path.active > 1)
2113 trace_xfs_dir2_node_to_leaf(args);
2119 * Get the last offset in the file.
2121 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
2124 fo -= mp->m_dirblkfsbs;
2126 * If there are freespace blocks other than the first one,
2127 * take this opportunity to remove trailing empty freespace blocks
2128 * that may have been left behind during no-space-reservation
2131 while (fo > mp->m_dirfreeblk) {
2132 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
2136 fo -= mp->m_dirblkfsbs;
2141 * Now find the block just before the freespace block.
2143 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
2147 * If it's not the single leaf block, give up.
2149 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
2151 lbp = state->path.blk[0].bp;
2153 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
2155 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
2156 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
2159 * Read the freespace block.
2161 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
2165 xfs_dir3_free_hdr_from_disk(&freehdr, free);
2167 ASSERT(!freehdr.firstdb);
2170 * Now see if the leafn and free data will fit in a leaf1.
2171 * If not, release the buffer and give up.
2173 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
2174 xfs_trans_brelse(tp, fbp);
2179 * If the leaf has any stale entries in it, compress them out.
2182 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
2184 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
2185 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
2186 ? XFS_DIR2_LEAF1_MAGIC
2187 : XFS_DIR3_LEAF1_MAGIC;
2190 * Set up the leaf tail from the freespace block.
2192 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
2193 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
2196 * Set up the leaf bests table.
2198 memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
2199 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
2201 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
2202 xfs_dir3_leaf_log_header(tp, lbp);
2203 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2204 xfs_dir3_leaf_log_tail(tp, lbp);
2205 xfs_dir3_leaf_check(mp, lbp);
2208 * Get rid of the freespace block.
2210 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
2213 * This can't fail here because it can only happen when
2214 * punching out the middle of an extent, and this is an
2217 ASSERT(error != ENOSPC);
2222 * Now see if we can convert the single-leaf directory
2223 * down to a block form directory.
2224 * This routine always kills the dabuf for the leaf, so
2225 * eliminate it from the path.
2227 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
2228 state->path.blk[0].bp = NULL;