blob: 93bd89a9a56f079cce47b9f9231f140bbd5b471d [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
Tomohiro Kusumi68afcf22017-04-09 02:11:36 +030026#define on_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;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090031
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090032bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090033{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090034 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090035 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080036 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090037 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090038 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090039
40 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080041
42 /* only uses low memory */
43 avail_ram = val.totalram - val.totalhigh;
44
Chao Yu429511c2015-02-05 17:54:31 +080045 /*
46 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
47 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090048 if (type == FREE_NIDS) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +080049 mem_size = (nm_i->nid_cnt[FREE_NID] *
Chao Yub8559dc2016-10-12 19:28:29 +080050 sizeof(struct free_nid)) >> PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080051 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090052 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080053 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030054 PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080055 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kime589c2c2016-06-02 15:24:24 -070056 if (excess_cached_nats(sbi))
57 res = false;
Jaegeuk Kima1257022015-10-08 10:40:07 -070058 } else if (type == DIRTY_DENTS) {
59 if (sbi->sb->s_bdi->wb.dirty_exceeded)
60 return false;
61 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
62 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080063 } else if (type == INO_ENTRIES) {
64 int i;
65
Chao Yu39d787b2017-09-29 13:59:38 +080066 for (i = 0; i < MAX_INO_ENTRY; i++)
Kinglong Mee8f73cbb2017-03-18 09:26:13 +080067 mem_size += sbi->im[i].ino_num *
68 sizeof(struct ino_entry);
69 mem_size >>= PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080070 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080071 } else if (type == EXTENT_CACHE) {
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080072 mem_size = (atomic_read(&sbi->total_ext_tree) *
73 sizeof(struct extent_tree) +
Chao Yu429511c2015-02-05 17:54:31 +080074 atomic_read(&sbi->total_ext_node) *
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030075 sizeof(struct extent_node)) >> PAGE_SHIFT;
Chao Yu429511c2015-02-05 17:54:31 +080076 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -070077 } else if (type == INMEM_PAGES) {
78 /* it allows 20% / total_ram for inmemory pages */
79 mem_size = get_pages(sbi, F2FS_INMEM_PAGES);
80 res = mem_size < (val.totalram / 5);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080081 } else {
Jaegeuk Kim1663cae2016-01-09 16:14:08 -080082 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
83 return true;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090084 }
85 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090086}
87
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090088static void clear_node_page_dirty(struct page *page)
89{
90 struct address_space *mapping = page->mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090091 unsigned int long flags;
92
93 if (PageDirty(page)) {
94 spin_lock_irqsave(&mapping->tree_lock, flags);
95 radix_tree_tag_clear(&mapping->page_tree,
96 page_index(page),
97 PAGECACHE_TAG_DIRTY);
98 spin_unlock_irqrestore(&mapping->tree_lock, flags);
99
100 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -0700101 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900102 }
103 ClearPageUptodate(page);
104}
105
106static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
107{
108 pgoff_t index = current_nat_addr(sbi, nid);
109 return get_meta_page(sbi, index);
110}
111
112static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
113{
114 struct page *src_page;
115 struct page *dst_page;
116 pgoff_t src_off;
117 pgoff_t dst_off;
118 void *src_addr;
119 void *dst_addr;
120 struct f2fs_nm_info *nm_i = NM_I(sbi);
121
122 src_off = current_nat_addr(sbi, nid);
123 dst_off = next_nat_addr(sbi, src_off);
124
125 /* get current nat block page with lock */
126 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900127 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700128 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900129
130 src_addr = page_address(src_page);
131 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300132 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900133 set_page_dirty(dst_page);
134 f2fs_put_page(src_page, 1);
135
136 set_to_next_nat(nm_i, nid);
137
138 return dst_page;
139}
140
Yunlei He12f9ef32017-11-10 13:36:51 -0800141static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail)
142{
143 struct nat_entry *new;
144
145 if (no_fail)
146 new = f2fs_kmem_cache_alloc(nat_entry_slab,
147 GFP_NOFS | __GFP_ZERO);
148 else
149 new = kmem_cache_alloc(nat_entry_slab,
150 GFP_NOFS | __GFP_ZERO);
151 if (new) {
152 nat_set_nid(new, nid);
153 nat_reset_flag(new);
154 }
155 return new;
156}
157
158static void __free_nat_entry(struct nat_entry *e)
159{
160 kmem_cache_free(nat_entry_slab, e);
161}
162
163/* must be locked by nat_tree_lock */
164static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
165 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
166{
167 if (no_fail)
168 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
169 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
170 return NULL;
171
172 if (raw_ne)
173 node_info_from_raw_nat(&ne->ni, raw_ne);
174 list_add_tail(&ne->list, &nm_i->nat_entries);
175 nm_i->nat_cnt++;
176 return ne;
177}
178
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900179static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
180{
181 return radix_tree_lookup(&nm_i->nat_root, n);
182}
183
184static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
185 nid_t start, unsigned int nr, struct nat_entry **ep)
186{
187 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
188}
189
190static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
191{
192 list_del(&e->list);
193 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
194 nm_i->nat_cnt--;
Yunlei He12f9ef32017-11-10 13:36:51 -0800195 __free_nat_entry(e);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900196}
197
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700198static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
199 struct nat_entry *ne)
200{
201 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
202 struct nat_entry_set *head;
203
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700204 head = radix_tree_lookup(&nm_i->nat_set_root, set);
205 if (!head) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700206 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700207
208 INIT_LIST_HEAD(&head->entry_list);
209 INIT_LIST_HEAD(&head->set_list);
210 head->set = set;
211 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800212 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700213 }
Chao Yufebeca62017-06-05 18:29:08 +0800214
215 if (get_nat_flag(ne, IS_DIRTY))
216 goto refresh_list;
217
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700218 nm_i->dirty_nat_cnt++;
219 head->entry_cnt++;
220 set_nat_flag(ne, IS_DIRTY, true);
Chao Yufebeca62017-06-05 18:29:08 +0800221refresh_list:
222 if (nat_get_blkaddr(ne) == NEW_ADDR)
223 list_del_init(&ne->list);
224 else
225 list_move_tail(&ne->list, &head->entry_list);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700226}
227
228static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
Kinglong Mee0b28b712017-02-28 21:34:47 +0800229 struct nat_entry_set *set, struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700230{
Kinglong Mee0b28b712017-02-28 21:34:47 +0800231 list_move_tail(&ne->list, &nm_i->nat_entries);
232 set_nat_flag(ne, IS_DIRTY, false);
233 set->entry_cnt--;
234 nm_i->dirty_nat_cnt--;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700235}
236
237static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
238 nid_t start, unsigned int nr, struct nat_entry_set **ep)
239{
240 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
241 start, nr);
242}
243
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700244int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
245{
246 struct f2fs_nm_info *nm_i = NM_I(sbi);
247 struct nat_entry *e;
248 bool need = false;
249
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700250 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700251 e = __lookup_nat_cache(nm_i, nid);
252 if (e) {
253 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
254 !get_nat_flag(e, HAS_FSYNCED_INODE))
255 need = true;
256 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700257 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700258 return need;
259}
260
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700261bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900262{
263 struct f2fs_nm_info *nm_i = NM_I(sbi);
264 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700265 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900266
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700267 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900268 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700269 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700270 is_cp = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700271 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900272 return is_cp;
273}
274
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700275bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900276{
277 struct f2fs_nm_info *nm_i = NM_I(sbi);
278 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700279 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900280
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700281 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700282 e = __lookup_nat_cache(nm_i, ino);
283 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
284 (get_nat_flag(e, IS_CHECKPOINTED) ||
285 get_nat_flag(e, HAS_FSYNCED_INODE)))
286 need_update = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700287 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700288 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900289}
290
Yunlei He12f9ef32017-11-10 13:36:51 -0800291/* must be locked by nat_tree_lock */
Chao Yu1515aef2016-02-19 18:12:28 +0800292static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900293 struct f2fs_nat_entry *ne)
294{
Chao Yu1515aef2016-02-19 18:12:28 +0800295 struct f2fs_nm_info *nm_i = NM_I(sbi);
Yunlei He12f9ef32017-11-10 13:36:51 -0800296 struct nat_entry *new, *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800297
Yunlei He12f9ef32017-11-10 13:36:51 -0800298 new = __alloc_nat_entry(nid, false);
299 if (!new)
300 return;
301
302 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900303 e = __lookup_nat_cache(nm_i, nid);
Yunlei He12f9ef32017-11-10 13:36:51 -0800304 if (!e)
305 e = __init_nat_entry(nm_i, new, ne, false);
306 else
Eric Biggers0c0b4712016-10-11 10:36:12 -0700307 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
308 nat_get_blkaddr(e) !=
309 le32_to_cpu(ne->block_addr) ||
Chao Yu1515aef2016-02-19 18:12:28 +0800310 nat_get_version(e) != ne->version);
Yunlei He12f9ef32017-11-10 13:36:51 -0800311 up_write(&nm_i->nat_tree_lock);
312 if (e != new)
313 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900314}
315
316static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900317 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900318{
319 struct f2fs_nm_info *nm_i = NM_I(sbi);
320 struct nat_entry *e;
Yunlei He12f9ef32017-11-10 13:36:51 -0800321 struct nat_entry *new = __alloc_nat_entry(ni->nid, true);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800322
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700323 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900324 e = __lookup_nat_cache(nm_i, ni->nid);
325 if (!e) {
Yunlei He12f9ef32017-11-10 13:36:51 -0800326 e = __init_nat_entry(nm_i, new, NULL, true);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800327 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700328 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900329 } else if (new_blkaddr == NEW_ADDR) {
330 /*
331 * when nid is reallocated,
332 * previous nat entry can be remained in nat cache.
333 * So, reinitialize it with new information.
334 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800335 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700336 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900337 }
Yunlei He12f9ef32017-11-10 13:36:51 -0800338 /* let's free early to reduce memory consumption */
339 if (e != new)
340 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900341
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900342 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700343 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
344 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900345 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700346 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900347 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700348 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900349 nat_get_blkaddr(e) != NULL_ADDR &&
350 new_blkaddr == NEW_ADDR);
351
arter97e1c42042014-08-06 23:22:50 +0900352 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900353 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
354 unsigned char version = nat_get_version(e);
355 nat_set_version(e, inc_node_version(version));
356 }
357
358 /* change address */
359 nat_set_blkaddr(e, new_blkaddr);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700360 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
361 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900362 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900363
364 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800365 if (ni->nid != ni->ino)
366 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700367 if (e) {
368 if (fsync_done && ni->nid == ni->ino)
369 set_nat_flag(e, HAS_FSYNCED_INODE, true);
370 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
371 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700372 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900373}
374
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900375int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900376{
377 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700378 int nr = nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900379
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700380 if (!down_write_trylock(&nm_i->nat_tree_lock))
381 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900382
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900383 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
384 struct nat_entry *ne;
385 ne = list_first_entry(&nm_i->nat_entries,
386 struct nat_entry, list);
387 __del_from_nat_cache(nm_i, ne);
388 nr_shrink--;
389 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700390 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700391 return nr - nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900392}
393
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900394/*
arter97e1c42042014-08-06 23:22:50 +0900395 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900396 */
397void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
398{
399 struct f2fs_nm_info *nm_i = NM_I(sbi);
400 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +0800401 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900402 nid_t start_nid = START_NID(nid);
403 struct f2fs_nat_block *nat_blk;
404 struct page *page = NULL;
405 struct f2fs_nat_entry ne;
406 struct nat_entry *e;
Yunlei He66a82d12017-04-22 18:06:26 +0800407 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900408 int i;
409
410 ni->nid = nid;
411
412 /* Check nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700413 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900414 e = __lookup_nat_cache(nm_i, nid);
415 if (e) {
416 ni->ino = nat_get_ino(e);
417 ni->blk_addr = nat_get_blkaddr(e);
418 ni->version = nat_get_version(e);
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700419 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900420 return;
Chao Yu1515aef2016-02-19 18:12:28 +0800421 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900422
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800423 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
424
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900425 /* Check current segment summary */
Chao Yub7ad7512016-02-19 18:08:46 +0800426 down_read(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +0800427 i = lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900428 if (i >= 0) {
Chao Yudfc08a12016-02-14 18:50:40 +0800429 ne = nat_in_journal(journal, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900430 node_info_from_raw_nat(ni, &ne);
431 }
Chao Yub7ad7512016-02-19 18:08:46 +0800432 up_read(&curseg->journal_rwsem);
Yunlei He66a82d12017-04-22 18:06:26 +0800433 if (i >= 0) {
434 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900435 goto cache;
Yunlei He66a82d12017-04-22 18:06:26 +0800436 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900437
438 /* Fill node_info from nat page */
Yunlei He66a82d12017-04-22 18:06:26 +0800439 index = current_nat_addr(sbi, nid);
440 up_read(&nm_i->nat_tree_lock);
441
442 page = get_meta_page(sbi, index);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900443 nat_blk = (struct f2fs_nat_block *)page_address(page);
444 ne = nat_blk->entries[nid - start_nid];
445 node_info_from_raw_nat(ni, &ne);
446 f2fs_put_page(page, 1);
447cache:
448 /* cache nat entry */
Chao Yu1515aef2016-02-19 18:12:28 +0800449 cache_nat_entry(sbi, nid, &ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900450}
451
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700452/*
453 * readahead MAX_RA_NODE number of node pages.
454 */
455static void ra_node_pages(struct page *parent, int start, int n)
456{
457 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
458 struct blk_plug plug;
459 int i, end;
460 nid_t nid;
461
462 blk_start_plug(&plug);
463
464 /* Then, try readahead for siblings of the desired node */
465 end = start + n;
466 end = min(end, NIDS_PER_BLOCK);
467 for (i = start; i < end; i++) {
468 nid = get_nid(parent, i, false);
469 ra_node_page(sbi, nid);
470 }
471
472 blk_finish_plug(&plug);
473}
474
Chao Yu3cf45742016-01-26 15:40:44 +0800475pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
476{
477 const long direct_index = ADDRS_PER_INODE(dn->inode);
478 const long direct_blks = ADDRS_PER_BLOCK;
479 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
480 unsigned int skipped_unit = ADDRS_PER_BLOCK;
481 int cur_level = dn->cur_level;
482 int max_level = dn->max_level;
483 pgoff_t base = 0;
484
485 if (!dn->max_level)
486 return pgofs + 1;
487
488 while (max_level-- > cur_level)
489 skipped_unit *= NIDS_PER_BLOCK;
490
491 switch (dn->max_level) {
492 case 3:
493 base += 2 * indirect_blks;
494 case 2:
495 base += 2 * direct_blks;
496 case 1:
497 base += direct_index;
498 break;
499 default:
500 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
501 }
502
503 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
504}
505
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900506/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900507 * The maximum depth is four.
508 * Offset[0] will have raw inode offset.
509 */
Chao Yu81ca7352016-01-26 15:39:35 +0800510static int get_node_path(struct inode *inode, long block,
Jaegeuk Kimde936532013-08-12 21:08:03 +0900511 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900512{
Chao Yu81ca7352016-01-26 15:39:35 +0800513 const long direct_index = ADDRS_PER_INODE(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900514 const long direct_blks = ADDRS_PER_BLOCK;
515 const long dptrs_per_blk = NIDS_PER_BLOCK;
516 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
517 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
518 int n = 0;
519 int level = 0;
520
521 noffset[0] = 0;
522
523 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900524 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900525 goto got;
526 }
527 block -= direct_index;
528 if (block < direct_blks) {
529 offset[n++] = NODE_DIR1_BLOCK;
530 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900531 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900532 level = 1;
533 goto got;
534 }
535 block -= direct_blks;
536 if (block < direct_blks) {
537 offset[n++] = NODE_DIR2_BLOCK;
538 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900539 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900540 level = 1;
541 goto got;
542 }
543 block -= direct_blks;
544 if (block < indirect_blks) {
545 offset[n++] = NODE_IND1_BLOCK;
546 noffset[n] = 3;
547 offset[n++] = block / direct_blks;
548 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900549 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900550 level = 2;
551 goto got;
552 }
553 block -= indirect_blks;
554 if (block < indirect_blks) {
555 offset[n++] = NODE_IND2_BLOCK;
556 noffset[n] = 4 + dptrs_per_blk;
557 offset[n++] = block / direct_blks;
558 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900559 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900560 level = 2;
561 goto got;
562 }
563 block -= indirect_blks;
564 if (block < dindirect_blks) {
565 offset[n++] = NODE_DIND_BLOCK;
566 noffset[n] = 5 + (dptrs_per_blk * 2);
567 offset[n++] = block / indirect_blks;
568 noffset[n] = 6 + (dptrs_per_blk * 2) +
569 offset[n - 1] * (dptrs_per_blk + 1);
570 offset[n++] = (block / direct_blks) % dptrs_per_blk;
571 noffset[n] = 7 + (dptrs_per_blk * 2) +
572 offset[n - 2] * (dptrs_per_blk + 1) +
573 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900574 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900575 level = 3;
576 goto got;
577 } else {
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700578 return -E2BIG;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900579 }
580got:
581 return level;
582}
583
584/*
585 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800586 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
587 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900588 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900589 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900590int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900591{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700592 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900593 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800594 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900595 int offset[4];
596 unsigned int noffset[4];
597 nid_t nids[4];
Chao Yu3cf45742016-01-26 15:40:44 +0800598 int level, i = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900599 int err = 0;
600
Chao Yu81ca7352016-01-26 15:39:35 +0800601 level = get_node_path(dn->inode, index, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700602 if (level < 0)
603 return level;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900604
605 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900606 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900607
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900608 if (!npage[0]) {
609 npage[0] = get_node_page(sbi, nids[0]);
610 if (IS_ERR(npage[0]))
611 return PTR_ERR(npage[0]);
612 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800613
614 /* if inline_data is set, should not report any block indices */
615 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800616 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800617 f2fs_put_page(npage[0], 1);
618 goto release_out;
619 }
620
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900621 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900622 if (level != 0)
623 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900624 dn->inode_page = npage[0];
625 dn->inode_page_locked = true;
626
627 /* get indirect or direct nodes */
628 for (i = 1; i <= level; i++) {
629 bool done = false;
630
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900631 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900632 /* alloc new node */
633 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900634 err = -ENOSPC;
635 goto release_pages;
636 }
637
638 dn->nid = nids[i];
Yunlei He5f4ce6a2017-07-17 19:16:11 +0800639 npage[i] = new_node_page(dn, noffset[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900640 if (IS_ERR(npage[i])) {
641 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900642 err = PTR_ERR(npage[i]);
643 goto release_pages;
644 }
645
646 set_nid(parent, offset[i - 1], nids[i], i == 1);
647 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900648 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900649 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900650 npage[i] = get_node_page_ra(parent, offset[i - 1]);
651 if (IS_ERR(npage[i])) {
652 err = PTR_ERR(npage[i]);
653 goto release_pages;
654 }
655 done = true;
656 }
657 if (i == 1) {
658 dn->inode_page_locked = false;
659 unlock_page(parent);
660 } else {
661 f2fs_put_page(parent, 1);
662 }
663
664 if (!done) {
665 npage[i] = get_node_page(sbi, nids[i]);
666 if (IS_ERR(npage[i])) {
667 err = PTR_ERR(npage[i]);
668 f2fs_put_page(npage[0], 0);
669 goto release_out;
670 }
671 }
672 if (i < level) {
673 parent = npage[i];
674 nids[i + 1] = get_nid(parent, offset[i], false);
675 }
676 }
677 dn->nid = nids[level];
678 dn->ofs_in_node = offset[level];
679 dn->node_page = npage[level];
Chao Yu7a2af762017-07-19 00:19:06 +0800680 dn->data_blkaddr = datablock_addr(dn->inode,
681 dn->node_page, dn->ofs_in_node);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900682 return 0;
683
684release_pages:
685 f2fs_put_page(parent, 1);
686 if (i > 1)
687 f2fs_put_page(npage[0], 0);
688release_out:
689 dn->inode_page = NULL;
690 dn->node_page = NULL;
Chao Yu3cf45742016-01-26 15:40:44 +0800691 if (err == -ENOENT) {
692 dn->cur_level = i;
693 dn->max_level = level;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700694 dn->ofs_in_node = offset[level];
Chao Yu3cf45742016-01-26 15:40:44 +0800695 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900696 return err;
697}
698
699static void truncate_node(struct dnode_of_data *dn)
700{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700701 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900702 struct node_info ni;
703
704 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700705 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900706
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900707 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900708 invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +0800709 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900710 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900711
712 if (dn->nid == dn->inode->i_ino) {
713 remove_orphan_inode(sbi, dn->nid);
714 dec_valid_inode_count(sbi);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700715 f2fs_inode_synced(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716 }
Chao Yu000519f2017-07-06 01:11:31 +0800717
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900718 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800719 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900720
721 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900722
723 invalidate_mapping_pages(NODE_MAPPING(sbi),
724 dn->node_page->index, dn->node_page->index);
725
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900726 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900727 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900728}
729
730static int truncate_dnode(struct dnode_of_data *dn)
731{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900732 struct page *page;
733
734 if (dn->nid == 0)
735 return 1;
736
737 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700738 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900739 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
740 return 1;
741 else if (IS_ERR(page))
742 return PTR_ERR(page);
743
744 /* Make dnode_of_data for parameter */
745 dn->node_page = page;
746 dn->ofs_in_node = 0;
747 truncate_data_blocks(dn);
748 truncate_node(dn);
749 return 1;
750}
751
752static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
753 int ofs, int depth)
754{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900755 struct dnode_of_data rdn = *dn;
756 struct page *page;
757 struct f2fs_node *rn;
758 nid_t child_nid;
759 unsigned int child_nofs;
760 int freed = 0;
761 int i, ret;
762
763 if (dn->nid == 0)
764 return NIDS_PER_BLOCK + 1;
765
Namjae Jeon51dd6242013-04-20 01:28:52 +0900766 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
767
Jaegeuk Kim40813632014-09-02 15:31:18 -0700768 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900769 if (IS_ERR(page)) {
770 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900771 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900772 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900773
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700774 ra_node_pages(page, ofs, NIDS_PER_BLOCK);
775
Gu Zheng45590712013-07-15 17:57:38 +0800776 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900777 if (depth < 3) {
778 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
779 child_nid = le32_to_cpu(rn->in.nid[i]);
780 if (child_nid == 0)
781 continue;
782 rdn.nid = child_nid;
783 ret = truncate_dnode(&rdn);
784 if (ret < 0)
785 goto out_err;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800786 if (set_nid(page, i, 0, false))
787 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900788 }
789 } else {
790 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
791 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
792 child_nid = le32_to_cpu(rn->in.nid[i]);
793 if (child_nid == 0) {
794 child_nofs += NIDS_PER_BLOCK + 1;
795 continue;
796 }
797 rdn.nid = child_nid;
798 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
799 if (ret == (NIDS_PER_BLOCK + 1)) {
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800800 if (set_nid(page, i, 0, false))
801 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900802 child_nofs += ret;
803 } else if (ret < 0 && ret != -ENOENT) {
804 goto out_err;
805 }
806 }
807 freed = child_nofs;
808 }
809
810 if (!ofs) {
811 /* remove current indirect node */
812 dn->node_page = page;
813 truncate_node(dn);
814 freed++;
815 } else {
816 f2fs_put_page(page, 1);
817 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900818 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900819 return freed;
820
821out_err:
822 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900823 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900824 return ret;
825}
826
827static int truncate_partial_nodes(struct dnode_of_data *dn,
828 struct f2fs_inode *ri, int *offset, int depth)
829{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900830 struct page *pages[2];
831 nid_t nid[3];
832 nid_t child_nid;
833 int err = 0;
834 int i;
835 int idx = depth - 2;
836
837 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
838 if (!nid[0])
839 return 0;
840
841 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800842 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900843 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700844 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900845 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900846 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800847 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900848 goto fail;
849 }
850 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
851 }
852
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700853 ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
854
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800856 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900857 child_nid = get_nid(pages[idx], i, false);
858 if (!child_nid)
859 continue;
860 dn->nid = child_nid;
861 err = truncate_dnode(dn);
862 if (err < 0)
863 goto fail;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800864 if (set_nid(pages[idx], i, 0, false))
865 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900866 }
867
shifei10.gea225dca2013-10-29 15:32:34 +0800868 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900869 dn->node_page = pages[idx];
870 dn->nid = nid[idx];
871 truncate_node(dn);
872 } else {
873 f2fs_put_page(pages[idx], 1);
874 }
875 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800876 offset[idx + 1] = 0;
877 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900878fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800879 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900880 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900881
882 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
883
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900884 return err;
885}
886
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900887/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900888 * All the block addresses of data and nodes should be nullified.
889 */
890int truncate_inode_blocks(struct inode *inode, pgoff_t from)
891{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700892 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900893 int err = 0, cont = 1;
894 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900895 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900896 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900897 struct dnode_of_data dn;
898 struct page *page;
899
Namjae Jeon51dd6242013-04-20 01:28:52 +0900900 trace_f2fs_truncate_inode_blocks_enter(inode, from);
901
Chao Yu81ca7352016-01-26 15:39:35 +0800902 level = get_node_path(inode, from, offset, noffset);
Jaegeuk Kimadb6dc192017-08-21 13:51:32 -0700903 if (level < 0)
904 return level;
Jaegeuk Kimff373552016-03-29 16:13:45 -0700905
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900907 if (IS_ERR(page)) {
908 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900909 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900910 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900911
912 set_new_dnode(&dn, inode, page, NULL, 0);
913 unlock_page(page);
914
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900915 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900916 switch (level) {
917 case 0:
918 case 1:
919 nofs = noffset[1];
920 break;
921 case 2:
922 nofs = noffset[1];
923 if (!offset[level - 1])
924 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900925 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900926 if (err < 0 && err != -ENOENT)
927 goto fail;
928 nofs += 1 + NIDS_PER_BLOCK;
929 break;
930 case 3:
931 nofs = 5 + 2 * NIDS_PER_BLOCK;
932 if (!offset[level - 1])
933 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900934 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900935 if (err < 0 && err != -ENOENT)
936 goto fail;
937 break;
938 default:
939 BUG();
940 }
941
942skip_partial:
943 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900944 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900945 switch (offset[0]) {
946 case NODE_DIR1_BLOCK:
947 case NODE_DIR2_BLOCK:
948 err = truncate_dnode(&dn);
949 break;
950
951 case NODE_IND1_BLOCK:
952 case NODE_IND2_BLOCK:
953 err = truncate_nodes(&dn, nofs, offset[1], 2);
954 break;
955
956 case NODE_DIND_BLOCK:
957 err = truncate_nodes(&dn, nofs, offset[1], 3);
958 cont = 0;
959 break;
960
961 default:
962 BUG();
963 }
964 if (err < 0 && err != -ENOENT)
965 goto fail;
966 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900967 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900968 lock_page(page);
Jaegeuk Kimff373552016-03-29 16:13:45 -0700969 BUG_ON(page->mapping != NODE_MAPPING(sbi));
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800970 f2fs_wait_on_page_writeback(page, NODE, true);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900971 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900972 set_page_dirty(page);
973 unlock_page(page);
974 }
975 offset[1] = 0;
976 offset[0]++;
977 nofs += err;
978 }
979fail:
980 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900981 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900982 return err > 0 ? 0 : err;
983}
984
Jaegeuk Kim9c77f752017-10-19 11:48:57 -0700985/* caller must lock inode page */
986int truncate_xattr_node(struct inode *inode)
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900987{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700988 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900989 nid_t nid = F2FS_I(inode)->i_xattr_nid;
990 struct dnode_of_data dn;
991 struct page *npage;
992
993 if (!nid)
994 return 0;
995
996 npage = get_node_page(sbi, nid);
997 if (IS_ERR(npage))
998 return PTR_ERR(npage);
999
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001000 f2fs_i_xnid_write(inode, 0);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001001
Jaegeuk Kim9c77f752017-10-19 11:48:57 -07001002 set_new_dnode(&dn, inode, NULL, npage, nid);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001003 truncate_node(&dn);
1004 return 0;
1005}
1006
Jaegeuk Kim39936832012-11-22 16:21:29 +09001007/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001008 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1009 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +09001010 */
Chao Yu13ec7292015-08-24 17:40:45 +08001011int remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001012{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001013 struct dnode_of_data dn;
Chao Yu13ec7292015-08-24 17:40:45 +08001014 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001015
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001016 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Chao Yu13ec7292015-08-24 17:40:45 +08001017 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
1018 if (err)
1019 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001020
Jaegeuk Kim9c77f752017-10-19 11:48:57 -07001021 err = truncate_xattr_node(inode);
Chao Yu13ec7292015-08-24 17:40:45 +08001022 if (err) {
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001023 f2fs_put_dnode(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +08001024 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001025 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001026
1027 /* remove potential inline_data blocks */
1028 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1029 S_ISLNK(inode->i_mode))
1030 truncate_data_blocks_range(&dn, 1);
1031
arter97e1c42042014-08-06 23:22:50 +09001032 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001033 f2fs_bug_on(F2FS_I_SB(inode),
Chao Yu0eb0ada2017-06-14 23:00:56 +08001034 inode->i_blocks != 0 && inode->i_blocks != 8);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001035
1036 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +09001037 truncate_node(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +08001038 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001039}
1040
Jaegeuk Kima014e032014-06-20 21:44:02 -07001041struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001042{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001043 struct dnode_of_data dn;
1044
1045 /* allocate inode page for new inode */
1046 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +09001047
1048 /* caller should f2fs_put_page(page, 1); */
Yunlei He5f4ce6a2017-07-17 19:16:11 +08001049 return new_node_page(&dn, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001050}
1051
Yunlei He5f4ce6a2017-07-17 19:16:11 +08001052struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001053{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001054 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001055 struct node_info new_ni;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001056 struct page *page;
1057 int err;
1058
Jaegeuk Kim91942322016-05-20 10:13:22 -07001059 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001060 return ERR_PTR(-EPERM);
1061
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001062 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001063 if (!page)
1064 return ERR_PTR(-ENOMEM);
1065
Chao Yu0abd6752017-07-09 00:13:07 +08001066 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001067 goto fail;
Chao Yu0abd6752017-07-09 00:13:07 +08001068
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001069#ifdef CONFIG_F2FS_CHECK_FS
1070 get_node_info(sbi, dn->nid, &new_ni);
1071 f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
1072#endif
1073 new_ni.nid = dn->nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001074 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim25cc5d32017-02-13 17:02:44 -08001075 new_ni.blk_addr = NULL_ADDR;
1076 new_ni.flag = 0;
1077 new_ni.version = 0;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001078 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001079
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001080 f2fs_wait_on_page_writeback(page, NODE, true);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001081 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +09001082 set_cold_node(dn->inode, page);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001083 if (!PageUptodate(page))
1084 SetPageUptodate(page);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001085 if (set_page_dirty(page))
1086 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001087
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001088 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001089 f2fs_i_xnid_write(dn->inode, dn->nid);
Jaegeuk Kim479bd732013-08-12 16:04:53 +09001090
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001091 if (ofs == 0)
1092 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001093 return page;
1094
1095fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +09001096 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001097 f2fs_put_page(page, 1);
1098 return ERR_PTR(err);
1099}
1100
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001101/*
1102 * Caller should do after getting the following values.
1103 * 0: f2fs_put_page(page, 0)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001104 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001105 */
Mike Christie04d328d2016-06-05 14:31:55 -05001106static int read_node_page(struct page *page, int op_flags)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001107{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001108 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001109 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001110 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001111 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001112 .type = NODE,
Mike Christie04d328d2016-06-05 14:31:55 -05001113 .op = REQ_OP_READ,
1114 .op_flags = op_flags,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001115 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001116 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001117 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001118
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001119 if (PageUptodate(page))
1120 return LOCKED_PAGE;
1121
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001122 get_node_info(sbi, page->index, &ni);
1123
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001124 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001125 ClearPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001126 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001127 }
1128
Chao Yu7a9d7542016-02-22 18:36:38 +08001129 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001130 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001131}
1132
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001133/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001134 * Readahead a node page
1135 */
1136void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1137{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001138 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001139 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001140
Chao Yue8458722016-01-08 20:13:37 +08001141 if (!nid)
1142 return;
1143 f2fs_bug_on(sbi, check_nid_range(sbi, nid));
1144
Fan Li999270d2016-02-29 14:29:51 +08001145 rcu_read_lock();
1146 apage = radix_tree_lookup(&NODE_MAPPING(sbi)->page_tree, nid);
1147 rcu_read_unlock();
1148 if (apage)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001149 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001150
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001151 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001152 if (!apage)
1153 return;
1154
Christoph Hellwig70246282016-07-19 11:28:41 +02001155 err = read_node_page(apage, REQ_RAHEAD);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001156 f2fs_put_page(apage, err ? 1 : 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001157}
1158
Jaegeuk Kim17a0ee52016-03-08 09:04:35 -08001159static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
Chao Yu0e022ea2016-01-05 16:52:46 +08001160 struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001161{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001162 struct page *page;
1163 int err;
Jaegeuk Kim4aa69d52015-12-23 14:17:47 -08001164
1165 if (!nid)
1166 return ERR_PTR(-ENOENT);
1167 f2fs_bug_on(sbi, check_nid_range(sbi, nid));
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001168repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001169 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001170 if (!page)
1171 return ERR_PTR(-ENOMEM);
1172
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001173 err = read_node_page(page, 0);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001174 if (err < 0) {
1175 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001176 return ERR_PTR(err);
Chao Yue1c51b9f2015-12-11 16:08:22 +08001177 } else if (err == LOCKED_PAGE) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001178 err = 0;
Chao Yue1c51b9f2015-12-11 16:08:22 +08001179 goto page_hit;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001180 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001181
Chao Yu0e022ea2016-01-05 16:52:46 +08001182 if (parent)
Jaegeuk Kim79344ef2016-05-06 16:19:43 -07001183 ra_node_pages(parent, start + 1, MAX_RA_NODE);
Chao Yu0e022ea2016-01-05 16:52:46 +08001184
Chao Yue1c51b9f2015-12-11 16:08:22 +08001185 lock_page(page);
1186
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001187 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001188 f2fs_put_page(page, 1);
1189 goto repeat;
1190 }
Chao Yu1563ac72016-07-03 22:05:12 +08001191
Chao Yu1f258ec2017-06-07 11:17:35 +08001192 if (unlikely(!PageUptodate(page))) {
1193 err = -EIO;
Chao Yu1563ac72016-07-03 22:05:12 +08001194 goto out_err;
Chao Yu1f258ec2017-06-07 11:17:35 +08001195 }
Chao Yu704956e2017-07-31 20:19:09 +08001196
1197 if (!f2fs_inode_chksum_verify(sbi, page)) {
1198 err = -EBADMSG;
1199 goto out_err;
1200 }
Chao Yue1c51b9f2015-12-11 16:08:22 +08001201page_hit:
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001202 if(unlikely(nid != nid_of_node(page))) {
Chao Yu1f258ec2017-06-07 11:17:35 +08001203 f2fs_msg(sbi->sb, KERN_WARNING, "inconsistent node block, "
1204 "nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1205 nid, nid_of_node(page), ino_of_node(page),
1206 ofs_of_node(page), cpver_of_node(page),
1207 next_blkaddr_of_node(page));
Chao Yu1f258ec2017-06-07 11:17:35 +08001208 err = -EINVAL;
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001209out_err:
Jaegeuk Kimee605232017-08-31 16:54:51 -07001210 ClearPageUptodate(page);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001211 f2fs_put_page(page, 1);
Chao Yu1f258ec2017-06-07 11:17:35 +08001212 return ERR_PTR(err);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001213 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001214 return page;
1215}
1216
Chao Yu0e022ea2016-01-05 16:52:46 +08001217struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1218{
1219 return __get_node_page(sbi, nid, NULL, 0);
1220}
1221
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001222struct page *get_node_page_ra(struct page *parent, int start)
1223{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001224 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Chao Yu0e022ea2016-01-05 16:52:46 +08001225 nid_t nid = get_nid(parent, start, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001226
Chao Yu0e022ea2016-01-05 16:52:46 +08001227 return __get_node_page(sbi, nid, parent, start);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001228}
1229
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001230static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1231{
1232 struct inode *inode;
1233 struct page *page;
Chao Yu0f3311a2016-05-21 00:11:09 +08001234 int ret;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001235
1236 /* should flush inline_data before evict_inode */
1237 inode = ilookup(sbi->sb, ino);
1238 if (!inode)
1239 return;
1240
Chao Yu01eccef2017-10-28 16:52:30 +08001241 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1242 FGP_LOCK|FGP_NOWAIT, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001243 if (!page)
1244 goto iput_out;
1245
1246 if (!PageUptodate(page))
1247 goto page_out;
1248
1249 if (!PageDirty(page))
1250 goto page_out;
1251
1252 if (!clear_page_dirty_for_io(page))
1253 goto page_out;
1254
Chao Yu0f3311a2016-05-21 00:11:09 +08001255 ret = f2fs_write_inline_data(inode, page);
1256 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08001257 remove_dirty_inode(inode);
Chao Yu0f3311a2016-05-21 00:11:09 +08001258 if (ret)
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001259 set_page_dirty(page);
1260page_out:
Jaegeuk Kim4a6de502016-03-30 11:25:31 -07001261 f2fs_put_page(page, 1);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001262iput_out:
1263 iput(inode);
1264}
1265
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001266static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001267{
Jan Kara028a63a2017-11-15 17:34:51 -08001268 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001269 struct pagevec pvec;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001270 struct page *last_page = NULL;
Jan Kara028a63a2017-11-15 17:34:51 -08001271 int nr_pages;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001272
Mel Gorman86679822017-11-15 17:37:52 -08001273 pagevec_init(&pvec);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001274 index = 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001275
Jan Kara028a63a2017-11-15 17:34:51 -08001276 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001277 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001278 int i;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001279
1280 for (i = 0; i < nr_pages; i++) {
1281 struct page *page = pvec.pages[i];
1282
1283 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001284 f2fs_put_page(last_page, 0);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001285 pagevec_release(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001286 return ERR_PTR(-EIO);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001287 }
1288
1289 if (!IS_DNODE(page) || !is_cold_node(page))
1290 continue;
1291 if (ino_of_node(page) != ino)
1292 continue;
1293
1294 lock_page(page);
1295
1296 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1297continue_unlock:
1298 unlock_page(page);
1299 continue;
1300 }
1301 if (ino_of_node(page) != ino)
1302 goto continue_unlock;
1303
1304 if (!PageDirty(page)) {
1305 /* someone wrote it for us */
1306 goto continue_unlock;
1307 }
1308
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001309 if (last_page)
1310 f2fs_put_page(last_page, 0);
1311
1312 get_page(page);
1313 last_page = page;
1314 unlock_page(page);
1315 }
1316 pagevec_release(&pvec);
1317 cond_resched();
1318 }
1319 return last_page;
1320}
1321
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001322static int __write_node_page(struct page *page, bool atomic, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001323 struct writeback_control *wbc, bool do_balance,
1324 enum iostat_type io_type)
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001325{
1326 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1327 nid_t nid;
1328 struct node_info ni;
1329 struct f2fs_io_info fio = {
1330 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001331 .ino = ino_of_node(page),
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001332 .type = NODE,
1333 .op = REQ_OP_WRITE,
1334 .op_flags = wbc_to_write_flags(wbc),
1335 .page = page,
1336 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001337 .submitted = false,
Chao Yub0af6d42017-08-02 23:21:48 +08001338 .io_type = io_type,
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001339 };
1340
1341 trace_f2fs_writepage(page, NODE);
1342
1343 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1344 goto redirty_out;
1345 if (unlikely(f2fs_cp_error(sbi)))
1346 goto redirty_out;
1347
1348 /* get old block addr of this node page */
1349 nid = nid_of_node(page);
1350 f2fs_bug_on(sbi, page->index != nid);
1351
1352 if (wbc->for_reclaim) {
1353 if (!down_read_trylock(&sbi->node_write))
1354 goto redirty_out;
1355 } else {
1356 down_read(&sbi->node_write);
1357 }
1358
1359 get_node_info(sbi, nid, &ni);
1360
1361 /* This page is already truncated */
1362 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1363 ClearPageUptodate(page);
1364 dec_page_count(sbi, F2FS_DIRTY_NODES);
1365 up_read(&sbi->node_write);
1366 unlock_page(page);
1367 return 0;
1368 }
1369
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001370 if (atomic && !test_opt(sbi, NOBARRIER))
1371 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1372
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001373 set_page_writeback(page);
1374 fio.old_blkaddr = ni.blk_addr;
1375 write_node_page(nid, &fio);
1376 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1377 dec_page_count(sbi, F2FS_DIRTY_NODES);
1378 up_read(&sbi->node_write);
1379
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001380 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001381 f2fs_submit_merged_write_cond(sbi, page->mapping->host, 0,
1382 page->index, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001383 submitted = NULL;
1384 }
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001385
1386 unlock_page(page);
1387
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001388 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001389 f2fs_submit_merged_write(sbi, NODE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001390 submitted = NULL;
1391 }
1392 if (submitted)
1393 *submitted = fio.submitted;
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001394
Yunlong Song401db792017-07-27 20:11:00 +08001395 if (do_balance)
1396 f2fs_balance_fs(sbi, false);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001397 return 0;
1398
1399redirty_out:
1400 redirty_page_for_writepage(wbc, page);
1401 return AOP_WRITEPAGE_ACTIVATE;
1402}
1403
Yunlei Hef15194f2017-10-30 14:18:55 +08001404void move_node_page(struct page *node_page, int gc_type)
1405{
1406 if (gc_type == FG_GC) {
1407 struct writeback_control wbc = {
1408 .sync_mode = WB_SYNC_ALL,
1409 .nr_to_write = 1,
1410 .for_reclaim = 0,
1411 };
1412
1413 set_page_dirty(node_page);
1414 f2fs_wait_on_page_writeback(node_page, NODE, true);
1415
1416 f2fs_bug_on(F2FS_P_SB(node_page), PageWriteback(node_page));
1417 if (!clear_page_dirty_for_io(node_page))
1418 goto out_page;
1419
1420 if (__write_node_page(node_page, false, NULL,
1421 &wbc, false, FS_GC_NODE_IO))
1422 unlock_page(node_page);
1423 goto release_page;
1424 } else {
1425 /* set page dirty and write it */
1426 if (!PageWriteback(node_page))
1427 set_page_dirty(node_page);
1428 }
1429out_page:
1430 unlock_page(node_page);
1431release_page:
1432 f2fs_put_page(node_page, 0);
1433}
1434
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001435static int f2fs_write_node_page(struct page *page,
1436 struct writeback_control *wbc)
1437{
Chao Yub0af6d42017-08-02 23:21:48 +08001438 return __write_node_page(page, false, NULL, wbc, false, FS_NODE_IO);
Jaegeuk Kimfaa24892017-02-02 18:27:17 -08001439}
1440
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001441int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001442 struct writeback_control *wbc, bool atomic)
1443{
Jan Kara028a63a2017-11-15 17:34:51 -08001444 pgoff_t index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001445 pgoff_t last_idx = ULONG_MAX;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001446 struct pagevec pvec;
1447 int ret = 0;
1448 struct page *last_page = NULL;
1449 bool marked = false;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001450 nid_t ino = inode->i_ino;
Jan Kara028a63a2017-11-15 17:34:51 -08001451 int nr_pages;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001452
1453 if (atomic) {
1454 last_page = last_fsync_dnode(sbi, ino);
1455 if (IS_ERR_OR_NULL(last_page))
1456 return PTR_ERR_OR_ZERO(last_page);
1457 }
1458retry:
Mel Gorman86679822017-11-15 17:37:52 -08001459 pagevec_init(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001460 index = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001461
Jan Kara028a63a2017-11-15 17:34:51 -08001462 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001463 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001464 int i;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001465
1466 for (i = 0; i < nr_pages; i++) {
1467 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001468 bool submitted = false;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001469
1470 if (unlikely(f2fs_cp_error(sbi))) {
1471 f2fs_put_page(last_page, 0);
1472 pagevec_release(&pvec);
Chao Yu9de69272016-10-11 22:57:06 +08001473 ret = -EIO;
1474 goto out;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001475 }
1476
1477 if (!IS_DNODE(page) || !is_cold_node(page))
1478 continue;
1479 if (ino_of_node(page) != ino)
1480 continue;
1481
1482 lock_page(page);
1483
1484 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1485continue_unlock:
1486 unlock_page(page);
1487 continue;
1488 }
1489 if (ino_of_node(page) != ino)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001490 goto continue_unlock;
1491
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001492 if (!PageDirty(page) && page != last_page) {
1493 /* someone wrote it for us */
1494 goto continue_unlock;
1495 }
1496
1497 f2fs_wait_on_page_writeback(page, NODE, true);
1498 BUG_ON(PageWriteback(page));
1499
Jaegeuk Kimd29fd172017-04-12 12:02:00 -07001500 set_fsync_mark(page, 0);
1501 set_dentry_mark(page, 0);
1502
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001503 if (!atomic || page == last_page) {
1504 set_fsync_mark(page, 1);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001505 if (IS_INODE(page)) {
1506 if (is_inode_flag_set(inode,
1507 FI_DIRTY_INODE))
1508 update_inode(inode, page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001509 set_dentry_mark(page,
Jaegeuk Kim52681372016-04-13 16:24:44 -07001510 need_dentry_mark(sbi, ino));
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001511 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001512 /* may be written by other thread */
1513 if (!PageDirty(page))
1514 set_page_dirty(page);
1515 }
1516
1517 if (!clear_page_dirty_for_io(page))
1518 goto continue_unlock;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001519
Jaegeuk Kime7c75ab2017-02-02 18:18:06 -08001520 ret = __write_node_page(page, atomic &&
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001521 page == last_page,
Chao Yub0af6d42017-08-02 23:21:48 +08001522 &submitted, wbc, true,
1523 FS_NODE_IO);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001524 if (ret) {
Jaegeuk Kim52681372016-04-13 16:24:44 -07001525 unlock_page(page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001526 f2fs_put_page(last_page, 0);
1527 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001528 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001529 last_idx = page->index;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001530 }
Chao Yu3f5f4952016-09-29 18:50:10 +08001531
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001532 if (page == last_page) {
1533 f2fs_put_page(page, 0);
1534 marked = true;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001535 break;
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001536 }
Jaegeuk Kim52681372016-04-13 16:24:44 -07001537 }
1538 pagevec_release(&pvec);
1539 cond_resched();
1540
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001541 if (ret || marked)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001542 break;
1543 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001544 if (!ret && atomic && !marked) {
1545 f2fs_msg(sbi->sb, KERN_DEBUG,
1546 "Retry to write fsync mark: ino=%u, idx=%lx",
1547 ino, last_page->index);
1548 lock_page(last_page);
Yunlei Hed40a43a2016-11-16 17:26:24 +08001549 f2fs_wait_on_page_writeback(last_page, NODE, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001550 set_page_dirty(last_page);
1551 unlock_page(last_page);
1552 goto retry;
1553 }
Chao Yu9de69272016-10-11 22:57:06 +08001554out:
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001555 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001556 f2fs_submit_merged_write_cond(sbi, NULL, ino, last_idx, NODE);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001557 return ret ? -EIO: 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001558}
1559
Yunlong Song401db792017-07-27 20:11:00 +08001560int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
Chao Yub0af6d42017-08-02 23:21:48 +08001561 bool do_balance, enum iostat_type io_type)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001562{
Jan Kara028a63a2017-11-15 17:34:51 -08001563 pgoff_t index;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001564 struct pagevec pvec;
1565 int step = 0;
Jaegeuk Kim12bb0a82016-03-11 15:33:22 -08001566 int nwritten = 0;
Chao Yu3f5f4952016-09-29 18:50:10 +08001567 int ret = 0;
Jan Kara028a63a2017-11-15 17:34:51 -08001568 int nr_pages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001569
Mel Gorman86679822017-11-15 17:37:52 -08001570 pagevec_init(&pvec);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001571
1572next_step:
1573 index = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001574
Jan Kara028a63a2017-11-15 17:34:51 -08001575 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001576 PAGECACHE_TAG_DIRTY))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001577 int i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001578
1579 for (i = 0; i < nr_pages; i++) {
1580 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001581 bool submitted = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001582
Chao Yu6d5a1492015-12-24 18:04:56 +08001583 if (unlikely(f2fs_cp_error(sbi))) {
1584 pagevec_release(&pvec);
Chao Yu3f5f4952016-09-29 18:50:10 +08001585 ret = -EIO;
1586 goto out;
Chao Yu6d5a1492015-12-24 18:04:56 +08001587 }
1588
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001589 /*
1590 * flushing sequence with step:
1591 * 0. indirect nodes
1592 * 1. dentry dnodes
1593 * 2. file dnodes
1594 */
1595 if (step == 0 && IS_DNODE(page))
1596 continue;
1597 if (step == 1 && (!IS_DNODE(page) ||
1598 is_cold_node(page)))
1599 continue;
1600 if (step == 2 && (!IS_DNODE(page) ||
1601 !is_cold_node(page)))
1602 continue;
Chao Yu9a4cbc92016-02-22 18:35:46 +08001603lock_node:
Jaegeuk Kim52681372016-04-13 16:24:44 -07001604 if (!trylock_page(page))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001605 continue;
1606
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001607 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001608continue_unlock:
1609 unlock_page(page);
1610 continue;
1611 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001612
1613 if (!PageDirty(page)) {
1614 /* someone wrote it for us */
1615 goto continue_unlock;
1616 }
1617
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001618 /* flush inline_data */
Jaegeuk Kim52681372016-04-13 16:24:44 -07001619 if (is_inline_node(page)) {
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001620 clear_inline_node(page);
1621 unlock_page(page);
1622 flush_inline_data(sbi, ino_of_node(page));
Chao Yu9a4cbc92016-02-22 18:35:46 +08001623 goto lock_node;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001624 }
1625
Jaegeuk Kimfa3d2bd2016-01-28 11:48:52 -08001626 f2fs_wait_on_page_writeback(page, NODE, true);
1627
1628 BUG_ON(PageWriteback(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001629 if (!clear_page_dirty_for_io(page))
1630 goto continue_unlock;
1631
Jaegeuk Kim52681372016-04-13 16:24:44 -07001632 set_fsync_mark(page, 0);
1633 set_dentry_mark(page, 0);
Jaegeuk Kim52746512014-08-11 18:18:36 -07001634
Yunlong Song401db792017-07-27 20:11:00 +08001635 ret = __write_node_page(page, false, &submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001636 wbc, do_balance, io_type);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001637 if (ret)
Jaegeuk Kim52746512014-08-11 18:18:36 -07001638 unlock_page(page);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001639 else if (submitted)
Chao Yu3f5f4952016-09-29 18:50:10 +08001640 nwritten++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001641
1642 if (--wbc->nr_to_write == 0)
1643 break;
1644 }
1645 pagevec_release(&pvec);
1646 cond_resched();
1647
1648 if (wbc->nr_to_write == 0) {
1649 step = 2;
1650 break;
1651 }
1652 }
1653
1654 if (step < 2) {
1655 step++;
1656 goto next_step;
1657 }
Chao Yu3f5f4952016-09-29 18:50:10 +08001658out:
1659 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001660 f2fs_submit_merged_write(sbi, NODE);
Chao Yu3f5f4952016-09-29 18:50:10 +08001661 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001662}
1663
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001664int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1665{
Jan Kara028a63a2017-11-15 17:34:51 -08001666 pgoff_t index = 0;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001667 struct pagevec pvec;
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001668 int ret2, ret = 0;
Jan Kara028a63a2017-11-15 17:34:51 -08001669 int nr_pages;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001670
Mel Gorman86679822017-11-15 17:37:52 -08001671 pagevec_init(&pvec);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001672
Jan Kara028a63a2017-11-15 17:34:51 -08001673 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara67fd7072017-11-15 17:35:19 -08001674 PAGECACHE_TAG_WRITEBACK))) {
Jan Kara028a63a2017-11-15 17:34:51 -08001675 int i;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001676
1677 for (i = 0; i < nr_pages; i++) {
1678 struct page *page = pvec.pages[i];
1679
Chao Yu4bf08ff2013-11-04 10:28:33 +08001680 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001681 f2fs_wait_on_page_writeback(page, NODE, true);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001682 if (TestClearPageError(page))
1683 ret = -EIO;
1684 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001685 }
1686 pagevec_release(&pvec);
1687 cond_resched();
1688 }
1689
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001690 ret2 = filemap_check_errors(NODE_MAPPING(sbi));
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001691 if (!ret)
1692 ret = ret2;
1693 return ret;
1694}
1695
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001696static int f2fs_write_node_pages(struct address_space *mapping,
1697 struct writeback_control *wbc)
1698{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001699 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001700 struct blk_plug plug;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001701 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001702
Chao Yu0771fcc2017-06-29 23:20:45 +08001703 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1704 goto skip_write;
1705
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001706 /* balancing f2fs's metadata in background */
1707 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001708
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001709 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001710 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001711 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001712
Yunlei Hed31c7c32016-02-04 16:14:00 +08001713 trace_f2fs_writepages(mapping->host, wbc, NODE);
1714
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001715 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001716 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001717 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08001718 sync_node_pages(sbi, wbc, true, FS_NODE_IO);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001719 blk_finish_plug(&plug);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001720 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001721 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001722
1723skip_write:
1724 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001725 trace_f2fs_writepages(mapping->host, wbc, NODE);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001726 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001727}
1728
1729static int f2fs_set_node_page_dirty(struct page *page)
1730{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001731 trace_f2fs_set_page_dirty(page, NODE);
1732
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001733 if (!PageUptodate(page))
1734 SetPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001735 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001736 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001737 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001738 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001739 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001740 return 1;
1741 }
1742 return 0;
1743}
1744
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001745/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001746 * Structure of the f2fs node operations
1747 */
1748const struct address_space_operations f2fs_node_aops = {
1749 .writepage = f2fs_write_node_page,
1750 .writepages = f2fs_write_node_pages,
1751 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001752 .invalidatepage = f2fs_invalidate_page,
1753 .releasepage = f2fs_release_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08001754#ifdef CONFIG_MIGRATION
1755 .migratepage = f2fs_migrate_page,
1756#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001757};
1758
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001759static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1760 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001761{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001762 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001763}
1764
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001765static int __insert_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08001766 struct free_nid *i, enum nid_state state)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001767{
Chao Yub8559dc2016-10-12 19:28:29 +08001768 struct f2fs_nm_info *nm_i = NM_I(sbi);
1769
Fan Lia0761f62017-10-28 19:03:37 +08001770 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
1771 if (err)
1772 return err;
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07001773
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001774 f2fs_bug_on(sbi, state != i->state);
1775 nm_i->nid_cnt[state]++;
1776 if (state == FREE_NID)
1777 list_add_tail(&i->list, &nm_i->free_nid_list);
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07001778 return 0;
Chao Yub8559dc2016-10-12 19:28:29 +08001779}
1780
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001781static void __remove_free_nid(struct f2fs_sb_info *sbi,
Fan Lia0761f62017-10-28 19:03:37 +08001782 struct free_nid *i, enum nid_state state)
Chao Yub8559dc2016-10-12 19:28:29 +08001783{
1784 struct f2fs_nm_info *nm_i = NM_I(sbi);
1785
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001786 f2fs_bug_on(sbi, state != i->state);
1787 nm_i->nid_cnt[state]--;
1788 if (state == FREE_NID)
1789 list_del(&i->list);
Fan Lia0761f62017-10-28 19:03:37 +08001790 radix_tree_delete(&nm_i->free_nid_root, i->nid);
1791}
1792
1793static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
1794 enum nid_state org_state, enum nid_state dst_state)
1795{
1796 struct f2fs_nm_info *nm_i = NM_I(sbi);
1797
1798 f2fs_bug_on(sbi, org_state != i->state);
1799 i->state = dst_state;
1800 nm_i->nid_cnt[org_state]--;
1801 nm_i->nid_cnt[dst_state]++;
1802
1803 switch (dst_state) {
1804 case PREALLOC_NID:
1805 list_del(&i->list);
1806 break;
1807 case FREE_NID:
1808 list_add_tail(&i->list, &nm_i->free_nid_list);
1809 break;
1810 default:
1811 BUG_ON(1);
1812 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001813}
1814
Chao Yu4ac91242017-02-23 10:53:49 +08001815/* return if the nid is recognized as free */
1816static bool add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001817{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001818 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu30a61dd2017-03-22 14:45:05 +08001819 struct free_nid *i, *e;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001820 struct nat_entry *ne;
Chao Yu30a61dd2017-03-22 14:45:05 +08001821 int err = -EINVAL;
1822 bool ret = false;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001823
1824 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001825 if (unlikely(nid == 0))
Chao Yu4ac91242017-02-23 10:53:49 +08001826 return false;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001827
Gu Zheng7bd59382013-10-22 14:52:26 +08001828 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001829 i->nid = nid;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001830 i->state = FREE_NID;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001831
Chao Yu30a61dd2017-03-22 14:45:05 +08001832 if (radix_tree_preload(GFP_NOFS))
1833 goto err;
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001834
Chao Yub8559dc2016-10-12 19:28:29 +08001835 spin_lock(&nm_i->nid_list_lock);
Chao Yu30a61dd2017-03-22 14:45:05 +08001836
1837 if (build) {
1838 /*
1839 * Thread A Thread B
1840 * - f2fs_create
1841 * - f2fs_new_inode
1842 * - alloc_nid
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001843 * - __insert_nid_to_list(PREALLOC_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08001844 * - f2fs_balance_fs_bg
1845 * - build_free_nids
1846 * - __build_free_nids
1847 * - scan_nat_page
1848 * - add_free_nid
1849 * - __lookup_nat_cache
1850 * - f2fs_add_link
1851 * - init_inode_metadata
1852 * - new_inode_page
1853 * - new_node_page
1854 * - set_node_addr
1855 * - alloc_nid_done
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001856 * - __remove_nid_from_list(PREALLOC_NID)
1857 * - __insert_nid_to_list(FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08001858 */
1859 ne = __lookup_nat_cache(nm_i, nid);
1860 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1861 nat_get_blkaddr(ne) != NULL_ADDR))
1862 goto err_out;
1863
1864 e = __lookup_free_nid_list(nm_i, nid);
1865 if (e) {
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001866 if (e->state == FREE_NID)
Chao Yu30a61dd2017-03-22 14:45:05 +08001867 ret = true;
1868 goto err_out;
1869 }
1870 }
1871 ret = true;
Fan Lia0761f62017-10-28 19:03:37 +08001872 err = __insert_free_nid(sbi, i, FREE_NID);
Chao Yu30a61dd2017-03-22 14:45:05 +08001873err_out:
Jaegeuk Kimeb0aa4b2016-10-12 10:09:59 -07001874 spin_unlock(&nm_i->nid_list_lock);
1875 radix_tree_preload_end();
Chao Yu30a61dd2017-03-22 14:45:05 +08001876err:
1877 if (err)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001878 kmem_cache_free(free_nid_slab, i);
Chao Yu30a61dd2017-03-22 14:45:05 +08001879 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001880}
1881
Chao Yub8559dc2016-10-12 19:28:29 +08001882static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001883{
Chao Yub8559dc2016-10-12 19:28:29 +08001884 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001885 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001886 bool need_free = false;
1887
Chao Yub8559dc2016-10-12 19:28:29 +08001888 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001889 i = __lookup_free_nid_list(nm_i, nid);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001890 if (i && i->state == FREE_NID) {
Fan Lia0761f62017-10-28 19:03:37 +08001891 __remove_free_nid(sbi, i, FREE_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001892 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001893 }
Chao Yub8559dc2016-10-12 19:28:29 +08001894 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001895
1896 if (need_free)
1897 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001898}
1899
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08001900static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
Chao Yu346fe752017-03-13 20:10:41 +08001901 bool set, bool build)
Chao Yu4ac91242017-02-23 10:53:49 +08001902{
1903 struct f2fs_nm_info *nm_i = NM_I(sbi);
1904 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
1905 unsigned int nid_ofs = nid - START_NID(nid);
1906
1907 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
1908 return;
1909
LiFan0dd99ca2017-11-10 15:41:42 +08001910 if (set) {
1911 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
1912 return;
Jaegeuk Kim23380b82017-03-07 14:11:06 -08001913 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
Chao Yu586d1492017-03-01 17:09:07 +08001914 nm_i->free_nid_count[nat_ofs]++;
LiFan0dd99ca2017-11-10 15:41:42 +08001915 } else {
1916 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
1917 return;
1918 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
1919 if (!build)
1920 nm_i->free_nid_count[nat_ofs]--;
1921 }
Chao Yu4ac91242017-02-23 10:53:49 +08001922}
1923
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001924static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001925 struct page *nat_page, nid_t start_nid)
1926{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001927 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001928 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1929 block_t blk_addr;
Chao Yu4ac91242017-02-23 10:53:49 +08001930 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001931 int i;
1932
Jaegeuk Kim23380b82017-03-07 14:11:06 -08001933 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
Chao Yu4ac91242017-02-23 10:53:49 +08001934
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001935 i = start_nid % NAT_ENTRY_PER_BLOCK;
1936
1937 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Chao Yu4ac91242017-02-23 10:53:49 +08001938 bool freed = false;
Haicheng Li23d38842013-05-06 23:15:43 +08001939
Chao Yucfb271d2013-12-05 17:15:22 +08001940 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001941 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001942
1943 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001944 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Chao Yu3a2ad562016-10-11 22:31:35 +08001945 if (blk_addr == NULL_ADDR)
Chao Yu4ac91242017-02-23 10:53:49 +08001946 freed = add_free_nid(sbi, start_nid, true);
Chao Yu346fe752017-03-13 20:10:41 +08001947 spin_lock(&NM_I(sbi)->nid_list_lock);
1948 update_free_nid_bitmap(sbi, start_nid, freed, true);
1949 spin_unlock(&NM_I(sbi)->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001950 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001951}
1952
Chao Yu2fbaa252017-11-08 17:47:36 +08001953static void scan_curseg_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08001954{
Chao Yu4ac91242017-02-23 10:53:49 +08001955 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1956 struct f2fs_journal *journal = curseg->journal;
Chao Yu2fbaa252017-11-08 17:47:36 +08001957 int i;
Chao Yu4ac91242017-02-23 10:53:49 +08001958
Chao Yu4ac91242017-02-23 10:53:49 +08001959 down_read(&curseg->journal_rwsem);
1960 for (i = 0; i < nats_in_cursum(journal); i++) {
1961 block_t addr;
1962 nid_t nid;
1963
1964 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
1965 nid = le32_to_cpu(nid_in_journal(journal, i));
1966 if (addr == NULL_ADDR)
1967 add_free_nid(sbi, nid, true);
1968 else
1969 remove_free_nid(sbi, nid);
1970 }
1971 up_read(&curseg->journal_rwsem);
Chao Yu2fbaa252017-11-08 17:47:36 +08001972}
1973
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001974static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08001975{
1976 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08001977 unsigned int i, idx;
Fan Li97456572017-11-07 19:14:24 +08001978 nid_t nid;
Chao Yu4ac91242017-02-23 10:53:49 +08001979
1980 down_read(&nm_i->nat_tree_lock);
1981
1982 for (i = 0; i < nm_i->nat_blocks; i++) {
1983 if (!test_bit_le(i, nm_i->nat_block_bitmap))
1984 continue;
1985 if (!nm_i->free_nid_count[i])
1986 continue;
1987 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
Fan Li97456572017-11-07 19:14:24 +08001988 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
1989 NAT_ENTRY_PER_BLOCK, idx);
1990 if (idx >= NAT_ENTRY_PER_BLOCK)
1991 break;
Chao Yu4ac91242017-02-23 10:53:49 +08001992
1993 nid = i * NAT_ENTRY_PER_BLOCK + idx;
1994 add_free_nid(sbi, nid, true);
1995
Chao Yu9a4ffdf2017-09-29 13:59:35 +08001996 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
Chao Yu4ac91242017-02-23 10:53:49 +08001997 goto out;
1998 }
1999 }
2000out:
Chao Yu2fbaa252017-11-08 17:47:36 +08002001 scan_curseg_cache(sbi);
Chao Yu4ac91242017-02-23 10:53:49 +08002002
Chao Yu4ac91242017-02-23 10:53:49 +08002003 up_read(&nm_i->nat_tree_lock);
2004}
2005
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002006static void __build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002007{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002008 struct f2fs_nm_info *nm_i = NM_I(sbi);
Haicheng Li87609522013-05-06 23:15:42 +08002009 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002010 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002011
Yunlei Hee9cdd302017-04-26 15:56:52 +08002012 if (unlikely(nid >= nm_i->max_nid))
2013 nid = 0;
2014
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002015 /* Enough entries */
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002016 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002017 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002018
Chao Yu3a2ad562016-10-11 22:31:35 +08002019 if (!sync && !available_free_memory(sbi, FREE_NIDS))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002020 return;
2021
Chao Yu4ac91242017-02-23 10:53:49 +08002022 if (!mount) {
2023 /* try to find free nids in free_nid_bitmap */
2024 scan_free_nid_bits(sbi);
2025
Fan Li74986212017-11-07 11:04:33 +08002026 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yu4ac91242017-02-23 10:53:49 +08002027 return;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002028 }
2029
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002030 /* readahead nat pages to be scanned */
Chao Yu26879fb2015-10-12 17:05:59 +08002031 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
2032 META_NAT, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002033
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002034 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002035
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002036 while (1) {
Yunlei He66e83362017-11-17 16:13:38 +08002037 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2038 nm_i->nat_block_bitmap)) {
2039 struct page *page = get_current_nat_page(sbi, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002040
Yunlei He66e83362017-11-17 16:13:38 +08002041 scan_nat_page(sbi, page, nid);
2042 f2fs_put_page(page, 1);
2043 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002044
2045 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08002046 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002047 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002048
Chao Yua6d494b2015-07-24 18:26:26 +08002049 if (++i >= FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002050 break;
2051 }
2052
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002053 /* go to the next free nat pages to find free nids abundantly */
2054 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002055
2056 /* find free nids from current sum_pages */
Chao Yu2fbaa252017-11-08 17:47:36 +08002057 scan_curseg_cache(sbi);
Chao Yudfc08a12016-02-14 18:50:40 +08002058
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002059 up_read(&nm_i->nat_tree_lock);
Chao Yu2db23882015-10-12 17:07:33 +08002060
2061 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
Chao Yuea1a29a02015-10-12 17:08:48 +08002062 nm_i->ra_nid_pages, META_NAT, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002063}
2064
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002065void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
Chao Yu2411cf52016-10-11 22:31:34 +08002066{
2067 mutex_lock(&NM_I(sbi)->build_lock);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002068 __build_free_nids(sbi, sync, mount);
Chao Yu2411cf52016-10-11 22:31:34 +08002069 mutex_unlock(&NM_I(sbi)->build_lock);
2070}
2071
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002072/*
2073 * If this function returns success, caller can obtain a new nid
2074 * from second parameter of this function.
2075 * The returned nid could be used ino as well as nid when inode is created.
2076 */
2077bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
2078{
2079 struct f2fs_nm_info *nm_i = NM_I(sbi);
2080 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002081retry:
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002082#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu55523512017-02-25 11:08:28 +08002083 if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
2084 f2fs_show_injection_info(FAULT_ALLOC_NID);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002085 return false;
Chao Yu55523512017-02-25 11:08:28 +08002086 }
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002087#endif
Chao Yub8559dc2016-10-12 19:28:29 +08002088 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002089
Chao Yu04d47e62016-11-17 20:53:11 +08002090 if (unlikely(nm_i->available_nids == 0)) {
2091 spin_unlock(&nm_i->nid_list_lock);
2092 return false;
2093 }
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002094
2095 /* We should not use stale free nids created by build_free_nids */
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002096 if (nm_i->nid_cnt[FREE_NID] && !on_build_free_nids(nm_i)) {
2097 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2098 i = list_first_entry(&nm_i->free_nid_list,
Chao Yub8559dc2016-10-12 19:28:29 +08002099 struct free_nid, list);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002100 *nid = i->nid;
Chao Yub8559dc2016-10-12 19:28:29 +08002101
Fan Lia0761f62017-10-28 19:03:37 +08002102 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
Chao Yu04d47e62016-11-17 20:53:11 +08002103 nm_i->available_nids--;
Chao Yu4ac91242017-02-23 10:53:49 +08002104
Chao Yu346fe752017-03-13 20:10:41 +08002105 update_free_nid_bitmap(sbi, *nid, false, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002106
Chao Yub8559dc2016-10-12 19:28:29 +08002107 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002108 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002109 }
Chao Yub8559dc2016-10-12 19:28:29 +08002110 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002111
2112 /* Let's scan nat pages and its caches to get free nids */
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002113 build_free_nids(sbi, true, false);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002114 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002115}
2116
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002117/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002118 * alloc_nid() should be called prior to this function.
2119 */
2120void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
2121{
2122 struct f2fs_nm_info *nm_i = NM_I(sbi);
2123 struct free_nid *i;
2124
Chao Yub8559dc2016-10-12 19:28:29 +08002125 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002126 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002127 f2fs_bug_on(sbi, !i);
Fan Lia0761f62017-10-28 19:03:37 +08002128 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08002129 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002130
2131 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002132}
2133
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002134/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002135 * alloc_nid() should be called prior to this function.
2136 */
2137void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
2138{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002139 struct f2fs_nm_info *nm_i = NM_I(sbi);
2140 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002141 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002142
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002143 if (!nid)
2144 return;
2145
Chao Yub8559dc2016-10-12 19:28:29 +08002146 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002147 i = __lookup_free_nid_list(nm_i, nid);
Chao Yub8559dc2016-10-12 19:28:29 +08002148 f2fs_bug_on(sbi, !i);
2149
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002150 if (!available_free_memory(sbi, FREE_NIDS)) {
Fan Lia0761f62017-10-28 19:03:37 +08002151 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002152 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08002153 } else {
Fan Lia0761f62017-10-28 19:03:37 +08002154 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
Haicheng Li95630cb2013-05-06 23:15:41 +08002155 }
Chao Yu04d47e62016-11-17 20:53:11 +08002156
2157 nm_i->available_nids++;
2158
Chao Yu346fe752017-03-13 20:10:41 +08002159 update_free_nid_bitmap(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002160
Chao Yub8559dc2016-10-12 19:28:29 +08002161 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002162
2163 if (need_free)
2164 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002165}
2166
Chao Yu31696582015-07-28 18:33:46 +08002167int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
2168{
2169 struct f2fs_nm_info *nm_i = NM_I(sbi);
2170 struct free_nid *i, *next;
2171 int nr = nr_shrink;
2172
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002173 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Jaegeuk Kimad4edb82016-06-16 16:41:49 -07002174 return 0;
2175
Chao Yu31696582015-07-28 18:33:46 +08002176 if (!mutex_trylock(&nm_i->build_lock))
2177 return 0;
2178
Chao Yub8559dc2016-10-12 19:28:29 +08002179 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002180 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
Chao Yub8559dc2016-10-12 19:28:29 +08002181 if (nr_shrink <= 0 ||
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002182 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Chao Yu31696582015-07-28 18:33:46 +08002183 break;
Chao Yub8559dc2016-10-12 19:28:29 +08002184
Fan Lia0761f62017-10-28 19:03:37 +08002185 __remove_free_nid(sbi, i, FREE_NID);
Chao Yu31696582015-07-28 18:33:46 +08002186 kmem_cache_free(free_nid_slab, i);
2187 nr_shrink--;
Chao Yu31696582015-07-28 18:33:46 +08002188 }
Chao Yub8559dc2016-10-12 19:28:29 +08002189 spin_unlock(&nm_i->nid_list_lock);
Chao Yu31696582015-07-28 18:33:46 +08002190 mutex_unlock(&nm_i->build_lock);
2191
2192 return nr - nr_shrink;
2193}
2194
Chao Yu70cfed82014-08-02 15:26:04 +08002195void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08002196{
Chao Yu28cdce02014-03-11 13:37:38 +08002197 void *src_addr, *dst_addr;
2198 size_t inline_size;
2199 struct page *ipage;
2200 struct f2fs_inode *ri;
2201
Jaegeuk Kim40813632014-09-02 15:31:18 -07002202 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002203 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08002204
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002205 ri = F2FS_INODE(page);
2206 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002207 clear_inode_flag(inode, FI_INLINE_XATTR);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002208 goto update_inode;
2209 }
2210
Chao Yu6afc6622017-09-06 21:59:50 +08002211 dst_addr = inline_xattr_addr(inode, ipage);
2212 src_addr = inline_xattr_addr(inode, page);
Chao Yu28cdce02014-03-11 13:37:38 +08002213 inline_size = inline_xattr_size(inode);
2214
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002215 f2fs_wait_on_page_writeback(ipage, NODE, true);
Chao Yu28cdce02014-03-11 13:37:38 +08002216 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002217update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08002218 update_inode(inode, ipage);
2219 f2fs_put_page(ipage, 1);
2220}
2221
Chao Yud2600812017-02-08 17:39:45 +08002222int recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002223{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002224 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002225 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
Yunlei He87905682017-07-18 09:48:12 +08002226 nid_t new_xnid;
2227 struct dnode_of_data dn;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002228 struct node_info ni;
Chao Yud2600812017-02-08 17:39:45 +08002229 struct page *xpage;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002230
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002231 if (!prev_xnid)
2232 goto recover_xnid;
2233
Chao Yud2600812017-02-08 17:39:45 +08002234 /* 1: invalidate the previous xattr nid */
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002235 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002236 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002237 invalidate_blocks(sbi, ni.blk_addr);
Chao Yu000519f2017-07-06 01:11:31 +08002238 dec_valid_node_count(sbi, inode, false);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002239 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002240
2241recover_xnid:
Chao Yud2600812017-02-08 17:39:45 +08002242 /* 2: update xattr nid in inode */
Yunlei He87905682017-07-18 09:48:12 +08002243 if (!alloc_nid(sbi, &new_xnid))
2244 return -ENOSPC;
2245
2246 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
2247 xpage = new_node_page(&dn, XATTR_NODE_OFFSET);
2248 if (IS_ERR(xpage)) {
2249 alloc_nid_failed(sbi, new_xnid);
2250 return PTR_ERR(xpage);
2251 }
2252
2253 alloc_nid_done(sbi, new_xnid);
Chao Yud2600812017-02-08 17:39:45 +08002254 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002255
Chao Yud2600812017-02-08 17:39:45 +08002256 /* 3: update and set xattr node page dirty */
Yunlei He87905682017-07-18 09:48:12 +08002257 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
Chao Yud2600812017-02-08 17:39:45 +08002258
Chao Yud2600812017-02-08 17:39:45 +08002259 set_page_dirty(xpage);
2260 f2fs_put_page(xpage, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002261
Chao Yud2600812017-02-08 17:39:45 +08002262 return 0;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002263}
2264
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002265int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
2266{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002267 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002268 nid_t ino = ino_of_node(page);
2269 struct node_info old_ni, new_ni;
2270 struct page *ipage;
2271
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09002272 get_node_info(sbi, ino, &old_ni);
2273
2274 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2275 return -EINVAL;
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002276retry:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07002277 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002278 if (!ipage) {
2279 congestion_wait(BLK_RW_ASYNC, HZ/50);
2280 goto retry;
2281 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002282
arter97e1c42042014-08-06 23:22:50 +09002283 /* Should not use this inode from free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08002284 remove_free_nid(sbi, ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002285
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002286 if (!PageUptodate(ipage))
2287 SetPageUptodate(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002288 fill_node_footer(ipage, ino, ino, 0, true);
2289
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002290 src = F2FS_INODE(page);
2291 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002292
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002293 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
2294 dst->i_size = 0;
2295 dst->i_blocks = cpu_to_le64(1);
2296 dst->i_links = cpu_to_le32(1);
2297 dst->i_xattr_nid = 0;
Chao Yu7a2af762017-07-19 00:19:06 +08002298 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
Chao Yu5c571322017-07-26 00:01:41 +08002299 if (dst->i_inline & F2FS_EXTRA_ATTR) {
Chao Yu7a2af762017-07-19 00:19:06 +08002300 dst->i_extra_isize = src->i_extra_isize;
Chao Yu6afc6622017-09-06 21:59:50 +08002301
2302 if (f2fs_sb_has_flexible_inline_xattr(sbi->sb) &&
2303 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2304 i_inline_xattr_size))
2305 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2306
Chao Yu5c571322017-07-26 00:01:41 +08002307 if (f2fs_sb_has_project_quota(sbi->sb) &&
2308 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2309 i_projid))
2310 dst->i_projid = src->i_projid;
2311 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002312
2313 new_ni = old_ni;
2314 new_ni.ino = ino;
2315
Chao Yu0abd6752017-07-09 00:13:07 +08002316 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09002317 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002318 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002319 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07002320 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002321 f2fs_put_page(ipage, 1);
2322 return 0;
2323}
2324
2325int restore_node_summary(struct f2fs_sb_info *sbi,
2326 unsigned int segno, struct f2fs_summary_block *sum)
2327{
2328 struct f2fs_node *rn;
2329 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002330 block_t addr;
Chao Yu9ecf4b82014-12-18 18:29:05 +08002331 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002332
2333 /* scan the node segment */
2334 last_offset = sbi->blocks_per_seg;
2335 addr = START_BLOCK(sbi, segno);
2336 sum_entry = &sum->entries[0];
2337
Chao Yu9ecf4b82014-12-18 18:29:05 +08002338 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Jaegeuk Kim664ba972016-10-18 11:07:45 -07002339 nrpages = min(last_offset - i, BIO_MAX_PAGES);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002340
arter97e1c42042014-08-06 23:22:50 +09002341 /* readahead node pages */
Chao Yu26879fb2015-10-12 17:05:59 +08002342 ra_meta_pages(sbi, addr, nrpages, META_POR, true);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002343
Chao Yu9ecf4b82014-12-18 18:29:05 +08002344 for (idx = addr; idx < addr + nrpages; idx++) {
Chao Yu2b947002015-10-12 17:04:21 +08002345 struct page *page = get_tmp_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08002346
Chao Yu9ecf4b82014-12-18 18:29:05 +08002347 rn = F2FS_NODE(page);
2348 sum_entry->nid = rn->footer.nid;
2349 sum_entry->version = 0;
2350 sum_entry->ofs_in_node = 0;
2351 sum_entry++;
2352 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08002353 }
Chao Yubac4eef2014-05-27 08:41:07 +08002354
Chao Yu9ecf4b82014-12-18 18:29:05 +08002355 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08002356 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002357 }
Chao Yu9ecf4b82014-12-18 18:29:05 +08002358 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002359}
2360
Chao Yuaec71382014-06-24 09:18:20 +08002361static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002362{
2363 struct f2fs_nm_info *nm_i = NM_I(sbi);
2364 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002365 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002366 int i;
2367
Chao Yub7ad7512016-02-19 18:08:46 +08002368 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002369 for (i = 0; i < nats_in_cursum(journal); i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002370 struct nat_entry *ne;
2371 struct f2fs_nat_entry raw_ne;
Chao Yudfc08a12016-02-14 18:50:40 +08002372 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002373
Chao Yudfc08a12016-02-14 18:50:40 +08002374 raw_ne = nat_in_journal(journal, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08002375
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002376 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002377 if (!ne) {
Yunlei He12f9ef32017-11-10 13:36:51 -08002378 ne = __alloc_nat_entry(nid, true);
2379 __init_nat_entry(nm_i, ne, &raw_ne, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002380 }
Chao Yu04d47e62016-11-17 20:53:11 +08002381
2382 /*
2383 * if a free nat in journal has not been used after last
2384 * checkpoint, we should remove it from available nids,
2385 * since later we will add it again.
2386 */
2387 if (!get_nat_flag(ne, IS_DIRTY) &&
2388 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2389 spin_lock(&nm_i->nid_list_lock);
2390 nm_i->available_nids--;
2391 spin_unlock(&nm_i->nid_list_lock);
2392 }
2393
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002394 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002395 }
Chao Yudfc08a12016-02-14 18:50:40 +08002396 update_nats_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002397 up_write(&curseg->journal_rwsem);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002398}
2399
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002400static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2401 struct list_head *head, int max)
2402{
2403 struct nat_entry_set *cur;
2404
2405 if (nes->entry_cnt >= max)
2406 goto add_out;
2407
2408 list_for_each_entry(cur, head, set_list) {
2409 if (cur->entry_cnt >= nes->entry_cnt) {
2410 list_add(&nes->set_list, cur->set_list.prev);
2411 return;
2412 }
2413 }
2414add_out:
2415 list_add_tail(&nes->set_list, head);
2416}
2417
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002418static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002419 struct page *page)
2420{
2421 struct f2fs_nm_info *nm_i = NM_I(sbi);
2422 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2423 struct f2fs_nat_block *nat_blk = page_address(page);
2424 int valid = 0;
Fan Li37a0ab22017-10-30 15:19:48 +08002425 int i = 0;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002426
2427 if (!enabled_nat_bits(sbi, NULL))
2428 return;
2429
Fan Li37a0ab22017-10-30 15:19:48 +08002430 if (nat_index == 0) {
2431 valid = 1;
2432 i = 1;
2433 }
2434 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
2435 if (nat_blk->entries[i].block_addr != NULL_ADDR)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002436 valid++;
2437 }
2438 if (valid == 0) {
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002439 __set_bit_le(nat_index, nm_i->empty_nat_bits);
2440 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002441 return;
2442 }
2443
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002444 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002445 if (valid == NAT_ENTRY_PER_BLOCK)
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002446 __set_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002447 else
Jaegeuk Kim23380b82017-03-07 14:11:06 -08002448 __clear_bit_le(nat_index, nm_i->full_nat_bits);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002449}
2450
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002451static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002452 struct nat_entry_set *set, struct cp_control *cpc)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002453{
2454 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002455 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002456 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
2457 bool to_journal = true;
2458 struct f2fs_nat_block *nat_blk;
2459 struct nat_entry *ne, *cur;
2460 struct page *page = NULL;
2461
2462 /*
2463 * there are two steps to flush nat entries:
2464 * #1, flush nat entries to journal in current hot data summary block.
2465 * #2, flush nat entries to nat page.
2466 */
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002467 if (enabled_nat_bits(sbi, cpc) ||
2468 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002469 to_journal = false;
2470
2471 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002472 down_write(&curseg->journal_rwsem);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002473 } else {
2474 page = get_next_nat_page(sbi, start_nid);
2475 nat_blk = page_address(page);
2476 f2fs_bug_on(sbi, !nat_blk);
2477 }
2478
2479 /* flush dirty nats in nat entry set */
2480 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
2481 struct f2fs_nat_entry *raw_ne;
2482 nid_t nid = nat_get_nid(ne);
2483 int offset;
2484
Chao Yufebeca62017-06-05 18:29:08 +08002485 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002486
2487 if (to_journal) {
Chao Yudfc08a12016-02-14 18:50:40 +08002488 offset = lookup_journal_in_cursum(journal,
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002489 NAT_JOURNAL, nid, 1);
2490 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002491 raw_ne = &nat_in_journal(journal, offset);
2492 nid_in_journal(journal, offset) = cpu_to_le32(nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002493 } else {
2494 raw_ne = &nat_blk->entries[nid - start_nid];
2495 }
2496 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002497 nat_reset_flag(ne);
Kinglong Mee0b28b712017-02-28 21:34:47 +08002498 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
Chao Yu04d47e62016-11-17 20:53:11 +08002499 if (nat_get_blkaddr(ne) == NULL_ADDR) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002500 add_free_nid(sbi, nid, false);
Chao Yu04d47e62016-11-17 20:53:11 +08002501 spin_lock(&NM_I(sbi)->nid_list_lock);
2502 NM_I(sbi)->available_nids++;
Chao Yu346fe752017-03-13 20:10:41 +08002503 update_free_nid_bitmap(sbi, nid, true, false);
Chao Yu4ac91242017-02-23 10:53:49 +08002504 spin_unlock(&NM_I(sbi)->nid_list_lock);
2505 } else {
2506 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu346fe752017-03-13 20:10:41 +08002507 update_free_nid_bitmap(sbi, nid, false, false);
Chao Yu04d47e62016-11-17 20:53:11 +08002508 spin_unlock(&NM_I(sbi)->nid_list_lock);
2509 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002510 }
2511
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002512 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002513 up_write(&curseg->journal_rwsem);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002514 } else {
2515 __update_nat_bits(sbi, start_nid, page);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002516 f2fs_put_page(page, 1);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002517 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002518
Yunlei He59c90812017-03-13 20:22:18 +08002519 /* Allow dirty nats by node block allocation in write_begin */
2520 if (!set->entry_cnt) {
2521 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
2522 kmem_cache_free(nat_entry_set_slab, set);
2523 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002524}
2525
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002526/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002527 * This function is called during the checkpointing process.
2528 */
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002529void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002530{
2531 struct f2fs_nm_info *nm_i = NM_I(sbi);
2532 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002533 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002534 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002535 struct nat_entry_set *set, *tmp;
2536 unsigned int found;
2537 nid_t set_idx = 0;
2538 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002539
Changman Lee20d047c2014-11-25 12:44:23 +09002540 if (!nm_i->dirty_nat_cnt)
2541 return;
Jaegeuk Kima5131192016-01-02 09:19:41 -08002542
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002543 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002544
Chao Yuaec71382014-06-24 09:18:20 +08002545 /*
2546 * if there are no enough space in journal to store dirty nat
2547 * entries, remove all entries from journal and merge them
2548 * into nat entry set.
2549 */
Jaegeuk Kim900f7362017-02-27 21:28:53 -08002550 if (enabled_nat_bits(sbi, cpc) ||
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002551 !__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08002552 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002553
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002554 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002555 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002556 unsigned idx;
2557 set_idx = setvec[found - 1]->set + 1;
2558 for (idx = 0; idx < found; idx++)
2559 __adjust_nat_entry_set(setvec[idx], &sets,
Chao Yudfc08a12016-02-14 18:50:40 +08002560 MAX_NAT_JENTRIES(journal));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002561 }
Chao Yuaec71382014-06-24 09:18:20 +08002562
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002563 /* flush dirty nats in nat entry set */
2564 list_for_each_entry_safe(set, tmp, &sets, set_list)
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002565 __flush_nat_entry_set(sbi, set, cpc);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002566
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002567 up_write(&nm_i->nat_tree_lock);
Yunlei He59c90812017-03-13 20:22:18 +08002568 /* Allow dirty nats by node block allocation in write_begin */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002569}
2570
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002571static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
2572{
2573 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2574 struct f2fs_nm_info *nm_i = NM_I(sbi);
2575 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
2576 unsigned int i;
2577 __u64 cp_ver = cur_cp_version(ckpt);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002578 block_t nat_bits_addr;
2579
2580 if (!enabled_nat_bits(sbi, NULL))
2581 return 0;
2582
2583 nm_i->nat_bits_blocks = F2FS_BYTES_TO_BLK((nat_bits_bytes << 1) + 8 +
2584 F2FS_BLKSIZE - 1);
2585 nm_i->nat_bits = kzalloc(nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS,
2586 GFP_KERNEL);
2587 if (!nm_i->nat_bits)
2588 return -ENOMEM;
2589
2590 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
2591 nm_i->nat_bits_blocks;
2592 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
2593 struct page *page = get_meta_page(sbi, nat_bits_addr++);
2594
2595 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
2596 page_address(page), F2FS_BLKSIZE);
2597 f2fs_put_page(page, 1);
2598 }
2599
Kinglong Meeced2c7e2017-02-25 19:53:39 +08002600 cp_ver |= (cur_cp_crc(ckpt) << 32);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002601 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
2602 disable_nat_bits(sbi, true);
2603 return 0;
2604 }
2605
2606 nm_i->full_nat_bits = nm_i->nat_bits + 8;
2607 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
2608
2609 f2fs_msg(sbi->sb, KERN_NOTICE, "Found nat_bits in checkpoint");
2610 return 0;
2611}
2612
Hou Pengyangbd80a4b2017-05-17 02:48:48 +00002613static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
Chao Yu7041d5d2017-03-08 20:07:49 +08002614{
2615 struct f2fs_nm_info *nm_i = NM_I(sbi);
2616 unsigned int i = 0;
2617 nid_t nid, last_nid;
2618
2619 if (!enabled_nat_bits(sbi, NULL))
2620 return;
2621
2622 for (i = 0; i < nm_i->nat_blocks; i++) {
2623 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
2624 if (i >= nm_i->nat_blocks)
2625 break;
2626
2627 __set_bit_le(i, nm_i->nat_block_bitmap);
2628
2629 nid = i * NAT_ENTRY_PER_BLOCK;
Fan Lif6986ed2017-11-02 11:02:52 +08002630 last_nid = nid + NAT_ENTRY_PER_BLOCK;
Chao Yu7041d5d2017-03-08 20:07:49 +08002631
Chao Yu346fe752017-03-13 20:10:41 +08002632 spin_lock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08002633 for (; nid < last_nid; nid++)
Chao Yu346fe752017-03-13 20:10:41 +08002634 update_free_nid_bitmap(sbi, nid, true, true);
2635 spin_unlock(&NM_I(sbi)->nid_list_lock);
Chao Yu7041d5d2017-03-08 20:07:49 +08002636 }
2637
2638 for (i = 0; i < nm_i->nat_blocks; i++) {
2639 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
2640 if (i >= nm_i->nat_blocks)
2641 break;
2642
2643 __set_bit_le(i, nm_i->nat_block_bitmap);
2644 }
2645}
2646
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002647static int init_node_manager(struct f2fs_sb_info *sbi)
2648{
2649 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
2650 struct f2fs_nm_info *nm_i = NM_I(sbi);
2651 unsigned char *version_bitmap;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002652 unsigned int nat_segs;
2653 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002654
2655 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
2656
2657 /* segment_count_nat includes pair segment so divide to 2. */
2658 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002659 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
2660 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09002661
Jaegeuk Kimb63da152014-02-17 12:44:20 +09002662 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yu04d47e62016-11-17 20:53:11 +08002663 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
Chao Yu292c196a2017-11-16 16:59:14 +08002664 sbi->nquota_files - F2FS_RESERVED_NODE_NUM;
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002665 nm_i->nid_cnt[FREE_NID] = 0;
2666 nm_i->nid_cnt[PREALLOC_NID] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002667 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09002668 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Chao Yuea1a29a02015-10-12 17:08:48 +08002669 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
Chao Yu2304cb02016-01-18 18:32:58 +08002670 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002671
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002672 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002673 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002674 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2675 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002676 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002677
2678 mutex_init(&nm_i->build_lock);
Chao Yub8559dc2016-10-12 19:28:29 +08002679 spin_lock_init(&nm_i->nid_list_lock);
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002680 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002681
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002682 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002683 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002684 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2685 if (!version_bitmap)
2686 return -EFAULT;
2687
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002688 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2689 GFP_KERNEL);
2690 if (!nm_i->nat_bitmap)
2691 return -ENOMEM;
Chao Yu599a09b2017-01-07 18:52:01 +08002692
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002693 err = __get_nat_bitmaps(sbi);
2694 if (err)
2695 return err;
2696
Chao Yu599a09b2017-01-07 18:52:01 +08002697#ifdef CONFIG_F2FS_CHECK_FS
2698 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
2699 GFP_KERNEL);
2700 if (!nm_i->nat_bitmap_mir)
2701 return -ENOMEM;
2702#endif
2703
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002704 return 0;
2705}
2706
Jaegeuk Kim9f7e4a22017-03-10 09:39:57 -08002707static int init_free_nid_cache(struct f2fs_sb_info *sbi)
Chao Yu4ac91242017-02-23 10:53:49 +08002708{
2709 struct f2fs_nm_info *nm_i = NM_I(sbi);
2710
Michal Hockoa7c3e902017-05-08 15:57:09 -07002711 nm_i->free_nid_bitmap = kvzalloc(nm_i->nat_blocks *
Chao Yu4ac91242017-02-23 10:53:49 +08002712 NAT_ENTRY_BITMAP_SIZE, GFP_KERNEL);
2713 if (!nm_i->free_nid_bitmap)
2714 return -ENOMEM;
2715
Michal Hockoa7c3e902017-05-08 15:57:09 -07002716 nm_i->nat_block_bitmap = kvzalloc(nm_i->nat_blocks / 8,
Chao Yu4ac91242017-02-23 10:53:49 +08002717 GFP_KERNEL);
2718 if (!nm_i->nat_block_bitmap)
2719 return -ENOMEM;
Chao Yu586d1492017-03-01 17:09:07 +08002720
Michal Hockoa7c3e902017-05-08 15:57:09 -07002721 nm_i->free_nid_count = kvzalloc(nm_i->nat_blocks *
Chao Yu586d1492017-03-01 17:09:07 +08002722 sizeof(unsigned short), GFP_KERNEL);
2723 if (!nm_i->free_nid_count)
2724 return -ENOMEM;
Chao Yu4ac91242017-02-23 10:53:49 +08002725 return 0;
2726}
2727
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002728int build_node_manager(struct f2fs_sb_info *sbi)
2729{
2730 int err;
2731
2732 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
2733 if (!sbi->nm_info)
2734 return -ENOMEM;
2735
2736 err = init_node_manager(sbi);
2737 if (err)
2738 return err;
2739
Chao Yu4ac91242017-02-23 10:53:49 +08002740 err = init_free_nid_cache(sbi);
2741 if (err)
2742 return err;
2743
Chao Yu7041d5d2017-03-08 20:07:49 +08002744 /* load free nid status from nat_bits table */
2745 load_free_nid_bitmap(sbi);
2746
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002747 build_free_nids(sbi, true, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002748 return 0;
2749}
2750
2751void destroy_node_manager(struct f2fs_sb_info *sbi)
2752{
2753 struct f2fs_nm_info *nm_i = NM_I(sbi);
2754 struct free_nid *i, *next_i;
2755 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002756 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002757 nid_t nid = 0;
2758 unsigned int found;
2759
2760 if (!nm_i)
2761 return;
2762
2763 /* destroy free nid list */
Chao Yub8559dc2016-10-12 19:28:29 +08002764 spin_lock(&nm_i->nid_list_lock);
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002765 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Fan Lia0761f62017-10-28 19:03:37 +08002766 __remove_free_nid(sbi, i, FREE_NID);
Chao Yub8559dc2016-10-12 19:28:29 +08002767 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002768 kmem_cache_free(free_nid_slab, i);
Chao Yub8559dc2016-10-12 19:28:29 +08002769 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002770 }
Chao Yu9a4ffdf2017-09-29 13:59:35 +08002771 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
2772 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
2773 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
Chao Yub8559dc2016-10-12 19:28:29 +08002774 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002775
2776 /* destroy nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002777 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002778 while ((found = __gang_lookup_nat_cache(nm_i,
2779 nid, NATVEC_SIZE, natvec))) {
2780 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002781
Gu Zhengb6ce3912014-03-07 18:43:24 +08002782 nid = nat_get_nid(natvec[found - 1]) + 1;
2783 for (idx = 0; idx < found; idx++)
2784 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002785 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002786 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002787
2788 /* destroy nat set cache */
2789 nid = 0;
2790 while ((found = __gang_lookup_nat_set(nm_i,
2791 nid, SETVEC_SIZE, setvec))) {
2792 unsigned idx;
2793
2794 nid = setvec[found - 1]->set + 1;
2795 for (idx = 0; idx < found; idx++) {
2796 /* entry_cnt is not zero, when cp_error was occurred */
2797 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2798 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2799 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2800 }
2801 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002802 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002803
Chao Yu4ac91242017-02-23 10:53:49 +08002804 kvfree(nm_i->nat_block_bitmap);
2805 kvfree(nm_i->free_nid_bitmap);
Chao Yu586d1492017-03-01 17:09:07 +08002806 kvfree(nm_i->free_nid_count);
Chao Yu4ac91242017-02-23 10:53:49 +08002807
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002808 kfree(nm_i->nat_bitmap);
Jaegeuk Kim22ad0b62017-02-09 10:38:09 -08002809 kfree(nm_i->nat_bits);
Chao Yu599a09b2017-01-07 18:52:01 +08002810#ifdef CONFIG_F2FS_CHECK_FS
2811 kfree(nm_i->nat_bitmap_mir);
2812#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002813 sbi->nm_info = NULL;
2814 kfree(nm_i);
2815}
2816
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002817int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002818{
2819 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002820 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002821 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002822 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002823
2824 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002825 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002826 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002827 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002828
2829 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2830 sizeof(struct nat_entry_set));
2831 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002832 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002833 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002834
Markus Elfringce3e6d22014-11-24 15:52:00 +01002835destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002836 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002837destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002838 kmem_cache_destroy(nat_entry_slab);
2839fail:
2840 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002841}
2842
2843void destroy_node_manager_caches(void)
2844{
Chao Yuaec71382014-06-24 09:18:20 +08002845 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002846 kmem_cache_destroy(free_nid_slab);
2847 kmem_cache_destroy(nat_entry_slab);
2848}