Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * btree.c - NILFS B-tree. |
| 3 | * |
| 4 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
Ryusuke Konishi | 4b420ab | 2016-05-23 16:23:09 -0700 | [diff] [blame] | 16 | * Written by Koji Sato. |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 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" |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 28 | #include "dat.h" |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 29 | |
Ryusuke Konishi | 957ed60 | 2015-02-27 15:51:56 -0800 | [diff] [blame] | 30 | static void __nilfs_btree_init(struct nilfs_bmap *bmap); |
| 31 | |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 32 | static struct nilfs_btree_path *nilfs_btree_alloc_path(void) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 33 | { |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 34 | struct nilfs_btree_path *path; |
| 35 | int level = NILFS_BTREE_LEVEL_DATA; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 36 | |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 37 | path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS); |
| 38 | if (path == NULL) |
| 39 | goto out; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 40 | |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 41 | for (; level < NILFS_BTREE_LEVEL_MAX; level++) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 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; |
| 48 | } |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 49 | |
| 50 | out: |
| 51 | return path; |
| 52 | } |
| 53 | |
Li Hong | 73bb488 | 2010-04-02 18:35:00 +0800 | [diff] [blame] | 54 | static void nilfs_btree_free_path(struct nilfs_btree_path *path) |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 55 | { |
Li Hong | 73bb488 | 2010-04-02 18:35:00 +0800 | [diff] [blame] | 56 | int level = NILFS_BTREE_LEVEL_DATA; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 57 | |
Li Hong | 73bb488 | 2010-04-02 18:35:00 +0800 | [diff] [blame] | 58 | for (; level < NILFS_BTREE_LEVEL_MAX; level++) |
Ryusuke Konishi | 3218929 | 2009-08-15 01:54:59 +0900 | [diff] [blame] | 59 | brelse(path[level].bp_bh); |
Li Hong | 73bb488 | 2010-04-02 18:35:00 +0800 | [diff] [blame] | 60 | |
| 61 | kmem_cache_free(nilfs_btree_path_cache, path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 64 | /* |
| 65 | * B-tree node operations |
| 66 | */ |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 67 | static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree, |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 68 | __u64 ptr, struct buffer_head **bhp) |
| 69 | { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 70 | struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache; |
Ryusuke Konishi | 45f4910 | 2009-11-13 16:25:19 +0900 | [diff] [blame] | 71 | struct buffer_head *bh; |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 72 | |
Ryusuke Konishi | 45f4910 | 2009-11-13 16:25:19 +0900 | [diff] [blame] | 73 | bh = nilfs_btnode_create_block(btnc, ptr); |
| 74 | if (!bh) |
| 75 | return -ENOMEM; |
| 76 | |
| 77 | set_buffer_nilfs_volatile(bh); |
| 78 | *bhp = bh; |
| 79 | return 0; |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 80 | } |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 81 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 82 | static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 83 | { |
| 84 | return node->bn_flags; |
| 85 | } |
| 86 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 87 | static void |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 88 | nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 89 | { |
| 90 | node->bn_flags = flags; |
| 91 | } |
| 92 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 93 | static int nilfs_btree_node_root(const struct nilfs_btree_node *node) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 94 | { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 95 | return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 98 | static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 99 | { |
| 100 | return node->bn_level; |
| 101 | } |
| 102 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 103 | static void |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 104 | nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 105 | { |
| 106 | node->bn_level = level; |
| 107 | } |
| 108 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 109 | static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 110 | { |
| 111 | return le16_to_cpu(node->bn_nchildren); |
| 112 | } |
| 113 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 114 | static void |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 115 | nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 116 | { |
| 117 | node->bn_nchildren = cpu_to_le16(nchildren); |
| 118 | } |
| 119 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 120 | static int nilfs_btree_node_size(const struct nilfs_bmap *btree) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 121 | { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 122 | return 1 << btree->b_inode->i_blkbits; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 125 | static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 126 | { |
Ryusuke Konishi | 5ad2686e9 | 2010-07-13 23:33:54 +0900 | [diff] [blame] | 127 | return btree->b_nchildren_per_block; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 130 | static __le64 * |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 131 | nilfs_btree_node_dkeys(const struct nilfs_btree_node *node) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 132 | { |
| 133 | return (__le64 *)((char *)(node + 1) + |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 134 | (nilfs_btree_node_root(node) ? |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 135 | 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE)); |
| 136 | } |
| 137 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 138 | static __le64 * |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 139 | nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 140 | { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 141 | return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 144 | static __u64 |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 145 | nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 146 | { |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 147 | return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 150 | static void |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 151 | nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 152 | { |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 153 | *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 156 | static __u64 |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 157 | nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index, |
| 158 | int ncmax) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 159 | { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 160 | return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 163 | static void |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 164 | nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr, |
| 165 | int ncmax) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 166 | { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 167 | *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 170 | static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags, |
| 171 | int level, int nchildren, int ncmax, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 172 | const __u64 *keys, const __u64 *ptrs) |
| 173 | { |
| 174 | __le64 *dkeys; |
| 175 | __le64 *dptrs; |
| 176 | int i; |
| 177 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 178 | nilfs_btree_node_set_flags(node, flags); |
| 179 | nilfs_btree_node_set_level(node, level); |
| 180 | nilfs_btree_node_set_nchildren(node, nchildren); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 181 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 182 | dkeys = nilfs_btree_node_dkeys(node); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 183 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 184 | for (i = 0; i < nchildren; i++) { |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 185 | dkeys[i] = cpu_to_le64(keys[i]); |
| 186 | dptrs[i] = cpu_to_le64(ptrs[i]); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | /* Assume the buffer heads corresponding to left and right are locked. */ |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 191 | static void nilfs_btree_node_move_left(struct nilfs_btree_node *left, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 192 | struct nilfs_btree_node *right, |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 193 | int n, int lncmax, int rncmax) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 194 | { |
| 195 | __le64 *ldkeys, *rdkeys; |
| 196 | __le64 *ldptrs, *rdptrs; |
| 197 | int lnchildren, rnchildren; |
| 198 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 199 | ldkeys = nilfs_btree_node_dkeys(left); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 200 | ldptrs = nilfs_btree_node_dptrs(left, lncmax); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 201 | lnchildren = nilfs_btree_node_get_nchildren(left); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 202 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 203 | rdkeys = nilfs_btree_node_dkeys(right); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 204 | rdptrs = nilfs_btree_node_dptrs(right, rncmax); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 205 | rnchildren = nilfs_btree_node_get_nchildren(right); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 206 | |
| 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)); |
| 211 | |
| 212 | lnchildren += n; |
| 213 | rnchildren -= n; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 214 | nilfs_btree_node_set_nchildren(left, lnchildren); |
| 215 | nilfs_btree_node_set_nchildren(right, rnchildren); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* Assume that the buffer heads corresponding to left and right are locked. */ |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 219 | static void nilfs_btree_node_move_right(struct nilfs_btree_node *left, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 220 | struct nilfs_btree_node *right, |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 221 | int n, int lncmax, int rncmax) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 222 | { |
| 223 | __le64 *ldkeys, *rdkeys; |
| 224 | __le64 *ldptrs, *rdptrs; |
| 225 | int lnchildren, rnchildren; |
| 226 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 227 | ldkeys = nilfs_btree_node_dkeys(left); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 228 | ldptrs = nilfs_btree_node_dptrs(left, lncmax); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 229 | lnchildren = nilfs_btree_node_get_nchildren(left); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 230 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 231 | rdkeys = nilfs_btree_node_dkeys(right); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 232 | rdptrs = nilfs_btree_node_dptrs(right, rncmax); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 233 | rnchildren = nilfs_btree_node_get_nchildren(right); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 234 | |
| 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)); |
| 239 | |
| 240 | lnchildren -= n; |
| 241 | rnchildren += n; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 242 | nilfs_btree_node_set_nchildren(left, lnchildren); |
| 243 | nilfs_btree_node_set_nchildren(right, rnchildren); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* Assume that the buffer head corresponding to node is locked. */ |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 247 | static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index, |
| 248 | __u64 key, __u64 ptr, int ncmax) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 249 | { |
| 250 | __le64 *dkeys; |
| 251 | __le64 *dptrs; |
| 252 | int nchildren; |
| 253 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 254 | dkeys = nilfs_btree_node_dkeys(node); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 255 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 256 | nchildren = nilfs_btree_node_get_nchildren(node); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 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)); |
| 262 | } |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 263 | dkeys[index] = cpu_to_le64(key); |
| 264 | dptrs[index] = cpu_to_le64(ptr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 265 | nchildren++; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 266 | nilfs_btree_node_set_nchildren(node, nchildren); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* Assume that the buffer head corresponding to node is locked. */ |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 270 | static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index, |
| 271 | __u64 *keyp, __u64 *ptrp, int ncmax) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 272 | { |
| 273 | __u64 key; |
| 274 | __u64 ptr; |
| 275 | __le64 *dkeys; |
| 276 | __le64 *dptrs; |
| 277 | int nchildren; |
| 278 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 279 | dkeys = nilfs_btree_node_dkeys(node); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 280 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 281 | key = le64_to_cpu(dkeys[index]); |
| 282 | ptr = le64_to_cpu(dptrs[index]); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 283 | nchildren = nilfs_btree_node_get_nchildren(node); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 284 | if (keyp != NULL) |
| 285 | *keyp = key; |
| 286 | if (ptrp != NULL) |
| 287 | *ptrp = ptr; |
| 288 | |
| 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)); |
| 294 | } |
| 295 | nchildren--; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 296 | nilfs_btree_node_set_nchildren(node, nchildren); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 299 | static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 300 | __u64 key, int *indexp) |
| 301 | { |
| 302 | __u64 nkey; |
| 303 | int index, low, high, s; |
| 304 | |
| 305 | /* binary search */ |
| 306 | low = 0; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 307 | high = nilfs_btree_node_get_nchildren(node) - 1; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 308 | index = 0; |
| 309 | s = 0; |
| 310 | while (low <= high) { |
| 311 | index = (low + high) / 2; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 312 | nkey = nilfs_btree_node_get_key(node, index); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 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; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* adjust index */ |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 326 | if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) { |
| 327 | if (s > 0 && index > 0) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 328 | index--; |
| 329 | } else if (s < 0) |
| 330 | index++; |
| 331 | |
| 332 | out: |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 333 | *indexp = index; |
| 334 | |
| 335 | return s == 0; |
| 336 | } |
| 337 | |
Ryusuke Konishi | 1d5385b | 2010-07-16 23:52:40 +0900 | [diff] [blame] | 338 | /** |
| 339 | * nilfs_btree_node_broken - verify consistency of btree node |
| 340 | * @node: btree node block to be examined |
| 341 | * @size: node size (in bytes) |
| 342 | * @blocknr: block number |
| 343 | * |
| 344 | * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned. |
| 345 | */ |
| 346 | static int nilfs_btree_node_broken(const struct nilfs_btree_node *node, |
| 347 | size_t size, sector_t blocknr) |
| 348 | { |
| 349 | int level, flags, nchildren; |
| 350 | int ret = 0; |
| 351 | |
| 352 | level = nilfs_btree_node_get_level(node); |
| 353 | flags = nilfs_btree_node_get_flags(node); |
| 354 | nchildren = nilfs_btree_node_get_nchildren(node); |
| 355 | |
| 356 | if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN || |
| 357 | level >= NILFS_BTREE_LEVEL_MAX || |
| 358 | (flags & NILFS_BTREE_NODE_ROOT) || |
| 359 | nchildren < 0 || |
| 360 | nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) { |
| 361 | printk(KERN_CRIT "NILFS: bad btree node (blocknr=%llu): " |
| 362 | "level = %d, flags = 0x%x, nchildren = %d\n", |
| 363 | (unsigned long long)blocknr, level, flags, nchildren); |
| 364 | ret = 1; |
| 365 | } |
| 366 | return ret; |
| 367 | } |
| 368 | |
Ryusuke Konishi | 957ed60 | 2015-02-27 15:51:56 -0800 | [diff] [blame] | 369 | /** |
| 370 | * nilfs_btree_root_broken - verify consistency of btree root node |
| 371 | * @node: btree root node to be examined |
| 372 | * @ino: inode number |
| 373 | * |
| 374 | * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned. |
| 375 | */ |
| 376 | static int nilfs_btree_root_broken(const struct nilfs_btree_node *node, |
| 377 | unsigned long ino) |
| 378 | { |
| 379 | int level, flags, nchildren; |
| 380 | int ret = 0; |
| 381 | |
| 382 | level = nilfs_btree_node_get_level(node); |
| 383 | flags = nilfs_btree_node_get_flags(node); |
| 384 | nchildren = nilfs_btree_node_get_nchildren(node); |
| 385 | |
| 386 | if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN || |
Ryusuke Konishi | d8fd150 | 2015-05-05 16:24:00 -0700 | [diff] [blame] | 387 | level >= NILFS_BTREE_LEVEL_MAX || |
Ryusuke Konishi | 957ed60 | 2015-02-27 15:51:56 -0800 | [diff] [blame] | 388 | nchildren < 0 || |
| 389 | nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) { |
| 390 | pr_crit("NILFS: bad btree root (inode number=%lu): level = %d, flags = 0x%x, nchildren = %d\n", |
| 391 | ino, level, flags, nchildren); |
| 392 | ret = 1; |
| 393 | } |
| 394 | return ret; |
| 395 | } |
| 396 | |
Ryusuke Konishi | 1d5385b | 2010-07-16 23:52:40 +0900 | [diff] [blame] | 397 | int nilfs_btree_broken_node_block(struct buffer_head *bh) |
| 398 | { |
Ryusuke Konishi | 4e13e66 | 2010-07-18 10:42:25 +0900 | [diff] [blame] | 399 | int ret; |
| 400 | |
| 401 | if (buffer_nilfs_checked(bh)) |
| 402 | return 0; |
| 403 | |
| 404 | ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data, |
Ryusuke Konishi | 1d5385b | 2010-07-16 23:52:40 +0900 | [diff] [blame] | 405 | bh->b_size, bh->b_blocknr); |
Ryusuke Konishi | 4e13e66 | 2010-07-18 10:42:25 +0900 | [diff] [blame] | 406 | if (likely(!ret)) |
| 407 | set_buffer_nilfs_checked(bh); |
| 408 | return ret; |
Ryusuke Konishi | 1d5385b | 2010-07-16 23:52:40 +0900 | [diff] [blame] | 409 | } |
| 410 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 411 | static struct nilfs_btree_node * |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 412 | nilfs_btree_get_root(const struct nilfs_bmap *btree) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 413 | { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 414 | return (struct nilfs_btree_node *)btree->b_u.u_data; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 417 | static struct nilfs_btree_node * |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 418 | nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 419 | { |
| 420 | return (struct nilfs_btree_node *)path[level].bp_bh->b_data; |
| 421 | } |
| 422 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 423 | static struct nilfs_btree_node * |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 424 | nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 425 | { |
| 426 | return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data; |
| 427 | } |
| 428 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 429 | static int nilfs_btree_height(const struct nilfs_bmap *btree) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 430 | { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 431 | return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 434 | static struct nilfs_btree_node * |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 435 | nilfs_btree_get_node(const struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 436 | const struct nilfs_btree_path *path, |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 437 | int level, int *ncmaxp) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 438 | { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 439 | struct nilfs_btree_node *node; |
| 440 | |
| 441 | if (level == nilfs_btree_height(btree) - 1) { |
| 442 | node = nilfs_btree_get_root(btree); |
| 443 | *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX; |
| 444 | } else { |
| 445 | node = nilfs_btree_get_nonroot_node(path, level); |
| 446 | *ncmaxp = nilfs_btree_nchildren_per_block(btree); |
| 447 | } |
| 448 | return node; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Ryusuke Konishi | 7c397a8 | 2010-07-13 23:33:55 +0900 | [diff] [blame] | 451 | static int |
Ryusuke Konishi | 9b945d5 | 2009-10-10 22:58:10 +0900 | [diff] [blame] | 452 | nilfs_btree_bad_node(struct nilfs_btree_node *node, int level) |
| 453 | { |
| 454 | if (unlikely(nilfs_btree_node_get_level(node) != level)) { |
| 455 | dump_stack(); |
| 456 | printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n", |
| 457 | nilfs_btree_node_get_level(node), level); |
| 458 | return 1; |
| 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | |
Ryusuke Konishi | 464ece8 | 2010-07-18 10:42:24 +0900 | [diff] [blame] | 463 | struct nilfs_btree_readahead_info { |
| 464 | struct nilfs_btree_node *node; /* parent node */ |
| 465 | int max_ra_blocks; /* max nof blocks to read ahead */ |
| 466 | int index; /* current index on the parent node */ |
| 467 | int ncmax; /* nof children in the parent node */ |
| 468 | }; |
| 469 | |
| 470 | static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr, |
| 471 | struct buffer_head **bhp, |
| 472 | const struct nilfs_btree_readahead_info *ra) |
| 473 | { |
| 474 | struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache; |
| 475 | struct buffer_head *bh, *ra_bh; |
| 476 | sector_t submit_ptr = 0; |
| 477 | int ret; |
| 478 | |
Mike Christie | 2a222ca | 2016-06-05 14:31:43 -0500 | [diff] [blame^] | 479 | ret = nilfs_btnode_submit_block(btnc, ptr, 0, REQ_OP_READ, 0, &bh, |
| 480 | &submit_ptr); |
Ryusuke Konishi | 464ece8 | 2010-07-18 10:42:24 +0900 | [diff] [blame] | 481 | if (ret) { |
| 482 | if (ret != -EEXIST) |
| 483 | return ret; |
| 484 | goto out_check; |
| 485 | } |
| 486 | |
| 487 | if (ra) { |
| 488 | int i, n; |
| 489 | __u64 ptr2; |
| 490 | |
| 491 | /* read ahead sibling nodes */ |
| 492 | for (n = ra->max_ra_blocks, i = ra->index + 1; |
| 493 | n > 0 && i < ra->ncmax; n--, i++) { |
| 494 | ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax); |
| 495 | |
Mike Christie | 2a222ca | 2016-06-05 14:31:43 -0500 | [diff] [blame^] | 496 | ret = nilfs_btnode_submit_block(btnc, ptr2, 0, |
| 497 | REQ_OP_READ, REQ_RAHEAD, |
Ryusuke Konishi | 464ece8 | 2010-07-18 10:42:24 +0900 | [diff] [blame] | 498 | &ra_bh, &submit_ptr); |
| 499 | if (likely(!ret || ret == -EEXIST)) |
| 500 | brelse(ra_bh); |
| 501 | else if (ret != -EBUSY) |
| 502 | break; |
| 503 | if (!buffer_locked(bh)) |
| 504 | goto out_no_wait; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | wait_on_buffer(bh); |
| 509 | |
| 510 | out_no_wait: |
| 511 | if (!buffer_uptodate(bh)) { |
| 512 | brelse(bh); |
| 513 | return -EIO; |
| 514 | } |
| 515 | |
| 516 | out_check: |
| 517 | if (nilfs_btree_broken_node_block(bh)) { |
| 518 | clear_buffer_uptodate(bh); |
| 519 | brelse(bh); |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
| 523 | *bhp = bh; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr, |
| 528 | struct buffer_head **bhp) |
| 529 | { |
| 530 | return __nilfs_btree_get_block(btree, ptr, bhp, NULL); |
| 531 | } |
| 532 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 533 | static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 534 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 535 | __u64 key, __u64 *ptrp, int minlevel, |
| 536 | int readahead) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 537 | { |
| 538 | struct nilfs_btree_node *node; |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 539 | struct nilfs_btree_readahead_info p, *ra; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 540 | __u64 ptr; |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 541 | int level, index, found, ncmax, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 542 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 543 | node = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 544 | level = nilfs_btree_node_get_level(node); |
| 545 | if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 546 | return -ENOENT; |
| 547 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 548 | found = nilfs_btree_node_lookup(node, key, &index); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 549 | ptr = nilfs_btree_node_get_ptr(node, index, |
| 550 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 551 | path[level].bp_bh = NULL; |
| 552 | path[level].bp_index = index; |
| 553 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 554 | ncmax = nilfs_btree_nchildren_per_block(btree); |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 555 | |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 556 | while (--level >= minlevel) { |
| 557 | ra = NULL; |
| 558 | if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) { |
| 559 | p.node = nilfs_btree_get_node(btree, path, level + 1, |
| 560 | &p.ncmax); |
| 561 | p.index = index; |
| 562 | p.max_ra_blocks = 7; |
| 563 | ra = &p; |
| 564 | } |
| 565 | ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh, |
| 566 | ra); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 567 | if (ret < 0) |
| 568 | return ret; |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 569 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 570 | node = nilfs_btree_get_nonroot_node(path, level); |
Ryusuke Konishi | 9b945d5 | 2009-10-10 22:58:10 +0900 | [diff] [blame] | 571 | if (nilfs_btree_bad_node(node, level)) |
| 572 | return -EINVAL; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 573 | if (!found) |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 574 | found = nilfs_btree_node_lookup(node, key, &index); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 575 | else |
| 576 | index = 0; |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 577 | if (index < ncmax) { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 578 | ptr = nilfs_btree_node_get_ptr(node, index, ncmax); |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 579 | } else { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 580 | WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 581 | /* insert */ |
| 582 | ptr = NILFS_BMAP_INVALID_PTR; |
| 583 | } |
| 584 | path[level].bp_index = index; |
| 585 | } |
| 586 | if (!found) |
| 587 | return -ENOENT; |
| 588 | |
| 589 | if (ptrp != NULL) |
| 590 | *ptrp = ptr; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 595 | static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 596 | struct nilfs_btree_path *path, |
| 597 | __u64 *keyp, __u64 *ptrp) |
| 598 | { |
| 599 | struct nilfs_btree_node *node; |
| 600 | __u64 ptr; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 601 | int index, level, ncmax, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 602 | |
| 603 | node = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 604 | index = nilfs_btree_node_get_nchildren(node) - 1; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 605 | if (index < 0) |
| 606 | return -ENOENT; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 607 | level = nilfs_btree_node_get_level(node); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 608 | ptr = nilfs_btree_node_get_ptr(node, index, |
| 609 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 610 | path[level].bp_bh = NULL; |
| 611 | path[level].bp_index = index; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 612 | ncmax = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 613 | |
| 614 | for (level--; level > 0; level--) { |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 615 | ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 616 | if (ret < 0) |
| 617 | return ret; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 618 | node = nilfs_btree_get_nonroot_node(path, level); |
Ryusuke Konishi | 9b945d5 | 2009-10-10 22:58:10 +0900 | [diff] [blame] | 619 | if (nilfs_btree_bad_node(node, level)) |
| 620 | return -EINVAL; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 621 | index = nilfs_btree_node_get_nchildren(node) - 1; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 622 | ptr = nilfs_btree_node_get_ptr(node, index, ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 623 | path[level].bp_index = index; |
| 624 | } |
| 625 | |
| 626 | if (keyp != NULL) |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 627 | *keyp = nilfs_btree_node_get_key(node, index); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 628 | if (ptrp != NULL) |
| 629 | *ptrp = ptr; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 634 | /** |
| 635 | * nilfs_btree_get_next_key - get next valid key from btree path array |
| 636 | * @btree: bmap struct of btree |
| 637 | * @path: array of nilfs_btree_path struct |
| 638 | * @minlevel: start level |
| 639 | * @nextkey: place to store the next valid key |
| 640 | * |
| 641 | * Return Value: If a next key was found, 0 is returned. Otherwise, |
| 642 | * -ENOENT is returned. |
| 643 | */ |
| 644 | static int nilfs_btree_get_next_key(const struct nilfs_bmap *btree, |
| 645 | const struct nilfs_btree_path *path, |
| 646 | int minlevel, __u64 *nextkey) |
| 647 | { |
| 648 | struct nilfs_btree_node *node; |
| 649 | int maxlevel = nilfs_btree_height(btree) - 1; |
| 650 | int index, next_adj, level; |
| 651 | |
| 652 | /* Next index is already set to bp_index for leaf nodes. */ |
| 653 | next_adj = 0; |
| 654 | for (level = minlevel; level <= maxlevel; level++) { |
| 655 | if (level == maxlevel) |
| 656 | node = nilfs_btree_get_root(btree); |
| 657 | else |
| 658 | node = nilfs_btree_get_nonroot_node(path, level); |
| 659 | |
| 660 | index = path[level].bp_index + next_adj; |
| 661 | if (index < nilfs_btree_node_get_nchildren(node)) { |
| 662 | /* Next key is in this node */ |
| 663 | *nextkey = nilfs_btree_node_get_key(node, index); |
| 664 | return 0; |
| 665 | } |
| 666 | /* For non-leaf nodes, next index is stored at bp_index + 1. */ |
| 667 | next_adj = 1; |
| 668 | } |
| 669 | return -ENOENT; |
| 670 | } |
| 671 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 672 | static int nilfs_btree_lookup(const struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 673 | __u64 key, int level, __u64 *ptrp) |
| 674 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 675 | struct nilfs_btree_path *path; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 676 | int ret; |
| 677 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 678 | path = nilfs_btree_alloc_path(); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 679 | if (path == NULL) |
| 680 | return -ENOMEM; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 681 | |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 682 | ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 683 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 684 | nilfs_btree_free_path(path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 685 | |
| 686 | return ret; |
| 687 | } |
| 688 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 689 | static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree, |
Ryusuke Konishi | 0c6c44c | 2016-05-23 16:23:39 -0700 | [diff] [blame] | 690 | __u64 key, __u64 *ptrp, |
| 691 | unsigned int maxblocks) |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 692 | { |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 693 | struct nilfs_btree_path *path; |
| 694 | struct nilfs_btree_node *node; |
| 695 | struct inode *dat = NULL; |
| 696 | __u64 ptr, ptr2; |
| 697 | sector_t blocknr; |
| 698 | int level = NILFS_BTREE_LEVEL_NODE_MIN; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 699 | int ret, cnt, index, maxlevel, ncmax; |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 700 | struct nilfs_btree_readahead_info p; |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 701 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 702 | path = nilfs_btree_alloc_path(); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 703 | if (path == NULL) |
| 704 | return -ENOMEM; |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 705 | |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 706 | ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 707 | if (ret < 0) |
| 708 | goto out; |
| 709 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 710 | if (NILFS_BMAP_USE_VBN(btree)) { |
| 711 | dat = nilfs_bmap_get_dat(btree); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 712 | ret = nilfs_dat_translate(dat, ptr, &blocknr); |
| 713 | if (ret < 0) |
| 714 | goto out; |
| 715 | ptr = blocknr; |
| 716 | } |
| 717 | cnt = 1; |
| 718 | if (cnt == maxblocks) |
| 719 | goto end; |
| 720 | |
| 721 | maxlevel = nilfs_btree_height(btree) - 1; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 722 | node = nilfs_btree_get_node(btree, path, level, &ncmax); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 723 | index = path[level].bp_index + 1; |
| 724 | for (;;) { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 725 | while (index < nilfs_btree_node_get_nchildren(node)) { |
| 726 | if (nilfs_btree_node_get_key(node, index) != |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 727 | key + cnt) |
| 728 | goto end; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 729 | ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 730 | if (dat) { |
| 731 | ret = nilfs_dat_translate(dat, ptr2, &blocknr); |
| 732 | if (ret < 0) |
| 733 | goto out; |
| 734 | ptr2 = blocknr; |
| 735 | } |
| 736 | if (ptr2 != ptr + cnt || ++cnt == maxblocks) |
| 737 | goto end; |
| 738 | index++; |
| 739 | continue; |
| 740 | } |
| 741 | if (level == maxlevel) |
| 742 | break; |
| 743 | |
| 744 | /* look-up right sibling node */ |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 745 | p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax); |
| 746 | p.index = path[level + 1].bp_index + 1; |
| 747 | p.max_ra_blocks = 7; |
| 748 | if (p.index >= nilfs_btree_node_get_nchildren(p.node) || |
| 749 | nilfs_btree_node_get_key(p.node, p.index) != key + cnt) |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 750 | break; |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 751 | ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax); |
| 752 | path[level + 1].bp_index = p.index; |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 753 | |
| 754 | brelse(path[level].bp_bh); |
| 755 | path[level].bp_bh = NULL; |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 756 | |
| 757 | ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh, |
| 758 | &p); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 759 | if (ret < 0) |
| 760 | goto out; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 761 | node = nilfs_btree_get_nonroot_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 762 | ncmax = nilfs_btree_nchildren_per_block(btree); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 763 | index = 0; |
| 764 | path[level].bp_index = index; |
| 765 | } |
| 766 | end: |
| 767 | *ptrp = ptr; |
| 768 | ret = cnt; |
| 769 | out: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 770 | nilfs_btree_free_path(path); |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 771 | return ret; |
| 772 | } |
| 773 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 774 | static void nilfs_btree_promote_key(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 775 | struct nilfs_btree_path *path, |
| 776 | int level, __u64 key) |
| 777 | { |
| 778 | if (level < nilfs_btree_height(btree) - 1) { |
| 779 | do { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 780 | nilfs_btree_node_set_key( |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 781 | nilfs_btree_get_nonroot_node(path, level), |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 782 | path[level].bp_index, key); |
| 783 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 784 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 785 | } while ((path[level].bp_index == 0) && |
| 786 | (++level < nilfs_btree_height(btree) - 1)); |
| 787 | } |
| 788 | |
| 789 | /* root */ |
| 790 | if (level == nilfs_btree_height(btree) - 1) { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 791 | nilfs_btree_node_set_key(nilfs_btree_get_root(btree), |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 792 | path[level].bp_index, key); |
| 793 | } |
| 794 | } |
| 795 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 796 | static void nilfs_btree_do_insert(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 797 | struct nilfs_btree_path *path, |
| 798 | int level, __u64 *keyp, __u64 *ptrp) |
| 799 | { |
| 800 | struct nilfs_btree_node *node; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 801 | int ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 802 | |
| 803 | if (level < nilfs_btree_height(btree) - 1) { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 804 | node = nilfs_btree_get_nonroot_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 805 | ncblk = nilfs_btree_nchildren_per_block(btree); |
| 806 | nilfs_btree_node_insert(node, path[level].bp_index, |
| 807 | *keyp, *ptrp, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 808 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 809 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 810 | |
| 811 | if (path[level].bp_index == 0) |
| 812 | nilfs_btree_promote_key(btree, path, level + 1, |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 813 | nilfs_btree_node_get_key(node, |
| 814 | 0)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 815 | } else { |
| 816 | node = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 817 | nilfs_btree_node_insert(node, path[level].bp_index, |
| 818 | *keyp, *ptrp, |
| 819 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 823 | static void nilfs_btree_carry_left(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 824 | struct nilfs_btree_path *path, |
| 825 | int level, __u64 *keyp, __u64 *ptrp) |
| 826 | { |
| 827 | struct nilfs_btree_node *node, *left; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 828 | int nchildren, lnchildren, n, move, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 829 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 830 | node = nilfs_btree_get_nonroot_node(path, level); |
| 831 | left = nilfs_btree_get_sib_node(path, level); |
| 832 | nchildren = nilfs_btree_node_get_nchildren(node); |
| 833 | lnchildren = nilfs_btree_node_get_nchildren(left); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 834 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 835 | move = 0; |
| 836 | |
| 837 | n = (nchildren + lnchildren + 1) / 2 - lnchildren; |
| 838 | if (n > path[level].bp_index) { |
| 839 | /* move insert point */ |
| 840 | n--; |
| 841 | move = 1; |
| 842 | } |
| 843 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 844 | nilfs_btree_node_move_left(left, node, n, ncblk, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 845 | |
| 846 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 847 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 848 | if (!buffer_dirty(path[level].bp_sib_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 849 | mark_buffer_dirty(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 850 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 851 | nilfs_btree_promote_key(btree, path, level + 1, |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 852 | nilfs_btree_node_get_key(node, 0)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 853 | |
| 854 | if (move) { |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 855 | brelse(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 856 | path[level].bp_bh = path[level].bp_sib_bh; |
| 857 | path[level].bp_sib_bh = NULL; |
| 858 | path[level].bp_index += lnchildren; |
| 859 | path[level + 1].bp_index--; |
| 860 | } else { |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 861 | brelse(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 862 | path[level].bp_sib_bh = NULL; |
| 863 | path[level].bp_index -= n; |
| 864 | } |
| 865 | |
| 866 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); |
| 867 | } |
| 868 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 869 | static void nilfs_btree_carry_right(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 870 | struct nilfs_btree_path *path, |
| 871 | int level, __u64 *keyp, __u64 *ptrp) |
| 872 | { |
| 873 | struct nilfs_btree_node *node, *right; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 874 | int nchildren, rnchildren, n, move, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 875 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 876 | node = nilfs_btree_get_nonroot_node(path, level); |
| 877 | right = nilfs_btree_get_sib_node(path, level); |
| 878 | nchildren = nilfs_btree_node_get_nchildren(node); |
| 879 | rnchildren = nilfs_btree_node_get_nchildren(right); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 880 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 881 | move = 0; |
| 882 | |
| 883 | n = (nchildren + rnchildren + 1) / 2 - rnchildren; |
| 884 | if (n > nchildren - path[level].bp_index) { |
| 885 | /* move insert point */ |
| 886 | n--; |
| 887 | move = 1; |
| 888 | } |
| 889 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 890 | nilfs_btree_node_move_right(node, right, n, ncblk, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 891 | |
| 892 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 893 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 894 | if (!buffer_dirty(path[level].bp_sib_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 895 | mark_buffer_dirty(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 896 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 897 | path[level + 1].bp_index++; |
| 898 | nilfs_btree_promote_key(btree, path, level + 1, |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 899 | nilfs_btree_node_get_key(right, 0)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 900 | path[level + 1].bp_index--; |
| 901 | |
| 902 | if (move) { |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 903 | brelse(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 904 | path[level].bp_bh = path[level].bp_sib_bh; |
| 905 | path[level].bp_sib_bh = NULL; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 906 | path[level].bp_index -= nilfs_btree_node_get_nchildren(node); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 907 | path[level + 1].bp_index++; |
| 908 | } else { |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 909 | brelse(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 910 | path[level].bp_sib_bh = NULL; |
| 911 | } |
| 912 | |
| 913 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); |
| 914 | } |
| 915 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 916 | static void nilfs_btree_split(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 917 | struct nilfs_btree_path *path, |
| 918 | int level, __u64 *keyp, __u64 *ptrp) |
| 919 | { |
| 920 | struct nilfs_btree_node *node, *right; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 921 | int nchildren, n, move, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 922 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 923 | node = nilfs_btree_get_nonroot_node(path, level); |
| 924 | right = nilfs_btree_get_sib_node(path, level); |
| 925 | nchildren = nilfs_btree_node_get_nchildren(node); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 926 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 927 | move = 0; |
| 928 | |
| 929 | n = (nchildren + 1) / 2; |
| 930 | if (n > nchildren - path[level].bp_index) { |
| 931 | n--; |
| 932 | move = 1; |
| 933 | } |
| 934 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 935 | nilfs_btree_node_move_right(node, right, n, ncblk, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 936 | |
| 937 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 938 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 939 | if (!buffer_dirty(path[level].bp_sib_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 940 | mark_buffer_dirty(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 941 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 942 | if (move) { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 943 | path[level].bp_index -= nilfs_btree_node_get_nchildren(node); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 944 | nilfs_btree_node_insert(right, path[level].bp_index, |
| 945 | *keyp, *ptrp, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 946 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 947 | *keyp = nilfs_btree_node_get_key(right, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 948 | *ptrp = path[level].bp_newreq.bpr_ptr; |
| 949 | |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 950 | brelse(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 951 | path[level].bp_bh = path[level].bp_sib_bh; |
| 952 | path[level].bp_sib_bh = NULL; |
| 953 | } else { |
| 954 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); |
| 955 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 956 | *keyp = nilfs_btree_node_get_key(right, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 957 | *ptrp = path[level].bp_newreq.bpr_ptr; |
| 958 | |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 959 | brelse(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 960 | path[level].bp_sib_bh = NULL; |
| 961 | } |
| 962 | |
| 963 | path[level + 1].bp_index++; |
| 964 | } |
| 965 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 966 | static void nilfs_btree_grow(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 967 | struct nilfs_btree_path *path, |
| 968 | int level, __u64 *keyp, __u64 *ptrp) |
| 969 | { |
| 970 | struct nilfs_btree_node *root, *child; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 971 | int n, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 972 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 973 | root = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 974 | child = nilfs_btree_get_sib_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 975 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 976 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 977 | n = nilfs_btree_node_get_nchildren(root); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 978 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 979 | nilfs_btree_node_move_right(root, child, n, |
| 980 | NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 981 | nilfs_btree_node_set_level(root, level + 1); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 982 | |
| 983 | if (!buffer_dirty(path[level].bp_sib_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 984 | mark_buffer_dirty(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 985 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 986 | path[level].bp_bh = path[level].bp_sib_bh; |
| 987 | path[level].bp_sib_bh = NULL; |
| 988 | |
| 989 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); |
| 990 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 991 | *keyp = nilfs_btree_node_get_key(child, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 992 | *ptrp = path[level].bp_newreq.bpr_ptr; |
| 993 | } |
| 994 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 995 | static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 996 | const struct nilfs_btree_path *path) |
| 997 | { |
| 998 | struct nilfs_btree_node *node; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 999 | int level, ncmax; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1000 | |
| 1001 | if (path == NULL) |
| 1002 | return NILFS_BMAP_INVALID_PTR; |
| 1003 | |
| 1004 | /* left sibling */ |
| 1005 | level = NILFS_BTREE_LEVEL_NODE_MIN; |
| 1006 | if (path[level].bp_index > 0) { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1007 | node = nilfs_btree_get_node(btree, path, level, &ncmax); |
| 1008 | return nilfs_btree_node_get_ptr(node, |
| 1009 | path[level].bp_index - 1, |
| 1010 | ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /* parent */ |
| 1014 | level = NILFS_BTREE_LEVEL_NODE_MIN + 1; |
| 1015 | if (level <= nilfs_btree_height(btree) - 1) { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1016 | node = nilfs_btree_get_node(btree, path, level, &ncmax); |
| 1017 | return nilfs_btree_node_get_ptr(node, path[level].bp_index, |
| 1018 | ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | return NILFS_BMAP_INVALID_PTR; |
| 1022 | } |
| 1023 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1024 | static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1025 | const struct nilfs_btree_path *path, |
| 1026 | __u64 key) |
| 1027 | { |
| 1028 | __u64 ptr; |
| 1029 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1030 | ptr = nilfs_bmap_find_target_seq(btree, key); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1031 | if (ptr != NILFS_BMAP_INVALID_PTR) |
| 1032 | /* sequential access */ |
| 1033 | return ptr; |
Ryusuke Konishi | 7f00184 | 2016-05-23 16:23:42 -0700 | [diff] [blame] | 1034 | |
| 1035 | ptr = nilfs_btree_find_near(btree, path); |
| 1036 | if (ptr != NILFS_BMAP_INVALID_PTR) |
| 1037 | /* near */ |
| 1038 | return ptr; |
| 1039 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1040 | /* block group */ |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1041 | return nilfs_bmap_find_target_in_group(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1044 | static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1045 | struct nilfs_btree_path *path, |
| 1046 | int *levelp, __u64 key, __u64 ptr, |
| 1047 | struct nilfs_bmap_stats *stats) |
| 1048 | { |
| 1049 | struct buffer_head *bh; |
| 1050 | struct nilfs_btree_node *node, *parent, *sib; |
| 1051 | __u64 sibptr; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1052 | int pindex, level, ncmax, ncblk, ret; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1053 | struct inode *dat = NULL; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1054 | |
| 1055 | stats->bs_nblocks = 0; |
| 1056 | level = NILFS_BTREE_LEVEL_DATA; |
| 1057 | |
| 1058 | /* allocate a new ptr for data block */ |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1059 | if (NILFS_BMAP_USE_VBN(btree)) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1060 | path[level].bp_newreq.bpr_ptr = |
Ryusuke Konishi | 7cde31d | 2009-05-24 18:07:59 +0900 | [diff] [blame] | 1061 | nilfs_btree_find_target_v(btree, path, key); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1062 | dat = nilfs_bmap_get_dat(btree); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1063 | } |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1064 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1065 | ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1066 | if (ret < 0) |
| 1067 | goto err_out_data; |
| 1068 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1069 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 1070 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1071 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; |
| 1072 | level < nilfs_btree_height(btree) - 1; |
| 1073 | level++) { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1074 | node = nilfs_btree_get_nonroot_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1075 | if (nilfs_btree_node_get_nchildren(node) < ncblk) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1076 | path[level].bp_op = nilfs_btree_do_insert; |
| 1077 | stats->bs_nblocks++; |
| 1078 | goto out; |
| 1079 | } |
| 1080 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1081 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1082 | pindex = path[level + 1].bp_index; |
| 1083 | |
| 1084 | /* left sibling */ |
| 1085 | if (pindex > 0) { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1086 | sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1, |
| 1087 | ncmax); |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1088 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1089 | if (ret < 0) |
| 1090 | goto err_out_child_node; |
| 1091 | sib = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1092 | if (nilfs_btree_node_get_nchildren(sib) < ncblk) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1093 | path[level].bp_sib_bh = bh; |
| 1094 | path[level].bp_op = nilfs_btree_carry_left; |
| 1095 | stats->bs_nblocks++; |
| 1096 | goto out; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1097 | } else { |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1098 | brelse(bh); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1099 | } |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* right sibling */ |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1103 | if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) { |
| 1104 | sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1, |
| 1105 | ncmax); |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1106 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1107 | if (ret < 0) |
| 1108 | goto err_out_child_node; |
| 1109 | sib = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1110 | if (nilfs_btree_node_get_nchildren(sib) < ncblk) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1111 | path[level].bp_sib_bh = bh; |
| 1112 | path[level].bp_op = nilfs_btree_carry_right; |
| 1113 | stats->bs_nblocks++; |
| 1114 | goto out; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1115 | } else { |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1116 | brelse(bh); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1117 | } |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /* split */ |
| 1121 | path[level].bp_newreq.bpr_ptr = |
| 1122 | path[level - 1].bp_newreq.bpr_ptr + 1; |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1123 | ret = nilfs_bmap_prepare_alloc_ptr(btree, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1124 | &path[level].bp_newreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1125 | if (ret < 0) |
| 1126 | goto err_out_child_node; |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1127 | ret = nilfs_btree_get_new_block(btree, |
| 1128 | path[level].bp_newreq.bpr_ptr, |
| 1129 | &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1130 | if (ret < 0) |
| 1131 | goto err_out_curr_node; |
| 1132 | |
| 1133 | stats->bs_nblocks++; |
| 1134 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1135 | sib = (struct nilfs_btree_node *)bh->b_data; |
| 1136 | nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1137 | path[level].bp_sib_bh = bh; |
| 1138 | path[level].bp_op = nilfs_btree_split; |
| 1139 | } |
| 1140 | |
| 1141 | /* root */ |
| 1142 | node = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1143 | if (nilfs_btree_node_get_nchildren(node) < |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 1144 | NILFS_BTREE_ROOT_NCHILDREN_MAX) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1145 | path[level].bp_op = nilfs_btree_do_insert; |
| 1146 | stats->bs_nblocks++; |
| 1147 | goto out; |
| 1148 | } |
| 1149 | |
| 1150 | /* grow */ |
| 1151 | path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1; |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1152 | ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1153 | if (ret < 0) |
| 1154 | goto err_out_child_node; |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1155 | ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr, |
| 1156 | &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1157 | if (ret < 0) |
| 1158 | goto err_out_curr_node; |
| 1159 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1160 | nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data, |
| 1161 | 0, level, 0, ncblk, NULL, NULL); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1162 | path[level].bp_sib_bh = bh; |
| 1163 | path[level].bp_op = nilfs_btree_grow; |
| 1164 | |
| 1165 | level++; |
| 1166 | path[level].bp_op = nilfs_btree_do_insert; |
| 1167 | |
| 1168 | /* a newly-created node block and a data block are added */ |
| 1169 | stats->bs_nblocks += 2; |
| 1170 | |
| 1171 | /* success */ |
| 1172 | out: |
| 1173 | *levelp = level; |
| 1174 | return ret; |
| 1175 | |
| 1176 | /* error */ |
| 1177 | err_out_curr_node: |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1178 | nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1179 | err_out_child_node: |
| 1180 | for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) { |
Ryusuke Konishi | 9f09890 | 2009-05-22 00:38:56 +0900 | [diff] [blame] | 1181 | nilfs_btnode_delete(path[level].bp_sib_bh); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1182 | nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1183 | |
| 1184 | } |
| 1185 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1186 | nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1187 | err_out_data: |
| 1188 | *levelp = level; |
| 1189 | stats->bs_nblocks = 0; |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1193 | static void nilfs_btree_commit_insert(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1194 | struct nilfs_btree_path *path, |
| 1195 | int maxlevel, __u64 key, __u64 ptr) |
| 1196 | { |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1197 | struct inode *dat = NULL; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1198 | int level; |
| 1199 | |
| 1200 | set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr)); |
| 1201 | ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr; |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1202 | if (NILFS_BMAP_USE_VBN(btree)) { |
Ryusuke Konishi | dc935be | 2010-07-10 22:21:54 +0900 | [diff] [blame] | 1203 | nilfs_bmap_set_target_v(btree, key, ptr); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1204 | dat = nilfs_bmap_get_dat(btree); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1205 | } |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1206 | |
| 1207 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1208 | nilfs_bmap_commit_alloc_ptr(btree, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1209 | &path[level - 1].bp_newreq, dat); |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 1210 | path[level].bp_op(btree, path, level, &key, &ptr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1213 | if (!nilfs_bmap_dirty(btree)) |
| 1214 | nilfs_bmap_set_dirty(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1217 | static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1218 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1219 | struct nilfs_btree_path *path; |
| 1220 | struct nilfs_bmap_stats stats; |
| 1221 | int level, ret; |
| 1222 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1223 | path = nilfs_btree_alloc_path(); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1224 | if (path == NULL) |
| 1225 | return -ENOMEM; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1226 | |
| 1227 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 1228 | NILFS_BTREE_LEVEL_NODE_MIN, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1229 | if (ret != -ENOENT) { |
| 1230 | if (ret == 0) |
| 1231 | ret = -EEXIST; |
| 1232 | goto out; |
| 1233 | } |
| 1234 | |
| 1235 | ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats); |
| 1236 | if (ret < 0) |
| 1237 | goto out; |
| 1238 | nilfs_btree_commit_insert(btree, path, level, key, ptr); |
Ryusuke Konishi | be66737 | 2011-03-05 00:19:32 +0900 | [diff] [blame] | 1239 | nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1240 | |
| 1241 | out: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1242 | nilfs_btree_free_path(path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1243 | return ret; |
| 1244 | } |
| 1245 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1246 | static void nilfs_btree_do_delete(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1247 | struct nilfs_btree_path *path, |
| 1248 | int level, __u64 *keyp, __u64 *ptrp) |
| 1249 | { |
| 1250 | struct nilfs_btree_node *node; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1251 | int ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1252 | |
| 1253 | if (level < nilfs_btree_height(btree) - 1) { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1254 | node = nilfs_btree_get_nonroot_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1255 | ncblk = nilfs_btree_nchildren_per_block(btree); |
| 1256 | nilfs_btree_node_delete(node, path[level].bp_index, |
| 1257 | keyp, ptrp, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1258 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1259 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1260 | if (path[level].bp_index == 0) |
| 1261 | nilfs_btree_promote_key(btree, path, level + 1, |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1262 | nilfs_btree_node_get_key(node, 0)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1263 | } else { |
| 1264 | node = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1265 | nilfs_btree_node_delete(node, path[level].bp_index, |
| 1266 | keyp, ptrp, |
| 1267 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1271 | static void nilfs_btree_borrow_left(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1272 | struct nilfs_btree_path *path, |
| 1273 | int level, __u64 *keyp, __u64 *ptrp) |
| 1274 | { |
| 1275 | struct nilfs_btree_node *node, *left; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1276 | int nchildren, lnchildren, n, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1277 | |
| 1278 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); |
| 1279 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1280 | node = nilfs_btree_get_nonroot_node(path, level); |
| 1281 | left = nilfs_btree_get_sib_node(path, level); |
| 1282 | nchildren = nilfs_btree_node_get_nchildren(node); |
| 1283 | lnchildren = nilfs_btree_node_get_nchildren(left); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1284 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1285 | |
| 1286 | n = (nchildren + lnchildren) / 2 - nchildren; |
| 1287 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1288 | nilfs_btree_node_move_right(left, node, n, ncblk, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1289 | |
| 1290 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1291 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1292 | if (!buffer_dirty(path[level].bp_sib_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1293 | mark_buffer_dirty(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1294 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1295 | nilfs_btree_promote_key(btree, path, level + 1, |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1296 | nilfs_btree_node_get_key(node, 0)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1297 | |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1298 | brelse(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1299 | path[level].bp_sib_bh = NULL; |
| 1300 | path[level].bp_index += n; |
| 1301 | } |
| 1302 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1303 | static void nilfs_btree_borrow_right(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1304 | struct nilfs_btree_path *path, |
| 1305 | int level, __u64 *keyp, __u64 *ptrp) |
| 1306 | { |
| 1307 | struct nilfs_btree_node *node, *right; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1308 | int nchildren, rnchildren, n, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1309 | |
| 1310 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); |
| 1311 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1312 | node = nilfs_btree_get_nonroot_node(path, level); |
| 1313 | right = nilfs_btree_get_sib_node(path, level); |
| 1314 | nchildren = nilfs_btree_node_get_nchildren(node); |
| 1315 | rnchildren = nilfs_btree_node_get_nchildren(right); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1316 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1317 | |
| 1318 | n = (nchildren + rnchildren) / 2 - nchildren; |
| 1319 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1320 | nilfs_btree_node_move_left(node, right, n, ncblk, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1321 | |
| 1322 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1323 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1324 | if (!buffer_dirty(path[level].bp_sib_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1325 | mark_buffer_dirty(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1326 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1327 | path[level + 1].bp_index++; |
| 1328 | nilfs_btree_promote_key(btree, path, level + 1, |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1329 | nilfs_btree_node_get_key(right, 0)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1330 | path[level + 1].bp_index--; |
| 1331 | |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1332 | brelse(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1333 | path[level].bp_sib_bh = NULL; |
| 1334 | } |
| 1335 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1336 | static void nilfs_btree_concat_left(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1337 | struct nilfs_btree_path *path, |
| 1338 | int level, __u64 *keyp, __u64 *ptrp) |
| 1339 | { |
| 1340 | struct nilfs_btree_node *node, *left; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1341 | int n, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1342 | |
| 1343 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); |
| 1344 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1345 | node = nilfs_btree_get_nonroot_node(path, level); |
| 1346 | left = nilfs_btree_get_sib_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1347 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1348 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1349 | n = nilfs_btree_node_get_nchildren(node); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1350 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1351 | nilfs_btree_node_move_left(left, node, n, ncblk, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1352 | |
| 1353 | if (!buffer_dirty(path[level].bp_sib_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1354 | mark_buffer_dirty(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1355 | |
Ryusuke Konishi | 9f09890 | 2009-05-22 00:38:56 +0900 | [diff] [blame] | 1356 | nilfs_btnode_delete(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1357 | path[level].bp_bh = path[level].bp_sib_bh; |
| 1358 | path[level].bp_sib_bh = NULL; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1359 | path[level].bp_index += nilfs_btree_node_get_nchildren(left); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1362 | static void nilfs_btree_concat_right(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1363 | struct nilfs_btree_path *path, |
| 1364 | int level, __u64 *keyp, __u64 *ptrp) |
| 1365 | { |
| 1366 | struct nilfs_btree_node *node, *right; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1367 | int n, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1368 | |
| 1369 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); |
| 1370 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1371 | node = nilfs_btree_get_nonroot_node(path, level); |
| 1372 | right = nilfs_btree_get_sib_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1373 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1374 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1375 | n = nilfs_btree_node_get_nchildren(right); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1376 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1377 | nilfs_btree_node_move_left(node, right, n, ncblk, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1378 | |
| 1379 | if (!buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1380 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1381 | |
Ryusuke Konishi | 9f09890 | 2009-05-22 00:38:56 +0900 | [diff] [blame] | 1382 | nilfs_btnode_delete(path[level].bp_sib_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1383 | path[level].bp_sib_bh = NULL; |
| 1384 | path[level + 1].bp_index++; |
| 1385 | } |
| 1386 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1387 | static void nilfs_btree_shrink(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1388 | struct nilfs_btree_path *path, |
| 1389 | int level, __u64 *keyp, __u64 *ptrp) |
| 1390 | { |
| 1391 | struct nilfs_btree_node *root, *child; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1392 | int n, ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1393 | |
| 1394 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); |
| 1395 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1396 | root = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1397 | child = nilfs_btree_get_nonroot_node(path, level); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1398 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1399 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1400 | nilfs_btree_node_delete(root, 0, NULL, NULL, |
| 1401 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1402 | nilfs_btree_node_set_level(root, level); |
| 1403 | n = nilfs_btree_node_get_nchildren(child); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1404 | nilfs_btree_node_move_left(root, child, n, |
| 1405 | NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1406 | |
Ryusuke Konishi | 9f09890 | 2009-05-22 00:38:56 +0900 | [diff] [blame] | 1407 | nilfs_btnode_delete(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1408 | path[level].bp_bh = NULL; |
| 1409 | } |
| 1410 | |
Ryusuke Konishi | d409905 | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1411 | static void nilfs_btree_nop(struct nilfs_bmap *btree, |
| 1412 | struct nilfs_btree_path *path, |
| 1413 | int level, __u64 *keyp, __u64 *ptrp) |
| 1414 | { |
| 1415 | } |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1416 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1417 | static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1418 | struct nilfs_btree_path *path, |
| 1419 | int *levelp, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1420 | struct nilfs_bmap_stats *stats, |
| 1421 | struct inode *dat) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1422 | { |
| 1423 | struct buffer_head *bh; |
| 1424 | struct nilfs_btree_node *node, *parent, *sib; |
| 1425 | __u64 sibptr; |
Ryusuke Konishi | fe744fd | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1426 | int pindex, dindex, level, ncmin, ncmax, ncblk, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1427 | |
| 1428 | ret = 0; |
| 1429 | stats->bs_nblocks = 0; |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 1430 | ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree)); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1431 | ncblk = nilfs_btree_nchildren_per_block(btree); |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 1432 | |
Ryusuke Konishi | fe744fd | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1433 | for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1434 | level < nilfs_btree_height(btree) - 1; |
| 1435 | level++) { |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1436 | node = nilfs_btree_get_nonroot_node(path, level); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1437 | path[level].bp_oldreq.bpr_ptr = |
Ryusuke Konishi | fe744fd | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1438 | nilfs_btree_node_get_ptr(node, dindex, ncblk); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1439 | ret = nilfs_bmap_prepare_end_ptr(btree, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1440 | &path[level].bp_oldreq, dat); |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 1441 | if (ret < 0) |
| 1442 | goto err_out_child_node; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1443 | |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 1444 | if (nilfs_btree_node_get_nchildren(node) > ncmin) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1445 | path[level].bp_op = nilfs_btree_do_delete; |
| 1446 | stats->bs_nblocks++; |
| 1447 | goto out; |
| 1448 | } |
| 1449 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1450 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1451 | pindex = path[level + 1].bp_index; |
Ryusuke Konishi | fe744fd | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1452 | dindex = pindex; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1453 | |
| 1454 | if (pindex > 0) { |
| 1455 | /* left sibling */ |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1456 | sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1, |
| 1457 | ncmax); |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1458 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1459 | if (ret < 0) |
| 1460 | goto err_out_curr_node; |
| 1461 | sib = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 1462 | if (nilfs_btree_node_get_nchildren(sib) > ncmin) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1463 | path[level].bp_sib_bh = bh; |
| 1464 | path[level].bp_op = nilfs_btree_borrow_left; |
| 1465 | stats->bs_nblocks++; |
| 1466 | goto out; |
| 1467 | } else { |
| 1468 | path[level].bp_sib_bh = bh; |
| 1469 | path[level].bp_op = nilfs_btree_concat_left; |
| 1470 | stats->bs_nblocks++; |
| 1471 | /* continue; */ |
| 1472 | } |
| 1473 | } else if (pindex < |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1474 | nilfs_btree_node_get_nchildren(parent) - 1) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1475 | /* right sibling */ |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1476 | sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1, |
| 1477 | ncmax); |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1478 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1479 | if (ret < 0) |
| 1480 | goto err_out_curr_node; |
| 1481 | sib = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | ea64ab8 | 2010-07-13 23:33:52 +0900 | [diff] [blame] | 1482 | if (nilfs_btree_node_get_nchildren(sib) > ncmin) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1483 | path[level].bp_sib_bh = bh; |
| 1484 | path[level].bp_op = nilfs_btree_borrow_right; |
| 1485 | stats->bs_nblocks++; |
| 1486 | goto out; |
| 1487 | } else { |
| 1488 | path[level].bp_sib_bh = bh; |
| 1489 | path[level].bp_op = nilfs_btree_concat_right; |
| 1490 | stats->bs_nblocks++; |
Ryusuke Konishi | fe744fd | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1491 | /* |
| 1492 | * When merging right sibling node |
| 1493 | * into the current node, pointer to |
| 1494 | * the right sibling node must be |
| 1495 | * terminated instead. The adjustment |
| 1496 | * below is required for that. |
| 1497 | */ |
| 1498 | dindex = pindex + 1; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1499 | /* continue; */ |
| 1500 | } |
| 1501 | } else { |
| 1502 | /* no siblings */ |
| 1503 | /* the only child of the root node */ |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1504 | WARN_ON(level != nilfs_btree_height(btree) - 2); |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1505 | if (nilfs_btree_node_get_nchildren(node) - 1 <= |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1506 | NILFS_BTREE_ROOT_NCHILDREN_MAX) { |
| 1507 | path[level].bp_op = nilfs_btree_shrink; |
| 1508 | stats->bs_nblocks += 2; |
Ryusuke Konishi | d409905 | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1509 | level++; |
| 1510 | path[level].bp_op = nilfs_btree_nop; |
| 1511 | goto shrink_root_child; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1512 | } else { |
| 1513 | path[level].bp_op = nilfs_btree_do_delete; |
| 1514 | stats->bs_nblocks++; |
Ryusuke Konishi | d409905 | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1515 | goto out; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1516 | } |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Ryusuke Konishi | d409905 | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1520 | /* child of the root node is deleted */ |
| 1521 | path[level].bp_op = nilfs_btree_do_delete; |
| 1522 | stats->bs_nblocks++; |
| 1523 | |
| 1524 | shrink_root_child: |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1525 | node = nilfs_btree_get_root(btree); |
| 1526 | path[level].bp_oldreq.bpr_ptr = |
Ryusuke Konishi | fe744fd | 2011-05-25 23:00:27 +0900 | [diff] [blame] | 1527 | nilfs_btree_node_get_ptr(node, dindex, |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1528 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 1529 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1530 | ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat); |
Ryusuke Konishi | d4b9615 | 2009-05-24 03:25:44 +0900 | [diff] [blame] | 1531 | if (ret < 0) |
| 1532 | goto err_out_child_node; |
| 1533 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1534 | /* success */ |
| 1535 | out: |
| 1536 | *levelp = level; |
| 1537 | return ret; |
| 1538 | |
| 1539 | /* error */ |
| 1540 | err_out_curr_node: |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1541 | nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1542 | err_out_child_node: |
| 1543 | for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) { |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1544 | brelse(path[level].bp_sib_bh); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1545 | nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1546 | } |
| 1547 | *levelp = level; |
| 1548 | stats->bs_nblocks = 0; |
| 1549 | return ret; |
| 1550 | } |
| 1551 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1552 | static void nilfs_btree_commit_delete(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1553 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1554 | int maxlevel, struct inode *dat) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1555 | { |
| 1556 | int level; |
| 1557 | |
| 1558 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1559 | nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat); |
Pekka Enberg | 8acfbf0 | 2009-04-06 19:01:49 -0700 | [diff] [blame] | 1560 | path[level].bp_op(btree, path, level, NULL, NULL); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1563 | if (!nilfs_bmap_dirty(btree)) |
| 1564 | nilfs_bmap_set_dirty(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1567 | static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1568 | |
| 1569 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1570 | struct nilfs_btree_path *path; |
| 1571 | struct nilfs_bmap_stats stats; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1572 | struct inode *dat; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1573 | int level, ret; |
| 1574 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1575 | path = nilfs_btree_alloc_path(); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1576 | if (path == NULL) |
| 1577 | return -ENOMEM; |
Li Hong | f905440 | 2010-04-02 17:36:34 +0800 | [diff] [blame] | 1578 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1579 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 1580 | NILFS_BTREE_LEVEL_NODE_MIN, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1581 | if (ret < 0) |
| 1582 | goto out; |
| 1583 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1584 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1585 | dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1586 | |
| 1587 | ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1588 | if (ret < 0) |
| 1589 | goto out; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1590 | nilfs_btree_commit_delete(btree, path, level, dat); |
Ryusuke Konishi | be66737 | 2011-03-05 00:19:32 +0900 | [diff] [blame] | 1591 | nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1592 | |
| 1593 | out: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1594 | nilfs_btree_free_path(path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1595 | return ret; |
| 1596 | } |
| 1597 | |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 1598 | static int nilfs_btree_seek_key(const struct nilfs_bmap *btree, __u64 start, |
| 1599 | __u64 *keyp) |
| 1600 | { |
| 1601 | struct nilfs_btree_path *path; |
| 1602 | const int minlevel = NILFS_BTREE_LEVEL_NODE_MIN; |
| 1603 | int ret; |
| 1604 | |
| 1605 | path = nilfs_btree_alloc_path(); |
| 1606 | if (!path) |
| 1607 | return -ENOMEM; |
| 1608 | |
| 1609 | ret = nilfs_btree_do_lookup(btree, path, start, NULL, minlevel, 0); |
| 1610 | if (!ret) |
| 1611 | *keyp = start; |
| 1612 | else if (ret == -ENOENT) |
| 1613 | ret = nilfs_btree_get_next_key(btree, path, minlevel, keyp); |
| 1614 | |
| 1615 | nilfs_btree_free_path(path); |
| 1616 | return ret; |
| 1617 | } |
| 1618 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1619 | static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1620 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1621 | struct nilfs_btree_path *path; |
| 1622 | int ret; |
| 1623 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1624 | path = nilfs_btree_alloc_path(); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1625 | if (path == NULL) |
| 1626 | return -ENOMEM; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1627 | |
| 1628 | ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL); |
| 1629 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1630 | nilfs_btree_free_path(path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1631 | |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1635 | static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1636 | { |
| 1637 | struct buffer_head *bh; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1638 | struct nilfs_btree_node *root, *node; |
| 1639 | __u64 maxkey, nextmaxkey; |
| 1640 | __u64 ptr; |
| 1641 | int nchildren, ret; |
| 1642 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1643 | root = nilfs_btree_get_root(btree); |
| 1644 | switch (nilfs_btree_height(btree)) { |
| 1645 | case 2: |
| 1646 | bh = NULL; |
| 1647 | node = root; |
| 1648 | break; |
| 1649 | case 3: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1650 | nchildren = nilfs_btree_node_get_nchildren(root); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1651 | if (nchildren > 1) |
| 1652 | return 0; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1653 | ptr = nilfs_btree_node_get_ptr(root, nchildren - 1, |
| 1654 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1655 | ret = nilfs_btree_get_block(btree, ptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1656 | if (ret < 0) |
| 1657 | return ret; |
| 1658 | node = (struct nilfs_btree_node *)bh->b_data; |
| 1659 | break; |
| 1660 | default: |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1664 | nchildren = nilfs_btree_node_get_nchildren(node); |
| 1665 | maxkey = nilfs_btree_node_get_key(node, nchildren - 1); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1666 | nextmaxkey = (nchildren > 1) ? |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1667 | nilfs_btree_node_get_key(node, nchildren - 2) : 0; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1668 | if (bh != NULL) |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1669 | brelse(bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1670 | |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 1671 | return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1674 | static int nilfs_btree_gather_data(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1675 | __u64 *keys, __u64 *ptrs, int nitems) |
| 1676 | { |
| 1677 | struct buffer_head *bh; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1678 | struct nilfs_btree_node *node, *root; |
| 1679 | __le64 *dkeys; |
| 1680 | __le64 *dptrs; |
| 1681 | __u64 ptr; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1682 | int nchildren, ncmax, i, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1683 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1684 | root = nilfs_btree_get_root(btree); |
| 1685 | switch (nilfs_btree_height(btree)) { |
| 1686 | case 2: |
| 1687 | bh = NULL; |
| 1688 | node = root; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1689 | ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1690 | break; |
| 1691 | case 3: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1692 | nchildren = nilfs_btree_node_get_nchildren(root); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1693 | WARN_ON(nchildren > 1); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1694 | ptr = nilfs_btree_node_get_ptr(root, nchildren - 1, |
| 1695 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1696 | ret = nilfs_btree_get_block(btree, ptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1697 | if (ret < 0) |
| 1698 | return ret; |
| 1699 | node = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1700 | ncmax = nilfs_btree_nchildren_per_block(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1701 | break; |
| 1702 | default: |
| 1703 | node = NULL; |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1704 | return -EINVAL; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1707 | nchildren = nilfs_btree_node_get_nchildren(node); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1708 | if (nchildren < nitems) |
| 1709 | nitems = nchildren; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 1710 | dkeys = nilfs_btree_node_dkeys(node); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1711 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1712 | for (i = 0; i < nitems; i++) { |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 1713 | keys[i] = le64_to_cpu(dkeys[i]); |
| 1714 | ptrs[i] = le64_to_cpu(dptrs[i]); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | if (bh != NULL) |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1718 | brelse(bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1719 | |
| 1720 | return nitems; |
| 1721 | } |
| 1722 | |
| 1723 | static int |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1724 | nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1725 | union nilfs_bmap_ptr_req *dreq, |
| 1726 | union nilfs_bmap_ptr_req *nreq, |
| 1727 | struct buffer_head **bhp, |
| 1728 | struct nilfs_bmap_stats *stats) |
| 1729 | { |
| 1730 | struct buffer_head *bh; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1731 | struct inode *dat = NULL; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1732 | int ret; |
| 1733 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1734 | stats->bs_nblocks = 0; |
| 1735 | |
| 1736 | /* for data */ |
| 1737 | /* cannot find near ptr */ |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1738 | if (NILFS_BMAP_USE_VBN(btree)) { |
Ryusuke Konishi | 7cde31d | 2009-05-24 18:07:59 +0900 | [diff] [blame] | 1739 | dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1740 | dat = nilfs_bmap_get_dat(btree); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1741 | } |
Ryusuke Konishi | 7cde31d | 2009-05-24 18:07:59 +0900 | [diff] [blame] | 1742 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1743 | ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1744 | if (ret < 0) |
| 1745 | return ret; |
| 1746 | |
| 1747 | *bhp = NULL; |
| 1748 | stats->bs_nblocks++; |
| 1749 | if (nreq != NULL) { |
| 1750 | nreq->bpr_ptr = dreq->bpr_ptr + 1; |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1751 | ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1752 | if (ret < 0) |
| 1753 | goto err_out_dreq; |
| 1754 | |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 1755 | ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1756 | if (ret < 0) |
| 1757 | goto err_out_nreq; |
| 1758 | |
| 1759 | *bhp = bh; |
| 1760 | stats->bs_nblocks++; |
| 1761 | } |
| 1762 | |
| 1763 | /* success */ |
| 1764 | return 0; |
| 1765 | |
| 1766 | /* error */ |
| 1767 | err_out_nreq: |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1768 | nilfs_bmap_abort_alloc_ptr(btree, nreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1769 | err_out_dreq: |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1770 | nilfs_bmap_abort_alloc_ptr(btree, dreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1771 | stats->bs_nblocks = 0; |
| 1772 | return ret; |
| 1773 | |
| 1774 | } |
| 1775 | |
| 1776 | static void |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1777 | nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1778 | __u64 key, __u64 ptr, |
| 1779 | const __u64 *keys, const __u64 *ptrs, |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 1780 | int n, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1781 | union nilfs_bmap_ptr_req *dreq, |
| 1782 | union nilfs_bmap_ptr_req *nreq, |
| 1783 | struct buffer_head *bh) |
| 1784 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1785 | struct nilfs_btree_node *node; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1786 | struct inode *dat; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1787 | __u64 tmpptr; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1788 | int ncblk; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1789 | |
| 1790 | /* free resources */ |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1791 | if (btree->b_ops->bop_clear != NULL) |
| 1792 | btree->b_ops->bop_clear(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1793 | |
| 1794 | /* ptr must be a pointer to a buffer head. */ |
| 1795 | set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr)); |
| 1796 | |
| 1797 | /* convert and insert */ |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1798 | dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL; |
Ryusuke Konishi | 957ed60 | 2015-02-27 15:51:56 -0800 | [diff] [blame] | 1799 | __nilfs_btree_init(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1800 | if (nreq != NULL) { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1801 | nilfs_bmap_commit_alloc_ptr(btree, dreq, dat); |
| 1802 | nilfs_bmap_commit_alloc_ptr(btree, nreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1803 | |
| 1804 | /* create child node at level 1 */ |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1805 | node = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1806 | ncblk = nilfs_btree_nchildren_per_block(btree); |
| 1807 | nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs); |
| 1808 | nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1809 | if (!buffer_dirty(bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1810 | mark_buffer_dirty(bh); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1811 | if (!nilfs_bmap_dirty(btree)) |
| 1812 | nilfs_bmap_set_dirty(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1813 | |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 1814 | brelse(bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1815 | |
| 1816 | /* create root node at level 2 */ |
| 1817 | node = nilfs_btree_get_root(btree); |
| 1818 | tmpptr = nreq->bpr_ptr; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1819 | nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1, |
| 1820 | NILFS_BTREE_ROOT_NCHILDREN_MAX, |
| 1821 | &keys[0], &tmpptr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1822 | } else { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1823 | nilfs_bmap_commit_alloc_ptr(btree, dreq, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1824 | |
| 1825 | /* create root node at level 1 */ |
| 1826 | node = nilfs_btree_get_root(btree); |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1827 | nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n, |
| 1828 | NILFS_BTREE_ROOT_NCHILDREN_MAX, |
| 1829 | keys, ptrs); |
| 1830 | nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, |
| 1831 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1832 | if (!nilfs_bmap_dirty(btree)) |
| 1833 | nilfs_bmap_set_dirty(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1836 | if (NILFS_BMAP_USE_VBN(btree)) |
Ryusuke Konishi | dc935be | 2010-07-10 22:21:54 +0900 | [diff] [blame] | 1837 | nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /** |
| 1841 | * nilfs_btree_convert_and_insert - |
| 1842 | * @bmap: |
| 1843 | * @key: |
| 1844 | * @ptr: |
| 1845 | * @keys: |
| 1846 | * @ptrs: |
| 1847 | * @n: |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1848 | */ |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1849 | int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1850 | __u64 key, __u64 ptr, |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 1851 | const __u64 *keys, const __u64 *ptrs, int n) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1852 | { |
Ryusuke Konishi | 4f05028 | 2015-11-06 16:32:16 -0800 | [diff] [blame] | 1853 | struct buffer_head *bh = NULL; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1854 | union nilfs_bmap_ptr_req dreq, nreq, *di, *ni; |
| 1855 | struct nilfs_bmap_stats stats; |
| 1856 | int ret; |
| 1857 | |
| 1858 | if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) { |
| 1859 | di = &dreq; |
| 1860 | ni = NULL; |
| 1861 | } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX( |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1862 | 1 << btree->b_inode->i_blkbits)) { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1863 | di = &dreq; |
| 1864 | ni = &nreq; |
| 1865 | } else { |
| 1866 | di = NULL; |
| 1867 | ni = NULL; |
| 1868 | BUG(); |
| 1869 | } |
| 1870 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1871 | ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1872 | &stats); |
| 1873 | if (ret < 0) |
| 1874 | return ret; |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1875 | nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n, |
Ryusuke Konishi | 3033342 | 2009-05-24 00:09:44 +0900 | [diff] [blame] | 1876 | di, ni, bh); |
Ryusuke Konishi | be66737 | 2011-03-05 00:19:32 +0900 | [diff] [blame] | 1877 | nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1878 | return 0; |
| 1879 | } |
| 1880 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1881 | static int nilfs_btree_propagate_p(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1882 | struct nilfs_btree_path *path, |
| 1883 | int level, |
| 1884 | struct buffer_head *bh) |
| 1885 | { |
| 1886 | while ((++level < nilfs_btree_height(btree) - 1) && |
| 1887 | !buffer_dirty(path[level].bp_bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 1888 | mark_buffer_dirty(path[level].bp_bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1889 | |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1893 | static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1894 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1895 | int level, struct inode *dat) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1896 | { |
| 1897 | struct nilfs_btree_node *parent; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1898 | int ncmax, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1899 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1900 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1901 | path[level].bp_oldreq.bpr_ptr = |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1902 | nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index, |
| 1903 | ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1904 | path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1905 | ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req, |
| 1906 | &path[level].bp_newreq.bpr_req); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1907 | if (ret < 0) |
| 1908 | return ret; |
| 1909 | |
| 1910 | if (buffer_nilfs_node(path[level].bp_bh)) { |
| 1911 | path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr; |
| 1912 | path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr; |
| 1913 | path[level].bp_ctxt.bh = path[level].bp_bh; |
| 1914 | ret = nilfs_btnode_prepare_change_key( |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1915 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1916 | &path[level].bp_ctxt); |
| 1917 | if (ret < 0) { |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1918 | nilfs_dat_abort_update(dat, |
| 1919 | &path[level].bp_oldreq.bpr_req, |
| 1920 | &path[level].bp_newreq.bpr_req); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1921 | return ret; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | return 0; |
| 1926 | } |
| 1927 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1928 | static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1929 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1930 | int level, struct inode *dat) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1931 | { |
| 1932 | struct nilfs_btree_node *parent; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1933 | int ncmax; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1934 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1935 | nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req, |
| 1936 | &path[level].bp_newreq.bpr_req, |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1937 | btree->b_ptr_type == NILFS_BMAP_PTR_VS); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1938 | |
| 1939 | if (buffer_nilfs_node(path[level].bp_bh)) { |
| 1940 | nilfs_btnode_commit_change_key( |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1941 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1942 | &path[level].bp_ctxt); |
| 1943 | path[level].bp_bh = path[level].bp_ctxt.bh; |
| 1944 | } |
| 1945 | set_buffer_nilfs_volatile(path[level].bp_bh); |
| 1946 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 1947 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
| 1948 | nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, |
| 1949 | path[level].bp_newreq.bpr_ptr, ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1952 | static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1953 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1954 | int level, struct inode *dat) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1955 | { |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1956 | nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req, |
| 1957 | &path[level].bp_newreq.bpr_req); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1958 | if (buffer_nilfs_node(path[level].bp_bh)) |
| 1959 | nilfs_btnode_abort_change_key( |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1960 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1961 | &path[level].bp_ctxt); |
| 1962 | } |
| 1963 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1964 | static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1965 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1966 | int minlevel, int *maxlevelp, |
| 1967 | struct inode *dat) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1968 | { |
| 1969 | int level, ret; |
| 1970 | |
| 1971 | level = minlevel; |
| 1972 | if (!buffer_nilfs_volatile(path[level].bp_bh)) { |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1973 | ret = nilfs_btree_prepare_update_v(btree, path, level, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1974 | if (ret < 0) |
| 1975 | return ret; |
| 1976 | } |
| 1977 | while ((++level < nilfs_btree_height(btree) - 1) && |
| 1978 | !buffer_dirty(path[level].bp_bh)) { |
| 1979 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 1980 | WARN_ON(buffer_nilfs_volatile(path[level].bp_bh)); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1981 | ret = nilfs_btree_prepare_update_v(btree, path, level, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1982 | if (ret < 0) |
| 1983 | goto out; |
| 1984 | } |
| 1985 | |
| 1986 | /* success */ |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1987 | *maxlevelp = level - 1; |
| 1988 | return 0; |
| 1989 | |
| 1990 | /* error */ |
| 1991 | out: |
| 1992 | while (--level > minlevel) |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1993 | nilfs_btree_abort_update_v(btree, path, level, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1994 | if (!buffer_nilfs_volatile(path[level].bp_bh)) |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 1995 | nilfs_btree_abort_update_v(btree, path, level, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 1996 | return ret; |
| 1997 | } |
| 1998 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 1999 | static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2000 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2001 | int minlevel, int maxlevel, |
| 2002 | struct buffer_head *bh, |
| 2003 | struct inode *dat) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2004 | { |
| 2005 | int level; |
| 2006 | |
| 2007 | if (!buffer_nilfs_volatile(path[minlevel].bp_bh)) |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2008 | nilfs_btree_commit_update_v(btree, path, minlevel, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2009 | |
| 2010 | for (level = minlevel + 1; level <= maxlevel; level++) |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2011 | nilfs_btree_commit_update_v(btree, path, level, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2014 | static int nilfs_btree_propagate_v(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2015 | struct nilfs_btree_path *path, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2016 | int level, struct buffer_head *bh) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2017 | { |
Li Hong | 308f441 | 2010-04-02 18:40:39 +0800 | [diff] [blame] | 2018 | int maxlevel = 0, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2019 | struct nilfs_btree_node *parent; |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2020 | struct inode *dat = nilfs_bmap_get_dat(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2021 | __u64 ptr; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 2022 | int ncmax; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2023 | |
| 2024 | get_bh(bh); |
| 2025 | path[level].bp_bh = bh; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2026 | ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel, |
| 2027 | dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2028 | if (ret < 0) |
| 2029 | goto out; |
| 2030 | |
| 2031 | if (buffer_nilfs_volatile(path[level].bp_bh)) { |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 2032 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
| 2033 | ptr = nilfs_btree_node_get_ptr(parent, |
| 2034 | path[level + 1].bp_index, |
| 2035 | ncmax); |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2036 | ret = nilfs_dat_mark_dirty(dat, ptr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2037 | if (ret < 0) |
| 2038 | goto out; |
| 2039 | } |
| 2040 | |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2041 | nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2042 | |
| 2043 | out: |
| 2044 | brelse(path[level].bp_bh); |
| 2045 | path[level].bp_bh = NULL; |
| 2046 | return ret; |
| 2047 | } |
| 2048 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2049 | static int nilfs_btree_propagate(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2050 | struct buffer_head *bh) |
| 2051 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2052 | struct nilfs_btree_path *path; |
| 2053 | struct nilfs_btree_node *node; |
| 2054 | __u64 key; |
| 2055 | int level, ret; |
| 2056 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 2057 | WARN_ON(!buffer_dirty(bh)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2058 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2059 | path = nilfs_btree_alloc_path(); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2060 | if (path == NULL) |
| 2061 | return -ENOMEM; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2062 | |
| 2063 | if (buffer_nilfs_node(bh)) { |
| 2064 | node = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2065 | key = nilfs_btree_node_get_key(node, 0); |
| 2066 | level = nilfs_btree_node_get_level(node); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2067 | } else { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2068 | key = nilfs_bmap_data_get_key(btree, bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2069 | level = NILFS_BTREE_LEVEL_DATA; |
| 2070 | } |
| 2071 | |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 2072 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2073 | if (ret < 0) { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 2074 | if (unlikely(ret == -ENOENT)) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2075 | printk(KERN_CRIT "%s: key = %llu, level == %d\n", |
| 2076 | __func__, (unsigned long long)key, level); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2077 | goto out; |
| 2078 | } |
| 2079 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2080 | ret = NILFS_BMAP_USE_VBN(btree) ? |
Ryusuke Konishi | 7cde31d | 2009-05-24 18:07:59 +0900 | [diff] [blame] | 2081 | nilfs_btree_propagate_v(btree, path, level, bh) : |
| 2082 | nilfs_btree_propagate_p(btree, path, level, bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2083 | |
| 2084 | out: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2085 | nilfs_btree_free_path(path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2086 | |
| 2087 | return ret; |
| 2088 | } |
| 2089 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2090 | static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2091 | struct buffer_head *bh) |
| 2092 | { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2093 | return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2096 | static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2097 | struct list_head *lists, |
| 2098 | struct buffer_head *bh) |
| 2099 | { |
| 2100 | struct list_head *head; |
| 2101 | struct buffer_head *cbh; |
| 2102 | struct nilfs_btree_node *node, *cnode; |
| 2103 | __u64 key, ckey; |
| 2104 | int level; |
| 2105 | |
| 2106 | get_bh(bh); |
| 2107 | node = (struct nilfs_btree_node *)bh->b_data; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2108 | key = nilfs_btree_node_get_key(node, 0); |
| 2109 | level = nilfs_btree_node_get_level(node); |
Ryusuke Konishi | cfa913a | 2010-07-07 17:19:54 +0900 | [diff] [blame] | 2110 | if (level < NILFS_BTREE_LEVEL_NODE_MIN || |
| 2111 | level >= NILFS_BTREE_LEVEL_MAX) { |
| 2112 | dump_stack(); |
| 2113 | printk(KERN_WARNING |
| 2114 | "%s: invalid btree level: %d (key=%llu, ino=%lu, " |
| 2115 | "blocknr=%llu)\n", |
| 2116 | __func__, level, (unsigned long long)key, |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2117 | NILFS_BMAP_I(btree)->vfs_inode.i_ino, |
Ryusuke Konishi | cfa913a | 2010-07-07 17:19:54 +0900 | [diff] [blame] | 2118 | (unsigned long long)bh->b_blocknr); |
| 2119 | return; |
| 2120 | } |
| 2121 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2122 | list_for_each(head, &lists[level]) { |
| 2123 | cbh = list_entry(head, struct buffer_head, b_assoc_buffers); |
| 2124 | cnode = (struct nilfs_btree_node *)cbh->b_data; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2125 | ckey = nilfs_btree_node_get_key(cnode, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2126 | if (key < ckey) |
| 2127 | break; |
| 2128 | } |
| 2129 | list_add_tail(&bh->b_assoc_buffers, head); |
| 2130 | } |
| 2131 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2132 | static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2133 | struct list_head *listp) |
| 2134 | { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2135 | struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2136 | struct list_head lists[NILFS_BTREE_LEVEL_MAX]; |
| 2137 | struct pagevec pvec; |
| 2138 | struct buffer_head *bh, *head; |
| 2139 | pgoff_t index = 0; |
| 2140 | int level, i; |
| 2141 | |
| 2142 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; |
| 2143 | level < NILFS_BTREE_LEVEL_MAX; |
| 2144 | level++) |
| 2145 | INIT_LIST_HEAD(&lists[level]); |
| 2146 | |
| 2147 | pagevec_init(&pvec, 0); |
| 2148 | |
| 2149 | while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY, |
| 2150 | PAGEVEC_SIZE)) { |
| 2151 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 2152 | bh = head = page_buffers(pvec.pages[i]); |
| 2153 | do { |
| 2154 | if (buffer_dirty(bh)) |
| 2155 | nilfs_btree_add_dirty_buffer(btree, |
| 2156 | lists, bh); |
| 2157 | } while ((bh = bh->b_this_page) != head); |
| 2158 | } |
| 2159 | pagevec_release(&pvec); |
| 2160 | cond_resched(); |
| 2161 | } |
| 2162 | |
| 2163 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; |
| 2164 | level < NILFS_BTREE_LEVEL_MAX; |
| 2165 | level++) |
Ryusuke Konishi | 0935db7 | 2009-11-29 02:39:11 +0900 | [diff] [blame] | 2166 | list_splice_tail(&lists[level], listp); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2169 | static int nilfs_btree_assign_p(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2170 | struct nilfs_btree_path *path, |
| 2171 | int level, |
| 2172 | struct buffer_head **bh, |
| 2173 | sector_t blocknr, |
| 2174 | union nilfs_binfo *binfo) |
| 2175 | { |
| 2176 | struct nilfs_btree_node *parent; |
| 2177 | __u64 key; |
| 2178 | __u64 ptr; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 2179 | int ncmax, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2180 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 2181 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
| 2182 | ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index, |
| 2183 | ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2184 | if (buffer_nilfs_node(*bh)) { |
| 2185 | path[level].bp_ctxt.oldkey = ptr; |
| 2186 | path[level].bp_ctxt.newkey = blocknr; |
| 2187 | path[level].bp_ctxt.bh = *bh; |
| 2188 | ret = nilfs_btnode_prepare_change_key( |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2189 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2190 | &path[level].bp_ctxt); |
| 2191 | if (ret < 0) |
| 2192 | return ret; |
| 2193 | nilfs_btnode_commit_change_key( |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2194 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2195 | &path[level].bp_ctxt); |
| 2196 | *bh = path[level].bp_ctxt.bh; |
| 2197 | } |
| 2198 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 2199 | nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr, |
| 2200 | ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2201 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2202 | key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2203 | /* on-disk format */ |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 2204 | binfo->bi_dat.bi_blkoff = cpu_to_le64(key); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2205 | binfo->bi_dat.bi_level = level; |
| 2206 | |
| 2207 | return 0; |
| 2208 | } |
| 2209 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2210 | static int nilfs_btree_assign_v(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2211 | struct nilfs_btree_path *path, |
| 2212 | int level, |
| 2213 | struct buffer_head **bh, |
| 2214 | sector_t blocknr, |
| 2215 | union nilfs_binfo *binfo) |
| 2216 | { |
| 2217 | struct nilfs_btree_node *parent; |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2218 | struct inode *dat = nilfs_bmap_get_dat(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2219 | __u64 key; |
| 2220 | __u64 ptr; |
| 2221 | union nilfs_bmap_ptr_req req; |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 2222 | int ncmax, ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2223 | |
Ryusuke Konishi | 9b7b265 | 2010-07-13 23:33:53 +0900 | [diff] [blame] | 2224 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
| 2225 | ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index, |
| 2226 | ncmax); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2227 | req.bpr_ptr = ptr; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2228 | ret = nilfs_dat_prepare_start(dat, &req.bpr_req); |
| 2229 | if (ret < 0) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2230 | return ret; |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2231 | nilfs_dat_commit_start(dat, &req.bpr_req, blocknr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2232 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2233 | key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2234 | /* on-disk format */ |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 2235 | binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr); |
| 2236 | binfo->bi_v.bi_blkoff = cpu_to_le64(key); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2237 | |
| 2238 | return 0; |
| 2239 | } |
| 2240 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2241 | static int nilfs_btree_assign(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2242 | struct buffer_head **bh, |
| 2243 | sector_t blocknr, |
| 2244 | union nilfs_binfo *binfo) |
| 2245 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2246 | struct nilfs_btree_path *path; |
| 2247 | struct nilfs_btree_node *node; |
| 2248 | __u64 key; |
| 2249 | int level, ret; |
| 2250 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2251 | path = nilfs_btree_alloc_path(); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2252 | if (path == NULL) |
| 2253 | return -ENOMEM; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2254 | |
| 2255 | if (buffer_nilfs_node(*bh)) { |
| 2256 | node = (struct nilfs_btree_node *)(*bh)->b_data; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2257 | key = nilfs_btree_node_get_key(node, 0); |
| 2258 | level = nilfs_btree_node_get_level(node); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2259 | } else { |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2260 | key = nilfs_bmap_data_get_key(btree, *bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2261 | level = NILFS_BTREE_LEVEL_DATA; |
| 2262 | } |
| 2263 | |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 2264 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2265 | if (ret < 0) { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 2266 | WARN_ON(ret == -ENOENT); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2267 | goto out; |
| 2268 | } |
| 2269 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2270 | ret = NILFS_BMAP_USE_VBN(btree) ? |
Ryusuke Konishi | 7cde31d | 2009-05-24 18:07:59 +0900 | [diff] [blame] | 2271 | nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) : |
| 2272 | nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2273 | |
| 2274 | out: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2275 | nilfs_btree_free_path(path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2276 | |
| 2277 | return ret; |
| 2278 | } |
| 2279 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2280 | static int nilfs_btree_assign_gc(struct nilfs_bmap *btree, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2281 | struct buffer_head **bh, |
| 2282 | sector_t blocknr, |
| 2283 | union nilfs_binfo *binfo) |
| 2284 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2285 | struct nilfs_btree_node *node; |
| 2286 | __u64 key; |
| 2287 | int ret; |
| 2288 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2289 | ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr, |
Ryusuke Konishi | 2e0c2c7 | 2009-08-15 15:34:33 +0900 | [diff] [blame] | 2290 | blocknr); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2291 | if (ret < 0) |
| 2292 | return ret; |
| 2293 | |
| 2294 | if (buffer_nilfs_node(*bh)) { |
| 2295 | node = (struct nilfs_btree_node *)(*bh)->b_data; |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2296 | key = nilfs_btree_node_get_key(node, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2297 | } else |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2298 | key = nilfs_bmap_data_get_key(btree, *bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2299 | |
| 2300 | /* on-disk format */ |
| 2301 | binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr); |
Ryusuke Konishi | 25b8d7d | 2010-07-10 16:50:41 +0900 | [diff] [blame] | 2302 | binfo->bi_v.bi_blkoff = cpu_to_le64(key); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2303 | |
| 2304 | return 0; |
| 2305 | } |
| 2306 | |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2307 | static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2308 | { |
| 2309 | struct buffer_head *bh; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2310 | struct nilfs_btree_path *path; |
| 2311 | __u64 ptr; |
| 2312 | int ret; |
| 2313 | |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2314 | path = nilfs_btree_alloc_path(); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2315 | if (path == NULL) |
| 2316 | return -ENOMEM; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2317 | |
Ryusuke Konishi | 03bdb5a | 2010-07-18 10:42:26 +0900 | [diff] [blame] | 2318 | ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2319 | if (ret < 0) { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 2320 | WARN_ON(ret == -ENOENT); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2321 | goto out; |
| 2322 | } |
Ryusuke Konishi | f198dbb | 2009-05-22 01:07:13 +0900 | [diff] [blame] | 2323 | ret = nilfs_btree_get_block(btree, ptr, &bh); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2324 | if (ret < 0) { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 2325 | WARN_ON(ret == -ENOENT); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2326 | goto out; |
| 2327 | } |
| 2328 | |
| 2329 | if (!buffer_dirty(bh)) |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 2330 | mark_buffer_dirty(bh); |
Ryusuke Konishi | 087d01b | 2009-05-22 00:33:13 +0900 | [diff] [blame] | 2331 | brelse(bh); |
Ryusuke Konishi | e7c274f | 2010-07-10 19:09:49 +0900 | [diff] [blame] | 2332 | if (!nilfs_bmap_dirty(btree)) |
| 2333 | nilfs_bmap_set_dirty(btree); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2334 | |
| 2335 | out: |
Ryusuke Konishi | 6d28f7e | 2009-08-15 01:14:10 +0900 | [diff] [blame] | 2336 | nilfs_btree_free_path(path); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2337 | return ret; |
| 2338 | } |
| 2339 | |
| 2340 | static const struct nilfs_bmap_operations nilfs_btree_ops = { |
| 2341 | .bop_lookup = nilfs_btree_lookup, |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 2342 | .bop_lookup_contig = nilfs_btree_lookup_contig, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2343 | .bop_insert = nilfs_btree_insert, |
| 2344 | .bop_delete = nilfs_btree_delete, |
| 2345 | .bop_clear = NULL, |
| 2346 | |
| 2347 | .bop_propagate = nilfs_btree_propagate, |
| 2348 | |
| 2349 | .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers, |
| 2350 | |
| 2351 | .bop_assign = nilfs_btree_assign, |
| 2352 | .bop_mark = nilfs_btree_mark, |
| 2353 | |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 2354 | .bop_seek_key = nilfs_btree_seek_key, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2355 | .bop_last_key = nilfs_btree_last_key, |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 2356 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2357 | .bop_check_insert = NULL, |
| 2358 | .bop_check_delete = nilfs_btree_check_delete, |
| 2359 | .bop_gather_data = nilfs_btree_gather_data, |
| 2360 | }; |
| 2361 | |
| 2362 | static const struct nilfs_bmap_operations nilfs_btree_ops_gc = { |
| 2363 | .bop_lookup = NULL, |
Ryusuke Konishi | c3a7abf | 2009-05-25 02:47:14 +0900 | [diff] [blame] | 2364 | .bop_lookup_contig = NULL, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2365 | .bop_insert = NULL, |
| 2366 | .bop_delete = NULL, |
| 2367 | .bop_clear = NULL, |
| 2368 | |
| 2369 | .bop_propagate = nilfs_btree_propagate_gc, |
| 2370 | |
| 2371 | .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers, |
| 2372 | |
| 2373 | .bop_assign = nilfs_btree_assign_gc, |
| 2374 | .bop_mark = NULL, |
| 2375 | |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 2376 | .bop_seek_key = NULL, |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2377 | .bop_last_key = NULL, |
Ryusuke Konishi | 5b20384 | 2015-04-16 12:46:36 -0700 | [diff] [blame] | 2378 | |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2379 | .bop_check_insert = NULL, |
| 2380 | .bop_check_delete = NULL, |
| 2381 | .bop_gather_data = NULL, |
| 2382 | }; |
| 2383 | |
Ryusuke Konishi | 957ed60 | 2015-02-27 15:51:56 -0800 | [diff] [blame] | 2384 | static void __nilfs_btree_init(struct nilfs_bmap *bmap) |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2385 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2386 | bmap->b_ops = &nilfs_btree_ops; |
Ryusuke Konishi | 5ad2686e9 | 2010-07-13 23:33:54 +0900 | [diff] [blame] | 2387 | bmap->b_nchildren_per_block = |
| 2388 | NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap)); |
Ryusuke Konishi | 957ed60 | 2015-02-27 15:51:56 -0800 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | int nilfs_btree_init(struct nilfs_bmap *bmap) |
| 2392 | { |
| 2393 | int ret = 0; |
| 2394 | |
| 2395 | __nilfs_btree_init(bmap); |
| 2396 | |
| 2397 | if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap), |
| 2398 | bmap->b_inode->i_ino)) |
| 2399 | ret = -EIO; |
| 2400 | return ret; |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | void nilfs_btree_init_gc(struct nilfs_bmap *bmap) |
| 2404 | { |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2405 | bmap->b_ops = &nilfs_btree_ops_gc; |
Ryusuke Konishi | 5ad2686e9 | 2010-07-13 23:33:54 +0900 | [diff] [blame] | 2406 | bmap->b_nchildren_per_block = |
| 2407 | NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap)); |
Koji Sato | 17c76b0 | 2009-04-06 19:01:24 -0700 | [diff] [blame] | 2408 | } |