xref: /linux/fs/f2fs/node.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
1 /*
2  * fs/f2fs/node.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/mpage.h>
14 #include <linux/backing-dev.h>
15 #include <linux/blkdev.h>
16 #include <linux/pagevec.h>
17 #include <linux/swap.h>
18 
19 #include "f2fs.h"
20 #include "node.h"
21 #include "segment.h"
22 #include "trace.h"
23 #include <trace/events/f2fs.h>
24 
25 #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
26 
27 static struct kmem_cache *nat_entry_slab;
28 static struct kmem_cache *free_nid_slab;
29 static struct kmem_cache *nat_entry_set_slab;
30 
31 bool available_free_memory(struct f2fs_sb_info *sbi, int type)
32 {
33 	struct f2fs_nm_info *nm_i = NM_I(sbi);
34 	struct sysinfo val;
35 	unsigned long avail_ram;
36 	unsigned long mem_size = 0;
37 	bool res = false;
38 
39 	si_meminfo(&val);
40 
41 	/* only uses low memory */
42 	avail_ram = val.totalram - val.totalhigh;
43 
44 	/*
45 	 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
46 	 */
47 	if (type == FREE_NIDS) {
48 		mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
49 							PAGE_CACHE_SHIFT;
50 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
51 	} else if (type == NAT_ENTRIES) {
52 		mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
53 							PAGE_CACHE_SHIFT;
54 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
55 	} else if (type == DIRTY_DENTS) {
56 		if (sbi->sb->s_bdi->wb.dirty_exceeded)
57 			return false;
58 		mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
59 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
60 	} else if (type == INO_ENTRIES) {
61 		int i;
62 
63 		for (i = 0; i <= UPDATE_INO; i++)
64 			mem_size += (sbi->im[i].ino_num *
65 				sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
66 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
67 	} else if (type == EXTENT_CACHE) {
68 		mem_size = (sbi->total_ext_tree * sizeof(struct extent_tree) +
69 				atomic_read(&sbi->total_ext_node) *
70 				sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT;
71 		res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
72 	} else {
73 		if (sbi->sb->s_bdi->wb.dirty_exceeded)
74 			return false;
75 	}
76 	return res;
77 }
78 
79 static void clear_node_page_dirty(struct page *page)
80 {
81 	struct address_space *mapping = page->mapping;
82 	unsigned int long flags;
83 
84 	if (PageDirty(page)) {
85 		spin_lock_irqsave(&mapping->tree_lock, flags);
86 		radix_tree_tag_clear(&mapping->page_tree,
87 				page_index(page),
88 				PAGECACHE_TAG_DIRTY);
89 		spin_unlock_irqrestore(&mapping->tree_lock, flags);
90 
91 		clear_page_dirty_for_io(page);
92 		dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
93 	}
94 	ClearPageUptodate(page);
95 }
96 
97 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
98 {
99 	pgoff_t index = current_nat_addr(sbi, nid);
100 	return get_meta_page(sbi, index);
101 }
102 
103 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
104 {
105 	struct page *src_page;
106 	struct page *dst_page;
107 	pgoff_t src_off;
108 	pgoff_t dst_off;
109 	void *src_addr;
110 	void *dst_addr;
111 	struct f2fs_nm_info *nm_i = NM_I(sbi);
112 
113 	src_off = current_nat_addr(sbi, nid);
114 	dst_off = next_nat_addr(sbi, src_off);
115 
116 	/* get current nat block page with lock */
117 	src_page = get_meta_page(sbi, src_off);
118 	dst_page = grab_meta_page(sbi, dst_off);
119 	f2fs_bug_on(sbi, PageDirty(src_page));
120 
121 	src_addr = page_address(src_page);
122 	dst_addr = page_address(dst_page);
123 	memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
124 	set_page_dirty(dst_page);
125 	f2fs_put_page(src_page, 1);
126 
127 	set_to_next_nat(nm_i, nid);
128 
129 	return dst_page;
130 }
131 
132 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
133 {
134 	return radix_tree_lookup(&nm_i->nat_root, n);
135 }
136 
137 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
138 		nid_t start, unsigned int nr, struct nat_entry **ep)
139 {
140 	return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
141 }
142 
143 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
144 {
145 	list_del(&e->list);
146 	radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
147 	nm_i->nat_cnt--;
148 	kmem_cache_free(nat_entry_slab, e);
149 }
150 
151 static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
152 						struct nat_entry *ne)
153 {
154 	nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
155 	struct nat_entry_set *head;
156 
157 	if (get_nat_flag(ne, IS_DIRTY))
158 		return;
159 
160 	head = radix_tree_lookup(&nm_i->nat_set_root, set);
161 	if (!head) {
162 		head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
163 
164 		INIT_LIST_HEAD(&head->entry_list);
165 		INIT_LIST_HEAD(&head->set_list);
166 		head->set = set;
167 		head->entry_cnt = 0;
168 		f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
169 	}
170 	list_move_tail(&ne->list, &head->entry_list);
171 	nm_i->dirty_nat_cnt++;
172 	head->entry_cnt++;
173 	set_nat_flag(ne, IS_DIRTY, true);
174 }
175 
176 static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
177 						struct nat_entry *ne)
178 {
179 	nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
180 	struct nat_entry_set *head;
181 
182 	head = radix_tree_lookup(&nm_i->nat_set_root, set);
183 	if (head) {
184 		list_move_tail(&ne->list, &nm_i->nat_entries);
185 		set_nat_flag(ne, IS_DIRTY, false);
186 		head->entry_cnt--;
187 		nm_i->dirty_nat_cnt--;
188 	}
189 }
190 
191 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
192 		nid_t start, unsigned int nr, struct nat_entry_set **ep)
193 {
194 	return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
195 							start, nr);
196 }
197 
198 int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
199 {
200 	struct f2fs_nm_info *nm_i = NM_I(sbi);
201 	struct nat_entry *e;
202 	bool need = false;
203 
204 	down_read(&nm_i->nat_tree_lock);
205 	e = __lookup_nat_cache(nm_i, nid);
206 	if (e) {
207 		if (!get_nat_flag(e, IS_CHECKPOINTED) &&
208 				!get_nat_flag(e, HAS_FSYNCED_INODE))
209 			need = true;
210 	}
211 	up_read(&nm_i->nat_tree_lock);
212 	return need;
213 }
214 
215 bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
216 {
217 	struct f2fs_nm_info *nm_i = NM_I(sbi);
218 	struct nat_entry *e;
219 	bool is_cp = true;
220 
221 	down_read(&nm_i->nat_tree_lock);
222 	e = __lookup_nat_cache(nm_i, nid);
223 	if (e && !get_nat_flag(e, IS_CHECKPOINTED))
224 		is_cp = false;
225 	up_read(&nm_i->nat_tree_lock);
226 	return is_cp;
227 }
228 
229 bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
230 {
231 	struct f2fs_nm_info *nm_i = NM_I(sbi);
232 	struct nat_entry *e;
233 	bool need_update = true;
234 
235 	down_read(&nm_i->nat_tree_lock);
236 	e = __lookup_nat_cache(nm_i, ino);
237 	if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
238 			(get_nat_flag(e, IS_CHECKPOINTED) ||
239 			 get_nat_flag(e, HAS_FSYNCED_INODE)))
240 		need_update = false;
241 	up_read(&nm_i->nat_tree_lock);
242 	return need_update;
243 }
244 
245 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
246 {
247 	struct nat_entry *new;
248 
249 	new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
250 	f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
251 	memset(new, 0, sizeof(struct nat_entry));
252 	nat_set_nid(new, nid);
253 	nat_reset_flag(new);
254 	list_add_tail(&new->list, &nm_i->nat_entries);
255 	nm_i->nat_cnt++;
256 	return new;
257 }
258 
259 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
260 						struct f2fs_nat_entry *ne)
261 {
262 	struct nat_entry *e;
263 
264 	down_write(&nm_i->nat_tree_lock);
265 	e = __lookup_nat_cache(nm_i, nid);
266 	if (!e) {
267 		e = grab_nat_entry(nm_i, nid);
268 		node_info_from_raw_nat(&e->ni, ne);
269 	}
270 	up_write(&nm_i->nat_tree_lock);
271 }
272 
273 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
274 			block_t new_blkaddr, bool fsync_done)
275 {
276 	struct f2fs_nm_info *nm_i = NM_I(sbi);
277 	struct nat_entry *e;
278 
279 	down_write(&nm_i->nat_tree_lock);
280 	e = __lookup_nat_cache(nm_i, ni->nid);
281 	if (!e) {
282 		e = grab_nat_entry(nm_i, ni->nid);
283 		copy_node_info(&e->ni, ni);
284 		f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
285 	} else if (new_blkaddr == NEW_ADDR) {
286 		/*
287 		 * when nid is reallocated,
288 		 * previous nat entry can be remained in nat cache.
289 		 * So, reinitialize it with new information.
290 		 */
291 		copy_node_info(&e->ni, ni);
292 		f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
293 	}
294 
295 	/* sanity check */
296 	f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
297 	f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
298 			new_blkaddr == NULL_ADDR);
299 	f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
300 			new_blkaddr == NEW_ADDR);
301 	f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
302 			nat_get_blkaddr(e) != NULL_ADDR &&
303 			new_blkaddr == NEW_ADDR);
304 
305 	/* increment version no as node is removed */
306 	if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
307 		unsigned char version = nat_get_version(e);
308 		nat_set_version(e, inc_node_version(version));
309 
310 		/* in order to reuse the nid */
311 		if (nm_i->next_scan_nid > ni->nid)
312 			nm_i->next_scan_nid = ni->nid;
313 	}
314 
315 	/* change address */
316 	nat_set_blkaddr(e, new_blkaddr);
317 	if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
318 		set_nat_flag(e, IS_CHECKPOINTED, false);
319 	__set_nat_cache_dirty(nm_i, e);
320 
321 	/* update fsync_mark if its inode nat entry is still alive */
322 	if (ni->nid != ni->ino)
323 		e = __lookup_nat_cache(nm_i, ni->ino);
324 	if (e) {
325 		if (fsync_done && ni->nid == ni->ino)
326 			set_nat_flag(e, HAS_FSYNCED_INODE, true);
327 		set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
328 	}
329 	up_write(&nm_i->nat_tree_lock);
330 }
331 
332 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
333 {
334 	struct f2fs_nm_info *nm_i = NM_I(sbi);
335 	int nr = nr_shrink;
336 
337 	if (!down_write_trylock(&nm_i->nat_tree_lock))
338 		return 0;
339 
340 	while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
341 		struct nat_entry *ne;
342 		ne = list_first_entry(&nm_i->nat_entries,
343 					struct nat_entry, list);
344 		__del_from_nat_cache(nm_i, ne);
345 		nr_shrink--;
346 	}
347 	up_write(&nm_i->nat_tree_lock);
348 	return nr - nr_shrink;
349 }
350 
351 /*
352  * This function always returns success
353  */
354 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
355 {
356 	struct f2fs_nm_info *nm_i = NM_I(sbi);
357 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
358 	struct f2fs_summary_block *sum = curseg->sum_blk;
359 	nid_t start_nid = START_NID(nid);
360 	struct f2fs_nat_block *nat_blk;
361 	struct page *page = NULL;
362 	struct f2fs_nat_entry ne;
363 	struct nat_entry *e;
364 	int i;
365 
366 	ni->nid = nid;
367 
368 	/* Check nat cache */
369 	down_read(&nm_i->nat_tree_lock);
370 	e = __lookup_nat_cache(nm_i, nid);
371 	if (e) {
372 		ni->ino = nat_get_ino(e);
373 		ni->blk_addr = nat_get_blkaddr(e);
374 		ni->version = nat_get_version(e);
375 	}
376 	up_read(&nm_i->nat_tree_lock);
377 	if (e)
378 		return;
379 
380 	memset(&ne, 0, sizeof(struct f2fs_nat_entry));
381 
382 	/* Check current segment summary */
383 	mutex_lock(&curseg->curseg_mutex);
384 	i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
385 	if (i >= 0) {
386 		ne = nat_in_journal(sum, i);
387 		node_info_from_raw_nat(ni, &ne);
388 	}
389 	mutex_unlock(&curseg->curseg_mutex);
390 	if (i >= 0)
391 		goto cache;
392 
393 	/* Fill node_info from nat page */
394 	page = get_current_nat_page(sbi, start_nid);
395 	nat_blk = (struct f2fs_nat_block *)page_address(page);
396 	ne = nat_blk->entries[nid - start_nid];
397 	node_info_from_raw_nat(ni, &ne);
398 	f2fs_put_page(page, 1);
399 cache:
400 	/* cache nat entry */
401 	cache_nat_entry(NM_I(sbi), nid, &ne);
402 }
403 
404 /*
405  * The maximum depth is four.
406  * Offset[0] will have raw inode offset.
407  */
408 static int get_node_path(struct f2fs_inode_info *fi, long block,
409 				int offset[4], unsigned int noffset[4])
410 {
411 	const long direct_index = ADDRS_PER_INODE(fi);
412 	const long direct_blks = ADDRS_PER_BLOCK;
413 	const long dptrs_per_blk = NIDS_PER_BLOCK;
414 	const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
415 	const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
416 	int n = 0;
417 	int level = 0;
418 
419 	noffset[0] = 0;
420 
421 	if (block < direct_index) {
422 		offset[n] = block;
423 		goto got;
424 	}
425 	block -= direct_index;
426 	if (block < direct_blks) {
427 		offset[n++] = NODE_DIR1_BLOCK;
428 		noffset[n] = 1;
429 		offset[n] = block;
430 		level = 1;
431 		goto got;
432 	}
433 	block -= direct_blks;
434 	if (block < direct_blks) {
435 		offset[n++] = NODE_DIR2_BLOCK;
436 		noffset[n] = 2;
437 		offset[n] = block;
438 		level = 1;
439 		goto got;
440 	}
441 	block -= direct_blks;
442 	if (block < indirect_blks) {
443 		offset[n++] = NODE_IND1_BLOCK;
444 		noffset[n] = 3;
445 		offset[n++] = block / direct_blks;
446 		noffset[n] = 4 + offset[n - 1];
447 		offset[n] = block % direct_blks;
448 		level = 2;
449 		goto got;
450 	}
451 	block -= indirect_blks;
452 	if (block < indirect_blks) {
453 		offset[n++] = NODE_IND2_BLOCK;
454 		noffset[n] = 4 + dptrs_per_blk;
455 		offset[n++] = block / direct_blks;
456 		noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
457 		offset[n] = block % direct_blks;
458 		level = 2;
459 		goto got;
460 	}
461 	block -= indirect_blks;
462 	if (block < dindirect_blks) {
463 		offset[n++] = NODE_DIND_BLOCK;
464 		noffset[n] = 5 + (dptrs_per_blk * 2);
465 		offset[n++] = block / indirect_blks;
466 		noffset[n] = 6 + (dptrs_per_blk * 2) +
467 			      offset[n - 1] * (dptrs_per_blk + 1);
468 		offset[n++] = (block / direct_blks) % dptrs_per_blk;
469 		noffset[n] = 7 + (dptrs_per_blk * 2) +
470 			      offset[n - 2] * (dptrs_per_blk + 1) +
471 			      offset[n - 1];
472 		offset[n] = block % direct_blks;
473 		level = 3;
474 		goto got;
475 	} else {
476 		BUG();
477 	}
478 got:
479 	return level;
480 }
481 
482 /*
483  * Caller should call f2fs_put_dnode(dn).
484  * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
485  * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
486  * In the case of RDONLY_NODE, we don't need to care about mutex.
487  */
488 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
489 {
490 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
491 	struct page *npage[4];
492 	struct page *parent = NULL;
493 	int offset[4];
494 	unsigned int noffset[4];
495 	nid_t nids[4];
496 	int level, i;
497 	int err = 0;
498 
499 	level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
500 
501 	nids[0] = dn->inode->i_ino;
502 	npage[0] = dn->inode_page;
503 
504 	if (!npage[0]) {
505 		npage[0] = get_node_page(sbi, nids[0]);
506 		if (IS_ERR(npage[0]))
507 			return PTR_ERR(npage[0]);
508 	}
509 
510 	/* if inline_data is set, should not report any block indices */
511 	if (f2fs_has_inline_data(dn->inode) && index) {
512 		err = -ENOENT;
513 		f2fs_put_page(npage[0], 1);
514 		goto release_out;
515 	}
516 
517 	parent = npage[0];
518 	if (level != 0)
519 		nids[1] = get_nid(parent, offset[0], true);
520 	dn->inode_page = npage[0];
521 	dn->inode_page_locked = true;
522 
523 	/* get indirect or direct nodes */
524 	for (i = 1; i <= level; i++) {
525 		bool done = false;
526 
527 		if (!nids[i] && mode == ALLOC_NODE) {
528 			/* alloc new node */
529 			if (!alloc_nid(sbi, &(nids[i]))) {
530 				err = -ENOSPC;
531 				goto release_pages;
532 			}
533 
534 			dn->nid = nids[i];
535 			npage[i] = new_node_page(dn, noffset[i], NULL);
536 			if (IS_ERR(npage[i])) {
537 				alloc_nid_failed(sbi, nids[i]);
538 				err = PTR_ERR(npage[i]);
539 				goto release_pages;
540 			}
541 
542 			set_nid(parent, offset[i - 1], nids[i], i == 1);
543 			alloc_nid_done(sbi, nids[i]);
544 			done = true;
545 		} else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
546 			npage[i] = get_node_page_ra(parent, offset[i - 1]);
547 			if (IS_ERR(npage[i])) {
548 				err = PTR_ERR(npage[i]);
549 				goto release_pages;
550 			}
551 			done = true;
552 		}
553 		if (i == 1) {
554 			dn->inode_page_locked = false;
555 			unlock_page(parent);
556 		} else {
557 			f2fs_put_page(parent, 1);
558 		}
559 
560 		if (!done) {
561 			npage[i] = get_node_page(sbi, nids[i]);
562 			if (IS_ERR(npage[i])) {
563 				err = PTR_ERR(npage[i]);
564 				f2fs_put_page(npage[0], 0);
565 				goto release_out;
566 			}
567 		}
568 		if (i < level) {
569 			parent = npage[i];
570 			nids[i + 1] = get_nid(parent, offset[i], false);
571 		}
572 	}
573 	dn->nid = nids[level];
574 	dn->ofs_in_node = offset[level];
575 	dn->node_page = npage[level];
576 	dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
577 	return 0;
578 
579 release_pages:
580 	f2fs_put_page(parent, 1);
581 	if (i > 1)
582 		f2fs_put_page(npage[0], 0);
583 release_out:
584 	dn->inode_page = NULL;
585 	dn->node_page = NULL;
586 	return err;
587 }
588 
589 static void truncate_node(struct dnode_of_data *dn)
590 {
591 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
592 	struct node_info ni;
593 
594 	get_node_info(sbi, dn->nid, &ni);
595 	if (dn->inode->i_blocks == 0) {
596 		f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
597 		goto invalidate;
598 	}
599 	f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
600 
601 	/* Deallocate node address */
602 	invalidate_blocks(sbi, ni.blk_addr);
603 	dec_valid_node_count(sbi, dn->inode);
604 	set_node_addr(sbi, &ni, NULL_ADDR, false);
605 
606 	if (dn->nid == dn->inode->i_ino) {
607 		remove_orphan_inode(sbi, dn->nid);
608 		dec_valid_inode_count(sbi);
609 	} else {
610 		sync_inode_page(dn);
611 	}
612 invalidate:
613 	clear_node_page_dirty(dn->node_page);
614 	set_sbi_flag(sbi, SBI_IS_DIRTY);
615 
616 	f2fs_put_page(dn->node_page, 1);
617 
618 	invalidate_mapping_pages(NODE_MAPPING(sbi),
619 			dn->node_page->index, dn->node_page->index);
620 
621 	dn->node_page = NULL;
622 	trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
623 }
624 
625 static int truncate_dnode(struct dnode_of_data *dn)
626 {
627 	struct page *page;
628 
629 	if (dn->nid == 0)
630 		return 1;
631 
632 	/* get direct node */
633 	page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
634 	if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
635 		return 1;
636 	else if (IS_ERR(page))
637 		return PTR_ERR(page);
638 
639 	/* Make dnode_of_data for parameter */
640 	dn->node_page = page;
641 	dn->ofs_in_node = 0;
642 	truncate_data_blocks(dn);
643 	truncate_node(dn);
644 	return 1;
645 }
646 
647 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
648 						int ofs, int depth)
649 {
650 	struct dnode_of_data rdn = *dn;
651 	struct page *page;
652 	struct f2fs_node *rn;
653 	nid_t child_nid;
654 	unsigned int child_nofs;
655 	int freed = 0;
656 	int i, ret;
657 
658 	if (dn->nid == 0)
659 		return NIDS_PER_BLOCK + 1;
660 
661 	trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
662 
663 	page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
664 	if (IS_ERR(page)) {
665 		trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
666 		return PTR_ERR(page);
667 	}
668 
669 	rn = F2FS_NODE(page);
670 	if (depth < 3) {
671 		for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
672 			child_nid = le32_to_cpu(rn->in.nid[i]);
673 			if (child_nid == 0)
674 				continue;
675 			rdn.nid = child_nid;
676 			ret = truncate_dnode(&rdn);
677 			if (ret < 0)
678 				goto out_err;
679 			set_nid(page, i, 0, false);
680 		}
681 	} else {
682 		child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
683 		for (i = ofs; i < NIDS_PER_BLOCK; i++) {
684 			child_nid = le32_to_cpu(rn->in.nid[i]);
685 			if (child_nid == 0) {
686 				child_nofs += NIDS_PER_BLOCK + 1;
687 				continue;
688 			}
689 			rdn.nid = child_nid;
690 			ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
691 			if (ret == (NIDS_PER_BLOCK + 1)) {
692 				set_nid(page, i, 0, false);
693 				child_nofs += ret;
694 			} else if (ret < 0 && ret != -ENOENT) {
695 				goto out_err;
696 			}
697 		}
698 		freed = child_nofs;
699 	}
700 
701 	if (!ofs) {
702 		/* remove current indirect node */
703 		dn->node_page = page;
704 		truncate_node(dn);
705 		freed++;
706 	} else {
707 		f2fs_put_page(page, 1);
708 	}
709 	trace_f2fs_truncate_nodes_exit(dn->inode, freed);
710 	return freed;
711 
712 out_err:
713 	f2fs_put_page(page, 1);
714 	trace_f2fs_truncate_nodes_exit(dn->inode, ret);
715 	return ret;
716 }
717 
718 static int truncate_partial_nodes(struct dnode_of_data *dn,
719 			struct f2fs_inode *ri, int *offset, int depth)
720 {
721 	struct page *pages[2];
722 	nid_t nid[3];
723 	nid_t child_nid;
724 	int err = 0;
725 	int i;
726 	int idx = depth - 2;
727 
728 	nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
729 	if (!nid[0])
730 		return 0;
731 
732 	/* get indirect nodes in the path */
733 	for (i = 0; i < idx + 1; i++) {
734 		/* reference count'll be increased */
735 		pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
736 		if (IS_ERR(pages[i])) {
737 			err = PTR_ERR(pages[i]);
738 			idx = i - 1;
739 			goto fail;
740 		}
741 		nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
742 	}
743 
744 	/* free direct nodes linked to a partial indirect node */
745 	for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
746 		child_nid = get_nid(pages[idx], i, false);
747 		if (!child_nid)
748 			continue;
749 		dn->nid = child_nid;
750 		err = truncate_dnode(dn);
751 		if (err < 0)
752 			goto fail;
753 		set_nid(pages[idx], i, 0, false);
754 	}
755 
756 	if (offset[idx + 1] == 0) {
757 		dn->node_page = pages[idx];
758 		dn->nid = nid[idx];
759 		truncate_node(dn);
760 	} else {
761 		f2fs_put_page(pages[idx], 1);
762 	}
763 	offset[idx]++;
764 	offset[idx + 1] = 0;
765 	idx--;
766 fail:
767 	for (i = idx; i >= 0; i--)
768 		f2fs_put_page(pages[i], 1);
769 
770 	trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
771 
772 	return err;
773 }
774 
775 /*
776  * All the block addresses of data and nodes should be nullified.
777  */
778 int truncate_inode_blocks(struct inode *inode, pgoff_t from)
779 {
780 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
781 	int err = 0, cont = 1;
782 	int level, offset[4], noffset[4];
783 	unsigned int nofs = 0;
784 	struct f2fs_inode *ri;
785 	struct dnode_of_data dn;
786 	struct page *page;
787 
788 	trace_f2fs_truncate_inode_blocks_enter(inode, from);
789 
790 	level = get_node_path(F2FS_I(inode), from, offset, noffset);
791 restart:
792 	page = get_node_page(sbi, inode->i_ino);
793 	if (IS_ERR(page)) {
794 		trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
795 		return PTR_ERR(page);
796 	}
797 
798 	set_new_dnode(&dn, inode, page, NULL, 0);
799 	unlock_page(page);
800 
801 	ri = F2FS_INODE(page);
802 	switch (level) {
803 	case 0:
804 	case 1:
805 		nofs = noffset[1];
806 		break;
807 	case 2:
808 		nofs = noffset[1];
809 		if (!offset[level - 1])
810 			goto skip_partial;
811 		err = truncate_partial_nodes(&dn, ri, offset, level);
812 		if (err < 0 && err != -ENOENT)
813 			goto fail;
814 		nofs += 1 + NIDS_PER_BLOCK;
815 		break;
816 	case 3:
817 		nofs = 5 + 2 * NIDS_PER_BLOCK;
818 		if (!offset[level - 1])
819 			goto skip_partial;
820 		err = truncate_partial_nodes(&dn, ri, offset, level);
821 		if (err < 0 && err != -ENOENT)
822 			goto fail;
823 		break;
824 	default:
825 		BUG();
826 	}
827 
828 skip_partial:
829 	while (cont) {
830 		dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
831 		switch (offset[0]) {
832 		case NODE_DIR1_BLOCK:
833 		case NODE_DIR2_BLOCK:
834 			err = truncate_dnode(&dn);
835 			break;
836 
837 		case NODE_IND1_BLOCK:
838 		case NODE_IND2_BLOCK:
839 			err = truncate_nodes(&dn, nofs, offset[1], 2);
840 			break;
841 
842 		case NODE_DIND_BLOCK:
843 			err = truncate_nodes(&dn, nofs, offset[1], 3);
844 			cont = 0;
845 			break;
846 
847 		default:
848 			BUG();
849 		}
850 		if (err < 0 && err != -ENOENT)
851 			goto fail;
852 		if (offset[1] == 0 &&
853 				ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
854 			lock_page(page);
855 			if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
856 				f2fs_put_page(page, 1);
857 				goto restart;
858 			}
859 			f2fs_wait_on_page_writeback(page, NODE);
860 			ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
861 			set_page_dirty(page);
862 			unlock_page(page);
863 		}
864 		offset[1] = 0;
865 		offset[0]++;
866 		nofs += err;
867 	}
868 fail:
869 	f2fs_put_page(page, 0);
870 	trace_f2fs_truncate_inode_blocks_exit(inode, err);
871 	return err > 0 ? 0 : err;
872 }
873 
874 int truncate_xattr_node(struct inode *inode, struct page *page)
875 {
876 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
877 	nid_t nid = F2FS_I(inode)->i_xattr_nid;
878 	struct dnode_of_data dn;
879 	struct page *npage;
880 
881 	if (!nid)
882 		return 0;
883 
884 	npage = get_node_page(sbi, nid);
885 	if (IS_ERR(npage))
886 		return PTR_ERR(npage);
887 
888 	F2FS_I(inode)->i_xattr_nid = 0;
889 
890 	/* need to do checkpoint during fsync */
891 	F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
892 
893 	set_new_dnode(&dn, inode, page, npage, nid);
894 
895 	if (page)
896 		dn.inode_page_locked = true;
897 	truncate_node(&dn);
898 	return 0;
899 }
900 
901 /*
902  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
903  * f2fs_unlock_op().
904  */
905 int remove_inode_page(struct inode *inode)
906 {
907 	struct dnode_of_data dn;
908 	int err;
909 
910 	set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
911 	err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
912 	if (err)
913 		return err;
914 
915 	err = truncate_xattr_node(inode, dn.inode_page);
916 	if (err) {
917 		f2fs_put_dnode(&dn);
918 		return err;
919 	}
920 
921 	/* remove potential inline_data blocks */
922 	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
923 				S_ISLNK(inode->i_mode))
924 		truncate_data_blocks_range(&dn, 1);
925 
926 	/* 0 is possible, after f2fs_new_inode() has failed */
927 	f2fs_bug_on(F2FS_I_SB(inode),
928 			inode->i_blocks != 0 && inode->i_blocks != 1);
929 
930 	/* will put inode & node pages */
931 	truncate_node(&dn);
932 	return 0;
933 }
934 
935 struct page *new_inode_page(struct inode *inode)
936 {
937 	struct dnode_of_data dn;
938 
939 	/* allocate inode page for new inode */
940 	set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
941 
942 	/* caller should f2fs_put_page(page, 1); */
943 	return new_node_page(&dn, 0, NULL);
944 }
945 
946 struct page *new_node_page(struct dnode_of_data *dn,
947 				unsigned int ofs, struct page *ipage)
948 {
949 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
950 	struct node_info old_ni, new_ni;
951 	struct page *page;
952 	int err;
953 
954 	if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
955 		return ERR_PTR(-EPERM);
956 
957 	page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
958 	if (!page)
959 		return ERR_PTR(-ENOMEM);
960 
961 	if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
962 		err = -ENOSPC;
963 		goto fail;
964 	}
965 
966 	get_node_info(sbi, dn->nid, &old_ni);
967 
968 	/* Reinitialize old_ni with new node page */
969 	f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
970 	new_ni = old_ni;
971 	new_ni.ino = dn->inode->i_ino;
972 	set_node_addr(sbi, &new_ni, NEW_ADDR, false);
973 
974 	f2fs_wait_on_page_writeback(page, NODE);
975 	fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
976 	set_cold_node(dn->inode, page);
977 	SetPageUptodate(page);
978 	set_page_dirty(page);
979 
980 	if (f2fs_has_xattr_block(ofs))
981 		F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
982 
983 	dn->node_page = page;
984 	if (ipage)
985 		update_inode(dn->inode, ipage);
986 	else
987 		sync_inode_page(dn);
988 	if (ofs == 0)
989 		inc_valid_inode_count(sbi);
990 
991 	return page;
992 
993 fail:
994 	clear_node_page_dirty(page);
995 	f2fs_put_page(page, 1);
996 	return ERR_PTR(err);
997 }
998 
999 /*
1000  * Caller should do after getting the following values.
1001  * 0: f2fs_put_page(page, 0)
1002  * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1003  */
1004 static int read_node_page(struct page *page, int rw)
1005 {
1006 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1007 	struct node_info ni;
1008 	struct f2fs_io_info fio = {
1009 		.sbi = sbi,
1010 		.type = NODE,
1011 		.rw = rw,
1012 		.page = page,
1013 		.encrypted_page = NULL,
1014 	};
1015 
1016 	get_node_info(sbi, page->index, &ni);
1017 
1018 	if (unlikely(ni.blk_addr == NULL_ADDR)) {
1019 		ClearPageUptodate(page);
1020 		return -ENOENT;
1021 	}
1022 
1023 	if (PageUptodate(page))
1024 		return LOCKED_PAGE;
1025 
1026 	fio.blk_addr = ni.blk_addr;
1027 	return f2fs_submit_page_bio(&fio);
1028 }
1029 
1030 /*
1031  * Readahead a node page
1032  */
1033 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1034 {
1035 	struct page *apage;
1036 	int err;
1037 
1038 	apage = find_get_page(NODE_MAPPING(sbi), nid);
1039 	if (apage && PageUptodate(apage)) {
1040 		f2fs_put_page(apage, 0);
1041 		return;
1042 	}
1043 	f2fs_put_page(apage, 0);
1044 
1045 	apage = grab_cache_page(NODE_MAPPING(sbi), nid);
1046 	if (!apage)
1047 		return;
1048 
1049 	err = read_node_page(apage, READA);
1050 	f2fs_put_page(apage, err ? 1 : 0);
1051 }
1052 
1053 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1054 {
1055 	struct page *page;
1056 	int err;
1057 repeat:
1058 	page = grab_cache_page(NODE_MAPPING(sbi), nid);
1059 	if (!page)
1060 		return ERR_PTR(-ENOMEM);
1061 
1062 	err = read_node_page(page, READ_SYNC);
1063 	if (err < 0) {
1064 		f2fs_put_page(page, 1);
1065 		return ERR_PTR(err);
1066 	} else if (err != LOCKED_PAGE) {
1067 		lock_page(page);
1068 	}
1069 
1070 	if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
1071 		ClearPageUptodate(page);
1072 		f2fs_put_page(page, 1);
1073 		return ERR_PTR(-EIO);
1074 	}
1075 	if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1076 		f2fs_put_page(page, 1);
1077 		goto repeat;
1078 	}
1079 	return page;
1080 }
1081 
1082 /*
1083  * Return a locked page for the desired node page.
1084  * And, readahead MAX_RA_NODE number of node pages.
1085  */
1086 struct page *get_node_page_ra(struct page *parent, int start)
1087 {
1088 	struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
1089 	struct blk_plug plug;
1090 	struct page *page;
1091 	int err, i, end;
1092 	nid_t nid;
1093 
1094 	/* First, try getting the desired direct node. */
1095 	nid = get_nid(parent, start, false);
1096 	if (!nid)
1097 		return ERR_PTR(-ENOENT);
1098 repeat:
1099 	page = grab_cache_page(NODE_MAPPING(sbi), nid);
1100 	if (!page)
1101 		return ERR_PTR(-ENOMEM);
1102 
1103 	err = read_node_page(page, READ_SYNC);
1104 	if (err < 0) {
1105 		f2fs_put_page(page, 1);
1106 		return ERR_PTR(err);
1107 	} else if (err == LOCKED_PAGE) {
1108 		goto page_hit;
1109 	}
1110 
1111 	blk_start_plug(&plug);
1112 
1113 	/* Then, try readahead for siblings of the desired node */
1114 	end = start + MAX_RA_NODE;
1115 	end = min(end, NIDS_PER_BLOCK);
1116 	for (i = start + 1; i < end; i++) {
1117 		nid = get_nid(parent, i, false);
1118 		if (!nid)
1119 			continue;
1120 		ra_node_page(sbi, nid);
1121 	}
1122 
1123 	blk_finish_plug(&plug);
1124 
1125 	lock_page(page);
1126 	if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1127 		f2fs_put_page(page, 1);
1128 		goto repeat;
1129 	}
1130 page_hit:
1131 	if (unlikely(!PageUptodate(page))) {
1132 		f2fs_put_page(page, 1);
1133 		return ERR_PTR(-EIO);
1134 	}
1135 	return page;
1136 }
1137 
1138 void sync_inode_page(struct dnode_of_data *dn)
1139 {
1140 	if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1141 		update_inode(dn->inode, dn->node_page);
1142 	} else if (dn->inode_page) {
1143 		if (!dn->inode_page_locked)
1144 			lock_page(dn->inode_page);
1145 		update_inode(dn->inode, dn->inode_page);
1146 		if (!dn->inode_page_locked)
1147 			unlock_page(dn->inode_page);
1148 	} else {
1149 		update_inode_page(dn->inode);
1150 	}
1151 }
1152 
1153 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1154 					struct writeback_control *wbc)
1155 {
1156 	pgoff_t index, end;
1157 	struct pagevec pvec;
1158 	int step = ino ? 2 : 0;
1159 	int nwritten = 0, wrote = 0;
1160 
1161 	pagevec_init(&pvec, 0);
1162 
1163 next_step:
1164 	index = 0;
1165 	end = LONG_MAX;
1166 
1167 	while (index <= end) {
1168 		int i, nr_pages;
1169 		nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1170 				PAGECACHE_TAG_DIRTY,
1171 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1172 		if (nr_pages == 0)
1173 			break;
1174 
1175 		for (i = 0; i < nr_pages; i++) {
1176 			struct page *page = pvec.pages[i];
1177 
1178 			/*
1179 			 * flushing sequence with step:
1180 			 * 0. indirect nodes
1181 			 * 1. dentry dnodes
1182 			 * 2. file dnodes
1183 			 */
1184 			if (step == 0 && IS_DNODE(page))
1185 				continue;
1186 			if (step == 1 && (!IS_DNODE(page) ||
1187 						is_cold_node(page)))
1188 				continue;
1189 			if (step == 2 && (!IS_DNODE(page) ||
1190 						!is_cold_node(page)))
1191 				continue;
1192 
1193 			/*
1194 			 * If an fsync mode,
1195 			 * we should not skip writing node pages.
1196 			 */
1197 			if (ino && ino_of_node(page) == ino)
1198 				lock_page(page);
1199 			else if (!trylock_page(page))
1200 				continue;
1201 
1202 			if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1203 continue_unlock:
1204 				unlock_page(page);
1205 				continue;
1206 			}
1207 			if (ino && ino_of_node(page) != ino)
1208 				goto continue_unlock;
1209 
1210 			if (!PageDirty(page)) {
1211 				/* someone wrote it for us */
1212 				goto continue_unlock;
1213 			}
1214 
1215 			if (!clear_page_dirty_for_io(page))
1216 				goto continue_unlock;
1217 
1218 			/* called by fsync() */
1219 			if (ino && IS_DNODE(page)) {
1220 				set_fsync_mark(page, 1);
1221 				if (IS_INODE(page))
1222 					set_dentry_mark(page,
1223 						need_dentry_mark(sbi, ino));
1224 				nwritten++;
1225 			} else {
1226 				set_fsync_mark(page, 0);
1227 				set_dentry_mark(page, 0);
1228 			}
1229 
1230 			if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1231 				unlock_page(page);
1232 			else
1233 				wrote++;
1234 
1235 			if (--wbc->nr_to_write == 0)
1236 				break;
1237 		}
1238 		pagevec_release(&pvec);
1239 		cond_resched();
1240 
1241 		if (wbc->nr_to_write == 0) {
1242 			step = 2;
1243 			break;
1244 		}
1245 	}
1246 
1247 	if (step < 2) {
1248 		step++;
1249 		goto next_step;
1250 	}
1251 
1252 	if (wrote)
1253 		f2fs_submit_merged_bio(sbi, NODE, WRITE);
1254 	return nwritten;
1255 }
1256 
1257 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1258 {
1259 	pgoff_t index = 0, end = LONG_MAX;
1260 	struct pagevec pvec;
1261 	int ret2 = 0, ret = 0;
1262 
1263 	pagevec_init(&pvec, 0);
1264 
1265 	while (index <= end) {
1266 		int i, nr_pages;
1267 		nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1268 				PAGECACHE_TAG_WRITEBACK,
1269 				min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1270 		if (nr_pages == 0)
1271 			break;
1272 
1273 		for (i = 0; i < nr_pages; i++) {
1274 			struct page *page = pvec.pages[i];
1275 
1276 			/* until radix tree lookup accepts end_index */
1277 			if (unlikely(page->index > end))
1278 				continue;
1279 
1280 			if (ino && ino_of_node(page) == ino) {
1281 				f2fs_wait_on_page_writeback(page, NODE);
1282 				if (TestClearPageError(page))
1283 					ret = -EIO;
1284 			}
1285 		}
1286 		pagevec_release(&pvec);
1287 		cond_resched();
1288 	}
1289 
1290 	if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
1291 		ret2 = -ENOSPC;
1292 	if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
1293 		ret2 = -EIO;
1294 	if (!ret)
1295 		ret = ret2;
1296 	return ret;
1297 }
1298 
1299 static int f2fs_write_node_page(struct page *page,
1300 				struct writeback_control *wbc)
1301 {
1302 	struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1303 	nid_t nid;
1304 	struct node_info ni;
1305 	struct f2fs_io_info fio = {
1306 		.sbi = sbi,
1307 		.type = NODE,
1308 		.rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
1309 		.page = page,
1310 		.encrypted_page = NULL,
1311 	};
1312 
1313 	trace_f2fs_writepage(page, NODE);
1314 
1315 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1316 		goto redirty_out;
1317 	if (unlikely(f2fs_cp_error(sbi)))
1318 		goto redirty_out;
1319 
1320 	f2fs_wait_on_page_writeback(page, NODE);
1321 
1322 	/* get old block addr of this node page */
1323 	nid = nid_of_node(page);
1324 	f2fs_bug_on(sbi, page->index != nid);
1325 
1326 	get_node_info(sbi, nid, &ni);
1327 
1328 	/* This page is already truncated */
1329 	if (unlikely(ni.blk_addr == NULL_ADDR)) {
1330 		ClearPageUptodate(page);
1331 		dec_page_count(sbi, F2FS_DIRTY_NODES);
1332 		unlock_page(page);
1333 		return 0;
1334 	}
1335 
1336 	if (wbc->for_reclaim) {
1337 		if (!down_read_trylock(&sbi->node_write))
1338 			goto redirty_out;
1339 	} else {
1340 		down_read(&sbi->node_write);
1341 	}
1342 
1343 	set_page_writeback(page);
1344 	fio.blk_addr = ni.blk_addr;
1345 	write_node_page(nid, &fio);
1346 	set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
1347 	dec_page_count(sbi, F2FS_DIRTY_NODES);
1348 	up_read(&sbi->node_write);
1349 	unlock_page(page);
1350 
1351 	if (wbc->for_reclaim)
1352 		f2fs_submit_merged_bio(sbi, NODE, WRITE);
1353 
1354 	return 0;
1355 
1356 redirty_out:
1357 	redirty_page_for_writepage(wbc, page);
1358 	return AOP_WRITEPAGE_ACTIVATE;
1359 }
1360 
1361 static int f2fs_write_node_pages(struct address_space *mapping,
1362 			    struct writeback_control *wbc)
1363 {
1364 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1365 	long diff;
1366 
1367 	trace_f2fs_writepages(mapping->host, wbc, NODE);
1368 
1369 	/* balancing f2fs's metadata in background */
1370 	f2fs_balance_fs_bg(sbi);
1371 
1372 	/* collect a number of dirty node pages and write together */
1373 	if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
1374 		goto skip_write;
1375 
1376 	diff = nr_pages_to_write(sbi, NODE, wbc);
1377 	wbc->sync_mode = WB_SYNC_NONE;
1378 	sync_node_pages(sbi, 0, wbc);
1379 	wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1380 	return 0;
1381 
1382 skip_write:
1383 	wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1384 	return 0;
1385 }
1386 
1387 static int f2fs_set_node_page_dirty(struct page *page)
1388 {
1389 	trace_f2fs_set_page_dirty(page, NODE);
1390 
1391 	SetPageUptodate(page);
1392 	if (!PageDirty(page)) {
1393 		__set_page_dirty_nobuffers(page);
1394 		inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
1395 		SetPagePrivate(page);
1396 		f2fs_trace_pid(page);
1397 		return 1;
1398 	}
1399 	return 0;
1400 }
1401 
1402 /*
1403  * Structure of the f2fs node operations
1404  */
1405 const struct address_space_operations f2fs_node_aops = {
1406 	.writepage	= f2fs_write_node_page,
1407 	.writepages	= f2fs_write_node_pages,
1408 	.set_page_dirty	= f2fs_set_node_page_dirty,
1409 	.invalidatepage	= f2fs_invalidate_page,
1410 	.releasepage	= f2fs_release_page,
1411 };
1412 
1413 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1414 						nid_t n)
1415 {
1416 	return radix_tree_lookup(&nm_i->free_nid_root, n);
1417 }
1418 
1419 static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1420 						struct free_nid *i)
1421 {
1422 	list_del(&i->list);
1423 	radix_tree_delete(&nm_i->free_nid_root, i->nid);
1424 }
1425 
1426 static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
1427 {
1428 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1429 	struct free_nid *i;
1430 	struct nat_entry *ne;
1431 	bool allocated = false;
1432 
1433 	if (!available_free_memory(sbi, FREE_NIDS))
1434 		return -1;
1435 
1436 	/* 0 nid should not be used */
1437 	if (unlikely(nid == 0))
1438 		return 0;
1439 
1440 	if (build) {
1441 		/* do not add allocated nids */
1442 		down_read(&nm_i->nat_tree_lock);
1443 		ne = __lookup_nat_cache(nm_i, nid);
1444 		if (ne &&
1445 			(!get_nat_flag(ne, IS_CHECKPOINTED) ||
1446 				nat_get_blkaddr(ne) != NULL_ADDR))
1447 			allocated = true;
1448 		up_read(&nm_i->nat_tree_lock);
1449 		if (allocated)
1450 			return 0;
1451 	}
1452 
1453 	i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1454 	i->nid = nid;
1455 	i->state = NID_NEW;
1456 
1457 	if (radix_tree_preload(GFP_NOFS)) {
1458 		kmem_cache_free(free_nid_slab, i);
1459 		return 0;
1460 	}
1461 
1462 	spin_lock(&nm_i->free_nid_list_lock);
1463 	if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
1464 		spin_unlock(&nm_i->free_nid_list_lock);
1465 		radix_tree_preload_end();
1466 		kmem_cache_free(free_nid_slab, i);
1467 		return 0;
1468 	}
1469 	list_add_tail(&i->list, &nm_i->free_nid_list);
1470 	nm_i->fcnt++;
1471 	spin_unlock(&nm_i->free_nid_list_lock);
1472 	radix_tree_preload_end();
1473 	return 1;
1474 }
1475 
1476 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1477 {
1478 	struct free_nid *i;
1479 	bool need_free = false;
1480 
1481 	spin_lock(&nm_i->free_nid_list_lock);
1482 	i = __lookup_free_nid_list(nm_i, nid);
1483 	if (i && i->state == NID_NEW) {
1484 		__del_from_free_nid_list(nm_i, i);
1485 		nm_i->fcnt--;
1486 		need_free = true;
1487 	}
1488 	spin_unlock(&nm_i->free_nid_list_lock);
1489 
1490 	if (need_free)
1491 		kmem_cache_free(free_nid_slab, i);
1492 }
1493 
1494 static void scan_nat_page(struct f2fs_sb_info *sbi,
1495 			struct page *nat_page, nid_t start_nid)
1496 {
1497 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1498 	struct f2fs_nat_block *nat_blk = page_address(nat_page);
1499 	block_t blk_addr;
1500 	int i;
1501 
1502 	i = start_nid % NAT_ENTRY_PER_BLOCK;
1503 
1504 	for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1505 
1506 		if (unlikely(start_nid >= nm_i->max_nid))
1507 			break;
1508 
1509 		blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1510 		f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
1511 		if (blk_addr == NULL_ADDR) {
1512 			if (add_free_nid(sbi, start_nid, true) < 0)
1513 				break;
1514 		}
1515 	}
1516 }
1517 
1518 static void build_free_nids(struct f2fs_sb_info *sbi)
1519 {
1520 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1521 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1522 	struct f2fs_summary_block *sum = curseg->sum_blk;
1523 	int i = 0;
1524 	nid_t nid = nm_i->next_scan_nid;
1525 
1526 	/* Enough entries */
1527 	if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1528 		return;
1529 
1530 	/* readahead nat pages to be scanned */
1531 	ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
1532 
1533 	while (1) {
1534 		struct page *page = get_current_nat_page(sbi, nid);
1535 
1536 		scan_nat_page(sbi, page, nid);
1537 		f2fs_put_page(page, 1);
1538 
1539 		nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1540 		if (unlikely(nid >= nm_i->max_nid))
1541 			nid = 0;
1542 
1543 		if (++i >= FREE_NID_PAGES)
1544 			break;
1545 	}
1546 
1547 	/* go to the next free nat pages to find free nids abundantly */
1548 	nm_i->next_scan_nid = nid;
1549 
1550 	/* find free nids from current sum_pages */
1551 	mutex_lock(&curseg->curseg_mutex);
1552 	for (i = 0; i < nats_in_cursum(sum); i++) {
1553 		block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1554 		nid = le32_to_cpu(nid_in_journal(sum, i));
1555 		if (addr == NULL_ADDR)
1556 			add_free_nid(sbi, nid, true);
1557 		else
1558 			remove_free_nid(nm_i, nid);
1559 	}
1560 	mutex_unlock(&curseg->curseg_mutex);
1561 }
1562 
1563 /*
1564  * If this function returns success, caller can obtain a new nid
1565  * from second parameter of this function.
1566  * The returned nid could be used ino as well as nid when inode is created.
1567  */
1568 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1569 {
1570 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1571 	struct free_nid *i = NULL;
1572 retry:
1573 	if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
1574 		return false;
1575 
1576 	spin_lock(&nm_i->free_nid_list_lock);
1577 
1578 	/* We should not use stale free nids created by build_free_nids */
1579 	if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
1580 		struct node_info ni;
1581 
1582 		f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
1583 		list_for_each_entry(i, &nm_i->free_nid_list, list)
1584 			if (i->state == NID_NEW)
1585 				break;
1586 
1587 		f2fs_bug_on(sbi, i->state != NID_NEW);
1588 		*nid = i->nid;
1589 		i->state = NID_ALLOC;
1590 		nm_i->fcnt--;
1591 		spin_unlock(&nm_i->free_nid_list_lock);
1592 
1593 		/* check nid is allocated already */
1594 		get_node_info(sbi, *nid, &ni);
1595 		if (ni.blk_addr != NULL_ADDR) {
1596 			alloc_nid_done(sbi, *nid);
1597 			goto retry;
1598 		}
1599 		return true;
1600 	}
1601 	spin_unlock(&nm_i->free_nid_list_lock);
1602 
1603 	/* Let's scan nat pages and its caches to get free nids */
1604 	mutex_lock(&nm_i->build_lock);
1605 	build_free_nids(sbi);
1606 	mutex_unlock(&nm_i->build_lock);
1607 	goto retry;
1608 }
1609 
1610 /*
1611  * alloc_nid() should be called prior to this function.
1612  */
1613 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1614 {
1615 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1616 	struct free_nid *i;
1617 
1618 	spin_lock(&nm_i->free_nid_list_lock);
1619 	i = __lookup_free_nid_list(nm_i, nid);
1620 	f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
1621 	__del_from_free_nid_list(nm_i, i);
1622 	spin_unlock(&nm_i->free_nid_list_lock);
1623 
1624 	kmem_cache_free(free_nid_slab, i);
1625 }
1626 
1627 /*
1628  * alloc_nid() should be called prior to this function.
1629  */
1630 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1631 {
1632 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1633 	struct free_nid *i;
1634 	bool need_free = false;
1635 
1636 	if (!nid)
1637 		return;
1638 
1639 	spin_lock(&nm_i->free_nid_list_lock);
1640 	i = __lookup_free_nid_list(nm_i, nid);
1641 	f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
1642 	if (!available_free_memory(sbi, FREE_NIDS)) {
1643 		__del_from_free_nid_list(nm_i, i);
1644 		need_free = true;
1645 	} else {
1646 		i->state = NID_NEW;
1647 		nm_i->fcnt++;
1648 	}
1649 	spin_unlock(&nm_i->free_nid_list_lock);
1650 
1651 	if (need_free)
1652 		kmem_cache_free(free_nid_slab, i);
1653 }
1654 
1655 int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
1656 {
1657 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1658 	struct free_nid *i, *next;
1659 	int nr = nr_shrink;
1660 
1661 	if (!mutex_trylock(&nm_i->build_lock))
1662 		return 0;
1663 
1664 	spin_lock(&nm_i->free_nid_list_lock);
1665 	list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
1666 		if (nr_shrink <= 0 || nm_i->fcnt <= NAT_ENTRY_PER_BLOCK)
1667 			break;
1668 		if (i->state == NID_ALLOC)
1669 			continue;
1670 		__del_from_free_nid_list(nm_i, i);
1671 		kmem_cache_free(free_nid_slab, i);
1672 		nm_i->fcnt--;
1673 		nr_shrink--;
1674 	}
1675 	spin_unlock(&nm_i->free_nid_list_lock);
1676 	mutex_unlock(&nm_i->build_lock);
1677 
1678 	return nr - nr_shrink;
1679 }
1680 
1681 void recover_inline_xattr(struct inode *inode, struct page *page)
1682 {
1683 	void *src_addr, *dst_addr;
1684 	size_t inline_size;
1685 	struct page *ipage;
1686 	struct f2fs_inode *ri;
1687 
1688 	ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
1689 	f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
1690 
1691 	ri = F2FS_INODE(page);
1692 	if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1693 		clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1694 		goto update_inode;
1695 	}
1696 
1697 	dst_addr = inline_xattr_addr(ipage);
1698 	src_addr = inline_xattr_addr(page);
1699 	inline_size = inline_xattr_size(inode);
1700 
1701 	f2fs_wait_on_page_writeback(ipage, NODE);
1702 	memcpy(dst_addr, src_addr, inline_size);
1703 update_inode:
1704 	update_inode(inode, ipage);
1705 	f2fs_put_page(ipage, 1);
1706 }
1707 
1708 void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1709 {
1710 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1711 	nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1712 	nid_t new_xnid = nid_of_node(page);
1713 	struct node_info ni;
1714 
1715 	/* 1: invalidate the previous xattr nid */
1716 	if (!prev_xnid)
1717 		goto recover_xnid;
1718 
1719 	/* Deallocate node address */
1720 	get_node_info(sbi, prev_xnid, &ni);
1721 	f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
1722 	invalidate_blocks(sbi, ni.blk_addr);
1723 	dec_valid_node_count(sbi, inode);
1724 	set_node_addr(sbi, &ni, NULL_ADDR, false);
1725 
1726 recover_xnid:
1727 	/* 2: allocate new xattr nid */
1728 	if (unlikely(!inc_valid_node_count(sbi, inode)))
1729 		f2fs_bug_on(sbi, 1);
1730 
1731 	remove_free_nid(NM_I(sbi), new_xnid);
1732 	get_node_info(sbi, new_xnid, &ni);
1733 	ni.ino = inode->i_ino;
1734 	set_node_addr(sbi, &ni, NEW_ADDR, false);
1735 	F2FS_I(inode)->i_xattr_nid = new_xnid;
1736 
1737 	/* 3: update xattr blkaddr */
1738 	refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
1739 	set_node_addr(sbi, &ni, blkaddr, false);
1740 
1741 	update_inode_page(inode);
1742 }
1743 
1744 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1745 {
1746 	struct f2fs_inode *src, *dst;
1747 	nid_t ino = ino_of_node(page);
1748 	struct node_info old_ni, new_ni;
1749 	struct page *ipage;
1750 
1751 	get_node_info(sbi, ino, &old_ni);
1752 
1753 	if (unlikely(old_ni.blk_addr != NULL_ADDR))
1754 		return -EINVAL;
1755 
1756 	ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
1757 	if (!ipage)
1758 		return -ENOMEM;
1759 
1760 	/* Should not use this inode from free nid list */
1761 	remove_free_nid(NM_I(sbi), ino);
1762 
1763 	SetPageUptodate(ipage);
1764 	fill_node_footer(ipage, ino, ino, 0, true);
1765 
1766 	src = F2FS_INODE(page);
1767 	dst = F2FS_INODE(ipage);
1768 
1769 	memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1770 	dst->i_size = 0;
1771 	dst->i_blocks = cpu_to_le64(1);
1772 	dst->i_links = cpu_to_le32(1);
1773 	dst->i_xattr_nid = 0;
1774 	dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
1775 
1776 	new_ni = old_ni;
1777 	new_ni.ino = ino;
1778 
1779 	if (unlikely(!inc_valid_node_count(sbi, NULL)))
1780 		WARN_ON(1);
1781 	set_node_addr(sbi, &new_ni, NEW_ADDR, false);
1782 	inc_valid_inode_count(sbi);
1783 	set_page_dirty(ipage);
1784 	f2fs_put_page(ipage, 1);
1785 	return 0;
1786 }
1787 
1788 int restore_node_summary(struct f2fs_sb_info *sbi,
1789 			unsigned int segno, struct f2fs_summary_block *sum)
1790 {
1791 	struct f2fs_node *rn;
1792 	struct f2fs_summary *sum_entry;
1793 	block_t addr;
1794 	int bio_blocks = MAX_BIO_BLOCKS(sbi);
1795 	int i, idx, last_offset, nrpages;
1796 
1797 	/* scan the node segment */
1798 	last_offset = sbi->blocks_per_seg;
1799 	addr = START_BLOCK(sbi, segno);
1800 	sum_entry = &sum->entries[0];
1801 
1802 	for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
1803 		nrpages = min(last_offset - i, bio_blocks);
1804 
1805 		/* readahead node pages */
1806 		ra_meta_pages(sbi, addr, nrpages, META_POR);
1807 
1808 		for (idx = addr; idx < addr + nrpages; idx++) {
1809 			struct page *page = get_meta_page(sbi, idx);
1810 
1811 			rn = F2FS_NODE(page);
1812 			sum_entry->nid = rn->footer.nid;
1813 			sum_entry->version = 0;
1814 			sum_entry->ofs_in_node = 0;
1815 			sum_entry++;
1816 			f2fs_put_page(page, 1);
1817 		}
1818 
1819 		invalidate_mapping_pages(META_MAPPING(sbi), addr,
1820 							addr + nrpages);
1821 	}
1822 	return 0;
1823 }
1824 
1825 static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
1826 {
1827 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1828 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1829 	struct f2fs_summary_block *sum = curseg->sum_blk;
1830 	int i;
1831 
1832 	mutex_lock(&curseg->curseg_mutex);
1833 	for (i = 0; i < nats_in_cursum(sum); i++) {
1834 		struct nat_entry *ne;
1835 		struct f2fs_nat_entry raw_ne;
1836 		nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1837 
1838 		raw_ne = nat_in_journal(sum, i);
1839 
1840 		down_write(&nm_i->nat_tree_lock);
1841 		ne = __lookup_nat_cache(nm_i, nid);
1842 		if (!ne) {
1843 			ne = grab_nat_entry(nm_i, nid);
1844 			node_info_from_raw_nat(&ne->ni, &raw_ne);
1845 		}
1846 		__set_nat_cache_dirty(nm_i, ne);
1847 		up_write(&nm_i->nat_tree_lock);
1848 	}
1849 	update_nats_in_cursum(sum, -i);
1850 	mutex_unlock(&curseg->curseg_mutex);
1851 }
1852 
1853 static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1854 						struct list_head *head, int max)
1855 {
1856 	struct nat_entry_set *cur;
1857 
1858 	if (nes->entry_cnt >= max)
1859 		goto add_out;
1860 
1861 	list_for_each_entry(cur, head, set_list) {
1862 		if (cur->entry_cnt >= nes->entry_cnt) {
1863 			list_add(&nes->set_list, cur->set_list.prev);
1864 			return;
1865 		}
1866 	}
1867 add_out:
1868 	list_add_tail(&nes->set_list, head);
1869 }
1870 
1871 static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1872 					struct nat_entry_set *set)
1873 {
1874 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1875 	struct f2fs_summary_block *sum = curseg->sum_blk;
1876 	nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1877 	bool to_journal = true;
1878 	struct f2fs_nat_block *nat_blk;
1879 	struct nat_entry *ne, *cur;
1880 	struct page *page = NULL;
1881 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1882 
1883 	/*
1884 	 * there are two steps to flush nat entries:
1885 	 * #1, flush nat entries to journal in current hot data summary block.
1886 	 * #2, flush nat entries to nat page.
1887 	 */
1888 	if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1889 		to_journal = false;
1890 
1891 	if (to_journal) {
1892 		mutex_lock(&curseg->curseg_mutex);
1893 	} else {
1894 		page = get_next_nat_page(sbi, start_nid);
1895 		nat_blk = page_address(page);
1896 		f2fs_bug_on(sbi, !nat_blk);
1897 	}
1898 
1899 	/* flush dirty nats in nat entry set */
1900 	list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1901 		struct f2fs_nat_entry *raw_ne;
1902 		nid_t nid = nat_get_nid(ne);
1903 		int offset;
1904 
1905 		if (nat_get_blkaddr(ne) == NEW_ADDR)
1906 			continue;
1907 
1908 		if (to_journal) {
1909 			offset = lookup_journal_in_cursum(sum,
1910 							NAT_JOURNAL, nid, 1);
1911 			f2fs_bug_on(sbi, offset < 0);
1912 			raw_ne = &nat_in_journal(sum, offset);
1913 			nid_in_journal(sum, offset) = cpu_to_le32(nid);
1914 		} else {
1915 			raw_ne = &nat_blk->entries[nid - start_nid];
1916 		}
1917 		raw_nat_from_node_info(raw_ne, &ne->ni);
1918 
1919 		down_write(&NM_I(sbi)->nat_tree_lock);
1920 		nat_reset_flag(ne);
1921 		__clear_nat_cache_dirty(NM_I(sbi), ne);
1922 		up_write(&NM_I(sbi)->nat_tree_lock);
1923 
1924 		if (nat_get_blkaddr(ne) == NULL_ADDR)
1925 			add_free_nid(sbi, nid, false);
1926 	}
1927 
1928 	if (to_journal)
1929 		mutex_unlock(&curseg->curseg_mutex);
1930 	else
1931 		f2fs_put_page(page, 1);
1932 
1933 	f2fs_bug_on(sbi, set->entry_cnt);
1934 
1935 	down_write(&nm_i->nat_tree_lock);
1936 	radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
1937 	up_write(&nm_i->nat_tree_lock);
1938 	kmem_cache_free(nat_entry_set_slab, set);
1939 }
1940 
1941 /*
1942  * This function is called during the checkpointing process.
1943  */
1944 void flush_nat_entries(struct f2fs_sb_info *sbi)
1945 {
1946 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1947 	struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1948 	struct f2fs_summary_block *sum = curseg->sum_blk;
1949 	struct nat_entry_set *setvec[SETVEC_SIZE];
1950 	struct nat_entry_set *set, *tmp;
1951 	unsigned int found;
1952 	nid_t set_idx = 0;
1953 	LIST_HEAD(sets);
1954 
1955 	if (!nm_i->dirty_nat_cnt)
1956 		return;
1957 	/*
1958 	 * if there are no enough space in journal to store dirty nat
1959 	 * entries, remove all entries from journal and merge them
1960 	 * into nat entry set.
1961 	 */
1962 	if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
1963 		remove_nats_in_journal(sbi);
1964 
1965 	down_write(&nm_i->nat_tree_lock);
1966 	while ((found = __gang_lookup_nat_set(nm_i,
1967 					set_idx, SETVEC_SIZE, setvec))) {
1968 		unsigned idx;
1969 		set_idx = setvec[found - 1]->set + 1;
1970 		for (idx = 0; idx < found; idx++)
1971 			__adjust_nat_entry_set(setvec[idx], &sets,
1972 							MAX_NAT_JENTRIES(sum));
1973 	}
1974 	up_write(&nm_i->nat_tree_lock);
1975 
1976 	/* flush dirty nats in nat entry set */
1977 	list_for_each_entry_safe(set, tmp, &sets, set_list)
1978 		__flush_nat_entry_set(sbi, set);
1979 
1980 	f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
1981 }
1982 
1983 static int init_node_manager(struct f2fs_sb_info *sbi)
1984 {
1985 	struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1986 	struct f2fs_nm_info *nm_i = NM_I(sbi);
1987 	unsigned char *version_bitmap;
1988 	unsigned int nat_segs, nat_blocks;
1989 
1990 	nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1991 
1992 	/* segment_count_nat includes pair segment so divide to 2. */
1993 	nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1994 	nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
1995 
1996 	nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1997 
1998 	/* not used nids: 0, node, meta, (and root counted as valid node) */
1999 	nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
2000 	nm_i->fcnt = 0;
2001 	nm_i->nat_cnt = 0;
2002 	nm_i->ram_thresh = DEF_RAM_THRESHOLD;
2003 
2004 	INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
2005 	INIT_LIST_HEAD(&nm_i->free_nid_list);
2006 	INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2007 	INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
2008 	INIT_LIST_HEAD(&nm_i->nat_entries);
2009 
2010 	mutex_init(&nm_i->build_lock);
2011 	spin_lock_init(&nm_i->free_nid_list_lock);
2012 	init_rwsem(&nm_i->nat_tree_lock);
2013 
2014 	nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
2015 	nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
2016 	version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2017 	if (!version_bitmap)
2018 		return -EFAULT;
2019 
2020 	nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2021 					GFP_KERNEL);
2022 	if (!nm_i->nat_bitmap)
2023 		return -ENOMEM;
2024 	return 0;
2025 }
2026 
2027 int build_node_manager(struct f2fs_sb_info *sbi)
2028 {
2029 	int err;
2030 
2031 	sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
2032 	if (!sbi->nm_info)
2033 		return -ENOMEM;
2034 
2035 	err = init_node_manager(sbi);
2036 	if (err)
2037 		return err;
2038 
2039 	build_free_nids(sbi);
2040 	return 0;
2041 }
2042 
2043 void destroy_node_manager(struct f2fs_sb_info *sbi)
2044 {
2045 	struct f2fs_nm_info *nm_i = NM_I(sbi);
2046 	struct free_nid *i, *next_i;
2047 	struct nat_entry *natvec[NATVEC_SIZE];
2048 	struct nat_entry_set *setvec[SETVEC_SIZE];
2049 	nid_t nid = 0;
2050 	unsigned int found;
2051 
2052 	if (!nm_i)
2053 		return;
2054 
2055 	/* destroy free nid list */
2056 	spin_lock(&nm_i->free_nid_list_lock);
2057 	list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
2058 		f2fs_bug_on(sbi, i->state == NID_ALLOC);
2059 		__del_from_free_nid_list(nm_i, i);
2060 		nm_i->fcnt--;
2061 		spin_unlock(&nm_i->free_nid_list_lock);
2062 		kmem_cache_free(free_nid_slab, i);
2063 		spin_lock(&nm_i->free_nid_list_lock);
2064 	}
2065 	f2fs_bug_on(sbi, nm_i->fcnt);
2066 	spin_unlock(&nm_i->free_nid_list_lock);
2067 
2068 	/* destroy nat cache */
2069 	down_write(&nm_i->nat_tree_lock);
2070 	while ((found = __gang_lookup_nat_cache(nm_i,
2071 					nid, NATVEC_SIZE, natvec))) {
2072 		unsigned idx;
2073 
2074 		nid = nat_get_nid(natvec[found - 1]) + 1;
2075 		for (idx = 0; idx < found; idx++)
2076 			__del_from_nat_cache(nm_i, natvec[idx]);
2077 	}
2078 	f2fs_bug_on(sbi, nm_i->nat_cnt);
2079 
2080 	/* destroy nat set cache */
2081 	nid = 0;
2082 	while ((found = __gang_lookup_nat_set(nm_i,
2083 					nid, SETVEC_SIZE, setvec))) {
2084 		unsigned idx;
2085 
2086 		nid = setvec[found - 1]->set + 1;
2087 		for (idx = 0; idx < found; idx++) {
2088 			/* entry_cnt is not zero, when cp_error was occurred */
2089 			f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2090 			radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2091 			kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2092 		}
2093 	}
2094 	up_write(&nm_i->nat_tree_lock);
2095 
2096 	kfree(nm_i->nat_bitmap);
2097 	sbi->nm_info = NULL;
2098 	kfree(nm_i);
2099 }
2100 
2101 int __init create_node_manager_caches(void)
2102 {
2103 	nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
2104 			sizeof(struct nat_entry));
2105 	if (!nat_entry_slab)
2106 		goto fail;
2107 
2108 	free_nid_slab = f2fs_kmem_cache_create("free_nid",
2109 			sizeof(struct free_nid));
2110 	if (!free_nid_slab)
2111 		goto destroy_nat_entry;
2112 
2113 	nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2114 			sizeof(struct nat_entry_set));
2115 	if (!nat_entry_set_slab)
2116 		goto destroy_free_nid;
2117 	return 0;
2118 
2119 destroy_free_nid:
2120 	kmem_cache_destroy(free_nid_slab);
2121 destroy_nat_entry:
2122 	kmem_cache_destroy(nat_entry_slab);
2123 fail:
2124 	return -ENOMEM;
2125 }
2126 
2127 void destroy_node_manager_caches(void)
2128 {
2129 	kmem_cache_destroy(nat_entry_set_slab);
2130 	kmem_cache_destroy(free_nid_slab);
2131 	kmem_cache_destroy(nat_entry_slab);
2132 }
2133