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