blob: fa2381c0bc475ecada0fbdeaafc5a86ef06bcda4 [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"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080020#include "trace.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090021#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090022
Chao Yu4d57b862018-05-30 00:20:41 +080023#define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock)
Gu Zhengf978f5a2014-02-21 18:08:29 +080024
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090025static struct kmem_cache *nat_entry_slab;
26static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080027static struct kmem_cache *nat_entry_set_slab;
Chao Yu50fa53e2018-08-02 23:03:19 +080028static struct kmem_cache *fsync_node_entry_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090029
Jaegeuk Kima4f843b2018-04-23 23:02:31 -060030/*
31 * Check whether the given nid is within node id range.
32 */
Chao Yu4d57b862018-05-30 00:20:41 +080033int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kima4f843b2018-04-23 23:02:31 -060034{
35 if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
36 set_sbi_flag(sbi, SBI_NEED_FSCK);
37 f2fs_msg(sbi->sb, KERN_WARNING,
38 "%s: out-of-range nid=%x, run fsck to fix.",
39 __func__, nid);
40 return -EINVAL;
41 }
42 return 0;
43}
44
Chao Yu4d57b862018-05-30 00:20:41 +080045bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090046{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090047 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090048 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080049 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090050 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090051 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090052
53 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080054
55 /* only uses low memory */
56 avail_ram = val.totalram - val.totalhigh;
57
Chao Yu429511c2015-02-05 17:54:31 +080058 /*
59 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
60 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090061 if (type == FREE_NIDS) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +080062 mem_size = (nm_i->nid_cnt[FREE_NID] *
Chao Yub8559dc2016-10-12 19:28:29 +080063 sizeof(struct free_nid)) >> PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080064 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090065 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080066 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030067 PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080068 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kime589c2c2016-06-02 15:24:24 -070069 if (excess_cached_nats(sbi))
70 res = false;
Jaegeuk Kima1257022015-10-08 10:40:07 -070071 } else if (type == DIRTY_DENTS) {
72 if (sbi->sb->s_bdi->wb.dirty_exceeded)
73 return false;
74 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
75 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080076 } else if (type == INO_ENTRIES) {
77 int i;
78
Chao Yu39d787b2017-09-29 13:59:38 +080079 for (i = 0; i < MAX_INO_ENTRY; i++)
Kinglong Mee8f73cbb2017-03-18 09:26:13 +080080 mem_size += sbi->im[i].ino_num *
81 sizeof(struct ino_entry);
82 mem_size >>= PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080083 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080084 } else if (type == EXTENT_CACHE) {
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080085 mem_size = (atomic_read(&sbi->total_ext_tree) *
86 sizeof(struct extent_tree) +
Chao Yu429511c2015-02-05 17:54:31 +080087 atomic_read(&sbi->total_ext_node) *
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030088 sizeof(struct extent_node)) >> PAGE_SHIFT;
Chao Yu429511c2015-02-05 17:54:31 +080089 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -070090 } else if (type == INMEM_PAGES) {
91 /* it allows 20% / total_ram for inmemory pages */
92 mem_size = get_pages(sbi, F2FS_INMEM_PAGES);
93 res = mem_size < (val.totalram / 5);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080094 } else {
Jaegeuk Kim1663cae2016-01-09 16:14:08 -080095 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
96 return true;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090097 }
98 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090099}
100
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900101static void clear_node_page_dirty(struct page *page)
102{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900103 if (PageDirty(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800104 f2fs_clear_radix_tree_dirty_tag(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900105 clear_page_dirty_for_io(page);
Chao Yuaec2f722018-05-26 18:03:35 +0800106 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900107 }
108 ClearPageUptodate(page);
109}
110
111static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
112{
Chao Yu80551d12018-07-17 20:41:46 +0800113 return f2fs_get_meta_page_nofail(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900114}
115
116static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
117{
118 struct page *src_page;
119 struct page *dst_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900120 pgoff_t dst_off;
121 void *src_addr;
122 void *dst_addr;
123 struct f2fs_nm_info *nm_i = NM_I(sbi);
124
Chao Yu80551d12018-07-17 20:41:46 +0800125 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900126
127 /* get current nat block page with lock */
Chao Yu80551d12018-07-17 20:41:46 +0800128 src_page = get_current_nat_page(sbi, nid);
Chao Yu4d57b862018-05-30 00:20:41 +0800129 dst_page = f2fs_grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700130 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900131
132 src_addr = page_address(src_page);
133 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300134 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900135 set_page_dirty(dst_page);
136 f2fs_put_page(src_page, 1);
137
138 set_to_next_nat(nm_i, nid);
139
140 return dst_page;
141}
142
Yunlei He12f9ef32017-11-10 13:36:51 -0800143static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail)
144{
145 struct nat_entry *new;
146
147 if (no_fail)
Chao Yu2882d342018-01-25 18:57:25 +0800148 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800149 else
Chao Yu2882d342018-01-25 18:57:25 +0800150 new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800151 if (new) {
152 nat_set_nid(new, nid);
153 nat_reset_flag(new);
154 }
155 return new;
156}
157
158static void __free_nat_entry(struct nat_entry *e)
159{
160 kmem_cache_free(nat_entry_slab, e);
161}
162
163/* must be locked by nat_tree_lock */
164static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
165 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
166{
167 if (no_fail)
168 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
169 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
170 return NULL;
171
172 if (raw_ne)
173 node_info_from_raw_nat(&ne->ni, raw_ne);
Chao Yu22969152018-08-05 23:08:59 +0800174
175 spin_lock(&nm_i->nat_list_lock);
Yunlei He12f9ef32017-11-10 13:36:51 -0800176 list_add_tail(&ne->list, &nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +0800177 spin_unlock(&nm_i->nat_list_lock);
178
Yunlei He12f9ef32017-11-10 13:36:51 -0800179 nm_i->nat_cnt++;
180 return ne;
181}
182
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900183static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
184{
Chao Yu22969152018-08-05 23:08:59 +0800185 struct nat_entry *ne;
186
187 ne = radix_tree_lookup(&nm_i->nat_root, n);
188
189 /* for recent accessed nat entry, move it to tail of lru list */
190 if (ne && !get_nat_flag(ne, IS_DIRTY)) {
191 spin_lock(&nm_i->nat_list_lock);
192 if (!list_empty(&ne->list))
193 list_move_tail(&ne->list, &nm_i->nat_entries);
194 spin_unlock(&nm_i->nat_list_lock);
195 }
196
197 return ne;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900198}
199
200static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
201 nid_t start, unsigned int nr, struct nat_entry **ep)
202{
203 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
204}
205
206static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
207{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900208 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
209 nm_i->nat_cnt--;
Yunlei He12f9ef32017-11-10 13:36:51 -0800210 __free_nat_entry(e);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900211}
212
Chao Yu780de472018-03-20 23:08:30 +0800213static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
214 struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700215{
216 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
217 struct nat_entry_set *head;
218
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700219 head = radix_tree_lookup(&nm_i->nat_set_root, set);
220 if (!head) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700221 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700222
223 INIT_LIST_HEAD(&head->entry_list);
224 INIT_LIST_HEAD(&head->set_list);
225 head->set = set;
226 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800227 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700228 }
Chao Yu780de472018-03-20 23:08:30 +0800229 return head;
230}
231
232static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
233 struct nat_entry *ne)
234{
235 struct nat_entry_set *head;
236 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
237
238 if (!new_ne)
239 head = __grab_nat_entry_set(nm_i, ne);
240
241 /*
242 * update entry_cnt in below condition:
243 * 1. update NEW_ADDR to valid block address;
244 * 2. update old block address to new one;
245 */
246 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
247 !get_nat_flag(ne, IS_DIRTY)))
248 head->entry_cnt++;
249
250 set_nat_flag(ne, IS_PREALLOC, new_ne);
Chao Yufebeca62017-06-05 18:29:08 +0800251
252 if (get_nat_flag(ne, IS_DIRTY))
253 goto refresh_list;
254
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700255 nm_i->dirty_nat_cnt++;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700256 set_nat_flag(ne, IS_DIRTY, true);
Chao Yufebeca62017-06-05 18:29:08 +0800257refresh_list:
Chao Yu22969152018-08-05 23:08:59 +0800258 spin_lock(&nm_i->nat_list_lock);
Chao Yu780de472018-03-20 23:08:30 +0800259 if (new_ne)
Chao Yufebeca62017-06-05 18:29:08 +0800260 list_del_init(&ne->list);
261 else
262 list_move_tail(&ne->list, &head->entry_list);
Chao Yu22969152018-08-05 23:08:59 +0800263 spin_unlock(&nm_i->nat_list_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700264}
265
266static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
Kinglong Mee0b28b712017-02-28 21:34:47 +0800267 struct nat_entry_set *set, struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700268{
Chao Yu22969152018-08-05 23:08:59 +0800269 spin_lock(&nm_i->nat_list_lock);
Kinglong Mee0b28b712017-02-28 21:34:47 +0800270 list_move_tail(&ne->list, &nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +0800271 spin_unlock(&nm_i->nat_list_lock);
272
Kinglong Mee0b28b712017-02-28 21:34:47 +0800273 set_nat_flag(ne, IS_DIRTY, false);
274 set->entry_cnt--;
275 nm_i->dirty_nat_cnt--;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700276}
277
278static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
279 nid_t start, unsigned int nr, struct nat_entry_set **ep)
280{
281 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
282 start, nr);
283}
284
Chao Yu50fa53e2018-08-02 23:03:19 +0800285bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
286{
287 return NODE_MAPPING(sbi) == page->mapping &&
288 IS_DNODE(page) && is_cold_node(page);
289}
290
291void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
292{
293 spin_lock_init(&sbi->fsync_node_lock);
294 INIT_LIST_HEAD(&sbi->fsync_node_list);
295 sbi->fsync_seg_id = 0;
296 sbi->fsync_node_num = 0;
297}
298
299static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
300 struct page *page)
301{
302 struct fsync_node_entry *fn;
303 unsigned long flags;
304 unsigned int seq_id;
305
306 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS);
307
308 get_page(page);
309 fn->page = page;
310 INIT_LIST_HEAD(&fn->list);
311
312 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
313 list_add_tail(&fn->list, &sbi->fsync_node_list);
314 fn->seq_id = sbi->fsync_seg_id++;
315 seq_id = fn->seq_id;
316 sbi->fsync_node_num++;
317 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
318
319 return seq_id;
320}
321
322void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
323{
324 struct fsync_node_entry *fn;
325 unsigned long flags;
326
327 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
328 list_for_each_entry(fn, &sbi->fsync_node_list, list) {
329 if (fn->page == page) {
330 list_del(&fn->list);
331 sbi->fsync_node_num--;
332 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
333 kmem_cache_free(fsync_node_entry_slab, fn);
334 put_page(page);
335 return;
336 }
337 }
338 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
339 f2fs_bug_on(sbi, 1);
340}
341
342void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
343{
344 unsigned long flags;
345
346 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
347 sbi->fsync_seg_id = 0;
348 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
349}
350
Chao Yu4d57b862018-05-30 00:20:41 +0800351int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700352{
353 struct f2fs_nm_info *nm_i = NM_I(sbi);
354 struct nat_entry *e;
355 bool need = false;
356
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700357 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700358 e = __lookup_nat_cache(nm_i, nid);
359 if (e) {
360 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
361 !get_nat_flag(e, HAS_FSYNCED_INODE))
362 need = true;
363 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700364 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700365 return need;
366}
367
Chao Yu4d57b862018-05-30 00:20:41 +0800368bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900369{
370 struct f2fs_nm_info *nm_i = NM_I(sbi);
371 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700372 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900373
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700374 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900375 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700376 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700377 is_cp = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700378 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900379 return is_cp;
380}
381
Chao Yu4d57b862018-05-30 00:20:41 +0800382bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900383{
384 struct f2fs_nm_info *nm_i = NM_I(sbi);
385 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700386 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900387
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700388 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700389 e = __lookup_nat_cache(nm_i, ino);
390 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
391 (get_nat_flag(e, IS_CHECKPOINTED) ||
392 get_nat_flag(e, HAS_FSYNCED_INODE)))
393 need_update = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700394 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700395 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900396}
397
Yunlei He12f9ef32017-11-10 13:36:51 -0800398/* must be locked by nat_tree_lock */
Chao Yu1515aef2016-02-19 18:12:28 +0800399static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900400 struct f2fs_nat_entry *ne)
401{
Chao Yu1515aef2016-02-19 18:12:28 +0800402 struct f2fs_nm_info *nm_i = NM_I(sbi);
Yunlei He12f9ef32017-11-10 13:36:51 -0800403 struct nat_entry *new, *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800404
Yunlei He12f9ef32017-11-10 13:36:51 -0800405 new = __alloc_nat_entry(nid, false);
406 if (!new)
407 return;
408
409 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900410 e = __lookup_nat_cache(nm_i, nid);
Yunlei He12f9ef32017-11-10 13:36:51 -0800411 if (!e)
412 e = __init_nat_entry(nm_i, new, ne, false);
413 else
Eric Biggers0c0b4712016-10-11 10:36:12 -0700414 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
415 nat_get_blkaddr(e) !=
416 le32_to_cpu(ne->block_addr) ||
Chao Yu1515aef2016-02-19 18:12:28 +0800417 nat_get_version(e) != ne->version);
Yunlei He12f9ef32017-11-10 13:36:51 -0800418 up_write(&nm_i->nat_tree_lock);
419 if (e != new)
420 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900421}
422
423static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900424 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900425{
426 struct f2fs_nm_info *nm_i = NM_I(sbi);
427 struct nat_entry *e;
Yunlei He12f9ef32017-11-10 13:36:51 -0800428 struct nat_entry *new = __alloc_nat_entry(ni->nid, true);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800429
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700430 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900431 e = __lookup_nat_cache(nm_i, ni->nid);
432 if (!e) {
Yunlei He12f9ef32017-11-10 13:36:51 -0800433 e = __init_nat_entry(nm_i, new, NULL, true);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800434 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700435 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900436 } else if (new_blkaddr == NEW_ADDR) {
437 /*
438 * when nid is reallocated,
439 * previous nat entry can be remained in nat cache.
440 * So, reinitialize it with new information.
441 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800442 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700443 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900444 }
Yunlei He12f9ef32017-11-10 13:36:51 -0800445 /* let's free early to reduce memory consumption */
446 if (e != new)
447 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900448
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900449 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700450 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
451 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900452 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700453 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900454 new_blkaddr == NEW_ADDR);
Chao Yue1da7872018-06-05 17:44:11 +0800455 f2fs_bug_on(sbi, is_valid_data_blkaddr(sbi, nat_get_blkaddr(e)) &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900456 new_blkaddr == NEW_ADDR);
457
arter97e1c42042014-08-06 23:22:50 +0900458 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900459 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
460 unsigned char version = nat_get_version(e);
461 nat_set_version(e, inc_node_version(version));
462 }
463
464 /* change address */
465 nat_set_blkaddr(e, new_blkaddr);
Chao Yue1da7872018-06-05 17:44:11 +0800466 if (!is_valid_data_blkaddr(sbi, new_blkaddr))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700467 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900468 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900469
470 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800471 if (ni->nid != ni->ino)
472 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700473 if (e) {
474 if (fsync_done && ni->nid == ni->ino)
475 set_nat_flag(e, HAS_FSYNCED_INODE, true);
476 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
477 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700478 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900479}
480
Chao Yu4d57b862018-05-30 00:20:41 +0800481int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900482{
483 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700484 int nr = nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900485
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700486 if (!down_write_trylock(&nm_i->nat_tree_lock))
487 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900488
Chao Yu22969152018-08-05 23:08:59 +0800489 spin_lock(&nm_i->nat_list_lock);
490 while (nr_shrink) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900491 struct nat_entry *ne;
Chao Yu22969152018-08-05 23:08:59 +0800492
493 if (list_empty(&nm_i->nat_entries))
494 break;
495
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900496 ne = list_first_entry(&nm_i->nat_entries,
497 struct nat_entry, list);
Chao Yu22969152018-08-05 23:08:59 +0800498 list_del(&ne->list);
499 spin_unlock(&nm_i->nat_list_lock);
500
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900501 __del_from_nat_cache(nm_i, ne);
502 nr_shrink--;
Chao Yu22969152018-08-05 23:08:59 +0800503
504 spin_lock(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900505 }
Chao Yu22969152018-08-05 23:08:59 +0800506 spin_unlock(&nm_i->nat_list_lock);
507
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700508 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700509 return nr - nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900510}
511
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900512/*
arter97e1c42042014-08-06 23:22:50 +0900513 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900514 */
Chao Yu77357302018-07-17 00:02:17 +0800515int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
Chao Yu4d57b862018-05-30 00:20:41 +0800516 struct node_info *ni)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900517{
518 struct f2fs_nm_info *nm_i = NM_I(sbi);
519 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +0800520 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900521 nid_t start_nid = START_NID(nid);
522 struct f2fs_nat_block *nat_blk;
523 struct page *page = NULL;
524 struct f2fs_nat_entry ne;
525 struct nat_entry *e;
Yunlei He66a82d12017-04-22 18:06:26 +0800526 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900527 int i;
528
529 ni->nid = nid;
530
531 /* Check nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700532 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900533 e = __lookup_nat_cache(nm_i, nid);
534 if (e) {
535 ni->ino = nat_get_ino(e);
536 ni->blk_addr = nat_get_blkaddr(e);
537 ni->version = nat_get_version(e);
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700538 up_read(&nm_i->nat_tree_lock);
Chao Yu77357302018-07-17 00:02:17 +0800539 return 0;
Chao Yu1515aef2016-02-19 18:12:28 +0800540 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900541
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800542 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
543
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900544 /* Check current segment summary */
Chao Yub7ad7512016-02-19 18:08:46 +0800545 down_read(&curseg->journal_rwsem);
Chao Yu4d57b862018-05-30 00:20:41 +0800546 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900547 if (i >= 0) {
Chao Yudfc08a12016-02-14 18:50:40 +0800548 ne = nat_in_journal(journal, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900549 node_info_from_raw_nat(ni, &ne);
550 }
Chao Yub7ad7512016-02-19 18:08:46 +0800551 up_read(&curseg->journal_rwsem);
Yunlei He66a82d12017-04-22 18:06:26 +0800552 if (i >= 0) {
553 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900554 goto cache;
Yunlei He66a82d12017-04-22 18:06:26 +0800555 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900556
557 /* Fill node_info from nat page */
Yunlei He66a82d12017-04-22 18:06:26 +0800558 index = current_nat_addr(sbi, nid);
559 up_read(&nm_i->nat_tree_lock);
560
Chao Yu4d57b862018-05-30 00:20:41 +0800561 page = f2fs_get_meta_page(sbi, index);
Chao Yu77357302018-07-17 00:02:17 +0800562 if (IS_ERR(page))
563 return PTR_ERR(page);
564
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900565 nat_blk = (struct f2fs_nat_block *)page_address(page);
566 ne = nat_blk->entries[nid - start_nid];
567 node_info_from_raw_nat(ni, &ne);
568 f2fs_put_page(page, 1);
569cache:
570 /* cache nat entry */
Chao Yu1515aef2016-02-19 18:12:28 +0800571 cache_nat_entry(sbi, nid, &ne);
Chao Yu77357302018-07-17 00:02:17 +0800572 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900573}
574
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700575/*
576 * readahead MAX_RA_NODE number of node pages.
577 */
Chao Yu4d57b862018-05-30 00:20:41 +0800578static void f2fs_ra_node_pages(struct page *parent, int start, int n)
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700579{
580 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
581 struct blk_plug plug;
582 int i, end;
583 nid_t nid;
584
585 blk_start_plug(&plug);
586
587 /* Then, try readahead for siblings of the desired node */
588 end = start + n;
589 end = min(end, NIDS_PER_BLOCK);
590 for (i = start; i < end; i++) {
591 nid = get_nid(parent, i, false);
Chao Yu4d57b862018-05-30 00:20:41 +0800592 f2fs_ra_node_page(sbi, nid);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700593 }
594
595 blk_finish_plug(&plug);
596}
597
Chao Yu4d57b862018-05-30 00:20:41 +0800598pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
Chao Yu3cf45742016-01-26 15:40:44 +0800599{
600 const long direct_index = ADDRS_PER_INODE(dn->inode);
601 const long direct_blks = ADDRS_PER_BLOCK;
602 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
603 unsigned int skipped_unit = ADDRS_PER_BLOCK;
604 int cur_level = dn->cur_level;
605 int max_level = dn->max_level;
606 pgoff_t base = 0;
607
608 if (!dn->max_level)
609 return pgofs + 1;
610
611 while (max_level-- > cur_level)
612 skipped_unit *= NIDS_PER_BLOCK;
613
614 switch (dn->max_level) {
615 case 3:
616 base += 2 * indirect_blks;
617 case 2:
618 base += 2 * direct_blks;
619 case 1:
620 base += direct_index;
621 break;
622 default:
623 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
624 }
625
626 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
627}
628
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900629/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900630 * The maximum depth is four.
631 * Offset[0] will have raw inode offset.
632 */
Chao Yu81ca7352016-01-26 15:39:35 +0800633static int get_node_path(struct inode *inode, long block,
Jaegeuk Kimde936532013-08-12 21:08:03 +0900634 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900635{
Chao Yu81ca7352016-01-26 15:39:35 +0800636 const long direct_index = ADDRS_PER_INODE(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900637 const long direct_blks = ADDRS_PER_BLOCK;
638 const long dptrs_per_blk = NIDS_PER_BLOCK;
639 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
640 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
641 int n = 0;
642 int level = 0;
643
644 noffset[0] = 0;
645
646 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900647 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900648 goto got;
649 }
650 block -= direct_index;
651 if (block < direct_blks) {
652 offset[n++] = NODE_DIR1_BLOCK;
653 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900654 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900655 level = 1;
656 goto got;
657 }
658 block -= direct_blks;
659 if (block < direct_blks) {
660 offset[n++] = NODE_DIR2_BLOCK;
661 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900662 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900663 level = 1;
664 goto got;
665 }
666 block -= direct_blks;
667 if (block < indirect_blks) {
668 offset[n++] = NODE_IND1_BLOCK;
669 noffset[n] = 3;
670 offset[n++] = block / direct_blks;
671 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900672 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900673 level = 2;
674 goto got;
675 }
676 block -= indirect_blks;
677 if (block < indirect_blks) {
678 offset[n++] = NODE_IND2_BLOCK;
679 noffset[n] = 4 + dptrs_per_blk;
680 offset[n++] = block / direct_blks;
681 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900682 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900683 level = 2;
684 goto got;
685 }
686 block -= indirect_blks;
687 if (block < dindirect_blks) {
688 offset[n++] = NODE_DIND_BLOCK;
689 noffset[n] = 5 + (dptrs_per_blk * 2);
690 offset[n++] = block / indirect_blks;
691 noffset[n] = 6 + (dptrs_per_blk * 2) +
692 offset[n - 1] * (dptrs_per_blk + 1);
693 offset[n++] = (block / direct_blks) % dptrs_per_blk;
694 noffset[n] = 7 + (dptrs_per_blk * 2) +
695 offset[n - 2] * (dptrs_per_blk + 1) +
696 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900697 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900698 level = 3;
699 goto got;
700 } else {
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700701 return -E2BIG;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900702 }
703got:
704 return level;
705}
706
707/*
708 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800709 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
710 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900711 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900712 */
Chao Yu4d57b862018-05-30 00:20:41 +0800713int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900714{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700715 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800717 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900718 int offset[4];
719 unsigned int noffset[4];
720 nid_t nids[4];
Chao Yu3cf45742016-01-26 15:40:44 +0800721 int level, i = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900722 int err = 0;
723
Chao Yu81ca7352016-01-26 15:39:35 +0800724 level = get_node_path(dn->inode, index, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700725 if (level < 0)
726 return level;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900727
728 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900729 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900730
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900731 if (!npage[0]) {
Chao Yu4d57b862018-05-30 00:20:41 +0800732 npage[0] = f2fs_get_node_page(sbi, nids[0]);
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900733 if (IS_ERR(npage[0]))
734 return PTR_ERR(npage[0]);
735 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800736
737 /* if inline_data is set, should not report any block indices */
738 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800739 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800740 f2fs_put_page(npage[0], 1);
741 goto release_out;
742 }
743
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900744 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900745 if (level != 0)
746 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900747 dn->inode_page = npage[0];
748 dn->inode_page_locked = true;
749
750 /* get indirect or direct nodes */
751 for (i = 1; i <= level; i++) {
752 bool done = false;
753
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900754 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900755 /* alloc new node */
Chao Yu4d57b862018-05-30 00:20:41 +0800756 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900757 err = -ENOSPC;
758 goto release_pages;
759 }
760
761 dn->nid = nids[i];
Chao Yu4d57b862018-05-30 00:20:41 +0800762 npage[i] = f2fs_new_node_page(dn, noffset[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900763 if (IS_ERR(npage[i])) {
Chao Yu4d57b862018-05-30 00:20:41 +0800764 f2fs_alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900765 err = PTR_ERR(npage[i]);
766 goto release_pages;
767 }
768
769 set_nid(parent, offset[i - 1], nids[i], i == 1);
Chao Yu4d57b862018-05-30 00:20:41 +0800770 f2fs_alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900771 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900772 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Chao Yu4d57b862018-05-30 00:20:41 +0800773 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900774 if (IS_ERR(npage[i])) {
775 err = PTR_ERR(npage[i]);
776 goto release_pages;
777 }
778 done = true;
779 }
780 if (i == 1) {
781 dn->inode_page_locked = false;
782 unlock_page(parent);
783 } else {
784 f2fs_put_page(parent, 1);
785 }
786
787 if (!done) {
Chao Yu4d57b862018-05-30 00:20:41 +0800788 npage[i] = f2fs_get_node_page(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900789 if (IS_ERR(npage[i])) {
790 err = PTR_ERR(npage[i]);
791 f2fs_put_page(npage[0], 0);
792 goto release_out;
793 }
794 }
795 if (i < level) {
796 parent = npage[i];
797 nids[i + 1] = get_nid(parent, offset[i], false);
798 }
799 }
800 dn->nid = nids[level];
801 dn->ofs_in_node = offset[level];
802 dn->node_page = npage[level];
Chao Yu7a2af762017-07-19 00:19:06 +0800803 dn->data_blkaddr = datablock_addr(dn->inode,
804 dn->node_page, dn->ofs_in_node);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900805 return 0;
806
807release_pages:
808 f2fs_put_page(parent, 1);
809 if (i > 1)
810 f2fs_put_page(npage[0], 0);
811release_out:
812 dn->inode_page = NULL;
813 dn->node_page = NULL;
Chao Yu3cf45742016-01-26 15:40:44 +0800814 if (err == -ENOENT) {
815 dn->cur_level = i;
816 dn->max_level = level;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700817 dn->ofs_in_node = offset[level];
Chao Yu3cf45742016-01-26 15:40:44 +0800818 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900819 return err;
820}
821
Chao Yu77357302018-07-17 00:02:17 +0800822static int truncate_node(struct dnode_of_data *dn)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900823{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700824 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900825 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800826 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900827
Chao Yu77357302018-07-17 00:02:17 +0800828 err = f2fs_get_node_info(sbi, dn->nid, &ni);
829 if (err)
830 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900831
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900832 /* Deallocate node address */
Chao Yu4d57b862018-05-30 00:20:41 +0800833 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +0800834 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900835 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900836
837 if (dn->nid == dn->inode->i_ino) {
Chao Yu4d57b862018-05-30 00:20:41 +0800838 f2fs_remove_orphan_inode(sbi, dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900839 dec_valid_inode_count(sbi);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700840 f2fs_inode_synced(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900841 }
Chao Yu000519f2017-07-06 01:11:31 +0800842
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900843 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800844 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900845
846 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900847
848 invalidate_mapping_pages(NODE_MAPPING(sbi),
849 dn->node_page->index, dn->node_page->index);
850
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900851 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900852 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Chao Yu77357302018-07-17 00:02:17 +0800853
854 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855}
856
857static int truncate_dnode(struct dnode_of_data *dn)
858{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900859 struct page *page;
Chao Yu77357302018-07-17 00:02:17 +0800860 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900861
862 if (dn->nid == 0)
863 return 1;
864
865 /* get direct node */
Chao Yu4d57b862018-05-30 00:20:41 +0800866 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900867 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
868 return 1;
869 else if (IS_ERR(page))
870 return PTR_ERR(page);
871
872 /* Make dnode_of_data for parameter */
873 dn->node_page = page;
874 dn->ofs_in_node = 0;
Chao Yu4d57b862018-05-30 00:20:41 +0800875 f2fs_truncate_data_blocks(dn);
Chao Yu77357302018-07-17 00:02:17 +0800876 err = truncate_node(dn);
877 if (err)
878 return err;
879
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900880 return 1;
881}
882
883static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
884 int ofs, int depth)
885{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900886 struct dnode_of_data rdn = *dn;
887 struct page *page;
888 struct f2fs_node *rn;
889 nid_t child_nid;
890 unsigned int child_nofs;
891 int freed = 0;
892 int i, ret;
893
894 if (dn->nid == 0)
895 return NIDS_PER_BLOCK + 1;
896
Namjae Jeon51dd6242013-04-20 01:28:52 +0900897 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
898
Chao Yu4d57b862018-05-30 00:20:41 +0800899 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900900 if (IS_ERR(page)) {
901 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900902 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900903 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900904
Chao Yu4d57b862018-05-30 00:20:41 +0800905 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700906
Gu Zheng45590712013-07-15 17:57:38 +0800907 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900908 if (depth < 3) {
909 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
910 child_nid = le32_to_cpu(rn->in.nid[i]);
911 if (child_nid == 0)
912 continue;
913 rdn.nid = child_nid;
914 ret = truncate_dnode(&rdn);
915 if (ret < 0)
916 goto out_err;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800917 if (set_nid(page, i, 0, false))
918 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900919 }
920 } else {
921 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
922 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
923 child_nid = le32_to_cpu(rn->in.nid[i]);
924 if (child_nid == 0) {
925 child_nofs += NIDS_PER_BLOCK + 1;
926 continue;
927 }
928 rdn.nid = child_nid;
929 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
930 if (ret == (NIDS_PER_BLOCK + 1)) {
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800931 if (set_nid(page, i, 0, false))
932 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900933 child_nofs += ret;
934 } else if (ret < 0 && ret != -ENOENT) {
935 goto out_err;
936 }
937 }
938 freed = child_nofs;
939 }
940
941 if (!ofs) {
942 /* remove current indirect node */
943 dn->node_page = page;
Chao Yu77357302018-07-17 00:02:17 +0800944 ret = truncate_node(dn);
945 if (ret)
946 goto out_err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900947 freed++;
948 } else {
949 f2fs_put_page(page, 1);
950 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900951 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900952 return freed;
953
954out_err:
955 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900956 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900957 return ret;
958}
959
960static int truncate_partial_nodes(struct dnode_of_data *dn,
961 struct f2fs_inode *ri, int *offset, int depth)
962{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900963 struct page *pages[2];
964 nid_t nid[3];
965 nid_t child_nid;
966 int err = 0;
967 int i;
968 int idx = depth - 2;
969
970 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
971 if (!nid[0])
972 return 0;
973
974 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800975 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900976 /* reference count'll be increased */
Chao Yu4d57b862018-05-30 00:20:41 +0800977 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900978 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800980 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900981 goto fail;
982 }
983 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
984 }
985
Chao Yu4d57b862018-05-30 00:20:41 +0800986 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700987
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900988 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800989 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900990 child_nid = get_nid(pages[idx], i, false);
991 if (!child_nid)
992 continue;
993 dn->nid = child_nid;
994 err = truncate_dnode(dn);
995 if (err < 0)
996 goto fail;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800997 if (set_nid(pages[idx], i, 0, false))
998 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999 }
1000
shifei10.gea225dca2013-10-29 15:32:34 +08001001 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001002 dn->node_page = pages[idx];
1003 dn->nid = nid[idx];
Chao Yu77357302018-07-17 00:02:17 +08001004 err = truncate_node(dn);
1005 if (err)
1006 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001007 } else {
1008 f2fs_put_page(pages[idx], 1);
1009 }
1010 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +08001011 offset[idx + 1] = 0;
1012 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001013fail:
shifei10.gea225dca2013-10-29 15:32:34 +08001014 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001015 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001016
1017 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1018
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001019 return err;
1020}
1021
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001022/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001023 * All the block addresses of data and nodes should be nullified.
1024 */
Chao Yu4d57b862018-05-30 00:20:41 +08001025int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001026{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001027 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001028 int err = 0, cont = 1;
1029 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +09001030 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001031 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001032 struct dnode_of_data dn;
1033 struct page *page;
1034
Namjae Jeon51dd6242013-04-20 01:28:52 +09001035 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1036
Chao Yu81ca7352016-01-26 15:39:35 +08001037 level = get_node_path(inode, from, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -07001038 if (level < 0)
1039 return level;
Jaegeuk Kimff373552016-03-29 16:13:45 -07001040
Chao Yu4d57b862018-05-30 00:20:41 +08001041 page = f2fs_get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001042 if (IS_ERR(page)) {
1043 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001044 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001045 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001046
1047 set_new_dnode(&dn, inode, page, NULL, 0);
1048 unlock_page(page);
1049
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001050 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001051 switch (level) {
1052 case 0:
1053 case 1:
1054 nofs = noffset[1];
1055 break;
1056 case 2:
1057 nofs = noffset[1];
1058 if (!offset[level - 1])
1059 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001060 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001061 if (err < 0 && err != -ENOENT)
1062 goto fail;
1063 nofs += 1 + NIDS_PER_BLOCK;
1064 break;
1065 case 3:
1066 nofs = 5 + 2 * NIDS_PER_BLOCK;
1067 if (!offset[level - 1])
1068 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001069 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001070 if (err < 0 && err != -ENOENT)
1071 goto fail;
1072 break;
1073 default:
1074 BUG();
1075 }
1076
1077skip_partial:
1078 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001079 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001080 switch (offset[0]) {
1081 case NODE_DIR1_BLOCK:
1082 case NODE_DIR2_BLOCK:
1083 err = truncate_dnode(&dn);
1084 break;
1085
1086 case NODE_IND1_BLOCK:
1087 case NODE_IND2_BLOCK:
1088 err = truncate_nodes(&dn, nofs, offset[1], 2);
1089 break;
1090
1091 case NODE_DIND_BLOCK:
1092 err = truncate_nodes(&dn, nofs, offset[1], 3);
1093 cont = 0;
1094 break;
1095
1096 default:
1097 BUG();
1098 }
1099 if (err < 0 && err != -ENOENT)
1100 goto fail;
1101 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001102 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001103 lock_page(page);
Jaegeuk Kimff373552016-03-29 16:13:45 -07001104 BUG_ON(page->mapping != NODE_MAPPING(sbi));
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001105 f2fs_wait_on_page_writeback(page, NODE, true);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001106 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001107 set_page_dirty(page);
1108 unlock_page(page);
1109 }
1110 offset[1] = 0;
1111 offset[0]++;
1112 nofs += err;
1113 }
1114fail:
1115 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001116 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001117 return err > 0 ? 0 : err;
1118}
1119
Jaegeuk Kim9c77f752017-10-19 11:48:57 -07001120/* caller must lock inode page */
Chao Yu4d57b862018-05-30 00:20:41 +08001121int f2fs_truncate_xattr_node(struct inode *inode)
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001122{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001123 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001124 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1125 struct dnode_of_data dn;
1126 struct page *npage;
Chao Yu77357302018-07-17 00:02:17 +08001127 int err;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001128
1129 if (!nid)
1130 return 0;
1131
Chao Yu4d57b862018-05-30 00:20:41 +08001132 npage = f2fs_get_node_page(sbi, nid);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001133 if (IS_ERR(npage))
1134 return PTR_ERR(npage);
1135
Chao Yu77357302018-07-17 00:02:17 +08001136 set_new_dnode(&dn, inode, NULL, npage, nid);
1137 err = truncate_node(&dn);
1138 if (err) {
1139 f2fs_put_page(npage, 1);
1140 return err;
1141 }
1142
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001143 f2fs_i_xnid_write(inode, 0);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001144
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001145 return 0;
1146}
1147
Jaegeuk Kim39936832012-11-22 16:21:29 +09001148/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001149 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1150 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +09001151 */
Chao Yu4d57b862018-05-30 00:20:41 +08001152int f2fs_remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001153{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001154 struct dnode_of_data dn;
Chao Yu13ec7292015-08-24 17:40:45 +08001155 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001156
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001157 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Chao Yu4d57b862018-05-30 00:20:41 +08001158 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
Chao Yu13ec7292015-08-24 17:40:45 +08001159 if (err)
1160 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001161
Chao Yu4d57b862018-05-30 00:20:41 +08001162 err = f2fs_truncate_xattr_node(inode);
Chao Yu13ec7292015-08-24 17:40:45 +08001163 if (err) {
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001164 f2fs_put_dnode(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +08001165 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001166 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001167
1168 /* remove potential inline_data blocks */
1169 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1170 S_ISLNK(inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +08001171 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001172
arter97e1c42042014-08-06 23:22:50 +09001173 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim8d714f82018-07-31 09:09:01 -07001174 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1175 f2fs_put_dnode(&dn);
1176 return -EIO;
1177 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001178 f2fs_bug_on(F2FS_I_SB(inode),
Chao Yu0eb0ada2017-06-14 23:00:56 +08001179 inode->i_blocks != 0 && inode->i_blocks != 8);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001180
1181 /* will put inode & node pages */
Chao Yu77357302018-07-17 00:02:17 +08001182 err = truncate_node(&dn);
1183 if (err) {
1184 f2fs_put_dnode(&dn);
1185 return err;
1186 }
Chao Yu13ec7292015-08-24 17:40:45 +08001187 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001188}
1189
Chao Yu4d57b862018-05-30 00:20:41 +08001190struct page *f2fs_new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001191{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001192 struct dnode_of_data dn;
1193
1194 /* allocate inode page for new inode */
1195 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +09001196
1197 /* caller should f2fs_put_page(page, 1); */
Chao Yu4d57b862018-05-30 00:20:41 +08001198 return f2fs_new_node_page(&dn, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001199}
1200
Chao Yu4d57b862018-05-30 00:20:41 +08001201struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001202{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001203 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001204 struct node_info new_ni;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001205 struct page *page;
1206 int err;
1207
Jaegeuk Kim91942322016-05-20 10:13:22 -07001208 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001209 return ERR_PTR(-EPERM);
1210
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001211 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001212 if (!page)
1213 return ERR_PTR(-ENOMEM);
1214
Chao Yu0abd6752017-07-09 00:13:07 +08001215 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001216 goto fail;
Chao Yu0abd6752017-07-09 00:13:07 +08001217
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001218#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu77357302018-07-17 00:02:17 +08001219 err = f2fs_get_node_info(sbi, dn->nid, &new_ni);
1220 if (err) {
1221 dec_valid_node_count(sbi, dn->inode, !ofs);
1222 goto fail;
1223 }
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001224 f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
1225#endif
1226 new_ni.nid = dn->nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001227 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001228 new_ni.blk_addr = NULL_ADDR;
1229 new_ni.flag = 0;
1230 new_ni.version = 0;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001231 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001232
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001233 f2fs_wait_on_page_writeback(page, NODE, true);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001234 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Chao Yuc5667572018-03-09 14:24:22 +08001235 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001236 if (!PageUptodate(page))
1237 SetPageUptodate(page);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001238 if (set_page_dirty(page))
1239 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001240
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001241 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001242 f2fs_i_xnid_write(dn->inode, dn->nid);
Jaegeuk Kim479bd732013-08-12 16:04:53 +09001243
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001244 if (ofs == 0)
1245 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001246 return page;
1247
1248fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +09001249 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001250 f2fs_put_page(page, 1);
1251 return ERR_PTR(err);
1252}
1253
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001254/*
1255 * Caller should do after getting the following values.
1256 * 0: f2fs_put_page(page, 0)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001257 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001258 */
Mike Christie04d328d2016-06-05 14:31:55 -05001259static int read_node_page(struct page *page, int op_flags)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001260{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001261 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001262 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001263 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001264 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001265 .type = NODE,
Mike Christie04d328d2016-06-05 14:31:55 -05001266 .op = REQ_OP_READ,
1267 .op_flags = op_flags,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001268 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001269 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001270 };
Chao Yu77357302018-07-17 00:02:17 +08001271 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001272
Weichao Guo54c55c42018-03-09 23:10:21 +08001273 if (PageUptodate(page)) {
1274#ifdef CONFIG_F2FS_CHECK_FS
1275 f2fs_bug_on(sbi, !f2fs_inode_chksum_verify(sbi, page));
1276#endif
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001277 return LOCKED_PAGE;
Weichao Guo54c55c42018-03-09 23:10:21 +08001278 }
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001279
Chao Yu77357302018-07-17 00:02:17 +08001280 err = f2fs_get_node_info(sbi, page->index, &ni);
1281 if (err)
1282 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001283
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001284 if (unlikely(ni.blk_addr == NULL_ADDR) ||
1285 is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001286 ClearPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001287 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001288 }
1289
Chao Yu7a9d7542016-02-22 18:36:38 +08001290 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001291 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001292}
1293
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001294/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001295 * Readahead a node page
1296 */
Chao Yu4d57b862018-05-30 00:20:41 +08001297void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001298{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001299 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001300 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001301
Chao Yue8458722016-01-08 20:13:37 +08001302 if (!nid)
1303 return;
Chao Yu4d57b862018-05-30 00:20:41 +08001304 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001305 return;
Chao Yue8458722016-01-08 20:13:37 +08001306
Fan Li999270d2016-02-29 14:29:51 +08001307 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001308 apage = radix_tree_lookup(&NODE_MAPPING(sbi)->i_pages, nid);
Fan Li999270d2016-02-29 14:29:51 +08001309 rcu_read_unlock();
1310 if (apage)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001311 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001312
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001313 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001314 if (!apage)
1315 return;
1316
Christoph Hellwig70246282016-07-19 11:28:41 +02001317 err = read_node_page(apage, REQ_RAHEAD);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001318 f2fs_put_page(apage, err ? 1 : 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001319}
1320
Jaegeuk Kim17a0ee52016-03-08 09:04:35 -08001321static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
Chao Yu0e022ea2016-01-05 16:52:46 +08001322 struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001323{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001324 struct page *page;
1325 int err;
Jaegeuk Kim4aa69d52015-12-23 14:17:47 -08001326
1327 if (!nid)
1328 return ERR_PTR(-ENOENT);
Chao Yu4d57b862018-05-30 00:20:41 +08001329 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001330 return ERR_PTR(-EINVAL);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001331repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001332 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001333 if (!page)
1334 return ERR_PTR(-ENOMEM);
1335
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001336 err = read_node_page(page, 0);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001337 if (err < 0) {
1338 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001339 return ERR_PTR(err);
Chao Yue1c51b9f2015-12-11 16:08:22 +08001340 } else if (err == LOCKED_PAGE) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001341 err = 0;
Chao Yue1c51b9f2015-12-11 16:08:22 +08001342 goto page_hit;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001343 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001344
Chao Yu0e022ea2016-01-05 16:52:46 +08001345 if (parent)
Chao Yu4d57b862018-05-30 00:20:41 +08001346 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
Chao Yu0e022ea2016-01-05 16:52:46 +08001347
Chao Yue1c51b9f2015-12-11 16:08:22 +08001348 lock_page(page);
1349
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001350 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001351 f2fs_put_page(page, 1);
1352 goto repeat;
1353 }
Chao Yu1563ac72016-07-03 22:05:12 +08001354
Chao Yu1f258ec2017-06-07 11:17:35 +08001355 if (unlikely(!PageUptodate(page))) {
1356 err = -EIO;
Chao Yu1563ac72016-07-03 22:05:12 +08001357 goto out_err;
Chao Yu1f258ec2017-06-07 11:17:35 +08001358 }
Chao Yu704956e2017-07-31 20:19:09 +08001359
1360 if (!f2fs_inode_chksum_verify(sbi, page)) {
1361 err = -EBADMSG;
1362 goto out_err;
1363 }
Chao Yue1c51b9f2015-12-11 16:08:22 +08001364page_hit:
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001365 if(unlikely(nid != nid_of_node(page))) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001366 f2fs_msg(sbi->sb, KERN_WARNING, "inconsistent node block, "
1367 "nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1368 nid, nid_of_node(page), ino_of_node(page),
1369 ofs_of_node(page), cpver_of_node(page),
1370 next_blkaddr_of_node(page));
Chao Yu1f258ec2017-06-07 11:17:35 +08001371 err = -EINVAL;
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001372out_err:
Jaegeuk Kimee605232017-08-31 16:54:51 -07001373 ClearPageUptodate(page);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001374 f2fs_put_page(page, 1);
Chao Yu1f258ec2017-06-07 11:17:35 +08001375 return ERR_PTR(err);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001376 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001377 return page;
1378}
1379
Chao Yu4d57b862018-05-30 00:20:41 +08001380struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
Chao Yu0e022ea2016-01-05 16:52:46 +08001381{
1382 return __get_node_page(sbi, nid, NULL, 0);
1383}
1384
Chao Yu4d57b862018-05-30 00:20:41 +08001385struct page *f2fs_get_node_page_ra(struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001387 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Chao Yu0e022ea2016-01-05 16:52:46 +08001388 nid_t nid = get_nid(parent, start, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001389
Chao Yu0e022ea2016-01-05 16:52:46 +08001390 return __get_node_page(sbi, nid, parent, start);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001391}
1392
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001393static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1394{
1395 struct inode *inode;
1396 struct page *page;
Chao Yu0f3311a2016-05-21 00:11:09 +08001397 int ret;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001398
1399 /* should flush inline_data before evict_inode */
1400 inode = ilookup(sbi->sb, ino);
1401 if (!inode)
1402 return;
1403
Chao Yu01eccef2017-10-28 16:52:30 +08001404 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1405 FGP_LOCK|FGP_NOWAIT, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001406 if (!page)
1407 goto iput_out;
1408
1409 if (!PageUptodate(page))
1410 goto page_out;
1411
1412 if (!PageDirty(page))
1413 goto page_out;
1414
1415 if (!clear_page_dirty_for_io(page))
1416 goto page_out;
1417
Chao Yu0f3311a2016-05-21 00:11:09 +08001418 ret = f2fs_write_inline_data(inode, page);
1419 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08001420 f2fs_remove_dirty_inode(inode);
Chao Yu0f3311a2016-05-21 00:11:09 +08001421 if (ret)
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001422 set_page_dirty(page);
1423page_out:
Jaegeuk Kim4a6de502016-03-30 11:25:31 -07001424 f2fs_put_page(page, 1);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001425iput_out:
1426 iput(inode);
1427}
1428
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001429static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001430{
Jan Kara028a63a2017-11-15 17:34:51 -08001431 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001432 struct pagevec pvec;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001433 struct page *last_page = NULL;
Jan Kara028a63a2017-11-15 17:34:51 -08001434 int nr_pages;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001435
Mel Gorman86679822017-11-15 17:37:52 -08001436 pagevec_init(&pvec);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001437 index = 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001438
Jan Kara028a63a2017-11-15 17:34:51 -08001439 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001440 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001441 int i;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001442
1443 for (i = 0; i < nr_pages; i++) {
1444 struct page *page = pvec.pages[i];
1445
1446 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001447 f2fs_put_page(last_page, 0);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001448 pagevec_release(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001449 return ERR_PTR(-EIO);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001450 }
1451
1452 if (!IS_DNODE(page) || !is_cold_node(page))
1453 continue;
1454 if (ino_of_node(page) != ino)
1455 continue;
1456
1457 lock_page(page);
1458
1459 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1460continue_unlock:
1461 unlock_page(page);
1462 continue;
1463 }
1464 if (ino_of_node(page) != ino)
1465 goto continue_unlock;
1466
1467 if (!PageDirty(page)) {
1468 /* someone wrote it for us */
1469 goto continue_unlock;
1470 }
1471
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001472 if (last_page)
1473 f2fs_put_page(last_page, 0);
1474
1475 get_page(page);
1476 last_page = page;
1477 unlock_page(page);
1478 }
1479 pagevec_release(&pvec);
1480 cond_resched();
1481 }
1482 return last_page;
1483}
1484
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001485static int __write_node_page(struct page *page, bool atomic, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001486 struct writeback_control *wbc, bool do_balance,
Chao Yu50fa53e2018-08-02 23:03:19 +08001487 enum iostat_type io_type, unsigned int *seq_id)
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001488{
1489 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1490 nid_t nid;
1491 struct node_info ni;
1492 struct f2fs_io_info fio = {
1493 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001494 .ino = ino_of_node(page),
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001495 .type = NODE,
1496 .op = REQ_OP_WRITE,
1497 .op_flags = wbc_to_write_flags(wbc),
1498 .page = page,
1499 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001500 .submitted = false,
Chao Yub0af6d42017-08-02 23:21:48 +08001501 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001502 .io_wbc = wbc,
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001503 };
Chao Yu50fa53e2018-08-02 23:03:19 +08001504 unsigned int seq;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001505
1506 trace_f2fs_writepage(page, NODE);
1507
Jaegeuk Kim868de612018-05-04 18:04:22 -07001508 if (unlikely(f2fs_cp_error(sbi)))
1509 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08001510
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001511 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1512 goto redirty_out;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001513
Chao Yufd8c8ca2018-07-25 19:16:21 +08001514 if (wbc->sync_mode == WB_SYNC_NONE &&
1515 IS_DNODE(page) && is_cold_node(page))
1516 goto redirty_out;
1517
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001518 /* get old block addr of this node page */
1519 nid = nid_of_node(page);
1520 f2fs_bug_on(sbi, page->index != nid);
1521
Chao Yu77357302018-07-17 00:02:17 +08001522 if (f2fs_get_node_info(sbi, nid, &ni))
1523 goto redirty_out;
1524
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001525 if (wbc->for_reclaim) {
1526 if (!down_read_trylock(&sbi->node_write))
1527 goto redirty_out;
1528 } else {
1529 down_read(&sbi->node_write);
1530 }
1531
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001532 /* This page is already truncated */
1533 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1534 ClearPageUptodate(page);
1535 dec_page_count(sbi, F2FS_DIRTY_NODES);
1536 up_read(&sbi->node_write);
1537 unlock_page(page);
1538 return 0;
1539 }
1540
Chao Yuc9b60782018-08-01 19:13:44 +08001541 if (__is_valid_data_blkaddr(ni.blk_addr) &&
1542 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC))
1543 goto redirty_out;
1544
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001545 if (atomic && !test_opt(sbi, NOBARRIER))
1546 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1547
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001548 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001549 ClearPageError(page);
Chao Yu50fa53e2018-08-02 23:03:19 +08001550
1551 if (f2fs_in_warm_node_list(sbi, page)) {
1552 seq = f2fs_add_fsync_node_entry(sbi, page);
1553 if (seq_id)
1554 *seq_id = seq;
1555 }
1556
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001557 fio.old_blkaddr = ni.blk_addr;
Chao Yu4d57b862018-05-30 00:20:41 +08001558 f2fs_do_write_node_page(nid, &fio);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001559 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1560 dec_page_count(sbi, F2FS_DIRTY_NODES);
1561 up_read(&sbi->node_write);
1562
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001563 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001564 f2fs_submit_merged_write_cond(sbi, page->mapping->host, 0,
1565 page->index, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001566 submitted = NULL;
1567 }
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001568
1569 unlock_page(page);
1570
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001571 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001572 f2fs_submit_merged_write(sbi, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001573 submitted = NULL;
1574 }
1575 if (submitted)
1576 *submitted = fio.submitted;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001577
Yunlong Song401db792017-07-27 20:11:00 +08001578 if (do_balance)
1579 f2fs_balance_fs(sbi, false);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001580 return 0;
1581
1582redirty_out:
1583 redirty_page_for_writepage(wbc, page);
1584 return AOP_WRITEPAGE_ACTIVATE;
1585}
1586
Chao Yu4d57b862018-05-30 00:20:41 +08001587void f2fs_move_node_page(struct page *node_page, int gc_type)
Yunlei Hef15194f2017-10-30 14:18:55 +08001588{
1589 if (gc_type == FG_GC) {
1590 struct writeback_control wbc = {
1591 .sync_mode = WB_SYNC_ALL,
1592 .nr_to_write = 1,
1593 .for_reclaim = 0,
1594 };
1595
1596 set_page_dirty(node_page);
1597 f2fs_wait_on_page_writeback(node_page, NODE, true);
1598
1599 f2fs_bug_on(F2FS_P_SB(node_page), PageWriteback(node_page));
1600 if (!clear_page_dirty_for_io(node_page))
1601 goto out_page;
1602
1603 if (__write_node_page(node_page, false, NULL,
Chao Yu50fa53e2018-08-02 23:03:19 +08001604 &wbc, false, FS_GC_NODE_IO, NULL))
Yunlei Hef15194f2017-10-30 14:18:55 +08001605 unlock_page(node_page);
1606 goto release_page;
1607 } else {
1608 /* set page dirty and write it */
1609 if (!PageWriteback(node_page))
1610 set_page_dirty(node_page);
1611 }
1612out_page:
1613 unlock_page(node_page);
1614release_page:
1615 f2fs_put_page(node_page, 0);
1616}
1617
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001618static int f2fs_write_node_page(struct page *page,
1619 struct writeback_control *wbc)
1620{
Chao Yu50fa53e2018-08-02 23:03:19 +08001621 return __write_node_page(page, false, NULL, wbc, false,
1622 FS_NODE_IO, NULL);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001623}
1624
Chao Yu4d57b862018-05-30 00:20:41 +08001625int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
Chao Yu50fa53e2018-08-02 23:03:19 +08001626 struct writeback_control *wbc, bool atomic,
1627 unsigned int *seq_id)
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001628{
Jan Kara028a63a2017-11-15 17:34:51 -08001629 pgoff_t index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001630 pgoff_t last_idx = ULONG_MAX;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001631 struct pagevec pvec;
1632 int ret = 0;
1633 struct page *last_page = NULL;
1634 bool marked = false;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001635 nid_t ino = inode->i_ino;
Jan Kara028a63a2017-11-15 17:34:51 -08001636 int nr_pages;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001637
1638 if (atomic) {
1639 last_page = last_fsync_dnode(sbi, ino);
1640 if (IS_ERR_OR_NULL(last_page))
1641 return PTR_ERR_OR_ZERO(last_page);
1642 }
1643retry:
Mel Gorman86679822017-11-15 17:37:52 -08001644 pagevec_init(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001645 index = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001646
Jan Kara028a63a2017-11-15 17:34:51 -08001647 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001648 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001649 int i;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001650
1651 for (i = 0; i < nr_pages; i++) {
1652 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001653 bool submitted = false;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001654
1655 if (unlikely(f2fs_cp_error(sbi))) {
1656 f2fs_put_page(last_page, 0);
1657 pagevec_release(&pvec);
Chao Yu9de69272016-10-11 22:57:06 +08001658 ret = -EIO;
1659 goto out;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001660 }
1661
1662 if (!IS_DNODE(page) || !is_cold_node(page))
1663 continue;
1664 if (ino_of_node(page) != ino)
1665 continue;
1666
1667 lock_page(page);
1668
1669 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1670continue_unlock:
1671 unlock_page(page);
1672 continue;
1673 }
1674 if (ino_of_node(page) != ino)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001675 goto continue_unlock;
1676
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001677 if (!PageDirty(page) && page != last_page) {
1678 /* someone wrote it for us */
1679 goto continue_unlock;
1680 }
1681
1682 f2fs_wait_on_page_writeback(page, NODE, true);
1683 BUG_ON(PageWriteback(page));
1684
Jaegeuk Kimd29fd172017-04-12 12:02:00 -07001685 set_fsync_mark(page, 0);
1686 set_dentry_mark(page, 0);
1687
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001688 if (!atomic || page == last_page) {
1689 set_fsync_mark(page, 1);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001690 if (IS_INODE(page)) {
1691 if (is_inode_flag_set(inode,
1692 FI_DIRTY_INODE))
Chao Yu4d57b862018-05-30 00:20:41 +08001693 f2fs_update_inode(inode, page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001694 set_dentry_mark(page,
Chao Yu4d57b862018-05-30 00:20:41 +08001695 f2fs_need_dentry_mark(sbi, ino));
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001696 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001697 /* may be written by other thread */
1698 if (!PageDirty(page))
1699 set_page_dirty(page);
1700 }
1701
1702 if (!clear_page_dirty_for_io(page))
1703 goto continue_unlock;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001704
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001705 ret = __write_node_page(page, atomic &&
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001706 page == last_page,
Chao Yub0af6d42017-08-02 23:21:48 +08001707 &submitted, wbc, true,
Chao Yu50fa53e2018-08-02 23:03:19 +08001708 FS_NODE_IO, seq_id);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001709 if (ret) {
Jaegeuk Kim52681372016-04-13 16:24:44 -07001710 unlock_page(page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001711 f2fs_put_page(last_page, 0);
1712 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001713 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001714 last_idx = page->index;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001715 }
Chao Yu3f5f4952016-09-29 18:50:10 +08001716
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001717 if (page == last_page) {
1718 f2fs_put_page(page, 0);
1719 marked = true;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001720 break;
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001721 }
Jaegeuk Kim52681372016-04-13 16:24:44 -07001722 }
1723 pagevec_release(&pvec);
1724 cond_resched();
1725
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001726 if (ret || marked)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001727 break;
1728 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001729 if (!ret && atomic && !marked) {
1730 f2fs_msg(sbi->sb, KERN_DEBUG,
1731 "Retry to write fsync mark: ino=%u, idx=%lx",
1732 ino, last_page->index);
1733 lock_page(last_page);
Yunlei Hed40a43a2016-11-16 17:26:24 +08001734 f2fs_wait_on_page_writeback(last_page, NODE, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001735 set_page_dirty(last_page);
1736 unlock_page(last_page);
1737 goto retry;
1738 }
Chao Yu9de69272016-10-11 22:57:06 +08001739out:
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001740 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001741 f2fs_submit_merged_write_cond(sbi, NULL, ino, last_idx, NODE);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001742 return ret ? -EIO: 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001743}
1744
Chao Yu4d57b862018-05-30 00:20:41 +08001745int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
1746 struct writeback_control *wbc,
Chao Yub0af6d42017-08-02 23:21:48 +08001747 bool do_balance, enum iostat_type io_type)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001748{
Jan Kara028a63a2017-11-15 17:34:51 -08001749 pgoff_t index;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001750 struct pagevec pvec;
1751 int step = 0;
Jaegeuk Kim12bb0a82016-03-11 15:33:22 -08001752 int nwritten = 0;
Chao Yu3f5f4952016-09-29 18:50:10 +08001753 int ret = 0;
Chao Yuc29fd0c2018-06-04 23:20:36 +08001754 int nr_pages, done = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001755
Mel Gorman86679822017-11-15 17:37:52 -08001756 pagevec_init(&pvec);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001757
1758next_step:
1759 index = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001760
Chao Yuc29fd0c2018-06-04 23:20:36 +08001761 while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
1762 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001763 int i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001764
1765 for (i = 0; i < nr_pages; i++) {
1766 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001767 bool submitted = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001768
Chao Yuc29fd0c2018-06-04 23:20:36 +08001769 /* give a priority to WB_SYNC threads */
1770 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
1771 wbc->sync_mode == WB_SYNC_NONE) {
1772 done = 1;
1773 break;
1774 }
1775
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001776 /*
1777 * flushing sequence with step:
1778 * 0. indirect nodes
1779 * 1. dentry dnodes
1780 * 2. file dnodes
1781 */
1782 if (step == 0 && IS_DNODE(page))
1783 continue;
1784 if (step == 1 && (!IS_DNODE(page) ||
1785 is_cold_node(page)))
1786 continue;
1787 if (step == 2 && (!IS_DNODE(page) ||
1788 !is_cold_node(page)))
1789 continue;
Chao Yu9a4cbc92016-02-22 18:35:46 +08001790lock_node:
Chao Yu4b270a82018-07-04 18:04:10 +08001791 if (wbc->sync_mode == WB_SYNC_ALL)
1792 lock_page(page);
1793 else if (!trylock_page(page))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001794 continue;
1795
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001796 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001797continue_unlock:
1798 unlock_page(page);
1799 continue;
1800 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001801
1802 if (!PageDirty(page)) {
1803 /* someone wrote it for us */
1804 goto continue_unlock;
1805 }
1806
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001807 /* flush inline_data */
Jaegeuk Kim52681372016-04-13 16:24:44 -07001808 if (is_inline_node(page)) {
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001809 clear_inline_node(page);
1810 unlock_page(page);
1811 flush_inline_data(sbi, ino_of_node(page));
Chao Yu9a4cbc92016-02-22 18:35:46 +08001812 goto lock_node;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001813 }
1814
Jaegeuk Kimfa3d2bd2016-01-28 11:48:52 -08001815 f2fs_wait_on_page_writeback(page, NODE, true);
1816
1817 BUG_ON(PageWriteback(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001818 if (!clear_page_dirty_for_io(page))
1819 goto continue_unlock;
1820
Jaegeuk Kim52681372016-04-13 16:24:44 -07001821 set_fsync_mark(page, 0);
1822 set_dentry_mark(page, 0);
Jaegeuk Kim52746512014-08-11 18:18:36 -07001823
Yunlong Song401db792017-07-27 20:11:00 +08001824 ret = __write_node_page(page, false, &submitted,
Chao Yu50fa53e2018-08-02 23:03:19 +08001825 wbc, do_balance, io_type, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001826 if (ret)
Jaegeuk Kim52746512014-08-11 18:18:36 -07001827 unlock_page(page);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001828 else if (submitted)
Chao Yu3f5f4952016-09-29 18:50:10 +08001829 nwritten++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001830
1831 if (--wbc->nr_to_write == 0)
1832 break;
1833 }
1834 pagevec_release(&pvec);
1835 cond_resched();
1836
1837 if (wbc->nr_to_write == 0) {
1838 step = 2;
1839 break;
1840 }
1841 }
1842
1843 if (step < 2) {
Chao Yufd8c8ca2018-07-25 19:16:21 +08001844 if (wbc->sync_mode == WB_SYNC_NONE && step == 1)
1845 goto out;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001846 step++;
1847 goto next_step;
1848 }
Chao Yufd8c8ca2018-07-25 19:16:21 +08001849out:
Chao Yu3f5f4952016-09-29 18:50:10 +08001850 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001851 f2fs_submit_merged_write(sbi, NODE);
Chao Yudb198ae2018-01-18 17:29:10 +08001852
1853 if (unlikely(f2fs_cp_error(sbi)))
1854 return -EIO;
Chao Yu3f5f4952016-09-29 18:50:10 +08001855 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001856}
1857
Chao Yu50fa53e2018-08-02 23:03:19 +08001858int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
1859 unsigned int seq_id)
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001860{
Chao Yu50fa53e2018-08-02 23:03:19 +08001861 struct fsync_node_entry *fn;
1862 struct page *page;
1863 struct list_head *head = &sbi->fsync_node_list;
1864 unsigned long flags;
1865 unsigned int cur_seq_id = 0;
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001866 int ret2, ret = 0;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001867
Chao Yu50fa53e2018-08-02 23:03:19 +08001868 while (seq_id && cur_seq_id < seq_id) {
1869 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
1870 if (list_empty(head)) {
1871 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1872 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001873 }
Chao Yu50fa53e2018-08-02 23:03:19 +08001874 fn = list_first_entry(head, struct fsync_node_entry, list);
1875 if (fn->seq_id > seq_id) {
1876 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1877 break;
1878 }
1879 cur_seq_id = fn->seq_id;
1880 page = fn->page;
1881 get_page(page);
1882 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1883
1884 f2fs_wait_on_page_writeback(page, NODE, true);
1885 if (TestClearPageError(page))
1886 ret = -EIO;
1887
1888 put_page(page);
1889
1890 if (ret)
1891 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001892 }
1893
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001894 ret2 = filemap_check_errors(NODE_MAPPING(sbi));
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001895 if (!ret)
1896 ret = ret2;
Chao Yu50fa53e2018-08-02 23:03:19 +08001897
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001898 return ret;
1899}
1900
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001901static int f2fs_write_node_pages(struct address_space *mapping,
1902 struct writeback_control *wbc)
1903{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001904 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001905 struct blk_plug plug;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001906 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001907
Chao Yu0771fcc2017-06-29 23:20:45 +08001908 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1909 goto skip_write;
1910
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001911 /* balancing f2fs's metadata in background */
1912 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001913
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001914 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001915 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001916 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001917
Chao Yuc29fd0c2018-06-04 23:20:36 +08001918 if (wbc->sync_mode == WB_SYNC_ALL)
1919 atomic_inc(&sbi->wb_sync_req[NODE]);
1920 else if (atomic_read(&sbi->wb_sync_req[NODE]))
1921 goto skip_write;
1922
Yunlei Hed31c7c32016-02-04 16:14:00 +08001923 trace_f2fs_writepages(mapping->host, wbc, NODE);
1924
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001925 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001926 blk_start_plug(&plug);
Chao Yu4d57b862018-05-30 00:20:41 +08001927 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001928 blk_finish_plug(&plug);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001929 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Chao Yuc29fd0c2018-06-04 23:20:36 +08001930
1931 if (wbc->sync_mode == WB_SYNC_ALL)
1932 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001933 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001934
1935skip_write:
1936 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001937 trace_f2fs_writepages(mapping->host, wbc, NODE);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001938 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001939}
1940
1941static int f2fs_set_node_page_dirty(struct page *page)
1942{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001943 trace_f2fs_set_page_dirty(page, NODE);
1944
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001945 if (!PageUptodate(page))
1946 SetPageUptodate(page);
Weichao Guo54c55c42018-03-09 23:10:21 +08001947#ifdef CONFIG_F2FS_CHECK_FS
1948 if (IS_INODE(page))
1949 f2fs_inode_chksum_set(F2FS_P_SB(page), page);
1950#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001951 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07001952 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001953 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001954 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001955 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001956 return 1;
1957 }
1958 return 0;
1959}
1960
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001961/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001962 * Structure of the f2fs node operations
1963 */
1964const struct address_space_operations f2fs_node_aops = {
1965 .writepage = f2fs_write_node_page,
1966 .writepages = f2fs_write_node_pages,
1967 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001968 .invalidatepage = f2fs_invalidate_page,
1969 .releasepage = f2fs_release_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08001970#ifdef CONFIG_MIGRATION
1971 .migratepage = f2fs_migrate_page,
1972#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001973};
1974
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001975static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1976 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001977{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001978 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001979}
1980
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001981static int __insert_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08001982 struct free_nid *i, enum nid_state state)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001983{
Chao Yub8559dc2016-10-12 19:28:29 +08001984 struct f2fs_nm_info *nm_i = NM_I(sbi);
1985
Fan Lia0761f62017-10-28 19:03:37 +08001986 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
1987 if (err)
1988 return err;
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07001989
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001990 f2fs_bug_on(sbi, state != i->state);
1991 nm_i->nid_cnt[state]++;
1992 if (state == FREE_NID)
1993 list_add_tail(&i->list, &nm_i->free_nid_list);
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07001994 return 0;
Chao Yub8559dc2016-10-12 19:28:29 +08001995}
1996
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001997static void __remove_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08001998 struct free_nid *i, enum nid_state state)
Chao Yub8559dc2016-10-12 19:28:29 +08001999{
2000 struct f2fs_nm_info *nm_i = NM_I(sbi);
2001
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002002 f2fs_bug_on(sbi, state != i->state);
2003 nm_i->nid_cnt[state]--;
2004 if (state == FREE_NID)
2005 list_del(&i->list);
Fan Lia0761f62017-10-28 19:03:37 +08002006 radix_tree_delete(&nm_i->free_nid_root, i->nid);
2007}
2008
2009static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2010 enum nid_state org_state, enum nid_state dst_state)
2011{
2012 struct f2fs_nm_info *nm_i = NM_I(sbi);
2013
2014 f2fs_bug_on(sbi, org_state != i->state);
2015 i->state = dst_state;
2016 nm_i->nid_cnt[org_state]--;
2017 nm_i->nid_cnt[dst_state]++;
2018
2019 switch (dst_state) {
2020 case PREALLOC_NID:
2021 list_del(&i->list);
2022 break;
2023 case FREE_NID:
2024 list_add_tail(&i->list, &nm_i->free_nid_list);
2025 break;
2026 default:
2027 BUG_ON(1);
2028 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002029}
2030
LiFan5921aaa2017-11-22 16:07:23 +08002031static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2032 bool set, bool build)
2033{
2034 struct f2fs_nm_info *nm_i = NM_I(sbi);
2035 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2036 unsigned int nid_ofs = nid - START_NID(nid);
2037
2038 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2039 return;
2040
2041 if (set) {
2042 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2043 return;
2044 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2045 nm_i->free_nid_count[nat_ofs]++;
2046 } else {
2047 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2048 return;
2049 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2050 if (!build)
2051 nm_i->free_nid_count[nat_ofs]--;
2052 }
2053}
2054
Chao Yu4ac91242017-02-23 10:53:49 +08002055/* return if the nid is recognized as free */
LiFan5921aaa2017-11-22 16:07:23 +08002056static bool add_free_nid(struct f2fs_sb_info *sbi,
2057 nid_t nid, bool build, bool update)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002058{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002059 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu30a61dd2017-03-22 14:45:05 +08002060 struct free_nid *i, *e;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002061 struct nat_entry *ne;
Chao Yu30a61dd2017-03-22 14:45:05 +08002062 int err = -EINVAL;
2063 bool ret = false;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09002064
2065 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08002066 if (unlikely(nid == 0))
Chao Yu4ac91242017-02-23 10:53:49 +08002067 return false;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002068
Gu Zheng7bd59382013-10-22 14:52:26 +08002069 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002070 i->nid = nid;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002071 i->state = FREE_NID;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002072
LiFan5921aaa2017-11-22 16:07:23 +08002073 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002074
Chao Yub8559dc2016-10-12 19:28:29 +08002075 spin_lock(&nm_i->nid_list_lock);
Chao Yu30a61dd2017-03-22 14:45:05 +08002076
2077 if (build) {
2078 /*
2079 * Thread A Thread B
2080 * - f2fs_create
2081 * - f2fs_new_inode
Chao Yu4d57b862018-05-30 00:20:41 +08002082 * - f2fs_alloc_nid
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002083 * - __insert_nid_to_list(PREALLOC_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002084 * - f2fs_balance_fs_bg
Chao Yu4d57b862018-05-30 00:20:41 +08002085 * - f2fs_build_free_nids
2086 * - __f2fs_build_free_nids
Chao Yu30a61dd2017-03-22 14:45:05 +08002087 * - scan_nat_page
2088 * - add_free_nid
2089 * - __lookup_nat_cache
2090 * - f2fs_add_link
Chao Yu4d57b862018-05-30 00:20:41 +08002091 * - f2fs_init_inode_metadata
2092 * - f2fs_new_inode_page
2093 * - f2fs_new_node_page
Chao Yu30a61dd2017-03-22 14:45:05 +08002094 * - set_node_addr
Chao Yu4d57b862018-05-30 00:20:41 +08002095 * - f2fs_alloc_nid_done
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002096 * - __remove_nid_from_list(PREALLOC_NID)
2097 * - __insert_nid_to_list(FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002098 */
2099 ne = __lookup_nat_cache(nm_i, nid);
2100 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2101 nat_get_blkaddr(ne) != NULL_ADDR))
2102 goto err_out;
2103
2104 e = __lookup_free_nid_list(nm_i, nid);
2105 if (e) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002106 if (e->state == FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002107 ret = true;
2108 goto err_out;
2109 }
2110 }
2111 ret = true;
Fan Lia0761f62017-10-28 19:03:37 +08002112 err = __insert_free_nid(sbi, i, FREE_NID);
Chao Yu30a61dd2017-03-22 14:45:05 +08002113err_out:
LiFan5921aaa2017-11-22 16:07:23 +08002114 if (update) {
2115 update_free_nid_bitmap(sbi, nid, ret, build);
2116 if (!build)
2117 nm_i->available_nids++;
2118 }
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002119 spin_unlock(&nm_i->nid_list_lock);
2120 radix_tree_preload_end();
LiFan5921aaa2017-11-22 16:07:23 +08002121
Chao Yu30a61dd2017-03-22 14:45:05 +08002122 if (err)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002123 kmem_cache_free(free_nid_slab, i);
Chao Yu30a61dd2017-03-22 14:45:05 +08002124 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002125}
2126
Chao Yub8559dc2016-10-12 19:28:29 +08002127static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002128{
Chao Yub8559dc2016-10-12 19:28:29 +08002129 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002130 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002131 bool need_free = false;
2132
Chao Yub8559dc2016-10-12 19:28:29 +08002133 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002134 i = __lookup_free_nid_list(nm_i, nid);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002135 if (i && i->state == FREE_NID) {
Fan Lia0761f62017-10-28 19:03:37 +08002136 __remove_free_nid(sbi, i, FREE_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002137 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002138 }
Chao Yub8559dc2016-10-12 19:28:29 +08002139 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002140
2141 if (need_free)
2142 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002143}
2144
Chao Yue2374012018-06-15 14:45:57 +08002145static int scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002146 struct page *nat_page, nid_t start_nid)
2147{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002148 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002149 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2150 block_t blk_addr;
Chao Yu4ac91242017-02-23 10:53:49 +08002151 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002152 int i;
2153
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002154 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
Chao Yu4ac91242017-02-23 10:53:49 +08002155
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002156 i = start_nid % NAT_ENTRY_PER_BLOCK;
2157
2158 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Chao Yucfb271d2013-12-05 17:15:22 +08002159 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09002160 break;
Haicheng Li23d38842013-05-06 23:15:43 +08002161
2162 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Chao Yue2374012018-06-15 14:45:57 +08002163
2164 if (blk_addr == NEW_ADDR)
2165 return -EINVAL;
2166
LiFan5921aaa2017-11-22 16:07:23 +08002167 if (blk_addr == NULL_ADDR) {
2168 add_free_nid(sbi, start_nid, true, true);
2169 } else {
2170 spin_lock(&NM_I(sbi)->nid_list_lock);
2171 update_free_nid_bitmap(sbi, start_nid, false, true);
2172 spin_unlock(&NM_I(sbi)->nid_list_lock);
2173 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002174 }
Chao Yue2374012018-06-15 14:45:57 +08002175
2176 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002177}
2178
Chao Yu2fbaa252017-11-08 17:47:36 +08002179static void scan_curseg_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002180{
Chao Yu4ac91242017-02-23 10:53:49 +08002181 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2182 struct f2fs_journal *journal = curseg->journal;
Chao Yu2fbaa252017-11-08 17:47:36 +08002183 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08002184
Chao Yu4ac91242017-02-23 10:53:49 +08002185 down_read(&curseg->journal_rwsem);
2186 for (i = 0; i < nats_in_cursum(journal); i++) {
2187 block_t addr;
2188 nid_t nid;
2189
2190 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2191 nid = le32_to_cpu(nid_in_journal(journal, i));
2192 if (addr == NULL_ADDR)
LiFan5921aaa2017-11-22 16:07:23 +08002193 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002194 else
2195 remove_free_nid(sbi, nid);
2196 }
2197 up_read(&curseg->journal_rwsem);
Chao Yu2fbaa252017-11-08 17:47:36 +08002198}
2199
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002200static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002201{
2202 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002203 unsigned int i, idx;
Fan Li97456572017-11-07 19:14:24 +08002204 nid_t nid;
Chao Yu4ac91242017-02-23 10:53:49 +08002205
2206 down_read(&nm_i->nat_tree_lock);
2207
2208 for (i = 0; i < nm_i->nat_blocks; i++) {
2209 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2210 continue;
2211 if (!nm_i->free_nid_count[i])
2212 continue;
2213 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
Fan Li97456572017-11-07 19:14:24 +08002214 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2215 NAT_ENTRY_PER_BLOCK, idx);
2216 if (idx >= NAT_ENTRY_PER_BLOCK)
2217 break;
Chao Yu4ac91242017-02-23 10:53:49 +08002218
2219 nid = i * NAT_ENTRY_PER_BLOCK + idx;
LiFan5921aaa2017-11-22 16:07:23 +08002220 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002221
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002222 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
Chao Yu4ac91242017-02-23 10:53:49 +08002223 goto out;
2224 }
2225 }
2226out:
Chao Yu2fbaa252017-11-08 17:47:36 +08002227 scan_curseg_cache(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002228
Chao Yu4ac91242017-02-23 10:53:49 +08002229 up_read(&nm_i->nat_tree_lock);
2230}
2231
Chao Yue2374012018-06-15 14:45:57 +08002232static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
Chao Yu4d57b862018-05-30 00:20:41 +08002233 bool sync, bool mount)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002234{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002235 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yue2374012018-06-15 14:45:57 +08002236 int i = 0, ret;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002237 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002238
Yunlei Hee9cdd302017-04-26 15:56:52 +08002239 if (unlikely(nid >= nm_i->max_nid))
2240 nid = 0;
2241
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002242 /* Enough entries */
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002243 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002244 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002245
Chao Yu4d57b862018-05-30 00:20:41 +08002246 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
Chao Yue2374012018-06-15 14:45:57 +08002247 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002248
Chao Yu4ac91242017-02-23 10:53:49 +08002249 if (!mount) {
2250 /* try to find free nids in free_nid_bitmap */
2251 scan_free_nid_bits(sbi);
2252
Fan Li74986212017-11-07 11:04:33 +08002253 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002254 return 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002255 }
2256
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002257 /* readahead nat pages to be scanned */
Chao Yu4d57b862018-05-30 00:20:41 +08002258 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
Chao Yu26879fb2015-10-12 17:05:59 +08002259 META_NAT, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002260
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002261 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002262
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002263 while (1) {
Yunlei He66e83362017-11-17 16:13:38 +08002264 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2265 nm_i->nat_block_bitmap)) {
2266 struct page *page = get_current_nat_page(sbi, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002267
Chao Yue2374012018-06-15 14:45:57 +08002268 ret = scan_nat_page(sbi, page, nid);
Yunlei He66e83362017-11-17 16:13:38 +08002269 f2fs_put_page(page, 1);
Chao Yue2374012018-06-15 14:45:57 +08002270
2271 if (ret) {
2272 up_read(&nm_i->nat_tree_lock);
2273 f2fs_bug_on(sbi, !mount);
2274 f2fs_msg(sbi->sb, KERN_ERR,
2275 "NAT is corrupt, run fsck to fix it");
2276 return -EINVAL;
2277 }
Yunlei He66e83362017-11-17 16:13:38 +08002278 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002279
2280 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08002281 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002282 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002283
Chao Yua6d494b2015-07-24 18:26:26 +08002284 if (++i >= FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002285 break;
2286 }
2287
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002288 /* go to the next free nat pages to find free nids abundantly */
2289 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002290
2291 /* find free nids from current sum_pages */
Chao Yu2fbaa252017-11-08 17:47:36 +08002292 scan_curseg_cache(sbi);
Chao Yudfc08a12016-02-14 18:50:40 +08002293
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002294 up_read(&nm_i->nat_tree_lock);
Chao Yu2db23882015-10-12 17:07:33 +08002295
Chao Yu4d57b862018-05-30 00:20:41 +08002296 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
Chao Yuea1a29a02015-10-12 17:08:48 +08002297 nm_i->ra_nid_pages, META_NAT, false);
Chao Yue2374012018-06-15 14:45:57 +08002298
2299 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002300}
2301
Chao Yue2374012018-06-15 14:45:57 +08002302int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
Chao Yu2411cf52016-10-11 22:31:34 +08002303{
Chao Yue2374012018-06-15 14:45:57 +08002304 int ret;
2305
Chao Yu2411cf52016-10-11 22:31:34 +08002306 mutex_lock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002307 ret = __f2fs_build_free_nids(sbi, sync, mount);
Chao Yu2411cf52016-10-11 22:31:34 +08002308 mutex_unlock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002309
2310 return ret;
Chao Yu2411cf52016-10-11 22:31:34 +08002311}
2312
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002313/*
2314 * If this function returns success, caller can obtain a new nid
2315 * from second parameter of this function.
2316 * The returned nid could be used ino as well as nid when inode is created.
2317 */
Chao Yu4d57b862018-05-30 00:20:41 +08002318bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002319{
2320 struct f2fs_nm_info *nm_i = NM_I(sbi);
2321 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002322retry:
Chao Yu55523512017-02-25 11:08:28 +08002323 if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
2324 f2fs_show_injection_info(FAULT_ALLOC_NID);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002325 return false;
Chao Yu55523512017-02-25 11:08:28 +08002326 }
Arnd Bergmann7fa750a2018-08-13 23:38:06 +02002327
Chao Yub8559dc2016-10-12 19:28:29 +08002328 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002329
Chao Yu04d47e62016-11-17 20:53:11 +08002330 if (unlikely(nm_i->available_nids == 0)) {
2331 spin_unlock(&nm_i->nid_list_lock);
2332 return false;
2333 }
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002334
Chao Yu4d57b862018-05-30 00:20:41 +08002335 /* We should not use stale free nids created by f2fs_build_free_nids */
2336 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002337 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2338 i = list_first_entry(&nm_i->free_nid_list,
Chao Yub8559dc2016-10-12 19:28:29 +08002339 struct free_nid, list);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002340 *nid = i->nid;
Chao Yub8559dc2016-10-12 19:28:29 +08002341
Fan Lia0761f62017-10-28 19:03:37 +08002342 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
Chao Yu04d47e62016-11-17 20:53:11 +08002343 nm_i->available_nids--;
Chao Yu4ac91242017-02-23 10:53:49 +08002344
Chao Yu346fe752017-03-13 20:10:41 +08002345 update_free_nid_bitmap(sbi, *nid, false, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002346
Chao Yub8559dc2016-10-12 19:28:29 +08002347 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002348 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002349 }
Chao Yub8559dc2016-10-12 19:28:29 +08002350 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002351
2352 /* Let's scan nat pages and its caches to get free nids */
Chao Yu4d57b862018-05-30 00:20:41 +08002353 f2fs_build_free_nids(sbi, true, false);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002354 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002355}
2356
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002357/*
Chao Yu4d57b862018-05-30 00:20:41 +08002358 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002359 */
Chao Yu4d57b862018-05-30 00:20:41 +08002360void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002361{
2362 struct f2fs_nm_info *nm_i = NM_I(sbi);
2363 struct free_nid *i;
2364
Chao Yub8559dc2016-10-12 19:28:29 +08002365 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002366 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002367 f2fs_bug_on(sbi, !i);
Fan Lia0761f62017-10-28 19:03:37 +08002368 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08002369 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002370
2371 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002372}
2373
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002374/*
Chao Yu4d57b862018-05-30 00:20:41 +08002375 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002376 */
Chao Yu4d57b862018-05-30 00:20:41 +08002377void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002378{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002379 struct f2fs_nm_info *nm_i = NM_I(sbi);
2380 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002381 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002382
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002383 if (!nid)
2384 return;
2385
Chao Yub8559dc2016-10-12 19:28:29 +08002386 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002387 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002388 f2fs_bug_on(sbi, !i);
2389
Chao Yu4d57b862018-05-30 00:20:41 +08002390 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
Fan Lia0761f62017-10-28 19:03:37 +08002391 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002392 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08002393 } else {
Fan Lia0761f62017-10-28 19:03:37 +08002394 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
Haicheng Li95630cb2013-05-06 23:15:41 +08002395 }
Chao Yu04d47e62016-11-17 20:53:11 +08002396
2397 nm_i->available_nids++;
2398
Chao Yu346fe752017-03-13 20:10:41 +08002399 update_free_nid_bitmap(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002400
Chao Yub8559dc2016-10-12 19:28:29 +08002401 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002402
2403 if (need_free)
2404 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002405}
2406
Chao Yu4d57b862018-05-30 00:20:41 +08002407int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
Chao Yu31696582015-07-28 18:33:46 +08002408{
2409 struct f2fs_nm_info *nm_i = NM_I(sbi);
2410 struct free_nid *i, *next;
2411 int nr = nr_shrink;
2412
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002413 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Jaegeuk Kimad4edb82016-06-16 16:41:49 -07002414 return 0;
2415
Chao Yu31696582015-07-28 18:33:46 +08002416 if (!mutex_trylock(&nm_i->build_lock))
2417 return 0;
2418
Chao Yub8559dc2016-10-12 19:28:29 +08002419 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002420 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
Chao Yub8559dc2016-10-12 19:28:29 +08002421 if (nr_shrink <= 0 ||
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002422 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Chao Yu31696582015-07-28 18:33:46 +08002423 break;
Chao Yub8559dc2016-10-12 19:28:29 +08002424
Fan Lia0761f62017-10-28 19:03:37 +08002425 __remove_free_nid(sbi, i, FREE_NID);
Chao Yu31696582015-07-28 18:33:46 +08002426 kmem_cache_free(free_nid_slab, i);
2427 nr_shrink--;
Chao Yu31696582015-07-28 18:33:46 +08002428 }
Chao Yub8559dc2016-10-12 19:28:29 +08002429 spin_unlock(&nm_i->nid_list_lock);
Chao Yu31696582015-07-28 18:33:46 +08002430 mutex_unlock(&nm_i->build_lock);
2431
2432 return nr - nr_shrink;
2433}
2434
Chao Yu4d57b862018-05-30 00:20:41 +08002435void f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08002436{
Chao Yu28cdce02014-03-11 13:37:38 +08002437 void *src_addr, *dst_addr;
2438 size_t inline_size;
2439 struct page *ipage;
2440 struct f2fs_inode *ri;
2441
Chao Yu4d57b862018-05-30 00:20:41 +08002442 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002443 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08002444
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002445 ri = F2FS_INODE(page);
Yunlei He1eca05a2018-01-03 18:03:04 +08002446 if (ri->i_inline & F2FS_INLINE_XATTR) {
2447 set_inode_flag(inode, FI_INLINE_XATTR);
2448 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002449 clear_inode_flag(inode, FI_INLINE_XATTR);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002450 goto update_inode;
2451 }
2452
Chao Yu6afc6622017-09-06 21:59:50 +08002453 dst_addr = inline_xattr_addr(inode, ipage);
2454 src_addr = inline_xattr_addr(inode, page);
Chao Yu28cdce02014-03-11 13:37:38 +08002455 inline_size = inline_xattr_size(inode);
2456
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002457 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu28cdce02014-03-11 13:37:38 +08002458 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002459update_inode:
Chao Yu4d57b862018-05-30 00:20:41 +08002460 f2fs_update_inode(inode, ipage);
Chao Yu28cdce02014-03-11 13:37:38 +08002461 f2fs_put_page(ipage, 1);
2462}
2463
Chao Yu4d57b862018-05-30 00:20:41 +08002464int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002465{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002466 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002467 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
Yunlei He87905682017-07-18 09:48:12 +08002468 nid_t new_xnid;
2469 struct dnode_of_data dn;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002470 struct node_info ni;
Chao Yud2600812017-02-08 17:39:45 +08002471 struct page *xpage;
Chao Yu77357302018-07-17 00:02:17 +08002472 int err;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002473
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002474 if (!prev_xnid)
2475 goto recover_xnid;
2476
Chao Yud2600812017-02-08 17:39:45 +08002477 /* 1: invalidate the previous xattr nid */
Chao Yu77357302018-07-17 00:02:17 +08002478 err = f2fs_get_node_info(sbi, prev_xnid, &ni);
2479 if (err)
2480 return err;
2481
Chao Yu4d57b862018-05-30 00:20:41 +08002482 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +08002483 dec_valid_node_count(sbi, inode, false);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002484 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002485
2486recover_xnid:
Chao Yud2600812017-02-08 17:39:45 +08002487 /* 2: update xattr nid in inode */
Chao Yu4d57b862018-05-30 00:20:41 +08002488 if (!f2fs_alloc_nid(sbi, &new_xnid))
Yunlei He87905682017-07-18 09:48:12 +08002489 return -ENOSPC;
2490
2491 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
Chao Yu4d57b862018-05-30 00:20:41 +08002492 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Yunlei He87905682017-07-18 09:48:12 +08002493 if (IS_ERR(xpage)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002494 f2fs_alloc_nid_failed(sbi, new_xnid);
Yunlei He87905682017-07-18 09:48:12 +08002495 return PTR_ERR(xpage);
2496 }
2497
Chao Yu4d57b862018-05-30 00:20:41 +08002498 f2fs_alloc_nid_done(sbi, new_xnid);
2499 f2fs_update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002500
Chao Yud2600812017-02-08 17:39:45 +08002501 /* 3: update and set xattr node page dirty */
Yunlei He87905682017-07-18 09:48:12 +08002502 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
Chao Yud2600812017-02-08 17:39:45 +08002503
Chao Yud2600812017-02-08 17:39:45 +08002504 set_page_dirty(xpage);
2505 f2fs_put_page(xpage, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002506
Chao Yud2600812017-02-08 17:39:45 +08002507 return 0;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002508}
2509
Chao Yu4d57b862018-05-30 00:20:41 +08002510int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002511{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002512 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002513 nid_t ino = ino_of_node(page);
2514 struct node_info old_ni, new_ni;
2515 struct page *ipage;
Chao Yu77357302018-07-17 00:02:17 +08002516 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002517
Chao Yu77357302018-07-17 00:02:17 +08002518 err = f2fs_get_node_info(sbi, ino, &old_ni);
2519 if (err)
2520 return err;
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09002521
2522 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2523 return -EINVAL;
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002524retry:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07002525 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002526 if (!ipage) {
2527 congestion_wait(BLK_RW_ASYNC, HZ/50);
2528 goto retry;
2529 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002530
arter97e1c42042014-08-06 23:22:50 +09002531 /* Should not use this inode from free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08002532 remove_free_nid(sbi, ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002533
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002534 if (!PageUptodate(ipage))
2535 SetPageUptodate(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002536 fill_node_footer(ipage, ino, ino, 0, true);
Chao Yuc5667572018-03-09 14:24:22 +08002537 set_cold_node(page, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002538
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002539 src = F2FS_INODE(page);
2540 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002541
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002542 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
2543 dst->i_size = 0;
2544 dst->i_blocks = cpu_to_le64(1);
2545 dst->i_links = cpu_to_le32(1);
2546 dst->i_xattr_nid = 0;
Chao Yu7a2af762017-07-19 00:19:06 +08002547 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
Chao Yu5c571322017-07-26 00:01:41 +08002548 if (dst->i_inline & F2FS_EXTRA_ATTR) {
Chao Yu7a2af762017-07-19 00:19:06 +08002549 dst->i_extra_isize = src->i_extra_isize;
Chao Yu6afc6622017-09-06 21:59:50 +08002550
2551 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb) &&
2552 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2553 i_inline_xattr_size))
2554 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2555
Chao Yu5c571322017-07-26 00:01:41 +08002556 if (f2fs_sb_has_project_quota(sbi->sb) &&
2557 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2558 i_projid))
2559 dst->i_projid = src->i_projid;
2560 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002561
2562 new_ni = old_ni;
2563 new_ni.ino = ino;
2564
Chao Yu0abd6752017-07-09 00:13:07 +08002565 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09002566 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002567 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002568 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07002569 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002570 f2fs_put_page(ipage, 1);
2571 return 0;
2572}
2573
Chao Yu77357302018-07-17 00:02:17 +08002574int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002575 unsigned int segno, struct f2fs_summary_block *sum)
2576{
2577 struct f2fs_node *rn;
2578 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002579 block_t addr;
Chao Yu9ecf4b82014-12-18 18:29:05 +08002580 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002581
2582 /* scan the node segment */
2583 last_offset = sbi->blocks_per_seg;
2584 addr = START_BLOCK(sbi, segno);
2585 sum_entry = &sum->entries[0];
2586
Chao Yu9ecf4b82014-12-18 18:29:05 +08002587 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Jaegeuk Kim664ba972016-10-18 11:07:45 -07002588 nrpages = min(last_offset - i, BIO_MAX_PAGES);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002589
arter97e1c42042014-08-06 23:22:50 +09002590 /* readahead node pages */
Chao Yu4d57b862018-05-30 00:20:41 +08002591 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002592
Chao Yu9ecf4b82014-12-18 18:29:05 +08002593 for (idx = addr; idx < addr + nrpages; idx++) {
Chao Yu4d57b862018-05-30 00:20:41 +08002594 struct page *page = f2fs_get_tmp_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08002595
Chao Yu77357302018-07-17 00:02:17 +08002596 if (IS_ERR(page))
2597 return PTR_ERR(page);
2598
Chao Yu9ecf4b82014-12-18 18:29:05 +08002599 rn = F2FS_NODE(page);
2600 sum_entry->nid = rn->footer.nid;
2601 sum_entry->version = 0;
2602 sum_entry->ofs_in_node = 0;
2603 sum_entry++;
2604 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08002605 }
Chao Yubac4eef2014-05-27 08:41:07 +08002606
Chao Yu9ecf4b82014-12-18 18:29:05 +08002607 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08002608 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002609 }
Chao Yu77357302018-07-17 00:02:17 +08002610 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002611}
2612
Chao Yuaec71382014-06-24 09:18:20 +08002613static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002614{
2615 struct f2fs_nm_info *nm_i = NM_I(sbi);
2616 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002617 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002618 int i;
2619
Chao Yub7ad7512016-02-19 18:08:46 +08002620 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002621 for (i = 0; i < nats_in_cursum(journal); i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002622 struct nat_entry *ne;
2623 struct f2fs_nat_entry raw_ne;
Chao Yudfc08a12016-02-14 18:50:40 +08002624 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002625
Chao Yudfc08a12016-02-14 18:50:40 +08002626 raw_ne = nat_in_journal(journal, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08002627
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002628 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002629 if (!ne) {
Yunlei He12f9ef32017-11-10 13:36:51 -08002630 ne = __alloc_nat_entry(nid, true);
2631 __init_nat_entry(nm_i, ne, &raw_ne, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002632 }
Chao Yu04d47e62016-11-17 20:53:11 +08002633
2634 /*
2635 * if a free nat in journal has not been used after last
2636 * checkpoint, we should remove it from available nids,
2637 * since later we will add it again.
2638 */
2639 if (!get_nat_flag(ne, IS_DIRTY) &&
2640 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2641 spin_lock(&nm_i->nid_list_lock);
2642 nm_i->available_nids--;
2643 spin_unlock(&nm_i->nid_list_lock);
2644 }
2645
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002646 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002647 }
Chao Yudfc08a12016-02-14 18:50:40 +08002648 update_nats_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002649 up_write(&curseg->journal_rwsem);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002650}
2651
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002652static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2653 struct list_head *head, int max)
2654{
2655 struct nat_entry_set *cur;
2656
2657 if (nes->entry_cnt >= max)
2658 goto add_out;
2659
2660 list_for_each_entry(cur, head, set_list) {
2661 if (cur->entry_cnt >= nes->entry_cnt) {
2662 list_add(&nes->set_list, cur->set_list.prev);
2663 return;
2664 }
2665 }
2666add_out:
2667 list_add_tail(&nes->set_list, head);
2668}
2669
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002670static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002671 struct page *page)
2672{
2673 struct f2fs_nm_info *nm_i = NM_I(sbi);
2674 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2675 struct f2fs_nat_block *nat_blk = page_address(page);
2676 int valid = 0;
Fan Li37a0ab22017-10-30 15:19:48 +08002677 int i = 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002678
2679 if (!enabled_nat_bits(sbi, NULL))
2680 return;
2681
Fan Li37a0ab22017-10-30 15:19:48 +08002682 if (nat_index == 0) {
2683 valid = 1;
2684 i = 1;
2685 }
2686 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
2687 if (nat_blk->entries[i].block_addr != NULL_ADDR)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002688 valid++;
2689 }
2690 if (valid == 0) {
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002691 __set_bit_le(nat_index, nm_i->empty_nat_bits);
2692 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002693 return;
2694 }
2695
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002696 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002697 if (valid == NAT_ENTRY_PER_BLOCK)
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002698 __set_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002699 else
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002700 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002701}
2702
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002703static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002704 struct nat_entry_set *set, struct cp_control *cpc)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002705{
2706 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002707 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002708 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
2709 bool to_journal = true;
2710 struct f2fs_nat_block *nat_blk;
2711 struct nat_entry *ne, *cur;
2712 struct page *page = NULL;
2713
2714 /*
2715 * there are two steps to flush nat entries:
2716 * #1, flush nat entries to journal in current hot data summary block.
2717 * #2, flush nat entries to nat page.
2718 */
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002719 if (enabled_nat_bits(sbi, cpc) ||
2720 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002721 to_journal = false;
2722
2723 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002724 down_write(&curseg->journal_rwsem);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002725 } else {
2726 page = get_next_nat_page(sbi, start_nid);
2727 nat_blk = page_address(page);
2728 f2fs_bug_on(sbi, !nat_blk);
2729 }
2730
2731 /* flush dirty nats in nat entry set */
2732 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
2733 struct f2fs_nat_entry *raw_ne;
2734 nid_t nid = nat_get_nid(ne);
2735 int offset;
2736
Chao Yufebeca62017-06-05 18:29:08 +08002737 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002738
2739 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08002740 offset = f2fs_lookup_journal_in_cursum(journal,
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002741 NAT_JOURNAL, nid, 1);
2742 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002743 raw_ne = &nat_in_journal(journal, offset);
2744 nid_in_journal(journal, offset) = cpu_to_le32(nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002745 } else {
2746 raw_ne = &nat_blk->entries[nid - start_nid];
2747 }
2748 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002749 nat_reset_flag(ne);
Kinglong Mee0b28b712017-02-28 21:34:47 +08002750 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
Chao Yu04d47e62016-11-17 20:53:11 +08002751 if (nat_get_blkaddr(ne) == NULL_ADDR) {
LiFan5921aaa2017-11-22 16:07:23 +08002752 add_free_nid(sbi, nid, false, true);
Chao Yu4ac91242017-02-23 10:53:49 +08002753 } else {
2754 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu346fe752017-03-13 20:10:41 +08002755 update_free_nid_bitmap(sbi, nid, false, false);
Chao Yu04d47e62016-11-17 20:53:11 +08002756 spin_unlock(&NM_I(sbi)->nid_list_lock);
2757 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002758 }
2759
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002760 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002761 up_write(&curseg->journal_rwsem);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002762 } else {
2763 __update_nat_bits(sbi, start_nid, page);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002764 f2fs_put_page(page, 1);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002765 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002766
Yunlei He59c90812017-03-13 20:22:18 +08002767 /* Allow dirty nats by node block allocation in write_begin */
2768 if (!set->entry_cnt) {
2769 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
2770 kmem_cache_free(nat_entry_set_slab, set);
2771 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002772}
2773
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002774/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002775 * This function is called during the checkpointing process.
2776 */
Chao Yu4d57b862018-05-30 00:20:41 +08002777void f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002778{
2779 struct f2fs_nm_info *nm_i = NM_I(sbi);
2780 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002781 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002782 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002783 struct nat_entry_set *set, *tmp;
2784 unsigned int found;
2785 nid_t set_idx = 0;
2786 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002787
Jaegeuk Kim7f2ecdd2018-06-28 19:34:40 -07002788 /* during unmount, let's flush nat_bits before checking dirty_nat_cnt */
2789 if (enabled_nat_bits(sbi, cpc)) {
2790 down_write(&nm_i->nat_tree_lock);
2791 remove_nats_in_journal(sbi);
2792 up_write(&nm_i->nat_tree_lock);
2793 }
2794
Changman Lee20d047c2014-11-25 12:44:23 +09002795 if (!nm_i->dirty_nat_cnt)
2796 return;
Jaegeuk Kima5131192016-01-02 09:19:41 -08002797
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002798 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002799
Chao Yuaec71382014-06-24 09:18:20 +08002800 /*
2801 * if there are no enough space in journal to store dirty nat
2802 * entries, remove all entries from journal and merge them
2803 * into nat entry set.
2804 */
Jaegeuk Kim900f7362017-02-27 21:28:53 -08002805 if (enabled_nat_bits(sbi, cpc) ||
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002806 !__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08002807 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002808
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002809 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002810 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002811 unsigned idx;
2812 set_idx = setvec[found - 1]->set + 1;
2813 for (idx = 0; idx < found; idx++)
2814 __adjust_nat_entry_set(setvec[idx], &sets,
Chao Yudfc08a12016-02-14 18:50:40 +08002815 MAX_NAT_JENTRIES(journal));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002816 }
Chao Yuaec71382014-06-24 09:18:20 +08002817
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002818 /* flush dirty nats in nat entry set */
2819 list_for_each_entry_safe(set, tmp, &sets, set_list)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002820 __flush_nat_entry_set(sbi, set, cpc);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002821
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002822 up_write(&nm_i->nat_tree_lock);
Yunlei He59c90812017-03-13 20:22:18 +08002823 /* Allow dirty nats by node block allocation in write_begin */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002824}
2825
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002826static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
2827{
2828 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2829 struct f2fs_nm_info *nm_i = NM_I(sbi);
2830 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
2831 unsigned int i;
2832 __u64 cp_ver = cur_cp_version(ckpt);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002833 block_t nat_bits_addr;
2834
2835 if (!enabled_nat_bits(sbi, NULL))
2836 return 0;
2837
Chao Yudf033ca2018-03-20 23:08:29 +08002838 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
Chao Yuacbf0542017-11-30 19:28:17 +08002839 nm_i->nat_bits = f2fs_kzalloc(sbi,
2840 nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002841 if (!nm_i->nat_bits)
2842 return -ENOMEM;
2843
2844 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
2845 nm_i->nat_bits_blocks;
2846 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
Chao Yu77357302018-07-17 00:02:17 +08002847 struct page *page;
2848
2849 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
2850 if (IS_ERR(page)) {
2851 disable_nat_bits(sbi, true);
2852 return PTR_ERR(page);
2853 }
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002854
2855 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
2856 page_address(page), F2FS_BLKSIZE);
2857 f2fs_put_page(page, 1);
2858 }
2859
Kinglong Meeced2c7e2017-02-25 19:53:39 +08002860 cp_ver |= (cur_cp_crc(ckpt) << 32);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002861 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
2862 disable_nat_bits(sbi, true);
2863 return 0;
2864 }
2865
2866 nm_i->full_nat_bits = nm_i->nat_bits + 8;
2867 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
2868
2869 f2fs_msg(sbi->sb, KERN_NOTICE, "Found nat_bits in checkpoint");
2870 return 0;
2871}
2872
Hou Pengyangbd80a4b2017-05-17 02:48:48 +00002873static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
Chao Yu7041d5d2017-03-08 20:07:49 +08002874{
2875 struct f2fs_nm_info *nm_i = NM_I(sbi);
2876 unsigned int i = 0;
2877 nid_t nid, last_nid;
2878
2879 if (!enabled_nat_bits(sbi, NULL))
2880 return;
2881
2882 for (i = 0; i < nm_i->nat_blocks; i++) {
2883 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
2884 if (i >= nm_i->nat_blocks)
2885 break;
2886
2887 __set_bit_le(i, nm_i->nat_block_bitmap);
2888
2889 nid = i * NAT_ENTRY_PER_BLOCK;
Fan Lif6986ed2017-11-02 11:02:52 +08002890 last_nid = nid + NAT_ENTRY_PER_BLOCK;
Chao Yu7041d5d2017-03-08 20:07:49 +08002891
Chao Yu346fe752017-03-13 20:10:41 +08002892 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08002893 for (; nid < last_nid; nid++)
Chao Yu346fe752017-03-13 20:10:41 +08002894 update_free_nid_bitmap(sbi, nid, true, true);
2895 spin_unlock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08002896 }
2897
2898 for (i = 0; i < nm_i->nat_blocks; i++) {
2899 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
2900 if (i >= nm_i->nat_blocks)
2901 break;
2902
2903 __set_bit_le(i, nm_i->nat_block_bitmap);
2904 }
2905}
2906
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002907static int init_node_manager(struct f2fs_sb_info *sbi)
2908{
2909 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
2910 struct f2fs_nm_info *nm_i = NM_I(sbi);
2911 unsigned char *version_bitmap;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002912 unsigned int nat_segs;
2913 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002914
2915 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
2916
2917 /* segment_count_nat includes pair segment so divide to 2. */
2918 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002919 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
2920 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09002921
Jaegeuk Kimb63da152014-02-17 12:44:20 +09002922 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yu04d47e62016-11-17 20:53:11 +08002923 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
Chao Yu292c196a2017-11-16 16:59:14 +08002924 sbi->nquota_files - F2FS_RESERVED_NODE_NUM;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002925 nm_i->nid_cnt[FREE_NID] = 0;
2926 nm_i->nid_cnt[PREALLOC_NID] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002927 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09002928 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Chao Yuea1a29a02015-10-12 17:08:48 +08002929 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
Chao Yu2304cb02016-01-18 18:32:58 +08002930 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002931
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002932 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002933 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002934 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2935 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002936 INIT_LIST_HEAD(&nm_i->nat_entries);
Chao Yu22969152018-08-05 23:08:59 +08002937 spin_lock_init(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002938
2939 mutex_init(&nm_i->build_lock);
Chao Yub8559dc2016-10-12 19:28:29 +08002940 spin_lock_init(&nm_i->nid_list_lock);
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002941 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002942
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002943 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002944 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002945 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2946 if (!version_bitmap)
2947 return -EFAULT;
2948
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002949 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2950 GFP_KERNEL);
2951 if (!nm_i->nat_bitmap)
2952 return -ENOMEM;
Chao Yu599a09b2017-01-07 18:52:01 +08002953
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002954 err = __get_nat_bitmaps(sbi);
2955 if (err)
2956 return err;
2957
Chao Yu599a09b2017-01-07 18:52:01 +08002958#ifdef CONFIG_F2FS_CHECK_FS
2959 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
2960 GFP_KERNEL);
2961 if (!nm_i->nat_bitmap_mir)
2962 return -ENOMEM;
2963#endif
2964
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002965 return 0;
2966}
2967
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002968static int init_free_nid_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002969{
2970 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08002971 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08002972
Kees Cook026f0502018-06-12 14:28:23 -07002973 nm_i->free_nid_bitmap =
2974 f2fs_kzalloc(sbi, array_size(sizeof(unsigned char *),
2975 nm_i->nat_blocks),
2976 GFP_KERNEL);
Chao Yu4ac91242017-02-23 10:53:49 +08002977 if (!nm_i->free_nid_bitmap)
2978 return -ENOMEM;
2979
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08002980 for (i = 0; i < nm_i->nat_blocks; i++) {
2981 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
Yunlei Hee15d54d2018-06-27 14:46:21 +08002982 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
Yunlei He68c43a22018-07-02 10:40:19 +08002983 if (!nm_i->free_nid_bitmap[i])
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08002984 return -ENOMEM;
2985 }
2986
Chao Yu628b3d12017-11-30 19:28:18 +08002987 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
Chao Yu4ac91242017-02-23 10:53:49 +08002988 GFP_KERNEL);
2989 if (!nm_i->nat_block_bitmap)
2990 return -ENOMEM;
Chao Yu586d1492017-03-01 17:09:07 +08002991
Kees Cook9d2a7892018-06-12 14:28:35 -07002992 nm_i->free_nid_count =
2993 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
2994 nm_i->nat_blocks),
2995 GFP_KERNEL);
Chao Yu586d1492017-03-01 17:09:07 +08002996 if (!nm_i->free_nid_count)
2997 return -ENOMEM;
Chao Yu4ac91242017-02-23 10:53:49 +08002998 return 0;
2999}
3000
Chao Yu4d57b862018-05-30 00:20:41 +08003001int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003002{
3003 int err;
3004
Chao Yuacbf0542017-11-30 19:28:17 +08003005 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3006 GFP_KERNEL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003007 if (!sbi->nm_info)
3008 return -ENOMEM;
3009
3010 err = init_node_manager(sbi);
3011 if (err)
3012 return err;
3013
Chao Yu4ac91242017-02-23 10:53:49 +08003014 err = init_free_nid_cache(sbi);
3015 if (err)
3016 return err;
3017
Chao Yu7041d5d2017-03-08 20:07:49 +08003018 /* load free nid status from nat_bits table */
3019 load_free_nid_bitmap(sbi);
3020
Chao Yue2374012018-06-15 14:45:57 +08003021 return f2fs_build_free_nids(sbi, true, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003022}
3023
Chao Yu4d57b862018-05-30 00:20:41 +08003024void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003025{
3026 struct f2fs_nm_info *nm_i = NM_I(sbi);
3027 struct free_nid *i, *next_i;
3028 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003029 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003030 nid_t nid = 0;
3031 unsigned int found;
3032
3033 if (!nm_i)
3034 return;
3035
3036 /* destroy free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08003037 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003038 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Fan Lia0761f62017-10-28 19:03:37 +08003039 __remove_free_nid(sbi, i, FREE_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08003040 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08003041 kmem_cache_free(free_nid_slab, i);
Chao Yub8559dc2016-10-12 19:28:29 +08003042 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003043 }
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003044 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3045 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3046 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
Chao Yub8559dc2016-10-12 19:28:29 +08003047 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003048
3049 /* destroy nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003050 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003051 while ((found = __gang_lookup_nat_cache(nm_i,
3052 nid, NATVEC_SIZE, natvec))) {
3053 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003054
Gu Zhengb6ce3912014-03-07 18:43:24 +08003055 nid = nat_get_nid(natvec[found - 1]) + 1;
Chao Yu22969152018-08-05 23:08:59 +08003056 for (idx = 0; idx < found; idx++) {
3057 spin_lock(&nm_i->nat_list_lock);
3058 list_del(&natvec[idx]->list);
3059 spin_unlock(&nm_i->nat_list_lock);
3060
Gu Zhengb6ce3912014-03-07 18:43:24 +08003061 __del_from_nat_cache(nm_i, natvec[idx]);
Chao Yu22969152018-08-05 23:08:59 +08003062 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003063 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003064 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003065
3066 /* destroy nat set cache */
3067 nid = 0;
3068 while ((found = __gang_lookup_nat_set(nm_i,
3069 nid, SETVEC_SIZE, setvec))) {
3070 unsigned idx;
3071
3072 nid = setvec[found - 1]->set + 1;
3073 for (idx = 0; idx < found; idx++) {
3074 /* entry_cnt is not zero, when cp_error was occurred */
3075 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3076 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3077 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3078 }
3079 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003080 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003081
Chao Yu4ac91242017-02-23 10:53:49 +08003082 kvfree(nm_i->nat_block_bitmap);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003083 if (nm_i->free_nid_bitmap) {
3084 int i;
3085
3086 for (i = 0; i < nm_i->nat_blocks; i++)
3087 kvfree(nm_i->free_nid_bitmap[i]);
3088 kfree(nm_i->free_nid_bitmap);
3089 }
Chao Yu586d1492017-03-01 17:09:07 +08003090 kvfree(nm_i->free_nid_count);
Chao Yu4ac91242017-02-23 10:53:49 +08003091
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003092 kfree(nm_i->nat_bitmap);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003093 kfree(nm_i->nat_bits);
Chao Yu599a09b2017-01-07 18:52:01 +08003094#ifdef CONFIG_F2FS_CHECK_FS
3095 kfree(nm_i->nat_bitmap_mir);
3096#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003097 sbi->nm_info = NULL;
3098 kfree(nm_i);
3099}
3100
Chao Yu4d57b862018-05-30 00:20:41 +08003101int __init f2fs_create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003102{
3103 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003104 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003105 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08003106 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003107
3108 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08003109 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08003110 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003111 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08003112
3113 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
3114 sizeof(struct nat_entry_set));
3115 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003116 goto destroy_free_nid;
Chao Yu50fa53e2018-08-02 23:03:19 +08003117
3118 fsync_node_entry_slab = f2fs_kmem_cache_create("fsync_node_entry",
3119 sizeof(struct fsync_node_entry));
3120 if (!fsync_node_entry_slab)
3121 goto destroy_nat_entry_set;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003122 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08003123
Chao Yu50fa53e2018-08-02 23:03:19 +08003124destroy_nat_entry_set:
3125 kmem_cache_destroy(nat_entry_set_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003126destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08003127 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003128destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08003129 kmem_cache_destroy(nat_entry_slab);
3130fail:
3131 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003132}
3133
Chao Yu4d57b862018-05-30 00:20:41 +08003134void f2fs_destroy_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003135{
Chao Yu50fa53e2018-08-02 23:03:19 +08003136 kmem_cache_destroy(fsync_node_entry_slab);
Chao Yuaec71382014-06-24 09:18:20 +08003137 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003138 kmem_cache_destroy(free_nid_slab);
3139 kmem_cache_destroy(nat_entry_slab);
3140}