HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / f2fs / node.c
blob5823738493329ae4273bb26aa9da10697c7c37f6
1 /*
2 * fs/f2fs/node.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
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>
19 #include "f2fs.h"
20 #include "node.h"
21 #include "segment.h"
22 #include "trace.h"
23 #include <trace/events/f2fs.h>
25 #define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
27 static struct kmem_cache *nat_entry_slab;
28 static struct kmem_cache *free_nid_slab;
29 static struct kmem_cache *nat_entry_set_slab;
31 bool available_free_memory(struct f2fs_sb_info *sbi, int type)
33 struct f2fs_nm_info *nm_i = NM_I(sbi);
34 struct sysinfo val;
35 unsigned long avail_ram;
36 unsigned long mem_size = 0;
37 bool res = false;
39 si_meminfo(&val);
41 /* only uses low memory */
42 avail_ram = val.totalram - val.totalhigh;
45 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
47 if (type == FREE_NIDS) {
48 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
49 PAGE_CACHE_SHIFT;
50 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
51 } else if (type == NAT_ENTRIES) {
52 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
53 PAGE_CACHE_SHIFT;
54 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
55 } else if (type == DIRTY_DENTS) {
56 if (sbi->sb->s_bdi->wb.dirty_exceeded)
57 return false;
58 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
59 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
60 } else if (type == INO_ENTRIES) {
61 int i;
63 for (i = 0; i <= UPDATE_INO; i++)
64 mem_size += (sbi->im[i].ino_num *
65 sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
66 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
67 } else if (type == EXTENT_CACHE) {
68 mem_size = (sbi->total_ext_tree * sizeof(struct extent_tree) +
69 atomic_read(&sbi->total_ext_node) *
70 sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT;
71 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
72 } else {
73 if (sbi->sb->s_bdi->wb.dirty_exceeded)
74 return false;
76 return res;
79 static void clear_node_page_dirty(struct page *page)
81 struct address_space *mapping = page->mapping;
82 unsigned int long flags;
84 if (PageDirty(page)) {
85 spin_lock_irqsave(&mapping->tree_lock, flags);
86 radix_tree_tag_clear(&mapping->page_tree,
87 page_index(page),
88 PAGECACHE_TAG_DIRTY);
89 spin_unlock_irqrestore(&mapping->tree_lock, flags);
91 clear_page_dirty_for_io(page);
92 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
94 ClearPageUptodate(page);
97 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
99 pgoff_t index = current_nat_addr(sbi, nid);
100 return get_meta_page(sbi, index);
103 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
105 struct page *src_page;
106 struct page *dst_page;
107 pgoff_t src_off;
108 pgoff_t dst_off;
109 void *src_addr;
110 void *dst_addr;
111 struct f2fs_nm_info *nm_i = NM_I(sbi);
113 src_off = current_nat_addr(sbi, nid);
114 dst_off = next_nat_addr(sbi, src_off);
116 /* get current nat block page with lock */
117 src_page = get_meta_page(sbi, src_off);
118 dst_page = grab_meta_page(sbi, dst_off);
119 f2fs_bug_on(sbi, PageDirty(src_page));
121 src_addr = page_address(src_page);
122 dst_addr = page_address(dst_page);
123 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
124 set_page_dirty(dst_page);
125 f2fs_put_page(src_page, 1);
127 set_to_next_nat(nm_i, nid);
129 return dst_page;
132 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
134 return radix_tree_lookup(&nm_i->nat_root, n);
137 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
138 nid_t start, unsigned int nr, struct nat_entry **ep)
140 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
143 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
145 list_del(&e->list);
146 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
147 nm_i->nat_cnt--;
148 kmem_cache_free(nat_entry_slab, e);
151 static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
152 struct nat_entry *ne)
154 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
155 struct nat_entry_set *head;
157 if (get_nat_flag(ne, IS_DIRTY))
158 return;
160 head = radix_tree_lookup(&nm_i->nat_set_root, set);
161 if (!head) {
162 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
164 INIT_LIST_HEAD(&head->entry_list);
165 INIT_LIST_HEAD(&head->set_list);
166 head->set = set;
167 head->entry_cnt = 0;
168 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
170 list_move_tail(&ne->list, &head->entry_list);
171 nm_i->dirty_nat_cnt++;
172 head->entry_cnt++;
173 set_nat_flag(ne, IS_DIRTY, true);
176 static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
177 struct nat_entry *ne)
179 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
180 struct nat_entry_set *head;
182 head = radix_tree_lookup(&nm_i->nat_set_root, set);
183 if (head) {
184 list_move_tail(&ne->list, &nm_i->nat_entries);
185 set_nat_flag(ne, IS_DIRTY, false);
186 head->entry_cnt--;
187 nm_i->dirty_nat_cnt--;
191 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
192 nid_t start, unsigned int nr, struct nat_entry_set **ep)
194 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
195 start, nr);
198 int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
200 struct f2fs_nm_info *nm_i = NM_I(sbi);
201 struct nat_entry *e;
202 bool need = false;
204 down_read(&nm_i->nat_tree_lock);
205 e = __lookup_nat_cache(nm_i, nid);
206 if (e) {
207 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
208 !get_nat_flag(e, HAS_FSYNCED_INODE))
209 need = true;
211 up_read(&nm_i->nat_tree_lock);
212 return need;
215 bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
217 struct f2fs_nm_info *nm_i = NM_I(sbi);
218 struct nat_entry *e;
219 bool is_cp = true;
221 down_read(&nm_i->nat_tree_lock);
222 e = __lookup_nat_cache(nm_i, nid);
223 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
224 is_cp = false;
225 up_read(&nm_i->nat_tree_lock);
226 return is_cp;
229 bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
231 struct f2fs_nm_info *nm_i = NM_I(sbi);
232 struct nat_entry *e;
233 bool need_update = true;
235 down_read(&nm_i->nat_tree_lock);
236 e = __lookup_nat_cache(nm_i, ino);
237 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
238 (get_nat_flag(e, IS_CHECKPOINTED) ||
239 get_nat_flag(e, HAS_FSYNCED_INODE)))
240 need_update = false;
241 up_read(&nm_i->nat_tree_lock);
242 return need_update;
245 static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
247 struct nat_entry *new;
249 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_NOFS);
250 f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
251 memset(new, 0, sizeof(struct nat_entry));
252 nat_set_nid(new, nid);
253 nat_reset_flag(new);
254 list_add_tail(&new->list, &nm_i->nat_entries);
255 nm_i->nat_cnt++;
256 return new;
259 static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
260 struct f2fs_nat_entry *ne)
262 struct nat_entry *e;
264 e = __lookup_nat_cache(nm_i, nid);
265 if (!e) {
266 e = grab_nat_entry(nm_i, nid);
267 node_info_from_raw_nat(&e->ni, ne);
271 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
272 block_t new_blkaddr, bool fsync_done)
274 struct f2fs_nm_info *nm_i = NM_I(sbi);
275 struct nat_entry *e;
277 down_write(&nm_i->nat_tree_lock);
278 e = __lookup_nat_cache(nm_i, ni->nid);
279 if (!e) {
280 e = grab_nat_entry(nm_i, ni->nid);
281 copy_node_info(&e->ni, ni);
282 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
283 } else if (new_blkaddr == NEW_ADDR) {
285 * when nid is reallocated,
286 * previous nat entry can be remained in nat cache.
287 * So, reinitialize it with new information.
289 copy_node_info(&e->ni, ni);
290 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
293 /* sanity check */
294 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
295 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
296 new_blkaddr == NULL_ADDR);
297 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
298 new_blkaddr == NEW_ADDR);
299 f2fs_bug_on(sbi, is_valid_data_blkaddr(sbi, nat_get_blkaddr(e)) &&
300 new_blkaddr == NEW_ADDR);
302 /* increment version no as node is removed */
303 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
304 unsigned char version = nat_get_version(e);
305 nat_set_version(e, inc_node_version(version));
307 /* in order to reuse the nid */
308 if (nm_i->next_scan_nid > ni->nid)
309 nm_i->next_scan_nid = ni->nid;
312 /* change address */
313 nat_set_blkaddr(e, new_blkaddr);
314 if (!is_valid_data_blkaddr(sbi, new_blkaddr))
315 set_nat_flag(e, IS_CHECKPOINTED, false);
316 __set_nat_cache_dirty(nm_i, e);
318 /* update fsync_mark if its inode nat entry is still alive */
319 if (ni->nid != ni->ino)
320 e = __lookup_nat_cache(nm_i, ni->ino);
321 if (e) {
322 if (fsync_done && ni->nid == ni->ino)
323 set_nat_flag(e, HAS_FSYNCED_INODE, true);
324 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
326 up_write(&nm_i->nat_tree_lock);
329 int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
331 struct f2fs_nm_info *nm_i = NM_I(sbi);
332 int nr = nr_shrink;
334 if (!down_write_trylock(&nm_i->nat_tree_lock))
335 return 0;
337 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
338 struct nat_entry *ne;
339 ne = list_first_entry(&nm_i->nat_entries,
340 struct nat_entry, list);
341 __del_from_nat_cache(nm_i, ne);
342 nr_shrink--;
344 up_write(&nm_i->nat_tree_lock);
345 return nr - nr_shrink;
349 * This function always returns success
351 void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
353 struct f2fs_nm_info *nm_i = NM_I(sbi);
354 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
355 struct f2fs_summary_block *sum = curseg->sum_blk;
356 nid_t start_nid = START_NID(nid);
357 struct f2fs_nat_block *nat_blk;
358 struct page *page = NULL;
359 struct f2fs_nat_entry ne;
360 struct nat_entry *e;
361 int i;
363 ni->nid = nid;
365 /* Check nat cache */
366 down_read(&nm_i->nat_tree_lock);
367 e = __lookup_nat_cache(nm_i, nid);
368 if (e) {
369 ni->ino = nat_get_ino(e);
370 ni->blk_addr = nat_get_blkaddr(e);
371 ni->version = nat_get_version(e);
373 up_read(&nm_i->nat_tree_lock);
374 if (e)
375 return;
377 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
379 down_write(&nm_i->nat_tree_lock);
381 /* Check current segment summary */
382 mutex_lock(&curseg->curseg_mutex);
383 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
384 if (i >= 0) {
385 ne = nat_in_journal(sum, i);
386 node_info_from_raw_nat(ni, &ne);
388 mutex_unlock(&curseg->curseg_mutex);
389 if (i >= 0)
390 goto cache;
392 /* Fill node_info from nat page */
393 page = get_current_nat_page(sbi, start_nid);
394 nat_blk = (struct f2fs_nat_block *)page_address(page);
395 ne = nat_blk->entries[nid - start_nid];
396 node_info_from_raw_nat(ni, &ne);
397 f2fs_put_page(page, 1);
398 cache:
399 /* cache nat entry */
400 cache_nat_entry(NM_I(sbi), nid, &ne);
401 up_write(&nm_i->nat_tree_lock);
405 * The maximum depth is four.
406 * Offset[0] will have raw inode offset.
408 static int get_node_path(struct f2fs_inode_info *fi, long block,
409 int offset[4], unsigned int noffset[4])
411 const long direct_index = ADDRS_PER_INODE(fi);
412 const long direct_blks = ADDRS_PER_BLOCK;
413 const long dptrs_per_blk = NIDS_PER_BLOCK;
414 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
415 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
416 int n = 0;
417 int level = 0;
419 noffset[0] = 0;
421 if (block < direct_index) {
422 offset[n] = block;
423 goto got;
425 block -= direct_index;
426 if (block < direct_blks) {
427 offset[n++] = NODE_DIR1_BLOCK;
428 noffset[n] = 1;
429 offset[n] = block;
430 level = 1;
431 goto got;
433 block -= direct_blks;
434 if (block < direct_blks) {
435 offset[n++] = NODE_DIR2_BLOCK;
436 noffset[n] = 2;
437 offset[n] = block;
438 level = 1;
439 goto got;
441 block -= direct_blks;
442 if (block < indirect_blks) {
443 offset[n++] = NODE_IND1_BLOCK;
444 noffset[n] = 3;
445 offset[n++] = block / direct_blks;
446 noffset[n] = 4 + offset[n - 1];
447 offset[n] = block % direct_blks;
448 level = 2;
449 goto got;
451 block -= indirect_blks;
452 if (block < indirect_blks) {
453 offset[n++] = NODE_IND2_BLOCK;
454 noffset[n] = 4 + dptrs_per_blk;
455 offset[n++] = block / direct_blks;
456 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
457 offset[n] = block % direct_blks;
458 level = 2;
459 goto got;
461 block -= indirect_blks;
462 if (block < dindirect_blks) {
463 offset[n++] = NODE_DIND_BLOCK;
464 noffset[n] = 5 + (dptrs_per_blk * 2);
465 offset[n++] = block / indirect_blks;
466 noffset[n] = 6 + (dptrs_per_blk * 2) +
467 offset[n - 1] * (dptrs_per_blk + 1);
468 offset[n++] = (block / direct_blks) % dptrs_per_blk;
469 noffset[n] = 7 + (dptrs_per_blk * 2) +
470 offset[n - 2] * (dptrs_per_blk + 1) +
471 offset[n - 1];
472 offset[n] = block % direct_blks;
473 level = 3;
474 goto got;
475 } else {
476 BUG();
478 got:
479 return level;
483 * Caller should call f2fs_put_dnode(dn).
484 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
485 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
486 * In the case of RDONLY_NODE, we don't need to care about mutex.
488 int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
490 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
491 struct page *npage[4];
492 struct page *parent = NULL;
493 int offset[4];
494 unsigned int noffset[4];
495 nid_t nids[4];
496 int level, i;
497 int err = 0;
499 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
501 nids[0] = dn->inode->i_ino;
502 npage[0] = dn->inode_page;
504 if (!npage[0]) {
505 npage[0] = get_node_page(sbi, nids[0]);
506 if (IS_ERR(npage[0]))
507 return PTR_ERR(npage[0]);
510 /* if inline_data is set, should not report any block indices */
511 if (f2fs_has_inline_data(dn->inode) && index) {
512 err = -ENOENT;
513 f2fs_put_page(npage[0], 1);
514 goto release_out;
517 parent = npage[0];
518 if (level != 0)
519 nids[1] = get_nid(parent, offset[0], true);
520 dn->inode_page = npage[0];
521 dn->inode_page_locked = true;
523 /* get indirect or direct nodes */
524 for (i = 1; i <= level; i++) {
525 bool done = false;
527 if (!nids[i] && mode == ALLOC_NODE) {
528 /* alloc new node */
529 if (!alloc_nid(sbi, &(nids[i]))) {
530 err = -ENOSPC;
531 goto release_pages;
534 dn->nid = nids[i];
535 npage[i] = new_node_page(dn, noffset[i], NULL);
536 if (IS_ERR(npage[i])) {
537 alloc_nid_failed(sbi, nids[i]);
538 err = PTR_ERR(npage[i]);
539 goto release_pages;
542 set_nid(parent, offset[i - 1], nids[i], i == 1);
543 alloc_nid_done(sbi, nids[i]);
544 done = true;
545 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
546 npage[i] = get_node_page_ra(parent, offset[i - 1]);
547 if (IS_ERR(npage[i])) {
548 err = PTR_ERR(npage[i]);
549 goto release_pages;
551 done = true;
553 if (i == 1) {
554 dn->inode_page_locked = false;
555 unlock_page(parent);
556 } else {
557 f2fs_put_page(parent, 1);
560 if (!done) {
561 npage[i] = get_node_page(sbi, nids[i]);
562 if (IS_ERR(npage[i])) {
563 err = PTR_ERR(npage[i]);
564 f2fs_put_page(npage[0], 0);
565 goto release_out;
568 if (i < level) {
569 parent = npage[i];
570 nids[i + 1] = get_nid(parent, offset[i], false);
573 dn->nid = nids[level];
574 dn->ofs_in_node = offset[level];
575 dn->node_page = npage[level];
576 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
577 return 0;
579 release_pages:
580 f2fs_put_page(parent, 1);
581 if (i > 1)
582 f2fs_put_page(npage[0], 0);
583 release_out:
584 dn->inode_page = NULL;
585 dn->node_page = NULL;
586 return err;
589 static void truncate_node(struct dnode_of_data *dn)
591 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
592 struct node_info ni;
593 pgoff_t index;
595 get_node_info(sbi, dn->nid, &ni);
596 if (dn->inode->i_blocks == 0) {
597 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
598 goto invalidate;
600 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
602 /* Deallocate node address */
603 invalidate_blocks(sbi, ni.blk_addr);
604 dec_valid_node_count(sbi, dn->inode);
605 set_node_addr(sbi, &ni, NULL_ADDR, false);
607 if (dn->nid == dn->inode->i_ino) {
608 remove_orphan_inode(sbi, dn->nid);
609 dec_valid_inode_count(sbi);
610 } else {
611 sync_inode_page(dn);
613 invalidate:
614 clear_node_page_dirty(dn->node_page);
615 set_sbi_flag(sbi, SBI_IS_DIRTY);
617 index = dn->node_page->index;
618 f2fs_put_page(dn->node_page, 1);
620 invalidate_mapping_pages(NODE_MAPPING(sbi),
621 index, index);
623 dn->node_page = NULL;
624 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
627 static int truncate_dnode(struct dnode_of_data *dn)
629 struct page *page;
631 if (dn->nid == 0)
632 return 1;
634 /* get direct node */
635 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
636 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
637 return 1;
638 else if (IS_ERR(page))
639 return PTR_ERR(page);
641 /* Make dnode_of_data for parameter */
642 dn->node_page = page;
643 dn->ofs_in_node = 0;
644 truncate_data_blocks(dn);
645 truncate_node(dn);
646 return 1;
649 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
650 int ofs, int depth)
652 struct dnode_of_data rdn = *dn;
653 struct page *page;
654 struct f2fs_node *rn;
655 nid_t child_nid;
656 unsigned int child_nofs;
657 int freed = 0;
658 int i, ret;
660 if (dn->nid == 0)
661 return NIDS_PER_BLOCK + 1;
663 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
665 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
666 if (IS_ERR(page)) {
667 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
668 return PTR_ERR(page);
671 rn = F2FS_NODE(page);
672 if (depth < 3) {
673 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
674 child_nid = le32_to_cpu(rn->in.nid[i]);
675 if (child_nid == 0)
676 continue;
677 rdn.nid = child_nid;
678 ret = truncate_dnode(&rdn);
679 if (ret < 0)
680 goto out_err;
681 set_nid(page, i, 0, false);
683 } else {
684 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
685 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
686 child_nid = le32_to_cpu(rn->in.nid[i]);
687 if (child_nid == 0) {
688 child_nofs += NIDS_PER_BLOCK + 1;
689 continue;
691 rdn.nid = child_nid;
692 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
693 if (ret == (NIDS_PER_BLOCK + 1)) {
694 set_nid(page, i, 0, false);
695 child_nofs += ret;
696 } else if (ret < 0 && ret != -ENOENT) {
697 goto out_err;
700 freed = child_nofs;
703 if (!ofs) {
704 /* remove current indirect node */
705 dn->node_page = page;
706 truncate_node(dn);
707 freed++;
708 } else {
709 f2fs_put_page(page, 1);
711 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
712 return freed;
714 out_err:
715 f2fs_put_page(page, 1);
716 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
717 return ret;
720 static int truncate_partial_nodes(struct dnode_of_data *dn,
721 struct f2fs_inode *ri, int *offset, int depth)
723 struct page *pages[2];
724 nid_t nid[3];
725 nid_t child_nid;
726 int err = 0;
727 int i;
728 int idx = depth - 2;
730 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
731 if (!nid[0])
732 return 0;
734 /* get indirect nodes in the path */
735 for (i = 0; i < idx + 1; i++) {
736 /* reference count'll be increased */
737 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
738 if (IS_ERR(pages[i])) {
739 err = PTR_ERR(pages[i]);
740 idx = i - 1;
741 goto fail;
743 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
746 /* free direct nodes linked to a partial indirect node */
747 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
748 child_nid = get_nid(pages[idx], i, false);
749 if (!child_nid)
750 continue;
751 dn->nid = child_nid;
752 err = truncate_dnode(dn);
753 if (err < 0)
754 goto fail;
755 set_nid(pages[idx], i, 0, false);
758 if (offset[idx + 1] == 0) {
759 dn->node_page = pages[idx];
760 dn->nid = nid[idx];
761 truncate_node(dn);
762 } else {
763 f2fs_put_page(pages[idx], 1);
765 offset[idx]++;
766 offset[idx + 1] = 0;
767 idx--;
768 fail:
769 for (i = idx; i >= 0; i--)
770 f2fs_put_page(pages[i], 1);
772 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
774 return err;
778 * All the block addresses of data and nodes should be nullified.
780 int truncate_inode_blocks(struct inode *inode, pgoff_t from)
782 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
783 int err = 0, cont = 1;
784 int level, offset[4], noffset[4];
785 unsigned int nofs = 0;
786 struct f2fs_inode *ri;
787 struct dnode_of_data dn;
788 struct page *page;
790 trace_f2fs_truncate_inode_blocks_enter(inode, from);
792 level = get_node_path(F2FS_I(inode), from, offset, noffset);
793 restart:
794 page = get_node_page(sbi, inode->i_ino);
795 if (IS_ERR(page)) {
796 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
797 return PTR_ERR(page);
800 set_new_dnode(&dn, inode, page, NULL, 0);
801 unlock_page(page);
803 ri = F2FS_INODE(page);
804 switch (level) {
805 case 0:
806 case 1:
807 nofs = noffset[1];
808 break;
809 case 2:
810 nofs = noffset[1];
811 if (!offset[level - 1])
812 goto skip_partial;
813 err = truncate_partial_nodes(&dn, ri, offset, level);
814 if (err < 0 && err != -ENOENT)
815 goto fail;
816 nofs += 1 + NIDS_PER_BLOCK;
817 break;
818 case 3:
819 nofs = 5 + 2 * NIDS_PER_BLOCK;
820 if (!offset[level - 1])
821 goto skip_partial;
822 err = truncate_partial_nodes(&dn, ri, offset, level);
823 if (err < 0 && err != -ENOENT)
824 goto fail;
825 break;
826 default:
827 BUG();
830 skip_partial:
831 while (cont) {
832 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
833 switch (offset[0]) {
834 case NODE_DIR1_BLOCK:
835 case NODE_DIR2_BLOCK:
836 err = truncate_dnode(&dn);
837 break;
839 case NODE_IND1_BLOCK:
840 case NODE_IND2_BLOCK:
841 err = truncate_nodes(&dn, nofs, offset[1], 2);
842 break;
844 case NODE_DIND_BLOCK:
845 err = truncate_nodes(&dn, nofs, offset[1], 3);
846 cont = 0;
847 break;
849 default:
850 BUG();
852 if (err < 0 && err != -ENOENT)
853 goto fail;
854 if (offset[1] == 0 &&
855 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
856 lock_page(page);
857 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
858 f2fs_put_page(page, 1);
859 goto restart;
861 f2fs_wait_on_page_writeback(page, NODE);
862 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
863 set_page_dirty(page);
864 unlock_page(page);
866 offset[1] = 0;
867 offset[0]++;
868 nofs += err;
870 fail:
871 f2fs_put_page(page, 0);
872 trace_f2fs_truncate_inode_blocks_exit(inode, err);
873 return err > 0 ? 0 : err;
876 int truncate_xattr_node(struct inode *inode, struct page *page)
878 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
879 nid_t nid = F2FS_I(inode)->i_xattr_nid;
880 struct dnode_of_data dn;
881 struct page *npage;
883 if (!nid)
884 return 0;
886 npage = get_node_page(sbi, nid);
887 if (IS_ERR(npage))
888 return PTR_ERR(npage);
890 F2FS_I(inode)->i_xattr_nid = 0;
892 /* need to do checkpoint during fsync */
893 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
895 set_new_dnode(&dn, inode, page, npage, nid);
897 if (page)
898 dn.inode_page_locked = true;
899 truncate_node(&dn);
900 return 0;
904 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
905 * f2fs_unlock_op().
907 int remove_inode_page(struct inode *inode)
909 struct dnode_of_data dn;
910 int err;
912 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
913 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
914 if (err)
915 return err;
917 err = truncate_xattr_node(inode, dn.inode_page);
918 if (err) {
919 f2fs_put_dnode(&dn);
920 return err;
923 /* remove potential inline_data blocks */
924 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
925 S_ISLNK(inode->i_mode))
926 truncate_data_blocks_range(&dn, 1);
928 /* 0 is possible, after f2fs_new_inode() has failed */
929 f2fs_bug_on(F2FS_I_SB(inode),
930 inode->i_blocks != 0 && inode->i_blocks != 1);
932 /* will put inode & node pages */
933 truncate_node(&dn);
934 return 0;
937 struct page *new_inode_page(struct inode *inode)
939 struct dnode_of_data dn;
941 /* allocate inode page for new inode */
942 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
944 /* caller should f2fs_put_page(page, 1); */
945 return new_node_page(&dn, 0, NULL);
948 struct page *new_node_page(struct dnode_of_data *dn,
949 unsigned int ofs, struct page *ipage)
951 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
952 struct node_info old_ni, new_ni;
953 struct page *page;
954 int err;
956 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
957 return ERR_PTR(-EPERM);
959 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
960 if (!page)
961 return ERR_PTR(-ENOMEM);
963 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
964 err = -ENOSPC;
965 goto fail;
968 get_node_info(sbi, dn->nid, &old_ni);
970 /* Reinitialize old_ni with new node page */
971 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
972 new_ni = old_ni;
973 new_ni.ino = dn->inode->i_ino;
974 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
976 f2fs_wait_on_page_writeback(page, NODE);
977 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
978 set_cold_node(dn->inode, page);
979 SetPageUptodate(page);
980 set_page_dirty(page);
982 if (f2fs_has_xattr_block(ofs))
983 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
985 dn->node_page = page;
986 if (ipage)
987 update_inode(dn->inode, ipage);
988 else
989 sync_inode_page(dn);
990 if (ofs == 0)
991 inc_valid_inode_count(sbi);
993 return page;
995 fail:
996 clear_node_page_dirty(page);
997 f2fs_put_page(page, 1);
998 return ERR_PTR(err);
1002 * Caller should do after getting the following values.
1003 * 0: f2fs_put_page(page, 0)
1004 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1006 static int read_node_page(struct page *page, int rw)
1008 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1009 struct node_info ni;
1010 struct f2fs_io_info fio = {
1011 .sbi = sbi,
1012 .type = NODE,
1013 .rw = rw,
1014 .page = page,
1015 .encrypted_page = NULL,
1018 get_node_info(sbi, page->index, &ni);
1020 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1021 ClearPageUptodate(page);
1022 return -ENOENT;
1025 if (PageUptodate(page))
1026 return LOCKED_PAGE;
1028 fio.blk_addr = ni.blk_addr;
1029 return f2fs_submit_page_bio(&fio);
1033 * Readahead a node page
1035 void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1037 struct page *apage;
1038 int err;
1040 apage = find_get_page(NODE_MAPPING(sbi), nid);
1041 if (apage && PageUptodate(apage)) {
1042 f2fs_put_page(apage, 0);
1043 return;
1045 f2fs_put_page(apage, 0);
1047 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
1048 if (!apage)
1049 return;
1051 err = read_node_page(apage, READA);
1052 f2fs_put_page(apage, err ? 1 : 0);
1055 struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1057 struct page *page;
1058 int err;
1059 repeat:
1060 page = grab_cache_page(NODE_MAPPING(sbi), nid);
1061 if (!page)
1062 return ERR_PTR(-ENOMEM);
1064 err = read_node_page(page, READ_SYNC);
1065 if (err < 0) {
1066 f2fs_put_page(page, 1);
1067 return ERR_PTR(err);
1068 } else if (err != LOCKED_PAGE) {
1069 lock_page(page);
1072 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
1073 ClearPageUptodate(page);
1074 f2fs_put_page(page, 1);
1075 return ERR_PTR(-EIO);
1077 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1078 f2fs_put_page(page, 1);
1079 goto repeat;
1081 return page;
1085 * Return a locked page for the desired node page.
1086 * And, readahead MAX_RA_NODE number of node pages.
1088 struct page *get_node_page_ra(struct page *parent, int start)
1090 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
1091 struct blk_plug plug;
1092 struct page *page;
1093 int err, i, end;
1094 nid_t nid;
1096 /* First, try getting the desired direct node. */
1097 nid = get_nid(parent, start, false);
1098 if (!nid)
1099 return ERR_PTR(-ENOENT);
1100 repeat:
1101 page = grab_cache_page(NODE_MAPPING(sbi), nid);
1102 if (!page)
1103 return ERR_PTR(-ENOMEM);
1105 err = read_node_page(page, READ_SYNC);
1106 if (err < 0) {
1107 f2fs_put_page(page, 1);
1108 return ERR_PTR(err);
1109 } else if (err == LOCKED_PAGE) {
1110 goto page_hit;
1113 blk_start_plug(&plug);
1115 /* Then, try readahead for siblings of the desired node */
1116 end = start + MAX_RA_NODE;
1117 end = min(end, NIDS_PER_BLOCK);
1118 for (i = start + 1; i < end; i++) {
1119 nid = get_nid(parent, i, false);
1120 if (!nid)
1121 continue;
1122 ra_node_page(sbi, nid);
1125 blk_finish_plug(&plug);
1127 lock_page(page);
1128 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1129 f2fs_put_page(page, 1);
1130 goto repeat;
1132 page_hit:
1133 if (unlikely(!PageUptodate(page))) {
1134 f2fs_put_page(page, 1);
1135 return ERR_PTR(-EIO);
1137 return page;
1140 void sync_inode_page(struct dnode_of_data *dn)
1142 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1143 update_inode(dn->inode, dn->node_page);
1144 } else if (dn->inode_page) {
1145 if (!dn->inode_page_locked)
1146 lock_page(dn->inode_page);
1147 update_inode(dn->inode, dn->inode_page);
1148 if (!dn->inode_page_locked)
1149 unlock_page(dn->inode_page);
1150 } else {
1151 update_inode_page(dn->inode);
1155 int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1156 struct writeback_control *wbc)
1158 pgoff_t index, end;
1159 struct pagevec pvec;
1160 int step = ino ? 2 : 0;
1161 int nwritten = 0, wrote = 0;
1163 pagevec_init(&pvec, 0);
1165 next_step:
1166 index = 0;
1167 end = LONG_MAX;
1169 while (index <= end) {
1170 int i, nr_pages;
1171 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1172 PAGECACHE_TAG_DIRTY,
1173 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1174 if (nr_pages == 0)
1175 break;
1177 for (i = 0; i < nr_pages; i++) {
1178 struct page *page = pvec.pages[i];
1181 * flushing sequence with step:
1182 * 0. indirect nodes
1183 * 1. dentry dnodes
1184 * 2. file dnodes
1186 if (step == 0 && IS_DNODE(page))
1187 continue;
1188 if (step == 1 && (!IS_DNODE(page) ||
1189 is_cold_node(page)))
1190 continue;
1191 if (step == 2 && (!IS_DNODE(page) ||
1192 !is_cold_node(page)))
1193 continue;
1196 * If an fsync mode,
1197 * we should not skip writing node pages.
1199 if (ino && ino_of_node(page) == ino)
1200 lock_page(page);
1201 else if (!trylock_page(page))
1202 continue;
1204 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1205 continue_unlock:
1206 unlock_page(page);
1207 continue;
1209 if (ino && ino_of_node(page) != ino)
1210 goto continue_unlock;
1212 if (!PageDirty(page)) {
1213 /* someone wrote it for us */
1214 goto continue_unlock;
1217 if (!clear_page_dirty_for_io(page))
1218 goto continue_unlock;
1220 /* called by fsync() */
1221 if (ino && IS_DNODE(page)) {
1222 set_fsync_mark(page, 1);
1223 if (IS_INODE(page))
1224 set_dentry_mark(page,
1225 need_dentry_mark(sbi, ino));
1226 nwritten++;
1227 } else {
1228 set_fsync_mark(page, 0);
1229 set_dentry_mark(page, 0);
1232 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1233 unlock_page(page);
1234 else
1235 wrote++;
1237 if (--wbc->nr_to_write == 0)
1238 break;
1240 pagevec_release(&pvec);
1241 cond_resched();
1243 if (wbc->nr_to_write == 0) {
1244 step = 2;
1245 break;
1249 if (step < 2) {
1250 step++;
1251 goto next_step;
1254 if (wrote)
1255 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1256 return nwritten;
1259 int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1261 pgoff_t index = 0, end = LONG_MAX;
1262 struct pagevec pvec;
1263 int ret2 = 0, ret = 0;
1265 pagevec_init(&pvec, 0);
1267 while (index <= end) {
1268 int i, nr_pages;
1269 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1270 PAGECACHE_TAG_WRITEBACK,
1271 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1272 if (nr_pages == 0)
1273 break;
1275 for (i = 0; i < nr_pages; i++) {
1276 struct page *page = pvec.pages[i];
1278 /* until radix tree lookup accepts end_index */
1279 if (unlikely(page->index > end))
1280 continue;
1282 if (ino && ino_of_node(page) == ino) {
1283 f2fs_wait_on_page_writeback(page, NODE);
1284 if (TestClearPageError(page))
1285 ret = -EIO;
1288 pagevec_release(&pvec);
1289 cond_resched();
1292 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
1293 ret2 = -ENOSPC;
1294 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
1295 ret2 = -EIO;
1296 if (!ret)
1297 ret = ret2;
1298 return ret;
1301 static int f2fs_write_node_page(struct page *page,
1302 struct writeback_control *wbc)
1304 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1305 nid_t nid;
1306 struct node_info ni;
1307 struct f2fs_io_info fio = {
1308 .sbi = sbi,
1309 .type = NODE,
1310 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
1311 .page = page,
1312 .encrypted_page = NULL,
1315 trace_f2fs_writepage(page, NODE);
1317 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1318 goto redirty_out;
1319 if (unlikely(f2fs_cp_error(sbi)))
1320 goto redirty_out;
1322 f2fs_wait_on_page_writeback(page, NODE);
1324 /* get old block addr of this node page */
1325 nid = nid_of_node(page);
1326 f2fs_bug_on(sbi, page->index != nid);
1328 if (wbc->for_reclaim) {
1329 if (!down_read_trylock(&sbi->node_write))
1330 goto redirty_out;
1331 } else {
1332 down_read(&sbi->node_write);
1335 get_node_info(sbi, nid, &ni);
1337 /* This page is already truncated */
1338 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1339 ClearPageUptodate(page);
1340 dec_page_count(sbi, F2FS_DIRTY_NODES);
1341 up_read(&sbi->node_write);
1342 unlock_page(page);
1343 return 0;
1346 if (__is_valid_data_blkaddr(ni.blk_addr) &&
1347 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC)) {
1348 up_read(&sbi->node_write);
1349 goto redirty_out;
1352 set_page_writeback(page);
1353 fio.blk_addr = ni.blk_addr;
1354 write_node_page(nid, &fio);
1355 set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
1356 dec_page_count(sbi, F2FS_DIRTY_NODES);
1357 up_read(&sbi->node_write);
1358 unlock_page(page);
1360 if (wbc->for_reclaim)
1361 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1363 return 0;
1365 redirty_out:
1366 redirty_page_for_writepage(wbc, page);
1367 return AOP_WRITEPAGE_ACTIVATE;
1370 static int f2fs_write_node_pages(struct address_space *mapping,
1371 struct writeback_control *wbc)
1373 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
1374 long diff;
1376 trace_f2fs_writepages(mapping->host, wbc, NODE);
1378 /* balancing f2fs's metadata in background */
1379 f2fs_balance_fs_bg(sbi);
1381 /* collect a number of dirty node pages and write together */
1382 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
1383 goto skip_write;
1385 diff = nr_pages_to_write(sbi, NODE, wbc);
1386 wbc->sync_mode = WB_SYNC_NONE;
1387 sync_node_pages(sbi, 0, wbc);
1388 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1389 return 0;
1391 skip_write:
1392 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1393 return 0;
1396 static int f2fs_set_node_page_dirty(struct page *page)
1398 trace_f2fs_set_page_dirty(page, NODE);
1400 SetPageUptodate(page);
1401 if (!PageDirty(page)) {
1402 __set_page_dirty_nobuffers(page);
1403 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
1404 SetPagePrivate(page);
1405 f2fs_trace_pid(page);
1406 return 1;
1408 return 0;
1412 * Structure of the f2fs node operations
1414 const struct address_space_operations f2fs_node_aops = {
1415 .writepage = f2fs_write_node_page,
1416 .writepages = f2fs_write_node_pages,
1417 .set_page_dirty = f2fs_set_node_page_dirty,
1418 .invalidatepage = f2fs_invalidate_page,
1419 .releasepage = f2fs_release_page,
1422 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1423 nid_t n)
1425 return radix_tree_lookup(&nm_i->free_nid_root, n);
1428 static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1429 struct free_nid *i)
1431 list_del(&i->list);
1432 radix_tree_delete(&nm_i->free_nid_root, i->nid);
1435 static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
1437 struct f2fs_nm_info *nm_i = NM_I(sbi);
1438 struct free_nid *i, *e;
1439 struct nat_entry *ne;
1440 int err = -EINVAL;
1442 if (!available_free_memory(sbi, FREE_NIDS))
1443 return -1;
1445 /* 0 nid should not be used */
1446 if (unlikely(nid == 0))
1447 return 0;
1449 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
1450 i->nid = nid;
1451 i->state = NID_NEW;
1453 if (radix_tree_preload(GFP_NOFS))
1454 goto err;
1456 spin_lock(&nm_i->free_nid_list_lock);
1458 if (build) {
1460 * Thread A Thread B
1461 * - f2fs_create
1462 * - f2fs_new_inode
1463 * - alloc_nid
1464 * - __insert_nid_to_list(ALLOC_NID_LIST)
1465 * - f2fs_balance_fs_bg
1466 * - build_free_nids
1467 * - __build_free_nids
1468 * - scan_nat_page
1469 * - add_free_nid
1470 * - __lookup_nat_cache
1471 * - f2fs_add_link
1472 * - init_inode_metadata
1473 * - new_inode_page
1474 * - new_node_page
1475 * - set_node_addr
1476 * - alloc_nid_done
1477 * - __remove_nid_from_list(ALLOC_NID_LIST)
1478 * - __insert_nid_to_list(FREE_NID_LIST)
1480 ne = __lookup_nat_cache(nm_i, nid);
1481 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1482 nat_get_blkaddr(ne) != NULL_ADDR))
1483 goto err_out;
1485 e = __lookup_free_nid_list(nm_i, nid);
1486 if (e)
1487 goto err_out;
1489 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i))
1490 goto err_out;
1491 err = 0;
1492 list_add_tail(&i->list, &nm_i->free_nid_list);
1493 nm_i->fcnt++;
1494 err_out:
1495 spin_unlock(&nm_i->free_nid_list_lock);
1496 radix_tree_preload_end();
1497 err:
1498 if (err)
1499 kmem_cache_free(free_nid_slab, i);
1500 return !err;
1503 static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1505 struct free_nid *i;
1506 bool need_free = false;
1508 spin_lock(&nm_i->free_nid_list_lock);
1509 i = __lookup_free_nid_list(nm_i, nid);
1510 if (i && i->state == NID_NEW) {
1511 __del_from_free_nid_list(nm_i, i);
1512 nm_i->fcnt--;
1513 need_free = true;
1515 spin_unlock(&nm_i->free_nid_list_lock);
1517 if (need_free)
1518 kmem_cache_free(free_nid_slab, i);
1521 static void scan_nat_page(struct f2fs_sb_info *sbi,
1522 struct page *nat_page, nid_t start_nid)
1524 struct f2fs_nm_info *nm_i = NM_I(sbi);
1525 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1526 block_t blk_addr;
1527 int i;
1529 i = start_nid % NAT_ENTRY_PER_BLOCK;
1531 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
1533 if (unlikely(start_nid >= nm_i->max_nid))
1534 break;
1536 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
1537 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
1538 if (blk_addr == NULL_ADDR) {
1539 if (add_free_nid(sbi, start_nid, true) < 0)
1540 break;
1545 static void build_free_nids(struct f2fs_sb_info *sbi)
1547 struct f2fs_nm_info *nm_i = NM_I(sbi);
1548 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1549 struct f2fs_summary_block *sum = curseg->sum_blk;
1550 int i = 0;
1551 nid_t nid = nm_i->next_scan_nid;
1553 /* Enough entries */
1554 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1555 return;
1557 /* readahead nat pages to be scanned */
1558 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
1559 META_NAT, true);
1561 down_read(&nm_i->nat_tree_lock);
1563 while (1) {
1564 struct page *page = get_current_nat_page(sbi, nid);
1566 scan_nat_page(sbi, page, nid);
1567 f2fs_put_page(page, 1);
1569 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1570 if (unlikely(nid >= nm_i->max_nid))
1571 nid = 0;
1573 if (++i >= FREE_NID_PAGES)
1574 break;
1577 /* go to the next free nat pages to find free nids abundantly */
1578 nm_i->next_scan_nid = nid;
1580 /* find free nids from current sum_pages */
1581 mutex_lock(&curseg->curseg_mutex);
1582 for (i = 0; i < nats_in_cursum(sum); i++) {
1583 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1584 nid = le32_to_cpu(nid_in_journal(sum, i));
1585 if (addr == NULL_ADDR)
1586 add_free_nid(sbi, nid, true);
1587 else
1588 remove_free_nid(nm_i, nid);
1590 mutex_unlock(&curseg->curseg_mutex);
1591 up_read(&nm_i->nat_tree_lock);
1593 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
1594 nm_i->ra_nid_pages, META_NAT, false);
1598 * If this function returns success, caller can obtain a new nid
1599 * from second parameter of this function.
1600 * The returned nid could be used ino as well as nid when inode is created.
1602 bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1604 struct f2fs_nm_info *nm_i = NM_I(sbi);
1605 struct free_nid *i = NULL;
1606 retry:
1607 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
1608 return false;
1610 spin_lock(&nm_i->free_nid_list_lock);
1612 /* We should not use stale free nids created by build_free_nids */
1613 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
1614 struct node_info ni;
1616 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
1617 list_for_each_entry(i, &nm_i->free_nid_list, list)
1618 if (i->state == NID_NEW)
1619 break;
1621 f2fs_bug_on(sbi, i->state != NID_NEW);
1622 *nid = i->nid;
1623 i->state = NID_ALLOC;
1624 nm_i->fcnt--;
1625 spin_unlock(&nm_i->free_nid_list_lock);
1627 /* check nid is allocated already */
1628 get_node_info(sbi, *nid, &ni);
1629 if (ni.blk_addr != NULL_ADDR) {
1630 alloc_nid_done(sbi, *nid);
1631 goto retry;
1633 return true;
1635 spin_unlock(&nm_i->free_nid_list_lock);
1637 /* Let's scan nat pages and its caches to get free nids */
1638 mutex_lock(&nm_i->build_lock);
1639 build_free_nids(sbi);
1640 mutex_unlock(&nm_i->build_lock);
1641 goto retry;
1645 * alloc_nid() should be called prior to this function.
1647 void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1649 struct f2fs_nm_info *nm_i = NM_I(sbi);
1650 struct free_nid *i;
1652 spin_lock(&nm_i->free_nid_list_lock);
1653 i = __lookup_free_nid_list(nm_i, nid);
1654 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
1655 __del_from_free_nid_list(nm_i, i);
1656 spin_unlock(&nm_i->free_nid_list_lock);
1658 kmem_cache_free(free_nid_slab, i);
1662 * alloc_nid() should be called prior to this function.
1664 void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1666 struct f2fs_nm_info *nm_i = NM_I(sbi);
1667 struct free_nid *i;
1668 bool need_free = false;
1670 if (!nid)
1671 return;
1673 spin_lock(&nm_i->free_nid_list_lock);
1674 i = __lookup_free_nid_list(nm_i, nid);
1675 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
1676 if (!available_free_memory(sbi, FREE_NIDS)) {
1677 __del_from_free_nid_list(nm_i, i);
1678 need_free = true;
1679 } else {
1680 i->state = NID_NEW;
1681 nm_i->fcnt++;
1683 spin_unlock(&nm_i->free_nid_list_lock);
1685 if (need_free)
1686 kmem_cache_free(free_nid_slab, i);
1689 int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
1691 struct f2fs_nm_info *nm_i = NM_I(sbi);
1692 struct free_nid *i, *next;
1693 int nr = nr_shrink;
1695 if (!mutex_trylock(&nm_i->build_lock))
1696 return 0;
1698 spin_lock(&nm_i->free_nid_list_lock);
1699 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
1700 if (nr_shrink <= 0 || nm_i->fcnt <= NAT_ENTRY_PER_BLOCK)
1701 break;
1702 if (i->state == NID_ALLOC)
1703 continue;
1704 __del_from_free_nid_list(nm_i, i);
1705 kmem_cache_free(free_nid_slab, i);
1706 nm_i->fcnt--;
1707 nr_shrink--;
1709 spin_unlock(&nm_i->free_nid_list_lock);
1710 mutex_unlock(&nm_i->build_lock);
1712 return nr - nr_shrink;
1715 void recover_inline_xattr(struct inode *inode, struct page *page)
1717 void *src_addr, *dst_addr;
1718 size_t inline_size;
1719 struct page *ipage;
1720 struct f2fs_inode *ri;
1722 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
1723 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
1725 ri = F2FS_INODE(page);
1726 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1727 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1728 goto update_inode;
1731 dst_addr = inline_xattr_addr(ipage);
1732 src_addr = inline_xattr_addr(page);
1733 inline_size = inline_xattr_size(inode);
1735 f2fs_wait_on_page_writeback(ipage, NODE);
1736 memcpy(dst_addr, src_addr, inline_size);
1737 update_inode:
1738 update_inode(inode, ipage);
1739 f2fs_put_page(ipage, 1);
1742 void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
1744 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1745 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1746 nid_t new_xnid = nid_of_node(page);
1747 struct node_info ni;
1749 /* 1: invalidate the previous xattr nid */
1750 if (!prev_xnid)
1751 goto recover_xnid;
1753 /* Deallocate node address */
1754 get_node_info(sbi, prev_xnid, &ni);
1755 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
1756 invalidate_blocks(sbi, ni.blk_addr);
1757 dec_valid_node_count(sbi, inode);
1758 set_node_addr(sbi, &ni, NULL_ADDR, false);
1760 recover_xnid:
1761 /* 2: allocate new xattr nid */
1762 if (unlikely(!inc_valid_node_count(sbi, inode)))
1763 f2fs_bug_on(sbi, 1);
1765 remove_free_nid(NM_I(sbi), new_xnid);
1766 get_node_info(sbi, new_xnid, &ni);
1767 ni.ino = inode->i_ino;
1768 set_node_addr(sbi, &ni, NEW_ADDR, false);
1769 F2FS_I(inode)->i_xattr_nid = new_xnid;
1771 /* 3: update xattr blkaddr */
1772 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
1773 set_node_addr(sbi, &ni, blkaddr, false);
1775 update_inode_page(inode);
1778 int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1780 struct f2fs_inode *src, *dst;
1781 nid_t ino = ino_of_node(page);
1782 struct node_info old_ni, new_ni;
1783 struct page *ipage;
1785 get_node_info(sbi, ino, &old_ni);
1787 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1788 return -EINVAL;
1790 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
1791 if (!ipage)
1792 return -ENOMEM;
1794 /* Should not use this inode from free nid list */
1795 remove_free_nid(NM_I(sbi), ino);
1797 SetPageUptodate(ipage);
1798 fill_node_footer(ipage, ino, ino, 0, true);
1800 src = F2FS_INODE(page);
1801 dst = F2FS_INODE(ipage);
1803 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1804 dst->i_size = 0;
1805 dst->i_blocks = cpu_to_le64(1);
1806 dst->i_links = cpu_to_le32(1);
1807 dst->i_xattr_nid = 0;
1808 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
1810 new_ni = old_ni;
1811 new_ni.ino = ino;
1813 if (unlikely(!inc_valid_node_count(sbi, NULL)))
1814 WARN_ON(1);
1815 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
1816 inc_valid_inode_count(sbi);
1817 set_page_dirty(ipage);
1818 f2fs_put_page(ipage, 1);
1819 return 0;
1822 int restore_node_summary(struct f2fs_sb_info *sbi,
1823 unsigned int segno, struct f2fs_summary_block *sum)
1825 struct f2fs_node *rn;
1826 struct f2fs_summary *sum_entry;
1827 block_t addr;
1828 int bio_blocks = MAX_BIO_BLOCKS(sbi);
1829 int i, idx, last_offset, nrpages;
1831 /* scan the node segment */
1832 last_offset = sbi->blocks_per_seg;
1833 addr = START_BLOCK(sbi, segno);
1834 sum_entry = &sum->entries[0];
1836 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
1837 nrpages = min(last_offset - i, bio_blocks);
1839 /* readahead node pages */
1840 ra_meta_pages(sbi, addr, nrpages, META_POR, true);
1842 for (idx = addr; idx < addr + nrpages; idx++) {
1843 struct page *page = get_tmp_page(sbi, idx);
1845 rn = F2FS_NODE(page);
1846 sum_entry->nid = rn->footer.nid;
1847 sum_entry->version = 0;
1848 sum_entry->ofs_in_node = 0;
1849 sum_entry++;
1850 f2fs_put_page(page, 1);
1853 invalidate_mapping_pages(META_MAPPING(sbi), addr,
1854 addr + nrpages);
1856 return 0;
1859 static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
1861 struct f2fs_nm_info *nm_i = NM_I(sbi);
1862 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1863 struct f2fs_summary_block *sum = curseg->sum_blk;
1864 int i;
1866 mutex_lock(&curseg->curseg_mutex);
1867 for (i = 0; i < nats_in_cursum(sum); i++) {
1868 struct nat_entry *ne;
1869 struct f2fs_nat_entry raw_ne;
1870 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1872 raw_ne = nat_in_journal(sum, i);
1874 ne = __lookup_nat_cache(nm_i, nid);
1875 if (!ne) {
1876 ne = grab_nat_entry(nm_i, nid);
1877 node_info_from_raw_nat(&ne->ni, &raw_ne);
1879 __set_nat_cache_dirty(nm_i, ne);
1881 update_nats_in_cursum(sum, -i);
1882 mutex_unlock(&curseg->curseg_mutex);
1885 static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1886 struct list_head *head, int max)
1888 struct nat_entry_set *cur;
1890 if (nes->entry_cnt >= max)
1891 goto add_out;
1893 list_for_each_entry(cur, head, set_list) {
1894 if (cur->entry_cnt >= nes->entry_cnt) {
1895 list_add(&nes->set_list, cur->set_list.prev);
1896 return;
1899 add_out:
1900 list_add_tail(&nes->set_list, head);
1903 static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1904 struct nat_entry_set *set)
1906 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1907 struct f2fs_summary_block *sum = curseg->sum_blk;
1908 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1909 bool to_journal = true;
1910 struct f2fs_nat_block *nat_blk;
1911 struct nat_entry *ne, *cur;
1912 struct page *page = NULL;
1915 * there are two steps to flush nat entries:
1916 * #1, flush nat entries to journal in current hot data summary block.
1917 * #2, flush nat entries to nat page.
1919 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1920 to_journal = false;
1922 if (to_journal) {
1923 mutex_lock(&curseg->curseg_mutex);
1924 } else {
1925 page = get_next_nat_page(sbi, start_nid);
1926 nat_blk = page_address(page);
1927 f2fs_bug_on(sbi, !nat_blk);
1930 /* flush dirty nats in nat entry set */
1931 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1932 struct f2fs_nat_entry *raw_ne;
1933 nid_t nid = nat_get_nid(ne);
1934 int offset;
1936 if (nat_get_blkaddr(ne) == NEW_ADDR)
1937 continue;
1939 if (to_journal) {
1940 offset = lookup_journal_in_cursum(sum,
1941 NAT_JOURNAL, nid, 1);
1942 f2fs_bug_on(sbi, offset < 0);
1943 raw_ne = &nat_in_journal(sum, offset);
1944 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1945 } else {
1946 raw_ne = &nat_blk->entries[nid - start_nid];
1948 raw_nat_from_node_info(raw_ne, &ne->ni);
1949 nat_reset_flag(ne);
1950 __clear_nat_cache_dirty(NM_I(sbi), ne);
1951 if (nat_get_blkaddr(ne) == NULL_ADDR)
1952 add_free_nid(sbi, nid, false);
1955 if (to_journal)
1956 mutex_unlock(&curseg->curseg_mutex);
1957 else
1958 f2fs_put_page(page, 1);
1960 f2fs_bug_on(sbi, set->entry_cnt);
1962 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
1963 kmem_cache_free(nat_entry_set_slab, set);
1967 * This function is called during the checkpointing process.
1969 void flush_nat_entries(struct f2fs_sb_info *sbi)
1971 struct f2fs_nm_info *nm_i = NM_I(sbi);
1972 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1973 struct f2fs_summary_block *sum = curseg->sum_blk;
1974 struct nat_entry_set *setvec[SETVEC_SIZE];
1975 struct nat_entry_set *set, *tmp;
1976 unsigned int found;
1977 nid_t set_idx = 0;
1978 LIST_HEAD(sets);
1980 if (!nm_i->dirty_nat_cnt)
1981 return;
1983 down_write(&nm_i->nat_tree_lock);
1986 * if there are no enough space in journal to store dirty nat
1987 * entries, remove all entries from journal and merge them
1988 * into nat entry set.
1990 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
1991 remove_nats_in_journal(sbi);
1993 while ((found = __gang_lookup_nat_set(nm_i,
1994 set_idx, SETVEC_SIZE, setvec))) {
1995 unsigned idx;
1996 set_idx = setvec[found - 1]->set + 1;
1997 for (idx = 0; idx < found; idx++)
1998 __adjust_nat_entry_set(setvec[idx], &sets,
1999 MAX_NAT_JENTRIES(sum));
2002 /* flush dirty nats in nat entry set */
2003 list_for_each_entry_safe(set, tmp, &sets, set_list)
2004 __flush_nat_entry_set(sbi, set);
2006 up_write(&nm_i->nat_tree_lock);
2008 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
2011 static int init_node_manager(struct f2fs_sb_info *sbi)
2013 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
2014 struct f2fs_nm_info *nm_i = NM_I(sbi);
2015 unsigned char *version_bitmap;
2016 unsigned int nat_segs, nat_blocks;
2018 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
2020 /* segment_count_nat includes pair segment so divide to 2. */
2021 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
2022 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
2024 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
2026 /* not used nids: 0, node, meta, (and root counted as valid node) */
2027 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
2028 nm_i->fcnt = 0;
2029 nm_i->nat_cnt = 0;
2030 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
2031 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
2033 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
2034 INIT_LIST_HEAD(&nm_i->free_nid_list);
2035 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2036 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
2037 INIT_LIST_HEAD(&nm_i->nat_entries);
2039 mutex_init(&nm_i->build_lock);
2040 spin_lock_init(&nm_i->free_nid_list_lock);
2041 init_rwsem(&nm_i->nat_tree_lock);
2043 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
2044 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
2045 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2046 if (!version_bitmap)
2047 return -EFAULT;
2049 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2050 GFP_KERNEL);
2051 if (!nm_i->nat_bitmap)
2052 return -ENOMEM;
2053 return 0;
2056 int build_node_manager(struct f2fs_sb_info *sbi)
2058 int err;
2060 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
2061 if (!sbi->nm_info)
2062 return -ENOMEM;
2064 err = init_node_manager(sbi);
2065 if (err)
2066 return err;
2068 build_free_nids(sbi);
2069 return 0;
2072 void destroy_node_manager(struct f2fs_sb_info *sbi)
2074 struct f2fs_nm_info *nm_i = NM_I(sbi);
2075 struct free_nid *i, *next_i;
2076 struct nat_entry *natvec[NATVEC_SIZE];
2077 struct nat_entry_set *setvec[SETVEC_SIZE];
2078 nid_t nid = 0;
2079 unsigned int found;
2081 if (!nm_i)
2082 return;
2084 /* destroy free nid list */
2085 spin_lock(&nm_i->free_nid_list_lock);
2086 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
2087 f2fs_bug_on(sbi, i->state == NID_ALLOC);
2088 __del_from_free_nid_list(nm_i, i);
2089 nm_i->fcnt--;
2090 spin_unlock(&nm_i->free_nid_list_lock);
2091 kmem_cache_free(free_nid_slab, i);
2092 spin_lock(&nm_i->free_nid_list_lock);
2094 f2fs_bug_on(sbi, nm_i->fcnt);
2095 spin_unlock(&nm_i->free_nid_list_lock);
2097 /* destroy nat cache */
2098 down_write(&nm_i->nat_tree_lock);
2099 while ((found = __gang_lookup_nat_cache(nm_i,
2100 nid, NATVEC_SIZE, natvec))) {
2101 unsigned idx;
2103 nid = nat_get_nid(natvec[found - 1]) + 1;
2104 for (idx = 0; idx < found; idx++)
2105 __del_from_nat_cache(nm_i, natvec[idx]);
2107 f2fs_bug_on(sbi, nm_i->nat_cnt);
2109 /* destroy nat set cache */
2110 nid = 0;
2111 while ((found = __gang_lookup_nat_set(nm_i,
2112 nid, SETVEC_SIZE, setvec))) {
2113 unsigned idx;
2115 nid = setvec[found - 1]->set + 1;
2116 for (idx = 0; idx < found; idx++) {
2117 /* entry_cnt is not zero, when cp_error was occurred */
2118 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2119 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2120 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2123 up_write(&nm_i->nat_tree_lock);
2125 kfree(nm_i->nat_bitmap);
2126 sbi->nm_info = NULL;
2127 kfree(nm_i);
2130 int __init create_node_manager_caches(void)
2132 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
2133 sizeof(struct nat_entry));
2134 if (!nat_entry_slab)
2135 goto fail;
2137 free_nid_slab = f2fs_kmem_cache_create("free_nid",
2138 sizeof(struct free_nid));
2139 if (!free_nid_slab)
2140 goto destroy_nat_entry;
2142 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2143 sizeof(struct nat_entry_set));
2144 if (!nat_entry_set_slab)
2145 goto destroy_free_nid;
2146 return 0;
2148 destroy_free_nid:
2149 kmem_cache_destroy(free_nid_slab);
2150 destroy_nat_entry:
2151 kmem_cache_destroy(nat_entry_slab);
2152 fail:
2153 return -ENOMEM;
2156 void destroy_node_manager_caches(void)
2158 kmem_cache_destroy(nat_entry_set_slab);
2159 kmem_cache_destroy(free_nid_slab);
2160 kmem_cache_destroy(nat_entry_slab);