1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Koji Sato.
10 #include <linux/slab.h>
11 #include <linux/string.h>
12 #include <linux/errno.h>
13 #include <linux/pagevec.h>
21 static void __nilfs_btree_init(struct nilfs_bmap
*bmap
);
23 static struct nilfs_btree_path
*nilfs_btree_alloc_path(void)
25 struct nilfs_btree_path
*path
;
26 int level
= NILFS_BTREE_LEVEL_DATA
;
28 path
= kmem_cache_alloc(nilfs_btree_path_cache
, GFP_NOFS
);
32 for (; level
< NILFS_BTREE_LEVEL_MAX
; level
++) {
33 path
[level
].bp_bh
= NULL
;
34 path
[level
].bp_sib_bh
= NULL
;
35 path
[level
].bp_index
= 0;
36 path
[level
].bp_oldreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
37 path
[level
].bp_newreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
38 path
[level
].bp_op
= NULL
;
45 static void nilfs_btree_free_path(struct nilfs_btree_path
*path
)
47 int level
= NILFS_BTREE_LEVEL_DATA
;
49 for (; level
< NILFS_BTREE_LEVEL_MAX
; level
++)
50 brelse(path
[level
].bp_bh
);
52 kmem_cache_free(nilfs_btree_path_cache
, path
);
56 * B-tree node operations
58 static int nilfs_btree_get_new_block(const struct nilfs_bmap
*btree
,
59 __u64 ptr
, struct buffer_head
**bhp
)
61 struct inode
*btnc_inode
= NILFS_BMAP_I(btree
)->i_assoc_inode
;
62 struct address_space
*btnc
= btnc_inode
->i_mapping
;
63 struct buffer_head
*bh
;
65 bh
= nilfs_btnode_create_block(btnc
, ptr
);
69 set_buffer_nilfs_volatile(bh
);
74 static int nilfs_btree_node_get_flags(const struct nilfs_btree_node
*node
)
76 return node
->bn_flags
;
80 nilfs_btree_node_set_flags(struct nilfs_btree_node
*node
, int flags
)
82 node
->bn_flags
= flags
;
85 static int nilfs_btree_node_root(const struct nilfs_btree_node
*node
)
87 return nilfs_btree_node_get_flags(node
) & NILFS_BTREE_NODE_ROOT
;
90 static int nilfs_btree_node_get_level(const struct nilfs_btree_node
*node
)
92 return node
->bn_level
;
96 nilfs_btree_node_set_level(struct nilfs_btree_node
*node
, int level
)
98 node
->bn_level
= level
;
101 static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node
*node
)
103 return le16_to_cpu(node
->bn_nchildren
);
107 nilfs_btree_node_set_nchildren(struct nilfs_btree_node
*node
, int nchildren
)
109 node
->bn_nchildren
= cpu_to_le16(nchildren
);
112 static int nilfs_btree_node_size(const struct nilfs_bmap
*btree
)
114 return i_blocksize(btree
->b_inode
);
117 static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap
*btree
)
119 return btree
->b_nchildren_per_block
;
123 nilfs_btree_node_dkeys(const struct nilfs_btree_node
*node
)
125 return (__le64
*)((char *)(node
+ 1) +
126 (nilfs_btree_node_root(node
) ?
127 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE
));
131 nilfs_btree_node_dptrs(const struct nilfs_btree_node
*node
, int ncmax
)
133 return (__le64
*)(nilfs_btree_node_dkeys(node
) + ncmax
);
137 nilfs_btree_node_get_key(const struct nilfs_btree_node
*node
, int index
)
139 return le64_to_cpu(*(nilfs_btree_node_dkeys(node
) + index
));
143 nilfs_btree_node_set_key(struct nilfs_btree_node
*node
, int index
, __u64 key
)
145 *(nilfs_btree_node_dkeys(node
) + index
) = cpu_to_le64(key
);
149 nilfs_btree_node_get_ptr(const struct nilfs_btree_node
*node
, int index
,
152 return le64_to_cpu(*(nilfs_btree_node_dptrs(node
, ncmax
) + index
));
156 nilfs_btree_node_set_ptr(struct nilfs_btree_node
*node
, int index
, __u64 ptr
,
159 *(nilfs_btree_node_dptrs(node
, ncmax
) + index
) = cpu_to_le64(ptr
);
162 static void nilfs_btree_node_init(struct nilfs_btree_node
*node
, int flags
,
163 int level
, int nchildren
, int ncmax
,
164 const __u64
*keys
, const __u64
*ptrs
)
170 nilfs_btree_node_set_flags(node
, flags
);
171 nilfs_btree_node_set_level(node
, level
);
172 nilfs_btree_node_set_nchildren(node
, nchildren
);
174 dkeys
= nilfs_btree_node_dkeys(node
);
175 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
176 for (i
= 0; i
< nchildren
; i
++) {
177 dkeys
[i
] = cpu_to_le64(keys
[i
]);
178 dptrs
[i
] = cpu_to_le64(ptrs
[i
]);
182 /* Assume the buffer heads corresponding to left and right are locked. */
183 static void nilfs_btree_node_move_left(struct nilfs_btree_node
*left
,
184 struct nilfs_btree_node
*right
,
185 int n
, int lncmax
, int rncmax
)
187 __le64
*ldkeys
, *rdkeys
;
188 __le64
*ldptrs
, *rdptrs
;
189 int lnchildren
, rnchildren
;
191 ldkeys
= nilfs_btree_node_dkeys(left
);
192 ldptrs
= nilfs_btree_node_dptrs(left
, lncmax
);
193 lnchildren
= nilfs_btree_node_get_nchildren(left
);
195 rdkeys
= nilfs_btree_node_dkeys(right
);
196 rdptrs
= nilfs_btree_node_dptrs(right
, rncmax
);
197 rnchildren
= nilfs_btree_node_get_nchildren(right
);
199 memcpy(ldkeys
+ lnchildren
, rdkeys
, n
* sizeof(*rdkeys
));
200 memcpy(ldptrs
+ lnchildren
, rdptrs
, n
* sizeof(*rdptrs
));
201 memmove(rdkeys
, rdkeys
+ n
, (rnchildren
- n
) * sizeof(*rdkeys
));
202 memmove(rdptrs
, rdptrs
+ n
, (rnchildren
- n
) * sizeof(*rdptrs
));
206 nilfs_btree_node_set_nchildren(left
, lnchildren
);
207 nilfs_btree_node_set_nchildren(right
, rnchildren
);
210 /* Assume that the buffer heads corresponding to left and right are locked. */
211 static void nilfs_btree_node_move_right(struct nilfs_btree_node
*left
,
212 struct nilfs_btree_node
*right
,
213 int n
, int lncmax
, int rncmax
)
215 __le64
*ldkeys
, *rdkeys
;
216 __le64
*ldptrs
, *rdptrs
;
217 int lnchildren
, rnchildren
;
219 ldkeys
= nilfs_btree_node_dkeys(left
);
220 ldptrs
= nilfs_btree_node_dptrs(left
, lncmax
);
221 lnchildren
= nilfs_btree_node_get_nchildren(left
);
223 rdkeys
= nilfs_btree_node_dkeys(right
);
224 rdptrs
= nilfs_btree_node_dptrs(right
, rncmax
);
225 rnchildren
= nilfs_btree_node_get_nchildren(right
);
227 memmove(rdkeys
+ n
, rdkeys
, rnchildren
* sizeof(*rdkeys
));
228 memmove(rdptrs
+ n
, rdptrs
, rnchildren
* sizeof(*rdptrs
));
229 memcpy(rdkeys
, ldkeys
+ lnchildren
- n
, n
* sizeof(*rdkeys
));
230 memcpy(rdptrs
, ldptrs
+ lnchildren
- n
, n
* sizeof(*rdptrs
));
234 nilfs_btree_node_set_nchildren(left
, lnchildren
);
235 nilfs_btree_node_set_nchildren(right
, rnchildren
);
238 /* Assume that the buffer head corresponding to node is locked. */
239 static void nilfs_btree_node_insert(struct nilfs_btree_node
*node
, int index
,
240 __u64 key
, __u64 ptr
, int ncmax
)
246 dkeys
= nilfs_btree_node_dkeys(node
);
247 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
248 nchildren
= nilfs_btree_node_get_nchildren(node
);
249 if (index
< nchildren
) {
250 memmove(dkeys
+ index
+ 1, dkeys
+ index
,
251 (nchildren
- index
) * sizeof(*dkeys
));
252 memmove(dptrs
+ index
+ 1, dptrs
+ index
,
253 (nchildren
- index
) * sizeof(*dptrs
));
255 dkeys
[index
] = cpu_to_le64(key
);
256 dptrs
[index
] = cpu_to_le64(ptr
);
258 nilfs_btree_node_set_nchildren(node
, nchildren
);
261 /* Assume that the buffer head corresponding to node is locked. */
262 static void nilfs_btree_node_delete(struct nilfs_btree_node
*node
, int index
,
263 __u64
*keyp
, __u64
*ptrp
, int ncmax
)
271 dkeys
= nilfs_btree_node_dkeys(node
);
272 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
273 key
= le64_to_cpu(dkeys
[index
]);
274 ptr
= le64_to_cpu(dptrs
[index
]);
275 nchildren
= nilfs_btree_node_get_nchildren(node
);
281 if (index
< nchildren
- 1) {
282 memmove(dkeys
+ index
, dkeys
+ index
+ 1,
283 (nchildren
- index
- 1) * sizeof(*dkeys
));
284 memmove(dptrs
+ index
, dptrs
+ index
+ 1,
285 (nchildren
- index
- 1) * sizeof(*dptrs
));
288 nilfs_btree_node_set_nchildren(node
, nchildren
);
291 static int nilfs_btree_node_lookup(const struct nilfs_btree_node
*node
,
292 __u64 key
, int *indexp
)
295 int index
, low
, high
, s
;
299 high
= nilfs_btree_node_get_nchildren(node
) - 1;
302 while (low
<= high
) {
303 index
= (low
+ high
) / 2;
304 nkey
= nilfs_btree_node_get_key(node
, index
);
308 } else if (nkey
< key
) {
318 if (nilfs_btree_node_get_level(node
) > NILFS_BTREE_LEVEL_NODE_MIN
) {
319 if (s
> 0 && index
> 0)
331 * nilfs_btree_node_broken - verify consistency of btree node
332 * @node: btree node block to be examined
333 * @size: node size (in bytes)
334 * @inode: host inode of btree
335 * @blocknr: block number
337 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
339 static int nilfs_btree_node_broken(const struct nilfs_btree_node
*node
,
340 size_t size
, struct inode
*inode
,
343 int level
, flags
, nchildren
;
346 level
= nilfs_btree_node_get_level(node
);
347 flags
= nilfs_btree_node_get_flags(node
);
348 nchildren
= nilfs_btree_node_get_nchildren(node
);
350 if (unlikely(level
< NILFS_BTREE_LEVEL_NODE_MIN
||
351 level
>= NILFS_BTREE_LEVEL_MAX
||
352 (flags
& NILFS_BTREE_NODE_ROOT
) ||
354 nchildren
> NILFS_BTREE_NODE_NCHILDREN_MAX(size
))) {
355 nilfs_crit(inode
->i_sb
,
356 "bad btree node (ino=%lu, blocknr=%llu): level = %d, flags = 0x%x, nchildren = %d",
357 inode
->i_ino
, (unsigned long long)blocknr
, level
,
365 * nilfs_btree_root_broken - verify consistency of btree root node
366 * @node: btree root node to be examined
367 * @inode: host inode of btree
369 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
371 static int nilfs_btree_root_broken(const struct nilfs_btree_node
*node
,
374 int level
, flags
, nchildren
;
377 level
= nilfs_btree_node_get_level(node
);
378 flags
= nilfs_btree_node_get_flags(node
);
379 nchildren
= nilfs_btree_node_get_nchildren(node
);
381 if (unlikely(level
< NILFS_BTREE_LEVEL_NODE_MIN
||
382 level
>= NILFS_BTREE_LEVEL_MAX
||
384 nchildren
> NILFS_BTREE_ROOT_NCHILDREN_MAX
||
385 (nchildren
== 0 && level
> NILFS_BTREE_LEVEL_NODE_MIN
))) {
386 nilfs_crit(inode
->i_sb
,
387 "bad btree root (ino=%lu): level = %d, flags = 0x%x, nchildren = %d",
388 inode
->i_ino
, level
, flags
, nchildren
);
394 int nilfs_btree_broken_node_block(struct buffer_head
*bh
)
399 if (buffer_nilfs_checked(bh
))
402 inode
= bh
->b_folio
->mapping
->host
;
403 ret
= nilfs_btree_node_broken((struct nilfs_btree_node
*)bh
->b_data
,
404 bh
->b_size
, inode
, bh
->b_blocknr
);
406 set_buffer_nilfs_checked(bh
);
410 static struct nilfs_btree_node
*
411 nilfs_btree_get_root(const struct nilfs_bmap
*btree
)
413 return (struct nilfs_btree_node
*)btree
->b_u
.u_data
;
416 static struct nilfs_btree_node
*
417 nilfs_btree_get_nonroot_node(const struct nilfs_btree_path
*path
, int level
)
419 return (struct nilfs_btree_node
*)path
[level
].bp_bh
->b_data
;
422 static struct nilfs_btree_node
*
423 nilfs_btree_get_sib_node(const struct nilfs_btree_path
*path
, int level
)
425 return (struct nilfs_btree_node
*)path
[level
].bp_sib_bh
->b_data
;
428 static int nilfs_btree_height(const struct nilfs_bmap
*btree
)
430 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree
)) + 1;
433 static struct nilfs_btree_node
*
434 nilfs_btree_get_node(const struct nilfs_bmap
*btree
,
435 const struct nilfs_btree_path
*path
,
436 int level
, int *ncmaxp
)
438 struct nilfs_btree_node
*node
;
440 if (level
== nilfs_btree_height(btree
) - 1) {
441 node
= nilfs_btree_get_root(btree
);
442 *ncmaxp
= NILFS_BTREE_ROOT_NCHILDREN_MAX
;
444 node
= nilfs_btree_get_nonroot_node(path
, level
);
445 *ncmaxp
= nilfs_btree_nchildren_per_block(btree
);
450 static int nilfs_btree_bad_node(const struct nilfs_bmap
*btree
,
451 struct nilfs_btree_node
*node
, int level
)
453 if (unlikely(nilfs_btree_node_get_level(node
) != level
)) {
455 nilfs_crit(btree
->b_inode
->i_sb
,
456 "btree level mismatch (ino=%lu): %d != %d",
457 btree
->b_inode
->i_ino
,
458 nilfs_btree_node_get_level(node
), level
);
464 struct nilfs_btree_readahead_info
{
465 struct nilfs_btree_node
*node
; /* parent node */
466 int max_ra_blocks
; /* max nof blocks to read ahead */
467 int index
; /* current index on the parent node */
468 int ncmax
; /* nof children in the parent node */
471 static int __nilfs_btree_get_block(const struct nilfs_bmap
*btree
, __u64 ptr
,
472 struct buffer_head
**bhp
,
473 const struct nilfs_btree_readahead_info
*ra
)
475 struct inode
*btnc_inode
= NILFS_BMAP_I(btree
)->i_assoc_inode
;
476 struct address_space
*btnc
= btnc_inode
->i_mapping
;
477 struct buffer_head
*bh
, *ra_bh
;
478 sector_t submit_ptr
= 0;
481 ret
= nilfs_btnode_submit_block(btnc
, ptr
, 0, REQ_OP_READ
, &bh
,
484 if (likely(ret
== -EEXIST
))
486 if (ret
== -ENOENT
) {
488 * Block address translation failed due to invalid
489 * value of 'ptr'. In this case, return internal code
490 * -EINVAL (broken bmap) to notify bmap layer of fatal
491 * metadata corruption.
502 /* read ahead sibling nodes */
503 for (n
= ra
->max_ra_blocks
, i
= ra
->index
+ 1;
504 n
> 0 && i
< ra
->ncmax
; n
--, i
++) {
505 ptr2
= nilfs_btree_node_get_ptr(ra
->node
, i
, ra
->ncmax
);
507 ret
= nilfs_btnode_submit_block(btnc
, ptr2
, 0,
508 REQ_OP_READ
| REQ_RAHEAD
,
509 &ra_bh
, &submit_ptr
);
510 if (likely(!ret
|| ret
== -EEXIST
))
512 else if (ret
!= -EBUSY
)
514 if (!buffer_locked(bh
))
522 if (!buffer_uptodate(bh
)) {
523 nilfs_err(btree
->b_inode
->i_sb
,
524 "I/O error reading b-tree node block (ino=%lu, blocknr=%llu)",
525 btree
->b_inode
->i_ino
, (unsigned long long)ptr
);
531 if (nilfs_btree_broken_node_block(bh
)) {
532 clear_buffer_uptodate(bh
);
541 static int nilfs_btree_get_block(const struct nilfs_bmap
*btree
, __u64 ptr
,
542 struct buffer_head
**bhp
)
544 return __nilfs_btree_get_block(btree
, ptr
, bhp
, NULL
);
547 static int nilfs_btree_do_lookup(const struct nilfs_bmap
*btree
,
548 struct nilfs_btree_path
*path
,
549 __u64 key
, __u64
*ptrp
, int minlevel
,
552 struct nilfs_btree_node
*node
;
553 struct nilfs_btree_readahead_info p
, *ra
;
555 int level
, index
, found
, ncmax
, ret
;
557 node
= nilfs_btree_get_root(btree
);
558 level
= nilfs_btree_node_get_level(node
);
559 if (level
< minlevel
|| nilfs_btree_node_get_nchildren(node
) <= 0)
562 found
= nilfs_btree_node_lookup(node
, key
, &index
);
563 ptr
= nilfs_btree_node_get_ptr(node
, index
,
564 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
565 path
[level
].bp_bh
= NULL
;
566 path
[level
].bp_index
= index
;
568 ncmax
= nilfs_btree_nchildren_per_block(btree
);
570 while (--level
>= minlevel
) {
572 if (level
== NILFS_BTREE_LEVEL_NODE_MIN
&& readahead
) {
573 p
.node
= nilfs_btree_get_node(btree
, path
, level
+ 1,
579 ret
= __nilfs_btree_get_block(btree
, ptr
, &path
[level
].bp_bh
,
584 node
= nilfs_btree_get_nonroot_node(path
, level
);
585 if (nilfs_btree_bad_node(btree
, node
, level
))
588 found
= nilfs_btree_node_lookup(node
, key
, &index
);
592 ptr
= nilfs_btree_node_get_ptr(node
, index
, ncmax
);
594 WARN_ON(found
|| level
!= NILFS_BTREE_LEVEL_NODE_MIN
);
596 ptr
= NILFS_BMAP_INVALID_PTR
;
598 path
[level
].bp_index
= index
;
609 static int nilfs_btree_do_lookup_last(const struct nilfs_bmap
*btree
,
610 struct nilfs_btree_path
*path
,
611 __u64
*keyp
, __u64
*ptrp
)
613 struct nilfs_btree_node
*node
;
615 int index
, level
, ncmax
, ret
;
617 node
= nilfs_btree_get_root(btree
);
618 index
= nilfs_btree_node_get_nchildren(node
) - 1;
621 level
= nilfs_btree_node_get_level(node
);
622 ptr
= nilfs_btree_node_get_ptr(node
, index
,
623 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
624 path
[level
].bp_bh
= NULL
;
625 path
[level
].bp_index
= index
;
626 ncmax
= nilfs_btree_nchildren_per_block(btree
);
628 for (level
--; level
> 0; level
--) {
629 ret
= nilfs_btree_get_block(btree
, ptr
, &path
[level
].bp_bh
);
632 node
= nilfs_btree_get_nonroot_node(path
, level
);
633 if (nilfs_btree_bad_node(btree
, node
, level
))
635 index
= nilfs_btree_node_get_nchildren(node
) - 1;
636 ptr
= nilfs_btree_node_get_ptr(node
, index
, ncmax
);
637 path
[level
].bp_index
= index
;
641 *keyp
= nilfs_btree_node_get_key(node
, index
);
649 * nilfs_btree_get_next_key - get next valid key from btree path array
650 * @btree: bmap struct of btree
651 * @path: array of nilfs_btree_path struct
652 * @minlevel: start level
653 * @nextkey: place to store the next valid key
655 * Return Value: If a next key was found, 0 is returned. Otherwise,
656 * -ENOENT is returned.
658 static int nilfs_btree_get_next_key(const struct nilfs_bmap
*btree
,
659 const struct nilfs_btree_path
*path
,
660 int minlevel
, __u64
*nextkey
)
662 struct nilfs_btree_node
*node
;
663 int maxlevel
= nilfs_btree_height(btree
) - 1;
664 int index
, next_adj
, level
;
666 /* Next index is already set to bp_index for leaf nodes. */
668 for (level
= minlevel
; level
<= maxlevel
; level
++) {
669 if (level
== maxlevel
)
670 node
= nilfs_btree_get_root(btree
);
672 node
= nilfs_btree_get_nonroot_node(path
, level
);
674 index
= path
[level
].bp_index
+ next_adj
;
675 if (index
< nilfs_btree_node_get_nchildren(node
)) {
676 /* Next key is in this node */
677 *nextkey
= nilfs_btree_node_get_key(node
, index
);
680 /* For non-leaf nodes, next index is stored at bp_index + 1. */
686 static int nilfs_btree_lookup(const struct nilfs_bmap
*btree
,
687 __u64 key
, int level
, __u64
*ptrp
)
689 struct nilfs_btree_path
*path
;
692 path
= nilfs_btree_alloc_path();
696 ret
= nilfs_btree_do_lookup(btree
, path
, key
, ptrp
, level
, 0);
698 nilfs_btree_free_path(path
);
703 static int nilfs_btree_lookup_contig(const struct nilfs_bmap
*btree
,
704 __u64 key
, __u64
*ptrp
,
705 unsigned int maxblocks
)
707 struct nilfs_btree_path
*path
;
708 struct nilfs_btree_node
*node
;
709 struct inode
*dat
= NULL
;
712 int level
= NILFS_BTREE_LEVEL_NODE_MIN
;
713 int ret
, cnt
, index
, maxlevel
, ncmax
;
714 struct nilfs_btree_readahead_info p
;
716 path
= nilfs_btree_alloc_path();
720 ret
= nilfs_btree_do_lookup(btree
, path
, key
, &ptr
, level
, 1);
724 if (NILFS_BMAP_USE_VBN(btree
)) {
725 dat
= nilfs_bmap_get_dat(btree
);
726 ret
= nilfs_dat_translate(dat
, ptr
, &blocknr
);
732 if (cnt
== maxblocks
)
735 maxlevel
= nilfs_btree_height(btree
) - 1;
736 node
= nilfs_btree_get_node(btree
, path
, level
, &ncmax
);
737 index
= path
[level
].bp_index
+ 1;
739 while (index
< nilfs_btree_node_get_nchildren(node
)) {
740 if (nilfs_btree_node_get_key(node
, index
) !=
743 ptr2
= nilfs_btree_node_get_ptr(node
, index
, ncmax
);
745 ret
= nilfs_dat_translate(dat
, ptr2
, &blocknr
);
750 if (ptr2
!= ptr
+ cnt
|| ++cnt
== maxblocks
)
754 if (level
== maxlevel
)
757 /* look-up right sibling node */
758 p
.node
= nilfs_btree_get_node(btree
, path
, level
+ 1, &p
.ncmax
);
759 p
.index
= path
[level
+ 1].bp_index
+ 1;
761 if (p
.index
>= nilfs_btree_node_get_nchildren(p
.node
) ||
762 nilfs_btree_node_get_key(p
.node
, p
.index
) != key
+ cnt
)
764 ptr2
= nilfs_btree_node_get_ptr(p
.node
, p
.index
, p
.ncmax
);
765 path
[level
+ 1].bp_index
= p
.index
;
767 brelse(path
[level
].bp_bh
);
768 path
[level
].bp_bh
= NULL
;
770 ret
= __nilfs_btree_get_block(btree
, ptr2
, &path
[level
].bp_bh
,
774 node
= nilfs_btree_get_nonroot_node(path
, level
);
775 ncmax
= nilfs_btree_nchildren_per_block(btree
);
777 path
[level
].bp_index
= index
;
783 nilfs_btree_free_path(path
);
788 ret
= -EINVAL
; /* Notify bmap layer of metadata corruption */
792 static void nilfs_btree_promote_key(struct nilfs_bmap
*btree
,
793 struct nilfs_btree_path
*path
,
794 int level
, __u64 key
)
796 if (level
< nilfs_btree_height(btree
) - 1) {
798 nilfs_btree_node_set_key(
799 nilfs_btree_get_nonroot_node(path
, level
),
800 path
[level
].bp_index
, key
);
801 if (!buffer_dirty(path
[level
].bp_bh
))
802 mark_buffer_dirty(path
[level
].bp_bh
);
803 } while ((path
[level
].bp_index
== 0) &&
804 (++level
< nilfs_btree_height(btree
) - 1));
808 if (level
== nilfs_btree_height(btree
) - 1) {
809 nilfs_btree_node_set_key(nilfs_btree_get_root(btree
),
810 path
[level
].bp_index
, key
);
814 static void nilfs_btree_do_insert(struct nilfs_bmap
*btree
,
815 struct nilfs_btree_path
*path
,
816 int level
, __u64
*keyp
, __u64
*ptrp
)
818 struct nilfs_btree_node
*node
;
821 if (level
< nilfs_btree_height(btree
) - 1) {
822 node
= nilfs_btree_get_nonroot_node(path
, level
);
823 ncblk
= nilfs_btree_nchildren_per_block(btree
);
824 nilfs_btree_node_insert(node
, path
[level
].bp_index
,
825 *keyp
, *ptrp
, ncblk
);
826 if (!buffer_dirty(path
[level
].bp_bh
))
827 mark_buffer_dirty(path
[level
].bp_bh
);
829 if (path
[level
].bp_index
== 0)
830 nilfs_btree_promote_key(btree
, path
, level
+ 1,
831 nilfs_btree_node_get_key(node
,
834 node
= nilfs_btree_get_root(btree
);
835 nilfs_btree_node_insert(node
, path
[level
].bp_index
,
837 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
841 static void nilfs_btree_carry_left(struct nilfs_bmap
*btree
,
842 struct nilfs_btree_path
*path
,
843 int level
, __u64
*keyp
, __u64
*ptrp
)
845 struct nilfs_btree_node
*node
, *left
;
846 int nchildren
, lnchildren
, n
, move
, ncblk
;
848 node
= nilfs_btree_get_nonroot_node(path
, level
);
849 left
= nilfs_btree_get_sib_node(path
, level
);
850 nchildren
= nilfs_btree_node_get_nchildren(node
);
851 lnchildren
= nilfs_btree_node_get_nchildren(left
);
852 ncblk
= nilfs_btree_nchildren_per_block(btree
);
855 n
= (nchildren
+ lnchildren
+ 1) / 2 - lnchildren
;
856 if (n
> path
[level
].bp_index
) {
857 /* move insert point */
862 nilfs_btree_node_move_left(left
, node
, n
, ncblk
, ncblk
);
864 if (!buffer_dirty(path
[level
].bp_bh
))
865 mark_buffer_dirty(path
[level
].bp_bh
);
866 if (!buffer_dirty(path
[level
].bp_sib_bh
))
867 mark_buffer_dirty(path
[level
].bp_sib_bh
);
869 nilfs_btree_promote_key(btree
, path
, level
+ 1,
870 nilfs_btree_node_get_key(node
, 0));
873 brelse(path
[level
].bp_bh
);
874 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
875 path
[level
].bp_sib_bh
= NULL
;
876 path
[level
].bp_index
+= lnchildren
;
877 path
[level
+ 1].bp_index
--;
879 brelse(path
[level
].bp_sib_bh
);
880 path
[level
].bp_sib_bh
= NULL
;
881 path
[level
].bp_index
-= n
;
884 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
887 static void nilfs_btree_carry_right(struct nilfs_bmap
*btree
,
888 struct nilfs_btree_path
*path
,
889 int level
, __u64
*keyp
, __u64
*ptrp
)
891 struct nilfs_btree_node
*node
, *right
;
892 int nchildren
, rnchildren
, n
, move
, ncblk
;
894 node
= nilfs_btree_get_nonroot_node(path
, level
);
895 right
= nilfs_btree_get_sib_node(path
, level
);
896 nchildren
= nilfs_btree_node_get_nchildren(node
);
897 rnchildren
= nilfs_btree_node_get_nchildren(right
);
898 ncblk
= nilfs_btree_nchildren_per_block(btree
);
901 n
= (nchildren
+ rnchildren
+ 1) / 2 - rnchildren
;
902 if (n
> nchildren
- path
[level
].bp_index
) {
903 /* move insert point */
908 nilfs_btree_node_move_right(node
, right
, n
, ncblk
, ncblk
);
910 if (!buffer_dirty(path
[level
].bp_bh
))
911 mark_buffer_dirty(path
[level
].bp_bh
);
912 if (!buffer_dirty(path
[level
].bp_sib_bh
))
913 mark_buffer_dirty(path
[level
].bp_sib_bh
);
915 path
[level
+ 1].bp_index
++;
916 nilfs_btree_promote_key(btree
, path
, level
+ 1,
917 nilfs_btree_node_get_key(right
, 0));
918 path
[level
+ 1].bp_index
--;
921 brelse(path
[level
].bp_bh
);
922 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
923 path
[level
].bp_sib_bh
= NULL
;
924 path
[level
].bp_index
-= nilfs_btree_node_get_nchildren(node
);
925 path
[level
+ 1].bp_index
++;
927 brelse(path
[level
].bp_sib_bh
);
928 path
[level
].bp_sib_bh
= NULL
;
931 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
934 static void nilfs_btree_split(struct nilfs_bmap
*btree
,
935 struct nilfs_btree_path
*path
,
936 int level
, __u64
*keyp
, __u64
*ptrp
)
938 struct nilfs_btree_node
*node
, *right
;
939 int nchildren
, n
, move
, ncblk
;
941 node
= nilfs_btree_get_nonroot_node(path
, level
);
942 right
= nilfs_btree_get_sib_node(path
, level
);
943 nchildren
= nilfs_btree_node_get_nchildren(node
);
944 ncblk
= nilfs_btree_nchildren_per_block(btree
);
947 n
= (nchildren
+ 1) / 2;
948 if (n
> nchildren
- path
[level
].bp_index
) {
953 nilfs_btree_node_move_right(node
, right
, n
, ncblk
, ncblk
);
955 if (!buffer_dirty(path
[level
].bp_bh
))
956 mark_buffer_dirty(path
[level
].bp_bh
);
957 if (!buffer_dirty(path
[level
].bp_sib_bh
))
958 mark_buffer_dirty(path
[level
].bp_sib_bh
);
961 path
[level
].bp_index
-= nilfs_btree_node_get_nchildren(node
);
962 nilfs_btree_node_insert(right
, path
[level
].bp_index
,
963 *keyp
, *ptrp
, ncblk
);
965 *keyp
= nilfs_btree_node_get_key(right
, 0);
966 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
968 brelse(path
[level
].bp_bh
);
969 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
970 path
[level
].bp_sib_bh
= NULL
;
972 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
974 *keyp
= nilfs_btree_node_get_key(right
, 0);
975 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
977 brelse(path
[level
].bp_sib_bh
);
978 path
[level
].bp_sib_bh
= NULL
;
981 path
[level
+ 1].bp_index
++;
984 static void nilfs_btree_grow(struct nilfs_bmap
*btree
,
985 struct nilfs_btree_path
*path
,
986 int level
, __u64
*keyp
, __u64
*ptrp
)
988 struct nilfs_btree_node
*root
, *child
;
991 root
= nilfs_btree_get_root(btree
);
992 child
= nilfs_btree_get_sib_node(path
, level
);
993 ncblk
= nilfs_btree_nchildren_per_block(btree
);
995 n
= nilfs_btree_node_get_nchildren(root
);
997 nilfs_btree_node_move_right(root
, child
, n
,
998 NILFS_BTREE_ROOT_NCHILDREN_MAX
, ncblk
);
999 nilfs_btree_node_set_level(root
, level
+ 1);
1001 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1002 mark_buffer_dirty(path
[level
].bp_sib_bh
);
1004 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
1005 path
[level
].bp_sib_bh
= NULL
;
1007 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
1009 *keyp
= nilfs_btree_node_get_key(child
, 0);
1010 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
1013 static __u64
nilfs_btree_find_near(const struct nilfs_bmap
*btree
,
1014 const struct nilfs_btree_path
*path
)
1016 struct nilfs_btree_node
*node
;
1020 return NILFS_BMAP_INVALID_PTR
;
1023 level
= NILFS_BTREE_LEVEL_NODE_MIN
;
1024 if (path
[level
].bp_index
> 0) {
1025 node
= nilfs_btree_get_node(btree
, path
, level
, &ncmax
);
1026 return nilfs_btree_node_get_ptr(node
,
1027 path
[level
].bp_index
- 1,
1032 level
= NILFS_BTREE_LEVEL_NODE_MIN
+ 1;
1033 if (level
<= nilfs_btree_height(btree
) - 1) {
1034 node
= nilfs_btree_get_node(btree
, path
, level
, &ncmax
);
1035 return nilfs_btree_node_get_ptr(node
, path
[level
].bp_index
,
1039 return NILFS_BMAP_INVALID_PTR
;
1042 static __u64
nilfs_btree_find_target_v(const struct nilfs_bmap
*btree
,
1043 const struct nilfs_btree_path
*path
,
1048 ptr
= nilfs_bmap_find_target_seq(btree
, key
);
1049 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
1050 /* sequential access */
1053 ptr
= nilfs_btree_find_near(btree
, path
);
1054 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
1059 return nilfs_bmap_find_target_in_group(btree
);
1062 static int nilfs_btree_prepare_insert(struct nilfs_bmap
*btree
,
1063 struct nilfs_btree_path
*path
,
1064 int *levelp
, __u64 key
, __u64 ptr
,
1065 struct nilfs_bmap_stats
*stats
)
1067 struct buffer_head
*bh
;
1068 struct nilfs_btree_node
*node
, *parent
, *sib
;
1070 int pindex
, level
, ncmax
, ncblk
, ret
;
1071 struct inode
*dat
= NULL
;
1073 stats
->bs_nblocks
= 0;
1074 level
= NILFS_BTREE_LEVEL_DATA
;
1076 /* allocate a new ptr for data block */
1077 if (NILFS_BMAP_USE_VBN(btree
)) {
1078 path
[level
].bp_newreq
.bpr_ptr
=
1079 nilfs_btree_find_target_v(btree
, path
, key
);
1080 dat
= nilfs_bmap_get_dat(btree
);
1083 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1087 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1089 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
1090 level
< nilfs_btree_height(btree
) - 1;
1092 node
= nilfs_btree_get_nonroot_node(path
, level
);
1093 if (nilfs_btree_node_get_nchildren(node
) < ncblk
) {
1094 path
[level
].bp_op
= nilfs_btree_do_insert
;
1095 stats
->bs_nblocks
++;
1099 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1100 pindex
= path
[level
+ 1].bp_index
;
1104 sibptr
= nilfs_btree_node_get_ptr(parent
, pindex
- 1,
1106 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1108 goto err_out_child_node
;
1109 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1110 if (nilfs_btree_node_get_nchildren(sib
) < ncblk
) {
1111 path
[level
].bp_sib_bh
= bh
;
1112 path
[level
].bp_op
= nilfs_btree_carry_left
;
1113 stats
->bs_nblocks
++;
1121 if (pindex
< nilfs_btree_node_get_nchildren(parent
) - 1) {
1122 sibptr
= nilfs_btree_node_get_ptr(parent
, pindex
+ 1,
1124 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1126 goto err_out_child_node
;
1127 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1128 if (nilfs_btree_node_get_nchildren(sib
) < ncblk
) {
1129 path
[level
].bp_sib_bh
= bh
;
1130 path
[level
].bp_op
= nilfs_btree_carry_right
;
1131 stats
->bs_nblocks
++;
1139 path
[level
].bp_newreq
.bpr_ptr
=
1140 path
[level
- 1].bp_newreq
.bpr_ptr
+ 1;
1141 ret
= nilfs_bmap_prepare_alloc_ptr(btree
,
1142 &path
[level
].bp_newreq
, dat
);
1144 goto err_out_child_node
;
1145 ret
= nilfs_btree_get_new_block(btree
,
1146 path
[level
].bp_newreq
.bpr_ptr
,
1149 goto err_out_curr_node
;
1151 stats
->bs_nblocks
++;
1153 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1154 nilfs_btree_node_init(sib
, 0, level
, 0, ncblk
, NULL
, NULL
);
1155 path
[level
].bp_sib_bh
= bh
;
1156 path
[level
].bp_op
= nilfs_btree_split
;
1160 node
= nilfs_btree_get_root(btree
);
1161 if (nilfs_btree_node_get_nchildren(node
) <
1162 NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1163 path
[level
].bp_op
= nilfs_btree_do_insert
;
1164 stats
->bs_nblocks
++;
1169 path
[level
].bp_newreq
.bpr_ptr
= path
[level
- 1].bp_newreq
.bpr_ptr
+ 1;
1170 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1172 goto err_out_child_node
;
1173 ret
= nilfs_btree_get_new_block(btree
, path
[level
].bp_newreq
.bpr_ptr
,
1176 goto err_out_curr_node
;
1178 nilfs_btree_node_init((struct nilfs_btree_node
*)bh
->b_data
,
1179 0, level
, 0, ncblk
, NULL
, NULL
);
1180 path
[level
].bp_sib_bh
= bh
;
1181 path
[level
].bp_op
= nilfs_btree_grow
;
1184 path
[level
].bp_op
= nilfs_btree_do_insert
;
1186 /* a newly-created node block and a data block are added */
1187 stats
->bs_nblocks
+= 2;
1196 nilfs_bmap_abort_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1198 for (level
--; level
> NILFS_BTREE_LEVEL_DATA
; level
--) {
1199 nilfs_btnode_delete(path
[level
].bp_sib_bh
);
1200 nilfs_bmap_abort_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1204 nilfs_bmap_abort_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1207 stats
->bs_nblocks
= 0;
1211 static void nilfs_btree_commit_insert(struct nilfs_bmap
*btree
,
1212 struct nilfs_btree_path
*path
,
1213 int maxlevel
, __u64 key
, __u64 ptr
)
1215 struct inode
*dat
= NULL
;
1218 set_buffer_nilfs_volatile((struct buffer_head
*)((unsigned long)ptr
));
1219 ptr
= path
[NILFS_BTREE_LEVEL_DATA
].bp_newreq
.bpr_ptr
;
1220 if (NILFS_BMAP_USE_VBN(btree
)) {
1221 nilfs_bmap_set_target_v(btree
, key
, ptr
);
1222 dat
= nilfs_bmap_get_dat(btree
);
1225 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
; level
<= maxlevel
; level
++) {
1226 nilfs_bmap_commit_alloc_ptr(btree
,
1227 &path
[level
- 1].bp_newreq
, dat
);
1228 path
[level
].bp_op(btree
, path
, level
, &key
, &ptr
);
1231 if (!nilfs_bmap_dirty(btree
))
1232 nilfs_bmap_set_dirty(btree
);
1235 static int nilfs_btree_insert(struct nilfs_bmap
*btree
, __u64 key
, __u64 ptr
)
1237 struct nilfs_btree_path
*path
;
1238 struct nilfs_bmap_stats stats
;
1241 path
= nilfs_btree_alloc_path();
1245 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
,
1246 NILFS_BTREE_LEVEL_NODE_MIN
, 0);
1247 if (ret
!= -ENOENT
) {
1253 ret
= nilfs_btree_prepare_insert(btree
, path
, &level
, key
, ptr
, &stats
);
1256 nilfs_btree_commit_insert(btree
, path
, level
, key
, ptr
);
1257 nilfs_inode_add_blocks(btree
->b_inode
, stats
.bs_nblocks
);
1260 nilfs_btree_free_path(path
);
1264 static void nilfs_btree_do_delete(struct nilfs_bmap
*btree
,
1265 struct nilfs_btree_path
*path
,
1266 int level
, __u64
*keyp
, __u64
*ptrp
)
1268 struct nilfs_btree_node
*node
;
1271 if (level
< nilfs_btree_height(btree
) - 1) {
1272 node
= nilfs_btree_get_nonroot_node(path
, level
);
1273 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1274 nilfs_btree_node_delete(node
, path
[level
].bp_index
,
1276 if (!buffer_dirty(path
[level
].bp_bh
))
1277 mark_buffer_dirty(path
[level
].bp_bh
);
1278 if (path
[level
].bp_index
== 0)
1279 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1280 nilfs_btree_node_get_key(node
, 0));
1282 node
= nilfs_btree_get_root(btree
);
1283 nilfs_btree_node_delete(node
, path
[level
].bp_index
,
1285 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1289 static void nilfs_btree_borrow_left(struct nilfs_bmap
*btree
,
1290 struct nilfs_btree_path
*path
,
1291 int level
, __u64
*keyp
, __u64
*ptrp
)
1293 struct nilfs_btree_node
*node
, *left
;
1294 int nchildren
, lnchildren
, n
, ncblk
;
1296 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1298 node
= nilfs_btree_get_nonroot_node(path
, level
);
1299 left
= nilfs_btree_get_sib_node(path
, level
);
1300 nchildren
= nilfs_btree_node_get_nchildren(node
);
1301 lnchildren
= nilfs_btree_node_get_nchildren(left
);
1302 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1304 n
= (nchildren
+ lnchildren
) / 2 - nchildren
;
1306 nilfs_btree_node_move_right(left
, node
, n
, ncblk
, ncblk
);
1308 if (!buffer_dirty(path
[level
].bp_bh
))
1309 mark_buffer_dirty(path
[level
].bp_bh
);
1310 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1311 mark_buffer_dirty(path
[level
].bp_sib_bh
);
1313 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1314 nilfs_btree_node_get_key(node
, 0));
1316 brelse(path
[level
].bp_sib_bh
);
1317 path
[level
].bp_sib_bh
= NULL
;
1318 path
[level
].bp_index
+= n
;
1321 static void nilfs_btree_borrow_right(struct nilfs_bmap
*btree
,
1322 struct nilfs_btree_path
*path
,
1323 int level
, __u64
*keyp
, __u64
*ptrp
)
1325 struct nilfs_btree_node
*node
, *right
;
1326 int nchildren
, rnchildren
, n
, ncblk
;
1328 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1330 node
= nilfs_btree_get_nonroot_node(path
, level
);
1331 right
= nilfs_btree_get_sib_node(path
, level
);
1332 nchildren
= nilfs_btree_node_get_nchildren(node
);
1333 rnchildren
= nilfs_btree_node_get_nchildren(right
);
1334 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1336 n
= (nchildren
+ rnchildren
) / 2 - nchildren
;
1338 nilfs_btree_node_move_left(node
, right
, n
, ncblk
, ncblk
);
1340 if (!buffer_dirty(path
[level
].bp_bh
))
1341 mark_buffer_dirty(path
[level
].bp_bh
);
1342 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1343 mark_buffer_dirty(path
[level
].bp_sib_bh
);
1345 path
[level
+ 1].bp_index
++;
1346 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1347 nilfs_btree_node_get_key(right
, 0));
1348 path
[level
+ 1].bp_index
--;
1350 brelse(path
[level
].bp_sib_bh
);
1351 path
[level
].bp_sib_bh
= NULL
;
1354 static void nilfs_btree_concat_left(struct nilfs_bmap
*btree
,
1355 struct nilfs_btree_path
*path
,
1356 int level
, __u64
*keyp
, __u64
*ptrp
)
1358 struct nilfs_btree_node
*node
, *left
;
1361 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1363 node
= nilfs_btree_get_nonroot_node(path
, level
);
1364 left
= nilfs_btree_get_sib_node(path
, level
);
1365 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1367 n
= nilfs_btree_node_get_nchildren(node
);
1369 nilfs_btree_node_move_left(left
, node
, n
, ncblk
, ncblk
);
1371 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1372 mark_buffer_dirty(path
[level
].bp_sib_bh
);
1374 nilfs_btnode_delete(path
[level
].bp_bh
);
1375 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
1376 path
[level
].bp_sib_bh
= NULL
;
1377 path
[level
].bp_index
+= nilfs_btree_node_get_nchildren(left
);
1380 static void nilfs_btree_concat_right(struct nilfs_bmap
*btree
,
1381 struct nilfs_btree_path
*path
,
1382 int level
, __u64
*keyp
, __u64
*ptrp
)
1384 struct nilfs_btree_node
*node
, *right
;
1387 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1389 node
= nilfs_btree_get_nonroot_node(path
, level
);
1390 right
= nilfs_btree_get_sib_node(path
, level
);
1391 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1393 n
= nilfs_btree_node_get_nchildren(right
);
1395 nilfs_btree_node_move_left(node
, right
, n
, ncblk
, ncblk
);
1397 if (!buffer_dirty(path
[level
].bp_bh
))
1398 mark_buffer_dirty(path
[level
].bp_bh
);
1400 nilfs_btnode_delete(path
[level
].bp_sib_bh
);
1401 path
[level
].bp_sib_bh
= NULL
;
1402 path
[level
+ 1].bp_index
++;
1405 static void nilfs_btree_shrink(struct nilfs_bmap
*btree
,
1406 struct nilfs_btree_path
*path
,
1407 int level
, __u64
*keyp
, __u64
*ptrp
)
1409 struct nilfs_btree_node
*root
, *child
;
1412 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1414 root
= nilfs_btree_get_root(btree
);
1415 child
= nilfs_btree_get_nonroot_node(path
, level
);
1416 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1418 nilfs_btree_node_delete(root
, 0, NULL
, NULL
,
1419 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1420 nilfs_btree_node_set_level(root
, level
);
1421 n
= nilfs_btree_node_get_nchildren(child
);
1422 nilfs_btree_node_move_left(root
, child
, n
,
1423 NILFS_BTREE_ROOT_NCHILDREN_MAX
, ncblk
);
1425 nilfs_btnode_delete(path
[level
].bp_bh
);
1426 path
[level
].bp_bh
= NULL
;
1429 static void nilfs_btree_nop(struct nilfs_bmap
*btree
,
1430 struct nilfs_btree_path
*path
,
1431 int level
, __u64
*keyp
, __u64
*ptrp
)
1435 static int nilfs_btree_prepare_delete(struct nilfs_bmap
*btree
,
1436 struct nilfs_btree_path
*path
,
1438 struct nilfs_bmap_stats
*stats
,
1441 struct buffer_head
*bh
;
1442 struct nilfs_btree_node
*node
, *parent
, *sib
;
1444 int pindex
, dindex
, level
, ncmin
, ncmax
, ncblk
, ret
;
1447 stats
->bs_nblocks
= 0;
1448 ncmin
= NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree
));
1449 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1451 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
, dindex
= path
[level
].bp_index
;
1452 level
< nilfs_btree_height(btree
) - 1;
1454 node
= nilfs_btree_get_nonroot_node(path
, level
);
1455 path
[level
].bp_oldreq
.bpr_ptr
=
1456 nilfs_btree_node_get_ptr(node
, dindex
, ncblk
);
1457 ret
= nilfs_bmap_prepare_end_ptr(btree
,
1458 &path
[level
].bp_oldreq
, dat
);
1460 goto err_out_child_node
;
1462 if (nilfs_btree_node_get_nchildren(node
) > ncmin
) {
1463 path
[level
].bp_op
= nilfs_btree_do_delete
;
1464 stats
->bs_nblocks
++;
1468 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1469 pindex
= path
[level
+ 1].bp_index
;
1474 sibptr
= nilfs_btree_node_get_ptr(parent
, pindex
- 1,
1476 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1478 goto err_out_curr_node
;
1479 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1480 if (nilfs_btree_node_get_nchildren(sib
) > ncmin
) {
1481 path
[level
].bp_sib_bh
= bh
;
1482 path
[level
].bp_op
= nilfs_btree_borrow_left
;
1483 stats
->bs_nblocks
++;
1486 path
[level
].bp_sib_bh
= bh
;
1487 path
[level
].bp_op
= nilfs_btree_concat_left
;
1488 stats
->bs_nblocks
++;
1492 nilfs_btree_node_get_nchildren(parent
) - 1) {
1494 sibptr
= nilfs_btree_node_get_ptr(parent
, pindex
+ 1,
1496 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1498 goto err_out_curr_node
;
1499 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1500 if (nilfs_btree_node_get_nchildren(sib
) > ncmin
) {
1501 path
[level
].bp_sib_bh
= bh
;
1502 path
[level
].bp_op
= nilfs_btree_borrow_right
;
1503 stats
->bs_nblocks
++;
1506 path
[level
].bp_sib_bh
= bh
;
1507 path
[level
].bp_op
= nilfs_btree_concat_right
;
1508 stats
->bs_nblocks
++;
1510 * When merging right sibling node
1511 * into the current node, pointer to
1512 * the right sibling node must be
1513 * terminated instead. The adjustment
1514 * below is required for that.
1516 dindex
= pindex
+ 1;
1521 /* the only child of the root node */
1522 WARN_ON(level
!= nilfs_btree_height(btree
) - 2);
1523 if (nilfs_btree_node_get_nchildren(node
) - 1 <=
1524 NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1525 path
[level
].bp_op
= nilfs_btree_shrink
;
1526 stats
->bs_nblocks
+= 2;
1528 path
[level
].bp_op
= nilfs_btree_nop
;
1529 goto shrink_root_child
;
1531 path
[level
].bp_op
= nilfs_btree_do_delete
;
1532 stats
->bs_nblocks
++;
1538 /* child of the root node is deleted */
1539 path
[level
].bp_op
= nilfs_btree_do_delete
;
1540 stats
->bs_nblocks
++;
1543 node
= nilfs_btree_get_root(btree
);
1544 path
[level
].bp_oldreq
.bpr_ptr
=
1545 nilfs_btree_node_get_ptr(node
, dindex
,
1546 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1548 ret
= nilfs_bmap_prepare_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1550 goto err_out_child_node
;
1559 nilfs_bmap_abort_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1561 for (level
--; level
>= NILFS_BTREE_LEVEL_NODE_MIN
; level
--) {
1562 brelse(path
[level
].bp_sib_bh
);
1563 nilfs_bmap_abort_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1566 stats
->bs_nblocks
= 0;
1570 static void nilfs_btree_commit_delete(struct nilfs_bmap
*btree
,
1571 struct nilfs_btree_path
*path
,
1572 int maxlevel
, struct inode
*dat
)
1576 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
; level
<= maxlevel
; level
++) {
1577 nilfs_bmap_commit_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1578 path
[level
].bp_op(btree
, path
, level
, NULL
, NULL
);
1581 if (!nilfs_bmap_dirty(btree
))
1582 nilfs_bmap_set_dirty(btree
);
1585 static int nilfs_btree_delete(struct nilfs_bmap
*btree
, __u64 key
)
1588 struct nilfs_btree_path
*path
;
1589 struct nilfs_bmap_stats stats
;
1593 path
= nilfs_btree_alloc_path();
1597 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
,
1598 NILFS_BTREE_LEVEL_NODE_MIN
, 0);
1603 dat
= NILFS_BMAP_USE_VBN(btree
) ? nilfs_bmap_get_dat(btree
) : NULL
;
1605 ret
= nilfs_btree_prepare_delete(btree
, path
, &level
, &stats
, dat
);
1608 nilfs_btree_commit_delete(btree
, path
, level
, dat
);
1609 nilfs_inode_sub_blocks(btree
->b_inode
, stats
.bs_nblocks
);
1612 nilfs_btree_free_path(path
);
1616 static int nilfs_btree_seek_key(const struct nilfs_bmap
*btree
, __u64 start
,
1619 struct nilfs_btree_path
*path
;
1620 const int minlevel
= NILFS_BTREE_LEVEL_NODE_MIN
;
1623 path
= nilfs_btree_alloc_path();
1627 ret
= nilfs_btree_do_lookup(btree
, path
, start
, NULL
, minlevel
, 0);
1630 else if (ret
== -ENOENT
)
1631 ret
= nilfs_btree_get_next_key(btree
, path
, minlevel
, keyp
);
1633 nilfs_btree_free_path(path
);
1637 static int nilfs_btree_last_key(const struct nilfs_bmap
*btree
, __u64
*keyp
)
1639 struct nilfs_btree_path
*path
;
1642 path
= nilfs_btree_alloc_path();
1646 ret
= nilfs_btree_do_lookup_last(btree
, path
, keyp
, NULL
);
1648 nilfs_btree_free_path(path
);
1653 static int nilfs_btree_check_delete(struct nilfs_bmap
*btree
, __u64 key
)
1655 struct buffer_head
*bh
;
1656 struct nilfs_btree_node
*root
, *node
;
1657 __u64 maxkey
, nextmaxkey
;
1661 root
= nilfs_btree_get_root(btree
);
1662 nchildren
= nilfs_btree_node_get_nchildren(root
);
1663 if (unlikely(nchildren
== 0))
1666 switch (nilfs_btree_height(btree
)) {
1674 ptr
= nilfs_btree_node_get_ptr(root
, nchildren
- 1,
1675 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1676 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
1679 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1680 nchildren
= nilfs_btree_node_get_nchildren(node
);
1686 maxkey
= nilfs_btree_node_get_key(node
, nchildren
- 1);
1687 nextmaxkey
= (nchildren
> 1) ?
1688 nilfs_btree_node_get_key(node
, nchildren
- 2) : 0;
1691 return (maxkey
== key
) && (nextmaxkey
< NILFS_BMAP_LARGE_LOW
);
1694 static int nilfs_btree_gather_data(struct nilfs_bmap
*btree
,
1695 __u64
*keys
, __u64
*ptrs
, int nitems
)
1697 struct buffer_head
*bh
;
1698 struct nilfs_btree_node
*node
, *root
;
1702 int nchildren
, ncmax
, i
, ret
;
1704 root
= nilfs_btree_get_root(btree
);
1705 switch (nilfs_btree_height(btree
)) {
1709 ncmax
= NILFS_BTREE_ROOT_NCHILDREN_MAX
;
1712 nchildren
= nilfs_btree_node_get_nchildren(root
);
1713 WARN_ON(nchildren
> 1);
1714 ptr
= nilfs_btree_node_get_ptr(root
, nchildren
- 1,
1715 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1716 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
1719 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1720 ncmax
= nilfs_btree_nchildren_per_block(btree
);
1727 nchildren
= nilfs_btree_node_get_nchildren(node
);
1728 if (nchildren
< nitems
)
1730 dkeys
= nilfs_btree_node_dkeys(node
);
1731 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
1732 for (i
= 0; i
< nitems
; i
++) {
1733 keys
[i
] = le64_to_cpu(dkeys
[i
]);
1734 ptrs
[i
] = le64_to_cpu(dptrs
[i
]);
1743 nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap
*btree
, __u64 key
,
1744 union nilfs_bmap_ptr_req
*dreq
,
1745 union nilfs_bmap_ptr_req
*nreq
,
1746 struct buffer_head
**bhp
,
1747 struct nilfs_bmap_stats
*stats
)
1749 struct buffer_head
*bh
;
1750 struct inode
*dat
= NULL
;
1753 stats
->bs_nblocks
= 0;
1756 /* cannot find near ptr */
1757 if (NILFS_BMAP_USE_VBN(btree
)) {
1758 dreq
->bpr_ptr
= nilfs_btree_find_target_v(btree
, NULL
, key
);
1759 dat
= nilfs_bmap_get_dat(btree
);
1762 ret
= nilfs_attach_btree_node_cache(&NILFS_BMAP_I(btree
)->vfs_inode
);
1766 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, dreq
, dat
);
1771 stats
->bs_nblocks
++;
1773 nreq
->bpr_ptr
= dreq
->bpr_ptr
+ 1;
1774 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, nreq
, dat
);
1778 ret
= nilfs_btree_get_new_block(btree
, nreq
->bpr_ptr
, &bh
);
1783 stats
->bs_nblocks
++;
1791 nilfs_bmap_abort_alloc_ptr(btree
, nreq
, dat
);
1793 nilfs_bmap_abort_alloc_ptr(btree
, dreq
, dat
);
1794 stats
->bs_nblocks
= 0;
1800 nilfs_btree_commit_convert_and_insert(struct nilfs_bmap
*btree
,
1801 __u64 key
, __u64 ptr
,
1802 const __u64
*keys
, const __u64
*ptrs
,
1804 union nilfs_bmap_ptr_req
*dreq
,
1805 union nilfs_bmap_ptr_req
*nreq
,
1806 struct buffer_head
*bh
)
1808 struct nilfs_btree_node
*node
;
1813 /* free resources */
1814 if (btree
->b_ops
->bop_clear
!= NULL
)
1815 btree
->b_ops
->bop_clear(btree
);
1817 /* ptr must be a pointer to a buffer head. */
1818 set_buffer_nilfs_volatile((struct buffer_head
*)((unsigned long)ptr
));
1820 /* convert and insert */
1821 dat
= NILFS_BMAP_USE_VBN(btree
) ? nilfs_bmap_get_dat(btree
) : NULL
;
1822 __nilfs_btree_init(btree
);
1824 nilfs_bmap_commit_alloc_ptr(btree
, dreq
, dat
);
1825 nilfs_bmap_commit_alloc_ptr(btree
, nreq
, dat
);
1827 /* create child node at level 1 */
1828 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1829 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1830 nilfs_btree_node_init(node
, 0, 1, n
, ncblk
, keys
, ptrs
);
1831 nilfs_btree_node_insert(node
, n
, key
, dreq
->bpr_ptr
, ncblk
);
1832 if (!buffer_dirty(bh
))
1833 mark_buffer_dirty(bh
);
1834 if (!nilfs_bmap_dirty(btree
))
1835 nilfs_bmap_set_dirty(btree
);
1839 /* create root node at level 2 */
1840 node
= nilfs_btree_get_root(btree
);
1841 tmpptr
= nreq
->bpr_ptr
;
1842 nilfs_btree_node_init(node
, NILFS_BTREE_NODE_ROOT
, 2, 1,
1843 NILFS_BTREE_ROOT_NCHILDREN_MAX
,
1846 nilfs_bmap_commit_alloc_ptr(btree
, dreq
, dat
);
1848 /* create root node at level 1 */
1849 node
= nilfs_btree_get_root(btree
);
1850 nilfs_btree_node_init(node
, NILFS_BTREE_NODE_ROOT
, 1, n
,
1851 NILFS_BTREE_ROOT_NCHILDREN_MAX
,
1853 nilfs_btree_node_insert(node
, n
, key
, dreq
->bpr_ptr
,
1854 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1855 if (!nilfs_bmap_dirty(btree
))
1856 nilfs_bmap_set_dirty(btree
);
1859 if (NILFS_BMAP_USE_VBN(btree
))
1860 nilfs_bmap_set_target_v(btree
, key
, dreq
->bpr_ptr
);
1864 * nilfs_btree_convert_and_insert - Convert and insert entries into a B-tree
1865 * @btree: NILFS B-tree structure
1866 * @key: Key of the new entry to be inserted
1867 * @ptr: Pointer (block number) associated with the key to be inserted
1868 * @keys: Array of keys to be inserted in addition to @key
1869 * @ptrs: Array of pointers associated with @keys
1870 * @n: Number of keys and pointers in @keys and @ptrs
1872 * This function is used to insert a new entry specified by @key and @ptr,
1873 * along with additional entries specified by @keys and @ptrs arrays, into a
1875 * It prepares the necessary changes by allocating the required blocks and any
1876 * necessary intermediate nodes. It converts configurations from other forms of
1877 * block mapping (the one that currently exists is direct mapping) to a B-tree.
1879 * Return: 0 on success or a negative error code on failure.
1881 int nilfs_btree_convert_and_insert(struct nilfs_bmap
*btree
,
1882 __u64 key
, __u64 ptr
,
1883 const __u64
*keys
, const __u64
*ptrs
, int n
)
1885 struct buffer_head
*bh
= NULL
;
1886 union nilfs_bmap_ptr_req dreq
, nreq
, *di
, *ni
;
1887 struct nilfs_bmap_stats stats
;
1890 if (n
+ 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1893 } else if ((n
+ 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
1894 nilfs_btree_node_size(btree
))) {
1903 ret
= nilfs_btree_prepare_convert_and_insert(btree
, key
, di
, ni
, &bh
,
1907 nilfs_btree_commit_convert_and_insert(btree
, key
, ptr
, keys
, ptrs
, n
,
1909 nilfs_inode_add_blocks(btree
->b_inode
, stats
.bs_nblocks
);
1913 static int nilfs_btree_propagate_p(struct nilfs_bmap
*btree
,
1914 struct nilfs_btree_path
*path
,
1916 struct buffer_head
*bh
)
1918 while ((++level
< nilfs_btree_height(btree
) - 1) &&
1919 !buffer_dirty(path
[level
].bp_bh
))
1920 mark_buffer_dirty(path
[level
].bp_bh
);
1925 static int nilfs_btree_prepare_update_v(struct nilfs_bmap
*btree
,
1926 struct nilfs_btree_path
*path
,
1927 int level
, struct inode
*dat
)
1929 struct nilfs_btree_node
*parent
;
1932 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1933 path
[level
].bp_oldreq
.bpr_ptr
=
1934 nilfs_btree_node_get_ptr(parent
, path
[level
+ 1].bp_index
,
1936 path
[level
].bp_newreq
.bpr_ptr
= path
[level
].bp_oldreq
.bpr_ptr
+ 1;
1937 ret
= nilfs_dat_prepare_update(dat
, &path
[level
].bp_oldreq
.bpr_req
,
1938 &path
[level
].bp_newreq
.bpr_req
);
1942 if (buffer_nilfs_node(path
[level
].bp_bh
)) {
1943 path
[level
].bp_ctxt
.oldkey
= path
[level
].bp_oldreq
.bpr_ptr
;
1944 path
[level
].bp_ctxt
.newkey
= path
[level
].bp_newreq
.bpr_ptr
;
1945 path
[level
].bp_ctxt
.bh
= path
[level
].bp_bh
;
1946 ret
= nilfs_btnode_prepare_change_key(
1947 NILFS_BMAP_I(btree
)->i_assoc_inode
->i_mapping
,
1948 &path
[level
].bp_ctxt
);
1950 nilfs_dat_abort_update(dat
,
1951 &path
[level
].bp_oldreq
.bpr_req
,
1952 &path
[level
].bp_newreq
.bpr_req
);
1960 static void nilfs_btree_commit_update_v(struct nilfs_bmap
*btree
,
1961 struct nilfs_btree_path
*path
,
1962 int level
, struct inode
*dat
)
1964 struct nilfs_btree_node
*parent
;
1967 nilfs_dat_commit_update(dat
, &path
[level
].bp_oldreq
.bpr_req
,
1968 &path
[level
].bp_newreq
.bpr_req
,
1969 btree
->b_ptr_type
== NILFS_BMAP_PTR_VS
);
1971 if (buffer_nilfs_node(path
[level
].bp_bh
)) {
1972 nilfs_btnode_commit_change_key(
1973 NILFS_BMAP_I(btree
)->i_assoc_inode
->i_mapping
,
1974 &path
[level
].bp_ctxt
);
1975 path
[level
].bp_bh
= path
[level
].bp_ctxt
.bh
;
1977 set_buffer_nilfs_volatile(path
[level
].bp_bh
);
1979 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1980 nilfs_btree_node_set_ptr(parent
, path
[level
+ 1].bp_index
,
1981 path
[level
].bp_newreq
.bpr_ptr
, ncmax
);
1984 static void nilfs_btree_abort_update_v(struct nilfs_bmap
*btree
,
1985 struct nilfs_btree_path
*path
,
1986 int level
, struct inode
*dat
)
1988 nilfs_dat_abort_update(dat
, &path
[level
].bp_oldreq
.bpr_req
,
1989 &path
[level
].bp_newreq
.bpr_req
);
1990 if (buffer_nilfs_node(path
[level
].bp_bh
))
1991 nilfs_btnode_abort_change_key(
1992 NILFS_BMAP_I(btree
)->i_assoc_inode
->i_mapping
,
1993 &path
[level
].bp_ctxt
);
1996 static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap
*btree
,
1997 struct nilfs_btree_path
*path
,
1998 int minlevel
, int *maxlevelp
,
2004 if (!buffer_nilfs_volatile(path
[level
].bp_bh
)) {
2005 ret
= nilfs_btree_prepare_update_v(btree
, path
, level
, dat
);
2009 while ((++level
< nilfs_btree_height(btree
) - 1) &&
2010 !buffer_dirty(path
[level
].bp_bh
)) {
2012 WARN_ON(buffer_nilfs_volatile(path
[level
].bp_bh
));
2013 ret
= nilfs_btree_prepare_update_v(btree
, path
, level
, dat
);
2019 *maxlevelp
= level
- 1;
2024 while (--level
> minlevel
)
2025 nilfs_btree_abort_update_v(btree
, path
, level
, dat
);
2026 if (!buffer_nilfs_volatile(path
[level
].bp_bh
))
2027 nilfs_btree_abort_update_v(btree
, path
, level
, dat
);
2031 static void nilfs_btree_commit_propagate_v(struct nilfs_bmap
*btree
,
2032 struct nilfs_btree_path
*path
,
2033 int minlevel
, int maxlevel
,
2034 struct buffer_head
*bh
,
2039 if (!buffer_nilfs_volatile(path
[minlevel
].bp_bh
))
2040 nilfs_btree_commit_update_v(btree
, path
, minlevel
, dat
);
2042 for (level
= minlevel
+ 1; level
<= maxlevel
; level
++)
2043 nilfs_btree_commit_update_v(btree
, path
, level
, dat
);
2046 static int nilfs_btree_propagate_v(struct nilfs_bmap
*btree
,
2047 struct nilfs_btree_path
*path
,
2048 int level
, struct buffer_head
*bh
)
2050 int maxlevel
= 0, ret
;
2051 struct nilfs_btree_node
*parent
;
2052 struct inode
*dat
= nilfs_bmap_get_dat(btree
);
2057 path
[level
].bp_bh
= bh
;
2058 ret
= nilfs_btree_prepare_propagate_v(btree
, path
, level
, &maxlevel
,
2063 if (buffer_nilfs_volatile(path
[level
].bp_bh
)) {
2064 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
2065 ptr
= nilfs_btree_node_get_ptr(parent
,
2066 path
[level
+ 1].bp_index
,
2068 ret
= nilfs_dat_mark_dirty(dat
, ptr
);
2073 nilfs_btree_commit_propagate_v(btree
, path
, level
, maxlevel
, bh
, dat
);
2076 brelse(path
[level
].bp_bh
);
2077 path
[level
].bp_bh
= NULL
;
2081 static int nilfs_btree_propagate(struct nilfs_bmap
*btree
,
2082 struct buffer_head
*bh
)
2084 struct nilfs_btree_path
*path
;
2085 struct nilfs_btree_node
*node
;
2089 WARN_ON(!buffer_dirty(bh
));
2091 path
= nilfs_btree_alloc_path();
2095 if (buffer_nilfs_node(bh
)) {
2096 node
= (struct nilfs_btree_node
*)bh
->b_data
;
2097 key
= nilfs_btree_node_get_key(node
, 0);
2098 level
= nilfs_btree_node_get_level(node
);
2100 key
= nilfs_bmap_data_get_key(btree
, bh
);
2101 level
= NILFS_BTREE_LEVEL_DATA
;
2104 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
, level
+ 1, 0);
2106 if (unlikely(ret
== -ENOENT
))
2107 nilfs_crit(btree
->b_inode
->i_sb
,
2108 "writing node/leaf block does not appear in b-tree (ino=%lu) at key=%llu, level=%d",
2109 btree
->b_inode
->i_ino
,
2110 (unsigned long long)key
, level
);
2114 ret
= NILFS_BMAP_USE_VBN(btree
) ?
2115 nilfs_btree_propagate_v(btree
, path
, level
, bh
) :
2116 nilfs_btree_propagate_p(btree
, path
, level
, bh
);
2119 nilfs_btree_free_path(path
);
2124 static int nilfs_btree_propagate_gc(struct nilfs_bmap
*btree
,
2125 struct buffer_head
*bh
)
2127 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree
), bh
->b_blocknr
);
2130 static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap
*btree
,
2131 struct list_head
*lists
,
2132 struct buffer_head
*bh
)
2134 struct list_head
*head
;
2135 struct buffer_head
*cbh
;
2136 struct nilfs_btree_node
*node
, *cnode
;
2141 node
= (struct nilfs_btree_node
*)bh
->b_data
;
2142 key
= nilfs_btree_node_get_key(node
, 0);
2143 level
= nilfs_btree_node_get_level(node
);
2144 if (level
< NILFS_BTREE_LEVEL_NODE_MIN
||
2145 level
>= NILFS_BTREE_LEVEL_MAX
) {
2147 nilfs_warn(btree
->b_inode
->i_sb
,
2148 "invalid btree level: %d (key=%llu, ino=%lu, blocknr=%llu)",
2149 level
, (unsigned long long)key
,
2150 btree
->b_inode
->i_ino
,
2151 (unsigned long long)bh
->b_blocknr
);
2155 list_for_each(head
, &lists
[level
]) {
2156 cbh
= list_entry(head
, struct buffer_head
, b_assoc_buffers
);
2157 cnode
= (struct nilfs_btree_node
*)cbh
->b_data
;
2158 ckey
= nilfs_btree_node_get_key(cnode
, 0);
2162 list_add_tail(&bh
->b_assoc_buffers
, head
);
2165 static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap
*btree
,
2166 struct list_head
*listp
)
2168 struct inode
*btnc_inode
= NILFS_BMAP_I(btree
)->i_assoc_inode
;
2169 struct address_space
*btcache
= btnc_inode
->i_mapping
;
2170 struct list_head lists
[NILFS_BTREE_LEVEL_MAX
];
2171 struct folio_batch fbatch
;
2172 struct buffer_head
*bh
, *head
;
2176 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
2177 level
< NILFS_BTREE_LEVEL_MAX
;
2179 INIT_LIST_HEAD(&lists
[level
]);
2181 folio_batch_init(&fbatch
);
2183 while (filemap_get_folios_tag(btcache
, &index
, (pgoff_t
)-1,
2184 PAGECACHE_TAG_DIRTY
, &fbatch
)) {
2185 for (i
= 0; i
< folio_batch_count(&fbatch
); i
++) {
2186 bh
= head
= folio_buffers(fbatch
.folios
[i
]);
2188 if (buffer_dirty(bh
))
2189 nilfs_btree_add_dirty_buffer(btree
,
2191 } while ((bh
= bh
->b_this_page
) != head
);
2193 folio_batch_release(&fbatch
);
2197 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
2198 level
< NILFS_BTREE_LEVEL_MAX
;
2200 list_splice_tail(&lists
[level
], listp
);
2203 static int nilfs_btree_assign_p(struct nilfs_bmap
*btree
,
2204 struct nilfs_btree_path
*path
,
2206 struct buffer_head
**bh
,
2208 union nilfs_binfo
*binfo
)
2210 struct nilfs_btree_node
*parent
;
2215 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
2216 ptr
= nilfs_btree_node_get_ptr(parent
, path
[level
+ 1].bp_index
,
2218 if (buffer_nilfs_node(*bh
)) {
2219 path
[level
].bp_ctxt
.oldkey
= ptr
;
2220 path
[level
].bp_ctxt
.newkey
= blocknr
;
2221 path
[level
].bp_ctxt
.bh
= *bh
;
2222 ret
= nilfs_btnode_prepare_change_key(
2223 NILFS_BMAP_I(btree
)->i_assoc_inode
->i_mapping
,
2224 &path
[level
].bp_ctxt
);
2227 nilfs_btnode_commit_change_key(
2228 NILFS_BMAP_I(btree
)->i_assoc_inode
->i_mapping
,
2229 &path
[level
].bp_ctxt
);
2230 *bh
= path
[level
].bp_ctxt
.bh
;
2233 nilfs_btree_node_set_ptr(parent
, path
[level
+ 1].bp_index
, blocknr
,
2236 key
= nilfs_btree_node_get_key(parent
, path
[level
+ 1].bp_index
);
2237 /* on-disk format */
2238 binfo
->bi_dat
.bi_blkoff
= cpu_to_le64(key
);
2239 binfo
->bi_dat
.bi_level
= level
;
2240 memset(binfo
->bi_dat
.bi_pad
, 0, sizeof(binfo
->bi_dat
.bi_pad
));
2245 static int nilfs_btree_assign_v(struct nilfs_bmap
*btree
,
2246 struct nilfs_btree_path
*path
,
2248 struct buffer_head
**bh
,
2250 union nilfs_binfo
*binfo
)
2252 struct nilfs_btree_node
*parent
;
2253 struct inode
*dat
= nilfs_bmap_get_dat(btree
);
2256 union nilfs_bmap_ptr_req req
;
2259 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
2260 ptr
= nilfs_btree_node_get_ptr(parent
, path
[level
+ 1].bp_index
,
2263 ret
= nilfs_dat_prepare_start(dat
, &req
.bpr_req
);
2266 nilfs_dat_commit_start(dat
, &req
.bpr_req
, blocknr
);
2268 key
= nilfs_btree_node_get_key(parent
, path
[level
+ 1].bp_index
);
2269 /* on-disk format */
2270 binfo
->bi_v
.bi_vblocknr
= cpu_to_le64(ptr
);
2271 binfo
->bi_v
.bi_blkoff
= cpu_to_le64(key
);
2276 static int nilfs_btree_assign(struct nilfs_bmap
*btree
,
2277 struct buffer_head
**bh
,
2279 union nilfs_binfo
*binfo
)
2281 struct nilfs_btree_path
*path
;
2282 struct nilfs_btree_node
*node
;
2286 path
= nilfs_btree_alloc_path();
2290 if (buffer_nilfs_node(*bh
)) {
2291 node
= (struct nilfs_btree_node
*)(*bh
)->b_data
;
2292 key
= nilfs_btree_node_get_key(node
, 0);
2293 level
= nilfs_btree_node_get_level(node
);
2295 key
= nilfs_bmap_data_get_key(btree
, *bh
);
2296 level
= NILFS_BTREE_LEVEL_DATA
;
2299 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
, level
+ 1, 0);
2301 WARN_ON(ret
== -ENOENT
);
2305 ret
= NILFS_BMAP_USE_VBN(btree
) ?
2306 nilfs_btree_assign_v(btree
, path
, level
, bh
, blocknr
, binfo
) :
2307 nilfs_btree_assign_p(btree
, path
, level
, bh
, blocknr
, binfo
);
2310 nilfs_btree_free_path(path
);
2315 static int nilfs_btree_assign_gc(struct nilfs_bmap
*btree
,
2316 struct buffer_head
**bh
,
2318 union nilfs_binfo
*binfo
)
2320 struct nilfs_btree_node
*node
;
2324 ret
= nilfs_dat_move(nilfs_bmap_get_dat(btree
), (*bh
)->b_blocknr
,
2329 if (buffer_nilfs_node(*bh
)) {
2330 node
= (struct nilfs_btree_node
*)(*bh
)->b_data
;
2331 key
= nilfs_btree_node_get_key(node
, 0);
2333 key
= nilfs_bmap_data_get_key(btree
, *bh
);
2335 /* on-disk format */
2336 binfo
->bi_v
.bi_vblocknr
= cpu_to_le64((*bh
)->b_blocknr
);
2337 binfo
->bi_v
.bi_blkoff
= cpu_to_le64(key
);
2342 static int nilfs_btree_mark(struct nilfs_bmap
*btree
, __u64 key
, int level
)
2344 struct buffer_head
*bh
;
2345 struct nilfs_btree_path
*path
;
2349 path
= nilfs_btree_alloc_path();
2353 ret
= nilfs_btree_do_lookup(btree
, path
, key
, &ptr
, level
+ 1, 0);
2355 WARN_ON(ret
== -ENOENT
);
2358 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
2360 WARN_ON(ret
== -ENOENT
);
2364 if (!buffer_dirty(bh
))
2365 mark_buffer_dirty(bh
);
2367 if (!nilfs_bmap_dirty(btree
))
2368 nilfs_bmap_set_dirty(btree
);
2371 nilfs_btree_free_path(path
);
2375 static const struct nilfs_bmap_operations nilfs_btree_ops
= {
2376 .bop_lookup
= nilfs_btree_lookup
,
2377 .bop_lookup_contig
= nilfs_btree_lookup_contig
,
2378 .bop_insert
= nilfs_btree_insert
,
2379 .bop_delete
= nilfs_btree_delete
,
2382 .bop_propagate
= nilfs_btree_propagate
,
2384 .bop_lookup_dirty_buffers
= nilfs_btree_lookup_dirty_buffers
,
2386 .bop_assign
= nilfs_btree_assign
,
2387 .bop_mark
= nilfs_btree_mark
,
2389 .bop_seek_key
= nilfs_btree_seek_key
,
2390 .bop_last_key
= nilfs_btree_last_key
,
2392 .bop_check_insert
= NULL
,
2393 .bop_check_delete
= nilfs_btree_check_delete
,
2394 .bop_gather_data
= nilfs_btree_gather_data
,
2397 static const struct nilfs_bmap_operations nilfs_btree_ops_gc
= {
2399 .bop_lookup_contig
= NULL
,
2404 .bop_propagate
= nilfs_btree_propagate_gc
,
2406 .bop_lookup_dirty_buffers
= nilfs_btree_lookup_dirty_buffers
,
2408 .bop_assign
= nilfs_btree_assign_gc
,
2411 .bop_seek_key
= NULL
,
2412 .bop_last_key
= NULL
,
2414 .bop_check_insert
= NULL
,
2415 .bop_check_delete
= NULL
,
2416 .bop_gather_data
= NULL
,
2419 static void __nilfs_btree_init(struct nilfs_bmap
*bmap
)
2421 bmap
->b_ops
= &nilfs_btree_ops
;
2422 bmap
->b_nchildren_per_block
=
2423 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap
));
2426 int nilfs_btree_init(struct nilfs_bmap
*bmap
)
2430 __nilfs_btree_init(bmap
);
2432 if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap
), bmap
->b_inode
))
2435 ret
= nilfs_attach_btree_node_cache(
2436 &NILFS_BMAP_I(bmap
)->vfs_inode
);
2441 void nilfs_btree_init_gc(struct nilfs_bmap
*bmap
)
2443 bmap
->b_ops
= &nilfs_btree_ops_gc
;
2444 bmap
->b_nchildren_per_block
=
2445 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap
));