blob: 2e315f9f2e51d6f19fae04ced022777af1186cdf [file] [log] [blame]
Koji Sato17c76b02009-04-06 19:01:24 -07001/*
2 * btree.c - NILFS B-tree.
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Written by Koji Sato.
Koji Sato17c76b02009-04-06 19:01:24 -070017 */
18
19#include <linux/slab.h>
20#include <linux/string.h>
21#include <linux/errno.h>
22#include <linux/pagevec.h>
23#include "nilfs.h"
24#include "page.h"
25#include "btnode.h"
26#include "btree.h"
27#include "alloc.h"
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090028#include "dat.h"
Koji Sato17c76b02009-04-06 19:01:24 -070029
Ryusuke Konishi957ed602015-02-27 15:51:56 -080030static void __nilfs_btree_init(struct nilfs_bmap *bmap);
31
Li Hongf9054402010-04-02 17:36:34 +080032static struct nilfs_btree_path *nilfs_btree_alloc_path(void)
Koji Sato17c76b02009-04-06 19:01:24 -070033{
Li Hongf9054402010-04-02 17:36:34 +080034 struct nilfs_btree_path *path;
35 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070036
Li Hongf9054402010-04-02 17:36:34 +080037 path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
38 if (path == NULL)
39 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -070040
Li Hongf9054402010-04-02 17:36:34 +080041 for (; level < NILFS_BTREE_LEVEL_MAX; level++) {
Koji Sato17c76b02009-04-06 19:01:24 -070042 path[level].bp_bh = NULL;
43 path[level].bp_sib_bh = NULL;
44 path[level].bp_index = 0;
45 path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
46 path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
47 path[level].bp_op = NULL;
48 }
Li Hongf9054402010-04-02 17:36:34 +080049
50out:
51 return path;
52}
53
Li Hong73bb4882010-04-02 18:35:00 +080054static void nilfs_btree_free_path(struct nilfs_btree_path *path)
Li Hongf9054402010-04-02 17:36:34 +080055{
Li Hong73bb4882010-04-02 18:35:00 +080056 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070057
Li Hong73bb4882010-04-02 18:35:00 +080058 for (; level < NILFS_BTREE_LEVEL_MAX; level++)
Ryusuke Konishi32189292009-08-15 01:54:59 +090059 brelse(path[level].bp_bh);
Li Hong73bb4882010-04-02 18:35:00 +080060
61 kmem_cache_free(nilfs_btree_path_cache, path);
Koji Sato17c76b02009-04-06 19:01:24 -070062}
63
Koji Sato17c76b02009-04-06 19:01:24 -070064/*
65 * B-tree node operations
66 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090067static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090068 __u64 ptr, struct buffer_head **bhp)
69{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090070 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
Ryusuke Konishi45f49102009-11-13 16:25:19 +090071 struct buffer_head *bh;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090072
Ryusuke Konishi45f49102009-11-13 16:25:19 +090073 bh = nilfs_btnode_create_block(btnc, ptr);
74 if (!bh)
75 return -ENOMEM;
76
77 set_buffer_nilfs_volatile(bh);
78 *bhp = bh;
79 return 0;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090080}
Koji Sato17c76b02009-04-06 19:01:24 -070081
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090082static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070083{
84 return node->bn_flags;
85}
86
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090087static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090088nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
Koji Sato17c76b02009-04-06 19:01:24 -070089{
90 node->bn_flags = flags;
91}
92
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090093static int nilfs_btree_node_root(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070094{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090095 return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
Koji Sato17c76b02009-04-06 19:01:24 -070096}
97
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090098static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070099{
100 return node->bn_level;
101}
102
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900103static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900104nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700105{
106 node->bn_level = level;
107}
108
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900109static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700110{
111 return le16_to_cpu(node->bn_nchildren);
112}
113
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900114static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900115nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
Koji Sato17c76b02009-04-06 19:01:24 -0700116{
117 node->bn_nchildren = cpu_to_le16(nchildren);
118}
119
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900120static int nilfs_btree_node_size(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700121{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900122 return 1 << btree->b_inode->i_blkbits;
Koji Sato17c76b02009-04-06 19:01:24 -0700123}
124
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900125static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700126{
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +0900127 return btree->b_nchildren_per_block;
Koji Sato17c76b02009-04-06 19:01:24 -0700128}
129
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900130static __le64 *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900131nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700132{
133 return (__le64 *)((char *)(node + 1) +
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900134 (nilfs_btree_node_root(node) ?
Koji Sato17c76b02009-04-06 19:01:24 -0700135 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
136}
137
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900138static __le64 *
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900139nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700140{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900141 return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700142}
143
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900144static __u64
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900145nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
Koji Sato17c76b02009-04-06 19:01:24 -0700146{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900147 return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700148}
149
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900150static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900151nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -0700152{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900153 *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -0700154}
155
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900156static __u64
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900157nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
158 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700159{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900160 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700161}
162
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900163static void
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900164nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
165 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700166{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900167 *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700168}
169
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900170static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
171 int level, int nchildren, int ncmax,
Koji Sato17c76b02009-04-06 19:01:24 -0700172 const __u64 *keys, const __u64 *ptrs)
173{
174 __le64 *dkeys;
175 __le64 *dptrs;
176 int i;
177
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900178 nilfs_btree_node_set_flags(node, flags);
179 nilfs_btree_node_set_level(node, level);
180 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700181
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900182 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900183 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700184 for (i = 0; i < nchildren; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900185 dkeys[i] = cpu_to_le64(keys[i]);
186 dptrs[i] = cpu_to_le64(ptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -0700187 }
188}
189
190/* Assume the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900191static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700192 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900193 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700194{
195 __le64 *ldkeys, *rdkeys;
196 __le64 *ldptrs, *rdptrs;
197 int lnchildren, rnchildren;
198
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900199 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900200 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900201 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700202
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900203 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900204 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900205 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700206
207 memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
208 memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs));
209 memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys));
210 memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs));
211
212 lnchildren += n;
213 rnchildren -= n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900214 nilfs_btree_node_set_nchildren(left, lnchildren);
215 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700216}
217
218/* Assume that the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900219static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700220 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900221 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700222{
223 __le64 *ldkeys, *rdkeys;
224 __le64 *ldptrs, *rdptrs;
225 int lnchildren, rnchildren;
226
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900227 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900228 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900229 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700230
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900231 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900232 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900233 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700234
235 memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
236 memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs));
237 memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys));
238 memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs));
239
240 lnchildren -= n;
241 rnchildren += n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900242 nilfs_btree_node_set_nchildren(left, lnchildren);
243 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700244}
245
246/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900247static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
248 __u64 key, __u64 ptr, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700249{
250 __le64 *dkeys;
251 __le64 *dptrs;
252 int nchildren;
253
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900254 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900255 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900256 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700257 if (index < nchildren) {
258 memmove(dkeys + index + 1, dkeys + index,
259 (nchildren - index) * sizeof(*dkeys));
260 memmove(dptrs + index + 1, dptrs + index,
261 (nchildren - index) * sizeof(*dptrs));
262 }
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900263 dkeys[index] = cpu_to_le64(key);
264 dptrs[index] = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700265 nchildren++;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900266 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700267}
268
269/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900270static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
271 __u64 *keyp, __u64 *ptrp, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700272{
273 __u64 key;
274 __u64 ptr;
275 __le64 *dkeys;
276 __le64 *dptrs;
277 int nchildren;
278
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900279 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900280 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900281 key = le64_to_cpu(dkeys[index]);
282 ptr = le64_to_cpu(dptrs[index]);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900283 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700284 if (keyp != NULL)
285 *keyp = key;
286 if (ptrp != NULL)
287 *ptrp = ptr;
288
289 if (index < nchildren - 1) {
290 memmove(dkeys + index, dkeys + index + 1,
291 (nchildren - index - 1) * sizeof(*dkeys));
292 memmove(dptrs + index, dptrs + index + 1,
293 (nchildren - index - 1) * sizeof(*dptrs));
294 }
295 nchildren--;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900296 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700297}
298
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900299static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
Koji Sato17c76b02009-04-06 19:01:24 -0700300 __u64 key, int *indexp)
301{
302 __u64 nkey;
303 int index, low, high, s;
304
305 /* binary search */
306 low = 0;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900307 high = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700308 index = 0;
309 s = 0;
310 while (low <= high) {
311 index = (low + high) / 2;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900312 nkey = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700313 if (nkey == key) {
314 s = 0;
315 goto out;
316 } else if (nkey < key) {
317 low = index + 1;
318 s = -1;
319 } else {
320 high = index - 1;
321 s = 1;
322 }
323 }
324
325 /* adjust index */
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900326 if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
327 if (s > 0 && index > 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700328 index--;
329 } else if (s < 0)
330 index++;
331
332 out:
Koji Sato17c76b02009-04-06 19:01:24 -0700333 *indexp = index;
334
335 return s == 0;
336}
337
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900338/**
339 * nilfs_btree_node_broken - verify consistency of btree node
340 * @node: btree node block to be examined
341 * @size: node size (in bytes)
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700342 * @inode: host inode of btree
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900343 * @blocknr: block number
344 *
345 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
346 */
347static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700348 size_t size, struct inode *inode,
349 sector_t blocknr)
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900350{
351 int level, flags, nchildren;
352 int ret = 0;
353
354 level = nilfs_btree_node_get_level(node);
355 flags = nilfs_btree_node_get_flags(node);
356 nchildren = nilfs_btree_node_get_nchildren(node);
357
358 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
359 level >= NILFS_BTREE_LEVEL_MAX ||
360 (flags & NILFS_BTREE_NODE_ROOT) ||
361 nchildren < 0 ||
362 nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700363 nilfs_msg(inode->i_sb, KERN_CRIT,
364 "bad btree node (ino=%lu, blocknr=%llu): level = %d, flags = 0x%x, nchildren = %d",
365 inode->i_ino, (unsigned long long)blocknr, level,
366 flags, nchildren);
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900367 ret = 1;
368 }
369 return ret;
370}
371
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800372/**
373 * nilfs_btree_root_broken - verify consistency of btree root node
374 * @node: btree root node to be examined
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700375 * @inode: host inode of btree
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800376 *
377 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
378 */
379static int nilfs_btree_root_broken(const struct nilfs_btree_node *node,
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700380 struct inode *inode)
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800381{
382 int level, flags, nchildren;
383 int ret = 0;
384
385 level = nilfs_btree_node_get_level(node);
386 flags = nilfs_btree_node_get_flags(node);
387 nchildren = nilfs_btree_node_get_nchildren(node);
388
389 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
Ryusuke Konishid8fd1502015-05-05 16:24:00 -0700390 level >= NILFS_BTREE_LEVEL_MAX ||
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800391 nchildren < 0 ||
392 nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700393 nilfs_msg(inode->i_sb, KERN_CRIT,
394 "bad btree root (ino=%lu): level = %d, flags = 0x%x, nchildren = %d",
395 inode->i_ino, level, flags, nchildren);
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800396 ret = 1;
397 }
398 return ret;
399}
400
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900401int nilfs_btree_broken_node_block(struct buffer_head *bh)
402{
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700403 struct inode *inode;
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900404 int ret;
405
406 if (buffer_nilfs_checked(bh))
407 return 0;
408
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700409 inode = bh->b_page->mapping->host;
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900410 ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700411 bh->b_size, inode, bh->b_blocknr);
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900412 if (likely(!ret))
413 set_buffer_nilfs_checked(bh);
414 return ret;
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900415}
416
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900417static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900418nilfs_btree_get_root(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700419{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900420 return (struct nilfs_btree_node *)btree->b_u.u_data;
Koji Sato17c76b02009-04-06 19:01:24 -0700421}
422
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900423static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900424nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700425{
426 return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
427}
428
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900429static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900430nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700431{
432 return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
433}
434
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900435static int nilfs_btree_height(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700436{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900437 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700438}
439
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900440static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900441nilfs_btree_get_node(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700442 const struct nilfs_btree_path *path,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900443 int level, int *ncmaxp)
Koji Sato17c76b02009-04-06 19:01:24 -0700444{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900445 struct nilfs_btree_node *node;
446
447 if (level == nilfs_btree_height(btree) - 1) {
448 node = nilfs_btree_get_root(btree);
449 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
450 } else {
451 node = nilfs_btree_get_nonroot_node(path, level);
452 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
453 }
454 return node;
Koji Sato17c76b02009-04-06 19:01:24 -0700455}
456
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700457static int nilfs_btree_bad_node(const struct nilfs_bmap *btree,
458 struct nilfs_btree_node *node, int level)
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900459{
460 if (unlikely(nilfs_btree_node_get_level(node) != level)) {
461 dump_stack();
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700462 nilfs_msg(btree->b_inode->i_sb, KERN_CRIT,
463 "btree level mismatch (ino=%lu): %d != %d",
464 btree->b_inode->i_ino,
465 nilfs_btree_node_get_level(node), level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900466 return 1;
467 }
468 return 0;
469}
470
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900471struct nilfs_btree_readahead_info {
472 struct nilfs_btree_node *node; /* parent node */
473 int max_ra_blocks; /* max nof blocks to read ahead */
474 int index; /* current index on the parent node */
475 int ncmax; /* nof children in the parent node */
476};
477
478static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
479 struct buffer_head **bhp,
480 const struct nilfs_btree_readahead_info *ra)
481{
482 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
483 struct buffer_head *bh, *ra_bh;
484 sector_t submit_ptr = 0;
485 int ret;
486
Mike Christie2a222ca2016-06-05 14:31:43 -0500487 ret = nilfs_btnode_submit_block(btnc, ptr, 0, REQ_OP_READ, 0, &bh,
488 &submit_ptr);
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900489 if (ret) {
490 if (ret != -EEXIST)
491 return ret;
492 goto out_check;
493 }
494
495 if (ra) {
496 int i, n;
497 __u64 ptr2;
498
499 /* read ahead sibling nodes */
500 for (n = ra->max_ra_blocks, i = ra->index + 1;
501 n > 0 && i < ra->ncmax; n--, i++) {
502 ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
503
Mike Christie2a222ca2016-06-05 14:31:43 -0500504 ret = nilfs_btnode_submit_block(btnc, ptr2, 0,
505 REQ_OP_READ, REQ_RAHEAD,
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900506 &ra_bh, &submit_ptr);
507 if (likely(!ret || ret == -EEXIST))
508 brelse(ra_bh);
509 else if (ret != -EBUSY)
510 break;
511 if (!buffer_locked(bh))
512 goto out_no_wait;
513 }
514 }
515
516 wait_on_buffer(bh);
517
518 out_no_wait:
519 if (!buffer_uptodate(bh)) {
Ryusuke Konishi39a9dcc2016-08-02 14:05:17 -0700520 nilfs_msg(btree->b_inode->i_sb, KERN_ERR,
521 "I/O error reading b-tree node block (ino=%lu, blocknr=%llu)",
522 btree->b_inode->i_ino, (unsigned long long)ptr);
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900523 brelse(bh);
524 return -EIO;
525 }
526
527 out_check:
528 if (nilfs_btree_broken_node_block(bh)) {
529 clear_buffer_uptodate(bh);
530 brelse(bh);
531 return -EINVAL;
532 }
533
534 *bhp = bh;
535 return 0;
536}
537
538static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
539 struct buffer_head **bhp)
540{
541 return __nilfs_btree_get_block(btree, ptr, bhp, NULL);
542}
543
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900544static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700545 struct nilfs_btree_path *path,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900546 __u64 key, __u64 *ptrp, int minlevel,
547 int readahead)
Koji Sato17c76b02009-04-06 19:01:24 -0700548{
549 struct nilfs_btree_node *node;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900550 struct nilfs_btree_readahead_info p, *ra;
Koji Sato17c76b02009-04-06 19:01:24 -0700551 __u64 ptr;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900552 int level, index, found, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700553
Koji Sato17c76b02009-04-06 19:01:24 -0700554 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900555 level = nilfs_btree_node_get_level(node);
556 if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700557 return -ENOENT;
558
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900559 found = nilfs_btree_node_lookup(node, key, &index);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900560 ptr = nilfs_btree_node_get_ptr(node, index,
561 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700562 path[level].bp_bh = NULL;
563 path[level].bp_index = index;
564
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900565 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900566
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900567 while (--level >= minlevel) {
568 ra = NULL;
569 if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) {
570 p.node = nilfs_btree_get_node(btree, path, level + 1,
571 &p.ncmax);
572 p.index = index;
573 p.max_ra_blocks = 7;
574 ra = &p;
575 }
576 ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh,
577 ra);
Koji Sato17c76b02009-04-06 19:01:24 -0700578 if (ret < 0)
579 return ret;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900580
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900581 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700582 if (nilfs_btree_bad_node(btree, node, level))
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900583 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -0700584 if (!found)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900585 found = nilfs_btree_node_lookup(node, key, &index);
Koji Sato17c76b02009-04-06 19:01:24 -0700586 else
587 index = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900588 if (index < ncmax) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900589 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900590 } else {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700591 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
Koji Sato17c76b02009-04-06 19:01:24 -0700592 /* insert */
593 ptr = NILFS_BMAP_INVALID_PTR;
594 }
595 path[level].bp_index = index;
596 }
597 if (!found)
598 return -ENOENT;
599
600 if (ptrp != NULL)
601 *ptrp = ptr;
602
603 return 0;
604}
605
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900606static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700607 struct nilfs_btree_path *path,
608 __u64 *keyp, __u64 *ptrp)
609{
610 struct nilfs_btree_node *node;
611 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900612 int index, level, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700613
614 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900615 index = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700616 if (index < 0)
617 return -ENOENT;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900618 level = nilfs_btree_node_get_level(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900619 ptr = nilfs_btree_node_get_ptr(node, index,
620 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700621 path[level].bp_bh = NULL;
622 path[level].bp_index = index;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900623 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700624
625 for (level--; level > 0; level--) {
Ryusuke Konishif198dbb2009-05-22 01:07:13 +0900626 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700627 if (ret < 0)
628 return ret;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900629 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700630 if (nilfs_btree_bad_node(btree, node, level))
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900631 return -EINVAL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900632 index = nilfs_btree_node_get_nchildren(node) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900633 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700634 path[level].bp_index = index;
635 }
636
637 if (keyp != NULL)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900638 *keyp = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700639 if (ptrp != NULL)
640 *ptrp = ptr;
641
642 return 0;
643}
644
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700645/**
646 * nilfs_btree_get_next_key - get next valid key from btree path array
647 * @btree: bmap struct of btree
648 * @path: array of nilfs_btree_path struct
649 * @minlevel: start level
650 * @nextkey: place to store the next valid key
651 *
652 * Return Value: If a next key was found, 0 is returned. Otherwise,
653 * -ENOENT is returned.
654 */
655static int nilfs_btree_get_next_key(const struct nilfs_bmap *btree,
656 const struct nilfs_btree_path *path,
657 int minlevel, __u64 *nextkey)
658{
659 struct nilfs_btree_node *node;
660 int maxlevel = nilfs_btree_height(btree) - 1;
661 int index, next_adj, level;
662
663 /* Next index is already set to bp_index for leaf nodes. */
664 next_adj = 0;
665 for (level = minlevel; level <= maxlevel; level++) {
666 if (level == maxlevel)
667 node = nilfs_btree_get_root(btree);
668 else
669 node = nilfs_btree_get_nonroot_node(path, level);
670
671 index = path[level].bp_index + next_adj;
672 if (index < nilfs_btree_node_get_nchildren(node)) {
673 /* Next key is in this node */
674 *nextkey = nilfs_btree_node_get_key(node, index);
675 return 0;
676 }
677 /* For non-leaf nodes, next index is stored at bp_index + 1. */
678 next_adj = 1;
679 }
680 return -ENOENT;
681}
682
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900683static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700684 __u64 key, int level, __u64 *ptrp)
685{
Koji Sato17c76b02009-04-06 19:01:24 -0700686 struct nilfs_btree_path *path;
Koji Sato17c76b02009-04-06 19:01:24 -0700687 int ret;
688
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900689 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -0700690 if (path == NULL)
691 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -0700692
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900693 ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700694
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900695 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -0700696
697 return ret;
698}
699
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900700static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700701 __u64 key, __u64 *ptrp,
702 unsigned int maxblocks)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900703{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900704 struct nilfs_btree_path *path;
705 struct nilfs_btree_node *node;
706 struct inode *dat = NULL;
707 __u64 ptr, ptr2;
708 sector_t blocknr;
709 int level = NILFS_BTREE_LEVEL_NODE_MIN;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900710 int ret, cnt, index, maxlevel, ncmax;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900711 struct nilfs_btree_readahead_info p;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900712
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900713 path = nilfs_btree_alloc_path();
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900714 if (path == NULL)
715 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +0800716
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900717 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900718 if (ret < 0)
719 goto out;
720
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900721 if (NILFS_BMAP_USE_VBN(btree)) {
722 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900723 ret = nilfs_dat_translate(dat, ptr, &blocknr);
724 if (ret < 0)
725 goto out;
726 ptr = blocknr;
727 }
728 cnt = 1;
729 if (cnt == maxblocks)
730 goto end;
731
732 maxlevel = nilfs_btree_height(btree) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900733 node = nilfs_btree_get_node(btree, path, level, &ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900734 index = path[level].bp_index + 1;
735 for (;;) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900736 while (index < nilfs_btree_node_get_nchildren(node)) {
737 if (nilfs_btree_node_get_key(node, index) !=
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900738 key + cnt)
739 goto end;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900740 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900741 if (dat) {
742 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
743 if (ret < 0)
744 goto out;
745 ptr2 = blocknr;
746 }
747 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
748 goto end;
749 index++;
750 continue;
751 }
752 if (level == maxlevel)
753 break;
754
755 /* look-up right sibling node */
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900756 p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax);
757 p.index = path[level + 1].bp_index + 1;
758 p.max_ra_blocks = 7;
759 if (p.index >= nilfs_btree_node_get_nchildren(p.node) ||
760 nilfs_btree_node_get_key(p.node, p.index) != key + cnt)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900761 break;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900762 ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax);
763 path[level + 1].bp_index = p.index;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900764
765 brelse(path[level].bp_bh);
766 path[level].bp_bh = NULL;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900767
768 ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
769 &p);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900770 if (ret < 0)
771 goto out;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900772 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900773 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900774 index = 0;
775 path[level].bp_index = index;
776 }
777 end:
778 *ptrp = ptr;
779 ret = cnt;
780 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900781 nilfs_btree_free_path(path);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900782 return ret;
783}
784
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900785static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700786 struct nilfs_btree_path *path,
787 int level, __u64 key)
788{
789 if (level < nilfs_btree_height(btree) - 1) {
790 do {
Koji Sato17c76b02009-04-06 19:01:24 -0700791 nilfs_btree_node_set_key(
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900792 nilfs_btree_get_nonroot_node(path, level),
Koji Sato17c76b02009-04-06 19:01:24 -0700793 path[level].bp_index, key);
794 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900795 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700796 } while ((path[level].bp_index == 0) &&
797 (++level < nilfs_btree_height(btree) - 1));
798 }
799
800 /* root */
801 if (level == nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900802 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
Koji Sato17c76b02009-04-06 19:01:24 -0700803 path[level].bp_index, key);
804 }
805}
806
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900807static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700808 struct nilfs_btree_path *path,
809 int level, __u64 *keyp, __u64 *ptrp)
810{
811 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900812 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700813
814 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900815 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900816 ncblk = nilfs_btree_nchildren_per_block(btree);
817 nilfs_btree_node_insert(node, path[level].bp_index,
818 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700819 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900820 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700821
822 if (path[level].bp_index == 0)
823 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900824 nilfs_btree_node_get_key(node,
825 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700826 } else {
827 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900828 nilfs_btree_node_insert(node, path[level].bp_index,
829 *keyp, *ptrp,
830 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700831 }
832}
833
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900834static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700835 struct nilfs_btree_path *path,
836 int level, __u64 *keyp, __u64 *ptrp)
837{
838 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900839 int nchildren, lnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700840
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900841 node = nilfs_btree_get_nonroot_node(path, level);
842 left = nilfs_btree_get_sib_node(path, level);
843 nchildren = nilfs_btree_node_get_nchildren(node);
844 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900845 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700846 move = 0;
847
848 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
849 if (n > path[level].bp_index) {
850 /* move insert point */
851 n--;
852 move = 1;
853 }
854
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900855 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700856
857 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900858 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700859 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900860 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700861
Koji Sato17c76b02009-04-06 19:01:24 -0700862 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900863 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700864
865 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900866 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700867 path[level].bp_bh = path[level].bp_sib_bh;
868 path[level].bp_sib_bh = NULL;
869 path[level].bp_index += lnchildren;
870 path[level + 1].bp_index--;
871 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900872 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700873 path[level].bp_sib_bh = NULL;
874 path[level].bp_index -= n;
875 }
876
877 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
878}
879
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900880static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700881 struct nilfs_btree_path *path,
882 int level, __u64 *keyp, __u64 *ptrp)
883{
884 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900885 int nchildren, rnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700886
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900887 node = nilfs_btree_get_nonroot_node(path, level);
888 right = nilfs_btree_get_sib_node(path, level);
889 nchildren = nilfs_btree_node_get_nchildren(node);
890 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900891 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700892 move = 0;
893
894 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
895 if (n > nchildren - path[level].bp_index) {
896 /* move insert point */
897 n--;
898 move = 1;
899 }
900
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900901 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700902
903 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900904 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700905 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900906 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700907
Koji Sato17c76b02009-04-06 19:01:24 -0700908 path[level + 1].bp_index++;
909 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900910 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700911 path[level + 1].bp_index--;
912
913 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900914 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700915 path[level].bp_bh = path[level].bp_sib_bh;
916 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900917 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700918 path[level + 1].bp_index++;
919 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900920 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700921 path[level].bp_sib_bh = NULL;
922 }
923
924 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
925}
926
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900927static void nilfs_btree_split(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700928 struct nilfs_btree_path *path,
929 int level, __u64 *keyp, __u64 *ptrp)
930{
931 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900932 int nchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700933
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900934 node = nilfs_btree_get_nonroot_node(path, level);
935 right = nilfs_btree_get_sib_node(path, level);
936 nchildren = nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900937 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700938 move = 0;
939
940 n = (nchildren + 1) / 2;
941 if (n > nchildren - path[level].bp_index) {
942 n--;
943 move = 1;
944 }
945
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900946 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700947
948 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900949 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700950 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900951 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700952
Koji Sato17c76b02009-04-06 19:01:24 -0700953 if (move) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900954 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900955 nilfs_btree_node_insert(right, path[level].bp_index,
956 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700957
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_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700962 path[level].bp_bh = path[level].bp_sib_bh;
963 path[level].bp_sib_bh = NULL;
964 } else {
965 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
966
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900967 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700968 *ptrp = path[level].bp_newreq.bpr_ptr;
969
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900970 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700971 path[level].bp_sib_bh = NULL;
972 }
973
974 path[level + 1].bp_index++;
975}
976
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900977static void nilfs_btree_grow(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700978 struct nilfs_btree_path *path,
979 int level, __u64 *keyp, __u64 *ptrp)
980{
981 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900982 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700983
Koji Sato17c76b02009-04-06 19:01:24 -0700984 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900985 child = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900986 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700987
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900988 n = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -0700989
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900990 nilfs_btree_node_move_right(root, child, n,
991 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900992 nilfs_btree_node_set_level(root, level + 1);
Koji Sato17c76b02009-04-06 19:01:24 -0700993
994 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900995 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700996
Koji Sato17c76b02009-04-06 19:01:24 -0700997 path[level].bp_bh = path[level].bp_sib_bh;
998 path[level].bp_sib_bh = NULL;
999
1000 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
1001
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001002 *keyp = nilfs_btree_node_get_key(child, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001003 *ptrp = path[level].bp_newreq.bpr_ptr;
1004}
1005
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001006static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001007 const struct nilfs_btree_path *path)
1008{
1009 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001010 int level, ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001011
1012 if (path == NULL)
1013 return NILFS_BMAP_INVALID_PTR;
1014
1015 /* left sibling */
1016 level = NILFS_BTREE_LEVEL_NODE_MIN;
1017 if (path[level].bp_index > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001018 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1019 return nilfs_btree_node_get_ptr(node,
1020 path[level].bp_index - 1,
1021 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001022 }
1023
1024 /* parent */
1025 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
1026 if (level <= nilfs_btree_height(btree) - 1) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001027 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1028 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
1029 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001030 }
1031
1032 return NILFS_BMAP_INVALID_PTR;
1033}
1034
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001035static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001036 const struct nilfs_btree_path *path,
1037 __u64 key)
1038{
1039 __u64 ptr;
1040
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001041 ptr = nilfs_bmap_find_target_seq(btree, key);
Koji Sato17c76b02009-04-06 19:01:24 -07001042 if (ptr != NILFS_BMAP_INVALID_PTR)
1043 /* sequential access */
1044 return ptr;
Ryusuke Konishi7f001842016-05-23 16:23:42 -07001045
1046 ptr = nilfs_btree_find_near(btree, path);
1047 if (ptr != NILFS_BMAP_INVALID_PTR)
1048 /* near */
1049 return ptr;
1050
Koji Sato17c76b02009-04-06 19:01:24 -07001051 /* block group */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001052 return nilfs_bmap_find_target_in_group(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001053}
1054
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001055static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001056 struct nilfs_btree_path *path,
1057 int *levelp, __u64 key, __u64 ptr,
1058 struct nilfs_bmap_stats *stats)
1059{
1060 struct buffer_head *bh;
1061 struct nilfs_btree_node *node, *parent, *sib;
1062 __u64 sibptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001063 int pindex, level, ncmax, ncblk, ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001064 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001065
1066 stats->bs_nblocks = 0;
1067 level = NILFS_BTREE_LEVEL_DATA;
1068
1069 /* allocate a new ptr for data block */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001070 if (NILFS_BMAP_USE_VBN(btree)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001071 path[level].bp_newreq.bpr_ptr =
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001072 nilfs_btree_find_target_v(btree, path, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001073 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001074 }
Koji Sato17c76b02009-04-06 19:01:24 -07001075
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001076 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001077 if (ret < 0)
1078 goto err_out_data;
1079
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001080 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001081
Koji Sato17c76b02009-04-06 19:01:24 -07001082 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1083 level < nilfs_btree_height(btree) - 1;
1084 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001085 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001086 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001087 path[level].bp_op = nilfs_btree_do_insert;
1088 stats->bs_nblocks++;
1089 goto out;
1090 }
1091
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001092 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001093 pindex = path[level + 1].bp_index;
1094
1095 /* left sibling */
1096 if (pindex > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001097 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1098 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001099 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001100 if (ret < 0)
1101 goto err_out_child_node;
1102 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001103 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001104 path[level].bp_sib_bh = bh;
1105 path[level].bp_op = nilfs_btree_carry_left;
1106 stats->bs_nblocks++;
1107 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001108 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001109 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001110 }
Koji Sato17c76b02009-04-06 19:01:24 -07001111 }
1112
1113 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001114 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1115 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1116 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001117 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001118 if (ret < 0)
1119 goto err_out_child_node;
1120 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001121 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001122 path[level].bp_sib_bh = bh;
1123 path[level].bp_op = nilfs_btree_carry_right;
1124 stats->bs_nblocks++;
1125 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001126 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001127 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001128 }
Koji Sato17c76b02009-04-06 19:01:24 -07001129 }
1130
1131 /* split */
1132 path[level].bp_newreq.bpr_ptr =
1133 path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001134 ret = nilfs_bmap_prepare_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001135 &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001136 if (ret < 0)
1137 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001138 ret = nilfs_btree_get_new_block(btree,
1139 path[level].bp_newreq.bpr_ptr,
1140 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001141 if (ret < 0)
1142 goto err_out_curr_node;
1143
1144 stats->bs_nblocks++;
1145
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001146 sib = (struct nilfs_btree_node *)bh->b_data;
1147 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001148 path[level].bp_sib_bh = bh;
1149 path[level].bp_op = nilfs_btree_split;
1150 }
1151
1152 /* root */
1153 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001154 if (nilfs_btree_node_get_nchildren(node) <
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001155 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
Koji Sato17c76b02009-04-06 19:01:24 -07001156 path[level].bp_op = nilfs_btree_do_insert;
1157 stats->bs_nblocks++;
1158 goto out;
1159 }
1160
1161 /* grow */
1162 path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001163 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001164 if (ret < 0)
1165 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001166 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1167 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001168 if (ret < 0)
1169 goto err_out_curr_node;
1170
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001171 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1172 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001173 path[level].bp_sib_bh = bh;
1174 path[level].bp_op = nilfs_btree_grow;
1175
1176 level++;
1177 path[level].bp_op = nilfs_btree_do_insert;
1178
1179 /* a newly-created node block and a data block are added */
1180 stats->bs_nblocks += 2;
1181
1182 /* success */
1183 out:
1184 *levelp = level;
1185 return ret;
1186
1187 /* error */
1188 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001189 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001190 err_out_child_node:
1191 for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001192 nilfs_btnode_delete(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001193 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001194
1195 }
1196
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001197 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001198 err_out_data:
1199 *levelp = level;
1200 stats->bs_nblocks = 0;
1201 return ret;
1202}
1203
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001204static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001205 struct nilfs_btree_path *path,
1206 int maxlevel, __u64 key, __u64 ptr)
1207{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001208 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001209 int level;
1210
1211 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1212 ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001213 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001214 nilfs_bmap_set_target_v(btree, key, ptr);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001215 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001216 }
Koji Sato17c76b02009-04-06 19:01:24 -07001217
1218 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001219 nilfs_bmap_commit_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001220 &path[level - 1].bp_newreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001221 path[level].bp_op(btree, path, level, &key, &ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001222 }
1223
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001224 if (!nilfs_bmap_dirty(btree))
1225 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001226}
1227
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001228static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
Koji Sato17c76b02009-04-06 19:01:24 -07001229{
Koji Sato17c76b02009-04-06 19:01:24 -07001230 struct nilfs_btree_path *path;
1231 struct nilfs_bmap_stats stats;
1232 int level, ret;
1233
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001234 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001235 if (path == NULL)
1236 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001237
1238 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001239 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001240 if (ret != -ENOENT) {
1241 if (ret == 0)
1242 ret = -EEXIST;
1243 goto out;
1244 }
1245
1246 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1247 if (ret < 0)
1248 goto out;
1249 nilfs_btree_commit_insert(btree, path, level, key, ptr);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001250 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001251
1252 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001253 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001254 return ret;
1255}
1256
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001257static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001258 struct nilfs_btree_path *path,
1259 int level, __u64 *keyp, __u64 *ptrp)
1260{
1261 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001262 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001263
1264 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001265 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001266 ncblk = nilfs_btree_nchildren_per_block(btree);
1267 nilfs_btree_node_delete(node, path[level].bp_index,
1268 keyp, ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001269 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001270 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001271 if (path[level].bp_index == 0)
1272 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001273 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001274 } else {
1275 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001276 nilfs_btree_node_delete(node, path[level].bp_index,
1277 keyp, ptrp,
1278 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -07001279 }
1280}
1281
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001282static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001283 struct nilfs_btree_path *path,
1284 int level, __u64 *keyp, __u64 *ptrp)
1285{
1286 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001287 int nchildren, lnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001288
1289 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1290
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001291 node = nilfs_btree_get_nonroot_node(path, level);
1292 left = nilfs_btree_get_sib_node(path, level);
1293 nchildren = nilfs_btree_node_get_nchildren(node);
1294 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001295 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001296
1297 n = (nchildren + lnchildren) / 2 - nchildren;
1298
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001299 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001300
1301 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001302 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001303 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001304 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001305
Koji Sato17c76b02009-04-06 19:01:24 -07001306 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001307 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001308
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001309 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001310 path[level].bp_sib_bh = NULL;
1311 path[level].bp_index += n;
1312}
1313
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001314static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001315 struct nilfs_btree_path *path,
1316 int level, __u64 *keyp, __u64 *ptrp)
1317{
1318 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001319 int nchildren, rnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001320
1321 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1322
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001323 node = nilfs_btree_get_nonroot_node(path, level);
1324 right = nilfs_btree_get_sib_node(path, level);
1325 nchildren = nilfs_btree_node_get_nchildren(node);
1326 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001327 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001328
1329 n = (nchildren + rnchildren) / 2 - nchildren;
1330
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001331 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001332
1333 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001334 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001335 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001336 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001337
Koji Sato17c76b02009-04-06 19:01:24 -07001338 path[level + 1].bp_index++;
1339 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001340 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001341 path[level + 1].bp_index--;
1342
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001343 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001344 path[level].bp_sib_bh = NULL;
1345}
1346
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001347static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001348 struct nilfs_btree_path *path,
1349 int level, __u64 *keyp, __u64 *ptrp)
1350{
1351 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001352 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001353
1354 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1355
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001356 node = nilfs_btree_get_nonroot_node(path, level);
1357 left = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001358 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001359
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001360 n = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001361
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001362 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001363
1364 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001365 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001366
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001367 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001368 path[level].bp_bh = path[level].bp_sib_bh;
1369 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001370 path[level].bp_index += nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -07001371}
1372
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001373static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001374 struct nilfs_btree_path *path,
1375 int level, __u64 *keyp, __u64 *ptrp)
1376{
1377 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001378 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001379
1380 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1381
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001382 node = nilfs_btree_get_nonroot_node(path, level);
1383 right = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001384 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001385
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001386 n = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -07001387
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001388 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001389
1390 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001391 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001392
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001393 nilfs_btnode_delete(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001394 path[level].bp_sib_bh = NULL;
1395 path[level + 1].bp_index++;
1396}
1397
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001398static void nilfs_btree_shrink(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001399 struct nilfs_btree_path *path,
1400 int level, __u64 *keyp, __u64 *ptrp)
1401{
1402 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001403 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001404
1405 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1406
Koji Sato17c76b02009-04-06 19:01:24 -07001407 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001408 child = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001409 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001410
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001411 nilfs_btree_node_delete(root, 0, NULL, NULL,
1412 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001413 nilfs_btree_node_set_level(root, level);
1414 n = nilfs_btree_node_get_nchildren(child);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001415 nilfs_btree_node_move_left(root, child, n,
1416 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001417
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001418 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001419 path[level].bp_bh = NULL;
1420}
1421
Ryusuke Konishid4099052011-05-25 23:00:27 +09001422static void nilfs_btree_nop(struct nilfs_bmap *btree,
1423 struct nilfs_btree_path *path,
1424 int level, __u64 *keyp, __u64 *ptrp)
1425{
1426}
Koji Sato17c76b02009-04-06 19:01:24 -07001427
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001428static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001429 struct nilfs_btree_path *path,
1430 int *levelp,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001431 struct nilfs_bmap_stats *stats,
1432 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001433{
1434 struct buffer_head *bh;
1435 struct nilfs_btree_node *node, *parent, *sib;
1436 __u64 sibptr;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001437 int pindex, dindex, level, ncmin, ncmax, ncblk, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001438
1439 ret = 0;
1440 stats->bs_nblocks = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001441 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001442 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001443
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001444 for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index;
Koji Sato17c76b02009-04-06 19:01:24 -07001445 level < nilfs_btree_height(btree) - 1;
1446 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001447 node = nilfs_btree_get_nonroot_node(path, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001448 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001449 nilfs_btree_node_get_ptr(node, dindex, ncblk);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001450 ret = nilfs_bmap_prepare_end_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001451 &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001452 if (ret < 0)
1453 goto err_out_child_node;
Koji Sato17c76b02009-04-06 19:01:24 -07001454
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001455 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001456 path[level].bp_op = nilfs_btree_do_delete;
1457 stats->bs_nblocks++;
1458 goto out;
1459 }
1460
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001461 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001462 pindex = path[level + 1].bp_index;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001463 dindex = pindex;
Koji Sato17c76b02009-04-06 19:01:24 -07001464
1465 if (pindex > 0) {
1466 /* left sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001467 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1468 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001469 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001470 if (ret < 0)
1471 goto err_out_curr_node;
1472 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001473 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001474 path[level].bp_sib_bh = bh;
1475 path[level].bp_op = nilfs_btree_borrow_left;
1476 stats->bs_nblocks++;
1477 goto out;
1478 } else {
1479 path[level].bp_sib_bh = bh;
1480 path[level].bp_op = nilfs_btree_concat_left;
1481 stats->bs_nblocks++;
1482 /* continue; */
1483 }
1484 } else if (pindex <
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001485 nilfs_btree_node_get_nchildren(parent) - 1) {
Koji Sato17c76b02009-04-06 19:01:24 -07001486 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001487 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1488 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001489 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001490 if (ret < 0)
1491 goto err_out_curr_node;
1492 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001493 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001494 path[level].bp_sib_bh = bh;
1495 path[level].bp_op = nilfs_btree_borrow_right;
1496 stats->bs_nblocks++;
1497 goto out;
1498 } else {
1499 path[level].bp_sib_bh = bh;
1500 path[level].bp_op = nilfs_btree_concat_right;
1501 stats->bs_nblocks++;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001502 /*
1503 * When merging right sibling node
1504 * into the current node, pointer to
1505 * the right sibling node must be
1506 * terminated instead. The adjustment
1507 * below is required for that.
1508 */
1509 dindex = pindex + 1;
Koji Sato17c76b02009-04-06 19:01:24 -07001510 /* continue; */
1511 }
1512 } else {
1513 /* no siblings */
1514 /* the only child of the root node */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001515 WARN_ON(level != nilfs_btree_height(btree) - 2);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001516 if (nilfs_btree_node_get_nchildren(node) - 1 <=
Koji Sato17c76b02009-04-06 19:01:24 -07001517 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1518 path[level].bp_op = nilfs_btree_shrink;
1519 stats->bs_nblocks += 2;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001520 level++;
1521 path[level].bp_op = nilfs_btree_nop;
1522 goto shrink_root_child;
Koji Sato17c76b02009-04-06 19:01:24 -07001523 } else {
1524 path[level].bp_op = nilfs_btree_do_delete;
1525 stats->bs_nblocks++;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001526 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -07001527 }
Koji Sato17c76b02009-04-06 19:01:24 -07001528 }
1529 }
1530
Ryusuke Konishid4099052011-05-25 23:00:27 +09001531 /* child of the root node is deleted */
1532 path[level].bp_op = nilfs_btree_do_delete;
1533 stats->bs_nblocks++;
1534
1535shrink_root_child:
Koji Sato17c76b02009-04-06 19:01:24 -07001536 node = nilfs_btree_get_root(btree);
1537 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001538 nilfs_btree_node_get_ptr(node, dindex,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001539 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001540
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001541 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001542 if (ret < 0)
1543 goto err_out_child_node;
1544
Koji Sato17c76b02009-04-06 19:01:24 -07001545 /* success */
1546 out:
1547 *levelp = level;
1548 return ret;
1549
1550 /* error */
1551 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001552 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001553 err_out_child_node:
1554 for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001555 brelse(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001556 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001557 }
1558 *levelp = level;
1559 stats->bs_nblocks = 0;
1560 return ret;
1561}
1562
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001563static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001564 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001565 int maxlevel, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001566{
1567 int level;
1568
1569 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001570 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001571 path[level].bp_op(btree, path, level, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001572 }
1573
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001574 if (!nilfs_bmap_dirty(btree))
1575 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001576}
1577
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001578static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001579
1580{
Koji Sato17c76b02009-04-06 19:01:24 -07001581 struct nilfs_btree_path *path;
1582 struct nilfs_bmap_stats stats;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001583 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001584 int level, ret;
1585
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001586 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001587 if (path == NULL)
1588 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +08001589
Koji Sato17c76b02009-04-06 19:01:24 -07001590 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001591 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001592 if (ret < 0)
1593 goto out;
1594
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001595
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001596 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001597
1598 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001599 if (ret < 0)
1600 goto out;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001601 nilfs_btree_commit_delete(btree, path, level, dat);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001602 nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001603
1604out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001605 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001606 return ret;
1607}
1608
Ryusuke Konishi5b203842015-04-16 12:46:36 -07001609static int nilfs_btree_seek_key(const struct nilfs_bmap *btree, __u64 start,
1610 __u64 *keyp)
1611{
1612 struct nilfs_btree_path *path;
1613 const int minlevel = NILFS_BTREE_LEVEL_NODE_MIN;
1614 int ret;
1615
1616 path = nilfs_btree_alloc_path();
1617 if (!path)
1618 return -ENOMEM;
1619
1620 ret = nilfs_btree_do_lookup(btree, path, start, NULL, minlevel, 0);
1621 if (!ret)
1622 *keyp = start;
1623 else if (ret == -ENOENT)
1624 ret = nilfs_btree_get_next_key(btree, path, minlevel, keyp);
1625
1626 nilfs_btree_free_path(path);
1627 return ret;
1628}
1629
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001630static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
Koji Sato17c76b02009-04-06 19:01:24 -07001631{
Koji Sato17c76b02009-04-06 19:01:24 -07001632 struct nilfs_btree_path *path;
1633 int ret;
1634
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001635 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001636 if (path == NULL)
1637 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001638
1639 ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
1640
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001641 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001642
1643 return ret;
1644}
1645
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001646static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001647{
1648 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001649 struct nilfs_btree_node *root, *node;
1650 __u64 maxkey, nextmaxkey;
1651 __u64 ptr;
1652 int nchildren, ret;
1653
Koji Sato17c76b02009-04-06 19:01:24 -07001654 root = nilfs_btree_get_root(btree);
1655 switch (nilfs_btree_height(btree)) {
1656 case 2:
1657 bh = NULL;
1658 node = root;
1659 break;
1660 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001661 nchildren = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -07001662 if (nchildren > 1)
1663 return 0;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001664 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1665 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001666 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001667 if (ret < 0)
1668 return ret;
1669 node = (struct nilfs_btree_node *)bh->b_data;
1670 break;
1671 default:
1672 return 0;
1673 }
1674
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001675 nchildren = nilfs_btree_node_get_nchildren(node);
1676 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
Koji Sato17c76b02009-04-06 19:01:24 -07001677 nextmaxkey = (nchildren > 1) ?
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001678 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
Koji Sato17c76b02009-04-06 19:01:24 -07001679 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001680 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001681
Ryusuke Konishi30333422009-05-24 00:09:44 +09001682 return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
Koji Sato17c76b02009-04-06 19:01:24 -07001683}
1684
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001685static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001686 __u64 *keys, __u64 *ptrs, int nitems)
1687{
1688 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001689 struct nilfs_btree_node *node, *root;
1690 __le64 *dkeys;
1691 __le64 *dptrs;
1692 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001693 int nchildren, ncmax, i, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001694
Koji Sato17c76b02009-04-06 19:01:24 -07001695 root = nilfs_btree_get_root(btree);
1696 switch (nilfs_btree_height(btree)) {
1697 case 2:
1698 bh = NULL;
1699 node = root;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001700 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
Koji Sato17c76b02009-04-06 19:01:24 -07001701 break;
1702 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001703 nchildren = nilfs_btree_node_get_nchildren(root);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001704 WARN_ON(nchildren > 1);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001705 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1706 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001707 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001708 if (ret < 0)
1709 return ret;
1710 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001711 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001712 break;
1713 default:
1714 node = NULL;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001715 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -07001716 }
1717
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001718 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001719 if (nchildren < nitems)
1720 nitems = nchildren;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001721 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001722 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001723 for (i = 0; i < nitems; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09001724 keys[i] = le64_to_cpu(dkeys[i]);
1725 ptrs[i] = le64_to_cpu(dptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -07001726 }
1727
1728 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001729 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001730
1731 return nitems;
1732}
1733
1734static int
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001735nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
Koji Sato17c76b02009-04-06 19:01:24 -07001736 union nilfs_bmap_ptr_req *dreq,
1737 union nilfs_bmap_ptr_req *nreq,
1738 struct buffer_head **bhp,
1739 struct nilfs_bmap_stats *stats)
1740{
1741 struct buffer_head *bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001742 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001743 int ret;
1744
Koji Sato17c76b02009-04-06 19:01:24 -07001745 stats->bs_nblocks = 0;
1746
1747 /* for data */
1748 /* cannot find near ptr */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001749 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001750 dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001751 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001752 }
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001753
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001754 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001755 if (ret < 0)
1756 return ret;
1757
1758 *bhp = NULL;
1759 stats->bs_nblocks++;
1760 if (nreq != NULL) {
1761 nreq->bpr_ptr = dreq->bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001762 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001763 if (ret < 0)
1764 goto err_out_dreq;
1765
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001766 ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001767 if (ret < 0)
1768 goto err_out_nreq;
1769
1770 *bhp = bh;
1771 stats->bs_nblocks++;
1772 }
1773
1774 /* success */
1775 return 0;
1776
1777 /* error */
1778 err_out_nreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001779 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001780 err_out_dreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001781 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001782 stats->bs_nblocks = 0;
1783 return ret;
1784
1785}
1786
1787static void
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001788nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001789 __u64 key, __u64 ptr,
1790 const __u64 *keys, const __u64 *ptrs,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001791 int n,
Koji Sato17c76b02009-04-06 19:01:24 -07001792 union nilfs_bmap_ptr_req *dreq,
1793 union nilfs_bmap_ptr_req *nreq,
1794 struct buffer_head *bh)
1795{
Koji Sato17c76b02009-04-06 19:01:24 -07001796 struct nilfs_btree_node *node;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001797 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001798 __u64 tmpptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001799 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001800
1801 /* free resources */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001802 if (btree->b_ops->bop_clear != NULL)
1803 btree->b_ops->bop_clear(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001804
1805 /* ptr must be a pointer to a buffer head. */
1806 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1807
1808 /* convert and insert */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001809 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi957ed602015-02-27 15:51:56 -08001810 __nilfs_btree_init(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001811 if (nreq != NULL) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001812 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1813 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001814
1815 /* create child node at level 1 */
Koji Sato17c76b02009-04-06 19:01:24 -07001816 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001817 ncblk = nilfs_btree_nchildren_per_block(btree);
1818 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1819 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001820 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001821 mark_buffer_dirty(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001822 if (!nilfs_bmap_dirty(btree))
1823 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001824
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001825 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001826
1827 /* create root node at level 2 */
1828 node = nilfs_btree_get_root(btree);
1829 tmpptr = nreq->bpr_ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001830 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1831 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1832 &keys[0], &tmpptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001833 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001834 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001835
1836 /* create root node at level 1 */
1837 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001838 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1839 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1840 keys, ptrs);
1841 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1842 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001843 if (!nilfs_bmap_dirty(btree))
1844 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001845 }
1846
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001847 if (NILFS_BMAP_USE_VBN(btree))
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001848 nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001849}
1850
1851/**
1852 * nilfs_btree_convert_and_insert -
1853 * @bmap:
1854 * @key:
1855 * @ptr:
1856 * @keys:
1857 * @ptrs:
1858 * @n:
Koji Sato17c76b02009-04-06 19:01:24 -07001859 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001860int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001861 __u64 key, __u64 ptr,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001862 const __u64 *keys, const __u64 *ptrs, int n)
Koji Sato17c76b02009-04-06 19:01:24 -07001863{
Ryusuke Konishi4f050282015-11-06 16:32:16 -08001864 struct buffer_head *bh = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001865 union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
1866 struct nilfs_bmap_stats stats;
1867 int ret;
1868
1869 if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1870 di = &dreq;
1871 ni = NULL;
1872 } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001873 1 << btree->b_inode->i_blkbits)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001874 di = &dreq;
1875 ni = &nreq;
1876 } else {
1877 di = NULL;
1878 ni = NULL;
1879 BUG();
1880 }
1881
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001882 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
Koji Sato17c76b02009-04-06 19:01:24 -07001883 &stats);
1884 if (ret < 0)
1885 return ret;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001886 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001887 di, ni, bh);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001888 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001889 return 0;
1890}
1891
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001892static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001893 struct nilfs_btree_path *path,
1894 int level,
1895 struct buffer_head *bh)
1896{
1897 while ((++level < nilfs_btree_height(btree) - 1) &&
1898 !buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001899 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001900
1901 return 0;
1902}
1903
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001904static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001905 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001906 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001907{
1908 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001909 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001910
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001911 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001912 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001913 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1914 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001915 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001916 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1917 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001918 if (ret < 0)
1919 return ret;
1920
1921 if (buffer_nilfs_node(path[level].bp_bh)) {
1922 path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
1923 path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
1924 path[level].bp_ctxt.bh = path[level].bp_bh;
1925 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001926 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001927 &path[level].bp_ctxt);
1928 if (ret < 0) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001929 nilfs_dat_abort_update(dat,
1930 &path[level].bp_oldreq.bpr_req,
1931 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001932 return ret;
1933 }
1934 }
1935
1936 return 0;
1937}
1938
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001939static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001940 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001941 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001942{
1943 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001944 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001945
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001946 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1947 &path[level].bp_newreq.bpr_req,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001948 btree->b_ptr_type == NILFS_BMAP_PTR_VS);
Koji Sato17c76b02009-04-06 19:01:24 -07001949
1950 if (buffer_nilfs_node(path[level].bp_bh)) {
1951 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001952 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001953 &path[level].bp_ctxt);
1954 path[level].bp_bh = path[level].bp_ctxt.bh;
1955 }
1956 set_buffer_nilfs_volatile(path[level].bp_bh);
1957
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001958 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1959 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1960 path[level].bp_newreq.bpr_ptr, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001961}
1962
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001963static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001964 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001965 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001966{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001967 nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
1968 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001969 if (buffer_nilfs_node(path[level].bp_bh))
1970 nilfs_btnode_abort_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001971 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001972 &path[level].bp_ctxt);
1973}
1974
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001975static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001976 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001977 int minlevel, int *maxlevelp,
1978 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001979{
1980 int level, ret;
1981
1982 level = minlevel;
1983 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001984 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001985 if (ret < 0)
1986 return ret;
1987 }
1988 while ((++level < nilfs_btree_height(btree) - 1) &&
1989 !buffer_dirty(path[level].bp_bh)) {
1990
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001991 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001992 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001993 if (ret < 0)
1994 goto out;
1995 }
1996
1997 /* success */
Koji Sato17c76b02009-04-06 19:01:24 -07001998 *maxlevelp = level - 1;
1999 return 0;
2000
2001 /* error */
2002 out:
2003 while (--level > minlevel)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002004 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002005 if (!buffer_nilfs_volatile(path[level].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002006 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002007 return ret;
2008}
2009
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002010static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002011 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002012 int minlevel, int maxlevel,
2013 struct buffer_head *bh,
2014 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07002015{
2016 int level;
2017
2018 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002019 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002020
2021 for (level = minlevel + 1; level <= maxlevel; level++)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002022 nilfs_btree_commit_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002023}
2024
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002025static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002026 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002027 int level, struct buffer_head *bh)
Koji Sato17c76b02009-04-06 19:01:24 -07002028{
Li Hong308f4412010-04-02 18:40:39 +08002029 int maxlevel = 0, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002030 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002031 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002032 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002033 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07002034
2035 get_bh(bh);
2036 path[level].bp_bh = bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002037 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
2038 dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002039 if (ret < 0)
2040 goto out;
2041
2042 if (buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002043 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2044 ptr = nilfs_btree_node_get_ptr(parent,
2045 path[level + 1].bp_index,
2046 ncmax);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002047 ret = nilfs_dat_mark_dirty(dat, ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07002048 if (ret < 0)
2049 goto out;
2050 }
2051
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002052 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002053
2054 out:
2055 brelse(path[level].bp_bh);
2056 path[level].bp_bh = NULL;
2057 return ret;
2058}
2059
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002060static int nilfs_btree_propagate(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002061 struct buffer_head *bh)
2062{
Koji Sato17c76b02009-04-06 19:01:24 -07002063 struct nilfs_btree_path *path;
2064 struct nilfs_btree_node *node;
2065 __u64 key;
2066 int level, ret;
2067
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002068 WARN_ON(!buffer_dirty(bh));
Koji Sato17c76b02009-04-06 19:01:24 -07002069
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002070 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002071 if (path == NULL)
2072 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002073
2074 if (buffer_nilfs_node(bh)) {
2075 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002076 key = nilfs_btree_node_get_key(node, 0);
2077 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002078 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002079 key = nilfs_bmap_data_get_key(btree, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002080 level = NILFS_BTREE_LEVEL_DATA;
2081 }
2082
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002083 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002084 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002085 if (unlikely(ret == -ENOENT))
Ryusuke Konishifeee8802016-08-02 14:05:10 -07002086 nilfs_msg(btree->b_inode->i_sb, KERN_CRIT,
2087 "writing node/leaf block does not appear in b-tree (ino=%lu) at key=%llu, level=%d",
2088 btree->b_inode->i_ino,
2089 (unsigned long long)key, level);
Koji Sato17c76b02009-04-06 19:01:24 -07002090 goto out;
2091 }
2092
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002093 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002094 nilfs_btree_propagate_v(btree, path, level, bh) :
2095 nilfs_btree_propagate_p(btree, path, level, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002096
2097 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002098 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002099
2100 return ret;
2101}
2102
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002103static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002104 struct buffer_head *bh)
2105{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002106 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002107}
2108
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002109static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002110 struct list_head *lists,
2111 struct buffer_head *bh)
2112{
2113 struct list_head *head;
2114 struct buffer_head *cbh;
2115 struct nilfs_btree_node *node, *cnode;
2116 __u64 key, ckey;
2117 int level;
2118
2119 get_bh(bh);
2120 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002121 key = nilfs_btree_node_get_key(node, 0);
2122 level = nilfs_btree_node_get_level(node);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002123 if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
2124 level >= NILFS_BTREE_LEVEL_MAX) {
2125 dump_stack();
Ryusuke Konishifeee8802016-08-02 14:05:10 -07002126 nilfs_msg(btree->b_inode->i_sb, KERN_WARNING,
2127 "invalid btree level: %d (key=%llu, ino=%lu, blocknr=%llu)",
2128 level, (unsigned long long)key,
2129 btree->b_inode->i_ino,
2130 (unsigned long long)bh->b_blocknr);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002131 return;
2132 }
2133
Koji Sato17c76b02009-04-06 19:01:24 -07002134 list_for_each(head, &lists[level]) {
2135 cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
2136 cnode = (struct nilfs_btree_node *)cbh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002137 ckey = nilfs_btree_node_get_key(cnode, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002138 if (key < ckey)
2139 break;
2140 }
2141 list_add_tail(&bh->b_assoc_buffers, head);
2142}
2143
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002144static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002145 struct list_head *listp)
2146{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002147 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
Koji Sato17c76b02009-04-06 19:01:24 -07002148 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2149 struct pagevec pvec;
2150 struct buffer_head *bh, *head;
2151 pgoff_t index = 0;
2152 int level, i;
2153
2154 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2155 level < NILFS_BTREE_LEVEL_MAX;
2156 level++)
2157 INIT_LIST_HEAD(&lists[level]);
2158
2159 pagevec_init(&pvec, 0);
2160
2161 while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
2162 PAGEVEC_SIZE)) {
2163 for (i = 0; i < pagevec_count(&pvec); i++) {
2164 bh = head = page_buffers(pvec.pages[i]);
2165 do {
2166 if (buffer_dirty(bh))
2167 nilfs_btree_add_dirty_buffer(btree,
2168 lists, bh);
2169 } while ((bh = bh->b_this_page) != head);
2170 }
2171 pagevec_release(&pvec);
2172 cond_resched();
2173 }
2174
2175 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2176 level < NILFS_BTREE_LEVEL_MAX;
2177 level++)
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002178 list_splice_tail(&lists[level], listp);
Koji Sato17c76b02009-04-06 19:01:24 -07002179}
2180
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002181static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002182 struct nilfs_btree_path *path,
2183 int level,
2184 struct buffer_head **bh,
2185 sector_t blocknr,
2186 union nilfs_binfo *binfo)
2187{
2188 struct nilfs_btree_node *parent;
2189 __u64 key;
2190 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002191 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002192
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002193 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2194 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2195 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002196 if (buffer_nilfs_node(*bh)) {
2197 path[level].bp_ctxt.oldkey = ptr;
2198 path[level].bp_ctxt.newkey = blocknr;
2199 path[level].bp_ctxt.bh = *bh;
2200 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002201 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002202 &path[level].bp_ctxt);
2203 if (ret < 0)
2204 return ret;
2205 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002206 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002207 &path[level].bp_ctxt);
2208 *bh = path[level].bp_ctxt.bh;
2209 }
2210
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002211 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2212 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002213
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002214 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002215 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002216 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002217 binfo->bi_dat.bi_level = level;
2218
2219 return 0;
2220}
2221
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002222static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002223 struct nilfs_btree_path *path,
2224 int level,
2225 struct buffer_head **bh,
2226 sector_t blocknr,
2227 union nilfs_binfo *binfo)
2228{
2229 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002230 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002231 __u64 key;
2232 __u64 ptr;
2233 union nilfs_bmap_ptr_req req;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002234 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002235
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002236 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2237 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2238 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002239 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002240 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2241 if (ret < 0)
Koji Sato17c76b02009-04-06 19:01:24 -07002242 return ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002243 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002244
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002245 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002246 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002247 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
2248 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002249
2250 return 0;
2251}
2252
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002253static int nilfs_btree_assign(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002254 struct buffer_head **bh,
2255 sector_t blocknr,
2256 union nilfs_binfo *binfo)
2257{
Koji Sato17c76b02009-04-06 19:01:24 -07002258 struct nilfs_btree_path *path;
2259 struct nilfs_btree_node *node;
2260 __u64 key;
2261 int level, ret;
2262
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002263 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002264 if (path == NULL)
2265 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002266
2267 if (buffer_nilfs_node(*bh)) {
2268 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002269 key = nilfs_btree_node_get_key(node, 0);
2270 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002271 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002272 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002273 level = NILFS_BTREE_LEVEL_DATA;
2274 }
2275
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002276 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002277 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002278 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002279 goto out;
2280 }
2281
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002282 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002283 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2284 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
Koji Sato17c76b02009-04-06 19:01:24 -07002285
2286 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002287 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002288
2289 return ret;
2290}
2291
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002292static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002293 struct buffer_head **bh,
2294 sector_t blocknr,
2295 union nilfs_binfo *binfo)
2296{
Koji Sato17c76b02009-04-06 19:01:24 -07002297 struct nilfs_btree_node *node;
2298 __u64 key;
2299 int ret;
2300
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002301 ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002302 blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002303 if (ret < 0)
2304 return ret;
2305
2306 if (buffer_nilfs_node(*bh)) {
2307 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002308 key = nilfs_btree_node_get_key(node, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002309 } else
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002310 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002311
2312 /* on-disk format */
2313 binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002314 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002315
2316 return 0;
2317}
2318
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002319static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
Koji Sato17c76b02009-04-06 19:01:24 -07002320{
2321 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07002322 struct nilfs_btree_path *path;
2323 __u64 ptr;
2324 int ret;
2325
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002326 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002327 if (path == NULL)
2328 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002329
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002330 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002331 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002332 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002333 goto out;
2334 }
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09002335 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002336 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002337 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002338 goto out;
2339 }
2340
2341 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09002342 mark_buffer_dirty(bh);
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09002343 brelse(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002344 if (!nilfs_bmap_dirty(btree))
2345 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002346
2347 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002348 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002349 return ret;
2350}
2351
2352static const struct nilfs_bmap_operations nilfs_btree_ops = {
2353 .bop_lookup = nilfs_btree_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002354 .bop_lookup_contig = nilfs_btree_lookup_contig,
Koji Sato17c76b02009-04-06 19:01:24 -07002355 .bop_insert = nilfs_btree_insert,
2356 .bop_delete = nilfs_btree_delete,
2357 .bop_clear = NULL,
2358
2359 .bop_propagate = nilfs_btree_propagate,
2360
2361 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2362
2363 .bop_assign = nilfs_btree_assign,
2364 .bop_mark = nilfs_btree_mark,
2365
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002366 .bop_seek_key = nilfs_btree_seek_key,
Koji Sato17c76b02009-04-06 19:01:24 -07002367 .bop_last_key = nilfs_btree_last_key,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002368
Koji Sato17c76b02009-04-06 19:01:24 -07002369 .bop_check_insert = NULL,
2370 .bop_check_delete = nilfs_btree_check_delete,
2371 .bop_gather_data = nilfs_btree_gather_data,
2372};
2373
2374static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
2375 .bop_lookup = NULL,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002376 .bop_lookup_contig = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002377 .bop_insert = NULL,
2378 .bop_delete = NULL,
2379 .bop_clear = NULL,
2380
2381 .bop_propagate = nilfs_btree_propagate_gc,
2382
2383 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2384
2385 .bop_assign = nilfs_btree_assign_gc,
2386 .bop_mark = NULL,
2387
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002388 .bop_seek_key = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002389 .bop_last_key = NULL,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002390
Koji Sato17c76b02009-04-06 19:01:24 -07002391 .bop_check_insert = NULL,
2392 .bop_check_delete = NULL,
2393 .bop_gather_data = NULL,
2394};
2395
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002396static void __nilfs_btree_init(struct nilfs_bmap *bmap)
Koji Sato17c76b02009-04-06 19:01:24 -07002397{
Koji Sato17c76b02009-04-06 19:01:24 -07002398 bmap->b_ops = &nilfs_btree_ops;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002399 bmap->b_nchildren_per_block =
2400 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002401}
2402
2403int nilfs_btree_init(struct nilfs_bmap *bmap)
2404{
2405 int ret = 0;
2406
2407 __nilfs_btree_init(bmap);
2408
Ryusuke Konishifeee8802016-08-02 14:05:10 -07002409 if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap), bmap->b_inode))
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002410 ret = -EIO;
2411 return ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002412}
2413
2414void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
2415{
Koji Sato17c76b02009-04-06 19:01:24 -07002416 bmap->b_ops = &nilfs_btree_ops_gc;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002417 bmap->b_nchildren_per_block =
2418 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002419}