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