2 * btree.c - NILFS B-tree.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * Written by Koji Sato.
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/pagevec.h>
30 static void __nilfs_btree_init(struct nilfs_bmap
*bmap
);
32 static struct nilfs_btree_path
*nilfs_btree_alloc_path(void)
34 struct nilfs_btree_path
*path
;
35 int level
= NILFS_BTREE_LEVEL_DATA
;
37 path
= kmem_cache_alloc(nilfs_btree_path_cache
, GFP_NOFS
);
41 for (; level
< NILFS_BTREE_LEVEL_MAX
; level
++) {
42 path
[level
].bp_bh
= NULL
;
43 path
[level
].bp_sib_bh
= NULL
;
44 path
[level
].bp_index
= 0;
45 path
[level
].bp_oldreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
46 path
[level
].bp_newreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
47 path
[level
].bp_op
= NULL
;
54 static void nilfs_btree_free_path(struct nilfs_btree_path
*path
)
56 int level
= NILFS_BTREE_LEVEL_DATA
;
58 for (; level
< NILFS_BTREE_LEVEL_MAX
; level
++)
59 brelse(path
[level
].bp_bh
);
61 kmem_cache_free(nilfs_btree_path_cache
, path
);
65 * B-tree node operations
67 static int nilfs_btree_get_new_block(const struct nilfs_bmap
*btree
,
68 __u64 ptr
, struct buffer_head
**bhp
)
70 struct address_space
*btnc
= &NILFS_BMAP_I(btree
)->i_btnode_cache
;
71 struct buffer_head
*bh
;
73 bh
= nilfs_btnode_create_block(btnc
, ptr
);
77 set_buffer_nilfs_volatile(bh
);
82 static int nilfs_btree_node_get_flags(const struct nilfs_btree_node
*node
)
84 return node
->bn_flags
;
88 nilfs_btree_node_set_flags(struct nilfs_btree_node
*node
, int flags
)
90 node
->bn_flags
= flags
;
93 static int nilfs_btree_node_root(const struct nilfs_btree_node
*node
)
95 return nilfs_btree_node_get_flags(node
) & NILFS_BTREE_NODE_ROOT
;
98 static int nilfs_btree_node_get_level(const struct nilfs_btree_node
*node
)
100 return node
->bn_level
;
104 nilfs_btree_node_set_level(struct nilfs_btree_node
*node
, int level
)
106 node
->bn_level
= level
;
109 static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node
*node
)
111 return le16_to_cpu(node
->bn_nchildren
);
115 nilfs_btree_node_set_nchildren(struct nilfs_btree_node
*node
, int nchildren
)
117 node
->bn_nchildren
= cpu_to_le16(nchildren
);
120 static int nilfs_btree_node_size(const struct nilfs_bmap
*btree
)
122 return 1 << btree
->b_inode
->i_blkbits
;
125 static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap
*btree
)
127 return btree
->b_nchildren_per_block
;
131 nilfs_btree_node_dkeys(const struct nilfs_btree_node
*node
)
133 return (__le64
*)((char *)(node
+ 1) +
134 (nilfs_btree_node_root(node
) ?
135 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE
));
139 nilfs_btree_node_dptrs(const struct nilfs_btree_node
*node
, int ncmax
)
141 return (__le64
*)(nilfs_btree_node_dkeys(node
) + ncmax
);
145 nilfs_btree_node_get_key(const struct nilfs_btree_node
*node
, int index
)
147 return le64_to_cpu(*(nilfs_btree_node_dkeys(node
) + index
));
151 nilfs_btree_node_set_key(struct nilfs_btree_node
*node
, int index
, __u64 key
)
153 *(nilfs_btree_node_dkeys(node
) + index
) = cpu_to_le64(key
);
157 nilfs_btree_node_get_ptr(const struct nilfs_btree_node
*node
, int index
,
160 return le64_to_cpu(*(nilfs_btree_node_dptrs(node
, ncmax
) + index
));
164 nilfs_btree_node_set_ptr(struct nilfs_btree_node
*node
, int index
, __u64 ptr
,
167 *(nilfs_btree_node_dptrs(node
, ncmax
) + index
) = cpu_to_le64(ptr
);
170 static void nilfs_btree_node_init(struct nilfs_btree_node
*node
, int flags
,
171 int level
, int nchildren
, int ncmax
,
172 const __u64
*keys
, const __u64
*ptrs
)
178 nilfs_btree_node_set_flags(node
, flags
);
179 nilfs_btree_node_set_level(node
, level
);
180 nilfs_btree_node_set_nchildren(node
, nchildren
);
182 dkeys
= nilfs_btree_node_dkeys(node
);
183 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
184 for (i
= 0; i
< nchildren
; i
++) {
185 dkeys
[i
] = cpu_to_le64(keys
[i
]);
186 dptrs
[i
] = cpu_to_le64(ptrs
[i
]);
190 /* Assume the buffer heads corresponding to left and right are locked. */
191 static void nilfs_btree_node_move_left(struct nilfs_btree_node
*left
,
192 struct nilfs_btree_node
*right
,
193 int n
, int lncmax
, int rncmax
)
195 __le64
*ldkeys
, *rdkeys
;
196 __le64
*ldptrs
, *rdptrs
;
197 int lnchildren
, rnchildren
;
199 ldkeys
= nilfs_btree_node_dkeys(left
);
200 ldptrs
= nilfs_btree_node_dptrs(left
, lncmax
);
201 lnchildren
= nilfs_btree_node_get_nchildren(left
);
203 rdkeys
= nilfs_btree_node_dkeys(right
);
204 rdptrs
= nilfs_btree_node_dptrs(right
, rncmax
);
205 rnchildren
= nilfs_btree_node_get_nchildren(right
);
207 memcpy(ldkeys
+ lnchildren
, rdkeys
, n
* sizeof(*rdkeys
));
208 memcpy(ldptrs
+ lnchildren
, rdptrs
, n
* sizeof(*rdptrs
));
209 memmove(rdkeys
, rdkeys
+ n
, (rnchildren
- n
) * sizeof(*rdkeys
));
210 memmove(rdptrs
, rdptrs
+ n
, (rnchildren
- n
) * sizeof(*rdptrs
));
214 nilfs_btree_node_set_nchildren(left
, lnchildren
);
215 nilfs_btree_node_set_nchildren(right
, rnchildren
);
218 /* Assume that the buffer heads corresponding to left and right are locked. */
219 static void nilfs_btree_node_move_right(struct nilfs_btree_node
*left
,
220 struct nilfs_btree_node
*right
,
221 int n
, int lncmax
, int rncmax
)
223 __le64
*ldkeys
, *rdkeys
;
224 __le64
*ldptrs
, *rdptrs
;
225 int lnchildren
, rnchildren
;
227 ldkeys
= nilfs_btree_node_dkeys(left
);
228 ldptrs
= nilfs_btree_node_dptrs(left
, lncmax
);
229 lnchildren
= nilfs_btree_node_get_nchildren(left
);
231 rdkeys
= nilfs_btree_node_dkeys(right
);
232 rdptrs
= nilfs_btree_node_dptrs(right
, rncmax
);
233 rnchildren
= nilfs_btree_node_get_nchildren(right
);
235 memmove(rdkeys
+ n
, rdkeys
, rnchildren
* sizeof(*rdkeys
));
236 memmove(rdptrs
+ n
, rdptrs
, rnchildren
* sizeof(*rdptrs
));
237 memcpy(rdkeys
, ldkeys
+ lnchildren
- n
, n
* sizeof(*rdkeys
));
238 memcpy(rdptrs
, ldptrs
+ lnchildren
- n
, n
* sizeof(*rdptrs
));
242 nilfs_btree_node_set_nchildren(left
, lnchildren
);
243 nilfs_btree_node_set_nchildren(right
, rnchildren
);
246 /* Assume that the buffer head corresponding to node is locked. */
247 static void nilfs_btree_node_insert(struct nilfs_btree_node
*node
, int index
,
248 __u64 key
, __u64 ptr
, int ncmax
)
254 dkeys
= nilfs_btree_node_dkeys(node
);
255 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
256 nchildren
= nilfs_btree_node_get_nchildren(node
);
257 if (index
< nchildren
) {
258 memmove(dkeys
+ index
+ 1, dkeys
+ index
,
259 (nchildren
- index
) * sizeof(*dkeys
));
260 memmove(dptrs
+ index
+ 1, dptrs
+ index
,
261 (nchildren
- index
) * sizeof(*dptrs
));
263 dkeys
[index
] = cpu_to_le64(key
);
264 dptrs
[index
] = cpu_to_le64(ptr
);
266 nilfs_btree_node_set_nchildren(node
, nchildren
);
269 /* Assume that the buffer head corresponding to node is locked. */
270 static void nilfs_btree_node_delete(struct nilfs_btree_node
*node
, int index
,
271 __u64
*keyp
, __u64
*ptrp
, int ncmax
)
279 dkeys
= nilfs_btree_node_dkeys(node
);
280 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
281 key
= le64_to_cpu(dkeys
[index
]);
282 ptr
= le64_to_cpu(dptrs
[index
]);
283 nchildren
= nilfs_btree_node_get_nchildren(node
);
289 if (index
< nchildren
- 1) {
290 memmove(dkeys
+ index
, dkeys
+ index
+ 1,
291 (nchildren
- index
- 1) * sizeof(*dkeys
));
292 memmove(dptrs
+ index
, dptrs
+ index
+ 1,
293 (nchildren
- index
- 1) * sizeof(*dptrs
));
296 nilfs_btree_node_set_nchildren(node
, nchildren
);
299 static int nilfs_btree_node_lookup(const struct nilfs_btree_node
*node
,
300 __u64 key
, int *indexp
)
303 int index
, low
, high
, s
;
307 high
= nilfs_btree_node_get_nchildren(node
) - 1;
310 while (low
<= high
) {
311 index
= (low
+ high
) / 2;
312 nkey
= nilfs_btree_node_get_key(node
, index
);
316 } else if (nkey
< key
) {
326 if (nilfs_btree_node_get_level(node
) > NILFS_BTREE_LEVEL_NODE_MIN
) {
327 if (s
> 0 && index
> 0)
339 * nilfs_btree_node_broken - verify consistency of btree node
340 * @node: btree node block to be examined
341 * @size: node size (in bytes)
342 * @blocknr: block number
344 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
346 static int nilfs_btree_node_broken(const struct nilfs_btree_node
*node
,
347 size_t size
, sector_t blocknr
)
349 int level
, flags
, nchildren
;
352 level
= nilfs_btree_node_get_level(node
);
353 flags
= nilfs_btree_node_get_flags(node
);
354 nchildren
= nilfs_btree_node_get_nchildren(node
);
356 if (unlikely(level
< NILFS_BTREE_LEVEL_NODE_MIN
||
357 level
>= NILFS_BTREE_LEVEL_MAX
||
358 (flags
& NILFS_BTREE_NODE_ROOT
) ||
360 nchildren
> NILFS_BTREE_NODE_NCHILDREN_MAX(size
))) {
361 printk(KERN_CRIT
"NILFS: bad btree node (blocknr=%llu): "
362 "level = %d, flags = 0x%x, nchildren = %d\n",
363 (unsigned long long)blocknr
, level
, flags
, nchildren
);
370 * nilfs_btree_root_broken - verify consistency of btree root node
371 * @node: btree root node to be examined
374 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
376 static int nilfs_btree_root_broken(const struct nilfs_btree_node
*node
,
379 int level
, flags
, nchildren
;
382 level
= nilfs_btree_node_get_level(node
);
383 flags
= nilfs_btree_node_get_flags(node
);
384 nchildren
= nilfs_btree_node_get_nchildren(node
);
386 if (unlikely(level
< NILFS_BTREE_LEVEL_NODE_MIN
||
387 level
>= NILFS_BTREE_LEVEL_MAX
||
389 nchildren
> NILFS_BTREE_ROOT_NCHILDREN_MAX
)) {
390 pr_crit("NILFS: bad btree root (inode number=%lu): level = %d, flags = 0x%x, nchildren = %d\n",
391 ino
, level
, flags
, nchildren
);
397 int nilfs_btree_broken_node_block(struct buffer_head
*bh
)
401 if (buffer_nilfs_checked(bh
))
404 ret
= nilfs_btree_node_broken((struct nilfs_btree_node
*)bh
->b_data
,
405 bh
->b_size
, bh
->b_blocknr
);
407 set_buffer_nilfs_checked(bh
);
411 static struct nilfs_btree_node
*
412 nilfs_btree_get_root(const struct nilfs_bmap
*btree
)
414 return (struct nilfs_btree_node
*)btree
->b_u
.u_data
;
417 static struct nilfs_btree_node
*
418 nilfs_btree_get_nonroot_node(const struct nilfs_btree_path
*path
, int level
)
420 return (struct nilfs_btree_node
*)path
[level
].bp_bh
->b_data
;
423 static struct nilfs_btree_node
*
424 nilfs_btree_get_sib_node(const struct nilfs_btree_path
*path
, int level
)
426 return (struct nilfs_btree_node
*)path
[level
].bp_sib_bh
->b_data
;
429 static int nilfs_btree_height(const struct nilfs_bmap
*btree
)
431 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree
)) + 1;
434 static struct nilfs_btree_node
*
435 nilfs_btree_get_node(const struct nilfs_bmap
*btree
,
436 const struct nilfs_btree_path
*path
,
437 int level
, int *ncmaxp
)
439 struct nilfs_btree_node
*node
;
441 if (level
== nilfs_btree_height(btree
) - 1) {
442 node
= nilfs_btree_get_root(btree
);
443 *ncmaxp
= NILFS_BTREE_ROOT_NCHILDREN_MAX
;
445 node
= nilfs_btree_get_nonroot_node(path
, level
);
446 *ncmaxp
= nilfs_btree_nchildren_per_block(btree
);
452 nilfs_btree_bad_node(struct nilfs_btree_node
*node
, int level
)
454 if (unlikely(nilfs_btree_node_get_level(node
) != level
)) {
456 printk(KERN_CRIT
"NILFS: btree level mismatch: %d != %d\n",
457 nilfs_btree_node_get_level(node
), level
);
463 struct nilfs_btree_readahead_info
{
464 struct nilfs_btree_node
*node
; /* parent node */
465 int max_ra_blocks
; /* max nof blocks to read ahead */
466 int index
; /* current index on the parent node */
467 int ncmax
; /* nof children in the parent node */
470 static int __nilfs_btree_get_block(const struct nilfs_bmap
*btree
, __u64 ptr
,
471 struct buffer_head
**bhp
,
472 const struct nilfs_btree_readahead_info
*ra
)
474 struct address_space
*btnc
= &NILFS_BMAP_I(btree
)->i_btnode_cache
;
475 struct buffer_head
*bh
, *ra_bh
;
476 sector_t submit_ptr
= 0;
479 ret
= nilfs_btnode_submit_block(btnc
, ptr
, 0, REQ_OP_READ
, 0, &bh
,
491 /* read ahead sibling nodes */
492 for (n
= ra
->max_ra_blocks
, i
= ra
->index
+ 1;
493 n
> 0 && i
< ra
->ncmax
; n
--, i
++) {
494 ptr2
= nilfs_btree_node_get_ptr(ra
->node
, i
, ra
->ncmax
);
496 ret
= nilfs_btnode_submit_block(btnc
, ptr2
, 0,
497 REQ_OP_READ
, REQ_RAHEAD
,
498 &ra_bh
, &submit_ptr
);
499 if (likely(!ret
|| ret
== -EEXIST
))
501 else if (ret
!= -EBUSY
)
503 if (!buffer_locked(bh
))
511 if (!buffer_uptodate(bh
)) {
517 if (nilfs_btree_broken_node_block(bh
)) {
518 clear_buffer_uptodate(bh
);
527 static int nilfs_btree_get_block(const struct nilfs_bmap
*btree
, __u64 ptr
,
528 struct buffer_head
**bhp
)
530 return __nilfs_btree_get_block(btree
, ptr
, bhp
, NULL
);
533 static int nilfs_btree_do_lookup(const struct nilfs_bmap
*btree
,
534 struct nilfs_btree_path
*path
,
535 __u64 key
, __u64
*ptrp
, int minlevel
,
538 struct nilfs_btree_node
*node
;
539 struct nilfs_btree_readahead_info p
, *ra
;
541 int level
, index
, found
, ncmax
, ret
;
543 node
= nilfs_btree_get_root(btree
);
544 level
= nilfs_btree_node_get_level(node
);
545 if (level
< minlevel
|| nilfs_btree_node_get_nchildren(node
) <= 0)
548 found
= nilfs_btree_node_lookup(node
, key
, &index
);
549 ptr
= nilfs_btree_node_get_ptr(node
, index
,
550 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
551 path
[level
].bp_bh
= NULL
;
552 path
[level
].bp_index
= index
;
554 ncmax
= nilfs_btree_nchildren_per_block(btree
);
556 while (--level
>= minlevel
) {
558 if (level
== NILFS_BTREE_LEVEL_NODE_MIN
&& readahead
) {
559 p
.node
= nilfs_btree_get_node(btree
, path
, level
+ 1,
565 ret
= __nilfs_btree_get_block(btree
, ptr
, &path
[level
].bp_bh
,
570 node
= nilfs_btree_get_nonroot_node(path
, level
);
571 if (nilfs_btree_bad_node(node
, level
))
574 found
= nilfs_btree_node_lookup(node
, key
, &index
);
578 ptr
= nilfs_btree_node_get_ptr(node
, index
, ncmax
);
580 WARN_ON(found
|| level
!= NILFS_BTREE_LEVEL_NODE_MIN
);
582 ptr
= NILFS_BMAP_INVALID_PTR
;
584 path
[level
].bp_index
= index
;
595 static int nilfs_btree_do_lookup_last(const struct nilfs_bmap
*btree
,
596 struct nilfs_btree_path
*path
,
597 __u64
*keyp
, __u64
*ptrp
)
599 struct nilfs_btree_node
*node
;
601 int index
, level
, ncmax
, ret
;
603 node
= nilfs_btree_get_root(btree
);
604 index
= nilfs_btree_node_get_nchildren(node
) - 1;
607 level
= nilfs_btree_node_get_level(node
);
608 ptr
= nilfs_btree_node_get_ptr(node
, index
,
609 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
610 path
[level
].bp_bh
= NULL
;
611 path
[level
].bp_index
= index
;
612 ncmax
= nilfs_btree_nchildren_per_block(btree
);
614 for (level
--; level
> 0; level
--) {
615 ret
= nilfs_btree_get_block(btree
, ptr
, &path
[level
].bp_bh
);
618 node
= nilfs_btree_get_nonroot_node(path
, level
);
619 if (nilfs_btree_bad_node(node
, level
))
621 index
= nilfs_btree_node_get_nchildren(node
) - 1;
622 ptr
= nilfs_btree_node_get_ptr(node
, index
, ncmax
);
623 path
[level
].bp_index
= index
;
627 *keyp
= nilfs_btree_node_get_key(node
, index
);
635 * nilfs_btree_get_next_key - get next valid key from btree path array
636 * @btree: bmap struct of btree
637 * @path: array of nilfs_btree_path struct
638 * @minlevel: start level
639 * @nextkey: place to store the next valid key
641 * Return Value: If a next key was found, 0 is returned. Otherwise,
642 * -ENOENT is returned.
644 static int nilfs_btree_get_next_key(const struct nilfs_bmap
*btree
,
645 const struct nilfs_btree_path
*path
,
646 int minlevel
, __u64
*nextkey
)
648 struct nilfs_btree_node
*node
;
649 int maxlevel
= nilfs_btree_height(btree
) - 1;
650 int index
, next_adj
, level
;
652 /* Next index is already set to bp_index for leaf nodes. */
654 for (level
= minlevel
; level
<= maxlevel
; level
++) {
655 if (level
== maxlevel
)
656 node
= nilfs_btree_get_root(btree
);
658 node
= nilfs_btree_get_nonroot_node(path
, level
);
660 index
= path
[level
].bp_index
+ next_adj
;
661 if (index
< nilfs_btree_node_get_nchildren(node
)) {
662 /* Next key is in this node */
663 *nextkey
= nilfs_btree_node_get_key(node
, index
);
666 /* For non-leaf nodes, next index is stored at bp_index + 1. */
672 static int nilfs_btree_lookup(const struct nilfs_bmap
*btree
,
673 __u64 key
, int level
, __u64
*ptrp
)
675 struct nilfs_btree_path
*path
;
678 path
= nilfs_btree_alloc_path();
682 ret
= nilfs_btree_do_lookup(btree
, path
, key
, ptrp
, level
, 0);
684 nilfs_btree_free_path(path
);
689 static int nilfs_btree_lookup_contig(const struct nilfs_bmap
*btree
,
690 __u64 key
, __u64
*ptrp
,
691 unsigned int maxblocks
)
693 struct nilfs_btree_path
*path
;
694 struct nilfs_btree_node
*node
;
695 struct inode
*dat
= NULL
;
698 int level
= NILFS_BTREE_LEVEL_NODE_MIN
;
699 int ret
, cnt
, index
, maxlevel
, ncmax
;
700 struct nilfs_btree_readahead_info p
;
702 path
= nilfs_btree_alloc_path();
706 ret
= nilfs_btree_do_lookup(btree
, path
, key
, &ptr
, level
, 1);
710 if (NILFS_BMAP_USE_VBN(btree
)) {
711 dat
= nilfs_bmap_get_dat(btree
);
712 ret
= nilfs_dat_translate(dat
, ptr
, &blocknr
);
718 if (cnt
== maxblocks
)
721 maxlevel
= nilfs_btree_height(btree
) - 1;
722 node
= nilfs_btree_get_node(btree
, path
, level
, &ncmax
);
723 index
= path
[level
].bp_index
+ 1;
725 while (index
< nilfs_btree_node_get_nchildren(node
)) {
726 if (nilfs_btree_node_get_key(node
, index
) !=
729 ptr2
= nilfs_btree_node_get_ptr(node
, index
, ncmax
);
731 ret
= nilfs_dat_translate(dat
, ptr2
, &blocknr
);
736 if (ptr2
!= ptr
+ cnt
|| ++cnt
== maxblocks
)
741 if (level
== maxlevel
)
744 /* look-up right sibling node */
745 p
.node
= nilfs_btree_get_node(btree
, path
, level
+ 1, &p
.ncmax
);
746 p
.index
= path
[level
+ 1].bp_index
+ 1;
748 if (p
.index
>= nilfs_btree_node_get_nchildren(p
.node
) ||
749 nilfs_btree_node_get_key(p
.node
, p
.index
) != key
+ cnt
)
751 ptr2
= nilfs_btree_node_get_ptr(p
.node
, p
.index
, p
.ncmax
);
752 path
[level
+ 1].bp_index
= p
.index
;
754 brelse(path
[level
].bp_bh
);
755 path
[level
].bp_bh
= NULL
;
757 ret
= __nilfs_btree_get_block(btree
, ptr2
, &path
[level
].bp_bh
,
761 node
= nilfs_btree_get_nonroot_node(path
, level
);
762 ncmax
= nilfs_btree_nchildren_per_block(btree
);
764 path
[level
].bp_index
= index
;
770 nilfs_btree_free_path(path
);
774 static void nilfs_btree_promote_key(struct nilfs_bmap
*btree
,
775 struct nilfs_btree_path
*path
,
776 int level
, __u64 key
)
778 if (level
< nilfs_btree_height(btree
) - 1) {
780 nilfs_btree_node_set_key(
781 nilfs_btree_get_nonroot_node(path
, level
),
782 path
[level
].bp_index
, key
);
783 if (!buffer_dirty(path
[level
].bp_bh
))
784 mark_buffer_dirty(path
[level
].bp_bh
);
785 } while ((path
[level
].bp_index
== 0) &&
786 (++level
< nilfs_btree_height(btree
) - 1));
790 if (level
== nilfs_btree_height(btree
) - 1) {
791 nilfs_btree_node_set_key(nilfs_btree_get_root(btree
),
792 path
[level
].bp_index
, key
);
796 static void nilfs_btree_do_insert(struct nilfs_bmap
*btree
,
797 struct nilfs_btree_path
*path
,
798 int level
, __u64
*keyp
, __u64
*ptrp
)
800 struct nilfs_btree_node
*node
;
803 if (level
< nilfs_btree_height(btree
) - 1) {
804 node
= nilfs_btree_get_nonroot_node(path
, level
);
805 ncblk
= nilfs_btree_nchildren_per_block(btree
);
806 nilfs_btree_node_insert(node
, path
[level
].bp_index
,
807 *keyp
, *ptrp
, ncblk
);
808 if (!buffer_dirty(path
[level
].bp_bh
))
809 mark_buffer_dirty(path
[level
].bp_bh
);
811 if (path
[level
].bp_index
== 0)
812 nilfs_btree_promote_key(btree
, path
, level
+ 1,
813 nilfs_btree_node_get_key(node
,
816 node
= nilfs_btree_get_root(btree
);
817 nilfs_btree_node_insert(node
, path
[level
].bp_index
,
819 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
823 static void nilfs_btree_carry_left(struct nilfs_bmap
*btree
,
824 struct nilfs_btree_path
*path
,
825 int level
, __u64
*keyp
, __u64
*ptrp
)
827 struct nilfs_btree_node
*node
, *left
;
828 int nchildren
, lnchildren
, n
, move
, ncblk
;
830 node
= nilfs_btree_get_nonroot_node(path
, level
);
831 left
= nilfs_btree_get_sib_node(path
, level
);
832 nchildren
= nilfs_btree_node_get_nchildren(node
);
833 lnchildren
= nilfs_btree_node_get_nchildren(left
);
834 ncblk
= nilfs_btree_nchildren_per_block(btree
);
837 n
= (nchildren
+ lnchildren
+ 1) / 2 - lnchildren
;
838 if (n
> path
[level
].bp_index
) {
839 /* move insert point */
844 nilfs_btree_node_move_left(left
, node
, n
, ncblk
, ncblk
);
846 if (!buffer_dirty(path
[level
].bp_bh
))
847 mark_buffer_dirty(path
[level
].bp_bh
);
848 if (!buffer_dirty(path
[level
].bp_sib_bh
))
849 mark_buffer_dirty(path
[level
].bp_sib_bh
);
851 nilfs_btree_promote_key(btree
, path
, level
+ 1,
852 nilfs_btree_node_get_key(node
, 0));
855 brelse(path
[level
].bp_bh
);
856 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
857 path
[level
].bp_sib_bh
= NULL
;
858 path
[level
].bp_index
+= lnchildren
;
859 path
[level
+ 1].bp_index
--;
861 brelse(path
[level
].bp_sib_bh
);
862 path
[level
].bp_sib_bh
= NULL
;
863 path
[level
].bp_index
-= n
;
866 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
869 static void nilfs_btree_carry_right(struct nilfs_bmap
*btree
,
870 struct nilfs_btree_path
*path
,
871 int level
, __u64
*keyp
, __u64
*ptrp
)
873 struct nilfs_btree_node
*node
, *right
;
874 int nchildren
, rnchildren
, n
, move
, ncblk
;
876 node
= nilfs_btree_get_nonroot_node(path
, level
);
877 right
= nilfs_btree_get_sib_node(path
, level
);
878 nchildren
= nilfs_btree_node_get_nchildren(node
);
879 rnchildren
= nilfs_btree_node_get_nchildren(right
);
880 ncblk
= nilfs_btree_nchildren_per_block(btree
);
883 n
= (nchildren
+ rnchildren
+ 1) / 2 - rnchildren
;
884 if (n
> nchildren
- path
[level
].bp_index
) {
885 /* move insert point */
890 nilfs_btree_node_move_right(node
, right
, n
, ncblk
, ncblk
);
892 if (!buffer_dirty(path
[level
].bp_bh
))
893 mark_buffer_dirty(path
[level
].bp_bh
);
894 if (!buffer_dirty(path
[level
].bp_sib_bh
))
895 mark_buffer_dirty(path
[level
].bp_sib_bh
);
897 path
[level
+ 1].bp_index
++;
898 nilfs_btree_promote_key(btree
, path
, level
+ 1,
899 nilfs_btree_node_get_key(right
, 0));
900 path
[level
+ 1].bp_index
--;
903 brelse(path
[level
].bp_bh
);
904 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
905 path
[level
].bp_sib_bh
= NULL
;
906 path
[level
].bp_index
-= nilfs_btree_node_get_nchildren(node
);
907 path
[level
+ 1].bp_index
++;
909 brelse(path
[level
].bp_sib_bh
);
910 path
[level
].bp_sib_bh
= NULL
;
913 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
916 static void nilfs_btree_split(struct nilfs_bmap
*btree
,
917 struct nilfs_btree_path
*path
,
918 int level
, __u64
*keyp
, __u64
*ptrp
)
920 struct nilfs_btree_node
*node
, *right
;
921 int nchildren
, n
, move
, ncblk
;
923 node
= nilfs_btree_get_nonroot_node(path
, level
);
924 right
= nilfs_btree_get_sib_node(path
, level
);
925 nchildren
= nilfs_btree_node_get_nchildren(node
);
926 ncblk
= nilfs_btree_nchildren_per_block(btree
);
929 n
= (nchildren
+ 1) / 2;
930 if (n
> nchildren
- path
[level
].bp_index
) {
935 nilfs_btree_node_move_right(node
, right
, n
, ncblk
, ncblk
);
937 if (!buffer_dirty(path
[level
].bp_bh
))
938 mark_buffer_dirty(path
[level
].bp_bh
);
939 if (!buffer_dirty(path
[level
].bp_sib_bh
))
940 mark_buffer_dirty(path
[level
].bp_sib_bh
);
943 path
[level
].bp_index
-= nilfs_btree_node_get_nchildren(node
);
944 nilfs_btree_node_insert(right
, path
[level
].bp_index
,
945 *keyp
, *ptrp
, ncblk
);
947 *keyp
= nilfs_btree_node_get_key(right
, 0);
948 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
950 brelse(path
[level
].bp_bh
);
951 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
952 path
[level
].bp_sib_bh
= NULL
;
954 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
956 *keyp
= nilfs_btree_node_get_key(right
, 0);
957 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
959 brelse(path
[level
].bp_sib_bh
);
960 path
[level
].bp_sib_bh
= NULL
;
963 path
[level
+ 1].bp_index
++;
966 static void nilfs_btree_grow(struct nilfs_bmap
*btree
,
967 struct nilfs_btree_path
*path
,
968 int level
, __u64
*keyp
, __u64
*ptrp
)
970 struct nilfs_btree_node
*root
, *child
;
973 root
= nilfs_btree_get_root(btree
);
974 child
= nilfs_btree_get_sib_node(path
, level
);
975 ncblk
= nilfs_btree_nchildren_per_block(btree
);
977 n
= nilfs_btree_node_get_nchildren(root
);
979 nilfs_btree_node_move_right(root
, child
, n
,
980 NILFS_BTREE_ROOT_NCHILDREN_MAX
, ncblk
);
981 nilfs_btree_node_set_level(root
, level
+ 1);
983 if (!buffer_dirty(path
[level
].bp_sib_bh
))
984 mark_buffer_dirty(path
[level
].bp_sib_bh
);
986 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
987 path
[level
].bp_sib_bh
= NULL
;
989 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
991 *keyp
= nilfs_btree_node_get_key(child
, 0);
992 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
995 static __u64
nilfs_btree_find_near(const struct nilfs_bmap
*btree
,
996 const struct nilfs_btree_path
*path
)
998 struct nilfs_btree_node
*node
;
1002 return NILFS_BMAP_INVALID_PTR
;
1005 level
= NILFS_BTREE_LEVEL_NODE_MIN
;
1006 if (path
[level
].bp_index
> 0) {
1007 node
= nilfs_btree_get_node(btree
, path
, level
, &ncmax
);
1008 return nilfs_btree_node_get_ptr(node
,
1009 path
[level
].bp_index
- 1,
1014 level
= NILFS_BTREE_LEVEL_NODE_MIN
+ 1;
1015 if (level
<= nilfs_btree_height(btree
) - 1) {
1016 node
= nilfs_btree_get_node(btree
, path
, level
, &ncmax
);
1017 return nilfs_btree_node_get_ptr(node
, path
[level
].bp_index
,
1021 return NILFS_BMAP_INVALID_PTR
;
1024 static __u64
nilfs_btree_find_target_v(const struct nilfs_bmap
*btree
,
1025 const struct nilfs_btree_path
*path
,
1030 ptr
= nilfs_bmap_find_target_seq(btree
, key
);
1031 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
1032 /* sequential access */
1035 ptr
= nilfs_btree_find_near(btree
, path
);
1036 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
1041 return nilfs_bmap_find_target_in_group(btree
);
1044 static int nilfs_btree_prepare_insert(struct nilfs_bmap
*btree
,
1045 struct nilfs_btree_path
*path
,
1046 int *levelp
, __u64 key
, __u64 ptr
,
1047 struct nilfs_bmap_stats
*stats
)
1049 struct buffer_head
*bh
;
1050 struct nilfs_btree_node
*node
, *parent
, *sib
;
1052 int pindex
, level
, ncmax
, ncblk
, ret
;
1053 struct inode
*dat
= NULL
;
1055 stats
->bs_nblocks
= 0;
1056 level
= NILFS_BTREE_LEVEL_DATA
;
1058 /* allocate a new ptr for data block */
1059 if (NILFS_BMAP_USE_VBN(btree
)) {
1060 path
[level
].bp_newreq
.bpr_ptr
=
1061 nilfs_btree_find_target_v(btree
, path
, key
);
1062 dat
= nilfs_bmap_get_dat(btree
);
1065 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1069 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1071 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
1072 level
< nilfs_btree_height(btree
) - 1;
1074 node
= nilfs_btree_get_nonroot_node(path
, level
);
1075 if (nilfs_btree_node_get_nchildren(node
) < ncblk
) {
1076 path
[level
].bp_op
= nilfs_btree_do_insert
;
1077 stats
->bs_nblocks
++;
1081 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1082 pindex
= path
[level
+ 1].bp_index
;
1086 sibptr
= nilfs_btree_node_get_ptr(parent
, pindex
- 1,
1088 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1090 goto err_out_child_node
;
1091 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1092 if (nilfs_btree_node_get_nchildren(sib
) < ncblk
) {
1093 path
[level
].bp_sib_bh
= bh
;
1094 path
[level
].bp_op
= nilfs_btree_carry_left
;
1095 stats
->bs_nblocks
++;
1103 if (pindex
< nilfs_btree_node_get_nchildren(parent
) - 1) {
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_right
;
1113 stats
->bs_nblocks
++;
1121 path
[level
].bp_newreq
.bpr_ptr
=
1122 path
[level
- 1].bp_newreq
.bpr_ptr
+ 1;
1123 ret
= nilfs_bmap_prepare_alloc_ptr(btree
,
1124 &path
[level
].bp_newreq
, dat
);
1126 goto err_out_child_node
;
1127 ret
= nilfs_btree_get_new_block(btree
,
1128 path
[level
].bp_newreq
.bpr_ptr
,
1131 goto err_out_curr_node
;
1133 stats
->bs_nblocks
++;
1135 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1136 nilfs_btree_node_init(sib
, 0, level
, 0, ncblk
, NULL
, NULL
);
1137 path
[level
].bp_sib_bh
= bh
;
1138 path
[level
].bp_op
= nilfs_btree_split
;
1142 node
= nilfs_btree_get_root(btree
);
1143 if (nilfs_btree_node_get_nchildren(node
) <
1144 NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1145 path
[level
].bp_op
= nilfs_btree_do_insert
;
1146 stats
->bs_nblocks
++;
1151 path
[level
].bp_newreq
.bpr_ptr
= path
[level
- 1].bp_newreq
.bpr_ptr
+ 1;
1152 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1154 goto err_out_child_node
;
1155 ret
= nilfs_btree_get_new_block(btree
, path
[level
].bp_newreq
.bpr_ptr
,
1158 goto err_out_curr_node
;
1160 nilfs_btree_node_init((struct nilfs_btree_node
*)bh
->b_data
,
1161 0, level
, 0, ncblk
, NULL
, NULL
);
1162 path
[level
].bp_sib_bh
= bh
;
1163 path
[level
].bp_op
= nilfs_btree_grow
;
1166 path
[level
].bp_op
= nilfs_btree_do_insert
;
1168 /* a newly-created node block and a data block are added */
1169 stats
->bs_nblocks
+= 2;
1178 nilfs_bmap_abort_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1180 for (level
--; level
> NILFS_BTREE_LEVEL_DATA
; level
--) {
1181 nilfs_btnode_delete(path
[level
].bp_sib_bh
);
1182 nilfs_bmap_abort_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1186 nilfs_bmap_abort_alloc_ptr(btree
, &path
[level
].bp_newreq
, dat
);
1189 stats
->bs_nblocks
= 0;
1193 static void nilfs_btree_commit_insert(struct nilfs_bmap
*btree
,
1194 struct nilfs_btree_path
*path
,
1195 int maxlevel
, __u64 key
, __u64 ptr
)
1197 struct inode
*dat
= NULL
;
1200 set_buffer_nilfs_volatile((struct buffer_head
*)((unsigned long)ptr
));
1201 ptr
= path
[NILFS_BTREE_LEVEL_DATA
].bp_newreq
.bpr_ptr
;
1202 if (NILFS_BMAP_USE_VBN(btree
)) {
1203 nilfs_bmap_set_target_v(btree
, key
, ptr
);
1204 dat
= nilfs_bmap_get_dat(btree
);
1207 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
; level
<= maxlevel
; level
++) {
1208 nilfs_bmap_commit_alloc_ptr(btree
,
1209 &path
[level
- 1].bp_newreq
, dat
);
1210 path
[level
].bp_op(btree
, path
, level
, &key
, &ptr
);
1213 if (!nilfs_bmap_dirty(btree
))
1214 nilfs_bmap_set_dirty(btree
);
1217 static int nilfs_btree_insert(struct nilfs_bmap
*btree
, __u64 key
, __u64 ptr
)
1219 struct nilfs_btree_path
*path
;
1220 struct nilfs_bmap_stats stats
;
1223 path
= nilfs_btree_alloc_path();
1227 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
,
1228 NILFS_BTREE_LEVEL_NODE_MIN
, 0);
1229 if (ret
!= -ENOENT
) {
1235 ret
= nilfs_btree_prepare_insert(btree
, path
, &level
, key
, ptr
, &stats
);
1238 nilfs_btree_commit_insert(btree
, path
, level
, key
, ptr
);
1239 nilfs_inode_add_blocks(btree
->b_inode
, stats
.bs_nblocks
);
1242 nilfs_btree_free_path(path
);
1246 static void nilfs_btree_do_delete(struct nilfs_bmap
*btree
,
1247 struct nilfs_btree_path
*path
,
1248 int level
, __u64
*keyp
, __u64
*ptrp
)
1250 struct nilfs_btree_node
*node
;
1253 if (level
< nilfs_btree_height(btree
) - 1) {
1254 node
= nilfs_btree_get_nonroot_node(path
, level
);
1255 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1256 nilfs_btree_node_delete(node
, path
[level
].bp_index
,
1258 if (!buffer_dirty(path
[level
].bp_bh
))
1259 mark_buffer_dirty(path
[level
].bp_bh
);
1260 if (path
[level
].bp_index
== 0)
1261 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1262 nilfs_btree_node_get_key(node
, 0));
1264 node
= nilfs_btree_get_root(btree
);
1265 nilfs_btree_node_delete(node
, path
[level
].bp_index
,
1267 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1271 static void nilfs_btree_borrow_left(struct nilfs_bmap
*btree
,
1272 struct nilfs_btree_path
*path
,
1273 int level
, __u64
*keyp
, __u64
*ptrp
)
1275 struct nilfs_btree_node
*node
, *left
;
1276 int nchildren
, lnchildren
, n
, ncblk
;
1278 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1280 node
= nilfs_btree_get_nonroot_node(path
, level
);
1281 left
= nilfs_btree_get_sib_node(path
, level
);
1282 nchildren
= nilfs_btree_node_get_nchildren(node
);
1283 lnchildren
= nilfs_btree_node_get_nchildren(left
);
1284 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1286 n
= (nchildren
+ lnchildren
) / 2 - nchildren
;
1288 nilfs_btree_node_move_right(left
, node
, n
, ncblk
, ncblk
);
1290 if (!buffer_dirty(path
[level
].bp_bh
))
1291 mark_buffer_dirty(path
[level
].bp_bh
);
1292 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1293 mark_buffer_dirty(path
[level
].bp_sib_bh
);
1295 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1296 nilfs_btree_node_get_key(node
, 0));
1298 brelse(path
[level
].bp_sib_bh
);
1299 path
[level
].bp_sib_bh
= NULL
;
1300 path
[level
].bp_index
+= n
;
1303 static void nilfs_btree_borrow_right(struct nilfs_bmap
*btree
,
1304 struct nilfs_btree_path
*path
,
1305 int level
, __u64
*keyp
, __u64
*ptrp
)
1307 struct nilfs_btree_node
*node
, *right
;
1308 int nchildren
, rnchildren
, n
, ncblk
;
1310 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1312 node
= nilfs_btree_get_nonroot_node(path
, level
);
1313 right
= nilfs_btree_get_sib_node(path
, level
);
1314 nchildren
= nilfs_btree_node_get_nchildren(node
);
1315 rnchildren
= nilfs_btree_node_get_nchildren(right
);
1316 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1318 n
= (nchildren
+ rnchildren
) / 2 - nchildren
;
1320 nilfs_btree_node_move_left(node
, right
, n
, ncblk
, ncblk
);
1322 if (!buffer_dirty(path
[level
].bp_bh
))
1323 mark_buffer_dirty(path
[level
].bp_bh
);
1324 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1325 mark_buffer_dirty(path
[level
].bp_sib_bh
);
1327 path
[level
+ 1].bp_index
++;
1328 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1329 nilfs_btree_node_get_key(right
, 0));
1330 path
[level
+ 1].bp_index
--;
1332 brelse(path
[level
].bp_sib_bh
);
1333 path
[level
].bp_sib_bh
= NULL
;
1336 static void nilfs_btree_concat_left(struct nilfs_bmap
*btree
,
1337 struct nilfs_btree_path
*path
,
1338 int level
, __u64
*keyp
, __u64
*ptrp
)
1340 struct nilfs_btree_node
*node
, *left
;
1343 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1345 node
= nilfs_btree_get_nonroot_node(path
, level
);
1346 left
= nilfs_btree_get_sib_node(path
, level
);
1347 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1349 n
= nilfs_btree_node_get_nchildren(node
);
1351 nilfs_btree_node_move_left(left
, node
, n
, ncblk
, ncblk
);
1353 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1354 mark_buffer_dirty(path
[level
].bp_sib_bh
);
1356 nilfs_btnode_delete(path
[level
].bp_bh
);
1357 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
1358 path
[level
].bp_sib_bh
= NULL
;
1359 path
[level
].bp_index
+= nilfs_btree_node_get_nchildren(left
);
1362 static void nilfs_btree_concat_right(struct nilfs_bmap
*btree
,
1363 struct nilfs_btree_path
*path
,
1364 int level
, __u64
*keyp
, __u64
*ptrp
)
1366 struct nilfs_btree_node
*node
, *right
;
1369 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1371 node
= nilfs_btree_get_nonroot_node(path
, level
);
1372 right
= nilfs_btree_get_sib_node(path
, level
);
1373 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1375 n
= nilfs_btree_node_get_nchildren(right
);
1377 nilfs_btree_node_move_left(node
, right
, n
, ncblk
, ncblk
);
1379 if (!buffer_dirty(path
[level
].bp_bh
))
1380 mark_buffer_dirty(path
[level
].bp_bh
);
1382 nilfs_btnode_delete(path
[level
].bp_sib_bh
);
1383 path
[level
].bp_sib_bh
= NULL
;
1384 path
[level
+ 1].bp_index
++;
1387 static void nilfs_btree_shrink(struct nilfs_bmap
*btree
,
1388 struct nilfs_btree_path
*path
,
1389 int level
, __u64
*keyp
, __u64
*ptrp
)
1391 struct nilfs_btree_node
*root
, *child
;
1394 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1396 root
= nilfs_btree_get_root(btree
);
1397 child
= nilfs_btree_get_nonroot_node(path
, level
);
1398 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1400 nilfs_btree_node_delete(root
, 0, NULL
, NULL
,
1401 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1402 nilfs_btree_node_set_level(root
, level
);
1403 n
= nilfs_btree_node_get_nchildren(child
);
1404 nilfs_btree_node_move_left(root
, child
, n
,
1405 NILFS_BTREE_ROOT_NCHILDREN_MAX
, ncblk
);
1407 nilfs_btnode_delete(path
[level
].bp_bh
);
1408 path
[level
].bp_bh
= NULL
;
1411 static void nilfs_btree_nop(struct nilfs_bmap
*btree
,
1412 struct nilfs_btree_path
*path
,
1413 int level
, __u64
*keyp
, __u64
*ptrp
)
1417 static int nilfs_btree_prepare_delete(struct nilfs_bmap
*btree
,
1418 struct nilfs_btree_path
*path
,
1420 struct nilfs_bmap_stats
*stats
,
1423 struct buffer_head
*bh
;
1424 struct nilfs_btree_node
*node
, *parent
, *sib
;
1426 int pindex
, dindex
, level
, ncmin
, ncmax
, ncblk
, ret
;
1429 stats
->bs_nblocks
= 0;
1430 ncmin
= NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree
));
1431 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1433 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
, dindex
= path
[level
].bp_index
;
1434 level
< nilfs_btree_height(btree
) - 1;
1436 node
= nilfs_btree_get_nonroot_node(path
, level
);
1437 path
[level
].bp_oldreq
.bpr_ptr
=
1438 nilfs_btree_node_get_ptr(node
, dindex
, ncblk
);
1439 ret
= nilfs_bmap_prepare_end_ptr(btree
,
1440 &path
[level
].bp_oldreq
, dat
);
1442 goto err_out_child_node
;
1444 if (nilfs_btree_node_get_nchildren(node
) > ncmin
) {
1445 path
[level
].bp_op
= nilfs_btree_do_delete
;
1446 stats
->bs_nblocks
++;
1450 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1451 pindex
= path
[level
+ 1].bp_index
;
1456 sibptr
= nilfs_btree_node_get_ptr(parent
, pindex
- 1,
1458 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1460 goto err_out_curr_node
;
1461 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1462 if (nilfs_btree_node_get_nchildren(sib
) > ncmin
) {
1463 path
[level
].bp_sib_bh
= bh
;
1464 path
[level
].bp_op
= nilfs_btree_borrow_left
;
1465 stats
->bs_nblocks
++;
1468 path
[level
].bp_sib_bh
= bh
;
1469 path
[level
].bp_op
= nilfs_btree_concat_left
;
1470 stats
->bs_nblocks
++;
1474 nilfs_btree_node_get_nchildren(parent
) - 1) {
1476 sibptr
= nilfs_btree_node_get_ptr(parent
, pindex
+ 1,
1478 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1480 goto err_out_curr_node
;
1481 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1482 if (nilfs_btree_node_get_nchildren(sib
) > ncmin
) {
1483 path
[level
].bp_sib_bh
= bh
;
1484 path
[level
].bp_op
= nilfs_btree_borrow_right
;
1485 stats
->bs_nblocks
++;
1488 path
[level
].bp_sib_bh
= bh
;
1489 path
[level
].bp_op
= nilfs_btree_concat_right
;
1490 stats
->bs_nblocks
++;
1492 * When merging right sibling node
1493 * into the current node, pointer to
1494 * the right sibling node must be
1495 * terminated instead. The adjustment
1496 * below is required for that.
1498 dindex
= pindex
+ 1;
1503 /* the only child of the root node */
1504 WARN_ON(level
!= nilfs_btree_height(btree
) - 2);
1505 if (nilfs_btree_node_get_nchildren(node
) - 1 <=
1506 NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1507 path
[level
].bp_op
= nilfs_btree_shrink
;
1508 stats
->bs_nblocks
+= 2;
1510 path
[level
].bp_op
= nilfs_btree_nop
;
1511 goto shrink_root_child
;
1513 path
[level
].bp_op
= nilfs_btree_do_delete
;
1514 stats
->bs_nblocks
++;
1520 /* child of the root node is deleted */
1521 path
[level
].bp_op
= nilfs_btree_do_delete
;
1522 stats
->bs_nblocks
++;
1525 node
= nilfs_btree_get_root(btree
);
1526 path
[level
].bp_oldreq
.bpr_ptr
=
1527 nilfs_btree_node_get_ptr(node
, dindex
,
1528 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1530 ret
= nilfs_bmap_prepare_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1532 goto err_out_child_node
;
1541 nilfs_bmap_abort_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1543 for (level
--; level
>= NILFS_BTREE_LEVEL_NODE_MIN
; level
--) {
1544 brelse(path
[level
].bp_sib_bh
);
1545 nilfs_bmap_abort_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1548 stats
->bs_nblocks
= 0;
1552 static void nilfs_btree_commit_delete(struct nilfs_bmap
*btree
,
1553 struct nilfs_btree_path
*path
,
1554 int maxlevel
, struct inode
*dat
)
1558 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
; level
<= maxlevel
; level
++) {
1559 nilfs_bmap_commit_end_ptr(btree
, &path
[level
].bp_oldreq
, dat
);
1560 path
[level
].bp_op(btree
, path
, level
, NULL
, NULL
);
1563 if (!nilfs_bmap_dirty(btree
))
1564 nilfs_bmap_set_dirty(btree
);
1567 static int nilfs_btree_delete(struct nilfs_bmap
*btree
, __u64 key
)
1570 struct nilfs_btree_path
*path
;
1571 struct nilfs_bmap_stats stats
;
1575 path
= nilfs_btree_alloc_path();
1579 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
,
1580 NILFS_BTREE_LEVEL_NODE_MIN
, 0);
1585 dat
= NILFS_BMAP_USE_VBN(btree
) ? nilfs_bmap_get_dat(btree
) : NULL
;
1587 ret
= nilfs_btree_prepare_delete(btree
, path
, &level
, &stats
, dat
);
1590 nilfs_btree_commit_delete(btree
, path
, level
, dat
);
1591 nilfs_inode_sub_blocks(btree
->b_inode
, stats
.bs_nblocks
);
1594 nilfs_btree_free_path(path
);
1598 static int nilfs_btree_seek_key(const struct nilfs_bmap
*btree
, __u64 start
,
1601 struct nilfs_btree_path
*path
;
1602 const int minlevel
= NILFS_BTREE_LEVEL_NODE_MIN
;
1605 path
= nilfs_btree_alloc_path();
1609 ret
= nilfs_btree_do_lookup(btree
, path
, start
, NULL
, minlevel
, 0);
1612 else if (ret
== -ENOENT
)
1613 ret
= nilfs_btree_get_next_key(btree
, path
, minlevel
, keyp
);
1615 nilfs_btree_free_path(path
);
1619 static int nilfs_btree_last_key(const struct nilfs_bmap
*btree
, __u64
*keyp
)
1621 struct nilfs_btree_path
*path
;
1624 path
= nilfs_btree_alloc_path();
1628 ret
= nilfs_btree_do_lookup_last(btree
, path
, keyp
, NULL
);
1630 nilfs_btree_free_path(path
);
1635 static int nilfs_btree_check_delete(struct nilfs_bmap
*btree
, __u64 key
)
1637 struct buffer_head
*bh
;
1638 struct nilfs_btree_node
*root
, *node
;
1639 __u64 maxkey
, nextmaxkey
;
1643 root
= nilfs_btree_get_root(btree
);
1644 switch (nilfs_btree_height(btree
)) {
1650 nchildren
= nilfs_btree_node_get_nchildren(root
);
1653 ptr
= nilfs_btree_node_get_ptr(root
, nchildren
- 1,
1654 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1655 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
1658 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1664 nchildren
= nilfs_btree_node_get_nchildren(node
);
1665 maxkey
= nilfs_btree_node_get_key(node
, nchildren
- 1);
1666 nextmaxkey
= (nchildren
> 1) ?
1667 nilfs_btree_node_get_key(node
, nchildren
- 2) : 0;
1671 return (maxkey
== key
) && (nextmaxkey
< NILFS_BMAP_LARGE_LOW
);
1674 static int nilfs_btree_gather_data(struct nilfs_bmap
*btree
,
1675 __u64
*keys
, __u64
*ptrs
, int nitems
)
1677 struct buffer_head
*bh
;
1678 struct nilfs_btree_node
*node
, *root
;
1682 int nchildren
, ncmax
, i
, ret
;
1684 root
= nilfs_btree_get_root(btree
);
1685 switch (nilfs_btree_height(btree
)) {
1689 ncmax
= NILFS_BTREE_ROOT_NCHILDREN_MAX
;
1692 nchildren
= nilfs_btree_node_get_nchildren(root
);
1693 WARN_ON(nchildren
> 1);
1694 ptr
= nilfs_btree_node_get_ptr(root
, nchildren
- 1,
1695 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1696 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
1699 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1700 ncmax
= nilfs_btree_nchildren_per_block(btree
);
1707 nchildren
= nilfs_btree_node_get_nchildren(node
);
1708 if (nchildren
< nitems
)
1710 dkeys
= nilfs_btree_node_dkeys(node
);
1711 dptrs
= nilfs_btree_node_dptrs(node
, ncmax
);
1712 for (i
= 0; i
< nitems
; i
++) {
1713 keys
[i
] = le64_to_cpu(dkeys
[i
]);
1714 ptrs
[i
] = le64_to_cpu(dptrs
[i
]);
1724 nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap
*btree
, __u64 key
,
1725 union nilfs_bmap_ptr_req
*dreq
,
1726 union nilfs_bmap_ptr_req
*nreq
,
1727 struct buffer_head
**bhp
,
1728 struct nilfs_bmap_stats
*stats
)
1730 struct buffer_head
*bh
;
1731 struct inode
*dat
= NULL
;
1734 stats
->bs_nblocks
= 0;
1737 /* cannot find near ptr */
1738 if (NILFS_BMAP_USE_VBN(btree
)) {
1739 dreq
->bpr_ptr
= nilfs_btree_find_target_v(btree
, NULL
, key
);
1740 dat
= nilfs_bmap_get_dat(btree
);
1743 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, dreq
, dat
);
1748 stats
->bs_nblocks
++;
1750 nreq
->bpr_ptr
= dreq
->bpr_ptr
+ 1;
1751 ret
= nilfs_bmap_prepare_alloc_ptr(btree
, nreq
, dat
);
1755 ret
= nilfs_btree_get_new_block(btree
, nreq
->bpr_ptr
, &bh
);
1760 stats
->bs_nblocks
++;
1768 nilfs_bmap_abort_alloc_ptr(btree
, nreq
, dat
);
1770 nilfs_bmap_abort_alloc_ptr(btree
, dreq
, dat
);
1771 stats
->bs_nblocks
= 0;
1777 nilfs_btree_commit_convert_and_insert(struct nilfs_bmap
*btree
,
1778 __u64 key
, __u64 ptr
,
1779 const __u64
*keys
, const __u64
*ptrs
,
1781 union nilfs_bmap_ptr_req
*dreq
,
1782 union nilfs_bmap_ptr_req
*nreq
,
1783 struct buffer_head
*bh
)
1785 struct nilfs_btree_node
*node
;
1790 /* free resources */
1791 if (btree
->b_ops
->bop_clear
!= NULL
)
1792 btree
->b_ops
->bop_clear(btree
);
1794 /* ptr must be a pointer to a buffer head. */
1795 set_buffer_nilfs_volatile((struct buffer_head
*)((unsigned long)ptr
));
1797 /* convert and insert */
1798 dat
= NILFS_BMAP_USE_VBN(btree
) ? nilfs_bmap_get_dat(btree
) : NULL
;
1799 __nilfs_btree_init(btree
);
1801 nilfs_bmap_commit_alloc_ptr(btree
, dreq
, dat
);
1802 nilfs_bmap_commit_alloc_ptr(btree
, nreq
, dat
);
1804 /* create child node at level 1 */
1805 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1806 ncblk
= nilfs_btree_nchildren_per_block(btree
);
1807 nilfs_btree_node_init(node
, 0, 1, n
, ncblk
, keys
, ptrs
);
1808 nilfs_btree_node_insert(node
, n
, key
, dreq
->bpr_ptr
, ncblk
);
1809 if (!buffer_dirty(bh
))
1810 mark_buffer_dirty(bh
);
1811 if (!nilfs_bmap_dirty(btree
))
1812 nilfs_bmap_set_dirty(btree
);
1816 /* create root node at level 2 */
1817 node
= nilfs_btree_get_root(btree
);
1818 tmpptr
= nreq
->bpr_ptr
;
1819 nilfs_btree_node_init(node
, NILFS_BTREE_NODE_ROOT
, 2, 1,
1820 NILFS_BTREE_ROOT_NCHILDREN_MAX
,
1823 nilfs_bmap_commit_alloc_ptr(btree
, dreq
, dat
);
1825 /* create root node at level 1 */
1826 node
= nilfs_btree_get_root(btree
);
1827 nilfs_btree_node_init(node
, NILFS_BTREE_NODE_ROOT
, 1, n
,
1828 NILFS_BTREE_ROOT_NCHILDREN_MAX
,
1830 nilfs_btree_node_insert(node
, n
, key
, dreq
->bpr_ptr
,
1831 NILFS_BTREE_ROOT_NCHILDREN_MAX
);
1832 if (!nilfs_bmap_dirty(btree
))
1833 nilfs_bmap_set_dirty(btree
);
1836 if (NILFS_BMAP_USE_VBN(btree
))
1837 nilfs_bmap_set_target_v(btree
, key
, dreq
->bpr_ptr
);
1841 * nilfs_btree_convert_and_insert -
1849 int nilfs_btree_convert_and_insert(struct nilfs_bmap
*btree
,
1850 __u64 key
, __u64 ptr
,
1851 const __u64
*keys
, const __u64
*ptrs
, int n
)
1853 struct buffer_head
*bh
= NULL
;
1854 union nilfs_bmap_ptr_req dreq
, nreq
, *di
, *ni
;
1855 struct nilfs_bmap_stats stats
;
1858 if (n
+ 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1861 } else if ((n
+ 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
1862 1 << btree
->b_inode
->i_blkbits
)) {
1871 ret
= nilfs_btree_prepare_convert_and_insert(btree
, key
, di
, ni
, &bh
,
1875 nilfs_btree_commit_convert_and_insert(btree
, key
, ptr
, keys
, ptrs
, n
,
1877 nilfs_inode_add_blocks(btree
->b_inode
, stats
.bs_nblocks
);
1881 static int nilfs_btree_propagate_p(struct nilfs_bmap
*btree
,
1882 struct nilfs_btree_path
*path
,
1884 struct buffer_head
*bh
)
1886 while ((++level
< nilfs_btree_height(btree
) - 1) &&
1887 !buffer_dirty(path
[level
].bp_bh
))
1888 mark_buffer_dirty(path
[level
].bp_bh
);
1893 static int nilfs_btree_prepare_update_v(struct nilfs_bmap
*btree
,
1894 struct nilfs_btree_path
*path
,
1895 int level
, struct inode
*dat
)
1897 struct nilfs_btree_node
*parent
;
1900 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1901 path
[level
].bp_oldreq
.bpr_ptr
=
1902 nilfs_btree_node_get_ptr(parent
, path
[level
+ 1].bp_index
,
1904 path
[level
].bp_newreq
.bpr_ptr
= path
[level
].bp_oldreq
.bpr_ptr
+ 1;
1905 ret
= nilfs_dat_prepare_update(dat
, &path
[level
].bp_oldreq
.bpr_req
,
1906 &path
[level
].bp_newreq
.bpr_req
);
1910 if (buffer_nilfs_node(path
[level
].bp_bh
)) {
1911 path
[level
].bp_ctxt
.oldkey
= path
[level
].bp_oldreq
.bpr_ptr
;
1912 path
[level
].bp_ctxt
.newkey
= path
[level
].bp_newreq
.bpr_ptr
;
1913 path
[level
].bp_ctxt
.bh
= path
[level
].bp_bh
;
1914 ret
= nilfs_btnode_prepare_change_key(
1915 &NILFS_BMAP_I(btree
)->i_btnode_cache
,
1916 &path
[level
].bp_ctxt
);
1918 nilfs_dat_abort_update(dat
,
1919 &path
[level
].bp_oldreq
.bpr_req
,
1920 &path
[level
].bp_newreq
.bpr_req
);
1928 static void nilfs_btree_commit_update_v(struct nilfs_bmap
*btree
,
1929 struct nilfs_btree_path
*path
,
1930 int level
, struct inode
*dat
)
1932 struct nilfs_btree_node
*parent
;
1935 nilfs_dat_commit_update(dat
, &path
[level
].bp_oldreq
.bpr_req
,
1936 &path
[level
].bp_newreq
.bpr_req
,
1937 btree
->b_ptr_type
== NILFS_BMAP_PTR_VS
);
1939 if (buffer_nilfs_node(path
[level
].bp_bh
)) {
1940 nilfs_btnode_commit_change_key(
1941 &NILFS_BMAP_I(btree
)->i_btnode_cache
,
1942 &path
[level
].bp_ctxt
);
1943 path
[level
].bp_bh
= path
[level
].bp_ctxt
.bh
;
1945 set_buffer_nilfs_volatile(path
[level
].bp_bh
);
1947 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
1948 nilfs_btree_node_set_ptr(parent
, path
[level
+ 1].bp_index
,
1949 path
[level
].bp_newreq
.bpr_ptr
, ncmax
);
1952 static void nilfs_btree_abort_update_v(struct nilfs_bmap
*btree
,
1953 struct nilfs_btree_path
*path
,
1954 int level
, struct inode
*dat
)
1956 nilfs_dat_abort_update(dat
, &path
[level
].bp_oldreq
.bpr_req
,
1957 &path
[level
].bp_newreq
.bpr_req
);
1958 if (buffer_nilfs_node(path
[level
].bp_bh
))
1959 nilfs_btnode_abort_change_key(
1960 &NILFS_BMAP_I(btree
)->i_btnode_cache
,
1961 &path
[level
].bp_ctxt
);
1964 static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap
*btree
,
1965 struct nilfs_btree_path
*path
,
1966 int minlevel
, int *maxlevelp
,
1972 if (!buffer_nilfs_volatile(path
[level
].bp_bh
)) {
1973 ret
= nilfs_btree_prepare_update_v(btree
, path
, level
, dat
);
1977 while ((++level
< nilfs_btree_height(btree
) - 1) &&
1978 !buffer_dirty(path
[level
].bp_bh
)) {
1980 WARN_ON(buffer_nilfs_volatile(path
[level
].bp_bh
));
1981 ret
= nilfs_btree_prepare_update_v(btree
, path
, level
, dat
);
1987 *maxlevelp
= level
- 1;
1992 while (--level
> minlevel
)
1993 nilfs_btree_abort_update_v(btree
, path
, level
, dat
);
1994 if (!buffer_nilfs_volatile(path
[level
].bp_bh
))
1995 nilfs_btree_abort_update_v(btree
, path
, level
, dat
);
1999 static void nilfs_btree_commit_propagate_v(struct nilfs_bmap
*btree
,
2000 struct nilfs_btree_path
*path
,
2001 int minlevel
, int maxlevel
,
2002 struct buffer_head
*bh
,
2007 if (!buffer_nilfs_volatile(path
[minlevel
].bp_bh
))
2008 nilfs_btree_commit_update_v(btree
, path
, minlevel
, dat
);
2010 for (level
= minlevel
+ 1; level
<= maxlevel
; level
++)
2011 nilfs_btree_commit_update_v(btree
, path
, level
, dat
);
2014 static int nilfs_btree_propagate_v(struct nilfs_bmap
*btree
,
2015 struct nilfs_btree_path
*path
,
2016 int level
, struct buffer_head
*bh
)
2018 int maxlevel
= 0, ret
;
2019 struct nilfs_btree_node
*parent
;
2020 struct inode
*dat
= nilfs_bmap_get_dat(btree
);
2025 path
[level
].bp_bh
= bh
;
2026 ret
= nilfs_btree_prepare_propagate_v(btree
, path
, level
, &maxlevel
,
2031 if (buffer_nilfs_volatile(path
[level
].bp_bh
)) {
2032 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
2033 ptr
= nilfs_btree_node_get_ptr(parent
,
2034 path
[level
+ 1].bp_index
,
2036 ret
= nilfs_dat_mark_dirty(dat
, ptr
);
2041 nilfs_btree_commit_propagate_v(btree
, path
, level
, maxlevel
, bh
, dat
);
2044 brelse(path
[level
].bp_bh
);
2045 path
[level
].bp_bh
= NULL
;
2049 static int nilfs_btree_propagate(struct nilfs_bmap
*btree
,
2050 struct buffer_head
*bh
)
2052 struct nilfs_btree_path
*path
;
2053 struct nilfs_btree_node
*node
;
2057 WARN_ON(!buffer_dirty(bh
));
2059 path
= nilfs_btree_alloc_path();
2063 if (buffer_nilfs_node(bh
)) {
2064 node
= (struct nilfs_btree_node
*)bh
->b_data
;
2065 key
= nilfs_btree_node_get_key(node
, 0);
2066 level
= nilfs_btree_node_get_level(node
);
2068 key
= nilfs_bmap_data_get_key(btree
, bh
);
2069 level
= NILFS_BTREE_LEVEL_DATA
;
2072 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
, level
+ 1, 0);
2074 if (unlikely(ret
== -ENOENT
))
2075 printk(KERN_CRIT
"%s: key = %llu, level == %d\n",
2076 __func__
, (unsigned long long)key
, level
);
2080 ret
= NILFS_BMAP_USE_VBN(btree
) ?
2081 nilfs_btree_propagate_v(btree
, path
, level
, bh
) :
2082 nilfs_btree_propagate_p(btree
, path
, level
, bh
);
2085 nilfs_btree_free_path(path
);
2090 static int nilfs_btree_propagate_gc(struct nilfs_bmap
*btree
,
2091 struct buffer_head
*bh
)
2093 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree
), bh
->b_blocknr
);
2096 static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap
*btree
,
2097 struct list_head
*lists
,
2098 struct buffer_head
*bh
)
2100 struct list_head
*head
;
2101 struct buffer_head
*cbh
;
2102 struct nilfs_btree_node
*node
, *cnode
;
2107 node
= (struct nilfs_btree_node
*)bh
->b_data
;
2108 key
= nilfs_btree_node_get_key(node
, 0);
2109 level
= nilfs_btree_node_get_level(node
);
2110 if (level
< NILFS_BTREE_LEVEL_NODE_MIN
||
2111 level
>= NILFS_BTREE_LEVEL_MAX
) {
2114 "%s: invalid btree level: %d (key=%llu, ino=%lu, "
2116 __func__
, level
, (unsigned long long)key
,
2117 NILFS_BMAP_I(btree
)->vfs_inode
.i_ino
,
2118 (unsigned long long)bh
->b_blocknr
);
2122 list_for_each(head
, &lists
[level
]) {
2123 cbh
= list_entry(head
, struct buffer_head
, b_assoc_buffers
);
2124 cnode
= (struct nilfs_btree_node
*)cbh
->b_data
;
2125 ckey
= nilfs_btree_node_get_key(cnode
, 0);
2129 list_add_tail(&bh
->b_assoc_buffers
, head
);
2132 static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap
*btree
,
2133 struct list_head
*listp
)
2135 struct address_space
*btcache
= &NILFS_BMAP_I(btree
)->i_btnode_cache
;
2136 struct list_head lists
[NILFS_BTREE_LEVEL_MAX
];
2137 struct pagevec pvec
;
2138 struct buffer_head
*bh
, *head
;
2142 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
2143 level
< NILFS_BTREE_LEVEL_MAX
;
2145 INIT_LIST_HEAD(&lists
[level
]);
2147 pagevec_init(&pvec
, 0);
2149 while (pagevec_lookup_tag(&pvec
, btcache
, &index
, PAGECACHE_TAG_DIRTY
,
2151 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
2152 bh
= head
= page_buffers(pvec
.pages
[i
]);
2154 if (buffer_dirty(bh
))
2155 nilfs_btree_add_dirty_buffer(btree
,
2157 } while ((bh
= bh
->b_this_page
) != head
);
2159 pagevec_release(&pvec
);
2163 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
2164 level
< NILFS_BTREE_LEVEL_MAX
;
2166 list_splice_tail(&lists
[level
], listp
);
2169 static int nilfs_btree_assign_p(struct nilfs_bmap
*btree
,
2170 struct nilfs_btree_path
*path
,
2172 struct buffer_head
**bh
,
2174 union nilfs_binfo
*binfo
)
2176 struct nilfs_btree_node
*parent
;
2181 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
2182 ptr
= nilfs_btree_node_get_ptr(parent
, path
[level
+ 1].bp_index
,
2184 if (buffer_nilfs_node(*bh
)) {
2185 path
[level
].bp_ctxt
.oldkey
= ptr
;
2186 path
[level
].bp_ctxt
.newkey
= blocknr
;
2187 path
[level
].bp_ctxt
.bh
= *bh
;
2188 ret
= nilfs_btnode_prepare_change_key(
2189 &NILFS_BMAP_I(btree
)->i_btnode_cache
,
2190 &path
[level
].bp_ctxt
);
2193 nilfs_btnode_commit_change_key(
2194 &NILFS_BMAP_I(btree
)->i_btnode_cache
,
2195 &path
[level
].bp_ctxt
);
2196 *bh
= path
[level
].bp_ctxt
.bh
;
2199 nilfs_btree_node_set_ptr(parent
, path
[level
+ 1].bp_index
, blocknr
,
2202 key
= nilfs_btree_node_get_key(parent
, path
[level
+ 1].bp_index
);
2203 /* on-disk format */
2204 binfo
->bi_dat
.bi_blkoff
= cpu_to_le64(key
);
2205 binfo
->bi_dat
.bi_level
= level
;
2210 static int nilfs_btree_assign_v(struct nilfs_bmap
*btree
,
2211 struct nilfs_btree_path
*path
,
2213 struct buffer_head
**bh
,
2215 union nilfs_binfo
*binfo
)
2217 struct nilfs_btree_node
*parent
;
2218 struct inode
*dat
= nilfs_bmap_get_dat(btree
);
2221 union nilfs_bmap_ptr_req req
;
2224 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1, &ncmax
);
2225 ptr
= nilfs_btree_node_get_ptr(parent
, path
[level
+ 1].bp_index
,
2228 ret
= nilfs_dat_prepare_start(dat
, &req
.bpr_req
);
2231 nilfs_dat_commit_start(dat
, &req
.bpr_req
, blocknr
);
2233 key
= nilfs_btree_node_get_key(parent
, path
[level
+ 1].bp_index
);
2234 /* on-disk format */
2235 binfo
->bi_v
.bi_vblocknr
= cpu_to_le64(ptr
);
2236 binfo
->bi_v
.bi_blkoff
= cpu_to_le64(key
);
2241 static int nilfs_btree_assign(struct nilfs_bmap
*btree
,
2242 struct buffer_head
**bh
,
2244 union nilfs_binfo
*binfo
)
2246 struct nilfs_btree_path
*path
;
2247 struct nilfs_btree_node
*node
;
2251 path
= nilfs_btree_alloc_path();
2255 if (buffer_nilfs_node(*bh
)) {
2256 node
= (struct nilfs_btree_node
*)(*bh
)->b_data
;
2257 key
= nilfs_btree_node_get_key(node
, 0);
2258 level
= nilfs_btree_node_get_level(node
);
2260 key
= nilfs_bmap_data_get_key(btree
, *bh
);
2261 level
= NILFS_BTREE_LEVEL_DATA
;
2264 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
, level
+ 1, 0);
2266 WARN_ON(ret
== -ENOENT
);
2270 ret
= NILFS_BMAP_USE_VBN(btree
) ?
2271 nilfs_btree_assign_v(btree
, path
, level
, bh
, blocknr
, binfo
) :
2272 nilfs_btree_assign_p(btree
, path
, level
, bh
, blocknr
, binfo
);
2275 nilfs_btree_free_path(path
);
2280 static int nilfs_btree_assign_gc(struct nilfs_bmap
*btree
,
2281 struct buffer_head
**bh
,
2283 union nilfs_binfo
*binfo
)
2285 struct nilfs_btree_node
*node
;
2289 ret
= nilfs_dat_move(nilfs_bmap_get_dat(btree
), (*bh
)->b_blocknr
,
2294 if (buffer_nilfs_node(*bh
)) {
2295 node
= (struct nilfs_btree_node
*)(*bh
)->b_data
;
2296 key
= nilfs_btree_node_get_key(node
, 0);
2298 key
= nilfs_bmap_data_get_key(btree
, *bh
);
2300 /* on-disk format */
2301 binfo
->bi_v
.bi_vblocknr
= cpu_to_le64((*bh
)->b_blocknr
);
2302 binfo
->bi_v
.bi_blkoff
= cpu_to_le64(key
);
2307 static int nilfs_btree_mark(struct nilfs_bmap
*btree
, __u64 key
, int level
)
2309 struct buffer_head
*bh
;
2310 struct nilfs_btree_path
*path
;
2314 path
= nilfs_btree_alloc_path();
2318 ret
= nilfs_btree_do_lookup(btree
, path
, key
, &ptr
, level
+ 1, 0);
2320 WARN_ON(ret
== -ENOENT
);
2323 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
2325 WARN_ON(ret
== -ENOENT
);
2329 if (!buffer_dirty(bh
))
2330 mark_buffer_dirty(bh
);
2332 if (!nilfs_bmap_dirty(btree
))
2333 nilfs_bmap_set_dirty(btree
);
2336 nilfs_btree_free_path(path
);
2340 static const struct nilfs_bmap_operations nilfs_btree_ops
= {
2341 .bop_lookup
= nilfs_btree_lookup
,
2342 .bop_lookup_contig
= nilfs_btree_lookup_contig
,
2343 .bop_insert
= nilfs_btree_insert
,
2344 .bop_delete
= nilfs_btree_delete
,
2347 .bop_propagate
= nilfs_btree_propagate
,
2349 .bop_lookup_dirty_buffers
= nilfs_btree_lookup_dirty_buffers
,
2351 .bop_assign
= nilfs_btree_assign
,
2352 .bop_mark
= nilfs_btree_mark
,
2354 .bop_seek_key
= nilfs_btree_seek_key
,
2355 .bop_last_key
= nilfs_btree_last_key
,
2357 .bop_check_insert
= NULL
,
2358 .bop_check_delete
= nilfs_btree_check_delete
,
2359 .bop_gather_data
= nilfs_btree_gather_data
,
2362 static const struct nilfs_bmap_operations nilfs_btree_ops_gc
= {
2364 .bop_lookup_contig
= NULL
,
2369 .bop_propagate
= nilfs_btree_propagate_gc
,
2371 .bop_lookup_dirty_buffers
= nilfs_btree_lookup_dirty_buffers
,
2373 .bop_assign
= nilfs_btree_assign_gc
,
2376 .bop_seek_key
= NULL
,
2377 .bop_last_key
= NULL
,
2379 .bop_check_insert
= NULL
,
2380 .bop_check_delete
= NULL
,
2381 .bop_gather_data
= NULL
,
2384 static void __nilfs_btree_init(struct nilfs_bmap
*bmap
)
2386 bmap
->b_ops
= &nilfs_btree_ops
;
2387 bmap
->b_nchildren_per_block
=
2388 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap
));
2391 int nilfs_btree_init(struct nilfs_bmap
*bmap
)
2395 __nilfs_btree_init(bmap
);
2397 if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap
),
2398 bmap
->b_inode
->i_ino
))
2403 void nilfs_btree_init_gc(struct nilfs_bmap
*bmap
)
2405 bmap
->b_ops
= &nilfs_btree_ops_gc
;
2406 bmap
->b_nchildren_per_block
=
2407 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap
));