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 
__lookup_rb_tree_fast(struct rb_entry * cached_re,unsigned int ofs)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 
__lookup_rb_tree_slow(struct rb_root * root,unsigned int ofs)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 
f2fs_lookup_rb_tree(struct rb_root * root,struct rb_entry * cached_re,unsigned int ofs)52 struct rb_entry *f2fs_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 
f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info * sbi,struct rb_root * root,struct rb_node ** parent,unsigned int ofs)64 struct rb_node **f2fs_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  */
f2fs_lookup_rb_tree_ret(struct rb_root * root,struct rb_entry * cached_re,unsigned int ofs,struct rb_entry ** prev_entry,struct rb_entry ** next_entry,struct rb_node *** insert_p,struct rb_node ** insert_parent,bool force)95 struct rb_entry *f2fs_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 
f2fs_check_rb_tree_consistence(struct f2fs_sb_info * sbi,struct rb_root * root)162 bool f2fs_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 
__attach_extent_node(struct f2fs_sb_info * sbi,struct extent_tree * et,struct extent_info * ei,struct rb_node * parent,struct rb_node ** p)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 
__detach_extent_node(struct f2fs_sb_info * sbi,struct extent_tree * et,struct extent_node * en)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  */
__release_extent_node(struct f2fs_sb_info * sbi,struct extent_tree * et,struct extent_node * en)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 
__grab_extent_tree(struct inode * inode)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 
__init_extent_tree(struct f2fs_sb_info * sbi,struct extent_tree * et,struct extent_info * ei)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 
__free_extent_tree(struct f2fs_sb_info * sbi,struct extent_tree * et)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 
__drop_largest_extent(struct extent_tree * et,pgoff_t fofs,unsigned int len)311 static void __drop_largest_extent(struct extent_tree *et,
312 					pgoff_t fofs, unsigned int len)
313 {
314 	if (fofs < et->largest.fofs + et->largest.len &&
315 			fofs + len > et->largest.fofs) {
316 		et->largest.len = 0;
317 		et->largest_updated = true;
318 	}
319 }
320 
321 /* return true, if inode page is changed */
__f2fs_init_extent_tree(struct inode * inode,struct f2fs_extent * i_ext)322 static bool __f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
323 {
324 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
325 	struct extent_tree *et;
326 	struct extent_node *en;
327 	struct extent_info ei;
328 
329 	if (!f2fs_may_extent_tree(inode)) {
330 		/* drop largest extent */
331 		if (i_ext && i_ext->len) {
332 			i_ext->len = 0;
333 			return true;
334 		}
335 		return false;
336 	}
337 
338 	et = __grab_extent_tree(inode);
339 
340 	if (!i_ext || !i_ext->len)
341 		return false;
342 
343 	get_extent_info(&ei, i_ext);
344 
345 	write_lock(&et->lock);
346 	if (atomic_read(&et->node_cnt))
347 		goto out;
348 
349 	en = __init_extent_tree(sbi, et, &ei);
350 	if (en) {
351 		spin_lock(&sbi->extent_lock);
352 		list_add_tail(&en->list, &sbi->extent_list);
353 		spin_unlock(&sbi->extent_lock);
354 	}
355 out:
356 	write_unlock(&et->lock);
357 	return false;
358 }
359 
f2fs_init_extent_tree(struct inode * inode,struct f2fs_extent * i_ext)360 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
361 {
362 	bool ret =  __f2fs_init_extent_tree(inode, i_ext);
363 
364 	if (!F2FS_I(inode)->extent_tree)
365 		set_inode_flag(inode, FI_NO_EXTENT);
366 
367 	return ret;
368 }
369 
f2fs_lookup_extent_tree(struct inode * inode,pgoff_t pgofs,struct extent_info * ei)370 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
371 							struct extent_info *ei)
372 {
373 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
374 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
375 	struct extent_node *en;
376 	bool ret = false;
377 
378 	if (!et)
379 		return false;
380 
381 	trace_f2fs_lookup_extent_tree_start(inode, pgofs);
382 
383 	read_lock(&et->lock);
384 
385 	if (et->largest.fofs <= pgofs &&
386 			et->largest.fofs + et->largest.len > pgofs) {
387 		*ei = et->largest;
388 		ret = true;
389 		stat_inc_largest_node_hit(sbi);
390 		goto out;
391 	}
392 
393 	en = (struct extent_node *)f2fs_lookup_rb_tree(&et->root,
394 				(struct rb_entry *)et->cached_en, pgofs);
395 	if (!en)
396 		goto out;
397 
398 	if (en == et->cached_en)
399 		stat_inc_cached_node_hit(sbi);
400 	else
401 		stat_inc_rbtree_node_hit(sbi);
402 
403 	*ei = en->ei;
404 	spin_lock(&sbi->extent_lock);
405 	if (!list_empty(&en->list)) {
406 		list_move_tail(&en->list, &sbi->extent_list);
407 		et->cached_en = en;
408 	}
409 	spin_unlock(&sbi->extent_lock);
410 	ret = true;
411 out:
412 	stat_inc_total_hit(sbi);
413 	read_unlock(&et->lock);
414 
415 	trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
416 	return ret;
417 }
418 
__try_merge_extent_node(struct f2fs_sb_info * sbi,struct extent_tree * et,struct extent_info * ei,struct extent_node * prev_ex,struct extent_node * next_ex)419 static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
420 				struct extent_tree *et, struct extent_info *ei,
421 				struct extent_node *prev_ex,
422 				struct extent_node *next_ex)
423 {
424 	struct extent_node *en = NULL;
425 
426 	if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
427 		prev_ex->ei.len += ei->len;
428 		ei = &prev_ex->ei;
429 		en = prev_ex;
430 	}
431 
432 	if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
433 		next_ex->ei.fofs = ei->fofs;
434 		next_ex->ei.blk = ei->blk;
435 		next_ex->ei.len += ei->len;
436 		if (en)
437 			__release_extent_node(sbi, et, prev_ex);
438 
439 		en = next_ex;
440 	}
441 
442 	if (!en)
443 		return NULL;
444 
445 	__try_update_largest_extent(et, en);
446 
447 	spin_lock(&sbi->extent_lock);
448 	if (!list_empty(&en->list)) {
449 		list_move_tail(&en->list, &sbi->extent_list);
450 		et->cached_en = en;
451 	}
452 	spin_unlock(&sbi->extent_lock);
453 	return en;
454 }
455 
__insert_extent_tree(struct f2fs_sb_info * sbi,struct extent_tree * et,struct extent_info * ei,struct rb_node ** insert_p,struct rb_node * insert_parent)456 static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
457 				struct extent_tree *et, struct extent_info *ei,
458 				struct rb_node **insert_p,
459 				struct rb_node *insert_parent)
460 {
461 	struct rb_node **p;
462 	struct rb_node *parent = NULL;
463 	struct extent_node *en = NULL;
464 
465 	if (insert_p && insert_parent) {
466 		parent = insert_parent;
467 		p = insert_p;
468 		goto do_insert;
469 	}
470 
471 	p = f2fs_lookup_rb_tree_for_insert(sbi, &et->root, &parent, ei->fofs);
472 do_insert:
473 	en = __attach_extent_node(sbi, et, ei, parent, p);
474 	if (!en)
475 		return NULL;
476 
477 	__try_update_largest_extent(et, en);
478 
479 	/* update in global extent list */
480 	spin_lock(&sbi->extent_lock);
481 	list_add_tail(&en->list, &sbi->extent_list);
482 	et->cached_en = en;
483 	spin_unlock(&sbi->extent_lock);
484 	return en;
485 }
486 
f2fs_update_extent_tree_range(struct inode * inode,pgoff_t fofs,block_t blkaddr,unsigned int len)487 static void f2fs_update_extent_tree_range(struct inode *inode,
488 				pgoff_t fofs, block_t blkaddr, unsigned int len)
489 {
490 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
491 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
492 	struct extent_node *en = NULL, *en1 = NULL;
493 	struct extent_node *prev_en = NULL, *next_en = NULL;
494 	struct extent_info ei, dei, prev;
495 	struct rb_node **insert_p = NULL, *insert_parent = NULL;
496 	unsigned int end = fofs + len;
497 	unsigned int pos = (unsigned int)fofs;
498 	bool updated = false;
499 
500 	if (!et)
501 		return;
502 
503 	trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
504 
505 	write_lock(&et->lock);
506 
507 	if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
508 		write_unlock(&et->lock);
509 		return;
510 	}
511 
512 	prev = et->largest;
513 	dei.len = 0;
514 
515 	/*
516 	 * drop largest extent before lookup, in case it's already
517 	 * been shrunk from extent tree
518 	 */
519 	__drop_largest_extent(et, fofs, len);
520 
521 	/* 1. lookup first extent node in range [fofs, fofs + len - 1] */
522 	en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root,
523 					(struct rb_entry *)et->cached_en, fofs,
524 					(struct rb_entry **)&prev_en,
525 					(struct rb_entry **)&next_en,
526 					&insert_p, &insert_parent, false);
527 	if (!en)
528 		en = next_en;
529 
530 	/* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
531 	while (en && en->ei.fofs < end) {
532 		unsigned int org_end;
533 		int parts = 0;	/* # of parts current extent split into */
534 
535 		next_en = en1 = NULL;
536 
537 		dei = en->ei;
538 		org_end = dei.fofs + dei.len;
539 		f2fs_bug_on(sbi, pos >= org_end);
540 
541 		if (pos > dei.fofs &&	pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
542 			en->ei.len = pos - en->ei.fofs;
543 			prev_en = en;
544 			parts = 1;
545 		}
546 
547 		if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
548 			if (parts) {
549 				set_extent_info(&ei, end,
550 						end - dei.fofs + dei.blk,
551 						org_end - end);
552 				en1 = __insert_extent_tree(sbi, et, &ei,
553 							NULL, NULL);
554 				next_en = en1;
555 			} else {
556 				en->ei.fofs = end;
557 				en->ei.blk += end - dei.fofs;
558 				en->ei.len -= end - dei.fofs;
559 				next_en = en;
560 			}
561 			parts++;
562 		}
563 
564 		if (!next_en) {
565 			struct rb_node *node = rb_next(&en->rb_node);
566 
567 			next_en = rb_entry_safe(node, struct extent_node,
568 						rb_node);
569 		}
570 
571 		if (parts)
572 			__try_update_largest_extent(et, en);
573 		else
574 			__release_extent_node(sbi, et, en);
575 
576 		/*
577 		 * if original extent is split into zero or two parts, extent
578 		 * tree has been altered by deletion or insertion, therefore
579 		 * invalidate pointers regard to tree.
580 		 */
581 		if (parts != 1) {
582 			insert_p = NULL;
583 			insert_parent = NULL;
584 		}
585 		en = next_en;
586 	}
587 
588 	/* 3. update extent in extent cache */
589 	if (blkaddr) {
590 
591 		set_extent_info(&ei, fofs, blkaddr, len);
592 		if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
593 			__insert_extent_tree(sbi, et, &ei,
594 						insert_p, insert_parent);
595 
596 		/* give up extent_cache, if split and small updates happen */
597 		if (dei.len >= 1 &&
598 				prev.len < F2FS_MIN_EXTENT_LEN &&
599 				et->largest.len < F2FS_MIN_EXTENT_LEN) {
600 			et->largest.len = 0;
601 			et->largest_updated = true;
602 			set_inode_flag(inode, FI_NO_EXTENT);
603 		}
604 	}
605 
606 	if (is_inode_flag_set(inode, FI_NO_EXTENT))
607 		__free_extent_tree(sbi, et);
608 
609 	if (et->largest_updated) {
610 		et->largest_updated = false;
611 		updated = true;
612 	}
613 
614 	write_unlock(&et->lock);
615 
616 	if (updated)
617 		f2fs_mark_inode_dirty_sync(inode, true);
618 }
619 
f2fs_shrink_extent_tree(struct f2fs_sb_info * sbi,int nr_shrink)620 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
621 {
622 	struct extent_tree *et, *next;
623 	struct extent_node *en;
624 	unsigned int node_cnt = 0, tree_cnt = 0;
625 	int remained;
626 
627 	if (!test_opt(sbi, EXTENT_CACHE))
628 		return 0;
629 
630 	if (!atomic_read(&sbi->total_zombie_tree))
631 		goto free_node;
632 
633 	if (!mutex_trylock(&sbi->extent_tree_lock))
634 		goto out;
635 
636 	/* 1. remove unreferenced extent tree */
637 	list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
638 		if (atomic_read(&et->node_cnt)) {
639 			write_lock(&et->lock);
640 			node_cnt += __free_extent_tree(sbi, et);
641 			write_unlock(&et->lock);
642 		}
643 		f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
644 		list_del_init(&et->list);
645 		radix_tree_delete(&sbi->extent_tree_root, et->ino);
646 		kmem_cache_free(extent_tree_slab, et);
647 		atomic_dec(&sbi->total_ext_tree);
648 		atomic_dec(&sbi->total_zombie_tree);
649 		tree_cnt++;
650 
651 		if (node_cnt + tree_cnt >= nr_shrink)
652 			goto unlock_out;
653 		cond_resched();
654 	}
655 	mutex_unlock(&sbi->extent_tree_lock);
656 
657 free_node:
658 	/* 2. remove LRU extent entries */
659 	if (!mutex_trylock(&sbi->extent_tree_lock))
660 		goto out;
661 
662 	remained = nr_shrink - (node_cnt + tree_cnt);
663 
664 	spin_lock(&sbi->extent_lock);
665 	for (; remained > 0; remained--) {
666 		if (list_empty(&sbi->extent_list))
667 			break;
668 		en = list_first_entry(&sbi->extent_list,
669 					struct extent_node, list);
670 		et = en->et;
671 		if (!write_trylock(&et->lock)) {
672 			/* refresh this extent node's position in extent list */
673 			list_move_tail(&en->list, &sbi->extent_list);
674 			continue;
675 		}
676 
677 		list_del_init(&en->list);
678 		spin_unlock(&sbi->extent_lock);
679 
680 		__detach_extent_node(sbi, et, en);
681 
682 		write_unlock(&et->lock);
683 		node_cnt++;
684 		spin_lock(&sbi->extent_lock);
685 	}
686 	spin_unlock(&sbi->extent_lock);
687 
688 unlock_out:
689 	mutex_unlock(&sbi->extent_tree_lock);
690 out:
691 	trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
692 
693 	return node_cnt + tree_cnt;
694 }
695 
f2fs_destroy_extent_node(struct inode * inode)696 unsigned int f2fs_destroy_extent_node(struct inode *inode)
697 {
698 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
699 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
700 	unsigned int node_cnt = 0;
701 
702 	if (!et || !atomic_read(&et->node_cnt))
703 		return 0;
704 
705 	write_lock(&et->lock);
706 	node_cnt = __free_extent_tree(sbi, et);
707 	write_unlock(&et->lock);
708 
709 	return node_cnt;
710 }
711 
f2fs_drop_extent_tree(struct inode * inode)712 void f2fs_drop_extent_tree(struct inode *inode)
713 {
714 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
715 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
716 	bool updated = false;
717 
718 	if (!f2fs_may_extent_tree(inode))
719 		return;
720 
721 	write_lock(&et->lock);
722 	set_inode_flag(inode, FI_NO_EXTENT);
723 	__free_extent_tree(sbi, et);
724 	if (et->largest.len) {
725 		et->largest.len = 0;
726 		updated = true;
727 	}
728 	write_unlock(&et->lock);
729 	if (updated)
730 		f2fs_mark_inode_dirty_sync(inode, true);
731 }
732 
f2fs_destroy_extent_tree(struct inode * inode)733 void f2fs_destroy_extent_tree(struct inode *inode)
734 {
735 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
736 	struct extent_tree *et = F2FS_I(inode)->extent_tree;
737 	unsigned int node_cnt = 0;
738 
739 	if (!et)
740 		return;
741 
742 	if (inode->i_nlink && !is_bad_inode(inode) &&
743 					atomic_read(&et->node_cnt)) {
744 		mutex_lock(&sbi->extent_tree_lock);
745 		list_add_tail(&et->list, &sbi->zombie_list);
746 		atomic_inc(&sbi->total_zombie_tree);
747 		mutex_unlock(&sbi->extent_tree_lock);
748 		return;
749 	}
750 
751 	/* free all extent info belong to this extent tree */
752 	node_cnt = f2fs_destroy_extent_node(inode);
753 
754 	/* delete extent tree entry in radix tree */
755 	mutex_lock(&sbi->extent_tree_lock);
756 	f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
757 	radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
758 	kmem_cache_free(extent_tree_slab, et);
759 	atomic_dec(&sbi->total_ext_tree);
760 	mutex_unlock(&sbi->extent_tree_lock);
761 
762 	F2FS_I(inode)->extent_tree = NULL;
763 
764 	trace_f2fs_destroy_extent_tree(inode, node_cnt);
765 }
766 
f2fs_lookup_extent_cache(struct inode * inode,pgoff_t pgofs,struct extent_info * ei)767 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
768 					struct extent_info *ei)
769 {
770 	if (!f2fs_may_extent_tree(inode))
771 		return false;
772 
773 	return f2fs_lookup_extent_tree(inode, pgofs, ei);
774 }
775 
f2fs_update_extent_cache(struct dnode_of_data * dn)776 void f2fs_update_extent_cache(struct dnode_of_data *dn)
777 {
778 	pgoff_t fofs;
779 	block_t blkaddr;
780 
781 	if (!f2fs_may_extent_tree(dn->inode))
782 		return;
783 
784 	if (dn->data_blkaddr == NEW_ADDR)
785 		blkaddr = NULL_ADDR;
786 	else
787 		blkaddr = dn->data_blkaddr;
788 
789 	fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
790 								dn->ofs_in_node;
791 	f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
792 }
793 
f2fs_update_extent_cache_range(struct dnode_of_data * dn,pgoff_t fofs,block_t blkaddr,unsigned int len)794 void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
795 				pgoff_t fofs, block_t blkaddr, unsigned int len)
796 
797 {
798 	if (!f2fs_may_extent_tree(dn->inode))
799 		return;
800 
801 	f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
802 }
803 
f2fs_init_extent_cache_info(struct f2fs_sb_info * sbi)804 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
805 {
806 	INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
807 	mutex_init(&sbi->extent_tree_lock);
808 	INIT_LIST_HEAD(&sbi->extent_list);
809 	spin_lock_init(&sbi->extent_lock);
810 	atomic_set(&sbi->total_ext_tree, 0);
811 	INIT_LIST_HEAD(&sbi->zombie_list);
812 	atomic_set(&sbi->total_zombie_tree, 0);
813 	atomic_set(&sbi->total_ext_node, 0);
814 }
815 
f2fs_create_extent_cache(void)816 int __init f2fs_create_extent_cache(void)
817 {
818 	extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
819 			sizeof(struct extent_tree));
820 	if (!extent_tree_slab)
821 		return -ENOMEM;
822 	extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
823 			sizeof(struct extent_node));
824 	if (!extent_node_slab) {
825 		kmem_cache_destroy(extent_tree_slab);
826 		return -ENOMEM;
827 	}
828 	return 0;
829 }
830 
f2fs_destroy_extent_cache(void)831 void f2fs_destroy_extent_cache(void)
832 {
833 	kmem_cache_destroy(extent_node_slab);
834 	kmem_cache_destroy(extent_tree_slab);
835 }
836