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] && nids[i] == dn->inode->i_ino) {
804 err = -EFSCORRUPTED;
805 f2fs_err_ratelimited(sbi,
806 "inode mapping table is corrupted, run fsck to fix it, "
807 "ino:%lu, nid:%u, level:%d, offset:%d",
808 dn->inode->i_ino, nids[i], level, offset[level]);
809 set_sbi_flag(sbi, SBI_NEED_FSCK);
810 goto release_pages;
811 }
812
813 if (!nids[i] && mode == ALLOC_NODE) {
814 /* alloc new node */
815 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
816 err = -ENOSPC;
817 goto release_pages;
818 }
819
820 dn->nid = nids[i];
821 npage[i] = f2fs_new_node_page(dn, noffset[i]);
822 if (IS_ERR(npage[i])) {
823 f2fs_alloc_nid_failed(sbi, nids[i]);
824 err = PTR_ERR(npage[i]);
825 goto release_pages;
826 }
827
828 set_nid(parent, offset[i - 1], nids[i], i == 1);
829 f2fs_alloc_nid_done(sbi, nids[i]);
830 done = true;
831 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
832 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
833 if (IS_ERR(npage[i])) {
834 err = PTR_ERR(npage[i]);
835 goto release_pages;
836 }
837 done = true;
838 }
839 if (i == 1) {
840 dn->inode_page_locked = false;
841 unlock_page(parent);
842 } else {
843 f2fs_put_page(parent, 1);
844 }
845
846 if (!done) {
847 npage[i] = f2fs_get_node_page(sbi, nids[i]);
848 if (IS_ERR(npage[i])) {
849 err = PTR_ERR(npage[i]);
850 f2fs_put_page(npage[0], 0);
851 goto release_out;
852 }
853 }
854 if (i < level) {
855 parent = npage[i];
856 nids[i + 1] = get_nid(parent, offset[i], false);
857 }
858 }
859 dn->nid = nids[level];
860 dn->ofs_in_node = offset[level];
861 dn->node_page = npage[level];
862 dn->data_blkaddr = f2fs_data_blkaddr(dn);
863
864 if (is_inode_flag_set(dn->inode, FI_COMPRESSED_FILE) &&
865 f2fs_sb_has_readonly(sbi)) {
866 unsigned int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
867 unsigned int ofs_in_node = dn->ofs_in_node;
868 pgoff_t fofs = index;
869 unsigned int c_len;
870 block_t blkaddr;
871
872 /* should align fofs and ofs_in_node to cluster_size */
873 if (fofs % cluster_size) {
874 fofs = round_down(fofs, cluster_size);
875 ofs_in_node = round_down(ofs_in_node, cluster_size);
876 }
877
878 c_len = f2fs_cluster_blocks_are_contiguous(dn, ofs_in_node);
879 if (!c_len)
880 goto out;
881
882 blkaddr = data_blkaddr(dn->inode, dn->node_page, ofs_in_node);
883 if (blkaddr == COMPRESS_ADDR)
884 blkaddr = data_blkaddr(dn->inode, dn->node_page,
885 ofs_in_node + 1);
886
887 f2fs_update_read_extent_tree_range_compressed(dn->inode,
888 fofs, blkaddr, cluster_size, c_len);
889 }
890 out:
891 return 0;
892
893 release_pages:
894 f2fs_put_page(parent, 1);
895 if (i > 1)
896 f2fs_put_page(npage[0], 0);
897 release_out:
898 dn->inode_page = NULL;
899 dn->node_page = NULL;
900 if (err == -ENOENT) {
901 dn->cur_level = i;
902 dn->max_level = level;
903 dn->ofs_in_node = offset[level];
904 }
905 return err;
906 }
907
truncate_node(struct dnode_of_data * dn)908 static int truncate_node(struct dnode_of_data *dn)
909 {
910 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
911 struct node_info ni;
912 int err;
913 pgoff_t index;
914
915 err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
916 if (err)
917 return err;
918
919 if (ni.blk_addr != NEW_ADDR &&
920 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC_ENHANCE)) {
921 f2fs_err_ratelimited(sbi,
922 "nat entry is corrupted, run fsck to fix it, ino:%u, "
923 "nid:%u, blkaddr:%u", ni.ino, ni.nid, ni.blk_addr);
924 set_sbi_flag(sbi, SBI_NEED_FSCK);
925 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NAT);
926 return -EFSCORRUPTED;
927 }
928
929 /* Deallocate node address */
930 f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
931 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
932 set_node_addr(sbi, &ni, NULL_ADDR, false);
933
934 if (dn->nid == dn->inode->i_ino) {
935 f2fs_remove_orphan_inode(sbi, dn->nid);
936 dec_valid_inode_count(sbi);
937 f2fs_inode_synced(dn->inode);
938 }
939
940 clear_node_page_dirty(dn->node_page);
941 set_sbi_flag(sbi, SBI_IS_DIRTY);
942
943 index = page_folio(dn->node_page)->index;
944 f2fs_put_page(dn->node_page, 1);
945
946 invalidate_mapping_pages(NODE_MAPPING(sbi),
947 index, index);
948
949 dn->node_page = NULL;
950 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
951
952 return 0;
953 }
954
truncate_dnode(struct dnode_of_data * dn)955 static int truncate_dnode(struct dnode_of_data *dn)
956 {
957 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
958 struct page *page;
959 int err;
960
961 if (dn->nid == 0)
962 return 1;
963
964 /* get direct node */
965 page = f2fs_get_node_page(sbi, dn->nid);
966 if (PTR_ERR(page) == -ENOENT)
967 return 1;
968 else if (IS_ERR(page))
969 return PTR_ERR(page);
970
971 if (IS_INODE(page) || ino_of_node(page) != dn->inode->i_ino) {
972 f2fs_err(sbi, "incorrect node reference, ino: %lu, nid: %u, ino_of_node: %u",
973 dn->inode->i_ino, dn->nid, ino_of_node(page));
974 set_sbi_flag(sbi, SBI_NEED_FSCK);
975 f2fs_handle_error(sbi, ERROR_INVALID_NODE_REFERENCE);
976 f2fs_put_page(page, 1);
977 return -EFSCORRUPTED;
978 }
979
980 /* Make dnode_of_data for parameter */
981 dn->node_page = page;
982 dn->ofs_in_node = 0;
983 f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
984 err = truncate_node(dn);
985 if (err) {
986 f2fs_put_page(page, 1);
987 return err;
988 }
989
990 return 1;
991 }
992
truncate_nodes(struct dnode_of_data * dn,unsigned int nofs,int ofs,int depth)993 static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
994 int ofs, int depth)
995 {
996 struct dnode_of_data rdn = *dn;
997 struct page *page;
998 struct f2fs_node *rn;
999 nid_t child_nid;
1000 unsigned int child_nofs;
1001 int freed = 0;
1002 int i, ret;
1003
1004 if (dn->nid == 0)
1005 return NIDS_PER_BLOCK + 1;
1006
1007 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
1008
1009 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
1010 if (IS_ERR(page)) {
1011 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
1012 return PTR_ERR(page);
1013 }
1014
1015 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
1016
1017 rn = F2FS_NODE(page);
1018 if (depth < 3) {
1019 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
1020 child_nid = le32_to_cpu(rn->in.nid[i]);
1021 if (child_nid == 0)
1022 continue;
1023 rdn.nid = child_nid;
1024 ret = truncate_dnode(&rdn);
1025 if (ret < 0)
1026 goto out_err;
1027 if (set_nid(page, i, 0, false))
1028 dn->node_changed = true;
1029 }
1030 } else {
1031 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
1032 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
1033 child_nid = le32_to_cpu(rn->in.nid[i]);
1034 if (child_nid == 0) {
1035 child_nofs += NIDS_PER_BLOCK + 1;
1036 continue;
1037 }
1038 rdn.nid = child_nid;
1039 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
1040 if (ret == (NIDS_PER_BLOCK + 1)) {
1041 if (set_nid(page, i, 0, false))
1042 dn->node_changed = true;
1043 child_nofs += ret;
1044 } else if (ret < 0 && ret != -ENOENT) {
1045 goto out_err;
1046 }
1047 }
1048 freed = child_nofs;
1049 }
1050
1051 if (!ofs) {
1052 /* remove current indirect node */
1053 dn->node_page = page;
1054 ret = truncate_node(dn);
1055 if (ret)
1056 goto out_err;
1057 freed++;
1058 } else {
1059 f2fs_put_page(page, 1);
1060 }
1061 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
1062 return freed;
1063
1064 out_err:
1065 f2fs_put_page(page, 1);
1066 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
1067 return ret;
1068 }
1069
truncate_partial_nodes(struct dnode_of_data * dn,struct f2fs_inode * ri,int * offset,int depth)1070 static int truncate_partial_nodes(struct dnode_of_data *dn,
1071 struct f2fs_inode *ri, int *offset, int depth)
1072 {
1073 struct page *pages[2];
1074 nid_t nid[3];
1075 nid_t child_nid;
1076 int err = 0;
1077 int i;
1078 int idx = depth - 2;
1079
1080 nid[0] = get_nid(dn->inode_page, offset[0], true);
1081 if (!nid[0])
1082 return 0;
1083
1084 /* get indirect nodes in the path */
1085 for (i = 0; i < idx + 1; i++) {
1086 /* reference count'll be increased */
1087 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
1088 if (IS_ERR(pages[i])) {
1089 err = PTR_ERR(pages[i]);
1090 idx = i - 1;
1091 goto fail;
1092 }
1093 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
1094 }
1095
1096 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
1097
1098 /* free direct nodes linked to a partial indirect node */
1099 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
1100 child_nid = get_nid(pages[idx], i, false);
1101 if (!child_nid)
1102 continue;
1103 dn->nid = child_nid;
1104 err = truncate_dnode(dn);
1105 if (err < 0)
1106 goto fail;
1107 if (set_nid(pages[idx], i, 0, false))
1108 dn->node_changed = true;
1109 }
1110
1111 if (offset[idx + 1] == 0) {
1112 dn->node_page = pages[idx];
1113 dn->nid = nid[idx];
1114 err = truncate_node(dn);
1115 if (err)
1116 goto fail;
1117 } else {
1118 f2fs_put_page(pages[idx], 1);
1119 }
1120 offset[idx]++;
1121 offset[idx + 1] = 0;
1122 idx--;
1123 fail:
1124 for (i = idx; i >= 0; i--)
1125 f2fs_put_page(pages[i], 1);
1126
1127 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1128
1129 return err;
1130 }
1131
1132 /*
1133 * All the block addresses of data and nodes should be nullified.
1134 */
f2fs_truncate_inode_blocks(struct inode * inode,pgoff_t from)1135 int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
1136 {
1137 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1138 int err = 0, cont = 1;
1139 int level, offset[4], noffset[4];
1140 unsigned int nofs = 0;
1141 struct f2fs_inode *ri;
1142 struct dnode_of_data dn;
1143 struct folio *folio;
1144
1145 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1146
1147 level = get_node_path(inode, from, offset, noffset);
1148 if (level <= 0) {
1149 if (!level) {
1150 level = -EFSCORRUPTED;
1151 f2fs_err(sbi, "%s: inode ino=%lx has corrupted node block, from:%lu addrs:%u",
1152 __func__, inode->i_ino,
1153 from, ADDRS_PER_INODE(inode));
1154 set_sbi_flag(sbi, SBI_NEED_FSCK);
1155 }
1156 trace_f2fs_truncate_inode_blocks_exit(inode, level);
1157 return level;
1158 }
1159
1160 folio = f2fs_get_inode_folio(sbi, inode->i_ino);
1161 if (IS_ERR(folio)) {
1162 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(folio));
1163 return PTR_ERR(folio);
1164 }
1165
1166 set_new_dnode(&dn, inode, &folio->page, NULL, 0);
1167 folio_unlock(folio);
1168
1169 ri = F2FS_INODE(&folio->page);
1170 switch (level) {
1171 case 0:
1172 case 1:
1173 nofs = noffset[1];
1174 break;
1175 case 2:
1176 nofs = noffset[1];
1177 if (!offset[level - 1])
1178 goto skip_partial;
1179 err = truncate_partial_nodes(&dn, ri, offset, level);
1180 if (err < 0 && err != -ENOENT)
1181 goto fail;
1182 nofs += 1 + NIDS_PER_BLOCK;
1183 break;
1184 case 3:
1185 nofs = 5 + 2 * NIDS_PER_BLOCK;
1186 if (!offset[level - 1])
1187 goto skip_partial;
1188 err = truncate_partial_nodes(&dn, ri, offset, level);
1189 if (err < 0 && err != -ENOENT)
1190 goto fail;
1191 break;
1192 default:
1193 BUG();
1194 }
1195
1196 skip_partial:
1197 while (cont) {
1198 dn.nid = get_nid(&folio->page, offset[0], true);
1199 switch (offset[0]) {
1200 case NODE_DIR1_BLOCK:
1201 case NODE_DIR2_BLOCK:
1202 err = truncate_dnode(&dn);
1203 break;
1204
1205 case NODE_IND1_BLOCK:
1206 case NODE_IND2_BLOCK:
1207 err = truncate_nodes(&dn, nofs, offset[1], 2);
1208 break;
1209
1210 case NODE_DIND_BLOCK:
1211 err = truncate_nodes(&dn, nofs, offset[1], 3);
1212 cont = 0;
1213 break;
1214
1215 default:
1216 BUG();
1217 }
1218 if (err == -ENOENT) {
1219 set_sbi_flag(F2FS_F_SB(folio), SBI_NEED_FSCK);
1220 f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR);
1221 f2fs_err_ratelimited(sbi,
1222 "truncate node fail, ino:%lu, nid:%u, "
1223 "offset[0]:%d, offset[1]:%d, nofs:%d",
1224 inode->i_ino, dn.nid, offset[0],
1225 offset[1], nofs);
1226 err = 0;
1227 }
1228 if (err < 0)
1229 goto fail;
1230 if (offset[1] == 0 && get_nid(&folio->page, offset[0], true)) {
1231 folio_lock(folio);
1232 BUG_ON(folio->mapping != NODE_MAPPING(sbi));
1233 set_nid(&folio->page, offset[0], 0, true);
1234 folio_unlock(folio);
1235 }
1236 offset[1] = 0;
1237 offset[0]++;
1238 nofs += err;
1239 }
1240 fail:
1241 f2fs_folio_put(folio, false);
1242 trace_f2fs_truncate_inode_blocks_exit(inode, err);
1243 return err > 0 ? 0 : err;
1244 }
1245
1246 /* caller must lock inode page */
f2fs_truncate_xattr_node(struct inode * inode)1247 int f2fs_truncate_xattr_node(struct inode *inode)
1248 {
1249 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1250 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1251 struct dnode_of_data dn;
1252 struct page *npage;
1253 int err;
1254
1255 if (!nid)
1256 return 0;
1257
1258 npage = f2fs_get_xnode_page(sbi, nid);
1259 if (IS_ERR(npage))
1260 return PTR_ERR(npage);
1261
1262 set_new_dnode(&dn, inode, NULL, npage, nid);
1263 err = truncate_node(&dn);
1264 if (err) {
1265 f2fs_put_page(npage, 1);
1266 return err;
1267 }
1268
1269 f2fs_i_xnid_write(inode, 0);
1270
1271 return 0;
1272 }
1273
1274 /*
1275 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1276 * f2fs_unlock_op().
1277 */
f2fs_remove_inode_page(struct inode * inode)1278 int f2fs_remove_inode_page(struct inode *inode)
1279 {
1280 struct dnode_of_data dn;
1281 int err;
1282
1283 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1284 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
1285 if (err)
1286 return err;
1287
1288 err = f2fs_truncate_xattr_node(inode);
1289 if (err) {
1290 f2fs_put_dnode(&dn);
1291 return err;
1292 }
1293
1294 /* remove potential inline_data blocks */
1295 if (!IS_DEVICE_ALIASING(inode) &&
1296 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1297 S_ISLNK(inode->i_mode)))
1298 f2fs_truncate_data_blocks_range(&dn, 1);
1299
1300 /* 0 is possible, after f2fs_new_inode() has failed */
1301 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1302 f2fs_put_dnode(&dn);
1303 return -EIO;
1304 }
1305
1306 if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
1307 f2fs_warn(F2FS_I_SB(inode),
1308 "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu",
1309 inode->i_ino, (unsigned long long)inode->i_blocks);
1310 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
1311 }
1312
1313 /* will put inode & node pages */
1314 err = truncate_node(&dn);
1315 if (err) {
1316 f2fs_put_dnode(&dn);
1317 return err;
1318 }
1319 return 0;
1320 }
1321
f2fs_new_inode_page(struct inode * inode)1322 struct page *f2fs_new_inode_page(struct inode *inode)
1323 {
1324 struct dnode_of_data dn;
1325
1326 /* allocate inode page for new inode */
1327 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
1328
1329 /* caller should f2fs_put_page(page, 1); */
1330 return f2fs_new_node_page(&dn, 0);
1331 }
1332
f2fs_new_node_page(struct dnode_of_data * dn,unsigned int ofs)1333 struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
1334 {
1335 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1336 struct node_info new_ni;
1337 struct page *page;
1338 int err;
1339
1340 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1341 return ERR_PTR(-EPERM);
1342
1343 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
1344 if (!page)
1345 return ERR_PTR(-ENOMEM);
1346
1347 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
1348 goto fail;
1349
1350 #ifdef CONFIG_F2FS_CHECK_FS
1351 err = f2fs_get_node_info(sbi, dn->nid, &new_ni, false);
1352 if (err) {
1353 dec_valid_node_count(sbi, dn->inode, !ofs);
1354 goto fail;
1355 }
1356 if (unlikely(new_ni.blk_addr != NULL_ADDR)) {
1357 err = -EFSCORRUPTED;
1358 dec_valid_node_count(sbi, dn->inode, !ofs);
1359 set_sbi_flag(sbi, SBI_NEED_FSCK);
1360 f2fs_warn_ratelimited(sbi,
1361 "f2fs_new_node_page: inconsistent nat entry, "
1362 "ino:%u, nid:%u, blkaddr:%u, ver:%u, flag:%u",
1363 new_ni.ino, new_ni.nid, new_ni.blk_addr,
1364 new_ni.version, new_ni.flag);
1365 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NAT);
1366 goto fail;
1367 }
1368 #endif
1369 new_ni.nid = dn->nid;
1370 new_ni.ino = dn->inode->i_ino;
1371 new_ni.blk_addr = NULL_ADDR;
1372 new_ni.flag = 0;
1373 new_ni.version = 0;
1374 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
1375
1376 f2fs_wait_on_page_writeback(page, NODE, true, true);
1377 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
1378 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
1379 if (!PageUptodate(page))
1380 SetPageUptodate(page);
1381 if (set_page_dirty(page))
1382 dn->node_changed = true;
1383
1384 if (f2fs_has_xattr_block(ofs))
1385 f2fs_i_xnid_write(dn->inode, dn->nid);
1386
1387 if (ofs == 0)
1388 inc_valid_inode_count(sbi);
1389 return page;
1390 fail:
1391 clear_node_page_dirty(page);
1392 f2fs_put_page(page, 1);
1393 return ERR_PTR(err);
1394 }
1395
1396 /*
1397 * Caller should do after getting the following values.
1398 * 0: f2fs_put_page(page, 0)
1399 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
1400 */
read_node_page(struct page * page,blk_opf_t op_flags)1401 static int read_node_page(struct page *page, blk_opf_t op_flags)
1402 {
1403 struct folio *folio = page_folio(page);
1404 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1405 struct node_info ni;
1406 struct f2fs_io_info fio = {
1407 .sbi = sbi,
1408 .type = NODE,
1409 .op = REQ_OP_READ,
1410 .op_flags = op_flags,
1411 .page = page,
1412 .encrypted_page = NULL,
1413 };
1414 int err;
1415
1416 if (folio_test_uptodate(folio)) {
1417 if (!f2fs_inode_chksum_verify(sbi, page)) {
1418 folio_clear_uptodate(folio);
1419 return -EFSBADCRC;
1420 }
1421 return LOCKED_PAGE;
1422 }
1423
1424 err = f2fs_get_node_info(sbi, folio->index, &ni, false);
1425 if (err)
1426 return err;
1427
1428 /* NEW_ADDR can be seen, after cp_error drops some dirty node pages */
1429 if (unlikely(ni.blk_addr == NULL_ADDR || ni.blk_addr == NEW_ADDR)) {
1430 folio_clear_uptodate(folio);
1431 return -ENOENT;
1432 }
1433
1434 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
1435
1436 err = f2fs_submit_page_bio(&fio);
1437
1438 if (!err)
1439 f2fs_update_iostat(sbi, NULL, FS_NODE_READ_IO, F2FS_BLKSIZE);
1440
1441 return err;
1442 }
1443
1444 /*
1445 * Readahead a node page
1446 */
f2fs_ra_node_page(struct f2fs_sb_info * sbi,nid_t nid)1447 void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1448 {
1449 struct page *apage;
1450 int err;
1451
1452 if (!nid)
1453 return;
1454 if (f2fs_check_nid_range(sbi, nid))
1455 return;
1456
1457 apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
1458 if (apage)
1459 return;
1460
1461 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
1462 if (!apage)
1463 return;
1464
1465 err = read_node_page(apage, REQ_RAHEAD);
1466 f2fs_put_page(apage, err ? 1 : 0);
1467 }
1468
sanity_check_node_footer(struct f2fs_sb_info * sbi,struct page * page,pgoff_t nid,enum node_type ntype)1469 static int sanity_check_node_footer(struct f2fs_sb_info *sbi,
1470 struct page *page, pgoff_t nid,
1471 enum node_type ntype)
1472 {
1473 if (unlikely(nid != nid_of_node(page) ||
1474 (ntype == NODE_TYPE_INODE && !IS_INODE(page)) ||
1475 (ntype == NODE_TYPE_XATTR &&
1476 !f2fs_has_xattr_block(ofs_of_node(page))) ||
1477 time_to_inject(sbi, FAULT_INCONSISTENT_FOOTER))) {
1478 f2fs_warn(sbi, "inconsistent node block, node_type:%d, nid:%lu, "
1479 "node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1480 ntype, nid, nid_of_node(page), ino_of_node(page),
1481 ofs_of_node(page), cpver_of_node(page),
1482 next_blkaddr_of_node(page));
1483 set_sbi_flag(sbi, SBI_NEED_FSCK);
1484 f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
1485 return -EFSCORRUPTED;
1486 }
1487 return 0;
1488 }
1489
__get_node_folio(struct f2fs_sb_info * sbi,pgoff_t nid,struct page * parent,int start,enum node_type ntype)1490 static struct folio *__get_node_folio(struct f2fs_sb_info *sbi, pgoff_t nid,
1491 struct page *parent, int start,
1492 enum node_type ntype)
1493 {
1494 struct folio *folio;
1495 int err;
1496
1497 if (!nid)
1498 return ERR_PTR(-ENOENT);
1499 if (f2fs_check_nid_range(sbi, nid))
1500 return ERR_PTR(-EINVAL);
1501 repeat:
1502 folio = f2fs_grab_cache_folio(NODE_MAPPING(sbi), nid, false);
1503 if (IS_ERR(folio))
1504 return folio;
1505
1506 err = read_node_page(&folio->page, 0);
1507 if (err < 0)
1508 goto out_put_err;
1509 if (err == LOCKED_PAGE)
1510 goto page_hit;
1511
1512 if (parent)
1513 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
1514
1515 folio_lock(folio);
1516
1517 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
1518 f2fs_folio_put(folio, true);
1519 goto repeat;
1520 }
1521
1522 if (unlikely(!folio_test_uptodate(folio))) {
1523 err = -EIO;
1524 goto out_err;
1525 }
1526
1527 if (!f2fs_inode_chksum_verify(sbi, &folio->page)) {
1528 err = -EFSBADCRC;
1529 goto out_err;
1530 }
1531 page_hit:
1532 err = sanity_check_node_footer(sbi, &folio->page, nid, ntype);
1533 if (!err)
1534 return folio;
1535 out_err:
1536 folio_clear_uptodate(folio);
1537 out_put_err:
1538 /* ENOENT comes from read_node_page which is not an error. */
1539 if (err != -ENOENT)
1540 f2fs_handle_page_eio(sbi, folio, NODE);
1541 f2fs_folio_put(folio, true);
1542 return ERR_PTR(err);
1543 }
1544
f2fs_get_node_page(struct f2fs_sb_info * sbi,pgoff_t nid)1545 struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1546 {
1547 struct folio *folio = __get_node_folio(sbi, nid, NULL, 0,
1548 NODE_TYPE_REGULAR);
1549
1550 return &folio->page;
1551 }
1552
f2fs_get_inode_folio(struct f2fs_sb_info * sbi,pgoff_t ino)1553 struct folio *f2fs_get_inode_folio(struct f2fs_sb_info *sbi, pgoff_t ino)
1554 {
1555 return __get_node_folio(sbi, ino, NULL, 0, NODE_TYPE_INODE);
1556 }
1557
f2fs_get_inode_page(struct f2fs_sb_info * sbi,pgoff_t ino)1558 struct page *f2fs_get_inode_page(struct f2fs_sb_info *sbi, pgoff_t ino)
1559 {
1560 struct folio *folio = f2fs_get_inode_folio(sbi, ino);
1561
1562 return &folio->page;
1563 }
1564
f2fs_get_xnode_page(struct f2fs_sb_info * sbi,pgoff_t xnid)1565 struct page *f2fs_get_xnode_page(struct f2fs_sb_info *sbi, pgoff_t xnid)
1566 {
1567 struct folio *folio = __get_node_folio(sbi, xnid, NULL, 0,
1568 NODE_TYPE_XATTR);
1569
1570 return &folio->page;
1571 }
1572
f2fs_get_node_page_ra(struct page * parent,int start)1573 struct page *f2fs_get_node_page_ra(struct page *parent, int start)
1574 {
1575 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
1576 nid_t nid = get_nid(parent, start, false);
1577 struct folio *folio = __get_node_folio(sbi, nid, parent, start,
1578 NODE_TYPE_REGULAR);
1579
1580 return &folio->page;
1581 }
1582
flush_inline_data(struct f2fs_sb_info * sbi,nid_t ino)1583 static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1584 {
1585 struct inode *inode;
1586 struct page *page;
1587 int ret;
1588
1589 /* should flush inline_data before evict_inode */
1590 inode = ilookup(sbi->sb, ino);
1591 if (!inode)
1592 return;
1593
1594 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1595 FGP_LOCK|FGP_NOWAIT, 0);
1596 if (!page)
1597 goto iput_out;
1598
1599 if (!PageUptodate(page))
1600 goto page_out;
1601
1602 if (!PageDirty(page))
1603 goto page_out;
1604
1605 if (!clear_page_dirty_for_io(page))
1606 goto page_out;
1607
1608 ret = f2fs_write_inline_data(inode, page_folio(page));
1609 inode_dec_dirty_pages(inode);
1610 f2fs_remove_dirty_inode(inode);
1611 if (ret)
1612 set_page_dirty(page);
1613 page_out:
1614 f2fs_put_page(page, 1);
1615 iput_out:
1616 iput(inode);
1617 }
1618
last_fsync_dnode(struct f2fs_sb_info * sbi,nid_t ino)1619 static struct folio *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
1620 {
1621 pgoff_t index;
1622 struct folio_batch fbatch;
1623 struct folio *last_folio = NULL;
1624 int nr_folios;
1625
1626 folio_batch_init(&fbatch);
1627 index = 0;
1628
1629 while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
1630 (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
1631 &fbatch))) {
1632 int i;
1633
1634 for (i = 0; i < nr_folios; i++) {
1635 struct folio *folio = fbatch.folios[i];
1636
1637 if (unlikely(f2fs_cp_error(sbi))) {
1638 f2fs_folio_put(last_folio, false);
1639 folio_batch_release(&fbatch);
1640 return ERR_PTR(-EIO);
1641 }
1642
1643 if (!IS_DNODE(&folio->page) || !is_cold_node(&folio->page))
1644 continue;
1645 if (ino_of_node(&folio->page) != ino)
1646 continue;
1647
1648 folio_lock(folio);
1649
1650 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
1651 continue_unlock:
1652 folio_unlock(folio);
1653 continue;
1654 }
1655 if (ino_of_node(&folio->page) != ino)
1656 goto continue_unlock;
1657
1658 if (!folio_test_dirty(folio)) {
1659 /* someone wrote it for us */
1660 goto continue_unlock;
1661 }
1662
1663 if (last_folio)
1664 f2fs_folio_put(last_folio, false);
1665
1666 folio_get(folio);
1667 last_folio = folio;
1668 folio_unlock(folio);
1669 }
1670 folio_batch_release(&fbatch);
1671 cond_resched();
1672 }
1673 return last_folio;
1674 }
1675
__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)1676 static int __write_node_page(struct page *page, bool atomic, bool *submitted,
1677 struct writeback_control *wbc, bool do_balance,
1678 enum iostat_type io_type, unsigned int *seq_id)
1679 {
1680 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1681 struct folio *folio = page_folio(page);
1682 nid_t nid;
1683 struct node_info ni;
1684 struct f2fs_io_info fio = {
1685 .sbi = sbi,
1686 .ino = ino_of_node(page),
1687 .type = NODE,
1688 .op = REQ_OP_WRITE,
1689 .op_flags = wbc_to_write_flags(wbc),
1690 .page = page,
1691 .encrypted_page = NULL,
1692 .submitted = 0,
1693 .io_type = io_type,
1694 .io_wbc = wbc,
1695 };
1696 unsigned int seq;
1697
1698 trace_f2fs_writepage(folio, NODE);
1699
1700 if (unlikely(f2fs_cp_error(sbi))) {
1701 /* keep node pages in remount-ro mode */
1702 if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
1703 goto redirty_out;
1704 folio_clear_uptodate(folio);
1705 dec_page_count(sbi, F2FS_DIRTY_NODES);
1706 folio_unlock(folio);
1707 return 0;
1708 }
1709
1710 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1711 goto redirty_out;
1712
1713 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1714 wbc->sync_mode == WB_SYNC_NONE &&
1715 IS_DNODE(page) && is_cold_node(page))
1716 goto redirty_out;
1717
1718 /* get old block addr of this node page */
1719 nid = nid_of_node(page);
1720 f2fs_bug_on(sbi, folio->index != nid);
1721
1722 if (f2fs_get_node_info(sbi, nid, &ni, !do_balance))
1723 goto redirty_out;
1724
1725 if (wbc->for_reclaim) {
1726 if (!f2fs_down_read_trylock(&sbi->node_write))
1727 goto redirty_out;
1728 } else {
1729 f2fs_down_read(&sbi->node_write);
1730 }
1731
1732 /* This page is already truncated */
1733 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1734 folio_clear_uptodate(folio);
1735 dec_page_count(sbi, F2FS_DIRTY_NODES);
1736 f2fs_up_read(&sbi->node_write);
1737 folio_unlock(folio);
1738 return 0;
1739 }
1740
1741 if (__is_valid_data_blkaddr(ni.blk_addr) &&
1742 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
1743 DATA_GENERIC_ENHANCE)) {
1744 f2fs_up_read(&sbi->node_write);
1745 goto redirty_out;
1746 }
1747
1748 if (atomic && !test_opt(sbi, NOBARRIER))
1749 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1750
1751 /* should add to global list before clearing PAGECACHE status */
1752 if (f2fs_in_warm_node_list(sbi, folio)) {
1753 seq = f2fs_add_fsync_node_entry(sbi, page);
1754 if (seq_id)
1755 *seq_id = seq;
1756 }
1757
1758 folio_start_writeback(folio);
1759
1760 fio.old_blkaddr = ni.blk_addr;
1761 f2fs_do_write_node_page(nid, &fio);
1762 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1763 dec_page_count(sbi, F2FS_DIRTY_NODES);
1764 f2fs_up_read(&sbi->node_write);
1765
1766 if (wbc->for_reclaim) {
1767 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
1768 submitted = NULL;
1769 }
1770
1771 folio_unlock(folio);
1772
1773 if (unlikely(f2fs_cp_error(sbi))) {
1774 f2fs_submit_merged_write(sbi, NODE);
1775 submitted = NULL;
1776 }
1777 if (submitted)
1778 *submitted = fio.submitted;
1779
1780 if (do_balance)
1781 f2fs_balance_fs(sbi, false);
1782 return 0;
1783
1784 redirty_out:
1785 folio_redirty_for_writepage(wbc, folio);
1786 return AOP_WRITEPAGE_ACTIVATE;
1787 }
1788
f2fs_move_node_page(struct page * node_page,int gc_type)1789 int f2fs_move_node_page(struct page *node_page, int gc_type)
1790 {
1791 int err = 0;
1792
1793 if (gc_type == FG_GC) {
1794 struct writeback_control wbc = {
1795 .sync_mode = WB_SYNC_ALL,
1796 .nr_to_write = 1,
1797 .for_reclaim = 0,
1798 };
1799
1800 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
1801
1802 set_page_dirty(node_page);
1803
1804 if (!clear_page_dirty_for_io(node_page)) {
1805 err = -EAGAIN;
1806 goto out_page;
1807 }
1808
1809 if (__write_node_page(node_page, false, NULL,
1810 &wbc, false, FS_GC_NODE_IO, NULL)) {
1811 err = -EAGAIN;
1812 unlock_page(node_page);
1813 }
1814 goto release_page;
1815 } else {
1816 /* set page dirty and write it */
1817 if (!folio_test_writeback(page_folio(node_page)))
1818 set_page_dirty(node_page);
1819 }
1820 out_page:
1821 unlock_page(node_page);
1822 release_page:
1823 f2fs_put_page(node_page, 0);
1824 return err;
1825 }
1826
f2fs_fsync_node_pages(struct f2fs_sb_info * sbi,struct inode * inode,struct writeback_control * wbc,bool atomic,unsigned int * seq_id)1827 int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
1828 struct writeback_control *wbc, bool atomic,
1829 unsigned int *seq_id)
1830 {
1831 pgoff_t index;
1832 struct folio_batch fbatch;
1833 int ret = 0;
1834 struct folio *last_folio = NULL;
1835 bool marked = false;
1836 nid_t ino = inode->i_ino;
1837 int nr_folios;
1838 int nwritten = 0;
1839
1840 if (atomic) {
1841 last_folio = last_fsync_dnode(sbi, ino);
1842 if (IS_ERR_OR_NULL(last_folio))
1843 return PTR_ERR_OR_ZERO(last_folio);
1844 }
1845 retry:
1846 folio_batch_init(&fbatch);
1847 index = 0;
1848
1849 while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
1850 (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
1851 &fbatch))) {
1852 int i;
1853
1854 for (i = 0; i < nr_folios; i++) {
1855 struct folio *folio = fbatch.folios[i];
1856 bool submitted = false;
1857
1858 if (unlikely(f2fs_cp_error(sbi))) {
1859 f2fs_folio_put(last_folio, false);
1860 folio_batch_release(&fbatch);
1861 ret = -EIO;
1862 goto out;
1863 }
1864
1865 if (!IS_DNODE(&folio->page) || !is_cold_node(&folio->page))
1866 continue;
1867 if (ino_of_node(&folio->page) != ino)
1868 continue;
1869
1870 folio_lock(folio);
1871
1872 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
1873 continue_unlock:
1874 folio_unlock(folio);
1875 continue;
1876 }
1877 if (ino_of_node(&folio->page) != ino)
1878 goto continue_unlock;
1879
1880 if (!folio_test_dirty(folio) && folio != last_folio) {
1881 /* someone wrote it for us */
1882 goto continue_unlock;
1883 }
1884
1885 f2fs_folio_wait_writeback(folio, NODE, true, true);
1886
1887 set_fsync_mark(&folio->page, 0);
1888 set_dentry_mark(&folio->page, 0);
1889
1890 if (!atomic || folio == last_folio) {
1891 set_fsync_mark(&folio->page, 1);
1892 percpu_counter_inc(&sbi->rf_node_block_count);
1893 if (IS_INODE(&folio->page)) {
1894 if (is_inode_flag_set(inode,
1895 FI_DIRTY_INODE))
1896 f2fs_update_inode(inode, &folio->page);
1897 set_dentry_mark(&folio->page,
1898 f2fs_need_dentry_mark(sbi, ino));
1899 }
1900 /* may be written by other thread */
1901 if (!folio_test_dirty(folio))
1902 folio_mark_dirty(folio);
1903 }
1904
1905 if (!folio_clear_dirty_for_io(folio))
1906 goto continue_unlock;
1907
1908 ret = __write_node_page(&folio->page, atomic &&
1909 folio == last_folio,
1910 &submitted, wbc, true,
1911 FS_NODE_IO, seq_id);
1912 if (ret) {
1913 folio_unlock(folio);
1914 f2fs_folio_put(last_folio, false);
1915 break;
1916 } else if (submitted) {
1917 nwritten++;
1918 }
1919
1920 if (folio == last_folio) {
1921 f2fs_folio_put(folio, false);
1922 marked = true;
1923 break;
1924 }
1925 }
1926 folio_batch_release(&fbatch);
1927 cond_resched();
1928
1929 if (ret || marked)
1930 break;
1931 }
1932 if (!ret && atomic && !marked) {
1933 f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
1934 ino, last_folio->index);
1935 folio_lock(last_folio);
1936 f2fs_folio_wait_writeback(last_folio, NODE, true, true);
1937 folio_mark_dirty(last_folio);
1938 folio_unlock(last_folio);
1939 goto retry;
1940 }
1941 out:
1942 if (nwritten)
1943 f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
1944 return ret ? -EIO : 0;
1945 }
1946
f2fs_match_ino(struct inode * inode,unsigned long ino,void * data)1947 static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data)
1948 {
1949 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1950 bool clean;
1951
1952 if (inode->i_ino != ino)
1953 return 0;
1954
1955 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
1956 return 0;
1957
1958 spin_lock(&sbi->inode_lock[DIRTY_META]);
1959 clean = list_empty(&F2FS_I(inode)->gdirty_list);
1960 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1961
1962 if (clean)
1963 return 0;
1964
1965 inode = igrab(inode);
1966 if (!inode)
1967 return 0;
1968 return 1;
1969 }
1970
flush_dirty_inode(struct folio * folio)1971 static bool flush_dirty_inode(struct folio *folio)
1972 {
1973 struct f2fs_sb_info *sbi = F2FS_F_SB(folio);
1974 struct inode *inode;
1975 nid_t ino = ino_of_node(&folio->page);
1976
1977 inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL);
1978 if (!inode)
1979 return false;
1980
1981 f2fs_update_inode(inode, &folio->page);
1982 folio_unlock(folio);
1983
1984 iput(inode);
1985 return true;
1986 }
1987
f2fs_flush_inline_data(struct f2fs_sb_info * sbi)1988 void f2fs_flush_inline_data(struct f2fs_sb_info *sbi)
1989 {
1990 pgoff_t index = 0;
1991 struct folio_batch fbatch;
1992 int nr_folios;
1993
1994 folio_batch_init(&fbatch);
1995
1996 while ((nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi), &index,
1997 (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
1998 &fbatch))) {
1999 int i;
2000
2001 for (i = 0; i < nr_folios; i++) {
2002 struct folio *folio = fbatch.folios[i];
2003
2004 if (!IS_INODE(&folio->page))
2005 continue;
2006
2007 folio_lock(folio);
2008
2009 if (unlikely(folio->mapping != NODE_MAPPING(sbi)))
2010 goto unlock;
2011 if (!folio_test_dirty(folio))
2012 goto unlock;
2013
2014 /* flush inline_data, if it's async context. */
2015 if (page_private_inline(&folio->page)) {
2016 clear_page_private_inline(&folio->page);
2017 folio_unlock(folio);
2018 flush_inline_data(sbi, ino_of_node(&folio->page));
2019 continue;
2020 }
2021 unlock:
2022 folio_unlock(folio);
2023 }
2024 folio_batch_release(&fbatch);
2025 cond_resched();
2026 }
2027 }
2028
f2fs_sync_node_pages(struct f2fs_sb_info * sbi,struct writeback_control * wbc,bool do_balance,enum iostat_type io_type)2029 int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
2030 struct writeback_control *wbc,
2031 bool do_balance, enum iostat_type io_type)
2032 {
2033 pgoff_t index;
2034 struct folio_batch fbatch;
2035 int step = 0;
2036 int nwritten = 0;
2037 int ret = 0;
2038 int nr_folios, done = 0;
2039
2040 folio_batch_init(&fbatch);
2041
2042 next_step:
2043 index = 0;
2044
2045 while (!done && (nr_folios = filemap_get_folios_tag(NODE_MAPPING(sbi),
2046 &index, (pgoff_t)-1, PAGECACHE_TAG_DIRTY,
2047 &fbatch))) {
2048 int i;
2049
2050 for (i = 0; i < nr_folios; i++) {
2051 struct folio *folio = fbatch.folios[i];
2052 bool submitted = false;
2053
2054 /* give a priority to WB_SYNC threads */
2055 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
2056 wbc->sync_mode == WB_SYNC_NONE) {
2057 done = 1;
2058 break;
2059 }
2060
2061 /*
2062 * flushing sequence with step:
2063 * 0. indirect nodes
2064 * 1. dentry dnodes
2065 * 2. file dnodes
2066 */
2067 if (step == 0 && IS_DNODE(&folio->page))
2068 continue;
2069 if (step == 1 && (!IS_DNODE(&folio->page) ||
2070 is_cold_node(&folio->page)))
2071 continue;
2072 if (step == 2 && (!IS_DNODE(&folio->page) ||
2073 !is_cold_node(&folio->page)))
2074 continue;
2075 lock_node:
2076 if (wbc->sync_mode == WB_SYNC_ALL)
2077 folio_lock(folio);
2078 else if (!folio_trylock(folio))
2079 continue;
2080
2081 if (unlikely(folio->mapping != NODE_MAPPING(sbi))) {
2082 continue_unlock:
2083 folio_unlock(folio);
2084 continue;
2085 }
2086
2087 if (!folio_test_dirty(folio)) {
2088 /* someone wrote it for us */
2089 goto continue_unlock;
2090 }
2091
2092 /* flush inline_data/inode, if it's async context. */
2093 if (!do_balance)
2094 goto write_node;
2095
2096 /* flush inline_data */
2097 if (page_private_inline(&folio->page)) {
2098 clear_page_private_inline(&folio->page);
2099 folio_unlock(folio);
2100 flush_inline_data(sbi, ino_of_node(&folio->page));
2101 goto lock_node;
2102 }
2103
2104 /* flush dirty inode */
2105 if (IS_INODE(&folio->page) && flush_dirty_inode(folio))
2106 goto lock_node;
2107 write_node:
2108 f2fs_folio_wait_writeback(folio, NODE, true, true);
2109
2110 if (!folio_clear_dirty_for_io(folio))
2111 goto continue_unlock;
2112
2113 set_fsync_mark(&folio->page, 0);
2114 set_dentry_mark(&folio->page, 0);
2115
2116 ret = __write_node_page(&folio->page, false, &submitted,
2117 wbc, do_balance, io_type, NULL);
2118 if (ret)
2119 folio_unlock(folio);
2120 else if (submitted)
2121 nwritten++;
2122
2123 if (--wbc->nr_to_write == 0)
2124 break;
2125 }
2126 folio_batch_release(&fbatch);
2127 cond_resched();
2128
2129 if (wbc->nr_to_write == 0) {
2130 step = 2;
2131 break;
2132 }
2133 }
2134
2135 if (step < 2) {
2136 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2137 wbc->sync_mode == WB_SYNC_NONE && step == 1)
2138 goto out;
2139 step++;
2140 goto next_step;
2141 }
2142 out:
2143 if (nwritten)
2144 f2fs_submit_merged_write(sbi, NODE);
2145
2146 if (unlikely(f2fs_cp_error(sbi)))
2147 return -EIO;
2148 return ret;
2149 }
2150
f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info * sbi,unsigned int seq_id)2151 int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
2152 unsigned int seq_id)
2153 {
2154 struct fsync_node_entry *fn;
2155 struct page *page;
2156 struct list_head *head = &sbi->fsync_node_list;
2157 unsigned long flags;
2158 unsigned int cur_seq_id = 0;
2159
2160 while (seq_id && cur_seq_id < seq_id) {
2161 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
2162 if (list_empty(head)) {
2163 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2164 break;
2165 }
2166 fn = list_first_entry(head, struct fsync_node_entry, list);
2167 if (fn->seq_id > seq_id) {
2168 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2169 break;
2170 }
2171 cur_seq_id = fn->seq_id;
2172 page = fn->page;
2173 get_page(page);
2174 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2175
2176 f2fs_wait_on_page_writeback(page, NODE, true, false);
2177
2178 put_page(page);
2179 }
2180
2181 return filemap_check_errors(NODE_MAPPING(sbi));
2182 }
2183
f2fs_write_node_pages(struct address_space * mapping,struct writeback_control * wbc)2184 static int f2fs_write_node_pages(struct address_space *mapping,
2185 struct writeback_control *wbc)
2186 {
2187 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2188 struct blk_plug plug;
2189 long diff;
2190
2191 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2192 goto skip_write;
2193
2194 /* balancing f2fs's metadata in background */
2195 f2fs_balance_fs_bg(sbi, true);
2196
2197 /* collect a number of dirty node pages and write together */
2198 if (wbc->sync_mode != WB_SYNC_ALL &&
2199 get_pages(sbi, F2FS_DIRTY_NODES) <
2200 nr_pages_to_skip(sbi, NODE))
2201 goto skip_write;
2202
2203 if (wbc->sync_mode == WB_SYNC_ALL)
2204 atomic_inc(&sbi->wb_sync_req[NODE]);
2205 else if (atomic_read(&sbi->wb_sync_req[NODE])) {
2206 /* to avoid potential deadlock */
2207 if (current->plug)
2208 blk_finish_plug(current->plug);
2209 goto skip_write;
2210 }
2211
2212 trace_f2fs_writepages(mapping->host, wbc, NODE);
2213
2214 diff = nr_pages_to_write(sbi, NODE, wbc);
2215 blk_start_plug(&plug);
2216 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
2217 blk_finish_plug(&plug);
2218 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
2219
2220 if (wbc->sync_mode == WB_SYNC_ALL)
2221 atomic_dec(&sbi->wb_sync_req[NODE]);
2222 return 0;
2223
2224 skip_write:
2225 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
2226 trace_f2fs_writepages(mapping->host, wbc, NODE);
2227 return 0;
2228 }
2229
f2fs_dirty_node_folio(struct address_space * mapping,struct folio * folio)2230 static bool f2fs_dirty_node_folio(struct address_space *mapping,
2231 struct folio *folio)
2232 {
2233 trace_f2fs_set_page_dirty(folio, NODE);
2234
2235 if (!folio_test_uptodate(folio))
2236 folio_mark_uptodate(folio);
2237 #ifdef CONFIG_F2FS_CHECK_FS
2238 if (IS_INODE(&folio->page))
2239 f2fs_inode_chksum_set(F2FS_M_SB(mapping), &folio->page);
2240 #endif
2241 if (filemap_dirty_folio(mapping, folio)) {
2242 inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
2243 set_page_private_reference(&folio->page);
2244 return true;
2245 }
2246 return false;
2247 }
2248
2249 /*
2250 * Structure of the f2fs node operations
2251 */
2252 const struct address_space_operations f2fs_node_aops = {
2253 .writepages = f2fs_write_node_pages,
2254 .dirty_folio = f2fs_dirty_node_folio,
2255 .invalidate_folio = f2fs_invalidate_folio,
2256 .release_folio = f2fs_release_folio,
2257 .migrate_folio = filemap_migrate_folio,
2258 };
2259
__lookup_free_nid_list(struct f2fs_nm_info * nm_i,nid_t n)2260 static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
2261 nid_t n)
2262 {
2263 return radix_tree_lookup(&nm_i->free_nid_root, n);
2264 }
2265
__insert_free_nid(struct f2fs_sb_info * sbi,struct free_nid * i)2266 static int __insert_free_nid(struct f2fs_sb_info *sbi,
2267 struct free_nid *i)
2268 {
2269 struct f2fs_nm_info *nm_i = NM_I(sbi);
2270 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
2271
2272 if (err)
2273 return err;
2274
2275 nm_i->nid_cnt[FREE_NID]++;
2276 list_add_tail(&i->list, &nm_i->free_nid_list);
2277 return 0;
2278 }
2279
__remove_free_nid(struct f2fs_sb_info * sbi,struct free_nid * i,enum nid_state state)2280 static void __remove_free_nid(struct f2fs_sb_info *sbi,
2281 struct free_nid *i, enum nid_state state)
2282 {
2283 struct f2fs_nm_info *nm_i = NM_I(sbi);
2284
2285 f2fs_bug_on(sbi, state != i->state);
2286 nm_i->nid_cnt[state]--;
2287 if (state == FREE_NID)
2288 list_del(&i->list);
2289 radix_tree_delete(&nm_i->free_nid_root, i->nid);
2290 }
2291
__move_free_nid(struct f2fs_sb_info * sbi,struct free_nid * i,enum nid_state org_state,enum nid_state dst_state)2292 static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2293 enum nid_state org_state, enum nid_state dst_state)
2294 {
2295 struct f2fs_nm_info *nm_i = NM_I(sbi);
2296
2297 f2fs_bug_on(sbi, org_state != i->state);
2298 i->state = dst_state;
2299 nm_i->nid_cnt[org_state]--;
2300 nm_i->nid_cnt[dst_state]++;
2301
2302 switch (dst_state) {
2303 case PREALLOC_NID:
2304 list_del(&i->list);
2305 break;
2306 case FREE_NID:
2307 list_add_tail(&i->list, &nm_i->free_nid_list);
2308 break;
2309 default:
2310 BUG_ON(1);
2311 }
2312 }
2313
update_free_nid_bitmap(struct f2fs_sb_info * sbi,nid_t nid,bool set,bool build)2314 static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2315 bool set, bool build)
2316 {
2317 struct f2fs_nm_info *nm_i = NM_I(sbi);
2318 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2319 unsigned int nid_ofs = nid - START_NID(nid);
2320
2321 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2322 return;
2323
2324 if (set) {
2325 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2326 return;
2327 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2328 nm_i->free_nid_count[nat_ofs]++;
2329 } else {
2330 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2331 return;
2332 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2333 if (!build)
2334 nm_i->free_nid_count[nat_ofs]--;
2335 }
2336 }
2337
2338 /* return if the nid is recognized as free */
add_free_nid(struct f2fs_sb_info * sbi,nid_t nid,bool build,bool update)2339 static bool add_free_nid(struct f2fs_sb_info *sbi,
2340 nid_t nid, bool build, bool update)
2341 {
2342 struct f2fs_nm_info *nm_i = NM_I(sbi);
2343 struct free_nid *i, *e;
2344 struct nat_entry *ne;
2345 int err = -EINVAL;
2346 bool ret = false;
2347
2348 /* 0 nid should not be used */
2349 if (unlikely(nid == 0))
2350 return false;
2351
2352 if (unlikely(f2fs_check_nid_range(sbi, nid)))
2353 return false;
2354
2355 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS, true, NULL);
2356 i->nid = nid;
2357 i->state = FREE_NID;
2358
2359 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
2360
2361 spin_lock(&nm_i->nid_list_lock);
2362
2363 if (build) {
2364 /*
2365 * Thread A Thread B
2366 * - f2fs_create
2367 * - f2fs_new_inode
2368 * - f2fs_alloc_nid
2369 * - __insert_nid_to_list(PREALLOC_NID)
2370 * - f2fs_balance_fs_bg
2371 * - f2fs_build_free_nids
2372 * - __f2fs_build_free_nids
2373 * - scan_nat_page
2374 * - add_free_nid
2375 * - __lookup_nat_cache
2376 * - f2fs_add_link
2377 * - f2fs_init_inode_metadata
2378 * - f2fs_new_inode_page
2379 * - f2fs_new_node_page
2380 * - set_node_addr
2381 * - f2fs_alloc_nid_done
2382 * - __remove_nid_from_list(PREALLOC_NID)
2383 * - __insert_nid_to_list(FREE_NID)
2384 */
2385 ne = __lookup_nat_cache(nm_i, nid);
2386 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2387 nat_get_blkaddr(ne) != NULL_ADDR))
2388 goto err_out;
2389
2390 e = __lookup_free_nid_list(nm_i, nid);
2391 if (e) {
2392 if (e->state == FREE_NID)
2393 ret = true;
2394 goto err_out;
2395 }
2396 }
2397 ret = true;
2398 err = __insert_free_nid(sbi, i);
2399 err_out:
2400 if (update) {
2401 update_free_nid_bitmap(sbi, nid, ret, build);
2402 if (!build)
2403 nm_i->available_nids++;
2404 }
2405 spin_unlock(&nm_i->nid_list_lock);
2406 radix_tree_preload_end();
2407
2408 if (err)
2409 kmem_cache_free(free_nid_slab, i);
2410 return ret;
2411 }
2412
remove_free_nid(struct f2fs_sb_info * sbi,nid_t nid)2413 static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
2414 {
2415 struct f2fs_nm_info *nm_i = NM_I(sbi);
2416 struct free_nid *i;
2417 bool need_free = false;
2418
2419 spin_lock(&nm_i->nid_list_lock);
2420 i = __lookup_free_nid_list(nm_i, nid);
2421 if (i && i->state == FREE_NID) {
2422 __remove_free_nid(sbi, i, FREE_NID);
2423 need_free = true;
2424 }
2425 spin_unlock(&nm_i->nid_list_lock);
2426
2427 if (need_free)
2428 kmem_cache_free(free_nid_slab, i);
2429 }
2430
scan_nat_page(struct f2fs_sb_info * sbi,struct page * nat_page,nid_t start_nid)2431 static int scan_nat_page(struct f2fs_sb_info *sbi,
2432 struct page *nat_page, nid_t start_nid)
2433 {
2434 struct f2fs_nm_info *nm_i = NM_I(sbi);
2435 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2436 block_t blk_addr;
2437 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
2438 int i;
2439
2440 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
2441
2442 i = start_nid % NAT_ENTRY_PER_BLOCK;
2443
2444 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
2445 if (unlikely(start_nid >= nm_i->max_nid))
2446 break;
2447
2448 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
2449
2450 if (blk_addr == NEW_ADDR)
2451 return -EFSCORRUPTED;
2452
2453 if (blk_addr == NULL_ADDR) {
2454 add_free_nid(sbi, start_nid, true, true);
2455 } else {
2456 spin_lock(&NM_I(sbi)->nid_list_lock);
2457 update_free_nid_bitmap(sbi, start_nid, false, true);
2458 spin_unlock(&NM_I(sbi)->nid_list_lock);
2459 }
2460 }
2461
2462 return 0;
2463 }
2464
scan_curseg_cache(struct f2fs_sb_info * sbi)2465 static void scan_curseg_cache(struct f2fs_sb_info *sbi)
2466 {
2467 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2468 struct f2fs_journal *journal = curseg->journal;
2469 int i;
2470
2471 down_read(&curseg->journal_rwsem);
2472 for (i = 0; i < nats_in_cursum(journal); i++) {
2473 block_t addr;
2474 nid_t nid;
2475
2476 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2477 nid = le32_to_cpu(nid_in_journal(journal, i));
2478 if (addr == NULL_ADDR)
2479 add_free_nid(sbi, nid, true, false);
2480 else
2481 remove_free_nid(sbi, nid);
2482 }
2483 up_read(&curseg->journal_rwsem);
2484 }
2485
scan_free_nid_bits(struct f2fs_sb_info * sbi)2486 static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
2487 {
2488 struct f2fs_nm_info *nm_i = NM_I(sbi);
2489 unsigned int i, idx;
2490 nid_t nid;
2491
2492 f2fs_down_read(&nm_i->nat_tree_lock);
2493
2494 for (i = 0; i < nm_i->nat_blocks; i++) {
2495 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2496 continue;
2497 if (!nm_i->free_nid_count[i])
2498 continue;
2499 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
2500 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2501 NAT_ENTRY_PER_BLOCK, idx);
2502 if (idx >= NAT_ENTRY_PER_BLOCK)
2503 break;
2504
2505 nid = i * NAT_ENTRY_PER_BLOCK + idx;
2506 add_free_nid(sbi, nid, true, false);
2507
2508 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
2509 goto out;
2510 }
2511 }
2512 out:
2513 scan_curseg_cache(sbi);
2514
2515 f2fs_up_read(&nm_i->nat_tree_lock);
2516 }
2517
__f2fs_build_free_nids(struct f2fs_sb_info * sbi,bool sync,bool mount)2518 static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
2519 bool sync, bool mount)
2520 {
2521 struct f2fs_nm_info *nm_i = NM_I(sbi);
2522 int i = 0, ret;
2523 nid_t nid = nm_i->next_scan_nid;
2524
2525 if (unlikely(nid >= nm_i->max_nid))
2526 nid = 0;
2527
2528 if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
2529 nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
2530
2531 /* Enough entries */
2532 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2533 return 0;
2534
2535 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
2536 return 0;
2537
2538 if (!mount) {
2539 /* try to find free nids in free_nid_bitmap */
2540 scan_free_nid_bits(sbi);
2541
2542 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
2543 return 0;
2544 }
2545
2546 /* readahead nat pages to be scanned */
2547 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
2548 META_NAT, true);
2549
2550 f2fs_down_read(&nm_i->nat_tree_lock);
2551
2552 while (1) {
2553 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2554 nm_i->nat_block_bitmap)) {
2555 struct page *page = get_current_nat_page(sbi, nid);
2556
2557 if (IS_ERR(page)) {
2558 ret = PTR_ERR(page);
2559 } else {
2560 ret = scan_nat_page(sbi, page, nid);
2561 f2fs_put_page(page, 1);
2562 }
2563
2564 if (ret) {
2565 f2fs_up_read(&nm_i->nat_tree_lock);
2566
2567 if (ret == -EFSCORRUPTED) {
2568 f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
2569 set_sbi_flag(sbi, SBI_NEED_FSCK);
2570 f2fs_handle_error(sbi,
2571 ERROR_INCONSISTENT_NAT);
2572 }
2573
2574 return ret;
2575 }
2576 }
2577
2578 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
2579 if (unlikely(nid >= nm_i->max_nid))
2580 nid = 0;
2581
2582 if (++i >= FREE_NID_PAGES)
2583 break;
2584 }
2585
2586 /* go to the next free nat pages to find free nids abundantly */
2587 nm_i->next_scan_nid = nid;
2588
2589 /* find free nids from current sum_pages */
2590 scan_curseg_cache(sbi);
2591
2592 f2fs_up_read(&nm_i->nat_tree_lock);
2593
2594 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
2595 nm_i->ra_nid_pages, META_NAT, false);
2596
2597 return 0;
2598 }
2599
f2fs_build_free_nids(struct f2fs_sb_info * sbi,bool sync,bool mount)2600 int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
2601 {
2602 int ret;
2603
2604 mutex_lock(&NM_I(sbi)->build_lock);
2605 ret = __f2fs_build_free_nids(sbi, sync, mount);
2606 mutex_unlock(&NM_I(sbi)->build_lock);
2607
2608 return ret;
2609 }
2610
2611 /*
2612 * If this function returns success, caller can obtain a new nid
2613 * from second parameter of this function.
2614 * The returned nid could be used ino as well as nid when inode is created.
2615 */
f2fs_alloc_nid(struct f2fs_sb_info * sbi,nid_t * nid)2616 bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
2617 {
2618 struct f2fs_nm_info *nm_i = NM_I(sbi);
2619 struct free_nid *i = NULL;
2620 retry:
2621 if (time_to_inject(sbi, FAULT_ALLOC_NID))
2622 return false;
2623
2624 spin_lock(&nm_i->nid_list_lock);
2625
2626 if (unlikely(nm_i->available_nids == 0)) {
2627 spin_unlock(&nm_i->nid_list_lock);
2628 return false;
2629 }
2630
2631 /* We should not use stale free nids created by f2fs_build_free_nids */
2632 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
2633 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2634 i = list_first_entry(&nm_i->free_nid_list,
2635 struct free_nid, list);
2636 *nid = i->nid;
2637
2638 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
2639 nm_i->available_nids--;
2640
2641 update_free_nid_bitmap(sbi, *nid, false, false);
2642
2643 spin_unlock(&nm_i->nid_list_lock);
2644 return true;
2645 }
2646 spin_unlock(&nm_i->nid_list_lock);
2647
2648 /* Let's scan nat pages and its caches to get free nids */
2649 if (!f2fs_build_free_nids(sbi, true, false))
2650 goto retry;
2651 return false;
2652 }
2653
2654 /*
2655 * f2fs_alloc_nid() should be called prior to this function.
2656 */
f2fs_alloc_nid_done(struct f2fs_sb_info * sbi,nid_t nid)2657 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
2658 {
2659 struct f2fs_nm_info *nm_i = NM_I(sbi);
2660 struct free_nid *i;
2661
2662 spin_lock(&nm_i->nid_list_lock);
2663 i = __lookup_free_nid_list(nm_i, nid);
2664 f2fs_bug_on(sbi, !i);
2665 __remove_free_nid(sbi, i, PREALLOC_NID);
2666 spin_unlock(&nm_i->nid_list_lock);
2667
2668 kmem_cache_free(free_nid_slab, i);
2669 }
2670
2671 /*
2672 * f2fs_alloc_nid() should be called prior to this function.
2673 */
f2fs_alloc_nid_failed(struct f2fs_sb_info * sbi,nid_t nid)2674 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
2675 {
2676 struct f2fs_nm_info *nm_i = NM_I(sbi);
2677 struct free_nid *i;
2678 bool need_free = false;
2679
2680 if (!nid)
2681 return;
2682
2683 spin_lock(&nm_i->nid_list_lock);
2684 i = __lookup_free_nid_list(nm_i, nid);
2685 f2fs_bug_on(sbi, !i);
2686
2687 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
2688 __remove_free_nid(sbi, i, PREALLOC_NID);
2689 need_free = true;
2690 } else {
2691 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
2692 }
2693
2694 nm_i->available_nids++;
2695
2696 update_free_nid_bitmap(sbi, nid, true, false);
2697
2698 spin_unlock(&nm_i->nid_list_lock);
2699
2700 if (need_free)
2701 kmem_cache_free(free_nid_slab, i);
2702 }
2703
f2fs_try_to_free_nids(struct f2fs_sb_info * sbi,int nr_shrink)2704 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
2705 {
2706 struct f2fs_nm_info *nm_i = NM_I(sbi);
2707 int nr = nr_shrink;
2708
2709 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2710 return 0;
2711
2712 if (!mutex_trylock(&nm_i->build_lock))
2713 return 0;
2714
2715 while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) {
2716 struct free_nid *i, *next;
2717 unsigned int batch = SHRINK_NID_BATCH_SIZE;
2718
2719 spin_lock(&nm_i->nid_list_lock);
2720 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
2721 if (!nr_shrink || !batch ||
2722 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2723 break;
2724 __remove_free_nid(sbi, i, FREE_NID);
2725 kmem_cache_free(free_nid_slab, i);
2726 nr_shrink--;
2727 batch--;
2728 }
2729 spin_unlock(&nm_i->nid_list_lock);
2730 }
2731
2732 mutex_unlock(&nm_i->build_lock);
2733
2734 return nr - nr_shrink;
2735 }
2736
f2fs_recover_inline_xattr(struct inode * inode,struct page * page)2737 int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
2738 {
2739 void *src_addr, *dst_addr;
2740 size_t inline_size;
2741 struct page *ipage;
2742 struct f2fs_inode *ri;
2743
2744 ipage = f2fs_get_inode_page(F2FS_I_SB(inode), inode->i_ino);
2745 if (IS_ERR(ipage))
2746 return PTR_ERR(ipage);
2747
2748 ri = F2FS_INODE(page);
2749 if (ri->i_inline & F2FS_INLINE_XATTR) {
2750 if (!f2fs_has_inline_xattr(inode)) {
2751 set_inode_flag(inode, FI_INLINE_XATTR);
2752 stat_inc_inline_xattr(inode);
2753 }
2754 } else {
2755 if (f2fs_has_inline_xattr(inode)) {
2756 stat_dec_inline_xattr(inode);
2757 clear_inode_flag(inode, FI_INLINE_XATTR);
2758 }
2759 goto update_inode;
2760 }
2761
2762 dst_addr = inline_xattr_addr(inode, ipage);
2763 src_addr = inline_xattr_addr(inode, page);
2764 inline_size = inline_xattr_size(inode);
2765
2766 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
2767 memcpy(dst_addr, src_addr, inline_size);
2768 update_inode:
2769 f2fs_update_inode(inode, ipage);
2770 f2fs_put_page(ipage, 1);
2771 return 0;
2772 }
2773
f2fs_recover_xattr_data(struct inode * inode,struct page * page)2774 int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
2775 {
2776 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2777 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
2778 nid_t new_xnid;
2779 struct dnode_of_data dn;
2780 struct node_info ni;
2781 struct page *xpage;
2782 int err;
2783
2784 if (!prev_xnid)
2785 goto recover_xnid;
2786
2787 /* 1: invalidate the previous xattr nid */
2788 err = f2fs_get_node_info(sbi, prev_xnid, &ni, false);
2789 if (err)
2790 return err;
2791
2792 f2fs_invalidate_blocks(sbi, ni.blk_addr, 1);
2793 dec_valid_node_count(sbi, inode, false);
2794 set_node_addr(sbi, &ni, NULL_ADDR, false);
2795
2796 recover_xnid:
2797 /* 2: update xattr nid in inode */
2798 if (!f2fs_alloc_nid(sbi, &new_xnid))
2799 return -ENOSPC;
2800
2801 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
2802 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
2803 if (IS_ERR(xpage)) {
2804 f2fs_alloc_nid_failed(sbi, new_xnid);
2805 return PTR_ERR(xpage);
2806 }
2807
2808 f2fs_alloc_nid_done(sbi, new_xnid);
2809 f2fs_update_inode_page(inode);
2810
2811 /* 3: update and set xattr node page dirty */
2812 if (page) {
2813 memcpy(F2FS_NODE(xpage), F2FS_NODE(page),
2814 VALID_XATTR_BLOCK_SIZE);
2815 set_page_dirty(xpage);
2816 }
2817 f2fs_put_page(xpage, 1);
2818
2819 return 0;
2820 }
2821
f2fs_recover_inode_page(struct f2fs_sb_info * sbi,struct page * page)2822 int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
2823 {
2824 struct f2fs_inode *src, *dst;
2825 nid_t ino = ino_of_node(page);
2826 struct node_info old_ni, new_ni;
2827 struct page *ipage;
2828 int err;
2829
2830 err = f2fs_get_node_info(sbi, ino, &old_ni, false);
2831 if (err)
2832 return err;
2833
2834 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2835 return -EINVAL;
2836 retry:
2837 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
2838 if (!ipage) {
2839 memalloc_retry_wait(GFP_NOFS);
2840 goto retry;
2841 }
2842
2843 /* Should not use this inode from free nid list */
2844 remove_free_nid(sbi, ino);
2845
2846 if (!PageUptodate(ipage))
2847 SetPageUptodate(ipage);
2848 fill_node_footer(ipage, ino, ino, 0, true);
2849 set_cold_node(ipage, false);
2850
2851 src = F2FS_INODE(page);
2852 dst = F2FS_INODE(ipage);
2853
2854 memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
2855 dst->i_size = 0;
2856 dst->i_blocks = cpu_to_le64(1);
2857 dst->i_links = cpu_to_le32(1);
2858 dst->i_xattr_nid = 0;
2859 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
2860 if (dst->i_inline & F2FS_EXTRA_ATTR) {
2861 dst->i_extra_isize = src->i_extra_isize;
2862
2863 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
2864 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2865 i_inline_xattr_size))
2866 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2867
2868 if (f2fs_sb_has_project_quota(sbi) &&
2869 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2870 i_projid))
2871 dst->i_projid = src->i_projid;
2872
2873 if (f2fs_sb_has_inode_crtime(sbi) &&
2874 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2875 i_crtime_nsec)) {
2876 dst->i_crtime = src->i_crtime;
2877 dst->i_crtime_nsec = src->i_crtime_nsec;
2878 }
2879 }
2880
2881 new_ni = old_ni;
2882 new_ni.ino = ino;
2883
2884 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
2885 WARN_ON(1);
2886 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
2887 inc_valid_inode_count(sbi);
2888 set_page_dirty(ipage);
2889 f2fs_put_page(ipage, 1);
2890 return 0;
2891 }
2892
f2fs_restore_node_summary(struct f2fs_sb_info * sbi,unsigned int segno,struct f2fs_summary_block * sum)2893 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2894 unsigned int segno, struct f2fs_summary_block *sum)
2895 {
2896 struct f2fs_node *rn;
2897 struct f2fs_summary *sum_entry;
2898 block_t addr;
2899 int i, idx, last_offset, nrpages;
2900
2901 /* scan the node segment */
2902 last_offset = BLKS_PER_SEG(sbi);
2903 addr = START_BLOCK(sbi, segno);
2904 sum_entry = &sum->entries[0];
2905
2906 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
2907 nrpages = bio_max_segs(last_offset - i);
2908
2909 /* readahead node pages */
2910 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
2911
2912 for (idx = addr; idx < addr + nrpages; idx++) {
2913 struct page *page = f2fs_get_tmp_page(sbi, idx);
2914
2915 if (IS_ERR(page))
2916 return PTR_ERR(page);
2917
2918 rn = F2FS_NODE(page);
2919 sum_entry->nid = rn->footer.nid;
2920 sum_entry->version = 0;
2921 sum_entry->ofs_in_node = 0;
2922 sum_entry++;
2923 f2fs_put_page(page, 1);
2924 }
2925
2926 invalidate_mapping_pages(META_MAPPING(sbi), addr,
2927 addr + nrpages);
2928 }
2929 return 0;
2930 }
2931
remove_nats_in_journal(struct f2fs_sb_info * sbi)2932 static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
2933 {
2934 struct f2fs_nm_info *nm_i = NM_I(sbi);
2935 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2936 struct f2fs_journal *journal = curseg->journal;
2937 int i;
2938
2939 down_write(&curseg->journal_rwsem);
2940 for (i = 0; i < nats_in_cursum(journal); i++) {
2941 struct nat_entry *ne;
2942 struct f2fs_nat_entry raw_ne;
2943 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
2944
2945 if (f2fs_check_nid_range(sbi, nid))
2946 continue;
2947
2948 raw_ne = nat_in_journal(journal, i);
2949
2950 ne = __lookup_nat_cache(nm_i, nid);
2951 if (!ne) {
2952 ne = __alloc_nat_entry(sbi, nid, true);
2953 __init_nat_entry(nm_i, ne, &raw_ne, true);
2954 }
2955
2956 /*
2957 * if a free nat in journal has not been used after last
2958 * checkpoint, we should remove it from available nids,
2959 * since later we will add it again.
2960 */
2961 if (!get_nat_flag(ne, IS_DIRTY) &&
2962 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2963 spin_lock(&nm_i->nid_list_lock);
2964 nm_i->available_nids--;
2965 spin_unlock(&nm_i->nid_list_lock);
2966 }
2967
2968 __set_nat_cache_dirty(nm_i, ne);
2969 }
2970 update_nats_in_cursum(journal, -i);
2971 up_write(&curseg->journal_rwsem);
2972 }
2973
__adjust_nat_entry_set(struct nat_entry_set * nes,struct list_head * head,int max)2974 static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2975 struct list_head *head, int max)
2976 {
2977 struct nat_entry_set *cur;
2978
2979 if (nes->entry_cnt >= max)
2980 goto add_out;
2981
2982 list_for_each_entry(cur, head, set_list) {
2983 if (cur->entry_cnt >= nes->entry_cnt) {
2984 list_add(&nes->set_list, cur->set_list.prev);
2985 return;
2986 }
2987 }
2988 add_out:
2989 list_add_tail(&nes->set_list, head);
2990 }
2991
__update_nat_bits(struct f2fs_sb_info * sbi,nid_t start_nid,struct page * page)2992 static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
2993 struct page *page)
2994 {
2995 struct f2fs_nm_info *nm_i = NM_I(sbi);
2996 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2997 struct f2fs_nat_block *nat_blk = page_address(page);
2998 int valid = 0;
2999 int i = 0;
3000
3001 if (!enabled_nat_bits(sbi, NULL))
3002 return;
3003
3004 if (nat_index == 0) {
3005 valid = 1;
3006 i = 1;
3007 }
3008 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
3009 if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
3010 valid++;
3011 }
3012 if (valid == 0) {
3013 __set_bit_le(nat_index, nm_i->empty_nat_bits);
3014 __clear_bit_le(nat_index, nm_i->full_nat_bits);
3015 return;
3016 }
3017
3018 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
3019 if (valid == NAT_ENTRY_PER_BLOCK)
3020 __set_bit_le(nat_index, nm_i->full_nat_bits);
3021 else
3022 __clear_bit_le(nat_index, nm_i->full_nat_bits);
3023 }
3024
__flush_nat_entry_set(struct f2fs_sb_info * sbi,struct nat_entry_set * set,struct cp_control * cpc)3025 static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
3026 struct nat_entry_set *set, struct cp_control *cpc)
3027 {
3028 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3029 struct f2fs_journal *journal = curseg->journal;
3030 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
3031 bool to_journal = true;
3032 struct f2fs_nat_block *nat_blk;
3033 struct nat_entry *ne, *cur;
3034 struct page *page = NULL;
3035
3036 /*
3037 * there are two steps to flush nat entries:
3038 * #1, flush nat entries to journal in current hot data summary block.
3039 * #2, flush nat entries to nat page.
3040 */
3041 if (enabled_nat_bits(sbi, cpc) ||
3042 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
3043 to_journal = false;
3044
3045 if (to_journal) {
3046 down_write(&curseg->journal_rwsem);
3047 } else {
3048 page = get_next_nat_page(sbi, start_nid);
3049 if (IS_ERR(page))
3050 return PTR_ERR(page);
3051
3052 nat_blk = page_address(page);
3053 f2fs_bug_on(sbi, !nat_blk);
3054 }
3055
3056 /* flush dirty nats in nat entry set */
3057 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
3058 struct f2fs_nat_entry *raw_ne;
3059 nid_t nid = nat_get_nid(ne);
3060 int offset;
3061
3062 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
3063
3064 if (to_journal) {
3065 offset = f2fs_lookup_journal_in_cursum(journal,
3066 NAT_JOURNAL, nid, 1);
3067 f2fs_bug_on(sbi, offset < 0);
3068 raw_ne = &nat_in_journal(journal, offset);
3069 nid_in_journal(journal, offset) = cpu_to_le32(nid);
3070 } else {
3071 raw_ne = &nat_blk->entries[nid - start_nid];
3072 }
3073 raw_nat_from_node_info(raw_ne, &ne->ni);
3074 nat_reset_flag(ne);
3075 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
3076 if (nat_get_blkaddr(ne) == NULL_ADDR) {
3077 add_free_nid(sbi, nid, false, true);
3078 } else {
3079 spin_lock(&NM_I(sbi)->nid_list_lock);
3080 update_free_nid_bitmap(sbi, nid, false, false);
3081 spin_unlock(&NM_I(sbi)->nid_list_lock);
3082 }
3083 }
3084
3085 if (to_journal) {
3086 up_write(&curseg->journal_rwsem);
3087 } else {
3088 __update_nat_bits(sbi, start_nid, page);
3089 f2fs_put_page(page, 1);
3090 }
3091
3092 /* Allow dirty nats by node block allocation in write_begin */
3093 if (!set->entry_cnt) {
3094 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
3095 kmem_cache_free(nat_entry_set_slab, set);
3096 }
3097 return 0;
3098 }
3099
3100 /*
3101 * This function is called during the checkpointing process.
3102 */
f2fs_flush_nat_entries(struct f2fs_sb_info * sbi,struct cp_control * cpc)3103 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
3104 {
3105 struct f2fs_nm_info *nm_i = NM_I(sbi);
3106 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
3107 struct f2fs_journal *journal = curseg->journal;
3108 struct nat_entry_set *setvec[NAT_VEC_SIZE];
3109 struct nat_entry_set *set, *tmp;
3110 unsigned int found;
3111 nid_t set_idx = 0;
3112 LIST_HEAD(sets);
3113 int err = 0;
3114
3115 /*
3116 * during unmount, let's flush nat_bits before checking
3117 * nat_cnt[DIRTY_NAT].
3118 */
3119 if (enabled_nat_bits(sbi, cpc)) {
3120 f2fs_down_write(&nm_i->nat_tree_lock);
3121 remove_nats_in_journal(sbi);
3122 f2fs_up_write(&nm_i->nat_tree_lock);
3123 }
3124
3125 if (!nm_i->nat_cnt[DIRTY_NAT])
3126 return 0;
3127
3128 f2fs_down_write(&nm_i->nat_tree_lock);
3129
3130 /*
3131 * if there are no enough space in journal to store dirty nat
3132 * entries, remove all entries from journal and merge them
3133 * into nat entry set.
3134 */
3135 if (enabled_nat_bits(sbi, cpc) ||
3136 !__has_cursum_space(journal,
3137 nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
3138 remove_nats_in_journal(sbi);
3139
3140 while ((found = __gang_lookup_nat_set(nm_i,
3141 set_idx, NAT_VEC_SIZE, setvec))) {
3142 unsigned idx;
3143
3144 set_idx = setvec[found - 1]->set + 1;
3145 for (idx = 0; idx < found; idx++)
3146 __adjust_nat_entry_set(setvec[idx], &sets,
3147 MAX_NAT_JENTRIES(journal));
3148 }
3149
3150 /* flush dirty nats in nat entry set */
3151 list_for_each_entry_safe(set, tmp, &sets, set_list) {
3152 err = __flush_nat_entry_set(sbi, set, cpc);
3153 if (err)
3154 break;
3155 }
3156
3157 f2fs_up_write(&nm_i->nat_tree_lock);
3158 /* Allow dirty nats by node block allocation in write_begin */
3159
3160 return err;
3161 }
3162
__get_nat_bitmaps(struct f2fs_sb_info * sbi)3163 static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
3164 {
3165 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3166 struct f2fs_nm_info *nm_i = NM_I(sbi);
3167 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
3168 unsigned int i;
3169 __u64 cp_ver = cur_cp_version(ckpt);
3170 block_t nat_bits_addr;
3171
3172 if (!enabled_nat_bits(sbi, NULL))
3173 return 0;
3174
3175 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3176 nm_i->nat_bits = f2fs_kvzalloc(sbi,
3177 F2FS_BLK_TO_BYTES(nm_i->nat_bits_blocks), GFP_KERNEL);
3178 if (!nm_i->nat_bits)
3179 return -ENOMEM;
3180
3181 nat_bits_addr = __start_cp_addr(sbi) + BLKS_PER_SEG(sbi) -
3182 nm_i->nat_bits_blocks;
3183 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
3184 struct page *page;
3185
3186 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
3187 if (IS_ERR(page))
3188 return PTR_ERR(page);
3189
3190 memcpy(nm_i->nat_bits + F2FS_BLK_TO_BYTES(i),
3191 page_address(page), F2FS_BLKSIZE);
3192 f2fs_put_page(page, 1);
3193 }
3194
3195 cp_ver |= (cur_cp_crc(ckpt) << 32);
3196 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
3197 disable_nat_bits(sbi, true);
3198 return 0;
3199 }
3200
3201 nm_i->full_nat_bits = nm_i->nat_bits + 8;
3202 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
3203
3204 f2fs_notice(sbi, "Found nat_bits in checkpoint");
3205 return 0;
3206 }
3207
load_free_nid_bitmap(struct f2fs_sb_info * sbi)3208 static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
3209 {
3210 struct f2fs_nm_info *nm_i = NM_I(sbi);
3211 unsigned int i = 0;
3212 nid_t nid, last_nid;
3213
3214 if (!enabled_nat_bits(sbi, NULL))
3215 return;
3216
3217 for (i = 0; i < nm_i->nat_blocks; i++) {
3218 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
3219 if (i >= nm_i->nat_blocks)
3220 break;
3221
3222 __set_bit_le(i, nm_i->nat_block_bitmap);
3223
3224 nid = i * NAT_ENTRY_PER_BLOCK;
3225 last_nid = nid + NAT_ENTRY_PER_BLOCK;
3226
3227 spin_lock(&NM_I(sbi)->nid_list_lock);
3228 for (; nid < last_nid; nid++)
3229 update_free_nid_bitmap(sbi, nid, true, true);
3230 spin_unlock(&NM_I(sbi)->nid_list_lock);
3231 }
3232
3233 for (i = 0; i < nm_i->nat_blocks; i++) {
3234 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
3235 if (i >= nm_i->nat_blocks)
3236 break;
3237
3238 __set_bit_le(i, nm_i->nat_block_bitmap);
3239 }
3240 }
3241
init_node_manager(struct f2fs_sb_info * sbi)3242 static int init_node_manager(struct f2fs_sb_info *sbi)
3243 {
3244 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
3245 struct f2fs_nm_info *nm_i = NM_I(sbi);
3246 unsigned char *version_bitmap;
3247 unsigned int nat_segs;
3248 int err;
3249
3250 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
3251
3252 /* segment_count_nat includes pair segment so divide to 2. */
3253 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
3254 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
3255 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
3256
3257 /* not used nids: 0, node, meta, (and root counted as valid node) */
3258 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
3259 F2FS_RESERVED_NODE_NUM;
3260 nm_i->nid_cnt[FREE_NID] = 0;
3261 nm_i->nid_cnt[PREALLOC_NID] = 0;
3262 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
3263 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
3264 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
3265 nm_i->max_rf_node_blocks = DEF_RF_NODE_BLOCKS;
3266
3267 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
3268 INIT_LIST_HEAD(&nm_i->free_nid_list);
3269 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
3270 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
3271 INIT_LIST_HEAD(&nm_i->nat_entries);
3272 spin_lock_init(&nm_i->nat_list_lock);
3273
3274 mutex_init(&nm_i->build_lock);
3275 spin_lock_init(&nm_i->nid_list_lock);
3276 init_f2fs_rwsem(&nm_i->nat_tree_lock);
3277
3278 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
3279 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
3280 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
3281 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
3282 GFP_KERNEL);
3283 if (!nm_i->nat_bitmap)
3284 return -ENOMEM;
3285
3286 if (!test_opt(sbi, NAT_BITS))
3287 disable_nat_bits(sbi, true);
3288
3289 err = __get_nat_bitmaps(sbi);
3290 if (err)
3291 return err;
3292
3293 #ifdef CONFIG_F2FS_CHECK_FS
3294 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
3295 GFP_KERNEL);
3296 if (!nm_i->nat_bitmap_mir)
3297 return -ENOMEM;
3298 #endif
3299
3300 return 0;
3301 }
3302
init_free_nid_cache(struct f2fs_sb_info * sbi)3303 static int init_free_nid_cache(struct f2fs_sb_info *sbi)
3304 {
3305 struct f2fs_nm_info *nm_i = NM_I(sbi);
3306 int i;
3307
3308 nm_i->free_nid_bitmap =
3309 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *),
3310 nm_i->nat_blocks),
3311 GFP_KERNEL);
3312 if (!nm_i->free_nid_bitmap)
3313 return -ENOMEM;
3314
3315 for (i = 0; i < nm_i->nat_blocks; i++) {
3316 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
3317 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
3318 if (!nm_i->free_nid_bitmap[i])
3319 return -ENOMEM;
3320 }
3321
3322 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
3323 GFP_KERNEL);
3324 if (!nm_i->nat_block_bitmap)
3325 return -ENOMEM;
3326
3327 nm_i->free_nid_count =
3328 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
3329 nm_i->nat_blocks),
3330 GFP_KERNEL);
3331 if (!nm_i->free_nid_count)
3332 return -ENOMEM;
3333 return 0;
3334 }
3335
f2fs_build_node_manager(struct f2fs_sb_info * sbi)3336 int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
3337 {
3338 int err;
3339
3340 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3341 GFP_KERNEL);
3342 if (!sbi->nm_info)
3343 return -ENOMEM;
3344
3345 err = init_node_manager(sbi);
3346 if (err)
3347 return err;
3348
3349 err = init_free_nid_cache(sbi);
3350 if (err)
3351 return err;
3352
3353 /* load free nid status from nat_bits table */
3354 load_free_nid_bitmap(sbi);
3355
3356 return f2fs_build_free_nids(sbi, true, true);
3357 }
3358
f2fs_destroy_node_manager(struct f2fs_sb_info * sbi)3359 void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
3360 {
3361 struct f2fs_nm_info *nm_i = NM_I(sbi);
3362 struct free_nid *i, *next_i;
3363 void *vec[NAT_VEC_SIZE];
3364 struct nat_entry **natvec = (struct nat_entry **)vec;
3365 struct nat_entry_set **setvec = (struct nat_entry_set **)vec;
3366 nid_t nid = 0;
3367 unsigned int found;
3368
3369 if (!nm_i)
3370 return;
3371
3372 /* destroy free nid list */
3373 spin_lock(&nm_i->nid_list_lock);
3374 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
3375 __remove_free_nid(sbi, i, FREE_NID);
3376 spin_unlock(&nm_i->nid_list_lock);
3377 kmem_cache_free(free_nid_slab, i);
3378 spin_lock(&nm_i->nid_list_lock);
3379 }
3380 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3381 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3382 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
3383 spin_unlock(&nm_i->nid_list_lock);
3384
3385 /* destroy nat cache */
3386 f2fs_down_write(&nm_i->nat_tree_lock);
3387 while ((found = __gang_lookup_nat_cache(nm_i,
3388 nid, NAT_VEC_SIZE, natvec))) {
3389 unsigned idx;
3390
3391 nid = nat_get_nid(natvec[found - 1]) + 1;
3392 for (idx = 0; idx < found; idx++) {
3393 spin_lock(&nm_i->nat_list_lock);
3394 list_del(&natvec[idx]->list);
3395 spin_unlock(&nm_i->nat_list_lock);
3396
3397 __del_from_nat_cache(nm_i, natvec[idx]);
3398 }
3399 }
3400 f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
3401
3402 /* destroy nat set cache */
3403 nid = 0;
3404 memset(vec, 0, sizeof(void *) * NAT_VEC_SIZE);
3405 while ((found = __gang_lookup_nat_set(nm_i,
3406 nid, NAT_VEC_SIZE, setvec))) {
3407 unsigned idx;
3408
3409 nid = setvec[found - 1]->set + 1;
3410 for (idx = 0; idx < found; idx++) {
3411 /* entry_cnt is not zero, when cp_error was occurred */
3412 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3413 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3414 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3415 }
3416 }
3417 f2fs_up_write(&nm_i->nat_tree_lock);
3418
3419 kvfree(nm_i->nat_block_bitmap);
3420 if (nm_i->free_nid_bitmap) {
3421 int i;
3422
3423 for (i = 0; i < nm_i->nat_blocks; i++)
3424 kvfree(nm_i->free_nid_bitmap[i]);
3425 kvfree(nm_i->free_nid_bitmap);
3426 }
3427 kvfree(nm_i->free_nid_count);
3428
3429 kvfree(nm_i->nat_bitmap);
3430 kvfree(nm_i->nat_bits);
3431 #ifdef CONFIG_F2FS_CHECK_FS
3432 kvfree(nm_i->nat_bitmap_mir);
3433 #endif
3434 sbi->nm_info = NULL;
3435 kfree(nm_i);
3436 }
3437
f2fs_create_node_manager_caches(void)3438 int __init f2fs_create_node_manager_caches(void)
3439 {
3440 nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry",
3441 sizeof(struct nat_entry));
3442 if (!nat_entry_slab)
3443 goto fail;
3444
3445 free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid",
3446 sizeof(struct free_nid));
3447 if (!free_nid_slab)
3448 goto destroy_nat_entry;
3449
3450 nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set",
3451 sizeof(struct nat_entry_set));
3452 if (!nat_entry_set_slab)
3453 goto destroy_free_nid;
3454
3455 fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry",
3456 sizeof(struct fsync_node_entry));
3457 if (!fsync_node_entry_slab)
3458 goto destroy_nat_entry_set;
3459 return 0;
3460
3461 destroy_nat_entry_set:
3462 kmem_cache_destroy(nat_entry_set_slab);
3463 destroy_free_nid:
3464 kmem_cache_destroy(free_nid_slab);
3465 destroy_nat_entry:
3466 kmem_cache_destroy(nat_entry_slab);
3467 fail:
3468 return -ENOMEM;
3469 }
3470
f2fs_destroy_node_manager_caches(void)3471 void f2fs_destroy_node_manager_caches(void)
3472 {
3473 kmem_cache_destroy(fsync_node_entry_slab);
3474 kmem_cache_destroy(nat_entry_set_slab);
3475 kmem_cache_destroy(free_nid_slab);
3476 kmem_cache_destroy(nat_entry_slab);
3477 }
3478