blob: b2e3ff34762070a4b37085c051223809b59e4a6c [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{
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900373 int ret;
374
375 if (buffer_nilfs_checked(bh))
376 return 0;
377
378 ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900379 bh->b_size, bh->b_blocknr);
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900380 if (likely(!ret))
381 set_buffer_nilfs_checked(bh);
382 return ret;
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900383}
384
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900385static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900386nilfs_btree_get_root(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700387{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900388 return (struct nilfs_btree_node *)btree->b_u.u_data;
Koji Sato17c76b02009-04-06 19:01:24 -0700389}
390
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900391static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900392nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700393{
394 return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
395}
396
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900397static struct nilfs_btree_node *
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900398nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
Koji Sato17c76b02009-04-06 19:01:24 -0700399{
400 return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
401}
402
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900403static int nilfs_btree_height(const struct nilfs_bmap *btree)
Koji Sato17c76b02009-04-06 19:01:24 -0700404{
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900405 return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700406}
407
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900408static struct nilfs_btree_node *
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900409nilfs_btree_get_node(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700410 const struct nilfs_btree_path *path,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900411 int level, int *ncmaxp)
Koji Sato17c76b02009-04-06 19:01:24 -0700412{
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900413 struct nilfs_btree_node *node;
414
415 if (level == nilfs_btree_height(btree) - 1) {
416 node = nilfs_btree_get_root(btree);
417 *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
418 } else {
419 node = nilfs_btree_get_nonroot_node(path, level);
420 *ncmaxp = nilfs_btree_nchildren_per_block(btree);
421 }
422 return node;
Koji Sato17c76b02009-04-06 19:01:24 -0700423}
424
Ryusuke Konishi7c397a82010-07-13 23:33:55 +0900425static int
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900426nilfs_btree_bad_node(struct nilfs_btree_node *node, int level)
427{
428 if (unlikely(nilfs_btree_node_get_level(node) != level)) {
429 dump_stack();
430 printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n",
431 nilfs_btree_node_get_level(node), level);
432 return 1;
433 }
434 return 0;
435}
436
Ryusuke Konishi464ece82010-07-18 10:42:24 +0900437struct nilfs_btree_readahead_info {
438 struct nilfs_btree_node *node; /* parent node */
439 int max_ra_blocks; /* max nof blocks to read ahead */
440 int index; /* current index on the parent node */
441 int ncmax; /* nof children in the parent node */
442};
443
444static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
445 struct buffer_head **bhp,
446 const struct nilfs_btree_readahead_info *ra)
447{
448 struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
449 struct buffer_head *bh, *ra_bh;
450 sector_t submit_ptr = 0;
451 int ret;
452
453 ret = nilfs_btnode_submit_block(btnc, ptr, 0, READ, &bh, &submit_ptr);
454 if (ret) {
455 if (ret != -EEXIST)
456 return ret;
457 goto out_check;
458 }
459
460 if (ra) {
461 int i, n;
462 __u64 ptr2;
463
464 /* read ahead sibling nodes */
465 for (n = ra->max_ra_blocks, i = ra->index + 1;
466 n > 0 && i < ra->ncmax; n--, i++) {
467 ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
468
469 ret = nilfs_btnode_submit_block(btnc, ptr2, 0, READA,
470 &ra_bh, &submit_ptr);
471 if (likely(!ret || ret == -EEXIST))
472 brelse(ra_bh);
473 else if (ret != -EBUSY)
474 break;
475 if (!buffer_locked(bh))
476 goto out_no_wait;
477 }
478 }
479
480 wait_on_buffer(bh);
481
482 out_no_wait:
483 if (!buffer_uptodate(bh)) {
484 brelse(bh);
485 return -EIO;
486 }
487
488 out_check:
489 if (nilfs_btree_broken_node_block(bh)) {
490 clear_buffer_uptodate(bh);
491 brelse(bh);
492 return -EINVAL;
493 }
494
495 *bhp = bh;
496 return 0;
497}
498
499static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
500 struct buffer_head **bhp)
501{
502 return __nilfs_btree_get_block(btree, ptr, bhp, NULL);
503}
504
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900505static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700506 struct nilfs_btree_path *path,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900507 __u64 key, __u64 *ptrp, int minlevel,
508 int readahead)
Koji Sato17c76b02009-04-06 19:01:24 -0700509{
510 struct nilfs_btree_node *node;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900511 struct nilfs_btree_readahead_info p, *ra;
Koji Sato17c76b02009-04-06 19:01:24 -0700512 __u64 ptr;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900513 int level, index, found, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700514
Koji Sato17c76b02009-04-06 19:01:24 -0700515 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900516 level = nilfs_btree_node_get_level(node);
517 if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
Koji Sato17c76b02009-04-06 19:01:24 -0700518 return -ENOENT;
519
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900520 found = nilfs_btree_node_lookup(node, key, &index);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900521 ptr = nilfs_btree_node_get_ptr(node, index,
522 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700523 path[level].bp_bh = NULL;
524 path[level].bp_index = index;
525
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900526 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900527
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900528 while (--level >= minlevel) {
529 ra = NULL;
530 if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) {
531 p.node = nilfs_btree_get_node(btree, path, level + 1,
532 &p.ncmax);
533 p.index = index;
534 p.max_ra_blocks = 7;
535 ra = &p;
536 }
537 ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh,
538 ra);
Koji Sato17c76b02009-04-06 19:01:24 -0700539 if (ret < 0)
540 return ret;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900541
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900542 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900543 if (nilfs_btree_bad_node(node, level))
544 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -0700545 if (!found)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900546 found = nilfs_btree_node_lookup(node, key, &index);
Koji Sato17c76b02009-04-06 19:01:24 -0700547 else
548 index = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900549 if (index < ncmax) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900550 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +0900551 } else {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700552 WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
Koji Sato17c76b02009-04-06 19:01:24 -0700553 /* insert */
554 ptr = NILFS_BMAP_INVALID_PTR;
555 }
556 path[level].bp_index = index;
557 }
558 if (!found)
559 return -ENOENT;
560
561 if (ptrp != NULL)
562 *ptrp = ptr;
563
564 return 0;
565}
566
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900567static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700568 struct nilfs_btree_path *path,
569 __u64 *keyp, __u64 *ptrp)
570{
571 struct nilfs_btree_node *node;
572 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900573 int index, level, ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -0700574
575 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900576 index = nilfs_btree_node_get_nchildren(node) - 1;
Koji Sato17c76b02009-04-06 19:01:24 -0700577 if (index < 0)
578 return -ENOENT;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900579 level = nilfs_btree_node_get_level(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900580 ptr = nilfs_btree_node_get_ptr(node, index,
581 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700582 path[level].bp_bh = NULL;
583 path[level].bp_index = index;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900584 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700585
586 for (level--; level > 0; level--) {
Ryusuke Konishif198dbb2009-05-22 01:07:13 +0900587 ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700588 if (ret < 0)
589 return ret;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900590 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b945d52009-10-10 22:58:10 +0900591 if (nilfs_btree_bad_node(node, level))
592 return -EINVAL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900593 index = nilfs_btree_node_get_nchildren(node) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900594 ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700595 path[level].bp_index = index;
596 }
597
598 if (keyp != NULL)
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900599 *keyp = nilfs_btree_node_get_key(node, index);
Koji Sato17c76b02009-04-06 19:01:24 -0700600 if (ptrp != NULL)
601 *ptrp = ptr;
602
603 return 0;
604}
605
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900606static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700607 __u64 key, int level, __u64 *ptrp)
608{
Koji Sato17c76b02009-04-06 19:01:24 -0700609 struct nilfs_btree_path *path;
Koji Sato17c76b02009-04-06 19:01:24 -0700610 int ret;
611
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900612 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -0700613 if (path == NULL)
614 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -0700615
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900616 ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700617
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900618 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -0700619
620 return ret;
621}
622
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900623static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900624 __u64 key, __u64 *ptrp, unsigned maxblocks)
625{
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900626 struct nilfs_btree_path *path;
627 struct nilfs_btree_node *node;
628 struct inode *dat = NULL;
629 __u64 ptr, ptr2;
630 sector_t blocknr;
631 int level = NILFS_BTREE_LEVEL_NODE_MIN;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900632 int ret, cnt, index, maxlevel, ncmax;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900633 struct nilfs_btree_readahead_info p;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900634
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900635 path = nilfs_btree_alloc_path();
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900636 if (path == NULL)
637 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +0800638
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900639 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900640 if (ret < 0)
641 goto out;
642
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900643 if (NILFS_BMAP_USE_VBN(btree)) {
644 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900645 ret = nilfs_dat_translate(dat, ptr, &blocknr);
646 if (ret < 0)
647 goto out;
648 ptr = blocknr;
649 }
650 cnt = 1;
651 if (cnt == maxblocks)
652 goto end;
653
654 maxlevel = nilfs_btree_height(btree) - 1;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900655 node = nilfs_btree_get_node(btree, path, level, &ncmax);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900656 index = path[level].bp_index + 1;
657 for (;;) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900658 while (index < nilfs_btree_node_get_nchildren(node)) {
659 if (nilfs_btree_node_get_key(node, index) !=
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900660 key + cnt)
661 goto end;
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 if (dat) {
664 ret = nilfs_dat_translate(dat, ptr2, &blocknr);
665 if (ret < 0)
666 goto out;
667 ptr2 = blocknr;
668 }
669 if (ptr2 != ptr + cnt || ++cnt == maxblocks)
670 goto end;
671 index++;
672 continue;
673 }
674 if (level == maxlevel)
675 break;
676
677 /* look-up right sibling node */
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900678 p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax);
679 p.index = path[level + 1].bp_index + 1;
680 p.max_ra_blocks = 7;
681 if (p.index >= nilfs_btree_node_get_nchildren(p.node) ||
682 nilfs_btree_node_get_key(p.node, p.index) != key + cnt)
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900683 break;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900684 ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax);
685 path[level + 1].bp_index = p.index;
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900686
687 brelse(path[level].bp_bh);
688 path[level].bp_bh = NULL;
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +0900689
690 ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
691 &p);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900692 if (ret < 0)
693 goto out;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900694 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900695 ncmax = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900696 index = 0;
697 path[level].bp_index = index;
698 }
699 end:
700 *ptrp = ptr;
701 ret = cnt;
702 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900703 nilfs_btree_free_path(path);
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +0900704 return ret;
705}
706
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900707static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700708 struct nilfs_btree_path *path,
709 int level, __u64 key)
710{
711 if (level < nilfs_btree_height(btree) - 1) {
712 do {
Koji Sato17c76b02009-04-06 19:01:24 -0700713 nilfs_btree_node_set_key(
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900714 nilfs_btree_get_nonroot_node(path, level),
Koji Sato17c76b02009-04-06 19:01:24 -0700715 path[level].bp_index, key);
716 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900717 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700718 } while ((path[level].bp_index == 0) &&
719 (++level < nilfs_btree_height(btree) - 1));
720 }
721
722 /* root */
723 if (level == nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900724 nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
Koji Sato17c76b02009-04-06 19:01:24 -0700725 path[level].bp_index, key);
726 }
727}
728
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900729static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700730 struct nilfs_btree_path *path,
731 int level, __u64 *keyp, __u64 *ptrp)
732{
733 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900734 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700735
736 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900737 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900738 ncblk = nilfs_btree_nchildren_per_block(btree);
739 nilfs_btree_node_insert(node, path[level].bp_index,
740 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700741 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900742 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700743
744 if (path[level].bp_index == 0)
745 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900746 nilfs_btree_node_get_key(node,
747 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700748 } else {
749 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900750 nilfs_btree_node_insert(node, path[level].bp_index,
751 *keyp, *ptrp,
752 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -0700753 }
754}
755
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900756static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700757 struct nilfs_btree_path *path,
758 int level, __u64 *keyp, __u64 *ptrp)
759{
760 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900761 int nchildren, lnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700762
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900763 node = nilfs_btree_get_nonroot_node(path, level);
764 left = nilfs_btree_get_sib_node(path, level);
765 nchildren = nilfs_btree_node_get_nchildren(node);
766 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900767 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700768 move = 0;
769
770 n = (nchildren + lnchildren + 1) / 2 - lnchildren;
771 if (n > path[level].bp_index) {
772 /* move insert point */
773 n--;
774 move = 1;
775 }
776
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900777 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700778
779 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900780 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700781 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900782 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700783
Koji Sato17c76b02009-04-06 19:01:24 -0700784 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900785 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700786
787 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900788 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700789 path[level].bp_bh = path[level].bp_sib_bh;
790 path[level].bp_sib_bh = NULL;
791 path[level].bp_index += lnchildren;
792 path[level + 1].bp_index--;
793 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900794 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700795 path[level].bp_sib_bh = NULL;
796 path[level].bp_index -= n;
797 }
798
799 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
800}
801
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900802static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700803 struct nilfs_btree_path *path,
804 int level, __u64 *keyp, __u64 *ptrp)
805{
806 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900807 int nchildren, rnchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700808
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900809 node = nilfs_btree_get_nonroot_node(path, level);
810 right = nilfs_btree_get_sib_node(path, level);
811 nchildren = nilfs_btree_node_get_nchildren(node);
812 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900813 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700814 move = 0;
815
816 n = (nchildren + rnchildren + 1) / 2 - rnchildren;
817 if (n > nchildren - path[level].bp_index) {
818 /* move insert point */
819 n--;
820 move = 1;
821 }
822
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900823 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700824
825 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900826 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700827 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900828 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700829
Koji Sato17c76b02009-04-06 19:01:24 -0700830 path[level + 1].bp_index++;
831 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900832 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -0700833 path[level + 1].bp_index--;
834
835 if (move) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900836 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700837 path[level].bp_bh = path[level].bp_sib_bh;
838 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900839 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -0700840 path[level + 1].bp_index++;
841 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900842 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700843 path[level].bp_sib_bh = NULL;
844 }
845
846 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
847}
848
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900849static void nilfs_btree_split(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700850 struct nilfs_btree_path *path,
851 int level, __u64 *keyp, __u64 *ptrp)
852{
853 struct nilfs_btree_node *node, *right;
854 __u64 newkey;
855 __u64 newptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900856 int nchildren, n, move, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700857
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900858 node = nilfs_btree_get_nonroot_node(path, level);
859 right = nilfs_btree_get_sib_node(path, level);
860 nchildren = nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900861 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700862 move = 0;
863
864 n = (nchildren + 1) / 2;
865 if (n > nchildren - path[level].bp_index) {
866 n--;
867 move = 1;
868 }
869
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900870 nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700871
872 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900873 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700874 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900875 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700876
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900877 newkey = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700878 newptr = path[level].bp_newreq.bpr_ptr;
879
880 if (move) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900881 path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900882 nilfs_btree_node_insert(right, path[level].bp_index,
883 *keyp, *ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -0700884
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900885 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700886 *ptrp = path[level].bp_newreq.bpr_ptr;
887
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900888 brelse(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700889 path[level].bp_bh = path[level].bp_sib_bh;
890 path[level].bp_sib_bh = NULL;
891 } else {
892 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
893
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900894 *keyp = nilfs_btree_node_get_key(right, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700895 *ptrp = path[level].bp_newreq.bpr_ptr;
896
Ryusuke Konishi087d01b2009-05-22 00:33:13 +0900897 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700898 path[level].bp_sib_bh = NULL;
899 }
900
901 path[level + 1].bp_index++;
902}
903
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900904static void nilfs_btree_grow(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700905 struct nilfs_btree_path *path,
906 int level, __u64 *keyp, __u64 *ptrp)
907{
908 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900909 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -0700910
Koji Sato17c76b02009-04-06 19:01:24 -0700911 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900912 child = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900913 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700914
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900915 n = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -0700916
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900917 nilfs_btree_node_move_right(root, child, n,
918 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900919 nilfs_btree_node_set_level(root, level + 1);
Koji Sato17c76b02009-04-06 19:01:24 -0700920
921 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900922 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -0700923
Koji Sato17c76b02009-04-06 19:01:24 -0700924 path[level].bp_bh = path[level].bp_sib_bh;
925 path[level].bp_sib_bh = NULL;
926
927 nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
928
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +0900929 *keyp = nilfs_btree_node_get_key(child, 0);
Koji Sato17c76b02009-04-06 19:01:24 -0700930 *ptrp = path[level].bp_newreq.bpr_ptr;
931}
932
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900933static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700934 const struct nilfs_btree_path *path)
935{
936 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900937 int level, ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -0700938
939 if (path == NULL)
940 return NILFS_BMAP_INVALID_PTR;
941
942 /* left sibling */
943 level = NILFS_BTREE_LEVEL_NODE_MIN;
944 if (path[level].bp_index > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900945 node = nilfs_btree_get_node(btree, path, level, &ncmax);
946 return nilfs_btree_node_get_ptr(node,
947 path[level].bp_index - 1,
948 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700949 }
950
951 /* parent */
952 level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
953 if (level <= nilfs_btree_height(btree) - 1) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900954 node = nilfs_btree_get_node(btree, path, level, &ncmax);
955 return nilfs_btree_node_get_ptr(node, path[level].bp_index,
956 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -0700957 }
958
959 return NILFS_BMAP_INVALID_PTR;
960}
961
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900962static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700963 const struct nilfs_btree_path *path,
964 __u64 key)
965{
966 __u64 ptr;
967
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900968 ptr = nilfs_bmap_find_target_seq(btree, key);
Koji Sato17c76b02009-04-06 19:01:24 -0700969 if (ptr != NILFS_BMAP_INVALID_PTR)
970 /* sequential access */
971 return ptr;
972 else {
973 ptr = nilfs_btree_find_near(btree, path);
974 if (ptr != NILFS_BMAP_INVALID_PTR)
975 /* near */
976 return ptr;
977 }
978 /* block group */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900979 return nilfs_bmap_find_target_in_group(btree);
Koji Sato17c76b02009-04-06 19:01:24 -0700980}
981
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900982static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -0700983 struct nilfs_btree_path *path,
984 int *levelp, __u64 key, __u64 ptr,
985 struct nilfs_bmap_stats *stats)
986{
987 struct buffer_head *bh;
988 struct nilfs_btree_node *node, *parent, *sib;
989 __u64 sibptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +0900990 int pindex, level, ncmax, ncblk, ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +0900991 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -0700992
993 stats->bs_nblocks = 0;
994 level = NILFS_BTREE_LEVEL_DATA;
995
996 /* allocate a new ptr for data block */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +0900997 if (NILFS_BMAP_USE_VBN(btree)) {
Koji Sato17c76b02009-04-06 19:01:24 -0700998 path[level].bp_newreq.bpr_ptr =
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +0900999 nilfs_btree_find_target_v(btree, path, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001000 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001001 }
Koji Sato17c76b02009-04-06 19:01:24 -07001002
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001003 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001004 if (ret < 0)
1005 goto err_out_data;
1006
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001007 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001008
Koji Sato17c76b02009-04-06 19:01:24 -07001009 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
1010 level < nilfs_btree_height(btree) - 1;
1011 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001012 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001013 if (nilfs_btree_node_get_nchildren(node) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001014 path[level].bp_op = nilfs_btree_do_insert;
1015 stats->bs_nblocks++;
1016 goto out;
1017 }
1018
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001019 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001020 pindex = path[level + 1].bp_index;
1021
1022 /* left sibling */
1023 if (pindex > 0) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001024 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1025 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001026 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001027 if (ret < 0)
1028 goto err_out_child_node;
1029 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001030 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001031 path[level].bp_sib_bh = bh;
1032 path[level].bp_op = nilfs_btree_carry_left;
1033 stats->bs_nblocks++;
1034 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001035 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001036 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001037 }
Koji Sato17c76b02009-04-06 19:01:24 -07001038 }
1039
1040 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001041 if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
1042 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1043 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001044 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001045 if (ret < 0)
1046 goto err_out_child_node;
1047 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001048 if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
Koji Sato17c76b02009-04-06 19:01:24 -07001049 path[level].bp_sib_bh = bh;
1050 path[level].bp_op = nilfs_btree_carry_right;
1051 stats->bs_nblocks++;
1052 goto out;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001053 } else {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001054 brelse(bh);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001055 }
Koji Sato17c76b02009-04-06 19:01:24 -07001056 }
1057
1058 /* split */
1059 path[level].bp_newreq.bpr_ptr =
1060 path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001061 ret = nilfs_bmap_prepare_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001062 &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001063 if (ret < 0)
1064 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001065 ret = nilfs_btree_get_new_block(btree,
1066 path[level].bp_newreq.bpr_ptr,
1067 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001068 if (ret < 0)
1069 goto err_out_curr_node;
1070
1071 stats->bs_nblocks++;
1072
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001073 sib = (struct nilfs_btree_node *)bh->b_data;
1074 nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001075 path[level].bp_sib_bh = bh;
1076 path[level].bp_op = nilfs_btree_split;
1077 }
1078
1079 /* root */
1080 node = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001081 if (nilfs_btree_node_get_nchildren(node) <
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001082 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
Koji Sato17c76b02009-04-06 19:01:24 -07001083 path[level].bp_op = nilfs_btree_do_insert;
1084 stats->bs_nblocks++;
1085 goto out;
1086 }
1087
1088 /* grow */
1089 path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001090 ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001091 if (ret < 0)
1092 goto err_out_child_node;
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001093 ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
1094 &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001095 if (ret < 0)
1096 goto err_out_curr_node;
1097
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001098 nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
1099 0, level, 0, ncblk, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001100 path[level].bp_sib_bh = bh;
1101 path[level].bp_op = nilfs_btree_grow;
1102
1103 level++;
1104 path[level].bp_op = nilfs_btree_do_insert;
1105
1106 /* a newly-created node block and a data block are added */
1107 stats->bs_nblocks += 2;
1108
1109 /* success */
1110 out:
1111 *levelp = level;
1112 return ret;
1113
1114 /* error */
1115 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001116 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001117 err_out_child_node:
1118 for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001119 nilfs_btnode_delete(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001120 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001121
1122 }
1123
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001124 nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001125 err_out_data:
1126 *levelp = level;
1127 stats->bs_nblocks = 0;
1128 return ret;
1129}
1130
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001131static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001132 struct nilfs_btree_path *path,
1133 int maxlevel, __u64 key, __u64 ptr)
1134{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001135 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001136 int level;
1137
1138 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1139 ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001140 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001141 nilfs_bmap_set_target_v(btree, key, ptr);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001142 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001143 }
Koji Sato17c76b02009-04-06 19:01:24 -07001144
1145 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001146 nilfs_bmap_commit_alloc_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001147 &path[level - 1].bp_newreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001148 path[level].bp_op(btree, path, level, &key, &ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001149 }
1150
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001151 if (!nilfs_bmap_dirty(btree))
1152 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001153}
1154
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001155static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
Koji Sato17c76b02009-04-06 19:01:24 -07001156{
Koji Sato17c76b02009-04-06 19:01:24 -07001157 struct nilfs_btree_path *path;
1158 struct nilfs_bmap_stats stats;
1159 int level, ret;
1160
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001161 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001162 if (path == NULL)
1163 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001164
1165 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001166 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001167 if (ret != -ENOENT) {
1168 if (ret == 0)
1169 ret = -EEXIST;
1170 goto out;
1171 }
1172
1173 ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
1174 if (ret < 0)
1175 goto out;
1176 nilfs_btree_commit_insert(btree, path, level, key, ptr);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001177 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001178
1179 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001180 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001181 return ret;
1182}
1183
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001184static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001185 struct nilfs_btree_path *path,
1186 int level, __u64 *keyp, __u64 *ptrp)
1187{
1188 struct nilfs_btree_node *node;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001189 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001190
1191 if (level < nilfs_btree_height(btree) - 1) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001192 node = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001193 ncblk = nilfs_btree_nchildren_per_block(btree);
1194 nilfs_btree_node_delete(node, path[level].bp_index,
1195 keyp, ptrp, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001196 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001197 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001198 if (path[level].bp_index == 0)
1199 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001200 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001201 } else {
1202 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001203 nilfs_btree_node_delete(node, path[level].bp_index,
1204 keyp, ptrp,
1205 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Koji Sato17c76b02009-04-06 19:01:24 -07001206 }
1207}
1208
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001209static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001210 struct nilfs_btree_path *path,
1211 int level, __u64 *keyp, __u64 *ptrp)
1212{
1213 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001214 int nchildren, lnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001215
1216 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1217
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001218 node = nilfs_btree_get_nonroot_node(path, level);
1219 left = nilfs_btree_get_sib_node(path, level);
1220 nchildren = nilfs_btree_node_get_nchildren(node);
1221 lnchildren = nilfs_btree_node_get_nchildren(left);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001222 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001223
1224 n = (nchildren + lnchildren) / 2 - nchildren;
1225
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001226 nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001227
1228 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001229 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001230 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001231 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001232
Koji Sato17c76b02009-04-06 19:01:24 -07001233 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001234 nilfs_btree_node_get_key(node, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001235
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001236 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001237 path[level].bp_sib_bh = NULL;
1238 path[level].bp_index += n;
1239}
1240
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001241static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001242 struct nilfs_btree_path *path,
1243 int level, __u64 *keyp, __u64 *ptrp)
1244{
1245 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001246 int nchildren, rnchildren, n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001247
1248 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1249
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001250 node = nilfs_btree_get_nonroot_node(path, level);
1251 right = nilfs_btree_get_sib_node(path, level);
1252 nchildren = nilfs_btree_node_get_nchildren(node);
1253 rnchildren = nilfs_btree_node_get_nchildren(right);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001254 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001255
1256 n = (nchildren + rnchildren) / 2 - nchildren;
1257
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001258 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001259
1260 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001261 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001262 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001263 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001264
Koji Sato17c76b02009-04-06 19:01:24 -07001265 path[level + 1].bp_index++;
1266 nilfs_btree_promote_key(btree, path, level + 1,
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001267 nilfs_btree_node_get_key(right, 0));
Koji Sato17c76b02009-04-06 19:01:24 -07001268 path[level + 1].bp_index--;
1269
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001270 brelse(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001271 path[level].bp_sib_bh = NULL;
1272}
1273
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001274static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001275 struct nilfs_btree_path *path,
1276 int level, __u64 *keyp, __u64 *ptrp)
1277{
1278 struct nilfs_btree_node *node, *left;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001279 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001280
1281 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1282
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001283 node = nilfs_btree_get_nonroot_node(path, level);
1284 left = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001285 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001286
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001287 n = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001288
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001289 nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001290
1291 if (!buffer_dirty(path[level].bp_sib_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001292 mark_buffer_dirty(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001293
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001294 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001295 path[level].bp_bh = path[level].bp_sib_bh;
1296 path[level].bp_sib_bh = NULL;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001297 path[level].bp_index += nilfs_btree_node_get_nchildren(left);
Koji Sato17c76b02009-04-06 19:01:24 -07001298}
1299
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001300static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001301 struct nilfs_btree_path *path,
1302 int level, __u64 *keyp, __u64 *ptrp)
1303{
1304 struct nilfs_btree_node *node, *right;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001305 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001306
1307 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1308
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001309 node = nilfs_btree_get_nonroot_node(path, level);
1310 right = nilfs_btree_get_sib_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001311 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001312
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001313 n = nilfs_btree_node_get_nchildren(right);
Koji Sato17c76b02009-04-06 19:01:24 -07001314
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001315 nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001316
1317 if (!buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001318 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001319
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001320 nilfs_btnode_delete(path[level].bp_sib_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001321 path[level].bp_sib_bh = NULL;
1322 path[level + 1].bp_index++;
1323}
1324
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001325static void nilfs_btree_shrink(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001326 struct nilfs_btree_path *path,
1327 int level, __u64 *keyp, __u64 *ptrp)
1328{
1329 struct nilfs_btree_node *root, *child;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001330 int n, ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001331
1332 nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
1333
Koji Sato17c76b02009-04-06 19:01:24 -07001334 root = nilfs_btree_get_root(btree);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001335 child = nilfs_btree_get_nonroot_node(path, level);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001336 ncblk = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001337
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001338 nilfs_btree_node_delete(root, 0, NULL, NULL,
1339 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001340 nilfs_btree_node_set_level(root, level);
1341 n = nilfs_btree_node_get_nchildren(child);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001342 nilfs_btree_node_move_left(root, child, n,
1343 NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001344
Ryusuke Konishi9f098902009-05-22 00:38:56 +09001345 nilfs_btnode_delete(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001346 path[level].bp_bh = NULL;
1347}
1348
Ryusuke Konishid4099052011-05-25 23:00:27 +09001349static void nilfs_btree_nop(struct nilfs_bmap *btree,
1350 struct nilfs_btree_path *path,
1351 int level, __u64 *keyp, __u64 *ptrp)
1352{
1353}
Koji Sato17c76b02009-04-06 19:01:24 -07001354
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001355static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001356 struct nilfs_btree_path *path,
1357 int *levelp,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001358 struct nilfs_bmap_stats *stats,
1359 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001360{
1361 struct buffer_head *bh;
1362 struct nilfs_btree_node *node, *parent, *sib;
1363 __u64 sibptr;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001364 int pindex, dindex, level, ncmin, ncmax, ncblk, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001365
1366 ret = 0;
1367 stats->bs_nblocks = 0;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001368 ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001369 ncblk = nilfs_btree_nchildren_per_block(btree);
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001370
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001371 for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index;
Koji Sato17c76b02009-04-06 19:01:24 -07001372 level < nilfs_btree_height(btree) - 1;
1373 level++) {
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001374 node = nilfs_btree_get_nonroot_node(path, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001375 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001376 nilfs_btree_node_get_ptr(node, dindex, ncblk);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001377 ret = nilfs_bmap_prepare_end_ptr(btree,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001378 &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001379 if (ret < 0)
1380 goto err_out_child_node;
Koji Sato17c76b02009-04-06 19:01:24 -07001381
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001382 if (nilfs_btree_node_get_nchildren(node) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001383 path[level].bp_op = nilfs_btree_do_delete;
1384 stats->bs_nblocks++;
1385 goto out;
1386 }
1387
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001388 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001389 pindex = path[level + 1].bp_index;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001390 dindex = pindex;
Koji Sato17c76b02009-04-06 19:01:24 -07001391
1392 if (pindex > 0) {
1393 /* left sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001394 sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
1395 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001396 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001397 if (ret < 0)
1398 goto err_out_curr_node;
1399 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001400 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001401 path[level].bp_sib_bh = bh;
1402 path[level].bp_op = nilfs_btree_borrow_left;
1403 stats->bs_nblocks++;
1404 goto out;
1405 } else {
1406 path[level].bp_sib_bh = bh;
1407 path[level].bp_op = nilfs_btree_concat_left;
1408 stats->bs_nblocks++;
1409 /* continue; */
1410 }
1411 } else if (pindex <
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001412 nilfs_btree_node_get_nchildren(parent) - 1) {
Koji Sato17c76b02009-04-06 19:01:24 -07001413 /* right sibling */
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001414 sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
1415 ncmax);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001416 ret = nilfs_btree_get_block(btree, sibptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001417 if (ret < 0)
1418 goto err_out_curr_node;
1419 sib = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishiea64ab82010-07-13 23:33:52 +09001420 if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
Koji Sato17c76b02009-04-06 19:01:24 -07001421 path[level].bp_sib_bh = bh;
1422 path[level].bp_op = nilfs_btree_borrow_right;
1423 stats->bs_nblocks++;
1424 goto out;
1425 } else {
1426 path[level].bp_sib_bh = bh;
1427 path[level].bp_op = nilfs_btree_concat_right;
1428 stats->bs_nblocks++;
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001429 /*
1430 * When merging right sibling node
1431 * into the current node, pointer to
1432 * the right sibling node must be
1433 * terminated instead. The adjustment
1434 * below is required for that.
1435 */
1436 dindex = pindex + 1;
Koji Sato17c76b02009-04-06 19:01:24 -07001437 /* continue; */
1438 }
1439 } else {
1440 /* no siblings */
1441 /* the only child of the root node */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001442 WARN_ON(level != nilfs_btree_height(btree) - 2);
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001443 if (nilfs_btree_node_get_nchildren(node) - 1 <=
Koji Sato17c76b02009-04-06 19:01:24 -07001444 NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1445 path[level].bp_op = nilfs_btree_shrink;
1446 stats->bs_nblocks += 2;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001447 level++;
1448 path[level].bp_op = nilfs_btree_nop;
1449 goto shrink_root_child;
Koji Sato17c76b02009-04-06 19:01:24 -07001450 } else {
1451 path[level].bp_op = nilfs_btree_do_delete;
1452 stats->bs_nblocks++;
Ryusuke Konishid4099052011-05-25 23:00:27 +09001453 goto out;
Koji Sato17c76b02009-04-06 19:01:24 -07001454 }
Koji Sato17c76b02009-04-06 19:01:24 -07001455 }
1456 }
1457
Ryusuke Konishid4099052011-05-25 23:00:27 +09001458 /* child of the root node is deleted */
1459 path[level].bp_op = nilfs_btree_do_delete;
1460 stats->bs_nblocks++;
1461
1462shrink_root_child:
Koji Sato17c76b02009-04-06 19:01:24 -07001463 node = nilfs_btree_get_root(btree);
1464 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishife744fd2011-05-25 23:00:27 +09001465 nilfs_btree_node_get_ptr(node, dindex,
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001466 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001467
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001468 ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
Ryusuke Konishid4b96152009-05-24 03:25:44 +09001469 if (ret < 0)
1470 goto err_out_child_node;
1471
Koji Sato17c76b02009-04-06 19:01:24 -07001472 /* success */
1473 out:
1474 *levelp = level;
1475 return ret;
1476
1477 /* error */
1478 err_out_curr_node:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001479 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001480 err_out_child_node:
1481 for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001482 brelse(path[level].bp_sib_bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001483 nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001484 }
1485 *levelp = level;
1486 stats->bs_nblocks = 0;
1487 return ret;
1488}
1489
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001490static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001491 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001492 int maxlevel, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001493{
1494 int level;
1495
1496 for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001497 nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
Pekka Enberg8acfbf02009-04-06 19:01:49 -07001498 path[level].bp_op(btree, path, level, NULL, NULL);
Koji Sato17c76b02009-04-06 19:01:24 -07001499 }
1500
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001501 if (!nilfs_bmap_dirty(btree))
1502 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001503}
1504
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001505static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001506
1507{
Koji Sato17c76b02009-04-06 19:01:24 -07001508 struct nilfs_btree_path *path;
1509 struct nilfs_bmap_stats stats;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001510 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001511 int level, ret;
1512
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001513 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001514 if (path == NULL)
1515 return -ENOMEM;
Li Hongf9054402010-04-02 17:36:34 +08001516
Koji Sato17c76b02009-04-06 19:01:24 -07001517 ret = nilfs_btree_do_lookup(btree, path, key, NULL,
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001518 NILFS_BTREE_LEVEL_NODE_MIN, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001519 if (ret < 0)
1520 goto out;
1521
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001522
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001523 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001524
1525 ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001526 if (ret < 0)
1527 goto out;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001528 nilfs_btree_commit_delete(btree, path, level, dat);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001529 nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001530
1531out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001532 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001533 return ret;
1534}
1535
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001536static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
Koji Sato17c76b02009-04-06 19:01:24 -07001537{
Koji Sato17c76b02009-04-06 19:01:24 -07001538 struct nilfs_btree_path *path;
1539 int ret;
1540
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001541 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001542 if (path == NULL)
1543 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001544
1545 ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
1546
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001547 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07001548
1549 return ret;
1550}
1551
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001552static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
Koji Sato17c76b02009-04-06 19:01:24 -07001553{
1554 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001555 struct nilfs_btree_node *root, *node;
1556 __u64 maxkey, nextmaxkey;
1557 __u64 ptr;
1558 int nchildren, ret;
1559
Koji Sato17c76b02009-04-06 19:01:24 -07001560 root = nilfs_btree_get_root(btree);
1561 switch (nilfs_btree_height(btree)) {
1562 case 2:
1563 bh = NULL;
1564 node = root;
1565 break;
1566 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001567 nchildren = nilfs_btree_node_get_nchildren(root);
Koji Sato17c76b02009-04-06 19:01:24 -07001568 if (nchildren > 1)
1569 return 0;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001570 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1571 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001572 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001573 if (ret < 0)
1574 return ret;
1575 node = (struct nilfs_btree_node *)bh->b_data;
1576 break;
1577 default:
1578 return 0;
1579 }
1580
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001581 nchildren = nilfs_btree_node_get_nchildren(node);
1582 maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
Koji Sato17c76b02009-04-06 19:01:24 -07001583 nextmaxkey = (nchildren > 1) ?
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001584 nilfs_btree_node_get_key(node, nchildren - 2) : 0;
Koji Sato17c76b02009-04-06 19:01:24 -07001585 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001586 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001587
Ryusuke Konishi30333422009-05-24 00:09:44 +09001588 return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
Koji Sato17c76b02009-04-06 19:01:24 -07001589}
1590
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001591static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001592 __u64 *keys, __u64 *ptrs, int nitems)
1593{
1594 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07001595 struct nilfs_btree_node *node, *root;
1596 __le64 *dkeys;
1597 __le64 *dptrs;
1598 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001599 int nchildren, ncmax, i, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001600
Koji Sato17c76b02009-04-06 19:01:24 -07001601 root = nilfs_btree_get_root(btree);
1602 switch (nilfs_btree_height(btree)) {
1603 case 2:
1604 bh = NULL;
1605 node = root;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001606 ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
Koji Sato17c76b02009-04-06 19:01:24 -07001607 break;
1608 case 3:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001609 nchildren = nilfs_btree_node_get_nchildren(root);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001610 WARN_ON(nchildren > 1);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001611 ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
1612 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001613 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001614 if (ret < 0)
1615 return ret;
1616 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001617 ncmax = nilfs_btree_nchildren_per_block(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001618 break;
1619 default:
1620 node = NULL;
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001621 return -EINVAL;
Koji Sato17c76b02009-04-06 19:01:24 -07001622 }
1623
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001624 nchildren = nilfs_btree_node_get_nchildren(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001625 if (nchildren < nitems)
1626 nitems = nchildren;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001627 dkeys = nilfs_btree_node_dkeys(node);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001628 dptrs = nilfs_btree_node_dptrs(node, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001629 for (i = 0; i < nitems; i++) {
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09001630 keys[i] = le64_to_cpu(dkeys[i]);
1631 ptrs[i] = le64_to_cpu(dptrs[i]);
Koji Sato17c76b02009-04-06 19:01:24 -07001632 }
1633
1634 if (bh != NULL)
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001635 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001636
1637 return nitems;
1638}
1639
1640static int
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001641nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
Koji Sato17c76b02009-04-06 19:01:24 -07001642 union nilfs_bmap_ptr_req *dreq,
1643 union nilfs_bmap_ptr_req *nreq,
1644 struct buffer_head **bhp,
1645 struct nilfs_bmap_stats *stats)
1646{
1647 struct buffer_head *bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001648 struct inode *dat = NULL;
Koji Sato17c76b02009-04-06 19:01:24 -07001649 int ret;
1650
Koji Sato17c76b02009-04-06 19:01:24 -07001651 stats->bs_nblocks = 0;
1652
1653 /* for data */
1654 /* cannot find near ptr */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001655 if (NILFS_BMAP_USE_VBN(btree)) {
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001656 dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001657 dat = nilfs_bmap_get_dat(btree);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001658 }
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001659
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001660 ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001661 if (ret < 0)
1662 return ret;
1663
1664 *bhp = NULL;
1665 stats->bs_nblocks++;
1666 if (nreq != NULL) {
1667 nreq->bpr_ptr = dreq->bpr_ptr + 1;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001668 ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001669 if (ret < 0)
1670 goto err_out_dreq;
1671
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09001672 ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001673 if (ret < 0)
1674 goto err_out_nreq;
1675
1676 *bhp = bh;
1677 stats->bs_nblocks++;
1678 }
1679
1680 /* success */
1681 return 0;
1682
1683 /* error */
1684 err_out_nreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001685 nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001686 err_out_dreq:
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001687 nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001688 stats->bs_nblocks = 0;
1689 return ret;
1690
1691}
1692
1693static void
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001694nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001695 __u64 key, __u64 ptr,
1696 const __u64 *keys, const __u64 *ptrs,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001697 int n,
Koji Sato17c76b02009-04-06 19:01:24 -07001698 union nilfs_bmap_ptr_req *dreq,
1699 union nilfs_bmap_ptr_req *nreq,
1700 struct buffer_head *bh)
1701{
Koji Sato17c76b02009-04-06 19:01:24 -07001702 struct nilfs_btree_node *node;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001703 struct inode *dat;
Koji Sato17c76b02009-04-06 19:01:24 -07001704 __u64 tmpptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001705 int ncblk;
Koji Sato17c76b02009-04-06 19:01:24 -07001706
1707 /* free resources */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001708 if (btree->b_ops->bop_clear != NULL)
1709 btree->b_ops->bop_clear(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001710
1711 /* ptr must be a pointer to a buffer head. */
1712 set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
1713
1714 /* convert and insert */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001715 dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
1716 nilfs_btree_init(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001717 if (nreq != NULL) {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001718 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
1719 nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001720
1721 /* create child node at level 1 */
Koji Sato17c76b02009-04-06 19:01:24 -07001722 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001723 ncblk = nilfs_btree_nchildren_per_block(btree);
1724 nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
1725 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
Koji Sato17c76b02009-04-06 19:01:24 -07001726 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001727 mark_buffer_dirty(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001728 if (!nilfs_bmap_dirty(btree))
1729 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001730
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09001731 brelse(bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001732
1733 /* create root node at level 2 */
1734 node = nilfs_btree_get_root(btree);
1735 tmpptr = nreq->bpr_ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001736 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
1737 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1738 &keys[0], &tmpptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001739 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001740 nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001741
1742 /* create root node at level 1 */
1743 node = nilfs_btree_get_root(btree);
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001744 nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
1745 NILFS_BTREE_ROOT_NCHILDREN_MAX,
1746 keys, ptrs);
1747 nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
1748 NILFS_BTREE_ROOT_NCHILDREN_MAX);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001749 if (!nilfs_bmap_dirty(btree))
1750 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001751 }
1752
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001753 if (NILFS_BMAP_USE_VBN(btree))
Ryusuke Konishidc935be2010-07-10 22:21:54 +09001754 nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001755}
1756
1757/**
1758 * nilfs_btree_convert_and_insert -
1759 * @bmap:
1760 * @key:
1761 * @ptr:
1762 * @keys:
1763 * @ptrs:
1764 * @n:
Koji Sato17c76b02009-04-06 19:01:24 -07001765 */
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001766int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001767 __u64 key, __u64 ptr,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001768 const __u64 *keys, const __u64 *ptrs, int n)
Koji Sato17c76b02009-04-06 19:01:24 -07001769{
1770 struct buffer_head *bh;
1771 union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
1772 struct nilfs_bmap_stats stats;
1773 int ret;
1774
1775 if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
1776 di = &dreq;
1777 ni = NULL;
1778 } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001779 1 << btree->b_inode->i_blkbits)) {
Koji Sato17c76b02009-04-06 19:01:24 -07001780 di = &dreq;
1781 ni = &nreq;
1782 } else {
1783 di = NULL;
1784 ni = NULL;
1785 BUG();
1786 }
1787
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001788 ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
Koji Sato17c76b02009-04-06 19:01:24 -07001789 &stats);
1790 if (ret < 0)
1791 return ret;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001792 nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
Ryusuke Konishi30333422009-05-24 00:09:44 +09001793 di, ni, bh);
Ryusuke Konishibe667372011-03-05 00:19:32 +09001794 nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
Koji Sato17c76b02009-04-06 19:01:24 -07001795 return 0;
1796}
1797
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001798static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001799 struct nilfs_btree_path *path,
1800 int level,
1801 struct buffer_head *bh)
1802{
1803 while ((++level < nilfs_btree_height(btree) - 1) &&
1804 !buffer_dirty(path[level].bp_bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09001805 mark_buffer_dirty(path[level].bp_bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001806
1807 return 0;
1808}
1809
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001810static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001811 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001812 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001813{
1814 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001815 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001816
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001817 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001818 path[level].bp_oldreq.bpr_ptr =
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001819 nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
1820 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001821 path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001822 ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
1823 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001824 if (ret < 0)
1825 return ret;
1826
1827 if (buffer_nilfs_node(path[level].bp_bh)) {
1828 path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
1829 path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
1830 path[level].bp_ctxt.bh = path[level].bp_bh;
1831 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001832 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001833 &path[level].bp_ctxt);
1834 if (ret < 0) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001835 nilfs_dat_abort_update(dat,
1836 &path[level].bp_oldreq.bpr_req,
1837 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001838 return ret;
1839 }
1840 }
1841
1842 return 0;
1843}
1844
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001845static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001846 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001847 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001848{
1849 struct nilfs_btree_node *parent;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001850 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001851
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001852 nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
1853 &path[level].bp_newreq.bpr_req,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001854 btree->b_ptr_type == NILFS_BMAP_PTR_VS);
Koji Sato17c76b02009-04-06 19:01:24 -07001855
1856 if (buffer_nilfs_node(path[level].bp_bh)) {
1857 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001858 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001859 &path[level].bp_ctxt);
1860 path[level].bp_bh = path[level].bp_ctxt.bh;
1861 }
1862 set_buffer_nilfs_volatile(path[level].bp_bh);
1863
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001864 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1865 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
1866 path[level].bp_newreq.bpr_ptr, ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07001867}
1868
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001869static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001870 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001871 int level, struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001872{
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001873 nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
1874 &path[level].bp_newreq.bpr_req);
Koji Sato17c76b02009-04-06 19:01:24 -07001875 if (buffer_nilfs_node(path[level].bp_bh))
1876 nilfs_btnode_abort_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001877 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07001878 &path[level].bp_ctxt);
1879}
1880
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001881static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001882 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001883 int minlevel, int *maxlevelp,
1884 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001885{
1886 int level, ret;
1887
1888 level = minlevel;
1889 if (!buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001890 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001891 if (ret < 0)
1892 return ret;
1893 }
1894 while ((++level < nilfs_btree_height(btree) - 1) &&
1895 !buffer_dirty(path[level].bp_bh)) {
1896
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001897 WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001898 ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001899 if (ret < 0)
1900 goto out;
1901 }
1902
1903 /* success */
Koji Sato17c76b02009-04-06 19:01:24 -07001904 *maxlevelp = level - 1;
1905 return 0;
1906
1907 /* error */
1908 out:
1909 while (--level > minlevel)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001910 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001911 if (!buffer_nilfs_volatile(path[level].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001912 nilfs_btree_abort_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001913 return ret;
1914}
1915
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001916static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001917 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001918 int minlevel, int maxlevel,
1919 struct buffer_head *bh,
1920 struct inode *dat)
Koji Sato17c76b02009-04-06 19:01:24 -07001921{
1922 int level;
1923
1924 if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001925 nilfs_btree_commit_update_v(btree, path, minlevel, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001926
1927 for (level = minlevel + 1; level <= maxlevel; level++)
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001928 nilfs_btree_commit_update_v(btree, path, level, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001929}
1930
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001931static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001932 struct nilfs_btree_path *path,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001933 int level, struct buffer_head *bh)
Koji Sato17c76b02009-04-06 19:01:24 -07001934{
Li Hong308f4412010-04-02 18:40:39 +08001935 int maxlevel = 0, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07001936 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001937 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07001938 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001939 int ncmax;
Koji Sato17c76b02009-04-06 19:01:24 -07001940
1941 get_bh(bh);
1942 path[level].bp_bh = bh;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001943 ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
1944 dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001945 if (ret < 0)
1946 goto out;
1947
1948 if (buffer_nilfs_volatile(path[level].bp_bh)) {
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09001949 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
1950 ptr = nilfs_btree_node_get_ptr(parent,
1951 path[level + 1].bp_index,
1952 ncmax);
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001953 ret = nilfs_dat_mark_dirty(dat, ptr);
Koji Sato17c76b02009-04-06 19:01:24 -07001954 if (ret < 0)
1955 goto out;
1956 }
1957
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09001958 nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
Koji Sato17c76b02009-04-06 19:01:24 -07001959
1960 out:
1961 brelse(path[level].bp_bh);
1962 path[level].bp_bh = NULL;
1963 return ret;
1964}
1965
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001966static int nilfs_btree_propagate(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07001967 struct buffer_head *bh)
1968{
Koji Sato17c76b02009-04-06 19:01:24 -07001969 struct nilfs_btree_path *path;
1970 struct nilfs_btree_node *node;
1971 __u64 key;
1972 int level, ret;
1973
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001974 WARN_ON(!buffer_dirty(bh));
Koji Sato17c76b02009-04-06 19:01:24 -07001975
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001976 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07001977 if (path == NULL)
1978 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07001979
1980 if (buffer_nilfs_node(bh)) {
1981 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09001982 key = nilfs_btree_node_get_key(node, 0);
1983 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07001984 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001985 key = nilfs_bmap_data_get_key(btree, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07001986 level = NILFS_BTREE_LEVEL_DATA;
1987 }
1988
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09001989 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07001990 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07001991 if (unlikely(ret == -ENOENT))
Koji Sato17c76b02009-04-06 19:01:24 -07001992 printk(KERN_CRIT "%s: key = %llu, level == %d\n",
1993 __func__, (unsigned long long)key, level);
Koji Sato17c76b02009-04-06 19:01:24 -07001994 goto out;
1995 }
1996
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09001997 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09001998 nilfs_btree_propagate_v(btree, path, level, bh) :
1999 nilfs_btree_propagate_p(btree, path, level, bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002000
2001 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002002 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002003
2004 return ret;
2005}
2006
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002007static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002008 struct buffer_head *bh)
2009{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002010 return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002011}
2012
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002013static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002014 struct list_head *lists,
2015 struct buffer_head *bh)
2016{
2017 struct list_head *head;
2018 struct buffer_head *cbh;
2019 struct nilfs_btree_node *node, *cnode;
2020 __u64 key, ckey;
2021 int level;
2022
2023 get_bh(bh);
2024 node = (struct nilfs_btree_node *)bh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002025 key = nilfs_btree_node_get_key(node, 0);
2026 level = nilfs_btree_node_get_level(node);
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002027 if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
2028 level >= NILFS_BTREE_LEVEL_MAX) {
2029 dump_stack();
2030 printk(KERN_WARNING
2031 "%s: invalid btree level: %d (key=%llu, ino=%lu, "
2032 "blocknr=%llu)\n",
2033 __func__, level, (unsigned long long)key,
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002034 NILFS_BMAP_I(btree)->vfs_inode.i_ino,
Ryusuke Konishicfa913a2010-07-07 17:19:54 +09002035 (unsigned long long)bh->b_blocknr);
2036 return;
2037 }
2038
Koji Sato17c76b02009-04-06 19:01:24 -07002039 list_for_each(head, &lists[level]) {
2040 cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
2041 cnode = (struct nilfs_btree_node *)cbh->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002042 ckey = nilfs_btree_node_get_key(cnode, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002043 if (key < ckey)
2044 break;
2045 }
2046 list_add_tail(&bh->b_assoc_buffers, head);
2047}
2048
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002049static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002050 struct list_head *listp)
2051{
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002052 struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
Koji Sato17c76b02009-04-06 19:01:24 -07002053 struct list_head lists[NILFS_BTREE_LEVEL_MAX];
2054 struct pagevec pvec;
2055 struct buffer_head *bh, *head;
2056 pgoff_t index = 0;
2057 int level, i;
2058
2059 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2060 level < NILFS_BTREE_LEVEL_MAX;
2061 level++)
2062 INIT_LIST_HEAD(&lists[level]);
2063
2064 pagevec_init(&pvec, 0);
2065
2066 while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
2067 PAGEVEC_SIZE)) {
2068 for (i = 0; i < pagevec_count(&pvec); i++) {
2069 bh = head = page_buffers(pvec.pages[i]);
2070 do {
2071 if (buffer_dirty(bh))
2072 nilfs_btree_add_dirty_buffer(btree,
2073 lists, bh);
2074 } while ((bh = bh->b_this_page) != head);
2075 }
2076 pagevec_release(&pvec);
2077 cond_resched();
2078 }
2079
2080 for (level = NILFS_BTREE_LEVEL_NODE_MIN;
2081 level < NILFS_BTREE_LEVEL_MAX;
2082 level++)
Ryusuke Konishi0935db72009-11-29 02:39:11 +09002083 list_splice_tail(&lists[level], listp);
Koji Sato17c76b02009-04-06 19:01:24 -07002084}
2085
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002086static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002087 struct nilfs_btree_path *path,
2088 int level,
2089 struct buffer_head **bh,
2090 sector_t blocknr,
2091 union nilfs_binfo *binfo)
2092{
2093 struct nilfs_btree_node *parent;
2094 __u64 key;
2095 __u64 ptr;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002096 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002097
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002098 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2099 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2100 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002101 if (buffer_nilfs_node(*bh)) {
2102 path[level].bp_ctxt.oldkey = ptr;
2103 path[level].bp_ctxt.newkey = blocknr;
2104 path[level].bp_ctxt.bh = *bh;
2105 ret = nilfs_btnode_prepare_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002106 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002107 &path[level].bp_ctxt);
2108 if (ret < 0)
2109 return ret;
2110 nilfs_btnode_commit_change_key(
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002111 &NILFS_BMAP_I(btree)->i_btnode_cache,
Koji Sato17c76b02009-04-06 19:01:24 -07002112 &path[level].bp_ctxt);
2113 *bh = path[level].bp_ctxt.bh;
2114 }
2115
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002116 nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
2117 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002118
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002119 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002120 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002121 binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002122 binfo->bi_dat.bi_level = level;
2123
2124 return 0;
2125}
2126
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002127static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002128 struct nilfs_btree_path *path,
2129 int level,
2130 struct buffer_head **bh,
2131 sector_t blocknr,
2132 union nilfs_binfo *binfo)
2133{
2134 struct nilfs_btree_node *parent;
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002135 struct inode *dat = nilfs_bmap_get_dat(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002136 __u64 key;
2137 __u64 ptr;
2138 union nilfs_bmap_ptr_req req;
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002139 int ncmax, ret;
Koji Sato17c76b02009-04-06 19:01:24 -07002140
Ryusuke Konishi9b7b2652010-07-13 23:33:53 +09002141 parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
2142 ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
2143 ncmax);
Koji Sato17c76b02009-04-06 19:01:24 -07002144 req.bpr_ptr = ptr;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002145 ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
2146 if (ret < 0)
Koji Sato17c76b02009-04-06 19:01:24 -07002147 return ret;
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002148 nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002149
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002150 key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
Koji Sato17c76b02009-04-06 19:01:24 -07002151 /* on-disk format */
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002152 binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
2153 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002154
2155 return 0;
2156}
2157
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002158static int nilfs_btree_assign(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_path *path;
2164 struct nilfs_btree_node *node;
2165 __u64 key;
2166 int level, ret;
2167
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002168 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002169 if (path == NULL)
2170 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002171
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);
2175 level = nilfs_btree_node_get_level(node);
Koji Sato17c76b02009-04-06 19:01:24 -07002176 } else {
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002177 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002178 level = NILFS_BTREE_LEVEL_DATA;
2179 }
2180
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002181 ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002182 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002183 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002184 goto out;
2185 }
2186
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002187 ret = NILFS_BMAP_USE_VBN(btree) ?
Ryusuke Konishi7cde31d2009-05-24 18:07:59 +09002188 nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
2189 nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
Koji Sato17c76b02009-04-06 19:01:24 -07002190
2191 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002192 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002193
2194 return ret;
2195}
2196
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002197static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
Koji Sato17c76b02009-04-06 19:01:24 -07002198 struct buffer_head **bh,
2199 sector_t blocknr,
2200 union nilfs_binfo *binfo)
2201{
Koji Sato17c76b02009-04-06 19:01:24 -07002202 struct nilfs_btree_node *node;
2203 __u64 key;
2204 int ret;
2205
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002206 ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
Ryusuke Konishi2e0c2c72009-08-15 15:34:33 +09002207 blocknr);
Koji Sato17c76b02009-04-06 19:01:24 -07002208 if (ret < 0)
2209 return ret;
2210
2211 if (buffer_nilfs_node(*bh)) {
2212 node = (struct nilfs_btree_node *)(*bh)->b_data;
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002213 key = nilfs_btree_node_get_key(node, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002214 } else
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002215 key = nilfs_bmap_data_get_key(btree, *bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002216
2217 /* on-disk format */
2218 binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
Ryusuke Konishi25b8d7d2010-07-10 16:50:41 +09002219 binfo->bi_v.bi_blkoff = cpu_to_le64(key);
Koji Sato17c76b02009-04-06 19:01:24 -07002220
2221 return 0;
2222}
2223
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002224static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
Koji Sato17c76b02009-04-06 19:01:24 -07002225{
2226 struct buffer_head *bh;
Koji Sato17c76b02009-04-06 19:01:24 -07002227 struct nilfs_btree_path *path;
2228 __u64 ptr;
2229 int ret;
2230
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002231 path = nilfs_btree_alloc_path();
Koji Sato17c76b02009-04-06 19:01:24 -07002232 if (path == NULL)
2233 return -ENOMEM;
Koji Sato17c76b02009-04-06 19:01:24 -07002234
Ryusuke Konishi03bdb5a2010-07-18 10:42:26 +09002235 ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
Koji Sato17c76b02009-04-06 19:01:24 -07002236 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002237 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002238 goto out;
2239 }
Ryusuke Konishif198dbb2009-05-22 01:07:13 +09002240 ret = nilfs_btree_get_block(btree, ptr, &bh);
Koji Sato17c76b02009-04-06 19:01:24 -07002241 if (ret < 0) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -07002242 WARN_ON(ret == -ENOENT);
Koji Sato17c76b02009-04-06 19:01:24 -07002243 goto out;
2244 }
2245
2246 if (!buffer_dirty(bh))
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +09002247 mark_buffer_dirty(bh);
Ryusuke Konishi087d01b2009-05-22 00:33:13 +09002248 brelse(bh);
Ryusuke Konishie7c274f2010-07-10 19:09:49 +09002249 if (!nilfs_bmap_dirty(btree))
2250 nilfs_bmap_set_dirty(btree);
Koji Sato17c76b02009-04-06 19:01:24 -07002251
2252 out:
Ryusuke Konishi6d28f7e2009-08-15 01:14:10 +09002253 nilfs_btree_free_path(path);
Koji Sato17c76b02009-04-06 19:01:24 -07002254 return ret;
2255}
2256
2257static const struct nilfs_bmap_operations nilfs_btree_ops = {
2258 .bop_lookup = nilfs_btree_lookup,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002259 .bop_lookup_contig = nilfs_btree_lookup_contig,
Koji Sato17c76b02009-04-06 19:01:24 -07002260 .bop_insert = nilfs_btree_insert,
2261 .bop_delete = nilfs_btree_delete,
2262 .bop_clear = NULL,
2263
2264 .bop_propagate = nilfs_btree_propagate,
2265
2266 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2267
2268 .bop_assign = nilfs_btree_assign,
2269 .bop_mark = nilfs_btree_mark,
2270
2271 .bop_last_key = nilfs_btree_last_key,
2272 .bop_check_insert = NULL,
2273 .bop_check_delete = nilfs_btree_check_delete,
2274 .bop_gather_data = nilfs_btree_gather_data,
2275};
2276
2277static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
2278 .bop_lookup = NULL,
Ryusuke Konishic3a7abf2009-05-25 02:47:14 +09002279 .bop_lookup_contig = NULL,
Koji Sato17c76b02009-04-06 19:01:24 -07002280 .bop_insert = NULL,
2281 .bop_delete = NULL,
2282 .bop_clear = NULL,
2283
2284 .bop_propagate = nilfs_btree_propagate_gc,
2285
2286 .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
2287
2288 .bop_assign = nilfs_btree_assign_gc,
2289 .bop_mark = NULL,
2290
2291 .bop_last_key = NULL,
2292 .bop_check_insert = NULL,
2293 .bop_check_delete = NULL,
2294 .bop_gather_data = NULL,
2295};
2296
Ryusuke Konishi30333422009-05-24 00:09:44 +09002297int nilfs_btree_init(struct nilfs_bmap *bmap)
Koji Sato17c76b02009-04-06 19:01:24 -07002298{
Koji Sato17c76b02009-04-06 19:01:24 -07002299 bmap->b_ops = &nilfs_btree_ops;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002300 bmap->b_nchildren_per_block =
2301 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002302 return 0;
2303}
2304
2305void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
2306{
Koji Sato17c76b02009-04-06 19:01:24 -07002307 bmap->b_ops = &nilfs_btree_ops_gc;
Ryusuke Konishi5ad2686e92010-07-13 23:33:54 +09002308 bmap->b_nchildren_per_block =
2309 NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
Koji Sato17c76b02009-04-06 19:01:24 -07002310}