blob: 81fb2f3edb5246d8950d795e1ffe0c899eb5099b [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002 * fs/f2fs/node.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/mpage.h>
14#include <linux/backing-dev.h>
15#include <linux/blkdev.h>
16#include <linux/pagevec.h>
17#include <linux/swap.h>
18
19#include "f2fs.h"
20#include "node.h"
21#include "segment.h"
Yunlei He87905682017-07-18 09:48:12 +080022#include "xattr.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080023#include "trace.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090024#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090025
Chao Yu4d57b862018-05-30 00:20:41 +080026#define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock)
Gu Zhengf978f5a2014-02-21 18:08:29 +080027
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090028static struct kmem_cache *nat_entry_slab;
29static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080030static struct kmem_cache *nat_entry_set_slab;
Chao Yu50fa53e2018-08-02 23:03:19 +080031static struct kmem_cache *fsync_node_entry_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090032
Jaegeuk Kima4f843b2018-04-23 23:02:31 -060033/*
34 * Check whether the given nid is within node id range.
35 */
Chao Yu4d57b862018-05-30 00:20:41 +080036int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kima4f843b2018-04-23 23:02:31 -060037{
38 if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
39 set_sbi_flag(sbi, SBI_NEED_FSCK);
40 f2fs_msg(sbi->sb, KERN_WARNING,
41 "%s: out-of-range nid=%x, run fsck to fix.",
42 __func__, nid);
43 return -EINVAL;
44 }
45 return 0;
46}
47
Chao Yu4d57b862018-05-30 00:20:41 +080048bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090049{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090050 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090051 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080052 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090053 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090054 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090055
56 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080057
58 /* only uses low memory */
59 avail_ram = val.totalram - val.totalhigh;
60
Chao Yu429511c2015-02-05 17:54:31 +080061 /*
62 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
63 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090064 if (type == FREE_NIDS) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +080065 mem_size = (nm_i->nid_cnt[FREE_NID] *
Chao Yub8559dc2016-10-12 19:28:29 +080066 sizeof(struct free_nid)) >> PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080067 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090068 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080069 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030070 PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080071 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kime589c2c2016-06-02 15:24:24 -070072 if (excess_cached_nats(sbi))
73 res = false;
Jaegeuk Kima1257022015-10-08 10:40:07 -070074 } else if (type == DIRTY_DENTS) {
75 if (sbi->sb->s_bdi->wb.dirty_exceeded)
76 return false;
77 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
78 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080079 } else if (type == INO_ENTRIES) {
80 int i;
81
Chao Yu39d787b2017-09-29 13:59:38 +080082 for (i = 0; i < MAX_INO_ENTRY; i++)
Kinglong Mee8f73cbb2017-03-18 09:26:13 +080083 mem_size += sbi->im[i].ino_num *
84 sizeof(struct ino_entry);
85 mem_size >>= PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080086 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080087 } else if (type == EXTENT_CACHE) {
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080088 mem_size = (atomic_read(&sbi->total_ext_tree) *
89 sizeof(struct extent_tree) +
Chao Yu429511c2015-02-05 17:54:31 +080090 atomic_read(&sbi->total_ext_node) *
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030091 sizeof(struct extent_node)) >> PAGE_SHIFT;
Chao Yu429511c2015-02-05 17:54:31 +080092 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -070093 } else if (type == INMEM_PAGES) {
94 /* it allows 20% / total_ram for inmemory pages */
95 mem_size = get_pages(sbi, F2FS_INMEM_PAGES);
96 res = mem_size < (val.totalram / 5);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080097 } else {
Jaegeuk Kim1663cae2016-01-09 16:14:08 -080098 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
99 return true;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900100 }
101 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +0900102}
103
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900104static void clear_node_page_dirty(struct page *page)
105{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900106 if (PageDirty(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +0800107 f2fs_clear_radix_tree_dirty_tag(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900108 clear_page_dirty_for_io(page);
Chao Yuaec2f722018-05-26 18:03:35 +0800109 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900110 }
111 ClearPageUptodate(page);
112}
113
114static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
115{
Chao Yu80551d12018-07-17 20:41:46 +0800116 return f2fs_get_meta_page_nofail(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900117}
118
119static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
120{
121 struct page *src_page;
122 struct page *dst_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900123 pgoff_t dst_off;
124 void *src_addr;
125 void *dst_addr;
126 struct f2fs_nm_info *nm_i = NM_I(sbi);
127
Chao Yu80551d12018-07-17 20:41:46 +0800128 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900129
130 /* get current nat block page with lock */
Chao Yu80551d12018-07-17 20:41:46 +0800131 src_page = get_current_nat_page(sbi, nid);
Chao Yu4d57b862018-05-30 00:20:41 +0800132 dst_page = f2fs_grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700133 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900134
135 src_addr = page_address(src_page);
136 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300137 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900138 set_page_dirty(dst_page);
139 f2fs_put_page(src_page, 1);
140
141 set_to_next_nat(nm_i, nid);
142
143 return dst_page;
144}
145
Yunlei He12f9ef32017-11-10 13:36:51 -0800146static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail)
147{
148 struct nat_entry *new;
149
150 if (no_fail)
Chao Yu2882d342018-01-25 18:57:25 +0800151 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800152 else
Chao Yu2882d342018-01-25 18:57:25 +0800153 new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He12f9ef32017-11-10 13:36:51 -0800154 if (new) {
155 nat_set_nid(new, nid);
156 nat_reset_flag(new);
157 }
158 return new;
159}
160
161static void __free_nat_entry(struct nat_entry *e)
162{
163 kmem_cache_free(nat_entry_slab, e);
164}
165
166/* must be locked by nat_tree_lock */
167static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
168 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
169{
170 if (no_fail)
171 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
172 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
173 return NULL;
174
175 if (raw_ne)
176 node_info_from_raw_nat(&ne->ni, raw_ne);
177 list_add_tail(&ne->list, &nm_i->nat_entries);
178 nm_i->nat_cnt++;
179 return ne;
180}
181
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900182static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
183{
184 return radix_tree_lookup(&nm_i->nat_root, n);
185}
186
187static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
188 nid_t start, unsigned int nr, struct nat_entry **ep)
189{
190 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
191}
192
193static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
194{
195 list_del(&e->list);
196 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
197 nm_i->nat_cnt--;
Yunlei He12f9ef32017-11-10 13:36:51 -0800198 __free_nat_entry(e);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900199}
200
Chao Yu780de472018-03-20 23:08:30 +0800201static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
202 struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700203{
204 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
205 struct nat_entry_set *head;
206
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700207 head = radix_tree_lookup(&nm_i->nat_set_root, set);
208 if (!head) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700209 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700210
211 INIT_LIST_HEAD(&head->entry_list);
212 INIT_LIST_HEAD(&head->set_list);
213 head->set = set;
214 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800215 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700216 }
Chao Yu780de472018-03-20 23:08:30 +0800217 return head;
218}
219
220static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
221 struct nat_entry *ne)
222{
223 struct nat_entry_set *head;
224 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
225
226 if (!new_ne)
227 head = __grab_nat_entry_set(nm_i, ne);
228
229 /*
230 * update entry_cnt in below condition:
231 * 1. update NEW_ADDR to valid block address;
232 * 2. update old block address to new one;
233 */
234 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
235 !get_nat_flag(ne, IS_DIRTY)))
236 head->entry_cnt++;
237
238 set_nat_flag(ne, IS_PREALLOC, new_ne);
Chao Yufebeca62017-06-05 18:29:08 +0800239
240 if (get_nat_flag(ne, IS_DIRTY))
241 goto refresh_list;
242
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700243 nm_i->dirty_nat_cnt++;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700244 set_nat_flag(ne, IS_DIRTY, true);
Chao Yufebeca62017-06-05 18:29:08 +0800245refresh_list:
Chao Yu780de472018-03-20 23:08:30 +0800246 if (new_ne)
Chao Yufebeca62017-06-05 18:29:08 +0800247 list_del_init(&ne->list);
248 else
249 list_move_tail(&ne->list, &head->entry_list);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700250}
251
252static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
Kinglong Mee0b28b712017-02-28 21:34:47 +0800253 struct nat_entry_set *set, struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700254{
Kinglong Mee0b28b712017-02-28 21:34:47 +0800255 list_move_tail(&ne->list, &nm_i->nat_entries);
256 set_nat_flag(ne, IS_DIRTY, false);
257 set->entry_cnt--;
258 nm_i->dirty_nat_cnt--;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700259}
260
261static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
262 nid_t start, unsigned int nr, struct nat_entry_set **ep)
263{
264 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
265 start, nr);
266}
267
Chao Yu50fa53e2018-08-02 23:03:19 +0800268bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
269{
270 return NODE_MAPPING(sbi) == page->mapping &&
271 IS_DNODE(page) && is_cold_node(page);
272}
273
274void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
275{
276 spin_lock_init(&sbi->fsync_node_lock);
277 INIT_LIST_HEAD(&sbi->fsync_node_list);
278 sbi->fsync_seg_id = 0;
279 sbi->fsync_node_num = 0;
280}
281
282static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
283 struct page *page)
284{
285 struct fsync_node_entry *fn;
286 unsigned long flags;
287 unsigned int seq_id;
288
289 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS);
290
291 get_page(page);
292 fn->page = page;
293 INIT_LIST_HEAD(&fn->list);
294
295 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
296 list_add_tail(&fn->list, &sbi->fsync_node_list);
297 fn->seq_id = sbi->fsync_seg_id++;
298 seq_id = fn->seq_id;
299 sbi->fsync_node_num++;
300 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
301
302 return seq_id;
303}
304
305void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
306{
307 struct fsync_node_entry *fn;
308 unsigned long flags;
309
310 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
311 list_for_each_entry(fn, &sbi->fsync_node_list, list) {
312 if (fn->page == page) {
313 list_del(&fn->list);
314 sbi->fsync_node_num--;
315 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
316 kmem_cache_free(fsync_node_entry_slab, fn);
317 put_page(page);
318 return;
319 }
320 }
321 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
322 f2fs_bug_on(sbi, 1);
323}
324
325void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
326{
327 unsigned long flags;
328
329 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
330 sbi->fsync_seg_id = 0;
331 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
332}
333
Chao Yu4d57b862018-05-30 00:20:41 +0800334int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700335{
336 struct f2fs_nm_info *nm_i = NM_I(sbi);
337 struct nat_entry *e;
338 bool need = false;
339
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700340 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700341 e = __lookup_nat_cache(nm_i, nid);
342 if (e) {
343 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
344 !get_nat_flag(e, HAS_FSYNCED_INODE))
345 need = true;
346 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700347 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700348 return need;
349}
350
Chao Yu4d57b862018-05-30 00:20:41 +0800351bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900352{
353 struct f2fs_nm_info *nm_i = NM_I(sbi);
354 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700355 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900356
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700357 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900358 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700359 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700360 is_cp = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700361 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900362 return is_cp;
363}
364
Chao Yu4d57b862018-05-30 00:20:41 +0800365bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900366{
367 struct f2fs_nm_info *nm_i = NM_I(sbi);
368 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700369 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900370
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700371 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700372 e = __lookup_nat_cache(nm_i, ino);
373 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
374 (get_nat_flag(e, IS_CHECKPOINTED) ||
375 get_nat_flag(e, HAS_FSYNCED_INODE)))
376 need_update = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700377 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700378 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900379}
380
Yunlei He12f9ef32017-11-10 13:36:51 -0800381/* must be locked by nat_tree_lock */
Chao Yu1515aef2016-02-19 18:12:28 +0800382static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900383 struct f2fs_nat_entry *ne)
384{
Chao Yu1515aef2016-02-19 18:12:28 +0800385 struct f2fs_nm_info *nm_i = NM_I(sbi);
Yunlei He12f9ef32017-11-10 13:36:51 -0800386 struct nat_entry *new, *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800387
Yunlei He12f9ef32017-11-10 13:36:51 -0800388 new = __alloc_nat_entry(nid, false);
389 if (!new)
390 return;
391
392 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900393 e = __lookup_nat_cache(nm_i, nid);
Yunlei He12f9ef32017-11-10 13:36:51 -0800394 if (!e)
395 e = __init_nat_entry(nm_i, new, ne, false);
396 else
Eric Biggers0c0b4712016-10-11 10:36:12 -0700397 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
398 nat_get_blkaddr(e) !=
399 le32_to_cpu(ne->block_addr) ||
Chao Yu1515aef2016-02-19 18:12:28 +0800400 nat_get_version(e) != ne->version);
Yunlei He12f9ef32017-11-10 13:36:51 -0800401 up_write(&nm_i->nat_tree_lock);
402 if (e != new)
403 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900404}
405
406static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900407 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900408{
409 struct f2fs_nm_info *nm_i = NM_I(sbi);
410 struct nat_entry *e;
Yunlei He12f9ef32017-11-10 13:36:51 -0800411 struct nat_entry *new = __alloc_nat_entry(ni->nid, true);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800412
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700413 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900414 e = __lookup_nat_cache(nm_i, ni->nid);
415 if (!e) {
Yunlei He12f9ef32017-11-10 13:36:51 -0800416 e = __init_nat_entry(nm_i, new, NULL, true);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800417 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700418 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900419 } else if (new_blkaddr == NEW_ADDR) {
420 /*
421 * when nid is reallocated,
422 * previous nat entry can be remained in nat cache.
423 * So, reinitialize it with new information.
424 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800425 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700426 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900427 }
Yunlei He12f9ef32017-11-10 13:36:51 -0800428 /* let's free early to reduce memory consumption */
429 if (e != new)
430 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900431
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900432 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700433 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
434 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900435 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700436 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900437 new_blkaddr == NEW_ADDR);
Chao Yue1da7872018-06-05 17:44:11 +0800438 f2fs_bug_on(sbi, is_valid_data_blkaddr(sbi, nat_get_blkaddr(e)) &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900439 new_blkaddr == NEW_ADDR);
440
arter97e1c42042014-08-06 23:22:50 +0900441 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900442 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
443 unsigned char version = nat_get_version(e);
444 nat_set_version(e, inc_node_version(version));
445 }
446
447 /* change address */
448 nat_set_blkaddr(e, new_blkaddr);
Chao Yue1da7872018-06-05 17:44:11 +0800449 if (!is_valid_data_blkaddr(sbi, new_blkaddr))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700450 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900451 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900452
453 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800454 if (ni->nid != ni->ino)
455 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700456 if (e) {
457 if (fsync_done && ni->nid == ni->ino)
458 set_nat_flag(e, HAS_FSYNCED_INODE, true);
459 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
460 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700461 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900462}
463
Chao Yu4d57b862018-05-30 00:20:41 +0800464int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900465{
466 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700467 int nr = nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900468
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700469 if (!down_write_trylock(&nm_i->nat_tree_lock))
470 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900471
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900472 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
473 struct nat_entry *ne;
474 ne = list_first_entry(&nm_i->nat_entries,
475 struct nat_entry, list);
476 __del_from_nat_cache(nm_i, ne);
477 nr_shrink--;
478 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700479 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700480 return nr - nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900481}
482
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900483/*
arter97e1c42042014-08-06 23:22:50 +0900484 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900485 */
Chao Yu77357302018-07-17 00:02:17 +0800486int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
Chao Yu4d57b862018-05-30 00:20:41 +0800487 struct node_info *ni)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900488{
489 struct f2fs_nm_info *nm_i = NM_I(sbi);
490 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +0800491 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900492 nid_t start_nid = START_NID(nid);
493 struct f2fs_nat_block *nat_blk;
494 struct page *page = NULL;
495 struct f2fs_nat_entry ne;
496 struct nat_entry *e;
Yunlei He66a82d12017-04-22 18:06:26 +0800497 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900498 int i;
499
500 ni->nid = nid;
501
502 /* Check nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700503 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900504 e = __lookup_nat_cache(nm_i, nid);
505 if (e) {
506 ni->ino = nat_get_ino(e);
507 ni->blk_addr = nat_get_blkaddr(e);
508 ni->version = nat_get_version(e);
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700509 up_read(&nm_i->nat_tree_lock);
Chao Yu77357302018-07-17 00:02:17 +0800510 return 0;
Chao Yu1515aef2016-02-19 18:12:28 +0800511 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900512
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800513 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
514
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900515 /* Check current segment summary */
Chao Yub7ad7512016-02-19 18:08:46 +0800516 down_read(&curseg->journal_rwsem);
Chao Yu4d57b862018-05-30 00:20:41 +0800517 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900518 if (i >= 0) {
Chao Yudfc08a12016-02-14 18:50:40 +0800519 ne = nat_in_journal(journal, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900520 node_info_from_raw_nat(ni, &ne);
521 }
Chao Yub7ad7512016-02-19 18:08:46 +0800522 up_read(&curseg->journal_rwsem);
Yunlei He66a82d12017-04-22 18:06:26 +0800523 if (i >= 0) {
524 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900525 goto cache;
Yunlei He66a82d12017-04-22 18:06:26 +0800526 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900527
528 /* Fill node_info from nat page */
Yunlei He66a82d12017-04-22 18:06:26 +0800529 index = current_nat_addr(sbi, nid);
530 up_read(&nm_i->nat_tree_lock);
531
Chao Yu4d57b862018-05-30 00:20:41 +0800532 page = f2fs_get_meta_page(sbi, index);
Chao Yu77357302018-07-17 00:02:17 +0800533 if (IS_ERR(page))
534 return PTR_ERR(page);
535
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900536 nat_blk = (struct f2fs_nat_block *)page_address(page);
537 ne = nat_blk->entries[nid - start_nid];
538 node_info_from_raw_nat(ni, &ne);
539 f2fs_put_page(page, 1);
540cache:
541 /* cache nat entry */
Chao Yu1515aef2016-02-19 18:12:28 +0800542 cache_nat_entry(sbi, nid, &ne);
Chao Yu77357302018-07-17 00:02:17 +0800543 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900544}
545
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700546/*
547 * readahead MAX_RA_NODE number of node pages.
548 */
Chao Yu4d57b862018-05-30 00:20:41 +0800549static void f2fs_ra_node_pages(struct page *parent, int start, int n)
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700550{
551 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
552 struct blk_plug plug;
553 int i, end;
554 nid_t nid;
555
556 blk_start_plug(&plug);
557
558 /* Then, try readahead for siblings of the desired node */
559 end = start + n;
560 end = min(end, NIDS_PER_BLOCK);
561 for (i = start; i < end; i++) {
562 nid = get_nid(parent, i, false);
Chao Yu4d57b862018-05-30 00:20:41 +0800563 f2fs_ra_node_page(sbi, nid);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700564 }
565
566 blk_finish_plug(&plug);
567}
568
Chao Yu4d57b862018-05-30 00:20:41 +0800569pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
Chao Yu3cf45742016-01-26 15:40:44 +0800570{
571 const long direct_index = ADDRS_PER_INODE(dn->inode);
572 const long direct_blks = ADDRS_PER_BLOCK;
573 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
574 unsigned int skipped_unit = ADDRS_PER_BLOCK;
575 int cur_level = dn->cur_level;
576 int max_level = dn->max_level;
577 pgoff_t base = 0;
578
579 if (!dn->max_level)
580 return pgofs + 1;
581
582 while (max_level-- > cur_level)
583 skipped_unit *= NIDS_PER_BLOCK;
584
585 switch (dn->max_level) {
586 case 3:
587 base += 2 * indirect_blks;
588 case 2:
589 base += 2 * direct_blks;
590 case 1:
591 base += direct_index;
592 break;
593 default:
594 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
595 }
596
597 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
598}
599
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900600/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900601 * The maximum depth is four.
602 * Offset[0] will have raw inode offset.
603 */
Chao Yu81ca7352016-01-26 15:39:35 +0800604static int get_node_path(struct inode *inode, long block,
Jaegeuk Kimde936532013-08-12 21:08:03 +0900605 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900606{
Chao Yu81ca7352016-01-26 15:39:35 +0800607 const long direct_index = ADDRS_PER_INODE(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900608 const long direct_blks = ADDRS_PER_BLOCK;
609 const long dptrs_per_blk = NIDS_PER_BLOCK;
610 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
611 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
612 int n = 0;
613 int level = 0;
614
615 noffset[0] = 0;
616
617 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900618 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900619 goto got;
620 }
621 block -= direct_index;
622 if (block < direct_blks) {
623 offset[n++] = NODE_DIR1_BLOCK;
624 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900625 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900626 level = 1;
627 goto got;
628 }
629 block -= direct_blks;
630 if (block < direct_blks) {
631 offset[n++] = NODE_DIR2_BLOCK;
632 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900633 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900634 level = 1;
635 goto got;
636 }
637 block -= direct_blks;
638 if (block < indirect_blks) {
639 offset[n++] = NODE_IND1_BLOCK;
640 noffset[n] = 3;
641 offset[n++] = block / direct_blks;
642 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900643 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900644 level = 2;
645 goto got;
646 }
647 block -= indirect_blks;
648 if (block < indirect_blks) {
649 offset[n++] = NODE_IND2_BLOCK;
650 noffset[n] = 4 + dptrs_per_blk;
651 offset[n++] = block / direct_blks;
652 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900653 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900654 level = 2;
655 goto got;
656 }
657 block -= indirect_blks;
658 if (block < dindirect_blks) {
659 offset[n++] = NODE_DIND_BLOCK;
660 noffset[n] = 5 + (dptrs_per_blk * 2);
661 offset[n++] = block / indirect_blks;
662 noffset[n] = 6 + (dptrs_per_blk * 2) +
663 offset[n - 1] * (dptrs_per_blk + 1);
664 offset[n++] = (block / direct_blks) % dptrs_per_blk;
665 noffset[n] = 7 + (dptrs_per_blk * 2) +
666 offset[n - 2] * (dptrs_per_blk + 1) +
667 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900668 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900669 level = 3;
670 goto got;
671 } else {
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700672 return -E2BIG;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900673 }
674got:
675 return level;
676}
677
678/*
679 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800680 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
681 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900682 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900683 */
Chao Yu4d57b862018-05-30 00:20:41 +0800684int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900685{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700686 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900687 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800688 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900689 int offset[4];
690 unsigned int noffset[4];
691 nid_t nids[4];
Chao Yu3cf45742016-01-26 15:40:44 +0800692 int level, i = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900693 int err = 0;
694
Chao Yu81ca7352016-01-26 15:39:35 +0800695 level = get_node_path(dn->inode, index, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700696 if (level < 0)
697 return level;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900698
699 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900700 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900701
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900702 if (!npage[0]) {
Chao Yu4d57b862018-05-30 00:20:41 +0800703 npage[0] = f2fs_get_node_page(sbi, nids[0]);
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900704 if (IS_ERR(npage[0]))
705 return PTR_ERR(npage[0]);
706 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800707
708 /* if inline_data is set, should not report any block indices */
709 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800710 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800711 f2fs_put_page(npage[0], 1);
712 goto release_out;
713 }
714
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900715 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900716 if (level != 0)
717 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900718 dn->inode_page = npage[0];
719 dn->inode_page_locked = true;
720
721 /* get indirect or direct nodes */
722 for (i = 1; i <= level; i++) {
723 bool done = false;
724
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900725 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900726 /* alloc new node */
Chao Yu4d57b862018-05-30 00:20:41 +0800727 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900728 err = -ENOSPC;
729 goto release_pages;
730 }
731
732 dn->nid = nids[i];
Chao Yu4d57b862018-05-30 00:20:41 +0800733 npage[i] = f2fs_new_node_page(dn, noffset[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900734 if (IS_ERR(npage[i])) {
Chao Yu4d57b862018-05-30 00:20:41 +0800735 f2fs_alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900736 err = PTR_ERR(npage[i]);
737 goto release_pages;
738 }
739
740 set_nid(parent, offset[i - 1], nids[i], i == 1);
Chao Yu4d57b862018-05-30 00:20:41 +0800741 f2fs_alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900742 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900743 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Chao Yu4d57b862018-05-30 00:20:41 +0800744 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900745 if (IS_ERR(npage[i])) {
746 err = PTR_ERR(npage[i]);
747 goto release_pages;
748 }
749 done = true;
750 }
751 if (i == 1) {
752 dn->inode_page_locked = false;
753 unlock_page(parent);
754 } else {
755 f2fs_put_page(parent, 1);
756 }
757
758 if (!done) {
Chao Yu4d57b862018-05-30 00:20:41 +0800759 npage[i] = f2fs_get_node_page(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900760 if (IS_ERR(npage[i])) {
761 err = PTR_ERR(npage[i]);
762 f2fs_put_page(npage[0], 0);
763 goto release_out;
764 }
765 }
766 if (i < level) {
767 parent = npage[i];
768 nids[i + 1] = get_nid(parent, offset[i], false);
769 }
770 }
771 dn->nid = nids[level];
772 dn->ofs_in_node = offset[level];
773 dn->node_page = npage[level];
Chao Yu7a2af762017-07-19 00:19:06 +0800774 dn->data_blkaddr = datablock_addr(dn->inode,
775 dn->node_page, dn->ofs_in_node);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900776 return 0;
777
778release_pages:
779 f2fs_put_page(parent, 1);
780 if (i > 1)
781 f2fs_put_page(npage[0], 0);
782release_out:
783 dn->inode_page = NULL;
784 dn->node_page = NULL;
Chao Yu3cf45742016-01-26 15:40:44 +0800785 if (err == -ENOENT) {
786 dn->cur_level = i;
787 dn->max_level = level;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700788 dn->ofs_in_node = offset[level];
Chao Yu3cf45742016-01-26 15:40:44 +0800789 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900790 return err;
791}
792
Chao Yu77357302018-07-17 00:02:17 +0800793static int truncate_node(struct dnode_of_data *dn)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900794{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700795 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900796 struct node_info ni;
Chao Yu77357302018-07-17 00:02:17 +0800797 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900798
Chao Yu77357302018-07-17 00:02:17 +0800799 err = f2fs_get_node_info(sbi, dn->nid, &ni);
800 if (err)
801 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900802
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900803 /* Deallocate node address */
Chao Yu4d57b862018-05-30 00:20:41 +0800804 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +0800805 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900806 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900807
808 if (dn->nid == dn->inode->i_ino) {
Chao Yu4d57b862018-05-30 00:20:41 +0800809 f2fs_remove_orphan_inode(sbi, dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900810 dec_valid_inode_count(sbi);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700811 f2fs_inode_synced(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900812 }
Chao Yu000519f2017-07-06 01:11:31 +0800813
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900814 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800815 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900816
817 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900818
819 invalidate_mapping_pages(NODE_MAPPING(sbi),
820 dn->node_page->index, dn->node_page->index);
821
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900822 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900823 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Chao Yu77357302018-07-17 00:02:17 +0800824
825 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900826}
827
828static int truncate_dnode(struct dnode_of_data *dn)
829{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900830 struct page *page;
Chao Yu77357302018-07-17 00:02:17 +0800831 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900832
833 if (dn->nid == 0)
834 return 1;
835
836 /* get direct node */
Chao Yu4d57b862018-05-30 00:20:41 +0800837 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900838 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
839 return 1;
840 else if (IS_ERR(page))
841 return PTR_ERR(page);
842
843 /* Make dnode_of_data for parameter */
844 dn->node_page = page;
845 dn->ofs_in_node = 0;
Chao Yu4d57b862018-05-30 00:20:41 +0800846 f2fs_truncate_data_blocks(dn);
Chao Yu77357302018-07-17 00:02:17 +0800847 err = truncate_node(dn);
848 if (err)
849 return err;
850
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900851 return 1;
852}
853
854static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
855 int ofs, int depth)
856{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900857 struct dnode_of_data rdn = *dn;
858 struct page *page;
859 struct f2fs_node *rn;
860 nid_t child_nid;
861 unsigned int child_nofs;
862 int freed = 0;
863 int i, ret;
864
865 if (dn->nid == 0)
866 return NIDS_PER_BLOCK + 1;
867
Namjae Jeon51dd6242013-04-20 01:28:52 +0900868 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
869
Chao Yu4d57b862018-05-30 00:20:41 +0800870 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900871 if (IS_ERR(page)) {
872 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900873 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900874 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900875
Chao Yu4d57b862018-05-30 00:20:41 +0800876 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700877
Gu Zheng45590712013-07-15 17:57:38 +0800878 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900879 if (depth < 3) {
880 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
881 child_nid = le32_to_cpu(rn->in.nid[i]);
882 if (child_nid == 0)
883 continue;
884 rdn.nid = child_nid;
885 ret = truncate_dnode(&rdn);
886 if (ret < 0)
887 goto out_err;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800888 if (set_nid(page, i, 0, false))
889 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900890 }
891 } else {
892 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
893 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
894 child_nid = le32_to_cpu(rn->in.nid[i]);
895 if (child_nid == 0) {
896 child_nofs += NIDS_PER_BLOCK + 1;
897 continue;
898 }
899 rdn.nid = child_nid;
900 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
901 if (ret == (NIDS_PER_BLOCK + 1)) {
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800902 if (set_nid(page, i, 0, false))
903 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900904 child_nofs += ret;
905 } else if (ret < 0 && ret != -ENOENT) {
906 goto out_err;
907 }
908 }
909 freed = child_nofs;
910 }
911
912 if (!ofs) {
913 /* remove current indirect node */
914 dn->node_page = page;
Chao Yu77357302018-07-17 00:02:17 +0800915 ret = truncate_node(dn);
916 if (ret)
917 goto out_err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900918 freed++;
919 } else {
920 f2fs_put_page(page, 1);
921 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900922 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900923 return freed;
924
925out_err:
926 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900927 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900928 return ret;
929}
930
931static int truncate_partial_nodes(struct dnode_of_data *dn,
932 struct f2fs_inode *ri, int *offset, int depth)
933{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900934 struct page *pages[2];
935 nid_t nid[3];
936 nid_t child_nid;
937 int err = 0;
938 int i;
939 int idx = depth - 2;
940
941 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
942 if (!nid[0])
943 return 0;
944
945 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800946 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900947 /* reference count'll be increased */
Chao Yu4d57b862018-05-30 00:20:41 +0800948 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900949 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900950 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800951 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900952 goto fail;
953 }
954 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
955 }
956
Chao Yu4d57b862018-05-30 00:20:41 +0800957 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700958
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900959 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800960 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900961 child_nid = get_nid(pages[idx], i, false);
962 if (!child_nid)
963 continue;
964 dn->nid = child_nid;
965 err = truncate_dnode(dn);
966 if (err < 0)
967 goto fail;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800968 if (set_nid(pages[idx], i, 0, false))
969 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900970 }
971
shifei10.gea225dca2013-10-29 15:32:34 +0800972 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900973 dn->node_page = pages[idx];
974 dn->nid = nid[idx];
Chao Yu77357302018-07-17 00:02:17 +0800975 err = truncate_node(dn);
976 if (err)
977 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900978 } else {
979 f2fs_put_page(pages[idx], 1);
980 }
981 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800982 offset[idx + 1] = 0;
983 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900984fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800985 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900986 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900987
988 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
989
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900990 return err;
991}
992
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900993/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900994 * All the block addresses of data and nodes should be nullified.
995 */
Chao Yu4d57b862018-05-30 00:20:41 +0800996int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900997{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700998 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999 int err = 0, cont = 1;
1000 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +09001001 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001002 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001003 struct dnode_of_data dn;
1004 struct page *page;
1005
Namjae Jeon51dd6242013-04-20 01:28:52 +09001006 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1007
Chao Yu81ca7352016-01-26 15:39:35 +08001008 level = get_node_path(inode, from, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -07001009 if (level < 0)
1010 return level;
Jaegeuk Kimff373552016-03-29 16:13:45 -07001011
Chao Yu4d57b862018-05-30 00:20:41 +08001012 page = f2fs_get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001013 if (IS_ERR(page)) {
1014 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001015 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001016 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017
1018 set_new_dnode(&dn, inode, page, NULL, 0);
1019 unlock_page(page);
1020
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001021 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001022 switch (level) {
1023 case 0:
1024 case 1:
1025 nofs = noffset[1];
1026 break;
1027 case 2:
1028 nofs = noffset[1];
1029 if (!offset[level - 1])
1030 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001031 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001032 if (err < 0 && err != -ENOENT)
1033 goto fail;
1034 nofs += 1 + NIDS_PER_BLOCK;
1035 break;
1036 case 3:
1037 nofs = 5 + 2 * NIDS_PER_BLOCK;
1038 if (!offset[level - 1])
1039 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001040 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001041 if (err < 0 && err != -ENOENT)
1042 goto fail;
1043 break;
1044 default:
1045 BUG();
1046 }
1047
1048skip_partial:
1049 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001050 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001051 switch (offset[0]) {
1052 case NODE_DIR1_BLOCK:
1053 case NODE_DIR2_BLOCK:
1054 err = truncate_dnode(&dn);
1055 break;
1056
1057 case NODE_IND1_BLOCK:
1058 case NODE_IND2_BLOCK:
1059 err = truncate_nodes(&dn, nofs, offset[1], 2);
1060 break;
1061
1062 case NODE_DIND_BLOCK:
1063 err = truncate_nodes(&dn, nofs, offset[1], 3);
1064 cont = 0;
1065 break;
1066
1067 default:
1068 BUG();
1069 }
1070 if (err < 0 && err != -ENOENT)
1071 goto fail;
1072 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001073 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001074 lock_page(page);
Jaegeuk Kimff373552016-03-29 16:13:45 -07001075 BUG_ON(page->mapping != NODE_MAPPING(sbi));
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001076 f2fs_wait_on_page_writeback(page, NODE, true);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001077 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001078 set_page_dirty(page);
1079 unlock_page(page);
1080 }
1081 offset[1] = 0;
1082 offset[0]++;
1083 nofs += err;
1084 }
1085fail:
1086 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001087 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001088 return err > 0 ? 0 : err;
1089}
1090
Jaegeuk Kim9c77f752017-10-19 11:48:57 -07001091/* caller must lock inode page */
Chao Yu4d57b862018-05-30 00:20:41 +08001092int f2fs_truncate_xattr_node(struct inode *inode)
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001093{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001094 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001095 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1096 struct dnode_of_data dn;
1097 struct page *npage;
Chao Yu77357302018-07-17 00:02:17 +08001098 int err;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001099
1100 if (!nid)
1101 return 0;
1102
Chao Yu4d57b862018-05-30 00:20:41 +08001103 npage = f2fs_get_node_page(sbi, nid);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001104 if (IS_ERR(npage))
1105 return PTR_ERR(npage);
1106
Chao Yu77357302018-07-17 00:02:17 +08001107 set_new_dnode(&dn, inode, NULL, npage, nid);
1108 err = truncate_node(&dn);
1109 if (err) {
1110 f2fs_put_page(npage, 1);
1111 return err;
1112 }
1113
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001114 f2fs_i_xnid_write(inode, 0);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001115
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001116 return 0;
1117}
1118
Jaegeuk Kim39936832012-11-22 16:21:29 +09001119/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001120 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1121 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +09001122 */
Chao Yu4d57b862018-05-30 00:20:41 +08001123int f2fs_remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001124{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001125 struct dnode_of_data dn;
Chao Yu13ec7292015-08-24 17:40:45 +08001126 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001127
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001128 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Chao Yu4d57b862018-05-30 00:20:41 +08001129 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
Chao Yu13ec7292015-08-24 17:40:45 +08001130 if (err)
1131 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001132
Chao Yu4d57b862018-05-30 00:20:41 +08001133 err = f2fs_truncate_xattr_node(inode);
Chao Yu13ec7292015-08-24 17:40:45 +08001134 if (err) {
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001135 f2fs_put_dnode(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +08001136 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001137 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001138
1139 /* remove potential inline_data blocks */
1140 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1141 S_ISLNK(inode->i_mode))
Chao Yu4d57b862018-05-30 00:20:41 +08001142 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001143
arter97e1c42042014-08-06 23:22:50 +09001144 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim8d714f82018-07-31 09:09:01 -07001145 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1146 f2fs_put_dnode(&dn);
1147 return -EIO;
1148 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001149 f2fs_bug_on(F2FS_I_SB(inode),
Chao Yu0eb0ada2017-06-14 23:00:56 +08001150 inode->i_blocks != 0 && inode->i_blocks != 8);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001151
1152 /* will put inode & node pages */
Chao Yu77357302018-07-17 00:02:17 +08001153 err = truncate_node(&dn);
1154 if (err) {
1155 f2fs_put_dnode(&dn);
1156 return err;
1157 }
Chao Yu13ec7292015-08-24 17:40:45 +08001158 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001159}
1160
Chao Yu4d57b862018-05-30 00:20:41 +08001161struct page *f2fs_new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001162{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001163 struct dnode_of_data dn;
1164
1165 /* allocate inode page for new inode */
1166 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +09001167
1168 /* caller should f2fs_put_page(page, 1); */
Chao Yu4d57b862018-05-30 00:20:41 +08001169 return f2fs_new_node_page(&dn, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001170}
1171
Chao Yu4d57b862018-05-30 00:20:41 +08001172struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001173{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001174 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001175 struct node_info new_ni;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001176 struct page *page;
1177 int err;
1178
Jaegeuk Kim91942322016-05-20 10:13:22 -07001179 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001180 return ERR_PTR(-EPERM);
1181
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001182 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001183 if (!page)
1184 return ERR_PTR(-ENOMEM);
1185
Chao Yu0abd6752017-07-09 00:13:07 +08001186 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001187 goto fail;
Chao Yu0abd6752017-07-09 00:13:07 +08001188
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001189#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu77357302018-07-17 00:02:17 +08001190 err = f2fs_get_node_info(sbi, dn->nid, &new_ni);
1191 if (err) {
1192 dec_valid_node_count(sbi, dn->inode, !ofs);
1193 goto fail;
1194 }
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001195 f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
1196#endif
1197 new_ni.nid = dn->nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001198 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001199 new_ni.blk_addr = NULL_ADDR;
1200 new_ni.flag = 0;
1201 new_ni.version = 0;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001202 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001203
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001204 f2fs_wait_on_page_writeback(page, NODE, true);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001205 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Chao Yuc5667572018-03-09 14:24:22 +08001206 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001207 if (!PageUptodate(page))
1208 SetPageUptodate(page);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001209 if (set_page_dirty(page))
1210 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001211
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001212 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001213 f2fs_i_xnid_write(dn->inode, dn->nid);
Jaegeuk Kim479bd732013-08-12 16:04:53 +09001214
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001215 if (ofs == 0)
1216 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001217 return page;
1218
1219fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +09001220 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001221 f2fs_put_page(page, 1);
1222 return ERR_PTR(err);
1223}
1224
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001225/*
1226 * Caller should do after getting the following values.
1227 * 0: f2fs_put_page(page, 0)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001228 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001229 */
Mike Christie04d328d2016-06-05 14:31:55 -05001230static int read_node_page(struct page *page, int op_flags)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001231{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001232 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001233 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001234 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001235 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001236 .type = NODE,
Mike Christie04d328d2016-06-05 14:31:55 -05001237 .op = REQ_OP_READ,
1238 .op_flags = op_flags,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001239 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001240 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001241 };
Chao Yu77357302018-07-17 00:02:17 +08001242 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001243
Weichao Guo54c55c42018-03-09 23:10:21 +08001244 if (PageUptodate(page)) {
1245#ifdef CONFIG_F2FS_CHECK_FS
1246 f2fs_bug_on(sbi, !f2fs_inode_chksum_verify(sbi, page));
1247#endif
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001248 return LOCKED_PAGE;
Weichao Guo54c55c42018-03-09 23:10:21 +08001249 }
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001250
Chao Yu77357302018-07-17 00:02:17 +08001251 err = f2fs_get_node_info(sbi, page->index, &ni);
1252 if (err)
1253 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001254
Jaegeuk Kim83a3bfd2018-06-21 13:46:23 -07001255 if (unlikely(ni.blk_addr == NULL_ADDR) ||
1256 is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001257 ClearPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001258 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001259 }
1260
Chao Yu7a9d7542016-02-22 18:36:38 +08001261 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001262 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001263}
1264
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001265/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001266 * Readahead a node page
1267 */
Chao Yu4d57b862018-05-30 00:20:41 +08001268void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001269{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001270 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001271 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001272
Chao Yue8458722016-01-08 20:13:37 +08001273 if (!nid)
1274 return;
Chao Yu4d57b862018-05-30 00:20:41 +08001275 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001276 return;
Chao Yue8458722016-01-08 20:13:37 +08001277
Fan Li999270d2016-02-29 14:29:51 +08001278 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001279 apage = radix_tree_lookup(&NODE_MAPPING(sbi)->i_pages, nid);
Fan Li999270d2016-02-29 14:29:51 +08001280 rcu_read_unlock();
1281 if (apage)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001282 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001283
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001284 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001285 if (!apage)
1286 return;
1287
Christoph Hellwig70246282016-07-19 11:28:41 +02001288 err = read_node_page(apage, REQ_RAHEAD);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001289 f2fs_put_page(apage, err ? 1 : 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001290}
1291
Jaegeuk Kim17a0ee52016-03-08 09:04:35 -08001292static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
Chao Yu0e022ea2016-01-05 16:52:46 +08001293 struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001294{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001295 struct page *page;
1296 int err;
Jaegeuk Kim4aa69d52015-12-23 14:17:47 -08001297
1298 if (!nid)
1299 return ERR_PTR(-ENOENT);
Chao Yu4d57b862018-05-30 00:20:41 +08001300 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kima4f843b2018-04-23 23:02:31 -06001301 return ERR_PTR(-EINVAL);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001302repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001303 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001304 if (!page)
1305 return ERR_PTR(-ENOMEM);
1306
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001307 err = read_node_page(page, 0);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001308 if (err < 0) {
1309 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001310 return ERR_PTR(err);
Chao Yue1c51b9f2015-12-11 16:08:22 +08001311 } else if (err == LOCKED_PAGE) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001312 err = 0;
Chao Yue1c51b9f2015-12-11 16:08:22 +08001313 goto page_hit;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001314 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001315
Chao Yu0e022ea2016-01-05 16:52:46 +08001316 if (parent)
Chao Yu4d57b862018-05-30 00:20:41 +08001317 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
Chao Yu0e022ea2016-01-05 16:52:46 +08001318
Chao Yue1c51b9f2015-12-11 16:08:22 +08001319 lock_page(page);
1320
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001321 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001322 f2fs_put_page(page, 1);
1323 goto repeat;
1324 }
Chao Yu1563ac72016-07-03 22:05:12 +08001325
Chao Yu1f258ec2017-06-07 11:17:35 +08001326 if (unlikely(!PageUptodate(page))) {
1327 err = -EIO;
Chao Yu1563ac72016-07-03 22:05:12 +08001328 goto out_err;
Chao Yu1f258ec2017-06-07 11:17:35 +08001329 }
Chao Yu704956e2017-07-31 20:19:09 +08001330
1331 if (!f2fs_inode_chksum_verify(sbi, page)) {
1332 err = -EBADMSG;
1333 goto out_err;
1334 }
Chao Yue1c51b9f2015-12-11 16:08:22 +08001335page_hit:
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001336 if(unlikely(nid != nid_of_node(page))) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001337 f2fs_msg(sbi->sb, KERN_WARNING, "inconsistent node block, "
1338 "nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1339 nid, nid_of_node(page), ino_of_node(page),
1340 ofs_of_node(page), cpver_of_node(page),
1341 next_blkaddr_of_node(page));
Chao Yu1f258ec2017-06-07 11:17:35 +08001342 err = -EINVAL;
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001343out_err:
Jaegeuk Kimee605232017-08-31 16:54:51 -07001344 ClearPageUptodate(page);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001345 f2fs_put_page(page, 1);
Chao Yu1f258ec2017-06-07 11:17:35 +08001346 return ERR_PTR(err);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001347 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001348 return page;
1349}
1350
Chao Yu4d57b862018-05-30 00:20:41 +08001351struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
Chao Yu0e022ea2016-01-05 16:52:46 +08001352{
1353 return __get_node_page(sbi, nid, NULL, 0);
1354}
1355
Chao Yu4d57b862018-05-30 00:20:41 +08001356struct page *f2fs_get_node_page_ra(struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001357{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001358 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Chao Yu0e022ea2016-01-05 16:52:46 +08001359 nid_t nid = get_nid(parent, start, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001360
Chao Yu0e022ea2016-01-05 16:52:46 +08001361 return __get_node_page(sbi, nid, parent, start);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001362}
1363
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001364static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1365{
1366 struct inode *inode;
1367 struct page *page;
Chao Yu0f3311a2016-05-21 00:11:09 +08001368 int ret;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001369
1370 /* should flush inline_data before evict_inode */
1371 inode = ilookup(sbi->sb, ino);
1372 if (!inode)
1373 return;
1374
Chao Yu01eccef2017-10-28 16:52:30 +08001375 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1376 FGP_LOCK|FGP_NOWAIT, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001377 if (!page)
1378 goto iput_out;
1379
1380 if (!PageUptodate(page))
1381 goto page_out;
1382
1383 if (!PageDirty(page))
1384 goto page_out;
1385
1386 if (!clear_page_dirty_for_io(page))
1387 goto page_out;
1388
Chao Yu0f3311a2016-05-21 00:11:09 +08001389 ret = f2fs_write_inline_data(inode, page);
1390 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08001391 f2fs_remove_dirty_inode(inode);
Chao Yu0f3311a2016-05-21 00:11:09 +08001392 if (ret)
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001393 set_page_dirty(page);
1394page_out:
Jaegeuk Kim4a6de502016-03-30 11:25:31 -07001395 f2fs_put_page(page, 1);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001396iput_out:
1397 iput(inode);
1398}
1399
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001400static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001401{
Jan Kara028a63a2017-11-15 17:34:51 -08001402 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001403 struct pagevec pvec;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001404 struct page *last_page = NULL;
Jan Kara028a63a2017-11-15 17:34:51 -08001405 int nr_pages;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001406
Mel Gorman86679822017-11-15 17:37:52 -08001407 pagevec_init(&pvec);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001408 index = 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001409
Jan Kara028a63a2017-11-15 17:34:51 -08001410 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001411 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001412 int i;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001413
1414 for (i = 0; i < nr_pages; i++) {
1415 struct page *page = pvec.pages[i];
1416
1417 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001418 f2fs_put_page(last_page, 0);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001419 pagevec_release(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001420 return ERR_PTR(-EIO);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001421 }
1422
1423 if (!IS_DNODE(page) || !is_cold_node(page))
1424 continue;
1425 if (ino_of_node(page) != ino)
1426 continue;
1427
1428 lock_page(page);
1429
1430 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1431continue_unlock:
1432 unlock_page(page);
1433 continue;
1434 }
1435 if (ino_of_node(page) != ino)
1436 goto continue_unlock;
1437
1438 if (!PageDirty(page)) {
1439 /* someone wrote it for us */
1440 goto continue_unlock;
1441 }
1442
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001443 if (last_page)
1444 f2fs_put_page(last_page, 0);
1445
1446 get_page(page);
1447 last_page = page;
1448 unlock_page(page);
1449 }
1450 pagevec_release(&pvec);
1451 cond_resched();
1452 }
1453 return last_page;
1454}
1455
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001456static int __write_node_page(struct page *page, bool atomic, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001457 struct writeback_control *wbc, bool do_balance,
Chao Yu50fa53e2018-08-02 23:03:19 +08001458 enum iostat_type io_type, unsigned int *seq_id)
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001459{
1460 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1461 nid_t nid;
1462 struct node_info ni;
1463 struct f2fs_io_info fio = {
1464 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001465 .ino = ino_of_node(page),
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001466 .type = NODE,
1467 .op = REQ_OP_WRITE,
1468 .op_flags = wbc_to_write_flags(wbc),
1469 .page = page,
1470 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001471 .submitted = false,
Chao Yub0af6d42017-08-02 23:21:48 +08001472 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001473 .io_wbc = wbc,
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001474 };
Chao Yu50fa53e2018-08-02 23:03:19 +08001475 unsigned int seq;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001476
1477 trace_f2fs_writepage(page, NODE);
1478
Jaegeuk Kim868de612018-05-04 18:04:22 -07001479 if (unlikely(f2fs_cp_error(sbi)))
1480 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08001481
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001482 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1483 goto redirty_out;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001484
Chao Yufd8c8ca2018-07-25 19:16:21 +08001485 if (wbc->sync_mode == WB_SYNC_NONE &&
1486 IS_DNODE(page) && is_cold_node(page))
1487 goto redirty_out;
1488
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001489 /* get old block addr of this node page */
1490 nid = nid_of_node(page);
1491 f2fs_bug_on(sbi, page->index != nid);
1492
Chao Yu77357302018-07-17 00:02:17 +08001493 if (f2fs_get_node_info(sbi, nid, &ni))
1494 goto redirty_out;
1495
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001496 if (wbc->for_reclaim) {
1497 if (!down_read_trylock(&sbi->node_write))
1498 goto redirty_out;
1499 } else {
1500 down_read(&sbi->node_write);
1501 }
1502
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001503 /* This page is already truncated */
1504 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1505 ClearPageUptodate(page);
1506 dec_page_count(sbi, F2FS_DIRTY_NODES);
1507 up_read(&sbi->node_write);
1508 unlock_page(page);
1509 return 0;
1510 }
1511
Chao Yuc9b60782018-08-01 19:13:44 +08001512 if (__is_valid_data_blkaddr(ni.blk_addr) &&
1513 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC))
1514 goto redirty_out;
1515
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001516 if (atomic && !test_opt(sbi, NOBARRIER))
1517 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1518
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001519 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001520 ClearPageError(page);
Chao Yu50fa53e2018-08-02 23:03:19 +08001521
1522 if (f2fs_in_warm_node_list(sbi, page)) {
1523 seq = f2fs_add_fsync_node_entry(sbi, page);
1524 if (seq_id)
1525 *seq_id = seq;
1526 }
1527
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001528 fio.old_blkaddr = ni.blk_addr;
Chao Yu4d57b862018-05-30 00:20:41 +08001529 f2fs_do_write_node_page(nid, &fio);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001530 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1531 dec_page_count(sbi, F2FS_DIRTY_NODES);
1532 up_read(&sbi->node_write);
1533
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001534 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001535 f2fs_submit_merged_write_cond(sbi, page->mapping->host, 0,
1536 page->index, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001537 submitted = NULL;
1538 }
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001539
1540 unlock_page(page);
1541
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001542 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001543 f2fs_submit_merged_write(sbi, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001544 submitted = NULL;
1545 }
1546 if (submitted)
1547 *submitted = fio.submitted;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001548
Yunlong Song401db792017-07-27 20:11:00 +08001549 if (do_balance)
1550 f2fs_balance_fs(sbi, false);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001551 return 0;
1552
1553redirty_out:
1554 redirty_page_for_writepage(wbc, page);
1555 return AOP_WRITEPAGE_ACTIVATE;
1556}
1557
Chao Yu4d57b862018-05-30 00:20:41 +08001558void f2fs_move_node_page(struct page *node_page, int gc_type)
Yunlei Hef15194f2017-10-30 14:18:55 +08001559{
1560 if (gc_type == FG_GC) {
1561 struct writeback_control wbc = {
1562 .sync_mode = WB_SYNC_ALL,
1563 .nr_to_write = 1,
1564 .for_reclaim = 0,
1565 };
1566
1567 set_page_dirty(node_page);
1568 f2fs_wait_on_page_writeback(node_page, NODE, true);
1569
1570 f2fs_bug_on(F2FS_P_SB(node_page), PageWriteback(node_page));
1571 if (!clear_page_dirty_for_io(node_page))
1572 goto out_page;
1573
1574 if (__write_node_page(node_page, false, NULL,
Chao Yu50fa53e2018-08-02 23:03:19 +08001575 &wbc, false, FS_GC_NODE_IO, NULL))
Yunlei Hef15194f2017-10-30 14:18:55 +08001576 unlock_page(node_page);
1577 goto release_page;
1578 } else {
1579 /* set page dirty and write it */
1580 if (!PageWriteback(node_page))
1581 set_page_dirty(node_page);
1582 }
1583out_page:
1584 unlock_page(node_page);
1585release_page:
1586 f2fs_put_page(node_page, 0);
1587}
1588
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001589static int f2fs_write_node_page(struct page *page,
1590 struct writeback_control *wbc)
1591{
Chao Yu50fa53e2018-08-02 23:03:19 +08001592 return __write_node_page(page, false, NULL, wbc, false,
1593 FS_NODE_IO, NULL);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001594}
1595
Chao Yu4d57b862018-05-30 00:20:41 +08001596int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
Chao Yu50fa53e2018-08-02 23:03:19 +08001597 struct writeback_control *wbc, bool atomic,
1598 unsigned int *seq_id)
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001599{
Jan Kara028a63a2017-11-15 17:34:51 -08001600 pgoff_t index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001601 pgoff_t last_idx = ULONG_MAX;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001602 struct pagevec pvec;
1603 int ret = 0;
1604 struct page *last_page = NULL;
1605 bool marked = false;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001606 nid_t ino = inode->i_ino;
Jan Kara028a63a2017-11-15 17:34:51 -08001607 int nr_pages;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001608
1609 if (atomic) {
1610 last_page = last_fsync_dnode(sbi, ino);
1611 if (IS_ERR_OR_NULL(last_page))
1612 return PTR_ERR_OR_ZERO(last_page);
1613 }
1614retry:
Mel Gorman86679822017-11-15 17:37:52 -08001615 pagevec_init(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001616 index = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001617
Jan Kara028a63a2017-11-15 17:34:51 -08001618 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001619 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001620 int i;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001621
1622 for (i = 0; i < nr_pages; i++) {
1623 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001624 bool submitted = false;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001625
1626 if (unlikely(f2fs_cp_error(sbi))) {
1627 f2fs_put_page(last_page, 0);
1628 pagevec_release(&pvec);
Chao Yu9de69272016-10-11 22:57:06 +08001629 ret = -EIO;
1630 goto out;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001631 }
1632
1633 if (!IS_DNODE(page) || !is_cold_node(page))
1634 continue;
1635 if (ino_of_node(page) != ino)
1636 continue;
1637
1638 lock_page(page);
1639
1640 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1641continue_unlock:
1642 unlock_page(page);
1643 continue;
1644 }
1645 if (ino_of_node(page) != ino)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001646 goto continue_unlock;
1647
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001648 if (!PageDirty(page) && page != last_page) {
1649 /* someone wrote it for us */
1650 goto continue_unlock;
1651 }
1652
1653 f2fs_wait_on_page_writeback(page, NODE, true);
1654 BUG_ON(PageWriteback(page));
1655
Jaegeuk Kimd29fd172017-04-12 12:02:00 -07001656 set_fsync_mark(page, 0);
1657 set_dentry_mark(page, 0);
1658
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001659 if (!atomic || page == last_page) {
1660 set_fsync_mark(page, 1);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001661 if (IS_INODE(page)) {
1662 if (is_inode_flag_set(inode,
1663 FI_DIRTY_INODE))
Chao Yu4d57b862018-05-30 00:20:41 +08001664 f2fs_update_inode(inode, page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001665 set_dentry_mark(page,
Chao Yu4d57b862018-05-30 00:20:41 +08001666 f2fs_need_dentry_mark(sbi, ino));
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001667 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001668 /* may be written by other thread */
1669 if (!PageDirty(page))
1670 set_page_dirty(page);
1671 }
1672
1673 if (!clear_page_dirty_for_io(page))
1674 goto continue_unlock;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001675
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001676 ret = __write_node_page(page, atomic &&
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001677 page == last_page,
Chao Yub0af6d42017-08-02 23:21:48 +08001678 &submitted, wbc, true,
Chao Yu50fa53e2018-08-02 23:03:19 +08001679 FS_NODE_IO, seq_id);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001680 if (ret) {
Jaegeuk Kim52681372016-04-13 16:24:44 -07001681 unlock_page(page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001682 f2fs_put_page(last_page, 0);
1683 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001684 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001685 last_idx = page->index;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001686 }
Chao Yu3f5f4952016-09-29 18:50:10 +08001687
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001688 if (page == last_page) {
1689 f2fs_put_page(page, 0);
1690 marked = true;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001691 break;
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001692 }
Jaegeuk Kim52681372016-04-13 16:24:44 -07001693 }
1694 pagevec_release(&pvec);
1695 cond_resched();
1696
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001697 if (ret || marked)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001698 break;
1699 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001700 if (!ret && atomic && !marked) {
1701 f2fs_msg(sbi->sb, KERN_DEBUG,
1702 "Retry to write fsync mark: ino=%u, idx=%lx",
1703 ino, last_page->index);
1704 lock_page(last_page);
Yunlei Hed40a43a2016-11-16 17:26:24 +08001705 f2fs_wait_on_page_writeback(last_page, NODE, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001706 set_page_dirty(last_page);
1707 unlock_page(last_page);
1708 goto retry;
1709 }
Chao Yu9de69272016-10-11 22:57:06 +08001710out:
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001711 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001712 f2fs_submit_merged_write_cond(sbi, NULL, ino, last_idx, NODE);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001713 return ret ? -EIO: 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001714}
1715
Chao Yu4d57b862018-05-30 00:20:41 +08001716int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
1717 struct writeback_control *wbc,
Chao Yub0af6d42017-08-02 23:21:48 +08001718 bool do_balance, enum iostat_type io_type)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001719{
Jan Kara028a63a2017-11-15 17:34:51 -08001720 pgoff_t index;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001721 struct pagevec pvec;
1722 int step = 0;
Jaegeuk Kim12bb0a82016-03-11 15:33:22 -08001723 int nwritten = 0;
Chao Yu3f5f4952016-09-29 18:50:10 +08001724 int ret = 0;
Chao Yuc29fd0c2018-06-04 23:20:36 +08001725 int nr_pages, done = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001726
Mel Gorman86679822017-11-15 17:37:52 -08001727 pagevec_init(&pvec);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001728
1729next_step:
1730 index = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001731
Chao Yuc29fd0c2018-06-04 23:20:36 +08001732 while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
1733 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001734 int i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001735
1736 for (i = 0; i < nr_pages; i++) {
1737 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001738 bool submitted = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001739
Chao Yuc29fd0c2018-06-04 23:20:36 +08001740 /* give a priority to WB_SYNC threads */
1741 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
1742 wbc->sync_mode == WB_SYNC_NONE) {
1743 done = 1;
1744 break;
1745 }
1746
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001747 /*
1748 * flushing sequence with step:
1749 * 0. indirect nodes
1750 * 1. dentry dnodes
1751 * 2. file dnodes
1752 */
1753 if (step == 0 && IS_DNODE(page))
1754 continue;
1755 if (step == 1 && (!IS_DNODE(page) ||
1756 is_cold_node(page)))
1757 continue;
1758 if (step == 2 && (!IS_DNODE(page) ||
1759 !is_cold_node(page)))
1760 continue;
Chao Yu9a4cbc92016-02-22 18:35:46 +08001761lock_node:
Chao Yu4b270a82018-07-04 18:04:10 +08001762 if (wbc->sync_mode == WB_SYNC_ALL)
1763 lock_page(page);
1764 else if (!trylock_page(page))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001765 continue;
1766
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001767 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001768continue_unlock:
1769 unlock_page(page);
1770 continue;
1771 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001772
1773 if (!PageDirty(page)) {
1774 /* someone wrote it for us */
1775 goto continue_unlock;
1776 }
1777
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001778 /* flush inline_data */
Jaegeuk Kim52681372016-04-13 16:24:44 -07001779 if (is_inline_node(page)) {
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001780 clear_inline_node(page);
1781 unlock_page(page);
1782 flush_inline_data(sbi, ino_of_node(page));
Chao Yu9a4cbc92016-02-22 18:35:46 +08001783 goto lock_node;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001784 }
1785
Jaegeuk Kimfa3d2bd2016-01-28 11:48:52 -08001786 f2fs_wait_on_page_writeback(page, NODE, true);
1787
1788 BUG_ON(PageWriteback(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001789 if (!clear_page_dirty_for_io(page))
1790 goto continue_unlock;
1791
Jaegeuk Kim52681372016-04-13 16:24:44 -07001792 set_fsync_mark(page, 0);
1793 set_dentry_mark(page, 0);
Jaegeuk Kim52746512014-08-11 18:18:36 -07001794
Yunlong Song401db792017-07-27 20:11:00 +08001795 ret = __write_node_page(page, false, &submitted,
Chao Yu50fa53e2018-08-02 23:03:19 +08001796 wbc, do_balance, io_type, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001797 if (ret)
Jaegeuk Kim52746512014-08-11 18:18:36 -07001798 unlock_page(page);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001799 else if (submitted)
Chao Yu3f5f4952016-09-29 18:50:10 +08001800 nwritten++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001801
1802 if (--wbc->nr_to_write == 0)
1803 break;
1804 }
1805 pagevec_release(&pvec);
1806 cond_resched();
1807
1808 if (wbc->nr_to_write == 0) {
1809 step = 2;
1810 break;
1811 }
1812 }
1813
1814 if (step < 2) {
Chao Yufd8c8ca2018-07-25 19:16:21 +08001815 if (wbc->sync_mode == WB_SYNC_NONE && step == 1)
1816 goto out;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001817 step++;
1818 goto next_step;
1819 }
Chao Yufd8c8ca2018-07-25 19:16:21 +08001820out:
Chao Yu3f5f4952016-09-29 18:50:10 +08001821 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001822 f2fs_submit_merged_write(sbi, NODE);
Chao Yudb198ae2018-01-18 17:29:10 +08001823
1824 if (unlikely(f2fs_cp_error(sbi)))
1825 return -EIO;
Chao Yu3f5f4952016-09-29 18:50:10 +08001826 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001827}
1828
Chao Yu50fa53e2018-08-02 23:03:19 +08001829int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
1830 unsigned int seq_id)
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001831{
Chao Yu50fa53e2018-08-02 23:03:19 +08001832 struct fsync_node_entry *fn;
1833 struct page *page;
1834 struct list_head *head = &sbi->fsync_node_list;
1835 unsigned long flags;
1836 unsigned int cur_seq_id = 0;
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001837 int ret2, ret = 0;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001838
Chao Yu50fa53e2018-08-02 23:03:19 +08001839 while (seq_id && cur_seq_id < seq_id) {
1840 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
1841 if (list_empty(head)) {
1842 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1843 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001844 }
Chao Yu50fa53e2018-08-02 23:03:19 +08001845 fn = list_first_entry(head, struct fsync_node_entry, list);
1846 if (fn->seq_id > seq_id) {
1847 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1848 break;
1849 }
1850 cur_seq_id = fn->seq_id;
1851 page = fn->page;
1852 get_page(page);
1853 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1854
1855 f2fs_wait_on_page_writeback(page, NODE, true);
1856 if (TestClearPageError(page))
1857 ret = -EIO;
1858
1859 put_page(page);
1860
1861 if (ret)
1862 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001863 }
1864
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001865 ret2 = filemap_check_errors(NODE_MAPPING(sbi));
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001866 if (!ret)
1867 ret = ret2;
Chao Yu50fa53e2018-08-02 23:03:19 +08001868
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001869 return ret;
1870}
1871
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001872static int f2fs_write_node_pages(struct address_space *mapping,
1873 struct writeback_control *wbc)
1874{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001875 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001876 struct blk_plug plug;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001877 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001878
Chao Yu0771fcc2017-06-29 23:20:45 +08001879 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1880 goto skip_write;
1881
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001882 /* balancing f2fs's metadata in background */
1883 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001884
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001885 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001886 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001887 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001888
Chao Yuc29fd0c2018-06-04 23:20:36 +08001889 if (wbc->sync_mode == WB_SYNC_ALL)
1890 atomic_inc(&sbi->wb_sync_req[NODE]);
1891 else if (atomic_read(&sbi->wb_sync_req[NODE]))
1892 goto skip_write;
1893
Yunlei Hed31c7c32016-02-04 16:14:00 +08001894 trace_f2fs_writepages(mapping->host, wbc, NODE);
1895
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001896 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001897 blk_start_plug(&plug);
Chao Yu4d57b862018-05-30 00:20:41 +08001898 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001899 blk_finish_plug(&plug);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001900 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Chao Yuc29fd0c2018-06-04 23:20:36 +08001901
1902 if (wbc->sync_mode == WB_SYNC_ALL)
1903 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001904 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001905
1906skip_write:
1907 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001908 trace_f2fs_writepages(mapping->host, wbc, NODE);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001909 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001910}
1911
1912static int f2fs_set_node_page_dirty(struct page *page)
1913{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001914 trace_f2fs_set_page_dirty(page, NODE);
1915
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001916 if (!PageUptodate(page))
1917 SetPageUptodate(page);
Weichao Guo54c55c42018-03-09 23:10:21 +08001918#ifdef CONFIG_F2FS_CHECK_FS
1919 if (IS_INODE(page))
1920 f2fs_inode_chksum_set(F2FS_P_SB(page), page);
1921#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001922 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07001923 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001924 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001925 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001926 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001927 return 1;
1928 }
1929 return 0;
1930}
1931
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001932/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001933 * Structure of the f2fs node operations
1934 */
1935const struct address_space_operations f2fs_node_aops = {
1936 .writepage = f2fs_write_node_page,
1937 .writepages = f2fs_write_node_pages,
1938 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001939 .invalidatepage = f2fs_invalidate_page,
1940 .releasepage = f2fs_release_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08001941#ifdef CONFIG_MIGRATION
1942 .migratepage = f2fs_migrate_page,
1943#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001944};
1945
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001946static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1947 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001948{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001949 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001950}
1951
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001952static int __insert_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08001953 struct free_nid *i, enum nid_state state)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001954{
Chao Yub8559dc2016-10-12 19:28:29 +08001955 struct f2fs_nm_info *nm_i = NM_I(sbi);
1956
Fan Lia0761f62017-10-28 19:03:37 +08001957 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
1958 if (err)
1959 return err;
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07001960
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001961 f2fs_bug_on(sbi, state != i->state);
1962 nm_i->nid_cnt[state]++;
1963 if (state == FREE_NID)
1964 list_add_tail(&i->list, &nm_i->free_nid_list);
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07001965 return 0;
Chao Yub8559dc2016-10-12 19:28:29 +08001966}
1967
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001968static void __remove_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08001969 struct free_nid *i, enum nid_state state)
Chao Yub8559dc2016-10-12 19:28:29 +08001970{
1971 struct f2fs_nm_info *nm_i = NM_I(sbi);
1972
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001973 f2fs_bug_on(sbi, state != i->state);
1974 nm_i->nid_cnt[state]--;
1975 if (state == FREE_NID)
1976 list_del(&i->list);
Fan Lia0761f62017-10-28 19:03:37 +08001977 radix_tree_delete(&nm_i->free_nid_root, i->nid);
1978}
1979
1980static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
1981 enum nid_state org_state, enum nid_state dst_state)
1982{
1983 struct f2fs_nm_info *nm_i = NM_I(sbi);
1984
1985 f2fs_bug_on(sbi, org_state != i->state);
1986 i->state = dst_state;
1987 nm_i->nid_cnt[org_state]--;
1988 nm_i->nid_cnt[dst_state]++;
1989
1990 switch (dst_state) {
1991 case PREALLOC_NID:
1992 list_del(&i->list);
1993 break;
1994 case FREE_NID:
1995 list_add_tail(&i->list, &nm_i->free_nid_list);
1996 break;
1997 default:
1998 BUG_ON(1);
1999 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002000}
2001
LiFan5921aaa2017-11-22 16:07:23 +08002002static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2003 bool set, bool build)
2004{
2005 struct f2fs_nm_info *nm_i = NM_I(sbi);
2006 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2007 unsigned int nid_ofs = nid - START_NID(nid);
2008
2009 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2010 return;
2011
2012 if (set) {
2013 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2014 return;
2015 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2016 nm_i->free_nid_count[nat_ofs]++;
2017 } else {
2018 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2019 return;
2020 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2021 if (!build)
2022 nm_i->free_nid_count[nat_ofs]--;
2023 }
2024}
2025
Chao Yu4ac91242017-02-23 10:53:49 +08002026/* return if the nid is recognized as free */
LiFan5921aaa2017-11-22 16:07:23 +08002027static bool add_free_nid(struct f2fs_sb_info *sbi,
2028 nid_t nid, bool build, bool update)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002029{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002030 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu30a61dd2017-03-22 14:45:05 +08002031 struct free_nid *i, *e;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002032 struct nat_entry *ne;
Chao Yu30a61dd2017-03-22 14:45:05 +08002033 int err = -EINVAL;
2034 bool ret = false;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09002035
2036 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08002037 if (unlikely(nid == 0))
Chao Yu4ac91242017-02-23 10:53:49 +08002038 return false;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09002039
Gu Zheng7bd59382013-10-22 14:52:26 +08002040 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002041 i->nid = nid;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002042 i->state = FREE_NID;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002043
LiFan5921aaa2017-11-22 16:07:23 +08002044 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002045
Chao Yub8559dc2016-10-12 19:28:29 +08002046 spin_lock(&nm_i->nid_list_lock);
Chao Yu30a61dd2017-03-22 14:45:05 +08002047
2048 if (build) {
2049 /*
2050 * Thread A Thread B
2051 * - f2fs_create
2052 * - f2fs_new_inode
Chao Yu4d57b862018-05-30 00:20:41 +08002053 * - f2fs_alloc_nid
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002054 * - __insert_nid_to_list(PREALLOC_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002055 * - f2fs_balance_fs_bg
Chao Yu4d57b862018-05-30 00:20:41 +08002056 * - f2fs_build_free_nids
2057 * - __f2fs_build_free_nids
Chao Yu30a61dd2017-03-22 14:45:05 +08002058 * - scan_nat_page
2059 * - add_free_nid
2060 * - __lookup_nat_cache
2061 * - f2fs_add_link
Chao Yu4d57b862018-05-30 00:20:41 +08002062 * - f2fs_init_inode_metadata
2063 * - f2fs_new_inode_page
2064 * - f2fs_new_node_page
Chao Yu30a61dd2017-03-22 14:45:05 +08002065 * - set_node_addr
Chao Yu4d57b862018-05-30 00:20:41 +08002066 * - f2fs_alloc_nid_done
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002067 * - __remove_nid_from_list(PREALLOC_NID)
2068 * - __insert_nid_to_list(FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002069 */
2070 ne = __lookup_nat_cache(nm_i, nid);
2071 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2072 nat_get_blkaddr(ne) != NULL_ADDR))
2073 goto err_out;
2074
2075 e = __lookup_free_nid_list(nm_i, nid);
2076 if (e) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002077 if (e->state == FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08002078 ret = true;
2079 goto err_out;
2080 }
2081 }
2082 ret = true;
Fan Lia0761f62017-10-28 19:03:37 +08002083 err = __insert_free_nid(sbi, i, FREE_NID);
Chao Yu30a61dd2017-03-22 14:45:05 +08002084err_out:
LiFan5921aaa2017-11-22 16:07:23 +08002085 if (update) {
2086 update_free_nid_bitmap(sbi, nid, ret, build);
2087 if (!build)
2088 nm_i->available_nids++;
2089 }
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07002090 spin_unlock(&nm_i->nid_list_lock);
2091 radix_tree_preload_end();
LiFan5921aaa2017-11-22 16:07:23 +08002092
Chao Yu30a61dd2017-03-22 14:45:05 +08002093 if (err)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002094 kmem_cache_free(free_nid_slab, i);
Chao Yu30a61dd2017-03-22 14:45:05 +08002095 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002096}
2097
Chao Yub8559dc2016-10-12 19:28:29 +08002098static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002099{
Chao Yub8559dc2016-10-12 19:28:29 +08002100 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002101 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002102 bool need_free = false;
2103
Chao Yub8559dc2016-10-12 19:28:29 +08002104 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002105 i = __lookup_free_nid_list(nm_i, nid);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002106 if (i && i->state == FREE_NID) {
Fan Lia0761f62017-10-28 19:03:37 +08002107 __remove_free_nid(sbi, i, FREE_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002108 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002109 }
Chao Yub8559dc2016-10-12 19:28:29 +08002110 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002111
2112 if (need_free)
2113 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002114}
2115
Chao Yue2374012018-06-15 14:45:57 +08002116static int scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002117 struct page *nat_page, nid_t start_nid)
2118{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002119 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002120 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2121 block_t blk_addr;
Chao Yu4ac91242017-02-23 10:53:49 +08002122 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002123 int i;
2124
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002125 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
Chao Yu4ac91242017-02-23 10:53:49 +08002126
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002127 i = start_nid % NAT_ENTRY_PER_BLOCK;
2128
2129 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Chao Yucfb271d2013-12-05 17:15:22 +08002130 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09002131 break;
Haicheng Li23d38842013-05-06 23:15:43 +08002132
2133 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Chao Yue2374012018-06-15 14:45:57 +08002134
2135 if (blk_addr == NEW_ADDR)
2136 return -EINVAL;
2137
LiFan5921aaa2017-11-22 16:07:23 +08002138 if (blk_addr == NULL_ADDR) {
2139 add_free_nid(sbi, start_nid, true, true);
2140 } else {
2141 spin_lock(&NM_I(sbi)->nid_list_lock);
2142 update_free_nid_bitmap(sbi, start_nid, false, true);
2143 spin_unlock(&NM_I(sbi)->nid_list_lock);
2144 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002145 }
Chao Yue2374012018-06-15 14:45:57 +08002146
2147 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002148}
2149
Chao Yu2fbaa252017-11-08 17:47:36 +08002150static void scan_curseg_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002151{
Chao Yu4ac91242017-02-23 10:53:49 +08002152 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2153 struct f2fs_journal *journal = curseg->journal;
Chao Yu2fbaa252017-11-08 17:47:36 +08002154 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08002155
Chao Yu4ac91242017-02-23 10:53:49 +08002156 down_read(&curseg->journal_rwsem);
2157 for (i = 0; i < nats_in_cursum(journal); i++) {
2158 block_t addr;
2159 nid_t nid;
2160
2161 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2162 nid = le32_to_cpu(nid_in_journal(journal, i));
2163 if (addr == NULL_ADDR)
LiFan5921aaa2017-11-22 16:07:23 +08002164 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002165 else
2166 remove_free_nid(sbi, nid);
2167 }
2168 up_read(&curseg->journal_rwsem);
Chao Yu2fbaa252017-11-08 17:47:36 +08002169}
2170
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002171static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002172{
2173 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002174 unsigned int i, idx;
Fan Li97456572017-11-07 19:14:24 +08002175 nid_t nid;
Chao Yu4ac91242017-02-23 10:53:49 +08002176
2177 down_read(&nm_i->nat_tree_lock);
2178
2179 for (i = 0; i < nm_i->nat_blocks; i++) {
2180 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2181 continue;
2182 if (!nm_i->free_nid_count[i])
2183 continue;
2184 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
Fan Li97456572017-11-07 19:14:24 +08002185 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2186 NAT_ENTRY_PER_BLOCK, idx);
2187 if (idx >= NAT_ENTRY_PER_BLOCK)
2188 break;
Chao Yu4ac91242017-02-23 10:53:49 +08002189
2190 nid = i * NAT_ENTRY_PER_BLOCK + idx;
LiFan5921aaa2017-11-22 16:07:23 +08002191 add_free_nid(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002192
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002193 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
Chao Yu4ac91242017-02-23 10:53:49 +08002194 goto out;
2195 }
2196 }
2197out:
Chao Yu2fbaa252017-11-08 17:47:36 +08002198 scan_curseg_cache(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002199
Chao Yu4ac91242017-02-23 10:53:49 +08002200 up_read(&nm_i->nat_tree_lock);
2201}
2202
Chao Yue2374012018-06-15 14:45:57 +08002203static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
Chao Yu4d57b862018-05-30 00:20:41 +08002204 bool sync, bool mount)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002205{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002206 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yue2374012018-06-15 14:45:57 +08002207 int i = 0, ret;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002208 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002209
Yunlei Hee9cdd302017-04-26 15:56:52 +08002210 if (unlikely(nid >= nm_i->max_nid))
2211 nid = 0;
2212
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002213 /* Enough entries */
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002214 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002215 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002216
Chao Yu4d57b862018-05-30 00:20:41 +08002217 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
Chao Yue2374012018-06-15 14:45:57 +08002218 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002219
Chao Yu4ac91242017-02-23 10:53:49 +08002220 if (!mount) {
2221 /* try to find free nids in free_nid_bitmap */
2222 scan_free_nid_bits(sbi);
2223
Fan Li74986212017-11-07 11:04:33 +08002224 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yue2374012018-06-15 14:45:57 +08002225 return 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002226 }
2227
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002228 /* readahead nat pages to be scanned */
Chao Yu4d57b862018-05-30 00:20:41 +08002229 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
Chao Yu26879fb2015-10-12 17:05:59 +08002230 META_NAT, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002231
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002232 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002233
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002234 while (1) {
Yunlei He66e83362017-11-17 16:13:38 +08002235 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2236 nm_i->nat_block_bitmap)) {
2237 struct page *page = get_current_nat_page(sbi, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002238
Chao Yue2374012018-06-15 14:45:57 +08002239 ret = scan_nat_page(sbi, page, nid);
Yunlei He66e83362017-11-17 16:13:38 +08002240 f2fs_put_page(page, 1);
Chao Yue2374012018-06-15 14:45:57 +08002241
2242 if (ret) {
2243 up_read(&nm_i->nat_tree_lock);
2244 f2fs_bug_on(sbi, !mount);
2245 f2fs_msg(sbi->sb, KERN_ERR,
2246 "NAT is corrupt, run fsck to fix it");
2247 return -EINVAL;
2248 }
Yunlei He66e83362017-11-17 16:13:38 +08002249 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002250
2251 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08002252 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002253 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002254
Chao Yua6d494b2015-07-24 18:26:26 +08002255 if (++i >= FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002256 break;
2257 }
2258
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002259 /* go to the next free nat pages to find free nids abundantly */
2260 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002261
2262 /* find free nids from current sum_pages */
Chao Yu2fbaa252017-11-08 17:47:36 +08002263 scan_curseg_cache(sbi);
Chao Yudfc08a12016-02-14 18:50:40 +08002264
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002265 up_read(&nm_i->nat_tree_lock);
Chao Yu2db23882015-10-12 17:07:33 +08002266
Chao Yu4d57b862018-05-30 00:20:41 +08002267 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
Chao Yuea1a29a02015-10-12 17:08:48 +08002268 nm_i->ra_nid_pages, META_NAT, false);
Chao Yue2374012018-06-15 14:45:57 +08002269
2270 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002271}
2272
Chao Yue2374012018-06-15 14:45:57 +08002273int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
Chao Yu2411cf52016-10-11 22:31:34 +08002274{
Chao Yue2374012018-06-15 14:45:57 +08002275 int ret;
2276
Chao Yu2411cf52016-10-11 22:31:34 +08002277 mutex_lock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002278 ret = __f2fs_build_free_nids(sbi, sync, mount);
Chao Yu2411cf52016-10-11 22:31:34 +08002279 mutex_unlock(&NM_I(sbi)->build_lock);
Chao Yue2374012018-06-15 14:45:57 +08002280
2281 return ret;
Chao Yu2411cf52016-10-11 22:31:34 +08002282}
2283
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002284/*
2285 * If this function returns success, caller can obtain a new nid
2286 * from second parameter of this function.
2287 * The returned nid could be used ino as well as nid when inode is created.
2288 */
Chao Yu4d57b862018-05-30 00:20:41 +08002289bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002290{
2291 struct f2fs_nm_info *nm_i = NM_I(sbi);
2292 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002293retry:
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002294#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu55523512017-02-25 11:08:28 +08002295 if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
2296 f2fs_show_injection_info(FAULT_ALLOC_NID);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002297 return false;
Chao Yu55523512017-02-25 11:08:28 +08002298 }
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002299#endif
Chao Yub8559dc2016-10-12 19:28:29 +08002300 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002301
Chao Yu04d47e62016-11-17 20:53:11 +08002302 if (unlikely(nm_i->available_nids == 0)) {
2303 spin_unlock(&nm_i->nid_list_lock);
2304 return false;
2305 }
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002306
Chao Yu4d57b862018-05-30 00:20:41 +08002307 /* We should not use stale free nids created by f2fs_build_free_nids */
2308 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002309 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2310 i = list_first_entry(&nm_i->free_nid_list,
Chao Yub8559dc2016-10-12 19:28:29 +08002311 struct free_nid, list);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002312 *nid = i->nid;
Chao Yub8559dc2016-10-12 19:28:29 +08002313
Fan Lia0761f62017-10-28 19:03:37 +08002314 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
Chao Yu04d47e62016-11-17 20:53:11 +08002315 nm_i->available_nids--;
Chao Yu4ac91242017-02-23 10:53:49 +08002316
Chao Yu346fe752017-03-13 20:10:41 +08002317 update_free_nid_bitmap(sbi, *nid, false, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002318
Chao Yub8559dc2016-10-12 19:28:29 +08002319 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002320 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002321 }
Chao Yub8559dc2016-10-12 19:28:29 +08002322 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002323
2324 /* Let's scan nat pages and its caches to get free nids */
Chao Yu4d57b862018-05-30 00:20:41 +08002325 f2fs_build_free_nids(sbi, true, false);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002326 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002327}
2328
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002329/*
Chao Yu4d57b862018-05-30 00:20:41 +08002330 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002331 */
Chao Yu4d57b862018-05-30 00:20:41 +08002332void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002333{
2334 struct f2fs_nm_info *nm_i = NM_I(sbi);
2335 struct free_nid *i;
2336
Chao Yub8559dc2016-10-12 19:28:29 +08002337 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002338 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002339 f2fs_bug_on(sbi, !i);
Fan Lia0761f62017-10-28 19:03:37 +08002340 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08002341 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002342
2343 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002344}
2345
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002346/*
Chao Yu4d57b862018-05-30 00:20:41 +08002347 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002348 */
Chao Yu4d57b862018-05-30 00:20:41 +08002349void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002350{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002351 struct f2fs_nm_info *nm_i = NM_I(sbi);
2352 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002353 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002354
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002355 if (!nid)
2356 return;
2357
Chao Yub8559dc2016-10-12 19:28:29 +08002358 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002359 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002360 f2fs_bug_on(sbi, !i);
2361
Chao Yu4d57b862018-05-30 00:20:41 +08002362 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
Fan Lia0761f62017-10-28 19:03:37 +08002363 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002364 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08002365 } else {
Fan Lia0761f62017-10-28 19:03:37 +08002366 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
Haicheng Li95630cb2013-05-06 23:15:41 +08002367 }
Chao Yu04d47e62016-11-17 20:53:11 +08002368
2369 nm_i->available_nids++;
2370
Chao Yu346fe752017-03-13 20:10:41 +08002371 update_free_nid_bitmap(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002372
Chao Yub8559dc2016-10-12 19:28:29 +08002373 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002374
2375 if (need_free)
2376 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002377}
2378
Chao Yu4d57b862018-05-30 00:20:41 +08002379int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
Chao Yu31696582015-07-28 18:33:46 +08002380{
2381 struct f2fs_nm_info *nm_i = NM_I(sbi);
2382 struct free_nid *i, *next;
2383 int nr = nr_shrink;
2384
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002385 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Jaegeuk Kimad4edb82016-06-16 16:41:49 -07002386 return 0;
2387
Chao Yu31696582015-07-28 18:33:46 +08002388 if (!mutex_trylock(&nm_i->build_lock))
2389 return 0;
2390
Chao Yub8559dc2016-10-12 19:28:29 +08002391 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002392 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
Chao Yub8559dc2016-10-12 19:28:29 +08002393 if (nr_shrink <= 0 ||
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002394 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Chao Yu31696582015-07-28 18:33:46 +08002395 break;
Chao Yub8559dc2016-10-12 19:28:29 +08002396
Fan Lia0761f62017-10-28 19:03:37 +08002397 __remove_free_nid(sbi, i, FREE_NID);
Chao Yu31696582015-07-28 18:33:46 +08002398 kmem_cache_free(free_nid_slab, i);
2399 nr_shrink--;
Chao Yu31696582015-07-28 18:33:46 +08002400 }
Chao Yub8559dc2016-10-12 19:28:29 +08002401 spin_unlock(&nm_i->nid_list_lock);
Chao Yu31696582015-07-28 18:33:46 +08002402 mutex_unlock(&nm_i->build_lock);
2403
2404 return nr - nr_shrink;
2405}
2406
Chao Yu4d57b862018-05-30 00:20:41 +08002407void f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08002408{
Chao Yu28cdce02014-03-11 13:37:38 +08002409 void *src_addr, *dst_addr;
2410 size_t inline_size;
2411 struct page *ipage;
2412 struct f2fs_inode *ri;
2413
Chao Yu4d57b862018-05-30 00:20:41 +08002414 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002415 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08002416
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002417 ri = F2FS_INODE(page);
Yunlei He1eca05a2018-01-03 18:03:04 +08002418 if (ri->i_inline & F2FS_INLINE_XATTR) {
2419 set_inode_flag(inode, FI_INLINE_XATTR);
2420 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002421 clear_inode_flag(inode, FI_INLINE_XATTR);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002422 goto update_inode;
2423 }
2424
Chao Yu6afc6622017-09-06 21:59:50 +08002425 dst_addr = inline_xattr_addr(inode, ipage);
2426 src_addr = inline_xattr_addr(inode, page);
Chao Yu28cdce02014-03-11 13:37:38 +08002427 inline_size = inline_xattr_size(inode);
2428
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002429 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu28cdce02014-03-11 13:37:38 +08002430 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002431update_inode:
Chao Yu4d57b862018-05-30 00:20:41 +08002432 f2fs_update_inode(inode, ipage);
Chao Yu28cdce02014-03-11 13:37:38 +08002433 f2fs_put_page(ipage, 1);
2434}
2435
Chao Yu4d57b862018-05-30 00:20:41 +08002436int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002437{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002439 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
Yunlei He87905682017-07-18 09:48:12 +08002440 nid_t new_xnid;
2441 struct dnode_of_data dn;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002442 struct node_info ni;
Chao Yud2600812017-02-08 17:39:45 +08002443 struct page *xpage;
Chao Yu77357302018-07-17 00:02:17 +08002444 int err;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002445
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002446 if (!prev_xnid)
2447 goto recover_xnid;
2448
Chao Yud2600812017-02-08 17:39:45 +08002449 /* 1: invalidate the previous xattr nid */
Chao Yu77357302018-07-17 00:02:17 +08002450 err = f2fs_get_node_info(sbi, prev_xnid, &ni);
2451 if (err)
2452 return err;
2453
Chao Yu4d57b862018-05-30 00:20:41 +08002454 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +08002455 dec_valid_node_count(sbi, inode, false);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002456 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002457
2458recover_xnid:
Chao Yud2600812017-02-08 17:39:45 +08002459 /* 2: update xattr nid in inode */
Chao Yu4d57b862018-05-30 00:20:41 +08002460 if (!f2fs_alloc_nid(sbi, &new_xnid))
Yunlei He87905682017-07-18 09:48:12 +08002461 return -ENOSPC;
2462
2463 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
Chao Yu4d57b862018-05-30 00:20:41 +08002464 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Yunlei He87905682017-07-18 09:48:12 +08002465 if (IS_ERR(xpage)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002466 f2fs_alloc_nid_failed(sbi, new_xnid);
Yunlei He87905682017-07-18 09:48:12 +08002467 return PTR_ERR(xpage);
2468 }
2469
Chao Yu4d57b862018-05-30 00:20:41 +08002470 f2fs_alloc_nid_done(sbi, new_xnid);
2471 f2fs_update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002472
Chao Yud2600812017-02-08 17:39:45 +08002473 /* 3: update and set xattr node page dirty */
Yunlei He87905682017-07-18 09:48:12 +08002474 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
Chao Yud2600812017-02-08 17:39:45 +08002475
Chao Yud2600812017-02-08 17:39:45 +08002476 set_page_dirty(xpage);
2477 f2fs_put_page(xpage, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002478
Chao Yud2600812017-02-08 17:39:45 +08002479 return 0;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002480}
2481
Chao Yu4d57b862018-05-30 00:20:41 +08002482int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002483{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002484 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002485 nid_t ino = ino_of_node(page);
2486 struct node_info old_ni, new_ni;
2487 struct page *ipage;
Chao Yu77357302018-07-17 00:02:17 +08002488 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002489
Chao Yu77357302018-07-17 00:02:17 +08002490 err = f2fs_get_node_info(sbi, ino, &old_ni);
2491 if (err)
2492 return err;
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09002493
2494 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2495 return -EINVAL;
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002496retry:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07002497 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002498 if (!ipage) {
2499 congestion_wait(BLK_RW_ASYNC, HZ/50);
2500 goto retry;
2501 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002502
arter97e1c42042014-08-06 23:22:50 +09002503 /* Should not use this inode from free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08002504 remove_free_nid(sbi, ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002505
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002506 if (!PageUptodate(ipage))
2507 SetPageUptodate(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002508 fill_node_footer(ipage, ino, ino, 0, true);
Chao Yuc5667572018-03-09 14:24:22 +08002509 set_cold_node(page, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002510
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002511 src = F2FS_INODE(page);
2512 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002513
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002514 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
2515 dst->i_size = 0;
2516 dst->i_blocks = cpu_to_le64(1);
2517 dst->i_links = cpu_to_le32(1);
2518 dst->i_xattr_nid = 0;
Chao Yu7a2af762017-07-19 00:19:06 +08002519 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
Chao Yu5c571322017-07-26 00:01:41 +08002520 if (dst->i_inline & F2FS_EXTRA_ATTR) {
Chao Yu7a2af762017-07-19 00:19:06 +08002521 dst->i_extra_isize = src->i_extra_isize;
Chao Yu6afc6622017-09-06 21:59:50 +08002522
2523 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb) &&
2524 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2525 i_inline_xattr_size))
2526 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2527
Chao Yu5c571322017-07-26 00:01:41 +08002528 if (f2fs_sb_has_project_quota(sbi->sb) &&
2529 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2530 i_projid))
2531 dst->i_projid = src->i_projid;
2532 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002533
2534 new_ni = old_ni;
2535 new_ni.ino = ino;
2536
Chao Yu0abd6752017-07-09 00:13:07 +08002537 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09002538 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002539 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002540 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07002541 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002542 f2fs_put_page(ipage, 1);
2543 return 0;
2544}
2545
Chao Yu77357302018-07-17 00:02:17 +08002546int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002547 unsigned int segno, struct f2fs_summary_block *sum)
2548{
2549 struct f2fs_node *rn;
2550 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002551 block_t addr;
Chao Yu9ecf4b82014-12-18 18:29:05 +08002552 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002553
2554 /* scan the node segment */
2555 last_offset = sbi->blocks_per_seg;
2556 addr = START_BLOCK(sbi, segno);
2557 sum_entry = &sum->entries[0];
2558
Chao Yu9ecf4b82014-12-18 18:29:05 +08002559 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Jaegeuk Kim664ba972016-10-18 11:07:45 -07002560 nrpages = min(last_offset - i, BIO_MAX_PAGES);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002561
arter97e1c42042014-08-06 23:22:50 +09002562 /* readahead node pages */
Chao Yu4d57b862018-05-30 00:20:41 +08002563 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002564
Chao Yu9ecf4b82014-12-18 18:29:05 +08002565 for (idx = addr; idx < addr + nrpages; idx++) {
Chao Yu4d57b862018-05-30 00:20:41 +08002566 struct page *page = f2fs_get_tmp_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08002567
Chao Yu77357302018-07-17 00:02:17 +08002568 if (IS_ERR(page))
2569 return PTR_ERR(page);
2570
Chao Yu9ecf4b82014-12-18 18:29:05 +08002571 rn = F2FS_NODE(page);
2572 sum_entry->nid = rn->footer.nid;
2573 sum_entry->version = 0;
2574 sum_entry->ofs_in_node = 0;
2575 sum_entry++;
2576 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08002577 }
Chao Yubac4eef2014-05-27 08:41:07 +08002578
Chao Yu9ecf4b82014-12-18 18:29:05 +08002579 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08002580 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002581 }
Chao Yu77357302018-07-17 00:02:17 +08002582 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002583}
2584
Chao Yuaec71382014-06-24 09:18:20 +08002585static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002586{
2587 struct f2fs_nm_info *nm_i = NM_I(sbi);
2588 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002589 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002590 int i;
2591
Chao Yub7ad7512016-02-19 18:08:46 +08002592 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002593 for (i = 0; i < nats_in_cursum(journal); i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002594 struct nat_entry *ne;
2595 struct f2fs_nat_entry raw_ne;
Chao Yudfc08a12016-02-14 18:50:40 +08002596 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002597
Chao Yudfc08a12016-02-14 18:50:40 +08002598 raw_ne = nat_in_journal(journal, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08002599
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002600 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002601 if (!ne) {
Yunlei He12f9ef32017-11-10 13:36:51 -08002602 ne = __alloc_nat_entry(nid, true);
2603 __init_nat_entry(nm_i, ne, &raw_ne, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002604 }
Chao Yu04d47e62016-11-17 20:53:11 +08002605
2606 /*
2607 * if a free nat in journal has not been used after last
2608 * checkpoint, we should remove it from available nids,
2609 * since later we will add it again.
2610 */
2611 if (!get_nat_flag(ne, IS_DIRTY) &&
2612 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2613 spin_lock(&nm_i->nid_list_lock);
2614 nm_i->available_nids--;
2615 spin_unlock(&nm_i->nid_list_lock);
2616 }
2617
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002618 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002619 }
Chao Yudfc08a12016-02-14 18:50:40 +08002620 update_nats_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002621 up_write(&curseg->journal_rwsem);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002622}
2623
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002624static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2625 struct list_head *head, int max)
2626{
2627 struct nat_entry_set *cur;
2628
2629 if (nes->entry_cnt >= max)
2630 goto add_out;
2631
2632 list_for_each_entry(cur, head, set_list) {
2633 if (cur->entry_cnt >= nes->entry_cnt) {
2634 list_add(&nes->set_list, cur->set_list.prev);
2635 return;
2636 }
2637 }
2638add_out:
2639 list_add_tail(&nes->set_list, head);
2640}
2641
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002642static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002643 struct page *page)
2644{
2645 struct f2fs_nm_info *nm_i = NM_I(sbi);
2646 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2647 struct f2fs_nat_block *nat_blk = page_address(page);
2648 int valid = 0;
Fan Li37a0ab22017-10-30 15:19:48 +08002649 int i = 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002650
2651 if (!enabled_nat_bits(sbi, NULL))
2652 return;
2653
Fan Li37a0ab22017-10-30 15:19:48 +08002654 if (nat_index == 0) {
2655 valid = 1;
2656 i = 1;
2657 }
2658 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
2659 if (nat_blk->entries[i].block_addr != NULL_ADDR)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002660 valid++;
2661 }
2662 if (valid == 0) {
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002663 __set_bit_le(nat_index, nm_i->empty_nat_bits);
2664 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002665 return;
2666 }
2667
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002668 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002669 if (valid == NAT_ENTRY_PER_BLOCK)
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002670 __set_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002671 else
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002672 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002673}
2674
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002675static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002676 struct nat_entry_set *set, struct cp_control *cpc)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002677{
2678 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002679 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002680 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
2681 bool to_journal = true;
2682 struct f2fs_nat_block *nat_blk;
2683 struct nat_entry *ne, *cur;
2684 struct page *page = NULL;
2685
2686 /*
2687 * there are two steps to flush nat entries:
2688 * #1, flush nat entries to journal in current hot data summary block.
2689 * #2, flush nat entries to nat page.
2690 */
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002691 if (enabled_nat_bits(sbi, cpc) ||
2692 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002693 to_journal = false;
2694
2695 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002696 down_write(&curseg->journal_rwsem);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002697 } else {
2698 page = get_next_nat_page(sbi, start_nid);
2699 nat_blk = page_address(page);
2700 f2fs_bug_on(sbi, !nat_blk);
2701 }
2702
2703 /* flush dirty nats in nat entry set */
2704 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
2705 struct f2fs_nat_entry *raw_ne;
2706 nid_t nid = nat_get_nid(ne);
2707 int offset;
2708
Chao Yufebeca62017-06-05 18:29:08 +08002709 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002710
2711 if (to_journal) {
Chao Yu4d57b862018-05-30 00:20:41 +08002712 offset = f2fs_lookup_journal_in_cursum(journal,
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002713 NAT_JOURNAL, nid, 1);
2714 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002715 raw_ne = &nat_in_journal(journal, offset);
2716 nid_in_journal(journal, offset) = cpu_to_le32(nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002717 } else {
2718 raw_ne = &nat_blk->entries[nid - start_nid];
2719 }
2720 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002721 nat_reset_flag(ne);
Kinglong Mee0b28b712017-02-28 21:34:47 +08002722 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
Chao Yu04d47e62016-11-17 20:53:11 +08002723 if (nat_get_blkaddr(ne) == NULL_ADDR) {
LiFan5921aaa2017-11-22 16:07:23 +08002724 add_free_nid(sbi, nid, false, true);
Chao Yu4ac91242017-02-23 10:53:49 +08002725 } else {
2726 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu346fe752017-03-13 20:10:41 +08002727 update_free_nid_bitmap(sbi, nid, false, false);
Chao Yu04d47e62016-11-17 20:53:11 +08002728 spin_unlock(&NM_I(sbi)->nid_list_lock);
2729 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002730 }
2731
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002732 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002733 up_write(&curseg->journal_rwsem);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002734 } else {
2735 __update_nat_bits(sbi, start_nid, page);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002736 f2fs_put_page(page, 1);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002737 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002738
Yunlei He59c90812017-03-13 20:22:18 +08002739 /* Allow dirty nats by node block allocation in write_begin */
2740 if (!set->entry_cnt) {
2741 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
2742 kmem_cache_free(nat_entry_set_slab, set);
2743 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002744}
2745
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002746/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002747 * This function is called during the checkpointing process.
2748 */
Chao Yu4d57b862018-05-30 00:20:41 +08002749void f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002750{
2751 struct f2fs_nm_info *nm_i = NM_I(sbi);
2752 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002753 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002754 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002755 struct nat_entry_set *set, *tmp;
2756 unsigned int found;
2757 nid_t set_idx = 0;
2758 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002759
Jaegeuk Kim7f2ecdd2018-06-28 19:34:40 -07002760 /* during unmount, let's flush nat_bits before checking dirty_nat_cnt */
2761 if (enabled_nat_bits(sbi, cpc)) {
2762 down_write(&nm_i->nat_tree_lock);
2763 remove_nats_in_journal(sbi);
2764 up_write(&nm_i->nat_tree_lock);
2765 }
2766
Changman Lee20d047c2014-11-25 12:44:23 +09002767 if (!nm_i->dirty_nat_cnt)
2768 return;
Jaegeuk Kima5131192016-01-02 09:19:41 -08002769
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002770 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002771
Chao Yuaec71382014-06-24 09:18:20 +08002772 /*
2773 * if there are no enough space in journal to store dirty nat
2774 * entries, remove all entries from journal and merge them
2775 * into nat entry set.
2776 */
Jaegeuk Kim900f7362017-02-27 21:28:53 -08002777 if (enabled_nat_bits(sbi, cpc) ||
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002778 !__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08002779 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002780
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002781 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002782 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002783 unsigned idx;
2784 set_idx = setvec[found - 1]->set + 1;
2785 for (idx = 0; idx < found; idx++)
2786 __adjust_nat_entry_set(setvec[idx], &sets,
Chao Yudfc08a12016-02-14 18:50:40 +08002787 MAX_NAT_JENTRIES(journal));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002788 }
Chao Yuaec71382014-06-24 09:18:20 +08002789
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002790 /* flush dirty nats in nat entry set */
2791 list_for_each_entry_safe(set, tmp, &sets, set_list)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002792 __flush_nat_entry_set(sbi, set, cpc);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002793
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002794 up_write(&nm_i->nat_tree_lock);
Yunlei He59c90812017-03-13 20:22:18 +08002795 /* Allow dirty nats by node block allocation in write_begin */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002796}
2797
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002798static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
2799{
2800 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2801 struct f2fs_nm_info *nm_i = NM_I(sbi);
2802 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
2803 unsigned int i;
2804 __u64 cp_ver = cur_cp_version(ckpt);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002805 block_t nat_bits_addr;
2806
2807 if (!enabled_nat_bits(sbi, NULL))
2808 return 0;
2809
Chao Yudf033ca2018-03-20 23:08:29 +08002810 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
Chao Yuacbf0542017-11-30 19:28:17 +08002811 nm_i->nat_bits = f2fs_kzalloc(sbi,
2812 nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002813 if (!nm_i->nat_bits)
2814 return -ENOMEM;
2815
2816 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
2817 nm_i->nat_bits_blocks;
2818 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
Chao Yu77357302018-07-17 00:02:17 +08002819 struct page *page;
2820
2821 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
2822 if (IS_ERR(page)) {
2823 disable_nat_bits(sbi, true);
2824 return PTR_ERR(page);
2825 }
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002826
2827 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
2828 page_address(page), F2FS_BLKSIZE);
2829 f2fs_put_page(page, 1);
2830 }
2831
Kinglong Meeced2c7e2017-02-25 19:53:39 +08002832 cp_ver |= (cur_cp_crc(ckpt) << 32);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002833 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
2834 disable_nat_bits(sbi, true);
2835 return 0;
2836 }
2837
2838 nm_i->full_nat_bits = nm_i->nat_bits + 8;
2839 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
2840
2841 f2fs_msg(sbi->sb, KERN_NOTICE, "Found nat_bits in checkpoint");
2842 return 0;
2843}
2844
Hou Pengyangbd80a4b2017-05-17 02:48:48 +00002845static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
Chao Yu7041d5d2017-03-08 20:07:49 +08002846{
2847 struct f2fs_nm_info *nm_i = NM_I(sbi);
2848 unsigned int i = 0;
2849 nid_t nid, last_nid;
2850
2851 if (!enabled_nat_bits(sbi, NULL))
2852 return;
2853
2854 for (i = 0; i < nm_i->nat_blocks; i++) {
2855 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
2856 if (i >= nm_i->nat_blocks)
2857 break;
2858
2859 __set_bit_le(i, nm_i->nat_block_bitmap);
2860
2861 nid = i * NAT_ENTRY_PER_BLOCK;
Fan Lif6986ed2017-11-02 11:02:52 +08002862 last_nid = nid + NAT_ENTRY_PER_BLOCK;
Chao Yu7041d5d2017-03-08 20:07:49 +08002863
Chao Yu346fe752017-03-13 20:10:41 +08002864 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08002865 for (; nid < last_nid; nid++)
Chao Yu346fe752017-03-13 20:10:41 +08002866 update_free_nid_bitmap(sbi, nid, true, true);
2867 spin_unlock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08002868 }
2869
2870 for (i = 0; i < nm_i->nat_blocks; i++) {
2871 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
2872 if (i >= nm_i->nat_blocks)
2873 break;
2874
2875 __set_bit_le(i, nm_i->nat_block_bitmap);
2876 }
2877}
2878
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002879static int init_node_manager(struct f2fs_sb_info *sbi)
2880{
2881 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
2882 struct f2fs_nm_info *nm_i = NM_I(sbi);
2883 unsigned char *version_bitmap;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002884 unsigned int nat_segs;
2885 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002886
2887 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
2888
2889 /* segment_count_nat includes pair segment so divide to 2. */
2890 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002891 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
2892 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09002893
Jaegeuk Kimb63da152014-02-17 12:44:20 +09002894 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yu04d47e62016-11-17 20:53:11 +08002895 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
Chao Yu292c196a2017-11-16 16:59:14 +08002896 sbi->nquota_files - F2FS_RESERVED_NODE_NUM;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002897 nm_i->nid_cnt[FREE_NID] = 0;
2898 nm_i->nid_cnt[PREALLOC_NID] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002899 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09002900 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Chao Yuea1a29a02015-10-12 17:08:48 +08002901 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
Chao Yu2304cb02016-01-18 18:32:58 +08002902 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002903
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002904 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002905 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002906 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2907 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002908 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002909
2910 mutex_init(&nm_i->build_lock);
Chao Yub8559dc2016-10-12 19:28:29 +08002911 spin_lock_init(&nm_i->nid_list_lock);
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002912 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002913
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002914 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002915 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002916 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2917 if (!version_bitmap)
2918 return -EFAULT;
2919
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002920 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2921 GFP_KERNEL);
2922 if (!nm_i->nat_bitmap)
2923 return -ENOMEM;
Chao Yu599a09b2017-01-07 18:52:01 +08002924
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002925 err = __get_nat_bitmaps(sbi);
2926 if (err)
2927 return err;
2928
Chao Yu599a09b2017-01-07 18:52:01 +08002929#ifdef CONFIG_F2FS_CHECK_FS
2930 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
2931 GFP_KERNEL);
2932 if (!nm_i->nat_bitmap_mir)
2933 return -ENOMEM;
2934#endif
2935
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002936 return 0;
2937}
2938
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002939static int init_free_nid_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002940{
2941 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08002942 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08002943
Kees Cook026f0502018-06-12 14:28:23 -07002944 nm_i->free_nid_bitmap =
2945 f2fs_kzalloc(sbi, array_size(sizeof(unsigned char *),
2946 nm_i->nat_blocks),
2947 GFP_KERNEL);
Chao Yu4ac91242017-02-23 10:53:49 +08002948 if (!nm_i->free_nid_bitmap)
2949 return -ENOMEM;
2950
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08002951 for (i = 0; i < nm_i->nat_blocks; i++) {
2952 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
Yunlei Hee15d54d2018-06-27 14:46:21 +08002953 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
Yunlei He68c43a22018-07-02 10:40:19 +08002954 if (!nm_i->free_nid_bitmap[i])
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08002955 return -ENOMEM;
2956 }
2957
Chao Yu628b3d12017-11-30 19:28:18 +08002958 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
Chao Yu4ac91242017-02-23 10:53:49 +08002959 GFP_KERNEL);
2960 if (!nm_i->nat_block_bitmap)
2961 return -ENOMEM;
Chao Yu586d1492017-03-01 17:09:07 +08002962
Kees Cook9d2a7892018-06-12 14:28:35 -07002963 nm_i->free_nid_count =
2964 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
2965 nm_i->nat_blocks),
2966 GFP_KERNEL);
Chao Yu586d1492017-03-01 17:09:07 +08002967 if (!nm_i->free_nid_count)
2968 return -ENOMEM;
Chao Yu4ac91242017-02-23 10:53:49 +08002969 return 0;
2970}
2971
Chao Yu4d57b862018-05-30 00:20:41 +08002972int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002973{
2974 int err;
2975
Chao Yuacbf0542017-11-30 19:28:17 +08002976 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
2977 GFP_KERNEL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002978 if (!sbi->nm_info)
2979 return -ENOMEM;
2980
2981 err = init_node_manager(sbi);
2982 if (err)
2983 return err;
2984
Chao Yu4ac91242017-02-23 10:53:49 +08002985 err = init_free_nid_cache(sbi);
2986 if (err)
2987 return err;
2988
Chao Yu7041d5d2017-03-08 20:07:49 +08002989 /* load free nid status from nat_bits table */
2990 load_free_nid_bitmap(sbi);
2991
Chao Yue2374012018-06-15 14:45:57 +08002992 return f2fs_build_free_nids(sbi, true, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002993}
2994
Chao Yu4d57b862018-05-30 00:20:41 +08002995void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002996{
2997 struct f2fs_nm_info *nm_i = NM_I(sbi);
2998 struct free_nid *i, *next_i;
2999 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003000 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003001 nid_t nid = 0;
3002 unsigned int found;
3003
3004 if (!nm_i)
3005 return;
3006
3007 /* destroy free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08003008 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003009 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Fan Lia0761f62017-10-28 19:03:37 +08003010 __remove_free_nid(sbi, i, FREE_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08003011 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08003012 kmem_cache_free(free_nid_slab, i);
Chao Yub8559dc2016-10-12 19:28:29 +08003013 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003014 }
Chao Yu9a4ffdf2017-09-29 13:59:35 +08003015 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3016 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3017 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
Chao Yub8559dc2016-10-12 19:28:29 +08003018 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003019
3020 /* destroy nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003021 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003022 while ((found = __gang_lookup_nat_cache(nm_i,
3023 nid, NATVEC_SIZE, natvec))) {
3024 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003025
Gu Zhengb6ce3912014-03-07 18:43:24 +08003026 nid = nat_get_nid(natvec[found - 1]) + 1;
3027 for (idx = 0; idx < found; idx++)
3028 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003029 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003030 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003031
3032 /* destroy nat set cache */
3033 nid = 0;
3034 while ((found = __gang_lookup_nat_set(nm_i,
3035 nid, SETVEC_SIZE, setvec))) {
3036 unsigned idx;
3037
3038 nid = setvec[found - 1]->set + 1;
3039 for (idx = 0; idx < found; idx++) {
3040 /* entry_cnt is not zero, when cp_error was occurred */
3041 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3042 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3043 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3044 }
3045 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003046 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003047
Chao Yu4ac91242017-02-23 10:53:49 +08003048 kvfree(nm_i->nat_block_bitmap);
Jaegeuk Kimbb1105e2018-03-09 17:42:28 -08003049 if (nm_i->free_nid_bitmap) {
3050 int i;
3051
3052 for (i = 0; i < nm_i->nat_blocks; i++)
3053 kvfree(nm_i->free_nid_bitmap[i]);
3054 kfree(nm_i->free_nid_bitmap);
3055 }
Chao Yu586d1492017-03-01 17:09:07 +08003056 kvfree(nm_i->free_nid_count);
Chao Yu4ac91242017-02-23 10:53:49 +08003057
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003058 kfree(nm_i->nat_bitmap);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08003059 kfree(nm_i->nat_bits);
Chao Yu599a09b2017-01-07 18:52:01 +08003060#ifdef CONFIG_F2FS_CHECK_FS
3061 kfree(nm_i->nat_bitmap_mir);
3062#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003063 sbi->nm_info = NULL;
3064 kfree(nm_i);
3065}
3066
Chao Yu4d57b862018-05-30 00:20:41 +08003067int __init f2fs_create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003068{
3069 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003070 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003071 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08003072 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003073
3074 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08003075 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08003076 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003077 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08003078
3079 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
3080 sizeof(struct nat_entry_set));
3081 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003082 goto destroy_free_nid;
Chao Yu50fa53e2018-08-02 23:03:19 +08003083
3084 fsync_node_entry_slab = f2fs_kmem_cache_create("fsync_node_entry",
3085 sizeof(struct fsync_node_entry));
3086 if (!fsync_node_entry_slab)
3087 goto destroy_nat_entry_set;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003088 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08003089
Chao Yu50fa53e2018-08-02 23:03:19 +08003090destroy_nat_entry_set:
3091 kmem_cache_destroy(nat_entry_set_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003092destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08003093 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003094destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08003095 kmem_cache_destroy(nat_entry_slab);
3096fail:
3097 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003098}
3099
Chao Yu4d57b862018-05-30 00:20:41 +08003100void f2fs_destroy_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003101{
Chao Yu50fa53e2018-08-02 23:03:19 +08003102 kmem_cache_destroy(fsync_node_entry_slab);
Chao Yuaec71382014-06-24 09:18:20 +08003103 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003104 kmem_cache_destroy(free_nid_slab);
3105 kmem_cache_destroy(nat_entry_slab);
3106}