blob: f42ab57201e7b6a702a08a03a8baf49ff54b1383 [file] [log] [blame]
Ryusuke Konishiae980432018-09-04 15:46:30 -07001// SPDX-License-Identifier: GPL-2.0+
Koji Sato17c76b02009-04-06 19:01:24 -07002/*
3 * btree.c - NILFS B-tree.
4 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -07007 * Written by Koji Sato.
Koji Sato17c76b02009-04-06 19:01:24 -07008 */
9
10#include <linux/slab.h>
11#include <linux/string.h>
12#include <linux/errno.h>
13#include <linux/pagevec.h>
14#include "nilfs.h"
15#include "page.h"
16#include "btnode.h"
17#include "btree.h"
18#include "alloc.h"
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090019#include "dat.h"
Koji Sato17c76b02009-04-06 19:01:24 -070020
Ryusuke Konishi957ed602015-02-27 15:51:56 -080021static void __nilfs_btree_init(struct nilfs_bmap *bmap);
22
Li Hongf9054402010-04-02 17:36:34 +080023static struct nilfs_btree_path *nilfs_btree_alloc_path(void)
Koji Sato17c76b02009-04-06 19:01:24 -070024{
Li Hongf9054402010-04-02 17:36:34 +080025 struct nilfs_btree_path *path;
26 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070027
Li Hongf9054402010-04-02 17:36:34 +080028 path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
29 if (path == NULL)
30 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -070031
Li Hongf9054402010-04-02 17:36:34 +080032 for (; level < NILFS_BTREE_LEVEL_MAX; level++) {
Koji Sato17c76b02009-04-06 19:01:24 -070033 path[level].bp_bh = NULL;
34 path[level].bp_sib_bh = NULL;
35 path[level].bp_index = 0;
36 path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
37 path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
38 path[level].bp_op = NULL;
39 }
Li Hongf9054402010-04-02 17:36:34 +080040
41out:
42 return path;
43}
44
Li Hong73bb4882010-04-02 18:35:00 +080045static void nilfs_btree_free_path(struct nilfs_btree_path *path)
Li Hongf9054402010-04-02 17:36:34 +080046{
Li Hong73bb4882010-04-02 18:35:00 +080047 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070048
Li Hong73bb4882010-04-02 18:35:00 +080049 for (; level < NILFS_BTREE_LEVEL_MAX; level++)
Ryusuke Konishi32189292009-08-15 01:54:59 +090050 brelse(path[level].bp_bh);
Li Hong73bb4882010-04-02 18:35:00 +080051
52 kmem_cache_free(nilfs_btree_path_cache, path);
Koji Sato17c76b02009-04-06 19:01:24 -070053}
54
Koji Sato17c76b02009-04-06 19:01:24 -070055/*
56 * B-tree node operations
57 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090058static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090059 __u64 ptr, struct buffer_head **bhp)
60{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090061 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
Ryusuke Konishi45f49102009-11-13 16:25:19 +090062 struct buffer_head *bh;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090063
Ryusuke Konishi45f49102009-11-13 16:25:19 +090064 bh = nilfs_btnode_create_block(btnc, ptr);
65 if (!bh)
66 return -ENOMEM;
67
68 set_buffer_nilfs_volatile(bh);
69 *bhp = bh;
70 return 0;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090071}
Koji Sato17c76b02009-04-06 19:01:24 -070072
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090073static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070074{
75 return node->bn_flags;
76}
77
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090078static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090079nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
Koji Sato17c76b02009-04-06 19:01:24 -070080{
81 node->bn_flags = flags;
82}
83
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090084static int nilfs_btree_node_root(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070085{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090086 return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
Koji Sato17c76b02009-04-06 19:01:24 -070087}
88
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090089static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070090{
91 return node->bn_level;
92}
93
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090094static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090095nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
Koji Sato17c76b02009-04-06 19:01:24 -070096{
97 node->bn_level = level;
98}
99
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900100static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700101{
102 return le16_to_cpu(node->bn_nchildren);
103}
104
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900105static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900106nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
Koji Sato17c76b02009-04-06 19:01:24 -0700107{
108 node->bn_nchildren = cpu_to_le16(nchildren);
109}
110
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900111static int nilfs_btree_node_size(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700112{
Geliang Tangf3048d12017-02-27 14:28:38 -0800113 return i_blocksize(btree->b_inode);
Koji Sato17c76b02009-04-06 19:01:24 -0700114}
115
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900116static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700117{
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +0900118 return btree->b_nchildren_per_block;
Koji Sato17c76b02009-04-06 19:01:24 -0700119}
120
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900121static __le64 *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900122nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700123{
124 return (__le64 *)((char *)(node + 1) +
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900125 (nilfs_btree_node_root(node) ?
Koji Sato17c76b02009-04-06 19:01:24 -0700126 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
127}
128
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900129static __le64 *
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900130nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700131{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900132 return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700133}
134
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900135static __u64
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900136nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
Koji Sato17c76b02009-04-06 19:01:24 -0700137{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900138 return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700139}
140
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900141static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900142nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -0700143{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900144 *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -0700145}
146
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900147static __u64
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900148nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
149 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700150{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900151 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700152}
153
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900154static void
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900155nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
156 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700157{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900158 *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700159}
160
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900161static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
162 int level, int nchildren, int ncmax,
Koji Sato17c76b02009-04-06 19:01:24 -0700163 const __u64 *keys, const __u64 *ptrs)
164{
165 __le64 *dkeys;
166 __le64 *dptrs;
167 int i;
168
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900169 nilfs_btree_node_set_flags(node, flags);
170 nilfs_btree_node_set_level(node, level);
171 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700172
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900173 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900174 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700175 for (i = 0; i < nchildren; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900176 dkeys[i] = cpu_to_le64(keys[i]);
177 dptrs[i] = cpu_to_le64(ptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -0700178 }
179}
180
181/* Assume the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900182static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700183 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900184 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700185{
186 __le64 *ldkeys, *rdkeys;
187 __le64 *ldptrs, *rdptrs;
188 int lnchildren, rnchildren;
189
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900190 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900191 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900192 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700193
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900194 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900195 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900196 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700197
198 memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
199 memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs));
200 memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys));
201 memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs));
202
203 lnchildren += n;
204 rnchildren -= n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900205 nilfs_btree_node_set_nchildren(left, lnchildren);
206 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700207}
208
209/* Assume that the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900210static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700211 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900212 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700213{
214 __le64 *ldkeys, *rdkeys;
215 __le64 *ldptrs, *rdptrs;
216 int lnchildren, rnchildren;
217
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900218 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900219 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900220 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700221
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900222 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900223 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900224 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700225
226 memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
227 memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs));
228 memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys));
229 memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs));
230
231 lnchildren -= n;
232 rnchildren += n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900233 nilfs_btree_node_set_nchildren(left, lnchildren);
234 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700235}
236
237/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900238static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
239 __u64 key, __u64 ptr, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700240{
241 __le64 *dkeys;
242 __le64 *dptrs;
243 int nchildren;
244
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900245 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900246 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900247 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700248 if (index < nchildren) {
249 memmove(dkeys + index + 1, dkeys + index,
250 (nchildren - index) * sizeof(*dkeys));
251 memmove(dptrs + index + 1, dptrs + index,
252 (nchildren - index) * sizeof(*dptrs));
253 }
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900254 dkeys[index] = cpu_to_le64(key);
255 dptrs[index] = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700256 nchildren++;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900257 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700258}
259
260/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900261static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
262 __u64 *keyp, __u64 *ptrp, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700263{
264 __u64 key;
265 __u64 ptr;
266 __le64 *dkeys;
267 __le64 *dptrs;
268 int nchildren;
269
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900270 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900271 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900272 key = le64_to_cpu(dkeys[index]);
273 ptr = le64_to_cpu(dptrs[index]);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900274 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700275 if (keyp != NULL)
276 *keyp = key;
277 if (ptrp != NULL)
278 *ptrp = ptr;
279
280 if (index < nchildren - 1) {
281 memmove(dkeys + index, dkeys + index + 1,
282 (nchildren - index - 1) * sizeof(*dkeys));
283 memmove(dptrs + index, dptrs + index + 1,
284 (nchildren - index - 1) * sizeof(*dptrs));
285 }
286 nchildren--;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900287 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700288}
289
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900290static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
Koji Sato17c76b02009-04-06 19:01:24 -0700291 __u64 key, int *indexp)
292{
293 __u64 nkey;
294 int index, low, high, s;
295
296 /* binary search */
297 low = 0;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900298 high = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700299 index = 0;
300 s = 0;
301 while (low <= high) {
302 index = (low + high) / 2;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900303 nkey = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700304 if (nkey == key) {
305 s = 0;
306 goto out;
307 } else if (nkey < key) {
308 low = index + 1;
309 s = -1;
310 } else {
311 high = index - 1;
312 s = 1;
313 }
314 }
315
316 /* adjust index */
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900317 if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
318 if (s > 0 && index > 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700319 index--;
320 } else if (s < 0)
321 index++;
322
323 out:
Koji Sato17c76b02009-04-06 19:01:24 -0700324 *indexp = index;
325
326 return s == 0;
327}
328
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900329/**
330 * nilfs_btree_node_broken - verify consistency of btree node
331 * @node: btree node block to be examined
332 * @size: node size (in bytes)
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700333 * @inode: host inode of btree
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900334 * @blocknr: block number
335 *
336 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
337 */
338static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700339 size_t size, struct inode *inode,
340 sector_t blocknr)
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900341{
342 int level, flags, nchildren;
343 int ret = 0;
344
345 level = nilfs_btree_node_get_level(node);
346 flags = nilfs_btree_node_get_flags(node);
347 nchildren = nilfs_btree_node_get_nchildren(node);
348
349 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
350 level >= NILFS_BTREE_LEVEL_MAX ||
351 (flags & NILFS_BTREE_NODE_ROOT) ||
352 nchildren < 0 ||
353 nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
Joe Perchesa1d07472020-08-11 18:35:49 -0700354 nilfs_crit(inode->i_sb,
355 "bad btree node (ino=%lu, blocknr=%llu): level = %d, flags = 0x%x, nchildren = %d",
356 inode->i_ino, (unsigned long long)blocknr, level,
357 flags, nchildren);
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900358 ret = 1;
359 }
360 return ret;
361}
362
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800363/**
364 * nilfs_btree_root_broken - verify consistency of btree root node
365 * @node: btree root node to be examined
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700366 * @inode: host inode of btree
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800367 *
368 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
369 */
370static int nilfs_btree_root_broken(const struct nilfs_btree_node *node,
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700371 struct inode *inode)
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800372{
373 int level, flags, nchildren;
374 int ret = 0;
375
376 level = nilfs_btree_node_get_level(node);
377 flags = nilfs_btree_node_get_flags(node);
378 nchildren = nilfs_btree_node_get_nchildren(node);
379
380 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
Ryusuke Konishid8fd1502015-05-05 16:24:00 -0700381 level >= NILFS_BTREE_LEVEL_MAX ||
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800382 nchildren < 0 ||
383 nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) {
Joe Perchesa1d07472020-08-11 18:35:49 -0700384 nilfs_crit(inode->i_sb,
385 "bad btree root (ino=%lu): level = %d, flags = 0x%x, nchildren = %d",
386 inode->i_ino, level, flags, nchildren);
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800387 ret = 1;
388 }
389 return ret;
390}
391
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900392int nilfs_btree_broken_node_block(struct buffer_head *bh)
393{
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700394 struct inode *inode;
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900395 int ret;
396
397 if (buffer_nilfs_checked(bh))
398 return 0;
399
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700400 inode = bh->b_page->mapping->host;
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900401 ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700402 bh->b_size, inode, bh->b_blocknr);
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900403 if (likely(!ret))
404 set_buffer_nilfs_checked(bh);
405 return ret;
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900406}
407
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900408static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900409nilfs_btree_get_root(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700410{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900411 return (struct nilfs_btree_node *)btree->b_u.u_data;
Koji Sato17c76b02009-04-06 19:01:24 -0700412}
413
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900414static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900415nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700416{
417 return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
418}
419
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900420static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900421nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700422{
423 return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
424}
425
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900426static int nilfs_btree_height(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700427{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900428 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700429}
430
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900431static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900432nilfs_btree_get_node(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700433 const struct nilfs_btree_path *path,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900434 int level, int *ncmaxp)
Koji Sato17c76b02009-04-06 19:01:24 -0700435{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900436 struct nilfs_btree_node *node;
437
438 if (level == nilfs_btree_height(btree) - 1) {
439 node = nilfs_btree_get_root(btree);
440 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
441 } else {
442 node = nilfs_btree_get_nonroot_node(path, level);
443 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
444 }
445 return node;
Koji Sato17c76b02009-04-06 19:01:24 -0700446}
447
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700448static int nilfs_btree_bad_node(const struct nilfs_bmap *btree,
449 struct nilfs_btree_node *node, int level)
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900450{
451 if (unlikely(nilfs_btree_node_get_level(node) != level)) {
452 dump_stack();
Joe Perchesa1d07472020-08-11 18:35:49 -0700453 nilfs_crit(btree->b_inode->i_sb,
454 "btree level mismatch (ino=%lu): %d != %d",
455 btree->b_inode->i_ino,
456 nilfs_btree_node_get_level(node), level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900457 return 1;
458 }
459 return 0;
460}
461
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900462struct nilfs_btree_readahead_info {
463 struct nilfs_btree_node *node; /* parent node */
464 int max_ra_blocks; /* max nof blocks to read ahead */
465 int index; /* current index on the parent node */
466 int ncmax; /* nof children in the parent node */
467};
468
469static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
470 struct buffer_head **bhp,
471 const struct nilfs_btree_readahead_info *ra)
472{
473 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
474 struct buffer_head *bh, *ra_bh;
475 sector_t submit_ptr = 0;
476 int ret;
477
Mike Christie2a222ca2016-06-05 14:31:43 -0500478 ret = nilfs_btnode_submit_block(btnc, ptr, 0, REQ_OP_READ, 0, &bh,
479 &submit_ptr);
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900480 if (ret) {
481 if (ret != -EEXIST)
482 return ret;
483 goto out_check;
484 }
485
486 if (ra) {
487 int i, n;
488 __u64 ptr2;
489
490 /* read ahead sibling nodes */
491 for (n = ra->max_ra_blocks, i = ra->index + 1;
492 n > 0 && i < ra->ncmax; n--, i++) {
493 ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
494
Mike Christie2a222ca2016-06-05 14:31:43 -0500495 ret = nilfs_btnode_submit_block(btnc, ptr2, 0,
496 REQ_OP_READ, REQ_RAHEAD,
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900497 &ra_bh, &submit_ptr);
498 if (likely(!ret || ret == -EEXIST))
499 brelse(ra_bh);
500 else if (ret != -EBUSY)
501 break;
502 if (!buffer_locked(bh))
503 goto out_no_wait;
504 }
505 }
506
507 wait_on_buffer(bh);
508
509 out_no_wait:
510 if (!buffer_uptodate(bh)) {
Joe Perchesa1d07472020-08-11 18:35:49 -0700511 nilfs_err(btree->b_inode->i_sb,
Ryusuke Konishi39a9dcc2016-08-02 14:05:17 -0700512 "I/O error reading b-tree node block (ino=%lu, blocknr=%llu)",
513 btree->b_inode->i_ino, (unsigned long long)ptr);
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900514 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 Konishifeee8802016-08-02 14:05:10 -0700573 if (nilfs_btree_bad_node(btree, node, level))
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900574 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 Konishifeee8802016-08-02 14:05:10 -0700621 if (nilfs_btree_bad_node(btree, node, level))
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900622 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 Konishi0c6c44c2016-05-23 16:23:39 -0700692 __u64 key, __u64 *ptrp,
693 unsigned int maxblocks)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900694{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900695 struct nilfs_btree_path *path;
696 struct nilfs_btree_node *node;
697 struct inode *dat = NULL;
698 __u64 ptr, ptr2;
699 sector_t blocknr;
700 int level = NILFS_BTREE_LEVEL_NODE_MIN;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900701 int ret, cnt, index, maxlevel, ncmax;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900702 struct nilfs_btree_readahead_info p;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900703
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900704 path = nilfs_btree_alloc_path();
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900705 if (path == NULL)
706 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +0800707
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900708 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900709 if (ret < 0)
710 goto out;
711
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900712 if (NILFS_BMAP_USE_VBN(btree)) {
713 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900714 ret = nilfs_dat_translate(dat, ptr, &blocknr);
715 if (ret < 0)
716 goto out;
717 ptr = blocknr;
718 }
719 cnt = 1;
720 if (cnt == maxblocks)
721 goto end;
722
723 maxlevel = nilfs_btree_height(btree) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900724 node = nilfs_btree_get_node(btree, path, level, &ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900725 index = path[level].bp_index + 1;
726 for (;;) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900727 while (index < nilfs_btree_node_get_nchildren(node)) {
728 if (nilfs_btree_node_get_key(node, index) !=
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900729 key + cnt)
730 goto end;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900731 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900732 if (dat) {
733 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
734 if (ret < 0)
735 goto out;
736 ptr2 = blocknr;
737 }
738 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
739 goto end;
740 index++;
741 continue;
742 }
743 if (level == maxlevel)
744 break;
745
746 /* look-up right sibling node */
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900747 p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax);
748 p.index = path[level + 1].bp_index + 1;
749 p.max_ra_blocks = 7;
750 if (p.index >= nilfs_btree_node_get_nchildren(p.node) ||
751 nilfs_btree_node_get_key(p.node, p.index) != key + cnt)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900752 break;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900753 ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax);
754 path[level + 1].bp_index = p.index;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900755
756 brelse(path[level].bp_bh);
757 path[level].bp_bh = NULL;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900758
759 ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
760 &p);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900761 if (ret < 0)
762 goto out;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900763 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900764 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900765 index = 0;
766 path[level].bp_index = index;
767 }
768 end:
769 *ptrp = ptr;
770 ret = cnt;
771 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900772 nilfs_btree_free_path(path);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900773 return ret;
774}
775
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900776static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700777 struct nilfs_btree_path *path,
778 int level, __u64 key)
779{
780 if (level < nilfs_btree_height(btree) - 1) {
781 do {
Koji Sato17c76b02009-04-06 19:01:24 -0700782 nilfs_btree_node_set_key(
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900783 nilfs_btree_get_nonroot_node(path, level),
Koji Sato17c76b02009-04-06 19:01:24 -0700784 path[level].bp_index, key);
785 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900786 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700787 } while ((path[level].bp_index == 0) &&
788 (++level < nilfs_btree_height(btree) - 1));
789 }
790
791 /* root */
792 if (level == nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900793 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
Koji Sato17c76b02009-04-06 19:01:24 -0700794 path[level].bp_index, key);
795 }
796}
797
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900798static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700799 struct nilfs_btree_path *path,
800 int level, __u64 *keyp, __u64 *ptrp)
801{
802 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900803 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700804
805 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900806 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900807 ncblk = nilfs_btree_nchildren_per_block(btree);
808 nilfs_btree_node_insert(node, path[level].bp_index,
809 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700810 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900811 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700812
813 if (path[level].bp_index == 0)
814 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900815 nilfs_btree_node_get_key(node,
816 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700817 } else {
818 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900819 nilfs_btree_node_insert(node, path[level].bp_index,
820 *keyp, *ptrp,
821 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700822 }
823}
824
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900825static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700826 struct nilfs_btree_path *path,
827 int level, __u64 *keyp, __u64 *ptrp)
828{
829 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900830 int nchildren, lnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700831
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900832 node = nilfs_btree_get_nonroot_node(path, level);
833 left = nilfs_btree_get_sib_node(path, level);
834 nchildren = nilfs_btree_node_get_nchildren(node);
835 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900836 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700837 move = 0;
838
839 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
840 if (n > path[level].bp_index) {
841 /* move insert point */
842 n--;
843 move = 1;
844 }
845
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900846 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700847
848 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900849 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700850 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900851 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700852
Koji Sato17c76b02009-04-06 19:01:24 -0700853 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900854 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700855
856 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900857 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700858 path[level].bp_bh = path[level].bp_sib_bh;
859 path[level].bp_sib_bh = NULL;
860 path[level].bp_index += lnchildren;
861 path[level + 1].bp_index--;
862 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900863 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700864 path[level].bp_sib_bh = NULL;
865 path[level].bp_index -= n;
866 }
867
868 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
869}
870
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900871static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700872 struct nilfs_btree_path *path,
873 int level, __u64 *keyp, __u64 *ptrp)
874{
875 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900876 int nchildren, rnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700877
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900878 node = nilfs_btree_get_nonroot_node(path, level);
879 right = nilfs_btree_get_sib_node(path, level);
880 nchildren = nilfs_btree_node_get_nchildren(node);
881 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900882 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700883 move = 0;
884
885 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
886 if (n > nchildren - path[level].bp_index) {
887 /* move insert point */
888 n--;
889 move = 1;
890 }
891
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900892 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700893
894 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900895 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700896 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900897 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700898
Koji Sato17c76b02009-04-06 19:01:24 -0700899 path[level + 1].bp_index++;
900 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900901 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700902 path[level + 1].bp_index--;
903
904 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900905 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700906 path[level].bp_bh = path[level].bp_sib_bh;
907 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900908 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700909 path[level + 1].bp_index++;
910 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900911 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700912 path[level].bp_sib_bh = NULL;
913 }
914
915 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
916}
917
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900918static void nilfs_btree_split(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700919 struct nilfs_btree_path *path,
920 int level, __u64 *keyp, __u64 *ptrp)
921{
922 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900923 int nchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700924
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900925 node = nilfs_btree_get_nonroot_node(path, level);
926 right = nilfs_btree_get_sib_node(path, level);
927 nchildren = nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900928 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700929 move = 0;
930
931 n = (nchildren + 1) / 2;
932 if (n > nchildren - path[level].bp_index) {
933 n--;
934 move = 1;
935 }
936
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900937 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700938
939 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900940 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700941 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900942 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700943
Koji Sato17c76b02009-04-06 19:01:24 -0700944 if (move) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900945 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900946 nilfs_btree_node_insert(right, path[level].bp_index,
947 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700948
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900949 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700950 *ptrp = path[level].bp_newreq.bpr_ptr;
951
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900952 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700953 path[level].bp_bh = path[level].bp_sib_bh;
954 path[level].bp_sib_bh = NULL;
955 } else {
956 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
957
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900958 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700959 *ptrp = path[level].bp_newreq.bpr_ptr;
960
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900961 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700962 path[level].bp_sib_bh = NULL;
963 }
964
965 path[level + 1].bp_index++;
966}
967
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900968static void nilfs_btree_grow(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700969 struct nilfs_btree_path *path,
970 int level, __u64 *keyp, __u64 *ptrp)
971{
972 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900973 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700974
Koji Sato17c76b02009-04-06 19:01:24 -0700975 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900976 child = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900977 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700978
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900979 n = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -0700980
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900981 nilfs_btree_node_move_right(root, child, n,
982 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900983 nilfs_btree_node_set_level(root, level + 1);
Koji Sato17c76b02009-04-06 19:01:24 -0700984
985 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900986 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700987
Koji Sato17c76b02009-04-06 19:01:24 -0700988 path[level].bp_bh = path[level].bp_sib_bh;
989 path[level].bp_sib_bh = NULL;
990
991 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
992
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900993 *keyp = nilfs_btree_node_get_key(child, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700994 *ptrp = path[level].bp_newreq.bpr_ptr;
995}
996
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900997static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700998 const struct nilfs_btree_path *path)
999{
1000 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001001 int level, ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001002
1003 if (path == NULL)
1004 return NILFS_BMAP_INVALID_PTR;
1005
1006 /* left sibling */
1007 level = NILFS_BTREE_LEVEL_NODE_MIN;
1008 if (path[level].bp_index > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001009 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1010 return nilfs_btree_node_get_ptr(node,
1011 path[level].bp_index - 1,
1012 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001013 }
1014
1015 /* parent */
1016 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
1017 if (level <= nilfs_btree_height(btree) - 1) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001018 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1019 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
1020 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001021 }
1022
1023 return NILFS_BMAP_INVALID_PTR;
1024}
1025
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001026static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001027 const struct nilfs_btree_path *path,
1028 __u64 key)
1029{
1030 __u64 ptr;
1031
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001032 ptr = nilfs_bmap_find_target_seq(btree, key);
Koji Sato17c76b02009-04-06 19:01:24 -07001033 if (ptr != NILFS_BMAP_INVALID_PTR)
1034 /* sequential access */
1035 return ptr;
Ryusuke Konishi7f001842016-05-23 16:23:42 -07001036
1037 ptr = nilfs_btree_find_near(btree, path);
1038 if (ptr != NILFS_BMAP_INVALID_PTR)
1039 /* near */
1040 return ptr;
1041
Koji Sato17c76b02009-04-06 19:01:24 -07001042 /* block group */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001043 return nilfs_bmap_find_target_in_group(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001044}
1045
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001046static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001047 struct nilfs_btree_path *path,
1048 int *levelp, __u64 key, __u64 ptr,
1049 struct nilfs_bmap_stats *stats)
1050{
1051 struct buffer_head *bh;
1052 struct nilfs_btree_node *node, *parent, *sib;
1053 __u64 sibptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001054 int pindex, level, ncmax, ncblk, ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001055 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001056
1057 stats->bs_nblocks = 0;
1058 level = NILFS_BTREE_LEVEL_DATA;
1059
1060 /* allocate a new ptr for data block */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001061 if (NILFS_BMAP_USE_VBN(btree)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001062 path[level].bp_newreq.bpr_ptr =
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001063 nilfs_btree_find_target_v(btree, path, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001064 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001065 }
Koji Sato17c76b02009-04-06 19:01:24 -07001066
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001067 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001068 if (ret < 0)
1069 goto err_out_data;
1070
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001071 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001072
Koji Sato17c76b02009-04-06 19:01:24 -07001073 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1074 level < nilfs_btree_height(btree) - 1;
1075 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001076 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001077 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001078 path[level].bp_op = nilfs_btree_do_insert;
1079 stats->bs_nblocks++;
1080 goto out;
1081 }
1082
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001083 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001084 pindex = path[level + 1].bp_index;
1085
1086 /* left sibling */
1087 if (pindex > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001088 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1089 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001090 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001091 if (ret < 0)
1092 goto err_out_child_node;
1093 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001094 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001095 path[level].bp_sib_bh = bh;
1096 path[level].bp_op = nilfs_btree_carry_left;
1097 stats->bs_nblocks++;
1098 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001099 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001100 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001101 }
Koji Sato17c76b02009-04-06 19:01:24 -07001102 }
1103
1104 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001105 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1106 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1107 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001108 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001109 if (ret < 0)
1110 goto err_out_child_node;
1111 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001112 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001113 path[level].bp_sib_bh = bh;
1114 path[level].bp_op = nilfs_btree_carry_right;
1115 stats->bs_nblocks++;
1116 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001117 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001118 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001119 }
Koji Sato17c76b02009-04-06 19:01:24 -07001120 }
1121
1122 /* split */
1123 path[level].bp_newreq.bpr_ptr =
1124 path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001125 ret = nilfs_bmap_prepare_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001126 &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001127 if (ret < 0)
1128 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001129 ret = nilfs_btree_get_new_block(btree,
1130 path[level].bp_newreq.bpr_ptr,
1131 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001132 if (ret < 0)
1133 goto err_out_curr_node;
1134
1135 stats->bs_nblocks++;
1136
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001137 sib = (struct nilfs_btree_node *)bh->b_data;
1138 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001139 path[level].bp_sib_bh = bh;
1140 path[level].bp_op = nilfs_btree_split;
1141 }
1142
1143 /* root */
1144 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001145 if (nilfs_btree_node_get_nchildren(node) <
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001146 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
Koji Sato17c76b02009-04-06 19:01:24 -07001147 path[level].bp_op = nilfs_btree_do_insert;
1148 stats->bs_nblocks++;
1149 goto out;
1150 }
1151
1152 /* grow */
1153 path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001154 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001155 if (ret < 0)
1156 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001157 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1158 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001159 if (ret < 0)
1160 goto err_out_curr_node;
1161
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001162 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1163 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001164 path[level].bp_sib_bh = bh;
1165 path[level].bp_op = nilfs_btree_grow;
1166
1167 level++;
1168 path[level].bp_op = nilfs_btree_do_insert;
1169
1170 /* a newly-created node block and a data block are added */
1171 stats->bs_nblocks += 2;
1172
1173 /* success */
1174 out:
1175 *levelp = level;
1176 return ret;
1177
1178 /* error */
1179 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001180 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001181 err_out_child_node:
1182 for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001183 nilfs_btnode_delete(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001184 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001185
1186 }
1187
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001188 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001189 err_out_data:
1190 *levelp = level;
1191 stats->bs_nblocks = 0;
1192 return ret;
1193}
1194
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001195static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001196 struct nilfs_btree_path *path,
1197 int maxlevel, __u64 key, __u64 ptr)
1198{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001199 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001200 int level;
1201
1202 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1203 ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001204 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001205 nilfs_bmap_set_target_v(btree, key, ptr);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001206 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001207 }
Koji Sato17c76b02009-04-06 19:01:24 -07001208
1209 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001210 nilfs_bmap_commit_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001211 &path[level - 1].bp_newreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001212 path[level].bp_op(btree, path, level, &key, &ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001213 }
1214
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001215 if (!nilfs_bmap_dirty(btree))
1216 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001217}
1218
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001219static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
Koji Sato17c76b02009-04-06 19:01:24 -07001220{
Koji Sato17c76b02009-04-06 19:01:24 -07001221 struct nilfs_btree_path *path;
1222 struct nilfs_bmap_stats stats;
1223 int level, ret;
1224
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001225 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001226 if (path == NULL)
1227 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001228
1229 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001230 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001231 if (ret != -ENOENT) {
1232 if (ret == 0)
1233 ret = -EEXIST;
1234 goto out;
1235 }
1236
1237 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1238 if (ret < 0)
1239 goto out;
1240 nilfs_btree_commit_insert(btree, path, level, key, ptr);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001241 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001242
1243 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001244 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001245 return ret;
1246}
1247
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001248static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001249 struct nilfs_btree_path *path,
1250 int level, __u64 *keyp, __u64 *ptrp)
1251{
1252 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001253 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001254
1255 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001256 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001257 ncblk = nilfs_btree_nchildren_per_block(btree);
1258 nilfs_btree_node_delete(node, path[level].bp_index,
1259 keyp, ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001260 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001261 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001262 if (path[level].bp_index == 0)
1263 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001264 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001265 } else {
1266 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001267 nilfs_btree_node_delete(node, path[level].bp_index,
1268 keyp, ptrp,
1269 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -07001270 }
1271}
1272
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001273static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001274 struct nilfs_btree_path *path,
1275 int level, __u64 *keyp, __u64 *ptrp)
1276{
1277 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001278 int nchildren, lnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001279
1280 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1281
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001282 node = nilfs_btree_get_nonroot_node(path, level);
1283 left = nilfs_btree_get_sib_node(path, level);
1284 nchildren = nilfs_btree_node_get_nchildren(node);
1285 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001286 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001287
1288 n = (nchildren + lnchildren) / 2 - nchildren;
1289
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001290 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001291
1292 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001293 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001294 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001295 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001296
Koji Sato17c76b02009-04-06 19:01:24 -07001297 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001298 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001299
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001300 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001301 path[level].bp_sib_bh = NULL;
1302 path[level].bp_index += n;
1303}
1304
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001305static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001306 struct nilfs_btree_path *path,
1307 int level, __u64 *keyp, __u64 *ptrp)
1308{
1309 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001310 int nchildren, rnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001311
1312 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1313
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001314 node = nilfs_btree_get_nonroot_node(path, level);
1315 right = nilfs_btree_get_sib_node(path, level);
1316 nchildren = nilfs_btree_node_get_nchildren(node);
1317 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001318 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001319
1320 n = (nchildren + rnchildren) / 2 - nchildren;
1321
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001322 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001323
1324 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001325 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001326 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001327 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001328
Koji Sato17c76b02009-04-06 19:01:24 -07001329 path[level + 1].bp_index++;
1330 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001331 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001332 path[level + 1].bp_index--;
1333
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001334 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001335 path[level].bp_sib_bh = NULL;
1336}
1337
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001338static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001339 struct nilfs_btree_path *path,
1340 int level, __u64 *keyp, __u64 *ptrp)
1341{
1342 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001343 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001344
1345 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1346
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001347 node = nilfs_btree_get_nonroot_node(path, level);
1348 left = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001349 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001350
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001351 n = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001352
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001353 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001354
1355 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001356 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001357
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001358 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001359 path[level].bp_bh = path[level].bp_sib_bh;
1360 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001361 path[level].bp_index += nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -07001362}
1363
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001364static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001365 struct nilfs_btree_path *path,
1366 int level, __u64 *keyp, __u64 *ptrp)
1367{
1368 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001369 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001370
1371 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1372
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001373 node = nilfs_btree_get_nonroot_node(path, level);
1374 right = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001375 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001376
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001377 n = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -07001378
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001379 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001380
1381 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001382 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001383
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001384 nilfs_btnode_delete(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001385 path[level].bp_sib_bh = NULL;
1386 path[level + 1].bp_index++;
1387}
1388
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001389static void nilfs_btree_shrink(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001390 struct nilfs_btree_path *path,
1391 int level, __u64 *keyp, __u64 *ptrp)
1392{
1393 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001394 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001395
1396 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1397
Koji Sato17c76b02009-04-06 19:01:24 -07001398 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001399 child = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001400 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001401
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001402 nilfs_btree_node_delete(root, 0, NULL, NULL,
1403 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001404 nilfs_btree_node_set_level(root, level);
1405 n = nilfs_btree_node_get_nchildren(child);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001406 nilfs_btree_node_move_left(root, child, n,
1407 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001408
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001409 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001410 path[level].bp_bh = NULL;
1411}
1412
Ryusuke Konishid4099052011-05-25 23:00:27 +09001413static void nilfs_btree_nop(struct nilfs_bmap *btree,
1414 struct nilfs_btree_path *path,
1415 int level, __u64 *keyp, __u64 *ptrp)
1416{
1417}
Koji Sato17c76b02009-04-06 19:01:24 -07001418
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001419static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001420 struct nilfs_btree_path *path,
1421 int *levelp,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001422 struct nilfs_bmap_stats *stats,
1423 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001424{
1425 struct buffer_head *bh;
1426 struct nilfs_btree_node *node, *parent, *sib;
1427 __u64 sibptr;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001428 int pindex, dindex, level, ncmin, ncmax, ncblk, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001429
1430 ret = 0;
1431 stats->bs_nblocks = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001432 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001433 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001434
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001435 for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index;
Koji Sato17c76b02009-04-06 19:01:24 -07001436 level < nilfs_btree_height(btree) - 1;
1437 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001438 node = nilfs_btree_get_nonroot_node(path, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001439 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001440 nilfs_btree_node_get_ptr(node, dindex, ncblk);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001441 ret = nilfs_bmap_prepare_end_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001442 &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001443 if (ret < 0)
1444 goto err_out_child_node;
Koji Sato17c76b02009-04-06 19:01:24 -07001445
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001446 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001447 path[level].bp_op = nilfs_btree_do_delete;
1448 stats->bs_nblocks++;
1449 goto out;
1450 }
1451
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001452 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001453 pindex = path[level + 1].bp_index;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001454 dindex = pindex;
Koji Sato17c76b02009-04-06 19:01:24 -07001455
1456 if (pindex > 0) {
1457 /* left sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001458 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1459 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001460 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001461 if (ret < 0)
1462 goto err_out_curr_node;
1463 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001464 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001465 path[level].bp_sib_bh = bh;
1466 path[level].bp_op = nilfs_btree_borrow_left;
1467 stats->bs_nblocks++;
1468 goto out;
1469 } else {
1470 path[level].bp_sib_bh = bh;
1471 path[level].bp_op = nilfs_btree_concat_left;
1472 stats->bs_nblocks++;
1473 /* continue; */
1474 }
1475 } else if (pindex <
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001476 nilfs_btree_node_get_nchildren(parent) - 1) {
Koji Sato17c76b02009-04-06 19:01:24 -07001477 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001478 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1479 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001480 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001481 if (ret < 0)
1482 goto err_out_curr_node;
1483 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001484 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001485 path[level].bp_sib_bh = bh;
1486 path[level].bp_op = nilfs_btree_borrow_right;
1487 stats->bs_nblocks++;
1488 goto out;
1489 } else {
1490 path[level].bp_sib_bh = bh;
1491 path[level].bp_op = nilfs_btree_concat_right;
1492 stats->bs_nblocks++;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001493 /*
1494 * When merging right sibling node
1495 * into the current node, pointer to
1496 * the right sibling node must be
1497 * terminated instead. The adjustment
1498 * below is required for that.
1499 */
1500 dindex = pindex + 1;
Koji Sato17c76b02009-04-06 19:01:24 -07001501 /* continue; */
1502 }
1503 } else {
1504 /* no siblings */
1505 /* the only child of the root node */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001506 WARN_ON(level != nilfs_btree_height(btree) - 2);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001507 if (nilfs_btree_node_get_nchildren(node) - 1 <=
Koji Sato17c76b02009-04-06 19:01:24 -07001508 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1509 path[level].bp_op = nilfs_btree_shrink;
1510 stats->bs_nblocks += 2;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001511 level++;
1512 path[level].bp_op = nilfs_btree_nop;
1513 goto shrink_root_child;
Koji Sato17c76b02009-04-06 19:01:24 -07001514 } else {
1515 path[level].bp_op = nilfs_btree_do_delete;
1516 stats->bs_nblocks++;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001517 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -07001518 }
Koji Sato17c76b02009-04-06 19:01:24 -07001519 }
1520 }
1521
Ryusuke Konishid4099052011-05-25 23:00:27 +09001522 /* child of the root node is deleted */
1523 path[level].bp_op = nilfs_btree_do_delete;
1524 stats->bs_nblocks++;
1525
1526shrink_root_child:
Koji Sato17c76b02009-04-06 19:01:24 -07001527 node = nilfs_btree_get_root(btree);
1528 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001529 nilfs_btree_node_get_ptr(node, dindex,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001530 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001531
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001532 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001533 if (ret < 0)
1534 goto err_out_child_node;
1535
Koji Sato17c76b02009-04-06 19:01:24 -07001536 /* success */
1537 out:
1538 *levelp = level;
1539 return ret;
1540
1541 /* error */
1542 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001543 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001544 err_out_child_node:
1545 for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001546 brelse(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001547 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001548 }
1549 *levelp = level;
1550 stats->bs_nblocks = 0;
1551 return ret;
1552}
1553
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001554static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001555 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001556 int maxlevel, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001557{
1558 int level;
1559
1560 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001561 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001562 path[level].bp_op(btree, path, level, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001563 }
1564
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001565 if (!nilfs_bmap_dirty(btree))
1566 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001567}
1568
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001569static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001570
1571{
Koji Sato17c76b02009-04-06 19:01:24 -07001572 struct nilfs_btree_path *path;
1573 struct nilfs_bmap_stats stats;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001574 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001575 int level, ret;
1576
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001577 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001578 if (path == NULL)
1579 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +08001580
Koji Sato17c76b02009-04-06 19:01:24 -07001581 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001582 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001583 if (ret < 0)
1584 goto out;
1585
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001586
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001587 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001588
1589 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001590 if (ret < 0)
1591 goto out;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001592 nilfs_btree_commit_delete(btree, path, level, dat);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001593 nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001594
1595out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001596 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001597 return ret;
1598}
1599
Ryusuke Konishi5b203842015-04-16 12:46:36 -07001600static int nilfs_btree_seek_key(const struct nilfs_bmap *btree, __u64 start,
1601 __u64 *keyp)
1602{
1603 struct nilfs_btree_path *path;
1604 const int minlevel = NILFS_BTREE_LEVEL_NODE_MIN;
1605 int ret;
1606
1607 path = nilfs_btree_alloc_path();
1608 if (!path)
1609 return -ENOMEM;
1610
1611 ret = nilfs_btree_do_lookup(btree, path, start, NULL, minlevel, 0);
1612 if (!ret)
1613 *keyp = start;
1614 else if (ret == -ENOENT)
1615 ret = nilfs_btree_get_next_key(btree, path, minlevel, keyp);
1616
1617 nilfs_btree_free_path(path);
1618 return ret;
1619}
1620
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001621static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
Koji Sato17c76b02009-04-06 19:01:24 -07001622{
Koji Sato17c76b02009-04-06 19:01:24 -07001623 struct nilfs_btree_path *path;
1624 int ret;
1625
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001626 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001627 if (path == NULL)
1628 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001629
1630 ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
1631
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001632 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001633
1634 return ret;
1635}
1636
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001637static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001638{
1639 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001640 struct nilfs_btree_node *root, *node;
1641 __u64 maxkey, nextmaxkey;
1642 __u64 ptr;
1643 int nchildren, ret;
1644
Koji Sato17c76b02009-04-06 19:01:24 -07001645 root = nilfs_btree_get_root(btree);
1646 switch (nilfs_btree_height(btree)) {
1647 case 2:
1648 bh = NULL;
1649 node = root;
1650 break;
1651 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001652 nchildren = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -07001653 if (nchildren > 1)
1654 return 0;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001655 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1656 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001657 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001658 if (ret < 0)
1659 return ret;
1660 node = (struct nilfs_btree_node *)bh->b_data;
1661 break;
1662 default:
1663 return 0;
1664 }
1665
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001666 nchildren = nilfs_btree_node_get_nchildren(node);
1667 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
Koji Sato17c76b02009-04-06 19:01:24 -07001668 nextmaxkey = (nchildren > 1) ?
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001669 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
Koji Sato17c76b02009-04-06 19:01:24 -07001670 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001671 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001672
Ryusuke Konishi30333422009-05-24 00:09:44 +09001673 return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
Koji Sato17c76b02009-04-06 19:01:24 -07001674}
1675
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001676static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001677 __u64 *keys, __u64 *ptrs, int nitems)
1678{
1679 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001680 struct nilfs_btree_node *node, *root;
1681 __le64 *dkeys;
1682 __le64 *dptrs;
1683 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001684 int nchildren, ncmax, i, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001685
Koji Sato17c76b02009-04-06 19:01:24 -07001686 root = nilfs_btree_get_root(btree);
1687 switch (nilfs_btree_height(btree)) {
1688 case 2:
1689 bh = NULL;
1690 node = root;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001691 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
Koji Sato17c76b02009-04-06 19:01:24 -07001692 break;
1693 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001694 nchildren = nilfs_btree_node_get_nchildren(root);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001695 WARN_ON(nchildren > 1);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001696 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1697 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001698 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001699 if (ret < 0)
1700 return ret;
1701 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001702 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001703 break;
1704 default:
1705 node = NULL;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001706 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -07001707 }
1708
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001709 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001710 if (nchildren < nitems)
1711 nitems = nchildren;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001712 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001713 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001714 for (i = 0; i < nitems; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09001715 keys[i] = le64_to_cpu(dkeys[i]);
1716 ptrs[i] = le64_to_cpu(dptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -07001717 }
1718
1719 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001720 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001721
1722 return nitems;
1723}
1724
1725static int
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001726nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
Koji Sato17c76b02009-04-06 19:01:24 -07001727 union nilfs_bmap_ptr_req *dreq,
1728 union nilfs_bmap_ptr_req *nreq,
1729 struct buffer_head **bhp,
1730 struct nilfs_bmap_stats *stats)
1731{
1732 struct buffer_head *bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001733 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001734 int ret;
1735
Koji Sato17c76b02009-04-06 19:01:24 -07001736 stats->bs_nblocks = 0;
1737
1738 /* for data */
1739 /* cannot find near ptr */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001740 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001741 dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001742 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001743 }
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001744
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001745 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001746 if (ret < 0)
1747 return ret;
1748
1749 *bhp = NULL;
1750 stats->bs_nblocks++;
1751 if (nreq != NULL) {
1752 nreq->bpr_ptr = dreq->bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001753 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001754 if (ret < 0)
1755 goto err_out_dreq;
1756
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001757 ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001758 if (ret < 0)
1759 goto err_out_nreq;
1760
1761 *bhp = bh;
1762 stats->bs_nblocks++;
1763 }
1764
1765 /* success */
1766 return 0;
1767
1768 /* error */
1769 err_out_nreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001770 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001771 err_out_dreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001772 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001773 stats->bs_nblocks = 0;
1774 return ret;
1775
1776}
1777
1778static void
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001779nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001780 __u64 key, __u64 ptr,
1781 const __u64 *keys, const __u64 *ptrs,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001782 int n,
Koji Sato17c76b02009-04-06 19:01:24 -07001783 union nilfs_bmap_ptr_req *dreq,
1784 union nilfs_bmap_ptr_req *nreq,
1785 struct buffer_head *bh)
1786{
Koji Sato17c76b02009-04-06 19:01:24 -07001787 struct nilfs_btree_node *node;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001788 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001789 __u64 tmpptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001790 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001791
1792 /* free resources */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001793 if (btree->b_ops->bop_clear != NULL)
1794 btree->b_ops->bop_clear(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001795
1796 /* ptr must be a pointer to a buffer head. */
1797 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1798
1799 /* convert and insert */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001800 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi957ed602015-02-27 15:51:56 -08001801 __nilfs_btree_init(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001802 if (nreq != NULL) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001803 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1804 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001805
1806 /* create child node at level 1 */
Koji Sato17c76b02009-04-06 19:01:24 -07001807 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001808 ncblk = nilfs_btree_nchildren_per_block(btree);
1809 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1810 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001811 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001812 mark_buffer_dirty(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001813 if (!nilfs_bmap_dirty(btree))
1814 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001815
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001816 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001817
1818 /* create root node at level 2 */
1819 node = nilfs_btree_get_root(btree);
1820 tmpptr = nreq->bpr_ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001821 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1822 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1823 &keys[0], &tmpptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001824 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001825 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001826
1827 /* create root node at level 1 */
1828 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001829 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1830 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1831 keys, ptrs);
1832 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1833 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001834 if (!nilfs_bmap_dirty(btree))
1835 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001836 }
1837
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001838 if (NILFS_BMAP_USE_VBN(btree))
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001839 nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001840}
1841
1842/**
1843 * nilfs_btree_convert_and_insert -
1844 * @bmap:
1845 * @key:
1846 * @ptr:
1847 * @keys:
1848 * @ptrs:
1849 * @n:
Koji Sato17c76b02009-04-06 19:01:24 -07001850 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001851int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001852 __u64 key, __u64 ptr,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001853 const __u64 *keys, const __u64 *ptrs, int n)
Koji Sato17c76b02009-04-06 19:01:24 -07001854{
Ryusuke Konishi4f050282015-11-06 16:32:16 -08001855 struct buffer_head *bh = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001856 union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
1857 struct nilfs_bmap_stats stats;
1858 int ret;
1859
1860 if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1861 di = &dreq;
1862 ni = NULL;
1863 } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
Geliang Tang1508ddc2017-02-27 14:28:35 -08001864 nilfs_btree_node_size(btree))) {
Koji Sato17c76b02009-04-06 19:01:24 -07001865 di = &dreq;
1866 ni = &nreq;
1867 } else {
1868 di = NULL;
1869 ni = NULL;
1870 BUG();
1871 }
1872
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001873 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
Koji Sato17c76b02009-04-06 19:01:24 -07001874 &stats);
1875 if (ret < 0)
1876 return ret;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001877 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001878 di, ni, bh);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001879 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001880 return 0;
1881}
1882
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001883static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001884 struct nilfs_btree_path *path,
1885 int level,
1886 struct buffer_head *bh)
1887{
1888 while ((++level < nilfs_btree_height(btree) - 1) &&
1889 !buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001890 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001891
1892 return 0;
1893}
1894
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001895static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001896 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001897 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001898{
1899 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001900 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001901
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001902 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001903 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001904 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1905 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001906 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001907 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1908 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001909 if (ret < 0)
1910 return ret;
1911
1912 if (buffer_nilfs_node(path[level].bp_bh)) {
1913 path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
1914 path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
1915 path[level].bp_ctxt.bh = path[level].bp_bh;
1916 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001917 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001918 &path[level].bp_ctxt);
1919 if (ret < 0) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001920 nilfs_dat_abort_update(dat,
1921 &path[level].bp_oldreq.bpr_req,
1922 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001923 return ret;
1924 }
1925 }
1926
1927 return 0;
1928}
1929
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001930static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001931 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001932 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001933{
1934 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001935 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001936
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001937 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1938 &path[level].bp_newreq.bpr_req,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001939 btree->b_ptr_type == NILFS_BMAP_PTR_VS);
Koji Sato17c76b02009-04-06 19:01:24 -07001940
1941 if (buffer_nilfs_node(path[level].bp_bh)) {
1942 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001943 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001944 &path[level].bp_ctxt);
1945 path[level].bp_bh = path[level].bp_ctxt.bh;
1946 }
1947 set_buffer_nilfs_volatile(path[level].bp_bh);
1948
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001949 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1950 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1951 path[level].bp_newreq.bpr_ptr, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001952}
1953
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001954static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001955 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001956 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001957{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001958 nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
1959 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001960 if (buffer_nilfs_node(path[level].bp_bh))
1961 nilfs_btnode_abort_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001962 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001963 &path[level].bp_ctxt);
1964}
1965
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001966static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001967 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001968 int minlevel, int *maxlevelp,
1969 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001970{
1971 int level, ret;
1972
1973 level = minlevel;
1974 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001975 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001976 if (ret < 0)
1977 return ret;
1978 }
1979 while ((++level < nilfs_btree_height(btree) - 1) &&
1980 !buffer_dirty(path[level].bp_bh)) {
1981
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001982 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001983 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001984 if (ret < 0)
1985 goto out;
1986 }
1987
1988 /* success */
Koji Sato17c76b02009-04-06 19:01:24 -07001989 *maxlevelp = level - 1;
1990 return 0;
1991
1992 /* error */
1993 out:
1994 while (--level > minlevel)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001995 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001996 if (!buffer_nilfs_volatile(path[level].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001997 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001998 return ret;
1999}
2000
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002001static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002002 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002003 int minlevel, int maxlevel,
2004 struct buffer_head *bh,
2005 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07002006{
2007 int level;
2008
2009 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002010 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002011
2012 for (level = minlevel + 1; level <= maxlevel; level++)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002013 nilfs_btree_commit_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002014}
2015
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002016static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002017 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002018 int level, struct buffer_head *bh)
Koji Sato17c76b02009-04-06 19:01:24 -07002019{
Li Hong308f4412010-04-02 18:40:39 +08002020 int maxlevel = 0, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002021 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002022 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002023 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002024 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07002025
2026 get_bh(bh);
2027 path[level].bp_bh = bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002028 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
2029 dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002030 if (ret < 0)
2031 goto out;
2032
2033 if (buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002034 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2035 ptr = nilfs_btree_node_get_ptr(parent,
2036 path[level + 1].bp_index,
2037 ncmax);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002038 ret = nilfs_dat_mark_dirty(dat, ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07002039 if (ret < 0)
2040 goto out;
2041 }
2042
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002043 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002044
2045 out:
2046 brelse(path[level].bp_bh);
2047 path[level].bp_bh = NULL;
2048 return ret;
2049}
2050
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002051static int nilfs_btree_propagate(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002052 struct buffer_head *bh)
2053{
Koji Sato17c76b02009-04-06 19:01:24 -07002054 struct nilfs_btree_path *path;
2055 struct nilfs_btree_node *node;
2056 __u64 key;
2057 int level, ret;
2058
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002059 WARN_ON(!buffer_dirty(bh));
Koji Sato17c76b02009-04-06 19:01:24 -07002060
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002061 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002062 if (path == NULL)
2063 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002064
2065 if (buffer_nilfs_node(bh)) {
2066 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002067 key = nilfs_btree_node_get_key(node, 0);
2068 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002069 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002070 key = nilfs_bmap_data_get_key(btree, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002071 level = NILFS_BTREE_LEVEL_DATA;
2072 }
2073
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002074 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002075 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002076 if (unlikely(ret == -ENOENT))
Joe Perchesa1d07472020-08-11 18:35:49 -07002077 nilfs_crit(btree->b_inode->i_sb,
2078 "writing node/leaf block does not appear in b-tree (ino=%lu) at key=%llu, level=%d",
2079 btree->b_inode->i_ino,
2080 (unsigned long long)key, level);
Koji Sato17c76b02009-04-06 19:01:24 -07002081 goto out;
2082 }
2083
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002084 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002085 nilfs_btree_propagate_v(btree, path, level, bh) :
2086 nilfs_btree_propagate_p(btree, path, level, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002087
2088 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002089 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002090
2091 return ret;
2092}
2093
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002094static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002095 struct buffer_head *bh)
2096{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002097 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002098}
2099
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002100static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002101 struct list_head *lists,
2102 struct buffer_head *bh)
2103{
2104 struct list_head *head;
2105 struct buffer_head *cbh;
2106 struct nilfs_btree_node *node, *cnode;
2107 __u64 key, ckey;
2108 int level;
2109
2110 get_bh(bh);
2111 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002112 key = nilfs_btree_node_get_key(node, 0);
2113 level = nilfs_btree_node_get_level(node);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002114 if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
2115 level >= NILFS_BTREE_LEVEL_MAX) {
2116 dump_stack();
Joe Perchesa1d07472020-08-11 18:35:49 -07002117 nilfs_warn(btree->b_inode->i_sb,
2118 "invalid btree level: %d (key=%llu, ino=%lu, blocknr=%llu)",
2119 level, (unsigned long long)key,
2120 btree->b_inode->i_ino,
2121 (unsigned long long)bh->b_blocknr);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002122 return;
2123 }
2124
Koji Sato17c76b02009-04-06 19:01:24 -07002125 list_for_each(head, &lists[level]) {
2126 cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
2127 cnode = (struct nilfs_btree_node *)cbh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002128 ckey = nilfs_btree_node_get_key(cnode, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002129 if (key < ckey)
2130 break;
2131 }
2132 list_add_tail(&bh->b_assoc_buffers, head);
2133}
2134
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002135static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002136 struct list_head *listp)
2137{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002138 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
Koji Sato17c76b02009-04-06 19:01:24 -07002139 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2140 struct pagevec pvec;
2141 struct buffer_head *bh, *head;
2142 pgoff_t index = 0;
2143 int level, i;
2144
2145 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2146 level < NILFS_BTREE_LEVEL_MAX;
2147 level++)
2148 INIT_LIST_HEAD(&lists[level]);
2149
Mel Gorman86679822017-11-15 17:37:52 -08002150 pagevec_init(&pvec);
Koji Sato17c76b02009-04-06 19:01:24 -07002151
Jan Kara67fd7072017-11-15 17:35:19 -08002152 while (pagevec_lookup_tag(&pvec, btcache, &index,
2153 PAGECACHE_TAG_DIRTY)) {
Koji Sato17c76b02009-04-06 19:01:24 -07002154 for (i = 0; i < pagevec_count(&pvec); i++) {
2155 bh = head = page_buffers(pvec.pages[i]);
2156 do {
2157 if (buffer_dirty(bh))
2158 nilfs_btree_add_dirty_buffer(btree,
2159 lists, bh);
2160 } while ((bh = bh->b_this_page) != head);
2161 }
2162 pagevec_release(&pvec);
2163 cond_resched();
2164 }
2165
2166 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2167 level < NILFS_BTREE_LEVEL_MAX;
2168 level++)
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002169 list_splice_tail(&lists[level], listp);
Koji Sato17c76b02009-04-06 19:01:24 -07002170}
2171
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002172static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002173 struct nilfs_btree_path *path,
2174 int level,
2175 struct buffer_head **bh,
2176 sector_t blocknr,
2177 union nilfs_binfo *binfo)
2178{
2179 struct nilfs_btree_node *parent;
2180 __u64 key;
2181 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002182 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002183
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002184 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2185 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2186 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002187 if (buffer_nilfs_node(*bh)) {
2188 path[level].bp_ctxt.oldkey = ptr;
2189 path[level].bp_ctxt.newkey = blocknr;
2190 path[level].bp_ctxt.bh = *bh;
2191 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002192 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002193 &path[level].bp_ctxt);
2194 if (ret < 0)
2195 return ret;
2196 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002197 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002198 &path[level].bp_ctxt);
2199 *bh = path[level].bp_ctxt.bh;
2200 }
2201
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002202 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2203 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002204
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002205 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002206 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002207 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002208 binfo->bi_dat.bi_level = level;
2209
2210 return 0;
2211}
2212
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002213static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002214 struct nilfs_btree_path *path,
2215 int level,
2216 struct buffer_head **bh,
2217 sector_t blocknr,
2218 union nilfs_binfo *binfo)
2219{
2220 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002221 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002222 __u64 key;
2223 __u64 ptr;
2224 union nilfs_bmap_ptr_req req;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002225 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002226
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002227 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2228 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2229 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002230 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002231 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2232 if (ret < 0)
Koji Sato17c76b02009-04-06 19:01:24 -07002233 return ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002234 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002235
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002236 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002237 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002238 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
2239 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002240
2241 return 0;
2242}
2243
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002244static int nilfs_btree_assign(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002245 struct buffer_head **bh,
2246 sector_t blocknr,
2247 union nilfs_binfo *binfo)
2248{
Koji Sato17c76b02009-04-06 19:01:24 -07002249 struct nilfs_btree_path *path;
2250 struct nilfs_btree_node *node;
2251 __u64 key;
2252 int level, ret;
2253
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002254 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002255 if (path == NULL)
2256 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002257
2258 if (buffer_nilfs_node(*bh)) {
2259 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002260 key = nilfs_btree_node_get_key(node, 0);
2261 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002262 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002263 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002264 level = NILFS_BTREE_LEVEL_DATA;
2265 }
2266
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002267 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002268 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002269 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002270 goto out;
2271 }
2272
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002273 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002274 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2275 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
Koji Sato17c76b02009-04-06 19:01:24 -07002276
2277 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002278 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002279
2280 return ret;
2281}
2282
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002283static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002284 struct buffer_head **bh,
2285 sector_t blocknr,
2286 union nilfs_binfo *binfo)
2287{
Koji Sato17c76b02009-04-06 19:01:24 -07002288 struct nilfs_btree_node *node;
2289 __u64 key;
2290 int ret;
2291
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002292 ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002293 blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002294 if (ret < 0)
2295 return ret;
2296
2297 if (buffer_nilfs_node(*bh)) {
2298 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002299 key = nilfs_btree_node_get_key(node, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002300 } else
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002301 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002302
2303 /* on-disk format */
2304 binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002305 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002306
2307 return 0;
2308}
2309
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002310static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
Koji Sato17c76b02009-04-06 19:01:24 -07002311{
2312 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07002313 struct nilfs_btree_path *path;
2314 __u64 ptr;
2315 int ret;
2316
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002317 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002318 if (path == NULL)
2319 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002320
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002321 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002322 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002323 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002324 goto out;
2325 }
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09002326 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002327 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002328 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002329 goto out;
2330 }
2331
2332 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09002333 mark_buffer_dirty(bh);
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09002334 brelse(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002335 if (!nilfs_bmap_dirty(btree))
2336 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002337
2338 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002339 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002340 return ret;
2341}
2342
2343static const struct nilfs_bmap_operations nilfs_btree_ops = {
2344 .bop_lookup = nilfs_btree_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002345 .bop_lookup_contig = nilfs_btree_lookup_contig,
Koji Sato17c76b02009-04-06 19:01:24 -07002346 .bop_insert = nilfs_btree_insert,
2347 .bop_delete = nilfs_btree_delete,
2348 .bop_clear = NULL,
2349
2350 .bop_propagate = nilfs_btree_propagate,
2351
2352 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2353
2354 .bop_assign = nilfs_btree_assign,
2355 .bop_mark = nilfs_btree_mark,
2356
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002357 .bop_seek_key = nilfs_btree_seek_key,
Koji Sato17c76b02009-04-06 19:01:24 -07002358 .bop_last_key = nilfs_btree_last_key,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002359
Koji Sato17c76b02009-04-06 19:01:24 -07002360 .bop_check_insert = NULL,
2361 .bop_check_delete = nilfs_btree_check_delete,
2362 .bop_gather_data = nilfs_btree_gather_data,
2363};
2364
2365static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
2366 .bop_lookup = NULL,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002367 .bop_lookup_contig = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002368 .bop_insert = NULL,
2369 .bop_delete = NULL,
2370 .bop_clear = NULL,
2371
2372 .bop_propagate = nilfs_btree_propagate_gc,
2373
2374 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2375
2376 .bop_assign = nilfs_btree_assign_gc,
2377 .bop_mark = NULL,
2378
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002379 .bop_seek_key = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002380 .bop_last_key = NULL,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002381
Koji Sato17c76b02009-04-06 19:01:24 -07002382 .bop_check_insert = NULL,
2383 .bop_check_delete = NULL,
2384 .bop_gather_data = NULL,
2385};
2386
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002387static void __nilfs_btree_init(struct nilfs_bmap *bmap)
Koji Sato17c76b02009-04-06 19:01:24 -07002388{
Koji Sato17c76b02009-04-06 19:01:24 -07002389 bmap->b_ops = &nilfs_btree_ops;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002390 bmap->b_nchildren_per_block =
2391 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002392}
2393
2394int nilfs_btree_init(struct nilfs_bmap *bmap)
2395{
2396 int ret = 0;
2397
2398 __nilfs_btree_init(bmap);
2399
Ryusuke Konishifeee8802016-08-02 14:05:10 -07002400 if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap), bmap->b_inode))
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002401 ret = -EIO;
2402 return ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002403}
2404
2405void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
2406{
Koji Sato17c76b02009-04-06 19:01:24 -07002407 bmap->b_ops = &nilfs_btree_ops_gc;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002408 bmap->b_nchildren_per_block =
2409 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002410}