aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nilfs2
diff options
context:
space:
mode:
authorRyusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>2010-07-13 10:33:53 -0400
committerRyusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>2010-07-22 21:02:14 -0400
commit9b7b265c9ab67fcd1245d6b64fa5ca2eda43ac88 (patch)
tree3aeed58de180351e46ecb4e58f2e55c097eeed0d /fs/nilfs2
parentea64ab87cdba9e1172392d247e6526359e301f12 (diff)
nilfs2: reduce repetitive calculation of max number of child nodes
The current btree implementation repeats the same calculation on the maximum number of child nodes. This is because a few low level routines use the calculation for index addressing in a btree node block. This reduces the calculation by explicitly passing the maximum number of child nodes (ncmax) through their argument. This changes parameter passing of the following functions: - nilfs_btree_node_dptrs - nilfs_btree_node_get_ptr - nilfs_btree_node_set_ptr - nilfs_btree_node_init - nilfs_btree_node_move_left - nilfs_btree_node_move_right - nilfs_btree_node_insert - nilfs_btree_node_delete, and - nilfs_btree_get_node The following functions are removed: - nilfs_btree_node_nchildren_min - nilfs_btree_node_nchildren_max Most middle level btree operations are rewritten to pass a proper ncmax value depending on whether each occurrence of node is "root" or not. A constant NILFS_BTREE_ROOT_NCHILDREN_MAX is used for the root node, whereas nilfs_btree_nchildren_per_block() function is used for non-root nodes. If a node could be either root or a non-root node, an output argument of nilfs_btree_get_node() is used to set up ncmax. Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Diffstat (limited to 'fs/nilfs2')
-rw-r--r--fs/nilfs2/btree.c339
1 files changed, 182 insertions, 157 deletions
diff --git a/fs/nilfs2/btree.c b/fs/nilfs2/btree.c
index c0266f7f0b26..829e145f1353 100644
--- a/fs/nilfs2/btree.c
+++ b/fs/nilfs2/btree.c
@@ -152,22 +152,9 @@ static inline int nilfs_btree_node_size(const struct nilfs_bmap *btree)
152 return 1 << btree->b_inode->i_blkbits; 152 return 1 << btree->b_inode->i_blkbits;
153} 153}
154 154
155static inline int 155static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
156nilfs_btree_node_nchildren_min(const struct nilfs_btree_node *node,
157 const struct nilfs_bmap *btree)
158{
159 return nilfs_btree_node_root(node) ?
160 NILFS_BTREE_ROOT_NCHILDREN_MIN :
161 NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
162}
163
164static inline int
165nilfs_btree_node_nchildren_max(const struct nilfs_btree_node *node,
166 const struct nilfs_bmap *btree)
167{ 156{
168 return nilfs_btree_node_root(node) ? 157 return NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree));
169 NILFS_BTREE_ROOT_NCHILDREN_MAX :
170 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree));
171} 158}
172 159
173static inline __le64 * 160static inline __le64 *
@@ -179,11 +166,9 @@ nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
179} 166}
180 167
181static inline __le64 * 168static inline __le64 *
182nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, 169nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
183 const struct nilfs_bmap *btree)
184{ 170{
185 return (__le64 *)(nilfs_btree_node_dkeys(node) + 171 return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
186 nilfs_btree_node_nchildren_max(node, btree));
187} 172}
188 173
189static inline __u64 174static inline __u64
@@ -199,22 +184,21 @@ nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
199} 184}
200 185
201static inline __u64 186static inline __u64
202nilfs_btree_node_get_ptr(const struct nilfs_bmap *btree, 187nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
203 const struct nilfs_btree_node *node, int index) 188 int ncmax)
204{ 189{
205 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, btree) + index)); 190 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
206} 191}
207 192
208static inline void 193static inline void
209nilfs_btree_node_set_ptr(struct nilfs_bmap *btree, 194nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
210 struct nilfs_btree_node *node, int index, __u64 ptr) 195 int ncmax)
211{ 196{
212 *(nilfs_btree_node_dptrs(node, btree) + index) = cpu_to_le64(ptr); 197 *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
213} 198}
214 199
215static void nilfs_btree_node_init(struct nilfs_bmap *btree, 200static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
216 struct nilfs_btree_node *node, 201 int level, int nchildren, int ncmax,
217 int flags, int level, int nchildren,
218 const __u64 *keys, const __u64 *ptrs) 202 const __u64 *keys, const __u64 *ptrs)
219{ 203{
220 __le64 *dkeys; 204 __le64 *dkeys;
@@ -226,7 +210,7 @@ static void nilfs_btree_node_init(struct nilfs_bmap *btree,
226 nilfs_btree_node_set_nchildren(node, nchildren); 210 nilfs_btree_node_set_nchildren(node, nchildren);
227 211
228 dkeys = nilfs_btree_node_dkeys(node); 212 dkeys = nilfs_btree_node_dkeys(node);
229 dptrs = nilfs_btree_node_dptrs(node, btree); 213 dptrs = nilfs_btree_node_dptrs(node, ncmax);
230 for (i = 0; i < nchildren; i++) { 214 for (i = 0; i < nchildren; i++) {
231 dkeys[i] = cpu_to_le64(keys[i]); 215 dkeys[i] = cpu_to_le64(keys[i]);
232 dptrs[i] = cpu_to_le64(ptrs[i]); 216 dptrs[i] = cpu_to_le64(ptrs[i]);
@@ -234,21 +218,20 @@ static void nilfs_btree_node_init(struct nilfs_bmap *btree,
234} 218}
235 219
236/* Assume the buffer heads corresponding to left and right are locked. */ 220/* Assume the buffer heads corresponding to left and right are locked. */
237static void nilfs_btree_node_move_left(struct nilfs_bmap *btree, 221static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
238 struct nilfs_btree_node *left,
239 struct nilfs_btree_node *right, 222 struct nilfs_btree_node *right,
240 int n) 223 int n, int lncmax, int rncmax)
241{ 224{
242 __le64 *ldkeys, *rdkeys; 225 __le64 *ldkeys, *rdkeys;
243 __le64 *ldptrs, *rdptrs; 226 __le64 *ldptrs, *rdptrs;
244 int lnchildren, rnchildren; 227 int lnchildren, rnchildren;
245 228
246 ldkeys = nilfs_btree_node_dkeys(left); 229 ldkeys = nilfs_btree_node_dkeys(left);
247 ldptrs = nilfs_btree_node_dptrs(left, btree); 230 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
248 lnchildren = nilfs_btree_node_get_nchildren(left); 231 lnchildren = nilfs_btree_node_get_nchildren(left);
249 232
250 rdkeys = nilfs_btree_node_dkeys(right); 233 rdkeys = nilfs_btree_node_dkeys(right);
251 rdptrs = nilfs_btree_node_dptrs(right, btree); 234 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
252 rnchildren = nilfs_btree_node_get_nchildren(right); 235 rnchildren = nilfs_btree_node_get_nchildren(right);
253 236
254 memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys)); 237 memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
@@ -263,21 +246,20 @@ static void nilfs_btree_node_move_left(struct nilfs_bmap *btree,
263} 246}
264 247
265/* Assume that the buffer heads corresponding to left and right are locked. */ 248/* Assume that the buffer heads corresponding to left and right are locked. */
266static void nilfs_btree_node_move_right(struct nilfs_bmap *btree, 249static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
267 struct nilfs_btree_node *left,
268 struct nilfs_btree_node *right, 250 struct nilfs_btree_node *right,
269 int n) 251 int n, int lncmax, int rncmax)
270{ 252{
271 __le64 *ldkeys, *rdkeys; 253 __le64 *ldkeys, *rdkeys;
272 __le64 *ldptrs, *rdptrs; 254 __le64 *ldptrs, *rdptrs;
273 int lnchildren, rnchildren; 255 int lnchildren, rnchildren;
274 256
275 ldkeys = nilfs_btree_node_dkeys(left); 257 ldkeys = nilfs_btree_node_dkeys(left);
276 ldptrs = nilfs_btree_node_dptrs(left, btree); 258 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
277 lnchildren = nilfs_btree_node_get_nchildren(left); 259 lnchildren = nilfs_btree_node_get_nchildren(left);
278 260
279 rdkeys = nilfs_btree_node_dkeys(right); 261 rdkeys = nilfs_btree_node_dkeys(right);
280 rdptrs = nilfs_btree_node_dptrs(right, btree); 262 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
281 rnchildren = nilfs_btree_node_get_nchildren(right); 263 rnchildren = nilfs_btree_node_get_nchildren(right);
282 264
283 memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys)); 265 memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
@@ -292,16 +274,15 @@ static void nilfs_btree_node_move_right(struct nilfs_bmap *btree,
292} 274}
293 275
294/* Assume that the buffer head corresponding to node is locked. */ 276/* Assume that the buffer head corresponding to node is locked. */
295static void nilfs_btree_node_insert(struct nilfs_bmap *btree, 277static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
296 struct nilfs_btree_node *node, 278 __u64 key, __u64 ptr, int ncmax)
297 __u64 key, __u64 ptr, int index)
298{ 279{
299 __le64 *dkeys; 280 __le64 *dkeys;
300 __le64 *dptrs; 281 __le64 *dptrs;
301 int nchildren; 282 int nchildren;
302 283
303 dkeys = nilfs_btree_node_dkeys(node); 284 dkeys = nilfs_btree_node_dkeys(node);
304 dptrs = nilfs_btree_node_dptrs(node, btree); 285 dptrs = nilfs_btree_node_dptrs(node, ncmax);
305 nchildren = nilfs_btree_node_get_nchildren(node); 286 nchildren = nilfs_btree_node_get_nchildren(node);
306 if (index < nchildren) { 287 if (index < nchildren) {
307 memmove(dkeys + index + 1, dkeys + index, 288 memmove(dkeys + index + 1, dkeys + index,
@@ -316,9 +297,8 @@ static void nilfs_btree_node_insert(struct nilfs_bmap *btree,
316} 297}
317 298
318/* Assume that the buffer head corresponding to node is locked. */ 299/* Assume that the buffer head corresponding to node is locked. */
319static void nilfs_btree_node_delete(struct nilfs_bmap *btree, 300static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
320 struct nilfs_btree_node *node, 301 __u64 *keyp, __u64 *ptrp, int ncmax)
321 __u64 *keyp, __u64 *ptrp, int index)
322{ 302{
323 __u64 key; 303 __u64 key;
324 __u64 ptr; 304 __u64 ptr;
@@ -327,7 +307,7 @@ static void nilfs_btree_node_delete(struct nilfs_bmap *btree,
327 int nchildren; 307 int nchildren;
328 308
329 dkeys = nilfs_btree_node_dkeys(node); 309 dkeys = nilfs_btree_node_dkeys(node);
330 dptrs = nilfs_btree_node_dptrs(node, btree); 310 dptrs = nilfs_btree_node_dptrs(node, ncmax);
331 key = le64_to_cpu(dkeys[index]); 311 key = le64_to_cpu(dkeys[index]);
332 ptr = le64_to_cpu(dptrs[index]); 312 ptr = le64_to_cpu(dptrs[index]);
333 nchildren = nilfs_btree_node_get_nchildren(node); 313 nchildren = nilfs_btree_node_get_nchildren(node);
@@ -445,14 +425,21 @@ static inline int nilfs_btree_height(const struct nilfs_bmap *btree)
445 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1; 425 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
446} 426}
447 427
448static inline struct nilfs_btree_node * 428static struct nilfs_btree_node *
449nilfs_btree_get_node(const struct nilfs_bmap *btree, 429nilfs_btree_get_node(const struct nilfs_bmap *btree,
450 const struct nilfs_btree_path *path, 430 const struct nilfs_btree_path *path,
451 int level) 431 int level, int *ncmaxp)
452{ 432{
453 return (level == nilfs_btree_height(btree) - 1) ? 433 struct nilfs_btree_node *node;
454 nilfs_btree_get_root(btree) : 434
455 nilfs_btree_get_nonroot_node(path, level); 435 if (level == nilfs_btree_height(btree) - 1) {
436 node = nilfs_btree_get_root(btree);
437 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
438 } else {
439 node = nilfs_btree_get_nonroot_node(path, level);
440 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
441 }
442 return node;
456} 443}
457 444
458static inline int 445static inline int
@@ -481,11 +468,12 @@ static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
481 return -ENOENT; 468 return -ENOENT;
482 469
483 found = nilfs_btree_node_lookup(node, key, &index); 470 found = nilfs_btree_node_lookup(node, key, &index);
484 ptr = nilfs_btree_node_get_ptr(btree, node, index); 471 ptr = nilfs_btree_node_get_ptr(node, index,
472 NILFS_BTREE_ROOT_NCHILDREN_MAX);
485 path[level].bp_bh = NULL; 473 path[level].bp_bh = NULL;
486 path[level].bp_index = index; 474 path[level].bp_index = index;
487 475
488 ncmax = NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree)); 476 ncmax = nilfs_btree_nchildren_per_block(btree);
489 477
490 for (level--; level >= minlevel; level--) { 478 for (level--; level >= minlevel; level--) {
491 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh); 479 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
@@ -499,7 +487,7 @@ static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
499 else 487 else
500 index = 0; 488 index = 0;
501 if (index < ncmax) { 489 if (index < ncmax) {
502 ptr = nilfs_btree_node_get_ptr(btree, node, index); 490 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
503 } else { 491 } else {
504 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN); 492 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
505 /* insert */ 493 /* insert */
@@ -522,16 +510,18 @@ static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
522{ 510{
523 struct nilfs_btree_node *node; 511 struct nilfs_btree_node *node;
524 __u64 ptr; 512 __u64 ptr;
525 int index, level, ret; 513 int index, level, ncmax, ret;
526 514
527 node = nilfs_btree_get_root(btree); 515 node = nilfs_btree_get_root(btree);
528 index = nilfs_btree_node_get_nchildren(node) - 1; 516 index = nilfs_btree_node_get_nchildren(node) - 1;
529 if (index < 0) 517 if (index < 0)
530 return -ENOENT; 518 return -ENOENT;
531 level = nilfs_btree_node_get_level(node); 519 level = nilfs_btree_node_get_level(node);
532 ptr = nilfs_btree_node_get_ptr(btree, node, index); 520 ptr = nilfs_btree_node_get_ptr(node, index,
521 NILFS_BTREE_ROOT_NCHILDREN_MAX);
533 path[level].bp_bh = NULL; 522 path[level].bp_bh = NULL;
534 path[level].bp_index = index; 523 path[level].bp_index = index;
524 ncmax = nilfs_btree_nchildren_per_block(btree);
535 525
536 for (level--; level > 0; level--) { 526 for (level--; level > 0; level--) {
537 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh); 527 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
@@ -541,7 +531,7 @@ static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
541 if (nilfs_btree_bad_node(node, level)) 531 if (nilfs_btree_bad_node(node, level))
542 return -EINVAL; 532 return -EINVAL;
543 index = nilfs_btree_node_get_nchildren(node) - 1; 533 index = nilfs_btree_node_get_nchildren(node) - 1;
544 ptr = nilfs_btree_node_get_ptr(btree, node, index); 534 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
545 path[level].bp_index = index; 535 path[level].bp_index = index;
546 } 536 }
547 537
@@ -579,7 +569,7 @@ static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
579 __u64 ptr, ptr2; 569 __u64 ptr, ptr2;
580 sector_t blocknr; 570 sector_t blocknr;
581 int level = NILFS_BTREE_LEVEL_NODE_MIN; 571 int level = NILFS_BTREE_LEVEL_NODE_MIN;
582 int ret, cnt, index, maxlevel; 572 int ret, cnt, index, maxlevel, ncmax;
583 573
584 path = nilfs_btree_alloc_path(); 574 path = nilfs_btree_alloc_path();
585 if (path == NULL) 575 if (path == NULL)
@@ -601,14 +591,14 @@ static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
601 goto end; 591 goto end;
602 592
603 maxlevel = nilfs_btree_height(btree) - 1; 593 maxlevel = nilfs_btree_height(btree) - 1;
604 node = nilfs_btree_get_node(btree, path, level); 594 node = nilfs_btree_get_node(btree, path, level, &ncmax);
605 index = path[level].bp_index + 1; 595 index = path[level].bp_index + 1;
606 for (;;) { 596 for (;;) {
607 while (index < nilfs_btree_node_get_nchildren(node)) { 597 while (index < nilfs_btree_node_get_nchildren(node)) {
608 if (nilfs_btree_node_get_key(node, index) != 598 if (nilfs_btree_node_get_key(node, index) !=
609 key + cnt) 599 key + cnt)
610 goto end; 600 goto end;
611 ptr2 = nilfs_btree_node_get_ptr(btree, node, index); 601 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
612 if (dat) { 602 if (dat) {
613 ret = nilfs_dat_translate(dat, ptr2, &blocknr); 603 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
614 if (ret < 0) 604 if (ret < 0)
@@ -624,12 +614,12 @@ static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
624 break; 614 break;
625 615
626 /* look-up right sibling node */ 616 /* look-up right sibling node */
627 node = nilfs_btree_get_node(btree, path, level + 1); 617 node = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
628 index = path[level + 1].bp_index + 1; 618 index = path[level + 1].bp_index + 1;
629 if (index >= nilfs_btree_node_get_nchildren(node) || 619 if (index >= nilfs_btree_node_get_nchildren(node) ||
630 nilfs_btree_node_get_key(node, index) != key + cnt) 620 nilfs_btree_node_get_key(node, index) != key + cnt)
631 break; 621 break;
632 ptr2 = nilfs_btree_node_get_ptr(btree, node, index); 622 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
633 path[level + 1].bp_index = index; 623 path[level + 1].bp_index = index;
634 624
635 brelse(path[level].bp_bh); 625 brelse(path[level].bp_bh);
@@ -638,6 +628,7 @@ static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
638 if (ret < 0) 628 if (ret < 0)
639 goto out; 629 goto out;
640 node = nilfs_btree_get_nonroot_node(path, level); 630 node = nilfs_btree_get_nonroot_node(path, level);
631 ncmax = nilfs_btree_nchildren_per_block(btree);
641 index = 0; 632 index = 0;
642 path[level].bp_index = index; 633 path[level].bp_index = index;
643 } 634 }
@@ -676,11 +667,13 @@ static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
676 int level, __u64 *keyp, __u64 *ptrp) 667 int level, __u64 *keyp, __u64 *ptrp)
677{ 668{
678 struct nilfs_btree_node *node; 669 struct nilfs_btree_node *node;
670 int ncblk;
679 671
680 if (level < nilfs_btree_height(btree) - 1) { 672 if (level < nilfs_btree_height(btree) - 1) {
681 node = nilfs_btree_get_nonroot_node(path, level); 673 node = nilfs_btree_get_nonroot_node(path, level);
682 nilfs_btree_node_insert(btree, node, *keyp, *ptrp, 674 ncblk = nilfs_btree_nchildren_per_block(btree);
683 path[level].bp_index); 675 nilfs_btree_node_insert(node, path[level].bp_index,
676 *keyp, *ptrp, ncblk);
684 if (!buffer_dirty(path[level].bp_bh)) 677 if (!buffer_dirty(path[level].bp_bh))
685 nilfs_btnode_mark_dirty(path[level].bp_bh); 678 nilfs_btnode_mark_dirty(path[level].bp_bh);
686 679
@@ -690,8 +683,9 @@ static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
690 0)); 683 0));
691 } else { 684 } else {
692 node = nilfs_btree_get_root(btree); 685 node = nilfs_btree_get_root(btree);
693 nilfs_btree_node_insert(btree, node, *keyp, *ptrp, 686 nilfs_btree_node_insert(node, path[level].bp_index,
694 path[level].bp_index); 687 *keyp, *ptrp,
688 NILFS_BTREE_ROOT_NCHILDREN_MAX);
695 } 689 }
696} 690}
697 691
@@ -700,12 +694,13 @@ static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
700 int level, __u64 *keyp, __u64 *ptrp) 694 int level, __u64 *keyp, __u64 *ptrp)
701{ 695{
702 struct nilfs_btree_node *node, *left; 696 struct nilfs_btree_node *node, *left;
703 int nchildren, lnchildren, n, move; 697 int nchildren, lnchildren, n, move, ncblk;
704 698
705 node = nilfs_btree_get_nonroot_node(path, level); 699 node = nilfs_btree_get_nonroot_node(path, level);
706 left = nilfs_btree_get_sib_node(path, level); 700 left = nilfs_btree_get_sib_node(path, level);
707 nchildren = nilfs_btree_node_get_nchildren(node); 701 nchildren = nilfs_btree_node_get_nchildren(node);
708 lnchildren = nilfs_btree_node_get_nchildren(left); 702 lnchildren = nilfs_btree_node_get_nchildren(left);
703 ncblk = nilfs_btree_nchildren_per_block(btree);
709 move = 0; 704 move = 0;
710 705
711 n = (nchildren + lnchildren + 1) / 2 - lnchildren; 706 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
@@ -715,7 +710,7 @@ static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
715 move = 1; 710 move = 1;
716 } 711 }
717 712
718 nilfs_btree_node_move_left(btree, left, node, n); 713 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
719 714
720 if (!buffer_dirty(path[level].bp_bh)) 715 if (!buffer_dirty(path[level].bp_bh))
721 nilfs_btnode_mark_dirty(path[level].bp_bh); 716 nilfs_btnode_mark_dirty(path[level].bp_bh);
@@ -745,12 +740,13 @@ static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
745 int level, __u64 *keyp, __u64 *ptrp) 740 int level, __u64 *keyp, __u64 *ptrp)
746{ 741{
747 struct nilfs_btree_node *node, *right; 742 struct nilfs_btree_node *node, *right;
748 int nchildren, rnchildren, n, move; 743 int nchildren, rnchildren, n, move, ncblk;
749 744
750 node = nilfs_btree_get_nonroot_node(path, level); 745 node = nilfs_btree_get_nonroot_node(path, level);
751 right = nilfs_btree_get_sib_node(path, level); 746 right = nilfs_btree_get_sib_node(path, level);
752 nchildren = nilfs_btree_node_get_nchildren(node); 747 nchildren = nilfs_btree_node_get_nchildren(node);
753 rnchildren = nilfs_btree_node_get_nchildren(right); 748 rnchildren = nilfs_btree_node_get_nchildren(right);
749 ncblk = nilfs_btree_nchildren_per_block(btree);
754 move = 0; 750 move = 0;
755 751
756 n = (nchildren + rnchildren + 1) / 2 - rnchildren; 752 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
@@ -760,7 +756,7 @@ static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
760 move = 1; 756 move = 1;
761 } 757 }
762 758
763 nilfs_btree_node_move_right(btree, node, right, n); 759 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
764 760
765 if (!buffer_dirty(path[level].bp_bh)) 761 if (!buffer_dirty(path[level].bp_bh))
766 nilfs_btnode_mark_dirty(path[level].bp_bh); 762 nilfs_btnode_mark_dirty(path[level].bp_bh);
@@ -793,11 +789,12 @@ static void nilfs_btree_split(struct nilfs_bmap *btree,
793 struct nilfs_btree_node *node, *right; 789 struct nilfs_btree_node *node, *right;
794 __u64 newkey; 790 __u64 newkey;
795 __u64 newptr; 791 __u64 newptr;
796 int nchildren, n, move; 792 int nchildren, n, move, ncblk;
797 793
798 node = nilfs_btree_get_nonroot_node(path, level); 794 node = nilfs_btree_get_nonroot_node(path, level);
799 right = nilfs_btree_get_sib_node(path, level); 795 right = nilfs_btree_get_sib_node(path, level);
800 nchildren = nilfs_btree_node_get_nchildren(node); 796 nchildren = nilfs_btree_node_get_nchildren(node);
797 ncblk = nilfs_btree_nchildren_per_block(btree);
801 move = 0; 798 move = 0;
802 799
803 n = (nchildren + 1) / 2; 800 n = (nchildren + 1) / 2;
@@ -806,7 +803,7 @@ static void nilfs_btree_split(struct nilfs_bmap *btree,
806 move = 1; 803 move = 1;
807 } 804 }
808 805
809 nilfs_btree_node_move_right(btree, node, right, n); 806 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
810 807
811 if (!buffer_dirty(path[level].bp_bh)) 808 if (!buffer_dirty(path[level].bp_bh))
812 nilfs_btnode_mark_dirty(path[level].bp_bh); 809 nilfs_btnode_mark_dirty(path[level].bp_bh);
@@ -818,8 +815,8 @@ static void nilfs_btree_split(struct nilfs_bmap *btree,
818 815
819 if (move) { 816 if (move) {
820 path[level].bp_index -= nilfs_btree_node_get_nchildren(node); 817 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
821 nilfs_btree_node_insert(btree, right, *keyp, *ptrp, 818 nilfs_btree_node_insert(right, path[level].bp_index,
822 path[level].bp_index); 819 *keyp, *ptrp, ncblk);
823 820
824 *keyp = nilfs_btree_node_get_key(right, 0); 821 *keyp = nilfs_btree_node_get_key(right, 0);
825 *ptrp = path[level].bp_newreq.bpr_ptr; 822 *ptrp = path[level].bp_newreq.bpr_ptr;
@@ -845,14 +842,16 @@ static void nilfs_btree_grow(struct nilfs_bmap *btree,
845 int level, __u64 *keyp, __u64 *ptrp) 842 int level, __u64 *keyp, __u64 *ptrp)
846{ 843{
847 struct nilfs_btree_node *root, *child; 844 struct nilfs_btree_node *root, *child;
848 int n; 845 int n, ncblk;
849 846
850 root = nilfs_btree_get_root(btree); 847 root = nilfs_btree_get_root(btree);
851 child = nilfs_btree_get_sib_node(path, level); 848 child = nilfs_btree_get_sib_node(path, level);
849 ncblk = nilfs_btree_nchildren_per_block(btree);
852 850
853 n = nilfs_btree_node_get_nchildren(root); 851 n = nilfs_btree_node_get_nchildren(root);
854 852
855 nilfs_btree_node_move_right(btree, root, child, n); 853 nilfs_btree_node_move_right(root, child, n,
854 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
856 nilfs_btree_node_set_level(root, level + 1); 855 nilfs_btree_node_set_level(root, level + 1);
857 856
858 if (!buffer_dirty(path[level].bp_sib_bh)) 857 if (!buffer_dirty(path[level].bp_sib_bh))
@@ -871,7 +870,7 @@ static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
871 const struct nilfs_btree_path *path) 870 const struct nilfs_btree_path *path)
872{ 871{
873 struct nilfs_btree_node *node; 872 struct nilfs_btree_node *node;
874 int level; 873 int level, ncmax;
875 874
876 if (path == NULL) 875 if (path == NULL)
877 return NILFS_BMAP_INVALID_PTR; 876 return NILFS_BMAP_INVALID_PTR;
@@ -879,17 +878,18 @@ static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
879 /* left sibling */ 878 /* left sibling */
880 level = NILFS_BTREE_LEVEL_NODE_MIN; 879 level = NILFS_BTREE_LEVEL_NODE_MIN;
881 if (path[level].bp_index > 0) { 880 if (path[level].bp_index > 0) {
882 node = nilfs_btree_get_node(btree, path, level); 881 node = nilfs_btree_get_node(btree, path, level, &ncmax);
883 return nilfs_btree_node_get_ptr(btree, node, 882 return nilfs_btree_node_get_ptr(node,
884 path[level].bp_index - 1); 883 path[level].bp_index - 1,
884 ncmax);
885 } 885 }
886 886
887 /* parent */ 887 /* parent */
888 level = NILFS_BTREE_LEVEL_NODE_MIN + 1; 888 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
889 if (level <= nilfs_btree_height(btree) - 1) { 889 if (level <= nilfs_btree_height(btree) - 1) {
890 node = nilfs_btree_get_node(btree, path, level); 890 node = nilfs_btree_get_node(btree, path, level, &ncmax);
891 return nilfs_btree_node_get_ptr(btree, node, 891 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
892 path[level].bp_index); 892 ncmax);
893 } 893 }
894 894
895 return NILFS_BMAP_INVALID_PTR; 895 return NILFS_BMAP_INVALID_PTR;
@@ -923,7 +923,7 @@ static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
923 struct buffer_head *bh; 923 struct buffer_head *bh;
924 struct nilfs_btree_node *node, *parent, *sib; 924 struct nilfs_btree_node *node, *parent, *sib;
925 __u64 sibptr; 925 __u64 sibptr;
926 int pindex, level, ncmax, ret; 926 int pindex, level, ncmax, ncblk, ret;
927 struct inode *dat = NULL; 927 struct inode *dat = NULL;
928 928
929 stats->bs_nblocks = 0; 929 stats->bs_nblocks = 0;
@@ -940,54 +940,55 @@ static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
940 if (ret < 0) 940 if (ret < 0)
941 goto err_out_data; 941 goto err_out_data;
942 942
943 ncmax = NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree)); 943 ncblk = nilfs_btree_nchildren_per_block(btree);
944 944
945 for (level = NILFS_BTREE_LEVEL_NODE_MIN; 945 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
946 level < nilfs_btree_height(btree) - 1; 946 level < nilfs_btree_height(btree) - 1;
947 level++) { 947 level++) {
948 node = nilfs_btree_get_nonroot_node(path, level); 948 node = nilfs_btree_get_nonroot_node(path, level);
949 if (nilfs_btree_node_get_nchildren(node) < ncmax) { 949 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
950 path[level].bp_op = nilfs_btree_do_insert; 950 path[level].bp_op = nilfs_btree_do_insert;
951 stats->bs_nblocks++; 951 stats->bs_nblocks++;
952 goto out; 952 goto out;
953 } 953 }
954 954
955 parent = nilfs_btree_get_node(btree, path, level + 1); 955 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
956 pindex = path[level + 1].bp_index; 956 pindex = path[level + 1].bp_index;
957 957
958 /* left sibling */ 958 /* left sibling */
959 if (pindex > 0) { 959 if (pindex > 0) {
960 sibptr = nilfs_btree_node_get_ptr(btree, parent, 960 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
961 pindex - 1); 961 ncmax);
962 ret = nilfs_btree_get_block(btree, sibptr, &bh); 962 ret = nilfs_btree_get_block(btree, sibptr, &bh);
963 if (ret < 0) 963 if (ret < 0)
964 goto err_out_child_node; 964 goto err_out_child_node;
965 sib = (struct nilfs_btree_node *)bh->b_data; 965 sib = (struct nilfs_btree_node *)bh->b_data;
966 if (nilfs_btree_node_get_nchildren(sib) < ncmax) { 966 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
967 path[level].bp_sib_bh = bh; 967 path[level].bp_sib_bh = bh;
968 path[level].bp_op = nilfs_btree_carry_left; 968 path[level].bp_op = nilfs_btree_carry_left;
969 stats->bs_nblocks++; 969 stats->bs_nblocks++;
970 goto out; 970 goto out;
971 } else 971 } else {
972 brelse(bh); 972 brelse(bh);
973 }
973 } 974 }
974 975
975 /* right sibling */ 976 /* right sibling */
976 if (pindex < 977 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
977 nilfs_btree_node_get_nchildren(parent) - 1) { 978 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
978 sibptr = nilfs_btree_node_get_ptr(btree, parent, 979 ncmax);
979 pindex + 1);
980 ret = nilfs_btree_get_block(btree, sibptr, &bh); 980 ret = nilfs_btree_get_block(btree, sibptr, &bh);
981 if (ret < 0) 981 if (ret < 0)
982 goto err_out_child_node; 982 goto err_out_child_node;
983 sib = (struct nilfs_btree_node *)bh->b_data; 983 sib = (struct nilfs_btree_node *)bh->b_data;
984 if (nilfs_btree_node_get_nchildren(sib) < ncmax) { 984 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
985 path[level].bp_sib_bh = bh; 985 path[level].bp_sib_bh = bh;
986 path[level].bp_op = nilfs_btree_carry_right; 986 path[level].bp_op = nilfs_btree_carry_right;
987 stats->bs_nblocks++; 987 stats->bs_nblocks++;
988 goto out; 988 goto out;
989 } else 989 } else {
990 brelse(bh); 990 brelse(bh);
991 }
991 } 992 }
992 993
993 /* split */ 994 /* split */
@@ -1005,9 +1006,8 @@ static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
1005 1006
1006 stats->bs_nblocks++; 1007 stats->bs_nblocks++;
1007 1008
1008 nilfs_btree_node_init(btree, 1009 sib = (struct nilfs_btree_node *)bh->b_data;
1009 (struct nilfs_btree_node *)bh->b_data, 1010 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
1010 0, level, 0, NULL, NULL);
1011 path[level].bp_sib_bh = bh; 1011 path[level].bp_sib_bh = bh;
1012 path[level].bp_op = nilfs_btree_split; 1012 path[level].bp_op = nilfs_btree_split;
1013 } 1013 }
@@ -1031,8 +1031,8 @@ static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
1031 if (ret < 0) 1031 if (ret < 0)
1032 goto err_out_curr_node; 1032 goto err_out_curr_node;
1033 1033
1034 nilfs_btree_node_init(btree, (struct nilfs_btree_node *)bh->b_data, 1034 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1035 0, level, 0, NULL, NULL); 1035 0, level, 0, ncblk, NULL, NULL);
1036 path[level].bp_sib_bh = bh; 1036 path[level].bp_sib_bh = bh;
1037 path[level].bp_op = nilfs_btree_grow; 1037 path[level].bp_op = nilfs_btree_grow;
1038 1038
@@ -1122,11 +1122,13 @@ static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
1122 int level, __u64 *keyp, __u64 *ptrp) 1122 int level, __u64 *keyp, __u64 *ptrp)
1123{ 1123{
1124 struct nilfs_btree_node *node; 1124 struct nilfs_btree_node *node;
1125 int ncblk;
1125 1126
1126 if (level < nilfs_btree_height(btree) - 1) { 1127 if (level < nilfs_btree_height(btree) - 1) {
1127 node = nilfs_btree_get_nonroot_node(path, level); 1128 node = nilfs_btree_get_nonroot_node(path, level);
1128 nilfs_btree_node_delete(btree, node, keyp, ptrp, 1129 ncblk = nilfs_btree_nchildren_per_block(btree);
1129 path[level].bp_index); 1130 nilfs_btree_node_delete(node, path[level].bp_index,
1131 keyp, ptrp, ncblk);
1130 if (!buffer_dirty(path[level].bp_bh)) 1132 if (!buffer_dirty(path[level].bp_bh))
1131 nilfs_btnode_mark_dirty(path[level].bp_bh); 1133 nilfs_btnode_mark_dirty(path[level].bp_bh);
1132 if (path[level].bp_index == 0) 1134 if (path[level].bp_index == 0)
@@ -1134,8 +1136,9 @@ static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
1134 nilfs_btree_node_get_key(node, 0)); 1136 nilfs_btree_node_get_key(node, 0));
1135 } else { 1137 } else {
1136 node = nilfs_btree_get_root(btree); 1138 node = nilfs_btree_get_root(btree);
1137 nilfs_btree_node_delete(btree, node, keyp, ptrp, 1139 nilfs_btree_node_delete(node, path[level].bp_index,
1138 path[level].bp_index); 1140 keyp, ptrp,
1141 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1139 } 1142 }
1140} 1143}
1141 1144
@@ -1144,7 +1147,7 @@ static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
1144 int level, __u64 *keyp, __u64 *ptrp) 1147 int level, __u64 *keyp, __u64 *ptrp)
1145{ 1148{
1146 struct nilfs_btree_node *node, *left; 1149 struct nilfs_btree_node *node, *left;
1147 int nchildren, lnchildren, n; 1150 int nchildren, lnchildren, n, ncblk;
1148 1151
1149 nilfs_btree_do_delete(btree, path, level, keyp, ptrp); 1152 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1150 1153
@@ -1152,10 +1155,11 @@ static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
1152 left = nilfs_btree_get_sib_node(path, level); 1155 left = nilfs_btree_get_sib_node(path, level);
1153 nchildren = nilfs_btree_node_get_nchildren(node); 1156 nchildren = nilfs_btree_node_get_nchildren(node);
1154 lnchildren = nilfs_btree_node_get_nchildren(left); 1157 lnchildren = nilfs_btree_node_get_nchildren(left);
1158 ncblk = nilfs_btree_nchildren_per_block(btree);
1155 1159
1156 n = (nchildren + lnchildren) / 2 - nchildren; 1160 n = (nchildren + lnchildren) / 2 - nchildren;
1157 1161
1158 nilfs_btree_node_move_right(btree, left, node, n); 1162 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
1159 1163
1160 if (!buffer_dirty(path[level].bp_bh)) 1164 if (!buffer_dirty(path[level].bp_bh))
1161 nilfs_btnode_mark_dirty(path[level].bp_bh); 1165 nilfs_btnode_mark_dirty(path[level].bp_bh);
@@ -1175,7 +1179,7 @@ static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
1175 int level, __u64 *keyp, __u64 *ptrp) 1179 int level, __u64 *keyp, __u64 *ptrp)
1176{ 1180{
1177 struct nilfs_btree_node *node, *right; 1181 struct nilfs_btree_node *node, *right;
1178 int nchildren, rnchildren, n; 1182 int nchildren, rnchildren, n, ncblk;
1179 1183
1180 nilfs_btree_do_delete(btree, path, level, keyp, ptrp); 1184 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1181 1185
@@ -1183,10 +1187,11 @@ static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
1183 right = nilfs_btree_get_sib_node(path, level); 1187 right = nilfs_btree_get_sib_node(path, level);
1184 nchildren = nilfs_btree_node_get_nchildren(node); 1188 nchildren = nilfs_btree_node_get_nchildren(node);
1185 rnchildren = nilfs_btree_node_get_nchildren(right); 1189 rnchildren = nilfs_btree_node_get_nchildren(right);
1190 ncblk = nilfs_btree_nchildren_per_block(btree);
1186 1191
1187 n = (nchildren + rnchildren) / 2 - nchildren; 1192 n = (nchildren + rnchildren) / 2 - nchildren;
1188 1193
1189 nilfs_btree_node_move_left(btree, node, right, n); 1194 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
1190 1195
1191 if (!buffer_dirty(path[level].bp_bh)) 1196 if (!buffer_dirty(path[level].bp_bh))
1192 nilfs_btnode_mark_dirty(path[level].bp_bh); 1197 nilfs_btnode_mark_dirty(path[level].bp_bh);
@@ -1207,16 +1212,17 @@ static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
1207 int level, __u64 *keyp, __u64 *ptrp) 1212 int level, __u64 *keyp, __u64 *ptrp)
1208{ 1213{
1209 struct nilfs_btree_node *node, *left; 1214 struct nilfs_btree_node *node, *left;
1210 int n; 1215 int n, ncblk;
1211 1216
1212 nilfs_btree_do_delete(btree, path, level, keyp, ptrp); 1217 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1213 1218
1214 node = nilfs_btree_get_nonroot_node(path, level); 1219 node = nilfs_btree_get_nonroot_node(path, level);
1215 left = nilfs_btree_get_sib_node(path, level); 1220 left = nilfs_btree_get_sib_node(path, level);
1221 ncblk = nilfs_btree_nchildren_per_block(btree);
1216 1222
1217 n = nilfs_btree_node_get_nchildren(node); 1223 n = nilfs_btree_node_get_nchildren(node);
1218 1224
1219 nilfs_btree_node_move_left(btree, left, node, n); 1225 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
1220 1226
1221 if (!buffer_dirty(path[level].bp_sib_bh)) 1227 if (!buffer_dirty(path[level].bp_sib_bh))
1222 nilfs_btnode_mark_dirty(path[level].bp_sib_bh); 1228 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
@@ -1232,16 +1238,17 @@ static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
1232 int level, __u64 *keyp, __u64 *ptrp) 1238 int level, __u64 *keyp, __u64 *ptrp)
1233{ 1239{
1234 struct nilfs_btree_node *node, *right; 1240 struct nilfs_btree_node *node, *right;
1235 int n; 1241 int n, ncblk;
1236 1242
1237 nilfs_btree_do_delete(btree, path, level, keyp, ptrp); 1243 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1238 1244
1239 node = nilfs_btree_get_nonroot_node(path, level); 1245 node = nilfs_btree_get_nonroot_node(path, level);
1240 right = nilfs_btree_get_sib_node(path, level); 1246 right = nilfs_btree_get_sib_node(path, level);
1247 ncblk = nilfs_btree_nchildren_per_block(btree);
1241 1248
1242 n = nilfs_btree_node_get_nchildren(right); 1249 n = nilfs_btree_node_get_nchildren(right);
1243 1250
1244 nilfs_btree_node_move_left(btree, node, right, n); 1251 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
1245 1252
1246 if (!buffer_dirty(path[level].bp_bh)) 1253 if (!buffer_dirty(path[level].bp_bh))
1247 nilfs_btnode_mark_dirty(path[level].bp_bh); 1254 nilfs_btnode_mark_dirty(path[level].bp_bh);
@@ -1256,17 +1263,20 @@ static void nilfs_btree_shrink(struct nilfs_bmap *btree,
1256 int level, __u64 *keyp, __u64 *ptrp) 1263 int level, __u64 *keyp, __u64 *ptrp)
1257{ 1264{
1258 struct nilfs_btree_node *root, *child; 1265 struct nilfs_btree_node *root, *child;
1259 int n; 1266 int n, ncblk;
1260 1267
1261 nilfs_btree_do_delete(btree, path, level, keyp, ptrp); 1268 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1262 1269
1263 root = nilfs_btree_get_root(btree); 1270 root = nilfs_btree_get_root(btree);
1264 child = nilfs_btree_get_nonroot_node(path, level); 1271 child = nilfs_btree_get_nonroot_node(path, level);
1272 ncblk = nilfs_btree_nchildren_per_block(btree);
1265 1273
1266 nilfs_btree_node_delete(btree, root, NULL, NULL, 0); 1274 nilfs_btree_node_delete(root, 0, NULL, NULL,
1275 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1267 nilfs_btree_node_set_level(root, level); 1276 nilfs_btree_node_set_level(root, level);
1268 n = nilfs_btree_node_get_nchildren(child); 1277 n = nilfs_btree_node_get_nchildren(child);
1269 nilfs_btree_node_move_left(btree, root, child, n); 1278 nilfs_btree_node_move_left(root, child, n,
1279 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
1270 1280
1271 nilfs_btnode_delete(path[level].bp_bh); 1281 nilfs_btnode_delete(path[level].bp_bh);
1272 path[level].bp_bh = NULL; 1282 path[level].bp_bh = NULL;
@@ -1282,19 +1292,20 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
1282 struct buffer_head *bh; 1292 struct buffer_head *bh;
1283 struct nilfs_btree_node *node, *parent, *sib; 1293 struct nilfs_btree_node *node, *parent, *sib;
1284 __u64 sibptr; 1294 __u64 sibptr;
1285 int pindex, level, ncmin, ret; 1295 int pindex, level, ncmin, ncmax, ncblk, ret;
1286 1296
1287 ret = 0; 1297 ret = 0;
1288 stats->bs_nblocks = 0; 1298 stats->bs_nblocks = 0;
1289 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree)); 1299 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
1300 ncblk = nilfs_btree_nchildren_per_block(btree);
1290 1301
1291 for (level = NILFS_BTREE_LEVEL_NODE_MIN; 1302 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1292 level < nilfs_btree_height(btree) - 1; 1303 level < nilfs_btree_height(btree) - 1;
1293 level++) { 1304 level++) {
1294 node = nilfs_btree_get_nonroot_node(path, level); 1305 node = nilfs_btree_get_nonroot_node(path, level);
1295 path[level].bp_oldreq.bpr_ptr = 1306 path[level].bp_oldreq.bpr_ptr =
1296 nilfs_btree_node_get_ptr(btree, node, 1307 nilfs_btree_node_get_ptr(node, path[level].bp_index,
1297 path[level].bp_index); 1308 ncblk);
1298 ret = nilfs_bmap_prepare_end_ptr(btree, 1309 ret = nilfs_bmap_prepare_end_ptr(btree,
1299 &path[level].bp_oldreq, dat); 1310 &path[level].bp_oldreq, dat);
1300 if (ret < 0) 1311 if (ret < 0)
@@ -1306,13 +1317,13 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
1306 goto out; 1317 goto out;
1307 } 1318 }
1308 1319
1309 parent = nilfs_btree_get_node(btree, path, level + 1); 1320 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1310 pindex = path[level + 1].bp_index; 1321 pindex = path[level + 1].bp_index;
1311 1322
1312 if (pindex > 0) { 1323 if (pindex > 0) {
1313 /* left sibling */ 1324 /* left sibling */
1314 sibptr = nilfs_btree_node_get_ptr(btree, parent, 1325 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1315 pindex - 1); 1326 ncmax);
1316 ret = nilfs_btree_get_block(btree, sibptr, &bh); 1327 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1317 if (ret < 0) 1328 if (ret < 0)
1318 goto err_out_curr_node; 1329 goto err_out_curr_node;
@@ -1331,8 +1342,8 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
1331 } else if (pindex < 1342 } else if (pindex <
1332 nilfs_btree_node_get_nchildren(parent) - 1) { 1343 nilfs_btree_node_get_nchildren(parent) - 1) {
1333 /* right sibling */ 1344 /* right sibling */
1334 sibptr = nilfs_btree_node_get_ptr(btree, parent, 1345 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1335 pindex + 1); 1346 ncmax);
1336 ret = nilfs_btree_get_block(btree, sibptr, &bh); 1347 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1337 if (ret < 0) 1348 if (ret < 0)
1338 goto err_out_curr_node; 1349 goto err_out_curr_node;
@@ -1368,7 +1379,8 @@ static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
1368 1379
1369 node = nilfs_btree_get_root(btree); 1380 node = nilfs_btree_get_root(btree);
1370 path[level].bp_oldreq.bpr_ptr = 1381 path[level].bp_oldreq.bpr_ptr =
1371 nilfs_btree_node_get_ptr(btree, node, path[level].bp_index); 1382 nilfs_btree_node_get_ptr(node, path[level].bp_index,
1383 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1372 1384
1373 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat); 1385 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
1374 if (ret < 0) 1386 if (ret < 0)
@@ -1476,7 +1488,8 @@ static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
1476 nchildren = nilfs_btree_node_get_nchildren(root); 1488 nchildren = nilfs_btree_node_get_nchildren(root);
1477 if (nchildren > 1) 1489 if (nchildren > 1)
1478 return 0; 1490 return 0;
1479 ptr = nilfs_btree_node_get_ptr(btree, root, nchildren - 1); 1491 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1492 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1480 ret = nilfs_btree_get_block(btree, ptr, &bh); 1493 ret = nilfs_btree_get_block(btree, ptr, &bh);
1481 if (ret < 0) 1494 if (ret < 0)
1482 return ret; 1495 return ret;
@@ -1504,22 +1517,25 @@ static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
1504 __le64 *dkeys; 1517 __le64 *dkeys;
1505 __le64 *dptrs; 1518 __le64 *dptrs;
1506 __u64 ptr; 1519 __u64 ptr;
1507 int nchildren, i, ret; 1520 int nchildren, ncmax, i, ret;
1508 1521
1509 root = nilfs_btree_get_root(btree); 1522 root = nilfs_btree_get_root(btree);
1510 switch (nilfs_btree_height(btree)) { 1523 switch (nilfs_btree_height(btree)) {
1511 case 2: 1524 case 2:
1512 bh = NULL; 1525 bh = NULL;
1513 node = root; 1526 node = root;
1527 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
1514 break; 1528 break;
1515 case 3: 1529 case 3:
1516 nchildren = nilfs_btree_node_get_nchildren(root); 1530 nchildren = nilfs_btree_node_get_nchildren(root);
1517 WARN_ON(nchildren > 1); 1531 WARN_ON(nchildren > 1);
1518 ptr = nilfs_btree_node_get_ptr(btree, root, nchildren - 1); 1532 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1533 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1519 ret = nilfs_btree_get_block(btree, ptr, &bh); 1534 ret = nilfs_btree_get_block(btree, ptr, &bh);
1520 if (ret < 0) 1535 if (ret < 0)
1521 return ret; 1536 return ret;
1522 node = (struct nilfs_btree_node *)bh->b_data; 1537 node = (struct nilfs_btree_node *)bh->b_data;
1538 ncmax = nilfs_btree_nchildren_per_block(btree);
1523 break; 1539 break;
1524 default: 1540 default:
1525 node = NULL; 1541 node = NULL;
@@ -1530,7 +1546,7 @@ static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
1530 if (nchildren < nitems) 1546 if (nchildren < nitems)
1531 nitems = nchildren; 1547 nitems = nchildren;
1532 dkeys = nilfs_btree_node_dkeys(node); 1548 dkeys = nilfs_btree_node_dkeys(node);
1533 dptrs = nilfs_btree_node_dptrs(node, btree); 1549 dptrs = nilfs_btree_node_dptrs(node, ncmax);
1534 for (i = 0; i < nitems; i++) { 1550 for (i = 0; i < nitems; i++) {
1535 keys[i] = le64_to_cpu(dkeys[i]); 1551 keys[i] = le64_to_cpu(dkeys[i]);
1536 ptrs[i] = le64_to_cpu(dptrs[i]); 1552 ptrs[i] = le64_to_cpu(dptrs[i]);
@@ -1607,6 +1623,7 @@ nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
1607 struct nilfs_btree_node *node; 1623 struct nilfs_btree_node *node;
1608 struct inode *dat; 1624 struct inode *dat;
1609 __u64 tmpptr; 1625 __u64 tmpptr;
1626 int ncblk;
1610 1627
1611 /* free resources */ 1628 /* free resources */
1612 if (btree->b_ops->bop_clear != NULL) 1629 if (btree->b_ops->bop_clear != NULL)
@@ -1624,8 +1641,9 @@ nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
1624 1641
1625 /* create child node at level 1 */ 1642 /* create child node at level 1 */
1626 node = (struct nilfs_btree_node *)bh->b_data; 1643 node = (struct nilfs_btree_node *)bh->b_data;
1627 nilfs_btree_node_init(btree, node, 0, 1, n, keys, ptrs); 1644 ncblk = nilfs_btree_nchildren_per_block(btree);
1628 nilfs_btree_node_insert(btree, node, key, dreq->bpr_ptr, n); 1645 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1646 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
1629 if (!buffer_dirty(bh)) 1647 if (!buffer_dirty(bh))
1630 nilfs_btnode_mark_dirty(bh); 1648 nilfs_btnode_mark_dirty(bh);
1631 if (!nilfs_bmap_dirty(btree)) 1649 if (!nilfs_bmap_dirty(btree))
@@ -1636,16 +1654,19 @@ nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
1636 /* create root node at level 2 */ 1654 /* create root node at level 2 */
1637 node = nilfs_btree_get_root(btree); 1655 node = nilfs_btree_get_root(btree);
1638 tmpptr = nreq->bpr_ptr; 1656 tmpptr = nreq->bpr_ptr;
1639 nilfs_btree_node_init(btree, node, NILFS_BTREE_NODE_ROOT, 1657 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1640 2, 1, &keys[0], &tmpptr); 1658 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1659 &keys[0], &tmpptr);
1641 } else { 1660 } else {
1642 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat); 1661 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1643 1662
1644 /* create root node at level 1 */ 1663 /* create root node at level 1 */
1645 node = nilfs_btree_get_root(btree); 1664 node = nilfs_btree_get_root(btree);
1646 nilfs_btree_node_init(btree, node, NILFS_BTREE_NODE_ROOT, 1665 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1647 1, n, keys, ptrs); 1666 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1648 nilfs_btree_node_insert(btree, node, key, dreq->bpr_ptr, n); 1667 keys, ptrs);
1668 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1669 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1649 if (!nilfs_bmap_dirty(btree)) 1670 if (!nilfs_bmap_dirty(btree))
1650 nilfs_bmap_set_dirty(btree); 1671 nilfs_bmap_set_dirty(btree);
1651 } 1672 }
@@ -1712,12 +1733,12 @@ static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
1712 int level, struct inode *dat) 1733 int level, struct inode *dat)
1713{ 1734{
1714 struct nilfs_btree_node *parent; 1735 struct nilfs_btree_node *parent;
1715 int ret; 1736 int ncmax, ret;
1716 1737
1717 parent = nilfs_btree_get_node(btree, path, level + 1); 1738 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1718 path[level].bp_oldreq.bpr_ptr = 1739 path[level].bp_oldreq.bpr_ptr =
1719 nilfs_btree_node_get_ptr(btree, parent, 1740 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1720 path[level + 1].bp_index); 1741 ncmax);
1721 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1; 1742 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
1722 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req, 1743 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1723 &path[level].bp_newreq.bpr_req); 1744 &path[level].bp_newreq.bpr_req);
@@ -1747,6 +1768,7 @@ static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
1747 int level, struct inode *dat) 1768 int level, struct inode *dat)
1748{ 1769{
1749 struct nilfs_btree_node *parent; 1770 struct nilfs_btree_node *parent;
1771 int ncmax;
1750 1772
1751 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req, 1773 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1752 &path[level].bp_newreq.bpr_req, 1774 &path[level].bp_newreq.bpr_req,
@@ -1760,9 +1782,9 @@ static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
1760 } 1782 }
1761 set_buffer_nilfs_volatile(path[level].bp_bh); 1783 set_buffer_nilfs_volatile(path[level].bp_bh);
1762 1784
1763 parent = nilfs_btree_get_node(btree, path, level + 1); 1785 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1764 nilfs_btree_node_set_ptr(btree, parent, path[level + 1].bp_index, 1786 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1765 path[level].bp_newreq.bpr_ptr); 1787 path[level].bp_newreq.bpr_ptr, ncmax);
1766} 1788}
1767 1789
1768static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree, 1790static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
@@ -1835,6 +1857,7 @@ static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
1835 struct nilfs_btree_node *parent; 1857 struct nilfs_btree_node *parent;
1836 struct inode *dat = nilfs_bmap_get_dat(btree); 1858 struct inode *dat = nilfs_bmap_get_dat(btree);
1837 __u64 ptr; 1859 __u64 ptr;
1860 int ncmax;
1838 1861
1839 get_bh(bh); 1862 get_bh(bh);
1840 path[level].bp_bh = bh; 1863 path[level].bp_bh = bh;
@@ -1844,9 +1867,10 @@ static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
1844 goto out; 1867 goto out;
1845 1868
1846 if (buffer_nilfs_volatile(path[level].bp_bh)) { 1869 if (buffer_nilfs_volatile(path[level].bp_bh)) {
1847 parent = nilfs_btree_get_node(btree, path, level + 1); 1870 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1848 ptr = nilfs_btree_node_get_ptr(btree, parent, 1871 ptr = nilfs_btree_node_get_ptr(parent,
1849 path[level + 1].bp_index); 1872 path[level + 1].bp_index,
1873 ncmax);
1850 ret = nilfs_dat_mark_dirty(dat, ptr); 1874 ret = nilfs_dat_mark_dirty(dat, ptr);
1851 if (ret < 0) 1875 if (ret < 0)
1852 goto out; 1876 goto out;
@@ -1990,11 +2014,11 @@ static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
1990 struct nilfs_btree_node *parent; 2014 struct nilfs_btree_node *parent;
1991 __u64 key; 2015 __u64 key;
1992 __u64 ptr; 2016 __u64 ptr;
1993 int ret; 2017 int ncmax, ret;
1994 2018
1995 parent = nilfs_btree_get_node(btree, path, level + 1); 2019 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1996 ptr = nilfs_btree_node_get_ptr(btree, parent, 2020 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1997 path[level + 1].bp_index); 2021 ncmax);
1998 if (buffer_nilfs_node(*bh)) { 2022 if (buffer_nilfs_node(*bh)) {
1999 path[level].bp_ctxt.oldkey = ptr; 2023 path[level].bp_ctxt.oldkey = ptr;
2000 path[level].bp_ctxt.newkey = blocknr; 2024 path[level].bp_ctxt.newkey = blocknr;
@@ -2010,8 +2034,8 @@ static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
2010 *bh = path[level].bp_ctxt.bh; 2034 *bh = path[level].bp_ctxt.bh;
2011 } 2035 }
2012 2036
2013 nilfs_btree_node_set_ptr(btree, parent, 2037 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2014 path[level + 1].bp_index, blocknr); 2038 ncmax);
2015 2039
2016 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index); 2040 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
2017 /* on-disk format */ 2041 /* on-disk format */
@@ -2033,10 +2057,11 @@ static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
2033 __u64 key; 2057 __u64 key;
2034 __u64 ptr; 2058 __u64 ptr;
2035 union nilfs_bmap_ptr_req req; 2059 union nilfs_bmap_ptr_req req;
2036 int ret; 2060 int ncmax, ret;
2037 2061
2038 parent = nilfs_btree_get_node(btree, path, level + 1); 2062 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2039 ptr = nilfs_btree_node_get_ptr(btree, parent, path[level + 1].bp_index); 2063 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2064 ncmax);
2040 req.bpr_ptr = ptr; 2065 req.bpr_ptr = ptr;
2041 ret = nilfs_dat_prepare_start(dat, &req.bpr_req); 2066 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2042 if (ret < 0) 2067 if (ret < 0)