btree.c 58.0 KB
Newer Older
K
Koji Sato 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * btree.c - NILFS B-tree.
 *
 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Written by Koji Sato <koji@osrg.net>.
 */

#include <linux/slab.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/pagevec.h>
#include "nilfs.h"
#include "page.h"
#include "btnode.h"
#include "btree.h"
#include "alloc.h"
32
#include "dat.h"
K
Koji Sato 已提交
33

34
static struct nilfs_btree_path *nilfs_btree_alloc_path(void)
K
Koji Sato 已提交
35
{
36 37
	struct nilfs_btree_path *path;
	int level = NILFS_BTREE_LEVEL_DATA;
K
Koji Sato 已提交
38

39 40 41
	path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
	if (path == NULL)
		goto out;
K
Koji Sato 已提交
42

43
	for (; level < NILFS_BTREE_LEVEL_MAX; level++) {
K
Koji Sato 已提交
44 45 46 47 48 49 50
		path[level].bp_bh = NULL;
		path[level].bp_sib_bh = NULL;
		path[level].bp_index = 0;
		path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
		path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
		path[level].bp_op = NULL;
	}
51 52 53 54 55

out:
	return path;
}

56
static void nilfs_btree_free_path(struct nilfs_btree_path *path)
57
{
58
	int level = NILFS_BTREE_LEVEL_DATA;
K
Koji Sato 已提交
59

60
	for (; level < NILFS_BTREE_LEVEL_MAX; level++)
61
		brelse(path[level].bp_bh);
62 63

	kmem_cache_free(nilfs_btree_path_cache, path);
K
Koji Sato 已提交
64 65 66 67 68
}

/*
 * B-tree node operations
 */
69
static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
70 71
				 struct buffer_head **bhp)
{
72
	struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
73
	struct buffer_head *bh;
74 75 76 77 78 79
	int err;

	err = nilfs_btnode_submit_block(btnc, ptr, 0, bhp);
	if (err)
		return err == -EEXIST ? 0 : err;

80 81 82 83
	bh = *bhp;
	wait_on_buffer(bh);
	if (!buffer_uptodate(bh)) {
		brelse(bh);
84 85
		return -EIO;
	}
86 87 88 89 90
	if (nilfs_btree_broken_node_block(bh)) {
		clear_buffer_uptodate(bh);
		brelse(bh);
		return -EINVAL;
	}
91
	return 0;
92 93
}

94
static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
95 96
				     __u64 ptr, struct buffer_head **bhp)
{
97
	struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
98
	struct buffer_head *bh;
99

100 101 102 103 104 105 106
	bh = nilfs_btnode_create_block(btnc, ptr);
	if (!bh)
		return -ENOMEM;

	set_buffer_nilfs_volatile(bh);
	*bhp = bh;
	return 0;
107
}
K
Koji Sato 已提交
108 109

static inline int
110
nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
111 112 113 114 115
{
	return node->bn_flags;
}

static inline void
116
nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
K
Koji Sato 已提交
117 118 119 120
{
	node->bn_flags = flags;
}

121
static inline int nilfs_btree_node_root(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
122
{
123
	return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
K
Koji Sato 已提交
124 125 126
}

static inline int
127
nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
128 129 130 131 132
{
	return node->bn_level;
}

static inline void
133
nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
K
Koji Sato 已提交
134 135 136 137 138
{
	node->bn_level = level;
}

static inline int
139
nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
140 141 142 143 144
{
	return le16_to_cpu(node->bn_nchildren);
}

static inline void
145
nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
K
Koji Sato 已提交
146 147 148 149
{
	node->bn_nchildren = cpu_to_le16(nchildren);
}

150
static inline int nilfs_btree_node_size(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
151
{
152
	return 1 << btree->b_inode->i_blkbits;
K
Koji Sato 已提交
153 154
}

155
static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
156
{
157
	return NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree));
K
Koji Sato 已提交
158 159 160
}

static inline __le64 *
161
nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
162 163
{
	return (__le64 *)((char *)(node + 1) +
164
			  (nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
165 166 167 168
			   0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
}

static inline __le64 *
169
nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
K
Koji Sato 已提交
170
{
171
	return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
K
Koji Sato 已提交
172 173 174
}

static inline __u64
175
nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
K
Koji Sato 已提交
176
{
177
	return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
K
Koji Sato 已提交
178 179 180
}

static inline void
181
nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
K
Koji Sato 已提交
182
{
183
	*(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
K
Koji Sato 已提交
184 185 186
}

static inline __u64
187 188
nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
			 int ncmax)
K
Koji Sato 已提交
189
{
190
	return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
K
Koji Sato 已提交
191 192 193
}

static inline void
194 195
nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
			 int ncmax)
K
Koji Sato 已提交
196
{
197
	*(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
K
Koji Sato 已提交
198 199
}

200 201
static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
				  int level, int nchildren, int ncmax,
K
Koji Sato 已提交
202 203 204 205 206 207
				  const __u64 *keys, const __u64 *ptrs)
{
	__le64 *dkeys;
	__le64 *dptrs;
	int i;

208 209 210
	nilfs_btree_node_set_flags(node, flags);
	nilfs_btree_node_set_level(node, level);
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
211

212
	dkeys = nilfs_btree_node_dkeys(node);
213
	dptrs = nilfs_btree_node_dptrs(node, ncmax);
K
Koji Sato 已提交
214
	for (i = 0; i < nchildren; i++) {
215 216
		dkeys[i] = cpu_to_le64(keys[i]);
		dptrs[i] = cpu_to_le64(ptrs[i]);
K
Koji Sato 已提交
217 218 219 220
	}
}

/* Assume the buffer heads corresponding to left and right are locked. */
221
static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
K
Koji Sato 已提交
222
				       struct nilfs_btree_node *right,
223
				       int n, int lncmax, int rncmax)
K
Koji Sato 已提交
224 225 226 227 228
{
	__le64 *ldkeys, *rdkeys;
	__le64 *ldptrs, *rdptrs;
	int lnchildren, rnchildren;

229
	ldkeys = nilfs_btree_node_dkeys(left);
230
	ldptrs = nilfs_btree_node_dptrs(left, lncmax);
231
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
232

233
	rdkeys = nilfs_btree_node_dkeys(right);
234
	rdptrs = nilfs_btree_node_dptrs(right, rncmax);
235
	rnchildren = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
236 237 238 239 240 241 242 243

	memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
	memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs));
	memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys));
	memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs));

	lnchildren += n;
	rnchildren -= n;
244 245
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
246 247 248
}

/* Assume that the buffer heads corresponding to left and right are locked. */
249
static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
K
Koji Sato 已提交
250
					struct nilfs_btree_node *right,
251
					int n, int lncmax, int rncmax)
K
Koji Sato 已提交
252 253 254 255 256
{
	__le64 *ldkeys, *rdkeys;
	__le64 *ldptrs, *rdptrs;
	int lnchildren, rnchildren;

257
	ldkeys = nilfs_btree_node_dkeys(left);
258
	ldptrs = nilfs_btree_node_dptrs(left, lncmax);
259
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
260

261
	rdkeys = nilfs_btree_node_dkeys(right);
262
	rdptrs = nilfs_btree_node_dptrs(right, rncmax);
263
	rnchildren = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
264 265 266 267 268 269 270 271

	memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
	memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs));
	memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys));
	memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs));

	lnchildren -= n;
	rnchildren += n;
272 273
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
274 275 276
}

/* Assume that the buffer head corresponding to node is locked. */
277 278
static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
				    __u64 key, __u64 ptr, int ncmax)
K
Koji Sato 已提交
279 280 281 282 283
{
	__le64 *dkeys;
	__le64 *dptrs;
	int nchildren;

284
	dkeys = nilfs_btree_node_dkeys(node);
285
	dptrs = nilfs_btree_node_dptrs(node, ncmax);
286
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
287 288 289 290 291 292
	if (index < nchildren) {
		memmove(dkeys + index + 1, dkeys + index,
			(nchildren - index) * sizeof(*dkeys));
		memmove(dptrs + index + 1, dptrs + index,
			(nchildren - index) * sizeof(*dptrs));
	}
293 294
	dkeys[index] = cpu_to_le64(key);
	dptrs[index] = cpu_to_le64(ptr);
K
Koji Sato 已提交
295
	nchildren++;
296
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
297 298 299
}

/* Assume that the buffer head corresponding to node is locked. */
300 301
static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
				    __u64 *keyp, __u64 *ptrp, int ncmax)
K
Koji Sato 已提交
302 303 304 305 306 307 308
{
	__u64 key;
	__u64 ptr;
	__le64 *dkeys;
	__le64 *dptrs;
	int nchildren;

309
	dkeys = nilfs_btree_node_dkeys(node);
310
	dptrs = nilfs_btree_node_dptrs(node, ncmax);
311 312
	key = le64_to_cpu(dkeys[index]);
	ptr = le64_to_cpu(dptrs[index]);
313
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
314 315 316 317 318 319 320 321 322 323 324 325
	if (keyp != NULL)
		*keyp = key;
	if (ptrp != NULL)
		*ptrp = ptr;

	if (index < nchildren - 1) {
		memmove(dkeys + index, dkeys + index + 1,
			(nchildren - index - 1) * sizeof(*dkeys));
		memmove(dptrs + index, dptrs + index + 1,
			(nchildren - index - 1) * sizeof(*dptrs));
	}
	nchildren--;
326
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
327 328
}

329
static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
K
Koji Sato 已提交
330 331 332 333 334 335 336
				   __u64 key, int *indexp)
{
	__u64 nkey;
	int index, low, high, s;

	/* binary search */
	low = 0;
337
	high = nilfs_btree_node_get_nchildren(node) - 1;
K
Koji Sato 已提交
338 339 340 341
	index = 0;
	s = 0;
	while (low <= high) {
		index = (low + high) / 2;
342
		nkey = nilfs_btree_node_get_key(node, index);
K
Koji Sato 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355
		if (nkey == key) {
			s = 0;
			goto out;
		} else if (nkey < key) {
			low = index + 1;
			s = -1;
		} else {
			high = index - 1;
			s = 1;
		}
	}

	/* adjust index */
356 357
	if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
		if (s > 0 && index > 0)
K
Koji Sato 已提交
358 359 360 361 362 363 364 365 366 367
			index--;
	} else if (s < 0)
		index++;

 out:
	*indexp = index;

	return s == 0;
}

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/**
 * nilfs_btree_node_broken - verify consistency of btree node
 * @node: btree node block to be examined
 * @size: node size (in bytes)
 * @blocknr: block number
 *
 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
 */
static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
				   size_t size, sector_t blocknr)
{
	int level, flags, nchildren;
	int ret = 0;

	level = nilfs_btree_node_get_level(node);
	flags = nilfs_btree_node_get_flags(node);
	nchildren = nilfs_btree_node_get_nchildren(node);

	if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
		     level >= NILFS_BTREE_LEVEL_MAX ||
		     (flags & NILFS_BTREE_NODE_ROOT) ||
		     nchildren < 0 ||
		     nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
		printk(KERN_CRIT "NILFS: bad btree node (blocknr=%llu): "
		       "level = %d, flags = 0x%x, nchildren = %d\n",
		       (unsigned long long)blocknr, level, flags, nchildren);
		ret = 1;
	}
	return ret;
}

int nilfs_btree_broken_node_block(struct buffer_head *bh)
{
	return nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
				       bh->b_size, bh->b_blocknr);
}

K
Koji Sato 已提交
405
static inline struct nilfs_btree_node *
406
nilfs_btree_get_root(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
407
{
408
	return (struct nilfs_btree_node *)btree->b_u.u_data;
K
Koji Sato 已提交
409 410 411
}

static inline struct nilfs_btree_node *
412
nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
K
Koji Sato 已提交
413 414 415 416 417
{
	return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
}

static inline struct nilfs_btree_node *
418
nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
K
Koji Sato 已提交
419 420 421 422
{
	return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
}

423
static inline int nilfs_btree_height(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
424
{
425
	return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
K
Koji Sato 已提交
426 427
}

428
static struct nilfs_btree_node *
429
nilfs_btree_get_node(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
430
		     const struct nilfs_btree_path *path,
431
		     int level, int *ncmaxp)
K
Koji Sato 已提交
432
{
433 434 435 436 437 438 439 440 441 442
	struct nilfs_btree_node *node;

	if (level == nilfs_btree_height(btree) - 1) {
		node = nilfs_btree_get_root(btree);
		*ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
	} else {
		node = nilfs_btree_get_nonroot_node(path, level);
		*ncmaxp = nilfs_btree_nchildren_per_block(btree);
	}
	return node;
K
Koji Sato 已提交
443 444
}

445 446 447 448 449 450 451 452 453 454 455 456
static inline int
nilfs_btree_bad_node(struct nilfs_btree_node *node, int level)
{
	if (unlikely(nilfs_btree_node_get_level(node) != level)) {
		dump_stack();
		printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n",
		       nilfs_btree_node_get_level(node), level);
		return 1;
	}
	return 0;
}

457
static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
458 459 460 461 462
				 struct nilfs_btree_path *path,
				 __u64 key, __u64 *ptrp, int minlevel)
{
	struct nilfs_btree_node *node;
	__u64 ptr;
463
	int level, index, found, ncmax, ret;
K
Koji Sato 已提交
464 465

	node = nilfs_btree_get_root(btree);
466 467
	level = nilfs_btree_node_get_level(node);
	if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
K
Koji Sato 已提交
468 469
		return -ENOENT;

470
	found = nilfs_btree_node_lookup(node, key, &index);
471 472
	ptr = nilfs_btree_node_get_ptr(node, index,
				       NILFS_BTREE_ROOT_NCHILDREN_MAX);
K
Koji Sato 已提交
473 474 475
	path[level].bp_bh = NULL;
	path[level].bp_index = index;

476
	ncmax = nilfs_btree_nchildren_per_block(btree);
477

K
Koji Sato 已提交
478
	for (level--; level >= minlevel; level--) {
479
		ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
K
Koji Sato 已提交
480 481
		if (ret < 0)
			return ret;
482
		node = nilfs_btree_get_nonroot_node(path, level);
483 484
		if (nilfs_btree_bad_node(node, level))
			return -EINVAL;
K
Koji Sato 已提交
485
		if (!found)
486
			found = nilfs_btree_node_lookup(node, key, &index);
K
Koji Sato 已提交
487 488
		else
			index = 0;
489
		if (index < ncmax) {
490
			ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
491
		} else {
492
			WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
K
Koji Sato 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506
			/* insert */
			ptr = NILFS_BMAP_INVALID_PTR;
		}
		path[level].bp_index = index;
	}
	if (!found)
		return -ENOENT;

	if (ptrp != NULL)
		*ptrp = ptr;

	return 0;
}

507
static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
508 509 510 511 512
				      struct nilfs_btree_path *path,
				      __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node;
	__u64 ptr;
513
	int index, level, ncmax, ret;
K
Koji Sato 已提交
514 515

	node = nilfs_btree_get_root(btree);
516
	index = nilfs_btree_node_get_nchildren(node) - 1;
K
Koji Sato 已提交
517 518
	if (index < 0)
		return -ENOENT;
519
	level = nilfs_btree_node_get_level(node);
520 521
	ptr = nilfs_btree_node_get_ptr(node, index,
				       NILFS_BTREE_ROOT_NCHILDREN_MAX);
K
Koji Sato 已提交
522 523
	path[level].bp_bh = NULL;
	path[level].bp_index = index;
524
	ncmax = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
525 526

	for (level--; level > 0; level--) {
527
		ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
K
Koji Sato 已提交
528 529
		if (ret < 0)
			return ret;
530
		node = nilfs_btree_get_nonroot_node(path, level);
531 532
		if (nilfs_btree_bad_node(node, level))
			return -EINVAL;
533
		index = nilfs_btree_node_get_nchildren(node) - 1;
534
		ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
K
Koji Sato 已提交
535 536 537 538
		path[level].bp_index = index;
	}

	if (keyp != NULL)
539
		*keyp = nilfs_btree_node_get_key(node, index);
K
Koji Sato 已提交
540 541 542 543 544 545
	if (ptrp != NULL)
		*ptrp = ptr;

	return 0;
}

546
static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
547 548 549 550 551
			      __u64 key, int level, __u64 *ptrp)
{
	struct nilfs_btree_path *path;
	int ret;

552
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
553 554 555
	if (path == NULL)
		return -ENOMEM;

556
	ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level);
K
Koji Sato 已提交
557

558
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
559 560 561 562

	return ret;
}

563
static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
564 565 566 567 568 569 570 571
				     __u64 key, __u64 *ptrp, unsigned maxblocks)
{
	struct nilfs_btree_path *path;
	struct nilfs_btree_node *node;
	struct inode *dat = NULL;
	__u64 ptr, ptr2;
	sector_t blocknr;
	int level = NILFS_BTREE_LEVEL_NODE_MIN;
572
	int ret, cnt, index, maxlevel, ncmax;
573

574
	path = nilfs_btree_alloc_path();
575 576
	if (path == NULL)
		return -ENOMEM;
577

578 579 580 581
	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level);
	if (ret < 0)
		goto out;

582 583
	if (NILFS_BMAP_USE_VBN(btree)) {
		dat = nilfs_bmap_get_dat(btree);
584 585 586 587 588 589 590 591 592 593
		ret = nilfs_dat_translate(dat, ptr, &blocknr);
		if (ret < 0)
			goto out;
		ptr = blocknr;
	}
	cnt = 1;
	if (cnt == maxblocks)
		goto end;

	maxlevel = nilfs_btree_height(btree) - 1;
594
	node = nilfs_btree_get_node(btree, path, level, &ncmax);
595 596
	index = path[level].bp_index + 1;
	for (;;) {
597 598
		while (index < nilfs_btree_node_get_nchildren(node)) {
			if (nilfs_btree_node_get_key(node, index) !=
599 600
			    key + cnt)
				goto end;
601
			ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
			if (dat) {
				ret = nilfs_dat_translate(dat, ptr2, &blocknr);
				if (ret < 0)
					goto out;
				ptr2 = blocknr;
			}
			if (ptr2 != ptr + cnt || ++cnt == maxblocks)
				goto end;
			index++;
			continue;
		}
		if (level == maxlevel)
			break;

		/* look-up right sibling node */
617
		node = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
618
		index = path[level + 1].bp_index + 1;
619 620
		if (index >= nilfs_btree_node_get_nchildren(node) ||
		    nilfs_btree_node_get_key(node, index) != key + cnt)
621
			break;
622
		ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
623 624 625 626 627 628 629
		path[level + 1].bp_index = index;

		brelse(path[level].bp_bh);
		path[level].bp_bh = NULL;
		ret = nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh);
		if (ret < 0)
			goto out;
630
		node = nilfs_btree_get_nonroot_node(path, level);
631
		ncmax = nilfs_btree_nchildren_per_block(btree);
632 633 634 635 636 637 638
		index = 0;
		path[level].bp_index = index;
	}
 end:
	*ptrp = ptr;
	ret = cnt;
 out:
639
	nilfs_btree_free_path(path);
640 641 642
	return ret;
}

643
static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
K
Koji Sato 已提交
644 645 646 647 648 649
				    struct nilfs_btree_path *path,
				    int level, __u64 key)
{
	if (level < nilfs_btree_height(btree) - 1) {
		do {
			nilfs_btree_node_set_key(
650
				nilfs_btree_get_nonroot_node(path, level),
K
Koji Sato 已提交
651 652 653 654 655 656 657 658 659
				path[level].bp_index, key);
			if (!buffer_dirty(path[level].bp_bh))
				nilfs_btnode_mark_dirty(path[level].bp_bh);
		} while ((path[level].bp_index == 0) &&
			 (++level < nilfs_btree_height(btree) - 1));
	}

	/* root */
	if (level == nilfs_btree_height(btree) - 1) {
660
		nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
K
Koji Sato 已提交
661 662 663 664
					 path[level].bp_index, key);
	}
}

665
static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
666 667 668 669
				  struct nilfs_btree_path *path,
				  int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node;
670
	int ncblk;
K
Koji Sato 已提交
671 672

	if (level < nilfs_btree_height(btree) - 1) {
673
		node = nilfs_btree_get_nonroot_node(path, level);
674 675 676
		ncblk = nilfs_btree_nchildren_per_block(btree);
		nilfs_btree_node_insert(node, path[level].bp_index,
					*keyp, *ptrp, ncblk);
K
Koji Sato 已提交
677 678 679 680 681
		if (!buffer_dirty(path[level].bp_bh))
			nilfs_btnode_mark_dirty(path[level].bp_bh);

		if (path[level].bp_index == 0)
			nilfs_btree_promote_key(btree, path, level + 1,
682 683
						nilfs_btree_node_get_key(node,
									 0));
K
Koji Sato 已提交
684 685
	} else {
		node = nilfs_btree_get_root(btree);
686 687 688
		nilfs_btree_node_insert(node, path[level].bp_index,
					*keyp, *ptrp,
					NILFS_BTREE_ROOT_NCHILDREN_MAX);
K
Koji Sato 已提交
689 690 691
	}
}

692
static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
K
Koji Sato 已提交
693 694 695 696
				   struct nilfs_btree_path *path,
				   int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *left;
697
	int nchildren, lnchildren, n, move, ncblk;
K
Koji Sato 已提交
698

699 700 701 702
	node = nilfs_btree_get_nonroot_node(path, level);
	left = nilfs_btree_get_sib_node(path, level);
	nchildren = nilfs_btree_node_get_nchildren(node);
	lnchildren = nilfs_btree_node_get_nchildren(left);
703
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
704 705 706 707 708 709 710 711 712
	move = 0;

	n = (nchildren + lnchildren + 1) / 2 - lnchildren;
	if (n > path[level].bp_index) {
		/* move insert point */
		n--;
		move = 1;
	}

713
	nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
K
Koji Sato 已提交
714 715 716 717 718 719 720

	if (!buffer_dirty(path[level].bp_bh))
		nilfs_btnode_mark_dirty(path[level].bp_bh);
	if (!buffer_dirty(path[level].bp_sib_bh))
		nilfs_btnode_mark_dirty(path[level].bp_sib_bh);

	nilfs_btree_promote_key(btree, path, level + 1,
721
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
722 723

	if (move) {
724
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
725 726 727 728 729
		path[level].bp_bh = path[level].bp_sib_bh;
		path[level].bp_sib_bh = NULL;
		path[level].bp_index += lnchildren;
		path[level + 1].bp_index--;
	} else {
730
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
731 732 733 734 735 736 737
		path[level].bp_sib_bh = NULL;
		path[level].bp_index -= n;
	}

	nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
}

738
static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
K
Koji Sato 已提交
739 740 741 742
				    struct nilfs_btree_path *path,
				    int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
743
	int nchildren, rnchildren, n, move, ncblk;
K
Koji Sato 已提交
744

745 746 747 748
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
	nchildren = nilfs_btree_node_get_nchildren(node);
	rnchildren = nilfs_btree_node_get_nchildren(right);
749
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
750 751 752 753 754 755 756 757 758
	move = 0;

	n = (nchildren + rnchildren + 1) / 2 - rnchildren;
	if (n > nchildren - path[level].bp_index) {
		/* move insert point */
		n--;
		move = 1;
	}

759
	nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
760 761 762 763 764 765 766 767

	if (!buffer_dirty(path[level].bp_bh))
		nilfs_btnode_mark_dirty(path[level].bp_bh);
	if (!buffer_dirty(path[level].bp_sib_bh))
		nilfs_btnode_mark_dirty(path[level].bp_sib_bh);

	path[level + 1].bp_index++;
	nilfs_btree_promote_key(btree, path, level + 1,
768
				nilfs_btree_node_get_key(right, 0));
K
Koji Sato 已提交
769 770 771
	path[level + 1].bp_index--;

	if (move) {
772
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
773 774
		path[level].bp_bh = path[level].bp_sib_bh;
		path[level].bp_sib_bh = NULL;
775
		path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
776 777
		path[level + 1].bp_index++;
	} else {
778
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
779 780 781 782 783 784
		path[level].bp_sib_bh = NULL;
	}

	nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
}

785
static void nilfs_btree_split(struct nilfs_bmap *btree,
K
Koji Sato 已提交
786 787 788 789 790 791
			      struct nilfs_btree_path *path,
			      int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
	__u64 newkey;
	__u64 newptr;
792
	int nchildren, n, move, ncblk;
K
Koji Sato 已提交
793

794 795 796
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
	nchildren = nilfs_btree_node_get_nchildren(node);
797
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
798 799 800 801 802 803 804 805
	move = 0;

	n = (nchildren + 1) / 2;
	if (n > nchildren - path[level].bp_index) {
		n--;
		move = 1;
	}

806
	nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
807 808 809 810 811 812

	if (!buffer_dirty(path[level].bp_bh))
		nilfs_btnode_mark_dirty(path[level].bp_bh);
	if (!buffer_dirty(path[level].bp_sib_bh))
		nilfs_btnode_mark_dirty(path[level].bp_sib_bh);

813
	newkey = nilfs_btree_node_get_key(right, 0);
K
Koji Sato 已提交
814 815 816
	newptr = path[level].bp_newreq.bpr_ptr;

	if (move) {
817
		path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
818 819
		nilfs_btree_node_insert(right, path[level].bp_index,
					*keyp, *ptrp, ncblk);
K
Koji Sato 已提交
820

821
		*keyp = nilfs_btree_node_get_key(right, 0);
K
Koji Sato 已提交
822 823
		*ptrp = path[level].bp_newreq.bpr_ptr;

824
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
825 826 827 828 829
		path[level].bp_bh = path[level].bp_sib_bh;
		path[level].bp_sib_bh = NULL;
	} else {
		nilfs_btree_do_insert(btree, path, level, keyp, ptrp);

830
		*keyp = nilfs_btree_node_get_key(right, 0);
K
Koji Sato 已提交
831 832
		*ptrp = path[level].bp_newreq.bpr_ptr;

833
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
834 835 836 837 838 839
		path[level].bp_sib_bh = NULL;
	}

	path[level + 1].bp_index++;
}

840
static void nilfs_btree_grow(struct nilfs_bmap *btree,
K
Koji Sato 已提交
841 842 843 844
			     struct nilfs_btree_path *path,
			     int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *root, *child;
845
	int n, ncblk;
K
Koji Sato 已提交
846 847

	root = nilfs_btree_get_root(btree);
848
	child = nilfs_btree_get_sib_node(path, level);
849
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
850

851
	n = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
852

853 854
	nilfs_btree_node_move_right(root, child, n,
				    NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
855
	nilfs_btree_node_set_level(root, level + 1);
K
Koji Sato 已提交
856 857 858 859 860 861 862 863 864

	if (!buffer_dirty(path[level].bp_sib_bh))
		nilfs_btnode_mark_dirty(path[level].bp_sib_bh);

	path[level].bp_bh = path[level].bp_sib_bh;
	path[level].bp_sib_bh = NULL;

	nilfs_btree_do_insert(btree, path, level, keyp, ptrp);

865
	*keyp = nilfs_btree_node_get_key(child, 0);
K
Koji Sato 已提交
866 867 868
	*ptrp = path[level].bp_newreq.bpr_ptr;
}

869
static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
870 871 872
				   const struct nilfs_btree_path *path)
{
	struct nilfs_btree_node *node;
873
	int level, ncmax;
K
Koji Sato 已提交
874 875 876 877 878 879 880

	if (path == NULL)
		return NILFS_BMAP_INVALID_PTR;

	/* left sibling */
	level = NILFS_BTREE_LEVEL_NODE_MIN;
	if (path[level].bp_index > 0) {
881 882 883 884
		node = nilfs_btree_get_node(btree, path, level, &ncmax);
		return nilfs_btree_node_get_ptr(node,
						path[level].bp_index - 1,
						ncmax);
K
Koji Sato 已提交
885 886 887 888 889
	}

	/* parent */
	level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
	if (level <= nilfs_btree_height(btree) - 1) {
890 891 892
		node = nilfs_btree_get_node(btree, path, level, &ncmax);
		return nilfs_btree_node_get_ptr(node, path[level].bp_index,
						ncmax);
K
Koji Sato 已提交
893 894 895 896 897
	}

	return NILFS_BMAP_INVALID_PTR;
}

898
static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
899 900 901 902 903
				       const struct nilfs_btree_path *path,
				       __u64 key)
{
	__u64 ptr;

904
	ptr = nilfs_bmap_find_target_seq(btree, key);
K
Koji Sato 已提交
905 906 907 908 909 910 911 912 913 914
	if (ptr != NILFS_BMAP_INVALID_PTR)
		/* sequential access */
		return ptr;
	else {
		ptr = nilfs_btree_find_near(btree, path);
		if (ptr != NILFS_BMAP_INVALID_PTR)
			/* near */
			return ptr;
	}
	/* block group */
915
	return nilfs_bmap_find_target_in_group(btree);
K
Koji Sato 已提交
916 917
}

918
static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
919 920 921 922 923 924 925
				      struct nilfs_btree_path *path,
				      int *levelp, __u64 key, __u64 ptr,
				      struct nilfs_bmap_stats *stats)
{
	struct buffer_head *bh;
	struct nilfs_btree_node *node, *parent, *sib;
	__u64 sibptr;
926
	int pindex, level, ncmax, ncblk, ret;
927
	struct inode *dat = NULL;
K
Koji Sato 已提交
928 929 930 931 932

	stats->bs_nblocks = 0;
	level = NILFS_BTREE_LEVEL_DATA;

	/* allocate a new ptr for data block */
933
	if (NILFS_BMAP_USE_VBN(btree)) {
K
Koji Sato 已提交
934
		path[level].bp_newreq.bpr_ptr =
935
			nilfs_btree_find_target_v(btree, path, key);
936
		dat = nilfs_bmap_get_dat(btree);
937
	}
K
Koji Sato 已提交
938

939
	ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
940 941 942
	if (ret < 0)
		goto err_out_data;

943
	ncblk = nilfs_btree_nchildren_per_block(btree);
944

K
Koji Sato 已提交
945 946 947
	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < nilfs_btree_height(btree) - 1;
	     level++) {
948
		node = nilfs_btree_get_nonroot_node(path, level);
949
		if (nilfs_btree_node_get_nchildren(node) < ncblk) {
K
Koji Sato 已提交
950 951 952 953 954
			path[level].bp_op = nilfs_btree_do_insert;
			stats->bs_nblocks++;
			goto out;
		}

955
		parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
K
Koji Sato 已提交
956 957 958 959
		pindex = path[level + 1].bp_index;

		/* left sibling */
		if (pindex > 0) {
960 961
			sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
							  ncmax);
962
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
963 964 965
			if (ret < 0)
				goto err_out_child_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
966
			if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
K
Koji Sato 已提交
967 968 969 970
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_carry_left;
				stats->bs_nblocks++;
				goto out;
971
			} else {
972
				brelse(bh);
973
			}
K
Koji Sato 已提交
974 975 976
		}

		/* right sibling */
977 978 979
		if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
			sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
							  ncmax);
980
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
981 982 983
			if (ret < 0)
				goto err_out_child_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
984
			if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
K
Koji Sato 已提交
985 986 987 988
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_carry_right;
				stats->bs_nblocks++;
				goto out;
989
			} else {
990
				brelse(bh);
991
			}
K
Koji Sato 已提交
992 993 994 995 996
		}

		/* split */
		path[level].bp_newreq.bpr_ptr =
			path[level - 1].bp_newreq.bpr_ptr + 1;
997
		ret = nilfs_bmap_prepare_alloc_ptr(btree,
998
						   &path[level].bp_newreq, dat);
K
Koji Sato 已提交
999 1000
		if (ret < 0)
			goto err_out_child_node;
1001 1002 1003
		ret = nilfs_btree_get_new_block(btree,
						path[level].bp_newreq.bpr_ptr,
						&bh);
K
Koji Sato 已提交
1004 1005 1006 1007 1008
		if (ret < 0)
			goto err_out_curr_node;

		stats->bs_nblocks++;

1009 1010
		sib = (struct nilfs_btree_node *)bh->b_data;
		nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
K
Koji Sato 已提交
1011 1012 1013 1014 1015 1016
		path[level].bp_sib_bh = bh;
		path[level].bp_op = nilfs_btree_split;
	}

	/* root */
	node = nilfs_btree_get_root(btree);
1017
	if (nilfs_btree_node_get_nchildren(node) <
1018
	    NILFS_BTREE_ROOT_NCHILDREN_MAX) {
K
Koji Sato 已提交
1019 1020 1021 1022 1023 1024 1025
		path[level].bp_op = nilfs_btree_do_insert;
		stats->bs_nblocks++;
		goto out;
	}

	/* grow */
	path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
1026
	ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1027 1028
	if (ret < 0)
		goto err_out_child_node;
1029 1030
	ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
					&bh);
K
Koji Sato 已提交
1031 1032 1033
	if (ret < 0)
		goto err_out_curr_node;

1034 1035
	nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
			      0, level, 0, ncblk, NULL, NULL);
K
Koji Sato 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	path[level].bp_sib_bh = bh;
	path[level].bp_op = nilfs_btree_grow;

	level++;
	path[level].bp_op = nilfs_btree_do_insert;

	/* a newly-created node block and a data block are added */
	stats->bs_nblocks += 2;

	/* success */
 out:
	*levelp = level;
	return ret;

	/* error */
 err_out_curr_node:
1052
	nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1053 1054
 err_out_child_node:
	for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
1055
		nilfs_btnode_delete(path[level].bp_sib_bh);
1056
		nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1057 1058 1059

	}

1060
	nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1061 1062 1063 1064 1065 1066
 err_out_data:
	*levelp = level;
	stats->bs_nblocks = 0;
	return ret;
}

1067
static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1068 1069 1070
				      struct nilfs_btree_path *path,
				      int maxlevel, __u64 key, __u64 ptr)
{
1071
	struct inode *dat = NULL;
K
Koji Sato 已提交
1072 1073 1074 1075
	int level;

	set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
	ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
1076
	if (NILFS_BMAP_USE_VBN(btree)) {
1077
		nilfs_bmap_set_target_v(btree, key, ptr);
1078
		dat = nilfs_bmap_get_dat(btree);
1079
	}
K
Koji Sato 已提交
1080 1081

	for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
1082
		nilfs_bmap_commit_alloc_ptr(btree,
1083
					    &path[level - 1].bp_newreq, dat);
1084
		path[level].bp_op(btree, path, level, &key, &ptr);
K
Koji Sato 已提交
1085 1086
	}

1087 1088
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1089 1090
}

1091
static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
K
Koji Sato 已提交
1092 1093 1094 1095 1096
{
	struct nilfs_btree_path *path;
	struct nilfs_bmap_stats stats;
	int level, ret;

1097
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	if (path == NULL)
		return -ENOMEM;

	ret = nilfs_btree_do_lookup(btree, path, key, NULL,
				    NILFS_BTREE_LEVEL_NODE_MIN);
	if (ret != -ENOENT) {
		if (ret == 0)
			ret = -EEXIST;
		goto out;
	}

	ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
	if (ret < 0)
		goto out;
	nilfs_btree_commit_insert(btree, path, level, key, ptr);
1113
	nilfs_bmap_add_blocks(btree, stats.bs_nblocks);
K
Koji Sato 已提交
1114 1115

 out:
1116
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1117 1118 1119
	return ret;
}

1120
static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1121 1122 1123 1124
				  struct nilfs_btree_path *path,
				  int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node;
1125
	int ncblk;
K
Koji Sato 已提交
1126 1127

	if (level < nilfs_btree_height(btree) - 1) {
1128
		node = nilfs_btree_get_nonroot_node(path, level);
1129 1130 1131
		ncblk = nilfs_btree_nchildren_per_block(btree);
		nilfs_btree_node_delete(node, path[level].bp_index,
					keyp, ptrp, ncblk);
K
Koji Sato 已提交
1132 1133 1134 1135
		if (!buffer_dirty(path[level].bp_bh))
			nilfs_btnode_mark_dirty(path[level].bp_bh);
		if (path[level].bp_index == 0)
			nilfs_btree_promote_key(btree, path, level + 1,
1136
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
1137 1138
	} else {
		node = nilfs_btree_get_root(btree);
1139 1140 1141
		nilfs_btree_node_delete(node, path[level].bp_index,
					keyp, ptrp,
					NILFS_BTREE_ROOT_NCHILDREN_MAX);
K
Koji Sato 已提交
1142 1143 1144
	}
}

1145
static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1146 1147 1148 1149
				    struct nilfs_btree_path *path,
				    int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *left;
1150
	int nchildren, lnchildren, n, ncblk;
K
Koji Sato 已提交
1151 1152 1153

	nilfs_btree_do_delete(btree, path, level, keyp, ptrp);

1154 1155 1156 1157
	node = nilfs_btree_get_nonroot_node(path, level);
	left = nilfs_btree_get_sib_node(path, level);
	nchildren = nilfs_btree_node_get_nchildren(node);
	lnchildren = nilfs_btree_node_get_nchildren(left);
1158
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1159 1160 1161

	n = (nchildren + lnchildren) / 2 - nchildren;

1162
	nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
K
Koji Sato 已提交
1163 1164 1165 1166 1167 1168 1169

	if (!buffer_dirty(path[level].bp_bh))
		nilfs_btnode_mark_dirty(path[level].bp_bh);
	if (!buffer_dirty(path[level].bp_sib_bh))
		nilfs_btnode_mark_dirty(path[level].bp_sib_bh);

	nilfs_btree_promote_key(btree, path, level + 1,
1170
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
1171

1172
	brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
1173 1174 1175 1176
	path[level].bp_sib_bh = NULL;
	path[level].bp_index += n;
}

1177
static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1178 1179 1180 1181
				     struct nilfs_btree_path *path,
				     int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
1182
	int nchildren, rnchildren, n, ncblk;
K
Koji Sato 已提交
1183 1184 1185

	nilfs_btree_do_delete(btree, path, level, keyp, ptrp);

1186 1187 1188 1189
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
	nchildren = nilfs_btree_node_get_nchildren(node);
	rnchildren = nilfs_btree_node_get_nchildren(right);
1190
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1191 1192 1193

	n = (nchildren + rnchildren) / 2 - nchildren;

1194
	nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
1195 1196 1197 1198 1199 1200 1201 1202

	if (!buffer_dirty(path[level].bp_bh))
		nilfs_btnode_mark_dirty(path[level].bp_bh);
	if (!buffer_dirty(path[level].bp_sib_bh))
		nilfs_btnode_mark_dirty(path[level].bp_sib_bh);

	path[level + 1].bp_index++;
	nilfs_btree_promote_key(btree, path, level + 1,
1203
				nilfs_btree_node_get_key(right, 0));
K
Koji Sato 已提交
1204 1205
	path[level + 1].bp_index--;

1206
	brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
1207 1208 1209
	path[level].bp_sib_bh = NULL;
}

1210
static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1211 1212 1213 1214
				    struct nilfs_btree_path *path,
				    int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *left;
1215
	int n, ncblk;
K
Koji Sato 已提交
1216 1217 1218

	nilfs_btree_do_delete(btree, path, level, keyp, ptrp);

1219 1220
	node = nilfs_btree_get_nonroot_node(path, level);
	left = nilfs_btree_get_sib_node(path, level);
1221
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1222

1223
	n = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
1224

1225
	nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
K
Koji Sato 已提交
1226 1227 1228 1229

	if (!buffer_dirty(path[level].bp_sib_bh))
		nilfs_btnode_mark_dirty(path[level].bp_sib_bh);

1230
	nilfs_btnode_delete(path[level].bp_bh);
K
Koji Sato 已提交
1231 1232
	path[level].bp_bh = path[level].bp_sib_bh;
	path[level].bp_sib_bh = NULL;
1233
	path[level].bp_index += nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
1234 1235
}

1236
static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1237 1238 1239 1240
				     struct nilfs_btree_path *path,
				     int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
1241
	int n, ncblk;
K
Koji Sato 已提交
1242 1243 1244

	nilfs_btree_do_delete(btree, path, level, keyp, ptrp);

1245 1246
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
1247
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1248

1249
	n = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
1250

1251
	nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
1252 1253 1254 1255

	if (!buffer_dirty(path[level].bp_bh))
		nilfs_btnode_mark_dirty(path[level].bp_bh);

1256
	nilfs_btnode_delete(path[level].bp_sib_bh);
K
Koji Sato 已提交
1257 1258 1259 1260
	path[level].bp_sib_bh = NULL;
	path[level + 1].bp_index++;
}

1261
static void nilfs_btree_shrink(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1262 1263 1264 1265
			       struct nilfs_btree_path *path,
			       int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *root, *child;
1266
	int n, ncblk;
K
Koji Sato 已提交
1267 1268 1269 1270

	nilfs_btree_do_delete(btree, path, level, keyp, ptrp);

	root = nilfs_btree_get_root(btree);
1271
	child = nilfs_btree_get_nonroot_node(path, level);
1272
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1273

1274 1275
	nilfs_btree_node_delete(root, 0, NULL, NULL,
				NILFS_BTREE_ROOT_NCHILDREN_MAX);
1276 1277
	nilfs_btree_node_set_level(root, level);
	n = nilfs_btree_node_get_nchildren(child);
1278 1279
	nilfs_btree_node_move_left(root, child, n,
				   NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
K
Koji Sato 已提交
1280

1281
	nilfs_btnode_delete(path[level].bp_bh);
K
Koji Sato 已提交
1282 1283 1284 1285
	path[level].bp_bh = NULL;
}


1286
static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1287 1288
				      struct nilfs_btree_path *path,
				      int *levelp,
1289 1290
				      struct nilfs_bmap_stats *stats,
				      struct inode *dat)
K
Koji Sato 已提交
1291 1292 1293 1294
{
	struct buffer_head *bh;
	struct nilfs_btree_node *node, *parent, *sib;
	__u64 sibptr;
1295
	int pindex, level, ncmin, ncmax, ncblk, ret;
K
Koji Sato 已提交
1296 1297 1298

	ret = 0;
	stats->bs_nblocks = 0;
1299
	ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
1300
	ncblk = nilfs_btree_nchildren_per_block(btree);
1301

K
Koji Sato 已提交
1302 1303 1304
	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < nilfs_btree_height(btree) - 1;
	     level++) {
1305
		node = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
1306
		path[level].bp_oldreq.bpr_ptr =
1307 1308
			nilfs_btree_node_get_ptr(node, path[level].bp_index,
						 ncblk);
1309
		ret = nilfs_bmap_prepare_end_ptr(btree,
1310
						 &path[level].bp_oldreq, dat);
1311 1312
		if (ret < 0)
			goto err_out_child_node;
K
Koji Sato 已提交
1313

1314
		if (nilfs_btree_node_get_nchildren(node) > ncmin) {
K
Koji Sato 已提交
1315 1316 1317 1318 1319
			path[level].bp_op = nilfs_btree_do_delete;
			stats->bs_nblocks++;
			goto out;
		}

1320
		parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
K
Koji Sato 已提交
1321 1322 1323 1324
		pindex = path[level + 1].bp_index;

		if (pindex > 0) {
			/* left sibling */
1325 1326
			sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
							  ncmax);
1327
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1328 1329 1330
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1331
			if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
K
Koji Sato 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_borrow_left;
				stats->bs_nblocks++;
				goto out;
			} else {
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_concat_left;
				stats->bs_nblocks++;
				/* continue; */
			}
		} else if (pindex <
1343
			   nilfs_btree_node_get_nchildren(parent) - 1) {
K
Koji Sato 已提交
1344
			/* right sibling */
1345 1346
			sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
							  ncmax);
1347
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1348 1349 1350
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1351
			if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
K
Koji Sato 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_borrow_right;
				stats->bs_nblocks++;
				goto out;
			} else {
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_concat_right;
				stats->bs_nblocks++;
				/* continue; */
			}
		} else {
			/* no siblings */
			/* the only child of the root node */
1365
			WARN_ON(level != nilfs_btree_height(btree) - 2);
1366
			if (nilfs_btree_node_get_nchildren(node) - 1 <=
K
Koji Sato 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
			    NILFS_BTREE_ROOT_NCHILDREN_MAX) {
				path[level].bp_op = nilfs_btree_shrink;
				stats->bs_nblocks += 2;
			} else {
				path[level].bp_op = nilfs_btree_do_delete;
				stats->bs_nblocks++;
			}

			goto out;

		}
	}

	node = nilfs_btree_get_root(btree);
	path[level].bp_oldreq.bpr_ptr =
1382 1383
		nilfs_btree_node_get_ptr(node, path[level].bp_index,
					 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1384

1385
	ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
1386 1387 1388
	if (ret < 0)
		goto err_out_child_node;

K
Koji Sato 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
	/* child of the root node is deleted */
	path[level].bp_op = nilfs_btree_do_delete;
	stats->bs_nblocks++;

	/* success */
 out:
	*levelp = level;
	return ret;

	/* error */
 err_out_curr_node:
1400
	nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1401 1402
 err_out_child_node:
	for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
1403
		brelse(path[level].bp_sib_bh);
1404
		nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1405 1406 1407 1408 1409 1410
	}
	*levelp = level;
	stats->bs_nblocks = 0;
	return ret;
}

1411
static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1412
				      struct nilfs_btree_path *path,
1413
				      int maxlevel, struct inode *dat)
K
Koji Sato 已提交
1414 1415 1416 1417
{
	int level;

	for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
1418
		nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
1419
		path[level].bp_op(btree, path, level, NULL, NULL);
K
Koji Sato 已提交
1420 1421
	}

1422 1423
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1424 1425
}

1426
static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
K
Koji Sato 已提交
1427 1428 1429 1430

{
	struct nilfs_btree_path *path;
	struct nilfs_bmap_stats stats;
1431
	struct inode *dat;
K
Koji Sato 已提交
1432 1433
	int level, ret;

1434
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1435 1436
	if (path == NULL)
		return -ENOMEM;
1437

K
Koji Sato 已提交
1438 1439 1440 1441 1442
	ret = nilfs_btree_do_lookup(btree, path, key, NULL,
				    NILFS_BTREE_LEVEL_NODE_MIN);
	if (ret < 0)
		goto out;

1443

1444
	dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
1445 1446

	ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
K
Koji Sato 已提交
1447 1448
	if (ret < 0)
		goto out;
1449
	nilfs_btree_commit_delete(btree, path, level, dat);
1450
	nilfs_bmap_sub_blocks(btree, stats.bs_nblocks);
K
Koji Sato 已提交
1451 1452

out:
1453
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1454 1455 1456
	return ret;
}

1457
static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
K
Koji Sato 已提交
1458 1459 1460 1461
{
	struct nilfs_btree_path *path;
	int ret;

1462
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1463 1464 1465 1466 1467
	if (path == NULL)
		return -ENOMEM;

	ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);

1468
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1469 1470 1471 1472

	return ret;
}

1473
static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
K
Koji Sato 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
{
	struct buffer_head *bh;
	struct nilfs_btree_node *root, *node;
	__u64 maxkey, nextmaxkey;
	__u64 ptr;
	int nchildren, ret;

	root = nilfs_btree_get_root(btree);
	switch (nilfs_btree_height(btree)) {
	case 2:
		bh = NULL;
		node = root;
		break;
	case 3:
1488
		nchildren = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
1489 1490
		if (nchildren > 1)
			return 0;
1491 1492
		ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
					       NILFS_BTREE_ROOT_NCHILDREN_MAX);
1493
		ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
1494 1495 1496 1497 1498 1499 1500 1501
		if (ret < 0)
			return ret;
		node = (struct nilfs_btree_node *)bh->b_data;
		break;
	default:
		return 0;
	}

1502 1503
	nchildren = nilfs_btree_node_get_nchildren(node);
	maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
K
Koji Sato 已提交
1504
	nextmaxkey = (nchildren > 1) ?
1505
		nilfs_btree_node_get_key(node, nchildren - 2) : 0;
K
Koji Sato 已提交
1506
	if (bh != NULL)
1507
		brelse(bh);
K
Koji Sato 已提交
1508

1509
	return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
K
Koji Sato 已提交
1510 1511
}

1512
static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1513 1514 1515 1516 1517 1518 1519
				   __u64 *keys, __u64 *ptrs, int nitems)
{
	struct buffer_head *bh;
	struct nilfs_btree_node *node, *root;
	__le64 *dkeys;
	__le64 *dptrs;
	__u64 ptr;
1520
	int nchildren, ncmax, i, ret;
K
Koji Sato 已提交
1521 1522 1523 1524 1525 1526

	root = nilfs_btree_get_root(btree);
	switch (nilfs_btree_height(btree)) {
	case 2:
		bh = NULL;
		node = root;
1527
		ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
K
Koji Sato 已提交
1528 1529
		break;
	case 3:
1530
		nchildren = nilfs_btree_node_get_nchildren(root);
1531
		WARN_ON(nchildren > 1);
1532 1533
		ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
					       NILFS_BTREE_ROOT_NCHILDREN_MAX);
1534
		ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
1535 1536 1537
		if (ret < 0)
			return ret;
		node = (struct nilfs_btree_node *)bh->b_data;
1538
		ncmax = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1539 1540 1541
		break;
	default:
		node = NULL;
1542
		return -EINVAL;
K
Koji Sato 已提交
1543 1544
	}

1545
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
1546 1547
	if (nchildren < nitems)
		nitems = nchildren;
1548
	dkeys = nilfs_btree_node_dkeys(node);
1549
	dptrs = nilfs_btree_node_dptrs(node, ncmax);
K
Koji Sato 已提交
1550
	for (i = 0; i < nitems; i++) {
1551 1552
		keys[i] = le64_to_cpu(dkeys[i]);
		ptrs[i] = le64_to_cpu(dptrs[i]);
K
Koji Sato 已提交
1553 1554 1555
	}

	if (bh != NULL)
1556
		brelse(bh);
K
Koji Sato 已提交
1557 1558 1559 1560 1561

	return nitems;
}

static int
1562
nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
K
Koji Sato 已提交
1563 1564 1565 1566 1567 1568
				       union nilfs_bmap_ptr_req *dreq,
				       union nilfs_bmap_ptr_req *nreq,
				       struct buffer_head **bhp,
				       struct nilfs_bmap_stats *stats)
{
	struct buffer_head *bh;
1569
	struct inode *dat = NULL;
K
Koji Sato 已提交
1570 1571 1572 1573 1574 1575
	int ret;

	stats->bs_nblocks = 0;

	/* for data */
	/* cannot find near ptr */
1576
	if (NILFS_BMAP_USE_VBN(btree)) {
1577
		dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
1578
		dat = nilfs_bmap_get_dat(btree);
1579
	}
1580

1581
	ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
K
Koji Sato 已提交
1582 1583 1584 1585 1586 1587 1588
	if (ret < 0)
		return ret;

	*bhp = NULL;
	stats->bs_nblocks++;
	if (nreq != NULL) {
		nreq->bpr_ptr = dreq->bpr_ptr + 1;
1589
		ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
K
Koji Sato 已提交
1590 1591 1592
		if (ret < 0)
			goto err_out_dreq;

1593
		ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
K
Koji Sato 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
		if (ret < 0)
			goto err_out_nreq;

		*bhp = bh;
		stats->bs_nblocks++;
	}

	/* success */
	return 0;

	/* error */
 err_out_nreq:
1606
	nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
K
Koji Sato 已提交
1607
 err_out_dreq:
1608
	nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
K
Koji Sato 已提交
1609 1610 1611 1612 1613 1614
	stats->bs_nblocks = 0;
	return ret;

}

static void
1615
nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1616 1617
				      __u64 key, __u64 ptr,
				      const __u64 *keys, const __u64 *ptrs,
1618
				      int n,
K
Koji Sato 已提交
1619 1620 1621 1622 1623
				      union nilfs_bmap_ptr_req *dreq,
				      union nilfs_bmap_ptr_req *nreq,
				      struct buffer_head *bh)
{
	struct nilfs_btree_node *node;
1624
	struct inode *dat;
K
Koji Sato 已提交
1625
	__u64 tmpptr;
1626
	int ncblk;
K
Koji Sato 已提交
1627 1628

	/* free resources */
1629 1630
	if (btree->b_ops->bop_clear != NULL)
		btree->b_ops->bop_clear(btree);
K
Koji Sato 已提交
1631 1632 1633 1634 1635

	/* ptr must be a pointer to a buffer head. */
	set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));

	/* convert and insert */
1636 1637
	dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
	nilfs_btree_init(btree);
K
Koji Sato 已提交
1638
	if (nreq != NULL) {
1639 1640
		nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
		nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
K
Koji Sato 已提交
1641 1642 1643

		/* create child node at level 1 */
		node = (struct nilfs_btree_node *)bh->b_data;
1644 1645 1646
		ncblk = nilfs_btree_nchildren_per_block(btree);
		nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
		nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
K
Koji Sato 已提交
1647 1648
		if (!buffer_dirty(bh))
			nilfs_btnode_mark_dirty(bh);
1649 1650
		if (!nilfs_bmap_dirty(btree))
			nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1651

1652
		brelse(bh);
K
Koji Sato 已提交
1653 1654 1655 1656

		/* create root node at level 2 */
		node = nilfs_btree_get_root(btree);
		tmpptr = nreq->bpr_ptr;
1657 1658 1659
		nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
				      NILFS_BTREE_ROOT_NCHILDREN_MAX,
				      &keys[0], &tmpptr);
K
Koji Sato 已提交
1660
	} else {
1661
		nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
K
Koji Sato 已提交
1662 1663 1664

		/* create root node at level 1 */
		node = nilfs_btree_get_root(btree);
1665 1666 1667 1668 1669
		nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
				      NILFS_BTREE_ROOT_NCHILDREN_MAX,
				      keys, ptrs);
		nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
					NILFS_BTREE_ROOT_NCHILDREN_MAX);
1670 1671
		if (!nilfs_bmap_dirty(btree))
			nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1672 1673
	}

1674
	if (NILFS_BMAP_USE_VBN(btree))
1675
		nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
K
Koji Sato 已提交
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
}

/**
 * nilfs_btree_convert_and_insert -
 * @bmap:
 * @key:
 * @ptr:
 * @keys:
 * @ptrs:
 * @n:
 */
1687
int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1688
				   __u64 key, __u64 ptr,
1689
				   const __u64 *keys, const __u64 *ptrs, int n)
K
Koji Sato 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
{
	struct buffer_head *bh;
	union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
	struct nilfs_bmap_stats stats;
	int ret;

	if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
		di = &dreq;
		ni = NULL;
	} else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
1700
			   1 << btree->b_inode->i_blkbits)) {
K
Koji Sato 已提交
1701 1702 1703 1704 1705 1706 1707 1708
		di = &dreq;
		ni = &nreq;
	} else {
		di = NULL;
		ni = NULL;
		BUG();
	}

1709
	ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
K
Koji Sato 已提交
1710 1711 1712
						     &stats);
	if (ret < 0)
		return ret;
1713
	nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
1714
					      di, ni, bh);
1715
	nilfs_bmap_add_blocks(btree, stats.bs_nblocks);
K
Koji Sato 已提交
1716 1717 1718
	return 0;
}

1719
static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
				   struct nilfs_btree_path *path,
				   int level,
				   struct buffer_head *bh)
{
	while ((++level < nilfs_btree_height(btree) - 1) &&
	       !buffer_dirty(path[level].bp_bh))
		nilfs_btnode_mark_dirty(path[level].bp_bh);

	return 0;
}

1731
static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1732
					struct nilfs_btree_path *path,
1733
					int level, struct inode *dat)
K
Koji Sato 已提交
1734 1735
{
	struct nilfs_btree_node *parent;
1736
	int ncmax, ret;
K
Koji Sato 已提交
1737

1738
	parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
K
Koji Sato 已提交
1739
	path[level].bp_oldreq.bpr_ptr =
1740 1741
		nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
					 ncmax);
K
Koji Sato 已提交
1742
	path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
1743 1744
	ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
				       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1745 1746 1747 1748 1749 1750 1751 1752
	if (ret < 0)
		return ret;

	if (buffer_nilfs_node(path[level].bp_bh)) {
		path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
		path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
		path[level].bp_ctxt.bh = path[level].bp_bh;
		ret = nilfs_btnode_prepare_change_key(
1753
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
1754 1755
			&path[level].bp_ctxt);
		if (ret < 0) {
1756 1757 1758
			nilfs_dat_abort_update(dat,
					       &path[level].bp_oldreq.bpr_req,
					       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1759 1760 1761 1762 1763 1764 1765
			return ret;
		}
	}

	return 0;
}

1766
static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1767
					struct nilfs_btree_path *path,
1768
					int level, struct inode *dat)
K
Koji Sato 已提交
1769 1770
{
	struct nilfs_btree_node *parent;
1771
	int ncmax;
K
Koji Sato 已提交
1772

1773 1774
	nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
				&path[level].bp_newreq.bpr_req,
1775
				btree->b_ptr_type == NILFS_BMAP_PTR_VS);
K
Koji Sato 已提交
1776 1777 1778

	if (buffer_nilfs_node(path[level].bp_bh)) {
		nilfs_btnode_commit_change_key(
1779
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
1780 1781 1782 1783 1784
			&path[level].bp_ctxt);
		path[level].bp_bh = path[level].bp_ctxt.bh;
	}
	set_buffer_nilfs_volatile(path[level].bp_bh);

1785 1786 1787
	parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
	nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
				 path[level].bp_newreq.bpr_ptr, ncmax);
K
Koji Sato 已提交
1788 1789
}

1790
static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1791
				       struct nilfs_btree_path *path,
1792
				       int level, struct inode *dat)
K
Koji Sato 已提交
1793
{
1794 1795
	nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
			       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1796 1797
	if (buffer_nilfs_node(path[level].bp_bh))
		nilfs_btnode_abort_change_key(
1798
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
1799 1800 1801
			&path[level].bp_ctxt);
}

1802
static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1803
					   struct nilfs_btree_path *path,
1804 1805
					   int minlevel, int *maxlevelp,
					   struct inode *dat)
K
Koji Sato 已提交
1806 1807 1808 1809 1810
{
	int level, ret;

	level = minlevel;
	if (!buffer_nilfs_volatile(path[level].bp_bh)) {
1811
		ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1812 1813 1814 1815 1816 1817
		if (ret < 0)
			return ret;
	}
	while ((++level < nilfs_btree_height(btree) - 1) &&
	       !buffer_dirty(path[level].bp_bh)) {

1818
		WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
1819
		ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
		if (ret < 0)
			goto out;
	}

	/* success */
	*maxlevelp = level - 1;
	return 0;

	/* error */
 out:
	while (--level > minlevel)
1831
		nilfs_btree_abort_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1832
	if (!buffer_nilfs_volatile(path[level].bp_bh))
1833
		nilfs_btree_abort_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1834 1835 1836
	return ret;
}

1837
static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1838
					   struct nilfs_btree_path *path,
1839 1840 1841
					   int minlevel, int maxlevel,
					   struct buffer_head *bh,
					   struct inode *dat)
K
Koji Sato 已提交
1842 1843 1844 1845
{
	int level;

	if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
1846
		nilfs_btree_commit_update_v(btree, path, minlevel, dat);
K
Koji Sato 已提交
1847 1848

	for (level = minlevel + 1; level <= maxlevel; level++)
1849
		nilfs_btree_commit_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1850 1851
}

1852
static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1853
				   struct nilfs_btree_path *path,
1854
				   int level, struct buffer_head *bh)
K
Koji Sato 已提交
1855
{
1856
	int maxlevel = 0, ret;
K
Koji Sato 已提交
1857
	struct nilfs_btree_node *parent;
1858
	struct inode *dat = nilfs_bmap_get_dat(btree);
K
Koji Sato 已提交
1859
	__u64 ptr;
1860
	int ncmax;
K
Koji Sato 已提交
1861 1862 1863

	get_bh(bh);
	path[level].bp_bh = bh;
1864 1865
	ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
					      dat);
K
Koji Sato 已提交
1866 1867 1868 1869
	if (ret < 0)
		goto out;

	if (buffer_nilfs_volatile(path[level].bp_bh)) {
1870 1871 1872 1873
		parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
		ptr = nilfs_btree_node_get_ptr(parent,
					       path[level + 1].bp_index,
					       ncmax);
1874
		ret = nilfs_dat_mark_dirty(dat, ptr);
K
Koji Sato 已提交
1875 1876 1877 1878
		if (ret < 0)
			goto out;
	}

1879
	nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
K
Koji Sato 已提交
1880 1881 1882 1883 1884 1885 1886

 out:
	brelse(path[level].bp_bh);
	path[level].bp_bh = NULL;
	return ret;
}

1887
static int nilfs_btree_propagate(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1888 1889 1890 1891 1892 1893 1894
				 struct buffer_head *bh)
{
	struct nilfs_btree_path *path;
	struct nilfs_btree_node *node;
	__u64 key;
	int level, ret;

1895
	WARN_ON(!buffer_dirty(bh));
K
Koji Sato 已提交
1896

1897
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1898 1899 1900 1901 1902
	if (path == NULL)
		return -ENOMEM;

	if (buffer_nilfs_node(bh)) {
		node = (struct nilfs_btree_node *)bh->b_data;
1903 1904
		key = nilfs_btree_node_get_key(node, 0);
		level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
1905
	} else {
1906
		key = nilfs_bmap_data_get_key(btree, bh);
K
Koji Sato 已提交
1907 1908 1909 1910 1911
		level = NILFS_BTREE_LEVEL_DATA;
	}

	ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
	if (ret < 0) {
1912
		if (unlikely(ret == -ENOENT))
K
Koji Sato 已提交
1913 1914 1915 1916 1917
			printk(KERN_CRIT "%s: key = %llu, level == %d\n",
			       __func__, (unsigned long long)key, level);
		goto out;
	}

1918
	ret = NILFS_BMAP_USE_VBN(btree) ?
1919 1920
		nilfs_btree_propagate_v(btree, path, level, bh) :
		nilfs_btree_propagate_p(btree, path, level, bh);
K
Koji Sato 已提交
1921 1922

 out:
1923
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1924 1925 1926 1927

	return ret;
}

1928
static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1929 1930
				    struct buffer_head *bh)
{
1931
	return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
K
Koji Sato 已提交
1932 1933
}

1934
static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
					 struct list_head *lists,
					 struct buffer_head *bh)
{
	struct list_head *head;
	struct buffer_head *cbh;
	struct nilfs_btree_node *node, *cnode;
	__u64 key, ckey;
	int level;

	get_bh(bh);
	node = (struct nilfs_btree_node *)bh->b_data;
1946 1947
	key = nilfs_btree_node_get_key(node, 0);
	level = nilfs_btree_node_get_level(node);
1948 1949 1950 1951 1952 1953 1954
	if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
	    level >= NILFS_BTREE_LEVEL_MAX) {
		dump_stack();
		printk(KERN_WARNING
		       "%s: invalid btree level: %d (key=%llu, ino=%lu, "
		       "blocknr=%llu)\n",
		       __func__, level, (unsigned long long)key,
1955
		       NILFS_BMAP_I(btree)->vfs_inode.i_ino,
1956 1957 1958 1959
		       (unsigned long long)bh->b_blocknr);
		return;
	}

K
Koji Sato 已提交
1960 1961 1962
	list_for_each(head, &lists[level]) {
		cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
		cnode = (struct nilfs_btree_node *)cbh->b_data;
1963
		ckey = nilfs_btree_node_get_key(cnode, 0);
K
Koji Sato 已提交
1964 1965 1966 1967 1968 1969
		if (key < ckey)
			break;
	}
	list_add_tail(&bh->b_assoc_buffers, head);
}

1970
static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1971 1972
					     struct list_head *listp)
{
1973
	struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
K
Koji Sato 已提交
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
	struct list_head lists[NILFS_BTREE_LEVEL_MAX];
	struct pagevec pvec;
	struct buffer_head *bh, *head;
	pgoff_t index = 0;
	int level, i;

	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < NILFS_BTREE_LEVEL_MAX;
	     level++)
		INIT_LIST_HEAD(&lists[level]);

	pagevec_init(&pvec, 0);

	while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
				  PAGEVEC_SIZE)) {
		for (i = 0; i < pagevec_count(&pvec); i++) {
			bh = head = page_buffers(pvec.pages[i]);
			do {
				if (buffer_dirty(bh))
					nilfs_btree_add_dirty_buffer(btree,
								     lists, bh);
			} while ((bh = bh->b_this_page) != head);
		}
		pagevec_release(&pvec);
		cond_resched();
	}

	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < NILFS_BTREE_LEVEL_MAX;
	     level++)
2004
		list_splice_tail(&lists[level], listp);
K
Koji Sato 已提交
2005 2006
}

2007
static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016
				struct nilfs_btree_path *path,
				int level,
				struct buffer_head **bh,
				sector_t blocknr,
				union nilfs_binfo *binfo)
{
	struct nilfs_btree_node *parent;
	__u64 key;
	__u64 ptr;
2017
	int ncmax, ret;
K
Koji Sato 已提交
2018

2019 2020 2021
	parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
	ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
				       ncmax);
K
Koji Sato 已提交
2022 2023 2024 2025 2026
	if (buffer_nilfs_node(*bh)) {
		path[level].bp_ctxt.oldkey = ptr;
		path[level].bp_ctxt.newkey = blocknr;
		path[level].bp_ctxt.bh = *bh;
		ret = nilfs_btnode_prepare_change_key(
2027
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
2028 2029 2030 2031
			&path[level].bp_ctxt);
		if (ret < 0)
			return ret;
		nilfs_btnode_commit_change_key(
2032
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
2033 2034 2035 2036
			&path[level].bp_ctxt);
		*bh = path[level].bp_ctxt.bh;
	}

2037 2038
	nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
				 ncmax);
K
Koji Sato 已提交
2039

2040
	key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
K
Koji Sato 已提交
2041
	/* on-disk format */
2042
	binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
K
Koji Sato 已提交
2043 2044 2045 2046 2047
	binfo->bi_dat.bi_level = level;

	return 0;
}

2048
static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2049 2050 2051 2052 2053 2054 2055
				struct nilfs_btree_path *path,
				int level,
				struct buffer_head **bh,
				sector_t blocknr,
				union nilfs_binfo *binfo)
{
	struct nilfs_btree_node *parent;
2056
	struct inode *dat = nilfs_bmap_get_dat(btree);
K
Koji Sato 已提交
2057 2058 2059
	__u64 key;
	__u64 ptr;
	union nilfs_bmap_ptr_req req;
2060
	int ncmax, ret;
K
Koji Sato 已提交
2061

2062 2063 2064
	parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
	ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
				       ncmax);
K
Koji Sato 已提交
2065
	req.bpr_ptr = ptr;
2066 2067
	ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
	if (ret < 0)
K
Koji Sato 已提交
2068
		return ret;
2069
	nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
K
Koji Sato 已提交
2070

2071
	key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
K
Koji Sato 已提交
2072
	/* on-disk format */
2073 2074
	binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
	binfo->bi_v.bi_blkoff = cpu_to_le64(key);
K
Koji Sato 已提交
2075 2076 2077 2078

	return 0;
}

2079
static int nilfs_btree_assign(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088
			      struct buffer_head **bh,
			      sector_t blocknr,
			      union nilfs_binfo *binfo)
{
	struct nilfs_btree_path *path;
	struct nilfs_btree_node *node;
	__u64 key;
	int level, ret;

2089
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2090 2091 2092 2093 2094
	if (path == NULL)
		return -ENOMEM;

	if (buffer_nilfs_node(*bh)) {
		node = (struct nilfs_btree_node *)(*bh)->b_data;
2095 2096
		key = nilfs_btree_node_get_key(node, 0);
		level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
2097
	} else {
2098
		key = nilfs_bmap_data_get_key(btree, *bh);
K
Koji Sato 已提交
2099 2100 2101 2102 2103
		level = NILFS_BTREE_LEVEL_DATA;
	}

	ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
	if (ret < 0) {
2104
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2105 2106 2107
		goto out;
	}

2108
	ret = NILFS_BMAP_USE_VBN(btree) ?
2109 2110
		nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
		nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
K
Koji Sato 已提交
2111 2112

 out:
2113
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2114 2115 2116 2117

	return ret;
}

2118
static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2119 2120 2121 2122 2123 2124 2125 2126
				 struct buffer_head **bh,
				 sector_t blocknr,
				 union nilfs_binfo *binfo)
{
	struct nilfs_btree_node *node;
	__u64 key;
	int ret;

2127
	ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
2128
			     blocknr);
K
Koji Sato 已提交
2129 2130 2131 2132 2133
	if (ret < 0)
		return ret;

	if (buffer_nilfs_node(*bh)) {
		node = (struct nilfs_btree_node *)(*bh)->b_data;
2134
		key = nilfs_btree_node_get_key(node, 0);
K
Koji Sato 已提交
2135
	} else
2136
		key = nilfs_bmap_data_get_key(btree, *bh);
K
Koji Sato 已提交
2137 2138 2139

	/* on-disk format */
	binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
2140
	binfo->bi_v.bi_blkoff = cpu_to_le64(key);
K
Koji Sato 已提交
2141 2142 2143 2144

	return 0;
}

2145
static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
K
Koji Sato 已提交
2146 2147 2148 2149 2150 2151
{
	struct buffer_head *bh;
	struct nilfs_btree_path *path;
	__u64 ptr;
	int ret;

2152
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2153 2154 2155 2156 2157
	if (path == NULL)
		return -ENOMEM;

	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1);
	if (ret < 0) {
2158
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2159 2160
		goto out;
	}
2161
	ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
2162
	if (ret < 0) {
2163
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2164 2165 2166 2167 2168
		goto out;
	}

	if (!buffer_dirty(bh))
		nilfs_btnode_mark_dirty(bh);
2169
	brelse(bh);
2170 2171
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
2172 2173

 out:
2174
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2175 2176 2177 2178 2179
	return ret;
}

static const struct nilfs_bmap_operations nilfs_btree_ops = {
	.bop_lookup		=	nilfs_btree_lookup,
2180
	.bop_lookup_contig	=	nilfs_btree_lookup_contig,
K
Koji Sato 已提交
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
	.bop_insert		=	nilfs_btree_insert,
	.bop_delete		=	nilfs_btree_delete,
	.bop_clear		=	NULL,

	.bop_propagate		=	nilfs_btree_propagate,

	.bop_lookup_dirty_buffers =	nilfs_btree_lookup_dirty_buffers,

	.bop_assign		=	nilfs_btree_assign,
	.bop_mark		=	nilfs_btree_mark,

	.bop_last_key		=	nilfs_btree_last_key,
	.bop_check_insert	=	NULL,
	.bop_check_delete	=	nilfs_btree_check_delete,
	.bop_gather_data	=	nilfs_btree_gather_data,
};

static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
	.bop_lookup		=	NULL,
2200
	.bop_lookup_contig	=	NULL,
K
Koji Sato 已提交
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
	.bop_insert		=	NULL,
	.bop_delete		=	NULL,
	.bop_clear		=	NULL,

	.bop_propagate		=	nilfs_btree_propagate_gc,

	.bop_lookup_dirty_buffers =	nilfs_btree_lookup_dirty_buffers,

	.bop_assign		=	nilfs_btree_assign_gc,
	.bop_mark		=	NULL,

	.bop_last_key		=	NULL,
	.bop_check_insert	=	NULL,
	.bop_check_delete	=	NULL,
	.bop_gather_data	=	NULL,
};

2218
int nilfs_btree_init(struct nilfs_bmap *bmap)
K
Koji Sato 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227
{
	bmap->b_ops = &nilfs_btree_ops;
	return 0;
}

void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
{
	bmap->b_ops = &nilfs_btree_ops_gc;
}