extent_cache.c 17.7 KB
Newer Older
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 32 33 34 35
/*
 * f2fs extent cache support
 *
 * Copyright (c) 2015 Motorola Mobility
 * Copyright (c) 2015 Samsung Electronics
 * Authors: Jaegeuk Kim <jaegeuk@kernel.org>
 *          Chao Yu <chao2.yu@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/fs.h>
#include <linux/f2fs_fs.h>

#include "f2fs.h"
#include "node.h"
#include <trace/events/f2fs.h>

static struct kmem_cache *extent_tree_slab;
static struct kmem_cache *extent_node_slab;

static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
				struct extent_tree *et, struct extent_info *ei,
				struct rb_node *parent, struct rb_node **p)
{
	struct extent_node *en;

	en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
	if (!en)
		return NULL;

	en->ei = *ei;
	INIT_LIST_HEAD(&en->list);
36
	en->et = et;
37 38 39

	rb_link_node(&en->rb_node, parent, p);
	rb_insert_color(&en->rb_node, &et->root);
40
	atomic_inc(&et->node_cnt);
41 42 43 44 45 46 47 48
	atomic_inc(&sbi->total_ext_node);
	return en;
}

static void __detach_extent_node(struct f2fs_sb_info *sbi,
				struct extent_tree *et, struct extent_node *en)
{
	rb_erase(&en->rb_node, &et->root);
49
	atomic_dec(&et->node_cnt);
50 51 52 53
	atomic_dec(&sbi->total_ext_node);

	if (et->cached_en == en)
		et->cached_en = NULL;
54 55 56 57 58 59 60 61 62 63 64 65 66
	kmem_cache_free(extent_node_slab, en);
}

/*
 * Flow to release an extent_node:
 * 1. list_del_init
 * 2. __detach_extent_node
 * 3. kmem_cache_free.
 */
static void __release_extent_node(struct f2fs_sb_info *sbi,
			struct extent_tree *et, struct extent_node *en)
{
	spin_lock(&sbi->extent_lock);
67 68
	f2fs_bug_on(sbi, list_empty(&en->list));
	list_del_init(&en->list);
69 70 71
	spin_unlock(&sbi->extent_lock);

	__detach_extent_node(sbi, et, en);
72 73 74 75 76 77 78 79
}

static struct extent_tree *__grab_extent_tree(struct inode *inode)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	struct extent_tree *et;
	nid_t ino = inode->i_ino;

80
	mutex_lock(&sbi->extent_tree_lock);
81 82 83 84 85 86 87 88 89
	et = radix_tree_lookup(&sbi->extent_tree_root, ino);
	if (!et) {
		et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
		f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
		memset(et, 0, sizeof(struct extent_tree));
		et->ino = ino;
		et->root = RB_ROOT;
		et->cached_en = NULL;
		rwlock_init(&et->lock);
90
		INIT_LIST_HEAD(&et->list);
91
		atomic_set(&et->node_cnt, 0);
92
		atomic_inc(&sbi->total_ext_tree);
93 94
	} else {
		atomic_dec(&sbi->total_zombie_tree);
95
		list_del_init(&et->list);
96
	}
97
	mutex_unlock(&sbi->extent_tree_lock);
98 99 100 101 102 103 104

	/* never died until evict_inode */
	F2FS_I(inode)->extent_tree = et;

	return et;
}

105 106
static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,
				struct extent_tree *et, unsigned int fofs)
107 108
{
	struct rb_node *node = et->root.rb_node;
109
	struct extent_node *en = et->cached_en;
110

111 112
	if (en) {
		struct extent_info *cei = &en->ei;
113

114 115
		if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) {
			stat_inc_cached_node_hit(sbi);
116
			return en;
117
		}
118 119 120 121 122
	}

	while (node) {
		en = rb_entry(node, struct extent_node, rb_node);

123
		if (fofs < en->ei.fofs) {
124
			node = node->rb_left;
125
		} else if (fofs >= en->ei.fofs + en->ei.len) {
126
			node = node->rb_right;
127 128
		} else {
			stat_inc_rbtree_node_hit(sbi);
129
			return en;
130
		}
131 132 133 134
	}
	return NULL;
}

135 136
static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
				struct extent_tree *et, struct extent_info *ei)
137 138 139 140
{
	struct rb_node **p = &et->root.rb_node;
	struct extent_node *en;

141
	en = __attach_extent_node(sbi, et, ei, NULL, p);
142 143
	if (!en)
		return NULL;
144 145

	et->largest = en->ei;
146 147 148 149 150
	et->cached_en = en;
	return en;
}

static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
151
					struct extent_tree *et)
152 153 154
{
	struct rb_node *node, *next;
	struct extent_node *en;
155
	unsigned int count = atomic_read(&et->node_cnt);
156 157 158 159 160

	node = rb_first(&et->root);
	while (node) {
		next = rb_next(node);
		en = rb_entry(node, struct extent_node, rb_node);
161
		__release_extent_node(sbi, et, en);
162 163 164
		node = next;
	}

165
	return count - atomic_read(&et->node_cnt);
166 167
}

F
Fan Li 已提交
168 169
static void __drop_largest_extent(struct inode *inode,
					pgoff_t fofs, unsigned int len)
170 171 172
{
	struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;

173
	if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs) {
174
		largest->len = 0;
175
		f2fs_mark_inode_dirty_sync(inode, true);
176
	}
177 178
}

179 180
/* return true, if inode page is changed */
bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
181 182 183 184 185 186
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	struct extent_tree *et;
	struct extent_node *en;
	struct extent_info ei;

187 188 189 190 191 192 193 194
	if (!f2fs_may_extent_tree(inode)) {
		/* drop largest extent */
		if (i_ext && i_ext->len) {
			i_ext->len = 0;
			return true;
		}
		return false;
	}
195 196 197

	et = __grab_extent_tree(inode);

198 199
	if (!i_ext || !i_ext->len)
		return false;
200

C
Chao Yu 已提交
201
	get_extent_info(&ei, i_ext);
202 203

	write_lock(&et->lock);
204
	if (atomic_read(&et->node_cnt))
205 206
		goto out;

207
	en = __init_extent_tree(sbi, et, &ei);
208 209 210 211 212 213 214
	if (en) {
		spin_lock(&sbi->extent_lock);
		list_add_tail(&en->list, &sbi->extent_list);
		spin_unlock(&sbi->extent_lock);
	}
out:
	write_unlock(&et->lock);
215
	return false;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}

static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
							struct extent_info *ei)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	struct extent_tree *et = F2FS_I(inode)->extent_tree;
	struct extent_node *en;
	bool ret = false;

	f2fs_bug_on(sbi, !et);

	trace_f2fs_lookup_extent_tree_start(inode, pgofs);

	read_lock(&et->lock);

	if (et->largest.fofs <= pgofs &&
			et->largest.fofs + et->largest.len > pgofs) {
		*ei = et->largest;
		ret = true;
236
		stat_inc_largest_node_hit(sbi);
237 238 239
		goto out;
	}

240
	en = __lookup_extent_tree(sbi, et, pgofs);
241 242 243
	if (en) {
		*ei = en->ei;
		spin_lock(&sbi->extent_lock);
244
		if (!list_empty(&en->list)) {
245
			list_move_tail(&en->list, &sbi->extent_list);
246 247
			et->cached_en = en;
		}
248 249 250 251
		spin_unlock(&sbi->extent_lock);
		ret = true;
	}
out:
252
	stat_inc_total_hit(sbi);
253 254 255 256 257 258
	read_unlock(&et->lock);

	trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
	return ret;
}

259 260 261 262 263 264 265 266 267 268 269

/*
 * lookup extent at @fofs, if hit, return the extent
 * if not, return NULL and
 * @prev_ex: extent before fofs
 * @next_ex: extent after fofs
 * @insert_p: insert point for new extent at fofs
 * in order to simpfy the insertion after.
 * tree must stay unchanged between lookup and insertion.
 */
static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
270 271
				unsigned int fofs,
				struct extent_node **prev_ex,
272 273 274 275 276 277
				struct extent_node **next_ex,
				struct rb_node ***insert_p,
				struct rb_node **insert_parent)
{
	struct rb_node **pnode = &et->root.rb_node;
	struct rb_node *parent = NULL, *tmp_node;
278
	struct extent_node *en = et->cached_en;
279

280 281 282 283 284 285 286 287 288 289
	*insert_p = NULL;
	*insert_parent = NULL;
	*prev_ex = NULL;
	*next_ex = NULL;

	if (RB_EMPTY_ROOT(&et->root))
		return NULL;

	if (en) {
		struct extent_info *cei = &en->ei;
290 291

		if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
292
			goto lookup_neighbors;
293 294 295 296 297 298 299 300 301 302 303
	}

	while (*pnode) {
		parent = *pnode;
		en = rb_entry(*pnode, struct extent_node, rb_node);

		if (fofs < en->ei.fofs)
			pnode = &(*pnode)->rb_left;
		else if (fofs >= en->ei.fofs + en->ei.len)
			pnode = &(*pnode)->rb_right;
		else
304
			goto lookup_neighbors;
305 306 307 308 309 310 311 312 313
	}

	*insert_p = pnode;
	*insert_parent = parent;

	en = rb_entry(parent, struct extent_node, rb_node);
	tmp_node = parent;
	if (parent && fofs > en->ei.fofs)
		tmp_node = rb_next(parent);
G
Geliang Tang 已提交
314
	*next_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
315 316 317 318

	tmp_node = parent;
	if (parent && fofs < en->ei.fofs)
		tmp_node = rb_prev(parent);
G
Geliang Tang 已提交
319
	*prev_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
320
	return NULL;
321 322 323 324 325

lookup_neighbors:
	if (fofs == en->ei.fofs) {
		/* lookup prev node for merging backward later */
		tmp_node = rb_prev(&en->rb_node);
G
Geliang Tang 已提交
326
		*prev_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
327 328 329 330
	}
	if (fofs == en->ei.fofs + en->ei.len - 1) {
		/* lookup next node for merging frontward later */
		tmp_node = rb_next(&en->rb_node);
G
Geliang Tang 已提交
331
		*next_ex = rb_entry_safe(tmp_node, struct extent_node, rb_node);
332 333
	}
	return en;
334 335
}

336
static struct extent_node *__try_merge_extent_node(struct inode *inode,
337 338
				struct extent_tree *et, struct extent_info *ei,
				struct extent_node *prev_ex,
339
				struct extent_node *next_ex)
340
{
341
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
342 343 344 345 346 347 348
	struct extent_node *en = NULL;

	if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
		prev_ex->ei.len += ei->len;
		ei = &prev_ex->ei;
		en = prev_ex;
	}
349

350 351 352 353
	if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
		next_ex->ei.fofs = ei->fofs;
		next_ex->ei.blk = ei->blk;
		next_ex->ei.len += ei->len;
354 355 356
		if (en)
			__release_extent_node(sbi, et, prev_ex);

357 358
		en = next_ex;
	}
359

360 361 362
	if (!en)
		return NULL;

363
	__try_update_largest_extent(inode, et, en);
364 365

	spin_lock(&sbi->extent_lock);
366
	if (!list_empty(&en->list)) {
367
		list_move_tail(&en->list, &sbi->extent_list);
368 369
		et->cached_en = en;
	}
370
	spin_unlock(&sbi->extent_lock);
371 372 373
	return en;
}

374
static struct extent_node *__insert_extent_tree(struct inode *inode,
375 376 377 378
				struct extent_tree *et, struct extent_info *ei,
				struct rb_node **insert_p,
				struct rb_node *insert_parent)
{
379
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
380 381 382
	struct rb_node **p = &et->root.rb_node;
	struct rb_node *parent = NULL;
	struct extent_node *en = NULL;
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

	if (insert_p && insert_parent) {
		parent = insert_parent;
		p = insert_p;
		goto do_insert;
	}

	while (*p) {
		parent = *p;
		en = rb_entry(parent, struct extent_node, rb_node);

		if (ei->fofs < en->ei.fofs)
			p = &(*p)->rb_left;
		else if (ei->fofs >= en->ei.fofs + en->ei.len)
			p = &(*p)->rb_right;
		else
			f2fs_bug_on(sbi, 1);
	}
do_insert:
	en = __attach_extent_node(sbi, et, ei, parent, p);
	if (!en)
		return NULL;
405

406
	__try_update_largest_extent(inode, et, en);
407 408 409 410

	/* update in global extent list */
	spin_lock(&sbi->extent_lock);
	list_add_tail(&en->list, &sbi->extent_list);
411
	et->cached_en = en;
412
	spin_unlock(&sbi->extent_lock);
413 414 415
	return en;
}

C
Chao Yu 已提交
416
static void f2fs_update_extent_tree_range(struct inode *inode,
C
Chao Yu 已提交
417
				pgoff_t fofs, block_t blkaddr, unsigned int len)
418 419 420
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	struct extent_tree *et = F2FS_I(inode)->extent_tree;
421
	struct extent_node *en = NULL, *en1 = NULL;
C
Chao Yu 已提交
422
	struct extent_node *prev_en = NULL, *next_en = NULL;
423
	struct extent_info ei, dei, prev;
424
	struct rb_node **insert_p = NULL, *insert_parent = NULL;
C
Chao Yu 已提交
425 426
	unsigned int end = fofs + len;
	unsigned int pos = (unsigned int)fofs;
427 428

	if (!et)
C
Chao Yu 已提交
429
		return;
430

431 432
	trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);

433 434
	write_lock(&et->lock);

435
	if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
436
		write_unlock(&et->lock);
C
Chao Yu 已提交
437
		return;
438 439 440 441 442
	}

	prev = et->largest;
	dei.len = 0;

443 444 445 446
	/*
	 * drop largest extent before lookup, in case it's already
	 * been shrunk from extent tree
	 */
F
Fan Li 已提交
447
	__drop_largest_extent(inode, fofs, len);
448

C
Chao Yu 已提交
449 450
	/* 1. lookup first extent node in range [fofs, fofs + len - 1] */
	en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
451
					&insert_p, &insert_parent);
452 453
	if (!en)
		en = next_en;
C
Chao Yu 已提交
454 455

	/* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
456 457 458
	while (en && en->ei.fofs < end) {
		unsigned int org_end;
		int parts = 0;	/* # of parts current extent split into */
C
Chao Yu 已提交
459

460
		next_en = en1 = NULL;
C
Chao Yu 已提交
461 462

		dei = en->ei;
463 464
		org_end = dei.fofs + dei.len;
		f2fs_bug_on(sbi, pos >= org_end);
C
Chao Yu 已提交
465

466 467 468 469 470
		if (pos > dei.fofs &&	pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
			en->ei.len = pos - en->ei.fofs;
			prev_en = en;
			parts = 1;
		}
C
Chao Yu 已提交
471

472 473 474 475 476
		if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
			if (parts) {
				set_extent_info(&ei, end,
						end - dei.fofs + dei.blk,
						org_end - end);
477
				en1 = __insert_extent_tree(inode, et, &ei,
478 479 480 481 482 483 484
							NULL, NULL);
				next_en = en1;
			} else {
				en->ei.fofs = end;
				en->ei.blk += end - dei.fofs;
				en->ei.len -= end - dei.fofs;
				next_en = en;
C
Chao Yu 已提交
485
			}
486
			parts++;
C
Chao Yu 已提交
487 488
		}

489 490
		if (!next_en) {
			struct rb_node *node = rb_next(&en->rb_node);
C
Chao Yu 已提交
491

G
Geliang Tang 已提交
492 493
			next_en = rb_entry_safe(node, struct extent_node,
						rb_node);
494 495
		}

496
		if (parts)
497
			__try_update_largest_extent(inode, et, en);
498
		else
499
			__release_extent_node(sbi, et, en);
C
Chao Yu 已提交
500 501

		/*
502 503 504
		 * if original extent is split into zero or two parts, extent
		 * tree has been altered by deletion or insertion, therefore
		 * invalidate pointers regard to tree.
C
Chao Yu 已提交
505
		 */
506 507 508
		if (parts != 1) {
			insert_p = NULL;
			insert_parent = NULL;
509
		}
510
		en = next_en;
511 512 513 514
	}

	/* 3. update extent in extent cache */
	if (blkaddr) {
C
Chao Yu 已提交
515 516

		set_extent_info(&ei, fofs, blkaddr, len);
517 518
		if (!__try_merge_extent_node(inode, et, &ei, prev_en, next_en))
			__insert_extent_tree(inode, et, &ei,
519
						insert_p, insert_parent);
520 521 522 523 524

		/* give up extent_cache, if split and small updates happen */
		if (dei.len >= 1 &&
				prev.len < F2FS_MIN_EXTENT_LEN &&
				et->largest.len < F2FS_MIN_EXTENT_LEN) {
525
			__drop_largest_extent(inode, 0, UINT_MAX);
526
			set_inode_flag(inode, FI_NO_EXTENT);
527
		}
C
Chao Yu 已提交
528
	}
529

530
	if (is_inode_flag_set(inode, FI_NO_EXTENT))
531
		__free_extent_tree(sbi, et);
532 533 534 535 536 537

	write_unlock(&et->lock);
}

unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
{
538
	struct extent_tree *et, *next;
539
	struct extent_node *en;
540 541 542 543 544 545
	unsigned int node_cnt = 0, tree_cnt = 0;
	int remained;

	if (!test_opt(sbi, EXTENT_CACHE))
		return 0;

546 547 548
	if (!atomic_read(&sbi->total_zombie_tree))
		goto free_node;

549
	if (!mutex_trylock(&sbi->extent_tree_lock))
550 551 552
		goto out;

	/* 1. remove unreferenced extent tree */
553
	list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
554 555
		if (atomic_read(&et->node_cnt)) {
			write_lock(&et->lock);
556
			node_cnt += __free_extent_tree(sbi, et);
557 558
			write_unlock(&et->lock);
		}
559
		f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
560 561 562 563 564 565
		list_del_init(&et->list);
		radix_tree_delete(&sbi->extent_tree_root, et->ino);
		kmem_cache_free(extent_tree_slab, et);
		atomic_dec(&sbi->total_ext_tree);
		atomic_dec(&sbi->total_zombie_tree);
		tree_cnt++;
566

567 568
		if (node_cnt + tree_cnt >= nr_shrink)
			goto unlock_out;
569
		cond_resched();
570
	}
571
	mutex_unlock(&sbi->extent_tree_lock);
572

573
free_node:
574
	/* 2. remove LRU extent entries */
575
	if (!mutex_trylock(&sbi->extent_tree_lock))
576 577 578 579 580
		goto out;

	remained = nr_shrink - (node_cnt + tree_cnt);

	spin_lock(&sbi->extent_lock);
581 582
	for (; remained > 0; remained--) {
		if (list_empty(&sbi->extent_list))
583
			break;
584 585 586 587 588 589 590 591
		en = list_first_entry(&sbi->extent_list,
					struct extent_node, list);
		et = en->et;
		if (!write_trylock(&et->lock)) {
			/* refresh this extent node's position in extent list */
			list_move_tail(&en->list, &sbi->extent_list);
			continue;
		}
592

593 594
		list_del_init(&en->list);
		spin_unlock(&sbi->extent_lock);
595

596
		__detach_extent_node(sbi, et, en);
597

598 599 600
		write_unlock(&et->lock);
		node_cnt++;
		spin_lock(&sbi->extent_lock);
601
	}
602 603
	spin_unlock(&sbi->extent_lock);

604
unlock_out:
605
	mutex_unlock(&sbi->extent_tree_lock);
606 607 608 609 610 611 612 613 614 615 616 617
out:
	trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);

	return node_cnt + tree_cnt;
}

unsigned int f2fs_destroy_extent_node(struct inode *inode)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	struct extent_tree *et = F2FS_I(inode)->extent_tree;
	unsigned int node_cnt = 0;

618
	if (!et || !atomic_read(&et->node_cnt))
619 620 621
		return 0;

	write_lock(&et->lock);
622
	node_cnt = __free_extent_tree(sbi, et);
623 624 625 626 627
	write_unlock(&et->lock);

	return node_cnt;
}

628 629 630 631 632 633 634 635 636 637 638 639 640
void f2fs_drop_extent_tree(struct inode *inode)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	struct extent_tree *et = F2FS_I(inode)->extent_tree;

	set_inode_flag(inode, FI_NO_EXTENT);

	write_lock(&et->lock);
	__free_extent_tree(sbi, et);
	__drop_largest_extent(inode, 0, UINT_MAX);
	write_unlock(&et->lock);
}

641 642 643 644 645 646 647 648 649
void f2fs_destroy_extent_tree(struct inode *inode)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	struct extent_tree *et = F2FS_I(inode)->extent_tree;
	unsigned int node_cnt = 0;

	if (!et)
		return;

650 651
	if (inode->i_nlink && !is_bad_inode(inode) &&
					atomic_read(&et->node_cnt)) {
652
		mutex_lock(&sbi->extent_tree_lock);
653
		list_add_tail(&et->list, &sbi->zombie_list);
654
		atomic_inc(&sbi->total_zombie_tree);
655
		mutex_unlock(&sbi->extent_tree_lock);
656 657 658 659 660 661 662
		return;
	}

	/* free all extent info belong to this extent tree */
	node_cnt = f2fs_destroy_extent_node(inode);

	/* delete extent tree entry in radix tree */
663
	mutex_lock(&sbi->extent_tree_lock);
664
	f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
665 666
	radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
	kmem_cache_free(extent_tree_slab, et);
667
	atomic_dec(&sbi->total_ext_tree);
668
	mutex_unlock(&sbi->extent_tree_lock);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

	F2FS_I(inode)->extent_tree = NULL;

	trace_f2fs_destroy_extent_tree(inode, node_cnt);
}

bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
					struct extent_info *ei)
{
	if (!f2fs_may_extent_tree(inode))
		return false;

	return f2fs_lookup_extent_tree(inode, pgofs, ei);
}

void f2fs_update_extent_cache(struct dnode_of_data *dn)
{
	pgoff_t fofs;
687
	block_t blkaddr;
688 689 690 691

	if (!f2fs_may_extent_tree(dn->inode))
		return;

692 693 694 695
	if (dn->data_blkaddr == NEW_ADDR)
		blkaddr = NULL_ADDR;
	else
		blkaddr = dn->data_blkaddr;
C
Chao Yu 已提交
696

697 698
	fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
								dn->ofs_in_node;
699
	f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
C
Chao Yu 已提交
700 701 702 703 704 705 706 707 708
}

void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
				pgoff_t fofs, block_t blkaddr, unsigned int len)

{
	if (!f2fs_may_extent_tree(dn->inode))
		return;

709
	f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
710 711 712 713 714
}

void init_extent_cache_info(struct f2fs_sb_info *sbi)
{
	INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
715
	mutex_init(&sbi->extent_tree_lock);
716 717
	INIT_LIST_HEAD(&sbi->extent_list);
	spin_lock_init(&sbi->extent_lock);
718
	atomic_set(&sbi->total_ext_tree, 0);
719
	INIT_LIST_HEAD(&sbi->zombie_list);
720
	atomic_set(&sbi->total_zombie_tree, 0);
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	atomic_set(&sbi->total_ext_node, 0);
}

int __init create_extent_cache(void)
{
	extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
			sizeof(struct extent_tree));
	if (!extent_tree_slab)
		return -ENOMEM;
	extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
			sizeof(struct extent_node));
	if (!extent_node_slab) {
		kmem_cache_destroy(extent_tree_slab);
		return -ENOMEM;
	}
	return 0;
}

void destroy_extent_cache(void)
{
	kmem_cache_destroy(extent_node_slab);
	kmem_cache_destroy(extent_tree_slab);
}