Linux 3.12.28
[linux/fpc-iii.git] / fs / xfs / xfs_dir2_leaf.c
blob1021c8356d0836318e300adefc4a35f170d120c3
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
4 * All Rights Reserved.
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
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_types.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.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"
32 #include "xfs_bmap.h"
33 #include "xfs_dir2_format.h"
34 #include "xfs_dir2.h"
35 #include "xfs_dir2_priv.h"
36 #include "xfs_error.h"
37 #include "xfs_trace.h"
38 #include "xfs_buf_item.h"
39 #include "xfs_cksum.h"
42 * Local function declarations.
44 static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
45 int *indexp, struct xfs_buf **dbpp);
46 static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
47 int first, int last);
48 static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
51 * Check the internal consistency of a leaf1 block.
52 * Pop an assert if something is wrong.
54 #ifdef DEBUG
55 #define xfs_dir3_leaf_check(mp, bp) \
56 do { \
57 if (!xfs_dir3_leaf1_check((mp), (bp))) \
58 ASSERT(0); \
59 } while (0);
61 STATIC bool
62 xfs_dir3_leaf1_check(
63 struct xfs_mount *mp,
64 struct xfs_buf *bp)
66 struct xfs_dir2_leaf *leaf = bp->b_addr;
67 struct xfs_dir3_icleaf_hdr leafhdr;
69 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
71 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
72 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
73 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
74 return false;
75 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
76 return false;
78 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
80 #else
81 #define xfs_dir3_leaf_check(mp, bp)
82 #endif
84 void
85 xfs_dir3_leaf_hdr_from_disk(
86 struct xfs_dir3_icleaf_hdr *to,
87 struct xfs_dir2_leaf *from)
89 if (from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
90 from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
91 to->forw = be32_to_cpu(from->hdr.info.forw);
92 to->back = be32_to_cpu(from->hdr.info.back);
93 to->magic = be16_to_cpu(from->hdr.info.magic);
94 to->count = be16_to_cpu(from->hdr.count);
95 to->stale = be16_to_cpu(from->hdr.stale);
96 } else {
97 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
99 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
100 to->back = be32_to_cpu(hdr3->info.hdr.back);
101 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
102 to->count = be16_to_cpu(hdr3->count);
103 to->stale = be16_to_cpu(hdr3->stale);
106 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
107 to->magic == XFS_DIR3_LEAF1_MAGIC ||
108 to->magic == XFS_DIR2_LEAFN_MAGIC ||
109 to->magic == XFS_DIR3_LEAFN_MAGIC);
112 void
113 xfs_dir3_leaf_hdr_to_disk(
114 struct xfs_dir2_leaf *to,
115 struct xfs_dir3_icleaf_hdr *from)
117 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
118 from->magic == XFS_DIR3_LEAF1_MAGIC ||
119 from->magic == XFS_DIR2_LEAFN_MAGIC ||
120 from->magic == XFS_DIR3_LEAFN_MAGIC);
122 if (from->magic == XFS_DIR2_LEAF1_MAGIC ||
123 from->magic == XFS_DIR2_LEAFN_MAGIC) {
124 to->hdr.info.forw = cpu_to_be32(from->forw);
125 to->hdr.info.back = cpu_to_be32(from->back);
126 to->hdr.info.magic = cpu_to_be16(from->magic);
127 to->hdr.count = cpu_to_be16(from->count);
128 to->hdr.stale = cpu_to_be16(from->stale);
129 } else {
130 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
132 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
133 hdr3->info.hdr.back = cpu_to_be32(from->back);
134 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
135 hdr3->count = cpu_to_be16(from->count);
136 hdr3->stale = cpu_to_be16(from->stale);
140 bool
141 xfs_dir3_leaf_check_int(
142 struct xfs_mount *mp,
143 struct xfs_dir3_icleaf_hdr *hdr,
144 struct xfs_dir2_leaf *leaf)
146 struct xfs_dir2_leaf_entry *ents;
147 xfs_dir2_leaf_tail_t *ltp;
148 int stale;
149 int i;
151 ents = xfs_dir3_leaf_ents_p(leaf);
152 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
155 * XXX (dgc): This value is not restrictive enough.
156 * Should factor in the size of the bests table as well.
157 * We can deduce a value for that from di_size.
159 if (hdr->count > xfs_dir3_max_leaf_ents(mp, leaf))
160 return false;
162 /* Leaves and bests don't overlap in leaf format. */
163 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
164 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
165 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
166 return false;
168 /* Check hash value order, count stale entries. */
169 for (i = stale = 0; i < hdr->count; i++) {
170 if (i + 1 < hdr->count) {
171 if (be32_to_cpu(ents[i].hashval) >
172 be32_to_cpu(ents[i + 1].hashval))
173 return false;
175 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
176 stale++;
178 if (hdr->stale != stale)
179 return false;
180 return true;
184 * We verify the magic numbers before decoding the leaf header so that on debug
185 * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
186 * to incorrect magic numbers.
188 static bool
189 xfs_dir3_leaf_verify(
190 struct xfs_buf *bp,
191 __uint16_t magic)
193 struct xfs_mount *mp = bp->b_target->bt_mount;
194 struct xfs_dir2_leaf *leaf = bp->b_addr;
195 struct xfs_dir3_icleaf_hdr leafhdr;
197 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
199 if (xfs_sb_version_hascrc(&mp->m_sb)) {
200 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
201 __uint16_t magic3;
203 magic3 = (magic == XFS_DIR2_LEAF1_MAGIC) ? XFS_DIR3_LEAF1_MAGIC
204 : XFS_DIR3_LEAFN_MAGIC;
206 if (leaf3->info.hdr.magic != cpu_to_be16(magic3))
207 return false;
208 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
209 return false;
210 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
211 return false;
212 } else {
213 if (leaf->hdr.info.magic != cpu_to_be16(magic))
214 return false;
217 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
218 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
221 static void
222 __read_verify(
223 struct xfs_buf *bp,
224 __uint16_t magic)
226 struct xfs_mount *mp = bp->b_target->bt_mount;
228 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
229 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
230 XFS_DIR3_LEAF_CRC_OFF)) ||
231 !xfs_dir3_leaf_verify(bp, magic)) {
232 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
233 xfs_buf_ioerror(bp, EFSCORRUPTED);
237 static void
238 __write_verify(
239 struct xfs_buf *bp,
240 __uint16_t magic)
242 struct xfs_mount *mp = bp->b_target->bt_mount;
243 struct xfs_buf_log_item *bip = bp->b_fspriv;
244 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
246 if (!xfs_dir3_leaf_verify(bp, magic)) {
247 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
248 xfs_buf_ioerror(bp, EFSCORRUPTED);
249 return;
252 if (!xfs_sb_version_hascrc(&mp->m_sb))
253 return;
255 if (bip)
256 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
258 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
261 static void
262 xfs_dir3_leaf1_read_verify(
263 struct xfs_buf *bp)
265 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
268 static void
269 xfs_dir3_leaf1_write_verify(
270 struct xfs_buf *bp)
272 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
275 static void
276 xfs_dir3_leafn_read_verify(
277 struct xfs_buf *bp)
279 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
282 static void
283 xfs_dir3_leafn_write_verify(
284 struct xfs_buf *bp)
286 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
289 const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
290 .verify_read = xfs_dir3_leaf1_read_verify,
291 .verify_write = xfs_dir3_leaf1_write_verify,
294 const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
295 .verify_read = xfs_dir3_leafn_read_verify,
296 .verify_write = xfs_dir3_leafn_write_verify,
299 static int
300 xfs_dir3_leaf_read(
301 struct xfs_trans *tp,
302 struct xfs_inode *dp,
303 xfs_dablk_t fbno,
304 xfs_daddr_t mappedbno,
305 struct xfs_buf **bpp)
307 int err;
309 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
310 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
311 if (!err && tp)
312 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
313 return err;
317 xfs_dir3_leafn_read(
318 struct xfs_trans *tp,
319 struct xfs_inode *dp,
320 xfs_dablk_t fbno,
321 xfs_daddr_t mappedbno,
322 struct xfs_buf **bpp)
324 int err;
326 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
327 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
328 if (!err && tp)
329 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
330 return err;
334 * Initialize a new leaf block, leaf1 or leafn magic accepted.
336 static void
337 xfs_dir3_leaf_init(
338 struct xfs_mount *mp,
339 struct xfs_trans *tp,
340 struct xfs_buf *bp,
341 xfs_ino_t owner,
342 __uint16_t type)
344 struct xfs_dir2_leaf *leaf = bp->b_addr;
346 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
348 if (xfs_sb_version_hascrc(&mp->m_sb)) {
349 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
351 memset(leaf3, 0, sizeof(*leaf3));
353 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
354 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
355 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
356 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
357 leaf3->info.owner = cpu_to_be64(owner);
358 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
359 } else {
360 memset(leaf, 0, sizeof(*leaf));
361 leaf->hdr.info.magic = cpu_to_be16(type);
365 * If it's a leaf-format directory initialize the tail.
366 * Caller is responsible for initialising the bests table.
368 if (type == XFS_DIR2_LEAF1_MAGIC) {
369 struct xfs_dir2_leaf_tail *ltp;
371 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
372 ltp->bestcount = 0;
373 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
374 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
375 } else {
376 bp->b_ops = &xfs_dir3_leafn_buf_ops;
377 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
382 xfs_dir3_leaf_get_buf(
383 xfs_da_args_t *args,
384 xfs_dir2_db_t bno,
385 struct xfs_buf **bpp,
386 __uint16_t magic)
388 struct xfs_inode *dp = args->dp;
389 struct xfs_trans *tp = args->trans;
390 struct xfs_mount *mp = dp->i_mount;
391 struct xfs_buf *bp;
392 int error;
394 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
395 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
396 bno < XFS_DIR2_FREE_FIRSTDB(mp));
398 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
399 XFS_DATA_FORK);
400 if (error)
401 return error;
403 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
404 xfs_dir3_leaf_log_header(tp, bp);
405 if (magic == XFS_DIR2_LEAF1_MAGIC)
406 xfs_dir3_leaf_log_tail(tp, bp);
407 *bpp = bp;
408 return 0;
412 * Convert a block form directory to a leaf form directory.
414 int /* error */
415 xfs_dir2_block_to_leaf(
416 xfs_da_args_t *args, /* operation arguments */
417 struct xfs_buf *dbp) /* input block's buffer */
419 __be16 *bestsp; /* leaf's bestsp entries */
420 xfs_dablk_t blkno; /* leaf block's bno */
421 xfs_dir2_data_hdr_t *hdr; /* block header */
422 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
423 xfs_dir2_block_tail_t *btp; /* block's tail */
424 xfs_inode_t *dp; /* incore directory inode */
425 int error; /* error return code */
426 struct xfs_buf *lbp; /* leaf block's buffer */
427 xfs_dir2_db_t ldb; /* leaf block's bno */
428 xfs_dir2_leaf_t *leaf; /* leaf structure */
429 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
430 xfs_mount_t *mp; /* filesystem mount point */
431 int needlog; /* need to log block header */
432 int needscan; /* need to rescan bestfree */
433 xfs_trans_t *tp; /* transaction pointer */
434 struct xfs_dir2_data_free *bf;
435 struct xfs_dir2_leaf_entry *ents;
436 struct xfs_dir3_icleaf_hdr leafhdr;
438 trace_xfs_dir2_block_to_leaf(args);
440 dp = args->dp;
441 mp = dp->i_mount;
442 tp = args->trans;
444 * Add the leaf block to the inode.
445 * This interface will only put blocks in the leaf/node range.
446 * Since that's empty now, we'll get the root (block 0 in range).
448 if ((error = xfs_da_grow_inode(args, &blkno))) {
449 return error;
451 ldb = xfs_dir2_da_to_db(mp, blkno);
452 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
454 * Initialize the leaf block, get a buffer for it.
456 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
457 if (error)
458 return error;
460 leaf = lbp->b_addr;
461 hdr = dbp->b_addr;
462 xfs_dir3_data_check(dp, dbp);
463 btp = xfs_dir2_block_tail_p(mp, hdr);
464 blp = xfs_dir2_block_leaf_p(btp);
465 bf = xfs_dir3_data_bestfree_p(hdr);
466 ents = xfs_dir3_leaf_ents_p(leaf);
469 * Set the counts in the leaf header.
471 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
472 leafhdr.count = be32_to_cpu(btp->count);
473 leafhdr.stale = be32_to_cpu(btp->stale);
474 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
475 xfs_dir3_leaf_log_header(tp, lbp);
478 * Could compact these but I think we always do the conversion
479 * after squeezing out stale entries.
481 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
482 xfs_dir3_leaf_log_ents(tp, lbp, 0, leafhdr.count - 1);
483 needscan = 0;
484 needlog = 1;
486 * Make the space formerly occupied by the leaf entries and block
487 * tail be free.
489 xfs_dir2_data_make_free(tp, dbp,
490 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
491 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
492 (char *)blp),
493 &needlog, &needscan);
495 * Fix up the block header, make it a data block.
497 dbp->b_ops = &xfs_dir3_data_buf_ops;
498 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
499 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
500 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
501 else
502 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
504 if (needscan)
505 xfs_dir2_data_freescan(mp, hdr, &needlog);
507 * Set up leaf tail and bests table.
509 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
510 ltp->bestcount = cpu_to_be32(1);
511 bestsp = xfs_dir2_leaf_bests_p(ltp);
512 bestsp[0] = bf[0].length;
514 * Log the data header and leaf bests table.
516 if (needlog)
517 xfs_dir2_data_log_header(tp, dbp);
518 xfs_dir3_leaf_check(mp, lbp);
519 xfs_dir3_data_check(dp, dbp);
520 xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
521 return 0;
524 STATIC void
525 xfs_dir3_leaf_find_stale(
526 struct xfs_dir3_icleaf_hdr *leafhdr,
527 struct xfs_dir2_leaf_entry *ents,
528 int index,
529 int *lowstale,
530 int *highstale)
533 * Find the first stale entry before our index, if any.
535 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
536 if (ents[*lowstale].address ==
537 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
538 break;
542 * Find the first stale entry at or after our index, if any.
543 * Stop if the result would require moving more entries than using
544 * lowstale.
546 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
547 if (ents[*highstale].address ==
548 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
549 break;
550 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
551 break;
555 struct xfs_dir2_leaf_entry *
556 xfs_dir3_leaf_find_entry(
557 struct xfs_dir3_icleaf_hdr *leafhdr,
558 struct xfs_dir2_leaf_entry *ents,
559 int index, /* leaf table position */
560 int compact, /* need to compact leaves */
561 int lowstale, /* index of prev stale leaf */
562 int highstale, /* index of next stale leaf */
563 int *lfloglow, /* low leaf logging index */
564 int *lfloghigh) /* high leaf logging index */
566 if (!leafhdr->stale) {
567 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
570 * Now we need to make room to insert the leaf entry.
572 * If there are no stale entries, just insert a hole at index.
574 lep = &ents[index];
575 if (index < leafhdr->count)
576 memmove(lep + 1, lep,
577 (leafhdr->count - index) * sizeof(*lep));
580 * Record low and high logging indices for the leaf.
582 *lfloglow = index;
583 *lfloghigh = leafhdr->count++;
584 return lep;
588 * There are stale entries.
590 * We will use one of them for the new entry. It's probably not at
591 * the right location, so we'll have to shift some up or down first.
593 * If we didn't compact before, we need to find the nearest stale
594 * entries before and after our insertion point.
596 if (compact == 0)
597 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
598 &lowstale, &highstale);
601 * If the low one is better, use it.
603 if (lowstale >= 0 &&
604 (highstale == leafhdr->count ||
605 index - lowstale - 1 < highstale - index)) {
606 ASSERT(index - lowstale - 1 >= 0);
607 ASSERT(ents[lowstale].address ==
608 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
611 * Copy entries up to cover the stale entry and make room
612 * for the new entry.
614 if (index - lowstale - 1 > 0) {
615 memmove(&ents[lowstale], &ents[lowstale + 1],
616 (index - lowstale - 1) *
617 sizeof(xfs_dir2_leaf_entry_t));
619 *lfloglow = MIN(lowstale, *lfloglow);
620 *lfloghigh = MAX(index - 1, *lfloghigh);
621 leafhdr->stale--;
622 return &ents[index - 1];
626 * The high one is better, so use that one.
628 ASSERT(highstale - index >= 0);
629 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
632 * Copy entries down to cover the stale entry and make room for the
633 * new entry.
635 if (highstale - index > 0) {
636 memmove(&ents[index + 1], &ents[index],
637 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
639 *lfloglow = MIN(index, *lfloglow);
640 *lfloghigh = MAX(highstale, *lfloghigh);
641 leafhdr->stale--;
642 return &ents[index];
646 * Add an entry to a leaf form directory.
648 int /* error */
649 xfs_dir2_leaf_addname(
650 xfs_da_args_t *args) /* operation arguments */
652 __be16 *bestsp; /* freespace table in leaf */
653 int compact; /* need to compact leaves */
654 xfs_dir2_data_hdr_t *hdr; /* data block header */
655 struct xfs_buf *dbp; /* data block buffer */
656 xfs_dir2_data_entry_t *dep; /* data block entry */
657 xfs_inode_t *dp; /* incore directory inode */
658 xfs_dir2_data_unused_t *dup; /* data unused entry */
659 int error; /* error return value */
660 int grown; /* allocated new data block */
661 int highstale; /* index of next stale leaf */
662 int i; /* temporary, index */
663 int index; /* leaf table position */
664 struct xfs_buf *lbp; /* leaf's buffer */
665 xfs_dir2_leaf_t *leaf; /* leaf structure */
666 int length; /* length of new entry */
667 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
668 int lfloglow; /* low leaf logging index */
669 int lfloghigh; /* high leaf logging index */
670 int lowstale; /* index of prev stale leaf */
671 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
672 xfs_mount_t *mp; /* filesystem mount point */
673 int needbytes; /* leaf block bytes needed */
674 int needlog; /* need to log data header */
675 int needscan; /* need to rescan data free */
676 __be16 *tagp; /* end of data entry */
677 xfs_trans_t *tp; /* transaction pointer */
678 xfs_dir2_db_t use_block; /* data block number */
679 struct xfs_dir2_data_free *bf; /* bestfree table */
680 struct xfs_dir2_leaf_entry *ents;
681 struct xfs_dir3_icleaf_hdr leafhdr;
683 trace_xfs_dir2_leaf_addname(args);
685 dp = args->dp;
686 tp = args->trans;
687 mp = dp->i_mount;
689 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
690 if (error)
691 return error;
694 * Look up the entry by hash value and name.
695 * We know it's not there, our caller has already done a lookup.
696 * So the index is of the entry to insert in front of.
697 * But if there are dup hash values the index is of the first of those.
699 index = xfs_dir2_leaf_search_hash(args, lbp);
700 leaf = lbp->b_addr;
701 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
702 ents = xfs_dir3_leaf_ents_p(leaf);
703 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
704 bestsp = xfs_dir2_leaf_bests_p(ltp);
705 length = xfs_dir3_data_entsize(mp, args->namelen);
708 * See if there are any entries with the same hash value
709 * and space in their block for the new entry.
710 * This is good because it puts multiple same-hash value entries
711 * in a data block, improving the lookup of those entries.
713 for (use_block = -1, lep = &ents[index];
714 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
715 index++, lep++) {
716 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
717 continue;
718 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
719 ASSERT(i < be32_to_cpu(ltp->bestcount));
720 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
721 if (be16_to_cpu(bestsp[i]) >= length) {
722 use_block = i;
723 break;
727 * Didn't find a block yet, linear search all the data blocks.
729 if (use_block == -1) {
730 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
732 * Remember a block we see that's missing.
734 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
735 use_block == -1)
736 use_block = i;
737 else if (be16_to_cpu(bestsp[i]) >= length) {
738 use_block = i;
739 break;
744 * How many bytes do we need in the leaf block?
746 needbytes = 0;
747 if (!leafhdr.stale)
748 needbytes += sizeof(xfs_dir2_leaf_entry_t);
749 if (use_block == -1)
750 needbytes += sizeof(xfs_dir2_data_off_t);
753 * Now kill use_block if it refers to a missing block, so we
754 * can use it as an indication of allocation needed.
756 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
757 use_block = -1;
759 * If we don't have enough free bytes but we can make enough
760 * by compacting out stale entries, we'll do that.
762 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
763 leafhdr.stale > 1)
764 compact = 1;
767 * Otherwise if we don't have enough free bytes we need to
768 * convert to node form.
770 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
772 * Just checking or no space reservation, give up.
774 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
775 args->total == 0) {
776 xfs_trans_brelse(tp, lbp);
777 return XFS_ERROR(ENOSPC);
780 * Convert to node form.
782 error = xfs_dir2_leaf_to_node(args, lbp);
783 if (error)
784 return error;
786 * Then add the new entry.
788 return xfs_dir2_node_addname(args);
791 * Otherwise it will fit without compaction.
793 else
794 compact = 0;
796 * If just checking, then it will fit unless we needed to allocate
797 * a new data block.
799 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
800 xfs_trans_brelse(tp, lbp);
801 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
804 * If no allocations are allowed, return now before we've
805 * changed anything.
807 if (args->total == 0 && use_block == -1) {
808 xfs_trans_brelse(tp, lbp);
809 return XFS_ERROR(ENOSPC);
812 * Need to compact the leaf entries, removing stale ones.
813 * Leave one stale entry behind - the one closest to our
814 * insertion index - and we'll shift that one to our insertion
815 * point later.
817 if (compact) {
818 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
819 &highstale, &lfloglow, &lfloghigh);
822 * There are stale entries, so we'll need log-low and log-high
823 * impossibly bad values later.
825 else if (leafhdr.stale) {
826 lfloglow = leafhdr.count;
827 lfloghigh = -1;
830 * If there was no data block space found, we need to allocate
831 * a new one.
833 if (use_block == -1) {
835 * Add the new data block.
837 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
838 &use_block))) {
839 xfs_trans_brelse(tp, lbp);
840 return error;
843 * Initialize the block.
845 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
846 xfs_trans_brelse(tp, lbp);
847 return error;
850 * If we're adding a new data block on the end we need to
851 * extend the bests table. Copy it up one entry.
853 if (use_block >= be32_to_cpu(ltp->bestcount)) {
854 bestsp--;
855 memmove(&bestsp[0], &bestsp[1],
856 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
857 be32_add_cpu(&ltp->bestcount, 1);
858 xfs_dir3_leaf_log_tail(tp, lbp);
859 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
862 * If we're filling in a previously empty block just log it.
864 else
865 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
866 hdr = dbp->b_addr;
867 bf = xfs_dir3_data_bestfree_p(hdr);
868 bestsp[use_block] = bf[0].length;
869 grown = 1;
870 } else {
872 * Already had space in some data block.
873 * Just read that one in.
875 error = xfs_dir3_data_read(tp, dp,
876 xfs_dir2_db_to_da(mp, use_block),
877 -1, &dbp);
878 if (error) {
879 xfs_trans_brelse(tp, lbp);
880 return error;
882 hdr = dbp->b_addr;
883 bf = xfs_dir3_data_bestfree_p(hdr);
884 grown = 0;
887 * Point to the biggest freespace in our data block.
889 dup = (xfs_dir2_data_unused_t *)
890 ((char *)hdr + be16_to_cpu(bf[0].offset));
891 ASSERT(be16_to_cpu(dup->length) >= length);
892 needscan = needlog = 0;
894 * Mark the initial part of our freespace in use for the new entry.
896 xfs_dir2_data_use_free(tp, dbp, dup,
897 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
898 &needlog, &needscan);
900 * Initialize our new entry (at last).
902 dep = (xfs_dir2_data_entry_t *)dup;
903 dep->inumber = cpu_to_be64(args->inumber);
904 dep->namelen = args->namelen;
905 memcpy(dep->name, args->name, dep->namelen);
906 xfs_dir3_dirent_put_ftype(mp, dep, args->filetype);
907 tagp = xfs_dir3_data_entry_tag_p(mp, dep);
908 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
910 * Need to scan fix up the bestfree table.
912 if (needscan)
913 xfs_dir2_data_freescan(mp, hdr, &needlog);
915 * Need to log the data block's header.
917 if (needlog)
918 xfs_dir2_data_log_header(tp, dbp);
919 xfs_dir2_data_log_entry(tp, dbp, dep);
921 * If the bests table needs to be changed, do it.
922 * Log the change unless we've already done that.
924 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
925 bestsp[use_block] = bf[0].length;
926 if (!grown)
927 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
930 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
931 highstale, &lfloglow, &lfloghigh);
934 * Fill in the new leaf entry.
936 lep->hashval = cpu_to_be32(args->hashval);
937 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
938 be16_to_cpu(*tagp)));
940 * Log the leaf fields and give up the buffers.
942 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
943 xfs_dir3_leaf_log_header(tp, lbp);
944 xfs_dir3_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
945 xfs_dir3_leaf_check(mp, lbp);
946 xfs_dir3_data_check(dp, dbp);
947 return 0;
951 * Compact out any stale entries in the leaf.
952 * Log the header and changed leaf entries, if any.
954 void
955 xfs_dir3_leaf_compact(
956 xfs_da_args_t *args, /* operation arguments */
957 struct xfs_dir3_icleaf_hdr *leafhdr,
958 struct xfs_buf *bp) /* leaf buffer */
960 int from; /* source leaf index */
961 xfs_dir2_leaf_t *leaf; /* leaf structure */
962 int loglow; /* first leaf entry to log */
963 int to; /* target leaf index */
964 struct xfs_dir2_leaf_entry *ents;
966 leaf = bp->b_addr;
967 if (!leafhdr->stale)
968 return;
971 * Compress out the stale entries in place.
973 ents = xfs_dir3_leaf_ents_p(leaf);
974 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
975 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
976 continue;
978 * Only actually copy the entries that are different.
980 if (from > to) {
981 if (loglow == -1)
982 loglow = to;
983 ents[to] = ents[from];
985 to++;
988 * Update and log the header, log the leaf entries.
990 ASSERT(leafhdr->stale == from - to);
991 leafhdr->count -= leafhdr->stale;
992 leafhdr->stale = 0;
994 xfs_dir3_leaf_hdr_to_disk(leaf, leafhdr);
995 xfs_dir3_leaf_log_header(args->trans, bp);
996 if (loglow != -1)
997 xfs_dir3_leaf_log_ents(args->trans, bp, loglow, to - 1);
1001 * Compact the leaf entries, removing stale ones.
1002 * Leave one stale entry behind - the one closest to our
1003 * insertion index - and the caller will shift that one to our insertion
1004 * point later.
1005 * Return new insertion index, where the remaining stale entry is,
1006 * and leaf logging indices.
1008 void
1009 xfs_dir3_leaf_compact_x1(
1010 struct xfs_dir3_icleaf_hdr *leafhdr,
1011 struct xfs_dir2_leaf_entry *ents,
1012 int *indexp, /* insertion index */
1013 int *lowstalep, /* out: stale entry before us */
1014 int *highstalep, /* out: stale entry after us */
1015 int *lowlogp, /* out: low log index */
1016 int *highlogp) /* out: high log index */
1018 int from; /* source copy index */
1019 int highstale; /* stale entry at/after index */
1020 int index; /* insertion index */
1021 int keepstale; /* source index of kept stale */
1022 int lowstale; /* stale entry before index */
1023 int newindex=0; /* new insertion index */
1024 int to; /* destination copy index */
1026 ASSERT(leafhdr->stale > 1);
1027 index = *indexp;
1029 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1032 * Pick the better of lowstale and highstale.
1034 if (lowstale >= 0 &&
1035 (highstale == leafhdr->count ||
1036 index - lowstale <= highstale - index))
1037 keepstale = lowstale;
1038 else
1039 keepstale = highstale;
1041 * Copy the entries in place, removing all the stale entries
1042 * except keepstale.
1044 for (from = to = 0; from < leafhdr->count; from++) {
1046 * Notice the new value of index.
1048 if (index == from)
1049 newindex = to;
1050 if (from != keepstale &&
1051 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1052 if (from == to)
1053 *lowlogp = to;
1054 continue;
1057 * Record the new keepstale value for the insertion.
1059 if (from == keepstale)
1060 lowstale = highstale = to;
1062 * Copy only the entries that have moved.
1064 if (from > to)
1065 ents[to] = ents[from];
1066 to++;
1068 ASSERT(from > to);
1070 * If the insertion point was past the last entry,
1071 * set the new insertion point accordingly.
1073 if (index == from)
1074 newindex = to;
1075 *indexp = newindex;
1077 * Adjust the leaf header values.
1079 leafhdr->count -= from - to;
1080 leafhdr->stale = 1;
1082 * Remember the low/high stale value only in the "right"
1083 * direction.
1085 if (lowstale >= newindex)
1086 lowstale = -1;
1087 else
1088 highstale = leafhdr->count;
1089 *highlogp = leafhdr->count - 1;
1090 *lowstalep = lowstale;
1091 *highstalep = highstale;
1095 * Log the bests entries indicated from a leaf1 block.
1097 static void
1098 xfs_dir3_leaf_log_bests(
1099 xfs_trans_t *tp, /* transaction pointer */
1100 struct xfs_buf *bp, /* leaf buffer */
1101 int first, /* first entry to log */
1102 int last) /* last entry to log */
1104 __be16 *firstb; /* pointer to first entry */
1105 __be16 *lastb; /* pointer to last entry */
1106 struct xfs_dir2_leaf *leaf = bp->b_addr;
1107 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1109 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1110 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1112 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1113 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1114 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1115 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1116 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1120 * Log the leaf entries indicated from a leaf1 or leafn block.
1122 void
1123 xfs_dir3_leaf_log_ents(
1124 xfs_trans_t *tp, /* transaction pointer */
1125 struct xfs_buf *bp, /* leaf buffer */
1126 int first, /* first entry to log */
1127 int last) /* last entry to log */
1129 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1130 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1131 struct xfs_dir2_leaf *leaf = bp->b_addr;
1132 struct xfs_dir2_leaf_entry *ents;
1134 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1135 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1136 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1137 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1139 ents = xfs_dir3_leaf_ents_p(leaf);
1140 firstlep = &ents[first];
1141 lastlep = &ents[last];
1142 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1143 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1147 * Log the header of the leaf1 or leafn block.
1149 void
1150 xfs_dir3_leaf_log_header(
1151 struct xfs_trans *tp,
1152 struct xfs_buf *bp)
1154 struct xfs_dir2_leaf *leaf = bp->b_addr;
1156 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1157 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1158 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1159 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1161 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1162 xfs_dir3_leaf_hdr_size(leaf) - 1);
1166 * Log the tail of the leaf1 block.
1168 STATIC void
1169 xfs_dir3_leaf_log_tail(
1170 struct xfs_trans *tp,
1171 struct xfs_buf *bp)
1173 struct xfs_dir2_leaf *leaf = bp->b_addr;
1174 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1175 struct xfs_mount *mp = tp->t_mountp;
1177 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1178 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1179 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1180 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1182 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1183 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1184 (uint)(mp->m_dirblksize - 1));
1188 * Look up the entry referred to by args in the leaf format directory.
1189 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1190 * is also used by the node-format code.
1193 xfs_dir2_leaf_lookup(
1194 xfs_da_args_t *args) /* operation arguments */
1196 struct xfs_buf *dbp; /* data block buffer */
1197 xfs_dir2_data_entry_t *dep; /* data block entry */
1198 xfs_inode_t *dp; /* incore directory inode */
1199 int error; /* error return code */
1200 int index; /* found entry index */
1201 struct xfs_buf *lbp; /* leaf buffer */
1202 xfs_dir2_leaf_t *leaf; /* leaf structure */
1203 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1204 xfs_trans_t *tp; /* transaction pointer */
1205 struct xfs_dir2_leaf_entry *ents;
1207 trace_xfs_dir2_leaf_lookup(args);
1210 * Look up name in the leaf block, returning both buffers and index.
1212 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1213 return error;
1215 tp = args->trans;
1216 dp = args->dp;
1217 xfs_dir3_leaf_check(dp->i_mount, lbp);
1218 leaf = lbp->b_addr;
1219 ents = xfs_dir3_leaf_ents_p(leaf);
1221 * Get to the leaf entry and contained data entry address.
1223 lep = &ents[index];
1226 * Point to the data entry.
1228 dep = (xfs_dir2_data_entry_t *)
1229 ((char *)dbp->b_addr +
1230 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1232 * Return the found inode number & CI name if appropriate
1234 args->inumber = be64_to_cpu(dep->inumber);
1235 args->filetype = xfs_dir3_dirent_get_ftype(dp->i_mount, dep);
1236 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1237 xfs_trans_brelse(tp, dbp);
1238 xfs_trans_brelse(tp, lbp);
1239 return XFS_ERROR(error);
1243 * Look up name/hash in the leaf block.
1244 * Fill in indexp with the found index, and dbpp with the data buffer.
1245 * If not found dbpp will be NULL, and ENOENT comes back.
1246 * lbpp will always be filled in with the leaf buffer unless there's an error.
1248 static int /* error */
1249 xfs_dir2_leaf_lookup_int(
1250 xfs_da_args_t *args, /* operation arguments */
1251 struct xfs_buf **lbpp, /* out: leaf buffer */
1252 int *indexp, /* out: index in leaf block */
1253 struct xfs_buf **dbpp) /* out: data buffer */
1255 xfs_dir2_db_t curdb = -1; /* current data block number */
1256 struct xfs_buf *dbp = NULL; /* data buffer */
1257 xfs_dir2_data_entry_t *dep; /* data entry */
1258 xfs_inode_t *dp; /* incore directory inode */
1259 int error; /* error return code */
1260 int index; /* index in leaf block */
1261 struct xfs_buf *lbp; /* leaf buffer */
1262 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1263 xfs_dir2_leaf_t *leaf; /* leaf structure */
1264 xfs_mount_t *mp; /* filesystem mount point */
1265 xfs_dir2_db_t newdb; /* new data block number */
1266 xfs_trans_t *tp; /* transaction pointer */
1267 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1268 enum xfs_dacmp cmp; /* name compare result */
1269 struct xfs_dir2_leaf_entry *ents;
1270 struct xfs_dir3_icleaf_hdr leafhdr;
1272 dp = args->dp;
1273 tp = args->trans;
1274 mp = dp->i_mount;
1276 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
1277 if (error)
1278 return error;
1280 *lbpp = lbp;
1281 leaf = lbp->b_addr;
1282 xfs_dir3_leaf_check(mp, lbp);
1283 ents = xfs_dir3_leaf_ents_p(leaf);
1284 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1287 * Look for the first leaf entry with our hash value.
1289 index = xfs_dir2_leaf_search_hash(args, lbp);
1291 * Loop over all the entries with the right hash value
1292 * looking to match the name.
1294 for (lep = &ents[index];
1295 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1296 lep++, index++) {
1298 * Skip over stale leaf entries.
1300 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1301 continue;
1303 * Get the new data block number.
1305 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1307 * If it's not the same as the old data block number,
1308 * need to pitch the old one and read the new one.
1310 if (newdb != curdb) {
1311 if (dbp)
1312 xfs_trans_brelse(tp, dbp);
1313 error = xfs_dir3_data_read(tp, dp,
1314 xfs_dir2_db_to_da(mp, newdb),
1315 -1, &dbp);
1316 if (error) {
1317 xfs_trans_brelse(tp, lbp);
1318 return error;
1320 curdb = newdb;
1323 * Point to the data entry.
1325 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1326 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1328 * Compare name and if it's an exact match, return the index
1329 * and buffer. If it's the first case-insensitive match, store
1330 * the index and buffer and continue looking for an exact match.
1332 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1333 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1334 args->cmpresult = cmp;
1335 *indexp = index;
1336 /* case exact match: return the current buffer. */
1337 if (cmp == XFS_CMP_EXACT) {
1338 *dbpp = dbp;
1339 return 0;
1341 cidb = curdb;
1344 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1346 * Here, we can only be doing a lookup (not a rename or remove).
1347 * If a case-insensitive match was found earlier, re-read the
1348 * appropriate data block if required and return it.
1350 if (args->cmpresult == XFS_CMP_CASE) {
1351 ASSERT(cidb != -1);
1352 if (cidb != curdb) {
1353 xfs_trans_brelse(tp, dbp);
1354 error = xfs_dir3_data_read(tp, dp,
1355 xfs_dir2_db_to_da(mp, cidb),
1356 -1, &dbp);
1357 if (error) {
1358 xfs_trans_brelse(tp, lbp);
1359 return error;
1362 *dbpp = dbp;
1363 return 0;
1366 * No match found, return ENOENT.
1368 ASSERT(cidb == -1);
1369 if (dbp)
1370 xfs_trans_brelse(tp, dbp);
1371 xfs_trans_brelse(tp, lbp);
1372 return XFS_ERROR(ENOENT);
1376 * Remove an entry from a leaf format directory.
1378 int /* error */
1379 xfs_dir2_leaf_removename(
1380 xfs_da_args_t *args) /* operation arguments */
1382 __be16 *bestsp; /* leaf block best freespace */
1383 xfs_dir2_data_hdr_t *hdr; /* data block header */
1384 xfs_dir2_db_t db; /* data block number */
1385 struct xfs_buf *dbp; /* data block buffer */
1386 xfs_dir2_data_entry_t *dep; /* data entry structure */
1387 xfs_inode_t *dp; /* incore directory inode */
1388 int error; /* error return code */
1389 xfs_dir2_db_t i; /* temporary data block # */
1390 int index; /* index into leaf entries */
1391 struct xfs_buf *lbp; /* leaf buffer */
1392 xfs_dir2_leaf_t *leaf; /* leaf structure */
1393 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1394 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1395 xfs_mount_t *mp; /* filesystem mount point */
1396 int needlog; /* need to log data header */
1397 int needscan; /* need to rescan data frees */
1398 xfs_dir2_data_off_t oldbest; /* old value of best free */
1399 xfs_trans_t *tp; /* transaction pointer */
1400 struct xfs_dir2_data_free *bf; /* bestfree table */
1401 struct xfs_dir2_leaf_entry *ents;
1402 struct xfs_dir3_icleaf_hdr leafhdr;
1404 trace_xfs_dir2_leaf_removename(args);
1407 * Lookup the leaf entry, get the leaf and data blocks read in.
1409 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1410 return error;
1412 dp = args->dp;
1413 tp = args->trans;
1414 mp = dp->i_mount;
1415 leaf = lbp->b_addr;
1416 hdr = dbp->b_addr;
1417 xfs_dir3_data_check(dp, dbp);
1418 bf = xfs_dir3_data_bestfree_p(hdr);
1419 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1420 ents = xfs_dir3_leaf_ents_p(leaf);
1422 * Point to the leaf entry, use that to point to the data entry.
1424 lep = &ents[index];
1425 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1426 dep = (xfs_dir2_data_entry_t *)
1427 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1428 needscan = needlog = 0;
1429 oldbest = be16_to_cpu(bf[0].length);
1430 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1431 bestsp = xfs_dir2_leaf_bests_p(ltp);
1432 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1434 * Mark the former data entry unused.
1436 xfs_dir2_data_make_free(tp, dbp,
1437 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1438 xfs_dir3_data_entsize(mp, dep->namelen), &needlog, &needscan);
1440 * We just mark the leaf entry stale by putting a null in it.
1442 leafhdr.stale++;
1443 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1444 xfs_dir3_leaf_log_header(tp, lbp);
1446 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1447 xfs_dir3_leaf_log_ents(tp, lbp, index, index);
1450 * Scan the freespace in the data block again if necessary,
1451 * log the data block header if necessary.
1453 if (needscan)
1454 xfs_dir2_data_freescan(mp, hdr, &needlog);
1455 if (needlog)
1456 xfs_dir2_data_log_header(tp, dbp);
1458 * If the longest freespace in the data block has changed,
1459 * put the new value in the bests table and log that.
1461 if (be16_to_cpu(bf[0].length) != oldbest) {
1462 bestsp[db] = bf[0].length;
1463 xfs_dir3_leaf_log_bests(tp, lbp, db, db);
1465 xfs_dir3_data_check(dp, dbp);
1467 * If the data block is now empty then get rid of the data block.
1469 if (be16_to_cpu(bf[0].length) ==
1470 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
1471 ASSERT(db != mp->m_dirdatablk);
1472 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1474 * Nope, can't get rid of it because it caused
1475 * allocation of a bmap btree block to do so.
1476 * Just go on, returning success, leaving the
1477 * empty block in place.
1479 if (error == ENOSPC && args->total == 0)
1480 error = 0;
1481 xfs_dir3_leaf_check(mp, lbp);
1482 return error;
1484 dbp = NULL;
1486 * If this is the last data block then compact the
1487 * bests table by getting rid of entries.
1489 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1491 * Look for the last active entry (i).
1493 for (i = db - 1; i > 0; i--) {
1494 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1495 break;
1498 * Copy the table down so inactive entries at the
1499 * end are removed.
1501 memmove(&bestsp[db - i], bestsp,
1502 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1503 be32_add_cpu(&ltp->bestcount, -(db - i));
1504 xfs_dir3_leaf_log_tail(tp, lbp);
1505 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1506 } else
1507 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1510 * If the data block was not the first one, drop it.
1512 else if (db != mp->m_dirdatablk)
1513 dbp = NULL;
1515 xfs_dir3_leaf_check(mp, lbp);
1517 * See if we can convert to block form.
1519 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1523 * Replace the inode number in a leaf format directory entry.
1525 int /* error */
1526 xfs_dir2_leaf_replace(
1527 xfs_da_args_t *args) /* operation arguments */
1529 struct xfs_buf *dbp; /* data block buffer */
1530 xfs_dir2_data_entry_t *dep; /* data block entry */
1531 xfs_inode_t *dp; /* incore directory inode */
1532 int error; /* error return code */
1533 int index; /* index of leaf entry */
1534 struct xfs_buf *lbp; /* leaf buffer */
1535 xfs_dir2_leaf_t *leaf; /* leaf structure */
1536 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1537 xfs_trans_t *tp; /* transaction pointer */
1538 struct xfs_dir2_leaf_entry *ents;
1540 trace_xfs_dir2_leaf_replace(args);
1543 * Look up the entry.
1545 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1546 return error;
1548 dp = args->dp;
1549 leaf = lbp->b_addr;
1550 ents = xfs_dir3_leaf_ents_p(leaf);
1552 * Point to the leaf entry, get data address from it.
1554 lep = &ents[index];
1556 * Point to the data entry.
1558 dep = (xfs_dir2_data_entry_t *)
1559 ((char *)dbp->b_addr +
1560 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1561 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1563 * Put the new inode number in, log it.
1565 dep->inumber = cpu_to_be64(args->inumber);
1566 xfs_dir3_dirent_put_ftype(dp->i_mount, dep, args->filetype);
1567 tp = args->trans;
1568 xfs_dir2_data_log_entry(tp, dbp, dep);
1569 xfs_dir3_leaf_check(dp->i_mount, lbp);
1570 xfs_trans_brelse(tp, lbp);
1571 return 0;
1575 * Return index in the leaf block (lbp) which is either the first
1576 * one with this hash value, or if there are none, the insert point
1577 * for that hash value.
1579 int /* index value */
1580 xfs_dir2_leaf_search_hash(
1581 xfs_da_args_t *args, /* operation arguments */
1582 struct xfs_buf *lbp) /* leaf buffer */
1584 xfs_dahash_t hash=0; /* hash from this entry */
1585 xfs_dahash_t hashwant; /* hash value looking for */
1586 int high; /* high leaf index */
1587 int low; /* low leaf index */
1588 xfs_dir2_leaf_t *leaf; /* leaf structure */
1589 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1590 int mid=0; /* current leaf index */
1591 struct xfs_dir2_leaf_entry *ents;
1592 struct xfs_dir3_icleaf_hdr leafhdr;
1594 leaf = lbp->b_addr;
1595 ents = xfs_dir3_leaf_ents_p(leaf);
1596 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1599 * Note, the table cannot be empty, so we have to go through the loop.
1600 * Binary search the leaf entries looking for our hash value.
1602 for (lep = ents, low = 0, high = leafhdr.count - 1,
1603 hashwant = args->hashval;
1604 low <= high; ) {
1605 mid = (low + high) >> 1;
1606 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1607 break;
1608 if (hash < hashwant)
1609 low = mid + 1;
1610 else
1611 high = mid - 1;
1614 * Found one, back up through all the equal hash values.
1616 if (hash == hashwant) {
1617 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1618 mid--;
1622 * Need to point to an entry higher than ours.
1624 else if (hash < hashwant)
1625 mid++;
1626 return mid;
1630 * Trim off a trailing data block. We know it's empty since the leaf
1631 * freespace table says so.
1633 int /* error */
1634 xfs_dir2_leaf_trim_data(
1635 xfs_da_args_t *args, /* operation arguments */
1636 struct xfs_buf *lbp, /* leaf buffer */
1637 xfs_dir2_db_t db) /* data block number */
1639 __be16 *bestsp; /* leaf bests table */
1640 struct xfs_buf *dbp; /* data block buffer */
1641 xfs_inode_t *dp; /* incore directory inode */
1642 int error; /* error return value */
1643 xfs_dir2_leaf_t *leaf; /* leaf structure */
1644 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1645 xfs_mount_t *mp; /* filesystem mount point */
1646 xfs_trans_t *tp; /* transaction pointer */
1648 dp = args->dp;
1649 mp = dp->i_mount;
1650 tp = args->trans;
1652 * Read the offending data block. We need its buffer.
1654 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
1655 if (error)
1656 return error;
1658 leaf = lbp->b_addr;
1659 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1661 #ifdef DEBUG
1663 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1664 struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
1666 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1667 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1668 ASSERT(be16_to_cpu(bf[0].length) ==
1669 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
1670 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1672 #endif
1675 * Get rid of the data block.
1677 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1678 ASSERT(error != ENOSPC);
1679 xfs_trans_brelse(tp, dbp);
1680 return error;
1683 * Eliminate the last bests entry from the table.
1685 bestsp = xfs_dir2_leaf_bests_p(ltp);
1686 be32_add_cpu(&ltp->bestcount, -1);
1687 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1688 xfs_dir3_leaf_log_tail(tp, lbp);
1689 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1690 return 0;
1693 static inline size_t
1694 xfs_dir3_leaf_size(
1695 struct xfs_dir3_icleaf_hdr *hdr,
1696 int counts)
1698 int entries;
1699 int hdrsize;
1701 entries = hdr->count - hdr->stale;
1702 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1703 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1704 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1705 else
1706 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1708 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1709 + counts * sizeof(xfs_dir2_data_off_t)
1710 + sizeof(xfs_dir2_leaf_tail_t);
1714 * Convert node form directory to leaf form directory.
1715 * The root of the node form dir needs to already be a LEAFN block.
1716 * Just return if we can't do anything.
1718 int /* error */
1719 xfs_dir2_node_to_leaf(
1720 xfs_da_state_t *state) /* directory operation state */
1722 xfs_da_args_t *args; /* operation arguments */
1723 xfs_inode_t *dp; /* incore directory inode */
1724 int error; /* error return code */
1725 struct xfs_buf *fbp; /* buffer for freespace block */
1726 xfs_fileoff_t fo; /* freespace file offset */
1727 xfs_dir2_free_t *free; /* freespace structure */
1728 struct xfs_buf *lbp; /* buffer for leaf block */
1729 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1730 xfs_dir2_leaf_t *leaf; /* leaf structure */
1731 xfs_mount_t *mp; /* filesystem mount point */
1732 int rval; /* successful free trim? */
1733 xfs_trans_t *tp; /* transaction pointer */
1734 struct xfs_dir3_icleaf_hdr leafhdr;
1735 struct xfs_dir3_icfree_hdr freehdr;
1738 * There's more than a leaf level in the btree, so there must
1739 * be multiple leafn blocks. Give up.
1741 if (state->path.active > 1)
1742 return 0;
1743 args = state->args;
1745 trace_xfs_dir2_node_to_leaf(args);
1747 mp = state->mp;
1748 dp = args->dp;
1749 tp = args->trans;
1751 * Get the last offset in the file.
1753 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1754 return error;
1756 fo -= mp->m_dirblkfsbs;
1758 * If there are freespace blocks other than the first one,
1759 * take this opportunity to remove trailing empty freespace blocks
1760 * that may have been left behind during no-space-reservation
1761 * operations.
1763 while (fo > mp->m_dirfreeblk) {
1764 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1765 return error;
1767 if (rval)
1768 fo -= mp->m_dirblkfsbs;
1769 else
1770 return 0;
1773 * Now find the block just before the freespace block.
1775 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1776 return error;
1779 * If it's not the single leaf block, give up.
1781 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1782 return 0;
1783 lbp = state->path.blk[0].bp;
1784 leaf = lbp->b_addr;
1785 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1787 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1788 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1791 * Read the freespace block.
1793 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
1794 if (error)
1795 return error;
1796 free = fbp->b_addr;
1797 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1799 ASSERT(!freehdr.firstdb);
1802 * Now see if the leafn and free data will fit in a leaf1.
1803 * If not, release the buffer and give up.
1805 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
1806 xfs_trans_brelse(tp, fbp);
1807 return 0;
1811 * If the leaf has any stale entries in it, compress them out.
1813 if (leafhdr.stale)
1814 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1816 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1817 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1818 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1819 ? XFS_DIR2_LEAF1_MAGIC
1820 : XFS_DIR3_LEAF1_MAGIC;
1823 * Set up the leaf tail from the freespace block.
1825 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1826 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1829 * Set up the leaf bests table.
1831 memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
1832 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1834 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1835 xfs_dir3_leaf_log_header(tp, lbp);
1836 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1837 xfs_dir3_leaf_log_tail(tp, lbp);
1838 xfs_dir3_leaf_check(mp, lbp);
1841 * Get rid of the freespace block.
1843 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1844 if (error) {
1846 * This can't fail here because it can only happen when
1847 * punching out the middle of an extent, and this is an
1848 * isolated block.
1850 ASSERT(error != ENOSPC);
1851 return error;
1853 fbp = NULL;
1855 * Now see if we can convert the single-leaf directory
1856 * down to a block form directory.
1857 * This routine always kills the dabuf for the leaf, so
1858 * eliminate it from the path.
1860 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1861 state->path.blk[0].bp = NULL;
1862 return error;