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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Koji Sato <koji@osrg.net>.
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/pagevec.h>
35 * struct nilfs_btree_path - A path on which B-tree operations are executed
36 * @bp_bh: buffer head of node block
37 * @bp_sib_bh: buffer head of sibling node block
38 * @bp_index: index of child node
39 * @bp_oldreq: ptr end request for old ptr
40 * @bp_newreq: ptr alloc request for new ptr
41 * @bp_op: rebalance operation
43 struct nilfs_btree_path
{
44 struct buffer_head
*bp_bh
;
45 struct buffer_head
*bp_sib_bh
;
47 union nilfs_bmap_ptr_req bp_oldreq
;
48 union nilfs_bmap_ptr_req bp_newreq
;
49 struct nilfs_btnode_chkey_ctxt bp_ctxt
;
50 void (*bp_op
)(struct nilfs_btree
*, struct nilfs_btree_path
*,
51 int, __u64
*, __u64
*);
55 * B-tree path operations
58 static struct kmem_cache
*nilfs_btree_path_cache
;
60 int __init
nilfs_btree_path_cache_init(void)
62 nilfs_btree_path_cache
=
63 kmem_cache_create("nilfs2_btree_path_cache",
64 sizeof(struct nilfs_btree_path
) *
65 NILFS_BTREE_LEVEL_MAX
, 0, 0, NULL
);
66 return (nilfs_btree_path_cache
!= NULL
) ? 0 : -ENOMEM
;
69 void nilfs_btree_path_cache_destroy(void)
71 kmem_cache_destroy(nilfs_btree_path_cache
);
74 static inline struct nilfs_btree_path
*
75 nilfs_btree_alloc_path(const struct nilfs_btree
*btree
)
77 return (struct nilfs_btree_path
*)
78 kmem_cache_alloc(nilfs_btree_path_cache
, GFP_NOFS
);
81 static inline void nilfs_btree_free_path(const struct nilfs_btree
*btree
,
82 struct nilfs_btree_path
*path
)
84 kmem_cache_free(nilfs_btree_path_cache
, path
);
87 static void nilfs_btree_init_path(const struct nilfs_btree
*btree
,
88 struct nilfs_btree_path
*path
)
92 for (level
= NILFS_BTREE_LEVEL_DATA
;
93 level
< NILFS_BTREE_LEVEL_MAX
;
95 path
[level
].bp_bh
= NULL
;
96 path
[level
].bp_sib_bh
= NULL
;
97 path
[level
].bp_index
= 0;
98 path
[level
].bp_oldreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
99 path
[level
].bp_newreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
100 path
[level
].bp_op
= NULL
;
104 static void nilfs_btree_clear_path(const struct nilfs_btree
*btree
,
105 struct nilfs_btree_path
*path
)
109 for (level
= NILFS_BTREE_LEVEL_DATA
;
110 level
< NILFS_BTREE_LEVEL_MAX
;
112 if (path
[level
].bp_bh
!= NULL
) {
113 brelse(path
[level
].bp_bh
);
114 path
[level
].bp_bh
= NULL
;
116 /* sib_bh is released or deleted by prepare or commit
118 path
[level
].bp_sib_bh
= NULL
;
119 path
[level
].bp_index
= 0;
120 path
[level
].bp_oldreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
121 path
[level
].bp_newreq
.bpr_ptr
= NILFS_BMAP_INVALID_PTR
;
122 path
[level
].bp_op
= NULL
;
127 * B-tree node operations
129 static int nilfs_btree_get_block(const struct nilfs_btree
*btree
, __u64 ptr
,
130 struct buffer_head
**bhp
)
132 struct address_space
*btnc
=
133 &NILFS_BMAP_I((struct nilfs_bmap
*)btree
)->i_btnode_cache
;
134 return nilfs_btnode_get(btnc
, ptr
, 0, bhp
, 0);
137 static int nilfs_btree_get_new_block(const struct nilfs_btree
*btree
,
138 __u64 ptr
, struct buffer_head
**bhp
)
140 struct address_space
*btnc
=
141 &NILFS_BMAP_I((struct nilfs_bmap
*)btree
)->i_btnode_cache
;
144 ret
= nilfs_btnode_get(btnc
, ptr
, 0, bhp
, 1);
146 set_buffer_nilfs_volatile(*bhp
);
151 nilfs_btree_node_get_flags(const struct nilfs_btree
*btree
,
152 const struct nilfs_btree_node
*node
)
154 return node
->bn_flags
;
158 nilfs_btree_node_set_flags(struct nilfs_btree
*btree
,
159 struct nilfs_btree_node
*node
,
162 node
->bn_flags
= flags
;
165 static inline int nilfs_btree_node_root(const struct nilfs_btree
*btree
,
166 const struct nilfs_btree_node
*node
)
168 return nilfs_btree_node_get_flags(btree
, node
) & NILFS_BTREE_NODE_ROOT
;
172 nilfs_btree_node_get_level(const struct nilfs_btree
*btree
,
173 const struct nilfs_btree_node
*node
)
175 return node
->bn_level
;
179 nilfs_btree_node_set_level(struct nilfs_btree
*btree
,
180 struct nilfs_btree_node
*node
,
183 node
->bn_level
= level
;
187 nilfs_btree_node_get_nchildren(const struct nilfs_btree
*btree
,
188 const struct nilfs_btree_node
*node
)
190 return le16_to_cpu(node
->bn_nchildren
);
194 nilfs_btree_node_set_nchildren(struct nilfs_btree
*btree
,
195 struct nilfs_btree_node
*node
,
198 node
->bn_nchildren
= cpu_to_le16(nchildren
);
202 nilfs_btree_node_size(const struct nilfs_btree
*btree
)
204 return 1 << btree
->bt_bmap
.b_inode
->i_blkbits
;
208 nilfs_btree_node_nchildren_min(const struct nilfs_btree
*btree
,
209 const struct nilfs_btree_node
*node
)
211 return nilfs_btree_node_root(btree
, node
) ?
212 NILFS_BTREE_ROOT_NCHILDREN_MIN
:
213 NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree
));
217 nilfs_btree_node_nchildren_max(const struct nilfs_btree
*btree
,
218 const struct nilfs_btree_node
*node
)
220 return nilfs_btree_node_root(btree
, node
) ?
221 NILFS_BTREE_ROOT_NCHILDREN_MAX
:
222 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree
));
225 static inline __le64
*
226 nilfs_btree_node_dkeys(const struct nilfs_btree
*btree
,
227 const struct nilfs_btree_node
*node
)
229 return (__le64
*)((char *)(node
+ 1) +
230 (nilfs_btree_node_root(btree
, node
) ?
231 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE
));
234 static inline __le64
*
235 nilfs_btree_node_dptrs(const struct nilfs_btree
*btree
,
236 const struct nilfs_btree_node
*node
)
238 return (__le64
*)(nilfs_btree_node_dkeys(btree
, node
) +
239 nilfs_btree_node_nchildren_max(btree
, node
));
243 nilfs_btree_node_get_key(const struct nilfs_btree
*btree
,
244 const struct nilfs_btree_node
*node
, int index
)
246 return nilfs_bmap_dkey_to_key(*(nilfs_btree_node_dkeys(btree
, node
) +
251 nilfs_btree_node_set_key(struct nilfs_btree
*btree
,
252 struct nilfs_btree_node
*node
, int index
, __u64 key
)
254 *(nilfs_btree_node_dkeys(btree
, node
) + index
) =
255 nilfs_bmap_key_to_dkey(key
);
259 nilfs_btree_node_get_ptr(const struct nilfs_btree
*btree
,
260 const struct nilfs_btree_node
*node
,
263 return nilfs_bmap_dptr_to_ptr(*(nilfs_btree_node_dptrs(btree
, node
) +
268 nilfs_btree_node_set_ptr(struct nilfs_btree
*btree
,
269 struct nilfs_btree_node
*node
,
273 *(nilfs_btree_node_dptrs(btree
, node
) + index
) =
274 nilfs_bmap_ptr_to_dptr(ptr
);
277 static void nilfs_btree_node_init(struct nilfs_btree
*btree
,
278 struct nilfs_btree_node
*node
,
279 int flags
, int level
, int nchildren
,
280 const __u64
*keys
, const __u64
*ptrs
)
286 nilfs_btree_node_set_flags(btree
, node
, flags
);
287 nilfs_btree_node_set_level(btree
, node
, level
);
288 nilfs_btree_node_set_nchildren(btree
, node
, nchildren
);
290 dkeys
= nilfs_btree_node_dkeys(btree
, node
);
291 dptrs
= nilfs_btree_node_dptrs(btree
, node
);
292 for (i
= 0; i
< nchildren
; i
++) {
293 dkeys
[i
] = nilfs_bmap_key_to_dkey(keys
[i
]);
294 dptrs
[i
] = nilfs_bmap_ptr_to_dptr(ptrs
[i
]);
298 /* Assume the buffer heads corresponding to left and right are locked. */
299 static void nilfs_btree_node_move_left(struct nilfs_btree
*btree
,
300 struct nilfs_btree_node
*left
,
301 struct nilfs_btree_node
*right
,
304 __le64
*ldkeys
, *rdkeys
;
305 __le64
*ldptrs
, *rdptrs
;
306 int lnchildren
, rnchildren
;
308 ldkeys
= nilfs_btree_node_dkeys(btree
, left
);
309 ldptrs
= nilfs_btree_node_dptrs(btree
, left
);
310 lnchildren
= nilfs_btree_node_get_nchildren(btree
, left
);
312 rdkeys
= nilfs_btree_node_dkeys(btree
, right
);
313 rdptrs
= nilfs_btree_node_dptrs(btree
, right
);
314 rnchildren
= nilfs_btree_node_get_nchildren(btree
, right
);
316 memcpy(ldkeys
+ lnchildren
, rdkeys
, n
* sizeof(*rdkeys
));
317 memcpy(ldptrs
+ lnchildren
, rdptrs
, n
* sizeof(*rdptrs
));
318 memmove(rdkeys
, rdkeys
+ n
, (rnchildren
- n
) * sizeof(*rdkeys
));
319 memmove(rdptrs
, rdptrs
+ n
, (rnchildren
- n
) * sizeof(*rdptrs
));
323 nilfs_btree_node_set_nchildren(btree
, left
, lnchildren
);
324 nilfs_btree_node_set_nchildren(btree
, right
, rnchildren
);
327 /* Assume that the buffer heads corresponding to left and right are locked. */
328 static void nilfs_btree_node_move_right(struct nilfs_btree
*btree
,
329 struct nilfs_btree_node
*left
,
330 struct nilfs_btree_node
*right
,
333 __le64
*ldkeys
, *rdkeys
;
334 __le64
*ldptrs
, *rdptrs
;
335 int lnchildren
, rnchildren
;
337 ldkeys
= nilfs_btree_node_dkeys(btree
, left
);
338 ldptrs
= nilfs_btree_node_dptrs(btree
, left
);
339 lnchildren
= nilfs_btree_node_get_nchildren(btree
, left
);
341 rdkeys
= nilfs_btree_node_dkeys(btree
, right
);
342 rdptrs
= nilfs_btree_node_dptrs(btree
, right
);
343 rnchildren
= nilfs_btree_node_get_nchildren(btree
, right
);
345 memmove(rdkeys
+ n
, rdkeys
, rnchildren
* sizeof(*rdkeys
));
346 memmove(rdptrs
+ n
, rdptrs
, rnchildren
* sizeof(*rdptrs
));
347 memcpy(rdkeys
, ldkeys
+ lnchildren
- n
, n
* sizeof(*rdkeys
));
348 memcpy(rdptrs
, ldptrs
+ lnchildren
- n
, n
* sizeof(*rdptrs
));
352 nilfs_btree_node_set_nchildren(btree
, left
, lnchildren
);
353 nilfs_btree_node_set_nchildren(btree
, right
, rnchildren
);
356 /* Assume that the buffer head corresponding to node is locked. */
357 static void nilfs_btree_node_insert(struct nilfs_btree
*btree
,
358 struct nilfs_btree_node
*node
,
359 __u64 key
, __u64 ptr
, int index
)
365 dkeys
= nilfs_btree_node_dkeys(btree
, node
);
366 dptrs
= nilfs_btree_node_dptrs(btree
, node
);
367 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
368 if (index
< nchildren
) {
369 memmove(dkeys
+ index
+ 1, dkeys
+ index
,
370 (nchildren
- index
) * sizeof(*dkeys
));
371 memmove(dptrs
+ index
+ 1, dptrs
+ index
,
372 (nchildren
- index
) * sizeof(*dptrs
));
374 dkeys
[index
] = nilfs_bmap_key_to_dkey(key
);
375 dptrs
[index
] = nilfs_bmap_ptr_to_dptr(ptr
);
377 nilfs_btree_node_set_nchildren(btree
, node
, nchildren
);
380 /* Assume that the buffer head corresponding to node is locked. */
381 static void nilfs_btree_node_delete(struct nilfs_btree
*btree
,
382 struct nilfs_btree_node
*node
,
383 __u64
*keyp
, __u64
*ptrp
, int index
)
391 dkeys
= nilfs_btree_node_dkeys(btree
, node
);
392 dptrs
= nilfs_btree_node_dptrs(btree
, node
);
393 key
= nilfs_bmap_dkey_to_key(dkeys
[index
]);
394 ptr
= nilfs_bmap_dptr_to_ptr(dptrs
[index
]);
395 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
401 if (index
< nchildren
- 1) {
402 memmove(dkeys
+ index
, dkeys
+ index
+ 1,
403 (nchildren
- index
- 1) * sizeof(*dkeys
));
404 memmove(dptrs
+ index
, dptrs
+ index
+ 1,
405 (nchildren
- index
- 1) * sizeof(*dptrs
));
408 nilfs_btree_node_set_nchildren(btree
, node
, nchildren
);
411 static int nilfs_btree_node_lookup(const struct nilfs_btree
*btree
,
412 const struct nilfs_btree_node
*node
,
413 __u64 key
, int *indexp
)
416 int index
, low
, high
, s
;
420 high
= nilfs_btree_node_get_nchildren(btree
, node
) - 1;
423 while (low
<= high
) {
424 index
= (low
+ high
) / 2;
425 nkey
= nilfs_btree_node_get_key(btree
, node
, index
);
429 } else if (nkey
< key
) {
439 if (nilfs_btree_node_get_level(btree
, node
) >
440 NILFS_BTREE_LEVEL_NODE_MIN
) {
441 if ((s
> 0) && (index
> 0))
452 static inline struct nilfs_btree_node
*
453 nilfs_btree_get_root(const struct nilfs_btree
*btree
)
455 return (struct nilfs_btree_node
*)btree
->bt_bmap
.b_u
.u_data
;
458 static inline struct nilfs_btree_node
*
459 nilfs_btree_get_nonroot_node(const struct nilfs_btree
*btree
,
460 const struct nilfs_btree_path
*path
,
463 return (struct nilfs_btree_node
*)path
[level
].bp_bh
->b_data
;
466 static inline struct nilfs_btree_node
*
467 nilfs_btree_get_sib_node(const struct nilfs_btree
*btree
,
468 const struct nilfs_btree_path
*path
,
471 return (struct nilfs_btree_node
*)path
[level
].bp_sib_bh
->b_data
;
474 static inline int nilfs_btree_height(const struct nilfs_btree
*btree
)
476 return nilfs_btree_node_get_level(btree
, nilfs_btree_get_root(btree
))
480 static inline struct nilfs_btree_node
*
481 nilfs_btree_get_node(const struct nilfs_btree
*btree
,
482 const struct nilfs_btree_path
*path
,
485 return (level
== nilfs_btree_height(btree
) - 1) ?
486 nilfs_btree_get_root(btree
) :
487 nilfs_btree_get_nonroot_node(btree
, path
, level
);
490 static int nilfs_btree_do_lookup(const struct nilfs_btree
*btree
,
491 struct nilfs_btree_path
*path
,
492 __u64 key
, __u64
*ptrp
, int minlevel
)
494 struct nilfs_btree_node
*node
;
496 int level
, index
, found
, ret
;
498 node
= nilfs_btree_get_root(btree
);
499 level
= nilfs_btree_node_get_level(btree
, node
);
500 if ((level
< minlevel
) ||
501 (nilfs_btree_node_get_nchildren(btree
, node
) <= 0))
504 found
= nilfs_btree_node_lookup(btree
, node
, key
, &index
);
505 ptr
= nilfs_btree_node_get_ptr(btree
, node
, index
);
506 path
[level
].bp_bh
= NULL
;
507 path
[level
].bp_index
= index
;
509 for (level
--; level
>= minlevel
; level
--) {
510 ret
= nilfs_btree_get_block(btree
, ptr
, &path
[level
].bp_bh
);
513 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
514 BUG_ON(level
!= nilfs_btree_node_get_level(btree
, node
));
516 found
= nilfs_btree_node_lookup(btree
, node
, key
,
520 if (index
< nilfs_btree_node_nchildren_max(btree
, node
))
521 ptr
= nilfs_btree_node_get_ptr(btree
, node
, index
);
523 WARN_ON(found
|| level
!= NILFS_BTREE_LEVEL_NODE_MIN
);
525 ptr
= NILFS_BMAP_INVALID_PTR
;
527 path
[level
].bp_index
= index
;
538 static int nilfs_btree_do_lookup_last(const struct nilfs_btree
*btree
,
539 struct nilfs_btree_path
*path
,
540 __u64
*keyp
, __u64
*ptrp
)
542 struct nilfs_btree_node
*node
;
544 int index
, level
, ret
;
546 node
= nilfs_btree_get_root(btree
);
547 index
= nilfs_btree_node_get_nchildren(btree
, node
) - 1;
550 level
= nilfs_btree_node_get_level(btree
, node
);
551 ptr
= nilfs_btree_node_get_ptr(btree
, node
, index
);
552 path
[level
].bp_bh
= NULL
;
553 path
[level
].bp_index
= index
;
555 for (level
--; level
> 0; level
--) {
556 ret
= nilfs_btree_get_block(btree
, ptr
, &path
[level
].bp_bh
);
559 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
560 BUG_ON(level
!= nilfs_btree_node_get_level(btree
, node
));
561 index
= nilfs_btree_node_get_nchildren(btree
, node
) - 1;
562 ptr
= nilfs_btree_node_get_ptr(btree
, node
, index
);
563 path
[level
].bp_index
= index
;
567 *keyp
= nilfs_btree_node_get_key(btree
, node
, index
);
574 static int nilfs_btree_lookup(const struct nilfs_bmap
*bmap
,
575 __u64 key
, int level
, __u64
*ptrp
)
577 struct nilfs_btree
*btree
;
578 struct nilfs_btree_path
*path
;
582 btree
= (struct nilfs_btree
*)bmap
;
583 path
= nilfs_btree_alloc_path(btree
);
586 nilfs_btree_init_path(btree
, path
);
588 ret
= nilfs_btree_do_lookup(btree
, path
, key
, &ptr
, level
);
593 nilfs_btree_clear_path(btree
, path
);
594 nilfs_btree_free_path(btree
, path
);
599 static int nilfs_btree_lookup_contig(const struct nilfs_bmap
*bmap
,
600 __u64 key
, __u64
*ptrp
, unsigned maxblocks
)
602 struct nilfs_btree
*btree
= (struct nilfs_btree
*)bmap
;
603 struct nilfs_btree_path
*path
;
604 struct nilfs_btree_node
*node
;
605 struct inode
*dat
= NULL
;
608 int level
= NILFS_BTREE_LEVEL_NODE_MIN
;
609 int ret
, cnt
, index
, maxlevel
;
611 path
= nilfs_btree_alloc_path(btree
);
614 nilfs_btree_init_path(btree
, path
);
615 ret
= nilfs_btree_do_lookup(btree
, path
, key
, &ptr
, level
);
619 if (NILFS_BMAP_USE_VBN(bmap
)) {
620 dat
= nilfs_bmap_get_dat(bmap
);
621 ret
= nilfs_dat_translate(dat
, ptr
, &blocknr
);
627 if (cnt
== maxblocks
)
630 maxlevel
= nilfs_btree_height(btree
) - 1;
631 node
= nilfs_btree_get_node(btree
, path
, level
);
632 index
= path
[level
].bp_index
+ 1;
634 while (index
< nilfs_btree_node_get_nchildren(btree
, node
)) {
635 if (nilfs_btree_node_get_key(btree
, node
, index
) !=
638 ptr2
= nilfs_btree_node_get_ptr(btree
, node
, index
);
640 ret
= nilfs_dat_translate(dat
, ptr2
, &blocknr
);
645 if (ptr2
!= ptr
+ cnt
|| ++cnt
== maxblocks
)
650 if (level
== maxlevel
)
653 /* look-up right sibling node */
654 node
= nilfs_btree_get_node(btree
, path
, level
+ 1);
655 index
= path
[level
+ 1].bp_index
+ 1;
656 if (index
>= nilfs_btree_node_get_nchildren(btree
, node
) ||
657 nilfs_btree_node_get_key(btree
, node
, index
) != key
+ cnt
)
659 ptr2
= nilfs_btree_node_get_ptr(btree
, node
, index
);
660 path
[level
+ 1].bp_index
= index
;
662 brelse(path
[level
].bp_bh
);
663 path
[level
].bp_bh
= NULL
;
664 ret
= nilfs_btree_get_block(btree
, ptr2
, &path
[level
].bp_bh
);
667 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
669 path
[level
].bp_index
= index
;
675 nilfs_btree_clear_path(btree
, path
);
676 nilfs_btree_free_path(btree
, path
);
680 static void nilfs_btree_promote_key(struct nilfs_btree
*btree
,
681 struct nilfs_btree_path
*path
,
682 int level
, __u64 key
)
684 if (level
< nilfs_btree_height(btree
) - 1) {
686 lock_buffer(path
[level
].bp_bh
);
687 nilfs_btree_node_set_key(
689 nilfs_btree_get_nonroot_node(
691 path
[level
].bp_index
, key
);
692 if (!buffer_dirty(path
[level
].bp_bh
))
693 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
694 unlock_buffer(path
[level
].bp_bh
);
695 } while ((path
[level
].bp_index
== 0) &&
696 (++level
< nilfs_btree_height(btree
) - 1));
700 if (level
== nilfs_btree_height(btree
) - 1) {
701 nilfs_btree_node_set_key(btree
,
702 nilfs_btree_get_root(btree
),
703 path
[level
].bp_index
, key
);
707 static void nilfs_btree_do_insert(struct nilfs_btree
*btree
,
708 struct nilfs_btree_path
*path
,
709 int level
, __u64
*keyp
, __u64
*ptrp
)
711 struct nilfs_btree_node
*node
;
713 if (level
< nilfs_btree_height(btree
) - 1) {
714 lock_buffer(path
[level
].bp_bh
);
715 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
716 nilfs_btree_node_insert(btree
, node
, *keyp
, *ptrp
,
717 path
[level
].bp_index
);
718 if (!buffer_dirty(path
[level
].bp_bh
))
719 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
720 unlock_buffer(path
[level
].bp_bh
);
722 if (path
[level
].bp_index
== 0)
723 nilfs_btree_promote_key(btree
, path
, level
+ 1,
724 nilfs_btree_node_get_key(
727 node
= nilfs_btree_get_root(btree
);
728 nilfs_btree_node_insert(btree
, node
, *keyp
, *ptrp
,
729 path
[level
].bp_index
);
733 static void nilfs_btree_carry_left(struct nilfs_btree
*btree
,
734 struct nilfs_btree_path
*path
,
735 int level
, __u64
*keyp
, __u64
*ptrp
)
737 struct nilfs_btree_node
*node
, *left
;
738 int nchildren
, lnchildren
, n
, move
;
740 lock_buffer(path
[level
].bp_bh
);
741 lock_buffer(path
[level
].bp_sib_bh
);
743 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
744 left
= nilfs_btree_get_sib_node(btree
, path
, level
);
745 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
746 lnchildren
= nilfs_btree_node_get_nchildren(btree
, left
);
749 n
= (nchildren
+ lnchildren
+ 1) / 2 - lnchildren
;
750 if (n
> path
[level
].bp_index
) {
751 /* move insert point */
756 nilfs_btree_node_move_left(btree
, left
, node
, n
);
758 if (!buffer_dirty(path
[level
].bp_bh
))
759 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
760 if (!buffer_dirty(path
[level
].bp_sib_bh
))
761 nilfs_btnode_mark_dirty(path
[level
].bp_sib_bh
);
763 unlock_buffer(path
[level
].bp_bh
);
764 unlock_buffer(path
[level
].bp_sib_bh
);
766 nilfs_btree_promote_key(btree
, path
, level
+ 1,
767 nilfs_btree_node_get_key(btree
, node
, 0));
770 brelse(path
[level
].bp_bh
);
771 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
772 path
[level
].bp_sib_bh
= NULL
;
773 path
[level
].bp_index
+= lnchildren
;
774 path
[level
+ 1].bp_index
--;
776 brelse(path
[level
].bp_sib_bh
);
777 path
[level
].bp_sib_bh
= NULL
;
778 path
[level
].bp_index
-= n
;
781 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
784 static void nilfs_btree_carry_right(struct nilfs_btree
*btree
,
785 struct nilfs_btree_path
*path
,
786 int level
, __u64
*keyp
, __u64
*ptrp
)
788 struct nilfs_btree_node
*node
, *right
;
789 int nchildren
, rnchildren
, n
, move
;
791 lock_buffer(path
[level
].bp_bh
);
792 lock_buffer(path
[level
].bp_sib_bh
);
794 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
795 right
= nilfs_btree_get_sib_node(btree
, path
, level
);
796 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
797 rnchildren
= nilfs_btree_node_get_nchildren(btree
, right
);
800 n
= (nchildren
+ rnchildren
+ 1) / 2 - rnchildren
;
801 if (n
> nchildren
- path
[level
].bp_index
) {
802 /* move insert point */
807 nilfs_btree_node_move_right(btree
, node
, right
, n
);
809 if (!buffer_dirty(path
[level
].bp_bh
))
810 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
811 if (!buffer_dirty(path
[level
].bp_sib_bh
))
812 nilfs_btnode_mark_dirty(path
[level
].bp_sib_bh
);
814 unlock_buffer(path
[level
].bp_bh
);
815 unlock_buffer(path
[level
].bp_sib_bh
);
817 path
[level
+ 1].bp_index
++;
818 nilfs_btree_promote_key(btree
, path
, level
+ 1,
819 nilfs_btree_node_get_key(btree
, right
, 0));
820 path
[level
+ 1].bp_index
--;
823 brelse(path
[level
].bp_bh
);
824 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
825 path
[level
].bp_sib_bh
= NULL
;
826 path
[level
].bp_index
-=
827 nilfs_btree_node_get_nchildren(btree
, node
);
828 path
[level
+ 1].bp_index
++;
830 brelse(path
[level
].bp_sib_bh
);
831 path
[level
].bp_sib_bh
= NULL
;
834 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
837 static void nilfs_btree_split(struct nilfs_btree
*btree
,
838 struct nilfs_btree_path
*path
,
839 int level
, __u64
*keyp
, __u64
*ptrp
)
841 struct nilfs_btree_node
*node
, *right
;
844 int nchildren
, n
, move
;
846 lock_buffer(path
[level
].bp_bh
);
847 lock_buffer(path
[level
].bp_sib_bh
);
849 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
850 right
= nilfs_btree_get_sib_node(btree
, path
, level
);
851 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
854 n
= (nchildren
+ 1) / 2;
855 if (n
> nchildren
- path
[level
].bp_index
) {
860 nilfs_btree_node_move_right(btree
, node
, right
, n
);
862 if (!buffer_dirty(path
[level
].bp_bh
))
863 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
864 if (!buffer_dirty(path
[level
].bp_sib_bh
))
865 nilfs_btnode_mark_dirty(path
[level
].bp_sib_bh
);
867 unlock_buffer(path
[level
].bp_bh
);
868 unlock_buffer(path
[level
].bp_sib_bh
);
870 newkey
= nilfs_btree_node_get_key(btree
, right
, 0);
871 newptr
= path
[level
].bp_newreq
.bpr_ptr
;
874 path
[level
].bp_index
-=
875 nilfs_btree_node_get_nchildren(btree
, node
);
876 nilfs_btree_node_insert(btree
, right
, *keyp
, *ptrp
,
877 path
[level
].bp_index
);
879 *keyp
= nilfs_btree_node_get_key(btree
, right
, 0);
880 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
882 brelse(path
[level
].bp_bh
);
883 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
884 path
[level
].bp_sib_bh
= NULL
;
886 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
888 *keyp
= nilfs_btree_node_get_key(btree
, right
, 0);
889 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
891 brelse(path
[level
].bp_sib_bh
);
892 path
[level
].bp_sib_bh
= NULL
;
895 path
[level
+ 1].bp_index
++;
898 static void nilfs_btree_grow(struct nilfs_btree
*btree
,
899 struct nilfs_btree_path
*path
,
900 int level
, __u64
*keyp
, __u64
*ptrp
)
902 struct nilfs_btree_node
*root
, *child
;
905 lock_buffer(path
[level
].bp_sib_bh
);
907 root
= nilfs_btree_get_root(btree
);
908 child
= nilfs_btree_get_sib_node(btree
, path
, level
);
910 n
= nilfs_btree_node_get_nchildren(btree
, root
);
912 nilfs_btree_node_move_right(btree
, root
, child
, n
);
913 nilfs_btree_node_set_level(btree
, root
, level
+ 1);
915 if (!buffer_dirty(path
[level
].bp_sib_bh
))
916 nilfs_btnode_mark_dirty(path
[level
].bp_sib_bh
);
918 unlock_buffer(path
[level
].bp_sib_bh
);
920 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
921 path
[level
].bp_sib_bh
= NULL
;
923 nilfs_btree_do_insert(btree
, path
, level
, keyp
, ptrp
);
925 *keyp
= nilfs_btree_node_get_key(btree
, child
, 0);
926 *ptrp
= path
[level
].bp_newreq
.bpr_ptr
;
929 static __u64
nilfs_btree_find_near(const struct nilfs_btree
*btree
,
930 const struct nilfs_btree_path
*path
)
932 struct nilfs_btree_node
*node
;
936 return NILFS_BMAP_INVALID_PTR
;
939 level
= NILFS_BTREE_LEVEL_NODE_MIN
;
940 if (path
[level
].bp_index
> 0) {
941 node
= nilfs_btree_get_node(btree
, path
, level
);
942 return nilfs_btree_node_get_ptr(btree
, node
,
943 path
[level
].bp_index
- 1);
947 level
= NILFS_BTREE_LEVEL_NODE_MIN
+ 1;
948 if (level
<= nilfs_btree_height(btree
) - 1) {
949 node
= nilfs_btree_get_node(btree
, path
, level
);
950 return nilfs_btree_node_get_ptr(btree
, node
,
951 path
[level
].bp_index
);
954 return NILFS_BMAP_INVALID_PTR
;
957 static __u64
nilfs_btree_find_target_v(const struct nilfs_btree
*btree
,
958 const struct nilfs_btree_path
*path
,
963 ptr
= nilfs_bmap_find_target_seq(&btree
->bt_bmap
, key
);
964 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
965 /* sequential access */
968 ptr
= nilfs_btree_find_near(btree
, path
);
969 if (ptr
!= NILFS_BMAP_INVALID_PTR
)
974 return nilfs_bmap_find_target_in_group(&btree
->bt_bmap
);
977 static void nilfs_btree_set_target_v(struct nilfs_btree
*btree
, __u64 key
,
980 btree
->bt_bmap
.b_last_allocated_key
= key
;
981 btree
->bt_bmap
.b_last_allocated_ptr
= ptr
;
984 static int nilfs_btree_prepare_insert(struct nilfs_btree
*btree
,
985 struct nilfs_btree_path
*path
,
986 int *levelp
, __u64 key
, __u64 ptr
,
987 struct nilfs_bmap_stats
*stats
)
989 struct buffer_head
*bh
;
990 struct nilfs_btree_node
*node
, *parent
, *sib
;
992 int pindex
, level
, ret
;
994 stats
->bs_nblocks
= 0;
995 level
= NILFS_BTREE_LEVEL_DATA
;
997 /* allocate a new ptr for data block */
998 if (NILFS_BMAP_USE_VBN(&btree
->bt_bmap
))
999 path
[level
].bp_newreq
.bpr_ptr
=
1000 nilfs_btree_find_target_v(btree
, path
, key
);
1002 ret
= nilfs_bmap_prepare_alloc_ptr(&btree
->bt_bmap
,
1003 &path
[level
].bp_newreq
);
1007 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
1008 level
< nilfs_btree_height(btree
) - 1;
1010 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1011 if (nilfs_btree_node_get_nchildren(btree
, node
) <
1012 nilfs_btree_node_nchildren_max(btree
, node
)) {
1013 path
[level
].bp_op
= nilfs_btree_do_insert
;
1014 stats
->bs_nblocks
++;
1018 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1);
1019 pindex
= path
[level
+ 1].bp_index
;
1023 sibptr
= nilfs_btree_node_get_ptr(btree
, parent
,
1025 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1027 goto err_out_child_node
;
1028 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1029 if (nilfs_btree_node_get_nchildren(btree
, sib
) <
1030 nilfs_btree_node_nchildren_max(btree
, sib
)) {
1031 path
[level
].bp_sib_bh
= bh
;
1032 path
[level
].bp_op
= nilfs_btree_carry_left
;
1033 stats
->bs_nblocks
++;
1041 nilfs_btree_node_get_nchildren(btree
, parent
) - 1) {
1042 sibptr
= nilfs_btree_node_get_ptr(btree
, parent
,
1044 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1046 goto err_out_child_node
;
1047 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1048 if (nilfs_btree_node_get_nchildren(btree
, sib
) <
1049 nilfs_btree_node_nchildren_max(btree
, sib
)) {
1050 path
[level
].bp_sib_bh
= bh
;
1051 path
[level
].bp_op
= nilfs_btree_carry_right
;
1052 stats
->bs_nblocks
++;
1059 path
[level
].bp_newreq
.bpr_ptr
=
1060 path
[level
- 1].bp_newreq
.bpr_ptr
+ 1;
1061 ret
= nilfs_bmap_prepare_alloc_ptr(&btree
->bt_bmap
,
1062 &path
[level
].bp_newreq
);
1064 goto err_out_child_node
;
1065 ret
= nilfs_btree_get_new_block(btree
,
1066 path
[level
].bp_newreq
.bpr_ptr
,
1069 goto err_out_curr_node
;
1071 stats
->bs_nblocks
++;
1074 nilfs_btree_node_init(btree
,
1075 (struct nilfs_btree_node
*)bh
->b_data
,
1076 0, level
, 0, NULL
, NULL
);
1078 path
[level
].bp_sib_bh
= bh
;
1079 path
[level
].bp_op
= nilfs_btree_split
;
1083 node
= nilfs_btree_get_root(btree
);
1084 if (nilfs_btree_node_get_nchildren(btree
, node
) <
1085 nilfs_btree_node_nchildren_max(btree
, node
)) {
1086 path
[level
].bp_op
= nilfs_btree_do_insert
;
1087 stats
->bs_nblocks
++;
1092 path
[level
].bp_newreq
.bpr_ptr
= path
[level
- 1].bp_newreq
.bpr_ptr
+ 1;
1093 ret
= nilfs_bmap_prepare_alloc_ptr(&btree
->bt_bmap
,
1094 &path
[level
].bp_newreq
);
1096 goto err_out_child_node
;
1097 ret
= nilfs_btree_get_new_block(btree
, path
[level
].bp_newreq
.bpr_ptr
,
1100 goto err_out_curr_node
;
1103 nilfs_btree_node_init(btree
, (struct nilfs_btree_node
*)bh
->b_data
,
1104 0, level
, 0, NULL
, NULL
);
1106 path
[level
].bp_sib_bh
= bh
;
1107 path
[level
].bp_op
= nilfs_btree_grow
;
1110 path
[level
].bp_op
= nilfs_btree_do_insert
;
1112 /* a newly-created node block and a data block are added */
1113 stats
->bs_nblocks
+= 2;
1122 nilfs_bmap_abort_alloc_ptr(&btree
->bt_bmap
, &path
[level
].bp_newreq
);
1124 for (level
--; level
> NILFS_BTREE_LEVEL_DATA
; level
--) {
1125 nilfs_btnode_delete(path
[level
].bp_sib_bh
);
1126 nilfs_bmap_abort_alloc_ptr(&btree
->bt_bmap
,
1127 &path
[level
].bp_newreq
);
1131 nilfs_bmap_abort_alloc_ptr(&btree
->bt_bmap
, &path
[level
].bp_newreq
);
1134 stats
->bs_nblocks
= 0;
1138 static void nilfs_btree_commit_insert(struct nilfs_btree
*btree
,
1139 struct nilfs_btree_path
*path
,
1140 int maxlevel
, __u64 key
, __u64 ptr
)
1144 set_buffer_nilfs_volatile((struct buffer_head
*)((unsigned long)ptr
));
1145 ptr
= path
[NILFS_BTREE_LEVEL_DATA
].bp_newreq
.bpr_ptr
;
1146 if (NILFS_BMAP_USE_VBN(&btree
->bt_bmap
))
1147 nilfs_btree_set_target_v(btree
, key
, ptr
);
1149 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
; level
<= maxlevel
; level
++) {
1150 nilfs_bmap_commit_alloc_ptr(&btree
->bt_bmap
,
1151 &path
[level
- 1].bp_newreq
);
1152 path
[level
].bp_op(btree
, path
, level
, &key
, &ptr
);
1155 if (!nilfs_bmap_dirty(&btree
->bt_bmap
))
1156 nilfs_bmap_set_dirty(&btree
->bt_bmap
);
1159 static int nilfs_btree_insert(struct nilfs_bmap
*bmap
, __u64 key
, __u64 ptr
)
1161 struct nilfs_btree
*btree
;
1162 struct nilfs_btree_path
*path
;
1163 struct nilfs_bmap_stats stats
;
1166 btree
= (struct nilfs_btree
*)bmap
;
1167 path
= nilfs_btree_alloc_path(btree
);
1170 nilfs_btree_init_path(btree
, path
);
1172 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
,
1173 NILFS_BTREE_LEVEL_NODE_MIN
);
1174 if (ret
!= -ENOENT
) {
1180 ret
= nilfs_btree_prepare_insert(btree
, path
, &level
, key
, ptr
, &stats
);
1183 nilfs_btree_commit_insert(btree
, path
, level
, key
, ptr
);
1184 nilfs_bmap_add_blocks(bmap
, stats
.bs_nblocks
);
1187 nilfs_btree_clear_path(btree
, path
);
1188 nilfs_btree_free_path(btree
, path
);
1192 static void nilfs_btree_do_delete(struct nilfs_btree
*btree
,
1193 struct nilfs_btree_path
*path
,
1194 int level
, __u64
*keyp
, __u64
*ptrp
)
1196 struct nilfs_btree_node
*node
;
1198 if (level
< nilfs_btree_height(btree
) - 1) {
1199 lock_buffer(path
[level
].bp_bh
);
1200 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1201 nilfs_btree_node_delete(btree
, node
, keyp
, ptrp
,
1202 path
[level
].bp_index
);
1203 if (!buffer_dirty(path
[level
].bp_bh
))
1204 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
1205 unlock_buffer(path
[level
].bp_bh
);
1206 if (path
[level
].bp_index
== 0)
1207 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1208 nilfs_btree_node_get_key(btree
, node
, 0));
1210 node
= nilfs_btree_get_root(btree
);
1211 nilfs_btree_node_delete(btree
, node
, keyp
, ptrp
,
1212 path
[level
].bp_index
);
1216 static void nilfs_btree_borrow_left(struct nilfs_btree
*btree
,
1217 struct nilfs_btree_path
*path
,
1218 int level
, __u64
*keyp
, __u64
*ptrp
)
1220 struct nilfs_btree_node
*node
, *left
;
1221 int nchildren
, lnchildren
, n
;
1223 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1225 lock_buffer(path
[level
].bp_bh
);
1226 lock_buffer(path
[level
].bp_sib_bh
);
1228 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1229 left
= nilfs_btree_get_sib_node(btree
, path
, level
);
1230 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
1231 lnchildren
= nilfs_btree_node_get_nchildren(btree
, left
);
1233 n
= (nchildren
+ lnchildren
) / 2 - nchildren
;
1235 nilfs_btree_node_move_right(btree
, left
, node
, n
);
1237 if (!buffer_dirty(path
[level
].bp_bh
))
1238 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
1239 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1240 nilfs_btnode_mark_dirty(path
[level
].bp_sib_bh
);
1242 unlock_buffer(path
[level
].bp_bh
);
1243 unlock_buffer(path
[level
].bp_sib_bh
);
1245 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1246 nilfs_btree_node_get_key(btree
, node
, 0));
1248 brelse(path
[level
].bp_sib_bh
);
1249 path
[level
].bp_sib_bh
= NULL
;
1250 path
[level
].bp_index
+= n
;
1253 static void nilfs_btree_borrow_right(struct nilfs_btree
*btree
,
1254 struct nilfs_btree_path
*path
,
1255 int level
, __u64
*keyp
, __u64
*ptrp
)
1257 struct nilfs_btree_node
*node
, *right
;
1258 int nchildren
, rnchildren
, n
;
1260 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1262 lock_buffer(path
[level
].bp_bh
);
1263 lock_buffer(path
[level
].bp_sib_bh
);
1265 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1266 right
= nilfs_btree_get_sib_node(btree
, path
, level
);
1267 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
1268 rnchildren
= nilfs_btree_node_get_nchildren(btree
, right
);
1270 n
= (nchildren
+ rnchildren
) / 2 - nchildren
;
1272 nilfs_btree_node_move_left(btree
, node
, right
, n
);
1274 if (!buffer_dirty(path
[level
].bp_bh
))
1275 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
1276 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1277 nilfs_btnode_mark_dirty(path
[level
].bp_sib_bh
);
1279 unlock_buffer(path
[level
].bp_bh
);
1280 unlock_buffer(path
[level
].bp_sib_bh
);
1282 path
[level
+ 1].bp_index
++;
1283 nilfs_btree_promote_key(btree
, path
, level
+ 1,
1284 nilfs_btree_node_get_key(btree
, right
, 0));
1285 path
[level
+ 1].bp_index
--;
1287 brelse(path
[level
].bp_sib_bh
);
1288 path
[level
].bp_sib_bh
= NULL
;
1291 static void nilfs_btree_concat_left(struct nilfs_btree
*btree
,
1292 struct nilfs_btree_path
*path
,
1293 int level
, __u64
*keyp
, __u64
*ptrp
)
1295 struct nilfs_btree_node
*node
, *left
;
1298 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1300 lock_buffer(path
[level
].bp_bh
);
1301 lock_buffer(path
[level
].bp_sib_bh
);
1303 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1304 left
= nilfs_btree_get_sib_node(btree
, path
, level
);
1306 n
= nilfs_btree_node_get_nchildren(btree
, node
);
1308 nilfs_btree_node_move_left(btree
, left
, node
, n
);
1310 if (!buffer_dirty(path
[level
].bp_sib_bh
))
1311 nilfs_btnode_mark_dirty(path
[level
].bp_sib_bh
);
1313 unlock_buffer(path
[level
].bp_bh
);
1314 unlock_buffer(path
[level
].bp_sib_bh
);
1316 nilfs_btnode_delete(path
[level
].bp_bh
);
1317 path
[level
].bp_bh
= path
[level
].bp_sib_bh
;
1318 path
[level
].bp_sib_bh
= NULL
;
1319 path
[level
].bp_index
+= nilfs_btree_node_get_nchildren(btree
, left
);
1322 static void nilfs_btree_concat_right(struct nilfs_btree
*btree
,
1323 struct nilfs_btree_path
*path
,
1324 int level
, __u64
*keyp
, __u64
*ptrp
)
1326 struct nilfs_btree_node
*node
, *right
;
1329 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1331 lock_buffer(path
[level
].bp_bh
);
1332 lock_buffer(path
[level
].bp_sib_bh
);
1334 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1335 right
= nilfs_btree_get_sib_node(btree
, path
, level
);
1337 n
= nilfs_btree_node_get_nchildren(btree
, right
);
1339 nilfs_btree_node_move_left(btree
, node
, right
, n
);
1341 if (!buffer_dirty(path
[level
].bp_bh
))
1342 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
1344 unlock_buffer(path
[level
].bp_bh
);
1345 unlock_buffer(path
[level
].bp_sib_bh
);
1347 nilfs_btnode_delete(path
[level
].bp_sib_bh
);
1348 path
[level
].bp_sib_bh
= NULL
;
1349 path
[level
+ 1].bp_index
++;
1352 static void nilfs_btree_shrink(struct nilfs_btree
*btree
,
1353 struct nilfs_btree_path
*path
,
1354 int level
, __u64
*keyp
, __u64
*ptrp
)
1356 struct nilfs_btree_node
*root
, *child
;
1359 nilfs_btree_do_delete(btree
, path
, level
, keyp
, ptrp
);
1361 lock_buffer(path
[level
].bp_bh
);
1362 root
= nilfs_btree_get_root(btree
);
1363 child
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1365 nilfs_btree_node_delete(btree
, root
, NULL
, NULL
, 0);
1366 nilfs_btree_node_set_level(btree
, root
, level
);
1367 n
= nilfs_btree_node_get_nchildren(btree
, child
);
1368 nilfs_btree_node_move_left(btree
, root
, child
, n
);
1369 unlock_buffer(path
[level
].bp_bh
);
1371 nilfs_btnode_delete(path
[level
].bp_bh
);
1372 path
[level
].bp_bh
= NULL
;
1376 static int nilfs_btree_prepare_delete(struct nilfs_btree
*btree
,
1377 struct nilfs_btree_path
*path
,
1379 struct nilfs_bmap_stats
*stats
)
1381 struct buffer_head
*bh
;
1382 struct nilfs_btree_node
*node
, *parent
, *sib
;
1384 int pindex
, level
, ret
;
1387 stats
->bs_nblocks
= 0;
1388 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
1389 level
< nilfs_btree_height(btree
) - 1;
1391 node
= nilfs_btree_get_nonroot_node(btree
, path
, level
);
1392 path
[level
].bp_oldreq
.bpr_ptr
=
1393 nilfs_btree_node_get_ptr(btree
, node
,
1394 path
[level
].bp_index
);
1395 ret
= nilfs_bmap_prepare_end_ptr(&btree
->bt_bmap
,
1396 &path
[level
].bp_oldreq
);
1398 goto err_out_child_node
;
1400 if (nilfs_btree_node_get_nchildren(btree
, node
) >
1401 nilfs_btree_node_nchildren_min(btree
, node
)) {
1402 path
[level
].bp_op
= nilfs_btree_do_delete
;
1403 stats
->bs_nblocks
++;
1407 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1);
1408 pindex
= path
[level
+ 1].bp_index
;
1412 sibptr
= nilfs_btree_node_get_ptr(btree
, parent
,
1414 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1416 goto err_out_curr_node
;
1417 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1418 if (nilfs_btree_node_get_nchildren(btree
, sib
) >
1419 nilfs_btree_node_nchildren_min(btree
, sib
)) {
1420 path
[level
].bp_sib_bh
= bh
;
1421 path
[level
].bp_op
= nilfs_btree_borrow_left
;
1422 stats
->bs_nblocks
++;
1425 path
[level
].bp_sib_bh
= bh
;
1426 path
[level
].bp_op
= nilfs_btree_concat_left
;
1427 stats
->bs_nblocks
++;
1431 nilfs_btree_node_get_nchildren(btree
, parent
) - 1) {
1433 sibptr
= nilfs_btree_node_get_ptr(btree
, parent
,
1435 ret
= nilfs_btree_get_block(btree
, sibptr
, &bh
);
1437 goto err_out_curr_node
;
1438 sib
= (struct nilfs_btree_node
*)bh
->b_data
;
1439 if (nilfs_btree_node_get_nchildren(btree
, sib
) >
1440 nilfs_btree_node_nchildren_min(btree
, sib
)) {
1441 path
[level
].bp_sib_bh
= bh
;
1442 path
[level
].bp_op
= nilfs_btree_borrow_right
;
1443 stats
->bs_nblocks
++;
1446 path
[level
].bp_sib_bh
= bh
;
1447 path
[level
].bp_op
= nilfs_btree_concat_right
;
1448 stats
->bs_nblocks
++;
1453 /* the only child of the root node */
1454 WARN_ON(level
!= nilfs_btree_height(btree
) - 2);
1455 if (nilfs_btree_node_get_nchildren(btree
, node
) - 1 <=
1456 NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1457 path
[level
].bp_op
= nilfs_btree_shrink
;
1458 stats
->bs_nblocks
+= 2;
1460 path
[level
].bp_op
= nilfs_btree_do_delete
;
1461 stats
->bs_nblocks
++;
1469 node
= nilfs_btree_get_root(btree
);
1470 path
[level
].bp_oldreq
.bpr_ptr
=
1471 nilfs_btree_node_get_ptr(btree
, node
, path
[level
].bp_index
);
1473 ret
= nilfs_bmap_prepare_end_ptr(&btree
->bt_bmap
,
1474 &path
[level
].bp_oldreq
);
1476 goto err_out_child_node
;
1478 /* child of the root node is deleted */
1479 path
[level
].bp_op
= nilfs_btree_do_delete
;
1480 stats
->bs_nblocks
++;
1489 nilfs_bmap_abort_end_ptr(&btree
->bt_bmap
, &path
[level
].bp_oldreq
);
1491 for (level
--; level
>= NILFS_BTREE_LEVEL_NODE_MIN
; level
--) {
1492 brelse(path
[level
].bp_sib_bh
);
1493 nilfs_bmap_abort_end_ptr(&btree
->bt_bmap
,
1494 &path
[level
].bp_oldreq
);
1497 stats
->bs_nblocks
= 0;
1501 static void nilfs_btree_commit_delete(struct nilfs_btree
*btree
,
1502 struct nilfs_btree_path
*path
,
1507 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
; level
<= maxlevel
; level
++) {
1508 nilfs_bmap_commit_end_ptr(&btree
->bt_bmap
,
1509 &path
[level
].bp_oldreq
);
1510 path
[level
].bp_op(btree
, path
, level
, NULL
, NULL
);
1513 if (!nilfs_bmap_dirty(&btree
->bt_bmap
))
1514 nilfs_bmap_set_dirty(&btree
->bt_bmap
);
1517 static int nilfs_btree_delete(struct nilfs_bmap
*bmap
, __u64 key
)
1520 struct nilfs_btree
*btree
;
1521 struct nilfs_btree_path
*path
;
1522 struct nilfs_bmap_stats stats
;
1525 btree
= (struct nilfs_btree
*)bmap
;
1526 path
= nilfs_btree_alloc_path(btree
);
1529 nilfs_btree_init_path(btree
, path
);
1530 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
,
1531 NILFS_BTREE_LEVEL_NODE_MIN
);
1535 ret
= nilfs_btree_prepare_delete(btree
, path
, &level
, &stats
);
1538 nilfs_btree_commit_delete(btree
, path
, level
);
1539 nilfs_bmap_sub_blocks(bmap
, stats
.bs_nblocks
);
1542 nilfs_btree_clear_path(btree
, path
);
1543 nilfs_btree_free_path(btree
, path
);
1547 static int nilfs_btree_last_key(const struct nilfs_bmap
*bmap
, __u64
*keyp
)
1549 struct nilfs_btree
*btree
;
1550 struct nilfs_btree_path
*path
;
1553 btree
= (struct nilfs_btree
*)bmap
;
1554 path
= nilfs_btree_alloc_path(btree
);
1557 nilfs_btree_init_path(btree
, path
);
1559 ret
= nilfs_btree_do_lookup_last(btree
, path
, keyp
, NULL
);
1561 nilfs_btree_clear_path(btree
, path
);
1562 nilfs_btree_free_path(btree
, path
);
1567 static int nilfs_btree_check_delete(struct nilfs_bmap
*bmap
, __u64 key
)
1569 struct buffer_head
*bh
;
1570 struct nilfs_btree
*btree
;
1571 struct nilfs_btree_node
*root
, *node
;
1572 __u64 maxkey
, nextmaxkey
;
1576 btree
= (struct nilfs_btree
*)bmap
;
1577 root
= nilfs_btree_get_root(btree
);
1578 switch (nilfs_btree_height(btree
)) {
1584 nchildren
= nilfs_btree_node_get_nchildren(btree
, root
);
1587 ptr
= nilfs_btree_node_get_ptr(btree
, root
, nchildren
- 1);
1588 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
1591 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1597 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
1598 maxkey
= nilfs_btree_node_get_key(btree
, node
, nchildren
- 1);
1599 nextmaxkey
= (nchildren
> 1) ?
1600 nilfs_btree_node_get_key(btree
, node
, nchildren
- 2) : 0;
1604 return (maxkey
== key
) && (nextmaxkey
< NILFS_BMAP_LARGE_LOW
);
1607 static int nilfs_btree_gather_data(struct nilfs_bmap
*bmap
,
1608 __u64
*keys
, __u64
*ptrs
, int nitems
)
1610 struct buffer_head
*bh
;
1611 struct nilfs_btree
*btree
;
1612 struct nilfs_btree_node
*node
, *root
;
1616 int nchildren
, i
, ret
;
1618 btree
= (struct nilfs_btree
*)bmap
;
1619 root
= nilfs_btree_get_root(btree
);
1620 switch (nilfs_btree_height(btree
)) {
1626 nchildren
= nilfs_btree_node_get_nchildren(btree
, root
);
1627 WARN_ON(nchildren
> 1);
1628 ptr
= nilfs_btree_node_get_ptr(btree
, root
, nchildren
- 1);
1629 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
1632 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1639 nchildren
= nilfs_btree_node_get_nchildren(btree
, node
);
1640 if (nchildren
< nitems
)
1642 dkeys
= nilfs_btree_node_dkeys(btree
, node
);
1643 dptrs
= nilfs_btree_node_dptrs(btree
, node
);
1644 for (i
= 0; i
< nitems
; i
++) {
1645 keys
[i
] = nilfs_bmap_dkey_to_key(dkeys
[i
]);
1646 ptrs
[i
] = nilfs_bmap_dptr_to_ptr(dptrs
[i
]);
1656 nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap
*bmap
, __u64 key
,
1657 union nilfs_bmap_ptr_req
*dreq
,
1658 union nilfs_bmap_ptr_req
*nreq
,
1659 struct buffer_head
**bhp
,
1660 struct nilfs_bmap_stats
*stats
)
1662 struct buffer_head
*bh
;
1663 struct nilfs_btree
*btree
;
1666 btree
= (struct nilfs_btree
*)bmap
;
1667 stats
->bs_nblocks
= 0;
1670 /* cannot find near ptr */
1671 if (NILFS_BMAP_USE_VBN(bmap
))
1672 dreq
->bpr_ptr
= nilfs_btree_find_target_v(btree
, NULL
, key
);
1674 ret
= nilfs_bmap_prepare_alloc_ptr(bmap
, dreq
);
1679 stats
->bs_nblocks
++;
1681 nreq
->bpr_ptr
= dreq
->bpr_ptr
+ 1;
1682 ret
= nilfs_bmap_prepare_alloc_ptr(bmap
, nreq
);
1686 ret
= nilfs_btree_get_new_block(btree
, nreq
->bpr_ptr
, &bh
);
1691 stats
->bs_nblocks
++;
1699 nilfs_bmap_abort_alloc_ptr(bmap
, nreq
);
1701 nilfs_bmap_abort_alloc_ptr(bmap
, dreq
);
1702 stats
->bs_nblocks
= 0;
1708 nilfs_btree_commit_convert_and_insert(struct nilfs_bmap
*bmap
,
1709 __u64 key
, __u64 ptr
,
1710 const __u64
*keys
, const __u64
*ptrs
,
1712 union nilfs_bmap_ptr_req
*dreq
,
1713 union nilfs_bmap_ptr_req
*nreq
,
1714 struct buffer_head
*bh
)
1716 struct nilfs_btree
*btree
;
1717 struct nilfs_btree_node
*node
;
1720 /* free resources */
1721 if (bmap
->b_ops
->bop_clear
!= NULL
)
1722 bmap
->b_ops
->bop_clear(bmap
);
1724 /* ptr must be a pointer to a buffer head. */
1725 set_buffer_nilfs_volatile((struct buffer_head
*)((unsigned long)ptr
));
1727 /* convert and insert */
1728 btree
= (struct nilfs_btree
*)bmap
;
1729 nilfs_btree_init(bmap
);
1731 nilfs_bmap_commit_alloc_ptr(bmap
, dreq
);
1732 nilfs_bmap_commit_alloc_ptr(bmap
, nreq
);
1734 /* create child node at level 1 */
1736 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1737 nilfs_btree_node_init(btree
, node
, 0, 1, n
, keys
, ptrs
);
1738 nilfs_btree_node_insert(btree
, node
,
1739 key
, dreq
->bpr_ptr
, n
);
1740 if (!buffer_dirty(bh
))
1741 nilfs_btnode_mark_dirty(bh
);
1742 if (!nilfs_bmap_dirty(bmap
))
1743 nilfs_bmap_set_dirty(bmap
);
1748 /* create root node at level 2 */
1749 node
= nilfs_btree_get_root(btree
);
1750 tmpptr
= nreq
->bpr_ptr
;
1751 nilfs_btree_node_init(btree
, node
, NILFS_BTREE_NODE_ROOT
,
1752 2, 1, &keys
[0], &tmpptr
);
1754 nilfs_bmap_commit_alloc_ptr(bmap
, dreq
);
1756 /* create root node at level 1 */
1757 node
= nilfs_btree_get_root(btree
);
1758 nilfs_btree_node_init(btree
, node
, NILFS_BTREE_NODE_ROOT
,
1760 nilfs_btree_node_insert(btree
, node
,
1761 key
, dreq
->bpr_ptr
, n
);
1762 if (!nilfs_bmap_dirty(bmap
))
1763 nilfs_bmap_set_dirty(bmap
);
1766 if (NILFS_BMAP_USE_VBN(bmap
))
1767 nilfs_btree_set_target_v(btree
, key
, dreq
->bpr_ptr
);
1771 * nilfs_btree_convert_and_insert -
1779 int nilfs_btree_convert_and_insert(struct nilfs_bmap
*bmap
,
1780 __u64 key
, __u64 ptr
,
1781 const __u64
*keys
, const __u64
*ptrs
, int n
)
1783 struct buffer_head
*bh
;
1784 union nilfs_bmap_ptr_req dreq
, nreq
, *di
, *ni
;
1785 struct nilfs_bmap_stats stats
;
1788 if (n
+ 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX
) {
1791 } else if ((n
+ 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
1792 1 << bmap
->b_inode
->i_blkbits
)) {
1801 ret
= nilfs_btree_prepare_convert_and_insert(bmap
, key
, di
, ni
, &bh
,
1805 nilfs_btree_commit_convert_and_insert(bmap
, key
, ptr
, keys
, ptrs
, n
,
1807 nilfs_bmap_add_blocks(bmap
, stats
.bs_nblocks
);
1811 static int nilfs_btree_propagate_p(struct nilfs_btree
*btree
,
1812 struct nilfs_btree_path
*path
,
1814 struct buffer_head
*bh
)
1816 while ((++level
< nilfs_btree_height(btree
) - 1) &&
1817 !buffer_dirty(path
[level
].bp_bh
))
1818 nilfs_btnode_mark_dirty(path
[level
].bp_bh
);
1823 static int nilfs_btree_prepare_update_v(struct nilfs_btree
*btree
,
1824 struct nilfs_btree_path
*path
,
1827 struct nilfs_btree_node
*parent
;
1830 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1);
1831 path
[level
].bp_oldreq
.bpr_ptr
=
1832 nilfs_btree_node_get_ptr(btree
, parent
,
1833 path
[level
+ 1].bp_index
);
1834 path
[level
].bp_newreq
.bpr_ptr
= path
[level
].bp_oldreq
.bpr_ptr
+ 1;
1835 ret
= nilfs_bmap_prepare_update_v(&btree
->bt_bmap
,
1836 &path
[level
].bp_oldreq
,
1837 &path
[level
].bp_newreq
);
1841 if (buffer_nilfs_node(path
[level
].bp_bh
)) {
1842 path
[level
].bp_ctxt
.oldkey
= path
[level
].bp_oldreq
.bpr_ptr
;
1843 path
[level
].bp_ctxt
.newkey
= path
[level
].bp_newreq
.bpr_ptr
;
1844 path
[level
].bp_ctxt
.bh
= path
[level
].bp_bh
;
1845 ret
= nilfs_btnode_prepare_change_key(
1846 &NILFS_BMAP_I(&btree
->bt_bmap
)->i_btnode_cache
,
1847 &path
[level
].bp_ctxt
);
1849 nilfs_bmap_abort_update_v(&btree
->bt_bmap
,
1850 &path
[level
].bp_oldreq
,
1851 &path
[level
].bp_newreq
);
1859 static void nilfs_btree_commit_update_v(struct nilfs_btree
*btree
,
1860 struct nilfs_btree_path
*path
,
1863 struct nilfs_btree_node
*parent
;
1865 nilfs_bmap_commit_update_v(&btree
->bt_bmap
,
1866 &path
[level
].bp_oldreq
,
1867 &path
[level
].bp_newreq
);
1869 if (buffer_nilfs_node(path
[level
].bp_bh
)) {
1870 nilfs_btnode_commit_change_key(
1871 &NILFS_BMAP_I(&btree
->bt_bmap
)->i_btnode_cache
,
1872 &path
[level
].bp_ctxt
);
1873 path
[level
].bp_bh
= path
[level
].bp_ctxt
.bh
;
1875 set_buffer_nilfs_volatile(path
[level
].bp_bh
);
1877 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1);
1878 nilfs_btree_node_set_ptr(btree
, parent
, path
[level
+ 1].bp_index
,
1879 path
[level
].bp_newreq
.bpr_ptr
);
1882 static void nilfs_btree_abort_update_v(struct nilfs_btree
*btree
,
1883 struct nilfs_btree_path
*path
,
1886 nilfs_bmap_abort_update_v(&btree
->bt_bmap
,
1887 &path
[level
].bp_oldreq
,
1888 &path
[level
].bp_newreq
);
1889 if (buffer_nilfs_node(path
[level
].bp_bh
))
1890 nilfs_btnode_abort_change_key(
1891 &NILFS_BMAP_I(&btree
->bt_bmap
)->i_btnode_cache
,
1892 &path
[level
].bp_ctxt
);
1895 static int nilfs_btree_prepare_propagate_v(struct nilfs_btree
*btree
,
1896 struct nilfs_btree_path
*path
,
1903 if (!buffer_nilfs_volatile(path
[level
].bp_bh
)) {
1904 ret
= nilfs_btree_prepare_update_v(btree
, path
, level
);
1908 while ((++level
< nilfs_btree_height(btree
) - 1) &&
1909 !buffer_dirty(path
[level
].bp_bh
)) {
1911 WARN_ON(buffer_nilfs_volatile(path
[level
].bp_bh
));
1912 ret
= nilfs_btree_prepare_update_v(btree
, path
, level
);
1918 *maxlevelp
= level
- 1;
1923 while (--level
> minlevel
)
1924 nilfs_btree_abort_update_v(btree
, path
, level
);
1925 if (!buffer_nilfs_volatile(path
[level
].bp_bh
))
1926 nilfs_btree_abort_update_v(btree
, path
, level
);
1930 static void nilfs_btree_commit_propagate_v(struct nilfs_btree
*btree
,
1931 struct nilfs_btree_path
*path
,
1934 struct buffer_head
*bh
)
1938 if (!buffer_nilfs_volatile(path
[minlevel
].bp_bh
))
1939 nilfs_btree_commit_update_v(btree
, path
, minlevel
);
1941 for (level
= minlevel
+ 1; level
<= maxlevel
; level
++)
1942 nilfs_btree_commit_update_v(btree
, path
, level
);
1945 static int nilfs_btree_propagate_v(struct nilfs_btree
*btree
,
1946 struct nilfs_btree_path
*path
,
1948 struct buffer_head
*bh
)
1951 struct nilfs_btree_node
*parent
;
1955 path
[level
].bp_bh
= bh
;
1956 ret
= nilfs_btree_prepare_propagate_v(btree
, path
, level
, &maxlevel
);
1960 if (buffer_nilfs_volatile(path
[level
].bp_bh
)) {
1961 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1);
1962 ptr
= nilfs_btree_node_get_ptr(btree
, parent
,
1963 path
[level
+ 1].bp_index
);
1964 ret
= nilfs_bmap_mark_dirty(&btree
->bt_bmap
, ptr
);
1969 nilfs_btree_commit_propagate_v(btree
, path
, level
, maxlevel
, bh
);
1972 brelse(path
[level
].bp_bh
);
1973 path
[level
].bp_bh
= NULL
;
1977 static int nilfs_btree_propagate(const struct nilfs_bmap
*bmap
,
1978 struct buffer_head
*bh
)
1980 struct nilfs_btree
*btree
;
1981 struct nilfs_btree_path
*path
;
1982 struct nilfs_btree_node
*node
;
1986 WARN_ON(!buffer_dirty(bh
));
1988 btree
= (struct nilfs_btree
*)bmap
;
1989 path
= nilfs_btree_alloc_path(btree
);
1992 nilfs_btree_init_path(btree
, path
);
1994 if (buffer_nilfs_node(bh
)) {
1995 node
= (struct nilfs_btree_node
*)bh
->b_data
;
1996 key
= nilfs_btree_node_get_key(btree
, node
, 0);
1997 level
= nilfs_btree_node_get_level(btree
, node
);
1999 key
= nilfs_bmap_data_get_key(bmap
, bh
);
2000 level
= NILFS_BTREE_LEVEL_DATA
;
2003 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
, level
+ 1);
2005 if (unlikely(ret
== -ENOENT
))
2006 printk(KERN_CRIT
"%s: key = %llu, level == %d\n",
2007 __func__
, (unsigned long long)key
, level
);
2011 ret
= NILFS_BMAP_USE_VBN(bmap
) ?
2012 nilfs_btree_propagate_v(btree
, path
, level
, bh
) :
2013 nilfs_btree_propagate_p(btree
, path
, level
, bh
);
2016 nilfs_btree_clear_path(btree
, path
);
2017 nilfs_btree_free_path(btree
, path
);
2022 static int nilfs_btree_propagate_gc(const struct nilfs_bmap
*bmap
,
2023 struct buffer_head
*bh
)
2025 return nilfs_bmap_mark_dirty(bmap
, bh
->b_blocknr
);
2028 static void nilfs_btree_add_dirty_buffer(struct nilfs_btree
*btree
,
2029 struct list_head
*lists
,
2030 struct buffer_head
*bh
)
2032 struct list_head
*head
;
2033 struct buffer_head
*cbh
;
2034 struct nilfs_btree_node
*node
, *cnode
;
2039 node
= (struct nilfs_btree_node
*)bh
->b_data
;
2040 key
= nilfs_btree_node_get_key(btree
, node
, 0);
2041 level
= nilfs_btree_node_get_level(btree
, node
);
2042 list_for_each(head
, &lists
[level
]) {
2043 cbh
= list_entry(head
, struct buffer_head
, b_assoc_buffers
);
2044 cnode
= (struct nilfs_btree_node
*)cbh
->b_data
;
2045 ckey
= nilfs_btree_node_get_key(btree
, cnode
, 0);
2049 list_add_tail(&bh
->b_assoc_buffers
, head
);
2052 static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap
*bmap
,
2053 struct list_head
*listp
)
2055 struct nilfs_btree
*btree
= (struct nilfs_btree
*)bmap
;
2056 struct address_space
*btcache
= &NILFS_BMAP_I(bmap
)->i_btnode_cache
;
2057 struct list_head lists
[NILFS_BTREE_LEVEL_MAX
];
2058 struct pagevec pvec
;
2059 struct buffer_head
*bh
, *head
;
2063 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
2064 level
< NILFS_BTREE_LEVEL_MAX
;
2066 INIT_LIST_HEAD(&lists
[level
]);
2068 pagevec_init(&pvec
, 0);
2070 while (pagevec_lookup_tag(&pvec
, btcache
, &index
, PAGECACHE_TAG_DIRTY
,
2072 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
2073 bh
= head
= page_buffers(pvec
.pages
[i
]);
2075 if (buffer_dirty(bh
))
2076 nilfs_btree_add_dirty_buffer(btree
,
2078 } while ((bh
= bh
->b_this_page
) != head
);
2080 pagevec_release(&pvec
);
2084 for (level
= NILFS_BTREE_LEVEL_NODE_MIN
;
2085 level
< NILFS_BTREE_LEVEL_MAX
;
2087 list_splice(&lists
[level
], listp
->prev
);
2090 static int nilfs_btree_assign_p(struct nilfs_btree
*btree
,
2091 struct nilfs_btree_path
*path
,
2093 struct buffer_head
**bh
,
2095 union nilfs_binfo
*binfo
)
2097 struct nilfs_btree_node
*parent
;
2102 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1);
2103 ptr
= nilfs_btree_node_get_ptr(btree
, parent
,
2104 path
[level
+ 1].bp_index
);
2105 if (buffer_nilfs_node(*bh
)) {
2106 path
[level
].bp_ctxt
.oldkey
= ptr
;
2107 path
[level
].bp_ctxt
.newkey
= blocknr
;
2108 path
[level
].bp_ctxt
.bh
= *bh
;
2109 ret
= nilfs_btnode_prepare_change_key(
2110 &NILFS_BMAP_I(&btree
->bt_bmap
)->i_btnode_cache
,
2111 &path
[level
].bp_ctxt
);
2114 nilfs_btnode_commit_change_key(
2115 &NILFS_BMAP_I(&btree
->bt_bmap
)->i_btnode_cache
,
2116 &path
[level
].bp_ctxt
);
2117 *bh
= path
[level
].bp_ctxt
.bh
;
2120 nilfs_btree_node_set_ptr(btree
, parent
,
2121 path
[level
+ 1].bp_index
, blocknr
);
2123 key
= nilfs_btree_node_get_key(btree
, parent
,
2124 path
[level
+ 1].bp_index
);
2125 /* on-disk format */
2126 binfo
->bi_dat
.bi_blkoff
= nilfs_bmap_key_to_dkey(key
);
2127 binfo
->bi_dat
.bi_level
= level
;
2132 static int nilfs_btree_assign_v(struct nilfs_btree
*btree
,
2133 struct nilfs_btree_path
*path
,
2135 struct buffer_head
**bh
,
2137 union nilfs_binfo
*binfo
)
2139 struct nilfs_btree_node
*parent
;
2142 union nilfs_bmap_ptr_req req
;
2145 parent
= nilfs_btree_get_node(btree
, path
, level
+ 1);
2146 ptr
= nilfs_btree_node_get_ptr(btree
, parent
,
2147 path
[level
+ 1].bp_index
);
2149 ret
= nilfs_bmap_start_v(&btree
->bt_bmap
, &req
, blocknr
);
2150 if (unlikely(ret
< 0))
2153 key
= nilfs_btree_node_get_key(btree
, parent
,
2154 path
[level
+ 1].bp_index
);
2155 /* on-disk format */
2156 binfo
->bi_v
.bi_vblocknr
= nilfs_bmap_ptr_to_dptr(ptr
);
2157 binfo
->bi_v
.bi_blkoff
= nilfs_bmap_key_to_dkey(key
);
2162 static int nilfs_btree_assign(struct nilfs_bmap
*bmap
,
2163 struct buffer_head
**bh
,
2165 union nilfs_binfo
*binfo
)
2167 struct nilfs_btree
*btree
;
2168 struct nilfs_btree_path
*path
;
2169 struct nilfs_btree_node
*node
;
2173 btree
= (struct nilfs_btree
*)bmap
;
2174 path
= nilfs_btree_alloc_path(btree
);
2177 nilfs_btree_init_path(btree
, path
);
2179 if (buffer_nilfs_node(*bh
)) {
2180 node
= (struct nilfs_btree_node
*)(*bh
)->b_data
;
2181 key
= nilfs_btree_node_get_key(btree
, node
, 0);
2182 level
= nilfs_btree_node_get_level(btree
, node
);
2184 key
= nilfs_bmap_data_get_key(bmap
, *bh
);
2185 level
= NILFS_BTREE_LEVEL_DATA
;
2188 ret
= nilfs_btree_do_lookup(btree
, path
, key
, NULL
, level
+ 1);
2190 WARN_ON(ret
== -ENOENT
);
2194 ret
= NILFS_BMAP_USE_VBN(bmap
) ?
2195 nilfs_btree_assign_v(btree
, path
, level
, bh
, blocknr
, binfo
) :
2196 nilfs_btree_assign_p(btree
, path
, level
, bh
, blocknr
, binfo
);
2199 nilfs_btree_clear_path(btree
, path
);
2200 nilfs_btree_free_path(btree
, path
);
2205 static int nilfs_btree_assign_gc(struct nilfs_bmap
*bmap
,
2206 struct buffer_head
**bh
,
2208 union nilfs_binfo
*binfo
)
2210 struct nilfs_btree
*btree
;
2211 struct nilfs_btree_node
*node
;
2215 btree
= (struct nilfs_btree
*)bmap
;
2216 ret
= nilfs_bmap_move_v(bmap
, (*bh
)->b_blocknr
, blocknr
);
2220 if (buffer_nilfs_node(*bh
)) {
2221 node
= (struct nilfs_btree_node
*)(*bh
)->b_data
;
2222 key
= nilfs_btree_node_get_key(btree
, node
, 0);
2224 key
= nilfs_bmap_data_get_key(bmap
, *bh
);
2226 /* on-disk format */
2227 binfo
->bi_v
.bi_vblocknr
= cpu_to_le64((*bh
)->b_blocknr
);
2228 binfo
->bi_v
.bi_blkoff
= nilfs_bmap_key_to_dkey(key
);
2233 static int nilfs_btree_mark(struct nilfs_bmap
*bmap
, __u64 key
, int level
)
2235 struct buffer_head
*bh
;
2236 struct nilfs_btree
*btree
;
2237 struct nilfs_btree_path
*path
;
2241 btree
= (struct nilfs_btree
*)bmap
;
2242 path
= nilfs_btree_alloc_path(btree
);
2245 nilfs_btree_init_path(btree
, path
);
2247 ret
= nilfs_btree_do_lookup(btree
, path
, key
, &ptr
, level
+ 1);
2249 WARN_ON(ret
== -ENOENT
);
2252 ret
= nilfs_btree_get_block(btree
, ptr
, &bh
);
2254 WARN_ON(ret
== -ENOENT
);
2258 if (!buffer_dirty(bh
))
2259 nilfs_btnode_mark_dirty(bh
);
2261 if (!nilfs_bmap_dirty(&btree
->bt_bmap
))
2262 nilfs_bmap_set_dirty(&btree
->bt_bmap
);
2265 nilfs_btree_clear_path(btree
, path
);
2266 nilfs_btree_free_path(btree
, path
);
2270 static const struct nilfs_bmap_operations nilfs_btree_ops
= {
2271 .bop_lookup
= nilfs_btree_lookup
,
2272 .bop_lookup_contig
= nilfs_btree_lookup_contig
,
2273 .bop_insert
= nilfs_btree_insert
,
2274 .bop_delete
= nilfs_btree_delete
,
2277 .bop_propagate
= nilfs_btree_propagate
,
2279 .bop_lookup_dirty_buffers
= nilfs_btree_lookup_dirty_buffers
,
2281 .bop_assign
= nilfs_btree_assign
,
2282 .bop_mark
= nilfs_btree_mark
,
2284 .bop_last_key
= nilfs_btree_last_key
,
2285 .bop_check_insert
= NULL
,
2286 .bop_check_delete
= nilfs_btree_check_delete
,
2287 .bop_gather_data
= nilfs_btree_gather_data
,
2290 static const struct nilfs_bmap_operations nilfs_btree_ops_gc
= {
2292 .bop_lookup_contig
= NULL
,
2297 .bop_propagate
= nilfs_btree_propagate_gc
,
2299 .bop_lookup_dirty_buffers
= nilfs_btree_lookup_dirty_buffers
,
2301 .bop_assign
= nilfs_btree_assign_gc
,
2304 .bop_last_key
= NULL
,
2305 .bop_check_insert
= NULL
,
2306 .bop_check_delete
= NULL
,
2307 .bop_gather_data
= NULL
,
2310 int nilfs_btree_init(struct nilfs_bmap
*bmap
)
2312 bmap
->b_ops
= &nilfs_btree_ops
;
2316 void nilfs_btree_init_gc(struct nilfs_bmap
*bmap
)
2318 bmap
->b_ops
= &nilfs_btree_ops_gc
;