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