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