xref: /linux/fs/f2fs/extent_cache.c (revision 905e46acd3272d04566fec49afbd7ad9e2ed9ae3)
1 /*
2  * f2fs extent cache support
3  *
4  * Copyright (c) 2015 Motorola Mobility
5  * Copyright (c) 2015 Samsung Electronics
6  * Authors: Jaegeuk Kim <jaegeuk@kernel.org>
7  *          Chao Yu <chao2.yu@samsung.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/fs.h>
15 #include <linux/f2fs_fs.h>
16 
17 #include "f2fs.h"
18 #include "node.h"
19 #include <trace/events/f2fs.h>
20 
21 static struct rb_entry *__lookup_rb_tree_fast(struct rb_entry *cached_re,
22 							unsigned int ofs)
23 {
24 	if (cached_re) {
25 		if (cached_re->ofs <= ofs &&
26 				cached_re->ofs + cached_re->len > ofs) {
27 			return cached_re;
28 		}
29 	}
30 	return NULL;
31 }
32 
33 static struct rb_entry *__lookup_rb_tree_slow(struct rb_root *root,
34 							unsigned int ofs)
35 {
36 	struct rb_node *node = root->rb_node;
37 	struct rb_entry *re;
38 
39 	while (node) {
40 		re = rb_entry(node, struct rb_entry, rb_node);
41 
42 		if (ofs < re->ofs)
43 			node = node->rb_left;
44 		else if (ofs >= re->ofs + re->len)
45 			node = node->rb_right;
46 		else
47 			return re;
48 	}
49 	return NULL;
50 }
51 
52 struct rb_entry *__lookup_rb_tree(struct rb_root *root,
53 				struct rb_entry *cached_re, unsigned int ofs)
54 {
55 	struct rb_entry *re;
56 
57 	re = __lookup_rb_tree_fast(cached_re, ofs);
58 	if (!re)
59 		return __lookup_rb_tree_slow(root, ofs);
60 
61 	return re;
62 }
63 
64 struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
65 				struct rb_root *root, struct rb_node **parent,
66 				unsigned int ofs)
67 {
68 	struct rb_node **p = &root->rb_node;
69 	struct rb_entry *re;
70 
71 	while (*p) {
72 		*parent = *p;
73 		re = rb_entry(*parent, struct rb_entry, rb_node);
74 
75 		if (ofs < re->ofs)
76 			p = &(*p)->rb_left;
77 		else if (ofs >= re->ofs + re->len)
78 			p = &(*p)->rb_right;
79 		else
80 			f2fs_bug_on(sbi, 1);
81 	}
82 
83 	return p;
84 }
85 
86 /*
87  * lookup rb entry in position of @ofs in rb-tree,
88  * if hit, return the entry, otherwise, return NULL
89  * @prev_ex: extent before ofs
90  * @next_ex: extent after ofs
91  * @insert_p: insert point for new extent at ofs
92  * in order to simpfy the insertion after.
93  * tree must stay unchanged between lookup and insertion.
94  */
95 struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
96 				struct rb_entry *cached_re,
97 				unsigned int ofs,
98 				struct rb_entry **prev_entry,
99 				struct rb_entry **next_entry,
100 				struct rb_node ***insert_p,
101 				struct rb_node **insert_parent,
102 				bool force)
103 {
104 	struct rb_node **pnode = &root->rb_node;
105 	struct rb_node *parent = NULL, *tmp_node;
106 	struct rb_entry *re = cached_re;
107 
108 	*insert_p = NULL;
109 	*insert_parent = NULL;
110 	*prev_entry = NULL;
111 	*next_entry = NULL;
112 
113 	if (RB_EMPTY_ROOT(root))
114 		return NULL;
115 
116 	if (re) {
117 		if (re->ofs <= ofs && re->ofs + re->len > ofs)
118 			goto lookup_neighbors;
119 	}
120 
121 	while (*pnode) {
122 		parent = *pnode;
123 		re = rb_entry(*pnode, struct rb_entry, rb_node);
124 
125 		if (ofs < re->ofs)
126 			pnode = &(*pnode)->rb_left;
127 		else if (ofs >= re->ofs + re->len)
128 			pnode = &(*pnode)->rb_right;
129 		else
130 			goto lookup_neighbors;
131 	}
132 
133 	*insert_p = pnode;
134 	*insert_parent = parent;
135 
136 	re = rb_entry(parent, struct rb_entry, rb_node);
137 	tmp_node = parent;
138 	if (parent && ofs > re->ofs)
139 		tmp_node = rb_next(parent);
140 	*next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
141 
142 	tmp_node = parent;
143 	if (parent && ofs < re->ofs)
144 		tmp_node = rb_prev(parent);
145 	*prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
146 	return NULL;
147 
148 lookup_neighbors:
149 	if (ofs == re->ofs || force) {
150 		/* lookup prev node for merging backward later */
151 		tmp_node = rb_prev(&re->rb_node);
152 		*prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
153 	}
154 	if (ofs == re->ofs + re->len - 1 || force) {
155 		/* lookup next node for merging frontward later */
156 		tmp_node = rb_next(&re->rb_node);
157 		*next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node);
158 	}
159 	return re;
160 }
161 
162 bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
163 						struct rb_root *root)
164 {
165 #ifdef CONFIG_F2FS_CHECK_FS
166 	struct rb_node *cur = rb_first(root), *next;
167 	struct rb_entry *cur_re, *next_re;
168 
169 	if (!cur)
170 		return true;
171 
172 	while (cur) {
173 		next = rb_next(cur);
174 		if (!next)
175 			return true;
176 
177 		cur_re = rb_entry(cur, struct rb_entry, rb_node);
178 		next_re = rb_entry(next, struct rb_entry, rb_node);
179 
180 		if (cur_re->ofs + cur_re->len > next_re->ofs) {
181 			f2fs_msg(sbi->sb, KERN_INFO, "inconsistent rbtree, "
182 				"cur(%u, %u) next(%u, %u)",
183 				cur_re->ofs, cur_re->len,
184 				next_re->ofs, next_re->len);
185 			return false;
186 		}
187 
188 		cur = next;
189 	}
190 #endif
191 	return true;
192 }
193 
194 static struct kmem_cache *extent_tree_slab;
195 static struct kmem_cache *extent_node_slab;
196 
197 static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
198 				struct extent_tree *et, struct extent_info *ei,
199 				struct rb_node *parent, struct rb_node **p)
200 {
201 	struct extent_node *en;
202 
203 	en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
204 	if (!en)
205 		return NULL;
206 
207 	en->ei = *ei;
208 	INIT_LIST_HEAD(&en->list);
209 	en->et = et;
210 
211 	rb_link_node(&en->rb_node, parent, p);
212 	rb_insert_color(&en->rb_node, &et->root);
213 	atomic_inc(&et->node_cnt);
214 	atomic_inc(&sbi->total_ext_node);
215 	return en;
216 }
217 
218 static void __detach_extent_node(struct f2fs_sb_info *sbi,
219 				struct extent_tree *et, struct extent_node *en)
220 {
221 	rb_erase(&en->rb_node, &et->root);
222 	atomic_dec(&et->node_cnt);
223 	atomic_dec(&sbi->total_ext_node);
224 
225 	if (et->cached_en == en)
226 		et->cached_en = NULL;
227 	kmem_cache_free(extent_node_slab, en);
228 }
229 
230 /*
231  * Flow to release an extent_node:
232  * 1. list_del_init
233  * 2. __detach_extent_node
234  * 3. kmem_cache_free.
235  */
236 static void __release_extent_node(struct f2fs_sb_info *sbi,
237 			struct extent_tree *et, struct extent_node *en)
238 {
239 	spin_lock(&sbi->extent_lock);
240 	f2fs_bug_on(sbi, list_empty(&en->list));
241 	list_del_init(&en->list);
242 	spin_unlock(&sbi->extent_lock);
243 
244 	__detach_extent_node(sbi, et, en);
245 }
246 
247 static struct extent_tree *__grab_extent_tree(struct inode *inode)
248 {
249 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
250 	struct extent_tree *et;
251 	nid_t ino = inode->i_ino;
252 
253 	mutex_lock(&sbi->extent_tree_lock);
254 	et = radix_tree_lookup(&sbi->extent_tree_root, ino);
255 	if (!et) {
256 		et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
257 		f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
258 		memset(et, 0, sizeof(struct extent_tree));
259 		et->ino = ino;
260 		et->root = RB_ROOT;
261 		et->cached_en = NULL;
262 		rwlock_init(&et->lock);
263 		INIT_LIST_HEAD(&et->list);
264 		atomic_set(&et->node_cnt, 0);
265 		atomic_inc(&sbi->total_ext_tree);
266 	} else {
267 		atomic_dec(&sbi->total_zombie_tree);
268 		list_del_init(&et->list);
269 	}
270 	mutex_unlock(&sbi->extent_tree_lock);
271 
272 	/* never died until evict_inode */
273 	F2FS_I(inode)->extent_tree = et;
274 
275 	return et;
276 }
277 
278 static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
279 				struct extent_tree *et, struct extent_info *ei)
280 {
281 	struct rb_node **p = &et->root.rb_node;
282 	struct extent_node *en;
283 
284 	en = __attach_extent_node(sbi, et, ei, NULL, p);
285 	if (!en)
286 		return NULL;
287 
288 	et->largest = en->ei;
289 	et->cached_en = en;
290 	return en;
291 }
292 
293 static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
294 					struct extent_tree *et)
295 {
296 	struct rb_node *node, *next;
297 	struct extent_node *en;
298 	unsigned int count = atomic_read(&et->node_cnt);
299 
300 	node = rb_first(&et->root);
301 	while (node) {
302 		next = rb_next(node);
303 		en = rb_entry(node, struct extent_node, rb_node);
304 		__release_extent_node(sbi, et, en);
305 		node = next;
306 	}
307 
308 	return count - atomic_read(&et->node_cnt);
309 }
310 
311 static void __drop_largest_extent(struct inode *inode,
312 					pgoff_t fofs, unsigned int len)
313 {
314 	struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
315 
316 	if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs) {
317 		largest->len = 0;
318 		f2fs_mark_inode_dirty_sync(inode, true);
319 	}
320 }
321 
322 /* return true, if inode page is changed */
323 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
324 {
325 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
326 	struct extent_tree *et;
327 	struct extent_node *en;
328 	struct extent_info ei;
329 
330 	if (!f2fs_may_extent_tree(inode)) {
331 		/* drop largest extent */
332 		if (i_ext && i_ext->len) {
333 			i_ext->len = 0;
334 			return true;
335 		}
336 		return false;
337 	}
338 
339 	et = __grab_extent_tree(inode);
340 
341 	if (!i_ext || !i_ext->len)
342 		return false;
343 
344 	get_extent_info(&ei, i_ext);
345 
346 	write_lock(&et->lock);
347 	if (atomic_read(&et->node_cnt))
348 		goto out;
349 
350 	en = __init_extent_tree(sbi, et, &ei);
351 	if (en) {
352 		spin_lock(&sbi->extent_lock);
353 		list_add_tail(&en->list, &sbi->extent_list);
354 		spin_unlock(&sbi->extent_lock);
355 	}
356 out:
357 	write_unlock(&et->lock);
358 	return false;
359 }
360 
361 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
362 							struct extent_info *ei)
363 {
364 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
365 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
366 	struct extent_node *en;
367 	bool ret = false;
368 
369 	f2fs_bug_on(sbi, !et);
370 
371 	trace_f2fs_lookup_extent_tree_start(inode, pgofs);
372 
373 	read_lock(&et->lock);
374 
375 	if (et->largest.fofs <= pgofs &&
376 			et->largest.fofs + et->largest.len > pgofs) {
377 		*ei = et->largest;
378 		ret = true;
379 		stat_inc_largest_node_hit(sbi);
380 		goto out;
381 	}
382 
383 	en = (struct extent_node *)__lookup_rb_tree(&et->root,
384 				(struct rb_entry *)et->cached_en, pgofs);
385 	if (!en)
386 		goto out;
387 
388 	if (en == et->cached_en)
389 		stat_inc_cached_node_hit(sbi);
390 	else
391 		stat_inc_rbtree_node_hit(sbi);
392 
393 	*ei = en->ei;
394 	spin_lock(&sbi->extent_lock);
395 	if (!list_empty(&en->list)) {
396 		list_move_tail(&en->list, &sbi->extent_list);
397 		et->cached_en = en;
398 	}
399 	spin_unlock(&sbi->extent_lock);
400 	ret = true;
401 out:
402 	stat_inc_total_hit(sbi);
403 	read_unlock(&et->lock);
404 
405 	trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
406 	return ret;
407 }
408 
409 static struct extent_node *__try_merge_extent_node(struct inode *inode,
410 				struct extent_tree *et, struct extent_info *ei,
411 				struct extent_node *prev_ex,
412 				struct extent_node *next_ex)
413 {
414 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
415 	struct extent_node *en = NULL;
416 
417 	if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
418 		prev_ex->ei.len += ei->len;
419 		ei = &prev_ex->ei;
420 		en = prev_ex;
421 	}
422 
423 	if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
424 		next_ex->ei.fofs = ei->fofs;
425 		next_ex->ei.blk = ei->blk;
426 		next_ex->ei.len += ei->len;
427 		if (en)
428 			__release_extent_node(sbi, et, prev_ex);
429 
430 		en = next_ex;
431 	}
432 
433 	if (!en)
434 		return NULL;
435 
436 	__try_update_largest_extent(inode, et, en);
437 
438 	spin_lock(&sbi->extent_lock);
439 	if (!list_empty(&en->list)) {
440 		list_move_tail(&en->list, &sbi->extent_list);
441 		et->cached_en = en;
442 	}
443 	spin_unlock(&sbi->extent_lock);
444 	return en;
445 }
446 
447 static struct extent_node *__insert_extent_tree(struct inode *inode,
448 				struct extent_tree *et, struct extent_info *ei,
449 				struct rb_node **insert_p,
450 				struct rb_node *insert_parent)
451 {
452 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
453 	struct rb_node **p = &et->root.rb_node;
454 	struct rb_node *parent = NULL;
455 	struct extent_node *en = NULL;
456 
457 	if (insert_p && insert_parent) {
458 		parent = insert_parent;
459 		p = insert_p;
460 		goto do_insert;
461 	}
462 
463 	p = __lookup_rb_tree_for_insert(sbi, &et->root, &parent, ei->fofs);
464 do_insert:
465 	en = __attach_extent_node(sbi, et, ei, parent, p);
466 	if (!en)
467 		return NULL;
468 
469 	__try_update_largest_extent(inode, et, en);
470 
471 	/* update in global extent list */
472 	spin_lock(&sbi->extent_lock);
473 	list_add_tail(&en->list, &sbi->extent_list);
474 	et->cached_en = en;
475 	spin_unlock(&sbi->extent_lock);
476 	return en;
477 }
478 
479 static void f2fs_update_extent_tree_range(struct inode *inode,
480 				pgoff_t fofs, block_t blkaddr, unsigned int len)
481 {
482 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
483 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
484 	struct extent_node *en = NULL, *en1 = NULL;
485 	struct extent_node *prev_en = NULL, *next_en = NULL;
486 	struct extent_info ei, dei, prev;
487 	struct rb_node **insert_p = NULL, *insert_parent = NULL;
488 	unsigned int end = fofs + len;
489 	unsigned int pos = (unsigned int)fofs;
490 
491 	if (!et)
492 		return;
493 
494 	trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
495 
496 	write_lock(&et->lock);
497 
498 	if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
499 		write_unlock(&et->lock);
500 		return;
501 	}
502 
503 	prev = et->largest;
504 	dei.len = 0;
505 
506 	/*
507 	 * drop largest extent before lookup, in case it's already
508 	 * been shrunk from extent tree
509 	 */
510 	__drop_largest_extent(inode, fofs, len);
511 
512 	/* 1. lookup first extent node in range [fofs, fofs + len - 1] */
513 	en = (struct extent_node *)__lookup_rb_tree_ret(&et->root,
514 					(struct rb_entry *)et->cached_en, fofs,
515 					(struct rb_entry **)&prev_en,
516 					(struct rb_entry **)&next_en,
517 					&insert_p, &insert_parent, false);
518 	if (!en)
519 		en = next_en;
520 
521 	/* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
522 	while (en && en->ei.fofs < end) {
523 		unsigned int org_end;
524 		int parts = 0;	/* # of parts current extent split into */
525 
526 		next_en = en1 = NULL;
527 
528 		dei = en->ei;
529 		org_end = dei.fofs + dei.len;
530 		f2fs_bug_on(sbi, pos >= org_end);
531 
532 		if (pos > dei.fofs &&	pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
533 			en->ei.len = pos - en->ei.fofs;
534 			prev_en = en;
535 			parts = 1;
536 		}
537 
538 		if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
539 			if (parts) {
540 				set_extent_info(&ei, end,
541 						end - dei.fofs + dei.blk,
542 						org_end - end);
543 				en1 = __insert_extent_tree(inode, et, &ei,
544 							NULL, NULL);
545 				next_en = en1;
546 			} else {
547 				en->ei.fofs = end;
548 				en->ei.blk += end - dei.fofs;
549 				en->ei.len -= end - dei.fofs;
550 				next_en = en;
551 			}
552 			parts++;
553 		}
554 
555 		if (!next_en) {
556 			struct rb_node *node = rb_next(&en->rb_node);
557 
558 			next_en = rb_entry_safe(node, struct extent_node,
559 						rb_node);
560 		}
561 
562 		if (parts)
563 			__try_update_largest_extent(inode, et, en);
564 		else
565 			__release_extent_node(sbi, et, en);
566 
567 		/*
568 		 * if original extent is split into zero or two parts, extent
569 		 * tree has been altered by deletion or insertion, therefore
570 		 * invalidate pointers regard to tree.
571 		 */
572 		if (parts != 1) {
573 			insert_p = NULL;
574 			insert_parent = NULL;
575 		}
576 		en = next_en;
577 	}
578 
579 	/* 3. update extent in extent cache */
580 	if (blkaddr) {
581 
582 		set_extent_info(&ei, fofs, blkaddr, len);
583 		if (!__try_merge_extent_node(inode, et, &ei, prev_en, next_en))
584 			__insert_extent_tree(inode, et, &ei,
585 						insert_p, insert_parent);
586 
587 		/* give up extent_cache, if split and small updates happen */
588 		if (dei.len >= 1 &&
589 				prev.len < F2FS_MIN_EXTENT_LEN &&
590 				et->largest.len < F2FS_MIN_EXTENT_LEN) {
591 			__drop_largest_extent(inode, 0, UINT_MAX);
592 			set_inode_flag(inode, FI_NO_EXTENT);
593 		}
594 	}
595 
596 	if (is_inode_flag_set(inode, FI_NO_EXTENT))
597 		__free_extent_tree(sbi, et);
598 
599 	write_unlock(&et->lock);
600 }
601 
602 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
603 {
604 	struct extent_tree *et, *next;
605 	struct extent_node *en;
606 	unsigned int node_cnt = 0, tree_cnt = 0;
607 	int remained;
608 
609 	if (!test_opt(sbi, EXTENT_CACHE))
610 		return 0;
611 
612 	if (!atomic_read(&sbi->total_zombie_tree))
613 		goto free_node;
614 
615 	if (!mutex_trylock(&sbi->extent_tree_lock))
616 		goto out;
617 
618 	/* 1. remove unreferenced extent tree */
619 	list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
620 		if (atomic_read(&et->node_cnt)) {
621 			write_lock(&et->lock);
622 			node_cnt += __free_extent_tree(sbi, et);
623 			write_unlock(&et->lock);
624 		}
625 		f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
626 		list_del_init(&et->list);
627 		radix_tree_delete(&sbi->extent_tree_root, et->ino);
628 		kmem_cache_free(extent_tree_slab, et);
629 		atomic_dec(&sbi->total_ext_tree);
630 		atomic_dec(&sbi->total_zombie_tree);
631 		tree_cnt++;
632 
633 		if (node_cnt + tree_cnt >= nr_shrink)
634 			goto unlock_out;
635 		cond_resched();
636 	}
637 	mutex_unlock(&sbi->extent_tree_lock);
638 
639 free_node:
640 	/* 2. remove LRU extent entries */
641 	if (!mutex_trylock(&sbi->extent_tree_lock))
642 		goto out;
643 
644 	remained = nr_shrink - (node_cnt + tree_cnt);
645 
646 	spin_lock(&sbi->extent_lock);
647 	for (; remained > 0; remained--) {
648 		if (list_empty(&sbi->extent_list))
649 			break;
650 		en = list_first_entry(&sbi->extent_list,
651 					struct extent_node, list);
652 		et = en->et;
653 		if (!write_trylock(&et->lock)) {
654 			/* refresh this extent node's position in extent list */
655 			list_move_tail(&en->list, &sbi->extent_list);
656 			continue;
657 		}
658 
659 		list_del_init(&en->list);
660 		spin_unlock(&sbi->extent_lock);
661 
662 		__detach_extent_node(sbi, et, en);
663 
664 		write_unlock(&et->lock);
665 		node_cnt++;
666 		spin_lock(&sbi->extent_lock);
667 	}
668 	spin_unlock(&sbi->extent_lock);
669 
670 unlock_out:
671 	mutex_unlock(&sbi->extent_tree_lock);
672 out:
673 	trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
674 
675 	return node_cnt + tree_cnt;
676 }
677 
678 unsigned int f2fs_destroy_extent_node(struct inode *inode)
679 {
680 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
681 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
682 	unsigned int node_cnt = 0;
683 
684 	if (!et || !atomic_read(&et->node_cnt))
685 		return 0;
686 
687 	write_lock(&et->lock);
688 	node_cnt = __free_extent_tree(sbi, et);
689 	write_unlock(&et->lock);
690 
691 	return node_cnt;
692 }
693 
694 void f2fs_drop_extent_tree(struct inode *inode)
695 {
696 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
697 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
698 
699 	set_inode_flag(inode, FI_NO_EXTENT);
700 
701 	write_lock(&et->lock);
702 	__free_extent_tree(sbi, et);
703 	__drop_largest_extent(inode, 0, UINT_MAX);
704 	write_unlock(&et->lock);
705 }
706 
707 void f2fs_destroy_extent_tree(struct inode *inode)
708 {
709 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
710 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
711 	unsigned int node_cnt = 0;
712 
713 	if (!et)
714 		return;
715 
716 	if (inode->i_nlink && !is_bad_inode(inode) &&
717 					atomic_read(&et->node_cnt)) {
718 		mutex_lock(&sbi->extent_tree_lock);
719 		list_add_tail(&et->list, &sbi->zombie_list);
720 		atomic_inc(&sbi->total_zombie_tree);
721 		mutex_unlock(&sbi->extent_tree_lock);
722 		return;
723 	}
724 
725 	/* free all extent info belong to this extent tree */
726 	node_cnt = f2fs_destroy_extent_node(inode);
727 
728 	/* delete extent tree entry in radix tree */
729 	mutex_lock(&sbi->extent_tree_lock);
730 	f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
731 	radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
732 	kmem_cache_free(extent_tree_slab, et);
733 	atomic_dec(&sbi->total_ext_tree);
734 	mutex_unlock(&sbi->extent_tree_lock);
735 
736 	F2FS_I(inode)->extent_tree = NULL;
737 
738 	trace_f2fs_destroy_extent_tree(inode, node_cnt);
739 }
740 
741 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
742 					struct extent_info *ei)
743 {
744 	if (!f2fs_may_extent_tree(inode))
745 		return false;
746 
747 	return f2fs_lookup_extent_tree(inode, pgofs, ei);
748 }
749 
750 void f2fs_update_extent_cache(struct dnode_of_data *dn)
751 {
752 	pgoff_t fofs;
753 	block_t blkaddr;
754 
755 	if (!f2fs_may_extent_tree(dn->inode))
756 		return;
757 
758 	if (dn->data_blkaddr == NEW_ADDR)
759 		blkaddr = NULL_ADDR;
760 	else
761 		blkaddr = dn->data_blkaddr;
762 
763 	fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
764 								dn->ofs_in_node;
765 	f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
766 }
767 
768 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
769 				pgoff_t fofs, block_t blkaddr, unsigned int len)
770 
771 {
772 	if (!f2fs_may_extent_tree(dn->inode))
773 		return;
774 
775 	f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
776 }
777 
778 void init_extent_cache_info(struct f2fs_sb_info *sbi)
779 {
780 	INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
781 	mutex_init(&sbi->extent_tree_lock);
782 	INIT_LIST_HEAD(&sbi->extent_list);
783 	spin_lock_init(&sbi->extent_lock);
784 	atomic_set(&sbi->total_ext_tree, 0);
785 	INIT_LIST_HEAD(&sbi->zombie_list);
786 	atomic_set(&sbi->total_zombie_tree, 0);
787 	atomic_set(&sbi->total_ext_node, 0);
788 }
789 
790 int __init create_extent_cache(void)
791 {
792 	extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
793 			sizeof(struct extent_tree));
794 	if (!extent_tree_slab)
795 		return -ENOMEM;
796 	extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
797 			sizeof(struct extent_node));
798 	if (!extent_node_slab) {
799 		kmem_cache_destroy(extent_tree_slab);
800 		return -ENOMEM;
801 	}
802 	return 0;
803 }
804 
805 void destroy_extent_cache(void)
806 {
807 	kmem_cache_destroy(extent_node_slab);
808 	kmem_cache_destroy(extent_tree_slab);
809 }
810