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