blob: 1b5321c0bcac86866d1f81eb555b46af26abac45 [file] [log] [blame]
Koji Sato17c76b02009-04-06 19:01:24 -07001/*
2 * btree.c - NILFS B-tree.
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Written by Koji Sato <koji@osrg.net>.
21 */
22
23#include <linux/slab.h>
24#include <linux/string.h>
25#include <linux/errno.h>
26#include <linux/pagevec.h>
27#include "nilfs.h"
28#include "page.h"
29#include "btnode.h"
30#include "btree.h"
31#include "alloc.h"
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +090032#include "dat.h"
Koji Sato17c76b02009-04-06 19:01:24 -070033
Li Hongf9054402010-04-02 17:36:34 +080034static struct nilfs_btree_path *nilfs_btree_alloc_path(void)
Koji Sato17c76b02009-04-06 19:01:24 -070035{
Li Hongf9054402010-04-02 17:36:34 +080036 struct nilfs_btree_path *path;
37 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070038
Li Hongf9054402010-04-02 17:36:34 +080039 path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
40 if (path == NULL)
41 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -070042
Li Hongf9054402010-04-02 17:36:34 +080043 for (; level < NILFS_BTREE_LEVEL_MAX; level++) {
Koji Sato17c76b02009-04-06 19:01:24 -070044 path[level].bp_bh = NULL;
45 path[level].bp_sib_bh = NULL;
46 path[level].bp_index = 0;
47 path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
48 path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
49 path[level].bp_op = NULL;
50 }
Li Hongf9054402010-04-02 17:36:34 +080051
52out:
53 return path;
54}
55
Li Hong73bb4882010-04-02 18:35:00 +080056static void nilfs_btree_free_path(struct nilfs_btree_path *path)
Li Hongf9054402010-04-02 17:36:34 +080057{
Li Hong73bb4882010-04-02 18:35:00 +080058 int level = NILFS_BTREE_LEVEL_DATA;
Koji Sato17c76b02009-04-06 19:01:24 -070059
Li Hong73bb4882010-04-02 18:35:00 +080060 for (; level < NILFS_BTREE_LEVEL_MAX; level++)
Ryusuke Konishi32189292009-08-15 01:54:59 +090061 brelse(path[level].bp_bh);
Li Hong73bb4882010-04-02 18:35:00 +080062
63 kmem_cache_free(nilfs_btree_path_cache, path);
Koji Sato17c76b02009-04-06 19:01:24 -070064}
65
Koji Sato17c76b02009-04-06 19:01:24 -070066/*
67 * B-tree node operations
68 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090069static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090070 __u64 ptr, struct buffer_head **bhp)
71{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +090072 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
Ryusuke Konishi45f49102009-11-13 16:25:19 +090073 struct buffer_head *bh;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090074
Ryusuke Konishi45f49102009-11-13 16:25:19 +090075 bh = nilfs_btnode_create_block(btnc, ptr);
76 if (!bh)
77 return -ENOMEM;
78
79 set_buffer_nilfs_volatile(bh);
80 *bhp = bh;
81 return 0;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +090082}
Koji Sato17c76b02009-04-06 19:01:24 -070083
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090084static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070085{
86 return node->bn_flags;
87}
88
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090089static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090090nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
Koji Sato17c76b02009-04-06 19:01:24 -070091{
92 node->bn_flags = flags;
93}
94
Ryusuke Konishi7c397a82010-07-13 23:33:55 +090095static int nilfs_btree_node_root(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -070096{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +090097 return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
Koji Sato17c76b02009-04-06 19:01:24 -070098}
99
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900100static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700101{
102 return node->bn_level;
103}
104
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900105static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900106nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700107{
108 node->bn_level = level;
109}
110
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900111static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700112{
113 return le16_to_cpu(node->bn_nchildren);
114}
115
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900116static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900117nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
Koji Sato17c76b02009-04-06 19:01:24 -0700118{
119 node->bn_nchildren = cpu_to_le16(nchildren);
120}
121
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900122static int nilfs_btree_node_size(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700123{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900124 return 1 << btree->b_inode->i_blkbits;
Koji Sato17c76b02009-04-06 19:01:24 -0700125}
126
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900127static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700128{
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +0900129 return btree->b_nchildren_per_block;
Koji Sato17c76b02009-04-06 19:01:24 -0700130}
131
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900132static __le64 *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900133nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
Koji Sato17c76b02009-04-06 19:01:24 -0700134{
135 return (__le64 *)((char *)(node + 1) +
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900136 (nilfs_btree_node_root(node) ?
Koji Sato17c76b02009-04-06 19:01:24 -0700137 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
138}
139
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900140static __le64 *
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900141nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700142{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900143 return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700144}
145
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900146static __u64
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900147nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
Koji Sato17c76b02009-04-06 19:01:24 -0700148{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900149 return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700150}
151
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900152static void
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900153nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -0700154{
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900155 *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -0700156}
157
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900158static __u64
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900159nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
160 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700161{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900162 return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
Koji Sato17c76b02009-04-06 19:01:24 -0700163}
164
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900165static void
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900166nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
167 int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700168{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900169 *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700170}
171
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900172static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
173 int level, int nchildren, int ncmax,
Koji Sato17c76b02009-04-06 19:01:24 -0700174 const __u64 *keys, const __u64 *ptrs)
175{
176 __le64 *dkeys;
177 __le64 *dptrs;
178 int i;
179
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900180 nilfs_btree_node_set_flags(node, flags);
181 nilfs_btree_node_set_level(node, level);
182 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700183
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900184 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900185 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700186 for (i = 0; i < nchildren; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900187 dkeys[i] = cpu_to_le64(keys[i]);
188 dptrs[i] = cpu_to_le64(ptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -0700189 }
190}
191
192/* Assume the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900193static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700194 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900195 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700196{
197 __le64 *ldkeys, *rdkeys;
198 __le64 *ldptrs, *rdptrs;
199 int lnchildren, rnchildren;
200
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900201 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900202 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900203 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700204
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900205 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900206 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900207 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700208
209 memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
210 memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs));
211 memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys));
212 memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs));
213
214 lnchildren += n;
215 rnchildren -= n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900216 nilfs_btree_node_set_nchildren(left, lnchildren);
217 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700218}
219
220/* Assume that the buffer heads corresponding to left and right are locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900221static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
Koji Sato17c76b02009-04-06 19:01:24 -0700222 struct nilfs_btree_node *right,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900223 int n, int lncmax, int rncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700224{
225 __le64 *ldkeys, *rdkeys;
226 __le64 *ldptrs, *rdptrs;
227 int lnchildren, rnchildren;
228
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900229 ldkeys = nilfs_btree_node_dkeys(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900230 ldptrs = nilfs_btree_node_dptrs(left, lncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900231 lnchildren = nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -0700232
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900233 rdkeys = nilfs_btree_node_dkeys(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900234 rdptrs = nilfs_btree_node_dptrs(right, rncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900235 rnchildren = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -0700236
237 memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
238 memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs));
239 memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys));
240 memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs));
241
242 lnchildren -= n;
243 rnchildren += n;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900244 nilfs_btree_node_set_nchildren(left, lnchildren);
245 nilfs_btree_node_set_nchildren(right, rnchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700246}
247
248/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900249static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
250 __u64 key, __u64 ptr, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700251{
252 __le64 *dkeys;
253 __le64 *dptrs;
254 int nchildren;
255
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900256 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900257 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900258 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700259 if (index < nchildren) {
260 memmove(dkeys + index + 1, dkeys + index,
261 (nchildren - index) * sizeof(*dkeys));
262 memmove(dptrs + index + 1, dptrs + index,
263 (nchildren - index) * sizeof(*dptrs));
264 }
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900265 dkeys[index] = cpu_to_le64(key);
266 dptrs[index] = cpu_to_le64(ptr);
Koji Sato17c76b02009-04-06 19:01:24 -0700267 nchildren++;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900268 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700269}
270
271/* Assume that the buffer head corresponding to node is locked. */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900272static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
273 __u64 *keyp, __u64 *ptrp, int ncmax)
Koji Sato17c76b02009-04-06 19:01:24 -0700274{
275 __u64 key;
276 __u64 ptr;
277 __le64 *dkeys;
278 __le64 *dptrs;
279 int nchildren;
280
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900281 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900282 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +0900283 key = le64_to_cpu(dkeys[index]);
284 ptr = le64_to_cpu(dptrs[index]);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900285 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700286 if (keyp != NULL)
287 *keyp = key;
288 if (ptrp != NULL)
289 *ptrp = ptr;
290
291 if (index < nchildren - 1) {
292 memmove(dkeys + index, dkeys + index + 1,
293 (nchildren - index - 1) * sizeof(*dkeys));
294 memmove(dptrs + index, dptrs + index + 1,
295 (nchildren - index - 1) * sizeof(*dptrs));
296 }
297 nchildren--;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900298 nilfs_btree_node_set_nchildren(node, nchildren);
Koji Sato17c76b02009-04-06 19:01:24 -0700299}
300
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900301static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
Koji Sato17c76b02009-04-06 19:01:24 -0700302 __u64 key, int *indexp)
303{
304 __u64 nkey;
305 int index, low, high, s;
306
307 /* binary search */
308 low = 0;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900309 high = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700310 index = 0;
311 s = 0;
312 while (low <= high) {
313 index = (low + high) / 2;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900314 nkey = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700315 if (nkey == key) {
316 s = 0;
317 goto out;
318 } else if (nkey < key) {
319 low = index + 1;
320 s = -1;
321 } else {
322 high = index - 1;
323 s = 1;
324 }
325 }
326
327 /* adjust index */
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900328 if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
329 if (s > 0 && index > 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700330 index--;
331 } else if (s < 0)
332 index++;
333
334 out:
Koji Sato17c76b02009-04-06 19:01:24 -0700335 *indexp = index;
336
337 return s == 0;
338}
339
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900340/**
341 * nilfs_btree_node_broken - verify consistency of btree node
342 * @node: btree node block to be examined
343 * @size: node size (in bytes)
344 * @blocknr: block number
345 *
346 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
347 */
348static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
349 size_t size, sector_t blocknr)
350{
351 int level, flags, nchildren;
352 int ret = 0;
353
354 level = nilfs_btree_node_get_level(node);
355 flags = nilfs_btree_node_get_flags(node);
356 nchildren = nilfs_btree_node_get_nchildren(node);
357
358 if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
359 level >= NILFS_BTREE_LEVEL_MAX ||
360 (flags & NILFS_BTREE_NODE_ROOT) ||
361 nchildren < 0 ||
362 nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
363 printk(KERN_CRIT "NILFS: bad btree node (blocknr=%llu): "
364 "level = %d, flags = 0x%x, nchildren = %d\n",
365 (unsigned long long)blocknr, level, flags, nchildren);
366 ret = 1;
367 }
368 return ret;
369}
370
371int nilfs_btree_broken_node_block(struct buffer_head *bh)
372{
373 return nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
374 bh->b_size, bh->b_blocknr);
375}
376
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900377static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900378nilfs_btree_get_root(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700379{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900380 return (struct nilfs_btree_node *)btree->b_u.u_data;
Koji Sato17c76b02009-04-06 19:01:24 -0700381}
382
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900383static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900384nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700385{
386 return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
387}
388
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900389static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900390nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700391{
392 return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
393}
394
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900395static int nilfs_btree_height(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700396{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900397 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700398}
399
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900400static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900401nilfs_btree_get_node(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700402 const struct nilfs_btree_path *path,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900403 int level, int *ncmaxp)
Koji Sato17c76b02009-04-06 19:01:24 -0700404{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900405 struct nilfs_btree_node *node;
406
407 if (level == nilfs_btree_height(btree) - 1) {
408 node = nilfs_btree_get_root(btree);
409 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
410 } else {
411 node = nilfs_btree_get_nonroot_node(path, level);
412 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
413 }
414 return node;
Koji Sato17c76b02009-04-06 19:01:24 -0700415}
416
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900417static int
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900418nilfs_btree_bad_node(struct nilfs_btree_node *node, int level)
419{
420 if (unlikely(nilfs_btree_node_get_level(node) != level)) {
421 dump_stack();
422 printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n",
423 nilfs_btree_node_get_level(node), level);
424 return 1;
425 }
426 return 0;
427}
428
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900429struct nilfs_btree_readahead_info {
430 struct nilfs_btree_node *node; /* parent node */
431 int max_ra_blocks; /* max nof blocks to read ahead */
432 int index; /* current index on the parent node */
433 int ncmax; /* nof children in the parent node */
434};
435
436static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
437 struct buffer_head **bhp,
438 const struct nilfs_btree_readahead_info *ra)
439{
440 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
441 struct buffer_head *bh, *ra_bh;
442 sector_t submit_ptr = 0;
443 int ret;
444
445 ret = nilfs_btnode_submit_block(btnc, ptr, 0, READ, &bh, &submit_ptr);
446 if (ret) {
447 if (ret != -EEXIST)
448 return ret;
449 goto out_check;
450 }
451
452 if (ra) {
453 int i, n;
454 __u64 ptr2;
455
456 /* read ahead sibling nodes */
457 for (n = ra->max_ra_blocks, i = ra->index + 1;
458 n > 0 && i < ra->ncmax; n--, i++) {
459 ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
460
461 ret = nilfs_btnode_submit_block(btnc, ptr2, 0, READA,
462 &ra_bh, &submit_ptr);
463 if (likely(!ret || ret == -EEXIST))
464 brelse(ra_bh);
465 else if (ret != -EBUSY)
466 break;
467 if (!buffer_locked(bh))
468 goto out_no_wait;
469 }
470 }
471
472 wait_on_buffer(bh);
473
474 out_no_wait:
475 if (!buffer_uptodate(bh)) {
476 brelse(bh);
477 return -EIO;
478 }
479
480 out_check:
481 if (nilfs_btree_broken_node_block(bh)) {
482 clear_buffer_uptodate(bh);
483 brelse(bh);
484 return -EINVAL;
485 }
486
487 *bhp = bh;
488 return 0;
489}
490
491static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
492 struct buffer_head **bhp)
493{
494 return __nilfs_btree_get_block(btree, ptr, bhp, NULL);
495}
496
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900497static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700498 struct nilfs_btree_path *path,
499 __u64 key, __u64 *ptrp, int minlevel)
500{
501 struct nilfs_btree_node *node;
502 __u64 ptr;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900503 int level, index, found, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700504
Koji Sato17c76b02009-04-06 19:01:24 -0700505 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900506 level = nilfs_btree_node_get_level(node);
507 if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700508 return -ENOENT;
509
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900510 found = nilfs_btree_node_lookup(node, key, &index);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900511 ptr = nilfs_btree_node_get_ptr(node, index,
512 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700513 path[level].bp_bh = NULL;
514 path[level].bp_index = index;
515
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900516 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900517
Koji Sato17c76b02009-04-06 19:01:24 -0700518 for (level--; level >= minlevel; level--) {
Ryusuke Konishif198dbb2009-05-22 01:07:13 +0900519 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700520 if (ret < 0)
521 return ret;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900522 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900523 if (nilfs_btree_bad_node(node, level))
524 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -0700525 if (!found)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900526 found = nilfs_btree_node_lookup(node, key, &index);
Koji Sato17c76b02009-04-06 19:01:24 -0700527 else
528 index = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900529 if (index < ncmax) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900530 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900531 } else {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700532 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
Koji Sato17c76b02009-04-06 19:01:24 -0700533 /* insert */
534 ptr = NILFS_BMAP_INVALID_PTR;
535 }
536 path[level].bp_index = index;
537 }
538 if (!found)
539 return -ENOENT;
540
541 if (ptrp != NULL)
542 *ptrp = ptr;
543
544 return 0;
545}
546
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900547static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700548 struct nilfs_btree_path *path,
549 __u64 *keyp, __u64 *ptrp)
550{
551 struct nilfs_btree_node *node;
552 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900553 int index, level, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700554
555 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900556 index = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700557 if (index < 0)
558 return -ENOENT;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900559 level = nilfs_btree_node_get_level(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900560 ptr = nilfs_btree_node_get_ptr(node, index,
561 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700562 path[level].bp_bh = NULL;
563 path[level].bp_index = index;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900564 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700565
566 for (level--; level > 0; level--) {
Ryusuke Konishif198dbb2009-05-22 01:07:13 +0900567 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700568 if (ret < 0)
569 return ret;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900570 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900571 if (nilfs_btree_bad_node(node, level))
572 return -EINVAL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900573 index = nilfs_btree_node_get_nchildren(node) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900574 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700575 path[level].bp_index = index;
576 }
577
578 if (keyp != NULL)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900579 *keyp = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700580 if (ptrp != NULL)
581 *ptrp = ptr;
582
583 return 0;
584}
585
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900586static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700587 __u64 key, int level, __u64 *ptrp)
588{
Koji Sato17c76b02009-04-06 19:01:24 -0700589 struct nilfs_btree_path *path;
Koji Sato17c76b02009-04-06 19:01:24 -0700590 int ret;
591
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900592 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -0700593 if (path == NULL)
594 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -0700595
Ryusuke Konishi364ec2d2010-07-13 23:33:51 +0900596 ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level);
Koji Sato17c76b02009-04-06 19:01:24 -0700597
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900598 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -0700599
600 return ret;
601}
602
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900603static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900604 __u64 key, __u64 *ptrp, unsigned maxblocks)
605{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900606 struct nilfs_btree_path *path;
607 struct nilfs_btree_node *node;
608 struct inode *dat = NULL;
609 __u64 ptr, ptr2;
610 sector_t blocknr;
611 int level = NILFS_BTREE_LEVEL_NODE_MIN;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900612 int ret, cnt, index, maxlevel, ncmax;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900613
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900614 path = nilfs_btree_alloc_path();
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900615 if (path == NULL)
616 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +0800617
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900618 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level);
619 if (ret < 0)
620 goto out;
621
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900622 if (NILFS_BMAP_USE_VBN(btree)) {
623 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900624 ret = nilfs_dat_translate(dat, ptr, &blocknr);
625 if (ret < 0)
626 goto out;
627 ptr = blocknr;
628 }
629 cnt = 1;
630 if (cnt == maxblocks)
631 goto end;
632
633 maxlevel = nilfs_btree_height(btree) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900634 node = nilfs_btree_get_node(btree, path, level, &ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900635 index = path[level].bp_index + 1;
636 for (;;) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900637 while (index < nilfs_btree_node_get_nchildren(node)) {
638 if (nilfs_btree_node_get_key(node, index) !=
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900639 key + cnt)
640 goto end;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900641 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900642 if (dat) {
643 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
644 if (ret < 0)
645 goto out;
646 ptr2 = blocknr;
647 }
648 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
649 goto end;
650 index++;
651 continue;
652 }
653 if (level == maxlevel)
654 break;
655
656 /* look-up right sibling node */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900657 node = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900658 index = path[level + 1].bp_index + 1;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900659 if (index >= nilfs_btree_node_get_nchildren(node) ||
660 nilfs_btree_node_get_key(node, index) != key + cnt)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900661 break;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900662 ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900663 path[level + 1].bp_index = index;
664
665 brelse(path[level].bp_bh);
666 path[level].bp_bh = NULL;
667 ret = nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh);
668 if (ret < 0)
669 goto out;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900670 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900671 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900672 index = 0;
673 path[level].bp_index = index;
674 }
675 end:
676 *ptrp = ptr;
677 ret = cnt;
678 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900679 nilfs_btree_free_path(path);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900680 return ret;
681}
682
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900683static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700684 struct nilfs_btree_path *path,
685 int level, __u64 key)
686{
687 if (level < nilfs_btree_height(btree) - 1) {
688 do {
Koji Sato17c76b02009-04-06 19:01:24 -0700689 nilfs_btree_node_set_key(
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900690 nilfs_btree_get_nonroot_node(path, level),
Koji Sato17c76b02009-04-06 19:01:24 -0700691 path[level].bp_index, key);
692 if (!buffer_dirty(path[level].bp_bh))
693 nilfs_btnode_mark_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700694 } while ((path[level].bp_index == 0) &&
695 (++level < nilfs_btree_height(btree) - 1));
696 }
697
698 /* root */
699 if (level == nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900700 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
Koji Sato17c76b02009-04-06 19:01:24 -0700701 path[level].bp_index, key);
702 }
703}
704
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900705static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700706 struct nilfs_btree_path *path,
707 int level, __u64 *keyp, __u64 *ptrp)
708{
709 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900710 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700711
712 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900713 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900714 ncblk = nilfs_btree_nchildren_per_block(btree);
715 nilfs_btree_node_insert(node, path[level].bp_index,
716 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700717 if (!buffer_dirty(path[level].bp_bh))
718 nilfs_btnode_mark_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700719
720 if (path[level].bp_index == 0)
721 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900722 nilfs_btree_node_get_key(node,
723 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700724 } else {
725 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900726 nilfs_btree_node_insert(node, path[level].bp_index,
727 *keyp, *ptrp,
728 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700729 }
730}
731
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900732static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700733 struct nilfs_btree_path *path,
734 int level, __u64 *keyp, __u64 *ptrp)
735{
736 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900737 int nchildren, lnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700738
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900739 node = nilfs_btree_get_nonroot_node(path, level);
740 left = nilfs_btree_get_sib_node(path, level);
741 nchildren = nilfs_btree_node_get_nchildren(node);
742 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900743 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700744 move = 0;
745
746 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
747 if (n > path[level].bp_index) {
748 /* move insert point */
749 n--;
750 move = 1;
751 }
752
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900753 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700754
755 if (!buffer_dirty(path[level].bp_bh))
756 nilfs_btnode_mark_dirty(path[level].bp_bh);
757 if (!buffer_dirty(path[level].bp_sib_bh))
758 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
759
Koji Sato17c76b02009-04-06 19:01:24 -0700760 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900761 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700762
763 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900764 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700765 path[level].bp_bh = path[level].bp_sib_bh;
766 path[level].bp_sib_bh = NULL;
767 path[level].bp_index += lnchildren;
768 path[level + 1].bp_index--;
769 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900770 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700771 path[level].bp_sib_bh = NULL;
772 path[level].bp_index -= n;
773 }
774
775 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
776}
777
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900778static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700779 struct nilfs_btree_path *path,
780 int level, __u64 *keyp, __u64 *ptrp)
781{
782 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900783 int nchildren, rnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700784
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900785 node = nilfs_btree_get_nonroot_node(path, level);
786 right = nilfs_btree_get_sib_node(path, level);
787 nchildren = nilfs_btree_node_get_nchildren(node);
788 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900789 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700790 move = 0;
791
792 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
793 if (n > nchildren - path[level].bp_index) {
794 /* move insert point */
795 n--;
796 move = 1;
797 }
798
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900799 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700800
801 if (!buffer_dirty(path[level].bp_bh))
802 nilfs_btnode_mark_dirty(path[level].bp_bh);
803 if (!buffer_dirty(path[level].bp_sib_bh))
804 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
805
Koji Sato17c76b02009-04-06 19:01:24 -0700806 path[level + 1].bp_index++;
807 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900808 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700809 path[level + 1].bp_index--;
810
811 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900812 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700813 path[level].bp_bh = path[level].bp_sib_bh;
814 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900815 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700816 path[level + 1].bp_index++;
817 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900818 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700819 path[level].bp_sib_bh = NULL;
820 }
821
822 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
823}
824
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900825static void nilfs_btree_split(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700826 struct nilfs_btree_path *path,
827 int level, __u64 *keyp, __u64 *ptrp)
828{
829 struct nilfs_btree_node *node, *right;
830 __u64 newkey;
831 __u64 newptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900832 int nchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700833
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900834 node = nilfs_btree_get_nonroot_node(path, level);
835 right = nilfs_btree_get_sib_node(path, level);
836 nchildren = nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900837 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700838 move = 0;
839
840 n = (nchildren + 1) / 2;
841 if (n > nchildren - path[level].bp_index) {
842 n--;
843 move = 1;
844 }
845
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900846 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700847
848 if (!buffer_dirty(path[level].bp_bh))
849 nilfs_btnode_mark_dirty(path[level].bp_bh);
850 if (!buffer_dirty(path[level].bp_sib_bh))
851 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
852
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900853 newkey = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700854 newptr = path[level].bp_newreq.bpr_ptr;
855
856 if (move) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900857 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900858 nilfs_btree_node_insert(right, path[level].bp_index,
859 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700860
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900861 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700862 *ptrp = path[level].bp_newreq.bpr_ptr;
863
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900864 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700865 path[level].bp_bh = path[level].bp_sib_bh;
866 path[level].bp_sib_bh = NULL;
867 } else {
868 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
869
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900870 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700871 *ptrp = path[level].bp_newreq.bpr_ptr;
872
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900873 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700874 path[level].bp_sib_bh = NULL;
875 }
876
877 path[level + 1].bp_index++;
878}
879
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900880static void nilfs_btree_grow(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700881 struct nilfs_btree_path *path,
882 int level, __u64 *keyp, __u64 *ptrp)
883{
884 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900885 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700886
Koji Sato17c76b02009-04-06 19:01:24 -0700887 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900888 child = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900889 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700890
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900891 n = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -0700892
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900893 nilfs_btree_node_move_right(root, child, n,
894 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900895 nilfs_btree_node_set_level(root, level + 1);
Koji Sato17c76b02009-04-06 19:01:24 -0700896
897 if (!buffer_dirty(path[level].bp_sib_bh))
898 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
899
Koji Sato17c76b02009-04-06 19:01:24 -0700900 path[level].bp_bh = path[level].bp_sib_bh;
901 path[level].bp_sib_bh = NULL;
902
903 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
904
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900905 *keyp = nilfs_btree_node_get_key(child, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700906 *ptrp = path[level].bp_newreq.bpr_ptr;
907}
908
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900909static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700910 const struct nilfs_btree_path *path)
911{
912 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900913 int level, ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -0700914
915 if (path == NULL)
916 return NILFS_BMAP_INVALID_PTR;
917
918 /* left sibling */
919 level = NILFS_BTREE_LEVEL_NODE_MIN;
920 if (path[level].bp_index > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900921 node = nilfs_btree_get_node(btree, path, level, &ncmax);
922 return nilfs_btree_node_get_ptr(node,
923 path[level].bp_index - 1,
924 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700925 }
926
927 /* parent */
928 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
929 if (level <= nilfs_btree_height(btree) - 1) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900930 node = nilfs_btree_get_node(btree, path, level, &ncmax);
931 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
932 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700933 }
934
935 return NILFS_BMAP_INVALID_PTR;
936}
937
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900938static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700939 const struct nilfs_btree_path *path,
940 __u64 key)
941{
942 __u64 ptr;
943
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900944 ptr = nilfs_bmap_find_target_seq(btree, key);
Koji Sato17c76b02009-04-06 19:01:24 -0700945 if (ptr != NILFS_BMAP_INVALID_PTR)
946 /* sequential access */
947 return ptr;
948 else {
949 ptr = nilfs_btree_find_near(btree, path);
950 if (ptr != NILFS_BMAP_INVALID_PTR)
951 /* near */
952 return ptr;
953 }
954 /* block group */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900955 return nilfs_bmap_find_target_in_group(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700956}
957
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900958static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700959 struct nilfs_btree_path *path,
960 int *levelp, __u64 key, __u64 ptr,
961 struct nilfs_bmap_stats *stats)
962{
963 struct buffer_head *bh;
964 struct nilfs_btree_node *node, *parent, *sib;
965 __u64 sibptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900966 int pindex, level, ncmax, ncblk, ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900967 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -0700968
969 stats->bs_nblocks = 0;
970 level = NILFS_BTREE_LEVEL_DATA;
971
972 /* allocate a new ptr for data block */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900973 if (NILFS_BMAP_USE_VBN(btree)) {
Koji Sato17c76b02009-04-06 19:01:24 -0700974 path[level].bp_newreq.bpr_ptr =
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +0900975 nilfs_btree_find_target_v(btree, path, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900976 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900977 }
Koji Sato17c76b02009-04-06 19:01:24 -0700978
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900979 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -0700980 if (ret < 0)
981 goto err_out_data;
982
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900983 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900984
Koji Sato17c76b02009-04-06 19:01:24 -0700985 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
986 level < nilfs_btree_height(btree) - 1;
987 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900988 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900989 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -0700990 path[level].bp_op = nilfs_btree_do_insert;
991 stats->bs_nblocks++;
992 goto out;
993 }
994
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900995 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700996 pindex = path[level + 1].bp_index;
997
998 /* left sibling */
999 if (pindex > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001000 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1001 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001002 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001003 if (ret < 0)
1004 goto err_out_child_node;
1005 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001006 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001007 path[level].bp_sib_bh = bh;
1008 path[level].bp_op = nilfs_btree_carry_left;
1009 stats->bs_nblocks++;
1010 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001011 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001012 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001013 }
Koji Sato17c76b02009-04-06 19:01:24 -07001014 }
1015
1016 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001017 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1018 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1019 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001020 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001021 if (ret < 0)
1022 goto err_out_child_node;
1023 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001024 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001025 path[level].bp_sib_bh = bh;
1026 path[level].bp_op = nilfs_btree_carry_right;
1027 stats->bs_nblocks++;
1028 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001029 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001030 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001031 }
Koji Sato17c76b02009-04-06 19:01:24 -07001032 }
1033
1034 /* split */
1035 path[level].bp_newreq.bpr_ptr =
1036 path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001037 ret = nilfs_bmap_prepare_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001038 &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001039 if (ret < 0)
1040 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001041 ret = nilfs_btree_get_new_block(btree,
1042 path[level].bp_newreq.bpr_ptr,
1043 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001044 if (ret < 0)
1045 goto err_out_curr_node;
1046
1047 stats->bs_nblocks++;
1048
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001049 sib = (struct nilfs_btree_node *)bh->b_data;
1050 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001051 path[level].bp_sib_bh = bh;
1052 path[level].bp_op = nilfs_btree_split;
1053 }
1054
1055 /* root */
1056 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001057 if (nilfs_btree_node_get_nchildren(node) <
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001058 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
Koji Sato17c76b02009-04-06 19:01:24 -07001059 path[level].bp_op = nilfs_btree_do_insert;
1060 stats->bs_nblocks++;
1061 goto out;
1062 }
1063
1064 /* grow */
1065 path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001066 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001067 if (ret < 0)
1068 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001069 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1070 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001071 if (ret < 0)
1072 goto err_out_curr_node;
1073
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001074 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1075 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001076 path[level].bp_sib_bh = bh;
1077 path[level].bp_op = nilfs_btree_grow;
1078
1079 level++;
1080 path[level].bp_op = nilfs_btree_do_insert;
1081
1082 /* a newly-created node block and a data block are added */
1083 stats->bs_nblocks += 2;
1084
1085 /* success */
1086 out:
1087 *levelp = level;
1088 return ret;
1089
1090 /* error */
1091 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001092 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001093 err_out_child_node:
1094 for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001095 nilfs_btnode_delete(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001096 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001097
1098 }
1099
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001100 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001101 err_out_data:
1102 *levelp = level;
1103 stats->bs_nblocks = 0;
1104 return ret;
1105}
1106
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001107static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001108 struct nilfs_btree_path *path,
1109 int maxlevel, __u64 key, __u64 ptr)
1110{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001111 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001112 int level;
1113
1114 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1115 ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001116 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001117 nilfs_bmap_set_target_v(btree, key, ptr);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001118 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001119 }
Koji Sato17c76b02009-04-06 19:01:24 -07001120
1121 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001122 nilfs_bmap_commit_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001123 &path[level - 1].bp_newreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001124 path[level].bp_op(btree, path, level, &key, &ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001125 }
1126
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001127 if (!nilfs_bmap_dirty(btree))
1128 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001129}
1130
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001131static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
Koji Sato17c76b02009-04-06 19:01:24 -07001132{
Koji Sato17c76b02009-04-06 19:01:24 -07001133 struct nilfs_btree_path *path;
1134 struct nilfs_bmap_stats stats;
1135 int level, ret;
1136
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001137 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001138 if (path == NULL)
1139 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001140
1141 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
1142 NILFS_BTREE_LEVEL_NODE_MIN);
1143 if (ret != -ENOENT) {
1144 if (ret == 0)
1145 ret = -EEXIST;
1146 goto out;
1147 }
1148
1149 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1150 if (ret < 0)
1151 goto out;
1152 nilfs_btree_commit_insert(btree, path, level, key, ptr);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001153 nilfs_bmap_add_blocks(btree, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001154
1155 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001156 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001157 return ret;
1158}
1159
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001160static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001161 struct nilfs_btree_path *path,
1162 int level, __u64 *keyp, __u64 *ptrp)
1163{
1164 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001165 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001166
1167 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001168 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001169 ncblk = nilfs_btree_nchildren_per_block(btree);
1170 nilfs_btree_node_delete(node, path[level].bp_index,
1171 keyp, ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001172 if (!buffer_dirty(path[level].bp_bh))
1173 nilfs_btnode_mark_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001174 if (path[level].bp_index == 0)
1175 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001176 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001177 } else {
1178 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001179 nilfs_btree_node_delete(node, path[level].bp_index,
1180 keyp, ptrp,
1181 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -07001182 }
1183}
1184
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001185static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001186 struct nilfs_btree_path *path,
1187 int level, __u64 *keyp, __u64 *ptrp)
1188{
1189 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001190 int nchildren, lnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001191
1192 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1193
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001194 node = nilfs_btree_get_nonroot_node(path, level);
1195 left = nilfs_btree_get_sib_node(path, level);
1196 nchildren = nilfs_btree_node_get_nchildren(node);
1197 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001198 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001199
1200 n = (nchildren + lnchildren) / 2 - nchildren;
1201
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001202 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001203
1204 if (!buffer_dirty(path[level].bp_bh))
1205 nilfs_btnode_mark_dirty(path[level].bp_bh);
1206 if (!buffer_dirty(path[level].bp_sib_bh))
1207 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
1208
Koji Sato17c76b02009-04-06 19:01:24 -07001209 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001210 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001211
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001212 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001213 path[level].bp_sib_bh = NULL;
1214 path[level].bp_index += n;
1215}
1216
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001217static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001218 struct nilfs_btree_path *path,
1219 int level, __u64 *keyp, __u64 *ptrp)
1220{
1221 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001222 int nchildren, rnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001223
1224 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1225
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001226 node = nilfs_btree_get_nonroot_node(path, level);
1227 right = nilfs_btree_get_sib_node(path, level);
1228 nchildren = nilfs_btree_node_get_nchildren(node);
1229 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001230 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001231
1232 n = (nchildren + rnchildren) / 2 - nchildren;
1233
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001234 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001235
1236 if (!buffer_dirty(path[level].bp_bh))
1237 nilfs_btnode_mark_dirty(path[level].bp_bh);
1238 if (!buffer_dirty(path[level].bp_sib_bh))
1239 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
1240
Koji Sato17c76b02009-04-06 19:01:24 -07001241 path[level + 1].bp_index++;
1242 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001243 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001244 path[level + 1].bp_index--;
1245
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001246 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001247 path[level].bp_sib_bh = NULL;
1248}
1249
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001250static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001251 struct nilfs_btree_path *path,
1252 int level, __u64 *keyp, __u64 *ptrp)
1253{
1254 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001255 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001256
1257 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1258
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001259 node = nilfs_btree_get_nonroot_node(path, level);
1260 left = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001261 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001262
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001263 n = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001264
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001265 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001266
1267 if (!buffer_dirty(path[level].bp_sib_bh))
1268 nilfs_btnode_mark_dirty(path[level].bp_sib_bh);
1269
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001270 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001271 path[level].bp_bh = path[level].bp_sib_bh;
1272 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001273 path[level].bp_index += nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -07001274}
1275
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001276static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001277 struct nilfs_btree_path *path,
1278 int level, __u64 *keyp, __u64 *ptrp)
1279{
1280 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001281 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001282
1283 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1284
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001285 node = nilfs_btree_get_nonroot_node(path, level);
1286 right = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001287 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001288
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001289 n = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -07001290
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001291 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001292
1293 if (!buffer_dirty(path[level].bp_bh))
1294 nilfs_btnode_mark_dirty(path[level].bp_bh);
1295
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001296 nilfs_btnode_delete(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001297 path[level].bp_sib_bh = NULL;
1298 path[level + 1].bp_index++;
1299}
1300
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001301static void nilfs_btree_shrink(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 *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001306 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001307
1308 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1309
Koji Sato17c76b02009-04-06 19:01:24 -07001310 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001311 child = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001312 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001313
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001314 nilfs_btree_node_delete(root, 0, NULL, NULL,
1315 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001316 nilfs_btree_node_set_level(root, level);
1317 n = nilfs_btree_node_get_nchildren(child);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001318 nilfs_btree_node_move_left(root, child, n,
1319 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001320
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001321 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001322 path[level].bp_bh = NULL;
1323}
1324
1325
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001326static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001327 struct nilfs_btree_path *path,
1328 int *levelp,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001329 struct nilfs_bmap_stats *stats,
1330 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001331{
1332 struct buffer_head *bh;
1333 struct nilfs_btree_node *node, *parent, *sib;
1334 __u64 sibptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001335 int pindex, level, ncmin, ncmax, ncblk, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001336
1337 ret = 0;
1338 stats->bs_nblocks = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001339 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001340 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001341
Koji Sato17c76b02009-04-06 19:01:24 -07001342 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1343 level < nilfs_btree_height(btree) - 1;
1344 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001345 node = nilfs_btree_get_nonroot_node(path, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001346 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001347 nilfs_btree_node_get_ptr(node, path[level].bp_index,
1348 ncblk);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001349 ret = nilfs_bmap_prepare_end_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001350 &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001351 if (ret < 0)
1352 goto err_out_child_node;
Koji Sato17c76b02009-04-06 19:01:24 -07001353
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001354 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001355 path[level].bp_op = nilfs_btree_do_delete;
1356 stats->bs_nblocks++;
1357 goto out;
1358 }
1359
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001360 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001361 pindex = path[level + 1].bp_index;
1362
1363 if (pindex > 0) {
1364 /* left sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001365 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1366 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001367 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001368 if (ret < 0)
1369 goto err_out_curr_node;
1370 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001371 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001372 path[level].bp_sib_bh = bh;
1373 path[level].bp_op = nilfs_btree_borrow_left;
1374 stats->bs_nblocks++;
1375 goto out;
1376 } else {
1377 path[level].bp_sib_bh = bh;
1378 path[level].bp_op = nilfs_btree_concat_left;
1379 stats->bs_nblocks++;
1380 /* continue; */
1381 }
1382 } else if (pindex <
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001383 nilfs_btree_node_get_nchildren(parent) - 1) {
Koji Sato17c76b02009-04-06 19:01:24 -07001384 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001385 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1386 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001387 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001388 if (ret < 0)
1389 goto err_out_curr_node;
1390 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001391 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001392 path[level].bp_sib_bh = bh;
1393 path[level].bp_op = nilfs_btree_borrow_right;
1394 stats->bs_nblocks++;
1395 goto out;
1396 } else {
1397 path[level].bp_sib_bh = bh;
1398 path[level].bp_op = nilfs_btree_concat_right;
1399 stats->bs_nblocks++;
1400 /* continue; */
1401 }
1402 } else {
1403 /* no siblings */
1404 /* the only child of the root node */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001405 WARN_ON(level != nilfs_btree_height(btree) - 2);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001406 if (nilfs_btree_node_get_nchildren(node) - 1 <=
Koji Sato17c76b02009-04-06 19:01:24 -07001407 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1408 path[level].bp_op = nilfs_btree_shrink;
1409 stats->bs_nblocks += 2;
1410 } else {
1411 path[level].bp_op = nilfs_btree_do_delete;
1412 stats->bs_nblocks++;
1413 }
1414
1415 goto out;
1416
1417 }
1418 }
1419
1420 node = nilfs_btree_get_root(btree);
1421 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001422 nilfs_btree_node_get_ptr(node, path[level].bp_index,
1423 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001424
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001425 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001426 if (ret < 0)
1427 goto err_out_child_node;
1428
Koji Sato17c76b02009-04-06 19:01:24 -07001429 /* child of the root node is deleted */
1430 path[level].bp_op = nilfs_btree_do_delete;
1431 stats->bs_nblocks++;
1432
1433 /* success */
1434 out:
1435 *levelp = level;
1436 return ret;
1437
1438 /* error */
1439 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001440 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001441 err_out_child_node:
1442 for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001443 brelse(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001444 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001445 }
1446 *levelp = level;
1447 stats->bs_nblocks = 0;
1448 return ret;
1449}
1450
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001451static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001452 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001453 int maxlevel, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001454{
1455 int level;
1456
1457 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001458 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001459 path[level].bp_op(btree, path, level, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001460 }
1461
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001462 if (!nilfs_bmap_dirty(btree))
1463 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001464}
1465
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001466static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001467
1468{
Koji Sato17c76b02009-04-06 19:01:24 -07001469 struct nilfs_btree_path *path;
1470 struct nilfs_bmap_stats stats;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001471 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001472 int level, ret;
1473
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001474 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001475 if (path == NULL)
1476 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +08001477
Koji Sato17c76b02009-04-06 19:01:24 -07001478 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
1479 NILFS_BTREE_LEVEL_NODE_MIN);
1480 if (ret < 0)
1481 goto out;
1482
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001483
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001484 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001485
1486 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001487 if (ret < 0)
1488 goto out;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001489 nilfs_btree_commit_delete(btree, path, level, dat);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001490 nilfs_bmap_sub_blocks(btree, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001491
1492out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001493 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001494 return ret;
1495}
1496
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001497static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
Koji Sato17c76b02009-04-06 19:01:24 -07001498{
Koji Sato17c76b02009-04-06 19:01:24 -07001499 struct nilfs_btree_path *path;
1500 int ret;
1501
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001502 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001503 if (path == NULL)
1504 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001505
1506 ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
1507
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001508 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001509
1510 return ret;
1511}
1512
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001513static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001514{
1515 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001516 struct nilfs_btree_node *root, *node;
1517 __u64 maxkey, nextmaxkey;
1518 __u64 ptr;
1519 int nchildren, ret;
1520
Koji Sato17c76b02009-04-06 19:01:24 -07001521 root = nilfs_btree_get_root(btree);
1522 switch (nilfs_btree_height(btree)) {
1523 case 2:
1524 bh = NULL;
1525 node = root;
1526 break;
1527 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001528 nchildren = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -07001529 if (nchildren > 1)
1530 return 0;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001531 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1532 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001533 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001534 if (ret < 0)
1535 return ret;
1536 node = (struct nilfs_btree_node *)bh->b_data;
1537 break;
1538 default:
1539 return 0;
1540 }
1541
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001542 nchildren = nilfs_btree_node_get_nchildren(node);
1543 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
Koji Sato17c76b02009-04-06 19:01:24 -07001544 nextmaxkey = (nchildren > 1) ?
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001545 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
Koji Sato17c76b02009-04-06 19:01:24 -07001546 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001547 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001548
Ryusuke Konishi30333422009-05-24 00:09:44 +09001549 return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
Koji Sato17c76b02009-04-06 19:01:24 -07001550}
1551
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001552static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001553 __u64 *keys, __u64 *ptrs, int nitems)
1554{
1555 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001556 struct nilfs_btree_node *node, *root;
1557 __le64 *dkeys;
1558 __le64 *dptrs;
1559 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001560 int nchildren, ncmax, i, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001561
Koji Sato17c76b02009-04-06 19:01:24 -07001562 root = nilfs_btree_get_root(btree);
1563 switch (nilfs_btree_height(btree)) {
1564 case 2:
1565 bh = NULL;
1566 node = root;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001567 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
Koji Sato17c76b02009-04-06 19:01:24 -07001568 break;
1569 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001570 nchildren = nilfs_btree_node_get_nchildren(root);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001571 WARN_ON(nchildren > 1);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001572 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1573 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001574 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001575 if (ret < 0)
1576 return ret;
1577 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001578 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001579 break;
1580 default:
1581 node = NULL;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001582 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -07001583 }
1584
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001585 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001586 if (nchildren < nitems)
1587 nitems = nchildren;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001588 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001589 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001590 for (i = 0; i < nitems; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09001591 keys[i] = le64_to_cpu(dkeys[i]);
1592 ptrs[i] = le64_to_cpu(dptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -07001593 }
1594
1595 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001596 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001597
1598 return nitems;
1599}
1600
1601static int
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001602nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
Koji Sato17c76b02009-04-06 19:01:24 -07001603 union nilfs_bmap_ptr_req *dreq,
1604 union nilfs_bmap_ptr_req *nreq,
1605 struct buffer_head **bhp,
1606 struct nilfs_bmap_stats *stats)
1607{
1608 struct buffer_head *bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001609 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001610 int ret;
1611
Koji Sato17c76b02009-04-06 19:01:24 -07001612 stats->bs_nblocks = 0;
1613
1614 /* for data */
1615 /* cannot find near ptr */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001616 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001617 dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001618 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001619 }
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001620
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001621 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001622 if (ret < 0)
1623 return ret;
1624
1625 *bhp = NULL;
1626 stats->bs_nblocks++;
1627 if (nreq != NULL) {
1628 nreq->bpr_ptr = dreq->bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001629 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001630 if (ret < 0)
1631 goto err_out_dreq;
1632
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001633 ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001634 if (ret < 0)
1635 goto err_out_nreq;
1636
1637 *bhp = bh;
1638 stats->bs_nblocks++;
1639 }
1640
1641 /* success */
1642 return 0;
1643
1644 /* error */
1645 err_out_nreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001646 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001647 err_out_dreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001648 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001649 stats->bs_nblocks = 0;
1650 return ret;
1651
1652}
1653
1654static void
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001655nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001656 __u64 key, __u64 ptr,
1657 const __u64 *keys, const __u64 *ptrs,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001658 int n,
Koji Sato17c76b02009-04-06 19:01:24 -07001659 union nilfs_bmap_ptr_req *dreq,
1660 union nilfs_bmap_ptr_req *nreq,
1661 struct buffer_head *bh)
1662{
Koji Sato17c76b02009-04-06 19:01:24 -07001663 struct nilfs_btree_node *node;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001664 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001665 __u64 tmpptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001666 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001667
1668 /* free resources */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001669 if (btree->b_ops->bop_clear != NULL)
1670 btree->b_ops->bop_clear(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001671
1672 /* ptr must be a pointer to a buffer head. */
1673 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1674
1675 /* convert and insert */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001676 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
1677 nilfs_btree_init(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001678 if (nreq != NULL) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001679 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1680 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001681
1682 /* create child node at level 1 */
Koji Sato17c76b02009-04-06 19:01:24 -07001683 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001684 ncblk = nilfs_btree_nchildren_per_block(btree);
1685 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1686 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001687 if (!buffer_dirty(bh))
1688 nilfs_btnode_mark_dirty(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001689 if (!nilfs_bmap_dirty(btree))
1690 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001691
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001692 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001693
1694 /* create root node at level 2 */
1695 node = nilfs_btree_get_root(btree);
1696 tmpptr = nreq->bpr_ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001697 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1698 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1699 &keys[0], &tmpptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001700 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001701 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001702
1703 /* create root node at level 1 */
1704 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001705 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1706 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1707 keys, ptrs);
1708 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1709 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001710 if (!nilfs_bmap_dirty(btree))
1711 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001712 }
1713
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001714 if (NILFS_BMAP_USE_VBN(btree))
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001715 nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001716}
1717
1718/**
1719 * nilfs_btree_convert_and_insert -
1720 * @bmap:
1721 * @key:
1722 * @ptr:
1723 * @keys:
1724 * @ptrs:
1725 * @n:
Koji Sato17c76b02009-04-06 19:01:24 -07001726 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001727int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001728 __u64 key, __u64 ptr,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001729 const __u64 *keys, const __u64 *ptrs, int n)
Koji Sato17c76b02009-04-06 19:01:24 -07001730{
1731 struct buffer_head *bh;
1732 union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
1733 struct nilfs_bmap_stats stats;
1734 int ret;
1735
1736 if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1737 di = &dreq;
1738 ni = NULL;
1739 } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001740 1 << btree->b_inode->i_blkbits)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001741 di = &dreq;
1742 ni = &nreq;
1743 } else {
1744 di = NULL;
1745 ni = NULL;
1746 BUG();
1747 }
1748
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001749 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
Koji Sato17c76b02009-04-06 19:01:24 -07001750 &stats);
1751 if (ret < 0)
1752 return ret;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001753 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001754 di, ni, bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001755 nilfs_bmap_add_blocks(btree, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001756 return 0;
1757}
1758
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001759static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001760 struct nilfs_btree_path *path,
1761 int level,
1762 struct buffer_head *bh)
1763{
1764 while ((++level < nilfs_btree_height(btree) - 1) &&
1765 !buffer_dirty(path[level].bp_bh))
1766 nilfs_btnode_mark_dirty(path[level].bp_bh);
1767
1768 return 0;
1769}
1770
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001771static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001772 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001773 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001774{
1775 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001776 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001777
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001778 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001779 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001780 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1781 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001782 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001783 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1784 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001785 if (ret < 0)
1786 return ret;
1787
1788 if (buffer_nilfs_node(path[level].bp_bh)) {
1789 path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
1790 path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
1791 path[level].bp_ctxt.bh = path[level].bp_bh;
1792 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001793 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001794 &path[level].bp_ctxt);
1795 if (ret < 0) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001796 nilfs_dat_abort_update(dat,
1797 &path[level].bp_oldreq.bpr_req,
1798 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001799 return ret;
1800 }
1801 }
1802
1803 return 0;
1804}
1805
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001806static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001807 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001808 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001809{
1810 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001811 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001812
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001813 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1814 &path[level].bp_newreq.bpr_req,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001815 btree->b_ptr_type == NILFS_BMAP_PTR_VS);
Koji Sato17c76b02009-04-06 19:01:24 -07001816
1817 if (buffer_nilfs_node(path[level].bp_bh)) {
1818 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001819 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001820 &path[level].bp_ctxt);
1821 path[level].bp_bh = path[level].bp_ctxt.bh;
1822 }
1823 set_buffer_nilfs_volatile(path[level].bp_bh);
1824
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001825 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1826 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1827 path[level].bp_newreq.bpr_ptr, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001828}
1829
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001830static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001831 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001832 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001833{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001834 nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
1835 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001836 if (buffer_nilfs_node(path[level].bp_bh))
1837 nilfs_btnode_abort_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001838 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001839 &path[level].bp_ctxt);
1840}
1841
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001842static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001843 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001844 int minlevel, int *maxlevelp,
1845 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001846{
1847 int level, ret;
1848
1849 level = minlevel;
1850 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001851 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001852 if (ret < 0)
1853 return ret;
1854 }
1855 while ((++level < nilfs_btree_height(btree) - 1) &&
1856 !buffer_dirty(path[level].bp_bh)) {
1857
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001858 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001859 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001860 if (ret < 0)
1861 goto out;
1862 }
1863
1864 /* success */
Koji Sato17c76b02009-04-06 19:01:24 -07001865 *maxlevelp = level - 1;
1866 return 0;
1867
1868 /* error */
1869 out:
1870 while (--level > minlevel)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001871 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001872 if (!buffer_nilfs_volatile(path[level].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001873 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001874 return ret;
1875}
1876
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001877static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001878 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001879 int minlevel, int maxlevel,
1880 struct buffer_head *bh,
1881 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001882{
1883 int level;
1884
1885 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001886 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001887
1888 for (level = minlevel + 1; level <= maxlevel; level++)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001889 nilfs_btree_commit_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001890}
1891
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001892static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001893 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001894 int level, struct buffer_head *bh)
Koji Sato17c76b02009-04-06 19:01:24 -07001895{
Li Hong308f4412010-04-02 18:40:39 +08001896 int maxlevel = 0, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001897 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001898 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001899 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001900 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001901
1902 get_bh(bh);
1903 path[level].bp_bh = bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001904 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
1905 dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001906 if (ret < 0)
1907 goto out;
1908
1909 if (buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001910 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1911 ptr = nilfs_btree_node_get_ptr(parent,
1912 path[level + 1].bp_index,
1913 ncmax);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001914 ret = nilfs_dat_mark_dirty(dat, ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001915 if (ret < 0)
1916 goto out;
1917 }
1918
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001919 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001920
1921 out:
1922 brelse(path[level].bp_bh);
1923 path[level].bp_bh = NULL;
1924 return ret;
1925}
1926
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001927static int nilfs_btree_propagate(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001928 struct buffer_head *bh)
1929{
Koji Sato17c76b02009-04-06 19:01:24 -07001930 struct nilfs_btree_path *path;
1931 struct nilfs_btree_node *node;
1932 __u64 key;
1933 int level, ret;
1934
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001935 WARN_ON(!buffer_dirty(bh));
Koji Sato17c76b02009-04-06 19:01:24 -07001936
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001937 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001938 if (path == NULL)
1939 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001940
1941 if (buffer_nilfs_node(bh)) {
1942 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001943 key = nilfs_btree_node_get_key(node, 0);
1944 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001945 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001946 key = nilfs_bmap_data_get_key(btree, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001947 level = NILFS_BTREE_LEVEL_DATA;
1948 }
1949
1950 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
1951 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001952 if (unlikely(ret == -ENOENT))
Koji Sato17c76b02009-04-06 19:01:24 -07001953 printk(KERN_CRIT "%s: key = %llu, level == %d\n",
1954 __func__, (unsigned long long)key, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001955 goto out;
1956 }
1957
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001958 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001959 nilfs_btree_propagate_v(btree, path, level, bh) :
1960 nilfs_btree_propagate_p(btree, path, level, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001961
1962 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001963 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001964
1965 return ret;
1966}
1967
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001968static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001969 struct buffer_head *bh)
1970{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001971 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07001972}
1973
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001974static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001975 struct list_head *lists,
1976 struct buffer_head *bh)
1977{
1978 struct list_head *head;
1979 struct buffer_head *cbh;
1980 struct nilfs_btree_node *node, *cnode;
1981 __u64 key, ckey;
1982 int level;
1983
1984 get_bh(bh);
1985 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001986 key = nilfs_btree_node_get_key(node, 0);
1987 level = nilfs_btree_node_get_level(node);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09001988 if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
1989 level >= NILFS_BTREE_LEVEL_MAX) {
1990 dump_stack();
1991 printk(KERN_WARNING
1992 "%s: invalid btree level: %d (key=%llu, ino=%lu, "
1993 "blocknr=%llu)\n",
1994 __func__, level, (unsigned long long)key,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001995 NILFS_BMAP_I(btree)->vfs_inode.i_ino,
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09001996 (unsigned long long)bh->b_blocknr);
1997 return;
1998 }
1999
Koji Sato17c76b02009-04-06 19:01:24 -07002000 list_for_each(head, &lists[level]) {
2001 cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
2002 cnode = (struct nilfs_btree_node *)cbh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002003 ckey = nilfs_btree_node_get_key(cnode, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002004 if (key < ckey)
2005 break;
2006 }
2007 list_add_tail(&bh->b_assoc_buffers, head);
2008}
2009
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002010static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002011 struct list_head *listp)
2012{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002013 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
Koji Sato17c76b02009-04-06 19:01:24 -07002014 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2015 struct pagevec pvec;
2016 struct buffer_head *bh, *head;
2017 pgoff_t index = 0;
2018 int level, i;
2019
2020 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2021 level < NILFS_BTREE_LEVEL_MAX;
2022 level++)
2023 INIT_LIST_HEAD(&lists[level]);
2024
2025 pagevec_init(&pvec, 0);
2026
2027 while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
2028 PAGEVEC_SIZE)) {
2029 for (i = 0; i < pagevec_count(&pvec); i++) {
2030 bh = head = page_buffers(pvec.pages[i]);
2031 do {
2032 if (buffer_dirty(bh))
2033 nilfs_btree_add_dirty_buffer(btree,
2034 lists, bh);
2035 } while ((bh = bh->b_this_page) != head);
2036 }
2037 pagevec_release(&pvec);
2038 cond_resched();
2039 }
2040
2041 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2042 level < NILFS_BTREE_LEVEL_MAX;
2043 level++)
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002044 list_splice_tail(&lists[level], listp);
Koji Sato17c76b02009-04-06 19:01:24 -07002045}
2046
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002047static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002048 struct nilfs_btree_path *path,
2049 int level,
2050 struct buffer_head **bh,
2051 sector_t blocknr,
2052 union nilfs_binfo *binfo)
2053{
2054 struct nilfs_btree_node *parent;
2055 __u64 key;
2056 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002057 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002058
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002059 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2060 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2061 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002062 if (buffer_nilfs_node(*bh)) {
2063 path[level].bp_ctxt.oldkey = ptr;
2064 path[level].bp_ctxt.newkey = blocknr;
2065 path[level].bp_ctxt.bh = *bh;
2066 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002067 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002068 &path[level].bp_ctxt);
2069 if (ret < 0)
2070 return ret;
2071 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002072 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002073 &path[level].bp_ctxt);
2074 *bh = path[level].bp_ctxt.bh;
2075 }
2076
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002077 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2078 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002079
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002080 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002081 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002082 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002083 binfo->bi_dat.bi_level = level;
2084
2085 return 0;
2086}
2087
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002088static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002089 struct nilfs_btree_path *path,
2090 int level,
2091 struct buffer_head **bh,
2092 sector_t blocknr,
2093 union nilfs_binfo *binfo)
2094{
2095 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002096 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002097 __u64 key;
2098 __u64 ptr;
2099 union nilfs_bmap_ptr_req req;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002100 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002101
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002102 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2103 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2104 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002105 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002106 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2107 if (ret < 0)
Koji Sato17c76b02009-04-06 19:01:24 -07002108 return ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002109 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002110
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002111 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002112 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002113 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
2114 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002115
2116 return 0;
2117}
2118
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002119static int nilfs_btree_assign(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002120 struct buffer_head **bh,
2121 sector_t blocknr,
2122 union nilfs_binfo *binfo)
2123{
Koji Sato17c76b02009-04-06 19:01:24 -07002124 struct nilfs_btree_path *path;
2125 struct nilfs_btree_node *node;
2126 __u64 key;
2127 int level, ret;
2128
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002129 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002130 if (path == NULL)
2131 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002132
2133 if (buffer_nilfs_node(*bh)) {
2134 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002135 key = nilfs_btree_node_get_key(node, 0);
2136 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002137 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002138 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002139 level = NILFS_BTREE_LEVEL_DATA;
2140 }
2141
2142 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
2143 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002144 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002145 goto out;
2146 }
2147
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002148 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002149 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2150 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
Koji Sato17c76b02009-04-06 19:01:24 -07002151
2152 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002153 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002154
2155 return ret;
2156}
2157
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002158static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002159 struct buffer_head **bh,
2160 sector_t blocknr,
2161 union nilfs_binfo *binfo)
2162{
Koji Sato17c76b02009-04-06 19:01:24 -07002163 struct nilfs_btree_node *node;
2164 __u64 key;
2165 int ret;
2166
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002167 ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002168 blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002169 if (ret < 0)
2170 return ret;
2171
2172 if (buffer_nilfs_node(*bh)) {
2173 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002174 key = nilfs_btree_node_get_key(node, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002175 } else
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002176 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002177
2178 /* on-disk format */
2179 binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002180 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002181
2182 return 0;
2183}
2184
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002185static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
Koji Sato17c76b02009-04-06 19:01:24 -07002186{
2187 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07002188 struct nilfs_btree_path *path;
2189 __u64 ptr;
2190 int ret;
2191
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002192 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002193 if (path == NULL)
2194 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002195
2196 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1);
2197 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002198 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002199 goto out;
2200 }
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09002201 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002202 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002203 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002204 goto out;
2205 }
2206
2207 if (!buffer_dirty(bh))
2208 nilfs_btnode_mark_dirty(bh);
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09002209 brelse(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002210 if (!nilfs_bmap_dirty(btree))
2211 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002212
2213 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002214 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002215 return ret;
2216}
2217
2218static const struct nilfs_bmap_operations nilfs_btree_ops = {
2219 .bop_lookup = nilfs_btree_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002220 .bop_lookup_contig = nilfs_btree_lookup_contig,
Koji Sato17c76b02009-04-06 19:01:24 -07002221 .bop_insert = nilfs_btree_insert,
2222 .bop_delete = nilfs_btree_delete,
2223 .bop_clear = NULL,
2224
2225 .bop_propagate = nilfs_btree_propagate,
2226
2227 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2228
2229 .bop_assign = nilfs_btree_assign,
2230 .bop_mark = nilfs_btree_mark,
2231
2232 .bop_last_key = nilfs_btree_last_key,
2233 .bop_check_insert = NULL,
2234 .bop_check_delete = nilfs_btree_check_delete,
2235 .bop_gather_data = nilfs_btree_gather_data,
2236};
2237
2238static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
2239 .bop_lookup = NULL,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002240 .bop_lookup_contig = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002241 .bop_insert = NULL,
2242 .bop_delete = NULL,
2243 .bop_clear = NULL,
2244
2245 .bop_propagate = nilfs_btree_propagate_gc,
2246
2247 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2248
2249 .bop_assign = nilfs_btree_assign_gc,
2250 .bop_mark = NULL,
2251
2252 .bop_last_key = NULL,
2253 .bop_check_insert = NULL,
2254 .bop_check_delete = NULL,
2255 .bop_gather_data = NULL,
2256};
2257
Ryusuke Konishi30333422009-05-24 00:09:44 +09002258int nilfs_btree_init(struct nilfs_bmap *bmap)
Koji Sato17c76b02009-04-06 19:01:24 -07002259{
Koji Sato17c76b02009-04-06 19:01:24 -07002260 bmap->b_ops = &nilfs_btree_ops;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002261 bmap->b_nchildren_per_block =
2262 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002263 return 0;
2264}
2265
2266void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
2267{
Koji Sato17c76b02009-04-06 19:01:24 -07002268 bmap->b_ops = &nilfs_btree_ops_gc;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002269 bmap->b_nchildren_per_block =
2270 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002271}