x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / fs / nilfs2 / btree.c
blob06ffa135dfa651590a7a0dc25c76d0cb632bfe94
1 /*
2 * btree.c - NILFS B-tree.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * Written by Koji Sato.
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/pagevec.h>
23 #include "nilfs.h"
24 #include "page.h"
25 #include "btnode.h"
26 #include "btree.h"
27 #include "alloc.h"
28 #include "dat.h"
30 static void __nilfs_btree_init(struct nilfs_bmap *bmap);
32 static struct nilfs_btree_path *nilfs_btree_alloc_path(void)
34 struct nilfs_btree_path *path;
35 int level = NILFS_BTREE_LEVEL_DATA;
37 path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
38 if (path == NULL)
39 goto out;
41 for (; level < NILFS_BTREE_LEVEL_MAX; level++) {
42 path[level].bp_bh = NULL;
43 path[level].bp_sib_bh = NULL;
44 path[level].bp_index = 0;
45 path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
46 path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
47 path[level].bp_op = NULL;
50 out:
51 return path;
54 static void nilfs_btree_free_path(struct nilfs_btree_path *path)
56 int level = NILFS_BTREE_LEVEL_DATA;
58 for (; level < NILFS_BTREE_LEVEL_MAX; level++)
59 brelse(path[level].bp_bh);
61 kmem_cache_free(nilfs_btree_path_cache, path);
65 * B-tree node operations
67 static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
68 __u64 ptr, struct buffer_head **bhp)
70 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
71 struct buffer_head *bh;
73 bh = nilfs_btnode_create_block(btnc, ptr);
74 if (!bh)
75 return -ENOMEM;
77 set_buffer_nilfs_volatile(bh);
78 *bhp = bh;
79 return 0;
82 static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
84 return node->bn_flags;
87 static void
88 nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
90 node->bn_flags = flags;
93 static int nilfs_btree_node_root(const struct nilfs_btree_node *node)
95 return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
98 static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
100 return node->bn_level;
103 static void
104 nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
106 node->bn_level = level;
109 static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
111 return le16_to_cpu(node->bn_nchildren);
114 static void
115 nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
117 node->bn_nchildren = cpu_to_le16(nchildren);
120 static int nilfs_btree_node_size(const struct nilfs_bmap *btree)
122 return i_blocksize(btree->b_inode);
125 static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
127 return btree->b_nchildren_per_block;
130 static __le64 *
131 nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
133 return (__le64 *)((char *)(node + 1) +
134 (nilfs_btree_node_root(node) ?
135 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
138 static __le64 *
139 nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
141 return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
144 static __u64
145 nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
147 return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
150 static void
151 nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
153 *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
156 static __u64
157 nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
158 int ncmax)
160 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
163 static void
164 nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
165 int ncmax)
167 *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
170 static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
171 int level, int nchildren, int ncmax,
172 const __u64 *keys, const __u64 *ptrs)
174 __le64 *dkeys;
175 __le64 *dptrs;
176 int i;
178 nilfs_btree_node_set_flags(node, flags);
179 nilfs_btree_node_set_level(node, level);
180 nilfs_btree_node_set_nchildren(node, nchildren);
182 dkeys = nilfs_btree_node_dkeys(node);
183 dptrs = nilfs_btree_node_dptrs(node, ncmax);
184 for (i = 0; i < nchildren; i++) {
185 dkeys[i] = cpu_to_le64(keys[i]);
186 dptrs[i] = cpu_to_le64(ptrs[i]);
190 /* Assume the buffer heads corresponding to left and right are locked. */
191 static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
192 struct nilfs_btree_node *right,
193 int n, int lncmax, int rncmax)
195 __le64 *ldkeys, *rdkeys;
196 __le64 *ldptrs, *rdptrs;
197 int lnchildren, rnchildren;
199 ldkeys = nilfs_btree_node_dkeys(left);
200 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
201 lnchildren = nilfs_btree_node_get_nchildren(left);
203 rdkeys = nilfs_btree_node_dkeys(right);
204 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
205 rnchildren = nilfs_btree_node_get_nchildren(right);
207 memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
208 memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs));
209 memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys));
210 memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs));
212 lnchildren += n;
213 rnchildren -= n;
214 nilfs_btree_node_set_nchildren(left, lnchildren);
215 nilfs_btree_node_set_nchildren(right, rnchildren);
218 /* Assume that the buffer heads corresponding to left and right are locked. */
219 static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
220 struct nilfs_btree_node *right,
221 int n, int lncmax, int rncmax)
223 __le64 *ldkeys, *rdkeys;
224 __le64 *ldptrs, *rdptrs;
225 int lnchildren, rnchildren;
227 ldkeys = nilfs_btree_node_dkeys(left);
228 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
229 lnchildren = nilfs_btree_node_get_nchildren(left);
231 rdkeys = nilfs_btree_node_dkeys(right);
232 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
233 rnchildren = nilfs_btree_node_get_nchildren(right);
235 memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
236 memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs));
237 memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys));
238 memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs));
240 lnchildren -= n;
241 rnchildren += n;
242 nilfs_btree_node_set_nchildren(left, lnchildren);
243 nilfs_btree_node_set_nchildren(right, rnchildren);
246 /* Assume that the buffer head corresponding to node is locked. */
247 static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
248 __u64 key, __u64 ptr, int ncmax)
250 __le64 *dkeys;
251 __le64 *dptrs;
252 int nchildren;
254 dkeys = nilfs_btree_node_dkeys(node);
255 dptrs = nilfs_btree_node_dptrs(node, ncmax);
256 nchildren = nilfs_btree_node_get_nchildren(node);
257 if (index < nchildren) {
258 memmove(dkeys + index + 1, dkeys + index,
259 (nchildren - index) * sizeof(*dkeys));
260 memmove(dptrs + index + 1, dptrs + index,
261 (nchildren - index) * sizeof(*dptrs));
263 dkeys[index] = cpu_to_le64(key);
264 dptrs[index] = cpu_to_le64(ptr);
265 nchildren++;
266 nilfs_btree_node_set_nchildren(node, nchildren);
269 /* Assume that the buffer head corresponding to node is locked. */
270 static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
271 __u64 *keyp, __u64 *ptrp, int ncmax)
273 __u64 key;
274 __u64 ptr;
275 __le64 *dkeys;
276 __le64 *dptrs;
277 int nchildren;
279 dkeys = nilfs_btree_node_dkeys(node);
280 dptrs = nilfs_btree_node_dptrs(node, ncmax);
281 key = le64_to_cpu(dkeys[index]);
282 ptr = le64_to_cpu(dptrs[index]);
283 nchildren = nilfs_btree_node_get_nchildren(node);
284 if (keyp != NULL)
285 *keyp = key;
286 if (ptrp != NULL)
287 *ptrp = ptr;
289 if (index < nchildren - 1) {
290 memmove(dkeys + index, dkeys + index + 1,
291 (nchildren - index - 1) * sizeof(*dkeys));
292 memmove(dptrs + index, dptrs + index + 1,
293 (nchildren - index - 1) * sizeof(*dptrs));
295 nchildren--;
296 nilfs_btree_node_set_nchildren(node, nchildren);
299 static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
300 __u64 key, int *indexp)
302 __u64 nkey;
303 int index, low, high, s;
305 /* binary search */
306 low = 0;
307 high = nilfs_btree_node_get_nchildren(node) - 1;
308 index = 0;
309 s = 0;
310 while (low <= high) {
311 index = (low + high) / 2;
312 nkey = nilfs_btree_node_get_key(node, index);
313 if (nkey == key) {
314 s = 0;
315 goto out;
316 } else if (nkey < key) {
317 low = index + 1;
318 s = -1;
319 } else {
320 high = index - 1;
321 s = 1;
325 /* adjust index */
326 if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
327 if (s > 0 && index > 0)
328 index--;
329 } else if (s < 0)
330 index++;
332 out:
333 *indexp = index;
335 return s == 0;
339 * nilfs_btree_node_broken - verify consistency of btree node
340 * @node: btree node block to be examined
341 * @size: node size (in bytes)
342 * @inode: host inode of btree
343 * @blocknr: block number
345 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
347 static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
348 size_t size, struct inode *inode,
349 sector_t blocknr)
351 int level, flags, nchildren;
352 int ret = 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);
358 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
359 level >= NILFS_BTREE_LEVEL_MAX ||
360 (flags & NILFS_BTREE_NODE_ROOT) ||
361 nchildren < 0 ||
362 nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
363 nilfs_msg(inode->i_sb, KERN_CRIT,
364 "bad btree node (ino=%lu, blocknr=%llu): level = %d, flags = 0x%x, nchildren = %d",
365 inode->i_ino, (unsigned long long)blocknr, level,
366 flags, nchildren);
367 ret = 1;
369 return ret;
373 * nilfs_btree_root_broken - verify consistency of btree root node
374 * @node: btree root node to be examined
375 * @inode: host inode of btree
377 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
379 static int nilfs_btree_root_broken(const struct nilfs_btree_node *node,
380 struct inode *inode)
382 int level, flags, nchildren;
383 int ret = 0;
385 level = nilfs_btree_node_get_level(node);
386 flags = nilfs_btree_node_get_flags(node);
387 nchildren = nilfs_btree_node_get_nchildren(node);
389 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
390 level >= NILFS_BTREE_LEVEL_MAX ||
391 nchildren < 0 ||
392 nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) {
393 nilfs_msg(inode->i_sb, KERN_CRIT,
394 "bad btree root (ino=%lu): level = %d, flags = 0x%x, nchildren = %d",
395 inode->i_ino, level, flags, nchildren);
396 ret = 1;
398 return ret;
401 int nilfs_btree_broken_node_block(struct buffer_head *bh)
403 struct inode *inode;
404 int ret;
406 if (buffer_nilfs_checked(bh))
407 return 0;
409 inode = bh->b_page->mapping->host;
410 ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
411 bh->b_size, inode, bh->b_blocknr);
412 if (likely(!ret))
413 set_buffer_nilfs_checked(bh);
414 return ret;
417 static struct nilfs_btree_node *
418 nilfs_btree_get_root(const struct nilfs_bmap *btree)
420 return (struct nilfs_btree_node *)btree->b_u.u_data;
423 static struct nilfs_btree_node *
424 nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
426 return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
429 static struct nilfs_btree_node *
430 nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
432 return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
435 static int nilfs_btree_height(const struct nilfs_bmap *btree)
437 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
440 static struct nilfs_btree_node *
441 nilfs_btree_get_node(const struct nilfs_bmap *btree,
442 const struct nilfs_btree_path *path,
443 int level, int *ncmaxp)
445 struct nilfs_btree_node *node;
447 if (level == nilfs_btree_height(btree) - 1) {
448 node = nilfs_btree_get_root(btree);
449 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
450 } else {
451 node = nilfs_btree_get_nonroot_node(path, level);
452 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
454 return node;
457 static int nilfs_btree_bad_node(const struct nilfs_bmap *btree,
458 struct nilfs_btree_node *node, int level)
460 if (unlikely(nilfs_btree_node_get_level(node) != level)) {
461 dump_stack();
462 nilfs_msg(btree->b_inode->i_sb, KERN_CRIT,
463 "btree level mismatch (ino=%lu): %d != %d",
464 btree->b_inode->i_ino,
465 nilfs_btree_node_get_level(node), level);
466 return 1;
468 return 0;
471 struct nilfs_btree_readahead_info {
472 struct nilfs_btree_node *node; /* parent node */
473 int max_ra_blocks; /* max nof blocks to read ahead */
474 int index; /* current index on the parent node */
475 int ncmax; /* nof children in the parent node */
478 static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
479 struct buffer_head **bhp,
480 const struct nilfs_btree_readahead_info *ra)
482 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
483 struct buffer_head *bh, *ra_bh;
484 sector_t submit_ptr = 0;
485 int ret;
487 ret = nilfs_btnode_submit_block(btnc, ptr, 0, REQ_OP_READ, 0, &bh,
488 &submit_ptr);
489 if (ret) {
490 if (ret != -EEXIST)
491 return ret;
492 goto out_check;
495 if (ra) {
496 int i, n;
497 __u64 ptr2;
499 /* read ahead sibling nodes */
500 for (n = ra->max_ra_blocks, i = ra->index + 1;
501 n > 0 && i < ra->ncmax; n--, i++) {
502 ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
504 ret = nilfs_btnode_submit_block(btnc, ptr2, 0,
505 REQ_OP_READ, REQ_RAHEAD,
506 &ra_bh, &submit_ptr);
507 if (likely(!ret || ret == -EEXIST))
508 brelse(ra_bh);
509 else if (ret != -EBUSY)
510 break;
511 if (!buffer_locked(bh))
512 goto out_no_wait;
516 wait_on_buffer(bh);
518 out_no_wait:
519 if (!buffer_uptodate(bh)) {
520 nilfs_msg(btree->b_inode->i_sb, KERN_ERR,
521 "I/O error reading b-tree node block (ino=%lu, blocknr=%llu)",
522 btree->b_inode->i_ino, (unsigned long long)ptr);
523 brelse(bh);
524 return -EIO;
527 out_check:
528 if (nilfs_btree_broken_node_block(bh)) {
529 clear_buffer_uptodate(bh);
530 brelse(bh);
531 return -EINVAL;
534 *bhp = bh;
535 return 0;
538 static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
539 struct buffer_head **bhp)
541 return __nilfs_btree_get_block(btree, ptr, bhp, NULL);
544 static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
545 struct nilfs_btree_path *path,
546 __u64 key, __u64 *ptrp, int minlevel,
547 int readahead)
549 struct nilfs_btree_node *node;
550 struct nilfs_btree_readahead_info p, *ra;
551 __u64 ptr;
552 int level, index, found, ncmax, ret;
554 node = nilfs_btree_get_root(btree);
555 level = nilfs_btree_node_get_level(node);
556 if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
557 return -ENOENT;
559 found = nilfs_btree_node_lookup(node, key, &index);
560 ptr = nilfs_btree_node_get_ptr(node, index,
561 NILFS_BTREE_ROOT_NCHILDREN_MAX);
562 path[level].bp_bh = NULL;
563 path[level].bp_index = index;
565 ncmax = nilfs_btree_nchildren_per_block(btree);
567 while (--level >= minlevel) {
568 ra = NULL;
569 if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) {
570 p.node = nilfs_btree_get_node(btree, path, level + 1,
571 &p.ncmax);
572 p.index = index;
573 p.max_ra_blocks = 7;
574 ra = &p;
576 ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh,
577 ra);
578 if (ret < 0)
579 return ret;
581 node = nilfs_btree_get_nonroot_node(path, level);
582 if (nilfs_btree_bad_node(btree, node, level))
583 return -EINVAL;
584 if (!found)
585 found = nilfs_btree_node_lookup(node, key, &index);
586 else
587 index = 0;
588 if (index < ncmax) {
589 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
590 } else {
591 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
592 /* insert */
593 ptr = NILFS_BMAP_INVALID_PTR;
595 path[level].bp_index = index;
597 if (!found)
598 return -ENOENT;
600 if (ptrp != NULL)
601 *ptrp = ptr;
603 return 0;
606 static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
607 struct nilfs_btree_path *path,
608 __u64 *keyp, __u64 *ptrp)
610 struct nilfs_btree_node *node;
611 __u64 ptr;
612 int index, level, ncmax, ret;
614 node = nilfs_btree_get_root(btree);
615 index = nilfs_btree_node_get_nchildren(node) - 1;
616 if (index < 0)
617 return -ENOENT;
618 level = nilfs_btree_node_get_level(node);
619 ptr = nilfs_btree_node_get_ptr(node, index,
620 NILFS_BTREE_ROOT_NCHILDREN_MAX);
621 path[level].bp_bh = NULL;
622 path[level].bp_index = index;
623 ncmax = nilfs_btree_nchildren_per_block(btree);
625 for (level--; level > 0; level--) {
626 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
627 if (ret < 0)
628 return ret;
629 node = nilfs_btree_get_nonroot_node(path, level);
630 if (nilfs_btree_bad_node(btree, node, level))
631 return -EINVAL;
632 index = nilfs_btree_node_get_nchildren(node) - 1;
633 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
634 path[level].bp_index = index;
637 if (keyp != NULL)
638 *keyp = nilfs_btree_node_get_key(node, index);
639 if (ptrp != NULL)
640 *ptrp = ptr;
642 return 0;
646 * nilfs_btree_get_next_key - get next valid key from btree path array
647 * @btree: bmap struct of btree
648 * @path: array of nilfs_btree_path struct
649 * @minlevel: start level
650 * @nextkey: place to store the next valid key
652 * Return Value: If a next key was found, 0 is returned. Otherwise,
653 * -ENOENT is returned.
655 static int nilfs_btree_get_next_key(const struct nilfs_bmap *btree,
656 const struct nilfs_btree_path *path,
657 int minlevel, __u64 *nextkey)
659 struct nilfs_btree_node *node;
660 int maxlevel = nilfs_btree_height(btree) - 1;
661 int index, next_adj, level;
663 /* Next index is already set to bp_index for leaf nodes. */
664 next_adj = 0;
665 for (level = minlevel; level <= maxlevel; level++) {
666 if (level == maxlevel)
667 node = nilfs_btree_get_root(btree);
668 else
669 node = nilfs_btree_get_nonroot_node(path, level);
671 index = path[level].bp_index + next_adj;
672 if (index < nilfs_btree_node_get_nchildren(node)) {
673 /* Next key is in this node */
674 *nextkey = nilfs_btree_node_get_key(node, index);
675 return 0;
677 /* For non-leaf nodes, next index is stored at bp_index + 1. */
678 next_adj = 1;
680 return -ENOENT;
683 static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
684 __u64 key, int level, __u64 *ptrp)
686 struct nilfs_btree_path *path;
687 int ret;
689 path = nilfs_btree_alloc_path();
690 if (path == NULL)
691 return -ENOMEM;
693 ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0);
695 nilfs_btree_free_path(path);
697 return ret;
700 static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
701 __u64 key, __u64 *ptrp,
702 unsigned int maxblocks)
704 struct nilfs_btree_path *path;
705 struct nilfs_btree_node *node;
706 struct inode *dat = NULL;
707 __u64 ptr, ptr2;
708 sector_t blocknr;
709 int level = NILFS_BTREE_LEVEL_NODE_MIN;
710 int ret, cnt, index, maxlevel, ncmax;
711 struct nilfs_btree_readahead_info p;
713 path = nilfs_btree_alloc_path();
714 if (path == NULL)
715 return -ENOMEM;
717 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
718 if (ret < 0)
719 goto out;
721 if (NILFS_BMAP_USE_VBN(btree)) {
722 dat = nilfs_bmap_get_dat(btree);
723 ret = nilfs_dat_translate(dat, ptr, &blocknr);
724 if (ret < 0)
725 goto out;
726 ptr = blocknr;
728 cnt = 1;
729 if (cnt == maxblocks)
730 goto end;
732 maxlevel = nilfs_btree_height(btree) - 1;
733 node = nilfs_btree_get_node(btree, path, level, &ncmax);
734 index = path[level].bp_index + 1;
735 for (;;) {
736 while (index < nilfs_btree_node_get_nchildren(node)) {
737 if (nilfs_btree_node_get_key(node, index) !=
738 key + cnt)
739 goto end;
740 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
741 if (dat) {
742 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
743 if (ret < 0)
744 goto out;
745 ptr2 = blocknr;
747 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
748 goto end;
749 index++;
750 continue;
752 if (level == maxlevel)
753 break;
755 /* look-up right sibling node */
756 p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax);
757 p.index = path[level + 1].bp_index + 1;
758 p.max_ra_blocks = 7;
759 if (p.index >= nilfs_btree_node_get_nchildren(p.node) ||
760 nilfs_btree_node_get_key(p.node, p.index) != key + cnt)
761 break;
762 ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax);
763 path[level + 1].bp_index = p.index;
765 brelse(path[level].bp_bh);
766 path[level].bp_bh = NULL;
768 ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
769 &p);
770 if (ret < 0)
771 goto out;
772 node = nilfs_btree_get_nonroot_node(path, level);
773 ncmax = nilfs_btree_nchildren_per_block(btree);
774 index = 0;
775 path[level].bp_index = index;
777 end:
778 *ptrp = ptr;
779 ret = cnt;
780 out:
781 nilfs_btree_free_path(path);
782 return ret;
785 static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
786 struct nilfs_btree_path *path,
787 int level, __u64 key)
789 if (level < nilfs_btree_height(btree) - 1) {
790 do {
791 nilfs_btree_node_set_key(
792 nilfs_btree_get_nonroot_node(path, level),
793 path[level].bp_index, key);
794 if (!buffer_dirty(path[level].bp_bh))
795 mark_buffer_dirty(path[level].bp_bh);
796 } while ((path[level].bp_index == 0) &&
797 (++level < nilfs_btree_height(btree) - 1));
800 /* root */
801 if (level == nilfs_btree_height(btree) - 1) {
802 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
803 path[level].bp_index, key);
807 static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
808 struct nilfs_btree_path *path,
809 int level, __u64 *keyp, __u64 *ptrp)
811 struct nilfs_btree_node *node;
812 int ncblk;
814 if (level < nilfs_btree_height(btree) - 1) {
815 node = nilfs_btree_get_nonroot_node(path, level);
816 ncblk = nilfs_btree_nchildren_per_block(btree);
817 nilfs_btree_node_insert(node, path[level].bp_index,
818 *keyp, *ptrp, ncblk);
819 if (!buffer_dirty(path[level].bp_bh))
820 mark_buffer_dirty(path[level].bp_bh);
822 if (path[level].bp_index == 0)
823 nilfs_btree_promote_key(btree, path, level + 1,
824 nilfs_btree_node_get_key(node,
825 0));
826 } else {
827 node = nilfs_btree_get_root(btree);
828 nilfs_btree_node_insert(node, path[level].bp_index,
829 *keyp, *ptrp,
830 NILFS_BTREE_ROOT_NCHILDREN_MAX);
834 static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
835 struct nilfs_btree_path *path,
836 int level, __u64 *keyp, __u64 *ptrp)
838 struct nilfs_btree_node *node, *left;
839 int nchildren, lnchildren, n, move, ncblk;
841 node = nilfs_btree_get_nonroot_node(path, level);
842 left = nilfs_btree_get_sib_node(path, level);
843 nchildren = nilfs_btree_node_get_nchildren(node);
844 lnchildren = nilfs_btree_node_get_nchildren(left);
845 ncblk = nilfs_btree_nchildren_per_block(btree);
846 move = 0;
848 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
849 if (n > path[level].bp_index) {
850 /* move insert point */
851 n--;
852 move = 1;
855 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
857 if (!buffer_dirty(path[level].bp_bh))
858 mark_buffer_dirty(path[level].bp_bh);
859 if (!buffer_dirty(path[level].bp_sib_bh))
860 mark_buffer_dirty(path[level].bp_sib_bh);
862 nilfs_btree_promote_key(btree, path, level + 1,
863 nilfs_btree_node_get_key(node, 0));
865 if (move) {
866 brelse(path[level].bp_bh);
867 path[level].bp_bh = path[level].bp_sib_bh;
868 path[level].bp_sib_bh = NULL;
869 path[level].bp_index += lnchildren;
870 path[level + 1].bp_index--;
871 } else {
872 brelse(path[level].bp_sib_bh);
873 path[level].bp_sib_bh = NULL;
874 path[level].bp_index -= n;
877 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
880 static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
881 struct nilfs_btree_path *path,
882 int level, __u64 *keyp, __u64 *ptrp)
884 struct nilfs_btree_node *node, *right;
885 int nchildren, rnchildren, n, move, ncblk;
887 node = nilfs_btree_get_nonroot_node(path, level);
888 right = nilfs_btree_get_sib_node(path, level);
889 nchildren = nilfs_btree_node_get_nchildren(node);
890 rnchildren = nilfs_btree_node_get_nchildren(right);
891 ncblk = nilfs_btree_nchildren_per_block(btree);
892 move = 0;
894 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
895 if (n > nchildren - path[level].bp_index) {
896 /* move insert point */
897 n--;
898 move = 1;
901 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
903 if (!buffer_dirty(path[level].bp_bh))
904 mark_buffer_dirty(path[level].bp_bh);
905 if (!buffer_dirty(path[level].bp_sib_bh))
906 mark_buffer_dirty(path[level].bp_sib_bh);
908 path[level + 1].bp_index++;
909 nilfs_btree_promote_key(btree, path, level + 1,
910 nilfs_btree_node_get_key(right, 0));
911 path[level + 1].bp_index--;
913 if (move) {
914 brelse(path[level].bp_bh);
915 path[level].bp_bh = path[level].bp_sib_bh;
916 path[level].bp_sib_bh = NULL;
917 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
918 path[level + 1].bp_index++;
919 } else {
920 brelse(path[level].bp_sib_bh);
921 path[level].bp_sib_bh = NULL;
924 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
927 static void nilfs_btree_split(struct nilfs_bmap *btree,
928 struct nilfs_btree_path *path,
929 int level, __u64 *keyp, __u64 *ptrp)
931 struct nilfs_btree_node *node, *right;
932 int nchildren, n, move, ncblk;
934 node = nilfs_btree_get_nonroot_node(path, level);
935 right = nilfs_btree_get_sib_node(path, level);
936 nchildren = nilfs_btree_node_get_nchildren(node);
937 ncblk = nilfs_btree_nchildren_per_block(btree);
938 move = 0;
940 n = (nchildren + 1) / 2;
941 if (n > nchildren - path[level].bp_index) {
942 n--;
943 move = 1;
946 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
948 if (!buffer_dirty(path[level].bp_bh))
949 mark_buffer_dirty(path[level].bp_bh);
950 if (!buffer_dirty(path[level].bp_sib_bh))
951 mark_buffer_dirty(path[level].bp_sib_bh);
953 if (move) {
954 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
955 nilfs_btree_node_insert(right, path[level].bp_index,
956 *keyp, *ptrp, ncblk);
958 *keyp = nilfs_btree_node_get_key(right, 0);
959 *ptrp = path[level].bp_newreq.bpr_ptr;
961 brelse(path[level].bp_bh);
962 path[level].bp_bh = path[level].bp_sib_bh;
963 path[level].bp_sib_bh = NULL;
964 } else {
965 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
967 *keyp = nilfs_btree_node_get_key(right, 0);
968 *ptrp = path[level].bp_newreq.bpr_ptr;
970 brelse(path[level].bp_sib_bh);
971 path[level].bp_sib_bh = NULL;
974 path[level + 1].bp_index++;
977 static void nilfs_btree_grow(struct nilfs_bmap *btree,
978 struct nilfs_btree_path *path,
979 int level, __u64 *keyp, __u64 *ptrp)
981 struct nilfs_btree_node *root, *child;
982 int n, ncblk;
984 root = nilfs_btree_get_root(btree);
985 child = nilfs_btree_get_sib_node(path, level);
986 ncblk = nilfs_btree_nchildren_per_block(btree);
988 n = nilfs_btree_node_get_nchildren(root);
990 nilfs_btree_node_move_right(root, child, n,
991 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
992 nilfs_btree_node_set_level(root, level + 1);
994 if (!buffer_dirty(path[level].bp_sib_bh))
995 mark_buffer_dirty(path[level].bp_sib_bh);
997 path[level].bp_bh = path[level].bp_sib_bh;
998 path[level].bp_sib_bh = NULL;
1000 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
1002 *keyp = nilfs_btree_node_get_key(child, 0);
1003 *ptrp = path[level].bp_newreq.bpr_ptr;
1006 static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
1007 const struct nilfs_btree_path *path)
1009 struct nilfs_btree_node *node;
1010 int level, ncmax;
1012 if (path == NULL)
1013 return NILFS_BMAP_INVALID_PTR;
1015 /* left sibling */
1016 level = NILFS_BTREE_LEVEL_NODE_MIN;
1017 if (path[level].bp_index > 0) {
1018 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1019 return nilfs_btree_node_get_ptr(node,
1020 path[level].bp_index - 1,
1021 ncmax);
1024 /* parent */
1025 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
1026 if (level <= nilfs_btree_height(btree) - 1) {
1027 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1028 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
1029 ncmax);
1032 return NILFS_BMAP_INVALID_PTR;
1035 static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
1036 const struct nilfs_btree_path *path,
1037 __u64 key)
1039 __u64 ptr;
1041 ptr = nilfs_bmap_find_target_seq(btree, key);
1042 if (ptr != NILFS_BMAP_INVALID_PTR)
1043 /* sequential access */
1044 return ptr;
1046 ptr = nilfs_btree_find_near(btree, path);
1047 if (ptr != NILFS_BMAP_INVALID_PTR)
1048 /* near */
1049 return ptr;
1051 /* block group */
1052 return nilfs_bmap_find_target_in_group(btree);
1055 static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
1056 struct nilfs_btree_path *path,
1057 int *levelp, __u64 key, __u64 ptr,
1058 struct nilfs_bmap_stats *stats)
1060 struct buffer_head *bh;
1061 struct nilfs_btree_node *node, *parent, *sib;
1062 __u64 sibptr;
1063 int pindex, level, ncmax, ncblk, ret;
1064 struct inode *dat = NULL;
1066 stats->bs_nblocks = 0;
1067 level = NILFS_BTREE_LEVEL_DATA;
1069 /* allocate a new ptr for data block */
1070 if (NILFS_BMAP_USE_VBN(btree)) {
1071 path[level].bp_newreq.bpr_ptr =
1072 nilfs_btree_find_target_v(btree, path, key);
1073 dat = nilfs_bmap_get_dat(btree);
1076 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
1077 if (ret < 0)
1078 goto err_out_data;
1080 ncblk = nilfs_btree_nchildren_per_block(btree);
1082 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1083 level < nilfs_btree_height(btree) - 1;
1084 level++) {
1085 node = nilfs_btree_get_nonroot_node(path, level);
1086 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
1087 path[level].bp_op = nilfs_btree_do_insert;
1088 stats->bs_nblocks++;
1089 goto out;
1092 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1093 pindex = path[level + 1].bp_index;
1095 /* left sibling */
1096 if (pindex > 0) {
1097 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1098 ncmax);
1099 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1100 if (ret < 0)
1101 goto err_out_child_node;
1102 sib = (struct nilfs_btree_node *)bh->b_data;
1103 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
1104 path[level].bp_sib_bh = bh;
1105 path[level].bp_op = nilfs_btree_carry_left;
1106 stats->bs_nblocks++;
1107 goto out;
1108 } else {
1109 brelse(bh);
1113 /* right sibling */
1114 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1115 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1116 ncmax);
1117 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1118 if (ret < 0)
1119 goto err_out_child_node;
1120 sib = (struct nilfs_btree_node *)bh->b_data;
1121 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
1122 path[level].bp_sib_bh = bh;
1123 path[level].bp_op = nilfs_btree_carry_right;
1124 stats->bs_nblocks++;
1125 goto out;
1126 } else {
1127 brelse(bh);
1131 /* split */
1132 path[level].bp_newreq.bpr_ptr =
1133 path[level - 1].bp_newreq.bpr_ptr + 1;
1134 ret = nilfs_bmap_prepare_alloc_ptr(btree,
1135 &path[level].bp_newreq, dat);
1136 if (ret < 0)
1137 goto err_out_child_node;
1138 ret = nilfs_btree_get_new_block(btree,
1139 path[level].bp_newreq.bpr_ptr,
1140 &bh);
1141 if (ret < 0)
1142 goto err_out_curr_node;
1144 stats->bs_nblocks++;
1146 sib = (struct nilfs_btree_node *)bh->b_data;
1147 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
1148 path[level].bp_sib_bh = bh;
1149 path[level].bp_op = nilfs_btree_split;
1152 /* root */
1153 node = nilfs_btree_get_root(btree);
1154 if (nilfs_btree_node_get_nchildren(node) <
1155 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1156 path[level].bp_op = nilfs_btree_do_insert;
1157 stats->bs_nblocks++;
1158 goto out;
1161 /* grow */
1162 path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
1163 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
1164 if (ret < 0)
1165 goto err_out_child_node;
1166 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1167 &bh);
1168 if (ret < 0)
1169 goto err_out_curr_node;
1171 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1172 0, level, 0, ncblk, NULL, NULL);
1173 path[level].bp_sib_bh = bh;
1174 path[level].bp_op = nilfs_btree_grow;
1176 level++;
1177 path[level].bp_op = nilfs_btree_do_insert;
1179 /* a newly-created node block and a data block are added */
1180 stats->bs_nblocks += 2;
1182 /* success */
1183 out:
1184 *levelp = level;
1185 return ret;
1187 /* error */
1188 err_out_curr_node:
1189 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
1190 err_out_child_node:
1191 for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
1192 nilfs_btnode_delete(path[level].bp_sib_bh);
1193 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
1197 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
1198 err_out_data:
1199 *levelp = level;
1200 stats->bs_nblocks = 0;
1201 return ret;
1204 static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
1205 struct nilfs_btree_path *path,
1206 int maxlevel, __u64 key, __u64 ptr)
1208 struct inode *dat = NULL;
1209 int level;
1211 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1212 ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
1213 if (NILFS_BMAP_USE_VBN(btree)) {
1214 nilfs_bmap_set_target_v(btree, key, ptr);
1215 dat = nilfs_bmap_get_dat(btree);
1218 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
1219 nilfs_bmap_commit_alloc_ptr(btree,
1220 &path[level - 1].bp_newreq, dat);
1221 path[level].bp_op(btree, path, level, &key, &ptr);
1224 if (!nilfs_bmap_dirty(btree))
1225 nilfs_bmap_set_dirty(btree);
1228 static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
1230 struct nilfs_btree_path *path;
1231 struct nilfs_bmap_stats stats;
1232 int level, ret;
1234 path = nilfs_btree_alloc_path();
1235 if (path == NULL)
1236 return -ENOMEM;
1238 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
1239 NILFS_BTREE_LEVEL_NODE_MIN, 0);
1240 if (ret != -ENOENT) {
1241 if (ret == 0)
1242 ret = -EEXIST;
1243 goto out;
1246 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1247 if (ret < 0)
1248 goto out;
1249 nilfs_btree_commit_insert(btree, path, level, key, ptr);
1250 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
1252 out:
1253 nilfs_btree_free_path(path);
1254 return ret;
1257 static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
1258 struct nilfs_btree_path *path,
1259 int level, __u64 *keyp, __u64 *ptrp)
1261 struct nilfs_btree_node *node;
1262 int ncblk;
1264 if (level < nilfs_btree_height(btree) - 1) {
1265 node = nilfs_btree_get_nonroot_node(path, level);
1266 ncblk = nilfs_btree_nchildren_per_block(btree);
1267 nilfs_btree_node_delete(node, path[level].bp_index,
1268 keyp, ptrp, ncblk);
1269 if (!buffer_dirty(path[level].bp_bh))
1270 mark_buffer_dirty(path[level].bp_bh);
1271 if (path[level].bp_index == 0)
1272 nilfs_btree_promote_key(btree, path, level + 1,
1273 nilfs_btree_node_get_key(node, 0));
1274 } else {
1275 node = nilfs_btree_get_root(btree);
1276 nilfs_btree_node_delete(node, path[level].bp_index,
1277 keyp, ptrp,
1278 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1282 static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
1283 struct nilfs_btree_path *path,
1284 int level, __u64 *keyp, __u64 *ptrp)
1286 struct nilfs_btree_node *node, *left;
1287 int nchildren, lnchildren, n, ncblk;
1289 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1291 node = nilfs_btree_get_nonroot_node(path, level);
1292 left = nilfs_btree_get_sib_node(path, level);
1293 nchildren = nilfs_btree_node_get_nchildren(node);
1294 lnchildren = nilfs_btree_node_get_nchildren(left);
1295 ncblk = nilfs_btree_nchildren_per_block(btree);
1297 n = (nchildren + lnchildren) / 2 - nchildren;
1299 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
1301 if (!buffer_dirty(path[level].bp_bh))
1302 mark_buffer_dirty(path[level].bp_bh);
1303 if (!buffer_dirty(path[level].bp_sib_bh))
1304 mark_buffer_dirty(path[level].bp_sib_bh);
1306 nilfs_btree_promote_key(btree, path, level + 1,
1307 nilfs_btree_node_get_key(node, 0));
1309 brelse(path[level].bp_sib_bh);
1310 path[level].bp_sib_bh = NULL;
1311 path[level].bp_index += n;
1314 static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
1315 struct nilfs_btree_path *path,
1316 int level, __u64 *keyp, __u64 *ptrp)
1318 struct nilfs_btree_node *node, *right;
1319 int nchildren, rnchildren, n, ncblk;
1321 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1323 node = nilfs_btree_get_nonroot_node(path, level);
1324 right = nilfs_btree_get_sib_node(path, level);
1325 nchildren = nilfs_btree_node_get_nchildren(node);
1326 rnchildren = nilfs_btree_node_get_nchildren(right);
1327 ncblk = nilfs_btree_nchildren_per_block(btree);
1329 n = (nchildren + rnchildren) / 2 - nchildren;
1331 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
1333 if (!buffer_dirty(path[level].bp_bh))
1334 mark_buffer_dirty(path[level].bp_bh);
1335 if (!buffer_dirty(path[level].bp_sib_bh))
1336 mark_buffer_dirty(path[level].bp_sib_bh);
1338 path[level + 1].bp_index++;
1339 nilfs_btree_promote_key(btree, path, level + 1,
1340 nilfs_btree_node_get_key(right, 0));
1341 path[level + 1].bp_index--;
1343 brelse(path[level].bp_sib_bh);
1344 path[level].bp_sib_bh = NULL;
1347 static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
1348 struct nilfs_btree_path *path,
1349 int level, __u64 *keyp, __u64 *ptrp)
1351 struct nilfs_btree_node *node, *left;
1352 int n, ncblk;
1354 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1356 node = nilfs_btree_get_nonroot_node(path, level);
1357 left = nilfs_btree_get_sib_node(path, level);
1358 ncblk = nilfs_btree_nchildren_per_block(btree);
1360 n = nilfs_btree_node_get_nchildren(node);
1362 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
1364 if (!buffer_dirty(path[level].bp_sib_bh))
1365 mark_buffer_dirty(path[level].bp_sib_bh);
1367 nilfs_btnode_delete(path[level].bp_bh);
1368 path[level].bp_bh = path[level].bp_sib_bh;
1369 path[level].bp_sib_bh = NULL;
1370 path[level].bp_index += nilfs_btree_node_get_nchildren(left);
1373 static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
1374 struct nilfs_btree_path *path,
1375 int level, __u64 *keyp, __u64 *ptrp)
1377 struct nilfs_btree_node *node, *right;
1378 int n, ncblk;
1380 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1382 node = nilfs_btree_get_nonroot_node(path, level);
1383 right = nilfs_btree_get_sib_node(path, level);
1384 ncblk = nilfs_btree_nchildren_per_block(btree);
1386 n = nilfs_btree_node_get_nchildren(right);
1388 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
1390 if (!buffer_dirty(path[level].bp_bh))
1391 mark_buffer_dirty(path[level].bp_bh);
1393 nilfs_btnode_delete(path[level].bp_sib_bh);
1394 path[level].bp_sib_bh = NULL;
1395 path[level + 1].bp_index++;
1398 static void nilfs_btree_shrink(struct nilfs_bmap *btree,
1399 struct nilfs_btree_path *path,
1400 int level, __u64 *keyp, __u64 *ptrp)
1402 struct nilfs_btree_node *root, *child;
1403 int n, ncblk;
1405 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1407 root = nilfs_btree_get_root(btree);
1408 child = nilfs_btree_get_nonroot_node(path, level);
1409 ncblk = nilfs_btree_nchildren_per_block(btree);
1411 nilfs_btree_node_delete(root, 0, NULL, NULL,
1412 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1413 nilfs_btree_node_set_level(root, level);
1414 n = nilfs_btree_node_get_nchildren(child);
1415 nilfs_btree_node_move_left(root, child, n,
1416 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
1418 nilfs_btnode_delete(path[level].bp_bh);
1419 path[level].bp_bh = NULL;
1422 static void nilfs_btree_nop(struct nilfs_bmap *btree,
1423 struct nilfs_btree_path *path,
1424 int level, __u64 *keyp, __u64 *ptrp)
1428 static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
1429 struct nilfs_btree_path *path,
1430 int *levelp,
1431 struct nilfs_bmap_stats *stats,
1432 struct inode *dat)
1434 struct buffer_head *bh;
1435 struct nilfs_btree_node *node, *parent, *sib;
1436 __u64 sibptr;
1437 int pindex, dindex, level, ncmin, ncmax, ncblk, ret;
1439 ret = 0;
1440 stats->bs_nblocks = 0;
1441 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
1442 ncblk = nilfs_btree_nchildren_per_block(btree);
1444 for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index;
1445 level < nilfs_btree_height(btree) - 1;
1446 level++) {
1447 node = nilfs_btree_get_nonroot_node(path, level);
1448 path[level].bp_oldreq.bpr_ptr =
1449 nilfs_btree_node_get_ptr(node, dindex, ncblk);
1450 ret = nilfs_bmap_prepare_end_ptr(btree,
1451 &path[level].bp_oldreq, dat);
1452 if (ret < 0)
1453 goto err_out_child_node;
1455 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
1456 path[level].bp_op = nilfs_btree_do_delete;
1457 stats->bs_nblocks++;
1458 goto out;
1461 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1462 pindex = path[level + 1].bp_index;
1463 dindex = pindex;
1465 if (pindex > 0) {
1466 /* left sibling */
1467 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1468 ncmax);
1469 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1470 if (ret < 0)
1471 goto err_out_curr_node;
1472 sib = (struct nilfs_btree_node *)bh->b_data;
1473 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
1474 path[level].bp_sib_bh = bh;
1475 path[level].bp_op = nilfs_btree_borrow_left;
1476 stats->bs_nblocks++;
1477 goto out;
1478 } else {
1479 path[level].bp_sib_bh = bh;
1480 path[level].bp_op = nilfs_btree_concat_left;
1481 stats->bs_nblocks++;
1482 /* continue; */
1484 } else if (pindex <
1485 nilfs_btree_node_get_nchildren(parent) - 1) {
1486 /* right sibling */
1487 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1488 ncmax);
1489 ret = nilfs_btree_get_block(btree, sibptr, &bh);
1490 if (ret < 0)
1491 goto err_out_curr_node;
1492 sib = (struct nilfs_btree_node *)bh->b_data;
1493 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
1494 path[level].bp_sib_bh = bh;
1495 path[level].bp_op = nilfs_btree_borrow_right;
1496 stats->bs_nblocks++;
1497 goto out;
1498 } else {
1499 path[level].bp_sib_bh = bh;
1500 path[level].bp_op = nilfs_btree_concat_right;
1501 stats->bs_nblocks++;
1503 * When merging right sibling node
1504 * into the current node, pointer to
1505 * the right sibling node must be
1506 * terminated instead. The adjustment
1507 * below is required for that.
1509 dindex = pindex + 1;
1510 /* continue; */
1512 } else {
1513 /* no siblings */
1514 /* the only child of the root node */
1515 WARN_ON(level != nilfs_btree_height(btree) - 2);
1516 if (nilfs_btree_node_get_nchildren(node) - 1 <=
1517 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1518 path[level].bp_op = nilfs_btree_shrink;
1519 stats->bs_nblocks += 2;
1520 level++;
1521 path[level].bp_op = nilfs_btree_nop;
1522 goto shrink_root_child;
1523 } else {
1524 path[level].bp_op = nilfs_btree_do_delete;
1525 stats->bs_nblocks++;
1526 goto out;
1531 /* child of the root node is deleted */
1532 path[level].bp_op = nilfs_btree_do_delete;
1533 stats->bs_nblocks++;
1535 shrink_root_child:
1536 node = nilfs_btree_get_root(btree);
1537 path[level].bp_oldreq.bpr_ptr =
1538 nilfs_btree_node_get_ptr(node, dindex,
1539 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1541 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
1542 if (ret < 0)
1543 goto err_out_child_node;
1545 /* success */
1546 out:
1547 *levelp = level;
1548 return ret;
1550 /* error */
1551 err_out_curr_node:
1552 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
1553 err_out_child_node:
1554 for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
1555 brelse(path[level].bp_sib_bh);
1556 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
1558 *levelp = level;
1559 stats->bs_nblocks = 0;
1560 return ret;
1563 static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
1564 struct nilfs_btree_path *path,
1565 int maxlevel, struct inode *dat)
1567 int level;
1569 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
1570 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
1571 path[level].bp_op(btree, path, level, NULL, NULL);
1574 if (!nilfs_bmap_dirty(btree))
1575 nilfs_bmap_set_dirty(btree);
1578 static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
1581 struct nilfs_btree_path *path;
1582 struct nilfs_bmap_stats stats;
1583 struct inode *dat;
1584 int level, ret;
1586 path = nilfs_btree_alloc_path();
1587 if (path == NULL)
1588 return -ENOMEM;
1590 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
1591 NILFS_BTREE_LEVEL_NODE_MIN, 0);
1592 if (ret < 0)
1593 goto out;
1596 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
1598 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
1599 if (ret < 0)
1600 goto out;
1601 nilfs_btree_commit_delete(btree, path, level, dat);
1602 nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks);
1604 out:
1605 nilfs_btree_free_path(path);
1606 return ret;
1609 static int nilfs_btree_seek_key(const struct nilfs_bmap *btree, __u64 start,
1610 __u64 *keyp)
1612 struct nilfs_btree_path *path;
1613 const int minlevel = NILFS_BTREE_LEVEL_NODE_MIN;
1614 int ret;
1616 path = nilfs_btree_alloc_path();
1617 if (!path)
1618 return -ENOMEM;
1620 ret = nilfs_btree_do_lookup(btree, path, start, NULL, minlevel, 0);
1621 if (!ret)
1622 *keyp = start;
1623 else if (ret == -ENOENT)
1624 ret = nilfs_btree_get_next_key(btree, path, minlevel, keyp);
1626 nilfs_btree_free_path(path);
1627 return ret;
1630 static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
1632 struct nilfs_btree_path *path;
1633 int ret;
1635 path = nilfs_btree_alloc_path();
1636 if (path == NULL)
1637 return -ENOMEM;
1639 ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
1641 nilfs_btree_free_path(path);
1643 return ret;
1646 static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
1648 struct buffer_head *bh;
1649 struct nilfs_btree_node *root, *node;
1650 __u64 maxkey, nextmaxkey;
1651 __u64 ptr;
1652 int nchildren, ret;
1654 root = nilfs_btree_get_root(btree);
1655 switch (nilfs_btree_height(btree)) {
1656 case 2:
1657 bh = NULL;
1658 node = root;
1659 break;
1660 case 3:
1661 nchildren = nilfs_btree_node_get_nchildren(root);
1662 if (nchildren > 1)
1663 return 0;
1664 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1665 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1666 ret = nilfs_btree_get_block(btree, ptr, &bh);
1667 if (ret < 0)
1668 return ret;
1669 node = (struct nilfs_btree_node *)bh->b_data;
1670 break;
1671 default:
1672 return 0;
1675 nchildren = nilfs_btree_node_get_nchildren(node);
1676 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
1677 nextmaxkey = (nchildren > 1) ?
1678 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
1679 if (bh != NULL)
1680 brelse(bh);
1682 return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
1685 static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
1686 __u64 *keys, __u64 *ptrs, int nitems)
1688 struct buffer_head *bh;
1689 struct nilfs_btree_node *node, *root;
1690 __le64 *dkeys;
1691 __le64 *dptrs;
1692 __u64 ptr;
1693 int nchildren, ncmax, i, ret;
1695 root = nilfs_btree_get_root(btree);
1696 switch (nilfs_btree_height(btree)) {
1697 case 2:
1698 bh = NULL;
1699 node = root;
1700 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
1701 break;
1702 case 3:
1703 nchildren = nilfs_btree_node_get_nchildren(root);
1704 WARN_ON(nchildren > 1);
1705 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1706 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1707 ret = nilfs_btree_get_block(btree, ptr, &bh);
1708 if (ret < 0)
1709 return ret;
1710 node = (struct nilfs_btree_node *)bh->b_data;
1711 ncmax = nilfs_btree_nchildren_per_block(btree);
1712 break;
1713 default:
1714 node = NULL;
1715 return -EINVAL;
1718 nchildren = nilfs_btree_node_get_nchildren(node);
1719 if (nchildren < nitems)
1720 nitems = nchildren;
1721 dkeys = nilfs_btree_node_dkeys(node);
1722 dptrs = nilfs_btree_node_dptrs(node, ncmax);
1723 for (i = 0; i < nitems; i++) {
1724 keys[i] = le64_to_cpu(dkeys[i]);
1725 ptrs[i] = le64_to_cpu(dptrs[i]);
1728 if (bh != NULL)
1729 brelse(bh);
1731 return nitems;
1734 static int
1735 nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
1736 union nilfs_bmap_ptr_req *dreq,
1737 union nilfs_bmap_ptr_req *nreq,
1738 struct buffer_head **bhp,
1739 struct nilfs_bmap_stats *stats)
1741 struct buffer_head *bh;
1742 struct inode *dat = NULL;
1743 int ret;
1745 stats->bs_nblocks = 0;
1747 /* for data */
1748 /* cannot find near ptr */
1749 if (NILFS_BMAP_USE_VBN(btree)) {
1750 dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
1751 dat = nilfs_bmap_get_dat(btree);
1754 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
1755 if (ret < 0)
1756 return ret;
1758 *bhp = NULL;
1759 stats->bs_nblocks++;
1760 if (nreq != NULL) {
1761 nreq->bpr_ptr = dreq->bpr_ptr + 1;
1762 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
1763 if (ret < 0)
1764 goto err_out_dreq;
1766 ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
1767 if (ret < 0)
1768 goto err_out_nreq;
1770 *bhp = bh;
1771 stats->bs_nblocks++;
1774 /* success */
1775 return 0;
1777 /* error */
1778 err_out_nreq:
1779 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
1780 err_out_dreq:
1781 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
1782 stats->bs_nblocks = 0;
1783 return ret;
1787 static void
1788 nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
1789 __u64 key, __u64 ptr,
1790 const __u64 *keys, const __u64 *ptrs,
1791 int n,
1792 union nilfs_bmap_ptr_req *dreq,
1793 union nilfs_bmap_ptr_req *nreq,
1794 struct buffer_head *bh)
1796 struct nilfs_btree_node *node;
1797 struct inode *dat;
1798 __u64 tmpptr;
1799 int ncblk;
1801 /* free resources */
1802 if (btree->b_ops->bop_clear != NULL)
1803 btree->b_ops->bop_clear(btree);
1805 /* ptr must be a pointer to a buffer head. */
1806 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1808 /* convert and insert */
1809 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
1810 __nilfs_btree_init(btree);
1811 if (nreq != NULL) {
1812 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1813 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
1815 /* create child node at level 1 */
1816 node = (struct nilfs_btree_node *)bh->b_data;
1817 ncblk = nilfs_btree_nchildren_per_block(btree);
1818 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1819 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
1820 if (!buffer_dirty(bh))
1821 mark_buffer_dirty(bh);
1822 if (!nilfs_bmap_dirty(btree))
1823 nilfs_bmap_set_dirty(btree);
1825 brelse(bh);
1827 /* create root node at level 2 */
1828 node = nilfs_btree_get_root(btree);
1829 tmpptr = nreq->bpr_ptr;
1830 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1831 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1832 &keys[0], &tmpptr);
1833 } else {
1834 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1836 /* create root node at level 1 */
1837 node = nilfs_btree_get_root(btree);
1838 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1839 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1840 keys, ptrs);
1841 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1842 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1843 if (!nilfs_bmap_dirty(btree))
1844 nilfs_bmap_set_dirty(btree);
1847 if (NILFS_BMAP_USE_VBN(btree))
1848 nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
1852 * nilfs_btree_convert_and_insert -
1853 * @bmap:
1854 * @key:
1855 * @ptr:
1856 * @keys:
1857 * @ptrs:
1858 * @n:
1860 int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
1861 __u64 key, __u64 ptr,
1862 const __u64 *keys, const __u64 *ptrs, int n)
1864 struct buffer_head *bh = NULL;
1865 union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
1866 struct nilfs_bmap_stats stats;
1867 int ret;
1869 if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1870 di = &dreq;
1871 ni = NULL;
1872 } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
1873 nilfs_btree_node_size(btree))) {
1874 di = &dreq;
1875 ni = &nreq;
1876 } else {
1877 di = NULL;
1878 ni = NULL;
1879 BUG();
1882 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
1883 &stats);
1884 if (ret < 0)
1885 return ret;
1886 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
1887 di, ni, bh);
1888 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
1889 return 0;
1892 static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
1893 struct nilfs_btree_path *path,
1894 int level,
1895 struct buffer_head *bh)
1897 while ((++level < nilfs_btree_height(btree) - 1) &&
1898 !buffer_dirty(path[level].bp_bh))
1899 mark_buffer_dirty(path[level].bp_bh);
1901 return 0;
1904 static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
1905 struct nilfs_btree_path *path,
1906 int level, struct inode *dat)
1908 struct nilfs_btree_node *parent;
1909 int ncmax, ret;
1911 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1912 path[level].bp_oldreq.bpr_ptr =
1913 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1914 ncmax);
1915 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
1916 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1917 &path[level].bp_newreq.bpr_req);
1918 if (ret < 0)
1919 return ret;
1921 if (buffer_nilfs_node(path[level].bp_bh)) {
1922 path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
1923 path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
1924 path[level].bp_ctxt.bh = path[level].bp_bh;
1925 ret = nilfs_btnode_prepare_change_key(
1926 &NILFS_BMAP_I(btree)->i_btnode_cache,
1927 &path[level].bp_ctxt);
1928 if (ret < 0) {
1929 nilfs_dat_abort_update(dat,
1930 &path[level].bp_oldreq.bpr_req,
1931 &path[level].bp_newreq.bpr_req);
1932 return ret;
1936 return 0;
1939 static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
1940 struct nilfs_btree_path *path,
1941 int level, struct inode *dat)
1943 struct nilfs_btree_node *parent;
1944 int ncmax;
1946 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1947 &path[level].bp_newreq.bpr_req,
1948 btree->b_ptr_type == NILFS_BMAP_PTR_VS);
1950 if (buffer_nilfs_node(path[level].bp_bh)) {
1951 nilfs_btnode_commit_change_key(
1952 &NILFS_BMAP_I(btree)->i_btnode_cache,
1953 &path[level].bp_ctxt);
1954 path[level].bp_bh = path[level].bp_ctxt.bh;
1956 set_buffer_nilfs_volatile(path[level].bp_bh);
1958 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1959 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1960 path[level].bp_newreq.bpr_ptr, ncmax);
1963 static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
1964 struct nilfs_btree_path *path,
1965 int level, struct inode *dat)
1967 nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
1968 &path[level].bp_newreq.bpr_req);
1969 if (buffer_nilfs_node(path[level].bp_bh))
1970 nilfs_btnode_abort_change_key(
1971 &NILFS_BMAP_I(btree)->i_btnode_cache,
1972 &path[level].bp_ctxt);
1975 static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
1976 struct nilfs_btree_path *path,
1977 int minlevel, int *maxlevelp,
1978 struct inode *dat)
1980 int level, ret;
1982 level = minlevel;
1983 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
1984 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
1985 if (ret < 0)
1986 return ret;
1988 while ((++level < nilfs_btree_height(btree) - 1) &&
1989 !buffer_dirty(path[level].bp_bh)) {
1991 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
1992 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
1993 if (ret < 0)
1994 goto out;
1997 /* success */
1998 *maxlevelp = level - 1;
1999 return 0;
2001 /* error */
2002 out:
2003 while (--level > minlevel)
2004 nilfs_btree_abort_update_v(btree, path, level, dat);
2005 if (!buffer_nilfs_volatile(path[level].bp_bh))
2006 nilfs_btree_abort_update_v(btree, path, level, dat);
2007 return ret;
2010 static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
2011 struct nilfs_btree_path *path,
2012 int minlevel, int maxlevel,
2013 struct buffer_head *bh,
2014 struct inode *dat)
2016 int level;
2018 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
2019 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
2021 for (level = minlevel + 1; level <= maxlevel; level++)
2022 nilfs_btree_commit_update_v(btree, path, level, dat);
2025 static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
2026 struct nilfs_btree_path *path,
2027 int level, struct buffer_head *bh)
2029 int maxlevel = 0, ret;
2030 struct nilfs_btree_node *parent;
2031 struct inode *dat = nilfs_bmap_get_dat(btree);
2032 __u64 ptr;
2033 int ncmax;
2035 get_bh(bh);
2036 path[level].bp_bh = bh;
2037 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
2038 dat);
2039 if (ret < 0)
2040 goto out;
2042 if (buffer_nilfs_volatile(path[level].bp_bh)) {
2043 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2044 ptr = nilfs_btree_node_get_ptr(parent,
2045 path[level + 1].bp_index,
2046 ncmax);
2047 ret = nilfs_dat_mark_dirty(dat, ptr);
2048 if (ret < 0)
2049 goto out;
2052 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
2054 out:
2055 brelse(path[level].bp_bh);
2056 path[level].bp_bh = NULL;
2057 return ret;
2060 static int nilfs_btree_propagate(struct nilfs_bmap *btree,
2061 struct buffer_head *bh)
2063 struct nilfs_btree_path *path;
2064 struct nilfs_btree_node *node;
2065 __u64 key;
2066 int level, ret;
2068 WARN_ON(!buffer_dirty(bh));
2070 path = nilfs_btree_alloc_path();
2071 if (path == NULL)
2072 return -ENOMEM;
2074 if (buffer_nilfs_node(bh)) {
2075 node = (struct nilfs_btree_node *)bh->b_data;
2076 key = nilfs_btree_node_get_key(node, 0);
2077 level = nilfs_btree_node_get_level(node);
2078 } else {
2079 key = nilfs_bmap_data_get_key(btree, bh);
2080 level = NILFS_BTREE_LEVEL_DATA;
2083 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
2084 if (ret < 0) {
2085 if (unlikely(ret == -ENOENT))
2086 nilfs_msg(btree->b_inode->i_sb, KERN_CRIT,
2087 "writing node/leaf block does not appear in b-tree (ino=%lu) at key=%llu, level=%d",
2088 btree->b_inode->i_ino,
2089 (unsigned long long)key, level);
2090 goto out;
2093 ret = NILFS_BMAP_USE_VBN(btree) ?
2094 nilfs_btree_propagate_v(btree, path, level, bh) :
2095 nilfs_btree_propagate_p(btree, path, level, bh);
2097 out:
2098 nilfs_btree_free_path(path);
2100 return ret;
2103 static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
2104 struct buffer_head *bh)
2106 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
2109 static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
2110 struct list_head *lists,
2111 struct buffer_head *bh)
2113 struct list_head *head;
2114 struct buffer_head *cbh;
2115 struct nilfs_btree_node *node, *cnode;
2116 __u64 key, ckey;
2117 int level;
2119 get_bh(bh);
2120 node = (struct nilfs_btree_node *)bh->b_data;
2121 key = nilfs_btree_node_get_key(node, 0);
2122 level = nilfs_btree_node_get_level(node);
2123 if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
2124 level >= NILFS_BTREE_LEVEL_MAX) {
2125 dump_stack();
2126 nilfs_msg(btree->b_inode->i_sb, KERN_WARNING,
2127 "invalid btree level: %d (key=%llu, ino=%lu, blocknr=%llu)",
2128 level, (unsigned long long)key,
2129 btree->b_inode->i_ino,
2130 (unsigned long long)bh->b_blocknr);
2131 return;
2134 list_for_each(head, &lists[level]) {
2135 cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
2136 cnode = (struct nilfs_btree_node *)cbh->b_data;
2137 ckey = nilfs_btree_node_get_key(cnode, 0);
2138 if (key < ckey)
2139 break;
2141 list_add_tail(&bh->b_assoc_buffers, head);
2144 static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
2145 struct list_head *listp)
2147 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
2148 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2149 struct pagevec pvec;
2150 struct buffer_head *bh, *head;
2151 pgoff_t index = 0;
2152 int level, i;
2154 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2155 level < NILFS_BTREE_LEVEL_MAX;
2156 level++)
2157 INIT_LIST_HEAD(&lists[level]);
2159 pagevec_init(&pvec, 0);
2161 while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
2162 PAGEVEC_SIZE)) {
2163 for (i = 0; i < pagevec_count(&pvec); i++) {
2164 bh = head = page_buffers(pvec.pages[i]);
2165 do {
2166 if (buffer_dirty(bh))
2167 nilfs_btree_add_dirty_buffer(btree,
2168 lists, bh);
2169 } while ((bh = bh->b_this_page) != head);
2171 pagevec_release(&pvec);
2172 cond_resched();
2175 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2176 level < NILFS_BTREE_LEVEL_MAX;
2177 level++)
2178 list_splice_tail(&lists[level], listp);
2181 static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
2182 struct nilfs_btree_path *path,
2183 int level,
2184 struct buffer_head **bh,
2185 sector_t blocknr,
2186 union nilfs_binfo *binfo)
2188 struct nilfs_btree_node *parent;
2189 __u64 key;
2190 __u64 ptr;
2191 int ncmax, ret;
2193 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2194 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2195 ncmax);
2196 if (buffer_nilfs_node(*bh)) {
2197 path[level].bp_ctxt.oldkey = ptr;
2198 path[level].bp_ctxt.newkey = blocknr;
2199 path[level].bp_ctxt.bh = *bh;
2200 ret = nilfs_btnode_prepare_change_key(
2201 &NILFS_BMAP_I(btree)->i_btnode_cache,
2202 &path[level].bp_ctxt);
2203 if (ret < 0)
2204 return ret;
2205 nilfs_btnode_commit_change_key(
2206 &NILFS_BMAP_I(btree)->i_btnode_cache,
2207 &path[level].bp_ctxt);
2208 *bh = path[level].bp_ctxt.bh;
2211 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2212 ncmax);
2214 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
2215 /* on-disk format */
2216 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
2217 binfo->bi_dat.bi_level = level;
2219 return 0;
2222 static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
2223 struct nilfs_btree_path *path,
2224 int level,
2225 struct buffer_head **bh,
2226 sector_t blocknr,
2227 union nilfs_binfo *binfo)
2229 struct nilfs_btree_node *parent;
2230 struct inode *dat = nilfs_bmap_get_dat(btree);
2231 __u64 key;
2232 __u64 ptr;
2233 union nilfs_bmap_ptr_req req;
2234 int ncmax, ret;
2236 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2237 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2238 ncmax);
2239 req.bpr_ptr = ptr;
2240 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2241 if (ret < 0)
2242 return ret;
2243 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
2245 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
2246 /* on-disk format */
2247 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
2248 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
2250 return 0;
2253 static int nilfs_btree_assign(struct nilfs_bmap *btree,
2254 struct buffer_head **bh,
2255 sector_t blocknr,
2256 union nilfs_binfo *binfo)
2258 struct nilfs_btree_path *path;
2259 struct nilfs_btree_node *node;
2260 __u64 key;
2261 int level, ret;
2263 path = nilfs_btree_alloc_path();
2264 if (path == NULL)
2265 return -ENOMEM;
2267 if (buffer_nilfs_node(*bh)) {
2268 node = (struct nilfs_btree_node *)(*bh)->b_data;
2269 key = nilfs_btree_node_get_key(node, 0);
2270 level = nilfs_btree_node_get_level(node);
2271 } else {
2272 key = nilfs_bmap_data_get_key(btree, *bh);
2273 level = NILFS_BTREE_LEVEL_DATA;
2276 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
2277 if (ret < 0) {
2278 WARN_ON(ret == -ENOENT);
2279 goto out;
2282 ret = NILFS_BMAP_USE_VBN(btree) ?
2283 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2284 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
2286 out:
2287 nilfs_btree_free_path(path);
2289 return ret;
2292 static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
2293 struct buffer_head **bh,
2294 sector_t blocknr,
2295 union nilfs_binfo *binfo)
2297 struct nilfs_btree_node *node;
2298 __u64 key;
2299 int ret;
2301 ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
2302 blocknr);
2303 if (ret < 0)
2304 return ret;
2306 if (buffer_nilfs_node(*bh)) {
2307 node = (struct nilfs_btree_node *)(*bh)->b_data;
2308 key = nilfs_btree_node_get_key(node, 0);
2309 } else
2310 key = nilfs_bmap_data_get_key(btree, *bh);
2312 /* on-disk format */
2313 binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
2314 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
2316 return 0;
2319 static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
2321 struct buffer_head *bh;
2322 struct nilfs_btree_path *path;
2323 __u64 ptr;
2324 int ret;
2326 path = nilfs_btree_alloc_path();
2327 if (path == NULL)
2328 return -ENOMEM;
2330 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
2331 if (ret < 0) {
2332 WARN_ON(ret == -ENOENT);
2333 goto out;
2335 ret = nilfs_btree_get_block(btree, ptr, &bh);
2336 if (ret < 0) {
2337 WARN_ON(ret == -ENOENT);
2338 goto out;
2341 if (!buffer_dirty(bh))
2342 mark_buffer_dirty(bh);
2343 brelse(bh);
2344 if (!nilfs_bmap_dirty(btree))
2345 nilfs_bmap_set_dirty(btree);
2347 out:
2348 nilfs_btree_free_path(path);
2349 return ret;
2352 static const struct nilfs_bmap_operations nilfs_btree_ops = {
2353 .bop_lookup = nilfs_btree_lookup,
2354 .bop_lookup_contig = nilfs_btree_lookup_contig,
2355 .bop_insert = nilfs_btree_insert,
2356 .bop_delete = nilfs_btree_delete,
2357 .bop_clear = NULL,
2359 .bop_propagate = nilfs_btree_propagate,
2361 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2363 .bop_assign = nilfs_btree_assign,
2364 .bop_mark = nilfs_btree_mark,
2366 .bop_seek_key = nilfs_btree_seek_key,
2367 .bop_last_key = nilfs_btree_last_key,
2369 .bop_check_insert = NULL,
2370 .bop_check_delete = nilfs_btree_check_delete,
2371 .bop_gather_data = nilfs_btree_gather_data,
2374 static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
2375 .bop_lookup = NULL,
2376 .bop_lookup_contig = NULL,
2377 .bop_insert = NULL,
2378 .bop_delete = NULL,
2379 .bop_clear = NULL,
2381 .bop_propagate = nilfs_btree_propagate_gc,
2383 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2385 .bop_assign = nilfs_btree_assign_gc,
2386 .bop_mark = NULL,
2388 .bop_seek_key = NULL,
2389 .bop_last_key = NULL,
2391 .bop_check_insert = NULL,
2392 .bop_check_delete = NULL,
2393 .bop_gather_data = NULL,
2396 static void __nilfs_btree_init(struct nilfs_bmap *bmap)
2398 bmap->b_ops = &nilfs_btree_ops;
2399 bmap->b_nchildren_per_block =
2400 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
2403 int nilfs_btree_init(struct nilfs_bmap *bmap)
2405 int ret = 0;
2407 __nilfs_btree_init(bmap);
2409 if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap), bmap->b_inode))
2410 ret = -EIO;
2411 return ret;
2414 void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
2416 bmap->b_ops = &nilfs_btree_ops_gc;
2417 bmap->b_nchildren_per_block =
2418 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));