blob: eccb1c89ccbb20b2a597798a11cbe7c4e802a0e9 [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)
342 * @blocknr: block number
343 *
344 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
345 */
346static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
347 size_t size, sector_t blocknr)
348{
349 int level, flags, nchildren;
350 int ret = 0;
351
352 level = nilfs_btree_node_get_level(node);
353 flags = nilfs_btree_node_get_flags(node);
354 nchildren = nilfs_btree_node_get_nchildren(node);
355
356 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
357 level >= NILFS_BTREE_LEVEL_MAX ||
358 (flags & NILFS_BTREE_NODE_ROOT) ||
359 nchildren < 0 ||
360 nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
361 printk(KERN_CRIT "NILFS: bad btree node (blocknr=%llu): "
362 "level = %d, flags = 0x%x, nchildren = %d\n",
363 (unsigned long long)blocknr, level, flags, nchildren);
364 ret = 1;
365 }
366 return ret;
367}
368
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800369/**
370 * nilfs_btree_root_broken - verify consistency of btree root node
371 * @node: btree root node to be examined
372 * @ino: inode number
373 *
374 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
375 */
376static int nilfs_btree_root_broken(const struct nilfs_btree_node *node,
377 unsigned long ino)
378{
379 int level, flags, nchildren;
380 int ret = 0;
381
382 level = nilfs_btree_node_get_level(node);
383 flags = nilfs_btree_node_get_flags(node);
384 nchildren = nilfs_btree_node_get_nchildren(node);
385
386 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
Ryusuke Konishid8fd1502015-05-05 16:24:00 -0700387 level >= NILFS_BTREE_LEVEL_MAX ||
Ryusuke Konishi957ed602015-02-27 15:51:56 -0800388 nchildren < 0 ||
389 nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) {
390 pr_crit("NILFS: bad btree root (inode number=%lu): level = %d, flags = 0x%x, nchildren = %d\n",
391 ino, level, flags, nchildren);
392 ret = 1;
393 }
394 return ret;
395}
396
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900397int nilfs_btree_broken_node_block(struct buffer_head *bh)
398{
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900399 int ret;
400
401 if (buffer_nilfs_checked(bh))
402 return 0;
403
404 ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900405 bh->b_size, bh->b_blocknr);
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900406 if (likely(!ret))
407 set_buffer_nilfs_checked(bh);
408 return ret;
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900409}
410
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900411static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900412nilfs_btree_get_root(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700413{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900414 return (struct nilfs_btree_node *)btree->b_u.u_data;
Koji Sato17c76b02009-04-06 19:01:24 -0700415}
416
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900417static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900418nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700419{
420 return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
421}
422
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900423static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900424nilfs_btree_get_sib_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_sib_bh->b_data;
427}
428
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900429static int nilfs_btree_height(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700430{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900431 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700432}
433
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900434static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900435nilfs_btree_get_node(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700436 const struct nilfs_btree_path *path,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900437 int level, int *ncmaxp)
Koji Sato17c76b02009-04-06 19:01:24 -0700438{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900439 struct nilfs_btree_node *node;
440
441 if (level == nilfs_btree_height(btree) - 1) {
442 node = nilfs_btree_get_root(btree);
443 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
444 } else {
445 node = nilfs_btree_get_nonroot_node(path, level);
446 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
447 }
448 return node;
Koji Sato17c76b02009-04-06 19:01:24 -0700449}
450
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900451static int
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900452nilfs_btree_bad_node(struct nilfs_btree_node *node, int level)
453{
454 if (unlikely(nilfs_btree_node_get_level(node) != level)) {
455 dump_stack();
456 printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n",
457 nilfs_btree_node_get_level(node), level);
458 return 1;
459 }
460 return 0;
461}
462
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900463struct nilfs_btree_readahead_info {
464 struct nilfs_btree_node *node; /* parent node */
465 int max_ra_blocks; /* max nof blocks to read ahead */
466 int index; /* current index on the parent node */
467 int ncmax; /* nof children in the parent node */
468};
469
470static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
471 struct buffer_head **bhp,
472 const struct nilfs_btree_readahead_info *ra)
473{
474 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
475 struct buffer_head *bh, *ra_bh;
476 sector_t submit_ptr = 0;
477 int ret;
478
479 ret = nilfs_btnode_submit_block(btnc, ptr, 0, READ, &bh, &submit_ptr);
480 if (ret) {
481 if (ret != -EEXIST)
482 return ret;
483 goto out_check;
484 }
485
486 if (ra) {
487 int i, n;
488 __u64 ptr2;
489
490 /* read ahead sibling nodes */
491 for (n = ra->max_ra_blocks, i = ra->index + 1;
492 n > 0 && i < ra->ncmax; n--, i++) {
493 ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
494
495 ret = nilfs_btnode_submit_block(btnc, ptr2, 0, READA,
496 &ra_bh, &submit_ptr);
497 if (likely(!ret || ret == -EEXIST))
498 brelse(ra_bh);
499 else if (ret != -EBUSY)
500 break;
501 if (!buffer_locked(bh))
502 goto out_no_wait;
503 }
504 }
505
506 wait_on_buffer(bh);
507
508 out_no_wait:
509 if (!buffer_uptodate(bh)) {
510 brelse(bh);
511 return -EIO;
512 }
513
514 out_check:
515 if (nilfs_btree_broken_node_block(bh)) {
516 clear_buffer_uptodate(bh);
517 brelse(bh);
518 return -EINVAL;
519 }
520
521 *bhp = bh;
522 return 0;
523}
524
525static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
526 struct buffer_head **bhp)
527{
528 return __nilfs_btree_get_block(btree, ptr, bhp, NULL);
529}
530
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900531static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700532 struct nilfs_btree_path *path,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900533 __u64 key, __u64 *ptrp, int minlevel,
534 int readahead)
Koji Sato17c76b02009-04-06 19:01:24 -0700535{
536 struct nilfs_btree_node *node;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900537 struct nilfs_btree_readahead_info p, *ra;
Koji Sato17c76b02009-04-06 19:01:24 -0700538 __u64 ptr;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900539 int level, index, found, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700540
Koji Sato17c76b02009-04-06 19:01:24 -0700541 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900542 level = nilfs_btree_node_get_level(node);
543 if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700544 return -ENOENT;
545
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900546 found = nilfs_btree_node_lookup(node, key, &index);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900547 ptr = nilfs_btree_node_get_ptr(node, index,
548 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700549 path[level].bp_bh = NULL;
550 path[level].bp_index = index;
551
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900552 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900553
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900554 while (--level >= minlevel) {
555 ra = NULL;
556 if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) {
557 p.node = nilfs_btree_get_node(btree, path, level + 1,
558 &p.ncmax);
559 p.index = index;
560 p.max_ra_blocks = 7;
561 ra = &p;
562 }
563 ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh,
564 ra);
Koji Sato17c76b02009-04-06 19:01:24 -0700565 if (ret < 0)
566 return ret;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900567
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900568 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900569 if (nilfs_btree_bad_node(node, level))
570 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -0700571 if (!found)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900572 found = nilfs_btree_node_lookup(node, key, &index);
Koji Sato17c76b02009-04-06 19:01:24 -0700573 else
574 index = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900575 if (index < ncmax) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900576 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900577 } else {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700578 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
Koji Sato17c76b02009-04-06 19:01:24 -0700579 /* insert */
580 ptr = NILFS_BMAP_INVALID_PTR;
581 }
582 path[level].bp_index = index;
583 }
584 if (!found)
585 return -ENOENT;
586
587 if (ptrp != NULL)
588 *ptrp = ptr;
589
590 return 0;
591}
592
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900593static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700594 struct nilfs_btree_path *path,
595 __u64 *keyp, __u64 *ptrp)
596{
597 struct nilfs_btree_node *node;
598 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900599 int index, level, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700600
601 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900602 index = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700603 if (index < 0)
604 return -ENOENT;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900605 level = nilfs_btree_node_get_level(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900606 ptr = nilfs_btree_node_get_ptr(node, index,
607 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700608 path[level].bp_bh = NULL;
609 path[level].bp_index = index;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900610 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700611
612 for (level--; level > 0; level--) {
Ryusuke Konishif198dbb2009-05-22 01:07:13 +0900613 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700614 if (ret < 0)
615 return ret;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900616 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900617 if (nilfs_btree_bad_node(node, level))
618 return -EINVAL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900619 index = nilfs_btree_node_get_nchildren(node) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900620 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700621 path[level].bp_index = index;
622 }
623
624 if (keyp != NULL)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900625 *keyp = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700626 if (ptrp != NULL)
627 *ptrp = ptr;
628
629 return 0;
630}
631
Ryusuke Konishi5b203842015-04-16 12:46:36 -0700632/**
633 * nilfs_btree_get_next_key - get next valid key from btree path array
634 * @btree: bmap struct of btree
635 * @path: array of nilfs_btree_path struct
636 * @minlevel: start level
637 * @nextkey: place to store the next valid key
638 *
639 * Return Value: If a next key was found, 0 is returned. Otherwise,
640 * -ENOENT is returned.
641 */
642static int nilfs_btree_get_next_key(const struct nilfs_bmap *btree,
643 const struct nilfs_btree_path *path,
644 int minlevel, __u64 *nextkey)
645{
646 struct nilfs_btree_node *node;
647 int maxlevel = nilfs_btree_height(btree) - 1;
648 int index, next_adj, level;
649
650 /* Next index is already set to bp_index for leaf nodes. */
651 next_adj = 0;
652 for (level = minlevel; level <= maxlevel; level++) {
653 if (level == maxlevel)
654 node = nilfs_btree_get_root(btree);
655 else
656 node = nilfs_btree_get_nonroot_node(path, level);
657
658 index = path[level].bp_index + next_adj;
659 if (index < nilfs_btree_node_get_nchildren(node)) {
660 /* Next key is in this node */
661 *nextkey = nilfs_btree_node_get_key(node, index);
662 return 0;
663 }
664 /* For non-leaf nodes, next index is stored at bp_index + 1. */
665 next_adj = 1;
666 }
667 return -ENOENT;
668}
669
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900670static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700671 __u64 key, int level, __u64 *ptrp)
672{
Koji Sato17c76b02009-04-06 19:01:24 -0700673 struct nilfs_btree_path *path;
Koji Sato17c76b02009-04-06 19:01:24 -0700674 int ret;
675
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900676 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -0700677 if (path == NULL)
678 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -0700679
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900680 ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700681
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900682 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -0700683
684 return ret;
685}
686
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900687static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700688 __u64 key, __u64 *ptrp,
689 unsigned int maxblocks)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900690{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900691 struct nilfs_btree_path *path;
692 struct nilfs_btree_node *node;
693 struct inode *dat = NULL;
694 __u64 ptr, ptr2;
695 sector_t blocknr;
696 int level = NILFS_BTREE_LEVEL_NODE_MIN;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900697 int ret, cnt, index, maxlevel, ncmax;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900698 struct nilfs_btree_readahead_info p;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900699
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900700 path = nilfs_btree_alloc_path();
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900701 if (path == NULL)
702 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +0800703
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900704 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900705 if (ret < 0)
706 goto out;
707
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900708 if (NILFS_BMAP_USE_VBN(btree)) {
709 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900710 ret = nilfs_dat_translate(dat, ptr, &blocknr);
711 if (ret < 0)
712 goto out;
713 ptr = blocknr;
714 }
715 cnt = 1;
716 if (cnt == maxblocks)
717 goto end;
718
719 maxlevel = nilfs_btree_height(btree) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900720 node = nilfs_btree_get_node(btree, path, level, &ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900721 index = path[level].bp_index + 1;
722 for (;;) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900723 while (index < nilfs_btree_node_get_nchildren(node)) {
724 if (nilfs_btree_node_get_key(node, index) !=
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900725 key + cnt)
726 goto end;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900727 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900728 if (dat) {
729 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
730 if (ret < 0)
731 goto out;
732 ptr2 = blocknr;
733 }
734 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
735 goto end;
736 index++;
737 continue;
738 }
739 if (level == maxlevel)
740 break;
741
742 /* look-up right sibling node */
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900743 p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax);
744 p.index = path[level + 1].bp_index + 1;
745 p.max_ra_blocks = 7;
746 if (p.index >= nilfs_btree_node_get_nchildren(p.node) ||
747 nilfs_btree_node_get_key(p.node, p.index) != key + cnt)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900748 break;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900749 ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax);
750 path[level + 1].bp_index = p.index;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900751
752 brelse(path[level].bp_bh);
753 path[level].bp_bh = NULL;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900754
755 ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
756 &p);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900757 if (ret < 0)
758 goto out;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900759 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900760 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900761 index = 0;
762 path[level].bp_index = index;
763 }
764 end:
765 *ptrp = ptr;
766 ret = cnt;
767 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900768 nilfs_btree_free_path(path);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900769 return ret;
770}
771
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900772static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700773 struct nilfs_btree_path *path,
774 int level, __u64 key)
775{
776 if (level < nilfs_btree_height(btree) - 1) {
777 do {
Koji Sato17c76b02009-04-06 19:01:24 -0700778 nilfs_btree_node_set_key(
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900779 nilfs_btree_get_nonroot_node(path, level),
Koji Sato17c76b02009-04-06 19:01:24 -0700780 path[level].bp_index, key);
781 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900782 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700783 } while ((path[level].bp_index == 0) &&
784 (++level < nilfs_btree_height(btree) - 1));
785 }
786
787 /* root */
788 if (level == nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900789 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
Koji Sato17c76b02009-04-06 19:01:24 -0700790 path[level].bp_index, key);
791 }
792}
793
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900794static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700795 struct nilfs_btree_path *path,
796 int level, __u64 *keyp, __u64 *ptrp)
797{
798 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900799 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700800
801 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900802 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900803 ncblk = nilfs_btree_nchildren_per_block(btree);
804 nilfs_btree_node_insert(node, path[level].bp_index,
805 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700806 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900807 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700808
809 if (path[level].bp_index == 0)
810 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900811 nilfs_btree_node_get_key(node,
812 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700813 } else {
814 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900815 nilfs_btree_node_insert(node, path[level].bp_index,
816 *keyp, *ptrp,
817 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700818 }
819}
820
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900821static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700822 struct nilfs_btree_path *path,
823 int level, __u64 *keyp, __u64 *ptrp)
824{
825 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900826 int nchildren, lnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700827
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900828 node = nilfs_btree_get_nonroot_node(path, level);
829 left = nilfs_btree_get_sib_node(path, level);
830 nchildren = nilfs_btree_node_get_nchildren(node);
831 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900832 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700833 move = 0;
834
835 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
836 if (n > path[level].bp_index) {
837 /* move insert point */
838 n--;
839 move = 1;
840 }
841
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900842 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700843
844 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900845 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700846 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900847 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700848
Koji Sato17c76b02009-04-06 19:01:24 -0700849 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900850 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700851
852 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900853 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700854 path[level].bp_bh = path[level].bp_sib_bh;
855 path[level].bp_sib_bh = NULL;
856 path[level].bp_index += lnchildren;
857 path[level + 1].bp_index--;
858 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900859 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700860 path[level].bp_sib_bh = NULL;
861 path[level].bp_index -= n;
862 }
863
864 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
865}
866
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900867static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700868 struct nilfs_btree_path *path,
869 int level, __u64 *keyp, __u64 *ptrp)
870{
871 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900872 int nchildren, rnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700873
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900874 node = nilfs_btree_get_nonroot_node(path, level);
875 right = nilfs_btree_get_sib_node(path, level);
876 nchildren = nilfs_btree_node_get_nchildren(node);
877 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900878 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700879 move = 0;
880
881 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
882 if (n > nchildren - path[level].bp_index) {
883 /* move insert point */
884 n--;
885 move = 1;
886 }
887
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900888 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700889
890 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900891 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700892 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900893 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700894
Koji Sato17c76b02009-04-06 19:01:24 -0700895 path[level + 1].bp_index++;
896 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900897 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700898 path[level + 1].bp_index--;
899
900 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900901 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700902 path[level].bp_bh = path[level].bp_sib_bh;
903 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900904 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700905 path[level + 1].bp_index++;
906 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900907 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700908 path[level].bp_sib_bh = NULL;
909 }
910
911 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
912}
913
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900914static void nilfs_btree_split(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700915 struct nilfs_btree_path *path,
916 int level, __u64 *keyp, __u64 *ptrp)
917{
918 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900919 int nchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700920
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900921 node = nilfs_btree_get_nonroot_node(path, level);
922 right = nilfs_btree_get_sib_node(path, level);
923 nchildren = nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900924 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700925 move = 0;
926
927 n = (nchildren + 1) / 2;
928 if (n > nchildren - path[level].bp_index) {
929 n--;
930 move = 1;
931 }
932
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900933 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700934
935 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900936 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700937 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900938 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700939
Koji Sato17c76b02009-04-06 19:01:24 -0700940 if (move) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900941 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900942 nilfs_btree_node_insert(right, path[level].bp_index,
943 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700944
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900945 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700946 *ptrp = path[level].bp_newreq.bpr_ptr;
947
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900948 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700949 path[level].bp_bh = path[level].bp_sib_bh;
950 path[level].bp_sib_bh = NULL;
951 } else {
952 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
953
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900954 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700955 *ptrp = path[level].bp_newreq.bpr_ptr;
956
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900957 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700958 path[level].bp_sib_bh = NULL;
959 }
960
961 path[level + 1].bp_index++;
962}
963
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900964static void nilfs_btree_grow(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700965 struct nilfs_btree_path *path,
966 int level, __u64 *keyp, __u64 *ptrp)
967{
968 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900969 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700970
Koji Sato17c76b02009-04-06 19:01:24 -0700971 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900972 child = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900973 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700974
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900975 n = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -0700976
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900977 nilfs_btree_node_move_right(root, child, n,
978 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900979 nilfs_btree_node_set_level(root, level + 1);
Koji Sato17c76b02009-04-06 19:01:24 -0700980
981 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900982 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700983
Koji Sato17c76b02009-04-06 19:01:24 -0700984 path[level].bp_bh = path[level].bp_sib_bh;
985 path[level].bp_sib_bh = NULL;
986
987 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
988
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900989 *keyp = nilfs_btree_node_get_key(child, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700990 *ptrp = path[level].bp_newreq.bpr_ptr;
991}
992
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900993static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700994 const struct nilfs_btree_path *path)
995{
996 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900997 int level, ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -0700998
999 if (path == NULL)
1000 return NILFS_BMAP_INVALID_PTR;
1001
1002 /* left sibling */
1003 level = NILFS_BTREE_LEVEL_NODE_MIN;
1004 if (path[level].bp_index > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001005 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1006 return nilfs_btree_node_get_ptr(node,
1007 path[level].bp_index - 1,
1008 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001009 }
1010
1011 /* parent */
1012 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
1013 if (level <= nilfs_btree_height(btree) - 1) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001014 node = nilfs_btree_get_node(btree, path, level, &ncmax);
1015 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
1016 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001017 }
1018
1019 return NILFS_BMAP_INVALID_PTR;
1020}
1021
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001022static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001023 const struct nilfs_btree_path *path,
1024 __u64 key)
1025{
1026 __u64 ptr;
1027
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001028 ptr = nilfs_bmap_find_target_seq(btree, key);
Koji Sato17c76b02009-04-06 19:01:24 -07001029 if (ptr != NILFS_BMAP_INVALID_PTR)
1030 /* sequential access */
1031 return ptr;
Ryusuke Konishi7f001842016-05-23 16:23:42 -07001032
1033 ptr = nilfs_btree_find_near(btree, path);
1034 if (ptr != NILFS_BMAP_INVALID_PTR)
1035 /* near */
1036 return ptr;
1037
Koji Sato17c76b02009-04-06 19:01:24 -07001038 /* block group */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001039 return nilfs_bmap_find_target_in_group(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001040}
1041
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001042static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001043 struct nilfs_btree_path *path,
1044 int *levelp, __u64 key, __u64 ptr,
1045 struct nilfs_bmap_stats *stats)
1046{
1047 struct buffer_head *bh;
1048 struct nilfs_btree_node *node, *parent, *sib;
1049 __u64 sibptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001050 int pindex, level, ncmax, ncblk, ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001051 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001052
1053 stats->bs_nblocks = 0;
1054 level = NILFS_BTREE_LEVEL_DATA;
1055
1056 /* allocate a new ptr for data block */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001057 if (NILFS_BMAP_USE_VBN(btree)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001058 path[level].bp_newreq.bpr_ptr =
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001059 nilfs_btree_find_target_v(btree, path, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001060 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001061 }
Koji Sato17c76b02009-04-06 19:01:24 -07001062
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001063 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001064 if (ret < 0)
1065 goto err_out_data;
1066
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001067 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001068
Koji Sato17c76b02009-04-06 19:01:24 -07001069 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1070 level < nilfs_btree_height(btree) - 1;
1071 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001072 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001073 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001074 path[level].bp_op = nilfs_btree_do_insert;
1075 stats->bs_nblocks++;
1076 goto out;
1077 }
1078
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001079 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001080 pindex = path[level + 1].bp_index;
1081
1082 /* left sibling */
1083 if (pindex > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001084 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1085 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001086 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001087 if (ret < 0)
1088 goto err_out_child_node;
1089 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001090 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001091 path[level].bp_sib_bh = bh;
1092 path[level].bp_op = nilfs_btree_carry_left;
1093 stats->bs_nblocks++;
1094 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001095 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001096 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001097 }
Koji Sato17c76b02009-04-06 19:01:24 -07001098 }
1099
1100 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001101 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1102 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1103 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001104 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001105 if (ret < 0)
1106 goto err_out_child_node;
1107 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001108 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001109 path[level].bp_sib_bh = bh;
1110 path[level].bp_op = nilfs_btree_carry_right;
1111 stats->bs_nblocks++;
1112 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001113 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001114 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001115 }
Koji Sato17c76b02009-04-06 19:01:24 -07001116 }
1117
1118 /* split */
1119 path[level].bp_newreq.bpr_ptr =
1120 path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001121 ret = nilfs_bmap_prepare_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001122 &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001123 if (ret < 0)
1124 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001125 ret = nilfs_btree_get_new_block(btree,
1126 path[level].bp_newreq.bpr_ptr,
1127 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001128 if (ret < 0)
1129 goto err_out_curr_node;
1130
1131 stats->bs_nblocks++;
1132
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001133 sib = (struct nilfs_btree_node *)bh->b_data;
1134 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001135 path[level].bp_sib_bh = bh;
1136 path[level].bp_op = nilfs_btree_split;
1137 }
1138
1139 /* root */
1140 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001141 if (nilfs_btree_node_get_nchildren(node) <
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001142 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
Koji Sato17c76b02009-04-06 19:01:24 -07001143 path[level].bp_op = nilfs_btree_do_insert;
1144 stats->bs_nblocks++;
1145 goto out;
1146 }
1147
1148 /* grow */
1149 path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001150 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001151 if (ret < 0)
1152 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001153 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1154 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001155 if (ret < 0)
1156 goto err_out_curr_node;
1157
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001158 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1159 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001160 path[level].bp_sib_bh = bh;
1161 path[level].bp_op = nilfs_btree_grow;
1162
1163 level++;
1164 path[level].bp_op = nilfs_btree_do_insert;
1165
1166 /* a newly-created node block and a data block are added */
1167 stats->bs_nblocks += 2;
1168
1169 /* success */
1170 out:
1171 *levelp = level;
1172 return ret;
1173
1174 /* error */
1175 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001176 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001177 err_out_child_node:
1178 for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001179 nilfs_btnode_delete(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001180 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001181
1182 }
1183
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001184 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001185 err_out_data:
1186 *levelp = level;
1187 stats->bs_nblocks = 0;
1188 return ret;
1189}
1190
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001191static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001192 struct nilfs_btree_path *path,
1193 int maxlevel, __u64 key, __u64 ptr)
1194{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001195 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001196 int level;
1197
1198 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1199 ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001200 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001201 nilfs_bmap_set_target_v(btree, key, ptr);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001202 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001203 }
Koji Sato17c76b02009-04-06 19:01:24 -07001204
1205 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001206 nilfs_bmap_commit_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001207 &path[level - 1].bp_newreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001208 path[level].bp_op(btree, path, level, &key, &ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001209 }
1210
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001211 if (!nilfs_bmap_dirty(btree))
1212 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001213}
1214
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001215static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
Koji Sato17c76b02009-04-06 19:01:24 -07001216{
Koji Sato17c76b02009-04-06 19:01:24 -07001217 struct nilfs_btree_path *path;
1218 struct nilfs_bmap_stats stats;
1219 int level, ret;
1220
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001221 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001222 if (path == NULL)
1223 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001224
1225 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001226 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001227 if (ret != -ENOENT) {
1228 if (ret == 0)
1229 ret = -EEXIST;
1230 goto out;
1231 }
1232
1233 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1234 if (ret < 0)
1235 goto out;
1236 nilfs_btree_commit_insert(btree, path, level, key, ptr);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001237 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001238
1239 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001240 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001241 return ret;
1242}
1243
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001244static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001245 struct nilfs_btree_path *path,
1246 int level, __u64 *keyp, __u64 *ptrp)
1247{
1248 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001249 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001250
1251 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001252 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001253 ncblk = nilfs_btree_nchildren_per_block(btree);
1254 nilfs_btree_node_delete(node, path[level].bp_index,
1255 keyp, ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001256 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001257 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001258 if (path[level].bp_index == 0)
1259 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001260 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001261 } else {
1262 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001263 nilfs_btree_node_delete(node, path[level].bp_index,
1264 keyp, ptrp,
1265 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -07001266 }
1267}
1268
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001269static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001270 struct nilfs_btree_path *path,
1271 int level, __u64 *keyp, __u64 *ptrp)
1272{
1273 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001274 int nchildren, lnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001275
1276 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1277
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001278 node = nilfs_btree_get_nonroot_node(path, level);
1279 left = nilfs_btree_get_sib_node(path, level);
1280 nchildren = nilfs_btree_node_get_nchildren(node);
1281 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001282 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001283
1284 n = (nchildren + lnchildren) / 2 - nchildren;
1285
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001286 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001287
1288 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001289 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001290 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001291 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001292
Koji Sato17c76b02009-04-06 19:01:24 -07001293 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001294 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001295
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001296 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001297 path[level].bp_sib_bh = NULL;
1298 path[level].bp_index += n;
1299}
1300
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001301static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001302 struct nilfs_btree_path *path,
1303 int level, __u64 *keyp, __u64 *ptrp)
1304{
1305 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001306 int nchildren, rnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001307
1308 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1309
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001310 node = nilfs_btree_get_nonroot_node(path, level);
1311 right = nilfs_btree_get_sib_node(path, level);
1312 nchildren = nilfs_btree_node_get_nchildren(node);
1313 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001314 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001315
1316 n = (nchildren + rnchildren) / 2 - nchildren;
1317
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001318 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001319
1320 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001321 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001322 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001323 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001324
Koji Sato17c76b02009-04-06 19:01:24 -07001325 path[level + 1].bp_index++;
1326 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001327 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001328 path[level + 1].bp_index--;
1329
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001330 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001331 path[level].bp_sib_bh = NULL;
1332}
1333
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001334static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001335 struct nilfs_btree_path *path,
1336 int level, __u64 *keyp, __u64 *ptrp)
1337{
1338 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001339 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001340
1341 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1342
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001343 node = nilfs_btree_get_nonroot_node(path, level);
1344 left = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001345 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001346
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001347 n = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001348
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001349 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001350
1351 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001352 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001353
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001354 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001355 path[level].bp_bh = path[level].bp_sib_bh;
1356 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001357 path[level].bp_index += nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -07001358}
1359
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001360static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001361 struct nilfs_btree_path *path,
1362 int level, __u64 *keyp, __u64 *ptrp)
1363{
1364 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001365 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001366
1367 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1368
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001369 node = nilfs_btree_get_nonroot_node(path, level);
1370 right = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001371 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001372
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001373 n = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -07001374
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001375 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001376
1377 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001378 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001379
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001380 nilfs_btnode_delete(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001381 path[level].bp_sib_bh = NULL;
1382 path[level + 1].bp_index++;
1383}
1384
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001385static void nilfs_btree_shrink(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001386 struct nilfs_btree_path *path,
1387 int level, __u64 *keyp, __u64 *ptrp)
1388{
1389 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001390 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001391
1392 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1393
Koji Sato17c76b02009-04-06 19:01:24 -07001394 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001395 child = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001396 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001397
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001398 nilfs_btree_node_delete(root, 0, NULL, NULL,
1399 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001400 nilfs_btree_node_set_level(root, level);
1401 n = nilfs_btree_node_get_nchildren(child);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001402 nilfs_btree_node_move_left(root, child, n,
1403 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001404
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001405 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001406 path[level].bp_bh = NULL;
1407}
1408
Ryusuke Konishid4099052011-05-25 23:00:27 +09001409static void nilfs_btree_nop(struct nilfs_bmap *btree,
1410 struct nilfs_btree_path *path,
1411 int level, __u64 *keyp, __u64 *ptrp)
1412{
1413}
Koji Sato17c76b02009-04-06 19:01:24 -07001414
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001415static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001416 struct nilfs_btree_path *path,
1417 int *levelp,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001418 struct nilfs_bmap_stats *stats,
1419 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001420{
1421 struct buffer_head *bh;
1422 struct nilfs_btree_node *node, *parent, *sib;
1423 __u64 sibptr;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001424 int pindex, dindex, level, ncmin, ncmax, ncblk, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001425
1426 ret = 0;
1427 stats->bs_nblocks = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001428 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001429 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001430
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001431 for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index;
Koji Sato17c76b02009-04-06 19:01:24 -07001432 level < nilfs_btree_height(btree) - 1;
1433 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001434 node = nilfs_btree_get_nonroot_node(path, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001435 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001436 nilfs_btree_node_get_ptr(node, dindex, ncblk);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001437 ret = nilfs_bmap_prepare_end_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001438 &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001439 if (ret < 0)
1440 goto err_out_child_node;
Koji Sato17c76b02009-04-06 19:01:24 -07001441
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001442 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001443 path[level].bp_op = nilfs_btree_do_delete;
1444 stats->bs_nblocks++;
1445 goto out;
1446 }
1447
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001448 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001449 pindex = path[level + 1].bp_index;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001450 dindex = pindex;
Koji Sato17c76b02009-04-06 19:01:24 -07001451
1452 if (pindex > 0) {
1453 /* left sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001454 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1455 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001456 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001457 if (ret < 0)
1458 goto err_out_curr_node;
1459 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001460 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001461 path[level].bp_sib_bh = bh;
1462 path[level].bp_op = nilfs_btree_borrow_left;
1463 stats->bs_nblocks++;
1464 goto out;
1465 } else {
1466 path[level].bp_sib_bh = bh;
1467 path[level].bp_op = nilfs_btree_concat_left;
1468 stats->bs_nblocks++;
1469 /* continue; */
1470 }
1471 } else if (pindex <
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001472 nilfs_btree_node_get_nchildren(parent) - 1) {
Koji Sato17c76b02009-04-06 19:01:24 -07001473 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001474 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1475 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001476 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001477 if (ret < 0)
1478 goto err_out_curr_node;
1479 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001480 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001481 path[level].bp_sib_bh = bh;
1482 path[level].bp_op = nilfs_btree_borrow_right;
1483 stats->bs_nblocks++;
1484 goto out;
1485 } else {
1486 path[level].bp_sib_bh = bh;
1487 path[level].bp_op = nilfs_btree_concat_right;
1488 stats->bs_nblocks++;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001489 /*
1490 * When merging right sibling node
1491 * into the current node, pointer to
1492 * the right sibling node must be
1493 * terminated instead. The adjustment
1494 * below is required for that.
1495 */
1496 dindex = pindex + 1;
Koji Sato17c76b02009-04-06 19:01:24 -07001497 /* continue; */
1498 }
1499 } else {
1500 /* no siblings */
1501 /* the only child of the root node */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001502 WARN_ON(level != nilfs_btree_height(btree) - 2);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001503 if (nilfs_btree_node_get_nchildren(node) - 1 <=
Koji Sato17c76b02009-04-06 19:01:24 -07001504 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1505 path[level].bp_op = nilfs_btree_shrink;
1506 stats->bs_nblocks += 2;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001507 level++;
1508 path[level].bp_op = nilfs_btree_nop;
1509 goto shrink_root_child;
Koji Sato17c76b02009-04-06 19:01:24 -07001510 } else {
1511 path[level].bp_op = nilfs_btree_do_delete;
1512 stats->bs_nblocks++;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001513 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -07001514 }
Koji Sato17c76b02009-04-06 19:01:24 -07001515 }
1516 }
1517
Ryusuke Konishid4099052011-05-25 23:00:27 +09001518 /* child of the root node is deleted */
1519 path[level].bp_op = nilfs_btree_do_delete;
1520 stats->bs_nblocks++;
1521
1522shrink_root_child:
Koji Sato17c76b02009-04-06 19:01:24 -07001523 node = nilfs_btree_get_root(btree);
1524 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001525 nilfs_btree_node_get_ptr(node, dindex,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001526 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001527
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001528 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001529 if (ret < 0)
1530 goto err_out_child_node;
1531
Koji Sato17c76b02009-04-06 19:01:24 -07001532 /* success */
1533 out:
1534 *levelp = level;
1535 return ret;
1536
1537 /* error */
1538 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001539 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001540 err_out_child_node:
1541 for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001542 brelse(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001543 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001544 }
1545 *levelp = level;
1546 stats->bs_nblocks = 0;
1547 return ret;
1548}
1549
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001550static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001551 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001552 int maxlevel, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001553{
1554 int level;
1555
1556 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001557 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001558 path[level].bp_op(btree, path, level, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001559 }
1560
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001561 if (!nilfs_bmap_dirty(btree))
1562 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001563}
1564
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001565static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001566
1567{
Koji Sato17c76b02009-04-06 19:01:24 -07001568 struct nilfs_btree_path *path;
1569 struct nilfs_bmap_stats stats;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001570 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001571 int level, ret;
1572
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001573 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001574 if (path == NULL)
1575 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +08001576
Koji Sato17c76b02009-04-06 19:01:24 -07001577 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001578 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001579 if (ret < 0)
1580 goto out;
1581
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001582
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001583 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001584
1585 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001586 if (ret < 0)
1587 goto out;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001588 nilfs_btree_commit_delete(btree, path, level, dat);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001589 nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001590
1591out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001592 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001593 return ret;
1594}
1595
Ryusuke Konishi5b203842015-04-16 12:46:36 -07001596static int nilfs_btree_seek_key(const struct nilfs_bmap *btree, __u64 start,
1597 __u64 *keyp)
1598{
1599 struct nilfs_btree_path *path;
1600 const int minlevel = NILFS_BTREE_LEVEL_NODE_MIN;
1601 int ret;
1602
1603 path = nilfs_btree_alloc_path();
1604 if (!path)
1605 return -ENOMEM;
1606
1607 ret = nilfs_btree_do_lookup(btree, path, start, NULL, minlevel, 0);
1608 if (!ret)
1609 *keyp = start;
1610 else if (ret == -ENOENT)
1611 ret = nilfs_btree_get_next_key(btree, path, minlevel, keyp);
1612
1613 nilfs_btree_free_path(path);
1614 return ret;
1615}
1616
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001617static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
Koji Sato17c76b02009-04-06 19:01:24 -07001618{
Koji Sato17c76b02009-04-06 19:01:24 -07001619 struct nilfs_btree_path *path;
1620 int ret;
1621
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001622 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001623 if (path == NULL)
1624 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001625
1626 ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
1627
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001628 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001629
1630 return ret;
1631}
1632
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001633static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001634{
1635 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001636 struct nilfs_btree_node *root, *node;
1637 __u64 maxkey, nextmaxkey;
1638 __u64 ptr;
1639 int nchildren, ret;
1640
Koji Sato17c76b02009-04-06 19:01:24 -07001641 root = nilfs_btree_get_root(btree);
1642 switch (nilfs_btree_height(btree)) {
1643 case 2:
1644 bh = NULL;
1645 node = root;
1646 break;
1647 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001648 nchildren = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -07001649 if (nchildren > 1)
1650 return 0;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001651 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1652 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001653 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001654 if (ret < 0)
1655 return ret;
1656 node = (struct nilfs_btree_node *)bh->b_data;
1657 break;
1658 default:
1659 return 0;
1660 }
1661
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001662 nchildren = nilfs_btree_node_get_nchildren(node);
1663 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
Koji Sato17c76b02009-04-06 19:01:24 -07001664 nextmaxkey = (nchildren > 1) ?
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001665 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
Koji Sato17c76b02009-04-06 19:01:24 -07001666 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001667 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001668
Ryusuke Konishi30333422009-05-24 00:09:44 +09001669 return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
Koji Sato17c76b02009-04-06 19:01:24 -07001670}
1671
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001672static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001673 __u64 *keys, __u64 *ptrs, int nitems)
1674{
1675 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001676 struct nilfs_btree_node *node, *root;
1677 __le64 *dkeys;
1678 __le64 *dptrs;
1679 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001680 int nchildren, ncmax, i, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001681
Koji Sato17c76b02009-04-06 19:01:24 -07001682 root = nilfs_btree_get_root(btree);
1683 switch (nilfs_btree_height(btree)) {
1684 case 2:
1685 bh = NULL;
1686 node = root;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001687 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
Koji Sato17c76b02009-04-06 19:01:24 -07001688 break;
1689 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001690 nchildren = nilfs_btree_node_get_nchildren(root);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001691 WARN_ON(nchildren > 1);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001692 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1693 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001694 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001695 if (ret < 0)
1696 return ret;
1697 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001698 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001699 break;
1700 default:
1701 node = NULL;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001702 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -07001703 }
1704
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001705 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001706 if (nchildren < nitems)
1707 nitems = nchildren;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001708 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001709 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001710 for (i = 0; i < nitems; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09001711 keys[i] = le64_to_cpu(dkeys[i]);
1712 ptrs[i] = le64_to_cpu(dptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -07001713 }
1714
1715 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001716 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001717
1718 return nitems;
1719}
1720
1721static int
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001722nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
Koji Sato17c76b02009-04-06 19:01:24 -07001723 union nilfs_bmap_ptr_req *dreq,
1724 union nilfs_bmap_ptr_req *nreq,
1725 struct buffer_head **bhp,
1726 struct nilfs_bmap_stats *stats)
1727{
1728 struct buffer_head *bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001729 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001730 int ret;
1731
Koji Sato17c76b02009-04-06 19:01:24 -07001732 stats->bs_nblocks = 0;
1733
1734 /* for data */
1735 /* cannot find near ptr */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001736 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001737 dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001738 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001739 }
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001740
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001741 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001742 if (ret < 0)
1743 return ret;
1744
1745 *bhp = NULL;
1746 stats->bs_nblocks++;
1747 if (nreq != NULL) {
1748 nreq->bpr_ptr = dreq->bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001749 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001750 if (ret < 0)
1751 goto err_out_dreq;
1752
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001753 ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001754 if (ret < 0)
1755 goto err_out_nreq;
1756
1757 *bhp = bh;
1758 stats->bs_nblocks++;
1759 }
1760
1761 /* success */
1762 return 0;
1763
1764 /* error */
1765 err_out_nreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001766 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001767 err_out_dreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001768 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001769 stats->bs_nblocks = 0;
1770 return ret;
1771
1772}
1773
1774static void
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001775nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001776 __u64 key, __u64 ptr,
1777 const __u64 *keys, const __u64 *ptrs,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001778 int n,
Koji Sato17c76b02009-04-06 19:01:24 -07001779 union nilfs_bmap_ptr_req *dreq,
1780 union nilfs_bmap_ptr_req *nreq,
1781 struct buffer_head *bh)
1782{
Koji Sato17c76b02009-04-06 19:01:24 -07001783 struct nilfs_btree_node *node;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001784 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001785 __u64 tmpptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001786 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001787
1788 /* free resources */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001789 if (btree->b_ops->bop_clear != NULL)
1790 btree->b_ops->bop_clear(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001791
1792 /* ptr must be a pointer to a buffer head. */
1793 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1794
1795 /* convert and insert */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001796 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi957ed602015-02-27 15:51:56 -08001797 __nilfs_btree_init(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001798 if (nreq != NULL) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001799 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1800 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001801
1802 /* create child node at level 1 */
Koji Sato17c76b02009-04-06 19:01:24 -07001803 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001804 ncblk = nilfs_btree_nchildren_per_block(btree);
1805 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1806 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001807 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001808 mark_buffer_dirty(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001809 if (!nilfs_bmap_dirty(btree))
1810 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001811
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001812 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001813
1814 /* create root node at level 2 */
1815 node = nilfs_btree_get_root(btree);
1816 tmpptr = nreq->bpr_ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001817 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1818 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1819 &keys[0], &tmpptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001820 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001821 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001822
1823 /* create root node at level 1 */
1824 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001825 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1826 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1827 keys, ptrs);
1828 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1829 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001830 if (!nilfs_bmap_dirty(btree))
1831 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001832 }
1833
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001834 if (NILFS_BMAP_USE_VBN(btree))
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001835 nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001836}
1837
1838/**
1839 * nilfs_btree_convert_and_insert -
1840 * @bmap:
1841 * @key:
1842 * @ptr:
1843 * @keys:
1844 * @ptrs:
1845 * @n:
Koji Sato17c76b02009-04-06 19:01:24 -07001846 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001847int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001848 __u64 key, __u64 ptr,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001849 const __u64 *keys, const __u64 *ptrs, int n)
Koji Sato17c76b02009-04-06 19:01:24 -07001850{
Ryusuke Konishi4f050282015-11-06 16:32:16 -08001851 struct buffer_head *bh = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001852 union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
1853 struct nilfs_bmap_stats stats;
1854 int ret;
1855
1856 if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1857 di = &dreq;
1858 ni = NULL;
1859 } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001860 1 << btree->b_inode->i_blkbits)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001861 di = &dreq;
1862 ni = &nreq;
1863 } else {
1864 di = NULL;
1865 ni = NULL;
1866 BUG();
1867 }
1868
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001869 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
Koji Sato17c76b02009-04-06 19:01:24 -07001870 &stats);
1871 if (ret < 0)
1872 return ret;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001873 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001874 di, ni, bh);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001875 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001876 return 0;
1877}
1878
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001879static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001880 struct nilfs_btree_path *path,
1881 int level,
1882 struct buffer_head *bh)
1883{
1884 while ((++level < nilfs_btree_height(btree) - 1) &&
1885 !buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001886 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001887
1888 return 0;
1889}
1890
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001891static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001892 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001893 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001894{
1895 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001896 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001897
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001898 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001899 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001900 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1901 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001902 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001903 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1904 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001905 if (ret < 0)
1906 return ret;
1907
1908 if (buffer_nilfs_node(path[level].bp_bh)) {
1909 path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
1910 path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
1911 path[level].bp_ctxt.bh = path[level].bp_bh;
1912 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001913 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001914 &path[level].bp_ctxt);
1915 if (ret < 0) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001916 nilfs_dat_abort_update(dat,
1917 &path[level].bp_oldreq.bpr_req,
1918 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001919 return ret;
1920 }
1921 }
1922
1923 return 0;
1924}
1925
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001926static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001927 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001928 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001929{
1930 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001931 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001932
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001933 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1934 &path[level].bp_newreq.bpr_req,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001935 btree->b_ptr_type == NILFS_BMAP_PTR_VS);
Koji Sato17c76b02009-04-06 19:01:24 -07001936
1937 if (buffer_nilfs_node(path[level].bp_bh)) {
1938 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001939 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001940 &path[level].bp_ctxt);
1941 path[level].bp_bh = path[level].bp_ctxt.bh;
1942 }
1943 set_buffer_nilfs_volatile(path[level].bp_bh);
1944
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001945 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1946 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1947 path[level].bp_newreq.bpr_ptr, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001948}
1949
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001950static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001951 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001952 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001953{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001954 nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
1955 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001956 if (buffer_nilfs_node(path[level].bp_bh))
1957 nilfs_btnode_abort_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001958 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001959 &path[level].bp_ctxt);
1960}
1961
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001962static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001963 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001964 int minlevel, int *maxlevelp,
1965 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001966{
1967 int level, ret;
1968
1969 level = minlevel;
1970 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001971 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001972 if (ret < 0)
1973 return ret;
1974 }
1975 while ((++level < nilfs_btree_height(btree) - 1) &&
1976 !buffer_dirty(path[level].bp_bh)) {
1977
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001978 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001979 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001980 if (ret < 0)
1981 goto out;
1982 }
1983
1984 /* success */
Koji Sato17c76b02009-04-06 19:01:24 -07001985 *maxlevelp = level - 1;
1986 return 0;
1987
1988 /* error */
1989 out:
1990 while (--level > minlevel)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001991 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001992 if (!buffer_nilfs_volatile(path[level].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001993 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001994 return ret;
1995}
1996
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001997static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001998 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001999 int minlevel, int maxlevel,
2000 struct buffer_head *bh,
2001 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07002002{
2003 int level;
2004
2005 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002006 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002007
2008 for (level = minlevel + 1; level <= maxlevel; level++)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002009 nilfs_btree_commit_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002010}
2011
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002012static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002013 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002014 int level, struct buffer_head *bh)
Koji Sato17c76b02009-04-06 19:01:24 -07002015{
Li Hong308f4412010-04-02 18:40:39 +08002016 int maxlevel = 0, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002017 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002018 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002019 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002020 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07002021
2022 get_bh(bh);
2023 path[level].bp_bh = bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002024 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
2025 dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002026 if (ret < 0)
2027 goto out;
2028
2029 if (buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002030 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2031 ptr = nilfs_btree_node_get_ptr(parent,
2032 path[level + 1].bp_index,
2033 ncmax);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002034 ret = nilfs_dat_mark_dirty(dat, ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07002035 if (ret < 0)
2036 goto out;
2037 }
2038
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002039 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07002040
2041 out:
2042 brelse(path[level].bp_bh);
2043 path[level].bp_bh = NULL;
2044 return ret;
2045}
2046
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002047static int nilfs_btree_propagate(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002048 struct buffer_head *bh)
2049{
Koji Sato17c76b02009-04-06 19:01:24 -07002050 struct nilfs_btree_path *path;
2051 struct nilfs_btree_node *node;
2052 __u64 key;
2053 int level, ret;
2054
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002055 WARN_ON(!buffer_dirty(bh));
Koji Sato17c76b02009-04-06 19:01:24 -07002056
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002057 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002058 if (path == NULL)
2059 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002060
2061 if (buffer_nilfs_node(bh)) {
2062 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002063 key = nilfs_btree_node_get_key(node, 0);
2064 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002065 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002066 key = nilfs_bmap_data_get_key(btree, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002067 level = NILFS_BTREE_LEVEL_DATA;
2068 }
2069
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002070 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002071 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002072 if (unlikely(ret == -ENOENT))
Koji Sato17c76b02009-04-06 19:01:24 -07002073 printk(KERN_CRIT "%s: key = %llu, level == %d\n",
2074 __func__, (unsigned long long)key, level);
Koji Sato17c76b02009-04-06 19:01:24 -07002075 goto out;
2076 }
2077
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002078 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002079 nilfs_btree_propagate_v(btree, path, level, bh) :
2080 nilfs_btree_propagate_p(btree, path, level, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002081
2082 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002083 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002084
2085 return ret;
2086}
2087
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002088static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002089 struct buffer_head *bh)
2090{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002091 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002092}
2093
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002094static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002095 struct list_head *lists,
2096 struct buffer_head *bh)
2097{
2098 struct list_head *head;
2099 struct buffer_head *cbh;
2100 struct nilfs_btree_node *node, *cnode;
2101 __u64 key, ckey;
2102 int level;
2103
2104 get_bh(bh);
2105 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002106 key = nilfs_btree_node_get_key(node, 0);
2107 level = nilfs_btree_node_get_level(node);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002108 if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
2109 level >= NILFS_BTREE_LEVEL_MAX) {
2110 dump_stack();
2111 printk(KERN_WARNING
2112 "%s: invalid btree level: %d (key=%llu, ino=%lu, "
2113 "blocknr=%llu)\n",
2114 __func__, level, (unsigned long long)key,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002115 NILFS_BMAP_I(btree)->vfs_inode.i_ino,
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002116 (unsigned long long)bh->b_blocknr);
2117 return;
2118 }
2119
Koji Sato17c76b02009-04-06 19:01:24 -07002120 list_for_each(head, &lists[level]) {
2121 cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
2122 cnode = (struct nilfs_btree_node *)cbh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002123 ckey = nilfs_btree_node_get_key(cnode, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002124 if (key < ckey)
2125 break;
2126 }
2127 list_add_tail(&bh->b_assoc_buffers, head);
2128}
2129
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002130static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002131 struct list_head *listp)
2132{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002133 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
Koji Sato17c76b02009-04-06 19:01:24 -07002134 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2135 struct pagevec pvec;
2136 struct buffer_head *bh, *head;
2137 pgoff_t index = 0;
2138 int level, i;
2139
2140 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2141 level < NILFS_BTREE_LEVEL_MAX;
2142 level++)
2143 INIT_LIST_HEAD(&lists[level]);
2144
2145 pagevec_init(&pvec, 0);
2146
2147 while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
2148 PAGEVEC_SIZE)) {
2149 for (i = 0; i < pagevec_count(&pvec); i++) {
2150 bh = head = page_buffers(pvec.pages[i]);
2151 do {
2152 if (buffer_dirty(bh))
2153 nilfs_btree_add_dirty_buffer(btree,
2154 lists, bh);
2155 } while ((bh = bh->b_this_page) != head);
2156 }
2157 pagevec_release(&pvec);
2158 cond_resched();
2159 }
2160
2161 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2162 level < NILFS_BTREE_LEVEL_MAX;
2163 level++)
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002164 list_splice_tail(&lists[level], listp);
Koji Sato17c76b02009-04-06 19:01:24 -07002165}
2166
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002167static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002168 struct nilfs_btree_path *path,
2169 int level,
2170 struct buffer_head **bh,
2171 sector_t blocknr,
2172 union nilfs_binfo *binfo)
2173{
2174 struct nilfs_btree_node *parent;
2175 __u64 key;
2176 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002177 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002178
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002179 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2180 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2181 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002182 if (buffer_nilfs_node(*bh)) {
2183 path[level].bp_ctxt.oldkey = ptr;
2184 path[level].bp_ctxt.newkey = blocknr;
2185 path[level].bp_ctxt.bh = *bh;
2186 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002187 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002188 &path[level].bp_ctxt);
2189 if (ret < 0)
2190 return ret;
2191 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002192 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002193 &path[level].bp_ctxt);
2194 *bh = path[level].bp_ctxt.bh;
2195 }
2196
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002197 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2198 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002199
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002200 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002201 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002202 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002203 binfo->bi_dat.bi_level = level;
2204
2205 return 0;
2206}
2207
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002208static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002209 struct nilfs_btree_path *path,
2210 int level,
2211 struct buffer_head **bh,
2212 sector_t blocknr,
2213 union nilfs_binfo *binfo)
2214{
2215 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002216 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002217 __u64 key;
2218 __u64 ptr;
2219 union nilfs_bmap_ptr_req req;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002220 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002221
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002222 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2223 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2224 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002225 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002226 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2227 if (ret < 0)
Koji Sato17c76b02009-04-06 19:01:24 -07002228 return ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002229 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002230
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002231 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002232 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002233 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
2234 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002235
2236 return 0;
2237}
2238
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002239static int nilfs_btree_assign(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002240 struct buffer_head **bh,
2241 sector_t blocknr,
2242 union nilfs_binfo *binfo)
2243{
Koji Sato17c76b02009-04-06 19:01:24 -07002244 struct nilfs_btree_path *path;
2245 struct nilfs_btree_node *node;
2246 __u64 key;
2247 int level, ret;
2248
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002249 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002250 if (path == NULL)
2251 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002252
2253 if (buffer_nilfs_node(*bh)) {
2254 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002255 key = nilfs_btree_node_get_key(node, 0);
2256 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002257 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002258 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002259 level = NILFS_BTREE_LEVEL_DATA;
2260 }
2261
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002262 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002263 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002264 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002265 goto out;
2266 }
2267
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002268 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002269 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2270 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
Koji Sato17c76b02009-04-06 19:01:24 -07002271
2272 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002273 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002274
2275 return ret;
2276}
2277
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002278static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002279 struct buffer_head **bh,
2280 sector_t blocknr,
2281 union nilfs_binfo *binfo)
2282{
Koji Sato17c76b02009-04-06 19:01:24 -07002283 struct nilfs_btree_node *node;
2284 __u64 key;
2285 int ret;
2286
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002287 ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002288 blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002289 if (ret < 0)
2290 return ret;
2291
2292 if (buffer_nilfs_node(*bh)) {
2293 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002294 key = nilfs_btree_node_get_key(node, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002295 } else
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002296 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002297
2298 /* on-disk format */
2299 binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002300 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002301
2302 return 0;
2303}
2304
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002305static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
Koji Sato17c76b02009-04-06 19:01:24 -07002306{
2307 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07002308 struct nilfs_btree_path *path;
2309 __u64 ptr;
2310 int ret;
2311
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002312 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002313 if (path == NULL)
2314 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002315
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002316 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002317 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002318 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002319 goto out;
2320 }
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09002321 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002322 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002323 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002324 goto out;
2325 }
2326
2327 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09002328 mark_buffer_dirty(bh);
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09002329 brelse(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002330 if (!nilfs_bmap_dirty(btree))
2331 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002332
2333 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002334 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002335 return ret;
2336}
2337
2338static const struct nilfs_bmap_operations nilfs_btree_ops = {
2339 .bop_lookup = nilfs_btree_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002340 .bop_lookup_contig = nilfs_btree_lookup_contig,
Koji Sato17c76b02009-04-06 19:01:24 -07002341 .bop_insert = nilfs_btree_insert,
2342 .bop_delete = nilfs_btree_delete,
2343 .bop_clear = NULL,
2344
2345 .bop_propagate = nilfs_btree_propagate,
2346
2347 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2348
2349 .bop_assign = nilfs_btree_assign,
2350 .bop_mark = nilfs_btree_mark,
2351
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002352 .bop_seek_key = nilfs_btree_seek_key,
Koji Sato17c76b02009-04-06 19:01:24 -07002353 .bop_last_key = nilfs_btree_last_key,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002354
Koji Sato17c76b02009-04-06 19:01:24 -07002355 .bop_check_insert = NULL,
2356 .bop_check_delete = nilfs_btree_check_delete,
2357 .bop_gather_data = nilfs_btree_gather_data,
2358};
2359
2360static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
2361 .bop_lookup = NULL,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002362 .bop_lookup_contig = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002363 .bop_insert = NULL,
2364 .bop_delete = NULL,
2365 .bop_clear = NULL,
2366
2367 .bop_propagate = nilfs_btree_propagate_gc,
2368
2369 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2370
2371 .bop_assign = nilfs_btree_assign_gc,
2372 .bop_mark = NULL,
2373
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002374 .bop_seek_key = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002375 .bop_last_key = NULL,
Ryusuke Konishi5b203842015-04-16 12:46:36 -07002376
Koji Sato17c76b02009-04-06 19:01:24 -07002377 .bop_check_insert = NULL,
2378 .bop_check_delete = NULL,
2379 .bop_gather_data = NULL,
2380};
2381
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002382static void __nilfs_btree_init(struct nilfs_bmap *bmap)
Koji Sato17c76b02009-04-06 19:01:24 -07002383{
Koji Sato17c76b02009-04-06 19:01:24 -07002384 bmap->b_ops = &nilfs_btree_ops;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002385 bmap->b_nchildren_per_block =
2386 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Ryusuke Konishi957ed602015-02-27 15:51:56 -08002387}
2388
2389int nilfs_btree_init(struct nilfs_bmap *bmap)
2390{
2391 int ret = 0;
2392
2393 __nilfs_btree_init(bmap);
2394
2395 if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap),
2396 bmap->b_inode->i_ino))
2397 ret = -EIO;
2398 return ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002399}
2400
2401void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
2402{
Koji Sato17c76b02009-04-06 19:01:24 -07002403 bmap->b_ops = &nilfs_btree_ops_gc;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002404 bmap->b_nchildren_per_block =
2405 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002406}