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