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