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 int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
110 111 112 113
{
	return node->bn_flags;
}

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

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

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

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

136
static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
137 138 139 140
{
	return le16_to_cpu(node->bn_nchildren);
}

141
static void
142
nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
K
Koji Sato 已提交
143 144 145 146
{
	node->bn_nchildren = cpu_to_le16(nchildren);
}

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

152
static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
153
{
154
	return btree->b_nchildren_per_block;
K
Koji Sato 已提交
155 156
}

157
static __le64 *
158
nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
159 160
{
	return (__le64 *)((char *)(node + 1) +
161
			  (nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
162 163 164
			   0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
}

165
static __le64 *
166
nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
K
Koji Sato 已提交
167
{
168
	return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
K
Koji Sato 已提交
169 170
}

171
static __u64
172
nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
K
Koji Sato 已提交
173
{
174
	return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
K
Koji Sato 已提交
175 176
}

177
static void
178
nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
K
Koji Sato 已提交
179
{
180
	*(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
K
Koji Sato 已提交
181 182
}

183
static __u64
184 185
nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
			 int ncmax)
K
Koji Sato 已提交
186
{
187
	return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
K
Koji Sato 已提交
188 189
}

190
static void
191 192
nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
			 int ncmax)
K
Koji Sato 已提交
193
{
194
	*(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
K
Koji Sato 已提交
195 196
}

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

205 206 207
	nilfs_btree_node_set_flags(node, flags);
	nilfs_btree_node_set_level(node, level);
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
208

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

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

226
	ldkeys = nilfs_btree_node_dkeys(left);
227
	ldptrs = nilfs_btree_node_dptrs(left, lncmax);
228
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
229

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

	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;
241 242
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
243 244 245
}

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

254
	ldkeys = nilfs_btree_node_dkeys(left);
255
	ldptrs = nilfs_btree_node_dptrs(left, lncmax);
256
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
257

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

	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;
269 270
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
271 272 273
}

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

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

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

306
	dkeys = nilfs_btree_node_dkeys(node);
307
	dptrs = nilfs_btree_node_dptrs(node, ncmax);
308 309
	key = le64_to_cpu(dkeys[index]);
	ptr = le64_to_cpu(dptrs[index]);
310
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
311 312 313 314 315 316 317 318 319 320 321 322
	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--;
323
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
324 325
}

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

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

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

 out:
	*indexp = index;

	return s == 0;
}

365 366 367 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
/**
 * 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);
}

402
static struct nilfs_btree_node *
403
nilfs_btree_get_root(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
404
{
405
	return (struct nilfs_btree_node *)btree->b_u.u_data;
K
Koji Sato 已提交
406 407
}

408
static struct nilfs_btree_node *
409
nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
K
Koji Sato 已提交
410 411 412 413
{
	return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
}

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

420
static int nilfs_btree_height(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
421
{
422
	return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
K
Koji Sato 已提交
423 424
}

425
static struct nilfs_btree_node *
426
nilfs_btree_get_node(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
427
		     const struct nilfs_btree_path *path,
428
		     int level, int *ncmaxp)
K
Koji Sato 已提交
429
{
430 431 432 433 434 435 436 437 438 439
	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 已提交
440 441
}

442
static int
443 444 445 446 447 448 449 450 451 452 453
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;
}

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

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

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

473
	ncmax = nilfs_btree_nchildren_per_block(btree);
474

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

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

	return 0;
}

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

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

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

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

	return 0;
}

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

549
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
550 551 552
	if (path == NULL)
		return -ENOMEM;

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

555
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
556 557 558 559

	return ret;
}

560
static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
561 562 563 564 565 566 567 568
				     __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;
569
	int ret, cnt, index, maxlevel, ncmax;
570

571
	path = nilfs_btree_alloc_path();
572 573
	if (path == NULL)
		return -ENOMEM;
574

575 576 577 578
	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level);
	if (ret < 0)
		goto out;

579 580
	if (NILFS_BMAP_USE_VBN(btree)) {
		dat = nilfs_bmap_get_dat(btree);
581 582 583 584 585 586 587 588 589 590
		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;
591
	node = nilfs_btree_get_node(btree, path, level, &ncmax);
592 593
	index = path[level].bp_index + 1;
	for (;;) {
594 595
		while (index < nilfs_btree_node_get_nchildren(node)) {
			if (nilfs_btree_node_get_key(node, index) !=
596 597
			    key + cnt)
				goto end;
598
			ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
			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 */
614
		node = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
615
		index = path[level + 1].bp_index + 1;
616 617
		if (index >= nilfs_btree_node_get_nchildren(node) ||
		    nilfs_btree_node_get_key(node, index) != key + cnt)
618
			break;
619
		ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
620 621 622 623 624 625 626
		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;
627
		node = nilfs_btree_get_nonroot_node(path, level);
628
		ncmax = nilfs_btree_nchildren_per_block(btree);
629 630 631 632 633 634 635
		index = 0;
		path[level].bp_index = index;
	}
 end:
	*ptrp = ptr;
	ret = cnt;
 out:
636
	nilfs_btree_free_path(path);
637 638 639
	return ret;
}

640
static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
K
Koji Sato 已提交
641 642 643 644 645 646
				    struct nilfs_btree_path *path,
				    int level, __u64 key)
{
	if (level < nilfs_btree_height(btree) - 1) {
		do {
			nilfs_btree_node_set_key(
647
				nilfs_btree_get_nonroot_node(path, level),
K
Koji Sato 已提交
648 649 650 651 652 653 654 655 656
				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) {
657
		nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
K
Koji Sato 已提交
658 659 660 661
					 path[level].bp_index, key);
	}
}

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

	if (level < nilfs_btree_height(btree) - 1) {
670
		node = nilfs_btree_get_nonroot_node(path, level);
671 672 673
		ncblk = nilfs_btree_nchildren_per_block(btree);
		nilfs_btree_node_insert(node, path[level].bp_index,
					*keyp, *ptrp, ncblk);
K
Koji Sato 已提交
674 675 676 677 678
		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,
679 680
						nilfs_btree_node_get_key(node,
									 0));
K
Koji Sato 已提交
681 682
	} else {
		node = nilfs_btree_get_root(btree);
683 684 685
		nilfs_btree_node_insert(node, path[level].bp_index,
					*keyp, *ptrp,
					NILFS_BTREE_ROOT_NCHILDREN_MAX);
K
Koji Sato 已提交
686 687 688
	}
}

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

696 697 698 699
	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);
700
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
701 702 703 704 705 706 707 708 709
	move = 0;

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

710
	nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
K
Koji Sato 已提交
711 712 713 714 715 716 717

	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,
718
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
719 720

	if (move) {
721
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
722 723 724 725 726
		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 {
727
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
728 729 730 731 732 733 734
		path[level].bp_sib_bh = NULL;
		path[level].bp_index -= n;
	}

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

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

742 743 744 745
	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);
746
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
747 748 749 750 751 752 753 754 755
	move = 0;

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

756
	nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
757 758 759 760 761 762 763 764

	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,
765
				nilfs_btree_node_get_key(right, 0));
K
Koji Sato 已提交
766 767 768
	path[level + 1].bp_index--;

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

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

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

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

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

803
	nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
804 805 806 807 808 809

	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);

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

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

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

821
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
822 823 824 825 826
		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);

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

830
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
831 832 833 834 835 836
		path[level].bp_sib_bh = NULL;
	}

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

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

	root = nilfs_btree_get_root(btree);
845
	child = nilfs_btree_get_sib_node(path, level);
846
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
847

848
	n = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
849

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

	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);

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

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

	if (path == NULL)
		return NILFS_BMAP_INVALID_PTR;

	/* left sibling */
	level = NILFS_BTREE_LEVEL_NODE_MIN;
	if (path[level].bp_index > 0) {
878 879 880 881
		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 已提交
882 883 884 885 886
	}

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

	return NILFS_BMAP_INVALID_PTR;
}

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

901
	ptr = nilfs_bmap_find_target_seq(btree, key);
K
Koji Sato 已提交
902 903 904 905 906 907 908 909 910 911
	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 */
912
	return nilfs_bmap_find_target_in_group(btree);
K
Koji Sato 已提交
913 914
}

915
static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
916 917 918 919 920 921 922
				      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;
923
	int pindex, level, ncmax, ncblk, ret;
924
	struct inode *dat = NULL;
K
Koji Sato 已提交
925 926 927 928 929

	stats->bs_nblocks = 0;
	level = NILFS_BTREE_LEVEL_DATA;

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

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

940
	ncblk = nilfs_btree_nchildren_per_block(btree);
941

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

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

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

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

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

		stats->bs_nblocks++;

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

	/* root */
	node = nilfs_btree_get_root(btree);
1014
	if (nilfs_btree_node_get_nchildren(node) <
1015
	    NILFS_BTREE_ROOT_NCHILDREN_MAX) {
K
Koji Sato 已提交
1016 1017 1018 1019 1020 1021 1022
		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;
1023
	ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1024 1025
	if (ret < 0)
		goto err_out_child_node;
1026 1027
	ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
					&bh);
K
Koji Sato 已提交
1028 1029 1030
	if (ret < 0)
		goto err_out_curr_node;

1031 1032
	nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
			      0, level, 0, ncblk, NULL, NULL);
K
Koji Sato 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	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:
1049
	nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1050 1051
 err_out_child_node:
	for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
1052
		nilfs_btnode_delete(path[level].bp_sib_bh);
1053
		nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1054 1055 1056

	}

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

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

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

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

1084 1085
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1086 1087
}

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

1094
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	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);
1110
	nilfs_bmap_add_blocks(btree, stats.bs_nblocks);
K
Koji Sato 已提交
1111 1112

 out:
1113
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1114 1115 1116
	return ret;
}

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

	if (level < nilfs_btree_height(btree) - 1) {
1125
		node = nilfs_btree_get_nonroot_node(path, level);
1126 1127 1128
		ncblk = nilfs_btree_nchildren_per_block(btree);
		nilfs_btree_node_delete(node, path[level].bp_index,
					keyp, ptrp, ncblk);
K
Koji Sato 已提交
1129 1130 1131 1132
		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,
1133
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
1134 1135
	} else {
		node = nilfs_btree_get_root(btree);
1136 1137 1138
		nilfs_btree_node_delete(node, path[level].bp_index,
					keyp, ptrp,
					NILFS_BTREE_ROOT_NCHILDREN_MAX);
K
Koji Sato 已提交
1139 1140 1141
	}
}

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

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

1151 1152 1153 1154
	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);
1155
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1156 1157 1158

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

1159
	nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
K
Koji Sato 已提交
1160 1161 1162 1163 1164 1165 1166

	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,
1167
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
1168

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

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

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

1183 1184 1185 1186
	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);
1187
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1188 1189 1190

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

1191
	nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
1192 1193 1194 1195 1196 1197 1198 1199

	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,
1200
				nilfs_btree_node_get_key(right, 0));
K
Koji Sato 已提交
1201 1202
	path[level + 1].bp_index--;

1203
	brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
1204 1205 1206
	path[level].bp_sib_bh = NULL;
}

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

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

1216 1217
	node = nilfs_btree_get_nonroot_node(path, level);
	left = nilfs_btree_get_sib_node(path, level);
1218
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1219

1220
	n = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
1221

1222
	nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
K
Koji Sato 已提交
1223 1224 1225 1226

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

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

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

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

1242 1243
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
1244
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1245

1246
	n = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
1247

1248
	nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
K
Koji Sato 已提交
1249 1250 1251 1252

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

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

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

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

	root = nilfs_btree_get_root(btree);
1268
	child = nilfs_btree_get_nonroot_node(path, level);
1269
	ncblk = nilfs_btree_nchildren_per_block(btree);
K
Koji Sato 已提交
1270

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

1278
	nilfs_btnode_delete(path[level].bp_bh);
K
Koji Sato 已提交
1279 1280 1281 1282
	path[level].bp_bh = NULL;
}


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

	ret = 0;
	stats->bs_nblocks = 0;
1296
	ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
1297
	ncblk = nilfs_btree_nchildren_per_block(btree);
1298

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

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

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

		if (pindex > 0) {
			/* left sibling */
1322 1323
			sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
							  ncmax);
1324
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1325 1326 1327
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1328
			if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
K
Koji Sato 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
				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 <
1340
			   nilfs_btree_node_get_nchildren(parent) - 1) {
K
Koji Sato 已提交
1341
			/* right sibling */
1342 1343
			sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
							  ncmax);
1344
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1345 1346 1347
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1348
			if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
K
Koji Sato 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
				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 */
1362
			WARN_ON(level != nilfs_btree_height(btree) - 2);
1363
			if (nilfs_btree_node_get_nchildren(node) - 1 <=
K
Koji Sato 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
			    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 =
1379 1380
		nilfs_btree_node_get_ptr(node, path[level].bp_index,
					 NILFS_BTREE_ROOT_NCHILDREN_MAX);
1381

1382
	ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
1383 1384 1385
	if (ret < 0)
		goto err_out_child_node;

K
Koji Sato 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
	/* 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:
1397
	nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1398 1399
 err_out_child_node:
	for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
1400
		brelse(path[level].bp_sib_bh);
1401
		nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1402 1403 1404 1405 1406 1407
	}
	*levelp = level;
	stats->bs_nblocks = 0;
	return ret;
}

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

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

1419 1420
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1421 1422
}

1423
static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
K
Koji Sato 已提交
1424 1425 1426 1427

{
	struct nilfs_btree_path *path;
	struct nilfs_bmap_stats stats;
1428
	struct inode *dat;
K
Koji Sato 已提交
1429 1430
	int level, ret;

1431
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1432 1433
	if (path == NULL)
		return -ENOMEM;
1434

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

1440

1441
	dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
1442 1443

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

out:
1450
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1451 1452 1453
	return ret;
}

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

1459
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1460 1461 1462 1463 1464
	if (path == NULL)
		return -ENOMEM;

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

1465
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1466 1467 1468 1469

	return ret;
}

1470
static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
K
Koji Sato 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
{
	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:
1485
		nchildren = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
1486 1487
		if (nchildren > 1)
			return 0;
1488 1489
		ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
					       NILFS_BTREE_ROOT_NCHILDREN_MAX);
1490
		ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
1491 1492 1493 1494 1495 1496 1497 1498
		if (ret < 0)
			return ret;
		node = (struct nilfs_btree_node *)bh->b_data;
		break;
	default:
		return 0;
	}

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

1506
	return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
K
Koji Sato 已提交
1507 1508
}

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

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

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

	if (bh != NULL)
1553
		brelse(bh);
K
Koji Sato 已提交
1554 1555 1556 1557 1558

	return nitems;
}

static int
1559
nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
K
Koji Sato 已提交
1560 1561 1562 1563 1564 1565
				       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;
1566
	struct inode *dat = NULL;
K
Koji Sato 已提交
1567 1568 1569 1570 1571 1572
	int ret;

	stats->bs_nblocks = 0;

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

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

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

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

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

	/* success */
	return 0;

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

}

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

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

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

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

		/* create child node at level 1 */
		node = (struct nilfs_btree_node *)bh->b_data;
1641 1642 1643
		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 已提交
1644 1645
		if (!buffer_dirty(bh))
			nilfs_btnode_mark_dirty(bh);
1646 1647
		if (!nilfs_bmap_dirty(btree))
			nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1648

1649
		brelse(bh);
K
Koji Sato 已提交
1650 1651 1652 1653

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

		/* create root node at level 1 */
		node = nilfs_btree_get_root(btree);
1662 1663 1664 1665 1666
		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);
1667 1668
		if (!nilfs_bmap_dirty(btree))
			nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1669 1670
	}

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

/**
 * nilfs_btree_convert_and_insert -
 * @bmap:
 * @key:
 * @ptr:
 * @keys:
 * @ptrs:
 * @n:
 */
1684
int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1685
				   __u64 key, __u64 ptr,
1686
				   const __u64 *keys, const __u64 *ptrs, int n)
K
Koji Sato 已提交
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
{
	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(
1697
			   1 << btree->b_inode->i_blkbits)) {
K
Koji Sato 已提交
1698 1699 1700 1701 1702 1703 1704 1705
		di = &dreq;
		ni = &nreq;
	} else {
		di = NULL;
		ni = NULL;
		BUG();
	}

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

1716
static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
				   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;
}

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

1735
	parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
K
Koji Sato 已提交
1736
	path[level].bp_oldreq.bpr_ptr =
1737 1738
		nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
					 ncmax);
K
Koji Sato 已提交
1739
	path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
1740 1741
	ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
				       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1742 1743 1744 1745 1746 1747 1748 1749
	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(
1750
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
1751 1752
			&path[level].bp_ctxt);
		if (ret < 0) {
1753 1754 1755
			nilfs_dat_abort_update(dat,
					       &path[level].bp_oldreq.bpr_req,
					       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1756 1757 1758 1759 1760 1761 1762
			return ret;
		}
	}

	return 0;
}

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

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

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

1782 1783 1784
	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 已提交
1785 1786
}

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

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

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

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

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

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

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

	if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
1843
		nilfs_btree_commit_update_v(btree, path, minlevel, dat);
K
Koji Sato 已提交
1844 1845

	for (level = minlevel + 1; level <= maxlevel; level++)
1846
		nilfs_btree_commit_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1847 1848
}

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

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

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

1876
	nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
K
Koji Sato 已提交
1877 1878 1879 1880 1881 1882 1883

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

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

1892
	WARN_ON(!buffer_dirty(bh));
K
Koji Sato 已提交
1893

1894
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1895 1896 1897 1898 1899
	if (path == NULL)
		return -ENOMEM;

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

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

1915
	ret = NILFS_BMAP_USE_VBN(btree) ?
1916 1917
		nilfs_btree_propagate_v(btree, path, level, bh) :
		nilfs_btree_propagate_p(btree, path, level, bh);
K
Koji Sato 已提交
1918 1919

 out:
1920
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1921 1922 1923 1924

	return ret;
}

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

1931
static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
					 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;
1943 1944
	key = nilfs_btree_node_get_key(node, 0);
	level = nilfs_btree_node_get_level(node);
1945 1946 1947 1948 1949 1950 1951
	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,
1952
		       NILFS_BMAP_I(btree)->vfs_inode.i_ino,
1953 1954 1955 1956
		       (unsigned long long)bh->b_blocknr);
		return;
	}

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

1967
static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1968 1969
					     struct list_head *listp)
{
1970
	struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
K
Koji Sato 已提交
1971 1972 1973 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
	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++)
2001
		list_splice_tail(&lists[level], listp);
K
Koji Sato 已提交
2002 2003
}

2004
static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2005 2006 2007 2008 2009 2010 2011 2012 2013
				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;
2014
	int ncmax, ret;
K
Koji Sato 已提交
2015

2016 2017 2018
	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 已提交
2019 2020 2021 2022 2023
	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(
2024
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
2025 2026 2027 2028
			&path[level].bp_ctxt);
		if (ret < 0)
			return ret;
		nilfs_btnode_commit_change_key(
2029
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
2030 2031 2032 2033
			&path[level].bp_ctxt);
		*bh = path[level].bp_ctxt.bh;
	}

2034 2035
	nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
				 ncmax);
K
Koji Sato 已提交
2036

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

	return 0;
}

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

2059 2060 2061
	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 已提交
2062
	req.bpr_ptr = ptr;
2063 2064
	ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
	if (ret < 0)
K
Koji Sato 已提交
2065
		return ret;
2066
	nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
K
Koji Sato 已提交
2067

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

	return 0;
}

2076
static int nilfs_btree_assign(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2077 2078 2079 2080 2081 2082 2083 2084 2085
			      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;

2086
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2087 2088 2089 2090 2091
	if (path == NULL)
		return -ENOMEM;

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

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

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

 out:
2110
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2111 2112 2113 2114

	return ret;
}

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

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

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

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

	return 0;
}

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

2149
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2150 2151 2152 2153 2154
	if (path == NULL)
		return -ENOMEM;

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

	if (!buffer_dirty(bh))
		nilfs_btnode_mark_dirty(bh);
2166
	brelse(bh);
2167 2168
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
2169 2170

 out:
2171
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2172 2173 2174 2175 2176
	return ret;
}

static const struct nilfs_bmap_operations nilfs_btree_ops = {
	.bop_lookup		=	nilfs_btree_lookup,
2177
	.bop_lookup_contig	=	nilfs_btree_lookup_contig,
K
Koji Sato 已提交
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	.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,
2197
	.bop_lookup_contig	=	NULL,
K
Koji Sato 已提交
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
	.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,
};

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

void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
{
	bmap->b_ops = &nilfs_btree_ops_gc;
2226 2227
	bmap->b_nchildren_per_block =
		NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
K
Koji Sato 已提交
2228
}