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