xref: /openbmc/linux/fs/f2fs/node.c (revision 03638e62)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/node.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/mpage.h>
11 #include <linux/backing-dev.h>
12 #include <linux/blkdev.h>
13 #include <linux/pagevec.h>
14 #include <linux/swap.h>
15 
16 #include "f2fs.h"
17 #include "node.h"
18 #include "segment.h"
19 #include "xattr.h"
20 #include "trace.h"
21 #include <trace/events/f2fs.h>
22 
23 #define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock)
24 
25 static struct kmem_cache *nat_entry_slab;
26 static struct kmem_cache *free_nid_slab;
27 static struct kmem_cache *nat_entry_set_slab;
28 static struct kmem_cache *fsync_node_entry_slab;
29 
30 /*
31  * Check whether the given nid is within node id range.
32  */
33 int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
34 {
35 	if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
36 		set_sbi_flag(sbi, SBI_NEED_FSCK);
37 		f2fs_warn(sbi, "%s: out-of-range nid=%x, run fsck to fix.",
38 			  __func__, nid);
39 		return -EFSCORRUPTED;
40 	}
41 	return 0;
42 }
43 
44 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
45 {
46 	struct f2fs_nm_info *nm_i = NM_I(sbi);
47 	struct sysinfo val;
48 	unsigned long avail_ram;
49 	unsigned long mem_size = 0;
50 	bool res = false;
51 
52 	si_meminfo(&val);
53 
54 	/* only uses low memory */
55 	avail_ram = val.totalram - val.totalhigh;
56 
57 	/*
58 	 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
59 	 */
60 	if (type == FREE_NIDS) {
61 		mem_size = (nm_i->nid_cnt[FREE_NID] *
62 				sizeof(struct free_nid)) >> PAGE_SHIFT;
63 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
64 	} else if (type == NAT_ENTRIES) {
65 		mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
66 							PAGE_SHIFT;
67 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
68 		if (excess_cached_nats(sbi))
69 			res = false;
70 	} else if (type == DIRTY_DENTS) {
71 		if (sbi->sb->s_bdi->wb.dirty_exceeded)
72 			return false;
73 		mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
74 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
75 	} else if (type == INO_ENTRIES) {
76 		int i;
77 
78 		for (i = 0; i < MAX_INO_ENTRY; i++)
79 			mem_size += sbi->im[i].ino_num *
80 						sizeof(struct ino_entry);
81 		mem_size >>= PAGE_SHIFT;
82 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
83 	} else if (type == EXTENT_CACHE) {
84 		mem_size = (atomic_read(&sbi->total_ext_tree) *
85 				sizeof(struct extent_tree) +
86 				atomic_read(&sbi->total_ext_node) *
87 				sizeof(struct extent_node)) >> PAGE_SHIFT;
88 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
89 	} else if (type == INMEM_PAGES) {
90 		/* it allows 20% / total_ram for inmemory pages */
91 		mem_size = get_pages(sbi, F2FS_INMEM_PAGES);
92 		res = mem_size < (val.totalram / 5);
93 	} else {
94 		if (!sbi->sb->s_bdi->wb.dirty_exceeded)
95 			return true;
96 	}
97 	return res;
98 }
99 
100 static void clear_node_page_dirty(struct page *page)
101 {
102 	if (PageDirty(page)) {
103 		f2fs_clear_page_cache_dirty_tag(page);
104 		clear_page_dirty_for_io(page);
105 		dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
106 	}
107 	ClearPageUptodate(page);
108 }
109 
110 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
111 {
112 	return f2fs_get_meta_page_nofail(sbi, current_nat_addr(sbi, nid));
113 }
114 
115 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
116 {
117 	struct page *src_page;
118 	struct page *dst_page;
119 	pgoff_t dst_off;
120 	void *src_addr;
121 	void *dst_addr;
122 	struct f2fs_nm_info *nm_i = NM_I(sbi);
123 
124 	dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
125 
126 	/* get current nat block page with lock */
127 	src_page = get_current_nat_page(sbi, nid);
128 	if (IS_ERR(src_page))
129 		return src_page;
130 	dst_page = f2fs_grab_meta_page(sbi, dst_off);
131 	f2fs_bug_on(sbi, PageDirty(src_page));
132 
133 	src_addr = page_address(src_page);
134 	dst_addr = page_address(dst_page);
135 	memcpy(dst_addr, src_addr, PAGE_SIZE);
136 	set_page_dirty(dst_page);
137 	f2fs_put_page(src_page, 1);
138 
139 	set_to_next_nat(nm_i, nid);
140 
141 	return dst_page;
142 }
143 
144 static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail)
145 {
146 	struct nat_entry *new;
147 
148 	if (no_fail)
149 		new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
150 	else
151 		new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
152 	if (new) {
153 		nat_set_nid(new, nid);
154 		nat_reset_flag(new);
155 	}
156 	return new;
157 }
158 
159 static void __free_nat_entry(struct nat_entry *e)
160 {
161 	kmem_cache_free(nat_entry_slab, e);
162 }
163 
164 /* must be locked by nat_tree_lock */
165 static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
166 	struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
167 {
168 	if (no_fail)
169 		f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
170 	else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
171 		return NULL;
172 
173 	if (raw_ne)
174 		node_info_from_raw_nat(&ne->ni, raw_ne);
175 
176 	spin_lock(&nm_i->nat_list_lock);
177 	list_add_tail(&ne->list, &nm_i->nat_entries);
178 	spin_unlock(&nm_i->nat_list_lock);
179 
180 	nm_i->nat_cnt++;
181 	return ne;
182 }
183 
184 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
185 {
186 	struct nat_entry *ne;
187 
188 	ne = radix_tree_lookup(&nm_i->nat_root, n);
189 
190 	/* for recent accessed nat entry, move it to tail of lru list */
191 	if (ne && !get_nat_flag(ne, IS_DIRTY)) {
192 		spin_lock(&nm_i->nat_list_lock);
193 		if (!list_empty(&ne->list))
194 			list_move_tail(&ne->list, &nm_i->nat_entries);
195 		spin_unlock(&nm_i->nat_list_lock);
196 	}
197 
198 	return ne;
199 }
200 
201 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
202 		nid_t start, unsigned int nr, struct nat_entry **ep)
203 {
204 	return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
205 }
206 
207 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
208 {
209 	radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
210 	nm_i->nat_cnt--;
211 	__free_nat_entry(e);
212 }
213 
214 static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
215 							struct nat_entry *ne)
216 {
217 	nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
218 	struct nat_entry_set *head;
219 
220 	head = radix_tree_lookup(&nm_i->nat_set_root, set);
221 	if (!head) {
222 		head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
223 
224 		INIT_LIST_HEAD(&head->entry_list);
225 		INIT_LIST_HEAD(&head->set_list);
226 		head->set = set;
227 		head->entry_cnt = 0;
228 		f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
229 	}
230 	return head;
231 }
232 
233 static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
234 						struct nat_entry *ne)
235 {
236 	struct nat_entry_set *head;
237 	bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
238 
239 	if (!new_ne)
240 		head = __grab_nat_entry_set(nm_i, ne);
241 
242 	/*
243 	 * update entry_cnt in below condition:
244 	 * 1. update NEW_ADDR to valid block address;
245 	 * 2. update old block address to new one;
246 	 */
247 	if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
248 				!get_nat_flag(ne, IS_DIRTY)))
249 		head->entry_cnt++;
250 
251 	set_nat_flag(ne, IS_PREALLOC, new_ne);
252 
253 	if (get_nat_flag(ne, IS_DIRTY))
254 		goto refresh_list;
255 
256 	nm_i->dirty_nat_cnt++;
257 	set_nat_flag(ne, IS_DIRTY, true);
258 refresh_list:
259 	spin_lock(&nm_i->nat_list_lock);
260 	if (new_ne)
261 		list_del_init(&ne->list);
262 	else
263 		list_move_tail(&ne->list, &head->entry_list);
264 	spin_unlock(&nm_i->nat_list_lock);
265 }
266 
267 static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
268 		struct nat_entry_set *set, struct nat_entry *ne)
269 {
270 	spin_lock(&nm_i->nat_list_lock);
271 	list_move_tail(&ne->list, &nm_i->nat_entries);
272 	spin_unlock(&nm_i->nat_list_lock);
273 
274 	set_nat_flag(ne, IS_DIRTY, false);
275 	set->entry_cnt--;
276 	nm_i->dirty_nat_cnt--;
277 }
278 
279 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
280 		nid_t start, unsigned int nr, struct nat_entry_set **ep)
281 {
282 	return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
283 							start, nr);
284 }
285 
286 bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
287 {
288 	return NODE_MAPPING(sbi) == page->mapping &&
289 			IS_DNODE(page) && is_cold_node(page);
290 }
291 
292 void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
293 {
294 	spin_lock_init(&sbi->fsync_node_lock);
295 	INIT_LIST_HEAD(&sbi->fsync_node_list);
296 	sbi->fsync_seg_id = 0;
297 	sbi->fsync_node_num = 0;
298 }
299 
300 static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
301 							struct page *page)
302 {
303 	struct fsync_node_entry *fn;
304 	unsigned long flags;
305 	unsigned int seq_id;
306 
307 	fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS);
308 
309 	get_page(page);
310 	fn->page = page;
311 	INIT_LIST_HEAD(&fn->list);
312 
313 	spin_lock_irqsave(&sbi->fsync_node_lock, flags);
314 	list_add_tail(&fn->list, &sbi->fsync_node_list);
315 	fn->seq_id = sbi->fsync_seg_id++;
316 	seq_id = fn->seq_id;
317 	sbi->fsync_node_num++;
318 	spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
319 
320 	return seq_id;
321 }
322 
323 void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
324 {
325 	struct fsync_node_entry *fn;
326 	unsigned long flags;
327 
328 	spin_lock_irqsave(&sbi->fsync_node_lock, flags);
329 	list_for_each_entry(fn, &sbi->fsync_node_list, list) {
330 		if (fn->page == page) {
331 			list_del(&fn->list);
332 			sbi->fsync_node_num--;
333 			spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
334 			kmem_cache_free(fsync_node_entry_slab, fn);
335 			put_page(page);
336 			return;
337 		}
338 	}
339 	spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
340 	f2fs_bug_on(sbi, 1);
341 }
342 
343 void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
344 {
345 	unsigned long flags;
346 
347 	spin_lock_irqsave(&sbi->fsync_node_lock, flags);
348 	sbi->fsync_seg_id = 0;
349 	spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
350 }
351 
352 int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
353 {
354 	struct f2fs_nm_info *nm_i = NM_I(sbi);
355 	struct nat_entry *e;
356 	bool need = false;
357 
358 	down_read(&nm_i->nat_tree_lock);
359 	e = __lookup_nat_cache(nm_i, nid);
360 	if (e) {
361 		if (!get_nat_flag(e, IS_CHECKPOINTED) &&
362 				!get_nat_flag(e, HAS_FSYNCED_INODE))
363 			need = true;
364 	}
365 	up_read(&nm_i->nat_tree_lock);
366 	return need;
367 }
368 
369 bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
370 {
371 	struct f2fs_nm_info *nm_i = NM_I(sbi);
372 	struct nat_entry *e;
373 	bool is_cp = true;
374 
375 	down_read(&nm_i->nat_tree_lock);
376 	e = __lookup_nat_cache(nm_i, nid);
377 	if (e && !get_nat_flag(e, IS_CHECKPOINTED))
378 		is_cp = false;
379 	up_read(&nm_i->nat_tree_lock);
380 	return is_cp;
381 }
382 
383 bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
384 {
385 	struct f2fs_nm_info *nm_i = NM_I(sbi);
386 	struct nat_entry *e;
387 	bool need_update = true;
388 
389 	down_read(&nm_i->nat_tree_lock);
390 	e = __lookup_nat_cache(nm_i, ino);
391 	if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
392 			(get_nat_flag(e, IS_CHECKPOINTED) ||
393 			 get_nat_flag(e, HAS_FSYNCED_INODE)))
394 		need_update = false;
395 	up_read(&nm_i->nat_tree_lock);
396 	return need_update;
397 }
398 
399 /* must be locked by nat_tree_lock */
400 static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
401 						struct f2fs_nat_entry *ne)
402 {
403 	struct f2fs_nm_info *nm_i = NM_I(sbi);
404 	struct nat_entry *new, *e;
405 
406 	new = __alloc_nat_entry(nid, false);
407 	if (!new)
408 		return;
409 
410 	down_write(&nm_i->nat_tree_lock);
411 	e = __lookup_nat_cache(nm_i, nid);
412 	if (!e)
413 		e = __init_nat_entry(nm_i, new, ne, false);
414 	else
415 		f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
416 				nat_get_blkaddr(e) !=
417 					le32_to_cpu(ne->block_addr) ||
418 				nat_get_version(e) != ne->version);
419 	up_write(&nm_i->nat_tree_lock);
420 	if (e != new)
421 		__free_nat_entry(new);
422 }
423 
424 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
425 			block_t new_blkaddr, bool fsync_done)
426 {
427 	struct f2fs_nm_info *nm_i = NM_I(sbi);
428 	struct nat_entry *e;
429 	struct nat_entry *new = __alloc_nat_entry(ni->nid, true);
430 
431 	down_write(&nm_i->nat_tree_lock);
432 	e = __lookup_nat_cache(nm_i, ni->nid);
433 	if (!e) {
434 		e = __init_nat_entry(nm_i, new, NULL, true);
435 		copy_node_info(&e->ni, ni);
436 		f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
437 	} else if (new_blkaddr == NEW_ADDR) {
438 		/*
439 		 * when nid is reallocated,
440 		 * previous nat entry can be remained in nat cache.
441 		 * So, reinitialize it with new information.
442 		 */
443 		copy_node_info(&e->ni, ni);
444 		f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
445 	}
446 	/* let's free early to reduce memory consumption */
447 	if (e != new)
448 		__free_nat_entry(new);
449 
450 	/* sanity check */
451 	f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
452 	f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
453 			new_blkaddr == NULL_ADDR);
454 	f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
455 			new_blkaddr == NEW_ADDR);
456 	f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) &&
457 			new_blkaddr == NEW_ADDR);
458 
459 	/* increment version no as node is removed */
460 	if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
461 		unsigned char version = nat_get_version(e);
462 		nat_set_version(e, inc_node_version(version));
463 	}
464 
465 	/* change address */
466 	nat_set_blkaddr(e, new_blkaddr);
467 	if (!__is_valid_data_blkaddr(new_blkaddr))
468 		set_nat_flag(e, IS_CHECKPOINTED, false);
469 	__set_nat_cache_dirty(nm_i, e);
470 
471 	/* update fsync_mark if its inode nat entry is still alive */
472 	if (ni->nid != ni->ino)
473 		e = __lookup_nat_cache(nm_i, ni->ino);
474 	if (e) {
475 		if (fsync_done && ni->nid == ni->ino)
476 			set_nat_flag(e, HAS_FSYNCED_INODE, true);
477 		set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
478 	}
479 	up_write(&nm_i->nat_tree_lock);
480 }
481 
482 int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
483 {
484 	struct f2fs_nm_info *nm_i = NM_I(sbi);
485 	int nr = nr_shrink;
486 
487 	if (!down_write_trylock(&nm_i->nat_tree_lock))
488 		return 0;
489 
490 	spin_lock(&nm_i->nat_list_lock);
491 	while (nr_shrink) {
492 		struct nat_entry *ne;
493 
494 		if (list_empty(&nm_i->nat_entries))
495 			break;
496 
497 		ne = list_first_entry(&nm_i->nat_entries,
498 					struct nat_entry, list);
499 		list_del(&ne->list);
500 		spin_unlock(&nm_i->nat_list_lock);
501 
502 		__del_from_nat_cache(nm_i, ne);
503 		nr_shrink--;
504 
505 		spin_lock(&nm_i->nat_list_lock);
506 	}
507 	spin_unlock(&nm_i->nat_list_lock);
508 
509 	up_write(&nm_i->nat_tree_lock);
510 	return nr - nr_shrink;
511 }
512 
513 /*
514  * This function always returns success
515  */
516 int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
517 						struct node_info *ni)
518 {
519 	struct f2fs_nm_info *nm_i = NM_I(sbi);
520 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
521 	struct f2fs_journal *journal = curseg->journal;
522 	nid_t start_nid = START_NID(nid);
523 	struct f2fs_nat_block *nat_blk;
524 	struct page *page = NULL;
525 	struct f2fs_nat_entry ne;
526 	struct nat_entry *e;
527 	pgoff_t index;
528 	block_t blkaddr;
529 	int i;
530 
531 	ni->nid = nid;
532 
533 	/* Check nat cache */
534 	down_read(&nm_i->nat_tree_lock);
535 	e = __lookup_nat_cache(nm_i, nid);
536 	if (e) {
537 		ni->ino = nat_get_ino(e);
538 		ni->blk_addr = nat_get_blkaddr(e);
539 		ni->version = nat_get_version(e);
540 		up_read(&nm_i->nat_tree_lock);
541 		return 0;
542 	}
543 
544 	memset(&ne, 0, sizeof(struct f2fs_nat_entry));
545 
546 	/* Check current segment summary */
547 	down_read(&curseg->journal_rwsem);
548 	i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
549 	if (i >= 0) {
550 		ne = nat_in_journal(journal, i);
551 		node_info_from_raw_nat(ni, &ne);
552 	}
553 	up_read(&curseg->journal_rwsem);
554 	if (i >= 0) {
555 		up_read(&nm_i->nat_tree_lock);
556 		goto cache;
557 	}
558 
559 	/* Fill node_info from nat page */
560 	index = current_nat_addr(sbi, nid);
561 	up_read(&nm_i->nat_tree_lock);
562 
563 	page = f2fs_get_meta_page(sbi, index);
564 	if (IS_ERR(page))
565 		return PTR_ERR(page);
566 
567 	nat_blk = (struct f2fs_nat_block *)page_address(page);
568 	ne = nat_blk->entries[nid - start_nid];
569 	node_info_from_raw_nat(ni, &ne);
570 	f2fs_put_page(page, 1);
571 cache:
572 	blkaddr = le32_to_cpu(ne.block_addr);
573 	if (__is_valid_data_blkaddr(blkaddr) &&
574 		!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE))
575 		return -EFAULT;
576 
577 	/* cache nat entry */
578 	cache_nat_entry(sbi, nid, &ne);
579 	return 0;
580 }
581 
582 /*
583  * readahead MAX_RA_NODE number of node pages.
584  */
585 static void f2fs_ra_node_pages(struct page *parent, int start, int n)
586 {
587 	struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
588 	struct blk_plug plug;
589 	int i, end;
590 	nid_t nid;
591 
592 	blk_start_plug(&plug);
593 
594 	/* Then, try readahead for siblings of the desired node */
595 	end = start + n;
596 	end = min(end, NIDS_PER_BLOCK);
597 	for (i = start; i < end; i++) {
598 		nid = get_nid(parent, i, false);
599 		f2fs_ra_node_page(sbi, nid);
600 	}
601 
602 	blk_finish_plug(&plug);
603 }
604 
605 pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
606 {
607 	const long direct_index = ADDRS_PER_INODE(dn->inode);
608 	const long direct_blks = ADDRS_PER_BLOCK(dn->inode);
609 	const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK;
610 	unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode);
611 	int cur_level = dn->cur_level;
612 	int max_level = dn->max_level;
613 	pgoff_t base = 0;
614 
615 	if (!dn->max_level)
616 		return pgofs + 1;
617 
618 	while (max_level-- > cur_level)
619 		skipped_unit *= NIDS_PER_BLOCK;
620 
621 	switch (dn->max_level) {
622 	case 3:
623 		base += 2 * indirect_blks;
624 		/* fall through */
625 	case 2:
626 		base += 2 * direct_blks;
627 		/* fall through */
628 	case 1:
629 		base += direct_index;
630 		break;
631 	default:
632 		f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
633 	}
634 
635 	return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
636 }
637 
638 /*
639  * The maximum depth is four.
640  * Offset[0] will have raw inode offset.
641  */
642 static int get_node_path(struct inode *inode, long block,
643 				int offset[4], unsigned int noffset[4])
644 {
645 	const long direct_index = ADDRS_PER_INODE(inode);
646 	const long direct_blks = ADDRS_PER_BLOCK(inode);
647 	const long dptrs_per_blk = NIDS_PER_BLOCK;
648 	const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK;
649 	const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
650 	int n = 0;
651 	int level = 0;
652 
653 	noffset[0] = 0;
654 
655 	if (block < direct_index) {
656 		offset[n] = block;
657 		goto got;
658 	}
659 	block -= direct_index;
660 	if (block < direct_blks) {
661 		offset[n++] = NODE_DIR1_BLOCK;
662 		noffset[n] = 1;
663 		offset[n] = block;
664 		level = 1;
665 		goto got;
666 	}
667 	block -= direct_blks;
668 	if (block < direct_blks) {
669 		offset[n++] = NODE_DIR2_BLOCK;
670 		noffset[n] = 2;
671 		offset[n] = block;
672 		level = 1;
673 		goto got;
674 	}
675 	block -= direct_blks;
676 	if (block < indirect_blks) {
677 		offset[n++] = NODE_IND1_BLOCK;
678 		noffset[n] = 3;
679 		offset[n++] = block / direct_blks;
680 		noffset[n] = 4 + offset[n - 1];
681 		offset[n] = block % direct_blks;
682 		level = 2;
683 		goto got;
684 	}
685 	block -= indirect_blks;
686 	if (block < indirect_blks) {
687 		offset[n++] = NODE_IND2_BLOCK;
688 		noffset[n] = 4 + dptrs_per_blk;
689 		offset[n++] = block / direct_blks;
690 		noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
691 		offset[n] = block % direct_blks;
692 		level = 2;
693 		goto got;
694 	}
695 	block -= indirect_blks;
696 	if (block < dindirect_blks) {
697 		offset[n++] = NODE_DIND_BLOCK;
698 		noffset[n] = 5 + (dptrs_per_blk * 2);
699 		offset[n++] = block / indirect_blks;
700 		noffset[n] = 6 + (dptrs_per_blk * 2) +
701 			      offset[n - 1] * (dptrs_per_blk + 1);
702 		offset[n++] = (block / direct_blks) % dptrs_per_blk;
703 		noffset[n] = 7 + (dptrs_per_blk * 2) +
704 			      offset[n - 2] * (dptrs_per_blk + 1) +
705 			      offset[n - 1];
706 		offset[n] = block % direct_blks;
707 		level = 3;
708 		goto got;
709 	} else {
710 		return -E2BIG;
711 	}
712 got:
713 	return level;
714 }
715 
716 /*
717  * Caller should call f2fs_put_dnode(dn).
718  * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
719  * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
720  * In the case of RDONLY_NODE, we don't need to care about mutex.
721  */
722 int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
723 {
724 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
725 	struct page *npage[4];
726 	struct page *parent = NULL;
727 	int offset[4];
728 	unsigned int noffset[4];
729 	nid_t nids[4];
730 	int level, i = 0;
731 	int err = 0;
732 
733 	level = get_node_path(dn->inode, index, offset, noffset);
734 	if (level < 0)
735 		return level;
736 
737 	nids[0] = dn->inode->i_ino;
738 	npage[0] = dn->inode_page;
739 
740 	if (!npage[0]) {
741 		npage[0] = f2fs_get_node_page(sbi, nids[0]);
742 		if (IS_ERR(npage[0]))
743 			return PTR_ERR(npage[0]);
744 	}
745 
746 	/* if inline_data is set, should not report any block indices */
747 	if (f2fs_has_inline_data(dn->inode) && index) {
748 		err = -ENOENT;
749 		f2fs_put_page(npage[0], 1);
750 		goto release_out;
751 	}
752 
753 	parent = npage[0];
754 	if (level != 0)
755 		nids[1] = get_nid(parent, offset[0], true);
756 	dn->inode_page = npage[0];
757 	dn->inode_page_locked = true;
758 
759 	/* get indirect or direct nodes */
760 	for (i = 1; i <= level; i++) {
761 		bool done = false;
762 
763 		if (!nids[i] && mode == ALLOC_NODE) {
764 			/* alloc new node */
765 			if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
766 				err = -ENOSPC;
767 				goto release_pages;
768 			}
769 
770 			dn->nid = nids[i];
771 			npage[i] = f2fs_new_node_page(dn, noffset[i]);
772 			if (IS_ERR(npage[i])) {
773 				f2fs_alloc_nid_failed(sbi, nids[i]);
774 				err = PTR_ERR(npage[i]);
775 				goto release_pages;
776 			}
777 
778 			set_nid(parent, offset[i - 1], nids[i], i == 1);
779 			f2fs_alloc_nid_done(sbi, nids[i]);
780 			done = true;
781 		} else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
782 			npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
783 			if (IS_ERR(npage[i])) {
784 				err = PTR_ERR(npage[i]);
785 				goto release_pages;
786 			}
787 			done = true;
788 		}
789 		if (i == 1) {
790 			dn->inode_page_locked = false;
791 			unlock_page(parent);
792 		} else {
793 			f2fs_put_page(parent, 1);
794 		}
795 
796 		if (!done) {
797 			npage[i] = f2fs_get_node_page(sbi, nids[i]);
798 			if (IS_ERR(npage[i])) {
799 				err = PTR_ERR(npage[i]);
800 				f2fs_put_page(npage[0], 0);
801 				goto release_out;
802 			}
803 		}
804 		if (i < level) {
805 			parent = npage[i];
806 			nids[i + 1] = get_nid(parent, offset[i], false);
807 		}
808 	}
809 	dn->nid = nids[level];
810 	dn->ofs_in_node = offset[level];
811 	dn->node_page = npage[level];
812 	dn->data_blkaddr = datablock_addr(dn->inode,
813 				dn->node_page, dn->ofs_in_node);
814 	return 0;
815 
816 release_pages:
817 	f2fs_put_page(parent, 1);
818 	if (i > 1)
819 		f2fs_put_page(npage[0], 0);
820 release_out:
821 	dn->inode_page = NULL;
822 	dn->node_page = NULL;
823 	if (err == -ENOENT) {
824 		dn->cur_level = i;
825 		dn->max_level = level;
826 		dn->ofs_in_node = offset[level];
827 	}
828 	return err;
829 }
830 
831 static int truncate_node(struct dnode_of_data *dn)
832 {
833 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
834 	struct node_info ni;
835 	int err;
836 	pgoff_t index;
837 
838 	err = f2fs_get_node_info(sbi, dn->nid, &ni);
839 	if (err)
840 		return err;
841 
842 	/* Deallocate node address */
843 	f2fs_invalidate_blocks(sbi, ni.blk_addr);
844 	dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
845 	set_node_addr(sbi, &ni, NULL_ADDR, false);
846 
847 	if (dn->nid == dn->inode->i_ino) {
848 		f2fs_remove_orphan_inode(sbi, dn->nid);
849 		dec_valid_inode_count(sbi);
850 		f2fs_inode_synced(dn->inode);
851 	}
852 
853 	clear_node_page_dirty(dn->node_page);
854 	set_sbi_flag(sbi, SBI_IS_DIRTY);
855 
856 	index = dn->node_page->index;
857 	f2fs_put_page(dn->node_page, 1);
858 
859 	invalidate_mapping_pages(NODE_MAPPING(sbi),
860 			index, index);
861 
862 	dn->node_page = NULL;
863 	trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
864 
865 	return 0;
866 }
867 
868 static int truncate_dnode(struct dnode_of_data *dn)
869 {
870 	struct page *page;
871 	int err;
872 
873 	if (dn->nid == 0)
874 		return 1;
875 
876 	/* get direct node */
877 	page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
878 	if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
879 		return 1;
880 	else if (IS_ERR(page))
881 		return PTR_ERR(page);
882 
883 	/* Make dnode_of_data for parameter */
884 	dn->node_page = page;
885 	dn->ofs_in_node = 0;
886 	f2fs_truncate_data_blocks(dn);
887 	err = truncate_node(dn);
888 	if (err)
889 		return err;
890 
891 	return 1;
892 }
893 
894 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
895 						int ofs, int depth)
896 {
897 	struct dnode_of_data rdn = *dn;
898 	struct page *page;
899 	struct f2fs_node *rn;
900 	nid_t child_nid;
901 	unsigned int child_nofs;
902 	int freed = 0;
903 	int i, ret;
904 
905 	if (dn->nid == 0)
906 		return NIDS_PER_BLOCK + 1;
907 
908 	trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
909 
910 	page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
911 	if (IS_ERR(page)) {
912 		trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
913 		return PTR_ERR(page);
914 	}
915 
916 	f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
917 
918 	rn = F2FS_NODE(page);
919 	if (depth < 3) {
920 		for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
921 			child_nid = le32_to_cpu(rn->in.nid[i]);
922 			if (child_nid == 0)
923 				continue;
924 			rdn.nid = child_nid;
925 			ret = truncate_dnode(&rdn);
926 			if (ret < 0)
927 				goto out_err;
928 			if (set_nid(page, i, 0, false))
929 				dn->node_changed = true;
930 		}
931 	} else {
932 		child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
933 		for (i = ofs; i < NIDS_PER_BLOCK; i++) {
934 			child_nid = le32_to_cpu(rn->in.nid[i]);
935 			if (child_nid == 0) {
936 				child_nofs += NIDS_PER_BLOCK + 1;
937 				continue;
938 			}
939 			rdn.nid = child_nid;
940 			ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
941 			if (ret == (NIDS_PER_BLOCK + 1)) {
942 				if (set_nid(page, i, 0, false))
943 					dn->node_changed = true;
944 				child_nofs += ret;
945 			} else if (ret < 0 && ret != -ENOENT) {
946 				goto out_err;
947 			}
948 		}
949 		freed = child_nofs;
950 	}
951 
952 	if (!ofs) {
953 		/* remove current indirect node */
954 		dn->node_page = page;
955 		ret = truncate_node(dn);
956 		if (ret)
957 			goto out_err;
958 		freed++;
959 	} else {
960 		f2fs_put_page(page, 1);
961 	}
962 	trace_f2fs_truncate_nodes_exit(dn->inode, freed);
963 	return freed;
964 
965 out_err:
966 	f2fs_put_page(page, 1);
967 	trace_f2fs_truncate_nodes_exit(dn->inode, ret);
968 	return ret;
969 }
970 
971 static int truncate_partial_nodes(struct dnode_of_data *dn,
972 			struct f2fs_inode *ri, int *offset, int depth)
973 {
974 	struct page *pages[2];
975 	nid_t nid[3];
976 	nid_t child_nid;
977 	int err = 0;
978 	int i;
979 	int idx = depth - 2;
980 
981 	nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
982 	if (!nid[0])
983 		return 0;
984 
985 	/* get indirect nodes in the path */
986 	for (i = 0; i < idx + 1; i++) {
987 		/* reference count'll be increased */
988 		pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
989 		if (IS_ERR(pages[i])) {
990 			err = PTR_ERR(pages[i]);
991 			idx = i - 1;
992 			goto fail;
993 		}
994 		nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
995 	}
996 
997 	f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
998 
999 	/* free direct nodes linked to a partial indirect node */
1000 	for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
1001 		child_nid = get_nid(pages[idx], i, false);
1002 		if (!child_nid)
1003 			continue;
1004 		dn->nid = child_nid;
1005 		err = truncate_dnode(dn);
1006 		if (err < 0)
1007 			goto fail;
1008 		if (set_nid(pages[idx], i, 0, false))
1009 			dn->node_changed = true;
1010 	}
1011 
1012 	if (offset[idx + 1] == 0) {
1013 		dn->node_page = pages[idx];
1014 		dn->nid = nid[idx];
1015 		err = truncate_node(dn);
1016 		if (err)
1017 			goto fail;
1018 	} else {
1019 		f2fs_put_page(pages[idx], 1);
1020 	}
1021 	offset[idx]++;
1022 	offset[idx + 1] = 0;
1023 	idx--;
1024 fail:
1025 	for (i = idx; i >= 0; i--)
1026 		f2fs_put_page(pages[i], 1);
1027 
1028 	trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1029 
1030 	return err;
1031 }
1032 
1033 /*
1034  * All the block addresses of data and nodes should be nullified.
1035  */
1036 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
1037 {
1038 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1039 	int err = 0, cont = 1;
1040 	int level, offset[4], noffset[4];
1041 	unsigned int nofs = 0;
1042 	struct f2fs_inode *ri;
1043 	struct dnode_of_data dn;
1044 	struct page *page;
1045 
1046 	trace_f2fs_truncate_inode_blocks_enter(inode, from);
1047 
1048 	level = get_node_path(inode, from, offset, noffset);
1049 	if (level < 0)
1050 		return level;
1051 
1052 	page = f2fs_get_node_page(sbi, inode->i_ino);
1053 	if (IS_ERR(page)) {
1054 		trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
1055 		return PTR_ERR(page);
1056 	}
1057 
1058 	set_new_dnode(&dn, inode, page, NULL, 0);
1059 	unlock_page(page);
1060 
1061 	ri = F2FS_INODE(page);
1062 	switch (level) {
1063 	case 0:
1064 	case 1:
1065 		nofs = noffset[1];
1066 		break;
1067 	case 2:
1068 		nofs = noffset[1];
1069 		if (!offset[level - 1])
1070 			goto skip_partial;
1071 		err = truncate_partial_nodes(&dn, ri, offset, level);
1072 		if (err < 0 && err != -ENOENT)
1073 			goto fail;
1074 		nofs += 1 + NIDS_PER_BLOCK;
1075 		break;
1076 	case 3:
1077 		nofs = 5 + 2 * NIDS_PER_BLOCK;
1078 		if (!offset[level - 1])
1079 			goto skip_partial;
1080 		err = truncate_partial_nodes(&dn, ri, offset, level);
1081 		if (err < 0 && err != -ENOENT)
1082 			goto fail;
1083 		break;
1084 	default:
1085 		BUG();
1086 	}
1087 
1088 skip_partial:
1089 	while (cont) {
1090 		dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
1091 		switch (offset[0]) {
1092 		case NODE_DIR1_BLOCK:
1093 		case NODE_DIR2_BLOCK:
1094 			err = truncate_dnode(&dn);
1095 			break;
1096 
1097 		case NODE_IND1_BLOCK:
1098 		case NODE_IND2_BLOCK:
1099 			err = truncate_nodes(&dn, nofs, offset[1], 2);
1100 			break;
1101 
1102 		case NODE_DIND_BLOCK:
1103 			err = truncate_nodes(&dn, nofs, offset[1], 3);
1104 			cont = 0;
1105 			break;
1106 
1107 		default:
1108 			BUG();
1109 		}
1110 		if (err < 0 && err != -ENOENT)
1111 			goto fail;
1112 		if (offset[1] == 0 &&
1113 				ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
1114 			lock_page(page);
1115 			BUG_ON(page->mapping != NODE_MAPPING(sbi));
1116 			f2fs_wait_on_page_writeback(page, NODE, true, true);
1117 			ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
1118 			set_page_dirty(page);
1119 			unlock_page(page);
1120 		}
1121 		offset[1] = 0;
1122 		offset[0]++;
1123 		nofs += err;
1124 	}
1125 fail:
1126 	f2fs_put_page(page, 0);
1127 	trace_f2fs_truncate_inode_blocks_exit(inode, err);
1128 	return err > 0 ? 0 : err;
1129 }
1130 
1131 /* caller must lock inode page */
1132 int f2fs_truncate_xattr_node(struct inode *inode)
1133 {
1134 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1135 	nid_t nid = F2FS_I(inode)->i_xattr_nid;
1136 	struct dnode_of_data dn;
1137 	struct page *npage;
1138 	int err;
1139 
1140 	if (!nid)
1141 		return 0;
1142 
1143 	npage = f2fs_get_node_page(sbi, nid);
1144 	if (IS_ERR(npage))
1145 		return PTR_ERR(npage);
1146 
1147 	set_new_dnode(&dn, inode, NULL, npage, nid);
1148 	err = truncate_node(&dn);
1149 	if (err) {
1150 		f2fs_put_page(npage, 1);
1151 		return err;
1152 	}
1153 
1154 	f2fs_i_xnid_write(inode, 0);
1155 
1156 	return 0;
1157 }
1158 
1159 /*
1160  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1161  * f2fs_unlock_op().
1162  */
1163 int f2fs_remove_inode_page(struct inode *inode)
1164 {
1165 	struct dnode_of_data dn;
1166 	int err;
1167 
1168 	set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1169 	err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
1170 	if (err)
1171 		return err;
1172 
1173 	err = f2fs_truncate_xattr_node(inode);
1174 	if (err) {
1175 		f2fs_put_dnode(&dn);
1176 		return err;
1177 	}
1178 
1179 	/* remove potential inline_data blocks */
1180 	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1181 				S_ISLNK(inode->i_mode))
1182 		f2fs_truncate_data_blocks_range(&dn, 1);
1183 
1184 	/* 0 is possible, after f2fs_new_inode() has failed */
1185 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1186 		f2fs_put_dnode(&dn);
1187 		return -EIO;
1188 	}
1189 
1190 	if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
1191 		f2fs_warn(F2FS_I_SB(inode), "Inconsistent i_blocks, ino:%lu, iblocks:%llu",
1192 			  inode->i_ino, (unsigned long long)inode->i_blocks);
1193 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
1194 	}
1195 
1196 	/* will put inode & node pages */
1197 	err = truncate_node(&dn);
1198 	if (err) {
1199 		f2fs_put_dnode(&dn);
1200 		return err;
1201 	}
1202 	return 0;
1203 }
1204 
1205 struct page *f2fs_new_inode_page(struct inode *inode)
1206 {
1207 	struct dnode_of_data dn;
1208 
1209 	/* allocate inode page for new inode */
1210 	set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1211 
1212 	/* caller should f2fs_put_page(page, 1); */
1213 	return f2fs_new_node_page(&dn, 0);
1214 }
1215 
1216 struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
1217 {
1218 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1219 	struct node_info new_ni;
1220 	struct page *page;
1221 	int err;
1222 
1223 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1224 		return ERR_PTR(-EPERM);
1225 
1226 	page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
1227 	if (!page)
1228 		return ERR_PTR(-ENOMEM);
1229 
1230 	if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
1231 		goto fail;
1232 
1233 #ifdef CONFIG_F2FS_CHECK_FS
1234 	err = f2fs_get_node_info(sbi, dn->nid, &new_ni);
1235 	if (err) {
1236 		dec_valid_node_count(sbi, dn->inode, !ofs);
1237 		goto fail;
1238 	}
1239 	f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
1240 #endif
1241 	new_ni.nid = dn->nid;
1242 	new_ni.ino = dn->inode->i_ino;
1243 	new_ni.blk_addr = NULL_ADDR;
1244 	new_ni.flag = 0;
1245 	new_ni.version = 0;
1246 	set_node_addr(sbi, &new_ni, NEW_ADDR, false);
1247 
1248 	f2fs_wait_on_page_writeback(page, NODE, true, true);
1249 	fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
1250 	set_cold_node(page, S_ISDIR(dn->inode->i_mode));
1251 	if (!PageUptodate(page))
1252 		SetPageUptodate(page);
1253 	if (set_page_dirty(page))
1254 		dn->node_changed = true;
1255 
1256 	if (f2fs_has_xattr_block(ofs))
1257 		f2fs_i_xnid_write(dn->inode, dn->nid);
1258 
1259 	if (ofs == 0)
1260 		inc_valid_inode_count(sbi);
1261 	return page;
1262 
1263 fail:
1264 	clear_node_page_dirty(page);
1265 	f2fs_put_page(page, 1);
1266 	return ERR_PTR(err);
1267 }
1268 
1269 /*
1270  * Caller should do after getting the following values.
1271  * 0: f2fs_put_page(page, 0)
1272  * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1273  */
1274 static int read_node_page(struct page *page, int op_flags)
1275 {
1276 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1277 	struct node_info ni;
1278 	struct f2fs_io_info fio = {
1279 		.sbi = sbi,
1280 		.type = NODE,
1281 		.op = REQ_OP_READ,
1282 		.op_flags = op_flags,
1283 		.page = page,
1284 		.encrypted_page = NULL,
1285 	};
1286 	int err;
1287 
1288 	if (PageUptodate(page)) {
1289 		if (!f2fs_inode_chksum_verify(sbi, page)) {
1290 			ClearPageUptodate(page);
1291 			return -EFSBADCRC;
1292 		}
1293 		return LOCKED_PAGE;
1294 	}
1295 
1296 	err = f2fs_get_node_info(sbi, page->index, &ni);
1297 	if (err)
1298 		return err;
1299 
1300 	if (unlikely(ni.blk_addr == NULL_ADDR) ||
1301 			is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) {
1302 		ClearPageUptodate(page);
1303 		return -ENOENT;
1304 	}
1305 
1306 	fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
1307 	return f2fs_submit_page_bio(&fio);
1308 }
1309 
1310 /*
1311  * Readahead a node page
1312  */
1313 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1314 {
1315 	struct page *apage;
1316 	int err;
1317 
1318 	if (!nid)
1319 		return;
1320 	if (f2fs_check_nid_range(sbi, nid))
1321 		return;
1322 
1323 	apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
1324 	if (apage)
1325 		return;
1326 
1327 	apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1328 	if (!apage)
1329 		return;
1330 
1331 	err = read_node_page(apage, REQ_RAHEAD);
1332 	f2fs_put_page(apage, err ? 1 : 0);
1333 }
1334 
1335 static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
1336 					struct page *parent, int start)
1337 {
1338 	struct page *page;
1339 	int err;
1340 
1341 	if (!nid)
1342 		return ERR_PTR(-ENOENT);
1343 	if (f2fs_check_nid_range(sbi, nid))
1344 		return ERR_PTR(-EINVAL);
1345 repeat:
1346 	page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1347 	if (!page)
1348 		return ERR_PTR(-ENOMEM);
1349 
1350 	err = read_node_page(page, 0);
1351 	if (err < 0) {
1352 		f2fs_put_page(page, 1);
1353 		return ERR_PTR(err);
1354 	} else if (err == LOCKED_PAGE) {
1355 		err = 0;
1356 		goto page_hit;
1357 	}
1358 
1359 	if (parent)
1360 		f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
1361 
1362 	lock_page(page);
1363 
1364 	if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1365 		f2fs_put_page(page, 1);
1366 		goto repeat;
1367 	}
1368 
1369 	if (unlikely(!PageUptodate(page))) {
1370 		err = -EIO;
1371 		goto out_err;
1372 	}
1373 
1374 	if (!f2fs_inode_chksum_verify(sbi, page)) {
1375 		err = -EFSBADCRC;
1376 		goto out_err;
1377 	}
1378 page_hit:
1379 	if(unlikely(nid != nid_of_node(page))) {
1380 		f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1381 			  nid, nid_of_node(page), ino_of_node(page),
1382 			  ofs_of_node(page), cpver_of_node(page),
1383 			  next_blkaddr_of_node(page));
1384 		err = -EINVAL;
1385 out_err:
1386 		ClearPageUptodate(page);
1387 		f2fs_put_page(page, 1);
1388 		return ERR_PTR(err);
1389 	}
1390 	return page;
1391 }
1392 
1393 struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1394 {
1395 	return __get_node_page(sbi, nid, NULL, 0);
1396 }
1397 
1398 struct page *f2fs_get_node_page_ra(struct page *parent, int start)
1399 {
1400 	struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
1401 	nid_t nid = get_nid(parent, start, false);
1402 
1403 	return __get_node_page(sbi, nid, parent, start);
1404 }
1405 
1406 static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1407 {
1408 	struct inode *inode;
1409 	struct page *page;
1410 	int ret;
1411 
1412 	/* should flush inline_data before evict_inode */
1413 	inode = ilookup(sbi->sb, ino);
1414 	if (!inode)
1415 		return;
1416 
1417 	page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1418 					FGP_LOCK|FGP_NOWAIT, 0);
1419 	if (!page)
1420 		goto iput_out;
1421 
1422 	if (!PageUptodate(page))
1423 		goto page_out;
1424 
1425 	if (!PageDirty(page))
1426 		goto page_out;
1427 
1428 	if (!clear_page_dirty_for_io(page))
1429 		goto page_out;
1430 
1431 	ret = f2fs_write_inline_data(inode, page);
1432 	inode_dec_dirty_pages(inode);
1433 	f2fs_remove_dirty_inode(inode);
1434 	if (ret)
1435 		set_page_dirty(page);
1436 page_out:
1437 	f2fs_put_page(page, 1);
1438 iput_out:
1439 	iput(inode);
1440 }
1441 
1442 static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
1443 {
1444 	pgoff_t index;
1445 	struct pagevec pvec;
1446 	struct page *last_page = NULL;
1447 	int nr_pages;
1448 
1449 	pagevec_init(&pvec);
1450 	index = 0;
1451 
1452 	while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1453 				PAGECACHE_TAG_DIRTY))) {
1454 		int i;
1455 
1456 		for (i = 0; i < nr_pages; i++) {
1457 			struct page *page = pvec.pages[i];
1458 
1459 			if (unlikely(f2fs_cp_error(sbi))) {
1460 				f2fs_put_page(last_page, 0);
1461 				pagevec_release(&pvec);
1462 				return ERR_PTR(-EIO);
1463 			}
1464 
1465 			if (!IS_DNODE(page) || !is_cold_node(page))
1466 				continue;
1467 			if (ino_of_node(page) != ino)
1468 				continue;
1469 
1470 			lock_page(page);
1471 
1472 			if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1473 continue_unlock:
1474 				unlock_page(page);
1475 				continue;
1476 			}
1477 			if (ino_of_node(page) != ino)
1478 				goto continue_unlock;
1479 
1480 			if (!PageDirty(page)) {
1481 				/* someone wrote it for us */
1482 				goto continue_unlock;
1483 			}
1484 
1485 			if (last_page)
1486 				f2fs_put_page(last_page, 0);
1487 
1488 			get_page(page);
1489 			last_page = page;
1490 			unlock_page(page);
1491 		}
1492 		pagevec_release(&pvec);
1493 		cond_resched();
1494 	}
1495 	return last_page;
1496 }
1497 
1498 static int __write_node_page(struct page *page, bool atomic, bool *submitted,
1499 				struct writeback_control *wbc, bool do_balance,
1500 				enum iostat_type io_type, unsigned int *seq_id)
1501 {
1502 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1503 	nid_t nid;
1504 	struct node_info ni;
1505 	struct f2fs_io_info fio = {
1506 		.sbi = sbi,
1507 		.ino = ino_of_node(page),
1508 		.type = NODE,
1509 		.op = REQ_OP_WRITE,
1510 		.op_flags = wbc_to_write_flags(wbc),
1511 		.page = page,
1512 		.encrypted_page = NULL,
1513 		.submitted = false,
1514 		.io_type = io_type,
1515 		.io_wbc = wbc,
1516 	};
1517 	unsigned int seq;
1518 
1519 	trace_f2fs_writepage(page, NODE);
1520 
1521 	if (unlikely(f2fs_cp_error(sbi)))
1522 		goto redirty_out;
1523 
1524 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1525 		goto redirty_out;
1526 
1527 	if (wbc->sync_mode == WB_SYNC_NONE &&
1528 			IS_DNODE(page) && is_cold_node(page))
1529 		goto redirty_out;
1530 
1531 	/* get old block addr of this node page */
1532 	nid = nid_of_node(page);
1533 	f2fs_bug_on(sbi, page->index != nid);
1534 
1535 	if (f2fs_get_node_info(sbi, nid, &ni))
1536 		goto redirty_out;
1537 
1538 	if (wbc->for_reclaim) {
1539 		if (!down_read_trylock(&sbi->node_write))
1540 			goto redirty_out;
1541 	} else {
1542 		down_read(&sbi->node_write);
1543 	}
1544 
1545 	/* This page is already truncated */
1546 	if (unlikely(ni.blk_addr == NULL_ADDR)) {
1547 		ClearPageUptodate(page);
1548 		dec_page_count(sbi, F2FS_DIRTY_NODES);
1549 		up_read(&sbi->node_write);
1550 		unlock_page(page);
1551 		return 0;
1552 	}
1553 
1554 	if (__is_valid_data_blkaddr(ni.blk_addr) &&
1555 		!f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
1556 					DATA_GENERIC_ENHANCE)) {
1557 		up_read(&sbi->node_write);
1558 		goto redirty_out;
1559 	}
1560 
1561 	if (atomic && !test_opt(sbi, NOBARRIER))
1562 		fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1563 
1564 	set_page_writeback(page);
1565 	ClearPageError(page);
1566 
1567 	if (f2fs_in_warm_node_list(sbi, page)) {
1568 		seq = f2fs_add_fsync_node_entry(sbi, page);
1569 		if (seq_id)
1570 			*seq_id = seq;
1571 	}
1572 
1573 	fio.old_blkaddr = ni.blk_addr;
1574 	f2fs_do_write_node_page(nid, &fio);
1575 	set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1576 	dec_page_count(sbi, F2FS_DIRTY_NODES);
1577 	up_read(&sbi->node_write);
1578 
1579 	if (wbc->for_reclaim) {
1580 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
1581 		submitted = NULL;
1582 	}
1583 
1584 	unlock_page(page);
1585 
1586 	if (unlikely(f2fs_cp_error(sbi))) {
1587 		f2fs_submit_merged_write(sbi, NODE);
1588 		submitted = NULL;
1589 	}
1590 	if (submitted)
1591 		*submitted = fio.submitted;
1592 
1593 	if (do_balance)
1594 		f2fs_balance_fs(sbi, false);
1595 	return 0;
1596 
1597 redirty_out:
1598 	redirty_page_for_writepage(wbc, page);
1599 	return AOP_WRITEPAGE_ACTIVATE;
1600 }
1601 
1602 int f2fs_move_node_page(struct page *node_page, int gc_type)
1603 {
1604 	int err = 0;
1605 
1606 	if (gc_type == FG_GC) {
1607 		struct writeback_control wbc = {
1608 			.sync_mode = WB_SYNC_ALL,
1609 			.nr_to_write = 1,
1610 			.for_reclaim = 0,
1611 		};
1612 
1613 		f2fs_wait_on_page_writeback(node_page, NODE, true, true);
1614 
1615 		set_page_dirty(node_page);
1616 
1617 		if (!clear_page_dirty_for_io(node_page)) {
1618 			err = -EAGAIN;
1619 			goto out_page;
1620 		}
1621 
1622 		if (__write_node_page(node_page, false, NULL,
1623 					&wbc, false, FS_GC_NODE_IO, NULL)) {
1624 			err = -EAGAIN;
1625 			unlock_page(node_page);
1626 		}
1627 		goto release_page;
1628 	} else {
1629 		/* set page dirty and write it */
1630 		if (!PageWriteback(node_page))
1631 			set_page_dirty(node_page);
1632 	}
1633 out_page:
1634 	unlock_page(node_page);
1635 release_page:
1636 	f2fs_put_page(node_page, 0);
1637 	return err;
1638 }
1639 
1640 static int f2fs_write_node_page(struct page *page,
1641 				struct writeback_control *wbc)
1642 {
1643 	return __write_node_page(page, false, NULL, wbc, false,
1644 						FS_NODE_IO, NULL);
1645 }
1646 
1647 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
1648 			struct writeback_control *wbc, bool atomic,
1649 			unsigned int *seq_id)
1650 {
1651 	pgoff_t index;
1652 	struct pagevec pvec;
1653 	int ret = 0;
1654 	struct page *last_page = NULL;
1655 	bool marked = false;
1656 	nid_t ino = inode->i_ino;
1657 	int nr_pages;
1658 	int nwritten = 0;
1659 
1660 	if (atomic) {
1661 		last_page = last_fsync_dnode(sbi, ino);
1662 		if (IS_ERR_OR_NULL(last_page))
1663 			return PTR_ERR_OR_ZERO(last_page);
1664 	}
1665 retry:
1666 	pagevec_init(&pvec);
1667 	index = 0;
1668 
1669 	while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1670 				PAGECACHE_TAG_DIRTY))) {
1671 		int i;
1672 
1673 		for (i = 0; i < nr_pages; i++) {
1674 			struct page *page = pvec.pages[i];
1675 			bool submitted = false;
1676 
1677 			if (unlikely(f2fs_cp_error(sbi))) {
1678 				f2fs_put_page(last_page, 0);
1679 				pagevec_release(&pvec);
1680 				ret = -EIO;
1681 				goto out;
1682 			}
1683 
1684 			if (!IS_DNODE(page) || !is_cold_node(page))
1685 				continue;
1686 			if (ino_of_node(page) != ino)
1687 				continue;
1688 
1689 			lock_page(page);
1690 
1691 			if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1692 continue_unlock:
1693 				unlock_page(page);
1694 				continue;
1695 			}
1696 			if (ino_of_node(page) != ino)
1697 				goto continue_unlock;
1698 
1699 			if (!PageDirty(page) && page != last_page) {
1700 				/* someone wrote it for us */
1701 				goto continue_unlock;
1702 			}
1703 
1704 			f2fs_wait_on_page_writeback(page, NODE, true, true);
1705 
1706 			set_fsync_mark(page, 0);
1707 			set_dentry_mark(page, 0);
1708 
1709 			if (!atomic || page == last_page) {
1710 				set_fsync_mark(page, 1);
1711 				if (IS_INODE(page)) {
1712 					if (is_inode_flag_set(inode,
1713 								FI_DIRTY_INODE))
1714 						f2fs_update_inode(inode, page);
1715 					set_dentry_mark(page,
1716 						f2fs_need_dentry_mark(sbi, ino));
1717 				}
1718 				/*  may be written by other thread */
1719 				if (!PageDirty(page))
1720 					set_page_dirty(page);
1721 			}
1722 
1723 			if (!clear_page_dirty_for_io(page))
1724 				goto continue_unlock;
1725 
1726 			ret = __write_node_page(page, atomic &&
1727 						page == last_page,
1728 						&submitted, wbc, true,
1729 						FS_NODE_IO, seq_id);
1730 			if (ret) {
1731 				unlock_page(page);
1732 				f2fs_put_page(last_page, 0);
1733 				break;
1734 			} else if (submitted) {
1735 				nwritten++;
1736 			}
1737 
1738 			if (page == last_page) {
1739 				f2fs_put_page(page, 0);
1740 				marked = true;
1741 				break;
1742 			}
1743 		}
1744 		pagevec_release(&pvec);
1745 		cond_resched();
1746 
1747 		if (ret || marked)
1748 			break;
1749 	}
1750 	if (!ret && atomic && !marked) {
1751 		f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
1752 			   ino, last_page->index);
1753 		lock_page(last_page);
1754 		f2fs_wait_on_page_writeback(last_page, NODE, true, true);
1755 		set_page_dirty(last_page);
1756 		unlock_page(last_page);
1757 		goto retry;
1758 	}
1759 out:
1760 	if (nwritten)
1761 		f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
1762 	return ret ? -EIO: 0;
1763 }
1764 
1765 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
1766 				struct writeback_control *wbc,
1767 				bool do_balance, enum iostat_type io_type)
1768 {
1769 	pgoff_t index;
1770 	struct pagevec pvec;
1771 	int step = 0;
1772 	int nwritten = 0;
1773 	int ret = 0;
1774 	int nr_pages, done = 0;
1775 
1776 	pagevec_init(&pvec);
1777 
1778 next_step:
1779 	index = 0;
1780 
1781 	while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
1782 			NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
1783 		int i;
1784 
1785 		for (i = 0; i < nr_pages; i++) {
1786 			struct page *page = pvec.pages[i];
1787 			bool submitted = false;
1788 
1789 			/* give a priority to WB_SYNC threads */
1790 			if (atomic_read(&sbi->wb_sync_req[NODE]) &&
1791 					wbc->sync_mode == WB_SYNC_NONE) {
1792 				done = 1;
1793 				break;
1794 			}
1795 
1796 			/*
1797 			 * flushing sequence with step:
1798 			 * 0. indirect nodes
1799 			 * 1. dentry dnodes
1800 			 * 2. file dnodes
1801 			 */
1802 			if (step == 0 && IS_DNODE(page))
1803 				continue;
1804 			if (step == 1 && (!IS_DNODE(page) ||
1805 						is_cold_node(page)))
1806 				continue;
1807 			if (step == 2 && (!IS_DNODE(page) ||
1808 						!is_cold_node(page)))
1809 				continue;
1810 lock_node:
1811 			if (wbc->sync_mode == WB_SYNC_ALL)
1812 				lock_page(page);
1813 			else if (!trylock_page(page))
1814 				continue;
1815 
1816 			if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1817 continue_unlock:
1818 				unlock_page(page);
1819 				continue;
1820 			}
1821 
1822 			if (!PageDirty(page)) {
1823 				/* someone wrote it for us */
1824 				goto continue_unlock;
1825 			}
1826 
1827 			/* flush inline_data */
1828 			if (is_inline_node(page)) {
1829 				clear_inline_node(page);
1830 				unlock_page(page);
1831 				flush_inline_data(sbi, ino_of_node(page));
1832 				goto lock_node;
1833 			}
1834 
1835 			f2fs_wait_on_page_writeback(page, NODE, true, true);
1836 
1837 			if (!clear_page_dirty_for_io(page))
1838 				goto continue_unlock;
1839 
1840 			set_fsync_mark(page, 0);
1841 			set_dentry_mark(page, 0);
1842 
1843 			ret = __write_node_page(page, false, &submitted,
1844 						wbc, do_balance, io_type, NULL);
1845 			if (ret)
1846 				unlock_page(page);
1847 			else if (submitted)
1848 				nwritten++;
1849 
1850 			if (--wbc->nr_to_write == 0)
1851 				break;
1852 		}
1853 		pagevec_release(&pvec);
1854 		cond_resched();
1855 
1856 		if (wbc->nr_to_write == 0) {
1857 			step = 2;
1858 			break;
1859 		}
1860 	}
1861 
1862 	if (step < 2) {
1863 		if (wbc->sync_mode == WB_SYNC_NONE && step == 1)
1864 			goto out;
1865 		step++;
1866 		goto next_step;
1867 	}
1868 out:
1869 	if (nwritten)
1870 		f2fs_submit_merged_write(sbi, NODE);
1871 
1872 	if (unlikely(f2fs_cp_error(sbi)))
1873 		return -EIO;
1874 	return ret;
1875 }
1876 
1877 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
1878 						unsigned int seq_id)
1879 {
1880 	struct fsync_node_entry *fn;
1881 	struct page *page;
1882 	struct list_head *head = &sbi->fsync_node_list;
1883 	unsigned long flags;
1884 	unsigned int cur_seq_id = 0;
1885 	int ret2, ret = 0;
1886 
1887 	while (seq_id && cur_seq_id < seq_id) {
1888 		spin_lock_irqsave(&sbi->fsync_node_lock, flags);
1889 		if (list_empty(head)) {
1890 			spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1891 			break;
1892 		}
1893 		fn = list_first_entry(head, struct fsync_node_entry, list);
1894 		if (fn->seq_id > seq_id) {
1895 			spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1896 			break;
1897 		}
1898 		cur_seq_id = fn->seq_id;
1899 		page = fn->page;
1900 		get_page(page);
1901 		spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1902 
1903 		f2fs_wait_on_page_writeback(page, NODE, true, false);
1904 		if (TestClearPageError(page))
1905 			ret = -EIO;
1906 
1907 		put_page(page);
1908 
1909 		if (ret)
1910 			break;
1911 	}
1912 
1913 	ret2 = filemap_check_errors(NODE_MAPPING(sbi));
1914 	if (!ret)
1915 		ret = ret2;
1916 
1917 	return ret;
1918 }
1919 
1920 static int f2fs_write_node_pages(struct address_space *mapping,
1921 			    struct writeback_control *wbc)
1922 {
1923 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1924 	struct blk_plug plug;
1925 	long diff;
1926 
1927 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1928 		goto skip_write;
1929 
1930 	/* balancing f2fs's metadata in background */
1931 	f2fs_balance_fs_bg(sbi);
1932 
1933 	/* collect a number of dirty node pages and write together */
1934 	if (wbc->sync_mode != WB_SYNC_ALL &&
1935 			get_pages(sbi, F2FS_DIRTY_NODES) <
1936 					nr_pages_to_skip(sbi, NODE))
1937 		goto skip_write;
1938 
1939 	if (wbc->sync_mode == WB_SYNC_ALL)
1940 		atomic_inc(&sbi->wb_sync_req[NODE]);
1941 	else if (atomic_read(&sbi->wb_sync_req[NODE]))
1942 		goto skip_write;
1943 
1944 	trace_f2fs_writepages(mapping->host, wbc, NODE);
1945 
1946 	diff = nr_pages_to_write(sbi, NODE, wbc);
1947 	blk_start_plug(&plug);
1948 	f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
1949 	blk_finish_plug(&plug);
1950 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1951 
1952 	if (wbc->sync_mode == WB_SYNC_ALL)
1953 		atomic_dec(&sbi->wb_sync_req[NODE]);
1954 	return 0;
1955 
1956 skip_write:
1957 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1958 	trace_f2fs_writepages(mapping->host, wbc, NODE);
1959 	return 0;
1960 }
1961 
1962 static int f2fs_set_node_page_dirty(struct page *page)
1963 {
1964 	trace_f2fs_set_page_dirty(page, NODE);
1965 
1966 	if (!PageUptodate(page))
1967 		SetPageUptodate(page);
1968 #ifdef CONFIG_F2FS_CHECK_FS
1969 	if (IS_INODE(page))
1970 		f2fs_inode_chksum_set(F2FS_P_SB(page), page);
1971 #endif
1972 	if (!PageDirty(page)) {
1973 		__set_page_dirty_nobuffers(page);
1974 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
1975 		f2fs_set_page_private(page, 0);
1976 		f2fs_trace_pid(page);
1977 		return 1;
1978 	}
1979 	return 0;
1980 }
1981 
1982 /*
1983  * Structure of the f2fs node operations
1984  */
1985 const struct address_space_operations f2fs_node_aops = {
1986 	.writepage	= f2fs_write_node_page,
1987 	.writepages	= f2fs_write_node_pages,
1988 	.set_page_dirty	= f2fs_set_node_page_dirty,
1989 	.invalidatepage	= f2fs_invalidate_page,
1990 	.releasepage	= f2fs_release_page,
1991 #ifdef CONFIG_MIGRATION
1992 	.migratepage    = f2fs_migrate_page,
1993 #endif
1994 };
1995 
1996 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1997 						nid_t n)
1998 {
1999 	return radix_tree_lookup(&nm_i->free_nid_root, n);
2000 }
2001 
2002 static int __insert_free_nid(struct f2fs_sb_info *sbi,
2003 			struct free_nid *i, enum nid_state state)
2004 {
2005 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2006 
2007 	int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
2008 	if (err)
2009 		return err;
2010 
2011 	f2fs_bug_on(sbi, state != i->state);
2012 	nm_i->nid_cnt[state]++;
2013 	if (state == FREE_NID)
2014 		list_add_tail(&i->list, &nm_i->free_nid_list);
2015 	return 0;
2016 }
2017 
2018 static void __remove_free_nid(struct f2fs_sb_info *sbi,
2019 			struct free_nid *i, enum nid_state state)
2020 {
2021 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2022 
2023 	f2fs_bug_on(sbi, state != i->state);
2024 	nm_i->nid_cnt[state]--;
2025 	if (state == FREE_NID)
2026 		list_del(&i->list);
2027 	radix_tree_delete(&nm_i->free_nid_root, i->nid);
2028 }
2029 
2030 static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2031 			enum nid_state org_state, enum nid_state dst_state)
2032 {
2033 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2034 
2035 	f2fs_bug_on(sbi, org_state != i->state);
2036 	i->state = dst_state;
2037 	nm_i->nid_cnt[org_state]--;
2038 	nm_i->nid_cnt[dst_state]++;
2039 
2040 	switch (dst_state) {
2041 	case PREALLOC_NID:
2042 		list_del(&i->list);
2043 		break;
2044 	case FREE_NID:
2045 		list_add_tail(&i->list, &nm_i->free_nid_list);
2046 		break;
2047 	default:
2048 		BUG_ON(1);
2049 	}
2050 }
2051 
2052 static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2053 							bool set, bool build)
2054 {
2055 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2056 	unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2057 	unsigned int nid_ofs = nid - START_NID(nid);
2058 
2059 	if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2060 		return;
2061 
2062 	if (set) {
2063 		if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2064 			return;
2065 		__set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2066 		nm_i->free_nid_count[nat_ofs]++;
2067 	} else {
2068 		if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2069 			return;
2070 		__clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2071 		if (!build)
2072 			nm_i->free_nid_count[nat_ofs]--;
2073 	}
2074 }
2075 
2076 /* return if the nid is recognized as free */
2077 static bool add_free_nid(struct f2fs_sb_info *sbi,
2078 				nid_t nid, bool build, bool update)
2079 {
2080 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2081 	struct free_nid *i, *e;
2082 	struct nat_entry *ne;
2083 	int err = -EINVAL;
2084 	bool ret = false;
2085 
2086 	/* 0 nid should not be used */
2087 	if (unlikely(nid == 0))
2088 		return false;
2089 
2090 	if (unlikely(f2fs_check_nid_range(sbi, nid)))
2091 		return false;
2092 
2093 	i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
2094 	i->nid = nid;
2095 	i->state = FREE_NID;
2096 
2097 	radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
2098 
2099 	spin_lock(&nm_i->nid_list_lock);
2100 
2101 	if (build) {
2102 		/*
2103 		 *   Thread A             Thread B
2104 		 *  - f2fs_create
2105 		 *   - f2fs_new_inode
2106 		 *    - f2fs_alloc_nid
2107 		 *     - __insert_nid_to_list(PREALLOC_NID)
2108 		 *                     - f2fs_balance_fs_bg
2109 		 *                      - f2fs_build_free_nids
2110 		 *                       - __f2fs_build_free_nids
2111 		 *                        - scan_nat_page
2112 		 *                         - add_free_nid
2113 		 *                          - __lookup_nat_cache
2114 		 *  - f2fs_add_link
2115 		 *   - f2fs_init_inode_metadata
2116 		 *    - f2fs_new_inode_page
2117 		 *     - f2fs_new_node_page
2118 		 *      - set_node_addr
2119 		 *  - f2fs_alloc_nid_done
2120 		 *   - __remove_nid_from_list(PREALLOC_NID)
2121 		 *                         - __insert_nid_to_list(FREE_NID)
2122 		 */
2123 		ne = __lookup_nat_cache(nm_i, nid);
2124 		if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2125 				nat_get_blkaddr(ne) != NULL_ADDR))
2126 			goto err_out;
2127 
2128 		e = __lookup_free_nid_list(nm_i, nid);
2129 		if (e) {
2130 			if (e->state == FREE_NID)
2131 				ret = true;
2132 			goto err_out;
2133 		}
2134 	}
2135 	ret = true;
2136 	err = __insert_free_nid(sbi, i, FREE_NID);
2137 err_out:
2138 	if (update) {
2139 		update_free_nid_bitmap(sbi, nid, ret, build);
2140 		if (!build)
2141 			nm_i->available_nids++;
2142 	}
2143 	spin_unlock(&nm_i->nid_list_lock);
2144 	radix_tree_preload_end();
2145 
2146 	if (err)
2147 		kmem_cache_free(free_nid_slab, i);
2148 	return ret;
2149 }
2150 
2151 static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
2152 {
2153 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2154 	struct free_nid *i;
2155 	bool need_free = false;
2156 
2157 	spin_lock(&nm_i->nid_list_lock);
2158 	i = __lookup_free_nid_list(nm_i, nid);
2159 	if (i && i->state == FREE_NID) {
2160 		__remove_free_nid(sbi, i, FREE_NID);
2161 		need_free = true;
2162 	}
2163 	spin_unlock(&nm_i->nid_list_lock);
2164 
2165 	if (need_free)
2166 		kmem_cache_free(free_nid_slab, i);
2167 }
2168 
2169 static int scan_nat_page(struct f2fs_sb_info *sbi,
2170 			struct page *nat_page, nid_t start_nid)
2171 {
2172 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2173 	struct f2fs_nat_block *nat_blk = page_address(nat_page);
2174 	block_t blk_addr;
2175 	unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
2176 	int i;
2177 
2178 	__set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
2179 
2180 	i = start_nid % NAT_ENTRY_PER_BLOCK;
2181 
2182 	for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
2183 		if (unlikely(start_nid >= nm_i->max_nid))
2184 			break;
2185 
2186 		blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
2187 
2188 		if (blk_addr == NEW_ADDR)
2189 			return -EINVAL;
2190 
2191 		if (blk_addr == NULL_ADDR) {
2192 			add_free_nid(sbi, start_nid, true, true);
2193 		} else {
2194 			spin_lock(&NM_I(sbi)->nid_list_lock);
2195 			update_free_nid_bitmap(sbi, start_nid, false, true);
2196 			spin_unlock(&NM_I(sbi)->nid_list_lock);
2197 		}
2198 	}
2199 
2200 	return 0;
2201 }
2202 
2203 static void scan_curseg_cache(struct f2fs_sb_info *sbi)
2204 {
2205 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2206 	struct f2fs_journal *journal = curseg->journal;
2207 	int i;
2208 
2209 	down_read(&curseg->journal_rwsem);
2210 	for (i = 0; i < nats_in_cursum(journal); i++) {
2211 		block_t addr;
2212 		nid_t nid;
2213 
2214 		addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2215 		nid = le32_to_cpu(nid_in_journal(journal, i));
2216 		if (addr == NULL_ADDR)
2217 			add_free_nid(sbi, nid, true, false);
2218 		else
2219 			remove_free_nid(sbi, nid);
2220 	}
2221 	up_read(&curseg->journal_rwsem);
2222 }
2223 
2224 static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
2225 {
2226 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2227 	unsigned int i, idx;
2228 	nid_t nid;
2229 
2230 	down_read(&nm_i->nat_tree_lock);
2231 
2232 	for (i = 0; i < nm_i->nat_blocks; i++) {
2233 		if (!test_bit_le(i, nm_i->nat_block_bitmap))
2234 			continue;
2235 		if (!nm_i->free_nid_count[i])
2236 			continue;
2237 		for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
2238 			idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2239 						NAT_ENTRY_PER_BLOCK, idx);
2240 			if (idx >= NAT_ENTRY_PER_BLOCK)
2241 				break;
2242 
2243 			nid = i * NAT_ENTRY_PER_BLOCK + idx;
2244 			add_free_nid(sbi, nid, true, false);
2245 
2246 			if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
2247 				goto out;
2248 		}
2249 	}
2250 out:
2251 	scan_curseg_cache(sbi);
2252 
2253 	up_read(&nm_i->nat_tree_lock);
2254 }
2255 
2256 static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
2257 						bool sync, bool mount)
2258 {
2259 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2260 	int i = 0, ret;
2261 	nid_t nid = nm_i->next_scan_nid;
2262 
2263 	if (unlikely(nid >= nm_i->max_nid))
2264 		nid = 0;
2265 
2266 	/* Enough entries */
2267 	if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2268 		return 0;
2269 
2270 	if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
2271 		return 0;
2272 
2273 	if (!mount) {
2274 		/* try to find free nids in free_nid_bitmap */
2275 		scan_free_nid_bits(sbi);
2276 
2277 		if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2278 			return 0;
2279 	}
2280 
2281 	/* readahead nat pages to be scanned */
2282 	f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
2283 							META_NAT, true);
2284 
2285 	down_read(&nm_i->nat_tree_lock);
2286 
2287 	while (1) {
2288 		if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2289 						nm_i->nat_block_bitmap)) {
2290 			struct page *page = get_current_nat_page(sbi, nid);
2291 
2292 			if (IS_ERR(page)) {
2293 				ret = PTR_ERR(page);
2294 			} else {
2295 				ret = scan_nat_page(sbi, page, nid);
2296 				f2fs_put_page(page, 1);
2297 			}
2298 
2299 			if (ret) {
2300 				up_read(&nm_i->nat_tree_lock);
2301 				f2fs_bug_on(sbi, !mount);
2302 				f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
2303 				return ret;
2304 			}
2305 		}
2306 
2307 		nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
2308 		if (unlikely(nid >= nm_i->max_nid))
2309 			nid = 0;
2310 
2311 		if (++i >= FREE_NID_PAGES)
2312 			break;
2313 	}
2314 
2315 	/* go to the next free nat pages to find free nids abundantly */
2316 	nm_i->next_scan_nid = nid;
2317 
2318 	/* find free nids from current sum_pages */
2319 	scan_curseg_cache(sbi);
2320 
2321 	up_read(&nm_i->nat_tree_lock);
2322 
2323 	f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
2324 					nm_i->ra_nid_pages, META_NAT, false);
2325 
2326 	return 0;
2327 }
2328 
2329 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
2330 {
2331 	int ret;
2332 
2333 	mutex_lock(&NM_I(sbi)->build_lock);
2334 	ret = __f2fs_build_free_nids(sbi, sync, mount);
2335 	mutex_unlock(&NM_I(sbi)->build_lock);
2336 
2337 	return ret;
2338 }
2339 
2340 /*
2341  * If this function returns success, caller can obtain a new nid
2342  * from second parameter of this function.
2343  * The returned nid could be used ino as well as nid when inode is created.
2344  */
2345 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
2346 {
2347 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2348 	struct free_nid *i = NULL;
2349 retry:
2350 	if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
2351 		f2fs_show_injection_info(FAULT_ALLOC_NID);
2352 		return false;
2353 	}
2354 
2355 	spin_lock(&nm_i->nid_list_lock);
2356 
2357 	if (unlikely(nm_i->available_nids == 0)) {
2358 		spin_unlock(&nm_i->nid_list_lock);
2359 		return false;
2360 	}
2361 
2362 	/* We should not use stale free nids created by f2fs_build_free_nids */
2363 	if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
2364 		f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2365 		i = list_first_entry(&nm_i->free_nid_list,
2366 					struct free_nid, list);
2367 		*nid = i->nid;
2368 
2369 		__move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
2370 		nm_i->available_nids--;
2371 
2372 		update_free_nid_bitmap(sbi, *nid, false, false);
2373 
2374 		spin_unlock(&nm_i->nid_list_lock);
2375 		return true;
2376 	}
2377 	spin_unlock(&nm_i->nid_list_lock);
2378 
2379 	/* Let's scan nat pages and its caches to get free nids */
2380 	if (!f2fs_build_free_nids(sbi, true, false))
2381 		goto retry;
2382 	return false;
2383 }
2384 
2385 /*
2386  * f2fs_alloc_nid() should be called prior to this function.
2387  */
2388 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
2389 {
2390 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2391 	struct free_nid *i;
2392 
2393 	spin_lock(&nm_i->nid_list_lock);
2394 	i = __lookup_free_nid_list(nm_i, nid);
2395 	f2fs_bug_on(sbi, !i);
2396 	__remove_free_nid(sbi, i, PREALLOC_NID);
2397 	spin_unlock(&nm_i->nid_list_lock);
2398 
2399 	kmem_cache_free(free_nid_slab, i);
2400 }
2401 
2402 /*
2403  * f2fs_alloc_nid() should be called prior to this function.
2404  */
2405 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
2406 {
2407 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2408 	struct free_nid *i;
2409 	bool need_free = false;
2410 
2411 	if (!nid)
2412 		return;
2413 
2414 	spin_lock(&nm_i->nid_list_lock);
2415 	i = __lookup_free_nid_list(nm_i, nid);
2416 	f2fs_bug_on(sbi, !i);
2417 
2418 	if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
2419 		__remove_free_nid(sbi, i, PREALLOC_NID);
2420 		need_free = true;
2421 	} else {
2422 		__move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
2423 	}
2424 
2425 	nm_i->available_nids++;
2426 
2427 	update_free_nid_bitmap(sbi, nid, true, false);
2428 
2429 	spin_unlock(&nm_i->nid_list_lock);
2430 
2431 	if (need_free)
2432 		kmem_cache_free(free_nid_slab, i);
2433 }
2434 
2435 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
2436 {
2437 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2438 	struct free_nid *i, *next;
2439 	int nr = nr_shrink;
2440 
2441 	if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2442 		return 0;
2443 
2444 	if (!mutex_trylock(&nm_i->build_lock))
2445 		return 0;
2446 
2447 	spin_lock(&nm_i->nid_list_lock);
2448 	list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
2449 		if (nr_shrink <= 0 ||
2450 				nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2451 			break;
2452 
2453 		__remove_free_nid(sbi, i, FREE_NID);
2454 		kmem_cache_free(free_nid_slab, i);
2455 		nr_shrink--;
2456 	}
2457 	spin_unlock(&nm_i->nid_list_lock);
2458 	mutex_unlock(&nm_i->build_lock);
2459 
2460 	return nr - nr_shrink;
2461 }
2462 
2463 void f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
2464 {
2465 	void *src_addr, *dst_addr;
2466 	size_t inline_size;
2467 	struct page *ipage;
2468 	struct f2fs_inode *ri;
2469 
2470 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
2471 	f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
2472 
2473 	ri = F2FS_INODE(page);
2474 	if (ri->i_inline & F2FS_INLINE_XATTR) {
2475 		set_inode_flag(inode, FI_INLINE_XATTR);
2476 	} else {
2477 		clear_inode_flag(inode, FI_INLINE_XATTR);
2478 		goto update_inode;
2479 	}
2480 
2481 	dst_addr = inline_xattr_addr(inode, ipage);
2482 	src_addr = inline_xattr_addr(inode, page);
2483 	inline_size = inline_xattr_size(inode);
2484 
2485 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
2486 	memcpy(dst_addr, src_addr, inline_size);
2487 update_inode:
2488 	f2fs_update_inode(inode, ipage);
2489 	f2fs_put_page(ipage, 1);
2490 }
2491 
2492 int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
2493 {
2494 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2495 	nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
2496 	nid_t new_xnid;
2497 	struct dnode_of_data dn;
2498 	struct node_info ni;
2499 	struct page *xpage;
2500 	int err;
2501 
2502 	if (!prev_xnid)
2503 		goto recover_xnid;
2504 
2505 	/* 1: invalidate the previous xattr nid */
2506 	err = f2fs_get_node_info(sbi, prev_xnid, &ni);
2507 	if (err)
2508 		return err;
2509 
2510 	f2fs_invalidate_blocks(sbi, ni.blk_addr);
2511 	dec_valid_node_count(sbi, inode, false);
2512 	set_node_addr(sbi, &ni, NULL_ADDR, false);
2513 
2514 recover_xnid:
2515 	/* 2: update xattr nid in inode */
2516 	if (!f2fs_alloc_nid(sbi, &new_xnid))
2517 		return -ENOSPC;
2518 
2519 	set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
2520 	xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
2521 	if (IS_ERR(xpage)) {
2522 		f2fs_alloc_nid_failed(sbi, new_xnid);
2523 		return PTR_ERR(xpage);
2524 	}
2525 
2526 	f2fs_alloc_nid_done(sbi, new_xnid);
2527 	f2fs_update_inode_page(inode);
2528 
2529 	/* 3: update and set xattr node page dirty */
2530 	memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
2531 
2532 	set_page_dirty(xpage);
2533 	f2fs_put_page(xpage, 1);
2534 
2535 	return 0;
2536 }
2537 
2538 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
2539 {
2540 	struct f2fs_inode *src, *dst;
2541 	nid_t ino = ino_of_node(page);
2542 	struct node_info old_ni, new_ni;
2543 	struct page *ipage;
2544 	int err;
2545 
2546 	err = f2fs_get_node_info(sbi, ino, &old_ni);
2547 	if (err)
2548 		return err;
2549 
2550 	if (unlikely(old_ni.blk_addr != NULL_ADDR))
2551 		return -EINVAL;
2552 retry:
2553 	ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
2554 	if (!ipage) {
2555 		congestion_wait(BLK_RW_ASYNC, HZ/50);
2556 		goto retry;
2557 	}
2558 
2559 	/* Should not use this inode from free nid list */
2560 	remove_free_nid(sbi, ino);
2561 
2562 	if (!PageUptodate(ipage))
2563 		SetPageUptodate(ipage);
2564 	fill_node_footer(ipage, ino, ino, 0, true);
2565 	set_cold_node(ipage, false);
2566 
2567 	src = F2FS_INODE(page);
2568 	dst = F2FS_INODE(ipage);
2569 
2570 	memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
2571 	dst->i_size = 0;
2572 	dst->i_blocks = cpu_to_le64(1);
2573 	dst->i_links = cpu_to_le32(1);
2574 	dst->i_xattr_nid = 0;
2575 	dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
2576 	if (dst->i_inline & F2FS_EXTRA_ATTR) {
2577 		dst->i_extra_isize = src->i_extra_isize;
2578 
2579 		if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
2580 			F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2581 							i_inline_xattr_size))
2582 			dst->i_inline_xattr_size = src->i_inline_xattr_size;
2583 
2584 		if (f2fs_sb_has_project_quota(sbi) &&
2585 			F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2586 								i_projid))
2587 			dst->i_projid = src->i_projid;
2588 
2589 		if (f2fs_sb_has_inode_crtime(sbi) &&
2590 			F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2591 							i_crtime_nsec)) {
2592 			dst->i_crtime = src->i_crtime;
2593 			dst->i_crtime_nsec = src->i_crtime_nsec;
2594 		}
2595 	}
2596 
2597 	new_ni = old_ni;
2598 	new_ni.ino = ino;
2599 
2600 	if (unlikely(inc_valid_node_count(sbi, NULL, true)))
2601 		WARN_ON(1);
2602 	set_node_addr(sbi, &new_ni, NEW_ADDR, false);
2603 	inc_valid_inode_count(sbi);
2604 	set_page_dirty(ipage);
2605 	f2fs_put_page(ipage, 1);
2606 	return 0;
2607 }
2608 
2609 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2610 			unsigned int segno, struct f2fs_summary_block *sum)
2611 {
2612 	struct f2fs_node *rn;
2613 	struct f2fs_summary *sum_entry;
2614 	block_t addr;
2615 	int i, idx, last_offset, nrpages;
2616 
2617 	/* scan the node segment */
2618 	last_offset = sbi->blocks_per_seg;
2619 	addr = START_BLOCK(sbi, segno);
2620 	sum_entry = &sum->entries[0];
2621 
2622 	for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
2623 		nrpages = min(last_offset - i, BIO_MAX_PAGES);
2624 
2625 		/* readahead node pages */
2626 		f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
2627 
2628 		for (idx = addr; idx < addr + nrpages; idx++) {
2629 			struct page *page = f2fs_get_tmp_page(sbi, idx);
2630 
2631 			if (IS_ERR(page))
2632 				return PTR_ERR(page);
2633 
2634 			rn = F2FS_NODE(page);
2635 			sum_entry->nid = rn->footer.nid;
2636 			sum_entry->version = 0;
2637 			sum_entry->ofs_in_node = 0;
2638 			sum_entry++;
2639 			f2fs_put_page(page, 1);
2640 		}
2641 
2642 		invalidate_mapping_pages(META_MAPPING(sbi), addr,
2643 							addr + nrpages);
2644 	}
2645 	return 0;
2646 }
2647 
2648 static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
2649 {
2650 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2651 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2652 	struct f2fs_journal *journal = curseg->journal;
2653 	int i;
2654 
2655 	down_write(&curseg->journal_rwsem);
2656 	for (i = 0; i < nats_in_cursum(journal); i++) {
2657 		struct nat_entry *ne;
2658 		struct f2fs_nat_entry raw_ne;
2659 		nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
2660 
2661 		raw_ne = nat_in_journal(journal, i);
2662 
2663 		ne = __lookup_nat_cache(nm_i, nid);
2664 		if (!ne) {
2665 			ne = __alloc_nat_entry(nid, true);
2666 			__init_nat_entry(nm_i, ne, &raw_ne, true);
2667 		}
2668 
2669 		/*
2670 		 * if a free nat in journal has not been used after last
2671 		 * checkpoint, we should remove it from available nids,
2672 		 * since later we will add it again.
2673 		 */
2674 		if (!get_nat_flag(ne, IS_DIRTY) &&
2675 				le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2676 			spin_lock(&nm_i->nid_list_lock);
2677 			nm_i->available_nids--;
2678 			spin_unlock(&nm_i->nid_list_lock);
2679 		}
2680 
2681 		__set_nat_cache_dirty(nm_i, ne);
2682 	}
2683 	update_nats_in_cursum(journal, -i);
2684 	up_write(&curseg->journal_rwsem);
2685 }
2686 
2687 static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2688 						struct list_head *head, int max)
2689 {
2690 	struct nat_entry_set *cur;
2691 
2692 	if (nes->entry_cnt >= max)
2693 		goto add_out;
2694 
2695 	list_for_each_entry(cur, head, set_list) {
2696 		if (cur->entry_cnt >= nes->entry_cnt) {
2697 			list_add(&nes->set_list, cur->set_list.prev);
2698 			return;
2699 		}
2700 	}
2701 add_out:
2702 	list_add_tail(&nes->set_list, head);
2703 }
2704 
2705 static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
2706 						struct page *page)
2707 {
2708 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2709 	unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2710 	struct f2fs_nat_block *nat_blk = page_address(page);
2711 	int valid = 0;
2712 	int i = 0;
2713 
2714 	if (!enabled_nat_bits(sbi, NULL))
2715 		return;
2716 
2717 	if (nat_index == 0) {
2718 		valid = 1;
2719 		i = 1;
2720 	}
2721 	for (; i < NAT_ENTRY_PER_BLOCK; i++) {
2722 		if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
2723 			valid++;
2724 	}
2725 	if (valid == 0) {
2726 		__set_bit_le(nat_index, nm_i->empty_nat_bits);
2727 		__clear_bit_le(nat_index, nm_i->full_nat_bits);
2728 		return;
2729 	}
2730 
2731 	__clear_bit_le(nat_index, nm_i->empty_nat_bits);
2732 	if (valid == NAT_ENTRY_PER_BLOCK)
2733 		__set_bit_le(nat_index, nm_i->full_nat_bits);
2734 	else
2735 		__clear_bit_le(nat_index, nm_i->full_nat_bits);
2736 }
2737 
2738 static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
2739 		struct nat_entry_set *set, struct cp_control *cpc)
2740 {
2741 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2742 	struct f2fs_journal *journal = curseg->journal;
2743 	nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
2744 	bool to_journal = true;
2745 	struct f2fs_nat_block *nat_blk;
2746 	struct nat_entry *ne, *cur;
2747 	struct page *page = NULL;
2748 
2749 	/*
2750 	 * there are two steps to flush nat entries:
2751 	 * #1, flush nat entries to journal in current hot data summary block.
2752 	 * #2, flush nat entries to nat page.
2753 	 */
2754 	if (enabled_nat_bits(sbi, cpc) ||
2755 		!__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
2756 		to_journal = false;
2757 
2758 	if (to_journal) {
2759 		down_write(&curseg->journal_rwsem);
2760 	} else {
2761 		page = get_next_nat_page(sbi, start_nid);
2762 		if (IS_ERR(page))
2763 			return PTR_ERR(page);
2764 
2765 		nat_blk = page_address(page);
2766 		f2fs_bug_on(sbi, !nat_blk);
2767 	}
2768 
2769 	/* flush dirty nats in nat entry set */
2770 	list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
2771 		struct f2fs_nat_entry *raw_ne;
2772 		nid_t nid = nat_get_nid(ne);
2773 		int offset;
2774 
2775 		f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
2776 
2777 		if (to_journal) {
2778 			offset = f2fs_lookup_journal_in_cursum(journal,
2779 							NAT_JOURNAL, nid, 1);
2780 			f2fs_bug_on(sbi, offset < 0);
2781 			raw_ne = &nat_in_journal(journal, offset);
2782 			nid_in_journal(journal, offset) = cpu_to_le32(nid);
2783 		} else {
2784 			raw_ne = &nat_blk->entries[nid - start_nid];
2785 		}
2786 		raw_nat_from_node_info(raw_ne, &ne->ni);
2787 		nat_reset_flag(ne);
2788 		__clear_nat_cache_dirty(NM_I(sbi), set, ne);
2789 		if (nat_get_blkaddr(ne) == NULL_ADDR) {
2790 			add_free_nid(sbi, nid, false, true);
2791 		} else {
2792 			spin_lock(&NM_I(sbi)->nid_list_lock);
2793 			update_free_nid_bitmap(sbi, nid, false, false);
2794 			spin_unlock(&NM_I(sbi)->nid_list_lock);
2795 		}
2796 	}
2797 
2798 	if (to_journal) {
2799 		up_write(&curseg->journal_rwsem);
2800 	} else {
2801 		__update_nat_bits(sbi, start_nid, page);
2802 		f2fs_put_page(page, 1);
2803 	}
2804 
2805 	/* Allow dirty nats by node block allocation in write_begin */
2806 	if (!set->entry_cnt) {
2807 		radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
2808 		kmem_cache_free(nat_entry_set_slab, set);
2809 	}
2810 	return 0;
2811 }
2812 
2813 /*
2814  * This function is called during the checkpointing process.
2815  */
2816 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
2817 {
2818 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2819 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2820 	struct f2fs_journal *journal = curseg->journal;
2821 	struct nat_entry_set *setvec[SETVEC_SIZE];
2822 	struct nat_entry_set *set, *tmp;
2823 	unsigned int found;
2824 	nid_t set_idx = 0;
2825 	LIST_HEAD(sets);
2826 	int err = 0;
2827 
2828 	/* during unmount, let's flush nat_bits before checking dirty_nat_cnt */
2829 	if (enabled_nat_bits(sbi, cpc)) {
2830 		down_write(&nm_i->nat_tree_lock);
2831 		remove_nats_in_journal(sbi);
2832 		up_write(&nm_i->nat_tree_lock);
2833 	}
2834 
2835 	if (!nm_i->dirty_nat_cnt)
2836 		return 0;
2837 
2838 	down_write(&nm_i->nat_tree_lock);
2839 
2840 	/*
2841 	 * if there are no enough space in journal to store dirty nat
2842 	 * entries, remove all entries from journal and merge them
2843 	 * into nat entry set.
2844 	 */
2845 	if (enabled_nat_bits(sbi, cpc) ||
2846 		!__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL))
2847 		remove_nats_in_journal(sbi);
2848 
2849 	while ((found = __gang_lookup_nat_set(nm_i,
2850 					set_idx, SETVEC_SIZE, setvec))) {
2851 		unsigned idx;
2852 		set_idx = setvec[found - 1]->set + 1;
2853 		for (idx = 0; idx < found; idx++)
2854 			__adjust_nat_entry_set(setvec[idx], &sets,
2855 						MAX_NAT_JENTRIES(journal));
2856 	}
2857 
2858 	/* flush dirty nats in nat entry set */
2859 	list_for_each_entry_safe(set, tmp, &sets, set_list) {
2860 		err = __flush_nat_entry_set(sbi, set, cpc);
2861 		if (err)
2862 			break;
2863 	}
2864 
2865 	up_write(&nm_i->nat_tree_lock);
2866 	/* Allow dirty nats by node block allocation in write_begin */
2867 
2868 	return err;
2869 }
2870 
2871 static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
2872 {
2873 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2874 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2875 	unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
2876 	unsigned int i;
2877 	__u64 cp_ver = cur_cp_version(ckpt);
2878 	block_t nat_bits_addr;
2879 
2880 	if (!enabled_nat_bits(sbi, NULL))
2881 		return 0;
2882 
2883 	nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
2884 	nm_i->nat_bits = f2fs_kzalloc(sbi,
2885 			nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
2886 	if (!nm_i->nat_bits)
2887 		return -ENOMEM;
2888 
2889 	nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
2890 						nm_i->nat_bits_blocks;
2891 	for (i = 0; i < nm_i->nat_bits_blocks; i++) {
2892 		struct page *page;
2893 
2894 		page = f2fs_get_meta_page(sbi, nat_bits_addr++);
2895 		if (IS_ERR(page))
2896 			return PTR_ERR(page);
2897 
2898 		memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
2899 					page_address(page), F2FS_BLKSIZE);
2900 		f2fs_put_page(page, 1);
2901 	}
2902 
2903 	cp_ver |= (cur_cp_crc(ckpt) << 32);
2904 	if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
2905 		disable_nat_bits(sbi, true);
2906 		return 0;
2907 	}
2908 
2909 	nm_i->full_nat_bits = nm_i->nat_bits + 8;
2910 	nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
2911 
2912 	f2fs_notice(sbi, "Found nat_bits in checkpoint");
2913 	return 0;
2914 }
2915 
2916 static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
2917 {
2918 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2919 	unsigned int i = 0;
2920 	nid_t nid, last_nid;
2921 
2922 	if (!enabled_nat_bits(sbi, NULL))
2923 		return;
2924 
2925 	for (i = 0; i < nm_i->nat_blocks; i++) {
2926 		i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
2927 		if (i >= nm_i->nat_blocks)
2928 			break;
2929 
2930 		__set_bit_le(i, nm_i->nat_block_bitmap);
2931 
2932 		nid = i * NAT_ENTRY_PER_BLOCK;
2933 		last_nid = nid + NAT_ENTRY_PER_BLOCK;
2934 
2935 		spin_lock(&NM_I(sbi)->nid_list_lock);
2936 		for (; nid < last_nid; nid++)
2937 			update_free_nid_bitmap(sbi, nid, true, true);
2938 		spin_unlock(&NM_I(sbi)->nid_list_lock);
2939 	}
2940 
2941 	for (i = 0; i < nm_i->nat_blocks; i++) {
2942 		i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
2943 		if (i >= nm_i->nat_blocks)
2944 			break;
2945 
2946 		__set_bit_le(i, nm_i->nat_block_bitmap);
2947 	}
2948 }
2949 
2950 static int init_node_manager(struct f2fs_sb_info *sbi)
2951 {
2952 	struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
2953 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2954 	unsigned char *version_bitmap;
2955 	unsigned int nat_segs;
2956 	int err;
2957 
2958 	nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
2959 
2960 	/* segment_count_nat includes pair segment so divide to 2. */
2961 	nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
2962 	nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
2963 	nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
2964 
2965 	/* not used nids: 0, node, meta, (and root counted as valid node) */
2966 	nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
2967 				sbi->nquota_files - F2FS_RESERVED_NODE_NUM;
2968 	nm_i->nid_cnt[FREE_NID] = 0;
2969 	nm_i->nid_cnt[PREALLOC_NID] = 0;
2970 	nm_i->nat_cnt = 0;
2971 	nm_i->ram_thresh = DEF_RAM_THRESHOLD;
2972 	nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
2973 	nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
2974 
2975 	INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
2976 	INIT_LIST_HEAD(&nm_i->free_nid_list);
2977 	INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2978 	INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
2979 	INIT_LIST_HEAD(&nm_i->nat_entries);
2980 	spin_lock_init(&nm_i->nat_list_lock);
2981 
2982 	mutex_init(&nm_i->build_lock);
2983 	spin_lock_init(&nm_i->nid_list_lock);
2984 	init_rwsem(&nm_i->nat_tree_lock);
2985 
2986 	nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
2987 	nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
2988 	version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2989 	if (!version_bitmap)
2990 		return -EFAULT;
2991 
2992 	nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2993 					GFP_KERNEL);
2994 	if (!nm_i->nat_bitmap)
2995 		return -ENOMEM;
2996 
2997 	err = __get_nat_bitmaps(sbi);
2998 	if (err)
2999 		return err;
3000 
3001 #ifdef CONFIG_F2FS_CHECK_FS
3002 	nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
3003 					GFP_KERNEL);
3004 	if (!nm_i->nat_bitmap_mir)
3005 		return -ENOMEM;
3006 #endif
3007 
3008 	return 0;
3009 }
3010 
3011 static int init_free_nid_cache(struct f2fs_sb_info *sbi)
3012 {
3013 	struct f2fs_nm_info *nm_i = NM_I(sbi);
3014 	int i;
3015 
3016 	nm_i->free_nid_bitmap =
3017 		f2fs_kzalloc(sbi, array_size(sizeof(unsigned char *),
3018 					     nm_i->nat_blocks),
3019 			     GFP_KERNEL);
3020 	if (!nm_i->free_nid_bitmap)
3021 		return -ENOMEM;
3022 
3023 	for (i = 0; i < nm_i->nat_blocks; i++) {
3024 		nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
3025 			f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
3026 		if (!nm_i->free_nid_bitmap[i])
3027 			return -ENOMEM;
3028 	}
3029 
3030 	nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
3031 								GFP_KERNEL);
3032 	if (!nm_i->nat_block_bitmap)
3033 		return -ENOMEM;
3034 
3035 	nm_i->free_nid_count =
3036 		f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
3037 					      nm_i->nat_blocks),
3038 			      GFP_KERNEL);
3039 	if (!nm_i->free_nid_count)
3040 		return -ENOMEM;
3041 	return 0;
3042 }
3043 
3044 int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
3045 {
3046 	int err;
3047 
3048 	sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3049 							GFP_KERNEL);
3050 	if (!sbi->nm_info)
3051 		return -ENOMEM;
3052 
3053 	err = init_node_manager(sbi);
3054 	if (err)
3055 		return err;
3056 
3057 	err = init_free_nid_cache(sbi);
3058 	if (err)
3059 		return err;
3060 
3061 	/* load free nid status from nat_bits table */
3062 	load_free_nid_bitmap(sbi);
3063 
3064 	return f2fs_build_free_nids(sbi, true, true);
3065 }
3066 
3067 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
3068 {
3069 	struct f2fs_nm_info *nm_i = NM_I(sbi);
3070 	struct free_nid *i, *next_i;
3071 	struct nat_entry *natvec[NATVEC_SIZE];
3072 	struct nat_entry_set *setvec[SETVEC_SIZE];
3073 	nid_t nid = 0;
3074 	unsigned int found;
3075 
3076 	if (!nm_i)
3077 		return;
3078 
3079 	/* destroy free nid list */
3080 	spin_lock(&nm_i->nid_list_lock);
3081 	list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
3082 		__remove_free_nid(sbi, i, FREE_NID);
3083 		spin_unlock(&nm_i->nid_list_lock);
3084 		kmem_cache_free(free_nid_slab, i);
3085 		spin_lock(&nm_i->nid_list_lock);
3086 	}
3087 	f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3088 	f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3089 	f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
3090 	spin_unlock(&nm_i->nid_list_lock);
3091 
3092 	/* destroy nat cache */
3093 	down_write(&nm_i->nat_tree_lock);
3094 	while ((found = __gang_lookup_nat_cache(nm_i,
3095 					nid, NATVEC_SIZE, natvec))) {
3096 		unsigned idx;
3097 
3098 		nid = nat_get_nid(natvec[found - 1]) + 1;
3099 		for (idx = 0; idx < found; idx++) {
3100 			spin_lock(&nm_i->nat_list_lock);
3101 			list_del(&natvec[idx]->list);
3102 			spin_unlock(&nm_i->nat_list_lock);
3103 
3104 			__del_from_nat_cache(nm_i, natvec[idx]);
3105 		}
3106 	}
3107 	f2fs_bug_on(sbi, nm_i->nat_cnt);
3108 
3109 	/* destroy nat set cache */
3110 	nid = 0;
3111 	while ((found = __gang_lookup_nat_set(nm_i,
3112 					nid, SETVEC_SIZE, setvec))) {
3113 		unsigned idx;
3114 
3115 		nid = setvec[found - 1]->set + 1;
3116 		for (idx = 0; idx < found; idx++) {
3117 			/* entry_cnt is not zero, when cp_error was occurred */
3118 			f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3119 			radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3120 			kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3121 		}
3122 	}
3123 	up_write(&nm_i->nat_tree_lock);
3124 
3125 	kvfree(nm_i->nat_block_bitmap);
3126 	if (nm_i->free_nid_bitmap) {
3127 		int i;
3128 
3129 		for (i = 0; i < nm_i->nat_blocks; i++)
3130 			kvfree(nm_i->free_nid_bitmap[i]);
3131 		kvfree(nm_i->free_nid_bitmap);
3132 	}
3133 	kvfree(nm_i->free_nid_count);
3134 
3135 	kvfree(nm_i->nat_bitmap);
3136 	kvfree(nm_i->nat_bits);
3137 #ifdef CONFIG_F2FS_CHECK_FS
3138 	kvfree(nm_i->nat_bitmap_mir);
3139 #endif
3140 	sbi->nm_info = NULL;
3141 	kvfree(nm_i);
3142 }
3143 
3144 int __init f2fs_create_node_manager_caches(void)
3145 {
3146 	nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
3147 			sizeof(struct nat_entry));
3148 	if (!nat_entry_slab)
3149 		goto fail;
3150 
3151 	free_nid_slab = f2fs_kmem_cache_create("free_nid",
3152 			sizeof(struct free_nid));
3153 	if (!free_nid_slab)
3154 		goto destroy_nat_entry;
3155 
3156 	nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
3157 			sizeof(struct nat_entry_set));
3158 	if (!nat_entry_set_slab)
3159 		goto destroy_free_nid;
3160 
3161 	fsync_node_entry_slab = f2fs_kmem_cache_create("fsync_node_entry",
3162 			sizeof(struct fsync_node_entry));
3163 	if (!fsync_node_entry_slab)
3164 		goto destroy_nat_entry_set;
3165 	return 0;
3166 
3167 destroy_nat_entry_set:
3168 	kmem_cache_destroy(nat_entry_set_slab);
3169 destroy_free_nid:
3170 	kmem_cache_destroy(free_nid_slab);
3171 destroy_nat_entry:
3172 	kmem_cache_destroy(nat_entry_slab);
3173 fail:
3174 	return -ENOMEM;
3175 }
3176 
3177 void f2fs_destroy_node_manager_caches(void)
3178 {
3179 	kmem_cache_destroy(fsync_node_entry_slab);
3180 	kmem_cache_destroy(nat_entry_set_slab);
3181 	kmem_cache_destroy(free_nid_slab);
3182 	kmem_cache_destroy(nat_entry_slab);
3183 }
3184