blob: eed7528a65603936894cce04b09191a705ed5316 [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 Tummala52861122021-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 Tummala52861122021-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 Kim317c4f52020-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 Tummala52861122021-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);
Chao Yuec3ea142021-05-20 19:51:50 +0800100 } else if (type == COMPRESS_PAGE) {
101#ifdef CONFIG_F2FS_FS_COMPRESSION
102 unsigned long free_ram = val.freeram;
103
104 /*
105 * free memory is lower than watermark or cached page count
106 * exceed threshold, deny caching compress page.
107 */
108 res = (free_ram > avail_ram * sbi->compress_watermark / 100) &&
109 (COMPRESS_MAPPING(sbi)->nrpages <
110 free_ram * sbi->compress_percent / 100);
111#else
112 res = false;
113#endif
Jaegeuk Kim1e843712014-12-09 06:08:59 -0800114 } else {
Jaegeuk Kim1663cae2016-01-09 16:14:08 -0800115 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
116 return true;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900117 }
118 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +0900119}
120
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900121static void clear_node_page_dirty(struct page *page)
122{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900123 if (PageDirty(page)) {
Matthew Wilcox5ec2d992017-12-04 20:25:25 -0500124 f2fs_clear_page_cache_dirty_tag(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900125 clear_page_dirty_for_io(page);
Chao Yuaec2f722018-05-26 18:03:35 +0800126 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900127 }
128 ClearPageUptodate(page);
129}
130
131static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
132{
Jaegeuk Kim2838d732020-10-25 07:35:47 -0700133 return f2fs_get_meta_page_retry(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900134}
135
136static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
137{
138 struct page *src_page;
139 struct page *dst_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900140 pgoff_t dst_off;
141 void *src_addr;
142 void *dst_addr;
143 struct f2fs_nm_info *nm_i = NM_I(sbi);
144
Chao Yu80551d12018-07-17 20:41:46 +0800145 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900146
147 /* get current nat block page with lock */
Chao Yu80551d12018-07-17 20:41:46 +0800148 src_page = get_current_nat_page(sbi, nid);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -0700149 if (IS_ERR(src_page))
150 return src_page;
Chao Yu4d57b862018-05-30 00:20:41 +0800151 dst_page = f2fs_grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700152 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900153
154 src_addr = page_address(src_page);
155 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300156 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900157 set_page_dirty(dst_page);
158 f2fs_put_page(src_page, 1);
159
160 set_to_next_nat(nm_i, nid);
161
162 return dst_page;
163}
164
Yunlei He12f9ef32017-11-10 13:36:51 -0800165static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail)
166{
167 struct nat_entry *new;
168
169 if (no_fail)
Chao Yu2882d342018-01-25 18:57:25 +0800170 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800171 else
Chao Yu2882d342018-01-25 18:57:25 +0800172 new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800173 if (new) {
174 nat_set_nid(new, nid);
175 nat_reset_flag(new);
176 }
177 return new;
178}
179
180static void __free_nat_entry(struct nat_entry *e)
181{
182 kmem_cache_free(nat_entry_slab, e);
183}
184
185/* must be locked by nat_tree_lock */
186static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
187 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
188{
189 if (no_fail)
190 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
191 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
192 return NULL;
193
194 if (raw_ne)
195 node_info_from_raw_nat(&ne->ni, raw_ne);
Chao Yu22969152018-08-05 23:08:59 +0800196
197 spin_lock(&nm_i->nat_list_lock);
Yunlei He12f9ef32017-11-10 13:36:51 -0800198 list_add_tail(&ne->list, &nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +0800199 spin_unlock(&nm_i->nat_list_lock);
200
Jaegeuk Kim317c4f52020-11-06 13:22:05 -0800201 nm_i->nat_cnt[TOTAL_NAT]++;
202 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
Yunlei He12f9ef32017-11-10 13:36:51 -0800203 return ne;
204}
205
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900206static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
207{
Chao Yu22969152018-08-05 23:08:59 +0800208 struct nat_entry *ne;
209
210 ne = radix_tree_lookup(&nm_i->nat_root, n);
211
212 /* for recent accessed nat entry, move it to tail of lru list */
213 if (ne && !get_nat_flag(ne, IS_DIRTY)) {
214 spin_lock(&nm_i->nat_list_lock);
215 if (!list_empty(&ne->list))
216 list_move_tail(&ne->list, &nm_i->nat_entries);
217 spin_unlock(&nm_i->nat_list_lock);
218 }
219
220 return ne;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900221}
222
223static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
224 nid_t start, unsigned int nr, struct nat_entry **ep)
225{
226 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
227}
228
229static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
230{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900231 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
Jaegeuk Kim317c4f52020-11-06 13:22:05 -0800232 nm_i->nat_cnt[TOTAL_NAT]--;
233 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
Yunlei He12f9ef32017-11-10 13:36:51 -0800234 __free_nat_entry(e);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900235}
236
Chao Yu780de472018-03-20 23:08:30 +0800237static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
238 struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700239{
240 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
241 struct nat_entry_set *head;
242
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700243 head = radix_tree_lookup(&nm_i->nat_set_root, set);
244 if (!head) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700245 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700246
247 INIT_LIST_HEAD(&head->entry_list);
248 INIT_LIST_HEAD(&head->set_list);
249 head->set = set;
250 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800251 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700252 }
Chao Yu780de472018-03-20 23:08:30 +0800253 return head;
254}
255
256static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
257 struct nat_entry *ne)
258{
259 struct nat_entry_set *head;
260 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
261
262 if (!new_ne)
263 head = __grab_nat_entry_set(nm_i, ne);
264
265 /*
266 * update entry_cnt in below condition:
267 * 1. update NEW_ADDR to valid block address;
268 * 2. update old block address to new one;
269 */
270 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
271 !get_nat_flag(ne, IS_DIRTY)))
272 head->entry_cnt++;
273
274 set_nat_flag(ne, IS_PREALLOC, new_ne);
Chao Yufebeca62017-06-05 18:29:08 +0800275
276 if (get_nat_flag(ne, IS_DIRTY))
277 goto refresh_list;
278
Jaegeuk Kim317c4f52020-11-06 13:22:05 -0800279 nm_i->nat_cnt[DIRTY_NAT]++;
280 nm_i->nat_cnt[RECLAIMABLE_NAT]--;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700281 set_nat_flag(ne, IS_DIRTY, true);
Chao Yufebeca62017-06-05 18:29:08 +0800282refresh_list:
Chao Yu22969152018-08-05 23:08:59 +0800283 spin_lock(&nm_i->nat_list_lock);
Chao Yu780de472018-03-20 23:08:30 +0800284 if (new_ne)
Chao Yufebeca62017-06-05 18:29:08 +0800285 list_del_init(&ne->list);
286 else
287 list_move_tail(&ne->list, &head->entry_list);
Chao Yu22969152018-08-05 23:08:59 +0800288 spin_unlock(&nm_i->nat_list_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700289}
290
291static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
Kinglong Mee0b28b712017-02-28 21:34:47 +0800292 struct nat_entry_set *set, struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700293{
Chao Yu22969152018-08-05 23:08:59 +0800294 spin_lock(&nm_i->nat_list_lock);
Kinglong Mee0b28b712017-02-28 21:34:47 +0800295 list_move_tail(&ne->list, &nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +0800296 spin_unlock(&nm_i->nat_list_lock);
297
Kinglong Mee0b28b712017-02-28 21:34:47 +0800298 set_nat_flag(ne, IS_DIRTY, false);
299 set->entry_cnt--;
Jaegeuk Kim317c4f52020-11-06 13:22:05 -0800300 nm_i->nat_cnt[DIRTY_NAT]--;
301 nm_i->nat_cnt[RECLAIMABLE_NAT]++;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700302}
303
304static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
305 nid_t start, unsigned int nr, struct nat_entry_set **ep)
306{
307 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
308 start, nr);
309}
310
Chao Yu50fa53e2018-08-02 23:03:19 +0800311bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
312{
313 return NODE_MAPPING(sbi) == page->mapping &&
314 IS_DNODE(page) && is_cold_node(page);
315}
316
317void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
318{
319 spin_lock_init(&sbi->fsync_node_lock);
320 INIT_LIST_HEAD(&sbi->fsync_node_list);
321 sbi->fsync_seg_id = 0;
322 sbi->fsync_node_num = 0;
323}
324
325static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
326 struct page *page)
327{
328 struct fsync_node_entry *fn;
329 unsigned long flags;
330 unsigned int seq_id;
331
332 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS);
333
334 get_page(page);
335 fn->page = page;
336 INIT_LIST_HEAD(&fn->list);
337
338 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
339 list_add_tail(&fn->list, &sbi->fsync_node_list);
340 fn->seq_id = sbi->fsync_seg_id++;
341 seq_id = fn->seq_id;
342 sbi->fsync_node_num++;
343 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
344
345 return seq_id;
346}
347
348void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
349{
350 struct fsync_node_entry *fn;
351 unsigned long flags;
352
353 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
354 list_for_each_entry(fn, &sbi->fsync_node_list, list) {
355 if (fn->page == page) {
356 list_del(&fn->list);
357 sbi->fsync_node_num--;
358 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
359 kmem_cache_free(fsync_node_entry_slab, fn);
360 put_page(page);
361 return;
362 }
363 }
364 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
365 f2fs_bug_on(sbi, 1);
366}
367
368void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
369{
370 unsigned long flags;
371
372 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
373 sbi->fsync_seg_id = 0;
374 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
375}
376
Chao Yu4d57b862018-05-30 00:20:41 +0800377int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700378{
379 struct f2fs_nm_info *nm_i = NM_I(sbi);
380 struct nat_entry *e;
381 bool need = false;
382
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700383 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700384 e = __lookup_nat_cache(nm_i, nid);
385 if (e) {
386 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
387 !get_nat_flag(e, HAS_FSYNCED_INODE))
388 need = true;
389 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700390 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700391 return need;
392}
393
Chao Yu4d57b862018-05-30 00:20:41 +0800394bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +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 is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900399
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700400 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900401 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700402 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700403 is_cp = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700404 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900405 return is_cp;
406}
407
Chao Yu4d57b862018-05-30 00:20:41 +0800408bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900409{
410 struct f2fs_nm_info *nm_i = NM_I(sbi);
411 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700412 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900413
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700414 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700415 e = __lookup_nat_cache(nm_i, ino);
416 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
417 (get_nat_flag(e, IS_CHECKPOINTED) ||
418 get_nat_flag(e, HAS_FSYNCED_INODE)))
419 need_update = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700420 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700421 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900422}
423
Yunlei He12f9ef32017-11-10 13:36:51 -0800424/* must be locked by nat_tree_lock */
Chao Yu1515aef2016-02-19 18:12:28 +0800425static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900426 struct f2fs_nat_entry *ne)
427{
Chao Yu1515aef2016-02-19 18:12:28 +0800428 struct f2fs_nm_info *nm_i = NM_I(sbi);
Yunlei He12f9ef32017-11-10 13:36:51 -0800429 struct nat_entry *new, *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800430
Yunlei He12f9ef32017-11-10 13:36:51 -0800431 new = __alloc_nat_entry(nid, false);
432 if (!new)
433 return;
434
435 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900436 e = __lookup_nat_cache(nm_i, nid);
Yunlei He12f9ef32017-11-10 13:36:51 -0800437 if (!e)
438 e = __init_nat_entry(nm_i, new, ne, false);
439 else
Eric Biggers0c0b4712016-10-11 10:36:12 -0700440 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
441 nat_get_blkaddr(e) !=
442 le32_to_cpu(ne->block_addr) ||
Chao Yu1515aef2016-02-19 18:12:28 +0800443 nat_get_version(e) != ne->version);
Yunlei He12f9ef32017-11-10 13:36:51 -0800444 up_write(&nm_i->nat_tree_lock);
445 if (e != new)
446 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900447}
448
449static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900450 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900451{
452 struct f2fs_nm_info *nm_i = NM_I(sbi);
453 struct nat_entry *e;
Yunlei He12f9ef32017-11-10 13:36:51 -0800454 struct nat_entry *new = __alloc_nat_entry(ni->nid, true);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800455
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700456 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900457 e = __lookup_nat_cache(nm_i, ni->nid);
458 if (!e) {
Yunlei He12f9ef32017-11-10 13:36:51 -0800459 e = __init_nat_entry(nm_i, new, NULL, true);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800460 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700461 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900462 } else if (new_blkaddr == NEW_ADDR) {
463 /*
464 * when nid is reallocated,
465 * previous nat entry can be remained in nat cache.
466 * So, reinitialize it with new information.
467 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800468 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700469 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900470 }
Yunlei He12f9ef32017-11-10 13:36:51 -0800471 /* let's free early to reduce memory consumption */
472 if (e != new)
473 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900474
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900475 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700476 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
477 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900478 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700479 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900480 new_blkaddr == NEW_ADDR);
Chao Yu93770ab2019-04-15 15:26:32 +0800481 f2fs_bug_on(sbi, __is_valid_data_blkaddr(nat_get_blkaddr(e)) &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900482 new_blkaddr == NEW_ADDR);
483
arter97e1c42042014-08-06 23:22:50 +0900484 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900485 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
486 unsigned char version = nat_get_version(e);
Yi Zhuangf10ea3c2021-04-06 09:47:35 +0800487
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900488 nat_set_version(e, inc_node_version(version));
489 }
490
491 /* change address */
492 nat_set_blkaddr(e, new_blkaddr);
Chao Yu93770ab2019-04-15 15:26:32 +0800493 if (!__is_valid_data_blkaddr(new_blkaddr))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700494 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900495 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900496
497 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800498 if (ni->nid != ni->ino)
499 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700500 if (e) {
501 if (fsync_done && ni->nid == ni->ino)
502 set_nat_flag(e, HAS_FSYNCED_INODE, true);
503 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
504 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700505 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900506}
507
Chao Yu4d57b862018-05-30 00:20:41 +0800508int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900509{
510 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700511 int nr = nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900512
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700513 if (!down_write_trylock(&nm_i->nat_tree_lock))
514 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900515
Chao Yu22969152018-08-05 23:08:59 +0800516 spin_lock(&nm_i->nat_list_lock);
517 while (nr_shrink) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900518 struct nat_entry *ne;
Chao Yu22969152018-08-05 23:08:59 +0800519
520 if (list_empty(&nm_i->nat_entries))
521 break;
522
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900523 ne = list_first_entry(&nm_i->nat_entries,
524 struct nat_entry, list);
Chao Yu22969152018-08-05 23:08:59 +0800525 list_del(&ne->list);
526 spin_unlock(&nm_i->nat_list_lock);
527
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900528 __del_from_nat_cache(nm_i, ne);
529 nr_shrink--;
Chao Yu22969152018-08-05 23:08:59 +0800530
531 spin_lock(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900532 }
Chao Yu22969152018-08-05 23:08:59 +0800533 spin_unlock(&nm_i->nat_list_lock);
534
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700535 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700536 return nr - nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900537}
538
Chao Yu77357302018-07-17 00:02:17 +0800539int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
Chao Yu4d57b862018-05-30 00:20:41 +0800540 struct node_info *ni)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900541{
542 struct f2fs_nm_info *nm_i = NM_I(sbi);
543 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +0800544 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900545 nid_t start_nid = START_NID(nid);
546 struct f2fs_nat_block *nat_blk;
547 struct page *page = NULL;
548 struct f2fs_nat_entry ne;
549 struct nat_entry *e;
Yunlei He66a82d12017-04-22 18:06:26 +0800550 pgoff_t index;
Chao Yu93770ab2019-04-15 15:26:32 +0800551 block_t blkaddr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900552 int i;
553
554 ni->nid = nid;
Jaegeuk Kimfc2d64e2021-07-22 10:30:58 -0700555retry:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900556 /* Check nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700557 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900558 e = __lookup_nat_cache(nm_i, nid);
559 if (e) {
560 ni->ino = nat_get_ino(e);
561 ni->blk_addr = nat_get_blkaddr(e);
562 ni->version = nat_get_version(e);
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700563 up_read(&nm_i->nat_tree_lock);
Chao Yu77357302018-07-17 00:02:17 +0800564 return 0;
Chao Yu1515aef2016-02-19 18:12:28 +0800565 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900566
Jaegeuk Kimfc2d64e2021-07-22 10:30:58 -0700567 /*
568 * Check current segment summary by trying to grab journal_rwsem first.
569 * This sem is on the critical path on the checkpoint requiring the above
570 * nat_tree_lock. Therefore, we should retry, if we failed to grab here
571 * while not bothering checkpoint.
572 */
573 if (!rwsem_is_locked(&sbi->cp_global_sem)) {
574 down_read(&curseg->journal_rwsem);
575 } else if (!down_read_trylock(&curseg->journal_rwsem)) {
576 up_read(&nm_i->nat_tree_lock);
577 goto retry;
578 }
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800579
Chao Yu4d57b862018-05-30 00:20:41 +0800580 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900581 if (i >= 0) {
Chao Yudfc08a12016-02-14 18:50:40 +0800582 ne = nat_in_journal(journal, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900583 node_info_from_raw_nat(ni, &ne);
584 }
Chao Yub7ad7512016-02-19 18:08:46 +0800585 up_read(&curseg->journal_rwsem);
Yunlei He66a82d12017-04-22 18:06:26 +0800586 if (i >= 0) {
587 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900588 goto cache;
Yunlei He66a82d12017-04-22 18:06:26 +0800589 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900590
591 /* Fill node_info from nat page */
Yunlei He66a82d12017-04-22 18:06:26 +0800592 index = current_nat_addr(sbi, nid);
593 up_read(&nm_i->nat_tree_lock);
594
Chao Yu4d57b862018-05-30 00:20:41 +0800595 page = f2fs_get_meta_page(sbi, index);
Chao Yu77357302018-07-17 00:02:17 +0800596 if (IS_ERR(page))
597 return PTR_ERR(page);
598
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900599 nat_blk = (struct f2fs_nat_block *)page_address(page);
600 ne = nat_blk->entries[nid - start_nid];
601 node_info_from_raw_nat(ni, &ne);
602 f2fs_put_page(page, 1);
603cache:
Chao Yu93770ab2019-04-15 15:26:32 +0800604 blkaddr = le32_to_cpu(ne.block_addr);
605 if (__is_valid_data_blkaddr(blkaddr) &&
606 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE))
607 return -EFAULT;
608
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900609 /* cache nat entry */
Chao Yu1515aef2016-02-19 18:12:28 +0800610 cache_nat_entry(sbi, nid, &ne);
Chao Yu77357302018-07-17 00:02:17 +0800611 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900612}
613
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700614/*
615 * readahead MAX_RA_NODE number of node pages.
616 */
Chao Yu4d57b862018-05-30 00:20:41 +0800617static void f2fs_ra_node_pages(struct page *parent, int start, int n)
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700618{
619 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
620 struct blk_plug plug;
621 int i, end;
622 nid_t nid;
623
624 blk_start_plug(&plug);
625
626 /* Then, try readahead for siblings of the desired node */
627 end = start + n;
628 end = min(end, NIDS_PER_BLOCK);
629 for (i = start; i < end; i++) {
630 nid = get_nid(parent, i, false);
Chao Yu4d57b862018-05-30 00:20:41 +0800631 f2fs_ra_node_page(sbi, nid);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700632 }
633
634 blk_finish_plug(&plug);
635}
636
Chao Yu4d57b862018-05-30 00:20:41 +0800637pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
Chao Yu3cf45742016-01-26 15:40:44 +0800638{
639 const long direct_index = ADDRS_PER_INODE(dn->inode);
Chao Yud02a6e62019-03-25 21:08:19 +0800640 const long direct_blks = ADDRS_PER_BLOCK(dn->inode);
641 const long indirect_blks = ADDRS_PER_BLOCK(dn->inode) * NIDS_PER_BLOCK;
642 unsigned int skipped_unit = ADDRS_PER_BLOCK(dn->inode);
Chao Yu3cf45742016-01-26 15:40:44 +0800643 int cur_level = dn->cur_level;
644 int max_level = dn->max_level;
645 pgoff_t base = 0;
646
647 if (!dn->max_level)
648 return pgofs + 1;
649
650 while (max_level-- > cur_level)
651 skipped_unit *= NIDS_PER_BLOCK;
652
653 switch (dn->max_level) {
654 case 3:
655 base += 2 * indirect_blks;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500656 fallthrough;
Chao Yu3cf45742016-01-26 15:40:44 +0800657 case 2:
658 base += 2 * direct_blks;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500659 fallthrough;
Chao Yu3cf45742016-01-26 15:40:44 +0800660 case 1:
661 base += direct_index;
662 break;
663 default:
664 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
665 }
666
667 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
668}
669
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900670/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900671 * The maximum depth is four.
672 * Offset[0] will have raw inode offset.
673 */
Chao Yu81ca7352016-01-26 15:39:35 +0800674static int get_node_path(struct inode *inode, long block,
Jaegeuk Kimde936532013-08-12 21:08:03 +0900675 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900676{
Chao Yu81ca7352016-01-26 15:39:35 +0800677 const long direct_index = ADDRS_PER_INODE(inode);
Chao Yud02a6e62019-03-25 21:08:19 +0800678 const long direct_blks = ADDRS_PER_BLOCK(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900679 const long dptrs_per_blk = NIDS_PER_BLOCK;
Chao Yud02a6e62019-03-25 21:08:19 +0800680 const long indirect_blks = ADDRS_PER_BLOCK(inode) * NIDS_PER_BLOCK;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900681 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
682 int n = 0;
683 int level = 0;
684
685 noffset[0] = 0;
686
687 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900688 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900689 goto got;
690 }
691 block -= direct_index;
692 if (block < direct_blks) {
693 offset[n++] = NODE_DIR1_BLOCK;
694 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900695 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900696 level = 1;
697 goto got;
698 }
699 block -= direct_blks;
700 if (block < direct_blks) {
701 offset[n++] = NODE_DIR2_BLOCK;
702 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900703 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900704 level = 1;
705 goto got;
706 }
707 block -= direct_blks;
708 if (block < indirect_blks) {
709 offset[n++] = NODE_IND1_BLOCK;
710 noffset[n] = 3;
711 offset[n++] = block / direct_blks;
712 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900713 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900714 level = 2;
715 goto got;
716 }
717 block -= indirect_blks;
718 if (block < indirect_blks) {
719 offset[n++] = NODE_IND2_BLOCK;
720 noffset[n] = 4 + dptrs_per_blk;
721 offset[n++] = block / direct_blks;
722 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900723 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900724 level = 2;
725 goto got;
726 }
727 block -= indirect_blks;
728 if (block < dindirect_blks) {
729 offset[n++] = NODE_DIND_BLOCK;
730 noffset[n] = 5 + (dptrs_per_blk * 2);
731 offset[n++] = block / indirect_blks;
732 noffset[n] = 6 + (dptrs_per_blk * 2) +
733 offset[n - 1] * (dptrs_per_blk + 1);
734 offset[n++] = (block / direct_blks) % dptrs_per_blk;
735 noffset[n] = 7 + (dptrs_per_blk * 2) +
736 offset[n - 2] * (dptrs_per_blk + 1) +
737 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900738 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900739 level = 3;
740 goto got;
741 } else {
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700742 return -E2BIG;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900743 }
744got:
745 return level;
746}
747
748/*
749 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800750 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
Chao Yu7a88ddb2020-02-27 19:30:05 +0800751 * f2fs_unlock_op() only if mode is set with ALLOC_NODE.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900752 */
Chao Yu4d57b862018-05-30 00:20:41 +0800753int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900754{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700755 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900756 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800757 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900758 int offset[4];
759 unsigned int noffset[4];
760 nid_t nids[4];
Chao Yu3cf45742016-01-26 15:40:44 +0800761 int level, i = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900762 int err = 0;
763
Chao Yu81ca7352016-01-26 15:39:35 +0800764 level = get_node_path(dn->inode, index, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700765 if (level < 0)
766 return level;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900767
768 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900769 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900770
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900771 if (!npage[0]) {
Chao Yu4d57b862018-05-30 00:20:41 +0800772 npage[0] = f2fs_get_node_page(sbi, nids[0]);
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900773 if (IS_ERR(npage[0]))
774 return PTR_ERR(npage[0]);
775 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800776
777 /* if inline_data is set, should not report any block indices */
778 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800779 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800780 f2fs_put_page(npage[0], 1);
781 goto release_out;
782 }
783
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900784 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900785 if (level != 0)
786 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900787 dn->inode_page = npage[0];
788 dn->inode_page_locked = true;
789
790 /* get indirect or direct nodes */
791 for (i = 1; i <= level; i++) {
792 bool done = false;
793
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900794 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900795 /* alloc new node */
Chao Yu4d57b862018-05-30 00:20:41 +0800796 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900797 err = -ENOSPC;
798 goto release_pages;
799 }
800
801 dn->nid = nids[i];
Chao Yu4d57b862018-05-30 00:20:41 +0800802 npage[i] = f2fs_new_node_page(dn, noffset[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900803 if (IS_ERR(npage[i])) {
Chao Yu4d57b862018-05-30 00:20:41 +0800804 f2fs_alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900805 err = PTR_ERR(npage[i]);
806 goto release_pages;
807 }
808
809 set_nid(parent, offset[i - 1], nids[i], i == 1);
Chao Yu4d57b862018-05-30 00:20:41 +0800810 f2fs_alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900811 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900812 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Chao Yu4d57b862018-05-30 00:20:41 +0800813 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900814 if (IS_ERR(npage[i])) {
815 err = PTR_ERR(npage[i]);
816 goto release_pages;
817 }
818 done = true;
819 }
820 if (i == 1) {
821 dn->inode_page_locked = false;
822 unlock_page(parent);
823 } else {
824 f2fs_put_page(parent, 1);
825 }
826
827 if (!done) {
Chao Yu4d57b862018-05-30 00:20:41 +0800828 npage[i] = f2fs_get_node_page(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900829 if (IS_ERR(npage[i])) {
830 err = PTR_ERR(npage[i]);
831 f2fs_put_page(npage[0], 0);
832 goto release_out;
833 }
834 }
835 if (i < level) {
836 parent = npage[i];
837 nids[i + 1] = get_nid(parent, offset[i], false);
838 }
839 }
840 dn->nid = nids[level];
841 dn->ofs_in_node = offset[level];
842 dn->node_page = npage[level];
Chao Yua2ced1c2020-02-14 17:44:10 +0800843 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844 return 0;
845
846release_pages:
847 f2fs_put_page(parent, 1);
848 if (i > 1)
849 f2fs_put_page(npage[0], 0);
850release_out:
851 dn->inode_page = NULL;
852 dn->node_page = NULL;
Chao Yu3cf45742016-01-26 15:40:44 +0800853 if (err == -ENOENT) {
854 dn->cur_level = i;
855 dn->max_level = level;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700856 dn->ofs_in_node = offset[level];
Chao Yu3cf45742016-01-26 15:40:44 +0800857 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900858 return err;
859}
860
Chao Yu77357302018-07-17 00:02:17 +0800861static int truncate_node(struct dnode_of_data *dn)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900862{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700863 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900864 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800865 int err;
Pan Bian0ea295d2018-11-22 18:58:46 +0800866 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900867
Chao Yu77357302018-07-17 00:02:17 +0800868 err = f2fs_get_node_info(sbi, dn->nid, &ni);
869 if (err)
870 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900871
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900872 /* Deallocate node address */
Chao Yu4d57b862018-05-30 00:20:41 +0800873 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +0800874 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900875 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900876
877 if (dn->nid == dn->inode->i_ino) {
Chao Yu4d57b862018-05-30 00:20:41 +0800878 f2fs_remove_orphan_inode(sbi, dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900879 dec_valid_inode_count(sbi);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700880 f2fs_inode_synced(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900881 }
Chao Yu000519f2017-07-06 01:11:31 +0800882
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900883 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800884 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900885
Pan Bian0ea295d2018-11-22 18:58:46 +0800886 index = dn->node_page->index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900887 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900888
889 invalidate_mapping_pages(NODE_MAPPING(sbi),
Pan Bian0ea295d2018-11-22 18:58:46 +0800890 index, index);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900891
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900892 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900893 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Chao Yu77357302018-07-17 00:02:17 +0800894
895 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900896}
897
898static int truncate_dnode(struct dnode_of_data *dn)
899{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900900 struct page *page;
Chao Yu77357302018-07-17 00:02:17 +0800901 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900902
903 if (dn->nid == 0)
904 return 1;
905
906 /* get direct node */
Chao Yu4d57b862018-05-30 00:20:41 +0800907 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Masahiro Yamada45586c72020-02-03 17:37:45 -0800908 if (PTR_ERR(page) == -ENOENT)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900909 return 1;
910 else if (IS_ERR(page))
911 return PTR_ERR(page);
912
913 /* Make dnode_of_data for parameter */
914 dn->node_page = page;
915 dn->ofs_in_node = 0;
Chao Yu4d57b862018-05-30 00:20:41 +0800916 f2fs_truncate_data_blocks(dn);
Chao Yu77357302018-07-17 00:02:17 +0800917 err = truncate_node(dn);
918 if (err)
919 return err;
920
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900921 return 1;
922}
923
924static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
925 int ofs, int depth)
926{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900927 struct dnode_of_data rdn = *dn;
928 struct page *page;
929 struct f2fs_node *rn;
930 nid_t child_nid;
931 unsigned int child_nofs;
932 int freed = 0;
933 int i, ret;
934
935 if (dn->nid == 0)
936 return NIDS_PER_BLOCK + 1;
937
Namjae Jeon51dd6242013-04-20 01:28:52 +0900938 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
939
Chao Yu4d57b862018-05-30 00:20:41 +0800940 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900941 if (IS_ERR(page)) {
942 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900943 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900944 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900945
Chao Yu4d57b862018-05-30 00:20:41 +0800946 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700947
Gu Zheng45590712013-07-15 17:57:38 +0800948 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900949 if (depth < 3) {
950 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
951 child_nid = le32_to_cpu(rn->in.nid[i]);
952 if (child_nid == 0)
953 continue;
954 rdn.nid = child_nid;
955 ret = truncate_dnode(&rdn);
956 if (ret < 0)
957 goto out_err;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800958 if (set_nid(page, i, 0, false))
959 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900960 }
961 } else {
962 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
963 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
964 child_nid = le32_to_cpu(rn->in.nid[i]);
965 if (child_nid == 0) {
966 child_nofs += NIDS_PER_BLOCK + 1;
967 continue;
968 }
969 rdn.nid = child_nid;
970 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
971 if (ret == (NIDS_PER_BLOCK + 1)) {
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800972 if (set_nid(page, i, 0, false))
973 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900974 child_nofs += ret;
975 } else if (ret < 0 && ret != -ENOENT) {
976 goto out_err;
977 }
978 }
979 freed = child_nofs;
980 }
981
982 if (!ofs) {
983 /* remove current indirect node */
984 dn->node_page = page;
Chao Yu77357302018-07-17 00:02:17 +0800985 ret = truncate_node(dn);
986 if (ret)
987 goto out_err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900988 freed++;
989 } else {
990 f2fs_put_page(page, 1);
991 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900992 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900993 return freed;
994
995out_err:
996 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900997 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900998 return ret;
999}
1000
1001static int truncate_partial_nodes(struct dnode_of_data *dn,
1002 struct f2fs_inode *ri, int *offset, int depth)
1003{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001004 struct page *pages[2];
1005 nid_t nid[3];
1006 nid_t child_nid;
1007 int err = 0;
1008 int i;
1009 int idx = depth - 2;
1010
1011 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
1012 if (!nid[0])
1013 return 0;
1014
1015 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +08001016 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +09001017 /* reference count'll be increased */
Chao Yu4d57b862018-05-30 00:20:41 +08001018 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001019 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001020 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +08001021 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001022 goto fail;
1023 }
1024 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
1025 }
1026
Chao Yu4d57b862018-05-30 00:20:41 +08001027 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -07001028
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001029 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +08001030 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001031 child_nid = get_nid(pages[idx], i, false);
1032 if (!child_nid)
1033 continue;
1034 dn->nid = child_nid;
1035 err = truncate_dnode(dn);
1036 if (err < 0)
1037 goto fail;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001038 if (set_nid(pages[idx], i, 0, false))
1039 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001040 }
1041
shifei10.gea225dca2013-10-29 15:32:34 +08001042 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001043 dn->node_page = pages[idx];
1044 dn->nid = nid[idx];
Chao Yu77357302018-07-17 00:02:17 +08001045 err = truncate_node(dn);
1046 if (err)
1047 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001048 } else {
1049 f2fs_put_page(pages[idx], 1);
1050 }
1051 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +08001052 offset[idx + 1] = 0;
1053 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001054fail:
shifei10.gea225dca2013-10-29 15:32:34 +08001055 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001056 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001057
1058 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1059
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001060 return err;
1061}
1062
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001063/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001064 * All the block addresses of data and nodes should be nullified.
1065 */
Chao Yu4d57b862018-05-30 00:20:41 +08001066int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001067{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001068 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001069 int err = 0, cont = 1;
1070 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +09001071 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001072 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001073 struct dnode_of_data dn;
1074 struct page *page;
1075
Namjae Jeon51dd6242013-04-20 01:28:52 +09001076 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1077
Chao Yu81ca7352016-01-26 15:39:35 +08001078 level = get_node_path(inode, from, offset, noffset);
Yubo Feng9039d832020-06-20 16:39:43 +08001079 if (level < 0) {
1080 trace_f2fs_truncate_inode_blocks_exit(inode, level);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -07001081 return level;
Yubo Feng9039d832020-06-20 16:39:43 +08001082 }
Jaegeuk Kimff373552016-03-29 16:13:45 -07001083
Chao Yu4d57b862018-05-30 00:20:41 +08001084 page = f2fs_get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001085 if (IS_ERR(page)) {
1086 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001087 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001088 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001089
1090 set_new_dnode(&dn, inode, page, NULL, 0);
1091 unlock_page(page);
1092
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001093 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001094 switch (level) {
1095 case 0:
1096 case 1:
1097 nofs = noffset[1];
1098 break;
1099 case 2:
1100 nofs = noffset[1];
1101 if (!offset[level - 1])
1102 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001103 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001104 if (err < 0 && err != -ENOENT)
1105 goto fail;
1106 nofs += 1 + NIDS_PER_BLOCK;
1107 break;
1108 case 3:
1109 nofs = 5 + 2 * NIDS_PER_BLOCK;
1110 if (!offset[level - 1])
1111 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001112 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001113 if (err < 0 && err != -ENOENT)
1114 goto fail;
1115 break;
1116 default:
1117 BUG();
1118 }
1119
1120skip_partial:
1121 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001122 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001123 switch (offset[0]) {
1124 case NODE_DIR1_BLOCK:
1125 case NODE_DIR2_BLOCK:
1126 err = truncate_dnode(&dn);
1127 break;
1128
1129 case NODE_IND1_BLOCK:
1130 case NODE_IND2_BLOCK:
1131 err = truncate_nodes(&dn, nofs, offset[1], 2);
1132 break;
1133
1134 case NODE_DIND_BLOCK:
1135 err = truncate_nodes(&dn, nofs, offset[1], 3);
1136 cont = 0;
1137 break;
1138
1139 default:
1140 BUG();
1141 }
1142 if (err < 0 && err != -ENOENT)
1143 goto fail;
1144 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001145 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001146 lock_page(page);
Jaegeuk Kimff373552016-03-29 16:13:45 -07001147 BUG_ON(page->mapping != NODE_MAPPING(sbi));
Chao Yubae0ee72018-12-25 17:43:42 +08001148 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001149 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001150 set_page_dirty(page);
1151 unlock_page(page);
1152 }
1153 offset[1] = 0;
1154 offset[0]++;
1155 nofs += err;
1156 }
1157fail:
1158 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001159 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001160 return err > 0 ? 0 : err;
1161}
1162
Jaegeuk Kim9c77f752017-10-19 11:48:57 -07001163/* caller must lock inode page */
Chao Yu4d57b862018-05-30 00:20:41 +08001164int f2fs_truncate_xattr_node(struct inode *inode)
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001165{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001166 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001167 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1168 struct dnode_of_data dn;
1169 struct page *npage;
Chao Yu77357302018-07-17 00:02:17 +08001170 int err;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001171
1172 if (!nid)
1173 return 0;
1174
Chao Yu4d57b862018-05-30 00:20:41 +08001175 npage = f2fs_get_node_page(sbi, nid);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001176 if (IS_ERR(npage))
1177 return PTR_ERR(npage);
1178
Chao Yu77357302018-07-17 00:02:17 +08001179 set_new_dnode(&dn, inode, NULL, npage, nid);
1180 err = truncate_node(&dn);
1181 if (err) {
1182 f2fs_put_page(npage, 1);
1183 return err;
1184 }
1185
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001186 f2fs_i_xnid_write(inode, 0);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001187
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001188 return 0;
1189}
1190
Jaegeuk Kim39936832012-11-22 16:21:29 +09001191/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001192 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1193 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +09001194 */
Chao Yu4d57b862018-05-30 00:20:41 +08001195int f2fs_remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001196{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001197 struct dnode_of_data dn;
Chao Yu13ec7292015-08-24 17:40:45 +08001198 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001199
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001200 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Chao Yu4d57b862018-05-30 00:20:41 +08001201 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
Chao Yu13ec7292015-08-24 17:40:45 +08001202 if (err)
1203 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001204
Chao Yu4d57b862018-05-30 00:20:41 +08001205 err = f2fs_truncate_xattr_node(inode);
Chao Yu13ec7292015-08-24 17:40:45 +08001206 if (err) {
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001207 f2fs_put_dnode(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +08001208 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001209 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001210
1211 /* remove potential inline_data blocks */
1212 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1213 S_ISLNK(inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +08001214 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001215
arter97e1c42042014-08-06 23:22:50 +09001216 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim8d714f82018-07-31 09:09:01 -07001217 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1218 f2fs_put_dnode(&dn);
1219 return -EIO;
1220 }
Chao Yu8b6810f2019-04-15 15:28:34 +08001221
1222 if (unlikely(inode->i_blocks != 0 && inode->i_blocks != 8)) {
Chao Yu097a7682020-02-24 19:20:19 +08001223 f2fs_warn(F2FS_I_SB(inode),
1224 "f2fs_remove_inode_page: inconsistent i_blocks, ino:%lu, iblocks:%llu",
1225 inode->i_ino, (unsigned long long)inode->i_blocks);
Chao Yu8b6810f2019-04-15 15:28:34 +08001226 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
1227 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001228
1229 /* will put inode & node pages */
Chao Yu77357302018-07-17 00:02:17 +08001230 err = truncate_node(&dn);
1231 if (err) {
1232 f2fs_put_dnode(&dn);
1233 return err;
1234 }
Chao Yu13ec7292015-08-24 17:40:45 +08001235 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001236}
1237
Chao Yu4d57b862018-05-30 00:20:41 +08001238struct page *f2fs_new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001239{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001240 struct dnode_of_data dn;
1241
1242 /* allocate inode page for new inode */
1243 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +09001244
1245 /* caller should f2fs_put_page(page, 1); */
Chao Yu4d57b862018-05-30 00:20:41 +08001246 return f2fs_new_node_page(&dn, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001247}
1248
Chao Yu4d57b862018-05-30 00:20:41 +08001249struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001250{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001251 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001252 struct node_info new_ni;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001253 struct page *page;
1254 int err;
1255
Jaegeuk Kim91942322016-05-20 10:13:22 -07001256 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001257 return ERR_PTR(-EPERM);
1258
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001259 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001260 if (!page)
1261 return ERR_PTR(-ENOMEM);
1262
Chao Yu0abd6752017-07-09 00:13:07 +08001263 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001264 goto fail;
Chao Yu0abd6752017-07-09 00:13:07 +08001265
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001266#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu77357302018-07-17 00:02:17 +08001267 err = f2fs_get_node_info(sbi, dn->nid, &new_ni);
1268 if (err) {
1269 dec_valid_node_count(sbi, dn->inode, !ofs);
1270 goto fail;
1271 }
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001272 f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
1273#endif
1274 new_ni.nid = dn->nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001275 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001276 new_ni.blk_addr = NULL_ADDR;
1277 new_ni.flag = 0;
1278 new_ni.version = 0;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001279 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001280
Chao Yubae0ee72018-12-25 17:43:42 +08001281 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001282 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Chao Yuc5667572018-03-09 14:24:22 +08001283 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001284 if (!PageUptodate(page))
1285 SetPageUptodate(page);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001286 if (set_page_dirty(page))
1287 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001288
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001289 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001290 f2fs_i_xnid_write(dn->inode, dn->nid);
Jaegeuk Kim479bd732013-08-12 16:04:53 +09001291
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001292 if (ofs == 0)
1293 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001294 return page;
1295
1296fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +09001297 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001298 f2fs_put_page(page, 1);
1299 return ERR_PTR(err);
1300}
1301
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001302/*
1303 * Caller should do after getting the following values.
1304 * 0: f2fs_put_page(page, 0)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001305 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001306 */
Mike Christie04d328d2016-06-05 14:31:55 -05001307static int read_node_page(struct page *page, int op_flags)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001308{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001309 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001310 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001311 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001312 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001313 .type = NODE,
Mike Christie04d328d2016-06-05 14:31:55 -05001314 .op = REQ_OP_READ,
1315 .op_flags = op_flags,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001316 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001317 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001318 };
Chao Yu77357302018-07-17 00:02:17 +08001319 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001320
Weichao Guo54c55c42018-03-09 23:10:21 +08001321 if (PageUptodate(page)) {
Chao Yub42b1792019-04-15 15:28:35 +08001322 if (!f2fs_inode_chksum_verify(sbi, page)) {
1323 ClearPageUptodate(page);
Chao Yu10f966b2019-06-20 11:36:14 +08001324 return -EFSBADCRC;
Chao Yub42b1792019-04-15 15:28:35 +08001325 }
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001326 return LOCKED_PAGE;
Weichao Guo54c55c42018-03-09 23:10:21 +08001327 }
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001328
Chao Yu77357302018-07-17 00:02:17 +08001329 err = f2fs_get_node_info(sbi, page->index, &ni);
1330 if (err)
1331 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001332
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001333 if (unlikely(ni.blk_addr == NULL_ADDR) ||
1334 is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001335 ClearPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001336 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001337 }
1338
Chao Yu7a9d7542016-02-22 18:36:38 +08001339 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
Chao Yu8b83ac82020-04-16 18:16:56 +08001340
1341 err = f2fs_submit_page_bio(&fio);
1342
1343 if (!err)
1344 f2fs_update_iostat(sbi, FS_NODE_READ_IO, F2FS_BLKSIZE);
1345
1346 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001347}
1348
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001349/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001350 * Readahead a node page
1351 */
Chao Yu4d57b862018-05-30 00:20:41 +08001352void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001353{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001354 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001355 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001356
Chao Yue8458722016-01-08 20:13:37 +08001357 if (!nid)
1358 return;
Chao Yu4d57b862018-05-30 00:20:41 +08001359 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001360 return;
Chao Yue8458722016-01-08 20:13:37 +08001361
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05001362 apage = xa_load(&NODE_MAPPING(sbi)->i_pages, nid);
Fan Li999270d2016-02-29 14:29:51 +08001363 if (apage)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001364 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001365
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001366 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001367 if (!apage)
1368 return;
1369
Christoph Hellwig70246282016-07-19 11:28:41 +02001370 err = read_node_page(apage, REQ_RAHEAD);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001371 f2fs_put_page(apage, err ? 1 : 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001372}
1373
Jaegeuk Kim17a0ee52016-03-08 09:04:35 -08001374static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
Chao Yu0e022ea2016-01-05 16:52:46 +08001375 struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001376{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001377 struct page *page;
1378 int err;
Jaegeuk Kim4aa69d52015-12-23 14:17:47 -08001379
1380 if (!nid)
1381 return ERR_PTR(-ENOENT);
Chao Yu4d57b862018-05-30 00:20:41 +08001382 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001383 return ERR_PTR(-EINVAL);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001384repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001385 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386 if (!page)
1387 return ERR_PTR(-ENOMEM);
1388
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001389 err = read_node_page(page, 0);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001390 if (err < 0) {
1391 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001392 return ERR_PTR(err);
Chao Yue1c51b9f2015-12-11 16:08:22 +08001393 } else if (err == LOCKED_PAGE) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001394 err = 0;
Chao Yue1c51b9f2015-12-11 16:08:22 +08001395 goto page_hit;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001396 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001397
Chao Yu0e022ea2016-01-05 16:52:46 +08001398 if (parent)
Chao Yu4d57b862018-05-30 00:20:41 +08001399 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
Chao Yu0e022ea2016-01-05 16:52:46 +08001400
Chao Yue1c51b9f2015-12-11 16:08:22 +08001401 lock_page(page);
1402
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001403 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001404 f2fs_put_page(page, 1);
1405 goto repeat;
1406 }
Chao Yu1563ac72016-07-03 22:05:12 +08001407
Chao Yu1f258ec2017-06-07 11:17:35 +08001408 if (unlikely(!PageUptodate(page))) {
1409 err = -EIO;
Chao Yu1563ac72016-07-03 22:05:12 +08001410 goto out_err;
Chao Yu1f258ec2017-06-07 11:17:35 +08001411 }
Chao Yu704956e2017-07-31 20:19:09 +08001412
1413 if (!f2fs_inode_chksum_verify(sbi, page)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001414 err = -EFSBADCRC;
Chao Yu704956e2017-07-31 20:19:09 +08001415 goto out_err;
1416 }
Chao Yue1c51b9f2015-12-11 16:08:22 +08001417page_hit:
Yi Zhuangf10ea3c2021-04-06 09:47:35 +08001418 if (unlikely(nid != nid_of_node(page))) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001419 f2fs_warn(sbi, "inconsistent node block, nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1420 nid, nid_of_node(page), ino_of_node(page),
1421 ofs_of_node(page), cpver_of_node(page),
1422 next_blkaddr_of_node(page));
Weichao Guo22423c92021-09-18 20:46:36 +08001423 set_sbi_flag(sbi, SBI_NEED_FSCK);
Chao Yu1f258ec2017-06-07 11:17:35 +08001424 err = -EINVAL;
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001425out_err:
Jaegeuk Kimee605232017-08-31 16:54:51 -07001426 ClearPageUptodate(page);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001427 f2fs_put_page(page, 1);
Chao Yu1f258ec2017-06-07 11:17:35 +08001428 return ERR_PTR(err);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001429 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001430 return page;
1431}
1432
Chao Yu4d57b862018-05-30 00:20:41 +08001433struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
Chao Yu0e022ea2016-01-05 16:52:46 +08001434{
1435 return __get_node_page(sbi, nid, NULL, 0);
1436}
1437
Chao Yu4d57b862018-05-30 00:20:41 +08001438struct page *f2fs_get_node_page_ra(struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001439{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001440 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Chao Yu0e022ea2016-01-05 16:52:46 +08001441 nid_t nid = get_nid(parent, start, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001442
Chao Yu0e022ea2016-01-05 16:52:46 +08001443 return __get_node_page(sbi, nid, parent, start);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001444}
1445
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001446static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1447{
1448 struct inode *inode;
1449 struct page *page;
Chao Yu0f3311a2016-05-21 00:11:09 +08001450 int ret;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001451
1452 /* should flush inline_data before evict_inode */
1453 inode = ilookup(sbi->sb, ino);
1454 if (!inode)
1455 return;
1456
Chao Yu01eccef2017-10-28 16:52:30 +08001457 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1458 FGP_LOCK|FGP_NOWAIT, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001459 if (!page)
1460 goto iput_out;
1461
1462 if (!PageUptodate(page))
1463 goto page_out;
1464
1465 if (!PageDirty(page))
1466 goto page_out;
1467
1468 if (!clear_page_dirty_for_io(page))
1469 goto page_out;
1470
Chao Yu0f3311a2016-05-21 00:11:09 +08001471 ret = f2fs_write_inline_data(inode, page);
1472 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08001473 f2fs_remove_dirty_inode(inode);
Chao Yu0f3311a2016-05-21 00:11:09 +08001474 if (ret)
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001475 set_page_dirty(page);
1476page_out:
Jaegeuk Kim4a6de502016-03-30 11:25:31 -07001477 f2fs_put_page(page, 1);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001478iput_out:
1479 iput(inode);
1480}
1481
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001482static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001483{
Jan Kara028a63a2017-11-15 17:34:51 -08001484 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001485 struct pagevec pvec;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001486 struct page *last_page = NULL;
Jan Kara028a63a2017-11-15 17:34:51 -08001487 int nr_pages;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001488
Mel Gorman86679822017-11-15 17:37:52 -08001489 pagevec_init(&pvec);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001490 index = 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001491
Jan Kara028a63a2017-11-15 17:34:51 -08001492 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001493 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001494 int i;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001495
1496 for (i = 0; i < nr_pages; i++) {
1497 struct page *page = pvec.pages[i];
1498
1499 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001500 f2fs_put_page(last_page, 0);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001501 pagevec_release(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001502 return ERR_PTR(-EIO);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001503 }
1504
1505 if (!IS_DNODE(page) || !is_cold_node(page))
1506 continue;
1507 if (ino_of_node(page) != ino)
1508 continue;
1509
1510 lock_page(page);
1511
1512 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1513continue_unlock:
1514 unlock_page(page);
1515 continue;
1516 }
1517 if (ino_of_node(page) != ino)
1518 goto continue_unlock;
1519
1520 if (!PageDirty(page)) {
1521 /* someone wrote it for us */
1522 goto continue_unlock;
1523 }
1524
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001525 if (last_page)
1526 f2fs_put_page(last_page, 0);
1527
1528 get_page(page);
1529 last_page = page;
1530 unlock_page(page);
1531 }
1532 pagevec_release(&pvec);
1533 cond_resched();
1534 }
1535 return last_page;
1536}
1537
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001538static int __write_node_page(struct page *page, bool atomic, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001539 struct writeback_control *wbc, bool do_balance,
Chao Yu50fa53e2018-08-02 23:03:19 +08001540 enum iostat_type io_type, unsigned int *seq_id)
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001541{
1542 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1543 nid_t nid;
1544 struct node_info ni;
1545 struct f2fs_io_info fio = {
1546 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001547 .ino = ino_of_node(page),
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001548 .type = NODE,
1549 .op = REQ_OP_WRITE,
1550 .op_flags = wbc_to_write_flags(wbc),
1551 .page = page,
1552 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001553 .submitted = false,
Chao Yub0af6d42017-08-02 23:21:48 +08001554 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001555 .io_wbc = wbc,
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001556 };
Chao Yu50fa53e2018-08-02 23:03:19 +08001557 unsigned int seq;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001558
1559 trace_f2fs_writepage(page, NODE);
1560
Jaegeuk Kim6d7c8652020-05-18 18:00:33 -07001561 if (unlikely(f2fs_cp_error(sbi))) {
1562 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
1563 ClearPageUptodate(page);
1564 dec_page_count(sbi, F2FS_DIRTY_NODES);
1565 unlock_page(page);
1566 return 0;
1567 }
Jaegeuk Kim868de612018-05-04 18:04:22 -07001568 goto redirty_out;
Jaegeuk Kim6d7c8652020-05-18 18:00:33 -07001569 }
Chao Yudb198ae2018-01-18 17:29:10 +08001570
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001571 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1572 goto redirty_out;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001573
Jaegeuk Kim100c0652019-08-28 19:58:26 -07001574 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1575 wbc->sync_mode == WB_SYNC_NONE &&
Chao Yufd8c8ca2018-07-25 19:16:21 +08001576 IS_DNODE(page) && is_cold_node(page))
1577 goto redirty_out;
1578
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001579 /* get old block addr of this node page */
1580 nid = nid_of_node(page);
1581 f2fs_bug_on(sbi, page->index != nid);
1582
Chao Yu77357302018-07-17 00:02:17 +08001583 if (f2fs_get_node_info(sbi, nid, &ni))
1584 goto redirty_out;
1585
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001586 if (wbc->for_reclaim) {
1587 if (!down_read_trylock(&sbi->node_write))
1588 goto redirty_out;
1589 } else {
1590 down_read(&sbi->node_write);
1591 }
1592
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001593 /* This page is already truncated */
1594 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1595 ClearPageUptodate(page);
1596 dec_page_count(sbi, F2FS_DIRTY_NODES);
1597 up_read(&sbi->node_write);
1598 unlock_page(page);
1599 return 0;
1600 }
1601
Chao Yuc9b60782018-08-01 19:13:44 +08001602 if (__is_valid_data_blkaddr(ni.blk_addr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001603 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
1604 DATA_GENERIC_ENHANCE)) {
Jaegeuk Kim89d13c32018-09-27 22:15:31 -07001605 up_read(&sbi->node_write);
Chao Yuc9b60782018-08-01 19:13:44 +08001606 goto redirty_out;
Jaegeuk Kim89d13c32018-09-27 22:15:31 -07001607 }
Chao Yuc9b60782018-08-01 19:13:44 +08001608
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001609 if (atomic && !test_opt(sbi, NOBARRIER))
1610 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1611
Chao Yudc5a9412020-02-14 17:45:12 +08001612 /* should add to global list before clearing PAGECACHE status */
Chao Yu50fa53e2018-08-02 23:03:19 +08001613 if (f2fs_in_warm_node_list(sbi, page)) {
1614 seq = f2fs_add_fsync_node_entry(sbi, page);
1615 if (seq_id)
1616 *seq_id = seq;
1617 }
1618
Chao Yudc5a9412020-02-14 17:45:12 +08001619 set_page_writeback(page);
1620 ClearPageError(page);
1621
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001622 fio.old_blkaddr = ni.blk_addr;
Chao Yu4d57b862018-05-30 00:20:41 +08001623 f2fs_do_write_node_page(nid, &fio);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001624 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1625 dec_page_count(sbi, F2FS_DIRTY_NODES);
1626 up_read(&sbi->node_write);
1627
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001628 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08001629 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001630 submitted = NULL;
1631 }
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001632
1633 unlock_page(page);
1634
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001635 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001636 f2fs_submit_merged_write(sbi, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001637 submitted = NULL;
1638 }
1639 if (submitted)
1640 *submitted = fio.submitted;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001641
Yunlong Song401db792017-07-27 20:11:00 +08001642 if (do_balance)
1643 f2fs_balance_fs(sbi, false);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001644 return 0;
1645
1646redirty_out:
1647 redirty_page_for_writepage(wbc, page);
1648 return AOP_WRITEPAGE_ACTIVATE;
1649}
1650
Chao Yu48018b42018-09-13 07:40:53 +08001651int f2fs_move_node_page(struct page *node_page, int gc_type)
Yunlei Hef15194f2017-10-30 14:18:55 +08001652{
Chao Yu48018b42018-09-13 07:40:53 +08001653 int err = 0;
1654
Yunlei Hef15194f2017-10-30 14:18:55 +08001655 if (gc_type == FG_GC) {
1656 struct writeback_control wbc = {
1657 .sync_mode = WB_SYNC_ALL,
1658 .nr_to_write = 1,
1659 .for_reclaim = 0,
1660 };
1661
Chao Yubae0ee72018-12-25 17:43:42 +08001662 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
Chao Yu8d64d362018-12-12 18:12:30 +08001663
1664 set_page_dirty(node_page);
1665
Chao Yu48018b42018-09-13 07:40:53 +08001666 if (!clear_page_dirty_for_io(node_page)) {
1667 err = -EAGAIN;
Yunlei Hef15194f2017-10-30 14:18:55 +08001668 goto out_page;
Chao Yu48018b42018-09-13 07:40:53 +08001669 }
Yunlei Hef15194f2017-10-30 14:18:55 +08001670
1671 if (__write_node_page(node_page, false, NULL,
Chao Yu48018b42018-09-13 07:40:53 +08001672 &wbc, false, FS_GC_NODE_IO, NULL)) {
1673 err = -EAGAIN;
Yunlei Hef15194f2017-10-30 14:18:55 +08001674 unlock_page(node_page);
Chao Yu48018b42018-09-13 07:40:53 +08001675 }
Yunlei Hef15194f2017-10-30 14:18:55 +08001676 goto release_page;
1677 } else {
1678 /* set page dirty and write it */
1679 if (!PageWriteback(node_page))
1680 set_page_dirty(node_page);
1681 }
1682out_page:
1683 unlock_page(node_page);
1684release_page:
1685 f2fs_put_page(node_page, 0);
Chao Yu48018b42018-09-13 07:40:53 +08001686 return err;
Yunlei Hef15194f2017-10-30 14:18:55 +08001687}
1688
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001689static int f2fs_write_node_page(struct page *page,
1690 struct writeback_control *wbc)
1691{
Chao Yu50fa53e2018-08-02 23:03:19 +08001692 return __write_node_page(page, false, NULL, wbc, false,
1693 FS_NODE_IO, NULL);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001694}
1695
Chao Yu4d57b862018-05-30 00:20:41 +08001696int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
Chao Yu50fa53e2018-08-02 23:03:19 +08001697 struct writeback_control *wbc, bool atomic,
1698 unsigned int *seq_id)
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001699{
Jan Kara028a63a2017-11-15 17:34:51 -08001700 pgoff_t index;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001701 struct pagevec pvec;
1702 int ret = 0;
1703 struct page *last_page = NULL;
1704 bool marked = false;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001705 nid_t ino = inode->i_ino;
Jan Kara028a63a2017-11-15 17:34:51 -08001706 int nr_pages;
Chao Yubab475c2018-09-27 23:41:16 +08001707 int nwritten = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001708
1709 if (atomic) {
1710 last_page = last_fsync_dnode(sbi, ino);
1711 if (IS_ERR_OR_NULL(last_page))
1712 return PTR_ERR_OR_ZERO(last_page);
1713 }
1714retry:
Mel Gorman86679822017-11-15 17:37:52 -08001715 pagevec_init(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001716 index = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001717
Jan Kara028a63a2017-11-15 17:34:51 -08001718 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001719 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001720 int i;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001721
1722 for (i = 0; i < nr_pages; i++) {
1723 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001724 bool submitted = false;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001725
1726 if (unlikely(f2fs_cp_error(sbi))) {
1727 f2fs_put_page(last_page, 0);
1728 pagevec_release(&pvec);
Chao Yu9de69272016-10-11 22:57:06 +08001729 ret = -EIO;
1730 goto out;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001731 }
1732
1733 if (!IS_DNODE(page) || !is_cold_node(page))
1734 continue;
1735 if (ino_of_node(page) != ino)
1736 continue;
1737
1738 lock_page(page);
1739
1740 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1741continue_unlock:
1742 unlock_page(page);
1743 continue;
1744 }
1745 if (ino_of_node(page) != ino)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001746 goto continue_unlock;
1747
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001748 if (!PageDirty(page) && page != last_page) {
1749 /* someone wrote it for us */
1750 goto continue_unlock;
1751 }
1752
Chao Yubae0ee72018-12-25 17:43:42 +08001753 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001754
Jaegeuk Kimd29fd172017-04-12 12:02:00 -07001755 set_fsync_mark(page, 0);
1756 set_dentry_mark(page, 0);
1757
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001758 if (!atomic || page == last_page) {
1759 set_fsync_mark(page, 1);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001760 if (IS_INODE(page)) {
1761 if (is_inode_flag_set(inode,
1762 FI_DIRTY_INODE))
Chao Yu4d57b862018-05-30 00:20:41 +08001763 f2fs_update_inode(inode, page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001764 set_dentry_mark(page,
Chao Yu4d57b862018-05-30 00:20:41 +08001765 f2fs_need_dentry_mark(sbi, ino));
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001766 }
Jack Qiua87aff12020-07-24 16:55:28 +08001767 /* may be written by other thread */
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001768 if (!PageDirty(page))
1769 set_page_dirty(page);
1770 }
1771
1772 if (!clear_page_dirty_for_io(page))
1773 goto continue_unlock;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001774
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001775 ret = __write_node_page(page, atomic &&
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001776 page == last_page,
Chao Yub0af6d42017-08-02 23:21:48 +08001777 &submitted, wbc, true,
Chao Yu50fa53e2018-08-02 23:03:19 +08001778 FS_NODE_IO, seq_id);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001779 if (ret) {
Jaegeuk Kim52681372016-04-13 16:24:44 -07001780 unlock_page(page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001781 f2fs_put_page(last_page, 0);
1782 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001783 } else if (submitted) {
Chao Yubab475c2018-09-27 23:41:16 +08001784 nwritten++;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001785 }
Chao Yu3f5f4952016-09-29 18:50:10 +08001786
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001787 if (page == last_page) {
1788 f2fs_put_page(page, 0);
1789 marked = true;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001790 break;
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001791 }
Jaegeuk Kim52681372016-04-13 16:24:44 -07001792 }
1793 pagevec_release(&pvec);
1794 cond_resched();
1795
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001796 if (ret || marked)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001797 break;
1798 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001799 if (!ret && atomic && !marked) {
Joe Perchesdcbb4c12019-06-18 17:48:42 +08001800 f2fs_debug(sbi, "Retry to write fsync mark: ino=%u, idx=%lx",
1801 ino, last_page->index);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001802 lock_page(last_page);
Chao Yubae0ee72018-12-25 17:43:42 +08001803 f2fs_wait_on_page_writeback(last_page, NODE, true, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001804 set_page_dirty(last_page);
1805 unlock_page(last_page);
1806 goto retry;
1807 }
Chao Yu9de69272016-10-11 22:57:06 +08001808out:
Chao Yubab475c2018-09-27 23:41:16 +08001809 if (nwritten)
1810 f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
Yi Zhuangf10ea3c2021-04-06 09:47:35 +08001811 return ret ? -EIO : 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001812}
1813
Chao Yu052a82d2019-08-22 20:17:56 +08001814static int f2fs_match_ino(struct inode *inode, unsigned long ino, void *data)
1815{
1816 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1817 bool clean;
1818
1819 if (inode->i_ino != ino)
1820 return 0;
1821
1822 if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
1823 return 0;
1824
1825 spin_lock(&sbi->inode_lock[DIRTY_META]);
1826 clean = list_empty(&F2FS_I(inode)->gdirty_list);
1827 spin_unlock(&sbi->inode_lock[DIRTY_META]);
1828
1829 if (clean)
1830 return 0;
1831
1832 inode = igrab(inode);
1833 if (!inode)
1834 return 0;
1835 return 1;
1836}
1837
1838static bool flush_dirty_inode(struct page *page)
1839{
1840 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1841 struct inode *inode;
1842 nid_t ino = ino_of_node(page);
1843
1844 inode = find_inode_nowait(sbi->sb, ino, f2fs_match_ino, NULL);
1845 if (!inode)
1846 return false;
1847
1848 f2fs_update_inode(inode, page);
1849 unlock_page(page);
1850
1851 iput(inode);
1852 return true;
1853}
1854
Jia Yang68e79ba2020-07-21 11:49:14 +08001855void f2fs_flush_inline_data(struct f2fs_sb_info *sbi)
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301856{
1857 pgoff_t index = 0;
1858 struct pagevec pvec;
1859 int nr_pages;
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301860
1861 pagevec_init(&pvec);
1862
1863 while ((nr_pages = pagevec_lookup_tag(&pvec,
1864 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
1865 int i;
1866
1867 for (i = 0; i < nr_pages; i++) {
1868 struct page *page = pvec.pages[i];
1869
1870 if (!IS_DNODE(page))
1871 continue;
1872
1873 lock_page(page);
1874
1875 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1876continue_unlock:
1877 unlock_page(page);
1878 continue;
1879 }
1880
1881 if (!PageDirty(page)) {
1882 /* someone wrote it for us */
1883 goto continue_unlock;
1884 }
1885
1886 /* flush inline_data, if it's async context. */
Chao Yubdc14e12021-04-28 17:20:31 +08001887 if (page_private_inline(page)) {
1888 clear_page_private_inline(page);
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301889 unlock_page(page);
1890 flush_inline_data(sbi, ino_of_node(page));
1891 continue;
1892 }
1893 unlock_page(page);
1894 }
1895 pagevec_release(&pvec);
1896 cond_resched();
1897 }
Sayali Lokhande34c061a2020-04-30 16:28:29 +05301898}
1899
Chao Yu4d57b862018-05-30 00:20:41 +08001900int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
1901 struct writeback_control *wbc,
Chao Yub0af6d42017-08-02 23:21:48 +08001902 bool do_balance, enum iostat_type io_type)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001903{
Jan Kara028a63a2017-11-15 17:34:51 -08001904 pgoff_t index;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001905 struct pagevec pvec;
1906 int step = 0;
Jaegeuk Kim12bb0a82016-03-11 15:33:22 -08001907 int nwritten = 0;
Chao Yu3f5f4952016-09-29 18:50:10 +08001908 int ret = 0;
Chao Yuc29fd0c2018-06-04 23:20:36 +08001909 int nr_pages, done = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001910
Mel Gorman86679822017-11-15 17:37:52 -08001911 pagevec_init(&pvec);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001912
1913next_step:
1914 index = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001915
Chao Yuc29fd0c2018-06-04 23:20:36 +08001916 while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
1917 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001918 int i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001919
1920 for (i = 0; i < nr_pages; i++) {
1921 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001922 bool submitted = false;
Chao Yu052a82d2019-08-22 20:17:56 +08001923 bool may_dirty = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001924
Chao Yuc29fd0c2018-06-04 23:20:36 +08001925 /* give a priority to WB_SYNC threads */
1926 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
1927 wbc->sync_mode == WB_SYNC_NONE) {
1928 done = 1;
1929 break;
1930 }
1931
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001932 /*
1933 * flushing sequence with step:
1934 * 0. indirect nodes
1935 * 1. dentry dnodes
1936 * 2. file dnodes
1937 */
1938 if (step == 0 && IS_DNODE(page))
1939 continue;
1940 if (step == 1 && (!IS_DNODE(page) ||
1941 is_cold_node(page)))
1942 continue;
1943 if (step == 2 && (!IS_DNODE(page) ||
1944 !is_cold_node(page)))
1945 continue;
Chao Yu9a4cbc92016-02-22 18:35:46 +08001946lock_node:
Chao Yu4b270a82018-07-04 18:04:10 +08001947 if (wbc->sync_mode == WB_SYNC_ALL)
1948 lock_page(page);
1949 else if (!trylock_page(page))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001950 continue;
1951
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001952 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001953continue_unlock:
1954 unlock_page(page);
1955 continue;
1956 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001957
1958 if (!PageDirty(page)) {
1959 /* someone wrote it for us */
1960 goto continue_unlock;
1961 }
1962
Jaegeuk Kimb0f3b872020-07-16 09:57:03 -07001963 /* flush inline_data/inode, if it's async context. */
1964 if (!do_balance)
1965 goto write_node;
1966
1967 /* flush inline_data */
Chao Yubdc14e12021-04-28 17:20:31 +08001968 if (page_private_inline(page)) {
1969 clear_page_private_inline(page);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001970 unlock_page(page);
1971 flush_inline_data(sbi, ino_of_node(page));
Chao Yu9a4cbc92016-02-22 18:35:46 +08001972 goto lock_node;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001973 }
1974
Chao Yu052a82d2019-08-22 20:17:56 +08001975 /* flush dirty inode */
1976 if (IS_INODE(page) && may_dirty) {
1977 may_dirty = false;
1978 if (flush_dirty_inode(page))
1979 goto lock_node;
1980 }
Jaegeuk Kimb0f3b872020-07-16 09:57:03 -07001981write_node:
Chao Yubae0ee72018-12-25 17:43:42 +08001982 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kimfa3d2bd2016-01-28 11:48:52 -08001983
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001984 if (!clear_page_dirty_for_io(page))
1985 goto continue_unlock;
1986
Jaegeuk Kim52681372016-04-13 16:24:44 -07001987 set_fsync_mark(page, 0);
1988 set_dentry_mark(page, 0);
Jaegeuk Kim52746512014-08-11 18:18:36 -07001989
Yunlong Song401db792017-07-27 20:11:00 +08001990 ret = __write_node_page(page, false, &submitted,
Chao Yu50fa53e2018-08-02 23:03:19 +08001991 wbc, do_balance, io_type, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001992 if (ret)
Jaegeuk Kim52746512014-08-11 18:18:36 -07001993 unlock_page(page);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001994 else if (submitted)
Chao Yu3f5f4952016-09-29 18:50:10 +08001995 nwritten++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001996
1997 if (--wbc->nr_to_write == 0)
1998 break;
1999 }
2000 pagevec_release(&pvec);
2001 cond_resched();
2002
2003 if (wbc->nr_to_write == 0) {
2004 step = 2;
2005 break;
2006 }
2007 }
2008
2009 if (step < 2) {
Jaegeuk Kim100c0652019-08-28 19:58:26 -07002010 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2011 wbc->sync_mode == WB_SYNC_NONE && step == 1)
Chao Yufd8c8ca2018-07-25 19:16:21 +08002012 goto out;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002013 step++;
2014 goto next_step;
2015 }
Chao Yufd8c8ca2018-07-25 19:16:21 +08002016out:
Chao Yu3f5f4952016-09-29 18:50:10 +08002017 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002018 f2fs_submit_merged_write(sbi, NODE);
Chao Yudb198ae2018-01-18 17:29:10 +08002019
2020 if (unlikely(f2fs_cp_error(sbi)))
2021 return -EIO;
Chao Yu3f5f4952016-09-29 18:50:10 +08002022 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002023}
2024
Chao Yu50fa53e2018-08-02 23:03:19 +08002025int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
2026 unsigned int seq_id)
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002027{
Chao Yu50fa53e2018-08-02 23:03:19 +08002028 struct fsync_node_entry *fn;
2029 struct page *page;
2030 struct list_head *head = &sbi->fsync_node_list;
2031 unsigned long flags;
2032 unsigned int cur_seq_id = 0;
Miklos Szeredi280db3c2016-09-16 12:44:21 +02002033 int ret2, ret = 0;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002034
Chao Yu50fa53e2018-08-02 23:03:19 +08002035 while (seq_id && cur_seq_id < seq_id) {
2036 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
2037 if (list_empty(head)) {
2038 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2039 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002040 }
Chao Yu50fa53e2018-08-02 23:03:19 +08002041 fn = list_first_entry(head, struct fsync_node_entry, list);
2042 if (fn->seq_id > seq_id) {
2043 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2044 break;
2045 }
2046 cur_seq_id = fn->seq_id;
2047 page = fn->page;
2048 get_page(page);
2049 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
2050
Chao Yubae0ee72018-12-25 17:43:42 +08002051 f2fs_wait_on_page_writeback(page, NODE, true, false);
Chao Yu50fa53e2018-08-02 23:03:19 +08002052 if (TestClearPageError(page))
2053 ret = -EIO;
2054
2055 put_page(page);
2056
2057 if (ret)
2058 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002059 }
2060
Miklos Szeredi280db3c2016-09-16 12:44:21 +02002061 ret2 = filemap_check_errors(NODE_MAPPING(sbi));
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002062 if (!ret)
2063 ret = ret2;
Chao Yu50fa53e2018-08-02 23:03:19 +08002064
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09002065 return ret;
2066}
2067
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002068static int f2fs_write_node_pages(struct address_space *mapping,
2069 struct writeback_control *wbc)
2070{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002071 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002072 struct blk_plug plug;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09002073 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002074
Chao Yu0771fcc2017-06-29 23:20:45 +08002075 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2076 goto skip_write;
2077
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09002078 /* balancing f2fs's metadata in background */
Chao Yu7bcd0cf2020-03-19 19:57:58 +08002079 f2fs_balance_fs_bg(sbi, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002080
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09002081 /* collect a number of dirty node pages and write together */
Jaegeuk Kim812a9592019-01-22 14:04:33 -08002082 if (wbc->sync_mode != WB_SYNC_ALL &&
2083 get_pages(sbi, F2FS_DIRTY_NODES) <
2084 nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002085 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09002086
Chao Yuc29fd0c2018-06-04 23:20:36 +08002087 if (wbc->sync_mode == WB_SYNC_ALL)
2088 atomic_inc(&sbi->wb_sync_req[NODE]);
2089 else if (atomic_read(&sbi->wb_sync_req[NODE]))
2090 goto skip_write;
2091
Yunlei Hed31c7c32016-02-04 16:14:00 +08002092 trace_f2fs_writepages(mapping->host, wbc, NODE);
2093
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09002094 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002095 blk_start_plug(&plug);
Chao Yu4d57b862018-05-30 00:20:41 +08002096 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002097 blk_finish_plug(&plug);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09002098 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Chao Yuc29fd0c2018-06-04 23:20:36 +08002099
2100 if (wbc->sync_mode == WB_SYNC_ALL)
2101 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002102 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002103
2104skip_write:
2105 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002106 trace_f2fs_writepages(mapping->host, wbc, NODE);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002107 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002108}
2109
2110static int f2fs_set_node_page_dirty(struct page *page)
2111{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002112 trace_f2fs_set_page_dirty(page, NODE);
2113
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002114 if (!PageUptodate(page))
2115 SetPageUptodate(page);
Weichao Guo54c55c42018-03-09 23:10:21 +08002116#ifdef CONFIG_F2FS_CHECK_FS
2117 if (IS_INODE(page))
2118 f2fs_inode_chksum_set(F2FS_P_SB(page), page);
2119#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002120 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07002121 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07002122 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Chao Yubdc14e12021-04-28 17:20:31 +08002123 set_page_private_reference(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002124 return 1;
2125 }
2126 return 0;
2127}
2128
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002129/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002130 * Structure of the f2fs node operations
2131 */
2132const struct address_space_operations f2fs_node_aops = {
2133 .writepage = f2fs_write_node_page,
2134 .writepages = f2fs_write_node_pages,
2135 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002136 .invalidatepage = f2fs_invalidate_page,
2137 .releasepage = f2fs_release_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002138#ifdef CONFIG_MIGRATION
Jack Qiua87aff12020-07-24 16:55:28 +08002139 .migratepage = f2fs_migrate_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002140#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002141};
2142
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002143static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
2144 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002145{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002146 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002147}
2148
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002149static int __insert_free_nid(struct f2fs_sb_info *sbi,
Liu Songb815bdc2020-06-28 21:48:13 +08002150 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002151{
Chao Yub8559dc2016-10-12 19:28:29 +08002152 struct f2fs_nm_info *nm_i = NM_I(sbi);
Fan Lia0761f62017-10-28 19:03:37 +08002153 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
Yi Zhuangf10ea3c2021-04-06 09:47:35 +08002154
Fan Lia0761f62017-10-28 19:03:37 +08002155 if (err)
2156 return err;
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002157
Liu Songb815bdc2020-06-28 21:48:13 +08002158 nm_i->nid_cnt[FREE_NID]++;
2159 list_add_tail(&i->list, &nm_i->free_nid_list);
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002160 return 0;
Chao Yub8559dc2016-10-12 19:28:29 +08002161}
2162
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002163static void __remove_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08002164 struct free_nid *i, enum nid_state state)
Chao Yub8559dc2016-10-12 19:28:29 +08002165{
2166 struct f2fs_nm_info *nm_i = NM_I(sbi);
2167
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002168 f2fs_bug_on(sbi, state != i->state);
2169 nm_i->nid_cnt[state]--;
2170 if (state == FREE_NID)
2171 list_del(&i->list);
Fan Lia0761f62017-10-28 19:03:37 +08002172 radix_tree_delete(&nm_i->free_nid_root, i->nid);
2173}
2174
2175static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2176 enum nid_state org_state, enum nid_state dst_state)
2177{
2178 struct f2fs_nm_info *nm_i = NM_I(sbi);
2179
2180 f2fs_bug_on(sbi, org_state != i->state);
2181 i->state = dst_state;
2182 nm_i->nid_cnt[org_state]--;
2183 nm_i->nid_cnt[dst_state]++;
2184
2185 switch (dst_state) {
2186 case PREALLOC_NID:
2187 list_del(&i->list);
2188 break;
2189 case FREE_NID:
2190 list_add_tail(&i->list, &nm_i->free_nid_list);
2191 break;
2192 default:
2193 BUG_ON(1);
2194 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002195}
2196
LiFan5921aaa2017-11-22 16:07:23 +08002197static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2198 bool set, bool build)
2199{
2200 struct f2fs_nm_info *nm_i = NM_I(sbi);
2201 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2202 unsigned int nid_ofs = nid - START_NID(nid);
2203
2204 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2205 return;
2206
2207 if (set) {
2208 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2209 return;
2210 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2211 nm_i->free_nid_count[nat_ofs]++;
2212 } else {
2213 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2214 return;
2215 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2216 if (!build)
2217 nm_i->free_nid_count[nat_ofs]--;
2218 }
2219}
2220
Chao Yu4ac91242017-02-23 10:53:49 +08002221/* return if the nid is recognized as free */
LiFan5921aaa2017-11-22 16:07:23 +08002222static bool add_free_nid(struct f2fs_sb_info *sbi,
2223 nid_t nid, bool build, bool update)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002224{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002225 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu30a61dd2017-03-22 14:45:05 +08002226 struct free_nid *i, *e;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002227 struct nat_entry *ne;
Chao Yu30a61dd2017-03-22 14:45:05 +08002228 int err = -EINVAL;
2229 bool ret = false;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09002230
2231 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08002232 if (unlikely(nid == 0))
Chao Yu4ac91242017-02-23 10:53:49 +08002233 return false;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002234
Chao Yu626bcf22019-04-15 15:28:36 +08002235 if (unlikely(f2fs_check_nid_range(sbi, nid)))
2236 return false;
2237
Gu Zheng7bd59382013-10-22 14:52:26 +08002238 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002239 i->nid = nid;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002240 i->state = FREE_NID;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002241
LiFan5921aaa2017-11-22 16:07:23 +08002242 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002243
Chao Yub8559dc2016-10-12 19:28:29 +08002244 spin_lock(&nm_i->nid_list_lock);
Chao Yu30a61dd2017-03-22 14:45:05 +08002245
2246 if (build) {
2247 /*
2248 * Thread A Thread B
2249 * - f2fs_create
2250 * - f2fs_new_inode
Chao Yu4d57b862018-05-30 00:20:41 +08002251 * - f2fs_alloc_nid
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002252 * - __insert_nid_to_list(PREALLOC_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002253 * - f2fs_balance_fs_bg
Chao Yu4d57b862018-05-30 00:20:41 +08002254 * - f2fs_build_free_nids
2255 * - __f2fs_build_free_nids
Chao Yu30a61dd2017-03-22 14:45:05 +08002256 * - scan_nat_page
2257 * - add_free_nid
2258 * - __lookup_nat_cache
2259 * - f2fs_add_link
Chao Yu4d57b862018-05-30 00:20:41 +08002260 * - f2fs_init_inode_metadata
2261 * - f2fs_new_inode_page
2262 * - f2fs_new_node_page
Chao Yu30a61dd2017-03-22 14:45:05 +08002263 * - set_node_addr
Chao Yu4d57b862018-05-30 00:20:41 +08002264 * - f2fs_alloc_nid_done
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002265 * - __remove_nid_from_list(PREALLOC_NID)
2266 * - __insert_nid_to_list(FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002267 */
2268 ne = __lookup_nat_cache(nm_i, nid);
2269 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2270 nat_get_blkaddr(ne) != NULL_ADDR))
2271 goto err_out;
2272
2273 e = __lookup_free_nid_list(nm_i, nid);
2274 if (e) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002275 if (e->state == FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002276 ret = true;
2277 goto err_out;
2278 }
2279 }
2280 ret = true;
Liu Songb815bdc2020-06-28 21:48:13 +08002281 err = __insert_free_nid(sbi, i);
Chao Yu30a61dd2017-03-22 14:45:05 +08002282err_out:
LiFan5921aaa2017-11-22 16:07:23 +08002283 if (update) {
2284 update_free_nid_bitmap(sbi, nid, ret, build);
2285 if (!build)
2286 nm_i->available_nids++;
2287 }
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002288 spin_unlock(&nm_i->nid_list_lock);
2289 radix_tree_preload_end();
LiFan5921aaa2017-11-22 16:07:23 +08002290
Chao Yu30a61dd2017-03-22 14:45:05 +08002291 if (err)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002292 kmem_cache_free(free_nid_slab, i);
Chao Yu30a61dd2017-03-22 14:45:05 +08002293 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002294}
2295
Chao Yub8559dc2016-10-12 19:28:29 +08002296static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002297{
Chao Yub8559dc2016-10-12 19:28:29 +08002298 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002299 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002300 bool need_free = false;
2301
Chao Yub8559dc2016-10-12 19:28:29 +08002302 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002303 i = __lookup_free_nid_list(nm_i, nid);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002304 if (i && i->state == FREE_NID) {
Fan Lia0761f62017-10-28 19:03:37 +08002305 __remove_free_nid(sbi, i, FREE_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002306 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002307 }
Chao Yub8559dc2016-10-12 19:28:29 +08002308 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002309
2310 if (need_free)
2311 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002312}
2313
Chao Yue2374012018-06-15 14:45:57 +08002314static int scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002315 struct page *nat_page, nid_t start_nid)
2316{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002317 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002318 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2319 block_t blk_addr;
Chao Yu4ac91242017-02-23 10:53:49 +08002320 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002321 int i;
2322
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002323 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
Chao Yu4ac91242017-02-23 10:53:49 +08002324
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002325 i = start_nid % NAT_ENTRY_PER_BLOCK;
2326
2327 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Chao Yucfb271d2013-12-05 17:15:22 +08002328 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09002329 break;
Haicheng Li23d38842013-05-06 23:15:43 +08002330
2331 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Chao Yue2374012018-06-15 14:45:57 +08002332
2333 if (blk_addr == NEW_ADDR)
2334 return -EINVAL;
2335
LiFan5921aaa2017-11-22 16:07:23 +08002336 if (blk_addr == NULL_ADDR) {
2337 add_free_nid(sbi, start_nid, true, true);
2338 } else {
2339 spin_lock(&NM_I(sbi)->nid_list_lock);
2340 update_free_nid_bitmap(sbi, start_nid, false, true);
2341 spin_unlock(&NM_I(sbi)->nid_list_lock);
2342 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002343 }
Chao Yue2374012018-06-15 14:45:57 +08002344
2345 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002346}
2347
Chao Yu2fbaa252017-11-08 17:47:36 +08002348static void scan_curseg_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002349{
Chao Yu4ac91242017-02-23 10:53:49 +08002350 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2351 struct f2fs_journal *journal = curseg->journal;
Chao Yu2fbaa252017-11-08 17:47:36 +08002352 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08002353
Chao Yu4ac91242017-02-23 10:53:49 +08002354 down_read(&curseg->journal_rwsem);
2355 for (i = 0; i < nats_in_cursum(journal); i++) {
2356 block_t addr;
2357 nid_t nid;
2358
2359 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2360 nid = le32_to_cpu(nid_in_journal(journal, i));
2361 if (addr == NULL_ADDR)
LiFan5921aaa2017-11-22 16:07:23 +08002362 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002363 else
2364 remove_free_nid(sbi, nid);
2365 }
2366 up_read(&curseg->journal_rwsem);
Chao Yu2fbaa252017-11-08 17:47:36 +08002367}
2368
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002369static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002370{
2371 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002372 unsigned int i, idx;
Fan Li97456572017-11-07 19:14:24 +08002373 nid_t nid;
Chao Yu4ac91242017-02-23 10:53:49 +08002374
2375 down_read(&nm_i->nat_tree_lock);
2376
2377 for (i = 0; i < nm_i->nat_blocks; i++) {
2378 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2379 continue;
2380 if (!nm_i->free_nid_count[i])
2381 continue;
2382 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
Fan Li97456572017-11-07 19:14:24 +08002383 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2384 NAT_ENTRY_PER_BLOCK, idx);
2385 if (idx >= NAT_ENTRY_PER_BLOCK)
2386 break;
Chao Yu4ac91242017-02-23 10:53:49 +08002387
2388 nid = i * NAT_ENTRY_PER_BLOCK + idx;
LiFan5921aaa2017-11-22 16:07:23 +08002389 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002390
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002391 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
Chao Yu4ac91242017-02-23 10:53:49 +08002392 goto out;
2393 }
2394 }
2395out:
Chao Yu2fbaa252017-11-08 17:47:36 +08002396 scan_curseg_cache(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002397
Chao Yu4ac91242017-02-23 10:53:49 +08002398 up_read(&nm_i->nat_tree_lock);
2399}
2400
Chao Yue2374012018-06-15 14:45:57 +08002401static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
Chao Yu4d57b862018-05-30 00:20:41 +08002402 bool sync, bool mount)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002403{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002404 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yue2374012018-06-15 14:45:57 +08002405 int i = 0, ret;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002406 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002407
Yunlei Hee9cdd302017-04-26 15:56:52 +08002408 if (unlikely(nid >= nm_i->max_nid))
2409 nid = 0;
2410
Sahitya Tummalae2cab032020-08-18 15:40:14 +05302411 if (unlikely(nid % NAT_ENTRY_PER_BLOCK))
2412 nid = NAT_BLOCK_OFFSET(nid) * NAT_ENTRY_PER_BLOCK;
2413
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002414 /* Enough entries */
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002415 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002416 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002417
Chao Yu4d57b862018-05-30 00:20:41 +08002418 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
Chao Yue2374012018-06-15 14:45:57 +08002419 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002420
Chao Yu4ac91242017-02-23 10:53:49 +08002421 if (!mount) {
2422 /* try to find free nids in free_nid_bitmap */
2423 scan_free_nid_bits(sbi);
2424
Fan Li74986212017-11-07 11:04:33 +08002425 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002426 return 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002427 }
2428
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002429 /* readahead nat pages to be scanned */
Chao Yu4d57b862018-05-30 00:20:41 +08002430 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
Chao Yu26879fb2015-10-12 17:05:59 +08002431 META_NAT, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002432
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002433 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002434
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002435 while (1) {
Yunlei He66e83362017-11-17 16:13:38 +08002436 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2437 nm_i->nat_block_bitmap)) {
2438 struct page *page = get_current_nat_page(sbi, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002439
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002440 if (IS_ERR(page)) {
2441 ret = PTR_ERR(page);
2442 } else {
2443 ret = scan_nat_page(sbi, page, nid);
2444 f2fs_put_page(page, 1);
2445 }
Chao Yue2374012018-06-15 14:45:57 +08002446
2447 if (ret) {
2448 up_read(&nm_i->nat_tree_lock);
Joe Perchesdcbb4c12019-06-18 17:48:42 +08002449 f2fs_err(sbi, "NAT is corrupt, run fsck to fix it");
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002450 return ret;
Chao Yue2374012018-06-15 14:45:57 +08002451 }
Yunlei He66e83362017-11-17 16:13:38 +08002452 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002453
2454 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08002455 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002456 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002457
Chao Yua6d494b2015-07-24 18:26:26 +08002458 if (++i >= FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002459 break;
2460 }
2461
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002462 /* go to the next free nat pages to find free nids abundantly */
2463 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002464
2465 /* find free nids from current sum_pages */
Chao Yu2fbaa252017-11-08 17:47:36 +08002466 scan_curseg_cache(sbi);
Chao Yudfc08a12016-02-14 18:50:40 +08002467
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002468 up_read(&nm_i->nat_tree_lock);
Chao Yu2db23882015-10-12 17:07:33 +08002469
Chao Yu4d57b862018-05-30 00:20:41 +08002470 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
Chao Yuea1a29a02015-10-12 17:08:48 +08002471 nm_i->ra_nid_pages, META_NAT, false);
Chao Yue2374012018-06-15 14:45:57 +08002472
2473 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002474}
2475
Chao Yue2374012018-06-15 14:45:57 +08002476int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
Chao Yu2411cf52016-10-11 22:31:34 +08002477{
Chao Yue2374012018-06-15 14:45:57 +08002478 int ret;
2479
Chao Yu2411cf52016-10-11 22:31:34 +08002480 mutex_lock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002481 ret = __f2fs_build_free_nids(sbi, sync, mount);
Chao Yu2411cf52016-10-11 22:31:34 +08002482 mutex_unlock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002483
2484 return ret;
Chao Yu2411cf52016-10-11 22:31:34 +08002485}
2486
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002487/*
2488 * If this function returns success, caller can obtain a new nid
2489 * from second parameter of this function.
2490 * The returned nid could be used ino as well as nid when inode is created.
2491 */
Chao Yu4d57b862018-05-30 00:20:41 +08002492bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002493{
2494 struct f2fs_nm_info *nm_i = NM_I(sbi);
2495 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002496retry:
Chao Yu55523512017-02-25 11:08:28 +08002497 if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
Chao Yuc45d6002019-11-01 17:53:23 +08002498 f2fs_show_injection_info(sbi, FAULT_ALLOC_NID);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002499 return false;
Chao Yu55523512017-02-25 11:08:28 +08002500 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +02002501
Chao Yub8559dc2016-10-12 19:28:29 +08002502 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002503
Chao Yu04d47e62016-11-17 20:53:11 +08002504 if (unlikely(nm_i->available_nids == 0)) {
2505 spin_unlock(&nm_i->nid_list_lock);
2506 return false;
2507 }
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002508
Chao Yu4d57b862018-05-30 00:20:41 +08002509 /* We should not use stale free nids created by f2fs_build_free_nids */
2510 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002511 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2512 i = list_first_entry(&nm_i->free_nid_list,
Chao Yub8559dc2016-10-12 19:28:29 +08002513 struct free_nid, list);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002514 *nid = i->nid;
Chao Yub8559dc2016-10-12 19:28:29 +08002515
Fan Lia0761f62017-10-28 19:03:37 +08002516 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
Chao Yu04d47e62016-11-17 20:53:11 +08002517 nm_i->available_nids--;
Chao Yu4ac91242017-02-23 10:53:49 +08002518
Chao Yu346fe752017-03-13 20:10:41 +08002519 update_free_nid_bitmap(sbi, *nid, false, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002520
Chao Yub8559dc2016-10-12 19:28:29 +08002521 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002522 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002523 }
Chao Yub8559dc2016-10-12 19:28:29 +08002524 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002525
2526 /* Let's scan nat pages and its caches to get free nids */
Jaegeuk Kimf84262b2018-09-19 15:45:19 -07002527 if (!f2fs_build_free_nids(sbi, true, false))
2528 goto retry;
2529 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002530}
2531
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002532/*
Chao Yu4d57b862018-05-30 00:20:41 +08002533 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002534 */
Chao Yu4d57b862018-05-30 00:20:41 +08002535void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002536{
2537 struct f2fs_nm_info *nm_i = NM_I(sbi);
2538 struct free_nid *i;
2539
Chao Yub8559dc2016-10-12 19:28:29 +08002540 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002541 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002542 f2fs_bug_on(sbi, !i);
Fan Lia0761f62017-10-28 19:03:37 +08002543 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08002544 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002545
2546 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002547}
2548
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002549/*
Chao Yu4d57b862018-05-30 00:20:41 +08002550 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002551 */
Chao Yu4d57b862018-05-30 00:20:41 +08002552void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002553{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002554 struct f2fs_nm_info *nm_i = NM_I(sbi);
2555 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002556 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002557
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002558 if (!nid)
2559 return;
2560
Chao Yub8559dc2016-10-12 19:28:29 +08002561 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002562 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002563 f2fs_bug_on(sbi, !i);
2564
Chao Yu4d57b862018-05-30 00:20:41 +08002565 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
Fan Lia0761f62017-10-28 19:03:37 +08002566 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002567 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08002568 } else {
Fan Lia0761f62017-10-28 19:03:37 +08002569 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
Haicheng Li95630cb2013-05-06 23:15:41 +08002570 }
Chao Yu04d47e62016-11-17 20:53:11 +08002571
2572 nm_i->available_nids++;
2573
Chao Yu346fe752017-03-13 20:10:41 +08002574 update_free_nid_bitmap(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002575
Chao Yub8559dc2016-10-12 19:28:29 +08002576 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002577
2578 if (need_free)
2579 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002580}
2581
Chao Yu4d57b862018-05-30 00:20:41 +08002582int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
Chao Yu31696582015-07-28 18:33:46 +08002583{
2584 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu31696582015-07-28 18:33:46 +08002585 int nr = nr_shrink;
2586
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002587 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Jaegeuk Kimad4edb82016-06-16 16:41:49 -07002588 return 0;
2589
Chao Yu31696582015-07-28 18:33:46 +08002590 if (!mutex_trylock(&nm_i->build_lock))
2591 return 0;
2592
Chao Yu042be372020-05-08 17:50:20 +08002593 while (nr_shrink && nm_i->nid_cnt[FREE_NID] > MAX_FREE_NIDS) {
2594 struct free_nid *i, *next;
2595 unsigned int batch = SHRINK_NID_BATCH_SIZE;
Chao Yub8559dc2016-10-12 19:28:29 +08002596
Chao Yu042be372020-05-08 17:50:20 +08002597 spin_lock(&nm_i->nid_list_lock);
2598 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
2599 if (!nr_shrink || !batch ||
2600 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
2601 break;
2602 __remove_free_nid(sbi, i, FREE_NID);
2603 kmem_cache_free(free_nid_slab, i);
2604 nr_shrink--;
2605 batch--;
2606 }
2607 spin_unlock(&nm_i->nid_list_lock);
Chao Yu31696582015-07-28 18:33:46 +08002608 }
Chao Yu042be372020-05-08 17:50:20 +08002609
Chao Yu31696582015-07-28 18:33:46 +08002610 mutex_unlock(&nm_i->build_lock);
2611
2612 return nr - nr_shrink;
2613}
2614
Chao Yu9627a7b2020-07-06 18:23:36 +08002615int f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08002616{
Chao Yu28cdce02014-03-11 13:37:38 +08002617 void *src_addr, *dst_addr;
2618 size_t inline_size;
2619 struct page *ipage;
2620 struct f2fs_inode *ri;
2621
Chao Yu4d57b862018-05-30 00:20:41 +08002622 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu9627a7b2020-07-06 18:23:36 +08002623 if (IS_ERR(ipage))
2624 return PTR_ERR(ipage);
Chao Yu28cdce02014-03-11 13:37:38 +08002625
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002626 ri = F2FS_INODE(page);
Yunlei He1eca05a2018-01-03 18:03:04 +08002627 if (ri->i_inline & F2FS_INLINE_XATTR) {
Chao Yub91c6822020-12-07 17:54:41 +08002628 if (!f2fs_has_inline_xattr(inode)) {
2629 set_inode_flag(inode, FI_INLINE_XATTR);
2630 stat_inc_inline_xattr(inode);
2631 }
Yunlei He1eca05a2018-01-03 18:03:04 +08002632 } else {
Chao Yub91c6822020-12-07 17:54:41 +08002633 if (f2fs_has_inline_xattr(inode)) {
2634 stat_dec_inline_xattr(inode);
2635 clear_inode_flag(inode, FI_INLINE_XATTR);
2636 }
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002637 goto update_inode;
2638 }
2639
Chao Yu6afc6622017-09-06 21:59:50 +08002640 dst_addr = inline_xattr_addr(inode, ipage);
2641 src_addr = inline_xattr_addr(inode, page);
Chao Yu28cdce02014-03-11 13:37:38 +08002642 inline_size = inline_xattr_size(inode);
2643
Chao Yubae0ee72018-12-25 17:43:42 +08002644 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yu28cdce02014-03-11 13:37:38 +08002645 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002646update_inode:
Chao Yu4d57b862018-05-30 00:20:41 +08002647 f2fs_update_inode(inode, ipage);
Chao Yu28cdce02014-03-11 13:37:38 +08002648 f2fs_put_page(ipage, 1);
Chao Yu9627a7b2020-07-06 18:23:36 +08002649 return 0;
Chao Yu28cdce02014-03-11 13:37:38 +08002650}
2651
Chao Yu4d57b862018-05-30 00:20:41 +08002652int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002653{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002654 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002655 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
Yunlei He87905682017-07-18 09:48:12 +08002656 nid_t new_xnid;
2657 struct dnode_of_data dn;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002658 struct node_info ni;
Chao Yud2600812017-02-08 17:39:45 +08002659 struct page *xpage;
Chao Yu77357302018-07-17 00:02:17 +08002660 int err;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002661
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002662 if (!prev_xnid)
2663 goto recover_xnid;
2664
Chao Yud2600812017-02-08 17:39:45 +08002665 /* 1: invalidate the previous xattr nid */
Chao Yu77357302018-07-17 00:02:17 +08002666 err = f2fs_get_node_info(sbi, prev_xnid, &ni);
2667 if (err)
2668 return err;
2669
Chao Yu4d57b862018-05-30 00:20:41 +08002670 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +08002671 dec_valid_node_count(sbi, inode, false);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002672 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002673
2674recover_xnid:
Chao Yud2600812017-02-08 17:39:45 +08002675 /* 2: update xattr nid in inode */
Chao Yu4d57b862018-05-30 00:20:41 +08002676 if (!f2fs_alloc_nid(sbi, &new_xnid))
Yunlei He87905682017-07-18 09:48:12 +08002677 return -ENOSPC;
2678
2679 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
Chao Yu4d57b862018-05-30 00:20:41 +08002680 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Yunlei He87905682017-07-18 09:48:12 +08002681 if (IS_ERR(xpage)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002682 f2fs_alloc_nid_failed(sbi, new_xnid);
Yunlei He87905682017-07-18 09:48:12 +08002683 return PTR_ERR(xpage);
2684 }
2685
Chao Yu4d57b862018-05-30 00:20:41 +08002686 f2fs_alloc_nid_done(sbi, new_xnid);
2687 f2fs_update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002688
Chao Yud2600812017-02-08 17:39:45 +08002689 /* 3: update and set xattr node page dirty */
Yunlei He87905682017-07-18 09:48:12 +08002690 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
Chao Yud2600812017-02-08 17:39:45 +08002691
Chao Yud2600812017-02-08 17:39:45 +08002692 set_page_dirty(xpage);
2693 f2fs_put_page(xpage, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002694
Chao Yud2600812017-02-08 17:39:45 +08002695 return 0;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002696}
2697
Chao Yu4d57b862018-05-30 00:20:41 +08002698int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002699{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002700 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002701 nid_t ino = ino_of_node(page);
2702 struct node_info old_ni, new_ni;
2703 struct page *ipage;
Chao Yu77357302018-07-17 00:02:17 +08002704 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002705
Chao Yu77357302018-07-17 00:02:17 +08002706 err = f2fs_get_node_info(sbi, ino, &old_ni);
2707 if (err)
2708 return err;
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09002709
2710 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2711 return -EINVAL;
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002712retry:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07002713 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002714 if (!ipage) {
Chao Yu5df7731f2020-02-17 17:45:44 +08002715 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002716 goto retry;
2717 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002718
arter97e1c42042014-08-06 23:22:50 +09002719 /* Should not use this inode from free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08002720 remove_free_nid(sbi, ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002721
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002722 if (!PageUptodate(ipage))
2723 SetPageUptodate(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002724 fill_node_footer(ipage, ino, ino, 0, true);
Chao Yuef2a0072018-10-03 22:32:44 +08002725 set_cold_node(ipage, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002726
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002727 src = F2FS_INODE(page);
2728 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002729
Zheng Yongjun14e6dc52020-12-22 21:34:15 +08002730 memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002731 dst->i_size = 0;
2732 dst->i_blocks = cpu_to_le64(1);
2733 dst->i_links = cpu_to_le32(1);
2734 dst->i_xattr_nid = 0;
Chao Yu7a2af762017-07-19 00:19:06 +08002735 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
Chao Yu5c571322017-07-26 00:01:41 +08002736 if (dst->i_inline & F2FS_EXTRA_ATTR) {
Chao Yu7a2af762017-07-19 00:19:06 +08002737 dst->i_extra_isize = src->i_extra_isize;
Chao Yu6afc6622017-09-06 21:59:50 +08002738
Chao Yu7beb01f2018-10-24 18:34:26 +08002739 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
Chao Yu6afc6622017-09-06 21:59:50 +08002740 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2741 i_inline_xattr_size))
2742 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2743
Chao Yu7beb01f2018-10-24 18:34:26 +08002744 if (f2fs_sb_has_project_quota(sbi) &&
Chao Yu5c571322017-07-26 00:01:41 +08002745 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2746 i_projid))
2747 dst->i_projid = src->i_projid;
Chao Yu5cd1f382018-09-25 15:36:01 +08002748
Chao Yu7beb01f2018-10-24 18:34:26 +08002749 if (f2fs_sb_has_inode_crtime(sbi) &&
Chao Yu5cd1f382018-09-25 15:36:01 +08002750 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2751 i_crtime_nsec)) {
2752 dst->i_crtime = src->i_crtime;
2753 dst->i_crtime_nsec = src->i_crtime_nsec;
2754 }
Chao Yu5c571322017-07-26 00:01:41 +08002755 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002756
2757 new_ni = old_ni;
2758 new_ni.ino = ino;
2759
Chao Yu0abd6752017-07-09 00:13:07 +08002760 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09002761 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002762 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002763 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07002764 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002765 f2fs_put_page(ipage, 1);
2766 return 0;
2767}
2768
Chao Yu77357302018-07-17 00:02:17 +08002769int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002770 unsigned int segno, struct f2fs_summary_block *sum)
2771{
2772 struct f2fs_node *rn;
2773 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002774 block_t addr;
Chao Yu9ecf4b82014-12-18 18:29:05 +08002775 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002776
2777 /* scan the node segment */
2778 last_offset = sbi->blocks_per_seg;
2779 addr = START_BLOCK(sbi, segno);
2780 sum_entry = &sum->entries[0];
2781
Chao Yu9ecf4b82014-12-18 18:29:05 +08002782 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Jaegeuk Kim664ba972016-10-18 11:07:45 -07002783 nrpages = min(last_offset - i, BIO_MAX_PAGES);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002784
arter97e1c42042014-08-06 23:22:50 +09002785 /* readahead node pages */
Chao Yu4d57b862018-05-30 00:20:41 +08002786 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002787
Chao Yu9ecf4b82014-12-18 18:29:05 +08002788 for (idx = addr; idx < addr + nrpages; idx++) {
Chao Yu4d57b862018-05-30 00:20:41 +08002789 struct page *page = f2fs_get_tmp_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08002790
Chao Yu77357302018-07-17 00:02:17 +08002791 if (IS_ERR(page))
2792 return PTR_ERR(page);
2793
Chao Yu9ecf4b82014-12-18 18:29:05 +08002794 rn = F2FS_NODE(page);
2795 sum_entry->nid = rn->footer.nid;
2796 sum_entry->version = 0;
2797 sum_entry->ofs_in_node = 0;
2798 sum_entry++;
2799 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08002800 }
Chao Yubac4eef2014-05-27 08:41:07 +08002801
Chao Yu9ecf4b82014-12-18 18:29:05 +08002802 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08002803 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002804 }
Chao Yu77357302018-07-17 00:02:17 +08002805 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002806}
2807
Chao Yuaec71382014-06-24 09:18:20 +08002808static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002809{
2810 struct f2fs_nm_info *nm_i = NM_I(sbi);
2811 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002812 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002813 int i;
2814
Chao Yub7ad7512016-02-19 18:08:46 +08002815 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002816 for (i = 0; i < nats_in_cursum(journal); i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002817 struct nat_entry *ne;
2818 struct f2fs_nat_entry raw_ne;
Chao Yudfc08a12016-02-14 18:50:40 +08002819 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002820
Chao Yu0d36b382021-03-22 19:47:30 +08002821 if (f2fs_check_nid_range(sbi, nid))
2822 continue;
2823
Chao Yudfc08a12016-02-14 18:50:40 +08002824 raw_ne = nat_in_journal(journal, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08002825
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002826 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002827 if (!ne) {
Yunlei He12f9ef32017-11-10 13:36:51 -08002828 ne = __alloc_nat_entry(nid, true);
2829 __init_nat_entry(nm_i, ne, &raw_ne, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002830 }
Chao Yu04d47e62016-11-17 20:53:11 +08002831
2832 /*
2833 * if a free nat in journal has not been used after last
2834 * checkpoint, we should remove it from available nids,
2835 * since later we will add it again.
2836 */
2837 if (!get_nat_flag(ne, IS_DIRTY) &&
2838 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2839 spin_lock(&nm_i->nid_list_lock);
2840 nm_i->available_nids--;
2841 spin_unlock(&nm_i->nid_list_lock);
2842 }
2843
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002844 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002845 }
Chao Yudfc08a12016-02-14 18:50:40 +08002846 update_nats_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002847 up_write(&curseg->journal_rwsem);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002848}
2849
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002850static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2851 struct list_head *head, int max)
2852{
2853 struct nat_entry_set *cur;
2854
2855 if (nes->entry_cnt >= max)
2856 goto add_out;
2857
2858 list_for_each_entry(cur, head, set_list) {
2859 if (cur->entry_cnt >= nes->entry_cnt) {
2860 list_add(&nes->set_list, cur->set_list.prev);
2861 return;
2862 }
2863 }
2864add_out:
2865 list_add_tail(&nes->set_list, head);
2866}
2867
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002868static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002869 struct page *page)
2870{
2871 struct f2fs_nm_info *nm_i = NM_I(sbi);
2872 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2873 struct f2fs_nat_block *nat_blk = page_address(page);
2874 int valid = 0;
Fan Li37a0ab22017-10-30 15:19:48 +08002875 int i = 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002876
2877 if (!enabled_nat_bits(sbi, NULL))
2878 return;
2879
Fan Li37a0ab22017-10-30 15:19:48 +08002880 if (nat_index == 0) {
2881 valid = 1;
2882 i = 1;
2883 }
2884 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
Chao Yu36af5f42019-05-28 17:23:33 +08002885 if (le32_to_cpu(nat_blk->entries[i].block_addr) != NULL_ADDR)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002886 valid++;
2887 }
2888 if (valid == 0) {
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002889 __set_bit_le(nat_index, nm_i->empty_nat_bits);
2890 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002891 return;
2892 }
2893
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002894 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002895 if (valid == NAT_ENTRY_PER_BLOCK)
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002896 __set_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002897 else
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002898 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002899}
2900
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002901static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002902 struct nat_entry_set *set, struct cp_control *cpc)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002903{
2904 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002905 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002906 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
2907 bool to_journal = true;
2908 struct f2fs_nat_block *nat_blk;
2909 struct nat_entry *ne, *cur;
2910 struct page *page = NULL;
2911
2912 /*
2913 * there are two steps to flush nat entries:
2914 * #1, flush nat entries to journal in current hot data summary block.
2915 * #2, flush nat entries to nat page.
2916 */
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002917 if (enabled_nat_bits(sbi, cpc) ||
2918 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002919 to_journal = false;
2920
2921 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002922 down_write(&curseg->journal_rwsem);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002923 } else {
2924 page = get_next_nat_page(sbi, start_nid);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002925 if (IS_ERR(page))
2926 return PTR_ERR(page);
2927
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002928 nat_blk = page_address(page);
2929 f2fs_bug_on(sbi, !nat_blk);
2930 }
2931
2932 /* flush dirty nats in nat entry set */
2933 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
2934 struct f2fs_nat_entry *raw_ne;
2935 nid_t nid = nat_get_nid(ne);
2936 int offset;
2937
Chao Yufebeca62017-06-05 18:29:08 +08002938 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002939
2940 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08002941 offset = f2fs_lookup_journal_in_cursum(journal,
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002942 NAT_JOURNAL, nid, 1);
2943 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002944 raw_ne = &nat_in_journal(journal, offset);
2945 nid_in_journal(journal, offset) = cpu_to_le32(nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002946 } else {
2947 raw_ne = &nat_blk->entries[nid - start_nid];
2948 }
2949 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002950 nat_reset_flag(ne);
Kinglong Mee0b28b712017-02-28 21:34:47 +08002951 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
Chao Yu04d47e62016-11-17 20:53:11 +08002952 if (nat_get_blkaddr(ne) == NULL_ADDR) {
LiFan5921aaa2017-11-22 16:07:23 +08002953 add_free_nid(sbi, nid, false, true);
Chao Yu4ac91242017-02-23 10:53:49 +08002954 } else {
2955 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu346fe752017-03-13 20:10:41 +08002956 update_free_nid_bitmap(sbi, nid, false, false);
Chao Yu04d47e62016-11-17 20:53:11 +08002957 spin_unlock(&NM_I(sbi)->nid_list_lock);
2958 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002959 }
2960
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002961 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002962 up_write(&curseg->journal_rwsem);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002963 } else {
2964 __update_nat_bits(sbi, start_nid, page);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002965 f2fs_put_page(page, 1);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002966 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002967
Yunlei He59c90812017-03-13 20:22:18 +08002968 /* Allow dirty nats by node block allocation in write_begin */
2969 if (!set->entry_cnt) {
2970 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
2971 kmem_cache_free(nat_entry_set_slab, set);
2972 }
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002973 return 0;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002974}
2975
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002976/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002977 * This function is called during the checkpointing process.
2978 */
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002979int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002980{
2981 struct f2fs_nm_info *nm_i = NM_I(sbi);
2982 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002983 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002984 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002985 struct nat_entry_set *set, *tmp;
2986 unsigned int found;
2987 nid_t set_idx = 0;
2988 LIST_HEAD(sets);
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07002989 int err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002990
Jaegeuk Kim317c4f52020-11-06 13:22:05 -08002991 /*
2992 * during unmount, let's flush nat_bits before checking
2993 * nat_cnt[DIRTY_NAT].
2994 */
Jaegeuk Kim7f2ecdd2018-06-28 19:34:40 -07002995 if (enabled_nat_bits(sbi, cpc)) {
2996 down_write(&nm_i->nat_tree_lock);
2997 remove_nats_in_journal(sbi);
2998 up_write(&nm_i->nat_tree_lock);
2999 }
3000
Jaegeuk Kim317c4f52020-11-06 13:22:05 -08003001 if (!nm_i->nat_cnt[DIRTY_NAT])
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07003002 return 0;
Jaegeuk Kima5131192016-01-02 09:19:41 -08003003
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003004 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08003005
Chao Yuaec71382014-06-24 09:18:20 +08003006 /*
3007 * if there are no enough space in journal to store dirty nat
3008 * entries, remove all entries from journal and merge them
3009 * into nat entry set.
3010 */
Jaegeuk Kim900f7362017-02-27 21:28:53 -08003011 if (enabled_nat_bits(sbi, cpc) ||
Jaegeuk Kim317c4f52020-11-06 13:22:05 -08003012 !__has_cursum_space(journal,
3013 nm_i->nat_cnt[DIRTY_NAT], NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08003014 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003015
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07003016 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003017 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07003018 unsigned idx;
Yi Zhuangf10ea3c2021-04-06 09:47:35 +08003019
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07003020 set_idx = setvec[found - 1]->set + 1;
3021 for (idx = 0; idx < found; idx++)
3022 __adjust_nat_entry_set(setvec[idx], &sets,
Chao Yudfc08a12016-02-14 18:50:40 +08003023 MAX_NAT_JENTRIES(journal));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003024 }
Chao Yuaec71382014-06-24 09:18:20 +08003025
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07003026 /* flush dirty nats in nat entry set */
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07003027 list_for_each_entry_safe(set, tmp, &sets, set_list) {
3028 err = __flush_nat_entry_set(sbi, set, cpc);
3029 if (err)
3030 break;
3031 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07003032
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003033 up_write(&nm_i->nat_tree_lock);
Yunlei He59c90812017-03-13 20:22:18 +08003034 /* Allow dirty nats by node block allocation in write_begin */
Jaegeuk Kimedc55aa2018-09-17 17:36:06 -07003035
3036 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003037}
3038
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003039static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
3040{
3041 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3042 struct f2fs_nm_info *nm_i = NM_I(sbi);
3043 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
3044 unsigned int i;
3045 __u64 cp_ver = cur_cp_version(ckpt);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003046 block_t nat_bits_addr;
3047
3048 if (!enabled_nat_bits(sbi, NULL))
3049 return 0;
3050
Chao Yudf033ca2018-03-20 23:08:29 +08003051 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
Eric Biggers0b6d4ca2020-06-04 21:57:48 -07003052 nm_i->nat_bits = f2fs_kvzalloc(sbi,
Chao Yuacbf0542017-11-30 19:28:17 +08003053 nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003054 if (!nm_i->nat_bits)
3055 return -ENOMEM;
3056
3057 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
3058 nm_i->nat_bits_blocks;
3059 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
Chao Yu77357302018-07-17 00:02:17 +08003060 struct page *page;
3061
3062 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
Chao Yu3b30eb12018-10-04 11:15:20 +08003063 if (IS_ERR(page))
Chao Yu77357302018-07-17 00:02:17 +08003064 return PTR_ERR(page);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003065
3066 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
3067 page_address(page), F2FS_BLKSIZE);
3068 f2fs_put_page(page, 1);
3069 }
3070
Kinglong Meeced2c7e2017-02-25 19:53:39 +08003071 cp_ver |= (cur_cp_crc(ckpt) << 32);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003072 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
3073 disable_nat_bits(sbi, true);
3074 return 0;
3075 }
3076
3077 nm_i->full_nat_bits = nm_i->nat_bits + 8;
3078 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
3079
Joe Perchesdcbb4c12019-06-18 17:48:42 +08003080 f2fs_notice(sbi, "Found nat_bits in checkpoint");
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003081 return 0;
3082}
3083
Hou Pengyangbd80a4b2017-05-17 02:48:48 +00003084static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
Chao Yu7041d5d2017-03-08 20:07:49 +08003085{
3086 struct f2fs_nm_info *nm_i = NM_I(sbi);
3087 unsigned int i = 0;
3088 nid_t nid, last_nid;
3089
3090 if (!enabled_nat_bits(sbi, NULL))
3091 return;
3092
3093 for (i = 0; i < nm_i->nat_blocks; i++) {
3094 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
3095 if (i >= nm_i->nat_blocks)
3096 break;
3097
3098 __set_bit_le(i, nm_i->nat_block_bitmap);
3099
3100 nid = i * NAT_ENTRY_PER_BLOCK;
Fan Lif6986ed2017-11-02 11:02:52 +08003101 last_nid = nid + NAT_ENTRY_PER_BLOCK;
Chao Yu7041d5d2017-03-08 20:07:49 +08003102
Chao Yu346fe752017-03-13 20:10:41 +08003103 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08003104 for (; nid < last_nid; nid++)
Chao Yu346fe752017-03-13 20:10:41 +08003105 update_free_nid_bitmap(sbi, nid, true, true);
3106 spin_unlock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08003107 }
3108
3109 for (i = 0; i < nm_i->nat_blocks; i++) {
3110 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
3111 if (i >= nm_i->nat_blocks)
3112 break;
3113
3114 __set_bit_le(i, nm_i->nat_block_bitmap);
3115 }
3116}
3117
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003118static int init_node_manager(struct f2fs_sb_info *sbi)
3119{
3120 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
3121 struct f2fs_nm_info *nm_i = NM_I(sbi);
3122 unsigned char *version_bitmap;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003123 unsigned int nat_segs;
3124 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003125
3126 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
3127
3128 /* segment_count_nat includes pair segment so divide to 2. */
3129 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003130 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
3131 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09003132
Jaegeuk Kimb63da152014-02-17 12:44:20 +09003133 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yu04d47e62016-11-17 20:53:11 +08003134 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
Chao Yu27cae0b2019-08-05 18:27:25 +08003135 F2FS_RESERVED_NODE_NUM;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003136 nm_i->nid_cnt[FREE_NID] = 0;
3137 nm_i->nid_cnt[PREALLOC_NID] = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09003138 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Chao Yuea1a29a02015-10-12 17:08:48 +08003139 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
Chao Yu2304cb02016-01-18 18:32:58 +08003140 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003141
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09003142 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003143 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08003144 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
3145 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003146 INIT_LIST_HEAD(&nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +08003147 spin_lock_init(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003148
3149 mutex_init(&nm_i->build_lock);
Chao Yub8559dc2016-10-12 19:28:29 +08003150 spin_lock_init(&nm_i->nid_list_lock);
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003151 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003152
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003153 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02003154 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003155 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02003156 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
3157 GFP_KERNEL);
3158 if (!nm_i->nat_bitmap)
3159 return -ENOMEM;
Chao Yu599a09b2017-01-07 18:52:01 +08003160
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003161 err = __get_nat_bitmaps(sbi);
3162 if (err)
3163 return err;
3164
Chao Yu599a09b2017-01-07 18:52:01 +08003165#ifdef CONFIG_F2FS_CHECK_FS
3166 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
3167 GFP_KERNEL);
3168 if (!nm_i->nat_bitmap_mir)
3169 return -ENOMEM;
3170#endif
3171
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003172 return 0;
3173}
3174
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08003175static int init_free_nid_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08003176{
3177 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003178 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08003179
Kees Cook026f0502018-06-12 14:28:23 -07003180 nm_i->free_nid_bitmap =
Eric Biggers0b6d4ca2020-06-04 21:57:48 -07003181 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned char *),
3182 nm_i->nat_blocks),
3183 GFP_KERNEL);
Chao Yu4ac91242017-02-23 10:53:49 +08003184 if (!nm_i->free_nid_bitmap)
3185 return -ENOMEM;
3186
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003187 for (i = 0; i < nm_i->nat_blocks; i++) {
3188 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
Yunlei Hee15d54d2018-06-27 14:46:21 +08003189 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
Yunlei He68c43a22018-07-02 10:40:19 +08003190 if (!nm_i->free_nid_bitmap[i])
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003191 return -ENOMEM;
3192 }
3193
Chao Yu628b3d12017-11-30 19:28:18 +08003194 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
Chao Yu4ac91242017-02-23 10:53:49 +08003195 GFP_KERNEL);
3196 if (!nm_i->nat_block_bitmap)
3197 return -ENOMEM;
Chao Yu586d1492017-03-01 17:09:07 +08003198
Kees Cook9d2a7892018-06-12 14:28:35 -07003199 nm_i->free_nid_count =
3200 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
3201 nm_i->nat_blocks),
3202 GFP_KERNEL);
Chao Yu586d1492017-03-01 17:09:07 +08003203 if (!nm_i->free_nid_count)
3204 return -ENOMEM;
Chao Yu4ac91242017-02-23 10:53:49 +08003205 return 0;
3206}
3207
Chao Yu4d57b862018-05-30 00:20:41 +08003208int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003209{
3210 int err;
3211
Chao Yuacbf0542017-11-30 19:28:17 +08003212 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3213 GFP_KERNEL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003214 if (!sbi->nm_info)
3215 return -ENOMEM;
3216
3217 err = init_node_manager(sbi);
3218 if (err)
3219 return err;
3220
Chao Yu4ac91242017-02-23 10:53:49 +08003221 err = init_free_nid_cache(sbi);
3222 if (err)
3223 return err;
3224
Chao Yu7041d5d2017-03-08 20:07:49 +08003225 /* load free nid status from nat_bits table */
3226 load_free_nid_bitmap(sbi);
3227
Chao Yue2374012018-06-15 14:45:57 +08003228 return f2fs_build_free_nids(sbi, true, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003229}
3230
Chao Yu4d57b862018-05-30 00:20:41 +08003231void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003232{
3233 struct f2fs_nm_info *nm_i = NM_I(sbi);
3234 struct free_nid *i, *next_i;
3235 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003236 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003237 nid_t nid = 0;
3238 unsigned int found;
3239
3240 if (!nm_i)
3241 return;
3242
3243 /* destroy free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08003244 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003245 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Fan Lia0761f62017-10-28 19:03:37 +08003246 __remove_free_nid(sbi, i, FREE_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08003247 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08003248 kmem_cache_free(free_nid_slab, i);
Chao Yub8559dc2016-10-12 19:28:29 +08003249 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003250 }
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003251 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3252 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3253 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
Chao Yub8559dc2016-10-12 19:28:29 +08003254 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003255
3256 /* destroy nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003257 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003258 while ((found = __gang_lookup_nat_cache(nm_i,
3259 nid, NATVEC_SIZE, natvec))) {
3260 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003261
Gu Zhengb6ce3912014-03-07 18:43:24 +08003262 nid = nat_get_nid(natvec[found - 1]) + 1;
Chao Yu22969152018-08-05 23:08:59 +08003263 for (idx = 0; idx < found; idx++) {
3264 spin_lock(&nm_i->nat_list_lock);
3265 list_del(&natvec[idx]->list);
3266 spin_unlock(&nm_i->nat_list_lock);
3267
Gu Zhengb6ce3912014-03-07 18:43:24 +08003268 __del_from_nat_cache(nm_i, natvec[idx]);
Chao Yu22969152018-08-05 23:08:59 +08003269 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003270 }
Jaegeuk Kim317c4f52020-11-06 13:22:05 -08003271 f2fs_bug_on(sbi, nm_i->nat_cnt[TOTAL_NAT]);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003272
3273 /* destroy nat set cache */
3274 nid = 0;
3275 while ((found = __gang_lookup_nat_set(nm_i,
3276 nid, SETVEC_SIZE, setvec))) {
3277 unsigned idx;
3278
3279 nid = setvec[found - 1]->set + 1;
3280 for (idx = 0; idx < found; idx++) {
3281 /* entry_cnt is not zero, when cp_error was occurred */
3282 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3283 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3284 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3285 }
3286 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003287 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003288
Chao Yu4ac91242017-02-23 10:53:49 +08003289 kvfree(nm_i->nat_block_bitmap);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003290 if (nm_i->free_nid_bitmap) {
3291 int i;
3292
3293 for (i = 0; i < nm_i->nat_blocks; i++)
3294 kvfree(nm_i->free_nid_bitmap[i]);
Jaegeuk Kim52225952018-12-13 18:38:33 -08003295 kvfree(nm_i->free_nid_bitmap);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003296 }
Chao Yu586d1492017-03-01 17:09:07 +08003297 kvfree(nm_i->free_nid_count);
Chao Yu4ac91242017-02-23 10:53:49 +08003298
Jaegeuk Kim52225952018-12-13 18:38:33 -08003299 kvfree(nm_i->nat_bitmap);
3300 kvfree(nm_i->nat_bits);
Chao Yu599a09b2017-01-07 18:52:01 +08003301#ifdef CONFIG_F2FS_CHECK_FS
Jaegeuk Kim52225952018-12-13 18:38:33 -08003302 kvfree(nm_i->nat_bitmap_mir);
Chao Yu599a09b2017-01-07 18:52:01 +08003303#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003304 sbi->nm_info = NULL;
Chao Yuc8eb7022020-09-14 16:47:00 +08003305 kfree(nm_i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003306}
3307
Chao Yu4d57b862018-05-30 00:20:41 +08003308int __init f2fs_create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003309{
Chao Yu98510002020-02-17 17:46:20 +08003310 nat_entry_slab = f2fs_kmem_cache_create("f2fs_nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003311 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003312 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08003313 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003314
Chao Yu98510002020-02-17 17:46:20 +08003315 free_nid_slab = f2fs_kmem_cache_create("f2fs_free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08003316 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08003317 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003318 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08003319
Chao Yu98510002020-02-17 17:46:20 +08003320 nat_entry_set_slab = f2fs_kmem_cache_create("f2fs_nat_entry_set",
Chao Yuaec71382014-06-24 09:18:20 +08003321 sizeof(struct nat_entry_set));
3322 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003323 goto destroy_free_nid;
Chao Yu50fa53e2018-08-02 23:03:19 +08003324
Chao Yu98510002020-02-17 17:46:20 +08003325 fsync_node_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_node_entry",
Chao Yu50fa53e2018-08-02 23:03:19 +08003326 sizeof(struct fsync_node_entry));
3327 if (!fsync_node_entry_slab)
3328 goto destroy_nat_entry_set;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003329 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08003330
Chao Yu50fa53e2018-08-02 23:03:19 +08003331destroy_nat_entry_set:
3332 kmem_cache_destroy(nat_entry_set_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003333destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08003334 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003335destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08003336 kmem_cache_destroy(nat_entry_slab);
3337fail:
3338 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003339}
3340
Chao Yu4d57b862018-05-30 00:20:41 +08003341void f2fs_destroy_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003342{
Chao Yu50fa53e2018-08-02 23:03:19 +08003343 kmem_cache_destroy(fsync_node_entry_slab);
Chao Yuaec71382014-06-24 09:18:20 +08003344 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003345 kmem_cache_destroy(free_nid_slab);
3346 kmem_cache_destroy(nat_entry_slab);
3347}