23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
61 brelse(path[level].
bp_bh);
69 static int nilfs_btree_get_new_block(
const struct nilfs_bmap *btree,
72 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
73 struct buffer_head *bh;
79 set_buffer_nilfs_volatile(bh);
122 static int nilfs_btree_node_size(
const struct nilfs_bmap *btree)
124 return 1 << btree->
b_inode->i_blkbits;
127 static int nilfs_btree_nchildren_per_block(
const struct nilfs_bmap *btree)
135 return (
__le64 *)((
char *)(node + 1) +
136 (nilfs_btree_node_root(node) ?
143 return (
__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
149 return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
162 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
173 int level,
int nchildren,
int ncmax,
180 nilfs_btree_node_set_flags(node, flags);
181 nilfs_btree_node_set_level(node, level);
182 nilfs_btree_node_set_nchildren(node, nchildren);
184 dkeys = nilfs_btree_node_dkeys(node);
185 dptrs = nilfs_btree_node_dptrs(node, ncmax);
186 for (i = 0; i < nchildren; i++) {
195 int n,
int lncmax,
int rncmax)
199 int lnchildren, rnchildren;
201 ldkeys = nilfs_btree_node_dkeys(left);
202 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
203 lnchildren = nilfs_btree_node_get_nchildren(left);
205 rdkeys = nilfs_btree_node_dkeys(right);
206 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
207 rnchildren = nilfs_btree_node_get_nchildren(right);
209 memcpy(ldkeys + lnchildren, rdkeys, n *
sizeof(*rdkeys));
210 memcpy(ldptrs + lnchildren, rdptrs, n *
sizeof(*rdptrs));
211 memmove(rdkeys, rdkeys + n, (rnchildren - n) *
sizeof(*rdkeys));
212 memmove(rdptrs, rdptrs + n, (rnchildren - n) *
sizeof(*rdptrs));
216 nilfs_btree_node_set_nchildren(left, lnchildren);
217 nilfs_btree_node_set_nchildren(right, rnchildren);
223 int n,
int lncmax,
int rncmax)
227 int lnchildren, rnchildren;
229 ldkeys = nilfs_btree_node_dkeys(left);
230 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
231 lnchildren = nilfs_btree_node_get_nchildren(left);
233 rdkeys = nilfs_btree_node_dkeys(right);
234 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
235 rnchildren = nilfs_btree_node_get_nchildren(right);
237 memmove(rdkeys + n, rdkeys, rnchildren *
sizeof(*rdkeys));
238 memmove(rdptrs + n, rdptrs, rnchildren *
sizeof(*rdptrs));
239 memcpy(rdkeys, ldkeys + lnchildren - n, n *
sizeof(*rdkeys));
240 memcpy(rdptrs, ldptrs + lnchildren - n, n *
sizeof(*rdptrs));
244 nilfs_btree_node_set_nchildren(left, lnchildren);
245 nilfs_btree_node_set_nchildren(right, rnchildren);
249 static void nilfs_btree_node_insert(
struct nilfs_btree_node *node,
int index,
256 dkeys = nilfs_btree_node_dkeys(node);
257 dptrs = nilfs_btree_node_dptrs(node, ncmax);
258 nchildren = nilfs_btree_node_get_nchildren(node);
259 if (index < nchildren) {
260 memmove(dkeys + index + 1, dkeys + index,
261 (nchildren - index) *
sizeof(*dkeys));
262 memmove(dptrs + index + 1, dptrs + index,
263 (nchildren - index) *
sizeof(*dptrs));
268 nilfs_btree_node_set_nchildren(node, nchildren);
272 static void nilfs_btree_node_delete(
struct nilfs_btree_node *node,
int index,
281 dkeys = nilfs_btree_node_dkeys(node);
282 dptrs = nilfs_btree_node_dptrs(node, ncmax);
285 nchildren = nilfs_btree_node_get_nchildren(node);
291 if (index < nchildren - 1) {
292 memmove(dkeys + index, dkeys + index + 1,
293 (nchildren - index - 1) *
sizeof(*dkeys));
294 memmove(dptrs + index, dptrs + index + 1,
295 (nchildren - index - 1) *
sizeof(*dptrs));
298 nilfs_btree_node_set_nchildren(node, nchildren);
302 __u64 key,
int *indexp)
309 high = nilfs_btree_node_get_nchildren(node) - 1;
312 while (low <= high) {
313 index = (low +
high) / 2;
314 nkey = nilfs_btree_node_get_key(node, index);
318 }
else if (nkey < key) {
329 if (s > 0 && index > 0)
354 level = nilfs_btree_node_get_level(node);
355 flags = nilfs_btree_node_get_flags(node);
356 nchildren = nilfs_btree_node_get_nchildren(node);
359 level >= NILFS_BTREE_LEVEL_MAX ||
364 "level = %d, flags = 0x%x, nchildren = %d\n",
365 (
unsigned long long)blocknr, level, flags, nchildren);
375 if (buffer_nilfs_checked(bh))
379 bh->b_size, bh->b_blocknr);
381 set_buffer_nilfs_checked(bh);
386 nilfs_btree_get_root(
const struct nilfs_bmap *btree)
392 nilfs_btree_get_nonroot_node(
const struct nilfs_btree_path *path,
int level)
403 static int nilfs_btree_height(
const struct nilfs_bmap *btree)
405 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
409 nilfs_btree_get_node(
const struct nilfs_bmap *btree,
411 int level,
int *ncmaxp)
415 if (level == nilfs_btree_height(btree) - 1) {
416 node = nilfs_btree_get_root(btree);
419 node = nilfs_btree_get_nonroot_node(path, level);
420 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
428 if (
unlikely(nilfs_btree_node_get_level(node) != level)) {
431 nilfs_btree_node_get_level(node), level);
444 static int __nilfs_btree_get_block(
const struct nilfs_bmap *btree,
__u64 ptr,
445 struct buffer_head **bhp,
448 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
449 struct buffer_head *bh, *ra_bh;
466 n > 0 && i < ra->ncmax; n--, i++) {
467 ptr2 = nilfs_btree_node_get_ptr(ra->
node, i, ra->
ncmax);
470 &ra_bh, &submit_ptr);
473 else if (ret != -
EBUSY)
475 if (!buffer_locked(bh))
483 if (!buffer_uptodate(bh)) {
490 clear_buffer_uptodate(bh);
499 static int nilfs_btree_get_block(
const struct nilfs_bmap *btree,
__u64 ptr,
500 struct buffer_head **bhp)
502 return __nilfs_btree_get_block(btree, ptr, bhp,
NULL);
505 static int nilfs_btree_do_lookup(
const struct nilfs_bmap *btree,
515 node = nilfs_btree_get_root(btree);
516 level = nilfs_btree_node_get_level(node);
517 if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
520 found = nilfs_btree_node_lookup(node, key, &index);
521 ptr = nilfs_btree_node_get_ptr(node, index,
526 ncmax = nilfs_btree_nchildren_per_block(btree);
528 while (--level >= minlevel) {
531 p.node = nilfs_btree_get_node(btree, path, level + 1,
537 ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh,
542 node = nilfs_btree_get_nonroot_node(path, level);
543 if (nilfs_btree_bad_node(node, level))
546 found = nilfs_btree_node_lookup(node, key, &index);
550 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
567 static int nilfs_btree_do_lookup_last(
const struct nilfs_bmap *btree,
575 node = nilfs_btree_get_root(btree);
576 index = nilfs_btree_node_get_nchildren(node) - 1;
579 level = nilfs_btree_node_get_level(node);
580 ptr = nilfs_btree_node_get_ptr(node, index,
584 ncmax = nilfs_btree_nchildren_per_block(btree);
586 for (level--; level > 0; level--) {
587 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
590 node = nilfs_btree_get_nonroot_node(path, level);
591 if (nilfs_btree_bad_node(node, level))
593 index = nilfs_btree_node_get_nchildren(node) - 1;
594 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
599 *keyp = nilfs_btree_node_get_key(node, index);
606 static int nilfs_btree_lookup(
const struct nilfs_bmap *btree,
612 path = nilfs_btree_alloc_path();
616 ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0);
618 nilfs_btree_free_path(path);
623 static int nilfs_btree_lookup_contig(
const struct nilfs_bmap *btree,
635 path = nilfs_btree_alloc_path();
639 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
651 if (cnt == maxblocks)
654 maxlevel = nilfs_btree_height(btree) - 1;
655 node = nilfs_btree_get_node(btree, path, level, &ncmax);
656 index = path[
level].bp_index + 1;
658 while (index < nilfs_btree_node_get_nchildren(node)) {
659 if (nilfs_btree_node_get_key(node, index) !=
662 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
669 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
674 if (level == maxlevel)
678 p.node = nilfs_btree_get_node(btree, path, level + 1, &
p.ncmax);
679 p.index = path[level + 1].bp_index + 1;
681 if (
p.index >= nilfs_btree_node_get_nchildren(
p.node) ||
682 nilfs_btree_node_get_key(
p.node,
p.index) != key + cnt)
684 ptr2 = nilfs_btree_node_get_ptr(
p.node,
p.index,
p.ncmax);
685 path[level + 1].bp_index =
p.index;
687 brelse(path[level].bp_bh);
690 ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
694 node = nilfs_btree_get_nonroot_node(path, level);
695 ncmax = nilfs_btree_nchildren_per_block(btree);
703 nilfs_btree_free_path(path);
707 static void nilfs_btree_promote_key(
struct nilfs_bmap *btree,
709 int level,
__u64 key)
711 if (level < nilfs_btree_height(btree) - 1) {
713 nilfs_btree_node_set_key(
714 nilfs_btree_get_nonroot_node(path, level),
715 path[level].bp_index, key);
716 if (!buffer_dirty(path[level].bp_bh))
718 }
while ((path[level].bp_index == 0) &&
719 (++level < nilfs_btree_height(btree) - 1));
723 if (level == nilfs_btree_height(btree) - 1) {
724 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
725 path[level].bp_index, key);
729 static void nilfs_btree_do_insert(
struct nilfs_bmap *btree,
736 if (level < nilfs_btree_height(btree) - 1) {
737 node = nilfs_btree_get_nonroot_node(path, level);
738 ncblk = nilfs_btree_nchildren_per_block(btree);
739 nilfs_btree_node_insert(node, path[level].bp_index,
740 *keyp, *ptrp, ncblk);
741 if (!buffer_dirty(path[level].bp_bh))
744 if (path[level].bp_index == 0)
745 nilfs_btree_promote_key(btree, path, level + 1,
746 nilfs_btree_node_get_key(node,
749 node = nilfs_btree_get_root(btree);
750 nilfs_btree_node_insert(node, path[level].bp_index,
756 static void nilfs_btree_carry_left(
struct nilfs_bmap *btree,
761 int nchildren, lnchildren,
n, move, ncblk;
763 node = nilfs_btree_get_nonroot_node(path, level);
764 left = nilfs_btree_get_sib_node(path, level);
765 nchildren = nilfs_btree_node_get_nchildren(node);
766 lnchildren = nilfs_btree_node_get_nchildren(left);
767 ncblk = nilfs_btree_nchildren_per_block(btree);
770 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
771 if (n > path[level].bp_index) {
777 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
779 if (!buffer_dirty(path[level].bp_bh))
781 if (!buffer_dirty(path[level].bp_sib_bh))
784 nilfs_btree_promote_key(btree, path, level + 1,
785 nilfs_btree_node_get_key(node, 0));
788 brelse(path[level].bp_bh);
794 brelse(path[level].bp_sib_bh);
799 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
802 static void nilfs_btree_carry_right(
struct nilfs_bmap *btree,
807 int nchildren, rnchildren,
n, move, ncblk;
809 node = nilfs_btree_get_nonroot_node(path, level);
810 right = nilfs_btree_get_sib_node(path, level);
811 nchildren = nilfs_btree_node_get_nchildren(node);
812 rnchildren = nilfs_btree_node_get_nchildren(right);
813 ncblk = nilfs_btree_nchildren_per_block(btree);
816 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
817 if (n > nchildren - path[level].bp_index) {
823 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
825 if (!buffer_dirty(path[level].bp_bh))
827 if (!buffer_dirty(path[level].bp_sib_bh))
831 nilfs_btree_promote_key(btree, path, level + 1,
832 nilfs_btree_node_get_key(right, 0));
836 brelse(path[level].bp_bh);
839 path[
level].
bp_index -= nilfs_btree_node_get_nchildren(node);
842 brelse(path[level].bp_sib_bh);
846 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
849 static void nilfs_btree_split(
struct nilfs_bmap *btree,
856 int nchildren,
n, move, ncblk;
858 node = nilfs_btree_get_nonroot_node(path, level);
859 right = nilfs_btree_get_sib_node(path, level);
860 nchildren = nilfs_btree_node_get_nchildren(node);
861 ncblk = nilfs_btree_nchildren_per_block(btree);
864 n = (nchildren + 1) / 2;
865 if (n > nchildren - path[level].bp_index) {
870 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
872 if (!buffer_dirty(path[level].bp_bh))
874 if (!buffer_dirty(path[level].bp_sib_bh))
877 newkey = nilfs_btree_node_get_key(right, 0);
881 path[
level].
bp_index -= nilfs_btree_node_get_nchildren(node);
882 nilfs_btree_node_insert(right, path[level].bp_index,
883 *keyp, *ptrp, ncblk);
885 *keyp = nilfs_btree_node_get_key(right, 0);
888 brelse(path[level].bp_bh);
892 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
894 *keyp = nilfs_btree_node_get_key(right, 0);
897 brelse(path[level].bp_sib_bh);
904 static void nilfs_btree_grow(
struct nilfs_bmap *btree,
911 root = nilfs_btree_get_root(btree);
912 child = nilfs_btree_get_sib_node(path, level);
913 ncblk = nilfs_btree_nchildren_per_block(btree);
915 n = nilfs_btree_node_get_nchildren(root);
917 nilfs_btree_node_move_right(root, child, n,
919 nilfs_btree_node_set_level(root, level + 1);
921 if (!buffer_dirty(path[level].bp_sib_bh))
927 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
929 *keyp = nilfs_btree_node_get_key(child, 0);
944 if (path[level].bp_index > 0) {
945 node = nilfs_btree_get_node(btree, path, level, &ncmax);
946 return nilfs_btree_node_get_ptr(node,
947 path[level].bp_index - 1,
953 if (level <= nilfs_btree_height(btree) - 1) {
954 node = nilfs_btree_get_node(btree, path, level, &ncmax);
955 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
973 ptr = nilfs_btree_find_near(btree, path);
982 static int nilfs_btree_prepare_insert(
struct nilfs_bmap *btree,
987 struct buffer_head *bh;
990 int pindex,
level, ncmax, ncblk,
ret;
999 nilfs_btree_find_target_v(btree, path, key);
1003 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
1007 ncblk = nilfs_btree_nchildren_per_block(btree);
1010 level < nilfs_btree_height(btree) - 1;
1012 node = nilfs_btree_get_nonroot_node(path, level);
1013 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
1019 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1024 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1026 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1028 goto err_out_child_node;
1030 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
1041 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1042 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1044 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1046 goto err_out_child_node;
1048 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
1061 ret = nilfs_bmap_prepare_alloc_ptr(btree,
1062 &path[level].bp_newreq, dat);
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;
1074 nilfs_btree_node_init(sib, 0, level, 0, ncblk,
NULL,
NULL);
1080 node = nilfs_btree_get_root(btree);
1081 if (nilfs_btree_node_get_nchildren(node) <
1090 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
1092 goto err_out_child_node;
1093 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1096 goto err_out_curr_node;
1116 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
1120 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
1124 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
1131 static void nilfs_btree_commit_insert(
struct nilfs_bmap *btree,
1138 set_buffer_nilfs_volatile((
struct buffer_head *)((
unsigned long)ptr));
1141 nilfs_bmap_set_target_v(btree, key, ptr);
1146 nilfs_bmap_commit_alloc_ptr(btree,
1147 &path[level - 1].bp_newreq, dat);
1148 path[
level].
bp_op(btree, path, level, &key, &ptr);
1151 if (!nilfs_bmap_dirty(btree))
1152 nilfs_bmap_set_dirty(btree);
1161 path = nilfs_btree_alloc_path();
1165 ret = nilfs_btree_do_lookup(btree, path, key,
NULL,
1173 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1176 nilfs_btree_commit_insert(btree, path, level, key, ptr);
1180 nilfs_btree_free_path(path);
1184 static void nilfs_btree_do_delete(
struct nilfs_bmap *btree,
1191 if (level < nilfs_btree_height(btree) - 1) {
1192 node = nilfs_btree_get_nonroot_node(path, level);
1193 ncblk = nilfs_btree_nchildren_per_block(btree);
1194 nilfs_btree_node_delete(node, path[level].bp_index,
1196 if (!buffer_dirty(path[level].bp_bh))
1198 if (path[level].bp_index == 0)
1199 nilfs_btree_promote_key(btree, path, level + 1,
1200 nilfs_btree_node_get_key(node, 0));
1202 node = nilfs_btree_get_root(btree);
1203 nilfs_btree_node_delete(node, path[level].bp_index,
1209 static void nilfs_btree_borrow_left(
struct nilfs_bmap *btree,
1214 int nchildren, lnchildren,
n, ncblk;
1216 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1218 node = nilfs_btree_get_nonroot_node(path, level);
1219 left = nilfs_btree_get_sib_node(path, level);
1220 nchildren = nilfs_btree_node_get_nchildren(node);
1221 lnchildren = nilfs_btree_node_get_nchildren(left);
1222 ncblk = nilfs_btree_nchildren_per_block(btree);
1224 n = (nchildren + lnchildren) / 2 - nchildren;
1226 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
1228 if (!buffer_dirty(path[level].bp_bh))
1230 if (!buffer_dirty(path[level].bp_sib_bh))
1233 nilfs_btree_promote_key(btree, path, level + 1,
1234 nilfs_btree_node_get_key(node, 0));
1236 brelse(path[level].bp_sib_bh);
1241 static void nilfs_btree_borrow_right(
struct nilfs_bmap *btree,
1246 int nchildren, rnchildren,
n, ncblk;
1248 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1250 node = nilfs_btree_get_nonroot_node(path, level);
1251 right = nilfs_btree_get_sib_node(path, level);
1252 nchildren = nilfs_btree_node_get_nchildren(node);
1253 rnchildren = nilfs_btree_node_get_nchildren(right);
1254 ncblk = nilfs_btree_nchildren_per_block(btree);
1256 n = (nchildren + rnchildren) / 2 - nchildren;
1258 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
1260 if (!buffer_dirty(path[level].bp_bh))
1262 if (!buffer_dirty(path[level].bp_sib_bh))
1266 nilfs_btree_promote_key(btree, path, level + 1,
1267 nilfs_btree_node_get_key(right, 0));
1270 brelse(path[level].bp_sib_bh);
1274 static void nilfs_btree_concat_left(
struct nilfs_bmap *btree,
1281 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1283 node = nilfs_btree_get_nonroot_node(path, level);
1284 left = nilfs_btree_get_sib_node(path, level);
1285 ncblk = nilfs_btree_nchildren_per_block(btree);
1287 n = nilfs_btree_node_get_nchildren(node);
1289 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
1291 if (!buffer_dirty(path[level].bp_sib_bh))
1297 path[
level].
bp_index += nilfs_btree_node_get_nchildren(left);
1300 static void nilfs_btree_concat_right(
struct nilfs_bmap *btree,
1307 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1309 node = nilfs_btree_get_nonroot_node(path, level);
1310 right = nilfs_btree_get_sib_node(path, level);
1311 ncblk = nilfs_btree_nchildren_per_block(btree);
1313 n = nilfs_btree_node_get_nchildren(right);
1315 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
1317 if (!buffer_dirty(path[level].bp_bh))
1325 static void nilfs_btree_shrink(
struct nilfs_bmap *btree,
1332 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1334 root = nilfs_btree_get_root(btree);
1335 child = nilfs_btree_get_nonroot_node(path, level);
1336 ncblk = nilfs_btree_nchildren_per_block(btree);
1338 nilfs_btree_node_delete(root, 0,
NULL,
NULL,
1340 nilfs_btree_node_set_level(root, level);
1341 n = nilfs_btree_node_get_nchildren(child);
1342 nilfs_btree_node_move_left(root, child, n,
1349 static void nilfs_btree_nop(
struct nilfs_bmap *btree,
1355 static int nilfs_btree_prepare_delete(
struct nilfs_bmap *btree,
1361 struct buffer_head *bh;
1364 int pindex, dindex,
level, ncmin, ncmax, ncblk,
ret;
1369 ncblk = nilfs_btree_nchildren_per_block(btree);
1372 level < nilfs_btree_height(btree) - 1;
1374 node = nilfs_btree_get_nonroot_node(path, level);
1376 nilfs_btree_node_get_ptr(node, dindex, ncblk);
1377 ret = nilfs_bmap_prepare_end_ptr(btree,
1378 &path[level].bp_oldreq, dat);
1380 goto err_out_child_node;
1382 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
1388 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1394 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1396 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1398 goto err_out_curr_node;
1400 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
1412 nilfs_btree_node_get_nchildren(parent) - 1) {
1414 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1416 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1418 goto err_out_curr_node;
1420 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
1422 path[
level].
bp_op = nilfs_btree_borrow_right;
1427 path[
level].
bp_op = nilfs_btree_concat_right;
1436 dindex = pindex + 1;
1442 WARN_ON(level != nilfs_btree_height(btree) - 2);
1443 if (nilfs_btree_node_get_nchildren(node) - 1 <=
1449 goto shrink_root_child;
1463 node = nilfs_btree_get_root(btree);
1465 nilfs_btree_node_get_ptr(node, dindex,
1468 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
1470 goto err_out_child_node;
1479 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
1482 brelse(path[level].bp_sib_bh);
1483 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
1490 static void nilfs_btree_commit_delete(
struct nilfs_bmap *btree,
1492 int maxlevel,
struct inode *dat)
1497 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
1501 if (!nilfs_bmap_dirty(btree))
1502 nilfs_bmap_set_dirty(btree);
1513 path = nilfs_btree_alloc_path();
1517 ret = nilfs_btree_do_lookup(btree, path, key,
NULL,
1525 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
1528 nilfs_btree_commit_delete(btree, path, level, dat);
1532 nilfs_btree_free_path(path);
1536 static int nilfs_btree_last_key(
const struct nilfs_bmap *btree,
__u64 *keyp)
1541 path = nilfs_btree_alloc_path();
1545 ret = nilfs_btree_do_lookup_last(btree, path, keyp,
NULL);
1547 nilfs_btree_free_path(path);
1552 static int nilfs_btree_check_delete(
struct nilfs_bmap *btree,
__u64 key)
1554 struct buffer_head *bh;
1556 __u64 maxkey, nextmaxkey;
1560 root = nilfs_btree_get_root(btree);
1561 switch (nilfs_btree_height(btree)) {
1567 nchildren = nilfs_btree_node_get_nchildren(root);
1570 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1572 ret = nilfs_btree_get_block(btree, ptr, &bh);
1581 nchildren = nilfs_btree_node_get_nchildren(node);
1582 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
1583 nextmaxkey = (nchildren > 1) ?
1584 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
1591 static int nilfs_btree_gather_data(
struct nilfs_bmap *btree,
1594 struct buffer_head *bh;
1599 int nchildren, ncmax,
i,
ret;
1601 root = nilfs_btree_get_root(btree);
1602 switch (nilfs_btree_height(btree)) {
1609 nchildren = nilfs_btree_node_get_nchildren(root);
1611 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1613 ret = nilfs_btree_get_block(btree, ptr, &bh);
1617 ncmax = nilfs_btree_nchildren_per_block(btree);
1624 nchildren = nilfs_btree_node_get_nchildren(node);
1625 if (nchildren < nitems)
1627 dkeys = nilfs_btree_node_dkeys(node);
1628 dptrs = nilfs_btree_node_dptrs(node, ncmax);
1629 for (i = 0; i < nitems; i++) {
1641 nilfs_btree_prepare_convert_and_insert(
struct nilfs_bmap *btree,
__u64 key,
1644 struct buffer_head **bhp,
1647 struct buffer_head *bh;
1656 dreq->
bpr_ptr = nilfs_btree_find_target_v(btree,
NULL, key);
1660 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
1668 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
1672 ret = nilfs_btree_get_new_block(btree, nreq->
bpr_ptr, &bh);
1685 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
1687 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
1694 nilfs_btree_commit_convert_and_insert(
struct nilfs_bmap *btree,
1700 struct buffer_head *bh)
1709 btree->
b_ops->bop_clear(btree);
1712 set_buffer_nilfs_volatile((
struct buffer_head *)((
unsigned long)ptr));
1718 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1719 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
1723 ncblk = nilfs_btree_nchildren_per_block(btree);
1724 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1725 nilfs_btree_node_insert(node, n, key, dreq->
bpr_ptr, ncblk);
1726 if (!buffer_dirty(bh))
1728 if (!nilfs_bmap_dirty(btree))
1729 nilfs_bmap_set_dirty(btree);
1734 node = nilfs_btree_get_root(btree);
1736 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1740 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1743 node = nilfs_btree_get_root(btree);
1744 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1747 nilfs_btree_node_insert(node, n, key, dreq->
bpr_ptr,
1749 if (!nilfs_bmap_dirty(btree))
1750 nilfs_bmap_set_dirty(btree);
1754 nilfs_bmap_set_target_v(btree, key, dreq->
bpr_ptr);
1770 struct buffer_head *bh;
1779 1 << btree->
b_inode->i_blkbits)) {
1788 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
1792 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
1798 static int nilfs_btree_propagate_p(
struct nilfs_bmap *btree,
1801 struct buffer_head *bh)
1803 while ((++level < nilfs_btree_height(btree) - 1) &&
1804 !buffer_dirty(path[level].bp_bh))
1810 static int nilfs_btree_prepare_update_v(
struct nilfs_bmap *btree,
1812 int level,
struct inode *dat)
1817 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1819 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1827 if (buffer_nilfs_node(path[level].bp_bh)) {
1832 &NILFS_BMAP_I(btree)->i_btnode_cache,
1833 &path[level].bp_ctxt);
1836 &path[level].bp_oldreq.bpr_req,
1845 static void nilfs_btree_commit_update_v(
struct nilfs_bmap *btree,
1847 int level,
struct inode *dat)
1856 if (buffer_nilfs_node(path[level].bp_bh)) {
1858 &NILFS_BMAP_I(btree)->i_btnode_cache,
1859 &path[level].bp_ctxt);
1862 set_buffer_nilfs_volatile(path[level].bp_bh);
1864 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1865 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1866 path[level].bp_newreq.bpr_ptr, ncmax);
1869 static void nilfs_btree_abort_update_v(
struct nilfs_bmap *btree,
1871 int level,
struct inode *dat)
1875 if (buffer_nilfs_node(path[level].bp_bh))
1877 &NILFS_BMAP_I(btree)->i_btnode_cache,
1878 &path[level].bp_ctxt);
1881 static int nilfs_btree_prepare_propagate_v(
struct nilfs_bmap *btree,
1883 int minlevel,
int *maxlevelp,
1889 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
1890 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
1894 while ((++level < nilfs_btree_height(btree) - 1) &&
1895 !buffer_dirty(path[level].bp_bh)) {
1897 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
1898 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
1904 *maxlevelp = level - 1;
1909 while (--level > minlevel)
1910 nilfs_btree_abort_update_v(btree, path, level, dat);
1911 if (!buffer_nilfs_volatile(path[level].bp_bh))
1912 nilfs_btree_abort_update_v(btree, path, level, dat);
1916 static void nilfs_btree_commit_propagate_v(
struct nilfs_bmap *btree,
1918 int minlevel,
int maxlevel,
1919 struct buffer_head *bh,
1924 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
1925 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
1927 for (level = minlevel + 1; level <= maxlevel; level++)
1928 nilfs_btree_commit_update_v(btree, path, level, dat);
1931 static int nilfs_btree_propagate_v(
struct nilfs_bmap *btree,
1933 int level,
struct buffer_head *bh)
1935 int maxlevel = 0,
ret;
1943 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
1948 if (buffer_nilfs_volatile(path[level].bp_bh)) {
1949 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1950 ptr = nilfs_btree_node_get_ptr(parent,
1951 path[level + 1].bp_index,
1958 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
1961 brelse(path[level].bp_bh);
1966 static int nilfs_btree_propagate(
struct nilfs_bmap *btree,
1967 struct buffer_head *bh)
1976 path = nilfs_btree_alloc_path();
1980 if (buffer_nilfs_node(bh)) {
1982 key = nilfs_btree_node_get_key(node, 0);
1983 level = nilfs_btree_node_get_level(node);
1989 ret = nilfs_btree_do_lookup(btree, path, key,
NULL, level + 1, 0);
1993 __func__, (
unsigned long long)key, level);
1998 nilfs_btree_propagate_v(btree, path, level, bh) :
1999 nilfs_btree_propagate_p(btree, path, level, bh);
2002 nilfs_btree_free_path(path);
2007 static int nilfs_btree_propagate_gc(
struct nilfs_bmap *btree,
2008 struct buffer_head *bh)
2013 static void nilfs_btree_add_dirty_buffer(
struct nilfs_bmap *btree,
2015 struct buffer_head *bh)
2018 struct buffer_head *cbh;
2025 key = nilfs_btree_node_get_key(node, 0);
2026 level = nilfs_btree_node_get_level(node);
2028 level >= NILFS_BTREE_LEVEL_MAX) {
2031 "%s: invalid btree level: %d (key=%llu, ino=%lu, "
2033 __func__, level, (
unsigned long long)key,
2034 NILFS_BMAP_I(btree)->vfs_inode.i_ino,
2035 (
unsigned long long)bh->b_blocknr);
2040 cbh =
list_entry(head,
struct buffer_head, b_assoc_buffers);
2042 ckey = nilfs_btree_node_get_key(cnode, 0);
2049 static void nilfs_btree_lookup_dirty_buffers(
struct nilfs_bmap *btree,
2052 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
2053 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2055 struct buffer_head *bh, *
head;
2062 INIT_LIST_HEAD(&lists[level]);
2064 pagevec_init(&pvec, 0);
2068 for (i = 0; i < pagevec_count(&pvec); i++) {
2069 bh = head = page_buffers(pvec.pages[i]);
2071 if (buffer_dirty(bh))
2072 nilfs_btree_add_dirty_buffer(btree,
2074 }
while ((bh = bh->b_this_page) != head);
2076 pagevec_release(&pvec);
2083 list_splice_tail(&lists[level], listp);
2086 static int nilfs_btree_assign_p(
struct nilfs_bmap *btree,
2089 struct buffer_head **bh,
2098 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2099 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2101 if (buffer_nilfs_node(*bh)) {
2106 &NILFS_BMAP_I(btree)->i_btnode_cache,
2107 &path[level].bp_ctxt);
2111 &NILFS_BMAP_I(btree)->i_btnode_cache,
2112 &path[level].bp_ctxt);
2116 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2119 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
2127 static int nilfs_btree_assign_v(
struct nilfs_bmap *btree,
2130 struct buffer_head **bh,
2141 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2142 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2150 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
2158 static int nilfs_btree_assign(
struct nilfs_bmap *btree,
2159 struct buffer_head **bh,
2168 path = nilfs_btree_alloc_path();
2172 if (buffer_nilfs_node(*bh)) {
2174 key = nilfs_btree_node_get_key(node, 0);
2175 level = nilfs_btree_node_get_level(node);
2181 ret = nilfs_btree_do_lookup(btree, path, key,
NULL, level + 1, 0);
2188 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2189 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
2192 nilfs_btree_free_path(path);
2197 static int nilfs_btree_assign_gc(
struct nilfs_bmap *btree,
2198 struct buffer_head **bh,
2211 if (buffer_nilfs_node(*bh)) {
2213 key = nilfs_btree_node_get_key(node, 0);
2224 static int nilfs_btree_mark(
struct nilfs_bmap *btree,
__u64 key,
int level)
2226 struct buffer_head *bh;
2231 path = nilfs_btree_alloc_path();
2235 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
2240 ret = nilfs_btree_get_block(btree, ptr, &bh);
2246 if (!buffer_dirty(bh))
2249 if (!nilfs_bmap_dirty(btree))
2250 nilfs_bmap_set_dirty(btree);
2253 nilfs_btree_free_path(path);
2258 .bop_lookup = nilfs_btree_lookup,
2259 .bop_lookup_contig = nilfs_btree_lookup_contig,
2260 .bop_insert = nilfs_btree_insert,
2261 .bop_delete = nilfs_btree_delete,
2264 .bop_propagate = nilfs_btree_propagate,
2266 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2268 .bop_assign = nilfs_btree_assign,
2269 .bop_mark = nilfs_btree_mark,
2271 .bop_last_key = nilfs_btree_last_key,
2272 .bop_check_insert =
NULL,
2273 .bop_check_delete = nilfs_btree_check_delete,
2274 .bop_gather_data = nilfs_btree_gather_data,
2279 .bop_lookup_contig =
NULL,
2284 .bop_propagate = nilfs_btree_propagate_gc,
2286 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2288 .bop_assign = nilfs_btree_assign_gc,
2291 .bop_last_key =
NULL,
2292 .bop_check_insert =
NULL,
2293 .bop_check_delete =
NULL,
2294 .bop_gather_data =
NULL,
2299 bmap->
b_ops = &nilfs_btree_ops;
2307 bmap->
b_ops = &nilfs_btree_ops_gc;