blob: 3a8f7afa50596baf31b6d47661cbb1737de2eb26 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003 * fs/f2fs/node.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/mpage.h>
11#include <linux/backing-dev.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"
Yunlei He87905682017-07-18 09:48:12 +080019#include "xattr.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090020#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090021
Chao Yu4d57b862018-05-30 00:20:41 +080022#define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock)
Gu Zhengf978f5a2014-02-21 18:08:29 +080023
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090024static struct kmem_cache *nat_entry_slab;
25static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080026static struct kmem_cache *nat_entry_set_slab;
Chao Yu50fa53e2018-08-02 23:03:19 +080027static struct kmem_cache *fsync_node_entry_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090028
Jaegeuk Kima4f843b2018-04-23 23:02:31 -060029/*
30 * Check whether the given nid is within node id range.
31 */
Chao Yu4d57b862018-05-30 00:20:41 +080032int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kima4f843b2018-04-23 23:02:31 -060033{
34 if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
35 set_sbi_flag(sbi, SBI_NEED_FSCK);
Joe Perchesdcbb4c12019-06-18 17:48:42 +080036 f2fs_warn(sbi, "%s: out-of-range nid=%x, run fsck to fix.",
37 __func__, nid);
Chao Yu10f966b2019-06-20 11:36:14 +080038 return -EFSCORRUPTED;
Jaegeuk Kima4f843b2018-04-23 23:02:31 -060039 }
40 return 0;
41}
42
Chao Yu4d57b862018-05-30 00:20:41 +080043bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090044{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090045 struct f2fs_nm_info *nm_i = NM_I(sbi);
Sahitya Tummalad6d2b492021-03-16 14:59:18 +053046 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090047 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080048 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090049 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090050 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090051
Sahitya Tummalad6d2b492021-03-16 14:59:18 +053052 if (!nm_i)
53 return true;
54
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090055 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080056
57 /* only uses low memory */
58 avail_ram = val.totalram - val.totalhigh;
59
Chao Yu429511c2015-02-05 17:54:31 +080060 /*
61 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
62 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090063 if (type == FREE_NIDS) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +080064 mem_size = (nm_i->nid_cnt[FREE_NID] *
Chao Yub8559dc2016-10-12 19:28:29 +080065 sizeof(struct free_nid)) >> PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080066 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090067 } else if (type == NAT_ENTRIES) {
Jaegeuk Kima95ba662020-11-06 13:22:05 -080068 mem_size = (nm_i->nat_cnt[TOTAL_NAT] *
69 sizeof(struct nat_entry)) >> PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080070 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kime589c2c2016-06-02 15:24:24 -070071 if (excess_cached_nats(sbi))
72 res = false;
Jaegeuk Kima1257022015-10-08 10:40:07 -070073 } else if (type == DIRTY_DENTS) {
74 if (sbi->sb->s_bdi->wb.dirty_exceeded)
75 return false;
76 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
77 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080078 } else if (type == INO_ENTRIES) {
79 int i;
80
Chao Yu39d787b2017-09-29 13:59:38 +080081 for (i = 0; i < MAX_INO_ENTRY; i++)
Kinglong Mee8f73cbb2017-03-18 09:26:13 +080082 mem_size += sbi->im[i].ino_num *
83 sizeof(struct ino_entry);
84 mem_size >>= PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080085 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080086 } else if (type == EXTENT_CACHE) {
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080087 mem_size = (atomic_read(&sbi->total_ext_tree) *
88 sizeof(struct extent_tree) +
Chao Yu429511c2015-02-05 17:54:31 +080089 atomic_read(&sbi->total_ext_node) *
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030090 sizeof(struct extent_node)) >> PAGE_SHIFT;
Chao Yu429511c2015-02-05 17:54:31 +080091 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -070092 } else if (type == INMEM_PAGES) {
93 /* it allows 20% / total_ram for inmemory pages */
94 mem_size = get_pages(sbi, F2FS_INMEM_PAGES);
95 res = mem_size < (val.totalram / 5);
Sahitya Tummalad6d2b492021-03-16 14:59:18 +053096 } else if (type == DISCARD_CACHE) {
97 mem_size = (atomic_read(&dcc->discard_cmd_cnt) *
98 sizeof(struct discard_cmd)) >> PAGE_SHIFT;
99 res = mem_size < (avail_ram * nm_i->ram_thresh / 100);
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800100 } else {
Jaegeuk Kim1663cae2016-01-09 16:14:08 -0800101 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
102 return true;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900103 }
104 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +0900105}
106
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900107static void clear_node_page_dirty(struct page *page)
108{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900109 if (PageDirty(page)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500110 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900111 clear_page_dirty_for_io(page);
Chao Yuaec2f722018-05-26 18:03:35 +0800112 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900113 }
114 ClearPageUptodate(page);
115}
116
117static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
118{
Jaegeuk Kim3acc4522020-10-25 07:35:47 -0700119 return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900120}
121
122static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
123{
124 struct page *src_page;
125 struct page *dst_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900126 pgoff_t dst_off;
127 void *src_addr;
128 void *dst_addr;
129 struct f2fs_nm_info *nm_i = NM_I(sbi);
130
Chao Yu80551d12018-07-17 20:41:46 +0800131 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900132
133 /* get current nat block page with lock */
Chao Yu80551d12018-07-17 20:41:46 +0800134 src_page = get_current_nat_page(sbi, nid);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -0700135 if (IS_ERR(src_page))
136 return src_page;
Chao Yu4d57b862018-05-30 00:20:41 +0800137 dst_page = f2fs_grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700138 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900139
140 src_addr = page_address(src_page);
141 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300142 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900143 set_page_dirty(dst_page);
144 f2fs_put_page(src_page, 1);
145
146 set_to_next_nat(nm_i, nid);
147
148 return dst_page;
149}
150
Yunlei He12f9ef32017-11-10 13:36:51 -0800151static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail)
152{
153 struct nat_entry *new;
154
155 if (no_fail)
Chao Yu2882d342018-01-25 18:57:25 +0800156 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800157 else
Chao Yu2882d342018-01-25 18:57:25 +0800158 new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800159 if (new) {
160 nat_set_nid(new, nid);
161 nat_reset_flag(new);
162 }
163 return new;
164}
165
166static void __free_nat_entry(struct nat_entry *e)
167{
168 kmem_cache_free(nat_entry_slab, e);
169}
170
171/* must be locked by nat_tree_lock */
172static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
173 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
174{
175 if (no_fail)
176 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
177 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
178 return NULL;
179
180 if (raw_ne)
181 node_info_from_raw_nat(&ne->ni, raw_ne);
Chao Yu22969152018-08-05 23:08:59 +0800182
183 spin_lock(&nm_i->nat_list_lock);
Yunlei He12f9ef32017-11-10 13:36:51 -0800184 list_add_tail(&ne->list, &nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +0800185 spin_unlock(&nm_i->nat_list_lock);
186
Jaegeuk Kima95ba662020-11-06 13:22:05 -0800187 nm_i->nat_cnt[TOTAL_NAT]++;
188 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
Yunlei He12f9ef32017-11-10 13:36:51 -0800189 return ne;
190}
191
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900192static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
193{
Chao Yu22969152018-08-05 23:08:59 +0800194 struct nat_entry *ne;
195
196 ne = radix_tree_lookup(&nm_i->nat_root, n);
197
198 /* for recent accessed nat entry, move it to tail of lru list */
199 if (ne && !get_nat_flag(ne, IS_DIRTY)) {
200 spin_lock(&nm_i->nat_list_lock);
201 if (!list_empty(&ne->list))
202 list_move_tail(&ne->list, &nm_i->nat_entries);
203 spin_unlock(&nm_i->nat_list_lock);
204 }
205
206 return ne;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900207}
208
209static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
210 nid_t start, unsigned int nr, struct nat_entry **ep)
211{
212 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
213}
214
215static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
216{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900217 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
Jaegeuk Kima95ba662020-11-06 13:22:05 -0800218 nm_i->nat_cnt[TOTAL_NAT]--;
219 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
Yunlei He12f9ef32017-11-10 13:36:51 -0800220 __free_nat_entry(e);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900221}
222
Chao Yu780de472018-03-20 23:08:30 +0800223static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
224 struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700225{
226 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
227 struct nat_entry_set *head;
228
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700229 head = radix_tree_lookup(&nm_i->nat_set_root, set);
230 if (!head) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700231 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700232
233 INIT_LIST_HEAD(&head->entry_list);
234 INIT_LIST_HEAD(&head->set_list);
235 head->set = set;
236 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800237 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700238 }
Chao Yu780de472018-03-20 23:08:30 +0800239 return head;
240}
241
242static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
243 struct nat_entry *ne)
244{
245 struct nat_entry_set *head;
246 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
247
248 if (!new_ne)
249 head = __grab_nat_entry_set(nm_i, ne);
250
251 /*
252 * update entry_cnt in below condition:
253 * 1. update NEW_ADDR to valid block address;
254 * 2. update old block address to new one;
255 */
256 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
257 !get_nat_flag(ne, IS_DIRTY)))
258 head->entry_cnt++;
259
260 set_nat_flag(ne, IS_PREALLOC, new_ne);
Chao Yufebeca62017-06-05 18:29:08 +0800261
262 if (get_nat_flag(ne, IS_DIRTY))
263 goto refresh_list;
264
Jaegeuk Kima95ba662020-11-06 13:22:05 -0800265 nm_i->nat_cnt[DIRTY_NAT]++;
266 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700267 set_nat_flag(ne, IS_DIRTY, true);
Chao Yufebeca62017-06-05 18:29:08 +0800268refresh_list:
Chao Yu22969152018-08-05 23:08:59 +0800269 spin_lock(&nm_i->nat_list_lock);
Chao Yu780de472018-03-20 23:08:30 +0800270 if (new_ne)
Chao Yufebeca62017-06-05 18:29:08 +0800271 list_del_init(&ne->list);
272 else
273 list_move_tail(&ne->list, &head->entry_list);
Chao Yu22969152018-08-05 23:08:59 +0800274 spin_unlock(&nm_i->nat_list_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700275}
276
277static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
Kinglong Mee0b28b712017-02-28 21:34:47 +0800278 struct nat_entry_set *set, struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700279{
Chao Yu22969152018-08-05 23:08:59 +0800280 spin_lock(&nm_i->nat_list_lock);
Kinglong Mee0b28b712017-02-28 21:34:47 +0800281 list_move_tail(&ne->list, &nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +0800282 spin_unlock(&nm_i->nat_list_lock);
283
Kinglong Mee0b28b712017-02-28 21:34:47 +0800284 set_nat_flag(ne, IS_DIRTY, false);
285 set->entry_cnt--;
Jaegeuk Kima95ba662020-11-06 13:22:05 -0800286 nm_i->nat_cnt[DIRTY_NAT]--;
287 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700288}
289
290static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
291 nid_t start, unsigned int nr, struct nat_entry_set **ep)
292{
293 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
294 start, nr);
295}
296
Chao Yu50fa53e2018-08-02 23:03:19 +0800297bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
298{
299 return NODE_MAPPING(sbi) == page->mapping &&
300 IS_DNODE(page) && is_cold_node(page);
301}
302
303void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
304{
305 spin_lock_init(&sbi->fsync_node_lock);
306 INIT_LIST_HEAD(&sbi->fsync_node_list);
307 sbi->fsync_seg_id = 0;
308 sbi->fsync_node_num = 0;
309}
310
311static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
312 struct page *page)
313{
314 struct fsync_node_entry *fn;
315 unsigned long flags;
316 unsigned int seq_id;
317
318 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS);
319
320 get_page(page);
321 fn->page = page;
322 INIT_LIST_HEAD(&fn->list);
323
324 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
325 list_add_tail(&fn->list, &sbi->fsync_node_list);
326 fn->seq_id = sbi->fsync_seg_id++;
327 seq_id = fn->seq_id;
328 sbi->fsync_node_num++;
329 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
330
331 return seq_id;
332}
333
334void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
335{
336 struct fsync_node_entry *fn;
337 unsigned long flags;
338
339 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
340 list_for_each_entry(fn, &sbi->fsync_node_list, list) {
341 if (fn->page == page) {
342 list_del(&fn->list);
343 sbi->fsync_node_num--;
344 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
345 kmem_cache_free(fsync_node_entry_slab, fn);
346 put_page(page);
347 return;
348 }
349 }
350 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
351 f2fs_bug_on(sbi, 1);
352}
353
354void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
355{
356 unsigned long flags;
357
358 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
359 sbi->fsync_seg_id = 0;
360 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
361}
362
Chao Yu4d57b862018-05-30 00:20:41 +0800363int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700364{
365 struct f2fs_nm_info *nm_i = NM_I(sbi);
366 struct nat_entry *e;
367 bool need = false;
368
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700369 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700370 e = __lookup_nat_cache(nm_i, nid);
371 if (e) {
372 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
373 !get_nat_flag(e, HAS_FSYNCED_INODE))
374 need = true;
375 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700376 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700377 return need;
378}
379
Chao Yu4d57b862018-05-30 00:20:41 +0800380bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900381{
382 struct f2fs_nm_info *nm_i = NM_I(sbi);
383 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700384 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900385
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700386 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900387 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700388 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700389 is_cp = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700390 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900391 return is_cp;
392}
393
Chao Yu4d57b862018-05-30 00:20:41 +0800394bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900395{
396 struct f2fs_nm_info *nm_i = NM_I(sbi);
397 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700398 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900399
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700400 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700401 e = __lookup_nat_cache(nm_i, ino);
402 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
403 (get_nat_flag(e, IS_CHECKPOINTED) ||
404 get_nat_flag(e, HAS_FSYNCED_INODE)))
405 need_update = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700406 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700407 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900408}
409
Yunlei He12f9ef32017-11-10 13:36:51 -0800410/* must be locked by nat_tree_lock */
Chao Yu1515aef2016-02-19 18:12:28 +0800411static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412 struct f2fs_nat_entry *ne)
413{
Chao Yu1515aef2016-02-19 18:12:28 +0800414 struct f2fs_nm_info *nm_i = NM_I(sbi);
Yunlei He12f9ef32017-11-10 13:36:51 -0800415 struct nat_entry *new, *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800416
Yunlei He12f9ef32017-11-10 13:36:51 -0800417 new = __alloc_nat_entry(nid, false);
418 if (!new)
419 return;
420
421 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900422 e = __lookup_nat_cache(nm_i, nid);
Yunlei He12f9ef32017-11-10 13:36:51 -0800423 if (!e)
424 e = __init_nat_entry(nm_i, new, ne, false);
425 else
Eric Biggers0c0b4712016-10-11 10:36:12 -0700426 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
427 nat_get_blkaddr(e) !=
428 le32_to_cpu(ne->block_addr) ||
Chao Yu1515aef2016-02-19 18:12:28 +0800429 nat_get_version(e) != ne->version);
Yunlei He12f9ef32017-11-10 13:36:51 -0800430 up_write(&nm_i->nat_tree_lock);
431 if (e != new)
432 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900433}
434
435static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900436 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900437{
438 struct f2fs_nm_info *nm_i = NM_I(sbi);
439 struct nat_entry *e;
Yunlei He12f9ef32017-11-10 13:36:51 -0800440 struct nat_entry *new = __alloc_nat_entry(ni->nid, true);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800441
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700442 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900443 e = __lookup_nat_cache(nm_i, ni->nid);
444 if (!e) {
Yunlei He12f9ef32017-11-10 13:36:51 -0800445 e = __init_nat_entry(nm_i, new, NULL, true);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800446 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700447 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900448 } else if (new_blkaddr == NEW_ADDR) {
449 /*
450 * when nid is reallocated,
451 * previous nat entry can be remained in nat cache.
452 * So, reinitialize it with new information.
453 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800454 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700455 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900456 }
Yunlei He12f9ef32017-11-10 13:36:51 -0800457 /* let's free early to reduce memory consumption */
458 if (e != new)
459 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900460
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900461 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700462 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
463 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900464 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700465 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900466 new_blkaddr == NEW_ADDR);
Chao Yu93770ab2019-04-15 15:26:32 +0800467 f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900468 new_blkaddr == NEW_ADDR);
469
arter97e1c42042014-08-06 23:22:50 +0900470 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900471 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
472 unsigned char version = nat_get_version(e);
Yi Zhuang5f029c02021-04-06 09:47:35 +0800473
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900474 nat_set_version(e, inc_node_version(version));
475 }
476
477 /* change address */
478 nat_set_blkaddr(e, new_blkaddr);
Chao Yu93770ab2019-04-15 15:26:32 +0800479 if (!__is_valid_data_blkaddr(new_blkaddr))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700480 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900481 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900482
483 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800484 if (ni->nid != ni->ino)
485 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700486 if (e) {
487 if (fsync_done && ni->nid == ni->ino)
488 set_nat_flag(e, HAS_FSYNCED_INODE, true);
489 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
490 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700491 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900492}
493
Chao Yu4d57b862018-05-30 00:20:41 +0800494int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900495{
496 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700497 int nr = nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900498
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700499 if (!down_write_trylock(&nm_i->nat_tree_lock))
500 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900501
Chao Yu22969152018-08-05 23:08:59 +0800502 spin_lock(&nm_i->nat_list_lock);
503 while (nr_shrink) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900504 struct nat_entry *ne;
Chao Yu22969152018-08-05 23:08:59 +0800505
506 if (list_empty(&nm_i->nat_entries))
507 break;
508
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900509 ne = list_first_entry(&nm_i->nat_entries,
510 struct nat_entry, list);
Chao Yu22969152018-08-05 23:08:59 +0800511 list_del(&ne->list);
512 spin_unlock(&nm_i->nat_list_lock);
513
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900514 __del_from_nat_cache(nm_i, ne);
515 nr_shrink--;
Chao Yu22969152018-08-05 23:08:59 +0800516
517 spin_lock(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900518 }
Chao Yu22969152018-08-05 23:08:59 +0800519 spin_unlock(&nm_i->nat_list_lock);
520
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700521 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700522 return nr - nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900523}
524
Chao Yu77357302018-07-17 00:02:17 +0800525int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
Chao Yu4d57b862018-05-30 00:20:41 +0800526 struct node_info *ni)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900527{
528 struct f2fs_nm_info *nm_i = NM_I(sbi);
529 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +0800530 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900531 nid_t start_nid = START_NID(nid);
532 struct f2fs_nat_block *nat_blk;
533 struct page *page = NULL;
534 struct f2fs_nat_entry ne;
535 struct nat_entry *e;
Yunlei He66a82d12017-04-22 18:06:26 +0800536 pgoff_t index;
Chao Yu93770ab2019-04-15 15:26:32 +0800537 block_t blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900538 int i;
539
540 ni->nid = nid;
541
542 /* Check nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700543 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900544 e = __lookup_nat_cache(nm_i, nid);
545 if (e) {
546 ni->ino = nat_get_ino(e);
547 ni->blk_addr = nat_get_blkaddr(e);
548 ni->version = nat_get_version(e);
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700549 up_read(&nm_i->nat_tree_lock);
Chao Yu77357302018-07-17 00:02:17 +0800550 return 0;
Chao Yu1515aef2016-02-19 18:12:28 +0800551 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900552
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800553 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
554
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900555 /* Check current segment summary */
Chao Yub7ad7512016-02-19 18:08:46 +0800556 down_read(&curseg->journal_rwsem);
Chao Yu4d57b862018-05-30 00:20:41 +0800557 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900558 if (i >= 0) {
Chao Yudfc08a12016-02-14 18:50:40 +0800559 ne = nat_in_journal(journal, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900560 node_info_from_raw_nat(ni, &ne);
561 }
Chao Yub7ad7512016-02-19 18:08:46 +0800562 up_read(&curseg->journal_rwsem);
Yunlei He66a82d12017-04-22 18:06:26 +0800563 if (i >= 0) {
564 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900565 goto cache;
Yunlei He66a82d12017-04-22 18:06:26 +0800566 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900567
568 /* Fill node_info from nat page */
Yunlei He66a82d12017-04-22 18:06:26 +0800569 index = current_nat_addr(sbi, nid);
570 up_read(&nm_i->nat_tree_lock);
571
Chao Yu4d57b862018-05-30 00:20:41 +0800572 page = f2fs_get_meta_page(sbi, index);
Chao Yu77357302018-07-17 00:02:17 +0800573 if (IS_ERR(page))
574 return PTR_ERR(page);
575
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900576 nat_blk = (struct f2fs_nat_block *)page_address(page);
577 ne = nat_blk->entries[nid - start_nid];
578 node_info_from_raw_nat(ni, &ne);
579 f2fs_put_page(page, 1);
580cache:
Chao Yu93770ab2019-04-15 15:26:32 +0800581 blkaddr = le32_to_cpu(ne.block_addr);
582 if (__is_valid_data_blkaddr(blkaddr) &&
583 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE))
584 return -EFAULT;
585
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900586 /* cache nat entry */
Chao Yu1515aef2016-02-19 18:12:28 +0800587 cache_nat_entry(sbi, nid, &ne);
Chao Yu77357302018-07-17 00:02:17 +0800588 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900589}
590
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700591/*
592 * readahead MAX_RA_NODE number of node pages.
593 */
Chao Yu4d57b862018-05-30 00:20:41 +0800594static void f2fs_ra_node_pages(struct page *parent, int start, int n)
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700595{
596 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
597 struct blk_plug plug;
598 int i, end;
599 nid_t nid;
600
601 blk_start_plug(&plug);
602
603 /* Then, try readahead for siblings of the desired node */
604 end = start + n;
605 end = min(end, NIDS_PER_BLOCK);
606 for (i = start; i < end; i++) {
607 nid = get_nid(parent, i, false);
Chao Yu4d57b862018-05-30 00:20:41 +0800608 f2fs_ra_node_page(sbi, nid);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700609 }
610
611 blk_finish_plug(&plug);
612}
613
Chao Yu4d57b862018-05-30 00:20:41 +0800614pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
Chao Yu3cf45742016-01-26 15:40:44 +0800615{
616 const long direct_index = ADDRS_PER_INODE(dn->inode);
Chao Yud02a6e62019-03-25 21:08:19 +0800617 const long direct_blks = ADDRS_PER_BLOCK(dn->inode);
618 const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK;
619 unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode);
Chao Yu3cf45742016-01-26 15:40:44 +0800620 int cur_level = dn->cur_level;
621 int max_level = dn->max_level;
622 pgoff_t base = 0;
623
624 if (!dn->max_level)
625 return pgofs + 1;
626
627 while (max_level-- > cur_level)
628 skipped_unit *= NIDS_PER_BLOCK;
629
630 switch (dn->max_level) {
631 case 3:
632 base += 2 * indirect_blks;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500633 fallthrough;
Chao Yu3cf45742016-01-26 15:40:44 +0800634 case 2:
635 base += 2 * direct_blks;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500636 fallthrough;
Chao Yu3cf45742016-01-26 15:40:44 +0800637 case 1:
638 base += direct_index;
639 break;
640 default:
641 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
642 }
643
644 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
645}
646
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900647/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900648 * The maximum depth is four.
649 * Offset[0] will have raw inode offset.
650 */
Chao Yu81ca7352016-01-26 15:39:35 +0800651static int get_node_path(struct inode *inode, long block,
Jaegeuk Kimde936532013-08-12 21:08:03 +0900652 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900653{
Chao Yu81ca7352016-01-26 15:39:35 +0800654 const long direct_index = ADDRS_PER_INODE(inode);
Chao Yud02a6e62019-03-25 21:08:19 +0800655 const long direct_blks = ADDRS_PER_BLOCK(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900656 const long dptrs_per_blk = NIDS_PER_BLOCK;
Chao Yud02a6e62019-03-25 21:08:19 +0800657 const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900658 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
659 int n = 0;
660 int level = 0;
661
662 noffset[0] = 0;
663
664 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900665 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900666 goto got;
667 }
668 block -= direct_index;
669 if (block < direct_blks) {
670 offset[n++] = NODE_DIR1_BLOCK;
671 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900672 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900673 level = 1;
674 goto got;
675 }
676 block -= direct_blks;
677 if (block < direct_blks) {
678 offset[n++] = NODE_DIR2_BLOCK;
679 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900680 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900681 level = 1;
682 goto got;
683 }
684 block -= direct_blks;
685 if (block < indirect_blks) {
686 offset[n++] = NODE_IND1_BLOCK;
687 noffset[n] = 3;
688 offset[n++] = block / direct_blks;
689 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900690 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900691 level = 2;
692 goto got;
693 }
694 block -= indirect_blks;
695 if (block < indirect_blks) {
696 offset[n++] = NODE_IND2_BLOCK;
697 noffset[n] = 4 + dptrs_per_blk;
698 offset[n++] = block / direct_blks;
699 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900700 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900701 level = 2;
702 goto got;
703 }
704 block -= indirect_blks;
705 if (block < dindirect_blks) {
706 offset[n++] = NODE_DIND_BLOCK;
707 noffset[n] = 5 + (dptrs_per_blk * 2);
708 offset[n++] = block / indirect_blks;
709 noffset[n] = 6 + (dptrs_per_blk * 2) +
710 offset[n - 1] * (dptrs_per_blk + 1);
711 offset[n++] = (block / direct_blks) % dptrs_per_blk;
712 noffset[n] = 7 + (dptrs_per_blk * 2) +
713 offset[n - 2] * (dptrs_per_blk + 1) +
714 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900715 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716 level = 3;
717 goto got;
718 } else {
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700719 return -E2BIG;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900720 }
721got:
722 return level;
723}
724
725/*
726 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800727 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
Chao Yu7a88ddb2020-02-27 19:30:05 +0800728 * f2fs_unlock_op() only if mode is set with ALLOC_NODE.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900729 */
Chao Yu4d57b862018-05-30 00:20:41 +0800730int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900731{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700732 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900733 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800734 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900735 int offset[4];
736 unsigned int noffset[4];
737 nid_t nids[4];
Chao Yu3cf45742016-01-26 15:40:44 +0800738 int level, i = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900739 int err = 0;
740
Chao Yu81ca7352016-01-26 15:39:35 +0800741 level = get_node_path(dn->inode, index, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700742 if (level < 0)
743 return level;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900744
745 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900746 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900747
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900748 if (!npage[0]) {
Chao Yu4d57b862018-05-30 00:20:41 +0800749 npage[0] = f2fs_get_node_page(sbi, nids[0]);
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900750 if (IS_ERR(npage[0]))
751 return PTR_ERR(npage[0]);
752 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800753
754 /* if inline_data is set, should not report any block indices */
755 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800756 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800757 f2fs_put_page(npage[0], 1);
758 goto release_out;
759 }
760
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900761 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900762 if (level != 0)
763 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900764 dn->inode_page = npage[0];
765 dn->inode_page_locked = true;
766
767 /* get indirect or direct nodes */
768 for (i = 1; i <= level; i++) {
769 bool done = false;
770
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900771 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900772 /* alloc new node */
Chao Yu4d57b862018-05-30 00:20:41 +0800773 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900774 err = -ENOSPC;
775 goto release_pages;
776 }
777
778 dn->nid = nids[i];
Chao Yu4d57b862018-05-30 00:20:41 +0800779 npage[i] = f2fs_new_node_page(dn, noffset[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900780 if (IS_ERR(npage[i])) {
Chao Yu4d57b862018-05-30 00:20:41 +0800781 f2fs_alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900782 err = PTR_ERR(npage[i]);
783 goto release_pages;
784 }
785
786 set_nid(parent, offset[i - 1], nids[i], i == 1);
Chao Yu4d57b862018-05-30 00:20:41 +0800787 f2fs_alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900788 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900789 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Chao Yu4d57b862018-05-30 00:20:41 +0800790 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900791 if (IS_ERR(npage[i])) {
792 err = PTR_ERR(npage[i]);
793 goto release_pages;
794 }
795 done = true;
796 }
797 if (i == 1) {
798 dn->inode_page_locked = false;
799 unlock_page(parent);
800 } else {
801 f2fs_put_page(parent, 1);
802 }
803
804 if (!done) {
Chao Yu4d57b862018-05-30 00:20:41 +0800805 npage[i] = f2fs_get_node_page(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900806 if (IS_ERR(npage[i])) {
807 err = PTR_ERR(npage[i]);
808 f2fs_put_page(npage[0], 0);
809 goto release_out;
810 }
811 }
812 if (i < level) {
813 parent = npage[i];
814 nids[i + 1] = get_nid(parent, offset[i], false);
815 }
816 }
817 dn->nid = nids[level];
818 dn->ofs_in_node = offset[level];
819 dn->node_page = npage[level];
Chao Yua2ced1c2020-02-14 17:44:10 +0800820 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900821 return 0;
822
823release_pages:
824 f2fs_put_page(parent, 1);
825 if (i > 1)
826 f2fs_put_page(npage[0], 0);
827release_out:
828 dn->inode_page = NULL;
829 dn->node_page = NULL;
Chao Yu3cf45742016-01-26 15:40:44 +0800830 if (err == -ENOENT) {
831 dn->cur_level = i;
832 dn->max_level = level;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700833 dn->ofs_in_node = offset[level];
Chao Yu3cf45742016-01-26 15:40:44 +0800834 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900835 return err;
836}
837
Chao Yu77357302018-07-17 00:02:17 +0800838static int truncate_node(struct dnode_of_data *dn)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900839{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700840 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900841 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800842 int err;
Pan Bian0ea295d2018-11-22 18:58:46 +0800843 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844
Chao Yu77357302018-07-17 00:02:17 +0800845 err = f2fs_get_node_info(sbi, dn->nid, &ni);
846 if (err)
847 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900848
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900849 /* Deallocate node address */
Chao Yu4d57b862018-05-30 00:20:41 +0800850 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +0800851 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900852 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900853
854 if (dn->nid == dn->inode->i_ino) {
Chao Yu4d57b862018-05-30 00:20:41 +0800855 f2fs_remove_orphan_inode(sbi, dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900856 dec_valid_inode_count(sbi);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700857 f2fs_inode_synced(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900858 }
Chao Yu000519f2017-07-06 01:11:31 +0800859
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900860 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800861 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900862
Pan Bian0ea295d2018-11-22 18:58:46 +0800863 index = dn->node_page->index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900864 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900865
866 invalidate_mapping_pages(NODE_MAPPING(sbi),
Pan Bian0ea295d2018-11-22 18:58:46 +0800867 index, index);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900868
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900869 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900870 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Chao Yu77357302018-07-17 00:02:17 +0800871
872 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900873}
874
875static int truncate_dnode(struct dnode_of_data *dn)
876{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900877 struct page *page;
Chao Yu77357302018-07-17 00:02:17 +0800878 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900879
880 if (dn->nid == 0)
881 return 1;
882
883 /* get direct node */
Chao Yu4d57b862018-05-30 00:20:41 +0800884 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Masahiro Yamada45586c72020-02-03 17:37:45 -0800885 if (PTR_ERR(page) == -ENOENT)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900886 return 1;
887 else if (IS_ERR(page))
888 return PTR_ERR(page);
889
890 /* Make dnode_of_data for parameter */
891 dn->node_page = page;
892 dn->ofs_in_node = 0;
Chao Yu4d57b862018-05-30 00:20:41 +0800893 f2fs_truncate_data_blocks(dn);
Chao Yu77357302018-07-17 00:02:17 +0800894 err = truncate_node(dn);
895 if (err)
896 return err;
897
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900898 return 1;
899}
900
901static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
902 int ofs, int depth)
903{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900904 struct dnode_of_data rdn = *dn;
905 struct page *page;
906 struct f2fs_node *rn;
907 nid_t child_nid;
908 unsigned int child_nofs;
909 int freed = 0;
910 int i, ret;
911
912 if (dn->nid == 0)
913 return NIDS_PER_BLOCK + 1;
914
Namjae Jeon51dd6242013-04-20 01:28:52 +0900915 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
916
Chao Yu4d57b862018-05-30 00:20:41 +0800917 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900918 if (IS_ERR(page)) {
919 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900920 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900921 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900922
Chao Yu4d57b862018-05-30 00:20:41 +0800923 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700924
Gu Zheng45590712013-07-15 17:57:38 +0800925 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900926 if (depth < 3) {
927 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
928 child_nid = le32_to_cpu(rn->in.nid[i]);
929 if (child_nid == 0)
930 continue;
931 rdn.nid = child_nid;
932 ret = truncate_dnode(&rdn);
933 if (ret < 0)
934 goto out_err;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800935 if (set_nid(page, i, 0, false))
936 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900937 }
938 } else {
939 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
940 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
941 child_nid = le32_to_cpu(rn->in.nid[i]);
942 if (child_nid == 0) {
943 child_nofs += NIDS_PER_BLOCK + 1;
944 continue;
945 }
946 rdn.nid = child_nid;
947 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
948 if (ret == (NIDS_PER_BLOCK + 1)) {
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800949 if (set_nid(page, i, 0, false))
950 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900951 child_nofs += ret;
952 } else if (ret < 0 && ret != -ENOENT) {
953 goto out_err;
954 }
955 }
956 freed = child_nofs;
957 }
958
959 if (!ofs) {
960 /* remove current indirect node */
961 dn->node_page = page;
Chao Yu77357302018-07-17 00:02:17 +0800962 ret = truncate_node(dn);
963 if (ret)
964 goto out_err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900965 freed++;
966 } else {
967 f2fs_put_page(page, 1);
968 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900969 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900970 return freed;
971
972out_err:
973 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900974 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900975 return ret;
976}
977
978static int truncate_partial_nodes(struct dnode_of_data *dn,
979 struct f2fs_inode *ri, int *offset, int depth)
980{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900981 struct page *pages[2];
982 nid_t nid[3];
983 nid_t child_nid;
984 int err = 0;
985 int i;
986 int idx = depth - 2;
987
988 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
989 if (!nid[0])
990 return 0;
991
992 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800993 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900994 /* reference count'll be increased */
Chao Yu4d57b862018-05-30 00:20:41 +0800995 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900996 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900997 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800998 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999 goto fail;
1000 }
1001 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
1002 }
1003
Chao Yu4d57b862018-05-30 00:20:41 +08001004 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -07001005
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001006 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +08001007 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001008 child_nid = get_nid(pages[idx], i, false);
1009 if (!child_nid)
1010 continue;
1011 dn->nid = child_nid;
1012 err = truncate_dnode(dn);
1013 if (err < 0)
1014 goto fail;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001015 if (set_nid(pages[idx], i, 0, false))
1016 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017 }
1018
shifei10.gea225dca2013-10-29 15:32:34 +08001019 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001020 dn->node_page = pages[idx];
1021 dn->nid = nid[idx];
Chao Yu77357302018-07-17 00:02:17 +08001022 err = truncate_node(dn);
1023 if (err)
1024 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001025 } else {
1026 f2fs_put_page(pages[idx], 1);
1027 }
1028 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +08001029 offset[idx + 1] = 0;
1030 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001031fail:
shifei10.gea225dca2013-10-29 15:32:34 +08001032 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001033 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001034
1035 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1036
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001037 return err;
1038}
1039
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001040/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001041 * All the block addresses of data and nodes should be nullified.
1042 */
Chao Yu4d57b862018-05-30 00:20:41 +08001043int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001044{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001045 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001046 int err = 0, cont = 1;
1047 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +09001048 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001049 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001050 struct dnode_of_data dn;
1051 struct page *page;
1052
Namjae Jeon51dd6242013-04-20 01:28:52 +09001053 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1054
Chao Yu81ca7352016-01-26 15:39:35 +08001055 level = get_node_path(inode, from, offset, noffset);
Yubo Feng9039d832020-06-20 16:39:43 +08001056 if (level < 0) {
1057 trace_f2fs_truncate_inode_blocks_exit(inode, level);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -07001058 return level;
Yubo Feng9039d832020-06-20 16:39:43 +08001059 }
Jaegeuk Kimff373552016-03-29 16:13:45 -07001060
Chao Yu4d57b862018-05-30 00:20:41 +08001061 page = f2fs_get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001062 if (IS_ERR(page)) {
1063 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001064 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001065 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001066
1067 set_new_dnode(&dn, inode, page, NULL, 0);
1068 unlock_page(page);
1069
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001070 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001071 switch (level) {
1072 case 0:
1073 case 1:
1074 nofs = noffset[1];
1075 break;
1076 case 2:
1077 nofs = noffset[1];
1078 if (!offset[level - 1])
1079 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001080 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001081 if (err < 0 && err != -ENOENT)
1082 goto fail;
1083 nofs += 1 + NIDS_PER_BLOCK;
1084 break;
1085 case 3:
1086 nofs = 5 + 2 * NIDS_PER_BLOCK;
1087 if (!offset[level - 1])
1088 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001089 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001090 if (err < 0 && err != -ENOENT)
1091 goto fail;
1092 break;
1093 default:
1094 BUG();
1095 }
1096
1097skip_partial:
1098 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001099 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001100 switch (offset[0]) {
1101 case NODE_DIR1_BLOCK:
1102 case NODE_DIR2_BLOCK:
1103 err = truncate_dnode(&dn);
1104 break;
1105
1106 case NODE_IND1_BLOCK:
1107 case NODE_IND2_BLOCK:
1108 err = truncate_nodes(&dn, nofs, offset[1], 2);
1109 break;
1110
1111 case NODE_DIND_BLOCK:
1112 err = truncate_nodes(&dn, nofs, offset[1], 3);
1113 cont = 0;
1114 break;
1115
1116 default:
1117 BUG();
1118 }
1119 if (err < 0 && err != -ENOENT)
1120 goto fail;
1121 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001122 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001123 lock_page(page);
Jaegeuk Kimff373552016-03-29 16:13:45 -07001124 BUG_ON(page->mapping != NODE_MAPPING(sbi));
Chao Yubae0ee72018-12-25 17:43:42 +08001125 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001126 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001127 set_page_dirty(page);
1128 unlock_page(page);
1129 }
1130 offset[1] = 0;
1131 offset[0]++;
1132 nofs += err;
1133 }
1134fail:
1135 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001136 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001137 return err > 0 ? 0 : err;
1138}
1139
Jaegeuk Kim9c77f752017-10-19 11:48:57 -07001140/* caller must lock inode page */
Chao Yu4d57b862018-05-30 00:20:41 +08001141int f2fs_truncate_xattr_node(struct inode *inode)
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001142{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001143 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001144 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1145 struct dnode_of_data dn;
1146 struct page *npage;
Chao Yu77357302018-07-17 00:02:17 +08001147 int err;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001148
1149 if (!nid)
1150 return 0;
1151
Chao Yu4d57b862018-05-30 00:20:41 +08001152 npage = f2fs_get_node_page(sbi, nid);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001153 if (IS_ERR(npage))
1154 return PTR_ERR(npage);
1155
Chao Yu77357302018-07-17 00:02:17 +08001156 set_new_dnode(&dn, inode, NULL, npage, nid);
1157 err = truncate_node(&dn);
1158 if (err) {
1159 f2fs_put_page(npage, 1);
1160 return err;
1161 }
1162
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001163 f2fs_i_xnid_write(inode, 0);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001164
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001165 return 0;
1166}
1167
Jaegeuk Kim39936832012-11-22 16:21:29 +09001168/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001169 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1170 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +09001171 */
Chao Yu4d57b862018-05-30 00:20:41 +08001172int f2fs_remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001173{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001174 struct dnode_of_data dn;
Chao Yu13ec7292015-08-24 17:40:45 +08001175 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001176
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001177 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Chao Yu4d57b862018-05-30 00:20:41 +08001178 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
Chao Yu13ec7292015-08-24 17:40:45 +08001179 if (err)
1180 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001181
Chao Yu4d57b862018-05-30 00:20:41 +08001182 err = f2fs_truncate_xattr_node(inode);
Chao Yu13ec7292015-08-24 17:40:45 +08001183 if (err) {
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001184 f2fs_put_dnode(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +08001185 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001186 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001187
1188 /* remove potential inline_data blocks */
1189 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1190 S_ISLNK(inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +08001191 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001192
arter97e1c42042014-08-06 23:22:50 +09001193 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim8d714f82018-07-31 09:09:01 -07001194 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1195 f2fs_put_dnode(&dn);
1196 return -EIO;
1197 }
Chao Yu8b6810f2019-04-15 15:28:34 +08001198
1199 if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
Chao Yu097a7682020-02-24 19:20:19 +08001200 f2fs_warn(F2FS_I_SB(inode),
1201 "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu",
1202 inode->i_ino, (unsigned long long)inode->i_blocks);
Chao Yu8b6810f2019-04-15 15:28:34 +08001203 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
1204 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001205
1206 /* will put inode & node pages */
Chao Yu77357302018-07-17 00:02:17 +08001207 err = truncate_node(&dn);
1208 if (err) {
1209 f2fs_put_dnode(&dn);
1210 return err;
1211 }
Chao Yu13ec7292015-08-24 17:40:45 +08001212 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001213}
1214
Chao Yu4d57b862018-05-30 00:20:41 +08001215struct page *f2fs_new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001216{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001217 struct dnode_of_data dn;
1218
1219 /* allocate inode page for new inode */
1220 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +09001221
1222 /* caller should f2fs_put_page(page, 1); */
Chao Yu4d57b862018-05-30 00:20:41 +08001223 return f2fs_new_node_page(&dn, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001224}
1225
Chao Yu4d57b862018-05-30 00:20:41 +08001226struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001227{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001228 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001229 struct node_info new_ni;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001230 struct page *page;
1231 int err;
1232
Jaegeuk Kim91942322016-05-20 10:13:22 -07001233 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001234 return ERR_PTR(-EPERM);
1235
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001236 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001237 if (!page)
1238 return ERR_PTR(-ENOMEM);
1239
Chao Yu0abd6752017-07-09 00:13:07 +08001240 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001241 goto fail;
Chao Yu0abd6752017-07-09 00:13:07 +08001242
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001243#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu77357302018-07-17 00:02:17 +08001244 err = f2fs_get_node_info(sbi, dn->nid, &new_ni);
1245 if (err) {
1246 dec_valid_node_count(sbi, dn->inode, !ofs);
1247 goto fail;
1248 }
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001249 f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
1250#endif
1251 new_ni.nid = dn->nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001252 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001253 new_ni.blk_addr = NULL_ADDR;
1254 new_ni.flag = 0;
1255 new_ni.version = 0;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001256 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001257
Chao Yubae0ee72018-12-25 17:43:42 +08001258 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001259 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Chao Yuc5667572018-03-09 14:24:22 +08001260 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001261 if (!PageUptodate(page))
1262 SetPageUptodate(page);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001263 if (set_page_dirty(page))
1264 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001265
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001266 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001267 f2fs_i_xnid_write(dn->inode, dn->nid);
Jaegeuk Kim479bd732013-08-12 16:04:53 +09001268
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001269 if (ofs == 0)
1270 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001271 return page;
1272
1273fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +09001274 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001275 f2fs_put_page(page, 1);
1276 return ERR_PTR(err);
1277}
1278
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001279/*
1280 * Caller should do after getting the following values.
1281 * 0: f2fs_put_page(page, 0)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001282 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001283 */
Mike Christie04d328d2016-06-05 14:31:55 -05001284static int read_node_page(struct page *page, int op_flags)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001285{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001286 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001287 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001288 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001289 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001290 .type = NODE,
Mike Christie04d328d2016-06-05 14:31:55 -05001291 .op = REQ_OP_READ,
1292 .op_flags = op_flags,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001293 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001294 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001295 };
Chao Yu77357302018-07-17 00:02:17 +08001296 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001297
Weichao Guo54c55c42018-03-09 23:10:21 +08001298 if (PageUptodate(page)) {
Chao Yub42b1792019-04-15 15:28:35 +08001299 if (!f2fs_inode_chksum_verify(sbi, page)) {
1300 ClearPageUptodate(page);
Chao Yu10f966b2019-06-20 11:36:14 +08001301 return -EFSBADCRC;
Chao Yub42b1792019-04-15 15:28:35 +08001302 }
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001303 return LOCKED_PAGE;
Weichao Guo54c55c42018-03-09 23:10:21 +08001304 }
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001305
Chao Yu77357302018-07-17 00:02:17 +08001306 err = f2fs_get_node_info(sbi, page->index, &ni);
1307 if (err)
1308 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001309
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001310 if (unlikely(ni.blk_addr == NULL_ADDR) ||
1311 is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001312 ClearPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001313 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001314 }
1315
Chao Yu7a9d7542016-02-22 18:36:38 +08001316 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
Chao Yu8b83ac82020-04-16 18:16:56 +08001317
1318 err = f2fs_submit_page_bio(&fio);
1319
1320 if (!err)
1321 f2fs_update_iostat(sbi, FS_NODE_READ_IO, F2FS_BLKSIZE);
1322
1323 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001324}
1325
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001326/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001327 * Readahead a node page
1328 */
Chao Yu4d57b862018-05-30 00:20:41 +08001329void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001330{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001331 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001332 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333
Chao Yue8458722016-01-08 20:13:37 +08001334 if (!nid)
1335 return;
Chao Yu4d57b862018-05-30 00:20:41 +08001336 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001337 return;
Chao Yue8458722016-01-08 20:13:37 +08001338
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05001339 apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
Fan Li999270d2016-02-29 14:29:51 +08001340 if (apage)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001341 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001342
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001343 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001344 if (!apage)
1345 return;
1346
Christoph Hellwig70246282016-07-19 11:28:41 +02001347 err = read_node_page(apage, REQ_RAHEAD);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001348 f2fs_put_page(apage, err ? 1 : 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001349}
1350
Jaegeuk Kim17a0ee52016-03-08 09:04:35 -08001351static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
Chao Yu0e022ea2016-01-05 16:52:46 +08001352 struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001353{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001354 struct page *page;
1355 int err;
Jaegeuk Kim4aa69d52015-12-23 14:17:47 -08001356
1357 if (!nid)
1358 return ERR_PTR(-ENOENT);
Chao Yu4d57b862018-05-30 00:20:41 +08001359 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001360 return ERR_PTR(-EINVAL);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001361repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001362 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001363 if (!page)
1364 return ERR_PTR(-ENOMEM);
1365
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001366 err = read_node_page(page, 0);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001367 if (err < 0) {
1368 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001369 return ERR_PTR(err);
Chao Yue1c51b9f2015-12-11 16:08:22 +08001370 } else if (err == LOCKED_PAGE) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001371 err = 0;
Chao Yue1c51b9f2015-12-11 16:08:22 +08001372 goto page_hit;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001373 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001374
Chao Yu0e022ea2016-01-05 16:52:46 +08001375 if (parent)
Chao Yu4d57b862018-05-30 00:20:41 +08001376 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
Chao Yu0e022ea2016-01-05 16:52:46 +08001377
Chao Yue1c51b9f2015-12-11 16:08:22 +08001378 lock_page(page);
1379
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001380 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001381 f2fs_put_page(page, 1);
1382 goto repeat;
1383 }
Chao Yu1563ac72016-07-03 22:05:12 +08001384
Chao Yu1f258ec2017-06-07 11:17:35 +08001385 if (unlikely(!PageUptodate(page))) {
1386 err = -EIO;
Chao Yu1563ac72016-07-03 22:05:12 +08001387 goto out_err;
Chao Yu1f258ec2017-06-07 11:17:35 +08001388 }
Chao Yu704956e2017-07-31 20:19:09 +08001389
1390 if (!f2fs_inode_chksum_verify(sbi, page)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001391 err = -EFSBADCRC;
Chao Yu704956e2017-07-31 20:19:09 +08001392 goto out_err;
1393 }
Chao Yue1c51b9f2015-12-11 16:08:22 +08001394page_hit:
Yi Zhuang5f029c02021-04-06 09:47:35 +08001395 if (unlikely(nid != nid_of_node(page))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001396 f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1397 nid, nid_of_node(page), ino_of_node(page),
1398 ofs_of_node(page), cpver_of_node(page),
1399 next_blkaddr_of_node(page));
Chao Yu1f258ec2017-06-07 11:17:35 +08001400 err = -EINVAL;
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001401out_err:
Jaegeuk Kimee605232017-08-31 16:54:51 -07001402 ClearPageUptodate(page);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001403 f2fs_put_page(page, 1);
Chao Yu1f258ec2017-06-07 11:17:35 +08001404 return ERR_PTR(err);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001405 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001406 return page;
1407}
1408
Chao Yu4d57b862018-05-30 00:20:41 +08001409struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
Chao Yu0e022ea2016-01-05 16:52:46 +08001410{
1411 return __get_node_page(sbi, nid, NULL, 0);
1412}
1413
Chao Yu4d57b862018-05-30 00:20:41 +08001414struct page *f2fs_get_node_page_ra(struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001415{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001416 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Chao Yu0e022ea2016-01-05 16:52:46 +08001417 nid_t nid = get_nid(parent, start, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001418
Chao Yu0e022ea2016-01-05 16:52:46 +08001419 return __get_node_page(sbi, nid, parent, start);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420}
1421
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001422static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1423{
1424 struct inode *inode;
1425 struct page *page;
Chao Yu0f3311a2016-05-21 00:11:09 +08001426 int ret;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001427
1428 /* should flush inline_data before evict_inode */
1429 inode = ilookup(sbi->sb, ino);
1430 if (!inode)
1431 return;
1432
Chao Yu01eccef2017-10-28 16:52:30 +08001433 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1434 FGP_LOCK|FGP_NOWAIT, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001435 if (!page)
1436 goto iput_out;
1437
1438 if (!PageUptodate(page))
1439 goto page_out;
1440
1441 if (!PageDirty(page))
1442 goto page_out;
1443
1444 if (!clear_page_dirty_for_io(page))
1445 goto page_out;
1446
Chao Yu0f3311a2016-05-21 00:11:09 +08001447 ret = f2fs_write_inline_data(inode, page);
1448 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08001449 f2fs_remove_dirty_inode(inode);
Chao Yu0f3311a2016-05-21 00:11:09 +08001450 if (ret)
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001451 set_page_dirty(page);
1452page_out:
Jaegeuk Kim4a6de502016-03-30 11:25:31 -07001453 f2fs_put_page(page, 1);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001454iput_out:
1455 iput(inode);
1456}
1457
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001458static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001459{
Jan Kara028a63a2017-11-15 17:34:51 -08001460 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001461 struct pagevec pvec;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001462 struct page *last_page = NULL;
Jan Kara028a63a2017-11-15 17:34:51 -08001463 int nr_pages;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001464
Mel Gorman86679822017-11-15 17:37:52 -08001465 pagevec_init(&pvec);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001466 index = 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001467
Jan Kara028a63a2017-11-15 17:34:51 -08001468 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001469 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001470 int i;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001471
1472 for (i = 0; i < nr_pages; i++) {
1473 struct page *page = pvec.pages[i];
1474
1475 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001476 f2fs_put_page(last_page, 0);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001477 pagevec_release(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001478 return ERR_PTR(-EIO);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001479 }
1480
1481 if (!IS_DNODE(page) || !is_cold_node(page))
1482 continue;
1483 if (ino_of_node(page) != ino)
1484 continue;
1485
1486 lock_page(page);
1487
1488 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1489continue_unlock:
1490 unlock_page(page);
1491 continue;
1492 }
1493 if (ino_of_node(page) != ino)
1494 goto continue_unlock;
1495
1496 if (!PageDirty(page)) {
1497 /* someone wrote it for us */
1498 goto continue_unlock;
1499 }
1500
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001501 if (last_page)
1502 f2fs_put_page(last_page, 0);
1503
1504 get_page(page);
1505 last_page = page;
1506 unlock_page(page);
1507 }
1508 pagevec_release(&pvec);
1509 cond_resched();
1510 }
1511 return last_page;
1512}
1513
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001514static int __write_node_page(struct page *page, bool atomic, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001515 struct writeback_control *wbc, bool do_balance,
Chao Yu50fa53e2018-08-02 23:03:19 +08001516 enum iostat_type io_type, unsigned int *seq_id)
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001517{
1518 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1519 nid_t nid;
1520 struct node_info ni;
1521 struct f2fs_io_info fio = {
1522 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001523 .ino = ino_of_node(page),
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001524 .type = NODE,
1525 .op = REQ_OP_WRITE,
1526 .op_flags = wbc_to_write_flags(wbc),
1527 .page = page,
1528 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001529 .submitted = false,
Chao Yub0af6d42017-08-02 23:21:48 +08001530 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001531 .io_wbc = wbc,
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001532 };
Chao Yu50fa53e2018-08-02 23:03:19 +08001533 unsigned int seq;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001534
1535 trace_f2fs_writepage(page, NODE);
1536
Jaegeuk Kim6d7c8652020-05-18 18:00:33 -07001537 if (unlikely(f2fs_cp_error(sbi))) {
1538 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
1539 ClearPageUptodate(page);
1540 dec_page_count(sbi, F2FS_DIRTY_NODES);
1541 unlock_page(page);
1542 return 0;
1543 }
Jaegeuk Kim868de612018-05-04 18:04:22 -07001544 goto redirty_out;
Jaegeuk Kim6d7c8652020-05-18 18:00:33 -07001545 }
Chao Yudb198ae2018-01-18 17:29:10 +08001546
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001547 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1548 goto redirty_out;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001549
Jaegeuk Kim100c0652019-08-28 19:58:26 -07001550 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1551 wbc->sync_mode == WB_SYNC_NONE &&
Chao Yufd8c8ca2018-07-25 19:16:21 +08001552 IS_DNODE(page) && is_cold_node(page))
1553 goto redirty_out;
1554
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001555 /* get old block addr of this node page */
1556 nid = nid_of_node(page);
1557 f2fs_bug_on(sbi, page->index != nid);
1558
Chao Yu77357302018-07-17 00:02:17 +08001559 if (f2fs_get_node_info(sbi, nid, &ni))
1560 goto redirty_out;
1561
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001562 if (wbc->for_reclaim) {
1563 if (!down_read_trylock(&sbi->node_write))
1564 goto redirty_out;
1565 } else {
1566 down_read(&sbi->node_write);
1567 }
1568
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001569 /* This page is already truncated */
1570 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1571 ClearPageUptodate(page);
1572 dec_page_count(sbi, F2FS_DIRTY_NODES);
1573 up_read(&sbi->node_write);
1574 unlock_page(page);
1575 return 0;
1576 }
1577
Chao Yuc9b60782018-08-01 19:13:44 +08001578 if (__is_valid_data_blkaddr(ni.blk_addr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001579 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
1580 DATA_GENERIC_ENHANCE)) {
Jaegeuk Kim89d13c32018-09-27 22:15:31 -07001581 up_read(&sbi->node_write);
Chao Yuc9b60782018-08-01 19:13:44 +08001582 goto redirty_out;
Jaegeuk Kim89d13c32018-09-27 22:15:31 -07001583 }
Chao Yuc9b60782018-08-01 19:13:44 +08001584
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001585 if (atomic && !test_opt(sbi, NOBARRIER))
1586 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1587
Chao Yudc5a9412020-02-14 17:45:12 +08001588 /* should add to global list before clearing PAGECACHE status */
Chao Yu50fa53e2018-08-02 23:03:19 +08001589 if (f2fs_in_warm_node_list(sbi, page)) {
1590 seq = f2fs_add_fsync_node_entry(sbi, page);
1591 if (seq_id)
1592 *seq_id = seq;
1593 }
1594
Chao Yudc5a9412020-02-14 17:45:12 +08001595 set_page_writeback(page);
1596 ClearPageError(page);
1597
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001598 fio.old_blkaddr = ni.blk_addr;
Chao Yu4d57b862018-05-30 00:20:41 +08001599 f2fs_do_write_node_page(nid, &fio);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001600 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1601 dec_page_count(sbi, F2FS_DIRTY_NODES);
1602 up_read(&sbi->node_write);
1603
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001604 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08001605 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001606 submitted = NULL;
1607 }
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001608
1609 unlock_page(page);
1610
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001611 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001612 f2fs_submit_merged_write(sbi, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001613 submitted = NULL;
1614 }
1615 if (submitted)
1616 *submitted = fio.submitted;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001617
Yunlong Song401db792017-07-27 20:11:00 +08001618 if (do_balance)
1619 f2fs_balance_fs(sbi, false);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001620 return 0;
1621
1622redirty_out:
1623 redirty_page_for_writepage(wbc, page);
1624 return AOP_WRITEPAGE_ACTIVATE;
1625}
1626
Chao Yu48018b42018-09-13 07:40:53 +08001627int f2fs_move_node_page(struct page *node_page, int gc_type)
Yunlei Hef15194f2017-10-30 14:18:55 +08001628{
Chao Yu48018b42018-09-13 07:40:53 +08001629 int err = 0;
1630
Yunlei Hef15194f2017-10-30 14:18:55 +08001631 if (gc_type == FG_GC) {
1632 struct writeback_control wbc = {
1633 .sync_mode = WB_SYNC_ALL,
1634 .nr_to_write = 1,
1635 .for_reclaim = 0,
1636 };
1637
Chao Yubae0ee72018-12-25 17:43:42 +08001638 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
Chao Yu8d64d362018-12-12 18:12:30 +08001639
1640 set_page_dirty(node_page);
1641
Chao Yu48018b42018-09-13 07:40:53 +08001642 if (!clear_page_dirty_for_io(node_page)) {
1643 err = -EAGAIN;
Yunlei Hef15194f2017-10-30 14:18:55 +08001644 goto out_page;
Chao Yu48018b42018-09-13 07:40:53 +08001645 }
Yunlei Hef15194f2017-10-30 14:18:55 +08001646
1647 if (__write_node_page(node_page, false, NULL,
Chao Yu48018b42018-09-13 07:40:53 +08001648 &wbc, false, FS_GC_NODE_IO, NULL)) {
1649 err = -EAGAIN;
Yunlei Hef15194f2017-10-30 14:18:55 +08001650 unlock_page(node_page);
Chao Yu48018b42018-09-13 07:40:53 +08001651 }
Yunlei Hef15194f2017-10-30 14:18:55 +08001652 goto release_page;
1653 } else {
1654 /* set page dirty and write it */
1655 if (!PageWriteback(node_page))
1656 set_page_dirty(node_page);
1657 }
1658out_page:
1659 unlock_page(node_page);
1660release_page:
1661 f2fs_put_page(node_page, 0);
Chao Yu48018b42018-09-13 07:40:53 +08001662 return err;
Yunlei Hef15194f2017-10-30 14:18:55 +08001663}
1664
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001665static int f2fs_write_node_page(struct page *page,
1666 struct writeback_control *wbc)
1667{
Chao Yu50fa53e2018-08-02 23:03:19 +08001668 return __write_node_page(page, false, NULL, wbc, false,
1669 FS_NODE_IO, NULL);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001670}
1671
Chao Yu4d57b862018-05-30 00:20:41 +08001672int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
Chao Yu50fa53e2018-08-02 23:03:19 +08001673 struct writeback_control *wbc, bool atomic,
1674 unsigned int *seq_id)
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001675{
Jan Kara028a63a2017-11-15 17:34:51 -08001676 pgoff_t index;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001677 struct pagevec pvec;
1678 int ret = 0;
1679 struct page *last_page = NULL;
1680 bool marked = false;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001681 nid_t ino = inode->i_ino;
Jan Kara028a63a2017-11-15 17:34:51 -08001682 int nr_pages;
Chao Yubab475c2018-09-27 23:41:16 +08001683 int nwritten = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001684
1685 if (atomic) {
1686 last_page = last_fsync_dnode(sbi, ino);
1687 if (IS_ERR_OR_NULL(last_page))
1688 return PTR_ERR_OR_ZERO(last_page);
1689 }
1690retry:
Mel Gorman86679822017-11-15 17:37:52 -08001691 pagevec_init(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001692 index = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001693
Jan Kara028a63a2017-11-15 17:34:51 -08001694 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001695 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001696 int i;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001697
1698 for (i = 0; i < nr_pages; i++) {
1699 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001700 bool submitted = false;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001701
1702 if (unlikely(f2fs_cp_error(sbi))) {
1703 f2fs_put_page(last_page, 0);
1704 pagevec_release(&pvec);
Chao Yu9de69272016-10-11 22:57:06 +08001705 ret = -EIO;
1706 goto out;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001707 }
1708
1709 if (!IS_DNODE(page) || !is_cold_node(page))
1710 continue;
1711 if (ino_of_node(page) != ino)
1712 continue;
1713
1714 lock_page(page);
1715
1716 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1717continue_unlock:
1718 unlock_page(page);
1719 continue;
1720 }
1721 if (ino_of_node(page) != ino)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001722 goto continue_unlock;
1723
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001724 if (!PageDirty(page) && page != last_page) {
1725 /* someone wrote it for us */
1726 goto continue_unlock;
1727 }
1728
Chao Yubae0ee72018-12-25 17:43:42 +08001729 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001730
Jaegeuk Kimd29fd172017-04-12 12:02:00 -07001731 set_fsync_mark(page, 0);
1732 set_dentry_mark(page, 0);
1733
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001734 if (!atomic || page == last_page) {
1735 set_fsync_mark(page, 1);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001736 if (IS_INODE(page)) {
1737 if (is_inode_flag_set(inode,
1738 FI_DIRTY_INODE))
Chao Yu4d57b862018-05-30 00:20:41 +08001739 f2fs_update_inode(inode, page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001740 set_dentry_mark(page,
Chao Yu4d57b862018-05-30 00:20:41 +08001741 f2fs_need_dentry_mark(sbi, ino));
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001742 }
Jack Qiua87aff12020-07-24 16:55:28 +08001743 /* may be written by other thread */
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001744 if (!PageDirty(page))
1745 set_page_dirty(page);
1746 }
1747
1748 if (!clear_page_dirty_for_io(page))
1749 goto continue_unlock;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001750
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001751 ret = __write_node_page(page, atomic &&
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001752 page == last_page,
Chao Yub0af6d42017-08-02 23:21:48 +08001753 &submitted, wbc, true,
Chao Yu50fa53e2018-08-02 23:03:19 +08001754 FS_NODE_IO, seq_id);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001755 if (ret) {
Jaegeuk Kim52681372016-04-13 16:24:44 -07001756 unlock_page(page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001757 f2fs_put_page(last_page, 0);
1758 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001759 } else if (submitted) {
Chao Yubab475c2018-09-27 23:41:16 +08001760 nwritten++;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001761 }
Chao Yu3f5f4952016-09-29 18:50:10 +08001762
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001763 if (page == last_page) {
1764 f2fs_put_page(page, 0);
1765 marked = true;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001766 break;
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001767 }
Jaegeuk Kim52681372016-04-13 16:24:44 -07001768 }
1769 pagevec_release(&pvec);
1770 cond_resched();
1771
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001772 if (ret || marked)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001773 break;
1774 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001775 if (!ret && atomic && !marked) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001776 f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
1777 ino, last_page->index);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001778 lock_page(last_page);
Chao Yubae0ee72018-12-25 17:43:42 +08001779 f2fs_wait_on_page_writeback(last_page, NODE, true, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001780 set_page_dirty(last_page);
1781 unlock_page(last_page);
1782 goto retry;
1783 }
Chao Yu9de69272016-10-11 22:57:06 +08001784out:
Chao Yubab475c2018-09-27 23:41:16 +08001785 if (nwritten)
1786 f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
Yi Zhuang5f029c02021-04-06 09:47:35 +08001787 return ret ? -EIO : 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001788}
1789
Chao Yu052a82d2019-08-22 20:17:56 +08001790static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data)
1791{
1792 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1793 bool clean;
1794
1795 if (inode->i_ino != ino)
1796 return 0;
1797
1798 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
1799 return 0;
1800
1801 spin_lock(&sbi->inode_lock[DIRTY_META]);
1802 clean = list_empty(&F2FS_I(inode)->gdirty_list);
1803 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1804
1805 if (clean)
1806 return 0;
1807
1808 inode = igrab(inode);
1809 if (!inode)
1810 return 0;
1811 return 1;
1812}
1813
1814static bool flush_dirty_inode(struct page *page)
1815{
1816 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1817 struct inode *inode;
1818 nid_t ino = ino_of_node(page);
1819
1820 inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL);
1821 if (!inode)
1822 return false;
1823
1824 f2fs_update_inode(inode, page);
1825 unlock_page(page);
1826
1827 iput(inode);
1828 return true;
1829}
1830
Jia Yang68e79ba2020-07-21 11:49:14 +08001831void f2fs_flush_inline_data(struct f2fs_sb_info *sbi)
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301832{
1833 pgoff_t index = 0;
1834 struct pagevec pvec;
1835 int nr_pages;
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301836
1837 pagevec_init(&pvec);
1838
1839 while ((nr_pages = pagevec_lookup_tag(&pvec,
1840 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
1841 int i;
1842
1843 for (i = 0; i < nr_pages; i++) {
1844 struct page *page = pvec.pages[i];
1845
1846 if (!IS_DNODE(page))
1847 continue;
1848
1849 lock_page(page);
1850
1851 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1852continue_unlock:
1853 unlock_page(page);
1854 continue;
1855 }
1856
1857 if (!PageDirty(page)) {
1858 /* someone wrote it for us */
1859 goto continue_unlock;
1860 }
1861
1862 /* flush inline_data, if it's async context. */
Chao Yub763f3b2021-04-28 17:20:31 +08001863 if (page_private_inline(page)) {
1864 clear_page_private_inline(page);
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301865 unlock_page(page);
1866 flush_inline_data(sbi, ino_of_node(page));
1867 continue;
1868 }
1869 unlock_page(page);
1870 }
1871 pagevec_release(&pvec);
1872 cond_resched();
1873 }
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301874}
1875
Chao Yu4d57b862018-05-30 00:20:41 +08001876int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
1877 struct writeback_control *wbc,
Chao Yub0af6d42017-08-02 23:21:48 +08001878 bool do_balance, enum iostat_type io_type)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001879{
Jan Kara028a63a2017-11-15 17:34:51 -08001880 pgoff_t index;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001881 struct pagevec pvec;
1882 int step = 0;
Jaegeuk Kim12bb0a82016-03-11 15:33:22 -08001883 int nwritten = 0;
Chao Yu3f5f4952016-09-29 18:50:10 +08001884 int ret = 0;
Chao Yuc29fd0c2018-06-04 23:20:36 +08001885 int nr_pages, done = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001886
Mel Gorman86679822017-11-15 17:37:52 -08001887 pagevec_init(&pvec);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001888
1889next_step:
1890 index = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001891
Chao Yuc29fd0c2018-06-04 23:20:36 +08001892 while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
1893 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001894 int i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001895
1896 for (i = 0; i < nr_pages; i++) {
1897 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001898 bool submitted = false;
Chao Yu052a82d2019-08-22 20:17:56 +08001899 bool may_dirty = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001900
Chao Yuc29fd0c2018-06-04 23:20:36 +08001901 /* give a priority to WB_SYNC threads */
1902 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
1903 wbc->sync_mode == WB_SYNC_NONE) {
1904 done = 1;
1905 break;
1906 }
1907
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001908 /*
1909 * flushing sequence with step:
1910 * 0. indirect nodes
1911 * 1. dentry dnodes
1912 * 2. file dnodes
1913 */
1914 if (step == 0 && IS_DNODE(page))
1915 continue;
1916 if (step == 1 && (!IS_DNODE(page) ||
1917 is_cold_node(page)))
1918 continue;
1919 if (step == 2 && (!IS_DNODE(page) ||
1920 !is_cold_node(page)))
1921 continue;
Chao Yu9a4cbc92016-02-22 18:35:46 +08001922lock_node:
Chao Yu4b270a82018-07-04 18:04:10 +08001923 if (wbc->sync_mode == WB_SYNC_ALL)
1924 lock_page(page);
1925 else if (!trylock_page(page))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001926 continue;
1927
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001928 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001929continue_unlock:
1930 unlock_page(page);
1931 continue;
1932 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001933
1934 if (!PageDirty(page)) {
1935 /* someone wrote it for us */
1936 goto continue_unlock;
1937 }
1938
Jaegeuk Kimb0f3b872020-07-16 09:57:03 -07001939 /* flush inline_data/inode, if it's async context. */
1940 if (!do_balance)
1941 goto write_node;
1942
1943 /* flush inline_data */
Chao Yub763f3b2021-04-28 17:20:31 +08001944 if (page_private_inline(page)) {
1945 clear_page_private_inline(page);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001946 unlock_page(page);
1947 flush_inline_data(sbi, ino_of_node(page));
Chao Yu9a4cbc92016-02-22 18:35:46 +08001948 goto lock_node;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001949 }
1950
Chao Yu052a82d2019-08-22 20:17:56 +08001951 /* flush dirty inode */
1952 if (IS_INODE(page) && may_dirty) {
1953 may_dirty = false;
1954 if (flush_dirty_inode(page))
1955 goto lock_node;
1956 }
Jaegeuk Kimb0f3b872020-07-16 09:57:03 -07001957write_node:
Chao Yubae0ee72018-12-25 17:43:42 +08001958 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kimfa3d2bd2016-01-28 11:48:52 -08001959
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001960 if (!clear_page_dirty_for_io(page))
1961 goto continue_unlock;
1962
Jaegeuk Kim52681372016-04-13 16:24:44 -07001963 set_fsync_mark(page, 0);
1964 set_dentry_mark(page, 0);
Jaegeuk Kim52746512014-08-11 18:18:36 -07001965
Yunlong Song401db792017-07-27 20:11:00 +08001966 ret = __write_node_page(page, false, &submitted,
Chao Yu50fa53e2018-08-02 23:03:19 +08001967 wbc, do_balance, io_type, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001968 if (ret)
Jaegeuk Kim52746512014-08-11 18:18:36 -07001969 unlock_page(page);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001970 else if (submitted)
Chao Yu3f5f4952016-09-29 18:50:10 +08001971 nwritten++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001972
1973 if (--wbc->nr_to_write == 0)
1974 break;
1975 }
1976 pagevec_release(&pvec);
1977 cond_resched();
1978
1979 if (wbc->nr_to_write == 0) {
1980 step = 2;
1981 break;
1982 }
1983 }
1984
1985 if (step < 2) {
Jaegeuk Kim100c0652019-08-28 19:58:26 -07001986 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1987 wbc->sync_mode == WB_SYNC_NONE && step == 1)
Chao Yufd8c8ca2018-07-25 19:16:21 +08001988 goto out;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001989 step++;
1990 goto next_step;
1991 }
Chao Yufd8c8ca2018-07-25 19:16:21 +08001992out:
Chao Yu3f5f4952016-09-29 18:50:10 +08001993 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001994 f2fs_submit_merged_write(sbi, NODE);
Chao Yudb198ae2018-01-18 17:29:10 +08001995
1996 if (unlikely(f2fs_cp_error(sbi)))
1997 return -EIO;
Chao Yu3f5f4952016-09-29 18:50:10 +08001998 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001999}
2000
Chao Yu50fa53e2018-08-02 23:03:19 +08002001int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
2002 unsigned int seq_id)
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002003{
Chao Yu50fa53e2018-08-02 23:03:19 +08002004 struct fsync_node_entry *fn;
2005 struct page *page;
2006 struct list_head *head = &sbi->fsync_node_list;
2007 unsigned long flags;
2008 unsigned int cur_seq_id = 0;
Miklos Szeredi280db3c2016-09-16 12:44:21 +02002009 int ret2, ret = 0;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002010
Chao Yu50fa53e2018-08-02 23:03:19 +08002011 while (seq_id && cur_seq_id < seq_id) {
2012 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
2013 if (list_empty(head)) {
2014 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2015 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002016 }
Chao Yu50fa53e2018-08-02 23:03:19 +08002017 fn = list_first_entry(head, struct fsync_node_entry, list);
2018 if (fn->seq_id > seq_id) {
2019 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2020 break;
2021 }
2022 cur_seq_id = fn->seq_id;
2023 page = fn->page;
2024 get_page(page);
2025 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2026
Chao Yubae0ee72018-12-25 17:43:42 +08002027 f2fs_wait_on_page_writeback(page, NODE, true, false);
Chao Yu50fa53e2018-08-02 23:03:19 +08002028 if (TestClearPageError(page))
2029 ret = -EIO;
2030
2031 put_page(page);
2032
2033 if (ret)
2034 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002035 }
2036
Miklos Szeredi280db3c2016-09-16 12:44:21 +02002037 ret2 = filemap_check_errors(NODE_MAPPING(sbi));
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002038 if (!ret)
2039 ret = ret2;
Chao Yu50fa53e2018-08-02 23:03:19 +08002040
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002041 return ret;
2042}
2043
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002044static int f2fs_write_node_pages(struct address_space *mapping,
2045 struct writeback_control *wbc)
2046{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002047 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002048 struct blk_plug plug;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09002049 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002050
Chao Yu0771fcc2017-06-29 23:20:45 +08002051 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2052 goto skip_write;
2053
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09002054 /* balancing f2fs's metadata in background */
Chao Yu7bcd0cf2020-03-19 19:57:58 +08002055 f2fs_balance_fs_bg(sbi, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002056
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09002057 /* collect a number of dirty node pages and write together */
Jaegeuk Kim812a9592019-01-22 14:04:33 -08002058 if (wbc->sync_mode != WB_SYNC_ALL &&
2059 get_pages(sbi, F2FS_DIRTY_NODES) <
2060 nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002061 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09002062
Chao Yuc29fd0c2018-06-04 23:20:36 +08002063 if (wbc->sync_mode == WB_SYNC_ALL)
2064 atomic_inc(&sbi->wb_sync_req[NODE]);
2065 else if (atomic_read(&sbi->wb_sync_req[NODE]))
2066 goto skip_write;
2067
Yunlei Hed31c7c32016-02-04 16:14:00 +08002068 trace_f2fs_writepages(mapping->host, wbc, NODE);
2069
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09002070 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002071 blk_start_plug(&plug);
Chao Yu4d57b862018-05-30 00:20:41 +08002072 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002073 blk_finish_plug(&plug);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09002074 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Chao Yuc29fd0c2018-06-04 23:20:36 +08002075
2076 if (wbc->sync_mode == WB_SYNC_ALL)
2077 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002078 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002079
2080skip_write:
2081 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002082 trace_f2fs_writepages(mapping->host, wbc, NODE);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002083 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002084}
2085
2086static int f2fs_set_node_page_dirty(struct page *page)
2087{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002088 trace_f2fs_set_page_dirty(page, NODE);
2089
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002090 if (!PageUptodate(page))
2091 SetPageUptodate(page);
Weichao Guo54c55c42018-03-09 23:10:21 +08002092#ifdef CONFIG_F2FS_CHECK_FS
2093 if (IS_INODE(page))
2094 f2fs_inode_chksum_set(F2FS_P_SB(page), page);
2095#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002096 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07002097 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07002098 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Chao Yub763f3b2021-04-28 17:20:31 +08002099 set_page_private_reference(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002100 return 1;
2101 }
2102 return 0;
2103}
2104
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002105/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002106 * Structure of the f2fs node operations
2107 */
2108const struct address_space_operations f2fs_node_aops = {
2109 .writepage = f2fs_write_node_page,
2110 .writepages = f2fs_write_node_pages,
2111 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002112 .invalidatepage = f2fs_invalidate_page,
2113 .releasepage = f2fs_release_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002114#ifdef CONFIG_MIGRATION
Jack Qiua87aff12020-07-24 16:55:28 +08002115 .migratepage = f2fs_migrate_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002116#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002117};
2118
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002119static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
2120 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002121{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002122 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002123}
2124
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002125static int __insert_free_nid(struct f2fs_sb_info *sbi,
Liu Songb815bdc2020-06-28 21:48:13 +08002126 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002127{
Chao Yub8559dc2016-10-12 19:28:29 +08002128 struct f2fs_nm_info *nm_i = NM_I(sbi);
Fan Lia0761f62017-10-28 19:03:37 +08002129 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
Yi Zhuang5f029c02021-04-06 09:47:35 +08002130
Fan Lia0761f62017-10-28 19:03:37 +08002131 if (err)
2132 return err;
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002133
Liu Songb815bdc2020-06-28 21:48:13 +08002134 nm_i->nid_cnt[FREE_NID]++;
2135 list_add_tail(&i->list, &nm_i->free_nid_list);
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002136 return 0;
Chao Yub8559dc2016-10-12 19:28:29 +08002137}
2138
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002139static void __remove_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08002140 struct free_nid *i, enum nid_state state)
Chao Yub8559dc2016-10-12 19:28:29 +08002141{
2142 struct f2fs_nm_info *nm_i = NM_I(sbi);
2143
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002144 f2fs_bug_on(sbi, state != i->state);
2145 nm_i->nid_cnt[state]--;
2146 if (state == FREE_NID)
2147 list_del(&i->list);
Fan Lia0761f62017-10-28 19:03:37 +08002148 radix_tree_delete(&nm_i->free_nid_root, i->nid);
2149}
2150
2151static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2152 enum nid_state org_state, enum nid_state dst_state)
2153{
2154 struct f2fs_nm_info *nm_i = NM_I(sbi);
2155
2156 f2fs_bug_on(sbi, org_state != i->state);
2157 i->state = dst_state;
2158 nm_i->nid_cnt[org_state]--;
2159 nm_i->nid_cnt[dst_state]++;
2160
2161 switch (dst_state) {
2162 case PREALLOC_NID:
2163 list_del(&i->list);
2164 break;
2165 case FREE_NID:
2166 list_add_tail(&i->list, &nm_i->free_nid_list);
2167 break;
2168 default:
2169 BUG_ON(1);
2170 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002171}
2172
LiFan5921aaa2017-11-22 16:07:23 +08002173static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2174 bool set, bool build)
2175{
2176 struct f2fs_nm_info *nm_i = NM_I(sbi);
2177 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2178 unsigned int nid_ofs = nid - START_NID(nid);
2179
2180 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2181 return;
2182
2183 if (set) {
2184 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2185 return;
2186 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2187 nm_i->free_nid_count[nat_ofs]++;
2188 } else {
2189 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2190 return;
2191 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2192 if (!build)
2193 nm_i->free_nid_count[nat_ofs]--;
2194 }
2195}
2196
Chao Yu4ac91242017-02-23 10:53:49 +08002197/* return if the nid is recognized as free */
LiFan5921aaa2017-11-22 16:07:23 +08002198static bool add_free_nid(struct f2fs_sb_info *sbi,
2199 nid_t nid, bool build, bool update)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002200{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002201 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu30a61dd2017-03-22 14:45:05 +08002202 struct free_nid *i, *e;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002203 struct nat_entry *ne;
Chao Yu30a61dd2017-03-22 14:45:05 +08002204 int err = -EINVAL;
2205 bool ret = false;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09002206
2207 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08002208 if (unlikely(nid == 0))
Chao Yu4ac91242017-02-23 10:53:49 +08002209 return false;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002210
Chao Yu626bcf22019-04-15 15:28:36 +08002211 if (unlikely(f2fs_check_nid_range(sbi, nid)))
2212 return false;
2213
Gu Zheng7bd59382013-10-22 14:52:26 +08002214 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002215 i->nid = nid;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002216 i->state = FREE_NID;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002217
LiFan5921aaa2017-11-22 16:07:23 +08002218 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002219
Chao Yub8559dc2016-10-12 19:28:29 +08002220 spin_lock(&nm_i->nid_list_lock);
Chao Yu30a61dd2017-03-22 14:45:05 +08002221
2222 if (build) {
2223 /*
2224 * Thread A Thread B
2225 * - f2fs_create
2226 * - f2fs_new_inode
Chao Yu4d57b862018-05-30 00:20:41 +08002227 * - f2fs_alloc_nid
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002228 * - __insert_nid_to_list(PREALLOC_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002229 * - f2fs_balance_fs_bg
Chao Yu4d57b862018-05-30 00:20:41 +08002230 * - f2fs_build_free_nids
2231 * - __f2fs_build_free_nids
Chao Yu30a61dd2017-03-22 14:45:05 +08002232 * - scan_nat_page
2233 * - add_free_nid
2234 * - __lookup_nat_cache
2235 * - f2fs_add_link
Chao Yu4d57b862018-05-30 00:20:41 +08002236 * - f2fs_init_inode_metadata
2237 * - f2fs_new_inode_page
2238 * - f2fs_new_node_page
Chao Yu30a61dd2017-03-22 14:45:05 +08002239 * - set_node_addr
Chao Yu4d57b862018-05-30 00:20:41 +08002240 * - f2fs_alloc_nid_done
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002241 * - __remove_nid_from_list(PREALLOC_NID)
2242 * - __insert_nid_to_list(FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002243 */
2244 ne = __lookup_nat_cache(nm_i, nid);
2245 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2246 nat_get_blkaddr(ne) != NULL_ADDR))
2247 goto err_out;
2248
2249 e = __lookup_free_nid_list(nm_i, nid);
2250 if (e) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002251 if (e->state == FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002252 ret = true;
2253 goto err_out;
2254 }
2255 }
2256 ret = true;
Liu Songb815bdc2020-06-28 21:48:13 +08002257 err = __insert_free_nid(sbi, i);
Chao Yu30a61dd2017-03-22 14:45:05 +08002258err_out:
LiFan5921aaa2017-11-22 16:07:23 +08002259 if (update) {
2260 update_free_nid_bitmap(sbi, nid, ret, build);
2261 if (!build)
2262 nm_i->available_nids++;
2263 }
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002264 spin_unlock(&nm_i->nid_list_lock);
2265 radix_tree_preload_end();
LiFan5921aaa2017-11-22 16:07:23 +08002266
Chao Yu30a61dd2017-03-22 14:45:05 +08002267 if (err)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002268 kmem_cache_free(free_nid_slab, i);
Chao Yu30a61dd2017-03-22 14:45:05 +08002269 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002270}
2271
Chao Yub8559dc2016-10-12 19:28:29 +08002272static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002273{
Chao Yub8559dc2016-10-12 19:28:29 +08002274 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002275 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002276 bool need_free = false;
2277
Chao Yub8559dc2016-10-12 19:28:29 +08002278 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002279 i = __lookup_free_nid_list(nm_i, nid);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002280 if (i && i->state == FREE_NID) {
Fan Lia0761f62017-10-28 19:03:37 +08002281 __remove_free_nid(sbi, i, FREE_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002282 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002283 }
Chao Yub8559dc2016-10-12 19:28:29 +08002284 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002285
2286 if (need_free)
2287 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002288}
2289
Chao Yue2374012018-06-15 14:45:57 +08002290static int scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002291 struct page *nat_page, nid_t start_nid)
2292{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002293 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002294 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2295 block_t blk_addr;
Chao Yu4ac91242017-02-23 10:53:49 +08002296 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002297 int i;
2298
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002299 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
Chao Yu4ac91242017-02-23 10:53:49 +08002300
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002301 i = start_nid % NAT_ENTRY_PER_BLOCK;
2302
2303 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Chao Yucfb271d2013-12-05 17:15:22 +08002304 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09002305 break;
Haicheng Li23d38842013-05-06 23:15:43 +08002306
2307 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Chao Yue2374012018-06-15 14:45:57 +08002308
2309 if (blk_addr == NEW_ADDR)
2310 return -EINVAL;
2311
LiFan5921aaa2017-11-22 16:07:23 +08002312 if (blk_addr == NULL_ADDR) {
2313 add_free_nid(sbi, start_nid, true, true);
2314 } else {
2315 spin_lock(&NM_I(sbi)->nid_list_lock);
2316 update_free_nid_bitmap(sbi, start_nid, false, true);
2317 spin_unlock(&NM_I(sbi)->nid_list_lock);
2318 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002319 }
Chao Yue2374012018-06-15 14:45:57 +08002320
2321 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002322}
2323
Chao Yu2fbaa252017-11-08 17:47:36 +08002324static void scan_curseg_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002325{
Chao Yu4ac91242017-02-23 10:53:49 +08002326 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2327 struct f2fs_journal *journal = curseg->journal;
Chao Yu2fbaa252017-11-08 17:47:36 +08002328 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08002329
Chao Yu4ac91242017-02-23 10:53:49 +08002330 down_read(&curseg->journal_rwsem);
2331 for (i = 0; i < nats_in_cursum(journal); i++) {
2332 block_t addr;
2333 nid_t nid;
2334
2335 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2336 nid = le32_to_cpu(nid_in_journal(journal, i));
2337 if (addr == NULL_ADDR)
LiFan5921aaa2017-11-22 16:07:23 +08002338 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002339 else
2340 remove_free_nid(sbi, nid);
2341 }
2342 up_read(&curseg->journal_rwsem);
Chao Yu2fbaa252017-11-08 17:47:36 +08002343}
2344
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002345static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002346{
2347 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002348 unsigned int i, idx;
Fan Li97456572017-11-07 19:14:24 +08002349 nid_t nid;
Chao Yu4ac91242017-02-23 10:53:49 +08002350
2351 down_read(&nm_i->nat_tree_lock);
2352
2353 for (i = 0; i < nm_i->nat_blocks; i++) {
2354 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2355 continue;
2356 if (!nm_i->free_nid_count[i])
2357 continue;
2358 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
Fan Li97456572017-11-07 19:14:24 +08002359 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2360 NAT_ENTRY_PER_BLOCK, idx);
2361 if (idx >= NAT_ENTRY_PER_BLOCK)
2362 break;
Chao Yu4ac91242017-02-23 10:53:49 +08002363
2364 nid = i * NAT_ENTRY_PER_BLOCK + idx;
LiFan5921aaa2017-11-22 16:07:23 +08002365 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002366
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002367 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
Chao Yu4ac91242017-02-23 10:53:49 +08002368 goto out;
2369 }
2370 }
2371out:
Chao Yu2fbaa252017-11-08 17:47:36 +08002372 scan_curseg_cache(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002373
Chao Yu4ac91242017-02-23 10:53:49 +08002374 up_read(&nm_i->nat_tree_lock);
2375}
2376
Chao Yue2374012018-06-15 14:45:57 +08002377static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
Chao Yu4d57b862018-05-30 00:20:41 +08002378 bool sync, bool mount)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002379{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002380 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yue2374012018-06-15 14:45:57 +08002381 int i = 0, ret;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002382 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002383
Yunlei Hee9cdd302017-04-26 15:56:52 +08002384 if (unlikely(nid >= nm_i->max_nid))
2385 nid = 0;
2386
Sahitya Tummalae2cab032020-08-18 15:40:14 +05302387 if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
2388 nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
2389
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002390 /* Enough entries */
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002391 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002392 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002393
Chao Yu4d57b862018-05-30 00:20:41 +08002394 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
Chao Yue2374012018-06-15 14:45:57 +08002395 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002396
Chao Yu4ac91242017-02-23 10:53:49 +08002397 if (!mount) {
2398 /* try to find free nids in free_nid_bitmap */
2399 scan_free_nid_bits(sbi);
2400
Fan Li74986212017-11-07 11:04:33 +08002401 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002402 return 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002403 }
2404
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002405 /* readahead nat pages to be scanned */
Chao Yu4d57b862018-05-30 00:20:41 +08002406 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
Chao Yu26879fb2015-10-12 17:05:59 +08002407 META_NAT, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002408
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002409 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002410
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002411 while (1) {
Yunlei He66e83362017-11-17 16:13:38 +08002412 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2413 nm_i->nat_block_bitmap)) {
2414 struct page *page = get_current_nat_page(sbi, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002415
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002416 if (IS_ERR(page)) {
2417 ret = PTR_ERR(page);
2418 } else {
2419 ret = scan_nat_page(sbi, page, nid);
2420 f2fs_put_page(page, 1);
2421 }
Chao Yue2374012018-06-15 14:45:57 +08002422
2423 if (ret) {
2424 up_read(&nm_i->nat_tree_lock);
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002425 f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002426 return ret;
Chao Yue2374012018-06-15 14:45:57 +08002427 }
Yunlei He66e83362017-11-17 16:13:38 +08002428 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002429
2430 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08002431 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002432 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002433
Chao Yua6d494b2015-07-24 18:26:26 +08002434 if (++i >= FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002435 break;
2436 }
2437
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002438 /* go to the next free nat pages to find free nids abundantly */
2439 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002440
2441 /* find free nids from current sum_pages */
Chao Yu2fbaa252017-11-08 17:47:36 +08002442 scan_curseg_cache(sbi);
Chao Yudfc08a12016-02-14 18:50:40 +08002443
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002444 up_read(&nm_i->nat_tree_lock);
Chao Yu2db23882015-10-12 17:07:33 +08002445
Chao Yu4d57b862018-05-30 00:20:41 +08002446 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
Chao Yuea1a29a02015-10-12 17:08:48 +08002447 nm_i->ra_nid_pages, META_NAT, false);
Chao Yue2374012018-06-15 14:45:57 +08002448
2449 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002450}
2451
Chao Yue2374012018-06-15 14:45:57 +08002452int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
Chao Yu2411cf52016-10-11 22:31:34 +08002453{
Chao Yue2374012018-06-15 14:45:57 +08002454 int ret;
2455
Chao Yu2411cf52016-10-11 22:31:34 +08002456 mutex_lock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002457 ret = __f2fs_build_free_nids(sbi, sync, mount);
Chao Yu2411cf52016-10-11 22:31:34 +08002458 mutex_unlock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002459
2460 return ret;
Chao Yu2411cf52016-10-11 22:31:34 +08002461}
2462
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002463/*
2464 * If this function returns success, caller can obtain a new nid
2465 * from second parameter of this function.
2466 * The returned nid could be used ino as well as nid when inode is created.
2467 */
Chao Yu4d57b862018-05-30 00:20:41 +08002468bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002469{
2470 struct f2fs_nm_info *nm_i = NM_I(sbi);
2471 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002472retry:
Chao Yu55523512017-02-25 11:08:28 +08002473 if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
Chao Yuc45d6002019-11-01 17:53:23 +08002474 f2fs_show_injection_info(sbi, FAULT_ALLOC_NID);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002475 return false;
Chao Yu55523512017-02-25 11:08:28 +08002476 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +02002477
Chao Yub8559dc2016-10-12 19:28:29 +08002478 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002479
Chao Yu04d47e62016-11-17 20:53:11 +08002480 if (unlikely(nm_i->available_nids == 0)) {
2481 spin_unlock(&nm_i->nid_list_lock);
2482 return false;
2483 }
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002484
Chao Yu4d57b862018-05-30 00:20:41 +08002485 /* We should not use stale free nids created by f2fs_build_free_nids */
2486 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002487 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2488 i = list_first_entry(&nm_i->free_nid_list,
Chao Yub8559dc2016-10-12 19:28:29 +08002489 struct free_nid, list);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002490 *nid = i->nid;
Chao Yub8559dc2016-10-12 19:28:29 +08002491
Fan Lia0761f62017-10-28 19:03:37 +08002492 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
Chao Yu04d47e62016-11-17 20:53:11 +08002493 nm_i->available_nids--;
Chao Yu4ac91242017-02-23 10:53:49 +08002494
Chao Yu346fe752017-03-13 20:10:41 +08002495 update_free_nid_bitmap(sbi, *nid, false, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002496
Chao Yub8559dc2016-10-12 19:28:29 +08002497 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002498 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002499 }
Chao Yub8559dc2016-10-12 19:28:29 +08002500 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002501
2502 /* Let's scan nat pages and its caches to get free nids */
Jaegeuk Kimf84262b2018-09-19 15:45:19 -07002503 if (!f2fs_build_free_nids(sbi, true, false))
2504 goto retry;
2505 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002506}
2507
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002508/*
Chao Yu4d57b862018-05-30 00:20:41 +08002509 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002510 */
Chao Yu4d57b862018-05-30 00:20:41 +08002511void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002512{
2513 struct f2fs_nm_info *nm_i = NM_I(sbi);
2514 struct free_nid *i;
2515
Chao Yub8559dc2016-10-12 19:28:29 +08002516 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002517 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002518 f2fs_bug_on(sbi, !i);
Fan Lia0761f62017-10-28 19:03:37 +08002519 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08002520 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002521
2522 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002523}
2524
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002525/*
Chao Yu4d57b862018-05-30 00:20:41 +08002526 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002527 */
Chao Yu4d57b862018-05-30 00:20:41 +08002528void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002529{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002530 struct f2fs_nm_info *nm_i = NM_I(sbi);
2531 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002532 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002533
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002534 if (!nid)
2535 return;
2536
Chao Yub8559dc2016-10-12 19:28:29 +08002537 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002538 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002539 f2fs_bug_on(sbi, !i);
2540
Chao Yu4d57b862018-05-30 00:20:41 +08002541 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
Fan Lia0761f62017-10-28 19:03:37 +08002542 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002543 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08002544 } else {
Fan Lia0761f62017-10-28 19:03:37 +08002545 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
Haicheng Li95630cb2013-05-06 23:15:41 +08002546 }
Chao Yu04d47e62016-11-17 20:53:11 +08002547
2548 nm_i->available_nids++;
2549
Chao Yu346fe752017-03-13 20:10:41 +08002550 update_free_nid_bitmap(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002551
Chao Yub8559dc2016-10-12 19:28:29 +08002552 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002553
2554 if (need_free)
2555 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002556}
2557
Chao Yu4d57b862018-05-30 00:20:41 +08002558int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
Chao Yu31696582015-07-28 18:33:46 +08002559{
2560 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu31696582015-07-28 18:33:46 +08002561 int nr = nr_shrink;
2562
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002563 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Jaegeuk Kimad4edb82016-06-16 16:41:49 -07002564 return 0;
2565
Chao Yu31696582015-07-28 18:33:46 +08002566 if (!mutex_trylock(&nm_i->build_lock))
2567 return 0;
2568
Chao Yu042be372020-05-08 17:50:20 +08002569 while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) {
2570 struct free_nid *i, *next;
2571 unsigned int batch = SHRINK_NID_BATCH_SIZE;
Chao Yub8559dc2016-10-12 19:28:29 +08002572
Chao Yu042be372020-05-08 17:50:20 +08002573 spin_lock(&nm_i->nid_list_lock);
2574 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
2575 if (!nr_shrink || !batch ||
2576 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2577 break;
2578 __remove_free_nid(sbi, i, FREE_NID);
2579 kmem_cache_free(free_nid_slab, i);
2580 nr_shrink--;
2581 batch--;
2582 }
2583 spin_unlock(&nm_i->nid_list_lock);
Chao Yu31696582015-07-28 18:33:46 +08002584 }
Chao Yu042be372020-05-08 17:50:20 +08002585
Chao Yu31696582015-07-28 18:33:46 +08002586 mutex_unlock(&nm_i->build_lock);
2587
2588 return nr - nr_shrink;
2589}
2590
Chao Yu9627a7b2020-07-06 18:23:36 +08002591int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08002592{
Chao Yu28cdce02014-03-11 13:37:38 +08002593 void *src_addr, *dst_addr;
2594 size_t inline_size;
2595 struct page *ipage;
2596 struct f2fs_inode *ri;
2597
Chao Yu4d57b862018-05-30 00:20:41 +08002598 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu9627a7b2020-07-06 18:23:36 +08002599 if (IS_ERR(ipage))
2600 return PTR_ERR(ipage);
Chao Yu28cdce02014-03-11 13:37:38 +08002601
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002602 ri = F2FS_INODE(page);
Yunlei He1eca05a2018-01-03 18:03:04 +08002603 if (ri->i_inline & F2FS_INLINE_XATTR) {
Chao Yu96dd0252020-12-07 17:54:41 +08002604 if (!f2fs_has_inline_xattr(inode)) {
2605 set_inode_flag(inode, FI_INLINE_XATTR);
2606 stat_inc_inline_xattr(inode);
2607 }
Yunlei He1eca05a2018-01-03 18:03:04 +08002608 } else {
Chao Yu96dd0252020-12-07 17:54:41 +08002609 if (f2fs_has_inline_xattr(inode)) {
2610 stat_dec_inline_xattr(inode);
2611 clear_inode_flag(inode, FI_INLINE_XATTR);
2612 }
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002613 goto update_inode;
2614 }
2615
Chao Yu6afc6622017-09-06 21:59:50 +08002616 dst_addr = inline_xattr_addr(inode, ipage);
2617 src_addr = inline_xattr_addr(inode, page);
Chao Yu28cdce02014-03-11 13:37:38 +08002618 inline_size = inline_xattr_size(inode);
2619
Chao Yubae0ee72018-12-25 17:43:42 +08002620 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yu28cdce02014-03-11 13:37:38 +08002621 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002622update_inode:
Chao Yu4d57b862018-05-30 00:20:41 +08002623 f2fs_update_inode(inode, ipage);
Chao Yu28cdce02014-03-11 13:37:38 +08002624 f2fs_put_page(ipage, 1);
Chao Yu9627a7b2020-07-06 18:23:36 +08002625 return 0;
Chao Yu28cdce02014-03-11 13:37:38 +08002626}
2627
Chao Yu4d57b862018-05-30 00:20:41 +08002628int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002629{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002630 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002631 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
Yunlei He87905682017-07-18 09:48:12 +08002632 nid_t new_xnid;
2633 struct dnode_of_data dn;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002634 struct node_info ni;
Chao Yud2600812017-02-08 17:39:45 +08002635 struct page *xpage;
Chao Yu77357302018-07-17 00:02:17 +08002636 int err;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002637
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002638 if (!prev_xnid)
2639 goto recover_xnid;
2640
Chao Yud2600812017-02-08 17:39:45 +08002641 /* 1: invalidate the previous xattr nid */
Chao Yu77357302018-07-17 00:02:17 +08002642 err = f2fs_get_node_info(sbi, prev_xnid, &ni);
2643 if (err)
2644 return err;
2645
Chao Yu4d57b862018-05-30 00:20:41 +08002646 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +08002647 dec_valid_node_count(sbi, inode, false);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002648 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002649
2650recover_xnid:
Chao Yud2600812017-02-08 17:39:45 +08002651 /* 2: update xattr nid in inode */
Chao Yu4d57b862018-05-30 00:20:41 +08002652 if (!f2fs_alloc_nid(sbi, &new_xnid))
Yunlei He87905682017-07-18 09:48:12 +08002653 return -ENOSPC;
2654
2655 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
Chao Yu4d57b862018-05-30 00:20:41 +08002656 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Yunlei He87905682017-07-18 09:48:12 +08002657 if (IS_ERR(xpage)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002658 f2fs_alloc_nid_failed(sbi, new_xnid);
Yunlei He87905682017-07-18 09:48:12 +08002659 return PTR_ERR(xpage);
2660 }
2661
Chao Yu4d57b862018-05-30 00:20:41 +08002662 f2fs_alloc_nid_done(sbi, new_xnid);
2663 f2fs_update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002664
Chao Yud2600812017-02-08 17:39:45 +08002665 /* 3: update and set xattr node page dirty */
Yunlei He87905682017-07-18 09:48:12 +08002666 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
Chao Yud2600812017-02-08 17:39:45 +08002667
Chao Yud2600812017-02-08 17:39:45 +08002668 set_page_dirty(xpage);
2669 f2fs_put_page(xpage, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002670
Chao Yud2600812017-02-08 17:39:45 +08002671 return 0;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002672}
2673
Chao Yu4d57b862018-05-30 00:20:41 +08002674int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002675{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002676 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002677 nid_t ino = ino_of_node(page);
2678 struct node_info old_ni, new_ni;
2679 struct page *ipage;
Chao Yu77357302018-07-17 00:02:17 +08002680 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002681
Chao Yu77357302018-07-17 00:02:17 +08002682 err = f2fs_get_node_info(sbi, ino, &old_ni);
2683 if (err)
2684 return err;
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09002685
2686 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2687 return -EINVAL;
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002688retry:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07002689 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002690 if (!ipage) {
Chao Yu5df7731f2020-02-17 17:45:44 +08002691 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002692 goto retry;
2693 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002694
arter97e1c42042014-08-06 23:22:50 +09002695 /* Should not use this inode from free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08002696 remove_free_nid(sbi, ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002697
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002698 if (!PageUptodate(ipage))
2699 SetPageUptodate(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002700 fill_node_footer(ipage, ino, ino, 0, true);
Chao Yuef2a0072018-10-03 22:32:44 +08002701 set_cold_node(ipage, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002702
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002703 src = F2FS_INODE(page);
2704 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002705
Zheng Yongjun36218b82020-12-22 21:34:15 +08002706 memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002707 dst->i_size = 0;
2708 dst->i_blocks = cpu_to_le64(1);
2709 dst->i_links = cpu_to_le32(1);
2710 dst->i_xattr_nid = 0;
Chao Yu7a2af762017-07-19 00:19:06 +08002711 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
Chao Yu5c571322017-07-26 00:01:41 +08002712 if (dst->i_inline & F2FS_EXTRA_ATTR) {
Chao Yu7a2af762017-07-19 00:19:06 +08002713 dst->i_extra_isize = src->i_extra_isize;
Chao Yu6afc6622017-09-06 21:59:50 +08002714
Chao Yu7beb01f2018-10-24 18:34:26 +08002715 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
Chao Yu6afc6622017-09-06 21:59:50 +08002716 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2717 i_inline_xattr_size))
2718 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2719
Chao Yu7beb01f2018-10-24 18:34:26 +08002720 if (f2fs_sb_has_project_quota(sbi) &&
Chao Yu5c571322017-07-26 00:01:41 +08002721 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2722 i_projid))
2723 dst->i_projid = src->i_projid;
Chao Yu5cd1f382018-09-25 15:36:01 +08002724
Chao Yu7beb01f2018-10-24 18:34:26 +08002725 if (f2fs_sb_has_inode_crtime(sbi) &&
Chao Yu5cd1f382018-09-25 15:36:01 +08002726 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2727 i_crtime_nsec)) {
2728 dst->i_crtime = src->i_crtime;
2729 dst->i_crtime_nsec = src->i_crtime_nsec;
2730 }
Chao Yu5c571322017-07-26 00:01:41 +08002731 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002732
2733 new_ni = old_ni;
2734 new_ni.ino = ino;
2735
Chao Yu0abd6752017-07-09 00:13:07 +08002736 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09002737 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002738 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002739 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07002740 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002741 f2fs_put_page(ipage, 1);
2742 return 0;
2743}
2744
Chao Yu77357302018-07-17 00:02:17 +08002745int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002746 unsigned int segno, struct f2fs_summary_block *sum)
2747{
2748 struct f2fs_node *rn;
2749 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002750 block_t addr;
Chao Yu9ecf4b82014-12-18 18:29:05 +08002751 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002752
2753 /* scan the node segment */
2754 last_offset = sbi->blocks_per_seg;
2755 addr = START_BLOCK(sbi, segno);
2756 sum_entry = &sum->entries[0];
2757
Chao Yu9ecf4b82014-12-18 18:29:05 +08002758 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +00002759 nrpages = bio_max_segs(last_offset - i);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002760
arter97e1c42042014-08-06 23:22:50 +09002761 /* readahead node pages */
Chao Yu4d57b862018-05-30 00:20:41 +08002762 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002763
Chao Yu9ecf4b82014-12-18 18:29:05 +08002764 for (idx = addr; idx < addr + nrpages; idx++) {
Chao Yu4d57b862018-05-30 00:20:41 +08002765 struct page *page = f2fs_get_tmp_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08002766
Chao Yu77357302018-07-17 00:02:17 +08002767 if (IS_ERR(page))
2768 return PTR_ERR(page);
2769
Chao Yu9ecf4b82014-12-18 18:29:05 +08002770 rn = F2FS_NODE(page);
2771 sum_entry->nid = rn->footer.nid;
2772 sum_entry->version = 0;
2773 sum_entry->ofs_in_node = 0;
2774 sum_entry++;
2775 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08002776 }
Chao Yubac4eef2014-05-27 08:41:07 +08002777
Chao Yu9ecf4b82014-12-18 18:29:05 +08002778 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08002779 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002780 }
Chao Yu77357302018-07-17 00:02:17 +08002781 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002782}
2783
Chao Yuaec71382014-06-24 09:18:20 +08002784static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002785{
2786 struct f2fs_nm_info *nm_i = NM_I(sbi);
2787 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002788 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002789 int i;
2790
Chao Yub7ad7512016-02-19 18:08:46 +08002791 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002792 for (i = 0; i < nats_in_cursum(journal); i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002793 struct nat_entry *ne;
2794 struct f2fs_nat_entry raw_ne;
Chao Yudfc08a12016-02-14 18:50:40 +08002795 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002796
Chao Yub862676e2021-03-22 19:47:30 +08002797 if (f2fs_check_nid_range(sbi, nid))
2798 continue;
2799
Chao Yudfc08a12016-02-14 18:50:40 +08002800 raw_ne = nat_in_journal(journal, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08002801
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002802 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002803 if (!ne) {
Yunlei He12f9ef32017-11-10 13:36:51 -08002804 ne = __alloc_nat_entry(nid, true);
2805 __init_nat_entry(nm_i, ne, &raw_ne, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002806 }
Chao Yu04d47e62016-11-17 20:53:11 +08002807
2808 /*
2809 * if a free nat in journal has not been used after last
2810 * checkpoint, we should remove it from available nids,
2811 * since later we will add it again.
2812 */
2813 if (!get_nat_flag(ne, IS_DIRTY) &&
2814 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2815 spin_lock(&nm_i->nid_list_lock);
2816 nm_i->available_nids--;
2817 spin_unlock(&nm_i->nid_list_lock);
2818 }
2819
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002820 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002821 }
Chao Yudfc08a12016-02-14 18:50:40 +08002822 update_nats_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002823 up_write(&curseg->journal_rwsem);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002824}
2825
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002826static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2827 struct list_head *head, int max)
2828{
2829 struct nat_entry_set *cur;
2830
2831 if (nes->entry_cnt >= max)
2832 goto add_out;
2833
2834 list_for_each_entry(cur, head, set_list) {
2835 if (cur->entry_cnt >= nes->entry_cnt) {
2836 list_add(&nes->set_list, cur->set_list.prev);
2837 return;
2838 }
2839 }
2840add_out:
2841 list_add_tail(&nes->set_list, head);
2842}
2843
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002844static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002845 struct page *page)
2846{
2847 struct f2fs_nm_info *nm_i = NM_I(sbi);
2848 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2849 struct f2fs_nat_block *nat_blk = page_address(page);
2850 int valid = 0;
Fan Li37a0ab22017-10-30 15:19:48 +08002851 int i = 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002852
2853 if (!enabled_nat_bits(sbi, NULL))
2854 return;
2855
Fan Li37a0ab22017-10-30 15:19:48 +08002856 if (nat_index == 0) {
2857 valid = 1;
2858 i = 1;
2859 }
2860 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
Chao Yu36af5f42019-05-28 17:23:33 +08002861 if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002862 valid++;
2863 }
2864 if (valid == 0) {
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002865 __set_bit_le(nat_index, nm_i->empty_nat_bits);
2866 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002867 return;
2868 }
2869
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002870 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002871 if (valid == NAT_ENTRY_PER_BLOCK)
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002872 __set_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002873 else
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002874 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002875}
2876
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002877static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002878 struct nat_entry_set *set, struct cp_control *cpc)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002879{
2880 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002881 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002882 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
2883 bool to_journal = true;
2884 struct f2fs_nat_block *nat_blk;
2885 struct nat_entry *ne, *cur;
2886 struct page *page = NULL;
2887
2888 /*
2889 * there are two steps to flush nat entries:
2890 * #1, flush nat entries to journal in current hot data summary block.
2891 * #2, flush nat entries to nat page.
2892 */
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002893 if (enabled_nat_bits(sbi, cpc) ||
2894 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002895 to_journal = false;
2896
2897 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002898 down_write(&curseg->journal_rwsem);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002899 } else {
2900 page = get_next_nat_page(sbi, start_nid);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002901 if (IS_ERR(page))
2902 return PTR_ERR(page);
2903
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002904 nat_blk = page_address(page);
2905 f2fs_bug_on(sbi, !nat_blk);
2906 }
2907
2908 /* flush dirty nats in nat entry set */
2909 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
2910 struct f2fs_nat_entry *raw_ne;
2911 nid_t nid = nat_get_nid(ne);
2912 int offset;
2913
Chao Yufebeca62017-06-05 18:29:08 +08002914 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002915
2916 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08002917 offset = f2fs_lookup_journal_in_cursum(journal,
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002918 NAT_JOURNAL, nid, 1);
2919 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002920 raw_ne = &nat_in_journal(journal, offset);
2921 nid_in_journal(journal, offset) = cpu_to_le32(nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002922 } else {
2923 raw_ne = &nat_blk->entries[nid - start_nid];
2924 }
2925 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002926 nat_reset_flag(ne);
Kinglong Mee0b28b712017-02-28 21:34:47 +08002927 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
Chao Yu04d47e62016-11-17 20:53:11 +08002928 if (nat_get_blkaddr(ne) == NULL_ADDR) {
LiFan5921aaa2017-11-22 16:07:23 +08002929 add_free_nid(sbi, nid, false, true);
Chao Yu4ac91242017-02-23 10:53:49 +08002930 } else {
2931 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu346fe752017-03-13 20:10:41 +08002932 update_free_nid_bitmap(sbi, nid, false, false);
Chao Yu04d47e62016-11-17 20:53:11 +08002933 spin_unlock(&NM_I(sbi)->nid_list_lock);
2934 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002935 }
2936
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002937 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002938 up_write(&curseg->journal_rwsem);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002939 } else {
2940 __update_nat_bits(sbi, start_nid, page);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002941 f2fs_put_page(page, 1);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002942 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002943
Yunlei He59c90812017-03-13 20:22:18 +08002944 /* Allow dirty nats by node block allocation in write_begin */
2945 if (!set->entry_cnt) {
2946 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
2947 kmem_cache_free(nat_entry_set_slab, set);
2948 }
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002949 return 0;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002950}
2951
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002952/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002953 * This function is called during the checkpointing process.
2954 */
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002955int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002956{
2957 struct f2fs_nm_info *nm_i = NM_I(sbi);
2958 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002959 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002960 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002961 struct nat_entry_set *set, *tmp;
2962 unsigned int found;
2963 nid_t set_idx = 0;
2964 LIST_HEAD(sets);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002965 int err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002966
Jaegeuk Kima95ba662020-11-06 13:22:05 -08002967 /*
2968 * during unmount, let's flush nat_bits before checking
2969 * nat_cnt[DIRTY_NAT].
2970 */
Jaegeuk Kim7f2ecdd2018-06-28 19:34:40 -07002971 if (enabled_nat_bits(sbi, cpc)) {
2972 down_write(&nm_i->nat_tree_lock);
2973 remove_nats_in_journal(sbi);
2974 up_write(&nm_i->nat_tree_lock);
2975 }
2976
Jaegeuk Kima95ba662020-11-06 13:22:05 -08002977 if (!nm_i->nat_cnt[DIRTY_NAT])
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002978 return 0;
Jaegeuk Kima5131192016-01-02 09:19:41 -08002979
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002980 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002981
Chao Yuaec71382014-06-24 09:18:20 +08002982 /*
2983 * if there are no enough space in journal to store dirty nat
2984 * entries, remove all entries from journal and merge them
2985 * into nat entry set.
2986 */
Jaegeuk Kim900f7362017-02-27 21:28:53 -08002987 if (enabled_nat_bits(sbi, cpc) ||
Jaegeuk Kima95ba662020-11-06 13:22:05 -08002988 !__has_cursum_space(journal,
2989 nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08002990 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002991
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002992 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002993 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002994 unsigned idx;
Yi Zhuang5f029c02021-04-06 09:47:35 +08002995
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002996 set_idx = setvec[found - 1]->set + 1;
2997 for (idx = 0; idx < found; idx++)
2998 __adjust_nat_entry_set(setvec[idx], &sets,
Chao Yudfc08a12016-02-14 18:50:40 +08002999 MAX_NAT_JENTRIES(journal));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003000 }
Chao Yuaec71382014-06-24 09:18:20 +08003001
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07003002 /* flush dirty nats in nat entry set */
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07003003 list_for_each_entry_safe(set, tmp, &sets, set_list) {
3004 err = __flush_nat_entry_set(sbi, set, cpc);
3005 if (err)
3006 break;
3007 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07003008
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003009 up_write(&nm_i->nat_tree_lock);
Yunlei He59c90812017-03-13 20:22:18 +08003010 /* Allow dirty nats by node block allocation in write_begin */
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07003011
3012 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003013}
3014
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003015static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
3016{
3017 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3018 struct f2fs_nm_info *nm_i = NM_I(sbi);
3019 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
3020 unsigned int i;
3021 __u64 cp_ver = cur_cp_version(ckpt);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003022 block_t nat_bits_addr;
3023
3024 if (!enabled_nat_bits(sbi, NULL))
3025 return 0;
3026
Chao Yudf033ca2018-03-20 23:08:29 +08003027 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
Eric Biggers0b6d4ca2020-06-04 21:57:48 -07003028 nm_i->nat_bits = f2fs_kvzalloc(sbi,
Chao Yuacbf0542017-11-30 19:28:17 +08003029 nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003030 if (!nm_i->nat_bits)
3031 return -ENOMEM;
3032
3033 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
3034 nm_i->nat_bits_blocks;
3035 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
Chao Yu77357302018-07-17 00:02:17 +08003036 struct page *page;
3037
3038 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
Chao Yu3b30eb12018-10-04 11:15:20 +08003039 if (IS_ERR(page))
Chao Yu77357302018-07-17 00:02:17 +08003040 return PTR_ERR(page);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003041
3042 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
3043 page_address(page), F2FS_BLKSIZE);
3044 f2fs_put_page(page, 1);
3045 }
3046
Kinglong Meeced2c7e2017-02-25 19:53:39 +08003047 cp_ver |= (cur_cp_crc(ckpt) << 32);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003048 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
3049 disable_nat_bits(sbi, true);
3050 return 0;
3051 }
3052
3053 nm_i->full_nat_bits = nm_i->nat_bits + 8;
3054 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
3055
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003056 f2fs_notice(sbi, "Found nat_bits in checkpoint");
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003057 return 0;
3058}
3059
Hou Pengyangbd80a4b2017-05-17 02:48:48 +00003060static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
Chao Yu7041d5d2017-03-08 20:07:49 +08003061{
3062 struct f2fs_nm_info *nm_i = NM_I(sbi);
3063 unsigned int i = 0;
3064 nid_t nid, last_nid;
3065
3066 if (!enabled_nat_bits(sbi, NULL))
3067 return;
3068
3069 for (i = 0; i < nm_i->nat_blocks; i++) {
3070 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
3071 if (i >= nm_i->nat_blocks)
3072 break;
3073
3074 __set_bit_le(i, nm_i->nat_block_bitmap);
3075
3076 nid = i * NAT_ENTRY_PER_BLOCK;
Fan Lif6986ed2017-11-02 11:02:52 +08003077 last_nid = nid + NAT_ENTRY_PER_BLOCK;
Chao Yu7041d5d2017-03-08 20:07:49 +08003078
Chao Yu346fe752017-03-13 20:10:41 +08003079 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08003080 for (; nid < last_nid; nid++)
Chao Yu346fe752017-03-13 20:10:41 +08003081 update_free_nid_bitmap(sbi, nid, true, true);
3082 spin_unlock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08003083 }
3084
3085 for (i = 0; i < nm_i->nat_blocks; i++) {
3086 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
3087 if (i >= nm_i->nat_blocks)
3088 break;
3089
3090 __set_bit_le(i, nm_i->nat_block_bitmap);
3091 }
3092}
3093
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003094static int init_node_manager(struct f2fs_sb_info *sbi)
3095{
3096 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
3097 struct f2fs_nm_info *nm_i = NM_I(sbi);
3098 unsigned char *version_bitmap;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003099 unsigned int nat_segs;
3100 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003101
3102 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
3103
3104 /* segment_count_nat includes pair segment so divide to 2. */
3105 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003106 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
3107 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09003108
Jaegeuk Kimb63da152014-02-17 12:44:20 +09003109 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yu04d47e62016-11-17 20:53:11 +08003110 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
Chao Yu27cae0b2019-08-05 18:27:25 +08003111 F2FS_RESERVED_NODE_NUM;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003112 nm_i->nid_cnt[FREE_NID] = 0;
3113 nm_i->nid_cnt[PREALLOC_NID] = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09003114 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Chao Yuea1a29a02015-10-12 17:08:48 +08003115 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
Chao Yu2304cb02016-01-18 18:32:58 +08003116 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003117
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09003118 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003119 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08003120 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
3121 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003122 INIT_LIST_HEAD(&nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +08003123 spin_lock_init(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003124
3125 mutex_init(&nm_i->build_lock);
Chao Yub8559dc2016-10-12 19:28:29 +08003126 spin_lock_init(&nm_i->nid_list_lock);
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003127 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003128
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003129 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02003130 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003131 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02003132 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
3133 GFP_KERNEL);
3134 if (!nm_i->nat_bitmap)
3135 return -ENOMEM;
Chao Yu599a09b2017-01-07 18:52:01 +08003136
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003137 err = __get_nat_bitmaps(sbi);
3138 if (err)
3139 return err;
3140
Chao Yu599a09b2017-01-07 18:52:01 +08003141#ifdef CONFIG_F2FS_CHECK_FS
3142 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
3143 GFP_KERNEL);
3144 if (!nm_i->nat_bitmap_mir)
3145 return -ENOMEM;
3146#endif
3147
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003148 return 0;
3149}
3150
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08003151static int init_free_nid_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08003152{
3153 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003154 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08003155
Kees Cook026f0502018-06-12 14:28:23 -07003156 nm_i->free_nid_bitmap =
Eric Biggers0b6d4ca2020-06-04 21:57:48 -07003157 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *),
3158 nm_i->nat_blocks),
3159 GFP_KERNEL);
Chao Yu4ac91242017-02-23 10:53:49 +08003160 if (!nm_i->free_nid_bitmap)
3161 return -ENOMEM;
3162
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003163 for (i = 0; i < nm_i->nat_blocks; i++) {
3164 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
Yunlei Hee15d54d2018-06-27 14:46:21 +08003165 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
Yunlei He68c43a22018-07-02 10:40:19 +08003166 if (!nm_i->free_nid_bitmap[i])
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003167 return -ENOMEM;
3168 }
3169
Chao Yu628b3d12017-11-30 19:28:18 +08003170 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
Chao Yu4ac91242017-02-23 10:53:49 +08003171 GFP_KERNEL);
3172 if (!nm_i->nat_block_bitmap)
3173 return -ENOMEM;
Chao Yu586d1492017-03-01 17:09:07 +08003174
Kees Cook9d2a7892018-06-12 14:28:35 -07003175 nm_i->free_nid_count =
3176 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
3177 nm_i->nat_blocks),
3178 GFP_KERNEL);
Chao Yu586d1492017-03-01 17:09:07 +08003179 if (!nm_i->free_nid_count)
3180 return -ENOMEM;
Chao Yu4ac91242017-02-23 10:53:49 +08003181 return 0;
3182}
3183
Chao Yu4d57b862018-05-30 00:20:41 +08003184int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003185{
3186 int err;
3187
Chao Yuacbf0542017-11-30 19:28:17 +08003188 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3189 GFP_KERNEL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003190 if (!sbi->nm_info)
3191 return -ENOMEM;
3192
3193 err = init_node_manager(sbi);
3194 if (err)
3195 return err;
3196
Chao Yu4ac91242017-02-23 10:53:49 +08003197 err = init_free_nid_cache(sbi);
3198 if (err)
3199 return err;
3200
Chao Yu7041d5d2017-03-08 20:07:49 +08003201 /* load free nid status from nat_bits table */
3202 load_free_nid_bitmap(sbi);
3203
Chao Yue2374012018-06-15 14:45:57 +08003204 return f2fs_build_free_nids(sbi, true, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003205}
3206
Chao Yu4d57b862018-05-30 00:20:41 +08003207void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003208{
3209 struct f2fs_nm_info *nm_i = NM_I(sbi);
3210 struct free_nid *i, *next_i;
3211 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003212 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003213 nid_t nid = 0;
3214 unsigned int found;
3215
3216 if (!nm_i)
3217 return;
3218
3219 /* destroy free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08003220 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003221 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Fan Lia0761f62017-10-28 19:03:37 +08003222 __remove_free_nid(sbi, i, FREE_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08003223 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08003224 kmem_cache_free(free_nid_slab, i);
Chao Yub8559dc2016-10-12 19:28:29 +08003225 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003226 }
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003227 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3228 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3229 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
Chao Yub8559dc2016-10-12 19:28:29 +08003230 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003231
3232 /* destroy nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003233 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003234 while ((found = __gang_lookup_nat_cache(nm_i,
3235 nid, NATVEC_SIZE, natvec))) {
3236 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003237
Gu Zhengb6ce3912014-03-07 18:43:24 +08003238 nid = nat_get_nid(natvec[found - 1]) + 1;
Chao Yu22969152018-08-05 23:08:59 +08003239 for (idx = 0; idx < found; idx++) {
3240 spin_lock(&nm_i->nat_list_lock);
3241 list_del(&natvec[idx]->list);
3242 spin_unlock(&nm_i->nat_list_lock);
3243
Gu Zhengb6ce3912014-03-07 18:43:24 +08003244 __del_from_nat_cache(nm_i, natvec[idx]);
Chao Yu22969152018-08-05 23:08:59 +08003245 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003246 }
Jaegeuk Kima95ba662020-11-06 13:22:05 -08003247 f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003248
3249 /* destroy nat set cache */
3250 nid = 0;
3251 while ((found = __gang_lookup_nat_set(nm_i,
3252 nid, SETVEC_SIZE, setvec))) {
3253 unsigned idx;
3254
3255 nid = setvec[found - 1]->set + 1;
3256 for (idx = 0; idx < found; idx++) {
3257 /* entry_cnt is not zero, when cp_error was occurred */
3258 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3259 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3260 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3261 }
3262 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003263 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003264
Chao Yu4ac91242017-02-23 10:53:49 +08003265 kvfree(nm_i->nat_block_bitmap);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003266 if (nm_i->free_nid_bitmap) {
3267 int i;
3268
3269 for (i = 0; i < nm_i->nat_blocks; i++)
3270 kvfree(nm_i->free_nid_bitmap[i]);
Jaegeuk Kim52225952018-12-13 18:38:33 -08003271 kvfree(nm_i->free_nid_bitmap);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003272 }
Chao Yu586d1492017-03-01 17:09:07 +08003273 kvfree(nm_i->free_nid_count);
Chao Yu4ac91242017-02-23 10:53:49 +08003274
Jaegeuk Kim52225952018-12-13 18:38:33 -08003275 kvfree(nm_i->nat_bitmap);
3276 kvfree(nm_i->nat_bits);
Chao Yu599a09b2017-01-07 18:52:01 +08003277#ifdef CONFIG_F2FS_CHECK_FS
Jaegeuk Kim52225952018-12-13 18:38:33 -08003278 kvfree(nm_i->nat_bitmap_mir);
Chao Yu599a09b2017-01-07 18:52:01 +08003279#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003280 sbi->nm_info = NULL;
Chao Yuc8eb7022020-09-14 16:47:00 +08003281 kfree(nm_i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003282}
3283
Chao Yu4d57b862018-05-30 00:20:41 +08003284int __init f2fs_create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003285{
Chao Yu98510002020-02-17 17:46:20 +08003286 nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003287 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003288 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08003289 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003290
Chao Yu98510002020-02-17 17:46:20 +08003291 free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08003292 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08003293 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003294 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08003295
Chao Yu98510002020-02-17 17:46:20 +08003296 nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set",
Chao Yuaec71382014-06-24 09:18:20 +08003297 sizeof(struct nat_entry_set));
3298 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003299 goto destroy_free_nid;
Chao Yu50fa53e2018-08-02 23:03:19 +08003300
Chao Yu98510002020-02-17 17:46:20 +08003301 fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry",
Chao Yu50fa53e2018-08-02 23:03:19 +08003302 sizeof(struct fsync_node_entry));
3303 if (!fsync_node_entry_slab)
3304 goto destroy_nat_entry_set;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003305 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08003306
Chao Yu50fa53e2018-08-02 23:03:19 +08003307destroy_nat_entry_set:
3308 kmem_cache_destroy(nat_entry_set_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003309destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08003310 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003311destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08003312 kmem_cache_destroy(nat_entry_slab);
3313fail:
3314 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003315}
3316
Chao Yu4d57b862018-05-30 00:20:41 +08003317void f2fs_destroy_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003318{
Chao Yu50fa53e2018-08-02 23:03:19 +08003319 kmem_cache_destroy(fsync_node_entry_slab);
Chao Yuaec71382014-06-24 09:18:20 +08003320 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003321 kmem_cache_destroy(free_nid_slab);
3322 kmem_cache_destroy(nat_entry_slab);
3323}