btree.c 58.6 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

/**
 * struct nilfs_btree_path - A path on which B-tree operations are executed
 * @bp_bh: buffer head of node block
 * @bp_sib_bh: buffer head of sibling node block
 * @bp_index: index of child node
 * @bp_oldreq: ptr end request for old ptr
 * @bp_newreq: ptr alloc request for new ptr
 * @bp_op: rebalance operation
 */
struct nilfs_btree_path {
	struct buffer_head *bp_bh;
	struct buffer_head *bp_sib_bh;
	int bp_index;
	union nilfs_bmap_ptr_req bp_oldreq;
	union nilfs_bmap_ptr_req bp_newreq;
	struct nilfs_btnode_chkey_ctxt bp_ctxt;
	void (*bp_op)(struct nilfs_btree *, struct nilfs_btree_path *,
		      int, __u64 *, __u64 *);
};

/*
 * B-tree path operations
 */

static struct kmem_cache *nilfs_btree_path_cache;

int __init nilfs_btree_path_cache_init(void)
{
	nilfs_btree_path_cache =
		kmem_cache_create("nilfs2_btree_path_cache",
				  sizeof(struct nilfs_btree_path) *
				  NILFS_BTREE_LEVEL_MAX, 0, 0, NULL);
	return (nilfs_btree_path_cache != NULL) ? 0 : -ENOMEM;
}

void nilfs_btree_path_cache_destroy(void)
{
	kmem_cache_destroy(nilfs_btree_path_cache);
}

74
static inline struct nilfs_btree_path *nilfs_btree_alloc_path(void)
K
Koji Sato 已提交
75
{
76
	return kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
K
Koji Sato 已提交
77 78
}

79
static inline void nilfs_btree_free_path(struct nilfs_btree_path *path)
K
Koji Sato 已提交
80 81 82 83
{
	kmem_cache_free(nilfs_btree_path_cache, path);
}

84
static void nilfs_btree_init_path(struct nilfs_btree_path *path)
K
Koji Sato 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
	int level;

	for (level = NILFS_BTREE_LEVEL_DATA;
	     level < NILFS_BTREE_LEVEL_MAX;
	     level++) {
		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;
	}
}

100
static void nilfs_btree_release_path(struct nilfs_btree_path *path)
K
Koji Sato 已提交
101 102 103
{
	int level;

104 105 106
	for (level = NILFS_BTREE_LEVEL_DATA; level < NILFS_BTREE_LEVEL_MAX;
	     level++)
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
107 108 109 110 111
}

/*
 * B-tree node operations
 */
112 113 114 115 116
static int nilfs_btree_get_block(const struct nilfs_btree *btree, __u64 ptr,
				 struct buffer_head **bhp)
{
	struct address_space *btnc =
		&NILFS_BMAP_I((struct nilfs_bmap *)btree)->i_btnode_cache;
117 118 119 120 121 122 123 124 125 126 127 128
	int err;

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

	wait_on_buffer(*bhp);
	if (!buffer_uptodate(*bhp)) {
		brelse(*bhp);
		return -EIO;
	}
	return 0;
129 130 131 132 133 134 135
}

static int nilfs_btree_get_new_block(const struct nilfs_btree *btree,
				     __u64 ptr, struct buffer_head **bhp)
{
	struct address_space *btnc =
		&NILFS_BMAP_I((struct nilfs_bmap *)btree)->i_btnode_cache;
136
	struct buffer_head *bh;
137

138 139 140 141 142 143 144
	bh = nilfs_btnode_create_block(btnc, ptr);
	if (!bh)
		return -ENOMEM;

	set_buffer_nilfs_volatile(bh);
	*bhp = bh;
	return 0;
145
}
K
Koji Sato 已提交
146 147

static inline int
148
nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
149 150 151 152 153
{
	return node->bn_flags;
}

static inline void
154
nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
K
Koji Sato 已提交
155 156 157 158
{
	node->bn_flags = flags;
}

159
static inline int nilfs_btree_node_root(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
160
{
161
	return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
K
Koji Sato 已提交
162 163 164
}

static inline int
165
nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
166 167 168 169 170
{
	return node->bn_level;
}

static inline void
171
nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
K
Koji Sato 已提交
172 173 174 175 176
{
	node->bn_level = level;
}

static inline int
177
nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
178 179 180 181 182
{
	return le16_to_cpu(node->bn_nchildren);
}

static inline void
183
nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
K
Koji Sato 已提交
184 185 186 187
{
	node->bn_nchildren = cpu_to_le16(nchildren);
}

188
static inline int nilfs_btree_node_size(const struct nilfs_btree *btree)
K
Koji Sato 已提交
189 190 191 192 193
{
	return 1 << btree->bt_bmap.b_inode->i_blkbits;
}

static inline int
194 195
nilfs_btree_node_nchildren_min(const struct nilfs_btree_node *node,
			       const struct nilfs_btree *btree)
K
Koji Sato 已提交
196
{
197
	return nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
198 199 200 201 202
		NILFS_BTREE_ROOT_NCHILDREN_MIN :
		NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
}

static inline int
203 204
nilfs_btree_node_nchildren_max(const struct nilfs_btree_node *node,
			       const struct nilfs_btree *btree)
K
Koji Sato 已提交
205
{
206
	return nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
207 208 209 210 211
		NILFS_BTREE_ROOT_NCHILDREN_MAX :
		NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree));
}

static inline __le64 *
212
nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
213 214
{
	return (__le64 *)((char *)(node + 1) +
215
			  (nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
216 217 218 219
			   0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
}

static inline __le64 *
220 221
nilfs_btree_node_dptrs(const struct nilfs_btree_node *node,
		       const struct nilfs_btree *btree)
K
Koji Sato 已提交
222
{
223 224
	return (__le64 *)(nilfs_btree_node_dkeys(node) +
			  nilfs_btree_node_nchildren_max(node, btree));
K
Koji Sato 已提交
225 226 227
}

static inline __u64
228
nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
K
Koji Sato 已提交
229
{
230
	return nilfs_bmap_dkey_to_key(*(nilfs_btree_node_dkeys(node) + index));
K
Koji Sato 已提交
231 232 233
}

static inline void
234
nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
K
Koji Sato 已提交
235
{
236
	*(nilfs_btree_node_dkeys(node) + index) = nilfs_bmap_key_to_dkey(key);
K
Koji Sato 已提交
237 238 239 240
}

static inline __u64
nilfs_btree_node_get_ptr(const struct nilfs_btree *btree,
241
			 const struct nilfs_btree_node *node, int index)
K
Koji Sato 已提交
242
{
243
	return nilfs_bmap_dptr_to_ptr(*(nilfs_btree_node_dptrs(node, btree) +
K
Koji Sato 已提交
244 245 246 247 248
					index));
}

static inline void
nilfs_btree_node_set_ptr(struct nilfs_btree *btree,
249
			 struct nilfs_btree_node *node, int index, __u64 ptr)
K
Koji Sato 已提交
250
{
251
	*(nilfs_btree_node_dptrs(node, btree) + index) =
K
Koji Sato 已提交
252 253 254 255 256 257 258 259 260 261 262 263
		nilfs_bmap_ptr_to_dptr(ptr);
}

static void nilfs_btree_node_init(struct nilfs_btree *btree,
				  struct nilfs_btree_node *node,
				  int flags, int level, int nchildren,
				  const __u64 *keys, const __u64 *ptrs)
{
	__le64 *dkeys;
	__le64 *dptrs;
	int i;

264 265 266
	nilfs_btree_node_set_flags(node, flags);
	nilfs_btree_node_set_level(node, level);
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
267

268 269
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
K
Koji Sato 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	for (i = 0; i < nchildren; i++) {
		dkeys[i] = nilfs_bmap_key_to_dkey(keys[i]);
		dptrs[i] = nilfs_bmap_ptr_to_dptr(ptrs[i]);
	}
}

/* Assume the buffer heads corresponding to left and right are locked. */
static void nilfs_btree_node_move_left(struct nilfs_btree *btree,
				       struct nilfs_btree_node *left,
				       struct nilfs_btree_node *right,
				       int n)
{
	__le64 *ldkeys, *rdkeys;
	__le64 *ldptrs, *rdptrs;
	int lnchildren, rnchildren;

286 287 288
	ldkeys = nilfs_btree_node_dkeys(left);
	ldptrs = nilfs_btree_node_dptrs(left, btree);
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
289

290 291 292
	rdkeys = nilfs_btree_node_dkeys(right);
	rdptrs = nilfs_btree_node_dptrs(right, btree);
	rnchildren = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
293 294 295 296 297 298 299 300

	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;
301 302
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
303 304 305 306 307 308 309 310 311 312 313 314
}

/* Assume that the buffer heads corresponding to left and right are locked. */
static void nilfs_btree_node_move_right(struct nilfs_btree *btree,
					struct nilfs_btree_node *left,
					struct nilfs_btree_node *right,
					int n)
{
	__le64 *ldkeys, *rdkeys;
	__le64 *ldptrs, *rdptrs;
	int lnchildren, rnchildren;

315 316 317
	ldkeys = nilfs_btree_node_dkeys(left);
	ldptrs = nilfs_btree_node_dptrs(left, btree);
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
318

319 320 321
	rdkeys = nilfs_btree_node_dkeys(right);
	rdptrs = nilfs_btree_node_dptrs(right, btree);
	rnchildren = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
322 323 324 325 326 327 328 329

	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;
330 331
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
332 333 334 335 336 337 338 339 340 341 342
}

/* Assume that the buffer head corresponding to node is locked. */
static void nilfs_btree_node_insert(struct nilfs_btree *btree,
				    struct nilfs_btree_node *node,
				    __u64 key, __u64 ptr, int index)
{
	__le64 *dkeys;
	__le64 *dptrs;
	int nchildren;

343 344 345
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
346 347 348 349 350 351 352 353 354
	if (index < nchildren) {
		memmove(dkeys + index + 1, dkeys + index,
			(nchildren - index) * sizeof(*dkeys));
		memmove(dptrs + index + 1, dptrs + index,
			(nchildren - index) * sizeof(*dptrs));
	}
	dkeys[index] = nilfs_bmap_key_to_dkey(key);
	dptrs[index] = nilfs_bmap_ptr_to_dptr(ptr);
	nchildren++;
355
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368
}

/* Assume that the buffer head corresponding to node is locked. */
static void nilfs_btree_node_delete(struct nilfs_btree *btree,
				    struct nilfs_btree_node *node,
				    __u64 *keyp, __u64 *ptrp, int index)
{
	__u64 key;
	__u64 ptr;
	__le64 *dkeys;
	__le64 *dptrs;
	int nchildren;

369 370
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
K
Koji Sato 已提交
371 372
	key = nilfs_bmap_dkey_to_key(dkeys[index]);
	ptr = nilfs_bmap_dptr_to_ptr(dptrs[index]);
373
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
374 375 376 377 378 379 380 381 382 383 384 385
	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--;
386
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
387 388
}

389
static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
K
Koji Sato 已提交
390 391 392 393 394 395 396
				   __u64 key, int *indexp)
{
	__u64 nkey;
	int index, low, high, s;

	/* binary search */
	low = 0;
397
	high = nilfs_btree_node_get_nchildren(node) - 1;
K
Koji Sato 已提交
398 399 400 401
	index = 0;
	s = 0;
	while (low <= high) {
		index = (low + high) / 2;
402
		nkey = nilfs_btree_node_get_key(node, index);
K
Koji Sato 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415
		if (nkey == key) {
			s = 0;
			goto out;
		} else if (nkey < key) {
			low = index + 1;
			s = -1;
		} else {
			high = index - 1;
			s = 1;
		}
	}

	/* adjust index */
416 417
	if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
		if (s > 0 && index > 0)
K
Koji Sato 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
			index--;
	} else if (s < 0)
		index++;

 out:
	*indexp = index;

	return s == 0;
}

static inline struct nilfs_btree_node *
nilfs_btree_get_root(const struct nilfs_btree *btree)
{
	return (struct nilfs_btree_node *)btree->bt_bmap.b_u.u_data;
}

static inline struct nilfs_btree_node *
435
nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
K
Koji Sato 已提交
436 437 438 439 440
{
	return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
}

static inline struct nilfs_btree_node *
441
nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
K
Koji Sato 已提交
442 443 444 445 446 447
{
	return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
}

static inline int nilfs_btree_height(const struct nilfs_btree *btree)
{
448
	return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
K
Koji Sato 已提交
449 450 451 452 453 454 455 456 457
}

static inline struct nilfs_btree_node *
nilfs_btree_get_node(const struct nilfs_btree *btree,
		     const struct nilfs_btree_path *path,
		     int level)
{
	return (level == nilfs_btree_height(btree) - 1) ?
		nilfs_btree_get_root(btree) :
458
		nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
459 460
}

461 462 463 464 465 466 467 468 469 470 471 472
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;
}

K
Koji Sato 已提交
473 474 475 476 477 478 479 480 481
static int nilfs_btree_do_lookup(const struct nilfs_btree *btree,
				 struct nilfs_btree_path *path,
				 __u64 key, __u64 *ptrp, int minlevel)
{
	struct nilfs_btree_node *node;
	__u64 ptr;
	int level, index, found, ret;

	node = nilfs_btree_get_root(btree);
482 483
	level = nilfs_btree_node_get_level(node);
	if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
K
Koji Sato 已提交
484 485
		return -ENOENT;

486
	found = nilfs_btree_node_lookup(node, key, &index);
K
Koji Sato 已提交
487 488 489 490 491
	ptr = nilfs_btree_node_get_ptr(btree, node, index);
	path[level].bp_bh = NULL;
	path[level].bp_index = index;

	for (level--; level >= minlevel; level--) {
492
		ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
K
Koji Sato 已提交
493 494
		if (ret < 0)
			return ret;
495
		node = nilfs_btree_get_nonroot_node(path, level);
496 497
		if (nilfs_btree_bad_node(node, level))
			return -EINVAL;
K
Koji Sato 已提交
498
		if (!found)
499
			found = nilfs_btree_node_lookup(node, key, &index);
K
Koji Sato 已提交
500 501
		else
			index = 0;
502
		if (index < nilfs_btree_node_nchildren_max(node, btree))
K
Koji Sato 已提交
503 504
			ptr = nilfs_btree_node_get_ptr(btree, node, index);
		else {
505
			WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
K
Koji Sato 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
			/* insert */
			ptr = NILFS_BMAP_INVALID_PTR;
		}
		path[level].bp_index = index;
	}
	if (!found)
		return -ENOENT;

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

	return 0;
}

static int nilfs_btree_do_lookup_last(const struct nilfs_btree *btree,
				      struct nilfs_btree_path *path,
				      __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node;
	__u64 ptr;
	int index, level, ret;

	node = nilfs_btree_get_root(btree);
529
	index = nilfs_btree_node_get_nchildren(node) - 1;
K
Koji Sato 已提交
530 531
	if (index < 0)
		return -ENOENT;
532
	level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
533 534 535 536 537
	ptr = nilfs_btree_node_get_ptr(btree, node, index);
	path[level].bp_bh = NULL;
	path[level].bp_index = index;

	for (level--; level > 0; level--) {
538
		ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
K
Koji Sato 已提交
539 540
		if (ret < 0)
			return ret;
541
		node = nilfs_btree_get_nonroot_node(path, level);
542 543
		if (nilfs_btree_bad_node(node, level))
			return -EINVAL;
544
		index = nilfs_btree_node_get_nchildren(node) - 1;
K
Koji Sato 已提交
545 546 547 548 549
		ptr = nilfs_btree_node_get_ptr(btree, node, index);
		path[level].bp_index = index;
	}

	if (keyp != NULL)
550
		*keyp = nilfs_btree_node_get_key(node, index);
K
Koji Sato 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	if (ptrp != NULL)
		*ptrp = ptr;

	return 0;
}

static int nilfs_btree_lookup(const struct nilfs_bmap *bmap,
			      __u64 key, int level, __u64 *ptrp)
{
	struct nilfs_btree *btree;
	struct nilfs_btree_path *path;
	__u64 ptr;
	int ret;

	btree = (struct nilfs_btree *)bmap;
566
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
567 568
	if (path == NULL)
		return -ENOMEM;
569
	nilfs_btree_init_path(path);
K
Koji Sato 已提交
570 571 572 573 574 575

	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level);

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

576
	nilfs_btree_release_path(path);
577
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
578 579 580 581

	return ret;
}

582 583 584 585 586 587 588 589 590 591 592 593
static int nilfs_btree_lookup_contig(const struct nilfs_bmap *bmap,
				     __u64 key, __u64 *ptrp, unsigned maxblocks)
{
	struct nilfs_btree *btree = (struct nilfs_btree *)bmap;
	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;
	int ret, cnt, index, maxlevel;

594
	path = nilfs_btree_alloc_path();
595 596
	if (path == NULL)
		return -ENOMEM;
597
	nilfs_btree_init_path(path);
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level);
	if (ret < 0)
		goto out;

	if (NILFS_BMAP_USE_VBN(bmap)) {
		dat = nilfs_bmap_get_dat(bmap);
		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;
	node = nilfs_btree_get_node(btree, path, level);
	index = path[level].bp_index + 1;
	for (;;) {
617 618
		while (index < nilfs_btree_node_get_nchildren(node)) {
			if (nilfs_btree_node_get_key(node, index) !=
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
			    key + cnt)
				goto end;
			ptr2 = nilfs_btree_node_get_ptr(btree, node, index);
			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 */
		node = nilfs_btree_get_node(btree, path, level + 1);
		index = path[level + 1].bp_index + 1;
639 640
		if (index >= nilfs_btree_node_get_nchildren(node) ||
		    nilfs_btree_node_get_key(node, index) != key + cnt)
641 642 643 644 645 646 647 648 649
			break;
		ptr2 = nilfs_btree_node_get_ptr(btree, node, index);
		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;
650
		node = nilfs_btree_get_nonroot_node(path, level);
651 652 653 654 655 656 657
		index = 0;
		path[level].bp_index = index;
	}
 end:
	*ptrp = ptr;
	ret = cnt;
 out:
658
	nilfs_btree_release_path(path);
659
	nilfs_btree_free_path(path);
660 661 662
	return ret;
}

K
Koji Sato 已提交
663 664 665 666 667 668 669
static void nilfs_btree_promote_key(struct nilfs_btree *btree,
				    struct nilfs_btree_path *path,
				    int level, __u64 key)
{
	if (level < nilfs_btree_height(btree) - 1) {
		do {
			nilfs_btree_node_set_key(
670
				nilfs_btree_get_nonroot_node(path, level),
K
Koji Sato 已提交
671 672 673 674 675 676 677 678 679
				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) {
680
		nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
K
Koji Sato 已提交
681 682 683 684 685 686 687 688 689 690 691
					 path[level].bp_index, key);
	}
}

static void nilfs_btree_do_insert(struct nilfs_btree *btree,
				  struct nilfs_btree_path *path,
				  int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node;

	if (level < nilfs_btree_height(btree) - 1) {
692
		node = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
693 694 695 696 697 698 699
		nilfs_btree_node_insert(btree, node, *keyp, *ptrp,
					path[level].bp_index);
		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,
700 701
						nilfs_btree_node_get_key(node,
									 0));
K
Koji Sato 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715
	} else {
		node = nilfs_btree_get_root(btree);
		nilfs_btree_node_insert(btree, node, *keyp, *ptrp,
					path[level].bp_index);
	}
}

static void nilfs_btree_carry_left(struct nilfs_btree *btree,
				   struct nilfs_btree_path *path,
				   int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *left;
	int nchildren, lnchildren, n, move;

716 717 718 719
	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);
K
Koji Sato 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	move = 0;

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

	nilfs_btree_node_move_left(btree, left, node, n);

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

	if (move) {
740
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
741 742 743 744 745
		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 {
746
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760
		path[level].bp_sib_bh = NULL;
		path[level].bp_index -= n;
	}

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

static void nilfs_btree_carry_right(struct nilfs_btree *btree,
				    struct nilfs_btree_path *path,
				    int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
	int nchildren, rnchildren, n, move;

761 762 763 764
	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);
K
Koji Sato 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	move = 0;

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

	nilfs_btree_node_move_right(btree, node, right, n);

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

	if (move) {
787
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
788 789
		path[level].bp_bh = path[level].bp_sib_bh;
		path[level].bp_sib_bh = NULL;
790
		path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
791 792
		path[level + 1].bp_index++;
	} else {
793
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
		path[level].bp_sib_bh = NULL;
	}

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

static void nilfs_btree_split(struct nilfs_btree *btree,
			      struct nilfs_btree_path *path,
			      int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
	__u64 newkey;
	__u64 newptr;
	int nchildren, n, move;

809 810 811
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	move = 0;

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

	nilfs_btree_node_move_right(btree, node, right, n);

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

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

	if (move) {
831
		path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
832 833 834
		nilfs_btree_node_insert(btree, right, *keyp, *ptrp,
					path[level].bp_index);

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

838
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
839 840 841 842 843
		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);

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

847
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861
		path[level].bp_sib_bh = NULL;
	}

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

static void nilfs_btree_grow(struct nilfs_btree *btree,
			     struct nilfs_btree_path *path,
			     int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *root, *child;
	int n;

	root = nilfs_btree_get_root(btree);
862
	child = nilfs_btree_get_sib_node(path, level);
K
Koji Sato 已提交
863

864
	n = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
865 866

	nilfs_btree_node_move_right(btree, root, child, n);
867
	nilfs_btree_node_set_level(root, level + 1);
K
Koji Sato 已提交
868 869 870 871 872 873 874 875 876

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

877
	*keyp = nilfs_btree_node_get_key(child, 0);
K
Koji Sato 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	*ptrp = path[level].bp_newreq.bpr_ptr;
}

static __u64 nilfs_btree_find_near(const struct nilfs_btree *btree,
				   const struct nilfs_btree_path *path)
{
	struct nilfs_btree_node *node;
	int level;

	if (path == NULL)
		return NILFS_BMAP_INVALID_PTR;

	/* left sibling */
	level = NILFS_BTREE_LEVEL_NODE_MIN;
	if (path[level].bp_index > 0) {
		node = nilfs_btree_get_node(btree, path, level);
		return nilfs_btree_node_get_ptr(btree, node,
						path[level].bp_index - 1);
	}

	/* parent */
	level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
	if (level <= nilfs_btree_height(btree) - 1) {
		node = nilfs_btree_get_node(btree, path, level);
		return nilfs_btree_node_get_ptr(btree, node,
						path[level].bp_index);
	}

	return NILFS_BMAP_INVALID_PTR;
}

static __u64 nilfs_btree_find_target_v(const struct nilfs_btree *btree,
				       const struct nilfs_btree_path *path,
				       __u64 key)
{
	__u64 ptr;

	ptr = nilfs_bmap_find_target_seq(&btree->bt_bmap, key);
	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 */
	return nilfs_bmap_find_target_in_group(&btree->bt_bmap);
}

static void nilfs_btree_set_target_v(struct nilfs_btree *btree, __u64 key,
				     __u64 ptr)
{
	btree->bt_bmap.b_last_allocated_key = key;
	btree->bt_bmap.b_last_allocated_ptr = ptr;
}

static int nilfs_btree_prepare_insert(struct nilfs_btree *btree,
				      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;
	int pindex, level, ret;
945
	struct inode *dat = NULL;
K
Koji Sato 已提交
946 947 948 949 950

	stats->bs_nblocks = 0;
	level = NILFS_BTREE_LEVEL_DATA;

	/* allocate a new ptr for data block */
951
	if (NILFS_BMAP_USE_VBN(&btree->bt_bmap)) {
K
Koji Sato 已提交
952
		path[level].bp_newreq.bpr_ptr =
953
			nilfs_btree_find_target_v(btree, path, key);
954 955
		dat = nilfs_bmap_get_dat(&btree->bt_bmap);
	}
K
Koji Sato 已提交
956

957
	ret = nilfs_bmap_prepare_alloc_ptr(&btree->bt_bmap,
958
					   &path[level].bp_newreq, dat);
K
Koji Sato 已提交
959 960 961 962 963 964
	if (ret < 0)
		goto err_out_data;

	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < nilfs_btree_height(btree) - 1;
	     level++) {
965 966 967
		node = nilfs_btree_get_nonroot_node(path, level);
		if (nilfs_btree_node_get_nchildren(node) <
		    nilfs_btree_node_nchildren_max(node, btree)) {
K
Koji Sato 已提交
968 969 970 971 972 973 974 975 976 977 978 979
			path[level].bp_op = nilfs_btree_do_insert;
			stats->bs_nblocks++;
			goto out;
		}

		parent = nilfs_btree_get_node(btree, path, level + 1);
		pindex = path[level + 1].bp_index;

		/* left sibling */
		if (pindex > 0) {
			sibptr = nilfs_btree_node_get_ptr(btree, parent,
							  pindex - 1);
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 985
			if (nilfs_btree_node_get_nchildren(sib) <
			    nilfs_btree_node_nchildren_max(sib, btree)) {
K
Koji Sato 已提交
986 987 988 989 990
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_carry_left;
				stats->bs_nblocks++;
				goto out;
			} else
991
				brelse(bh);
K
Koji Sato 已提交
992 993 994 995
		}

		/* right sibling */
		if (pindex <
996
		    nilfs_btree_node_get_nchildren(parent) - 1) {
K
Koji Sato 已提交
997 998
			sibptr = nilfs_btree_node_get_ptr(btree, parent,
							  pindex + 1);
999
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1000 1001 1002
			if (ret < 0)
				goto err_out_child_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1003 1004
			if (nilfs_btree_node_get_nchildren(sib) <
			    nilfs_btree_node_nchildren_max(sib, btree)) {
K
Koji Sato 已提交
1005 1006 1007 1008 1009
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_carry_right;
				stats->bs_nblocks++;
				goto out;
			} else
1010
				brelse(bh);
K
Koji Sato 已提交
1011 1012 1013 1014 1015
		}

		/* split */
		path[level].bp_newreq.bpr_ptr =
			path[level - 1].bp_newreq.bpr_ptr + 1;
1016
		ret = nilfs_bmap_prepare_alloc_ptr(&btree->bt_bmap,
1017
						   &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1018 1019
		if (ret < 0)
			goto err_out_child_node;
1020 1021 1022
		ret = nilfs_btree_get_new_block(btree,
						path[level].bp_newreq.bpr_ptr,
						&bh);
K
Koji Sato 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		if (ret < 0)
			goto err_out_curr_node;

		stats->bs_nblocks++;

		nilfs_btree_node_init(btree,
				      (struct nilfs_btree_node *)bh->b_data,
				      0, level, 0, NULL, NULL);
		path[level].bp_sib_bh = bh;
		path[level].bp_op = nilfs_btree_split;
	}

	/* root */
	node = nilfs_btree_get_root(btree);
1037 1038
	if (nilfs_btree_node_get_nchildren(node) <
	    nilfs_btree_node_nchildren_max(node, btree)) {
K
Koji Sato 已提交
1039 1040 1041 1042 1043 1044 1045
		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;
1046
	ret = nilfs_bmap_prepare_alloc_ptr(&btree->bt_bmap,
1047
					   &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1048 1049
	if (ret < 0)
		goto err_out_child_node;
1050 1051
	ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
					&bh);
K
Koji Sato 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	if (ret < 0)
		goto err_out_curr_node;

	nilfs_btree_node_init(btree, (struct nilfs_btree_node *)bh->b_data,
			      0, level, 0, NULL, NULL);
	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:
1073 1074
	nilfs_bmap_abort_alloc_ptr(&btree->bt_bmap, &path[level].bp_newreq,
				   dat);
K
Koji Sato 已提交
1075 1076
 err_out_child_node:
	for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
1077
		nilfs_btnode_delete(path[level].bp_sib_bh);
1078
		nilfs_bmap_abort_alloc_ptr(&btree->bt_bmap,
1079
					   &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1080 1081 1082

	}

1083 1084
	nilfs_bmap_abort_alloc_ptr(&btree->bt_bmap, &path[level].bp_newreq,
				   dat);
K
Koji Sato 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
 err_out_data:
	*levelp = level;
	stats->bs_nblocks = 0;
	return ret;
}

static void nilfs_btree_commit_insert(struct nilfs_btree *btree,
				      struct nilfs_btree_path *path,
				      int maxlevel, __u64 key, __u64 ptr)
{
1095
	struct inode *dat = NULL;
K
Koji Sato 已提交
1096 1097 1098 1099
	int level;

	set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
	ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
1100
	if (NILFS_BMAP_USE_VBN(&btree->bt_bmap)) {
1101
		nilfs_btree_set_target_v(btree, key, ptr);
1102 1103
		dat = nilfs_bmap_get_dat(&btree->bt_bmap);
	}
K
Koji Sato 已提交
1104 1105

	for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
1106
		nilfs_bmap_commit_alloc_ptr(&btree->bt_bmap,
1107
					    &path[level - 1].bp_newreq, dat);
1108
		path[level].bp_op(btree, path, level, &key, &ptr);
K
Koji Sato 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	}

	if (!nilfs_bmap_dirty(&btree->bt_bmap))
		nilfs_bmap_set_dirty(&btree->bt_bmap);
}

static int nilfs_btree_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
{
	struct nilfs_btree *btree;
	struct nilfs_btree_path *path;
	struct nilfs_bmap_stats stats;
	int level, ret;

	btree = (struct nilfs_btree *)bmap;
1123
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1124 1125
	if (path == NULL)
		return -ENOMEM;
1126
	nilfs_btree_init_path(path);
K
Koji Sato 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

	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);
	nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);

 out:
1143
	nilfs_btree_release_path(path);
1144
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	return ret;
}

static void nilfs_btree_do_delete(struct nilfs_btree *btree,
				  struct nilfs_btree_path *path,
				  int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node;

	if (level < nilfs_btree_height(btree) - 1) {
1155
		node = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
1156 1157 1158 1159 1160 1161
		nilfs_btree_node_delete(btree, node, keyp, ptrp,
					path[level].bp_index);
		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,
1162
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	} else {
		node = nilfs_btree_get_root(btree);
		nilfs_btree_node_delete(btree, node, keyp, ptrp,
					path[level].bp_index);
	}
}

static void nilfs_btree_borrow_left(struct nilfs_btree *btree,
				    struct nilfs_btree_path *path,
				    int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *left;
	int nchildren, lnchildren, n;

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

1179 1180 1181 1182
	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);
K
Koji Sato 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

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

	nilfs_btree_node_move_right(btree, left, node, n);

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

1196
	brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	path[level].bp_sib_bh = NULL;
	path[level].bp_index += n;
}

static void nilfs_btree_borrow_right(struct nilfs_btree *btree,
				     struct nilfs_btree_path *path,
				     int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
	int nchildren, rnchildren, n;

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

1210 1211 1212 1213
	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);
K
Koji Sato 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

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

	nilfs_btree_node_move_left(btree, node, right, n);

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

1229
	brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	path[level].bp_sib_bh = NULL;
}

static void nilfs_btree_concat_left(struct nilfs_btree *btree,
				    struct nilfs_btree_path *path,
				    int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *left;
	int n;

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

1242 1243
	node = nilfs_btree_get_nonroot_node(path, level);
	left = nilfs_btree_get_sib_node(path, level);
K
Koji Sato 已提交
1244

1245
	n = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
1246 1247 1248 1249 1250 1251

	nilfs_btree_node_move_left(btree, left, node, n);

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

1252
	nilfs_btnode_delete(path[level].bp_bh);
K
Koji Sato 已提交
1253 1254
	path[level].bp_bh = path[level].bp_sib_bh;
	path[level].bp_sib_bh = NULL;
1255
	path[level].bp_index += nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
}

static void nilfs_btree_concat_right(struct nilfs_btree *btree,
				     struct nilfs_btree_path *path,
				     int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
	int n;

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

1267 1268
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
K
Koji Sato 已提交
1269

1270
	n = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
1271 1272 1273 1274 1275 1276

	nilfs_btree_node_move_left(btree, node, right, n);

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

1277
	nilfs_btnode_delete(path[level].bp_sib_bh);
K
Koji Sato 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
	path[level].bp_sib_bh = NULL;
	path[level + 1].bp_index++;
}

static void nilfs_btree_shrink(struct nilfs_btree *btree,
			       struct nilfs_btree_path *path,
			       int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *root, *child;
	int n;

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

	root = nilfs_btree_get_root(btree);
1292
	child = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
1293 1294

	nilfs_btree_node_delete(btree, root, NULL, NULL, 0);
1295 1296
	nilfs_btree_node_set_level(root, level);
	n = nilfs_btree_node_get_nchildren(child);
K
Koji Sato 已提交
1297 1298
	nilfs_btree_node_move_left(btree, root, child, n);

1299
	nilfs_btnode_delete(path[level].bp_bh);
K
Koji Sato 已提交
1300 1301 1302 1303 1304 1305 1306
	path[level].bp_bh = NULL;
}


static int nilfs_btree_prepare_delete(struct nilfs_btree *btree,
				      struct nilfs_btree_path *path,
				      int *levelp,
1307 1308
				      struct nilfs_bmap_stats *stats,
				      struct inode *dat)
K
Koji Sato 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
{
	struct buffer_head *bh;
	struct nilfs_btree_node *node, *parent, *sib;
	__u64 sibptr;
	int pindex, level, ret;

	ret = 0;
	stats->bs_nblocks = 0;
	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < nilfs_btree_height(btree) - 1;
	     level++) {
1320
		node = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
1321 1322 1323
		path[level].bp_oldreq.bpr_ptr =
			nilfs_btree_node_get_ptr(btree, node,
						 path[level].bp_index);
1324
		ret = nilfs_bmap_prepare_end_ptr(&btree->bt_bmap,
1325
						 &path[level].bp_oldreq, dat);
1326 1327
		if (ret < 0)
			goto err_out_child_node;
K
Koji Sato 已提交
1328

1329 1330
		if (nilfs_btree_node_get_nchildren(node) >
		    nilfs_btree_node_nchildren_min(node, btree)) {
K
Koji Sato 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
			path[level].bp_op = nilfs_btree_do_delete;
			stats->bs_nblocks++;
			goto out;
		}

		parent = nilfs_btree_get_node(btree, path, level + 1);
		pindex = path[level + 1].bp_index;

		if (pindex > 0) {
			/* left sibling */
			sibptr = nilfs_btree_node_get_ptr(btree, parent,
							  pindex - 1);
1343
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1344 1345 1346
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1347 1348
			if (nilfs_btree_node_get_nchildren(sib) >
			    nilfs_btree_node_nchildren_min(sib, btree)) {
K
Koji Sato 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
				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 <
1360
			   nilfs_btree_node_get_nchildren(parent) - 1) {
K
Koji Sato 已提交
1361 1362 1363
			/* right sibling */
			sibptr = nilfs_btree_node_get_ptr(btree, parent,
							  pindex + 1);
1364
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1365 1366 1367
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1368 1369
			if (nilfs_btree_node_get_nchildren(sib) >
			    nilfs_btree_node_nchildren_min(sib, btree)) {
K
Koji Sato 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
				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 */
1383
			WARN_ON(level != nilfs_btree_height(btree) - 2);
1384
			if (nilfs_btree_node_get_nchildren(node) - 1 <=
K
Koji Sato 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
			    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 =
		nilfs_btree_node_get_ptr(btree, node, path[level].bp_index);
1401 1402

	ret = nilfs_bmap_prepare_end_ptr(&btree->bt_bmap,
1403
					 &path[level].bp_oldreq, dat);
1404 1405 1406
	if (ret < 0)
		goto err_out_child_node;

K
Koji Sato 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
	/* 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:
1418
	nilfs_bmap_abort_end_ptr(&btree->bt_bmap, &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1419 1420
 err_out_child_node:
	for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
1421
		brelse(path[level].bp_sib_bh);
1422
		nilfs_bmap_abort_end_ptr(&btree->bt_bmap,
1423
					 &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1424 1425 1426 1427 1428 1429 1430 1431
	}
	*levelp = level;
	stats->bs_nblocks = 0;
	return ret;
}

static void nilfs_btree_commit_delete(struct nilfs_btree *btree,
				      struct nilfs_btree_path *path,
1432
				      int maxlevel, struct inode *dat)
K
Koji Sato 已提交
1433 1434 1435 1436
{
	int level;

	for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
1437
		nilfs_bmap_commit_end_ptr(&btree->bt_bmap,
1438
					  &path[level].bp_oldreq, dat);
1439
		path[level].bp_op(btree, path, level, NULL, NULL);
K
Koji Sato 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	}

	if (!nilfs_bmap_dirty(&btree->bt_bmap))
		nilfs_bmap_set_dirty(&btree->bt_bmap);
}

static int nilfs_btree_delete(struct nilfs_bmap *bmap, __u64 key)

{
	struct nilfs_btree *btree;
	struct nilfs_btree_path *path;
	struct nilfs_bmap_stats stats;
1452
	struct inode *dat;
K
Koji Sato 已提交
1453 1454 1455
	int level, ret;

	btree = (struct nilfs_btree *)bmap;
1456
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1457 1458
	if (path == NULL)
		return -ENOMEM;
1459
	nilfs_btree_init_path(path);
K
Koji Sato 已提交
1460 1461 1462 1463 1464
	ret = nilfs_btree_do_lookup(btree, path, key, NULL,
				    NILFS_BTREE_LEVEL_NODE_MIN);
	if (ret < 0)
		goto out;

1465 1466 1467 1468 1469

	dat = NILFS_BMAP_USE_VBN(&btree->bt_bmap) ?
		nilfs_bmap_get_dat(&btree->bt_bmap) : NULL;

	ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
K
Koji Sato 已提交
1470 1471
	if (ret < 0)
		goto out;
1472
	nilfs_btree_commit_delete(btree, path, level, dat);
K
Koji Sato 已提交
1473 1474 1475
	nilfs_bmap_sub_blocks(bmap, stats.bs_nblocks);

out:
1476
	nilfs_btree_release_path(path);
1477
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	return ret;
}

static int nilfs_btree_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
{
	struct nilfs_btree *btree;
	struct nilfs_btree_path *path;
	int ret;

	btree = (struct nilfs_btree *)bmap;
1488
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1489 1490
	if (path == NULL)
		return -ENOMEM;
1491
	nilfs_btree_init_path(path);
K
Koji Sato 已提交
1492 1493 1494

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

1495
	nilfs_btree_release_path(path);
1496
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

	return ret;
}

static int nilfs_btree_check_delete(struct nilfs_bmap *bmap, __u64 key)
{
	struct buffer_head *bh;
	struct nilfs_btree *btree;
	struct nilfs_btree_node *root, *node;
	__u64 maxkey, nextmaxkey;
	__u64 ptr;
	int nchildren, ret;

	btree = (struct nilfs_btree *)bmap;
	root = nilfs_btree_get_root(btree);
	switch (nilfs_btree_height(btree)) {
	case 2:
		bh = NULL;
		node = root;
		break;
	case 3:
1518
		nchildren = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
1519 1520 1521
		if (nchildren > 1)
			return 0;
		ptr = nilfs_btree_node_get_ptr(btree, root, nchildren - 1);
1522
		ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
1523 1524 1525 1526 1527 1528 1529 1530
		if (ret < 0)
			return ret;
		node = (struct nilfs_btree_node *)bh->b_data;
		break;
	default:
		return 0;
	}

1531 1532
	nchildren = nilfs_btree_node_get_nchildren(node);
	maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
K
Koji Sato 已提交
1533
	nextmaxkey = (nchildren > 1) ?
1534
		nilfs_btree_node_get_key(node, nchildren - 2) : 0;
K
Koji Sato 已提交
1535
	if (bh != NULL)
1536
		brelse(bh);
K
Koji Sato 已提交
1537

1538
	return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
K
Koji Sato 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
}

static int nilfs_btree_gather_data(struct nilfs_bmap *bmap,
				   __u64 *keys, __u64 *ptrs, int nitems)
{
	struct buffer_head *bh;
	struct nilfs_btree *btree;
	struct nilfs_btree_node *node, *root;
	__le64 *dkeys;
	__le64 *dptrs;
	__u64 ptr;
	int nchildren, i, ret;

	btree = (struct nilfs_btree *)bmap;
	root = nilfs_btree_get_root(btree);
	switch (nilfs_btree_height(btree)) {
	case 2:
		bh = NULL;
		node = root;
		break;
	case 3:
1560
		nchildren = nilfs_btree_node_get_nchildren(root);
1561
		WARN_ON(nchildren > 1);
K
Koji Sato 已提交
1562
		ptr = nilfs_btree_node_get_ptr(btree, root, nchildren - 1);
1563
		ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
1564 1565 1566 1567 1568 1569
		if (ret < 0)
			return ret;
		node = (struct nilfs_btree_node *)bh->b_data;
		break;
	default:
		node = NULL;
1570
		return -EINVAL;
K
Koji Sato 已提交
1571 1572
	}

1573
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
1574 1575
	if (nchildren < nitems)
		nitems = nchildren;
1576 1577
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
K
Koji Sato 已提交
1578 1579 1580 1581 1582 1583
	for (i = 0; i < nitems; i++) {
		keys[i] = nilfs_bmap_dkey_to_key(dkeys[i]);
		ptrs[i] = nilfs_bmap_dptr_to_ptr(dptrs[i]);
	}

	if (bh != NULL)
1584
		brelse(bh);
K
Koji Sato 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596

	return nitems;
}

static int
nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *bmap, __u64 key,
				       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;
1597 1598
	struct nilfs_btree *btree = (struct nilfs_btree *)bmap;
	struct inode *dat = NULL;
K
Koji Sato 已提交
1599 1600 1601 1602 1603 1604
	int ret;

	stats->bs_nblocks = 0;

	/* for data */
	/* cannot find near ptr */
1605
	if (NILFS_BMAP_USE_VBN(bmap)) {
1606
		dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
1607 1608
		dat = nilfs_bmap_get_dat(bmap);
	}
1609

1610
	ret = nilfs_bmap_prepare_alloc_ptr(bmap, dreq, dat);
K
Koji Sato 已提交
1611 1612 1613 1614 1615 1616 1617
	if (ret < 0)
		return ret;

	*bhp = NULL;
	stats->bs_nblocks++;
	if (nreq != NULL) {
		nreq->bpr_ptr = dreq->bpr_ptr + 1;
1618
		ret = nilfs_bmap_prepare_alloc_ptr(bmap, nreq, dat);
K
Koji Sato 已提交
1619 1620 1621
		if (ret < 0)
			goto err_out_dreq;

1622
		ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
K
Koji Sato 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
		if (ret < 0)
			goto err_out_nreq;

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

	/* success */
	return 0;

	/* error */
 err_out_nreq:
1635
	nilfs_bmap_abort_alloc_ptr(bmap, nreq, dat);
K
Koji Sato 已提交
1636
 err_out_dreq:
1637
	nilfs_bmap_abort_alloc_ptr(bmap, dreq, dat);
K
Koji Sato 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646
	stats->bs_nblocks = 0;
	return ret;

}

static void
nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *bmap,
				      __u64 key, __u64 ptr,
				      const __u64 *keys, const __u64 *ptrs,
1647
				      int n,
K
Koji Sato 已提交
1648 1649 1650 1651
				      union nilfs_bmap_ptr_req *dreq,
				      union nilfs_bmap_ptr_req *nreq,
				      struct buffer_head *bh)
{
1652
	struct nilfs_btree *btree = (struct nilfs_btree *)bmap;
K
Koji Sato 已提交
1653
	struct nilfs_btree_node *node;
1654
	struct inode *dat;
K
Koji Sato 已提交
1655 1656 1657 1658
	__u64 tmpptr;

	/* free resources */
	if (bmap->b_ops->bop_clear != NULL)
1659
		bmap->b_ops->bop_clear(bmap);
K
Koji Sato 已提交
1660 1661 1662 1663 1664

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

	/* convert and insert */
1665
	dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
1666
	nilfs_btree_init(bmap);
K
Koji Sato 已提交
1667
	if (nreq != NULL) {
1668 1669
		nilfs_bmap_commit_alloc_ptr(bmap, dreq, dat);
		nilfs_bmap_commit_alloc_ptr(bmap, nreq, dat);
K
Koji Sato 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

		/* create child node at level 1 */
		node = (struct nilfs_btree_node *)bh->b_data;
		nilfs_btree_node_init(btree, node, 0, 1, n, keys, ptrs);
		nilfs_btree_node_insert(btree, node,
					key, dreq->bpr_ptr, n);
		if (!buffer_dirty(bh))
			nilfs_btnode_mark_dirty(bh);
		if (!nilfs_bmap_dirty(bmap))
			nilfs_bmap_set_dirty(bmap);

1681
		brelse(bh);
K
Koji Sato 已提交
1682 1683 1684 1685 1686 1687 1688

		/* create root node at level 2 */
		node = nilfs_btree_get_root(btree);
		tmpptr = nreq->bpr_ptr;
		nilfs_btree_node_init(btree, node, NILFS_BTREE_NODE_ROOT,
				      2, 1, &keys[0], &tmpptr);
	} else {
1689
		nilfs_bmap_commit_alloc_ptr(bmap, dreq, dat);
K
Koji Sato 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700

		/* create root node at level 1 */
		node = nilfs_btree_get_root(btree);
		nilfs_btree_node_init(btree, node, NILFS_BTREE_NODE_ROOT,
				      1, n, keys, ptrs);
		nilfs_btree_node_insert(btree, node,
					key, dreq->bpr_ptr, n);
		if (!nilfs_bmap_dirty(bmap))
			nilfs_bmap_set_dirty(bmap);
	}

1701 1702
	if (NILFS_BMAP_USE_VBN(bmap))
		nilfs_btree_set_target_v(btree, key, dreq->bpr_ptr);
K
Koji Sato 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
}

/**
 * nilfs_btree_convert_and_insert -
 * @bmap:
 * @key:
 * @ptr:
 * @keys:
 * @ptrs:
 * @n:
 */
int nilfs_btree_convert_and_insert(struct nilfs_bmap *bmap,
				   __u64 key, __u64 ptr,
1716
				   const __u64 *keys, const __u64 *ptrs, int n)
K
Koji Sato 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
{
	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(
			   1 << bmap->b_inode->i_blkbits)) {
		di = &dreq;
		ni = &nreq;
	} else {
		di = NULL;
		ni = NULL;
		BUG();
	}

	ret = nilfs_btree_prepare_convert_and_insert(bmap, key, di, ni, &bh,
						     &stats);
	if (ret < 0)
		return ret;
	nilfs_btree_commit_convert_and_insert(bmap, key, ptr, keys, ptrs, n,
1741
					      di, ni, bh);
K
Koji Sato 已提交
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	nilfs_bmap_add_blocks(bmap, stats.bs_nblocks);
	return 0;
}

static int nilfs_btree_propagate_p(struct nilfs_btree *btree,
				   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;
}

static int nilfs_btree_prepare_update_v(struct nilfs_btree *btree,
					struct nilfs_btree_path *path,
1760
					int level, struct inode *dat)
K
Koji Sato 已提交
1761 1762 1763 1764 1765 1766 1767 1768 1769
{
	struct nilfs_btree_node *parent;
	int ret;

	parent = nilfs_btree_get_node(btree, path, level + 1);
	path[level].bp_oldreq.bpr_ptr =
		nilfs_btree_node_get_ptr(btree, parent,
					 path[level + 1].bp_index);
	path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
1770 1771
	ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
				       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
	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(
			&NILFS_BMAP_I(&btree->bt_bmap)->i_btnode_cache,
			&path[level].bp_ctxt);
		if (ret < 0) {
1783 1784 1785
			nilfs_dat_abort_update(dat,
					       &path[level].bp_oldreq.bpr_req,
					       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794
			return ret;
		}
	}

	return 0;
}

static void nilfs_btree_commit_update_v(struct nilfs_btree *btree,
					struct nilfs_btree_path *path,
1795
					int level, struct inode *dat)
K
Koji Sato 已提交
1796 1797 1798
{
	struct nilfs_btree_node *parent;

1799 1800 1801
	nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
				&path[level].bp_newreq.bpr_req,
				btree->bt_bmap.b_ptr_type == NILFS_BMAP_PTR_VS);
K
Koji Sato 已提交
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817

	if (buffer_nilfs_node(path[level].bp_bh)) {
		nilfs_btnode_commit_change_key(
			&NILFS_BMAP_I(&btree->bt_bmap)->i_btnode_cache,
			&path[level].bp_ctxt);
		path[level].bp_bh = path[level].bp_ctxt.bh;
	}
	set_buffer_nilfs_volatile(path[level].bp_bh);

	parent = nilfs_btree_get_node(btree, path, level + 1);
	nilfs_btree_node_set_ptr(btree, parent, path[level + 1].bp_index,
				 path[level].bp_newreq.bpr_ptr);
}

static void nilfs_btree_abort_update_v(struct nilfs_btree *btree,
				       struct nilfs_btree_path *path,
1818
				       int level, struct inode *dat)
K
Koji Sato 已提交
1819
{
1820 1821
	nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
			       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1822 1823 1824 1825 1826 1827 1828 1829
	if (buffer_nilfs_node(path[level].bp_bh))
		nilfs_btnode_abort_change_key(
			&NILFS_BMAP_I(&btree->bt_bmap)->i_btnode_cache,
			&path[level].bp_ctxt);
}

static int nilfs_btree_prepare_propagate_v(struct nilfs_btree *btree,
					   struct nilfs_btree_path *path,
1830 1831
					   int minlevel, int *maxlevelp,
					   struct inode *dat)
K
Koji Sato 已提交
1832 1833 1834 1835 1836
{
	int level, ret;

	level = minlevel;
	if (!buffer_nilfs_volatile(path[level].bp_bh)) {
1837
		ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1838 1839 1840 1841 1842 1843
		if (ret < 0)
			return ret;
	}
	while ((++level < nilfs_btree_height(btree) - 1) &&
	       !buffer_dirty(path[level].bp_bh)) {

1844
		WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
1845
		ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
		if (ret < 0)
			goto out;
	}

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

	/* error */
 out:
	while (--level > minlevel)
1857
		nilfs_btree_abort_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1858
	if (!buffer_nilfs_volatile(path[level].bp_bh))
1859
		nilfs_btree_abort_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1860 1861 1862 1863 1864
	return ret;
}

static void nilfs_btree_commit_propagate_v(struct nilfs_btree *btree,
					   struct nilfs_btree_path *path,
1865 1866 1867
					   int minlevel, int maxlevel,
					   struct buffer_head *bh,
					   struct inode *dat)
K
Koji Sato 已提交
1868 1869 1870 1871
{
	int level;

	if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
1872
		nilfs_btree_commit_update_v(btree, path, minlevel, dat);
K
Koji Sato 已提交
1873 1874

	for (level = minlevel + 1; level <= maxlevel; level++)
1875
		nilfs_btree_commit_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1876 1877 1878 1879
}

static int nilfs_btree_propagate_v(struct nilfs_btree *btree,
				   struct nilfs_btree_path *path,
1880
				   int level, struct buffer_head *bh)
K
Koji Sato 已提交
1881
{
1882
	int maxlevel = 0, ret;
K
Koji Sato 已提交
1883
	struct nilfs_btree_node *parent;
1884
	struct inode *dat = nilfs_bmap_get_dat(&btree->bt_bmap);
K
Koji Sato 已提交
1885 1886 1887 1888
	__u64 ptr;

	get_bh(bh);
	path[level].bp_bh = bh;
1889 1890
	ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
					      dat);
K
Koji Sato 已提交
1891 1892 1893 1894 1895 1896 1897
	if (ret < 0)
		goto out;

	if (buffer_nilfs_volatile(path[level].bp_bh)) {
		parent = nilfs_btree_get_node(btree, path, level + 1);
		ptr = nilfs_btree_node_get_ptr(btree, parent,
					       path[level + 1].bp_index);
1898
		ret = nilfs_dat_mark_dirty(dat, ptr);
K
Koji Sato 已提交
1899 1900 1901 1902
		if (ret < 0)
			goto out;
	}

1903
	nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
K
Koji Sato 已提交
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919

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

static int nilfs_btree_propagate(const struct nilfs_bmap *bmap,
				 struct buffer_head *bh)
{
	struct nilfs_btree *btree;
	struct nilfs_btree_path *path;
	struct nilfs_btree_node *node;
	__u64 key;
	int level, ret;

1920
	WARN_ON(!buffer_dirty(bh));
K
Koji Sato 已提交
1921 1922

	btree = (struct nilfs_btree *)bmap;
1923
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1924 1925
	if (path == NULL)
		return -ENOMEM;
1926
	nilfs_btree_init_path(path);
K
Koji Sato 已提交
1927 1928 1929

	if (buffer_nilfs_node(bh)) {
		node = (struct nilfs_btree_node *)bh->b_data;
1930 1931
		key = nilfs_btree_node_get_key(node, 0);
		level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
1932 1933 1934 1935 1936 1937 1938
	} else {
		key = nilfs_bmap_data_get_key(bmap, bh);
		level = NILFS_BTREE_LEVEL_DATA;
	}

	ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
	if (ret < 0) {
1939
		if (unlikely(ret == -ENOENT))
K
Koji Sato 已提交
1940 1941 1942 1943 1944
			printk(KERN_CRIT "%s: key = %llu, level == %d\n",
			       __func__, (unsigned long long)key, level);
		goto out;
	}

1945 1946 1947
	ret = NILFS_BMAP_USE_VBN(bmap) ?
		nilfs_btree_propagate_v(btree, path, level, bh) :
		nilfs_btree_propagate_p(btree, path, level, bh);
K
Koji Sato 已提交
1948 1949

 out:
1950
	nilfs_btree_release_path(path);
1951
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1952 1953 1954 1955 1956 1957 1958

	return ret;
}

static int nilfs_btree_propagate_gc(const struct nilfs_bmap *bmap,
				    struct buffer_head *bh)
{
1959
	return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(bmap), bh->b_blocknr);
K
Koji Sato 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
}

static void nilfs_btree_add_dirty_buffer(struct nilfs_btree *btree,
					 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;
1974 1975
	key = nilfs_btree_node_get_key(node, 0);
	level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
1976 1977 1978
	list_for_each(head, &lists[level]) {
		cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
		cnode = (struct nilfs_btree_node *)cbh->b_data;
1979
		ckey = nilfs_btree_node_get_key(cnode, 0);
K
Koji Sato 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
		if (key < ckey)
			break;
	}
	list_add_tail(&bh->b_assoc_buffers, head);
}

static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *bmap,
					     struct list_head *listp)
{
	struct nilfs_btree *btree = (struct nilfs_btree *)bmap;
	struct address_space *btcache = &NILFS_BMAP_I(bmap)->i_btnode_cache;
	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++)
2021
		list_splice_tail(&lists[level], listp);
K
Koji Sato 已提交
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
}

static int nilfs_btree_assign_p(struct nilfs_btree *btree,
				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;
	int ret;

	parent = nilfs_btree_get_node(btree, path, level + 1);
	ptr = nilfs_btree_node_get_ptr(btree, parent,
				       path[level + 1].bp_index);
	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(
			&NILFS_BMAP_I(&btree->bt_bmap)->i_btnode_cache,
			&path[level].bp_ctxt);
		if (ret < 0)
			return ret;
		nilfs_btnode_commit_change_key(
			&NILFS_BMAP_I(&btree->bt_bmap)->i_btnode_cache,
			&path[level].bp_ctxt);
		*bh = path[level].bp_ctxt.bh;
	}

	nilfs_btree_node_set_ptr(btree, parent,
				 path[level + 1].bp_index, blocknr);

2057
	key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
K
Koji Sato 已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
	/* on-disk format */
	binfo->bi_dat.bi_blkoff = nilfs_bmap_key_to_dkey(key);
	binfo->bi_dat.bi_level = level;

	return 0;
}

static int nilfs_btree_assign_v(struct nilfs_btree *btree,
				struct nilfs_btree_path *path,
				int level,
				struct buffer_head **bh,
				sector_t blocknr,
				union nilfs_binfo *binfo)
{
	struct nilfs_btree_node *parent;
2073
	struct inode *dat = nilfs_bmap_get_dat(&btree->bt_bmap);
K
Koji Sato 已提交
2074 2075 2076 2077 2078 2079 2080 2081 2082
	__u64 key;
	__u64 ptr;
	union nilfs_bmap_ptr_req req;
	int ret;

	parent = nilfs_btree_get_node(btree, path, level + 1);
	ptr = nilfs_btree_node_get_ptr(btree, parent,
				       path[level + 1].bp_index);
	req.bpr_ptr = ptr;
2083 2084
	ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
	if (ret < 0)
K
Koji Sato 已提交
2085
		return ret;
2086
	nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
K
Koji Sato 已提交
2087

2088
	key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
K
Koji Sato 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
	/* on-disk format */
	binfo->bi_v.bi_vblocknr = nilfs_bmap_ptr_to_dptr(ptr);
	binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);

	return 0;
}

static int nilfs_btree_assign(struct nilfs_bmap *bmap,
			      struct buffer_head **bh,
			      sector_t blocknr,
			      union nilfs_binfo *binfo)
{
	struct nilfs_btree *btree;
	struct nilfs_btree_path *path;
	struct nilfs_btree_node *node;
	__u64 key;
	int level, ret;

	btree = (struct nilfs_btree *)bmap;
2108
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2109 2110
	if (path == NULL)
		return -ENOMEM;
2111
	nilfs_btree_init_path(path);
K
Koji Sato 已提交
2112 2113 2114

	if (buffer_nilfs_node(*bh)) {
		node = (struct nilfs_btree_node *)(*bh)->b_data;
2115 2116
		key = nilfs_btree_node_get_key(node, 0);
		level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
2117 2118 2119 2120 2121 2122 2123
	} else {
		key = nilfs_bmap_data_get_key(bmap, *bh);
		level = NILFS_BTREE_LEVEL_DATA;
	}

	ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
	if (ret < 0) {
2124
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2125 2126 2127
		goto out;
	}

2128 2129 2130
	ret = NILFS_BMAP_USE_VBN(bmap) ?
		nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
		nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
K
Koji Sato 已提交
2131 2132

 out:
2133
	nilfs_btree_release_path(path);
2134
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147

	return ret;
}

static int nilfs_btree_assign_gc(struct nilfs_bmap *bmap,
				 struct buffer_head **bh,
				 sector_t blocknr,
				 union nilfs_binfo *binfo)
{
	struct nilfs_btree_node *node;
	__u64 key;
	int ret;

2148 2149
	ret = nilfs_dat_move(nilfs_bmap_get_dat(bmap), (*bh)->b_blocknr,
			     blocknr);
K
Koji Sato 已提交
2150 2151 2152 2153 2154
	if (ret < 0)
		return ret;

	if (buffer_nilfs_node(*bh)) {
		node = (struct nilfs_btree_node *)(*bh)->b_data;
2155
		key = nilfs_btree_node_get_key(node, 0);
K
Koji Sato 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	} else
		key = nilfs_bmap_data_get_key(bmap, *bh);

	/* on-disk format */
	binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
	binfo->bi_v.bi_blkoff = nilfs_bmap_key_to_dkey(key);

	return 0;
}

static int nilfs_btree_mark(struct nilfs_bmap *bmap, __u64 key, int level)
{
	struct buffer_head *bh;
	struct nilfs_btree *btree;
	struct nilfs_btree_path *path;
	__u64 ptr;
	int ret;

	btree = (struct nilfs_btree *)bmap;
2175
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2176 2177
	if (path == NULL)
		return -ENOMEM;
2178
	nilfs_btree_init_path(path);
K
Koji Sato 已提交
2179 2180 2181

	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1);
	if (ret < 0) {
2182
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2183 2184
		goto out;
	}
2185
	ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
2186
	if (ret < 0) {
2187
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2188 2189 2190 2191 2192
		goto out;
	}

	if (!buffer_dirty(bh))
		nilfs_btnode_mark_dirty(bh);
2193
	brelse(bh);
K
Koji Sato 已提交
2194 2195 2196 2197
	if (!nilfs_bmap_dirty(&btree->bt_bmap))
		nilfs_bmap_set_dirty(&btree->bt_bmap);

 out:
2198
	nilfs_btree_release_path(path);
2199
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2200 2201 2202 2203 2204
	return ret;
}

static const struct nilfs_bmap_operations nilfs_btree_ops = {
	.bop_lookup		=	nilfs_btree_lookup,
2205
	.bop_lookup_contig	=	nilfs_btree_lookup_contig,
K
Koji Sato 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
	.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,
2225
	.bop_lookup_contig	=	NULL,
K
Koji Sato 已提交
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
	.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,
};

2243
int nilfs_btree_init(struct nilfs_bmap *bmap)
K
Koji Sato 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252
{
	bmap->b_ops = &nilfs_btree_ops;
	return 0;
}

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