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

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

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

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

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

out:
	return path;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

static inline int
156
nilfs_btree_node_nchildren_min(const struct nilfs_btree_node *node,
157
			       const struct nilfs_bmap *btree)
K
Koji Sato 已提交
158
{
159
	return nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
160 161 162 163 164
		NILFS_BTREE_ROOT_NCHILDREN_MIN :
		NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
}

static inline int
165
nilfs_btree_node_nchildren_max(const struct nilfs_btree_node *node,
166
			       const struct nilfs_bmap *btree)
K
Koji Sato 已提交
167
{
168
	return nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
169 170 171 172 173
		NILFS_BTREE_ROOT_NCHILDREN_MAX :
		NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree));
}

static inline __le64 *
174
nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
K
Koji Sato 已提交
175 176
{
	return (__le64 *)((char *)(node + 1) +
177
			  (nilfs_btree_node_root(node) ?
K
Koji Sato 已提交
178 179 180 181
			   0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
}

static inline __le64 *
182
nilfs_btree_node_dptrs(const struct nilfs_btree_node *node,
183
		       const struct nilfs_bmap *btree)
K
Koji Sato 已提交
184
{
185 186
	return (__le64 *)(nilfs_btree_node_dkeys(node) +
			  nilfs_btree_node_nchildren_max(node, btree));
K
Koji Sato 已提交
187 188 189
}

static inline __u64
190
nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
K
Koji Sato 已提交
191
{
192
	return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
K
Koji Sato 已提交
193 194 195
}

static inline void
196
nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
K
Koji Sato 已提交
197
{
198
	*(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
K
Koji Sato 已提交
199 200 201
}

static inline __u64
202
nilfs_btree_node_get_ptr(const struct nilfs_bmap *btree,
203
			 const struct nilfs_btree_node *node, int index)
K
Koji Sato 已提交
204
{
205
	return le64_to_cpu(*(nilfs_btree_node_dptrs(node, btree) + index));
K
Koji Sato 已提交
206 207 208
}

static inline void
209
nilfs_btree_node_set_ptr(struct nilfs_bmap *btree,
210
			 struct nilfs_btree_node *node, int index, __u64 ptr)
K
Koji Sato 已提交
211
{
212
	*(nilfs_btree_node_dptrs(node, btree) + index) = cpu_to_le64(ptr);
K
Koji Sato 已提交
213 214
}

215
static void nilfs_btree_node_init(struct nilfs_bmap *btree,
K
Koji Sato 已提交
216 217 218 219 220 221 222 223
				  struct nilfs_btree_node *node,
				  int flags, int level, int nchildren,
				  const __u64 *keys, const __u64 *ptrs)
{
	__le64 *dkeys;
	__le64 *dptrs;
	int i;

224 225 226
	nilfs_btree_node_set_flags(node, flags);
	nilfs_btree_node_set_level(node, level);
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
227

228 229
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
K
Koji Sato 已提交
230
	for (i = 0; i < nchildren; i++) {
231 232
		dkeys[i] = cpu_to_le64(keys[i]);
		dptrs[i] = cpu_to_le64(ptrs[i]);
K
Koji Sato 已提交
233 234 235 236
	}
}

/* Assume the buffer heads corresponding to left and right are locked. */
237
static void nilfs_btree_node_move_left(struct nilfs_bmap *btree,
K
Koji Sato 已提交
238 239 240 241 242 243 244 245
				       struct nilfs_btree_node *left,
				       struct nilfs_btree_node *right,
				       int n)
{
	__le64 *ldkeys, *rdkeys;
	__le64 *ldptrs, *rdptrs;
	int lnchildren, rnchildren;

246 247 248
	ldkeys = nilfs_btree_node_dkeys(left);
	ldptrs = nilfs_btree_node_dptrs(left, btree);
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
249

250 251 252
	rdkeys = nilfs_btree_node_dkeys(right);
	rdptrs = nilfs_btree_node_dptrs(right, btree);
	rnchildren = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
253 254 255 256 257 258 259 260

	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;
261 262
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
263 264 265
}

/* Assume that the buffer heads corresponding to left and right are locked. */
266
static void nilfs_btree_node_move_right(struct nilfs_bmap *btree,
K
Koji Sato 已提交
267 268 269 270 271 272 273 274
					struct nilfs_btree_node *left,
					struct nilfs_btree_node *right,
					int n)
{
	__le64 *ldkeys, *rdkeys;
	__le64 *ldptrs, *rdptrs;
	int lnchildren, rnchildren;

275 276 277
	ldkeys = nilfs_btree_node_dkeys(left);
	ldptrs = nilfs_btree_node_dptrs(left, btree);
	lnchildren = nilfs_btree_node_get_nchildren(left);
K
Koji Sato 已提交
278

279 280 281
	rdkeys = nilfs_btree_node_dkeys(right);
	rdptrs = nilfs_btree_node_dptrs(right, btree);
	rnchildren = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
282 283 284 285 286 287 288 289

	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;
290 291
	nilfs_btree_node_set_nchildren(left, lnchildren);
	nilfs_btree_node_set_nchildren(right, rnchildren);
K
Koji Sato 已提交
292 293 294
}

/* Assume that the buffer head corresponding to node is locked. */
295
static void nilfs_btree_node_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
296 297 298 299 300 301 302
				    struct nilfs_btree_node *node,
				    __u64 key, __u64 ptr, int index)
{
	__le64 *dkeys;
	__le64 *dptrs;
	int nchildren;

303 304 305
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
306 307 308 309 310 311
	if (index < nchildren) {
		memmove(dkeys + index + 1, dkeys + index,
			(nchildren - index) * sizeof(*dkeys));
		memmove(dptrs + index + 1, dptrs + index,
			(nchildren - index) * sizeof(*dptrs));
	}
312 313
	dkeys[index] = cpu_to_le64(key);
	dptrs[index] = cpu_to_le64(ptr);
K
Koji Sato 已提交
314
	nchildren++;
315
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
316 317 318
}

/* Assume that the buffer head corresponding to node is locked. */
319
static void nilfs_btree_node_delete(struct nilfs_bmap *btree,
K
Koji Sato 已提交
320 321 322 323 324 325 326 327 328
				    struct nilfs_btree_node *node,
				    __u64 *keyp, __u64 *ptrp, int index)
{
	__u64 key;
	__u64 ptr;
	__le64 *dkeys;
	__le64 *dptrs;
	int nchildren;

329 330
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
331 332
	key = le64_to_cpu(dkeys[index]);
	ptr = le64_to_cpu(dptrs[index]);
333
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
334 335 336 337 338 339 340 341 342 343 344 345
	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--;
346
	nilfs_btree_node_set_nchildren(node, nchildren);
K
Koji Sato 已提交
347 348
}

349
static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
K
Koji Sato 已提交
350 351 352 353 354 355 356
				   __u64 key, int *indexp)
{
	__u64 nkey;
	int index, low, high, s;

	/* binary search */
	low = 0;
357
	high = nilfs_btree_node_get_nchildren(node) - 1;
K
Koji Sato 已提交
358 359 360 361
	index = 0;
	s = 0;
	while (low <= high) {
		index = (low + high) / 2;
362
		nkey = nilfs_btree_node_get_key(node, index);
K
Koji Sato 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375
		if (nkey == key) {
			s = 0;
			goto out;
		} else if (nkey < key) {
			low = index + 1;
			s = -1;
		} else {
			high = index - 1;
			s = 1;
		}
	}

	/* adjust index */
376 377
	if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
		if (s > 0 && index > 0)
K
Koji Sato 已提交
378 379 380 381 382 383 384 385 386 387
			index--;
	} else if (s < 0)
		index++;

 out:
	*indexp = index;

	return s == 0;
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
/**
 * nilfs_btree_node_broken - verify consistency of btree node
 * @node: btree node block to be examined
 * @size: node size (in bytes)
 * @blocknr: block number
 *
 * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
 */
static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
				   size_t size, sector_t blocknr)
{
	int level, flags, nchildren;
	int ret = 0;

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

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

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

K
Koji Sato 已提交
425
static inline struct nilfs_btree_node *
426
nilfs_btree_get_root(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
427
{
428
	return (struct nilfs_btree_node *)btree->b_u.u_data;
K
Koji Sato 已提交
429 430 431
}

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

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

443
static inline int nilfs_btree_height(const struct nilfs_bmap *btree)
K
Koji Sato 已提交
444
{
445
	return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
K
Koji Sato 已提交
446 447 448
}

static inline struct nilfs_btree_node *
449
nilfs_btree_get_node(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
450 451 452 453 454
		     const struct nilfs_btree_path *path,
		     int level)
{
	return (level == nilfs_btree_height(btree) - 1) ?
		nilfs_btree_get_root(btree) :
455
		nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
456 457
}

458 459 460 461 462 463 464 465 466 467 468 469
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;
}

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

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

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

488 489
	ncmax = NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree));

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

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

	return 0;
}

519
static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
520 521 522 523 524 525 526 527
				      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);
528
	index = nilfs_btree_node_get_nchildren(node) - 1;
K
Koji Sato 已提交
529 530
	if (index < 0)
		return -ENOENT;
531
	level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
532 533 534 535 536
	ptr = nilfs_btree_node_get_ptr(btree, node, index);
	path[level].bp_bh = NULL;
	path[level].bp_index = index;

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

	if (keyp != NULL)
549
		*keyp = nilfs_btree_node_get_key(node, index);
K
Koji Sato 已提交
550 551 552 553 554 555
	if (ptrp != NULL)
		*ptrp = ptr;

	return 0;
}

556
static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
557 558 559 560 561
			      __u64 key, int level, __u64 *ptrp)
{
	struct nilfs_btree_path *path;
	int ret;

562
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
563 564 565
	if (path == NULL)
		return -ENOMEM;

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

568
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
569 570 571 572

	return ret;
}

573
static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
574 575 576 577 578 579 580 581 582 583
				     __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;
	int ret, cnt, index, maxlevel;

584
	path = nilfs_btree_alloc_path();
585 586
	if (path == NULL)
		return -ENOMEM;
587

588 589 590 591
	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level);
	if (ret < 0)
		goto out;

592 593
	if (NILFS_BMAP_USE_VBN(btree)) {
		dat = nilfs_bmap_get_dat(btree);
594 595 596 597 598 599 600 601 602 603 604 605 606
		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 (;;) {
607 608
		while (index < nilfs_btree_node_get_nchildren(node)) {
			if (nilfs_btree_node_get_key(node, index) !=
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
			    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;
629 630
		if (index >= nilfs_btree_node_get_nchildren(node) ||
		    nilfs_btree_node_get_key(node, index) != key + cnt)
631 632 633 634 635 636 637 638 639
			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;
640
		node = nilfs_btree_get_nonroot_node(path, level);
641 642 643 644 645 646 647
		index = 0;
		path[level].bp_index = index;
	}
 end:
	*ptrp = ptr;
	ret = cnt;
 out:
648
	nilfs_btree_free_path(path);
649 650 651
	return ret;
}

652
static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
K
Koji Sato 已提交
653 654 655 656 657 658
				    struct nilfs_btree_path *path,
				    int level, __u64 key)
{
	if (level < nilfs_btree_height(btree) - 1) {
		do {
			nilfs_btree_node_set_key(
659
				nilfs_btree_get_nonroot_node(path, level),
K
Koji Sato 已提交
660 661 662 663 664 665 666 667 668
				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) {
669
		nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
K
Koji Sato 已提交
670 671 672 673
					 path[level].bp_index, key);
	}
}

674
static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
675 676 677 678 679 680
				  struct nilfs_btree_path *path,
				  int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node;

	if (level < nilfs_btree_height(btree) - 1) {
681
		node = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
682 683 684 685 686 687 688
		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,
689 690
						nilfs_btree_node_get_key(node,
									 0));
K
Koji Sato 已提交
691 692 693 694 695 696 697
	} else {
		node = nilfs_btree_get_root(btree);
		nilfs_btree_node_insert(btree, node, *keyp, *ptrp,
					path[level].bp_index);
	}
}

698
static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
K
Koji Sato 已提交
699 700 701 702 703 704
				   struct nilfs_btree_path *path,
				   int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *left;
	int nchildren, lnchildren, n, move;

705 706 707 708
	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 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	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,
726
				nilfs_btree_node_get_key(node, 0));
K
Koji Sato 已提交
727 728

	if (move) {
729
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
730 731 732 733 734
		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 {
735
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
736 737 738 739 740 741 742
		path[level].bp_sib_bh = NULL;
		path[level].bp_index -= n;
	}

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

743
static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
K
Koji Sato 已提交
744 745 746 747 748 749
				    struct nilfs_btree_path *path,
				    int level, __u64 *keyp, __u64 *ptrp)
{
	struct nilfs_btree_node *node, *right;
	int nchildren, rnchildren, n, move;

750 751 752 753
	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 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	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,
772
				nilfs_btree_node_get_key(right, 0));
K
Koji Sato 已提交
773 774 775
	path[level + 1].bp_index--;

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

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

789
static void nilfs_btree_split(struct nilfs_bmap *btree,
K
Koji Sato 已提交
790 791 792 793 794 795 796 797
			      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;

798 799 800
	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 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
	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);

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

	if (move) {
820
		path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
821 822 823
		nilfs_btree_node_insert(btree, right, *keyp, *ptrp,
					path[level].bp_index);

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

827
		brelse(path[level].bp_bh);
K
Koji Sato 已提交
828 829 830 831 832
		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);

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

836
		brelse(path[level].bp_sib_bh);
K
Koji Sato 已提交
837 838 839 840 841 842
		path[level].bp_sib_bh = NULL;
	}

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

843
static void nilfs_btree_grow(struct nilfs_bmap *btree,
K
Koji Sato 已提交
844 845 846 847 848 849 850
			     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);
851
	child = nilfs_btree_get_sib_node(path, level);
K
Koji Sato 已提交
852

853
	n = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
854 855

	nilfs_btree_node_move_right(btree, root, child, n);
856
	nilfs_btree_node_set_level(root, level + 1);
K
Koji Sato 已提交
857 858 859 860 861 862 863 864 865

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

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

870
static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
K
Koji Sato 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
				   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;
}

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

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

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

	stats->bs_nblocks = 0;
	level = NILFS_BTREE_LEVEL_DATA;

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

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

943 944
	ncmax = NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(btree));

K
Koji Sato 已提交
945 946 947
	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < nilfs_btree_height(btree) - 1;
	     level++) {
948
		node = nilfs_btree_get_nonroot_node(path, level);
949
		if (nilfs_btree_node_get_nchildren(node) < ncmax) {
K
Koji Sato 已提交
950 951 952 953 954 955 956 957 958 959 960 961
			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);
962
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
963 964 965
			if (ret < 0)
				goto err_out_child_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
966
			if (nilfs_btree_node_get_nchildren(sib) < ncmax) {
K
Koji Sato 已提交
967 968 969 970 971
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_carry_left;
				stats->bs_nblocks++;
				goto out;
			} else
972
				brelse(bh);
K
Koji Sato 已提交
973 974 975 976
		}

		/* right sibling */
		if (pindex <
977
		    nilfs_btree_node_get_nchildren(parent) - 1) {
K
Koji Sato 已提交
978 979
			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
			if (nilfs_btree_node_get_nchildren(sib) < ncmax) {
K
Koji Sato 已提交
985 986 987 988 989
				path[level].bp_sib_bh = bh;
				path[level].bp_op = nilfs_btree_carry_right;
				stats->bs_nblocks++;
				goto out;
			} else
990
				brelse(bh);
K
Koji Sato 已提交
991 992 993 994 995
		}

		/* split */
		path[level].bp_newreq.bpr_ptr =
			path[level - 1].bp_newreq.bpr_ptr + 1;
996
		ret = nilfs_bmap_prepare_alloc_ptr(btree,
997
						   &path[level].bp_newreq, dat);
K
Koji Sato 已提交
998 999
		if (ret < 0)
			goto err_out_child_node;
1000 1001 1002
		ret = nilfs_btree_get_new_block(btree,
						path[level].bp_newreq.bpr_ptr,
						&bh);
K
Koji Sato 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		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);
1017
	if (nilfs_btree_node_get_nchildren(node) <
1018
	    NILFS_BTREE_ROOT_NCHILDREN_MAX) {
K
Koji Sato 已提交
1019 1020 1021 1022 1023 1024 1025
		path[level].bp_op = nilfs_btree_do_insert;
		stats->bs_nblocks++;
		goto out;
	}

	/* grow */
	path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
1026
	ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1027 1028
	if (ret < 0)
		goto err_out_child_node;
1029 1030
	ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
					&bh);
K
Koji Sato 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	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:
1052
	nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1053 1054
 err_out_child_node:
	for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
1055
		nilfs_btnode_delete(path[level].bp_sib_bh);
1056
		nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
K
Koji Sato 已提交
1057 1058 1059

	}

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

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

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

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

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

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

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

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

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

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

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

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

1142
static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1143 1144 1145 1146 1147 1148 1149 1150
				    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);

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);
K
Koji Sato 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

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

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

1173
static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1174 1175 1176 1177 1178 1179 1180 1181
				     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);

1182 1183 1184 1185
	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 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

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

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

1205
static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1206 1207 1208 1209 1210 1211 1212 1213
				    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);

1214 1215
	node = nilfs_btree_get_nonroot_node(path, level);
	left = nilfs_btree_get_sib_node(path, level);
K
Koji Sato 已提交
1216

1217
	n = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
1218 1219 1220 1221 1222 1223

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

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

1230
static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1231 1232 1233 1234 1235 1236 1237 1238
				     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);

1239 1240
	node = nilfs_btree_get_nonroot_node(path, level);
	right = nilfs_btree_get_sib_node(path, level);
K
Koji Sato 已提交
1241

1242
	n = nilfs_btree_node_get_nchildren(right);
K
Koji Sato 已提交
1243 1244 1245 1246 1247 1248

	nilfs_btree_node_move_left(btree, node, right, n);

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

1249
	nilfs_btnode_delete(path[level].bp_sib_bh);
K
Koji Sato 已提交
1250 1251 1252 1253
	path[level].bp_sib_bh = NULL;
	path[level + 1].bp_index++;
}

1254
static void nilfs_btree_shrink(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263
			       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);
1264
	child = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
1265 1266

	nilfs_btree_node_delete(btree, root, NULL, NULL, 0);
1267 1268
	nilfs_btree_node_set_level(root, level);
	n = nilfs_btree_node_get_nchildren(child);
K
Koji Sato 已提交
1269 1270
	nilfs_btree_node_move_left(btree, root, child, n);

1271
	nilfs_btnode_delete(path[level].bp_bh);
K
Koji Sato 已提交
1272 1273 1274 1275
	path[level].bp_bh = NULL;
}


1276
static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1277 1278
				      struct nilfs_btree_path *path,
				      int *levelp,
1279 1280
				      struct nilfs_bmap_stats *stats,
				      struct inode *dat)
K
Koji Sato 已提交
1281 1282 1283 1284
{
	struct buffer_head *bh;
	struct nilfs_btree_node *node, *parent, *sib;
	__u64 sibptr;
1285
	int pindex, level, ncmin, ret;
K
Koji Sato 已提交
1286 1287 1288

	ret = 0;
	stats->bs_nblocks = 0;
1289 1290
	ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));

K
Koji Sato 已提交
1291 1292 1293
	for (level = NILFS_BTREE_LEVEL_NODE_MIN;
	     level < nilfs_btree_height(btree) - 1;
	     level++) {
1294
		node = nilfs_btree_get_nonroot_node(path, level);
K
Koji Sato 已提交
1295 1296 1297
		path[level].bp_oldreq.bpr_ptr =
			nilfs_btree_node_get_ptr(btree, node,
						 path[level].bp_index);
1298
		ret = nilfs_bmap_prepare_end_ptr(btree,
1299
						 &path[level].bp_oldreq, dat);
1300 1301
		if (ret < 0)
			goto err_out_child_node;
K
Koji Sato 已提交
1302

1303
		if (nilfs_btree_node_get_nchildren(node) > ncmin) {
K
Koji Sato 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
			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);
1316
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1317 1318 1319
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1320
			if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
K
Koji Sato 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
				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 <
1332
			   nilfs_btree_node_get_nchildren(parent) - 1) {
K
Koji Sato 已提交
1333 1334 1335
			/* right sibling */
			sibptr = nilfs_btree_node_get_ptr(btree, parent,
							  pindex + 1);
1336
			ret = nilfs_btree_get_block(btree, sibptr, &bh);
K
Koji Sato 已提交
1337 1338 1339
			if (ret < 0)
				goto err_out_curr_node;
			sib = (struct nilfs_btree_node *)bh->b_data;
1340
			if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
K
Koji Sato 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
				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 */
1354
			WARN_ON(level != nilfs_btree_height(btree) - 2);
1355
			if (nilfs_btree_node_get_nchildren(node) - 1 <=
K
Koji Sato 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
			    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);
1372

1373
	ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
1374 1375 1376
	if (ret < 0)
		goto err_out_child_node;

K
Koji Sato 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
	/* 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:
1388
	nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1389 1390
 err_out_child_node:
	for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
1391
		brelse(path[level].bp_sib_bh);
1392
		nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
K
Koji Sato 已提交
1393 1394 1395 1396 1397 1398
	}
	*levelp = level;
	stats->bs_nblocks = 0;
	return ret;
}

1399
static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1400
				      struct nilfs_btree_path *path,
1401
				      int maxlevel, struct inode *dat)
K
Koji Sato 已提交
1402 1403 1404 1405
{
	int level;

	for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
1406
		nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
1407
		path[level].bp_op(btree, path, level, NULL, NULL);
K
Koji Sato 已提交
1408 1409
	}

1410 1411
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1412 1413
}

1414
static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
K
Koji Sato 已提交
1415 1416 1417 1418

{
	struct nilfs_btree_path *path;
	struct nilfs_bmap_stats stats;
1419
	struct inode *dat;
K
Koji Sato 已提交
1420 1421
	int level, ret;

1422
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1423 1424
	if (path == NULL)
		return -ENOMEM;
1425

K
Koji Sato 已提交
1426 1427 1428 1429 1430
	ret = nilfs_btree_do_lookup(btree, path, key, NULL,
				    NILFS_BTREE_LEVEL_NODE_MIN);
	if (ret < 0)
		goto out;

1431

1432
	dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
1433 1434

	ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
K
Koji Sato 已提交
1435 1436
	if (ret < 0)
		goto out;
1437
	nilfs_btree_commit_delete(btree, path, level, dat);
1438
	nilfs_bmap_sub_blocks(btree, stats.bs_nblocks);
K
Koji Sato 已提交
1439 1440

out:
1441
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1442 1443 1444
	return ret;
}

1445
static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
K
Koji Sato 已提交
1446 1447 1448 1449
{
	struct nilfs_btree_path *path;
	int ret;

1450
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1451 1452 1453 1454 1455
	if (path == NULL)
		return -ENOMEM;

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

1456
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1457 1458 1459 1460

	return ret;
}

1461
static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
K
Koji Sato 已提交
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
{
	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:
1476
		nchildren = nilfs_btree_node_get_nchildren(root);
K
Koji Sato 已提交
1477 1478 1479
		if (nchildren > 1)
			return 0;
		ptr = nilfs_btree_node_get_ptr(btree, root, nchildren - 1);
1480
		ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
1481 1482 1483 1484 1485 1486 1487 1488
		if (ret < 0)
			return ret;
		node = (struct nilfs_btree_node *)bh->b_data;
		break;
	default:
		return 0;
	}

1489 1490
	nchildren = nilfs_btree_node_get_nchildren(node);
	maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
K
Koji Sato 已提交
1491
	nextmaxkey = (nchildren > 1) ?
1492
		nilfs_btree_node_get_key(node, nchildren - 2) : 0;
K
Koji Sato 已提交
1493
	if (bh != NULL)
1494
		brelse(bh);
K
Koji Sato 已提交
1495

1496
	return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
K
Koji Sato 已提交
1497 1498
}

1499
static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
				   __u64 *keys, __u64 *ptrs, int nitems)
{
	struct buffer_head *bh;
	struct nilfs_btree_node *node, *root;
	__le64 *dkeys;
	__le64 *dptrs;
	__u64 ptr;
	int nchildren, i, ret;

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

1529
	nchildren = nilfs_btree_node_get_nchildren(node);
K
Koji Sato 已提交
1530 1531
	if (nchildren < nitems)
		nitems = nchildren;
1532 1533
	dkeys = nilfs_btree_node_dkeys(node);
	dptrs = nilfs_btree_node_dptrs(node, btree);
K
Koji Sato 已提交
1534
	for (i = 0; i < nitems; i++) {
1535 1536
		keys[i] = le64_to_cpu(dkeys[i]);
		ptrs[i] = le64_to_cpu(dptrs[i]);
K
Koji Sato 已提交
1537 1538 1539
	}

	if (bh != NULL)
1540
		brelse(bh);
K
Koji Sato 已提交
1541 1542 1543 1544 1545

	return nitems;
}

static int
1546
nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
K
Koji Sato 已提交
1547 1548 1549 1550 1551 1552
				       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;
1553
	struct inode *dat = NULL;
K
Koji Sato 已提交
1554 1555 1556 1557 1558 1559
	int ret;

	stats->bs_nblocks = 0;

	/* for data */
	/* cannot find near ptr */
1560
	if (NILFS_BMAP_USE_VBN(btree)) {
1561
		dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
1562
		dat = nilfs_bmap_get_dat(btree);
1563
	}
1564

1565
	ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
K
Koji Sato 已提交
1566 1567 1568 1569 1570 1571 1572
	if (ret < 0)
		return ret;

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

1577
		ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
K
Koji Sato 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
		if (ret < 0)
			goto err_out_nreq;

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

	/* success */
	return 0;

	/* error */
 err_out_nreq:
1590
	nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
K
Koji Sato 已提交
1591
 err_out_dreq:
1592
	nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
K
Koji Sato 已提交
1593 1594 1595 1596 1597 1598
	stats->bs_nblocks = 0;
	return ret;

}

static void
1599
nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1600 1601
				      __u64 key, __u64 ptr,
				      const __u64 *keys, const __u64 *ptrs,
1602
				      int n,
K
Koji Sato 已提交
1603 1604 1605 1606 1607
				      union nilfs_bmap_ptr_req *dreq,
				      union nilfs_bmap_ptr_req *nreq,
				      struct buffer_head *bh)
{
	struct nilfs_btree_node *node;
1608
	struct inode *dat;
K
Koji Sato 已提交
1609 1610 1611
	__u64 tmpptr;

	/* free resources */
1612 1613
	if (btree->b_ops->bop_clear != NULL)
		btree->b_ops->bop_clear(btree);
K
Koji Sato 已提交
1614 1615 1616 1617 1618

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

	/* convert and insert */
1619 1620
	dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
	nilfs_btree_init(btree);
K
Koji Sato 已提交
1621
	if (nreq != NULL) {
1622 1623
		nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
		nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
K
Koji Sato 已提交
1624 1625 1626 1627

		/* 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);
1628
		nilfs_btree_node_insert(btree, node, key, dreq->bpr_ptr, n);
K
Koji Sato 已提交
1629 1630
		if (!buffer_dirty(bh))
			nilfs_btnode_mark_dirty(bh);
1631 1632
		if (!nilfs_bmap_dirty(btree))
			nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1633

1634
		brelse(bh);
K
Koji Sato 已提交
1635 1636 1637 1638 1639 1640 1641

		/* 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 {
1642
		nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
K
Koji Sato 已提交
1643 1644 1645 1646 1647

		/* 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);
1648 1649 1650
		nilfs_btree_node_insert(btree, node, key, dreq->bpr_ptr, n);
		if (!nilfs_bmap_dirty(btree))
			nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
1651 1652
	}

1653
	if (NILFS_BMAP_USE_VBN(btree))
1654
		nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
K
Koji Sato 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
}

/**
 * nilfs_btree_convert_and_insert -
 * @bmap:
 * @key:
 * @ptr:
 * @keys:
 * @ptrs:
 * @n:
 */
1666
int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1667
				   __u64 key, __u64 ptr,
1668
				   const __u64 *keys, const __u64 *ptrs, int n)
K
Koji Sato 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
{
	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(
1679
			   1 << btree->b_inode->i_blkbits)) {
K
Koji Sato 已提交
1680 1681 1682 1683 1684 1685 1686 1687
		di = &dreq;
		ni = &nreq;
	} else {
		di = NULL;
		ni = NULL;
		BUG();
	}

1688
	ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
K
Koji Sato 已提交
1689 1690 1691
						     &stats);
	if (ret < 0)
		return ret;
1692
	nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
1693
					      di, ni, bh);
1694
	nilfs_bmap_add_blocks(btree, stats.bs_nblocks);
K
Koji Sato 已提交
1695 1696 1697
	return 0;
}

1698
static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
				   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;
}

1710
static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1711
					struct nilfs_btree_path *path,
1712
					int level, struct inode *dat)
K
Koji Sato 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721
{
	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;
1722 1723
	ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
				       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1724 1725 1726 1727 1728 1729 1730 1731
	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(
1732
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
1733 1734
			&path[level].bp_ctxt);
		if (ret < 0) {
1735 1736 1737
			nilfs_dat_abort_update(dat,
					       &path[level].bp_oldreq.bpr_req,
					       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1738 1739 1740 1741 1742 1743 1744
			return ret;
		}
	}

	return 0;
}

1745
static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1746
					struct nilfs_btree_path *path,
1747
					int level, struct inode *dat)
K
Koji Sato 已提交
1748 1749 1750
{
	struct nilfs_btree_node *parent;

1751 1752
	nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
				&path[level].bp_newreq.bpr_req,
1753
				btree->b_ptr_type == NILFS_BMAP_PTR_VS);
K
Koji Sato 已提交
1754 1755 1756

	if (buffer_nilfs_node(path[level].bp_bh)) {
		nilfs_btnode_commit_change_key(
1757
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
			&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);
}

1768
static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1769
				       struct nilfs_btree_path *path,
1770
				       int level, struct inode *dat)
K
Koji Sato 已提交
1771
{
1772 1773
	nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
			       &path[level].bp_newreq.bpr_req);
K
Koji Sato 已提交
1774 1775
	if (buffer_nilfs_node(path[level].bp_bh))
		nilfs_btnode_abort_change_key(
1776
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
1777 1778 1779
			&path[level].bp_ctxt);
}

1780
static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1781
					   struct nilfs_btree_path *path,
1782 1783
					   int minlevel, int *maxlevelp,
					   struct inode *dat)
K
Koji Sato 已提交
1784 1785 1786 1787 1788
{
	int level, ret;

	level = minlevel;
	if (!buffer_nilfs_volatile(path[level].bp_bh)) {
1789
		ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1790 1791 1792 1793 1794 1795
		if (ret < 0)
			return ret;
	}
	while ((++level < nilfs_btree_height(btree) - 1) &&
	       !buffer_dirty(path[level].bp_bh)) {

1796
		WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
1797
		ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
		if (ret < 0)
			goto out;
	}

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

	/* error */
 out:
	while (--level > minlevel)
1809
		nilfs_btree_abort_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1810
	if (!buffer_nilfs_volatile(path[level].bp_bh))
1811
		nilfs_btree_abort_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1812 1813 1814
	return ret;
}

1815
static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1816
					   struct nilfs_btree_path *path,
1817 1818 1819
					   int minlevel, int maxlevel,
					   struct buffer_head *bh,
					   struct inode *dat)
K
Koji Sato 已提交
1820 1821 1822 1823
{
	int level;

	if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
1824
		nilfs_btree_commit_update_v(btree, path, minlevel, dat);
K
Koji Sato 已提交
1825 1826

	for (level = minlevel + 1; level <= maxlevel; level++)
1827
		nilfs_btree_commit_update_v(btree, path, level, dat);
K
Koji Sato 已提交
1828 1829
}

1830
static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1831
				   struct nilfs_btree_path *path,
1832
				   int level, struct buffer_head *bh)
K
Koji Sato 已提交
1833
{
1834
	int maxlevel = 0, ret;
K
Koji Sato 已提交
1835
	struct nilfs_btree_node *parent;
1836
	struct inode *dat = nilfs_bmap_get_dat(btree);
K
Koji Sato 已提交
1837 1838 1839 1840
	__u64 ptr;

	get_bh(bh);
	path[level].bp_bh = bh;
1841 1842
	ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
					      dat);
K
Koji Sato 已提交
1843 1844 1845 1846 1847 1848 1849
	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);
1850
		ret = nilfs_dat_mark_dirty(dat, ptr);
K
Koji Sato 已提交
1851 1852 1853 1854
		if (ret < 0)
			goto out;
	}

1855
	nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
K
Koji Sato 已提交
1856 1857 1858 1859 1860 1861 1862

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

1863
static int nilfs_btree_propagate(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1864 1865 1866 1867 1868 1869 1870
				 struct buffer_head *bh)
{
	struct nilfs_btree_path *path;
	struct nilfs_btree_node *node;
	__u64 key;
	int level, ret;

1871
	WARN_ON(!buffer_dirty(bh));
K
Koji Sato 已提交
1872

1873
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
1874 1875 1876 1877 1878
	if (path == NULL)
		return -ENOMEM;

	if (buffer_nilfs_node(bh)) {
		node = (struct nilfs_btree_node *)bh->b_data;
1879 1880
		key = nilfs_btree_node_get_key(node, 0);
		level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
1881
	} else {
1882
		key = nilfs_bmap_data_get_key(btree, bh);
K
Koji Sato 已提交
1883 1884 1885 1886 1887
		level = NILFS_BTREE_LEVEL_DATA;
	}

	ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
	if (ret < 0) {
1888
		if (unlikely(ret == -ENOENT))
K
Koji Sato 已提交
1889 1890 1891 1892 1893
			printk(KERN_CRIT "%s: key = %llu, level == %d\n",
			       __func__, (unsigned long long)key, level);
		goto out;
	}

1894
	ret = NILFS_BMAP_USE_VBN(btree) ?
1895 1896
		nilfs_btree_propagate_v(btree, path, level, bh) :
		nilfs_btree_propagate_p(btree, path, level, bh);
K
Koji Sato 已提交
1897 1898

 out:
1899
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
1900 1901 1902 1903

	return ret;
}

1904
static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1905 1906
				    struct buffer_head *bh)
{
1907
	return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
K
Koji Sato 已提交
1908 1909
}

1910
static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
					 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;
1922 1923
	key = nilfs_btree_node_get_key(node, 0);
	level = nilfs_btree_node_get_level(node);
1924 1925 1926 1927 1928 1929 1930
	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,
1931
		       NILFS_BMAP_I(btree)->vfs_inode.i_ino,
1932 1933 1934 1935
		       (unsigned long long)bh->b_blocknr);
		return;
	}

K
Koji Sato 已提交
1936 1937 1938
	list_for_each(head, &lists[level]) {
		cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
		cnode = (struct nilfs_btree_node *)cbh->b_data;
1939
		ckey = nilfs_btree_node_get_key(cnode, 0);
K
Koji Sato 已提交
1940 1941 1942 1943 1944 1945
		if (key < ckey)
			break;
	}
	list_add_tail(&bh->b_assoc_buffers, head);
}

1946
static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1947 1948
					     struct list_head *listp)
{
1949
	struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
K
Koji Sato 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
	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++)
1980
		list_splice_tail(&lists[level], listp);
K
Koji Sato 已提交
1981 1982
}

1983
static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
K
Koji Sato 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
				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(
2003
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
2004 2005 2006 2007
			&path[level].bp_ctxt);
		if (ret < 0)
			return ret;
		nilfs_btnode_commit_change_key(
2008
			&NILFS_BMAP_I(btree)->i_btnode_cache,
K
Koji Sato 已提交
2009 2010 2011 2012 2013 2014 2015
			&path[level].bp_ctxt);
		*bh = path[level].bp_ctxt.bh;
	}

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

2016
	key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
K
Koji Sato 已提交
2017
	/* on-disk format */
2018
	binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
K
Koji Sato 已提交
2019 2020 2021 2022 2023
	binfo->bi_dat.bi_level = level;

	return 0;
}

2024
static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2025 2026 2027 2028 2029 2030 2031
				struct nilfs_btree_path *path,
				int level,
				struct buffer_head **bh,
				sector_t blocknr,
				union nilfs_binfo *binfo)
{
	struct nilfs_btree_node *parent;
2032
	struct inode *dat = nilfs_bmap_get_dat(btree);
K
Koji Sato 已提交
2033 2034 2035 2036 2037 2038
	__u64 key;
	__u64 ptr;
	union nilfs_bmap_ptr_req req;
	int ret;

	parent = nilfs_btree_get_node(btree, path, level + 1);
2039
	ptr = nilfs_btree_node_get_ptr(btree, parent, path[level + 1].bp_index);
K
Koji Sato 已提交
2040
	req.bpr_ptr = ptr;
2041 2042
	ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
	if (ret < 0)
K
Koji Sato 已提交
2043
		return ret;
2044
	nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
K
Koji Sato 已提交
2045

2046
	key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
K
Koji Sato 已提交
2047
	/* on-disk format */
2048 2049
	binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
	binfo->bi_v.bi_blkoff = cpu_to_le64(key);
K
Koji Sato 已提交
2050 2051 2052 2053

	return 0;
}

2054
static int nilfs_btree_assign(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2055 2056 2057 2058 2059 2060 2061 2062 2063
			      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;

2064
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2065 2066 2067 2068 2069
	if (path == NULL)
		return -ENOMEM;

	if (buffer_nilfs_node(*bh)) {
		node = (struct nilfs_btree_node *)(*bh)->b_data;
2070 2071
		key = nilfs_btree_node_get_key(node, 0);
		level = nilfs_btree_node_get_level(node);
K
Koji Sato 已提交
2072
	} else {
2073
		key = nilfs_bmap_data_get_key(btree, *bh);
K
Koji Sato 已提交
2074 2075 2076 2077 2078
		level = NILFS_BTREE_LEVEL_DATA;
	}

	ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
	if (ret < 0) {
2079
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2080 2081 2082
		goto out;
	}

2083
	ret = NILFS_BMAP_USE_VBN(btree) ?
2084 2085
		nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
		nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
K
Koji Sato 已提交
2086 2087

 out:
2088
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2089 2090 2091 2092

	return ret;
}

2093
static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
K
Koji Sato 已提交
2094 2095 2096 2097 2098 2099 2100 2101
				 struct buffer_head **bh,
				 sector_t blocknr,
				 union nilfs_binfo *binfo)
{
	struct nilfs_btree_node *node;
	__u64 key;
	int ret;

2102
	ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
2103
			     blocknr);
K
Koji Sato 已提交
2104 2105 2106 2107 2108
	if (ret < 0)
		return ret;

	if (buffer_nilfs_node(*bh)) {
		node = (struct nilfs_btree_node *)(*bh)->b_data;
2109
		key = nilfs_btree_node_get_key(node, 0);
K
Koji Sato 已提交
2110
	} else
2111
		key = nilfs_bmap_data_get_key(btree, *bh);
K
Koji Sato 已提交
2112 2113 2114

	/* on-disk format */
	binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
2115
	binfo->bi_v.bi_blkoff = cpu_to_le64(key);
K
Koji Sato 已提交
2116 2117 2118 2119

	return 0;
}

2120
static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
K
Koji Sato 已提交
2121 2122 2123 2124 2125 2126
{
	struct buffer_head *bh;
	struct nilfs_btree_path *path;
	__u64 ptr;
	int ret;

2127
	path = nilfs_btree_alloc_path();
K
Koji Sato 已提交
2128 2129 2130 2131 2132
	if (path == NULL)
		return -ENOMEM;

	ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1);
	if (ret < 0) {
2133
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2134 2135
		goto out;
	}
2136
	ret = nilfs_btree_get_block(btree, ptr, &bh);
K
Koji Sato 已提交
2137
	if (ret < 0) {
2138
		WARN_ON(ret == -ENOENT);
K
Koji Sato 已提交
2139 2140 2141 2142 2143
		goto out;
	}

	if (!buffer_dirty(bh))
		nilfs_btnode_mark_dirty(bh);
2144
	brelse(bh);
2145 2146
	if (!nilfs_bmap_dirty(btree))
		nilfs_bmap_set_dirty(btree);
K
Koji Sato 已提交
2147 2148

 out:
2149
	nilfs_btree_free_path(path);
K
Koji Sato 已提交
2150 2151 2152 2153 2154
	return ret;
}

static const struct nilfs_bmap_operations nilfs_btree_ops = {
	.bop_lookup		=	nilfs_btree_lookup,
2155
	.bop_lookup_contig	=	nilfs_btree_lookup_contig,
K
Koji Sato 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
	.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,
2175
	.bop_lookup_contig	=	NULL,
K
Koji Sato 已提交
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
	.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,
};

2193
int nilfs_btree_init(struct nilfs_bmap *bmap)
K
Koji Sato 已提交
2194 2195 2196 2197 2198 2199 2200 2201 2202
{
	bmap->b_ops = &nilfs_btree_ops;
	return 0;
}

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