1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/node.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/mpage.h>
11 #include <linux/sched/mm.h>
12 #include <linux/blkdev.h>
13 #include <linux/pagevec.h>
14 #include <linux/swap.h>
15
16 #include "f2fs.h"
17 #include "node.h"
18 #include "segment.h"
19 #include "xattr.h"
20 #include "iostat.h"
21 #include <trace/events/f2fs.h>
22
23 #define on_f2fs_build_free_nids(nm_i) mutex_is_locked(&(nm_i)->build_lock)
24
25 static struct kmem_cache *nat_entry_slab;
26 static struct kmem_cache *free_nid_slab;
27 static struct kmem_cache *nat_entry_set_slab;
28 static struct kmem_cache *fsync_node_entry_slab;
29
30 /*
31 * Check whether the given nid is within node id range.
32 */
f2fs_check_nid_range(struct f2fs_sb_info * sbi,nid_t nid)33 int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
34 {
35 if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
36 set_sbi_flag(sbi, SBI_NEED_FSCK);
37 f2fs_warn(sbi, "%s: out-of-range nid=%x, run fsck to fix.",
38 __func__, nid);
39 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
40 return -EFSCORRUPTED;
41 }
42 return 0;
43 }
44
f2fs_available_free_memory(struct f2fs_sb_info * sbi,int type)45 bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
46 {
47 struct f2fs_nm_info *nm_i = NM_I(sbi);
48 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
49 struct sysinfo val;
50 unsigned long avail_ram;
51 unsigned long mem_size = 0;
52 bool res = false;
53
54 if (!nm_i)
55 return true;
56
57 si_meminfo(&val);
58
59 /* only uses low memory */
60 avail_ram = val.totalram - val.totalhigh;
61
62 /*
63 * give 25%, 25%, 50%, 50%, 25%, 25% memory for each components respectively
64 */
65 if (type == FREE_NIDS) {
66 mem_size = (nm_i->nid_cnt[FREE_NID] *
67 sizeof(struct free_nid)) >> PAGE_SHIFT;
68 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
69 } else if (type == NAT_ENTRIES) {
70 mem_size = (nm_i->nat_cnt[TOTAL_NAT] *
71 sizeof(struct nat_entry)) >> PAGE_SHIFT;
72 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
73 if (excess_cached_nats(sbi))
74 res = false;
75 } else if (type == DIRTY_DENTS) {
76 if (sbi->sb->s_bdi->wb.dirty_exceeded)
77 return false;
78 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
79 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
80 } else if (type == INO_ENTRIES) {
81 int i;
82
83 for (i = 0; i < MAX_INO_ENTRY; i++)
84 mem_size += sbi->im[i].ino_num *
85 sizeof(struct ino_entry);
86 mem_size >>= PAGE_SHIFT;
87 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
88 } else if (type == READ_EXTENT_CACHE || type == AGE_EXTENT_CACHE) {
89 enum extent_type etype = type == READ_EXTENT_CACHE ?
90 EX_READ : EX_BLOCK_AGE;
91 struct extent_tree_info *eti = &sbi->extent_tree[etype];
92
93 mem_size = (atomic_read(&eti->total_ext_tree) *
94 sizeof(struct extent_tree) +
95 atomic_read(&eti->total_ext_node) *
96 sizeof(struct extent_node)) >> PAGE_SHIFT;
97 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
98 } else if (type == DISCARD_CACHE) {
99 mem_size = (atomic_read(&dcc->discard_cmd_cnt) *
100 sizeof(struct discard_cmd)) >> PAGE_SHIFT;
101 res = mem_size < (avail_ram * nm_i->ram_thresh / 100);
102 } else if (type == COMPRESS_PAGE) {
103 #ifdef CONFIG_F2FS_FS_COMPRESSION
104 unsigned long free_ram = val.freeram;
105
106 /*
107 * free memory is lower than watermark or cached page count
108 * exceed threshold, deny caching compress page.
109 */
110 res = (free_ram > avail_ram * sbi->compress_watermark / 100) &&
111 (COMPRESS_MAPPING(sbi)->nrpages <
112 free_ram * sbi->compress_percent / 100);
113 #else
114 res = false;
115 #endif
116 } else {
117 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
118 return true;
119 }
120 return res;
121 }
122
clear_node_page_dirty(struct page * page)123 static void clear_node_page_dirty(struct page *page)
124 {
125 if (PageDirty(page)) {
126 f2fs_clear_page_cache_dirty_tag(page_folio(page));
127 clear_page_dirty_for_io(page);
128 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
129 }
130 ClearPageUptodate(page);
131 }
132
get_current_nat_page(struct f2fs_sb_info * sbi,nid_t nid)133 static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
134 {
135 return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid));
136 }
137
get_next_nat_page(struct f2fs_sb_info * sbi,nid_t nid)138 static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
139 {
140 struct page *src_page;
141 struct page *dst_page;
142 pgoff_t dst_off;
143 void *src_addr;
144 void *dst_addr;
145 struct f2fs_nm_info *nm_i = NM_I(sbi);
146
147 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
148
149 /* get current nat block page with lock */
150 src_page = get_current_nat_page(sbi, nid);
151 if (IS_ERR(src_page))
152 return src_page;
153 dst_page = f2fs_grab_meta_page(sbi, dst_off);
154 f2fs_bug_on(sbi, PageDirty(src_page));
155
156 src_addr = page_address(src_page);
157 dst_addr = page_address(dst_page);
158 memcpy(dst_addr, src_addr, PAGE_SIZE);
159 set_page_dirty(dst_page);
160 f2fs_put_page(src_page, 1);
161
162 set_to_next_nat(nm_i, nid);
163
164 return dst_page;
165 }
166
__alloc_nat_entry(struct f2fs_sb_info * sbi,nid_t nid,bool no_fail)167 static struct nat_entry *__alloc_nat_entry(struct f2fs_sb_info *sbi,
168 nid_t nid, bool no_fail)
169 {
170 struct nat_entry *new;
171
172 new = f2fs_kmem_cache_alloc(nat_entry_slab,
173 GFP_F2FS_ZERO, no_fail, sbi);
174 if (new) {
175 nat_set_nid(new, nid);
176 nat_reset_flag(new);
177 }
178 return new;
179 }
180
__free_nat_entry(struct nat_entry * e)181 static void __free_nat_entry(struct nat_entry *e)
182 {
183 kmem_cache_free(nat_entry_slab, e);
184 }
185
186 /* must be locked by nat_tree_lock */
__init_nat_entry(struct f2fs_nm_info * nm_i,struct nat_entry * ne,struct f2fs_nat_entry * raw_ne,bool no_fail)187 static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
188 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
189 {
190 if (no_fail)
191 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
192 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
193 return NULL;
194
195 if (raw_ne)
196 node_info_from_raw_nat(&ne->ni, raw_ne);
197
198 spin_lock(&nm_i->nat_list_lock);
199 list_add_tail(&ne->list, &nm_i->nat_entries);
200 spin_unlock(&nm_i->nat_list_lock);
201
202 nm_i->nat_cnt[TOTAL_NAT]++;
203 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
204 return ne;
205 }
206
__lookup_nat_cache(struct f2fs_nm_info * nm_i,nid_t n)207 static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
208 {
209 struct nat_entry *ne;
210
211 ne = radix_tree_lookup(&nm_i->nat_root, n);
212
213 /* for recent accessed nat entry, move it to tail of lru list */
214 if (ne && !get_nat_flag(ne, IS_DIRTY)) {
215 spin_lock(&nm_i->nat_list_lock);
216 if (!list_empty(&ne->list))
217 list_move_tail(&ne->list, &nm_i->nat_entries);
218 spin_unlock(&nm_i->nat_list_lock);
219 }
220
221 return ne;
222 }
223
__gang_lookup_nat_cache(struct f2fs_nm_info * nm_i,nid_t start,unsigned int nr,struct nat_entry ** ep)224 static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
225 nid_t start, unsigned int nr, struct nat_entry **ep)
226 {
227 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
228 }
229
__del_from_nat_cache(struct f2fs_nm_info * nm_i,struct nat_entry * e)230 static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
231 {
232 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
233 nm_i->nat_cnt[TOTAL_NAT]--;
234 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
235 __free_nat_entry(e);
236 }
237
__grab_nat_entry_set(struct f2fs_nm_info * nm_i,struct nat_entry * ne)238 static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
239 struct nat_entry *ne)
240 {
241 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
242 struct nat_entry_set *head;
243
244 head = radix_tree_lookup(&nm_i->nat_set_root, set);
245 if (!head) {
246 head = f2fs_kmem_cache_alloc(nat_entry_set_slab,
247 GFP_NOFS, true, NULL);
248
249 INIT_LIST_HEAD(&head->entry_list);
250 INIT_LIST_HEAD(&head->set_list);
251 head->set = set;
252 head->entry_cnt = 0;
253 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
254 }
255 return head;
256 }
257
__set_nat_cache_dirty(struct f2fs_nm_info * nm_i,struct nat_entry * ne)258 static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
259 struct nat_entry *ne)
260 {
261 struct nat_entry_set *head;
262 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
263
264 if (!new_ne)
265 head = __grab_nat_entry_set(nm_i, ne);
266
267 /*
268 * update entry_cnt in below condition:
269 * 1. update NEW_ADDR to valid block address;
270 * 2. update old block address to new one;
271 */
272 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
273 !get_nat_flag(ne, IS_DIRTY)))
274 head->entry_cnt++;
275
276 set_nat_flag(ne, IS_PREALLOC, new_ne);
277
278 if (get_nat_flag(ne, IS_DIRTY))
279 goto refresh_list;
280
281 nm_i->nat_cnt[DIRTY_NAT]++;
282 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
283 set_nat_flag(ne, IS_DIRTY, true);
284 refresh_list:
285 spin_lock(&nm_i->nat_list_lock);
286 if (new_ne)
287 list_del_init(&ne->list);
288 else
289 list_move_tail(&ne->list, &head->entry_list);
290 spin_unlock(&nm_i->nat_list_lock);
291 }
292
__clear_nat_cache_dirty(struct f2fs_nm_info * nm_i,struct nat_entry_set * set,struct nat_entry * ne)293 static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
294 struct nat_entry_set *set, struct nat_entry *ne)
295 {
296 spin_lock(&nm_i->nat_list_lock);
297 list_move_tail(&ne->list, &nm_i->nat_entries);
298 spin_unlock(&nm_i->nat_list_lock);
299
300 set_nat_flag(ne, IS_DIRTY, false);
301 set->entry_cnt--;
302 nm_i->nat_cnt[DIRTY_NAT]--;
303 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
304 }
305
__gang_lookup_nat_set(struct f2fs_nm_info * nm_i,nid_t start,unsigned int nr,struct nat_entry_set ** ep)306 static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
307 nid_t start, unsigned int nr, struct nat_entry_set **ep)
308 {
309 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
310 start, nr);
311 }
312
f2fs_in_warm_node_list(struct f2fs_sb_info * sbi,const struct folio * folio)313 bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, const struct folio *folio)
314 {
315 return NODE_MAPPING(sbi) == folio->mapping &&
316 IS_DNODE(&folio->page) && is_cold_node(&folio->page);
317 }
318
f2fs_init_fsync_node_info(struct f2fs_sb_info * sbi)319 void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
320 {
321 spin_lock_init(&sbi->fsync_node_lock);
322 INIT_LIST_HEAD(&sbi->fsync_node_list);
323 sbi->fsync_seg_id = 0;
324 sbi->fsync_node_num = 0;
325 }
326
f2fs_add_fsync_node_entry(struct f2fs_sb_info * sbi,struct page * page)327 static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
328 struct page *page)
329 {
330 struct fsync_node_entry *fn;
331 unsigned long flags;
332 unsigned int seq_id;
333
334 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab,
335 GFP_NOFS, true, NULL);
336
337 get_page(page);
338 fn->page = page;
339 INIT_LIST_HEAD(&fn->list);
340
341 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
342 list_add_tail(&fn->list, &sbi->fsync_node_list);
343 fn->seq_id = sbi->fsync_seg_id++;
344 seq_id = fn->seq_id;
345 sbi->fsync_node_num++;
346 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
347
348 return seq_id;
349 }
350
f2fs_del_fsync_node_entry(struct f2fs_sb_info * sbi,struct page * page)351 void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
352 {
353 struct fsync_node_entry *fn;
354 unsigned long flags;
355
356 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
357 list_for_each_entry(fn, &sbi->fsync_node_list, list) {
358 if (fn->page == page) {
359 list_del(&fn->list);
360 sbi->fsync_node_num--;
361 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
362 kmem_cache_free(fsync_node_entry_slab, fn);
363 put_page(page);
364 return;
365 }
366 }
367 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
368 f2fs_bug_on(sbi, 1);
369 }
370
f2fs_reset_fsync_node_info(struct f2fs_sb_info * sbi)371 void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
372 {
373 unsigned long flags;
374
375 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
376 sbi->fsync_seg_id = 0;
377 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
378 }
379
f2fs_need_dentry_mark(struct f2fs_sb_info * sbi,nid_t nid)380 int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
381 {
382 struct f2fs_nm_info *nm_i = NM_I(sbi);
383 struct nat_entry *e;
384 bool need = false;
385
386 f2fs_down_read(&nm_i->nat_tree_lock);
387 e = __lookup_nat_cache(nm_i, nid);
388 if (e) {
389 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
390 !get_nat_flag(e, HAS_FSYNCED_INODE))
391 need = true;
392 }
393 f2fs_up_read(&nm_i->nat_tree_lock);
394 return need;
395 }
396
f2fs_is_checkpointed_node(struct f2fs_sb_info * sbi,nid_t nid)397 bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
398 {
399 struct f2fs_nm_info *nm_i = NM_I(sbi);
400 struct nat_entry *e;
401 bool is_cp = true;
402
403 f2fs_down_read(&nm_i->nat_tree_lock);
404 e = __lookup_nat_cache(nm_i, nid);
405 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
406 is_cp = false;
407 f2fs_up_read(&nm_i->nat_tree_lock);
408 return is_cp;
409 }
410
f2fs_need_inode_block_update(struct f2fs_sb_info * sbi,nid_t ino)411 bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
412 {
413 struct f2fs_nm_info *nm_i = NM_I(sbi);
414 struct nat_entry *e;
415 bool need_update = true;
416
417 f2fs_down_read(&nm_i->nat_tree_lock);
418 e = __lookup_nat_cache(nm_i, ino);
419 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
420 (get_nat_flag(e, IS_CHECKPOINTED) ||
421 get_nat_flag(e, HAS_FSYNCED_INODE)))
422 need_update = false;
423 f2fs_up_read(&nm_i->nat_tree_lock);
424 return need_update;
425 }
426
427 /* must be locked by nat_tree_lock */
cache_nat_entry(struct f2fs_sb_info * sbi,nid_t nid,struct f2fs_nat_entry * ne)428 static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
429 struct f2fs_nat_entry *ne)
430 {
431 struct f2fs_nm_info *nm_i = NM_I(sbi);
432 struct nat_entry *new, *e;
433
434 /* Let's mitigate lock contention of nat_tree_lock during checkpoint */
435 if (f2fs_rwsem_is_locked(&sbi->cp_global_sem))
436 return;
437
438 new = __alloc_nat_entry(sbi, nid, false);
439 if (!new)
440 return;
441
442 f2fs_down_write(&nm_i->nat_tree_lock);
443 e = __lookup_nat_cache(nm_i, nid);
444 if (!e)
445 e = __init_nat_entry(nm_i, new, ne, false);
446 else
447 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
448 nat_get_blkaddr(e) !=
449 le32_to_cpu(ne->block_addr) ||
450 nat_get_version(e) != ne->version);
451 f2fs_up_write(&nm_i->nat_tree_lock);
452 if (e != new)
453 __free_nat_entry(new);
454 }
455
set_node_addr(struct f2fs_sb_info * sbi,struct node_info * ni,block_t new_blkaddr,bool fsync_done)456 static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
457 block_t new_blkaddr, bool fsync_done)
458 {
459 struct f2fs_nm_info *nm_i = NM_I(sbi);
460 struct nat_entry *e;
461 struct nat_entry *new = __alloc_nat_entry(sbi, ni->nid, true);
462
463 f2fs_down_write(&nm_i->nat_tree_lock);
464 e = __lookup_nat_cache(nm_i, ni->nid);
465 if (!e) {
466 e = __init_nat_entry(nm_i, new, NULL, true);
467 copy_node_info(&e->ni, ni);
468 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
469 } else if (new_blkaddr == NEW_ADDR) {
470 /*
471 * when nid is reallocated,
472 * previous nat entry can be remained in nat cache.
473 * So, reinitialize it with new information.
474 */
475 copy_node_info(&e->ni, ni);
476 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
477 }
478 /* let's free early to reduce memory consumption */
479 if (e != new)
480 __free_nat_entry(new);
481
482 /* sanity check */
483 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
484 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
485 new_blkaddr == NULL_ADDR);
486 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
487 new_blkaddr == NEW_ADDR);
488 f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) &&
489 new_blkaddr == NEW_ADDR);
490
491 /* increment version no as node is removed */
492 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
493 unsigned char version = nat_get_version(e);
494
495 nat_set_version(e, inc_node_version(version));
496 }
497
498 /* change address */
499 nat_set_blkaddr(e, new_blkaddr);
500 if (!__is_valid_data_blkaddr(new_blkaddr))
501 set_nat_flag(e, IS_CHECKPOINTED, false);
502 __set_nat_cache_dirty(nm_i, e);
503
504 /* update fsync_mark if its inode nat entry is still alive */
505 if (ni->nid != ni->ino)
506 e = __lookup_nat_cache(nm_i, ni->ino);
507 if (e) {
508 if (fsync_done && ni->nid == ni->ino)
509 set_nat_flag(e, HAS_FSYNCED_INODE, true);
510 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
511 }
512 f2fs_up_write(&nm_i->nat_tree_lock);
513 }
514
f2fs_try_to_free_nats(struct f2fs_sb_info * sbi,int nr_shrink)515 int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
516 {
517 struct f2fs_nm_info *nm_i = NM_I(sbi);
518 int nr = nr_shrink;
519
520 if (!f2fs_down_write_trylock(&nm_i->nat_tree_lock))
521 return 0;
522
523 spin_lock(&nm_i->nat_list_lock);
524 while (nr_shrink) {
525 struct nat_entry *ne;
526
527 if (list_empty(&nm_i->nat_entries))
528 break;
529
530 ne = list_first_entry(&nm_i->nat_entries,
531 struct nat_entry, list);
532 list_del(&ne->list);
533 spin_unlock(&nm_i->nat_list_lock);
534
535 __del_from_nat_cache(nm_i, ne);
536 nr_shrink--;
537
538 spin_lock(&nm_i->nat_list_lock);
539 }
540 spin_unlock(&nm_i->nat_list_lock);
541
542 f2fs_up_write(&nm_i->nat_tree_lock);
543 return nr - nr_shrink;
544 }
545
f2fs_get_node_info(struct f2fs_sb_info * sbi,nid_t nid,struct node_info * ni,bool checkpoint_context)546 int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
547 struct node_info *ni, bool checkpoint_context)
548 {
549 struct f2fs_nm_info *nm_i = NM_I(sbi);
550 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
551 struct f2fs_journal *journal = curseg->journal;
552 nid_t start_nid = START_NID(nid);
553 struct f2fs_nat_block *nat_blk;
554 struct page *page = NULL;
555 struct f2fs_nat_entry ne;
556 struct nat_entry *e;
557 pgoff_t index;
558 block_t blkaddr;
559 int i;
560
561 ni->flag = 0;
562 ni->nid = nid;
563 retry:
564 /* Check nat cache */
565 f2fs_down_read(&nm_i->nat_tree_lock);
566 e = __lookup_nat_cache(nm_i, nid);
567 if (e) {
568 ni->ino = nat_get_ino(e);
569 ni->blk_addr = nat_get_blkaddr(e);
570 ni->version = nat_get_version(e);
571 f2fs_up_read(&nm_i->nat_tree_lock);
572 return 0;
573 }
574
575 /*
576 * Check current segment summary by trying to grab journal_rwsem first.
577 * This sem is on the critical path on the checkpoint requiring the above
578 * nat_tree_lock. Therefore, we should retry, if we failed to grab here
579 * while not bothering checkpoint.
580 */
581 if (!f2fs_rwsem_is_locked(&sbi->cp_global_sem) || checkpoint_context) {
582 down_read(&curseg->journal_rwsem);
583 } else if (f2fs_rwsem_is_contended(&nm_i->nat_tree_lock) ||
584 !down_read_trylock(&curseg->journal_rwsem)) {
585 f2fs_up_read(&nm_i->nat_tree_lock);
586 goto retry;
587 }
588
589 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
590 if (i >= 0) {
591 ne = nat_in_journal(journal, i);
592 node_info_from_raw_nat(ni, &ne);
593 }
594 up_read(&curseg->journal_rwsem);
595 if (i >= 0) {
596 f2fs_up_read(&nm_i->nat_tree_lock);
597 goto cache;
598 }
599
600 /* Fill node_info from nat page */
601 index = current_nat_addr(sbi, nid);
602 f2fs_up_read(&nm_i->nat_tree_lock);
603
604 page = f2fs_get_meta_page(sbi, index);
605 if (IS_ERR(page))
606 return PTR_ERR(page);
607
608 nat_blk = (struct f2fs_nat_block *)page_address(page);
609 ne = nat_blk->entries[nid - start_nid];
610 node_info_from_raw_nat(ni, &ne);
611 f2fs_put_page(page, 1);
612 cache:
613 blkaddr = le32_to_cpu(ne.block_addr);
614 if (__is_valid_data_blkaddr(blkaddr) &&
615 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE))
616 return -EFAULT;
617
618 /* cache nat entry */
619 cache_nat_entry(sbi, nid, &ne);
620 return 0;
621 }
622
623 /*
624 * readahead MAX_RA_NODE number of node pages.
625 */
f2fs_ra_node_pages(struct page * parent,int start,int n)626 static void f2fs_ra_node_pages(struct page *parent, int start, int n)
627 {
628 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
629 struct blk_plug plug;
630 int i, end;
631 nid_t nid;
632
633 blk_start_plug(&plug);
634
635 /* Then, try readahead for siblings of the desired node */
636 end = start + n;
637 end = min(end, (int)NIDS_PER_BLOCK);
638 for (i = start; i < end; i++) {
639 nid = get_nid(parent, i, false);
640 f2fs_ra_node_page(sbi, nid);
641 }
642
643 blk_finish_plug(&plug);
644 }
645
f2fs_get_next_page_offset(struct dnode_of_data * dn,pgoff_t pgofs)646 pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
647 {
648 const long direct_index = ADDRS_PER_INODE(dn->inode);
649 const long direct_blks = ADDRS_PER_BLOCK(dn->inode);
650 const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK;
651 unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode);
652 int cur_level = dn->cur_level;
653 int max_level = dn->max_level;
654 pgoff_t base = 0;
655
656 if (!dn->max_level)
657 return pgofs + 1;
658
659 while (max_level-- > cur_level)
660 skipped_unit *= NIDS_PER_BLOCK;
661
662 switch (dn->max_level) {
663 case 3:
664 base += 2 * indirect_blks;
665 fallthrough;
666 case 2:
667 base += 2 * direct_blks;
668 fallthrough;
669 case 1:
670 base += direct_index;
671 break;
672 default:
673 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
674 }
675
676 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
677 }
678
679 /*
680 * The maximum depth is four.
681 * Offset[0] will have raw inode offset.
682 */
get_node_path(struct inode * inode,long block,int offset[4],unsigned int noffset[4])683 static int get_node_path(struct inode *inode, long block,
684 int offset[4], unsigned int noffset[4])
685 {
686 const long direct_index = ADDRS_PER_INODE(inode);
687 const long direct_blks = ADDRS_PER_BLOCK(inode);
688 const long dptrs_per_blk = NIDS_PER_BLOCK;
689 const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK;
690 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
691 int n = 0;
692 int level = 0;
693
694 noffset[0] = 0;
695
696 if (block < direct_index) {
697 offset[n] = block;
698 goto got;
699 }
700 block -= direct_index;
701 if (block < direct_blks) {
702 offset[n++] = NODE_DIR1_BLOCK;
703 noffset[n] = 1;
704 offset[n] = block;
705 level = 1;
706 goto got;
707 }
708 block -= direct_blks;
709 if (block < direct_blks) {
710 offset[n++] = NODE_DIR2_BLOCK;
711 noffset[n] = 2;
712 offset[n] = block;
713 level = 1;
714 goto got;
715 }
716 block -= direct_blks;
717 if (block < indirect_blks) {
718 offset[n++] = NODE_IND1_BLOCK;
719 noffset[n] = 3;
720 offset[n++] = block / direct_blks;
721 noffset[n] = 4 + offset[n - 1];
722 offset[n] = block % direct_blks;
723 level = 2;
724 goto got;
725 }
726 block -= indirect_blks;
727 if (block < indirect_blks) {
728 offset[n++] = NODE_IND2_BLOCK;
729 noffset[n] = 4 + dptrs_per_blk;
730 offset[n++] = block / direct_blks;
731 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
732 offset[n] = block % direct_blks;
733 level = 2;
734 goto got;
735 }
736 block -= indirect_blks;
737 if (block < dindirect_blks) {
738 offset[n++] = NODE_DIND_BLOCK;
739 noffset[n] = 5 + (dptrs_per_blk * 2);
740 offset[n++] = block / indirect_blks;
741 noffset[n] = 6 + (dptrs_per_blk * 2) +
742 offset[n - 1] * (dptrs_per_blk + 1);
743 offset[n++] = (block / direct_blks) % dptrs_per_blk;
744 noffset[n] = 7 + (dptrs_per_blk * 2) +
745 offset[n - 2] * (dptrs_per_blk + 1) +
746 offset[n - 1];
747 offset[n] = block % direct_blks;
748 level = 3;
749 goto got;
750 } else {
751 return -E2BIG;
752 }
753 got:
754 return level;
755 }
756
757 /*
758 * Caller should call f2fs_put_dnode(dn).
759 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
760 * f2fs_unlock_op() only if mode is set with ALLOC_NODE.
761 */
f2fs_get_dnode_of_data(struct dnode_of_data * dn,pgoff_t index,int mode)762 int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
763 {
764 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
765 struct page *npage[4];
766 struct page *parent = NULL;
767 int offset[4];
768 unsigned int noffset[4];
769 nid_t nids[4];
770 int level, i = 0;
771 int err = 0;
772
773 level = get_node_path(dn->inode, index, offset, noffset);
774 if (level < 0)
775 return level;
776
777 nids[0] = dn->inode->i_ino;
778 npage[0] = dn->inode_page;
779
780 if (!npage[0]) {
781 npage[0] = f2fs_get_inode_page(sbi, nids[0]);
782 if (IS_ERR(npage[0]))
783 return PTR_ERR(npage[0]);
784 }
785
786 /* if inline_data is set, should not report any block indices */
787 if (f2fs_has_inline_data(dn->inode) && index) {
788 err = -ENOENT;
789 f2fs_put_page(npage[0], 1);
790 goto release_out;
791 }
792
793 parent = npage[0];
794 if (level != 0)
795 nids[1] = get_nid(parent, offset[0], true);
796 dn->inode_page = npage[0];
797 dn->inode_page_locked = true;
798
799 /* get indirect or direct nodes */
800 for (i = 1; i <= level; i++) {
801 bool done = false;
802
803 if (!nids[i] && mode == ALLOC_NODE) {
804 /* alloc new node */
805 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
806 err = -ENOSPC;
807 goto release_pages;
808 }
809
810 dn->nid = nids[i];
811 npage[i] = f2fs_new_node_page(dn, noffset[i]);
812 if (IS_ERR(npage[i])) {
813 f2fs_alloc_nid_failed(sbi, nids[i]);
814 err = PTR_ERR(npage[i]);
815 goto release_pages;
816 }
817
818 set_nid(parent, offset[i - 1], nids[i], i == 1);
819 f2fs_alloc_nid_done(sbi, nids[i]);
820 done = true;
821 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
822 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
823 if (IS_ERR(npage[i])) {
824 err = PTR_ERR(npage[i]);
825 goto release_pages;
826 }
827 done = true;
828 }
829 if (i == 1) {
830 dn->inode_page_locked = false;
831 unlock_page(parent);
832 } else {
833 f2fs_put_page(parent, 1);
834 }
835
836 if (!done) {
837 npage[i] = f2fs_get_node_page(sbi, nids[i]);
838 if (IS_ERR(npage[i])) {
839 err = PTR_ERR(npage[i]);
840 f2fs_put_page(npage[0], 0);
841 goto release_out;
842 }
843 }
844 if (i < level) {
845 parent = npage[i];
846 nids[i + 1] = get_nid(parent, offset[i], false);
847 }
848 }
849 dn->nid = nids[level];
850 dn->ofs_in_node = offset[level];
851 dn->node_page = npage[level];
852 dn->data_blkaddr = f2fs_data_blkaddr(dn);
853
854 if (is_inode_flag_set(dn->inode, FI_COMPRESSED_FILE) &&
855 f2fs_sb_has_readonly(sbi)) {
856 unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
857 unsigned int ofs_in_node = dn->ofs_in_node;
858 pgoff_t fofs = index;
859 unsigned int c_len;
860 block_t blkaddr;
861
862 /* should align fofs and ofs_in_node to cluster_size */
863 if (fofs % cluster_size) {
864 fofs = round_down(fofs, cluster_size);
865 ofs_in_node = round_down(ofs_in_node, cluster_size);
866 }
867
868 c_len = f2fs_cluster_blocks_are_contiguous(dn, ofs_in_node);
869 if (!c_len)
870 goto out;
871
872 blkaddr = data_blkaddr(dn->inode, dn->node_page, ofs_in_node);
873 if (blkaddr == COMPRESS_ADDR)
874 blkaddr = data_blkaddr(dn->inode, dn->node_page,
875 ofs_in_node + 1);
876
877 f2fs_update_read_extent_tree_range_compressed(dn->inode,
878 fofs, blkaddr, cluster_size, c_len);
879 }
880 out:
881 return 0;
882
883 release_pages:
884 f2fs_put_page(parent, 1);
885 if (i > 1)
886 f2fs_put_page(npage[0], 0);
887 release_out:
888 dn->inode_page = NULL;
889 dn->node_page = NULL;
890 if (err == -ENOENT) {
891 dn->cur_level = i;
892 dn->max_level = level;
893 dn->ofs_in_node = offset[level];
894 }
895 return err;
896 }
897
truncate_node(struct dnode_of_data * dn)898 static int truncate_node(struct dnode_of_data *dn)
899 {
900 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
901 struct node_info ni;
902 int err;
903 pgoff_t index;
904
905 err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
906 if (err)
907 return err;
908
909 if (ni.blk_addr != NEW_ADDR &&
910 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC_ENHANCE)) {
911 f2fs_err_ratelimited(sbi,
912 "nat entry is corrupted, run fsck to fix it, ino:%u, "
913 "nid:%u, blkaddr:%u", ni.ino, ni.nid, ni.blk_addr);
914 set_sbi_flag(sbi, SBI_NEED_FSCK);
915 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NAT);
916 return -EFSCORRUPTED;
917 }
918
919 /* Deallocate node address */
920 f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
921 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
922 set_node_addr(sbi, &ni, NULL_ADDR, false);
923
924 if (dn->nid == dn->inode->i_ino) {
925 f2fs_remove_orphan_inode(sbi, dn->nid);
926 dec_valid_inode_count(sbi);
927 f2fs_inode_synced(dn->inode);
928 }
929
930 clear_node_page_dirty(dn->node_page);
931 set_sbi_flag(sbi, SBI_IS_DIRTY);
932
933 index = page_folio(dn->node_page)->index;
934 f2fs_put_page(dn->node_page, 1);
935
936 invalidate_mapping_pages(NODE_MAPPING(sbi),
937 index, index);
938
939 dn->node_page = NULL;
940 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
941
942 return 0;
943 }
944
truncate_dnode(struct dnode_of_data * dn)945 static int truncate_dnode(struct dnode_of_data *dn)
946 {
947 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
948 struct page *page;
949 int err;
950
951 if (dn->nid == 0)
952 return 1;
953
954 /* get direct node */
955 page = f2fs_get_node_page(sbi, dn->nid);
956 if (PTR_ERR(page) == -ENOENT)
957 return 1;
958 else if (IS_ERR(page))
959 return PTR_ERR(page);
960
961 if (IS_INODE(page) || ino_of_node(page) != dn->inode->i_ino) {
962 f2fs_err(sbi, "incorrect node reference, ino: %lu, nid: %u, ino_of_node: %u",
963 dn->inode->i_ino, dn->nid, ino_of_node(page));
964 set_sbi_flag(sbi, SBI_NEED_FSCK);
965 f2fs_handle_error(sbi, ERROR_INVALID_NODE_REFERENCE);
966 f2fs_put_page(page, 1);
967 return -EFSCORRUPTED;
968 }
969
970 /* Make dnode_of_data for parameter */
971 dn->node_page = page;
972 dn->ofs_in_node = 0;
973 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
974 err = truncate_node(dn);
975 if (err) {
976 f2fs_put_page(page, 1);
977 return err;
978 }
979
980 return 1;
981 }
982
truncate_nodes(struct dnode_of_data * dn,unsigned int nofs,int ofs,int depth)983 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
984 int ofs, int depth)
985 {
986 struct dnode_of_data rdn = *dn;
987 struct page *page;
988 struct f2fs_node *rn;
989 nid_t child_nid;
990 unsigned int child_nofs;
991 int freed = 0;
992 int i, ret;
993
994 if (dn->nid == 0)
995 return NIDS_PER_BLOCK + 1;
996
997 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
998
999 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
1000 if (IS_ERR(page)) {
1001 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
1002 return PTR_ERR(page);
1003 }
1004
1005 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
1006
1007 rn = F2FS_NODE(page);
1008 if (depth < 3) {
1009 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
1010 child_nid = le32_to_cpu(rn->in.nid[i]);
1011 if (child_nid == 0)
1012 continue;
1013 rdn.nid = child_nid;
1014 ret = truncate_dnode(&rdn);
1015 if (ret < 0)
1016 goto out_err;
1017 if (set_nid(page, i, 0, false))
1018 dn->node_changed = true;
1019 }
1020 } else {
1021 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
1022 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
1023 child_nid = le32_to_cpu(rn->in.nid[i]);
1024 if (child_nid == 0) {
1025 child_nofs += NIDS_PER_BLOCK + 1;
1026 continue;
1027 }
1028 rdn.nid = child_nid;
1029 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
1030 if (ret == (NIDS_PER_BLOCK + 1)) {
1031 if (set_nid(page, i, 0, false))
1032 dn->node_changed = true;
1033 child_nofs += ret;
1034 } else if (ret < 0 && ret != -ENOENT) {
1035 goto out_err;
1036 }
1037 }
1038 freed = child_nofs;
1039 }
1040
1041 if (!ofs) {
1042 /* remove current indirect node */
1043 dn->node_page = page;
1044 ret = truncate_node(dn);
1045 if (ret)
1046 goto out_err;
1047 freed++;
1048 } else {
1049 f2fs_put_page(page, 1);
1050 }
1051 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
1052 return freed;
1053
1054 out_err:
1055 f2fs_put_page(page, 1);
1056 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
1057 return ret;
1058 }
1059
truncate_partial_nodes(struct dnode_of_data * dn,struct f2fs_inode * ri,int * offset,int depth)1060 static int truncate_partial_nodes(struct dnode_of_data *dn,
1061 struct f2fs_inode *ri, int *offset, int depth)
1062 {
1063 struct page *pages[2];
1064 nid_t nid[3];
1065 nid_t child_nid;
1066 int err = 0;
1067 int i;
1068 int idx = depth - 2;
1069
1070 nid[0] = get_nid(dn->inode_page, offset[0], true);
1071 if (!nid[0])
1072 return 0;
1073
1074 /* get indirect nodes in the path */
1075 for (i = 0; i < idx + 1; i++) {
1076 /* reference count'll be increased */
1077 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
1078 if (IS_ERR(pages[i])) {
1079 err = PTR_ERR(pages[i]);
1080 idx = i - 1;
1081 goto fail;
1082 }
1083 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
1084 }
1085
1086 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
1087
1088 /* free direct nodes linked to a partial indirect node */
1089 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
1090 child_nid = get_nid(pages[idx], i, false);
1091 if (!child_nid)
1092 continue;
1093 dn->nid = child_nid;
1094 err = truncate_dnode(dn);
1095 if (err < 0)
1096 goto fail;
1097 if (set_nid(pages[idx], i, 0, false))
1098 dn->node_changed = true;
1099 }
1100
1101 if (offset[idx + 1] == 0) {
1102 dn->node_page = pages[idx];
1103 dn->nid = nid[idx];
1104 err = truncate_node(dn);
1105 if (err)
1106 goto fail;
1107 } else {
1108 f2fs_put_page(pages[idx], 1);
1109 }
1110 offset[idx]++;
1111 offset[idx + 1] = 0;
1112 idx--;
1113 fail:
1114 for (i = idx; i >= 0; i--)
1115 f2fs_put_page(pages[i], 1);
1116
1117 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1118
1119 return err;
1120 }
1121
1122 /*
1123 * All the block addresses of data and nodes should be nullified.
1124 */
f2fs_truncate_inode_blocks(struct inode * inode,pgoff_t from)1125 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
1126 {
1127 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1128 int err = 0, cont = 1;
1129 int level, offset[4], noffset[4];
1130 unsigned int nofs = 0;
1131 struct f2fs_inode *ri;
1132 struct dnode_of_data dn;
1133 struct folio *folio;
1134
1135 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1136
1137 level = get_node_path(inode, from, offset, noffset);
1138 if (level <= 0) {
1139 if (!level) {
1140 level = -EFSCORRUPTED;
1141 f2fs_err(sbi, "%s: inode ino=%lx has corrupted node block, from:%lu addrs:%u",
1142 __func__, inode->i_ino,
1143 from, ADDRS_PER_INODE(inode));
1144 set_sbi_flag(sbi, SBI_NEED_FSCK);
1145 }
1146 trace_f2fs_truncate_inode_blocks_exit(inode, level);
1147 return level;
1148 }
1149
1150 folio = f2fs_get_inode_folio(sbi, inode->i_ino);
1151 if (IS_ERR(folio)) {
1152 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(folio));
1153 return PTR_ERR(folio);
1154 }
1155
1156 set_new_dnode(&dn, inode, &folio->page, NULL, 0);
1157 folio_unlock(folio);
1158
1159 ri = F2FS_INODE(&folio->page);
1160 switch (level) {
1161 case 0:
1162 case 1:
1163 nofs = noffset[1];
1164 break;
1165 case 2:
1166 nofs = noffset[1];
1167 if (!offset[level - 1])
1168 goto skip_partial;
1169 err = truncate_partial_nodes(&dn, ri, offset, level);
1170 if (err < 0 && err != -ENOENT)
1171 goto fail;
1172 nofs += 1 + NIDS_PER_BLOCK;
1173 break;
1174 case 3:
1175 nofs = 5 + 2 * NIDS_PER_BLOCK;
1176 if (!offset[level - 1])
1177 goto skip_partial;
1178 err = truncate_partial_nodes(&dn, ri, offset, level);
1179 if (err < 0 && err != -ENOENT)
1180 goto fail;
1181 break;
1182 default:
1183 BUG();
1184 }
1185
1186 skip_partial:
1187 while (cont) {
1188 dn.nid = get_nid(&folio->page, offset[0], true);
1189 switch (offset[0]) {
1190 case NODE_DIR1_BLOCK:
1191 case NODE_DIR2_BLOCK:
1192 err = truncate_dnode(&dn);
1193 break;
1194
1195 case NODE_IND1_BLOCK:
1196 case NODE_IND2_BLOCK:
1197 err = truncate_nodes(&dn, nofs, offset[1], 2);
1198 break;
1199
1200 case NODE_DIND_BLOCK:
1201 err = truncate_nodes(&dn, nofs, offset[1], 3);
1202 cont = 0;
1203 break;
1204
1205 default:
1206 BUG();
1207 }
1208 if (err == -ENOENT) {
1209 set_sbi_flag(F2FS_F_SB(folio), SBI_NEED_FSCK);
1210 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1211 f2fs_err_ratelimited(sbi,
1212 "truncate node fail, ino:%lu, nid:%u, "
1213 "offset[0]:%d, offset[1]:%d, nofs:%d",
1214 inode->i_ino, dn.nid, offset[0],
1215 offset[1], nofs);
1216 err = 0;
1217 }
1218 if (err < 0)
1219 goto fail;
1220 if (offset[1] == 0 && get_nid(&folio->page, offset[0], true)) {
1221 folio_lock(folio);
1222 BUG_ON(folio->mapping != NODE_MAPPING(sbi));
1223 set_nid(&folio->page, offset[0], 0, true);
1224 folio_unlock(folio);
1225 }
1226 offset[1] = 0;
1227 offset[0]++;
1228 nofs += err;
1229 }
1230 fail:
1231 f2fs_folio_put(folio, false);
1232 trace_f2fs_truncate_inode_blocks_exit(inode, err);
1233 return err > 0 ? 0 : err;
1234 }
1235
1236 /* caller must lock inode page */
f2fs_truncate_xattr_node(struct inode * inode)1237 int f2fs_truncate_xattr_node(struct inode *inode)
1238 {
1239 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1240 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1241 struct dnode_of_data dn;
1242 struct page *npage;
1243 int err;
1244
1245 if (!nid)
1246 return 0;
1247
1248 npage = f2fs_get_xnode_page(sbi, nid);
1249 if (IS_ERR(npage))
1250 return PTR_ERR(npage);
1251
1252 set_new_dnode(&dn, inode, NULL, npage, nid);
1253 err = truncate_node(&dn);
1254 if (err) {
1255 f2fs_put_page(npage, 1);
1256 return err;
1257 }
1258
1259 f2fs_i_xnid_write(inode, 0);
1260
1261 return 0;
1262 }
1263
1264 /*
1265 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1266 * f2fs_unlock_op().
1267 */
f2fs_remove_inode_page(struct inode * inode)1268 int f2fs_remove_inode_page(struct inode *inode)
1269 {
1270 struct dnode_of_data dn;
1271 int err;
1272
1273 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1274 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
1275 if (err)
1276 return err;
1277
1278 err = f2fs_truncate_xattr_node(inode);
1279 if (err) {
1280 f2fs_put_dnode(&dn);
1281 return err;
1282 }
1283
1284 /* remove potential inline_data blocks */
1285 if (!IS_DEVICE_ALIASING(inode) &&
1286 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1287 S_ISLNK(inode->i_mode)))
1288 f2fs_truncate_data_blocks_range(&dn, 1);
1289
1290 /* 0 is possible, after f2fs_new_inode() has failed */
1291 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1292 f2fs_put_dnode(&dn);
1293 return -EIO;
1294 }
1295
1296 if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
1297 f2fs_warn(F2FS_I_SB(inode),
1298 "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu",
1299 inode->i_ino, (unsigned long long)inode->i_blocks);
1300 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
1301 }
1302
1303 /* will put inode & node pages */
1304 err = truncate_node(&dn);
1305 if (err) {
1306 f2fs_put_dnode(&dn);
1307 return err;
1308 }
1309 return 0;
1310 }
1311
f2fs_new_inode_page(struct inode * inode)1312 struct page *f2fs_new_inode_page(struct inode *inode)
1313 {
1314 struct dnode_of_data dn;
1315
1316 /* allocate inode page for new inode */
1317 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1318
1319 /* caller should f2fs_put_page(page, 1); */
1320 return f2fs_new_node_page(&dn, 0);
1321 }
1322
f2fs_new_node_page(struct dnode_of_data * dn,unsigned int ofs)1323 struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
1324 {
1325 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1326 struct node_info new_ni;
1327 struct page *page;
1328 int err;
1329
1330 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1331 return ERR_PTR(-EPERM);
1332
1333 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
1334 if (!page)
1335 return ERR_PTR(-ENOMEM);
1336
1337 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
1338 goto fail;
1339
1340 #ifdef CONFIG_F2FS_CHECK_FS
1341 err = f2fs_get_node_info(sbi, dn->nid, &new_ni, false);
1342 if (err) {
1343 dec_valid_node_count(sbi, dn->inode, !ofs);
1344 goto fail;
1345 }
1346 if (unlikely(new_ni.blk_addr != NULL_ADDR)) {
1347 err = -EFSCORRUPTED;
1348 dec_valid_node_count(sbi, dn->inode, !ofs);
1349 set_sbi_flag(sbi, SBI_NEED_FSCK);
1350 f2fs_warn_ratelimited(sbi,
1351 "f2fs_new_node_page: inconsistent nat entry, "
1352 "ino:%u, nid:%u, blkaddr:%u, ver:%u, flag:%u",
1353 new_ni.ino, new_ni.nid, new_ni.blk_addr,
1354 new_ni.version, new_ni.flag);
1355 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NAT);
1356 goto fail;
1357 }
1358 #endif
1359 new_ni.nid = dn->nid;
1360 new_ni.ino = dn->inode->i_ino;
1361 new_ni.blk_addr = NULL_ADDR;
1362 new_ni.flag = 0;
1363 new_ni.version = 0;
1364 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
1365
1366 f2fs_wait_on_page_writeback(page, NODE, true, true);
1367 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
1368 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
1369 if (!PageUptodate(page))
1370 SetPageUptodate(page);
1371 if (set_page_dirty(page))
1372 dn->node_changed = true;
1373
1374 if (f2fs_has_xattr_block(ofs))
1375 f2fs_i_xnid_write(dn->inode, dn->nid);
1376
1377 if (ofs == 0)
1378 inc_valid_inode_count(sbi);
1379 return page;
1380 fail:
1381 clear_node_page_dirty(page);
1382 f2fs_put_page(page, 1);
1383 return ERR_PTR(err);
1384 }
1385
1386 /*
1387 * Caller should do after getting the following values.
1388 * 0: f2fs_put_page(page, 0)
1389 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1390 */
read_node_page(struct page * page,blk_opf_t op_flags)1391 static int read_node_page(struct page *page, blk_opf_t op_flags)
1392 {
1393 struct folio *folio = page_folio(page);
1394 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1395 struct node_info ni;
1396 struct f2fs_io_info fio = {
1397 .sbi = sbi,
1398 .type = NODE,
1399 .op = REQ_OP_READ,
1400 .op_flags = op_flags,
1401 .page = page,
1402 .encrypted_page = NULL,
1403 };
1404 int err;
1405
1406 if (folio_test_uptodate(folio)) {
1407 if (!f2fs_inode_chksum_verify(sbi, page)) {
1408 folio_clear_uptodate(folio);
1409 return -EFSBADCRC;
1410 }
1411 return LOCKED_PAGE;
1412 }
1413
1414 err = f2fs_get_node_info(sbi, folio->index, &ni, false);
1415 if (err)
1416 return err;
1417
1418 /* NEW_ADDR can be seen, after cp_error drops some dirty node pages */
1419 if (unlikely(ni.blk_addr == NULL_ADDR || ni.blk_addr == NEW_ADDR)) {
1420 folio_clear_uptodate(folio);
1421 return -ENOENT;
1422 }
1423
1424 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
1425
1426 err = f2fs_submit_page_bio(&fio);
1427
1428 if (!err)
1429 f2fs_update_iostat(sbi, NULL, FS_NODE_READ_IO, F2FS_BLKSIZE);
1430
1431 return err;
1432 }
1433
1434 /*
1435 * Readahead a node page
1436 */
f2fs_ra_node_page(struct f2fs_sb_info * sbi,nid_t nid)1437 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1438 {
1439 struct page *apage;
1440 int err;
1441
1442 if (!nid)
1443 return;
1444 if (f2fs_check_nid_range(sbi, nid))
1445 return;
1446
1447 apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
1448 if (apage)
1449 return;
1450
1451 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1452 if (!apage)
1453 return;
1454
1455 err = read_node_page(apage, REQ_RAHEAD);
1456 f2fs_put_page(apage, err ? 1 : 0);
1457 }
1458
sanity_check_node_footer(struct f2fs_sb_info * sbi,struct page * page,pgoff_t nid,enum node_type ntype)1459 static int sanity_check_node_footer(struct f2fs_sb_info *sbi,
1460 struct page *page, pgoff_t nid,
1461 enum node_type ntype)
1462 {
1463 if (unlikely(nid != nid_of_node(page) ||
1464 (ntype == NODE_TYPE_INODE && !IS_INODE(page)) ||
1465 (ntype == NODE_TYPE_XATTR &&
1466 !f2fs_has_xattr_block(ofs_of_node(page))) ||
1467 time_to_inject(sbi, FAULT_INCONSISTENT_FOOTER))) {
1468 f2fs_warn(sbi, "inconsistent node block, node_type:%d, nid:%lu, "
1469 "node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1470 ntype, nid, nid_of_node(page), ino_of_node(page),
1471 ofs_of_node(page), cpver_of_node(page),
1472 next_blkaddr_of_node(page));
1473 set_sbi_flag(sbi, SBI_NEED_FSCK);
1474 f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
1475 return -EFSCORRUPTED;
1476 }
1477 return 0;
1478 }
1479
__get_node_folio(struct f2fs_sb_info * sbi,pgoff_t nid,struct page * parent,int start,enum node_type ntype)1480 static struct folio *__get_node_folio(struct f2fs_sb_info *sbi, pgoff_t nid,
1481 struct page *parent, int start,
1482 enum node_type ntype)
1483 {
1484 struct folio *folio;
1485 int err;
1486
1487 if (!nid)
1488 return ERR_PTR(-ENOENT);
1489 if (f2fs_check_nid_range(sbi, nid))
1490 return ERR_PTR(-EINVAL);
1491 repeat:
1492 folio = f2fs_grab_cache_folio(NODE_MAPPING(sbi), nid, false);
1493 if (IS_ERR(folio))
1494 return folio;
1495
1496 err = read_node_page(&folio->page, 0);
1497 if (err < 0) {
1498 goto out_put_err;
1499 } else if (err == LOCKED_PAGE) {
1500 err = 0;
1501 goto page_hit;
1502 }
1503
1504 if (parent)
1505 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
1506
1507 folio_lock(folio);
1508
1509 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
1510 f2fs_folio_put(folio, true);
1511 goto repeat;
1512 }
1513
1514 if (unlikely(!folio_test_uptodate(folio))) {
1515 err = -EIO;
1516 goto out_err;
1517 }
1518
1519 if (!f2fs_inode_chksum_verify(sbi, &folio->page)) {
1520 err = -EFSBADCRC;
1521 goto out_err;
1522 }
1523 page_hit:
1524 err = sanity_check_node_footer(sbi, &folio->page, nid, ntype);
1525 if (!err)
1526 return folio;
1527 out_err:
1528 folio_clear_uptodate(folio);
1529 out_put_err:
1530 /* ENOENT comes from read_node_page which is not an error. */
1531 if (err != -ENOENT)
1532 f2fs_handle_page_eio(sbi, folio, NODE);
1533 f2fs_folio_put(folio, true);
1534 return ERR_PTR(err);
1535 }
1536
f2fs_get_node_page(struct f2fs_sb_info * sbi,pgoff_t nid)1537 struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1538 {
1539 struct folio *folio = __get_node_folio(sbi, nid, NULL, 0,
1540 NODE_TYPE_REGULAR);
1541
1542 return &folio->page;
1543 }
1544
f2fs_get_inode_folio(struct f2fs_sb_info * sbi,pgoff_t ino)1545 struct folio *f2fs_get_inode_folio(struct f2fs_sb_info *sbi, pgoff_t ino)
1546 {
1547 return __get_node_folio(sbi, ino, NULL, 0, NODE_TYPE_INODE);
1548 }
1549
f2fs_get_inode_page(struct f2fs_sb_info * sbi,pgoff_t ino)1550 struct page *f2fs_get_inode_page(struct f2fs_sb_info *sbi, pgoff_t ino)
1551 {
1552 struct folio *folio = f2fs_get_inode_folio(sbi, ino);
1553
1554 return &folio->page;
1555 }
1556
f2fs_get_xnode_page(struct f2fs_sb_info * sbi,pgoff_t xnid)1557 struct page *f2fs_get_xnode_page(struct f2fs_sb_info *sbi, pgoff_t xnid)
1558 {
1559 struct folio *folio = __get_node_folio(sbi, xnid, NULL, 0,
1560 NODE_TYPE_XATTR);
1561
1562 return &folio->page;
1563 }
1564
f2fs_get_node_page_ra(struct page * parent,int start)1565 struct page *f2fs_get_node_page_ra(struct page *parent, int start)
1566 {
1567 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
1568 nid_t nid = get_nid(parent, start, false);
1569 struct folio *folio = __get_node_folio(sbi, nid, parent, start,
1570 NODE_TYPE_REGULAR);
1571
1572 return &folio->page;
1573 }
1574
flush_inline_data(struct f2fs_sb_info * sbi,nid_t ino)1575 static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1576 {
1577 struct inode *inode;
1578 struct page *page;
1579 int ret;
1580
1581 /* should flush inline_data before evict_inode */
1582 inode = ilookup(sbi->sb, ino);
1583 if (!inode)
1584 return;
1585
1586 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1587 FGP_LOCK|FGP_NOWAIT, 0);
1588 if (!page)
1589 goto iput_out;
1590
1591 if (!PageUptodate(page))
1592 goto page_out;
1593
1594 if (!PageDirty(page))
1595 goto page_out;
1596
1597 if (!clear_page_dirty_for_io(page))
1598 goto page_out;
1599
1600 ret = f2fs_write_inline_data(inode, page_folio(page));
1601 inode_dec_dirty_pages(inode);
1602 f2fs_remove_dirty_inode(inode);
1603 if (ret)
1604 set_page_dirty(page);
1605 page_out:
1606 f2fs_put_page(page, 1);
1607 iput_out:
1608 iput(inode);
1609 }
1610
last_fsync_dnode(struct f2fs_sb_info * sbi,nid_t ino)1611 static struct folio *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
1612 {
1613 pgoff_t index;
1614 struct folio_batch fbatch;
1615 struct folio *last_folio = NULL;
1616 int nr_folios;
1617
1618 folio_batch_init(&fbatch);
1619 index = 0;
1620
1621 while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
1622 (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
1623 &fbatch))) {
1624 int i;
1625
1626 for (i = 0; i < nr_folios; i++) {
1627 struct folio *folio = fbatch.folios[i];
1628
1629 if (unlikely(f2fs_cp_error(sbi))) {
1630 f2fs_folio_put(last_folio, false);
1631 folio_batch_release(&fbatch);
1632 return ERR_PTR(-EIO);
1633 }
1634
1635 if (!IS_DNODE(&folio->page) || !is_cold_node(&folio->page))
1636 continue;
1637 if (ino_of_node(&folio->page) != ino)
1638 continue;
1639
1640 folio_lock(folio);
1641
1642 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
1643 continue_unlock:
1644 folio_unlock(folio);
1645 continue;
1646 }
1647 if (ino_of_node(&folio->page) != ino)
1648 goto continue_unlock;
1649
1650 if (!folio_test_dirty(folio)) {
1651 /* someone wrote it for us */
1652 goto continue_unlock;
1653 }
1654
1655 if (last_folio)
1656 f2fs_folio_put(last_folio, false);
1657
1658 folio_get(folio);
1659 last_folio = folio;
1660 folio_unlock(folio);
1661 }
1662 folio_batch_release(&fbatch);
1663 cond_resched();
1664 }
1665 return last_folio;
1666 }
1667
__write_node_page(struct page * page,bool atomic,bool * submitted,struct writeback_control * wbc,bool do_balance,enum iostat_type io_type,unsigned int * seq_id)1668 static int __write_node_page(struct page *page, bool atomic, bool *submitted,
1669 struct writeback_control *wbc, bool do_balance,
1670 enum iostat_type io_type, unsigned int *seq_id)
1671 {
1672 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1673 struct folio *folio = page_folio(page);
1674 nid_t nid;
1675 struct node_info ni;
1676 struct f2fs_io_info fio = {
1677 .sbi = sbi,
1678 .ino = ino_of_node(page),
1679 .type = NODE,
1680 .op = REQ_OP_WRITE,
1681 .op_flags = wbc_to_write_flags(wbc),
1682 .page = page,
1683 .encrypted_page = NULL,
1684 .submitted = 0,
1685 .io_type = io_type,
1686 .io_wbc = wbc,
1687 };
1688 unsigned int seq;
1689
1690 trace_f2fs_writepage(folio, NODE);
1691
1692 if (unlikely(f2fs_cp_error(sbi))) {
1693 /* keep node pages in remount-ro mode */
1694 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
1695 goto redirty_out;
1696 folio_clear_uptodate(folio);
1697 dec_page_count(sbi, F2FS_DIRTY_NODES);
1698 folio_unlock(folio);
1699 return 0;
1700 }
1701
1702 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1703 goto redirty_out;
1704
1705 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1706 wbc->sync_mode == WB_SYNC_NONE &&
1707 IS_DNODE(page) && is_cold_node(page))
1708 goto redirty_out;
1709
1710 /* get old block addr of this node page */
1711 nid = nid_of_node(page);
1712 f2fs_bug_on(sbi, folio->index != nid);
1713
1714 if (f2fs_get_node_info(sbi, nid, &ni, !do_balance))
1715 goto redirty_out;
1716
1717 if (wbc->for_reclaim) {
1718 if (!f2fs_down_read_trylock(&sbi->node_write))
1719 goto redirty_out;
1720 } else {
1721 f2fs_down_read(&sbi->node_write);
1722 }
1723
1724 /* This page is already truncated */
1725 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1726 folio_clear_uptodate(folio);
1727 dec_page_count(sbi, F2FS_DIRTY_NODES);
1728 f2fs_up_read(&sbi->node_write);
1729 folio_unlock(folio);
1730 return 0;
1731 }
1732
1733 if (__is_valid_data_blkaddr(ni.blk_addr) &&
1734 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
1735 DATA_GENERIC_ENHANCE)) {
1736 f2fs_up_read(&sbi->node_write);
1737 goto redirty_out;
1738 }
1739
1740 if (atomic && !test_opt(sbi, NOBARRIER))
1741 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1742
1743 /* should add to global list before clearing PAGECACHE status */
1744 if (f2fs_in_warm_node_list(sbi, folio)) {
1745 seq = f2fs_add_fsync_node_entry(sbi, page);
1746 if (seq_id)
1747 *seq_id = seq;
1748 }
1749
1750 folio_start_writeback(folio);
1751
1752 fio.old_blkaddr = ni.blk_addr;
1753 f2fs_do_write_node_page(nid, &fio);
1754 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1755 dec_page_count(sbi, F2FS_DIRTY_NODES);
1756 f2fs_up_read(&sbi->node_write);
1757
1758 if (wbc->for_reclaim) {
1759 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
1760 submitted = NULL;
1761 }
1762
1763 folio_unlock(folio);
1764
1765 if (unlikely(f2fs_cp_error(sbi))) {
1766 f2fs_submit_merged_write(sbi, NODE);
1767 submitted = NULL;
1768 }
1769 if (submitted)
1770 *submitted = fio.submitted;
1771
1772 if (do_balance)
1773 f2fs_balance_fs(sbi, false);
1774 return 0;
1775
1776 redirty_out:
1777 folio_redirty_for_writepage(wbc, folio);
1778 return AOP_WRITEPAGE_ACTIVATE;
1779 }
1780
f2fs_move_node_page(struct page * node_page,int gc_type)1781 int f2fs_move_node_page(struct page *node_page, int gc_type)
1782 {
1783 int err = 0;
1784
1785 if (gc_type == FG_GC) {
1786 struct writeback_control wbc = {
1787 .sync_mode = WB_SYNC_ALL,
1788 .nr_to_write = 1,
1789 .for_reclaim = 0,
1790 };
1791
1792 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
1793
1794 set_page_dirty(node_page);
1795
1796 if (!clear_page_dirty_for_io(node_page)) {
1797 err = -EAGAIN;
1798 goto out_page;
1799 }
1800
1801 if (__write_node_page(node_page, false, NULL,
1802 &wbc, false, FS_GC_NODE_IO, NULL)) {
1803 err = -EAGAIN;
1804 unlock_page(node_page);
1805 }
1806 goto release_page;
1807 } else {
1808 /* set page dirty and write it */
1809 if (!folio_test_writeback(page_folio(node_page)))
1810 set_page_dirty(node_page);
1811 }
1812 out_page:
1813 unlock_page(node_page);
1814 release_page:
1815 f2fs_put_page(node_page, 0);
1816 return err;
1817 }
1818
f2fs_fsync_node_pages(struct f2fs_sb_info * sbi,struct inode * inode,struct writeback_control * wbc,bool atomic,unsigned int * seq_id)1819 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
1820 struct writeback_control *wbc, bool atomic,
1821 unsigned int *seq_id)
1822 {
1823 pgoff_t index;
1824 struct folio_batch fbatch;
1825 int ret = 0;
1826 struct folio *last_folio = NULL;
1827 bool marked = false;
1828 nid_t ino = inode->i_ino;
1829 int nr_folios;
1830 int nwritten = 0;
1831
1832 if (atomic) {
1833 last_folio = last_fsync_dnode(sbi, ino);
1834 if (IS_ERR_OR_NULL(last_folio))
1835 return PTR_ERR_OR_ZERO(last_folio);
1836 }
1837 retry:
1838 folio_batch_init(&fbatch);
1839 index = 0;
1840
1841 while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
1842 (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
1843 &fbatch))) {
1844 int i;
1845
1846 for (i = 0; i < nr_folios; i++) {
1847 struct folio *folio = fbatch.folios[i];
1848 bool submitted = false;
1849
1850 if (unlikely(f2fs_cp_error(sbi))) {
1851 f2fs_folio_put(last_folio, false);
1852 folio_batch_release(&fbatch);
1853 ret = -EIO;
1854 goto out;
1855 }
1856
1857 if (!IS_DNODE(&folio->page) || !is_cold_node(&folio->page))
1858 continue;
1859 if (ino_of_node(&folio->page) != ino)
1860 continue;
1861
1862 folio_lock(folio);
1863
1864 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
1865 continue_unlock:
1866 folio_unlock(folio);
1867 continue;
1868 }
1869 if (ino_of_node(&folio->page) != ino)
1870 goto continue_unlock;
1871
1872 if (!folio_test_dirty(folio) && folio != last_folio) {
1873 /* someone wrote it for us */
1874 goto continue_unlock;
1875 }
1876
1877 f2fs_folio_wait_writeback(folio, NODE, true, true);
1878
1879 set_fsync_mark(&folio->page, 0);
1880 set_dentry_mark(&folio->page, 0);
1881
1882 if (!atomic || folio == last_folio) {
1883 set_fsync_mark(&folio->page, 1);
1884 percpu_counter_inc(&sbi->rf_node_block_count);
1885 if (IS_INODE(&folio->page)) {
1886 if (is_inode_flag_set(inode,
1887 FI_DIRTY_INODE))
1888 f2fs_update_inode(inode, &folio->page);
1889 set_dentry_mark(&folio->page,
1890 f2fs_need_dentry_mark(sbi, ino));
1891 }
1892 /* may be written by other thread */
1893 if (!folio_test_dirty(folio))
1894 folio_mark_dirty(folio);
1895 }
1896
1897 if (!folio_clear_dirty_for_io(folio))
1898 goto continue_unlock;
1899
1900 ret = __write_node_page(&folio->page, atomic &&
1901 folio == last_folio,
1902 &submitted, wbc, true,
1903 FS_NODE_IO, seq_id);
1904 if (ret) {
1905 folio_unlock(folio);
1906 f2fs_folio_put(last_folio, false);
1907 break;
1908 } else if (submitted) {
1909 nwritten++;
1910 }
1911
1912 if (folio == last_folio) {
1913 f2fs_folio_put(folio, false);
1914 marked = true;
1915 break;
1916 }
1917 }
1918 folio_batch_release(&fbatch);
1919 cond_resched();
1920
1921 if (ret || marked)
1922 break;
1923 }
1924 if (!ret && atomic && !marked) {
1925 f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
1926 ino, last_folio->index);
1927 folio_lock(last_folio);
1928 f2fs_folio_wait_writeback(last_folio, NODE, true, true);
1929 folio_mark_dirty(last_folio);
1930 folio_unlock(last_folio);
1931 goto retry;
1932 }
1933 out:
1934 if (nwritten)
1935 f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
1936 return ret ? -EIO : 0;
1937 }
1938
f2fs_match_ino(struct inode * inode,unsigned long ino,void * data)1939 static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data)
1940 {
1941 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1942 bool clean;
1943
1944 if (inode->i_ino != ino)
1945 return 0;
1946
1947 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
1948 return 0;
1949
1950 spin_lock(&sbi->inode_lock[DIRTY_META]);
1951 clean = list_empty(&F2FS_I(inode)->gdirty_list);
1952 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1953
1954 if (clean)
1955 return 0;
1956
1957 inode = igrab(inode);
1958 if (!inode)
1959 return 0;
1960 return 1;
1961 }
1962
flush_dirty_inode(struct folio * folio)1963 static bool flush_dirty_inode(struct folio *folio)
1964 {
1965 struct f2fs_sb_info *sbi = F2FS_F_SB(folio);
1966 struct inode *inode;
1967 nid_t ino = ino_of_node(&folio->page);
1968
1969 inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL);
1970 if (!inode)
1971 return false;
1972
1973 f2fs_update_inode(inode, &folio->page);
1974 folio_unlock(folio);
1975
1976 iput(inode);
1977 return true;
1978 }
1979
f2fs_flush_inline_data(struct f2fs_sb_info * sbi)1980 void f2fs_flush_inline_data(struct f2fs_sb_info *sbi)
1981 {
1982 pgoff_t index = 0;
1983 struct folio_batch fbatch;
1984 int nr_folios;
1985
1986 folio_batch_init(&fbatch);
1987
1988 while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
1989 (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
1990 &fbatch))) {
1991 int i;
1992
1993 for (i = 0; i < nr_folios; i++) {
1994 struct folio *folio = fbatch.folios[i];
1995
1996 if (!IS_INODE(&folio->page))
1997 continue;
1998
1999 folio_lock(folio);
2000
2001 if (unlikely(folio->mapping != NODE_MAPPING(sbi)))
2002 goto unlock;
2003 if (!folio_test_dirty(folio))
2004 goto unlock;
2005
2006 /* flush inline_data, if it's async context. */
2007 if (page_private_inline(&folio->page)) {
2008 clear_page_private_inline(&folio->page);
2009 folio_unlock(folio);
2010 flush_inline_data(sbi, ino_of_node(&folio->page));
2011 continue;
2012 }
2013 unlock:
2014 folio_unlock(folio);
2015 }
2016 folio_batch_release(&fbatch);
2017 cond_resched();
2018 }
2019 }
2020
f2fs_sync_node_pages(struct f2fs_sb_info * sbi,struct writeback_control * wbc,bool do_balance,enum iostat_type io_type)2021 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
2022 struct writeback_control *wbc,
2023 bool do_balance, enum iostat_type io_type)
2024 {
2025 pgoff_t index;
2026 struct folio_batch fbatch;
2027 int step = 0;
2028 int nwritten = 0;
2029 int ret = 0;
2030 int nr_folios, done = 0;
2031
2032 folio_batch_init(&fbatch);
2033
2034 next_step:
2035 index = 0;
2036
2037 while (!done && (nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi),
2038 &index, (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
2039 &fbatch))) {
2040 int i;
2041
2042 for (i = 0; i < nr_folios; i++) {
2043 struct folio *folio = fbatch.folios[i];
2044 bool submitted = false;
2045
2046 /* give a priority to WB_SYNC threads */
2047 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
2048 wbc->sync_mode == WB_SYNC_NONE) {
2049 done = 1;
2050 break;
2051 }
2052
2053 /*
2054 * flushing sequence with step:
2055 * 0. indirect nodes
2056 * 1. dentry dnodes
2057 * 2. file dnodes
2058 */
2059 if (step == 0 && IS_DNODE(&folio->page))
2060 continue;
2061 if (step == 1 && (!IS_DNODE(&folio->page) ||
2062 is_cold_node(&folio->page)))
2063 continue;
2064 if (step == 2 && (!IS_DNODE(&folio->page) ||
2065 !is_cold_node(&folio->page)))
2066 continue;
2067 lock_node:
2068 if (wbc->sync_mode == WB_SYNC_ALL)
2069 folio_lock(folio);
2070 else if (!folio_trylock(folio))
2071 continue;
2072
2073 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
2074 continue_unlock:
2075 folio_unlock(folio);
2076 continue;
2077 }
2078
2079 if (!folio_test_dirty(folio)) {
2080 /* someone wrote it for us */
2081 goto continue_unlock;
2082 }
2083
2084 /* flush inline_data/inode, if it's async context. */
2085 if (!do_balance)
2086 goto write_node;
2087
2088 /* flush inline_data */
2089 if (page_private_inline(&folio->page)) {
2090 clear_page_private_inline(&folio->page);
2091 folio_unlock(folio);
2092 flush_inline_data(sbi, ino_of_node(&folio->page));
2093 goto lock_node;
2094 }
2095
2096 /* flush dirty inode */
2097 if (IS_INODE(&folio->page) && flush_dirty_inode(folio))
2098 goto lock_node;
2099 write_node:
2100 f2fs_folio_wait_writeback(folio, NODE, true, true);
2101
2102 if (!folio_clear_dirty_for_io(folio))
2103 goto continue_unlock;
2104
2105 set_fsync_mark(&folio->page, 0);
2106 set_dentry_mark(&folio->page, 0);
2107
2108 ret = __write_node_page(&folio->page, false, &submitted,
2109 wbc, do_balance, io_type, NULL);
2110 if (ret)
2111 folio_unlock(folio);
2112 else if (submitted)
2113 nwritten++;
2114
2115 if (--wbc->nr_to_write == 0)
2116 break;
2117 }
2118 folio_batch_release(&fbatch);
2119 cond_resched();
2120
2121 if (wbc->nr_to_write == 0) {
2122 step = 2;
2123 break;
2124 }
2125 }
2126
2127 if (step < 2) {
2128 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2129 wbc->sync_mode == WB_SYNC_NONE && step == 1)
2130 goto out;
2131 step++;
2132 goto next_step;
2133 }
2134 out:
2135 if (nwritten)
2136 f2fs_submit_merged_write(sbi, NODE);
2137
2138 if (unlikely(f2fs_cp_error(sbi)))
2139 return -EIO;
2140 return ret;
2141 }
2142
f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info * sbi,unsigned int seq_id)2143 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
2144 unsigned int seq_id)
2145 {
2146 struct fsync_node_entry *fn;
2147 struct page *page;
2148 struct list_head *head = &sbi->fsync_node_list;
2149 unsigned long flags;
2150 unsigned int cur_seq_id = 0;
2151
2152 while (seq_id && cur_seq_id < seq_id) {
2153 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
2154 if (list_empty(head)) {
2155 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2156 break;
2157 }
2158 fn = list_first_entry(head, struct fsync_node_entry, list);
2159 if (fn->seq_id > seq_id) {
2160 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2161 break;
2162 }
2163 cur_seq_id = fn->seq_id;
2164 page = fn->page;
2165 get_page(page);
2166 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2167
2168 f2fs_wait_on_page_writeback(page, NODE, true, false);
2169
2170 put_page(page);
2171 }
2172
2173 return filemap_check_errors(NODE_MAPPING(sbi));
2174 }
2175
f2fs_write_node_pages(struct address_space * mapping,struct writeback_control * wbc)2176 static int f2fs_write_node_pages(struct address_space *mapping,
2177 struct writeback_control *wbc)
2178 {
2179 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2180 struct blk_plug plug;
2181 long diff;
2182
2183 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2184 goto skip_write;
2185
2186 /* balancing f2fs's metadata in background */
2187 f2fs_balance_fs_bg(sbi, true);
2188
2189 /* collect a number of dirty node pages and write together */
2190 if (wbc->sync_mode != WB_SYNC_ALL &&
2191 get_pages(sbi, F2FS_DIRTY_NODES) <
2192 nr_pages_to_skip(sbi, NODE))
2193 goto skip_write;
2194
2195 if (wbc->sync_mode == WB_SYNC_ALL)
2196 atomic_inc(&sbi->wb_sync_req[NODE]);
2197 else if (atomic_read(&sbi->wb_sync_req[NODE])) {
2198 /* to avoid potential deadlock */
2199 if (current->plug)
2200 blk_finish_plug(current->plug);
2201 goto skip_write;
2202 }
2203
2204 trace_f2fs_writepages(mapping->host, wbc, NODE);
2205
2206 diff = nr_pages_to_write(sbi, NODE, wbc);
2207 blk_start_plug(&plug);
2208 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
2209 blk_finish_plug(&plug);
2210 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
2211
2212 if (wbc->sync_mode == WB_SYNC_ALL)
2213 atomic_dec(&sbi->wb_sync_req[NODE]);
2214 return 0;
2215
2216 skip_write:
2217 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
2218 trace_f2fs_writepages(mapping->host, wbc, NODE);
2219 return 0;
2220 }
2221
f2fs_dirty_node_folio(struct address_space * mapping,struct folio * folio)2222 static bool f2fs_dirty_node_folio(struct address_space *mapping,
2223 struct folio *folio)
2224 {
2225 trace_f2fs_set_page_dirty(folio, NODE);
2226
2227 if (!folio_test_uptodate(folio))
2228 folio_mark_uptodate(folio);
2229 #ifdef CONFIG_F2FS_CHECK_FS
2230 if (IS_INODE(&folio->page))
2231 f2fs_inode_chksum_set(F2FS_M_SB(mapping), &folio->page);
2232 #endif
2233 if (filemap_dirty_folio(mapping, folio)) {
2234 inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
2235 set_page_private_reference(&folio->page);
2236 return true;
2237 }
2238 return false;
2239 }
2240
2241 /*
2242 * Structure of the f2fs node operations
2243 */
2244 const struct address_space_operations f2fs_node_aops = {
2245 .writepages = f2fs_write_node_pages,
2246 .dirty_folio = f2fs_dirty_node_folio,
2247 .invalidate_folio = f2fs_invalidate_folio,
2248 .release_folio = f2fs_release_folio,
2249 .migrate_folio = filemap_migrate_folio,
2250 };
2251
__lookup_free_nid_list(struct f2fs_nm_info * nm_i,nid_t n)2252 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
2253 nid_t n)
2254 {
2255 return radix_tree_lookup(&nm_i->free_nid_root, n);
2256 }
2257
__insert_free_nid(struct f2fs_sb_info * sbi,struct free_nid * i)2258 static int __insert_free_nid(struct f2fs_sb_info *sbi,
2259 struct free_nid *i)
2260 {
2261 struct f2fs_nm_info *nm_i = NM_I(sbi);
2262 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
2263
2264 if (err)
2265 return err;
2266
2267 nm_i->nid_cnt[FREE_NID]++;
2268 list_add_tail(&i->list, &nm_i->free_nid_list);
2269 return 0;
2270 }
2271
__remove_free_nid(struct f2fs_sb_info * sbi,struct free_nid * i,enum nid_state state)2272 static void __remove_free_nid(struct f2fs_sb_info *sbi,
2273 struct free_nid *i, enum nid_state state)
2274 {
2275 struct f2fs_nm_info *nm_i = NM_I(sbi);
2276
2277 f2fs_bug_on(sbi, state != i->state);
2278 nm_i->nid_cnt[state]--;
2279 if (state == FREE_NID)
2280 list_del(&i->list);
2281 radix_tree_delete(&nm_i->free_nid_root, i->nid);
2282 }
2283
__move_free_nid(struct f2fs_sb_info * sbi,struct free_nid * i,enum nid_state org_state,enum nid_state dst_state)2284 static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2285 enum nid_state org_state, enum nid_state dst_state)
2286 {
2287 struct f2fs_nm_info *nm_i = NM_I(sbi);
2288
2289 f2fs_bug_on(sbi, org_state != i->state);
2290 i->state = dst_state;
2291 nm_i->nid_cnt[org_state]--;
2292 nm_i->nid_cnt[dst_state]++;
2293
2294 switch (dst_state) {
2295 case PREALLOC_NID:
2296 list_del(&i->list);
2297 break;
2298 case FREE_NID:
2299 list_add_tail(&i->list, &nm_i->free_nid_list);
2300 break;
2301 default:
2302 BUG_ON(1);
2303 }
2304 }
2305
update_free_nid_bitmap(struct f2fs_sb_info * sbi,nid_t nid,bool set,bool build)2306 static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2307 bool set, bool build)
2308 {
2309 struct f2fs_nm_info *nm_i = NM_I(sbi);
2310 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2311 unsigned int nid_ofs = nid - START_NID(nid);
2312
2313 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2314 return;
2315
2316 if (set) {
2317 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2318 return;
2319 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2320 nm_i->free_nid_count[nat_ofs]++;
2321 } else {
2322 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2323 return;
2324 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2325 if (!build)
2326 nm_i->free_nid_count[nat_ofs]--;
2327 }
2328 }
2329
2330 /* return if the nid is recognized as free */
add_free_nid(struct f2fs_sb_info * sbi,nid_t nid,bool build,bool update)2331 static bool add_free_nid(struct f2fs_sb_info *sbi,
2332 nid_t nid, bool build, bool update)
2333 {
2334 struct f2fs_nm_info *nm_i = NM_I(sbi);
2335 struct free_nid *i, *e;
2336 struct nat_entry *ne;
2337 int err = -EINVAL;
2338 bool ret = false;
2339
2340 /* 0 nid should not be used */
2341 if (unlikely(nid == 0))
2342 return false;
2343
2344 if (unlikely(f2fs_check_nid_range(sbi, nid)))
2345 return false;
2346
2347 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS, true, NULL);
2348 i->nid = nid;
2349 i->state = FREE_NID;
2350
2351 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
2352
2353 spin_lock(&nm_i->nid_list_lock);
2354
2355 if (build) {
2356 /*
2357 * Thread A Thread B
2358 * - f2fs_create
2359 * - f2fs_new_inode
2360 * - f2fs_alloc_nid
2361 * - __insert_nid_to_list(PREALLOC_NID)
2362 * - f2fs_balance_fs_bg
2363 * - f2fs_build_free_nids
2364 * - __f2fs_build_free_nids
2365 * - scan_nat_page
2366 * - add_free_nid
2367 * - __lookup_nat_cache
2368 * - f2fs_add_link
2369 * - f2fs_init_inode_metadata
2370 * - f2fs_new_inode_page
2371 * - f2fs_new_node_page
2372 * - set_node_addr
2373 * - f2fs_alloc_nid_done
2374 * - __remove_nid_from_list(PREALLOC_NID)
2375 * - __insert_nid_to_list(FREE_NID)
2376 */
2377 ne = __lookup_nat_cache(nm_i, nid);
2378 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2379 nat_get_blkaddr(ne) != NULL_ADDR))
2380 goto err_out;
2381
2382 e = __lookup_free_nid_list(nm_i, nid);
2383 if (e) {
2384 if (e->state == FREE_NID)
2385 ret = true;
2386 goto err_out;
2387 }
2388 }
2389 ret = true;
2390 err = __insert_free_nid(sbi, i);
2391 err_out:
2392 if (update) {
2393 update_free_nid_bitmap(sbi, nid, ret, build);
2394 if (!build)
2395 nm_i->available_nids++;
2396 }
2397 spin_unlock(&nm_i->nid_list_lock);
2398 radix_tree_preload_end();
2399
2400 if (err)
2401 kmem_cache_free(free_nid_slab, i);
2402 return ret;
2403 }
2404
remove_free_nid(struct f2fs_sb_info * sbi,nid_t nid)2405 static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
2406 {
2407 struct f2fs_nm_info *nm_i = NM_I(sbi);
2408 struct free_nid *i;
2409 bool need_free = false;
2410
2411 spin_lock(&nm_i->nid_list_lock);
2412 i = __lookup_free_nid_list(nm_i, nid);
2413 if (i && i->state == FREE_NID) {
2414 __remove_free_nid(sbi, i, FREE_NID);
2415 need_free = true;
2416 }
2417 spin_unlock(&nm_i->nid_list_lock);
2418
2419 if (need_free)
2420 kmem_cache_free(free_nid_slab, i);
2421 }
2422
scan_nat_page(struct f2fs_sb_info * sbi,struct page * nat_page,nid_t start_nid)2423 static int scan_nat_page(struct f2fs_sb_info *sbi,
2424 struct page *nat_page, nid_t start_nid)
2425 {
2426 struct f2fs_nm_info *nm_i = NM_I(sbi);
2427 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2428 block_t blk_addr;
2429 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
2430 int i;
2431
2432 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
2433
2434 i = start_nid % NAT_ENTRY_PER_BLOCK;
2435
2436 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
2437 if (unlikely(start_nid >= nm_i->max_nid))
2438 break;
2439
2440 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
2441
2442 if (blk_addr == NEW_ADDR)
2443 return -EFSCORRUPTED;
2444
2445 if (blk_addr == NULL_ADDR) {
2446 add_free_nid(sbi, start_nid, true, true);
2447 } else {
2448 spin_lock(&NM_I(sbi)->nid_list_lock);
2449 update_free_nid_bitmap(sbi, start_nid, false, true);
2450 spin_unlock(&NM_I(sbi)->nid_list_lock);
2451 }
2452 }
2453
2454 return 0;
2455 }
2456
scan_curseg_cache(struct f2fs_sb_info * sbi)2457 static void scan_curseg_cache(struct f2fs_sb_info *sbi)
2458 {
2459 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2460 struct f2fs_journal *journal = curseg->journal;
2461 int i;
2462
2463 down_read(&curseg->journal_rwsem);
2464 for (i = 0; i < nats_in_cursum(journal); i++) {
2465 block_t addr;
2466 nid_t nid;
2467
2468 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2469 nid = le32_to_cpu(nid_in_journal(journal, i));
2470 if (addr == NULL_ADDR)
2471 add_free_nid(sbi, nid, true, false);
2472 else
2473 remove_free_nid(sbi, nid);
2474 }
2475 up_read(&curseg->journal_rwsem);
2476 }
2477
scan_free_nid_bits(struct f2fs_sb_info * sbi)2478 static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
2479 {
2480 struct f2fs_nm_info *nm_i = NM_I(sbi);
2481 unsigned int i, idx;
2482 nid_t nid;
2483
2484 f2fs_down_read(&nm_i->nat_tree_lock);
2485
2486 for (i = 0; i < nm_i->nat_blocks; i++) {
2487 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2488 continue;
2489 if (!nm_i->free_nid_count[i])
2490 continue;
2491 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
2492 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2493 NAT_ENTRY_PER_BLOCK, idx);
2494 if (idx >= NAT_ENTRY_PER_BLOCK)
2495 break;
2496
2497 nid = i * NAT_ENTRY_PER_BLOCK + idx;
2498 add_free_nid(sbi, nid, true, false);
2499
2500 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
2501 goto out;
2502 }
2503 }
2504 out:
2505 scan_curseg_cache(sbi);
2506
2507 f2fs_up_read(&nm_i->nat_tree_lock);
2508 }
2509
__f2fs_build_free_nids(struct f2fs_sb_info * sbi,bool sync,bool mount)2510 static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
2511 bool sync, bool mount)
2512 {
2513 struct f2fs_nm_info *nm_i = NM_I(sbi);
2514 int i = 0, ret;
2515 nid_t nid = nm_i->next_scan_nid;
2516
2517 if (unlikely(nid >= nm_i->max_nid))
2518 nid = 0;
2519
2520 if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
2521 nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
2522
2523 /* Enough entries */
2524 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2525 return 0;
2526
2527 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
2528 return 0;
2529
2530 if (!mount) {
2531 /* try to find free nids in free_nid_bitmap */
2532 scan_free_nid_bits(sbi);
2533
2534 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2535 return 0;
2536 }
2537
2538 /* readahead nat pages to be scanned */
2539 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
2540 META_NAT, true);
2541
2542 f2fs_down_read(&nm_i->nat_tree_lock);
2543
2544 while (1) {
2545 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2546 nm_i->nat_block_bitmap)) {
2547 struct page *page = get_current_nat_page(sbi, nid);
2548
2549 if (IS_ERR(page)) {
2550 ret = PTR_ERR(page);
2551 } else {
2552 ret = scan_nat_page(sbi, page, nid);
2553 f2fs_put_page(page, 1);
2554 }
2555
2556 if (ret) {
2557 f2fs_up_read(&nm_i->nat_tree_lock);
2558
2559 if (ret == -EFSCORRUPTED) {
2560 f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
2561 set_sbi_flag(sbi, SBI_NEED_FSCK);
2562 f2fs_handle_error(sbi,
2563 ERROR_INCONSISTENT_NAT);
2564 }
2565
2566 return ret;
2567 }
2568 }
2569
2570 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
2571 if (unlikely(nid >= nm_i->max_nid))
2572 nid = 0;
2573
2574 if (++i >= FREE_NID_PAGES)
2575 break;
2576 }
2577
2578 /* go to the next free nat pages to find free nids abundantly */
2579 nm_i->next_scan_nid = nid;
2580
2581 /* find free nids from current sum_pages */
2582 scan_curseg_cache(sbi);
2583
2584 f2fs_up_read(&nm_i->nat_tree_lock);
2585
2586 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
2587 nm_i->ra_nid_pages, META_NAT, false);
2588
2589 return 0;
2590 }
2591
f2fs_build_free_nids(struct f2fs_sb_info * sbi,bool sync,bool mount)2592 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
2593 {
2594 int ret;
2595
2596 mutex_lock(&NM_I(sbi)->build_lock);
2597 ret = __f2fs_build_free_nids(sbi, sync, mount);
2598 mutex_unlock(&NM_I(sbi)->build_lock);
2599
2600 return ret;
2601 }
2602
2603 /*
2604 * If this function returns success, caller can obtain a new nid
2605 * from second parameter of this function.
2606 * The returned nid could be used ino as well as nid when inode is created.
2607 */
f2fs_alloc_nid(struct f2fs_sb_info * sbi,nid_t * nid)2608 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
2609 {
2610 struct f2fs_nm_info *nm_i = NM_I(sbi);
2611 struct free_nid *i = NULL;
2612 retry:
2613 if (time_to_inject(sbi, FAULT_ALLOC_NID))
2614 return false;
2615
2616 spin_lock(&nm_i->nid_list_lock);
2617
2618 if (unlikely(nm_i->available_nids == 0)) {
2619 spin_unlock(&nm_i->nid_list_lock);
2620 return false;
2621 }
2622
2623 /* We should not use stale free nids created by f2fs_build_free_nids */
2624 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
2625 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2626 i = list_first_entry(&nm_i->free_nid_list,
2627 struct free_nid, list);
2628 *nid = i->nid;
2629
2630 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
2631 nm_i->available_nids--;
2632
2633 update_free_nid_bitmap(sbi, *nid, false, false);
2634
2635 spin_unlock(&nm_i->nid_list_lock);
2636 return true;
2637 }
2638 spin_unlock(&nm_i->nid_list_lock);
2639
2640 /* Let's scan nat pages and its caches to get free nids */
2641 if (!f2fs_build_free_nids(sbi, true, false))
2642 goto retry;
2643 return false;
2644 }
2645
2646 /*
2647 * f2fs_alloc_nid() should be called prior to this function.
2648 */
f2fs_alloc_nid_done(struct f2fs_sb_info * sbi,nid_t nid)2649 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
2650 {
2651 struct f2fs_nm_info *nm_i = NM_I(sbi);
2652 struct free_nid *i;
2653
2654 spin_lock(&nm_i->nid_list_lock);
2655 i = __lookup_free_nid_list(nm_i, nid);
2656 f2fs_bug_on(sbi, !i);
2657 __remove_free_nid(sbi, i, PREALLOC_NID);
2658 spin_unlock(&nm_i->nid_list_lock);
2659
2660 kmem_cache_free(free_nid_slab, i);
2661 }
2662
2663 /*
2664 * f2fs_alloc_nid() should be called prior to this function.
2665 */
f2fs_alloc_nid_failed(struct f2fs_sb_info * sbi,nid_t nid)2666 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
2667 {
2668 struct f2fs_nm_info *nm_i = NM_I(sbi);
2669 struct free_nid *i;
2670 bool need_free = false;
2671
2672 if (!nid)
2673 return;
2674
2675 spin_lock(&nm_i->nid_list_lock);
2676 i = __lookup_free_nid_list(nm_i, nid);
2677 f2fs_bug_on(sbi, !i);
2678
2679 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
2680 __remove_free_nid(sbi, i, PREALLOC_NID);
2681 need_free = true;
2682 } else {
2683 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
2684 }
2685
2686 nm_i->available_nids++;
2687
2688 update_free_nid_bitmap(sbi, nid, true, false);
2689
2690 spin_unlock(&nm_i->nid_list_lock);
2691
2692 if (need_free)
2693 kmem_cache_free(free_nid_slab, i);
2694 }
2695
f2fs_try_to_free_nids(struct f2fs_sb_info * sbi,int nr_shrink)2696 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
2697 {
2698 struct f2fs_nm_info *nm_i = NM_I(sbi);
2699 int nr = nr_shrink;
2700
2701 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2702 return 0;
2703
2704 if (!mutex_trylock(&nm_i->build_lock))
2705 return 0;
2706
2707 while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) {
2708 struct free_nid *i, *next;
2709 unsigned int batch = SHRINK_NID_BATCH_SIZE;
2710
2711 spin_lock(&nm_i->nid_list_lock);
2712 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
2713 if (!nr_shrink || !batch ||
2714 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2715 break;
2716 __remove_free_nid(sbi, i, FREE_NID);
2717 kmem_cache_free(free_nid_slab, i);
2718 nr_shrink--;
2719 batch--;
2720 }
2721 spin_unlock(&nm_i->nid_list_lock);
2722 }
2723
2724 mutex_unlock(&nm_i->build_lock);
2725
2726 return nr - nr_shrink;
2727 }
2728
f2fs_recover_inline_xattr(struct inode * inode,struct page * page)2729 int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
2730 {
2731 void *src_addr, *dst_addr;
2732 size_t inline_size;
2733 struct page *ipage;
2734 struct f2fs_inode *ri;
2735
2736 ipage = f2fs_get_inode_page(F2FS_I_SB(inode), inode->i_ino);
2737 if (IS_ERR(ipage))
2738 return PTR_ERR(ipage);
2739
2740 ri = F2FS_INODE(page);
2741 if (ri->i_inline & F2FS_INLINE_XATTR) {
2742 if (!f2fs_has_inline_xattr(inode)) {
2743 set_inode_flag(inode, FI_INLINE_XATTR);
2744 stat_inc_inline_xattr(inode);
2745 }
2746 } else {
2747 if (f2fs_has_inline_xattr(inode)) {
2748 stat_dec_inline_xattr(inode);
2749 clear_inode_flag(inode, FI_INLINE_XATTR);
2750 }
2751 goto update_inode;
2752 }
2753
2754 dst_addr = inline_xattr_addr(inode, ipage);
2755 src_addr = inline_xattr_addr(inode, page);
2756 inline_size = inline_xattr_size(inode);
2757
2758 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
2759 memcpy(dst_addr, src_addr, inline_size);
2760 update_inode:
2761 f2fs_update_inode(inode, ipage);
2762 f2fs_put_page(ipage, 1);
2763 return 0;
2764 }
2765
f2fs_recover_xattr_data(struct inode * inode,struct page * page)2766 int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
2767 {
2768 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2769 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
2770 nid_t new_xnid;
2771 struct dnode_of_data dn;
2772 struct node_info ni;
2773 struct page *xpage;
2774 int err;
2775
2776 if (!prev_xnid)
2777 goto recover_xnid;
2778
2779 /* 1: invalidate the previous xattr nid */
2780 err = f2fs_get_node_info(sbi, prev_xnid, &ni, false);
2781 if (err)
2782 return err;
2783
2784 f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
2785 dec_valid_node_count(sbi, inode, false);
2786 set_node_addr(sbi, &ni, NULL_ADDR, false);
2787
2788 recover_xnid:
2789 /* 2: update xattr nid in inode */
2790 if (!f2fs_alloc_nid(sbi, &new_xnid))
2791 return -ENOSPC;
2792
2793 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
2794 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
2795 if (IS_ERR(xpage)) {
2796 f2fs_alloc_nid_failed(sbi, new_xnid);
2797 return PTR_ERR(xpage);
2798 }
2799
2800 f2fs_alloc_nid_done(sbi, new_xnid);
2801 f2fs_update_inode_page(inode);
2802
2803 /* 3: update and set xattr node page dirty */
2804 if (page) {
2805 memcpy(F2FS_NODE(xpage), F2FS_NODE(page),
2806 VALID_XATTR_BLOCK_SIZE);
2807 set_page_dirty(xpage);
2808 }
2809 f2fs_put_page(xpage, 1);
2810
2811 return 0;
2812 }
2813
f2fs_recover_inode_page(struct f2fs_sb_info * sbi,struct page * page)2814 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
2815 {
2816 struct f2fs_inode *src, *dst;
2817 nid_t ino = ino_of_node(page);
2818 struct node_info old_ni, new_ni;
2819 struct page *ipage;
2820 int err;
2821
2822 err = f2fs_get_node_info(sbi, ino, &old_ni, false);
2823 if (err)
2824 return err;
2825
2826 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2827 return -EINVAL;
2828 retry:
2829 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
2830 if (!ipage) {
2831 memalloc_retry_wait(GFP_NOFS);
2832 goto retry;
2833 }
2834
2835 /* Should not use this inode from free nid list */
2836 remove_free_nid(sbi, ino);
2837
2838 if (!PageUptodate(ipage))
2839 SetPageUptodate(ipage);
2840 fill_node_footer(ipage, ino, ino, 0, true);
2841 set_cold_node(ipage, false);
2842
2843 src = F2FS_INODE(page);
2844 dst = F2FS_INODE(ipage);
2845
2846 memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
2847 dst->i_size = 0;
2848 dst->i_blocks = cpu_to_le64(1);
2849 dst->i_links = cpu_to_le32(1);
2850 dst->i_xattr_nid = 0;
2851 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
2852 if (dst->i_inline & F2FS_EXTRA_ATTR) {
2853 dst->i_extra_isize = src->i_extra_isize;
2854
2855 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
2856 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2857 i_inline_xattr_size))
2858 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2859
2860 if (f2fs_sb_has_project_quota(sbi) &&
2861 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2862 i_projid))
2863 dst->i_projid = src->i_projid;
2864
2865 if (f2fs_sb_has_inode_crtime(sbi) &&
2866 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2867 i_crtime_nsec)) {
2868 dst->i_crtime = src->i_crtime;
2869 dst->i_crtime_nsec = src->i_crtime_nsec;
2870 }
2871 }
2872
2873 new_ni = old_ni;
2874 new_ni.ino = ino;
2875
2876 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
2877 WARN_ON(1);
2878 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
2879 inc_valid_inode_count(sbi);
2880 set_page_dirty(ipage);
2881 f2fs_put_page(ipage, 1);
2882 return 0;
2883 }
2884
f2fs_restore_node_summary(struct f2fs_sb_info * sbi,unsigned int segno,struct f2fs_summary_block * sum)2885 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2886 unsigned int segno, struct f2fs_summary_block *sum)
2887 {
2888 struct f2fs_node *rn;
2889 struct f2fs_summary *sum_entry;
2890 block_t addr;
2891 int i, idx, last_offset, nrpages;
2892
2893 /* scan the node segment */
2894 last_offset = BLKS_PER_SEG(sbi);
2895 addr = START_BLOCK(sbi, segno);
2896 sum_entry = &sum->entries[0];
2897
2898 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
2899 nrpages = bio_max_segs(last_offset - i);
2900
2901 /* readahead node pages */
2902 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
2903
2904 for (idx = addr; idx < addr + nrpages; idx++) {
2905 struct page *page = f2fs_get_tmp_page(sbi, idx);
2906
2907 if (IS_ERR(page))
2908 return PTR_ERR(page);
2909
2910 rn = F2FS_NODE(page);
2911 sum_entry->nid = rn->footer.nid;
2912 sum_entry->version = 0;
2913 sum_entry->ofs_in_node = 0;
2914 sum_entry++;
2915 f2fs_put_page(page, 1);
2916 }
2917
2918 invalidate_mapping_pages(META_MAPPING(sbi), addr,
2919 addr + nrpages);
2920 }
2921 return 0;
2922 }
2923
remove_nats_in_journal(struct f2fs_sb_info * sbi)2924 static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
2925 {
2926 struct f2fs_nm_info *nm_i = NM_I(sbi);
2927 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2928 struct f2fs_journal *journal = curseg->journal;
2929 int i;
2930
2931 down_write(&curseg->journal_rwsem);
2932 for (i = 0; i < nats_in_cursum(journal); i++) {
2933 struct nat_entry *ne;
2934 struct f2fs_nat_entry raw_ne;
2935 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
2936
2937 if (f2fs_check_nid_range(sbi, nid))
2938 continue;
2939
2940 raw_ne = nat_in_journal(journal, i);
2941
2942 ne = __lookup_nat_cache(nm_i, nid);
2943 if (!ne) {
2944 ne = __alloc_nat_entry(sbi, nid, true);
2945 __init_nat_entry(nm_i, ne, &raw_ne, true);
2946 }
2947
2948 /*
2949 * if a free nat in journal has not been used after last
2950 * checkpoint, we should remove it from available nids,
2951 * since later we will add it again.
2952 */
2953 if (!get_nat_flag(ne, IS_DIRTY) &&
2954 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2955 spin_lock(&nm_i->nid_list_lock);
2956 nm_i->available_nids--;
2957 spin_unlock(&nm_i->nid_list_lock);
2958 }
2959
2960 __set_nat_cache_dirty(nm_i, ne);
2961 }
2962 update_nats_in_cursum(journal, -i);
2963 up_write(&curseg->journal_rwsem);
2964 }
2965
__adjust_nat_entry_set(struct nat_entry_set * nes,struct list_head * head,int max)2966 static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2967 struct list_head *head, int max)
2968 {
2969 struct nat_entry_set *cur;
2970
2971 if (nes->entry_cnt >= max)
2972 goto add_out;
2973
2974 list_for_each_entry(cur, head, set_list) {
2975 if (cur->entry_cnt >= nes->entry_cnt) {
2976 list_add(&nes->set_list, cur->set_list.prev);
2977 return;
2978 }
2979 }
2980 add_out:
2981 list_add_tail(&nes->set_list, head);
2982 }
2983
__update_nat_bits(struct f2fs_sb_info * sbi,nid_t start_nid,struct page * page)2984 static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
2985 struct page *page)
2986 {
2987 struct f2fs_nm_info *nm_i = NM_I(sbi);
2988 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2989 struct f2fs_nat_block *nat_blk = page_address(page);
2990 int valid = 0;
2991 int i = 0;
2992
2993 if (!enabled_nat_bits(sbi, NULL))
2994 return;
2995
2996 if (nat_index == 0) {
2997 valid = 1;
2998 i = 1;
2999 }
3000 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
3001 if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
3002 valid++;
3003 }
3004 if (valid == 0) {
3005 __set_bit_le(nat_index, nm_i->empty_nat_bits);
3006 __clear_bit_le(nat_index, nm_i->full_nat_bits);
3007 return;
3008 }
3009
3010 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
3011 if (valid == NAT_ENTRY_PER_BLOCK)
3012 __set_bit_le(nat_index, nm_i->full_nat_bits);
3013 else
3014 __clear_bit_le(nat_index, nm_i->full_nat_bits);
3015 }
3016
__flush_nat_entry_set(struct f2fs_sb_info * sbi,struct nat_entry_set * set,struct cp_control * cpc)3017 static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
3018 struct nat_entry_set *set, struct cp_control *cpc)
3019 {
3020 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3021 struct f2fs_journal *journal = curseg->journal;
3022 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
3023 bool to_journal = true;
3024 struct f2fs_nat_block *nat_blk;
3025 struct nat_entry *ne, *cur;
3026 struct page *page = NULL;
3027
3028 /*
3029 * there are two steps to flush nat entries:
3030 * #1, flush nat entries to journal in current hot data summary block.
3031 * #2, flush nat entries to nat page.
3032 */
3033 if (enabled_nat_bits(sbi, cpc) ||
3034 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
3035 to_journal = false;
3036
3037 if (to_journal) {
3038 down_write(&curseg->journal_rwsem);
3039 } else {
3040 page = get_next_nat_page(sbi, start_nid);
3041 if (IS_ERR(page))
3042 return PTR_ERR(page);
3043
3044 nat_blk = page_address(page);
3045 f2fs_bug_on(sbi, !nat_blk);
3046 }
3047
3048 /* flush dirty nats in nat entry set */
3049 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
3050 struct f2fs_nat_entry *raw_ne;
3051 nid_t nid = nat_get_nid(ne);
3052 int offset;
3053
3054 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
3055
3056 if (to_journal) {
3057 offset = f2fs_lookup_journal_in_cursum(journal,
3058 NAT_JOURNAL, nid, 1);
3059 f2fs_bug_on(sbi, offset < 0);
3060 raw_ne = &nat_in_journal(journal, offset);
3061 nid_in_journal(journal, offset) = cpu_to_le32(nid);
3062 } else {
3063 raw_ne = &nat_blk->entries[nid - start_nid];
3064 }
3065 raw_nat_from_node_info(raw_ne, &ne->ni);
3066 nat_reset_flag(ne);
3067 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
3068 if (nat_get_blkaddr(ne) == NULL_ADDR) {
3069 add_free_nid(sbi, nid, false, true);
3070 } else {
3071 spin_lock(&NM_I(sbi)->nid_list_lock);
3072 update_free_nid_bitmap(sbi, nid, false, false);
3073 spin_unlock(&NM_I(sbi)->nid_list_lock);
3074 }
3075 }
3076
3077 if (to_journal) {
3078 up_write(&curseg->journal_rwsem);
3079 } else {
3080 __update_nat_bits(sbi, start_nid, page);
3081 f2fs_put_page(page, 1);
3082 }
3083
3084 /* Allow dirty nats by node block allocation in write_begin */
3085 if (!set->entry_cnt) {
3086 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
3087 kmem_cache_free(nat_entry_set_slab, set);
3088 }
3089 return 0;
3090 }
3091
3092 /*
3093 * This function is called during the checkpointing process.
3094 */
f2fs_flush_nat_entries(struct f2fs_sb_info * sbi,struct cp_control * cpc)3095 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
3096 {
3097 struct f2fs_nm_info *nm_i = NM_I(sbi);
3098 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3099 struct f2fs_journal *journal = curseg->journal;
3100 struct nat_entry_set *setvec[NAT_VEC_SIZE];
3101 struct nat_entry_set *set, *tmp;
3102 unsigned int found;
3103 nid_t set_idx = 0;
3104 LIST_HEAD(sets);
3105 int err = 0;
3106
3107 /*
3108 * during unmount, let's flush nat_bits before checking
3109 * nat_cnt[DIRTY_NAT].
3110 */
3111 if (enabled_nat_bits(sbi, cpc)) {
3112 f2fs_down_write(&nm_i->nat_tree_lock);
3113 remove_nats_in_journal(sbi);
3114 f2fs_up_write(&nm_i->nat_tree_lock);
3115 }
3116
3117 if (!nm_i->nat_cnt[DIRTY_NAT])
3118 return 0;
3119
3120 f2fs_down_write(&nm_i->nat_tree_lock);
3121
3122 /*
3123 * if there are no enough space in journal to store dirty nat
3124 * entries, remove all entries from journal and merge them
3125 * into nat entry set.
3126 */
3127 if (enabled_nat_bits(sbi, cpc) ||
3128 !__has_cursum_space(journal,
3129 nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
3130 remove_nats_in_journal(sbi);
3131
3132 while ((found = __gang_lookup_nat_set(nm_i,
3133 set_idx, NAT_VEC_SIZE, setvec))) {
3134 unsigned idx;
3135
3136 set_idx = setvec[found - 1]->set + 1;
3137 for (idx = 0; idx < found; idx++)
3138 __adjust_nat_entry_set(setvec[idx], &sets,
3139 MAX_NAT_JENTRIES(journal));
3140 }
3141
3142 /* flush dirty nats in nat entry set */
3143 list_for_each_entry_safe(set, tmp, &sets, set_list) {
3144 err = __flush_nat_entry_set(sbi, set, cpc);
3145 if (err)
3146 break;
3147 }
3148
3149 f2fs_up_write(&nm_i->nat_tree_lock);
3150 /* Allow dirty nats by node block allocation in write_begin */
3151
3152 return err;
3153 }
3154
__get_nat_bitmaps(struct f2fs_sb_info * sbi)3155 static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
3156 {
3157 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3158 struct f2fs_nm_info *nm_i = NM_I(sbi);
3159 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
3160 unsigned int i;
3161 __u64 cp_ver = cur_cp_version(ckpt);
3162 block_t nat_bits_addr;
3163
3164 if (!enabled_nat_bits(sbi, NULL))
3165 return 0;
3166
3167 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3168 nm_i->nat_bits = f2fs_kvzalloc(sbi,
3169 F2FS_BLK_TO_BYTES(nm_i->nat_bits_blocks), GFP_KERNEL);
3170 if (!nm_i->nat_bits)
3171 return -ENOMEM;
3172
3173 nat_bits_addr = __start_cp_addr(sbi) + BLKS_PER_SEG(sbi) -
3174 nm_i->nat_bits_blocks;
3175 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
3176 struct page *page;
3177
3178 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
3179 if (IS_ERR(page))
3180 return PTR_ERR(page);
3181
3182 memcpy(nm_i->nat_bits + F2FS_BLK_TO_BYTES(i),
3183 page_address(page), F2FS_BLKSIZE);
3184 f2fs_put_page(page, 1);
3185 }
3186
3187 cp_ver |= (cur_cp_crc(ckpt) << 32);
3188 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
3189 disable_nat_bits(sbi, true);
3190 return 0;
3191 }
3192
3193 nm_i->full_nat_bits = nm_i->nat_bits + 8;
3194 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
3195
3196 f2fs_notice(sbi, "Found nat_bits in checkpoint");
3197 return 0;
3198 }
3199
load_free_nid_bitmap(struct f2fs_sb_info * sbi)3200 static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
3201 {
3202 struct f2fs_nm_info *nm_i = NM_I(sbi);
3203 unsigned int i = 0;
3204 nid_t nid, last_nid;
3205
3206 if (!enabled_nat_bits(sbi, NULL))
3207 return;
3208
3209 for (i = 0; i < nm_i->nat_blocks; i++) {
3210 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
3211 if (i >= nm_i->nat_blocks)
3212 break;
3213
3214 __set_bit_le(i, nm_i->nat_block_bitmap);
3215
3216 nid = i * NAT_ENTRY_PER_BLOCK;
3217 last_nid = nid + NAT_ENTRY_PER_BLOCK;
3218
3219 spin_lock(&NM_I(sbi)->nid_list_lock);
3220 for (; nid < last_nid; nid++)
3221 update_free_nid_bitmap(sbi, nid, true, true);
3222 spin_unlock(&NM_I(sbi)->nid_list_lock);
3223 }
3224
3225 for (i = 0; i < nm_i->nat_blocks; i++) {
3226 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
3227 if (i >= nm_i->nat_blocks)
3228 break;
3229
3230 __set_bit_le(i, nm_i->nat_block_bitmap);
3231 }
3232 }
3233
init_node_manager(struct f2fs_sb_info * sbi)3234 static int init_node_manager(struct f2fs_sb_info *sbi)
3235 {
3236 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
3237 struct f2fs_nm_info *nm_i = NM_I(sbi);
3238 unsigned char *version_bitmap;
3239 unsigned int nat_segs;
3240 int err;
3241
3242 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
3243
3244 /* segment_count_nat includes pair segment so divide to 2. */
3245 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
3246 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
3247 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
3248
3249 /* not used nids: 0, node, meta, (and root counted as valid node) */
3250 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
3251 F2FS_RESERVED_NODE_NUM;
3252 nm_i->nid_cnt[FREE_NID] = 0;
3253 nm_i->nid_cnt[PREALLOC_NID] = 0;
3254 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
3255 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
3256 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
3257 nm_i->max_rf_node_blocks = DEF_RF_NODE_BLOCKS;
3258
3259 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
3260 INIT_LIST_HEAD(&nm_i->free_nid_list);
3261 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
3262 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
3263 INIT_LIST_HEAD(&nm_i->nat_entries);
3264 spin_lock_init(&nm_i->nat_list_lock);
3265
3266 mutex_init(&nm_i->build_lock);
3267 spin_lock_init(&nm_i->nid_list_lock);
3268 init_f2fs_rwsem(&nm_i->nat_tree_lock);
3269
3270 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
3271 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
3272 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
3273 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
3274 GFP_KERNEL);
3275 if (!nm_i->nat_bitmap)
3276 return -ENOMEM;
3277
3278 if (!test_opt(sbi, NAT_BITS))
3279 disable_nat_bits(sbi, true);
3280
3281 err = __get_nat_bitmaps(sbi);
3282 if (err)
3283 return err;
3284
3285 #ifdef CONFIG_F2FS_CHECK_FS
3286 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
3287 GFP_KERNEL);
3288 if (!nm_i->nat_bitmap_mir)
3289 return -ENOMEM;
3290 #endif
3291
3292 return 0;
3293 }
3294
init_free_nid_cache(struct f2fs_sb_info * sbi)3295 static int init_free_nid_cache(struct f2fs_sb_info *sbi)
3296 {
3297 struct f2fs_nm_info *nm_i = NM_I(sbi);
3298 int i;
3299
3300 nm_i->free_nid_bitmap =
3301 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *),
3302 nm_i->nat_blocks),
3303 GFP_KERNEL);
3304 if (!nm_i->free_nid_bitmap)
3305 return -ENOMEM;
3306
3307 for (i = 0; i < nm_i->nat_blocks; i++) {
3308 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
3309 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
3310 if (!nm_i->free_nid_bitmap[i])
3311 return -ENOMEM;
3312 }
3313
3314 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
3315 GFP_KERNEL);
3316 if (!nm_i->nat_block_bitmap)
3317 return -ENOMEM;
3318
3319 nm_i->free_nid_count =
3320 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
3321 nm_i->nat_blocks),
3322 GFP_KERNEL);
3323 if (!nm_i->free_nid_count)
3324 return -ENOMEM;
3325 return 0;
3326 }
3327
f2fs_build_node_manager(struct f2fs_sb_info * sbi)3328 int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
3329 {
3330 int err;
3331
3332 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3333 GFP_KERNEL);
3334 if (!sbi->nm_info)
3335 return -ENOMEM;
3336
3337 err = init_node_manager(sbi);
3338 if (err)
3339 return err;
3340
3341 err = init_free_nid_cache(sbi);
3342 if (err)
3343 return err;
3344
3345 /* load free nid status from nat_bits table */
3346 load_free_nid_bitmap(sbi);
3347
3348 return f2fs_build_free_nids(sbi, true, true);
3349 }
3350
f2fs_destroy_node_manager(struct f2fs_sb_info * sbi)3351 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
3352 {
3353 struct f2fs_nm_info *nm_i = NM_I(sbi);
3354 struct free_nid *i, *next_i;
3355 void *vec[NAT_VEC_SIZE];
3356 struct nat_entry **natvec = (struct nat_entry **)vec;
3357 struct nat_entry_set **setvec = (struct nat_entry_set **)vec;
3358 nid_t nid = 0;
3359 unsigned int found;
3360
3361 if (!nm_i)
3362 return;
3363
3364 /* destroy free nid list */
3365 spin_lock(&nm_i->nid_list_lock);
3366 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
3367 __remove_free_nid(sbi, i, FREE_NID);
3368 spin_unlock(&nm_i->nid_list_lock);
3369 kmem_cache_free(free_nid_slab, i);
3370 spin_lock(&nm_i->nid_list_lock);
3371 }
3372 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3373 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3374 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
3375 spin_unlock(&nm_i->nid_list_lock);
3376
3377 /* destroy nat cache */
3378 f2fs_down_write(&nm_i->nat_tree_lock);
3379 while ((found = __gang_lookup_nat_cache(nm_i,
3380 nid, NAT_VEC_SIZE, natvec))) {
3381 unsigned idx;
3382
3383 nid = nat_get_nid(natvec[found - 1]) + 1;
3384 for (idx = 0; idx < found; idx++) {
3385 spin_lock(&nm_i->nat_list_lock);
3386 list_del(&natvec[idx]->list);
3387 spin_unlock(&nm_i->nat_list_lock);
3388
3389 __del_from_nat_cache(nm_i, natvec[idx]);
3390 }
3391 }
3392 f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
3393
3394 /* destroy nat set cache */
3395 nid = 0;
3396 memset(vec, 0, sizeof(void *) * NAT_VEC_SIZE);
3397 while ((found = __gang_lookup_nat_set(nm_i,
3398 nid, NAT_VEC_SIZE, setvec))) {
3399 unsigned idx;
3400
3401 nid = setvec[found - 1]->set + 1;
3402 for (idx = 0; idx < found; idx++) {
3403 /* entry_cnt is not zero, when cp_error was occurred */
3404 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3405 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3406 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3407 }
3408 }
3409 f2fs_up_write(&nm_i->nat_tree_lock);
3410
3411 kvfree(nm_i->nat_block_bitmap);
3412 if (nm_i->free_nid_bitmap) {
3413 int i;
3414
3415 for (i = 0; i < nm_i->nat_blocks; i++)
3416 kvfree(nm_i->free_nid_bitmap[i]);
3417 kvfree(nm_i->free_nid_bitmap);
3418 }
3419 kvfree(nm_i->free_nid_count);
3420
3421 kvfree(nm_i->nat_bitmap);
3422 kvfree(nm_i->nat_bits);
3423 #ifdef CONFIG_F2FS_CHECK_FS
3424 kvfree(nm_i->nat_bitmap_mir);
3425 #endif
3426 sbi->nm_info = NULL;
3427 kfree(nm_i);
3428 }
3429
f2fs_create_node_manager_caches(void)3430 int __init f2fs_create_node_manager_caches(void)
3431 {
3432 nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry",
3433 sizeof(struct nat_entry));
3434 if (!nat_entry_slab)
3435 goto fail;
3436
3437 free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid",
3438 sizeof(struct free_nid));
3439 if (!free_nid_slab)
3440 goto destroy_nat_entry;
3441
3442 nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set",
3443 sizeof(struct nat_entry_set));
3444 if (!nat_entry_set_slab)
3445 goto destroy_free_nid;
3446
3447 fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry",
3448 sizeof(struct fsync_node_entry));
3449 if (!fsync_node_entry_slab)
3450 goto destroy_nat_entry_set;
3451 return 0;
3452
3453 destroy_nat_entry_set:
3454 kmem_cache_destroy(nat_entry_set_slab);
3455 destroy_free_nid:
3456 kmem_cache_destroy(free_nid_slab);
3457 destroy_nat_entry:
3458 kmem_cache_destroy(nat_entry_slab);
3459 fail:
3460 return -ENOMEM;
3461 }
3462
f2fs_destroy_node_manager_caches(void)3463 void f2fs_destroy_node_manager_caches(void)
3464 {
3465 kmem_cache_destroy(fsync_node_entry_slab);
3466 kmem_cache_destroy(nat_entry_set_slab);
3467 kmem_cache_destroy(free_nid_slab);
3468 kmem_cache_destroy(nat_entry_slab);
3469 }
3470