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