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