blob: 3a3821b00486b531e72ff931be80b3627a0ddca1 [file] [log] [blame]
Koji Sato17c76b02009-04-06 19:01:24 -07001/*
2 * btree.c - NILFS B-tree.
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Written by Koji Sato <koji@osrg.net>.
21 */
22
23#include <linux/slab.h>
24#include <linux/string.h>
25#include <linux/errno.h>
26#include <linux/pagevec.h>
27#include "nilfs.h"
28#include "page.h"
29#include "btnode.h"
30#include "btree.h"
31#include "alloc.h"
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090032#include "dat.h"
Koji Sato17c76b02009-04-06 19:01:24 -070033
Ryusuke Konishi957ed602015-02-27 15:51:56 -080034static void __nilfs_btree_init(struct nilfs_bmap *bmap);
35
Li Hongf9054402010-04-02 17:36:34 +080036static struct nilfs_btree_path *nilfs_btree_alloc_path(void)
Koji Sato17c76b02009-04-06 19:01:24 -070037{
Li Hongf9054402010-04-02 17:36:34 +080038 struct nilfs_btree_path *path;
39 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070040
Li Hongf9054402010-04-02 17:36:34 +080041 path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
42 if (path == NULL)
43 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -070044
Li Hongf9054402010-04-02 17:36:34 +080045 for (; level < NILFS_BTREE_LEVEL_MAX; level++) {
Koji Sato17c76b02009-04-06 19:01:24 -070046 path[level].bp_bh = NULL;
47 path[level].bp_sib_bh = NULL;
48 path[level].bp_index = 0;
49 path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
50 path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
51 path[level].bp_op = NULL;
52 }
Li Hongf9054402010-04-02 17:36:34 +080053
54out:
55 return path;
56}
57
Li Hong73bb4882010-04-02 18:35:00 +080058static void nilfs_btree_free_path(struct nilfs_btree_path *path)
Li Hongf9054402010-04-02 17:36:34 +080059{
Li Hong73bb4882010-04-02 18:35:00 +080060 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070061
Li Hong73bb4882010-04-02 18:35:00 +080062 for (; level < NILFS_BTREE_LEVEL_MAX; level++)
Ryusuke Konishi32189292009-08-15 01:54:59 +090063 brelse(path[level].bp_bh);
Li Hong73bb4882010-04-02 18:35:00 +080064
65 kmem_cache_free(nilfs_btree_path_cache, path);
Koji Sato17c76b02009-04-06 19:01:24 -070066}
67
Koji Sato17c76b02009-04-06 19:01:24 -070068/*
69 * B-tree node operations
70 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090071static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090072 __u64 ptr, struct buffer_head **bhp)
73{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090074 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
Ryusuke Konishi45f49102009-11-13 16:25:19 +090075 struct buffer_head *bh;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090076
Ryusuke Konishi45f49102009-11-13 16:25:19 +090077 bh = nilfs_btnode_create_block(btnc, ptr);
78 if (!bh)
79 return -ENOMEM;
80
81 set_buffer_nilfs_volatile(bh);
82 *bhp = bh;
83 return 0;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090084}
Koji Sato17c76b02009-04-06 19:01:24 -070085
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090086static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070087{
88 return node->bn_flags;
89}
90
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090091static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090092nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
Koji Sato17c76b02009-04-06 19:01:24 -070093{
94 node->bn_flags = flags;
95}
96
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090097static int nilfs_btree_node_root(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070098{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090099 return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
Koji Sato17c76b02009-04-06 19:01:24 -0700100}
101
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900102static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700103{
104 return node->bn_level;
105}
106
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900107static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900108nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700109{
110 node->bn_level = level;
111}
112
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900113static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700114{
115 return le16_to_cpu(node->bn_nchildren);
116}
117
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900118static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900119nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
Koji Sato17c76b02009-04-06 19:01:24 -0700120{
121 node->bn_nchildren = cpu_to_le16(nchildren);
122}
123
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900124static int nilfs_btree_node_size(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700125{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900126 return 1 << btree->b_inode->i_blkbits;
Koji Sato17c76b02009-04-06 19:01:24 -0700127}
128
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900129static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700130{
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +0900131 return btree->b_nchildren_per_block;
Koji Sato17c76b02009-04-06 19:01:24 -0700132}
133
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900134static __le64 *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900135nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700136{
137 return (__le64 *)((char *)(node + 1) +
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900138 (nilfs_btree_node_root(node) ?
Koji Sato17c76b02009-04-06 19:01:24 -0700139 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
140}
141
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900142static __le64 *
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900143nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700144{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900145 return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700146}
147
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900148static __u64
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900149nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
Koji Sato17c76b02009-04-06 19:01:24 -0700150{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900151 return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700152}
153
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900154static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900155nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -0700156{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900157 *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -0700158}
159
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900160static __u64
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900161nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
162 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700163{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900164 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700165}
166
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900167static void
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900168nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
169 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700170{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900171 *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700172}
173
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900174static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
175 int level, int nchildren, int ncmax,
Koji Sato17c76b02009-04-06 19:01:24 -0700176 const __u64 *keys, const __u64 *ptrs)
177{
178 __le64 *dkeys;
179 __le64 *dptrs;
180 int i;
181
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900182 nilfs_btree_node_set_flags(node, flags);
183 nilfs_btree_node_set_level(node, level);
184 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700185
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900186 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900187 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700188 for (i = 0; i < nchildren; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900189 dkeys[i] = cpu_to_le64(keys[i]);
190 dptrs[i] = cpu_to_le64(ptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -0700191 }
192}
193
194/* Assume the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900195static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700196 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900197 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700198{
199 __le64 *ldkeys, *rdkeys;
200 __le64 *ldptrs, *rdptrs;
201 int lnchildren, rnchildren;
202
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900203 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900204 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900205 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700206
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900207 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900208 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900209 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700210
211 memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
212 memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs));
213 memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys));
214 memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs));
215
216 lnchildren += n;
217 rnchildren -= n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900218 nilfs_btree_node_set_nchildren(left, lnchildren);
219 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700220}
221
222/* Assume that the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900223static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700224 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900225 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700226{
227 __le64 *ldkeys, *rdkeys;
228 __le64 *ldptrs, *rdptrs;
229 int lnchildren, rnchildren;
230
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900231 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900232 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900233 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700234
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900235 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900236 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900237 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700238
239 memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
240 memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs));
241 memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys));
242 memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs));
243
244 lnchildren -= n;
245 rnchildren += n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900246 nilfs_btree_node_set_nchildren(left, lnchildren);
247 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700248}
249
250/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900251static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
252 __u64 key, __u64 ptr, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700253{
254 __le64 *dkeys;
255 __le64 *dptrs;
256 int nchildren;
257
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900258 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900259 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900260 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700261 if (index < nchildren) {
262 memmove(dkeys + index + 1, dkeys + index,
263 (nchildren - index) * sizeof(*dkeys));
264 memmove(dptrs + index + 1, dptrs + index,
265 (nchildren - index) * sizeof(*dptrs));
266 }
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900267 dkeys[index] = cpu_to_le64(key);
268 dptrs[index] = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700269 nchildren++;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900270 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700271}
272
273/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900274static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
275 __u64 *keyp, __u64 *ptrp, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700276{
277 __u64 key;
278 __u64 ptr;
279 __le64 *dkeys;
280 __le64 *dptrs;
281 int nchildren;
282
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900283 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900284 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900285 key = le64_to_cpu(dkeys[index]);
286 ptr = le64_to_cpu(dptrs[index]);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900287 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700288 if (keyp != NULL)
289 *keyp = key;
290 if (ptrp != NULL)
291 *ptrp = ptr;
292
293 if (index < nchildren - 1) {
294 memmove(dkeys + index, dkeys + index + 1,
295 (nchildren - index - 1) * sizeof(*dkeys));
296 memmove(dptrs + index, dptrs + index + 1,
297 (nchildren - index - 1) * sizeof(*dptrs));
298 }
299 nchildren--;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900300 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700301}
302
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900303static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
Koji Sato17c76b02009-04-06 19:01:24 -0700304 __u64 key, int *indexp)
305{
306 __u64 nkey;
307 int index, low, high, s;
308
309 /* binary search */
310 low = 0;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900311 high = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700312 index = 0;
313 s = 0;
314 while (low <= high) {
315 index = (low + high) / 2;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900316 nkey = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700317 if (nkey == key) {
318 s = 0;
319 goto out;
320 } else if (nkey < key) {
321 low = index + 1;
322 s = -1;
323 } else {
324 high = index - 1;
325 s = 1;
326 }
327 }
328
329 /* adjust index */
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900330 if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
331 if (s > 0 && index > 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700332 index--;
333 } else if (s < 0)
334 index++;
335
336 out:
Koji Sato17c76b02009-04-06 19:01:24 -0700337 *indexp = index;
338
339 return s == 0;
340}
341
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900342/**
343 * nilfs_btree_node_broken - verify consistency of btree node
344 * @node: btree node block to be examined
345 * @size: node size (in bytes)
346 * @blocknr: block number
347 *
348 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
349 */
350static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
351 size_t size, sector_t blocknr)
352{
353 int level, flags, nchildren;
354 int ret = 0;
355
356 level = nilfs_btree_node_get_level(node);
357 flags = nilfs_btree_node_get_flags(node);
358 nchildren = nilfs_btree_node_get_nchildren(node);
359
360 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
361 level >= NILFS_BTREE_LEVEL_MAX ||
362 (flags & NILFS_BTREE_NODE_ROOT) ||
363 nchildren < 0 ||
364 nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
365 printk(KERN_CRIT "NILFS: bad btree node (blocknr=%llu): "
366 "level = %d, flags = 0x%x, nchildren = %d\n",
367 (unsigned long long)blocknr, level, flags, nchildren);
368 ret = 1;
369 }
370 return ret;
371}
372
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800373/**
374 * nilfs_btree_root_broken - verify consistency of btree root node
375 * @node: btree root node to be examined
376 * @ino: inode number
377 *
378 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
379 */
380static int nilfs_btree_root_broken(const struct nilfs_btree_node *node,
381 unsigned long ino)
382{
383 int level, flags, nchildren;
384 int ret = 0;
385
386 level = nilfs_btree_node_get_level(node);
387 flags = nilfs_btree_node_get_flags(node);
388 nchildren = nilfs_btree_node_get_nchildren(node);
389
390 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
Ryusuke Konishid8fd1502015-05-05 16:24:00 -0700391 level >= NILFS_BTREE_LEVEL_MAX ||
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800392 nchildren < 0 ||
393 nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) {
394 pr_crit("NILFS: bad btree root (inode number=%lu): level = %d, flags = 0x%x, nchildren = %d\n",
395 ino, level, flags, nchildren);
396 ret = 1;
397 }
398 return ret;
399}
400
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900401int nilfs_btree_broken_node_block(struct buffer_head *bh)
402{
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900403 int ret;
404
405 if (buffer_nilfs_checked(bh))
406 return 0;
407
408 ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900409 bh->b_size, bh->b_blocknr);
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900410 if (likely(!ret))
411 set_buffer_nilfs_checked(bh);
412 return ret;
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900413}
414
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900415static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900416nilfs_btree_get_root(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700417{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900418 return (struct nilfs_btree_node *)btree->b_u.u_data;
Koji Sato17c76b02009-04-06 19:01:24 -0700419}
420
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900421static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900422nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700423{
424 return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
425}
426
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900427static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900428nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700429{
430 return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
431}
432
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900433static int nilfs_btree_height(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700434{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900435 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700436}
437
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900438static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900439nilfs_btree_get_node(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700440 const struct nilfs_btree_path *path,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900441 int level, int *ncmaxp)
Koji Sato17c76b02009-04-06 19:01:24 -0700442{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900443 struct nilfs_btree_node *node;
444
445 if (level == nilfs_btree_height(btree) - 1) {
446 node = nilfs_btree_get_root(btree);
447 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
448 } else {
449 node = nilfs_btree_get_nonroot_node(path, level);
450 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
451 }
452 return node;
Koji Sato17c76b02009-04-06 19:01:24 -0700453}
454
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900455static int
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900456nilfs_btree_bad_node(struct nilfs_btree_node *node, int level)
457{
458 if (unlikely(nilfs_btree_node_get_level(node) != level)) {
459 dump_stack();
460 printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n",
461 nilfs_btree_node_get_level(node), level);
462 return 1;
463 }
464 return 0;
465}
466
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900467struct nilfs_btree_readahead_info {
468 struct nilfs_btree_node *node; /* parent node */
469 int max_ra_blocks; /* max nof blocks to read ahead */
470 int index; /* current index on the parent node */
471 int ncmax; /* nof children in the parent node */
472};
473
474static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
475 struct buffer_head **bhp,
476 const struct nilfs_btree_readahead_info *ra)
477{
478 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
479 struct buffer_head *bh, *ra_bh;
480 sector_t submit_ptr = 0;
481 int ret;
482
483 ret = nilfs_btnode_submit_block(btnc, ptr, 0, READ, &bh, &submit_ptr);
484 if (ret) {
485 if (ret != -EEXIST)
486 return ret;
487 goto out_check;
488 }
489
490 if (ra) {
491 int i, n;
492 __u64 ptr2;
493
494 /* read ahead sibling nodes */
495 for (n = ra->max_ra_blocks, i = ra->index + 1;
496 n > 0 && i < ra->ncmax; n--, i++) {
497 ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
498
499 ret = nilfs_btnode_submit_block(btnc, ptr2, 0, READA,
500 &ra_bh, &submit_ptr);
501 if (likely(!ret || ret == -EEXIST))
502 brelse(ra_bh);
503 else if (ret != -EBUSY)
504 break;
505 if (!buffer_locked(bh))
506 goto out_no_wait;
507 }
508 }
509
510 wait_on_buffer(bh);
511
512 out_no_wait:
513 if (!buffer_uptodate(bh)) {
514 brelse(bh);
515 return -EIO;
516 }
517
518 out_check:
519 if (nilfs_btree_broken_node_block(bh)) {
520 clear_buffer_uptodate(bh);
521 brelse(bh);
522 return -EINVAL;
523 }
524
525 *bhp = bh;
526 return 0;
527}
528
529static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
530 struct buffer_head **bhp)
531{
532 return __nilfs_btree_get_block(btree, ptr, bhp, NULL);
533}
534
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900535static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700536 struct nilfs_btree_path *path,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900537 __u64 key, __u64 *ptrp, int minlevel,
538 int readahead)
Koji Sato17c76b02009-04-06 19:01:24 -0700539{
540 struct nilfs_btree_node *node;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900541 struct nilfs_btree_readahead_info p, *ra;
Koji Sato17c76b02009-04-06 19:01:24 -0700542 __u64 ptr;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900543 int level, index, found, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700544
Koji Sato17c76b02009-04-06 19:01:24 -0700545 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900546 level = nilfs_btree_node_get_level(node);
547 if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700548 return -ENOENT;
549
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900550 found = nilfs_btree_node_lookup(node, key, &index);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900551 ptr = nilfs_btree_node_get_ptr(node, index,
552 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700553 path[level].bp_bh = NULL;
554 path[level].bp_index = index;
555
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900556 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900557
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900558 while (--level >= minlevel) {
559 ra = NULL;
560 if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) {
561 p.node = nilfs_btree_get_node(btree, path, level + 1,
562 &p.ncmax);
563 p.index = index;
564 p.max_ra_blocks = 7;
565 ra = &p;
566 }
567 ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh,
568 ra);
Koji Sato17c76b02009-04-06 19:01:24 -0700569 if (ret < 0)
570 return ret;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900571
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900572 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900573 if (nilfs_btree_bad_node(node, level))
574 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -0700575 if (!found)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900576 found = nilfs_btree_node_lookup(node, key, &index);
Koji Sato17c76b02009-04-06 19:01:24 -0700577 else
578 index = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900579 if (index < ncmax) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900580 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900581 } else {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700582 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
Koji Sato17c76b02009-04-06 19:01:24 -0700583 /* insert */
584 ptr = NILFS_BMAP_INVALID_PTR;
585 }
586 path[level].bp_index = index;
587 }
588 if (!found)
589 return -ENOENT;
590
591 if (ptrp != NULL)
592 *ptrp = ptr;
593
594 return 0;
595}
596
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900597static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700598 struct nilfs_btree_path *path,
599 __u64 *keyp, __u64 *ptrp)
600{
601 struct nilfs_btree_node *node;
602 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900603 int index, level, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700604
605 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900606 index = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700607 if (index < 0)
608 return -ENOENT;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900609 level = nilfs_btree_node_get_level(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900610 ptr = nilfs_btree_node_get_ptr(node, index,
611 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700612 path[level].bp_bh = NULL;
613 path[level].bp_index = index;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900614 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700615
616 for (level--; level > 0; level--) {
Ryusuke Konishif198dbb2009-05-22 01:07:13 +0900617 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700618 if (ret < 0)
619 return ret;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900620 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900621 if (nilfs_btree_bad_node(node, level))
622 return -EINVAL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900623 index = nilfs_btree_node_get_nchildren(node) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900624 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700625 path[level].bp_index = index;
626 }
627
628 if (keyp != NULL)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900629 *keyp = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700630 if (ptrp != NULL)
631 *ptrp = ptr;
632
633 return 0;
634}
635
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700636/**
637 * nilfs_btree_get_next_key - get next valid key from btree path array
638 * @btree: bmap struct of btree
639 * @path: array of nilfs_btree_path struct
640 * @minlevel: start level
641 * @nextkey: place to store the next valid key
642 *
643 * Return Value: If a next key was found, 0 is returned. Otherwise,
644 * -ENOENT is returned.
645 */
646static int nilfs_btree_get_next_key(const struct nilfs_bmap *btree,
647 const struct nilfs_btree_path *path,
648 int minlevel, __u64 *nextkey)
649{
650 struct nilfs_btree_node *node;
651 int maxlevel = nilfs_btree_height(btree) - 1;
652 int index, next_adj, level;
653
654 /* Next index is already set to bp_index for leaf nodes. */
655 next_adj = 0;
656 for (level = minlevel; level <= maxlevel; level++) {
657 if (level == maxlevel)
658 node = nilfs_btree_get_root(btree);
659 else
660 node = nilfs_btree_get_nonroot_node(path, level);
661
662 index = path[level].bp_index + next_adj;
663 if (index < nilfs_btree_node_get_nchildren(node)) {
664 /* Next key is in this node */
665 *nextkey = nilfs_btree_node_get_key(node, index);
666 return 0;
667 }
668 /* For non-leaf nodes, next index is stored at bp_index + 1. */
669 next_adj = 1;
670 }
671 return -ENOENT;
672}
673
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900674static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700675 __u64 key, int level, __u64 *ptrp)
676{
Koji Sato17c76b02009-04-06 19:01:24 -0700677 struct nilfs_btree_path *path;
Koji Sato17c76b02009-04-06 19:01:24 -0700678 int ret;
679
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900680 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -0700681 if (path == NULL)
682 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -0700683
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900684 ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700685
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900686 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -0700687
688 return ret;
689}
690
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900691static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900692 __u64 key, __u64 *ptrp, unsigned maxblocks)
693{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900694 struct nilfs_btree_path *path;
695 struct nilfs_btree_node *node;
696 struct inode *dat = NULL;
697 __u64 ptr, ptr2;
698 sector_t blocknr;
699 int level = NILFS_BTREE_LEVEL_NODE_MIN;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900700 int ret, cnt, index, maxlevel, ncmax;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900701 struct nilfs_btree_readahead_info p;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900702
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900703 path = nilfs_btree_alloc_path();
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900704 if (path == NULL)
705 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +0800706
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900707 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900708 if (ret < 0)
709 goto out;
710
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900711 if (NILFS_BMAP_USE_VBN(btree)) {
712 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900713 ret = nilfs_dat_translate(dat, ptr, &blocknr);
714 if (ret < 0)
715 goto out;
716 ptr = blocknr;
717 }
718 cnt = 1;
719 if (cnt == maxblocks)
720 goto end;
721
722 maxlevel = nilfs_btree_height(btree) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900723 node = nilfs_btree_get_node(btree, path, level, &ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900724 index = path[level].bp_index + 1;
725 for (;;) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900726 while (index < nilfs_btree_node_get_nchildren(node)) {
727 if (nilfs_btree_node_get_key(node, index) !=
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900728 key + cnt)
729 goto end;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900730 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900731 if (dat) {
732 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
733 if (ret < 0)
734 goto out;
735 ptr2 = blocknr;
736 }
737 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
738 goto end;
739 index++;
740 continue;
741 }
742 if (level == maxlevel)
743 break;
744
745 /* look-up right sibling node */
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900746 p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax);
747 p.index = path[level + 1].bp_index + 1;
748 p.max_ra_blocks = 7;
749 if (p.index >= nilfs_btree_node_get_nchildren(p.node) ||
750 nilfs_btree_node_get_key(p.node, p.index) != key + cnt)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900751 break;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900752 ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax);
753 path[level + 1].bp_index = p.index;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900754
755 brelse(path[level].bp_bh);
756 path[level].bp_bh = NULL;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900757
758 ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
759 &p);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900760 if (ret < 0)
761 goto out;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900762 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900763 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900764 index = 0;
765 path[level].bp_index = index;
766 }
767 end:
768 *ptrp = ptr;
769 ret = cnt;
770 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900771 nilfs_btree_free_path(path);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900772 return ret;
773}
774
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900775static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700776 struct nilfs_btree_path *path,
777 int level, __u64 key)
778{
779 if (level < nilfs_btree_height(btree) - 1) {
780 do {
Koji Sato17c76b02009-04-06 19:01:24 -0700781 nilfs_btree_node_set_key(
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900782 nilfs_btree_get_nonroot_node(path, level),
Koji Sato17c76b02009-04-06 19:01:24 -0700783 path[level].bp_index, key);
784 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900785 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700786 } while ((path[level].bp_index == 0) &&
787 (++level < nilfs_btree_height(btree) - 1));
788 }
789
790 /* root */
791 if (level == nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900792 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
Koji Sato17c76b02009-04-06 19:01:24 -0700793 path[level].bp_index, key);
794 }
795}
796
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900797static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700798 struct nilfs_btree_path *path,
799 int level, __u64 *keyp, __u64 *ptrp)
800{
801 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900802 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700803
804 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900805 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900806 ncblk = nilfs_btree_nchildren_per_block(btree);
807 nilfs_btree_node_insert(node, path[level].bp_index,
808 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700809 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900810 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700811
812 if (path[level].bp_index == 0)
813 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900814 nilfs_btree_node_get_key(node,
815 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700816 } else {
817 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900818 nilfs_btree_node_insert(node, path[level].bp_index,
819 *keyp, *ptrp,
820 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700821 }
822}
823
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900824static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700825 struct nilfs_btree_path *path,
826 int level, __u64 *keyp, __u64 *ptrp)
827{
828 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900829 int nchildren, lnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700830
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900831 node = nilfs_btree_get_nonroot_node(path, level);
832 left = nilfs_btree_get_sib_node(path, level);
833 nchildren = nilfs_btree_node_get_nchildren(node);
834 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900835 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700836 move = 0;
837
838 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
839 if (n > path[level].bp_index) {
840 /* move insert point */
841 n--;
842 move = 1;
843 }
844
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900845 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700846
847 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900848 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700849 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900850 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700851
Koji Sato17c76b02009-04-06 19:01:24 -0700852 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900853 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700854
855 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900856 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700857 path[level].bp_bh = path[level].bp_sib_bh;
858 path[level].bp_sib_bh = NULL;
859 path[level].bp_index += lnchildren;
860 path[level + 1].bp_index--;
861 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900862 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700863 path[level].bp_sib_bh = NULL;
864 path[level].bp_index -= n;
865 }
866
867 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
868}
869
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900870static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700871 struct nilfs_btree_path *path,
872 int level, __u64 *keyp, __u64 *ptrp)
873{
874 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900875 int nchildren, rnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700876
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900877 node = nilfs_btree_get_nonroot_node(path, level);
878 right = nilfs_btree_get_sib_node(path, level);
879 nchildren = nilfs_btree_node_get_nchildren(node);
880 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900881 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700882 move = 0;
883
884 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
885 if (n > nchildren - path[level].bp_index) {
886 /* move insert point */
887 n--;
888 move = 1;
889 }
890
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900891 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700892
893 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900894 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700895 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900896 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700897
Koji Sato17c76b02009-04-06 19:01:24 -0700898 path[level + 1].bp_index++;
899 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900900 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700901 path[level + 1].bp_index--;
902
903 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900904 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700905 path[level].bp_bh = path[level].bp_sib_bh;
906 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900907 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700908 path[level + 1].bp_index++;
909 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900910 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700911 path[level].bp_sib_bh = NULL;
912 }
913
914 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
915}
916
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900917static void nilfs_btree_split(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700918 struct nilfs_btree_path *path,
919 int level, __u64 *keyp, __u64 *ptrp)
920{
921 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900922 int nchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700923
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900924 node = nilfs_btree_get_nonroot_node(path, level);
925 right = nilfs_btree_get_sib_node(path, level);
926 nchildren = nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900927 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700928 move = 0;
929
930 n = (nchildren + 1) / 2;
931 if (n > nchildren - path[level].bp_index) {
932 n--;
933 move = 1;
934 }
935
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900936 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700937
938 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900939 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700940 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900941 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700942
Koji Sato17c76b02009-04-06 19:01:24 -0700943 if (move) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900944 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900945 nilfs_btree_node_insert(right, path[level].bp_index,
946 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700947
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900948 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700949 *ptrp = path[level].bp_newreq.bpr_ptr;
950
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900951 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700952 path[level].bp_bh = path[level].bp_sib_bh;
953 path[level].bp_sib_bh = NULL;
954 } else {
955 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
956
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900957 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700958 *ptrp = path[level].bp_newreq.bpr_ptr;
959
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900960 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700961 path[level].bp_sib_bh = NULL;
962 }
963
964 path[level + 1].bp_index++;
965}
966
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900967static void nilfs_btree_grow(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700968 struct nilfs_btree_path *path,
969 int level, __u64 *keyp, __u64 *ptrp)
970{
971 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900972 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700973
Koji Sato17c76b02009-04-06 19:01:24 -0700974 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900975 child = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900976 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700977
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900978 n = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -0700979
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900980 nilfs_btree_node_move_right(root, child, n,
981 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900982 nilfs_btree_node_set_level(root, level + 1);
Koji Sato17c76b02009-04-06 19:01:24 -0700983
984 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900985 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700986
Koji Sato17c76b02009-04-06 19:01:24 -0700987 path[level].bp_bh = path[level].bp_sib_bh;
988 path[level].bp_sib_bh = NULL;
989
990 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
991
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900992 *keyp = nilfs_btree_node_get_key(child, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700993 *ptrp = path[level].bp_newreq.bpr_ptr;
994}
995
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900996static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700997 const struct nilfs_btree_path *path)
998{
999 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001000 int level, ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001001
1002 if (path == NULL)
1003 return NILFS_BMAP_INVALID_PTR;
1004
1005 /* left sibling */
1006 level = NILFS_BTREE_LEVEL_NODE_MIN;
1007 if (path[level].bp_index > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001008 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1009 return nilfs_btree_node_get_ptr(node,
1010 path[level].bp_index - 1,
1011 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001012 }
1013
1014 /* parent */
1015 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
1016 if (level <= nilfs_btree_height(btree) - 1) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001017 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1018 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
1019 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001020 }
1021
1022 return NILFS_BMAP_INVALID_PTR;
1023}
1024
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001025static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001026 const struct nilfs_btree_path *path,
1027 __u64 key)
1028{
1029 __u64 ptr;
1030
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001031 ptr = nilfs_bmap_find_target_seq(btree, key);
Koji Sato17c76b02009-04-06 19:01:24 -07001032 if (ptr != NILFS_BMAP_INVALID_PTR)
1033 /* sequential access */
1034 return ptr;
1035 else {
1036 ptr = nilfs_btree_find_near(btree, path);
1037 if (ptr != NILFS_BMAP_INVALID_PTR)
1038 /* near */
1039 return ptr;
1040 }
1041 /* block group */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001042 return nilfs_bmap_find_target_in_group(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001043}
1044
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001045static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001046 struct nilfs_btree_path *path,
1047 int *levelp, __u64 key, __u64 ptr,
1048 struct nilfs_bmap_stats *stats)
1049{
1050 struct buffer_head *bh;
1051 struct nilfs_btree_node *node, *parent, *sib;
1052 __u64 sibptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001053 int pindex, level, ncmax, ncblk, ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001054 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001055
1056 stats->bs_nblocks = 0;
1057 level = NILFS_BTREE_LEVEL_DATA;
1058
1059 /* allocate a new ptr for data block */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001060 if (NILFS_BMAP_USE_VBN(btree)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001061 path[level].bp_newreq.bpr_ptr =
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001062 nilfs_btree_find_target_v(btree, path, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001063 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001064 }
Koji Sato17c76b02009-04-06 19:01:24 -07001065
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001066 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001067 if (ret < 0)
1068 goto err_out_data;
1069
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001070 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001071
Koji Sato17c76b02009-04-06 19:01:24 -07001072 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1073 level < nilfs_btree_height(btree) - 1;
1074 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001075 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001076 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001077 path[level].bp_op = nilfs_btree_do_insert;
1078 stats->bs_nblocks++;
1079 goto out;
1080 }
1081
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001082 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001083 pindex = path[level + 1].bp_index;
1084
1085 /* left sibling */
1086 if (pindex > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001087 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1088 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001089 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001090 if (ret < 0)
1091 goto err_out_child_node;
1092 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001093 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001094 path[level].bp_sib_bh = bh;
1095 path[level].bp_op = nilfs_btree_carry_left;
1096 stats->bs_nblocks++;
1097 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001098 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001099 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001100 }
Koji Sato17c76b02009-04-06 19:01:24 -07001101 }
1102
1103 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001104 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1105 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1106 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001107 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001108 if (ret < 0)
1109 goto err_out_child_node;
1110 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001111 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001112 path[level].bp_sib_bh = bh;
1113 path[level].bp_op = nilfs_btree_carry_right;
1114 stats->bs_nblocks++;
1115 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001116 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001117 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001118 }
Koji Sato17c76b02009-04-06 19:01:24 -07001119 }
1120
1121 /* split */
1122 path[level].bp_newreq.bpr_ptr =
1123 path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001124 ret = nilfs_bmap_prepare_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001125 &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001126 if (ret < 0)
1127 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001128 ret = nilfs_btree_get_new_block(btree,
1129 path[level].bp_newreq.bpr_ptr,
1130 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001131 if (ret < 0)
1132 goto err_out_curr_node;
1133
1134 stats->bs_nblocks++;
1135
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001136 sib = (struct nilfs_btree_node *)bh->b_data;
1137 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001138 path[level].bp_sib_bh = bh;
1139 path[level].bp_op = nilfs_btree_split;
1140 }
1141
1142 /* root */
1143 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001144 if (nilfs_btree_node_get_nchildren(node) <
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001145 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
Koji Sato17c76b02009-04-06 19:01:24 -07001146 path[level].bp_op = nilfs_btree_do_insert;
1147 stats->bs_nblocks++;
1148 goto out;
1149 }
1150
1151 /* grow */
1152 path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001153 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001154 if (ret < 0)
1155 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001156 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1157 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001158 if (ret < 0)
1159 goto err_out_curr_node;
1160
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001161 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1162 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001163 path[level].bp_sib_bh = bh;
1164 path[level].bp_op = nilfs_btree_grow;
1165
1166 level++;
1167 path[level].bp_op = nilfs_btree_do_insert;
1168
1169 /* a newly-created node block and a data block are added */
1170 stats->bs_nblocks += 2;
1171
1172 /* success */
1173 out:
1174 *levelp = level;
1175 return ret;
1176
1177 /* error */
1178 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001179 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001180 err_out_child_node:
1181 for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001182 nilfs_btnode_delete(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001183 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001184
1185 }
1186
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001187 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001188 err_out_data:
1189 *levelp = level;
1190 stats->bs_nblocks = 0;
1191 return ret;
1192}
1193
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001194static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001195 struct nilfs_btree_path *path,
1196 int maxlevel, __u64 key, __u64 ptr)
1197{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001198 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001199 int level;
1200
1201 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1202 ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001203 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001204 nilfs_bmap_set_target_v(btree, key, ptr);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001205 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001206 }
Koji Sato17c76b02009-04-06 19:01:24 -07001207
1208 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001209 nilfs_bmap_commit_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001210 &path[level - 1].bp_newreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001211 path[level].bp_op(btree, path, level, &key, &ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001212 }
1213
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001214 if (!nilfs_bmap_dirty(btree))
1215 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001216}
1217
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001218static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
Koji Sato17c76b02009-04-06 19:01:24 -07001219{
Koji Sato17c76b02009-04-06 19:01:24 -07001220 struct nilfs_btree_path *path;
1221 struct nilfs_bmap_stats stats;
1222 int level, ret;
1223
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001224 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001225 if (path == NULL)
1226 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001227
1228 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001229 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001230 if (ret != -ENOENT) {
1231 if (ret == 0)
1232 ret = -EEXIST;
1233 goto out;
1234 }
1235
1236 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1237 if (ret < 0)
1238 goto out;
1239 nilfs_btree_commit_insert(btree, path, level, key, ptr);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001240 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001241
1242 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001243 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001244 return ret;
1245}
1246
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001247static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001248 struct nilfs_btree_path *path,
1249 int level, __u64 *keyp, __u64 *ptrp)
1250{
1251 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001252 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001253
1254 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001255 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001256 ncblk = nilfs_btree_nchildren_per_block(btree);
1257 nilfs_btree_node_delete(node, path[level].bp_index,
1258 keyp, ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001259 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001260 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001261 if (path[level].bp_index == 0)
1262 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001263 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001264 } else {
1265 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001266 nilfs_btree_node_delete(node, path[level].bp_index,
1267 keyp, ptrp,
1268 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -07001269 }
1270}
1271
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001272static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001273 struct nilfs_btree_path *path,
1274 int level, __u64 *keyp, __u64 *ptrp)
1275{
1276 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001277 int nchildren, lnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001278
1279 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1280
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001281 node = nilfs_btree_get_nonroot_node(path, level);
1282 left = nilfs_btree_get_sib_node(path, level);
1283 nchildren = nilfs_btree_node_get_nchildren(node);
1284 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001285 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001286
1287 n = (nchildren + lnchildren) / 2 - nchildren;
1288
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001289 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001290
1291 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001292 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001293 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001294 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001295
Koji Sato17c76b02009-04-06 19:01:24 -07001296 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001297 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001298
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001299 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001300 path[level].bp_sib_bh = NULL;
1301 path[level].bp_index += n;
1302}
1303
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001304static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001305 struct nilfs_btree_path *path,
1306 int level, __u64 *keyp, __u64 *ptrp)
1307{
1308 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001309 int nchildren, rnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001310
1311 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1312
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001313 node = nilfs_btree_get_nonroot_node(path, level);
1314 right = nilfs_btree_get_sib_node(path, level);
1315 nchildren = nilfs_btree_node_get_nchildren(node);
1316 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001317 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001318
1319 n = (nchildren + rnchildren) / 2 - nchildren;
1320
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001321 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001322
1323 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001324 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001325 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001326 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001327
Koji Sato17c76b02009-04-06 19:01:24 -07001328 path[level + 1].bp_index++;
1329 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001330 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001331 path[level + 1].bp_index--;
1332
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001333 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001334 path[level].bp_sib_bh = NULL;
1335}
1336
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001337static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001338 struct nilfs_btree_path *path,
1339 int level, __u64 *keyp, __u64 *ptrp)
1340{
1341 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001342 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001343
1344 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1345
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001346 node = nilfs_btree_get_nonroot_node(path, level);
1347 left = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001348 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001349
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001350 n = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001351
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001352 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001353
1354 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001355 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001356
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001357 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001358 path[level].bp_bh = path[level].bp_sib_bh;
1359 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001360 path[level].bp_index += nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -07001361}
1362
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001363static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001364 struct nilfs_btree_path *path,
1365 int level, __u64 *keyp, __u64 *ptrp)
1366{
1367 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001368 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001369
1370 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1371
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001372 node = nilfs_btree_get_nonroot_node(path, level);
1373 right = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001374 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001375
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001376 n = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -07001377
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001378 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001379
1380 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001381 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001382
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001383 nilfs_btnode_delete(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001384 path[level].bp_sib_bh = NULL;
1385 path[level + 1].bp_index++;
1386}
1387
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001388static void nilfs_btree_shrink(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001389 struct nilfs_btree_path *path,
1390 int level, __u64 *keyp, __u64 *ptrp)
1391{
1392 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001393 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001394
1395 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1396
Koji Sato17c76b02009-04-06 19:01:24 -07001397 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001398 child = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001399 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001400
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001401 nilfs_btree_node_delete(root, 0, NULL, NULL,
1402 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001403 nilfs_btree_node_set_level(root, level);
1404 n = nilfs_btree_node_get_nchildren(child);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001405 nilfs_btree_node_move_left(root, child, n,
1406 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001407
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001408 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001409 path[level].bp_bh = NULL;
1410}
1411
Ryusuke Konishid4099052011-05-25 23:00:27 +09001412static void nilfs_btree_nop(struct nilfs_bmap *btree,
1413 struct nilfs_btree_path *path,
1414 int level, __u64 *keyp, __u64 *ptrp)
1415{
1416}
Koji Sato17c76b02009-04-06 19:01:24 -07001417
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001418static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001419 struct nilfs_btree_path *path,
1420 int *levelp,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001421 struct nilfs_bmap_stats *stats,
1422 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001423{
1424 struct buffer_head *bh;
1425 struct nilfs_btree_node *node, *parent, *sib;
1426 __u64 sibptr;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001427 int pindex, dindex, level, ncmin, ncmax, ncblk, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001428
1429 ret = 0;
1430 stats->bs_nblocks = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001431 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001432 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001433
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001434 for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index;
Koji Sato17c76b02009-04-06 19:01:24 -07001435 level < nilfs_btree_height(btree) - 1;
1436 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001437 node = nilfs_btree_get_nonroot_node(path, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001438 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001439 nilfs_btree_node_get_ptr(node, dindex, ncblk);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001440 ret = nilfs_bmap_prepare_end_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001441 &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001442 if (ret < 0)
1443 goto err_out_child_node;
Koji Sato17c76b02009-04-06 19:01:24 -07001444
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001445 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001446 path[level].bp_op = nilfs_btree_do_delete;
1447 stats->bs_nblocks++;
1448 goto out;
1449 }
1450
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001451 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001452 pindex = path[level + 1].bp_index;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001453 dindex = pindex;
Koji Sato17c76b02009-04-06 19:01:24 -07001454
1455 if (pindex > 0) {
1456 /* left sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001457 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1458 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001459 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001460 if (ret < 0)
1461 goto err_out_curr_node;
1462 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001463 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001464 path[level].bp_sib_bh = bh;
1465 path[level].bp_op = nilfs_btree_borrow_left;
1466 stats->bs_nblocks++;
1467 goto out;
1468 } else {
1469 path[level].bp_sib_bh = bh;
1470 path[level].bp_op = nilfs_btree_concat_left;
1471 stats->bs_nblocks++;
1472 /* continue; */
1473 }
1474 } else if (pindex <
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001475 nilfs_btree_node_get_nchildren(parent) - 1) {
Koji Sato17c76b02009-04-06 19:01:24 -07001476 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001477 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1478 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001479 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001480 if (ret < 0)
1481 goto err_out_curr_node;
1482 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001483 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001484 path[level].bp_sib_bh = bh;
1485 path[level].bp_op = nilfs_btree_borrow_right;
1486 stats->bs_nblocks++;
1487 goto out;
1488 } else {
1489 path[level].bp_sib_bh = bh;
1490 path[level].bp_op = nilfs_btree_concat_right;
1491 stats->bs_nblocks++;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001492 /*
1493 * When merging right sibling node
1494 * into the current node, pointer to
1495 * the right sibling node must be
1496 * terminated instead. The adjustment
1497 * below is required for that.
1498 */
1499 dindex = pindex + 1;
Koji Sato17c76b02009-04-06 19:01:24 -07001500 /* continue; */
1501 }
1502 } else {
1503 /* no siblings */
1504 /* the only child of the root node */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001505 WARN_ON(level != nilfs_btree_height(btree) - 2);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001506 if (nilfs_btree_node_get_nchildren(node) - 1 <=
Koji Sato17c76b02009-04-06 19:01:24 -07001507 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1508 path[level].bp_op = nilfs_btree_shrink;
1509 stats->bs_nblocks += 2;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001510 level++;
1511 path[level].bp_op = nilfs_btree_nop;
1512 goto shrink_root_child;
Koji Sato17c76b02009-04-06 19:01:24 -07001513 } else {
1514 path[level].bp_op = nilfs_btree_do_delete;
1515 stats->bs_nblocks++;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001516 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -07001517 }
Koji Sato17c76b02009-04-06 19:01:24 -07001518 }
1519 }
1520
Ryusuke Konishid4099052011-05-25 23:00:27 +09001521 /* child of the root node is deleted */
1522 path[level].bp_op = nilfs_btree_do_delete;
1523 stats->bs_nblocks++;
1524
1525shrink_root_child:
Koji Sato17c76b02009-04-06 19:01:24 -07001526 node = nilfs_btree_get_root(btree);
1527 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001528 nilfs_btree_node_get_ptr(node, dindex,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001529 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001530
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001531 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001532 if (ret < 0)
1533 goto err_out_child_node;
1534
Koji Sato17c76b02009-04-06 19:01:24 -07001535 /* success */
1536 out:
1537 *levelp = level;
1538 return ret;
1539
1540 /* error */
1541 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001542 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001543 err_out_child_node:
1544 for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001545 brelse(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001546 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001547 }
1548 *levelp = level;
1549 stats->bs_nblocks = 0;
1550 return ret;
1551}
1552
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001553static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001554 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001555 int maxlevel, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001556{
1557 int level;
1558
1559 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001560 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001561 path[level].bp_op(btree, path, level, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001562 }
1563
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001564 if (!nilfs_bmap_dirty(btree))
1565 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001566}
1567
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001568static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001569
1570{
Koji Sato17c76b02009-04-06 19:01:24 -07001571 struct nilfs_btree_path *path;
1572 struct nilfs_bmap_stats stats;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001573 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001574 int level, ret;
1575
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001576 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001577 if (path == NULL)
1578 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +08001579
Koji Sato17c76b02009-04-06 19:01:24 -07001580 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001581 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001582 if (ret < 0)
1583 goto out;
1584
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001585
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001586 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001587
1588 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001589 if (ret < 0)
1590 goto out;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001591 nilfs_btree_commit_delete(btree, path, level, dat);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001592 nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001593
1594out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001595 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001596 return ret;
1597}
1598
Ryusuke Konishi5b203842015-04-16 12:46:36 -07001599static int nilfs_btree_seek_key(const struct nilfs_bmap *btree, __u64 start,
1600 __u64 *keyp)
1601{
1602 struct nilfs_btree_path *path;
1603 const int minlevel = NILFS_BTREE_LEVEL_NODE_MIN;
1604 int ret;
1605
1606 path = nilfs_btree_alloc_path();
1607 if (!path)
1608 return -ENOMEM;
1609
1610 ret = nilfs_btree_do_lookup(btree, path, start, NULL, minlevel, 0);
1611 if (!ret)
1612 *keyp = start;
1613 else if (ret == -ENOENT)
1614 ret = nilfs_btree_get_next_key(btree, path, minlevel, keyp);
1615
1616 nilfs_btree_free_path(path);
1617 return ret;
1618}
1619
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001620static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
Koji Sato17c76b02009-04-06 19:01:24 -07001621{
Koji Sato17c76b02009-04-06 19:01:24 -07001622 struct nilfs_btree_path *path;
1623 int ret;
1624
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001625 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001626 if (path == NULL)
1627 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001628
1629 ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
1630
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001631 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001632
1633 return ret;
1634}
1635
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001636static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001637{
1638 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001639 struct nilfs_btree_node *root, *node;
1640 __u64 maxkey, nextmaxkey;
1641 __u64 ptr;
1642 int nchildren, ret;
1643
Koji Sato17c76b02009-04-06 19:01:24 -07001644 root = nilfs_btree_get_root(btree);
1645 switch (nilfs_btree_height(btree)) {
1646 case 2:
1647 bh = NULL;
1648 node = root;
1649 break;
1650 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001651 nchildren = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -07001652 if (nchildren > 1)
1653 return 0;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001654 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1655 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001656 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001657 if (ret < 0)
1658 return ret;
1659 node = (struct nilfs_btree_node *)bh->b_data;
1660 break;
1661 default:
1662 return 0;
1663 }
1664
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001665 nchildren = nilfs_btree_node_get_nchildren(node);
1666 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
Koji Sato17c76b02009-04-06 19:01:24 -07001667 nextmaxkey = (nchildren > 1) ?
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001668 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
Koji Sato17c76b02009-04-06 19:01:24 -07001669 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001670 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001671
Ryusuke Konishi30333422009-05-24 00:09:44 +09001672 return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
Koji Sato17c76b02009-04-06 19:01:24 -07001673}
1674
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001675static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001676 __u64 *keys, __u64 *ptrs, int nitems)
1677{
1678 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001679 struct nilfs_btree_node *node, *root;
1680 __le64 *dkeys;
1681 __le64 *dptrs;
1682 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001683 int nchildren, ncmax, i, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001684
Koji Sato17c76b02009-04-06 19:01:24 -07001685 root = nilfs_btree_get_root(btree);
1686 switch (nilfs_btree_height(btree)) {
1687 case 2:
1688 bh = NULL;
1689 node = root;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001690 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
Koji Sato17c76b02009-04-06 19:01:24 -07001691 break;
1692 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001693 nchildren = nilfs_btree_node_get_nchildren(root);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001694 WARN_ON(nchildren > 1);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001695 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1696 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001697 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001698 if (ret < 0)
1699 return ret;
1700 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001701 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001702 break;
1703 default:
1704 node = NULL;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001705 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -07001706 }
1707
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001708 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001709 if (nchildren < nitems)
1710 nitems = nchildren;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001711 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001712 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001713 for (i = 0; i < nitems; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09001714 keys[i] = le64_to_cpu(dkeys[i]);
1715 ptrs[i] = le64_to_cpu(dptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -07001716 }
1717
1718 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001719 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001720
1721 return nitems;
1722}
1723
1724static int
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001725nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
Koji Sato17c76b02009-04-06 19:01:24 -07001726 union nilfs_bmap_ptr_req *dreq,
1727 union nilfs_bmap_ptr_req *nreq,
1728 struct buffer_head **bhp,
1729 struct nilfs_bmap_stats *stats)
1730{
1731 struct buffer_head *bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001732 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001733 int ret;
1734
Koji Sato17c76b02009-04-06 19:01:24 -07001735 stats->bs_nblocks = 0;
1736
1737 /* for data */
1738 /* cannot find near ptr */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001739 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001740 dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001741 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001742 }
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001743
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001744 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001745 if (ret < 0)
1746 return ret;
1747
1748 *bhp = NULL;
1749 stats->bs_nblocks++;
1750 if (nreq != NULL) {
1751 nreq->bpr_ptr = dreq->bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001752 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001753 if (ret < 0)
1754 goto err_out_dreq;
1755
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001756 ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001757 if (ret < 0)
1758 goto err_out_nreq;
1759
1760 *bhp = bh;
1761 stats->bs_nblocks++;
1762 }
1763
1764 /* success */
1765 return 0;
1766
1767 /* error */
1768 err_out_nreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001769 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001770 err_out_dreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001771 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001772 stats->bs_nblocks = 0;
1773 return ret;
1774
1775}
1776
1777static void
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001778nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001779 __u64 key, __u64 ptr,
1780 const __u64 *keys, const __u64 *ptrs,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001781 int n,
Koji Sato17c76b02009-04-06 19:01:24 -07001782 union nilfs_bmap_ptr_req *dreq,
1783 union nilfs_bmap_ptr_req *nreq,
1784 struct buffer_head *bh)
1785{
Koji Sato17c76b02009-04-06 19:01:24 -07001786 struct nilfs_btree_node *node;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001787 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001788 __u64 tmpptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001789 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001790
1791 /* free resources */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001792 if (btree->b_ops->bop_clear != NULL)
1793 btree->b_ops->bop_clear(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001794
1795 /* ptr must be a pointer to a buffer head. */
1796 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1797
1798 /* convert and insert */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001799 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi957ed602015-02-27 15:51:56 -08001800 __nilfs_btree_init(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001801 if (nreq != NULL) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001802 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1803 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001804
1805 /* create child node at level 1 */
Koji Sato17c76b02009-04-06 19:01:24 -07001806 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001807 ncblk = nilfs_btree_nchildren_per_block(btree);
1808 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1809 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001810 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001811 mark_buffer_dirty(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001812 if (!nilfs_bmap_dirty(btree))
1813 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001814
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001815 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001816
1817 /* create root node at level 2 */
1818 node = nilfs_btree_get_root(btree);
1819 tmpptr = nreq->bpr_ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001820 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1821 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1822 &keys[0], &tmpptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001823 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001824 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001825
1826 /* create root node at level 1 */
1827 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001828 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1829 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1830 keys, ptrs);
1831 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1832 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001833 if (!nilfs_bmap_dirty(btree))
1834 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001835 }
1836
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001837 if (NILFS_BMAP_USE_VBN(btree))
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001838 nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001839}
1840
1841/**
1842 * nilfs_btree_convert_and_insert -
1843 * @bmap:
1844 * @key:
1845 * @ptr:
1846 * @keys:
1847 * @ptrs:
1848 * @n:
Koji Sato17c76b02009-04-06 19:01:24 -07001849 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001850int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001851 __u64 key, __u64 ptr,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001852 const __u64 *keys, const __u64 *ptrs, int n)
Koji Sato17c76b02009-04-06 19:01:24 -07001853{
Ryusuke Konishi4f050282015-11-06 16:32:16 -08001854 struct buffer_head *bh = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001855 union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
1856 struct nilfs_bmap_stats stats;
1857 int ret;
1858
1859 if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1860 di = &dreq;
1861 ni = NULL;
1862 } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001863 1 << btree->b_inode->i_blkbits)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001864 di = &dreq;
1865 ni = &nreq;
1866 } else {
1867 di = NULL;
1868 ni = NULL;
1869 BUG();
1870 }
1871
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001872 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
Koji Sato17c76b02009-04-06 19:01:24 -07001873 &stats);
1874 if (ret < 0)
1875 return ret;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001876 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001877 di, ni, bh);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001878 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001879 return 0;
1880}
1881
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001882static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001883 struct nilfs_btree_path *path,
1884 int level,
1885 struct buffer_head *bh)
1886{
1887 while ((++level < nilfs_btree_height(btree) - 1) &&
1888 !buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001889 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001890
1891 return 0;
1892}
1893
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001894static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001895 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001896 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001897{
1898 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001899 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001900
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001901 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001902 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001903 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1904 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001905 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001906 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1907 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001908 if (ret < 0)
1909 return ret;
1910
1911 if (buffer_nilfs_node(path[level].bp_bh)) {
1912 path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
1913 path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
1914 path[level].bp_ctxt.bh = path[level].bp_bh;
1915 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001916 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001917 &path[level].bp_ctxt);
1918 if (ret < 0) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001919 nilfs_dat_abort_update(dat,
1920 &path[level].bp_oldreq.bpr_req,
1921 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001922 return ret;
1923 }
1924 }
1925
1926 return 0;
1927}
1928
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001929static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001930 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001931 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001932{
1933 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001934 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001935
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001936 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1937 &path[level].bp_newreq.bpr_req,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001938 btree->b_ptr_type == NILFS_BMAP_PTR_VS);
Koji Sato17c76b02009-04-06 19:01:24 -07001939
1940 if (buffer_nilfs_node(path[level].bp_bh)) {
1941 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001942 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001943 &path[level].bp_ctxt);
1944 path[level].bp_bh = path[level].bp_ctxt.bh;
1945 }
1946 set_buffer_nilfs_volatile(path[level].bp_bh);
1947
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001948 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1949 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1950 path[level].bp_newreq.bpr_ptr, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001951}
1952
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001953static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001954 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001955 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001956{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001957 nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
1958 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001959 if (buffer_nilfs_node(path[level].bp_bh))
1960 nilfs_btnode_abort_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001961 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001962 &path[level].bp_ctxt);
1963}
1964
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001965static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001966 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001967 int minlevel, int *maxlevelp,
1968 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001969{
1970 int level, ret;
1971
1972 level = minlevel;
1973 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001974 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001975 if (ret < 0)
1976 return ret;
1977 }
1978 while ((++level < nilfs_btree_height(btree) - 1) &&
1979 !buffer_dirty(path[level].bp_bh)) {
1980
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001981 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001982 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001983 if (ret < 0)
1984 goto out;
1985 }
1986
1987 /* success */
Koji Sato17c76b02009-04-06 19:01:24 -07001988 *maxlevelp = level - 1;
1989 return 0;
1990
1991 /* error */
1992 out:
1993 while (--level > minlevel)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001994 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001995 if (!buffer_nilfs_volatile(path[level].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001996 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001997 return ret;
1998}
1999
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002000static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002001 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002002 int minlevel, int maxlevel,
2003 struct buffer_head *bh,
2004 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07002005{
2006 int level;
2007
2008 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002009 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002010
2011 for (level = minlevel + 1; level <= maxlevel; level++)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002012 nilfs_btree_commit_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002013}
2014
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002015static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002016 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002017 int level, struct buffer_head *bh)
Koji Sato17c76b02009-04-06 19:01:24 -07002018{
Li Hong308f4412010-04-02 18:40:39 +08002019 int maxlevel = 0, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002020 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002021 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002022 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002023 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07002024
2025 get_bh(bh);
2026 path[level].bp_bh = bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002027 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
2028 dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002029 if (ret < 0)
2030 goto out;
2031
2032 if (buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002033 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2034 ptr = nilfs_btree_node_get_ptr(parent,
2035 path[level + 1].bp_index,
2036 ncmax);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002037 ret = nilfs_dat_mark_dirty(dat, ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07002038 if (ret < 0)
2039 goto out;
2040 }
2041
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002042 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002043
2044 out:
2045 brelse(path[level].bp_bh);
2046 path[level].bp_bh = NULL;
2047 return ret;
2048}
2049
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002050static int nilfs_btree_propagate(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002051 struct buffer_head *bh)
2052{
Koji Sato17c76b02009-04-06 19:01:24 -07002053 struct nilfs_btree_path *path;
2054 struct nilfs_btree_node *node;
2055 __u64 key;
2056 int level, ret;
2057
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002058 WARN_ON(!buffer_dirty(bh));
Koji Sato17c76b02009-04-06 19:01:24 -07002059
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002060 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002061 if (path == NULL)
2062 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002063
2064 if (buffer_nilfs_node(bh)) {
2065 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002066 key = nilfs_btree_node_get_key(node, 0);
2067 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002068 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002069 key = nilfs_bmap_data_get_key(btree, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002070 level = NILFS_BTREE_LEVEL_DATA;
2071 }
2072
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002073 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002074 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002075 if (unlikely(ret == -ENOENT))
Koji Sato17c76b02009-04-06 19:01:24 -07002076 printk(KERN_CRIT "%s: key = %llu, level == %d\n",
2077 __func__, (unsigned long long)key, level);
Koji Sato17c76b02009-04-06 19:01:24 -07002078 goto out;
2079 }
2080
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002081 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002082 nilfs_btree_propagate_v(btree, path, level, bh) :
2083 nilfs_btree_propagate_p(btree, path, level, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002084
2085 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002086 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002087
2088 return ret;
2089}
2090
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002091static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002092 struct buffer_head *bh)
2093{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002094 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002095}
2096
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002097static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002098 struct list_head *lists,
2099 struct buffer_head *bh)
2100{
2101 struct list_head *head;
2102 struct buffer_head *cbh;
2103 struct nilfs_btree_node *node, *cnode;
2104 __u64 key, ckey;
2105 int level;
2106
2107 get_bh(bh);
2108 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002109 key = nilfs_btree_node_get_key(node, 0);
2110 level = nilfs_btree_node_get_level(node);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002111 if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
2112 level >= NILFS_BTREE_LEVEL_MAX) {
2113 dump_stack();
2114 printk(KERN_WARNING
2115 "%s: invalid btree level: %d (key=%llu, ino=%lu, "
2116 "blocknr=%llu)\n",
2117 __func__, level, (unsigned long long)key,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002118 NILFS_BMAP_I(btree)->vfs_inode.i_ino,
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002119 (unsigned long long)bh->b_blocknr);
2120 return;
2121 }
2122
Koji Sato17c76b02009-04-06 19:01:24 -07002123 list_for_each(head, &lists[level]) {
2124 cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
2125 cnode = (struct nilfs_btree_node *)cbh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002126 ckey = nilfs_btree_node_get_key(cnode, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002127 if (key < ckey)
2128 break;
2129 }
2130 list_add_tail(&bh->b_assoc_buffers, head);
2131}
2132
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002133static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002134 struct list_head *listp)
2135{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002136 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
Koji Sato17c76b02009-04-06 19:01:24 -07002137 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2138 struct pagevec pvec;
2139 struct buffer_head *bh, *head;
2140 pgoff_t index = 0;
2141 int level, i;
2142
2143 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2144 level < NILFS_BTREE_LEVEL_MAX;
2145 level++)
2146 INIT_LIST_HEAD(&lists[level]);
2147
2148 pagevec_init(&pvec, 0);
2149
2150 while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
2151 PAGEVEC_SIZE)) {
2152 for (i = 0; i < pagevec_count(&pvec); i++) {
2153 bh = head = page_buffers(pvec.pages[i]);
2154 do {
2155 if (buffer_dirty(bh))
2156 nilfs_btree_add_dirty_buffer(btree,
2157 lists, bh);
2158 } while ((bh = bh->b_this_page) != head);
2159 }
2160 pagevec_release(&pvec);
2161 cond_resched();
2162 }
2163
2164 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2165 level < NILFS_BTREE_LEVEL_MAX;
2166 level++)
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002167 list_splice_tail(&lists[level], listp);
Koji Sato17c76b02009-04-06 19:01:24 -07002168}
2169
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002170static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002171 struct nilfs_btree_path *path,
2172 int level,
2173 struct buffer_head **bh,
2174 sector_t blocknr,
2175 union nilfs_binfo *binfo)
2176{
2177 struct nilfs_btree_node *parent;
2178 __u64 key;
2179 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002180 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002181
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002182 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2183 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2184 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002185 if (buffer_nilfs_node(*bh)) {
2186 path[level].bp_ctxt.oldkey = ptr;
2187 path[level].bp_ctxt.newkey = blocknr;
2188 path[level].bp_ctxt.bh = *bh;
2189 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002190 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002191 &path[level].bp_ctxt);
2192 if (ret < 0)
2193 return ret;
2194 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002195 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002196 &path[level].bp_ctxt);
2197 *bh = path[level].bp_ctxt.bh;
2198 }
2199
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002200 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2201 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002202
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002203 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002204 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002205 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002206 binfo->bi_dat.bi_level = level;
2207
2208 return 0;
2209}
2210
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002211static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002212 struct nilfs_btree_path *path,
2213 int level,
2214 struct buffer_head **bh,
2215 sector_t blocknr,
2216 union nilfs_binfo *binfo)
2217{
2218 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002219 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002220 __u64 key;
2221 __u64 ptr;
2222 union nilfs_bmap_ptr_req req;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002223 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002224
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002225 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2226 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2227 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002228 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002229 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2230 if (ret < 0)
Koji Sato17c76b02009-04-06 19:01:24 -07002231 return ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002232 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002233
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002234 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002235 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002236 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
2237 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002238
2239 return 0;
2240}
2241
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002242static int nilfs_btree_assign(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002243 struct buffer_head **bh,
2244 sector_t blocknr,
2245 union nilfs_binfo *binfo)
2246{
Koji Sato17c76b02009-04-06 19:01:24 -07002247 struct nilfs_btree_path *path;
2248 struct nilfs_btree_node *node;
2249 __u64 key;
2250 int level, ret;
2251
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002252 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002253 if (path == NULL)
2254 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002255
2256 if (buffer_nilfs_node(*bh)) {
2257 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002258 key = nilfs_btree_node_get_key(node, 0);
2259 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002260 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002261 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002262 level = NILFS_BTREE_LEVEL_DATA;
2263 }
2264
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002265 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002266 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002267 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002268 goto out;
2269 }
2270
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002271 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002272 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2273 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
Koji Sato17c76b02009-04-06 19:01:24 -07002274
2275 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002276 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002277
2278 return ret;
2279}
2280
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002281static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002282 struct buffer_head **bh,
2283 sector_t blocknr,
2284 union nilfs_binfo *binfo)
2285{
Koji Sato17c76b02009-04-06 19:01:24 -07002286 struct nilfs_btree_node *node;
2287 __u64 key;
2288 int ret;
2289
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002290 ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002291 blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002292 if (ret < 0)
2293 return ret;
2294
2295 if (buffer_nilfs_node(*bh)) {
2296 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002297 key = nilfs_btree_node_get_key(node, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002298 } else
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002299 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002300
2301 /* on-disk format */
2302 binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002303 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002304
2305 return 0;
2306}
2307
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002308static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
Koji Sato17c76b02009-04-06 19:01:24 -07002309{
2310 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07002311 struct nilfs_btree_path *path;
2312 __u64 ptr;
2313 int ret;
2314
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002315 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002316 if (path == NULL)
2317 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002318
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002319 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002320 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002321 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002322 goto out;
2323 }
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09002324 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002325 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002326 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002327 goto out;
2328 }
2329
2330 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09002331 mark_buffer_dirty(bh);
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09002332 brelse(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002333 if (!nilfs_bmap_dirty(btree))
2334 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002335
2336 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002337 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002338 return ret;
2339}
2340
2341static const struct nilfs_bmap_operations nilfs_btree_ops = {
2342 .bop_lookup = nilfs_btree_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002343 .bop_lookup_contig = nilfs_btree_lookup_contig,
Koji Sato17c76b02009-04-06 19:01:24 -07002344 .bop_insert = nilfs_btree_insert,
2345 .bop_delete = nilfs_btree_delete,
2346 .bop_clear = NULL,
2347
2348 .bop_propagate = nilfs_btree_propagate,
2349
2350 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2351
2352 .bop_assign = nilfs_btree_assign,
2353 .bop_mark = nilfs_btree_mark,
2354
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002355 .bop_seek_key = nilfs_btree_seek_key,
Koji Sato17c76b02009-04-06 19:01:24 -07002356 .bop_last_key = nilfs_btree_last_key,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002357
Koji Sato17c76b02009-04-06 19:01:24 -07002358 .bop_check_insert = NULL,
2359 .bop_check_delete = nilfs_btree_check_delete,
2360 .bop_gather_data = nilfs_btree_gather_data,
2361};
2362
2363static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
2364 .bop_lookup = NULL,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002365 .bop_lookup_contig = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002366 .bop_insert = NULL,
2367 .bop_delete = NULL,
2368 .bop_clear = NULL,
2369
2370 .bop_propagate = nilfs_btree_propagate_gc,
2371
2372 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2373
2374 .bop_assign = nilfs_btree_assign_gc,
2375 .bop_mark = NULL,
2376
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002377 .bop_seek_key = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002378 .bop_last_key = NULL,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002379
Koji Sato17c76b02009-04-06 19:01:24 -07002380 .bop_check_insert = NULL,
2381 .bop_check_delete = NULL,
2382 .bop_gather_data = NULL,
2383};
2384
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002385static void __nilfs_btree_init(struct nilfs_bmap *bmap)
Koji Sato17c76b02009-04-06 19:01:24 -07002386{
Koji Sato17c76b02009-04-06 19:01:24 -07002387 bmap->b_ops = &nilfs_btree_ops;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002388 bmap->b_nchildren_per_block =
2389 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002390}
2391
2392int nilfs_btree_init(struct nilfs_bmap *bmap)
2393{
2394 int ret = 0;
2395
2396 __nilfs_btree_init(bmap);
2397
2398 if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap),
2399 bmap->b_inode->i_ino))
2400 ret = -EIO;
2401 return ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002402}
2403
2404void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
2405{
Koji Sato17c76b02009-04-06 19:01:24 -07002406 bmap->b_ops = &nilfs_btree_ops_gc;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002407 bmap->b_nchildren_per_block =
2408 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002409}