blob: 93e9d422c99a1aba2a1fc08b81a6b4a2c97da4d8 [file] [log] [blame]
Chao Yu6d1a8322018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003 * fs/f2fs/node.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/mpage.h>
11#include <linux/backing-dev.h>
12#include <linux/blkdev.h>
13#include <linux/pagevec.h>
14#include <linux/swap.h>
15
16#include "f2fs.h"
17#include "node.h"
18#include "segment.h"
Yunlei Hee21d43c2017-07-18 09:48:12 +080019#include "xattr.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080020#include "trace.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090021#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090022
Chao Yu6b4d6a82018-05-30 00:20:41 +080023#define on_f2fs_build_free_nids(nmi) mutex_is_locked(&(nm_i)->build_lock)
Gu Zhengf978f5a2014-02-21 18:08:29 +080024
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090025static struct kmem_cache *nat_entry_slab;
26static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080027static struct kmem_cache *nat_entry_set_slab;
Chao Yu3d5c54a2018-08-02 23:03:19 +080028static struct kmem_cache *fsync_node_entry_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090029
Jaegeuk Kimb733d012018-04-23 23:02:31 -060030/*
31 * Check whether the given nid is within node id range.
32 */
Chao Yu6b4d6a82018-05-30 00:20:41 +080033int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kimb733d012018-04-23 23:02:31 -060034{
35 if (unlikely(nid < F2FS_ROOT_INO(sbi) || nid >= NM_I(sbi)->max_nid)) {
36 set_sbi_flag(sbi, SBI_NEED_FSCK);
37 f2fs_msg(sbi->sb, KERN_WARNING,
38 "%s: out-of-range nid=%x, run fsck to fix.",
39 __func__, nid);
40 return -EINVAL;
41 }
42 return 0;
43}
44
Chao Yu6b4d6a82018-05-30 00:20:41 +080045bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090046{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090047 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090048 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080049 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090050 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090051 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090052
53 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080054
55 /* only uses low memory */
56 avail_ram = val.totalram - val.totalhigh;
57
Chao Yu429511c2015-02-05 17:54:31 +080058 /*
59 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
60 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090061 if (type == FREE_NIDS) {
Chao Yu84af6ae2017-09-29 13:59:35 +080062 mem_size = (nm_i->nid_cnt[FREE_NID] *
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070063 sizeof(struct free_nid)) >> PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080064 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090065 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080066 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030067 PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080068 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kime589c2c2016-06-02 15:24:24 -070069 if (excess_cached_nats(sbi))
70 res = false;
Jaegeuk Kima1257022015-10-08 10:40:07 -070071 } else if (type == DIRTY_DENTS) {
72 if (sbi->sb->s_bdi->wb.dirty_exceeded)
73 return false;
74 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
75 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080076 } else if (type == INO_ENTRIES) {
77 int i;
78
Chao Yuf014be82017-09-29 13:59:38 +080079 for (i = 0; i < MAX_INO_ENTRY; i++)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070080 mem_size += sbi->im[i].ino_num *
81 sizeof(struct ino_entry);
82 mem_size >>= PAGE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080083 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080084 } else if (type == EXTENT_CACHE) {
Jaegeuk Kim7441cce2015-12-21 19:20:15 -080085 mem_size = (atomic_read(&sbi->total_ext_tree) *
86 sizeof(struct extent_tree) +
Chao Yu429511c2015-02-05 17:54:31 +080087 atomic_read(&sbi->total_ext_node) *
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030088 sizeof(struct extent_node)) >> PAGE_SHIFT;
Chao Yu429511c2015-02-05 17:54:31 +080089 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -070090 } else if (type == INMEM_PAGES) {
91 /* it allows 20% / total_ram for inmemory pages */
92 mem_size = get_pages(sbi, F2FS_INMEM_PAGES);
93 res = mem_size < (val.totalram / 5);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080094 } else {
Jaegeuk Kim1663cae2016-01-09 16:14:08 -080095 if (!sbi->sb->s_bdi->wb.dirty_exceeded)
96 return true;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090097 }
98 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090099}
100
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900101static void clear_node_page_dirty(struct page *page)
102{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900103 if (PageDirty(page)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800104 f2fs_clear_radix_tree_dirty_tag(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900105 clear_page_dirty_for_io(page);
Chao Yubcbcda42018-05-26 18:03:35 +0800106 dec_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900107 }
108 ClearPageUptodate(page);
109}
110
111static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
112{
Chao Yu7f78e222018-07-17 20:41:46 +0800113 return f2fs_get_meta_page_nofail(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900114}
115
116static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
117{
118 struct page *src_page;
119 struct page *dst_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900120 pgoff_t dst_off;
121 void *src_addr;
122 void *dst_addr;
123 struct f2fs_nm_info *nm_i = NM_I(sbi);
124
Chao Yu7f78e222018-07-17 20:41:46 +0800125 dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900126
127 /* get current nat block page with lock */
Chao Yu7f78e222018-07-17 20:41:46 +0800128 src_page = get_current_nat_page(sbi, nid);
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -0700129 if (IS_ERR(src_page))
130 return src_page;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800131 dst_page = f2fs_grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700132 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900133
134 src_addr = page_address(src_page);
135 dst_addr = page_address(dst_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300136 memcpy(dst_addr, src_addr, PAGE_SIZE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900137 set_page_dirty(dst_page);
138 f2fs_put_page(src_page, 1);
139
140 set_to_next_nat(nm_i, nid);
141
142 return dst_page;
143}
144
Yunlei He45d6e702017-11-10 13:36:51 -0800145static struct nat_entry *__alloc_nat_entry(nid_t nid, bool no_fail)
146{
147 struct nat_entry *new;
148
149 if (no_fail)
Chao Yucfabb6e2018-01-25 18:57:25 +0800150 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He45d6e702017-11-10 13:36:51 -0800151 else
Chao Yucfabb6e2018-01-25 18:57:25 +0800152 new = kmem_cache_alloc(nat_entry_slab, GFP_F2FS_ZERO);
Yunlei He45d6e702017-11-10 13:36:51 -0800153 if (new) {
154 nat_set_nid(new, nid);
155 nat_reset_flag(new);
156 }
157 return new;
158}
159
160static void __free_nat_entry(struct nat_entry *e)
161{
162 kmem_cache_free(nat_entry_slab, e);
163}
164
165/* must be locked by nat_tree_lock */
166static struct nat_entry *__init_nat_entry(struct f2fs_nm_info *nm_i,
167 struct nat_entry *ne, struct f2fs_nat_entry *raw_ne, bool no_fail)
168{
169 if (no_fail)
170 f2fs_radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne);
171 else if (radix_tree_insert(&nm_i->nat_root, nat_get_nid(ne), ne))
172 return NULL;
173
174 if (raw_ne)
175 node_info_from_raw_nat(&ne->ni, raw_ne);
Chao Yu7309a5f2018-08-05 23:08:59 +0800176
177 spin_lock(&nm_i->nat_list_lock);
Yunlei He45d6e702017-11-10 13:36:51 -0800178 list_add_tail(&ne->list, &nm_i->nat_entries);
Chao Yu7309a5f2018-08-05 23:08:59 +0800179 spin_unlock(&nm_i->nat_list_lock);
180
Yunlei He45d6e702017-11-10 13:36:51 -0800181 nm_i->nat_cnt++;
182 return ne;
183}
184
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900185static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
186{
Chao Yu7309a5f2018-08-05 23:08:59 +0800187 struct nat_entry *ne;
188
189 ne = radix_tree_lookup(&nm_i->nat_root, n);
190
191 /* for recent accessed nat entry, move it to tail of lru list */
192 if (ne && !get_nat_flag(ne, IS_DIRTY)) {
193 spin_lock(&nm_i->nat_list_lock);
194 if (!list_empty(&ne->list))
195 list_move_tail(&ne->list, &nm_i->nat_entries);
196 spin_unlock(&nm_i->nat_list_lock);
197 }
198
199 return ne;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900200}
201
202static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
203 nid_t start, unsigned int nr, struct nat_entry **ep)
204{
205 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
206}
207
208static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
209{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900210 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
211 nm_i->nat_cnt--;
Yunlei He45d6e702017-11-10 13:36:51 -0800212 __free_nat_entry(e);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900213}
214
Chao Yu83b9bb92018-03-20 23:08:30 +0800215static struct nat_entry_set *__grab_nat_entry_set(struct f2fs_nm_info *nm_i,
216 struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700217{
218 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
219 struct nat_entry_set *head;
220
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700221 head = radix_tree_lookup(&nm_i->nat_set_root, set);
222 if (!head) {
Jaegeuk Kim80c54502015-08-20 08:51:56 -0700223 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_NOFS);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700224
225 INIT_LIST_HEAD(&head->entry_list);
226 INIT_LIST_HEAD(&head->set_list);
227 head->set = set;
228 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800229 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700230 }
Chao Yu83b9bb92018-03-20 23:08:30 +0800231 return head;
232}
233
234static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
235 struct nat_entry *ne)
236{
237 struct nat_entry_set *head;
238 bool new_ne = nat_get_blkaddr(ne) == NEW_ADDR;
239
240 if (!new_ne)
241 head = __grab_nat_entry_set(nm_i, ne);
242
243 /*
244 * update entry_cnt in below condition:
245 * 1. update NEW_ADDR to valid block address;
246 * 2. update old block address to new one;
247 */
248 if (!new_ne && (get_nat_flag(ne, IS_PREALLOC) ||
249 !get_nat_flag(ne, IS_DIRTY)))
250 head->entry_cnt++;
251
252 set_nat_flag(ne, IS_PREALLOC, new_ne);
Chao Yu86aadea2017-06-05 18:29:08 +0800253
254 if (get_nat_flag(ne, IS_DIRTY))
255 goto refresh_list;
256
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700257 nm_i->dirty_nat_cnt++;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700258 set_nat_flag(ne, IS_DIRTY, true);
Chao Yu86aadea2017-06-05 18:29:08 +0800259refresh_list:
Chao Yu7309a5f2018-08-05 23:08:59 +0800260 spin_lock(&nm_i->nat_list_lock);
Chao Yu83b9bb92018-03-20 23:08:30 +0800261 if (new_ne)
Chao Yu86aadea2017-06-05 18:29:08 +0800262 list_del_init(&ne->list);
263 else
264 list_move_tail(&ne->list, &head->entry_list);
Chao Yu7309a5f2018-08-05 23:08:59 +0800265 spin_unlock(&nm_i->nat_list_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700266}
267
268static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700269 struct nat_entry_set *set, struct nat_entry *ne)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700270{
Chao Yu7309a5f2018-08-05 23:08:59 +0800271 spin_lock(&nm_i->nat_list_lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700272 list_move_tail(&ne->list, &nm_i->nat_entries);
Chao Yu7309a5f2018-08-05 23:08:59 +0800273 spin_unlock(&nm_i->nat_list_lock);
274
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700275 set_nat_flag(ne, IS_DIRTY, false);
276 set->entry_cnt--;
277 nm_i->dirty_nat_cnt--;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700278}
279
280static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
281 nid_t start, unsigned int nr, struct nat_entry_set **ep)
282{
283 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
284 start, nr);
285}
286
Chao Yu3d5c54a2018-08-02 23:03:19 +0800287bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page)
288{
289 return NODE_MAPPING(sbi) == page->mapping &&
290 IS_DNODE(page) && is_cold_node(page);
291}
292
293void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi)
294{
295 spin_lock_init(&sbi->fsync_node_lock);
296 INIT_LIST_HEAD(&sbi->fsync_node_list);
297 sbi->fsync_seg_id = 0;
298 sbi->fsync_node_num = 0;
299}
300
301static unsigned int f2fs_add_fsync_node_entry(struct f2fs_sb_info *sbi,
302 struct page *page)
303{
304 struct fsync_node_entry *fn;
305 unsigned long flags;
306 unsigned int seq_id;
307
308 fn = f2fs_kmem_cache_alloc(fsync_node_entry_slab, GFP_NOFS);
309
310 get_page(page);
311 fn->page = page;
312 INIT_LIST_HEAD(&fn->list);
313
314 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
315 list_add_tail(&fn->list, &sbi->fsync_node_list);
316 fn->seq_id = sbi->fsync_seg_id++;
317 seq_id = fn->seq_id;
318 sbi->fsync_node_num++;
319 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
320
321 return seq_id;
322}
323
324void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page)
325{
326 struct fsync_node_entry *fn;
327 unsigned long flags;
328
329 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
330 list_for_each_entry(fn, &sbi->fsync_node_list, list) {
331 if (fn->page == page) {
332 list_del(&fn->list);
333 sbi->fsync_node_num--;
334 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
335 kmem_cache_free(fsync_node_entry_slab, fn);
336 put_page(page);
337 return;
338 }
339 }
340 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
341 f2fs_bug_on(sbi, 1);
342}
343
344void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi)
345{
346 unsigned long flags;
347
348 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
349 sbi->fsync_seg_id = 0;
350 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
351}
352
Chao Yu6b4d6a82018-05-30 00:20:41 +0800353int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700354{
355 struct f2fs_nm_info *nm_i = NM_I(sbi);
356 struct nat_entry *e;
357 bool need = false;
358
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700359 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700360 e = __lookup_nat_cache(nm_i, nid);
361 if (e) {
362 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
363 !get_nat_flag(e, HAS_FSYNCED_INODE))
364 need = true;
365 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700366 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700367 return need;
368}
369
Chao Yu6b4d6a82018-05-30 00:20:41 +0800370bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900371{
372 struct f2fs_nm_info *nm_i = NM_I(sbi);
373 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700374 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900375
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700376 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900377 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700378 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700379 is_cp = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700380 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900381 return is_cp;
382}
383
Chao Yu6b4d6a82018-05-30 00:20:41 +0800384bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900385{
386 struct f2fs_nm_info *nm_i = NM_I(sbi);
387 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700388 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900389
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700390 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700391 e = __lookup_nat_cache(nm_i, ino);
392 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
393 (get_nat_flag(e, IS_CHECKPOINTED) ||
394 get_nat_flag(e, HAS_FSYNCED_INODE)))
395 need_update = false;
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700396 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700397 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900398}
399
Yunlei He45d6e702017-11-10 13:36:51 -0800400/* must be locked by nat_tree_lock */
Chao Yu1515aef2016-02-19 18:12:28 +0800401static void cache_nat_entry(struct f2fs_sb_info *sbi, nid_t nid,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900402 struct f2fs_nat_entry *ne)
403{
Chao Yu1515aef2016-02-19 18:12:28 +0800404 struct f2fs_nm_info *nm_i = NM_I(sbi);
Yunlei He45d6e702017-11-10 13:36:51 -0800405 struct nat_entry *new, *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800406
Yunlei He45d6e702017-11-10 13:36:51 -0800407 new = __alloc_nat_entry(nid, false);
408 if (!new)
409 return;
410
411 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900412 e = __lookup_nat_cache(nm_i, nid);
Yunlei He45d6e702017-11-10 13:36:51 -0800413 if (!e)
414 e = __init_nat_entry(nm_i, new, ne, false);
415 else
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700416 f2fs_bug_on(sbi, nat_get_ino(e) != le32_to_cpu(ne->ino) ||
417 nat_get_blkaddr(e) !=
418 le32_to_cpu(ne->block_addr) ||
Chao Yu1515aef2016-02-19 18:12:28 +0800419 nat_get_version(e) != ne->version);
Yunlei He45d6e702017-11-10 13:36:51 -0800420 up_write(&nm_i->nat_tree_lock);
421 if (e != new)
422 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900423}
424
425static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900426 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900427{
428 struct f2fs_nm_info *nm_i = NM_I(sbi);
429 struct nat_entry *e;
Yunlei He45d6e702017-11-10 13:36:51 -0800430 struct nat_entry *new = __alloc_nat_entry(ni->nid, true);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800431
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700432 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900433 e = __lookup_nat_cache(nm_i, ni->nid);
434 if (!e) {
Yunlei He45d6e702017-11-10 13:36:51 -0800435 e = __init_nat_entry(nm_i, new, NULL, true);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800436 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700437 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900438 } else if (new_blkaddr == NEW_ADDR) {
439 /*
440 * when nid is reallocated,
441 * previous nat entry can be remained in nat cache.
442 * So, reinitialize it with new information.
443 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800444 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700445 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900446 }
Yunlei He45d6e702017-11-10 13:36:51 -0800447 /* let's free early to reduce memory consumption */
448 if (e != new)
449 __free_nat_entry(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900450
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900451 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700452 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
453 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900454 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700455 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900456 new_blkaddr == NEW_ADDR);
Chao Yud05e8712018-06-05 17:44:11 +0800457 f2fs_bug_on(sbi, is_valid_data_blkaddr(sbi, nat_get_blkaddr(e)) &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900458 new_blkaddr == NEW_ADDR);
459
arter97e1c42042014-08-06 23:22:50 +0900460 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900461 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
462 unsigned char version = nat_get_version(e);
463 nat_set_version(e, inc_node_version(version));
464 }
465
466 /* change address */
467 nat_set_blkaddr(e, new_blkaddr);
Chao Yud05e8712018-06-05 17:44:11 +0800468 if (!is_valid_data_blkaddr(sbi, new_blkaddr))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700469 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900470 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900471
472 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800473 if (ni->nid != ni->ino)
474 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700475 if (e) {
476 if (fsync_done && ni->nid == ni->ino)
477 set_nat_flag(e, HAS_FSYNCED_INODE, true);
478 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
479 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700480 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900481}
482
Chao Yu6b4d6a82018-05-30 00:20:41 +0800483int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900484{
485 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700486 int nr = nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900487
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700488 if (!down_write_trylock(&nm_i->nat_tree_lock))
489 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900490
Chao Yu7309a5f2018-08-05 23:08:59 +0800491 spin_lock(&nm_i->nat_list_lock);
492 while (nr_shrink) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900493 struct nat_entry *ne;
Chao Yu7309a5f2018-08-05 23:08:59 +0800494
495 if (list_empty(&nm_i->nat_entries))
496 break;
497
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900498 ne = list_first_entry(&nm_i->nat_entries,
499 struct nat_entry, list);
Chao Yu7309a5f2018-08-05 23:08:59 +0800500 list_del(&ne->list);
501 spin_unlock(&nm_i->nat_list_lock);
502
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900503 __del_from_nat_cache(nm_i, ne);
504 nr_shrink--;
Chao Yu7309a5f2018-08-05 23:08:59 +0800505
506 spin_lock(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900507 }
Chao Yu7309a5f2018-08-05 23:08:59 +0800508 spin_unlock(&nm_i->nat_list_lock);
509
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700510 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kim1b38dc82015-06-19 15:36:07 -0700511 return nr - nr_shrink;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900512}
513
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900514/*
arter97e1c42042014-08-06 23:22:50 +0900515 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900516 */
Chao Yu3c743262018-07-17 00:02:17 +0800517int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
Chao Yu6b4d6a82018-05-30 00:20:41 +0800518 struct node_info *ni)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900519{
520 struct f2fs_nm_info *nm_i = NM_I(sbi);
521 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +0800522 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900523 nid_t start_nid = START_NID(nid);
524 struct f2fs_nat_block *nat_blk;
525 struct page *page = NULL;
526 struct f2fs_nat_entry ne;
527 struct nat_entry *e;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700528 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900529 int i;
530
531 ni->nid = nid;
532
533 /* Check nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700534 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900535 e = __lookup_nat_cache(nm_i, nid);
536 if (e) {
537 ni->ino = nat_get_ino(e);
538 ni->blk_addr = nat_get_blkaddr(e);
539 ni->version = nat_get_version(e);
Jaegeuk Kimb873b792016-08-04 11:38:25 -0700540 up_read(&nm_i->nat_tree_lock);
Chao Yu3c743262018-07-17 00:02:17 +0800541 return 0;
Chao Yu1515aef2016-02-19 18:12:28 +0800542 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900543
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800544 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
545
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900546 /* Check current segment summary */
Chao Yub7ad7512016-02-19 18:08:46 +0800547 down_read(&curseg->journal_rwsem);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800548 i = f2fs_lookup_journal_in_cursum(journal, NAT_JOURNAL, nid, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900549 if (i >= 0) {
Chao Yudfc08a12016-02-14 18:50:40 +0800550 ne = nat_in_journal(journal, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900551 node_info_from_raw_nat(ni, &ne);
552 }
Chao Yub7ad7512016-02-19 18:08:46 +0800553 up_read(&curseg->journal_rwsem);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700554 if (i >= 0) {
555 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900556 goto cache;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700557 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900558
559 /* Fill node_info from nat page */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700560 index = current_nat_addr(sbi, nid);
561 up_read(&nm_i->nat_tree_lock);
562
Chao Yu6b4d6a82018-05-30 00:20:41 +0800563 page = f2fs_get_meta_page(sbi, index);
Chao Yu3c743262018-07-17 00:02:17 +0800564 if (IS_ERR(page))
565 return PTR_ERR(page);
566
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900567 nat_blk = (struct f2fs_nat_block *)page_address(page);
568 ne = nat_blk->entries[nid - start_nid];
569 node_info_from_raw_nat(ni, &ne);
570 f2fs_put_page(page, 1);
571cache:
572 /* cache nat entry */
Chao Yu1515aef2016-02-19 18:12:28 +0800573 cache_nat_entry(sbi, nid, &ne);
Chao Yu3c743262018-07-17 00:02:17 +0800574 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900575}
576
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700577/*
578 * readahead MAX_RA_NODE number of node pages.
579 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800580static void f2fs_ra_node_pages(struct page *parent, int start, int n)
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700581{
582 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
583 struct blk_plug plug;
584 int i, end;
585 nid_t nid;
586
587 blk_start_plug(&plug);
588
589 /* Then, try readahead for siblings of the desired node */
590 end = start + n;
591 end = min(end, NIDS_PER_BLOCK);
592 for (i = start; i < end; i++) {
593 nid = get_nid(parent, i, false);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800594 f2fs_ra_node_page(sbi, nid);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700595 }
596
597 blk_finish_plug(&plug);
598}
599
Chao Yu6b4d6a82018-05-30 00:20:41 +0800600pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)
Chao Yu3cf45742016-01-26 15:40:44 +0800601{
602 const long direct_index = ADDRS_PER_INODE(dn->inode);
603 const long direct_blks = ADDRS_PER_BLOCK;
604 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
605 unsigned int skipped_unit = ADDRS_PER_BLOCK;
606 int cur_level = dn->cur_level;
607 int max_level = dn->max_level;
608 pgoff_t base = 0;
609
610 if (!dn->max_level)
611 return pgofs + 1;
612
613 while (max_level-- > cur_level)
614 skipped_unit *= NIDS_PER_BLOCK;
615
616 switch (dn->max_level) {
617 case 3:
618 base += 2 * indirect_blks;
619 case 2:
620 base += 2 * direct_blks;
621 case 1:
622 base += direct_index;
623 break;
624 default:
625 f2fs_bug_on(F2FS_I_SB(dn->inode), 1);
626 }
627
628 return ((pgofs - base) / skipped_unit + 1) * skipped_unit + base;
629}
630
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900631/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900632 * The maximum depth is four.
633 * Offset[0] will have raw inode offset.
634 */
Chao Yu81ca7352016-01-26 15:39:35 +0800635static int get_node_path(struct inode *inode, long block,
Jaegeuk Kimde936532013-08-12 21:08:03 +0900636 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900637{
Chao Yu81ca7352016-01-26 15:39:35 +0800638 const long direct_index = ADDRS_PER_INODE(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900639 const long direct_blks = ADDRS_PER_BLOCK;
640 const long dptrs_per_blk = NIDS_PER_BLOCK;
641 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
642 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
643 int n = 0;
644 int level = 0;
645
646 noffset[0] = 0;
647
648 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900649 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900650 goto got;
651 }
652 block -= direct_index;
653 if (block < direct_blks) {
654 offset[n++] = NODE_DIR1_BLOCK;
655 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900656 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900657 level = 1;
658 goto got;
659 }
660 block -= direct_blks;
661 if (block < direct_blks) {
662 offset[n++] = NODE_DIR2_BLOCK;
663 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900664 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900665 level = 1;
666 goto got;
667 }
668 block -= direct_blks;
669 if (block < indirect_blks) {
670 offset[n++] = NODE_IND1_BLOCK;
671 noffset[n] = 3;
672 offset[n++] = block / direct_blks;
673 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900674 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900675 level = 2;
676 goto got;
677 }
678 block -= indirect_blks;
679 if (block < indirect_blks) {
680 offset[n++] = NODE_IND2_BLOCK;
681 noffset[n] = 4 + dptrs_per_blk;
682 offset[n++] = block / direct_blks;
683 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900684 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900685 level = 2;
686 goto got;
687 }
688 block -= indirect_blks;
689 if (block < dindirect_blks) {
690 offset[n++] = NODE_DIND_BLOCK;
691 noffset[n] = 5 + (dptrs_per_blk * 2);
692 offset[n++] = block / indirect_blks;
693 noffset[n] = 6 + (dptrs_per_blk * 2) +
694 offset[n - 1] * (dptrs_per_blk + 1);
695 offset[n++] = (block / direct_blks) % dptrs_per_blk;
696 noffset[n] = 7 + (dptrs_per_blk * 2) +
697 offset[n - 2] * (dptrs_per_blk + 1) +
698 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900699 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900700 level = 3;
701 goto got;
702 } else {
Jaegeuk Kimd6f5d632017-08-21 13:51:32 -0700703 return -E2BIG;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900704 }
705got:
706 return level;
707}
708
709/*
710 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800711 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
712 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900713 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900714 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800715int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700717 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900718 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800719 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900720 int offset[4];
721 unsigned int noffset[4];
722 nid_t nids[4];
Chao Yu3cf45742016-01-26 15:40:44 +0800723 int level, i = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900724 int err = 0;
725
Chao Yu81ca7352016-01-26 15:39:35 +0800726 level = get_node_path(dn->inode, index, offset, noffset);
Jaegeuk Kimd6f5d632017-08-21 13:51:32 -0700727 if (level < 0)
728 return level;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900729
730 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900731 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900732
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900733 if (!npage[0]) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800734 npage[0] = f2fs_get_node_page(sbi, nids[0]);
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900735 if (IS_ERR(npage[0]))
736 return PTR_ERR(npage[0]);
737 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800738
739 /* if inline_data is set, should not report any block indices */
740 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800741 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800742 f2fs_put_page(npage[0], 1);
743 goto release_out;
744 }
745
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900746 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900747 if (level != 0)
748 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900749 dn->inode_page = npage[0];
750 dn->inode_page_locked = true;
751
752 /* get indirect or direct nodes */
753 for (i = 1; i <= level; i++) {
754 bool done = false;
755
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900756 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900757 /* alloc new node */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800758 if (!f2fs_alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900759 err = -ENOSPC;
760 goto release_pages;
761 }
762
763 dn->nid = nids[i];
Chao Yu6b4d6a82018-05-30 00:20:41 +0800764 npage[i] = f2fs_new_node_page(dn, noffset[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900765 if (IS_ERR(npage[i])) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800766 f2fs_alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900767 err = PTR_ERR(npage[i]);
768 goto release_pages;
769 }
770
771 set_nid(parent, offset[i - 1], nids[i], i == 1);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800772 f2fs_alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900773 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900774 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800775 npage[i] = f2fs_get_node_page_ra(parent, offset[i - 1]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900776 if (IS_ERR(npage[i])) {
777 err = PTR_ERR(npage[i]);
778 goto release_pages;
779 }
780 done = true;
781 }
782 if (i == 1) {
783 dn->inode_page_locked = false;
784 unlock_page(parent);
785 } else {
786 f2fs_put_page(parent, 1);
787 }
788
789 if (!done) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800790 npage[i] = f2fs_get_node_page(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900791 if (IS_ERR(npage[i])) {
792 err = PTR_ERR(npage[i]);
793 f2fs_put_page(npage[0], 0);
794 goto release_out;
795 }
796 }
797 if (i < level) {
798 parent = npage[i];
799 nids[i + 1] = get_nid(parent, offset[i], false);
800 }
801 }
802 dn->nid = nids[level];
803 dn->ofs_in_node = offset[level];
804 dn->node_page = npage[level];
Chao Yufbcf9312017-07-19 00:19:06 +0800805 dn->data_blkaddr = datablock_addr(dn->inode,
806 dn->node_page, dn->ofs_in_node);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900807 return 0;
808
809release_pages:
810 f2fs_put_page(parent, 1);
811 if (i > 1)
812 f2fs_put_page(npage[0], 0);
813release_out:
814 dn->inode_page = NULL;
815 dn->node_page = NULL;
Chao Yu3cf45742016-01-26 15:40:44 +0800816 if (err == -ENOENT) {
817 dn->cur_level = i;
818 dn->max_level = level;
Jaegeuk Kim0a2aa8f2016-07-08 17:42:21 -0700819 dn->ofs_in_node = offset[level];
Chao Yu3cf45742016-01-26 15:40:44 +0800820 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900821 return err;
822}
823
Chao Yu3c743262018-07-17 00:02:17 +0800824static int truncate_node(struct dnode_of_data *dn)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900825{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700826 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900827 struct node_info ni;
Chao Yu3c743262018-07-17 00:02:17 +0800828 int err;
Pan Bian74ec2012018-11-22 18:58:46 +0800829 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900830
Chao Yu3c743262018-07-17 00:02:17 +0800831 err = f2fs_get_node_info(sbi, dn->nid, &ni);
832 if (err)
833 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900834
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900835 /* Deallocate node address */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800836 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yud9bfbbb2017-07-06 01:11:31 +0800837 dec_valid_node_count(sbi, dn->inode, dn->nid == dn->inode->i_ino);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900838 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900839
840 if (dn->nid == dn->inode->i_ino) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800841 f2fs_remove_orphan_inode(sbi, dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900842 dec_valid_inode_count(sbi);
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700843 f2fs_inode_synced(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900844 }
Chao Yud9bfbbb2017-07-06 01:11:31 +0800845
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900846 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800847 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900848
Pan Bian74ec2012018-11-22 18:58:46 +0800849 index = dn->node_page->index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900850 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900851
852 invalidate_mapping_pages(NODE_MAPPING(sbi),
Pan Bian74ec2012018-11-22 18:58:46 +0800853 index, index);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900854
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900855 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900856 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Chao Yu3c743262018-07-17 00:02:17 +0800857
858 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900859}
860
861static int truncate_dnode(struct dnode_of_data *dn)
862{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900863 struct page *page;
Chao Yu3c743262018-07-17 00:02:17 +0800864 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900865
866 if (dn->nid == 0)
867 return 1;
868
869 /* get direct node */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800870 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900871 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
872 return 1;
873 else if (IS_ERR(page))
874 return PTR_ERR(page);
875
876 /* Make dnode_of_data for parameter */
877 dn->node_page = page;
878 dn->ofs_in_node = 0;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800879 f2fs_truncate_data_blocks(dn);
Chao Yu3c743262018-07-17 00:02:17 +0800880 err = truncate_node(dn);
881 if (err)
882 return err;
883
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900884 return 1;
885}
886
887static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
888 int ofs, int depth)
889{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900890 struct dnode_of_data rdn = *dn;
891 struct page *page;
892 struct f2fs_node *rn;
893 nid_t child_nid;
894 unsigned int child_nofs;
895 int freed = 0;
896 int i, ret;
897
898 if (dn->nid == 0)
899 return NIDS_PER_BLOCK + 1;
900
Namjae Jeon51dd6242013-04-20 01:28:52 +0900901 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
902
Chao Yu6b4d6a82018-05-30 00:20:41 +0800903 page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900904 if (IS_ERR(page)) {
905 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900907 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900908
Chao Yu6b4d6a82018-05-30 00:20:41 +0800909 f2fs_ra_node_pages(page, ofs, NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700910
Gu Zheng45590712013-07-15 17:57:38 +0800911 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900912 if (depth < 3) {
913 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
914 child_nid = le32_to_cpu(rn->in.nid[i]);
915 if (child_nid == 0)
916 continue;
917 rdn.nid = child_nid;
918 ret = truncate_dnode(&rdn);
919 if (ret < 0)
920 goto out_err;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800921 if (set_nid(page, i, 0, false))
922 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900923 }
924 } else {
925 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
926 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
927 child_nid = le32_to_cpu(rn->in.nid[i]);
928 if (child_nid == 0) {
929 child_nofs += NIDS_PER_BLOCK + 1;
930 continue;
931 }
932 rdn.nid = child_nid;
933 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
934 if (ret == (NIDS_PER_BLOCK + 1)) {
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800935 if (set_nid(page, i, 0, false))
936 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900937 child_nofs += ret;
938 } else if (ret < 0 && ret != -ENOENT) {
939 goto out_err;
940 }
941 }
942 freed = child_nofs;
943 }
944
945 if (!ofs) {
946 /* remove current indirect node */
947 dn->node_page = page;
Chao Yu3c743262018-07-17 00:02:17 +0800948 ret = truncate_node(dn);
949 if (ret)
950 goto out_err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900951 freed++;
952 } else {
953 f2fs_put_page(page, 1);
954 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900955 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900956 return freed;
957
958out_err:
959 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900960 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900961 return ret;
962}
963
964static int truncate_partial_nodes(struct dnode_of_data *dn,
965 struct f2fs_inode *ri, int *offset, int depth)
966{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900967 struct page *pages[2];
968 nid_t nid[3];
969 nid_t child_nid;
970 int err = 0;
971 int i;
972 int idx = depth - 2;
973
974 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
975 if (!nid[0])
976 return 0;
977
978 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800979 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900980 /* reference count'll be increased */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800981 pages[i] = f2fs_get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900982 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900983 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800984 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900985 goto fail;
986 }
987 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
988 }
989
Chao Yu6b4d6a82018-05-30 00:20:41 +0800990 f2fs_ra_node_pages(pages[idx], offset[idx + 1], NIDS_PER_BLOCK);
Jaegeuk Kim79344ef2016-05-06 16:19:43 -0700991
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900992 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800993 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900994 child_nid = get_nid(pages[idx], i, false);
995 if (!child_nid)
996 continue;
997 dn->nid = child_nid;
998 err = truncate_dnode(dn);
999 if (err < 0)
1000 goto fail;
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001001 if (set_nid(pages[idx], i, 0, false))
1002 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001003 }
1004
shifei10.gea225dca2013-10-29 15:32:34 +08001005 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001006 dn->node_page = pages[idx];
1007 dn->nid = nid[idx];
Chao Yu3c743262018-07-17 00:02:17 +08001008 err = truncate_node(dn);
1009 if (err)
1010 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001011 } else {
1012 f2fs_put_page(pages[idx], 1);
1013 }
1014 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +08001015 offset[idx + 1] = 0;
1016 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017fail:
shifei10.gea225dca2013-10-29 15:32:34 +08001018 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001019 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001020
1021 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
1022
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001023 return err;
1024}
1025
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001026/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001027 * All the block addresses of data and nodes should be nullified.
1028 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001029int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001030{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001031 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001032 int err = 0, cont = 1;
1033 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +09001034 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001035 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001036 struct dnode_of_data dn;
1037 struct page *page;
1038
Namjae Jeon51dd6242013-04-20 01:28:52 +09001039 trace_f2fs_truncate_inode_blocks_enter(inode, from);
1040
Chao Yu81ca7352016-01-26 15:39:35 +08001041 level = get_node_path(inode, from, offset, noffset);
Jaegeuk Kimd6f5d632017-08-21 13:51:32 -07001042 if (level < 0)
1043 return level;
Jaegeuk Kimff373552016-03-29 16:13:45 -07001044
Chao Yu6b4d6a82018-05-30 00:20:41 +08001045 page = f2fs_get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001046 if (IS_ERR(page)) {
1047 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001048 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001049 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001050
1051 set_new_dnode(&dn, inode, page, NULL, 0);
1052 unlock_page(page);
1053
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001054 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001055 switch (level) {
1056 case 0:
1057 case 1:
1058 nofs = noffset[1];
1059 break;
1060 case 2:
1061 nofs = noffset[1];
1062 if (!offset[level - 1])
1063 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001064 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001065 if (err < 0 && err != -ENOENT)
1066 goto fail;
1067 nofs += 1 + NIDS_PER_BLOCK;
1068 break;
1069 case 3:
1070 nofs = 5 + 2 * NIDS_PER_BLOCK;
1071 if (!offset[level - 1])
1072 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001073 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001074 if (err < 0 && err != -ENOENT)
1075 goto fail;
1076 break;
1077 default:
1078 BUG();
1079 }
1080
1081skip_partial:
1082 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001083 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001084 switch (offset[0]) {
1085 case NODE_DIR1_BLOCK:
1086 case NODE_DIR2_BLOCK:
1087 err = truncate_dnode(&dn);
1088 break;
1089
1090 case NODE_IND1_BLOCK:
1091 case NODE_IND2_BLOCK:
1092 err = truncate_nodes(&dn, nofs, offset[1], 2);
1093 break;
1094
1095 case NODE_DIND_BLOCK:
1096 err = truncate_nodes(&dn, nofs, offset[1], 3);
1097 cont = 0;
1098 break;
1099
1100 default:
1101 BUG();
1102 }
1103 if (err < 0 && err != -ENOENT)
1104 goto fail;
1105 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001106 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001107 lock_page(page);
Jaegeuk Kimff373552016-03-29 16:13:45 -07001108 BUG_ON(page->mapping != NODE_MAPPING(sbi));
Chao Yud9b3df52018-12-25 17:43:42 +08001109 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001110 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001111 set_page_dirty(page);
1112 unlock_page(page);
1113 }
1114 offset[1] = 0;
1115 offset[0]++;
1116 nofs += err;
1117 }
1118fail:
1119 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +09001120 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001121 return err > 0 ? 0 : err;
1122}
1123
Jaegeuk Kim41d59232017-10-19 11:48:57 -07001124/* caller must lock inode page */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001125int f2fs_truncate_xattr_node(struct inode *inode)
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001126{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001127 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001128 nid_t nid = F2FS_I(inode)->i_xattr_nid;
1129 struct dnode_of_data dn;
1130 struct page *npage;
Chao Yu3c743262018-07-17 00:02:17 +08001131 int err;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001132
1133 if (!nid)
1134 return 0;
1135
Chao Yu6b4d6a82018-05-30 00:20:41 +08001136 npage = f2fs_get_node_page(sbi, nid);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001137 if (IS_ERR(npage))
1138 return PTR_ERR(npage);
1139
Chao Yu3c743262018-07-17 00:02:17 +08001140 set_new_dnode(&dn, inode, NULL, npage, nid);
1141 err = truncate_node(&dn);
1142 if (err) {
1143 f2fs_put_page(npage, 1);
1144 return err;
1145 }
1146
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001147 f2fs_i_xnid_write(inode, 0);
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001148
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +09001149 return 0;
1150}
1151
Jaegeuk Kim39936832012-11-22 16:21:29 +09001152/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001153 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
1154 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +09001155 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001156int f2fs_remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001157{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001158 struct dnode_of_data dn;
Chao Yu13ec7292015-08-24 17:40:45 +08001159 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001160
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001161 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001162 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
Chao Yu13ec7292015-08-24 17:40:45 +08001163 if (err)
1164 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001165
Chao Yu6b4d6a82018-05-30 00:20:41 +08001166 err = f2fs_truncate_xattr_node(inode);
Chao Yu13ec7292015-08-24 17:40:45 +08001167 if (err) {
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001168 f2fs_put_dnode(&dn);
Chao Yu13ec7292015-08-24 17:40:45 +08001169 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001170 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001171
1172 /* remove potential inline_data blocks */
1173 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1174 S_ISLNK(inode->i_mode))
Chao Yu6b4d6a82018-05-30 00:20:41 +08001175 f2fs_truncate_data_blocks_range(&dn, 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001176
arter97e1c42042014-08-06 23:22:50 +09001177 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim08187682018-07-31 09:09:01 -07001178 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
1179 f2fs_put_dnode(&dn);
1180 return -EIO;
1181 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001182 f2fs_bug_on(F2FS_I_SB(inode),
Chao Yu56a0d932017-06-14 23:00:56 +08001183 inode->i_blocks != 0 && inode->i_blocks != 8);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -07001184
1185 /* will put inode & node pages */
Chao Yu3c743262018-07-17 00:02:17 +08001186 err = truncate_node(&dn);
1187 if (err) {
1188 f2fs_put_dnode(&dn);
1189 return err;
1190 }
Chao Yu13ec7292015-08-24 17:40:45 +08001191 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001192}
1193
Chao Yu6b4d6a82018-05-30 00:20:41 +08001194struct page *f2fs_new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001195{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001196 struct dnode_of_data dn;
1197
1198 /* allocate inode page for new inode */
1199 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +09001200
1201 /* caller should f2fs_put_page(page, 1); */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001202 return f2fs_new_node_page(&dn, 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001203}
1204
Chao Yu6b4d6a82018-05-30 00:20:41 +08001205struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001206{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001207 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001208 struct node_info new_ni;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001209 struct page *page;
1210 int err;
1211
Jaegeuk Kim91942322016-05-20 10:13:22 -07001212 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001213 return ERR_PTR(-EPERM);
1214
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001215 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), dn->nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001216 if (!page)
1217 return ERR_PTR(-ENOMEM);
1218
Chao Yu09c3a722017-07-09 00:13:07 +08001219 if (unlikely((err = inc_valid_node_count(sbi, dn->inode, !ofs))))
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001220 goto fail;
Chao Yu09c3a722017-07-09 00:13:07 +08001221
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001222#ifdef CONFIG_F2FS_CHECK_FS
Chao Yu3c743262018-07-17 00:02:17 +08001223 err = f2fs_get_node_info(sbi, dn->nid, &new_ni);
1224 if (err) {
1225 dec_valid_node_count(sbi, dn->inode, !ofs);
1226 goto fail;
1227 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001228 f2fs_bug_on(sbi, new_ni.blk_addr != NULL_ADDR);
1229#endif
1230 new_ni.nid = dn->nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001231 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001232 new_ni.blk_addr = NULL_ADDR;
1233 new_ni.flag = 0;
1234 new_ni.version = 0;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001235 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001236
Chao Yud9b3df52018-12-25 17:43:42 +08001237 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim9c027402013-08-12 16:00:46 +09001238 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Chao Yuc7930ee82018-03-09 14:24:22 +08001239 set_cold_node(page, S_ISDIR(dn->inode->i_mode));
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001240 if (!PageUptodate(page))
1241 SetPageUptodate(page);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001242 if (set_page_dirty(page))
1243 dn->node_changed = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001244
Chao Yu4bc8e9b2014-03-17 16:35:06 +08001245 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim205b9822016-05-20 09:52:20 -07001246 f2fs_i_xnid_write(dn->inode, dn->nid);
Jaegeuk Kim479bd732013-08-12 16:04:53 +09001247
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001248 if (ofs == 0)
1249 inc_valid_inode_count(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001250 return page;
1251
1252fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +09001253 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001254 f2fs_put_page(page, 1);
1255 return ERR_PTR(err);
1256}
1257
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001258/*
1259 * Caller should do after getting the following values.
1260 * 0: f2fs_put_page(page, 0)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001261 * LOCKED_PAGE or error: f2fs_put_page(page, 1)
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001262 */
Mike Christie04d328d2016-06-05 14:31:55 -05001263static int read_node_page(struct page *page, int op_flags)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001264{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001265 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001266 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001267 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001268 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001269 .type = NODE,
Mike Christie04d328d2016-06-05 14:31:55 -05001270 .op = REQ_OP_READ,
1271 .op_flags = op_flags,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001272 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001273 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001274 };
Chao Yu3c743262018-07-17 00:02:17 +08001275 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001276
Weichao Guof7467672018-03-09 23:10:21 +08001277 if (PageUptodate(page)) {
1278#ifdef CONFIG_F2FS_CHECK_FS
1279 f2fs_bug_on(sbi, !f2fs_inode_chksum_verify(sbi, page));
1280#endif
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001281 return LOCKED_PAGE;
Weichao Guof7467672018-03-09 23:10:21 +08001282 }
Jaegeuk Kim3bdad3c2016-06-30 19:04:16 -07001283
Chao Yu3c743262018-07-17 00:02:17 +08001284 err = f2fs_get_node_info(sbi, page->index, &ni);
1285 if (err)
1286 return err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001287
Jaegeuk Kimc92b7c72018-06-21 13:46:23 -07001288 if (unlikely(ni.blk_addr == NULL_ADDR) ||
1289 is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001290 ClearPageUptodate(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001292 }
1293
Chao Yu7a9d7542016-02-22 18:36:38 +08001294 fio.new_blkaddr = fio.old_blkaddr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001295 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001296}
1297
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001298/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001299 * Readahead a node page
1300 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001301void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001302{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001303 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001304 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001305
Chao Yue84587252016-01-08 20:13:37 +08001306 if (!nid)
1307 return;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001308 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kimb733d012018-04-23 23:02:31 -06001309 return;
Chao Yue84587252016-01-08 20:13:37 +08001310
Fan Li999270d2016-02-29 14:29:51 +08001311 rcu_read_lock();
1312 apage = radix_tree_lookup(&NODE_MAPPING(sbi)->page_tree, nid);
1313 rcu_read_unlock();
1314 if (apage)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001315 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001316
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001317 apage = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001318 if (!apage)
1319 return;
1320
Christoph Hellwig70246282016-07-19 11:28:41 +02001321 err = read_node_page(apage, REQ_RAHEAD);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001322 f2fs_put_page(apage, err ? 1 : 0);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001323}
1324
Jaegeuk Kim17a0ee52016-03-08 09:04:35 -08001325static struct page *__get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid,
Chao Yu0e022ea2016-01-05 16:52:46 +08001326 struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001327{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001328 struct page *page;
1329 int err;
Jaegeuk Kim4aa69d52015-12-23 14:17:47 -08001330
1331 if (!nid)
1332 return ERR_PTR(-ENOENT);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001333 if (f2fs_check_nid_range(sbi, nid))
Jaegeuk Kimb733d012018-04-23 23:02:31 -06001334 return ERR_PTR(-EINVAL);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001335repeat:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07001336 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), nid, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001337 if (!page)
1338 return ERR_PTR(-ENOMEM);
1339
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001340 err = read_node_page(page, 0);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001341 if (err < 0) {
1342 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001343 return ERR_PTR(err);
Chao Yue1c51b9f2015-12-11 16:08:22 +08001344 } else if (err == LOCKED_PAGE) {
Chao Yub8329ee2017-06-07 11:17:35 +08001345 err = 0;
Chao Yue1c51b9f2015-12-11 16:08:22 +08001346 goto page_hit;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001347 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001348
Chao Yu0e022ea2016-01-05 16:52:46 +08001349 if (parent)
Chao Yu6b4d6a82018-05-30 00:20:41 +08001350 f2fs_ra_node_pages(parent, start + 1, MAX_RA_NODE);
Chao Yu0e022ea2016-01-05 16:52:46 +08001351
Chao Yue1c51b9f2015-12-11 16:08:22 +08001352 lock_page(page);
1353
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001354 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001355 f2fs_put_page(page, 1);
1356 goto repeat;
1357 }
Chao Yu1563ac72016-07-03 22:05:12 +08001358
Chao Yub8329ee2017-06-07 11:17:35 +08001359 if (unlikely(!PageUptodate(page))) {
1360 err = -EIO;
Chao Yu1563ac72016-07-03 22:05:12 +08001361 goto out_err;
Chao Yub8329ee2017-06-07 11:17:35 +08001362 }
Chao Yu43101252017-07-31 20:19:09 +08001363
1364 if (!f2fs_inode_chksum_verify(sbi, page)) {
1365 err = -EBADMSG;
1366 goto out_err;
1367 }
Chao Yue1c51b9f2015-12-11 16:08:22 +08001368page_hit:
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001369 if(unlikely(nid != nid_of_node(page))) {
Chao Yub8329ee2017-06-07 11:17:35 +08001370 f2fs_msg(sbi->sb, KERN_WARNING, "inconsistent node block, "
1371 "nid:%lu, node_footer[nid:%u,ino:%u,ofs:%u,cpver:%llu,blkaddr:%u]",
1372 nid, nid_of_node(page), ino_of_node(page),
1373 ofs_of_node(page), cpver_of_node(page),
1374 next_blkaddr_of_node(page));
Chao Yub8329ee2017-06-07 11:17:35 +08001375 err = -EINVAL;
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001376out_err:
Jaegeuk Kimc5497ec2017-08-31 16:54:51 -07001377 ClearPageUptodate(page);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001378 f2fs_put_page(page, 1);
Chao Yub8329ee2017-06-07 11:17:35 +08001379 return ERR_PTR(err);
Yunlong Song0c9df7f2016-05-26 19:40:29 +08001380 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381 return page;
1382}
1383
Chao Yu6b4d6a82018-05-30 00:20:41 +08001384struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
Chao Yu0e022ea2016-01-05 16:52:46 +08001385{
1386 return __get_node_page(sbi, nid, NULL, 0);
1387}
1388
Chao Yu6b4d6a82018-05-30 00:20:41 +08001389struct page *f2fs_get_node_page_ra(struct page *parent, int start)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001390{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001391 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Chao Yu0e022ea2016-01-05 16:52:46 +08001392 nid_t nid = get_nid(parent, start, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001393
Chao Yu0e022ea2016-01-05 16:52:46 +08001394 return __get_node_page(sbi, nid, parent, start);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001395}
1396
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001397static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino)
1398{
1399 struct inode *inode;
1400 struct page *page;
Chao Yu0f3311a2016-05-21 00:11:09 +08001401 int ret;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001402
1403 /* should flush inline_data before evict_inode */
1404 inode = ilookup(sbi->sb, ino);
1405 if (!inode)
1406 return;
1407
Chao Yu58ddec82017-10-28 16:52:30 +08001408 page = f2fs_pagecache_get_page(inode->i_mapping, 0,
1409 FGP_LOCK|FGP_NOWAIT, 0);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001410 if (!page)
1411 goto iput_out;
1412
1413 if (!PageUptodate(page))
1414 goto page_out;
1415
1416 if (!PageDirty(page))
1417 goto page_out;
1418
1419 if (!clear_page_dirty_for_io(page))
1420 goto page_out;
1421
Chao Yu0f3311a2016-05-21 00:11:09 +08001422 ret = f2fs_write_inline_data(inode, page);
1423 inode_dec_dirty_pages(inode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001424 f2fs_remove_dirty_inode(inode);
Chao Yu0f3311a2016-05-21 00:11:09 +08001425 if (ret)
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001426 set_page_dirty(page);
1427page_out:
Jaegeuk Kim4a6de502016-03-30 11:25:31 -07001428 f2fs_put_page(page, 1);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001429iput_out:
1430 iput(inode);
1431}
1432
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001433static struct page *last_fsync_dnode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001434{
Jan Kara45ec63b2017-11-15 17:34:51 -08001435 pgoff_t index;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001436 struct pagevec pvec;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001437 struct page *last_page = NULL;
Jan Kara45ec63b2017-11-15 17:34:51 -08001438 int nr_pages;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001439
1440 pagevec_init(&pvec, 0);
1441 index = 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001442
Jan Kara45ec63b2017-11-15 17:34:51 -08001443 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara4d93a432017-11-15 17:35:19 -08001444 PAGECACHE_TAG_DIRTY))) {
Jan Kara45ec63b2017-11-15 17:34:51 -08001445 int i;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001446
1447 for (i = 0; i < nr_pages; i++) {
1448 struct page *page = pvec.pages[i];
1449
1450 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001451 f2fs_put_page(last_page, 0);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001452 pagevec_release(&pvec);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001453 return ERR_PTR(-EIO);
Jaegeuk Kim52681372016-04-13 16:24:44 -07001454 }
1455
1456 if (!IS_DNODE(page) || !is_cold_node(page))
1457 continue;
1458 if (ino_of_node(page) != ino)
1459 continue;
1460
1461 lock_page(page);
1462
1463 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1464continue_unlock:
1465 unlock_page(page);
1466 continue;
1467 }
1468 if (ino_of_node(page) != ino)
1469 goto continue_unlock;
1470
1471 if (!PageDirty(page)) {
1472 /* someone wrote it for us */
1473 goto continue_unlock;
1474 }
1475
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001476 if (last_page)
1477 f2fs_put_page(last_page, 0);
1478
1479 get_page(page);
1480 last_page = page;
1481 unlock_page(page);
1482 }
1483 pagevec_release(&pvec);
1484 cond_resched();
1485 }
1486 return last_page;
1487}
1488
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001489static int __write_node_page(struct page *page, bool atomic, bool *submitted,
Chao Yuc0fe4882017-08-02 23:21:48 +08001490 struct writeback_control *wbc, bool do_balance,
Chao Yu3d5c54a2018-08-02 23:03:19 +08001491 enum iostat_type io_type, unsigned int *seq_id)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001492{
1493 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
1494 nid_t nid;
1495 struct node_info ni;
1496 struct f2fs_io_info fio = {
1497 .sbi = sbi,
Chao Yuf014be82017-09-29 13:59:38 +08001498 .ino = ino_of_node(page),
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001499 .type = NODE,
1500 .op = REQ_OP_WRITE,
1501 .op_flags = wbc_to_write_flags(wbc),
1502 .page = page,
1503 .encrypted_page = NULL,
1504 .submitted = false,
Chao Yuc0fe4882017-08-02 23:21:48 +08001505 .io_type = io_type,
Yufen Yucbd5e5a2018-01-09 19:33:39 +08001506 .io_wbc = wbc,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001507 };
Chao Yu3d5c54a2018-08-02 23:03:19 +08001508 unsigned int seq;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001509
1510 trace_f2fs_writepage(page, NODE);
1511
Jaegeuk Kim04fa5dc2018-05-04 18:04:22 -07001512 if (unlikely(f2fs_cp_error(sbi)))
1513 goto redirty_out;
Chao Yu86f78c12018-01-18 17:29:10 +08001514
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001515 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1516 goto redirty_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001517
Chao Yu5cf8d7b2018-07-25 19:16:21 +08001518 if (wbc->sync_mode == WB_SYNC_NONE &&
1519 IS_DNODE(page) && is_cold_node(page))
1520 goto redirty_out;
1521
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001522 /* get old block addr of this node page */
1523 nid = nid_of_node(page);
1524 f2fs_bug_on(sbi, page->index != nid);
1525
Chao Yu3c743262018-07-17 00:02:17 +08001526 if (f2fs_get_node_info(sbi, nid, &ni))
1527 goto redirty_out;
1528
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001529 if (wbc->for_reclaim) {
1530 if (!down_read_trylock(&sbi->node_write))
1531 goto redirty_out;
1532 } else {
1533 down_read(&sbi->node_write);
1534 }
1535
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001536 /* This page is already truncated */
1537 if (unlikely(ni.blk_addr == NULL_ADDR)) {
1538 ClearPageUptodate(page);
1539 dec_page_count(sbi, F2FS_DIRTY_NODES);
1540 up_read(&sbi->node_write);
1541 unlock_page(page);
1542 return 0;
1543 }
1544
Chao Yub3c869a2018-08-01 19:13:44 +08001545 if (__is_valid_data_blkaddr(ni.blk_addr) &&
Jaegeuk Kim9cc70322018-09-27 22:15:31 -07001546 !f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC)) {
1547 up_read(&sbi->node_write);
Chao Yub3c869a2018-08-01 19:13:44 +08001548 goto redirty_out;
Jaegeuk Kim9cc70322018-09-27 22:15:31 -07001549 }
Chao Yub3c869a2018-08-01 19:13:44 +08001550
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001551 if (atomic && !test_opt(sbi, NOBARRIER))
1552 fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
1553
1554 set_page_writeback(page);
Jaegeuk Kimf4531472018-04-11 23:09:04 -07001555 ClearPageError(page);
Chao Yu3d5c54a2018-08-02 23:03:19 +08001556
1557 if (f2fs_in_warm_node_list(sbi, page)) {
1558 seq = f2fs_add_fsync_node_entry(sbi, page);
1559 if (seq_id)
1560 *seq_id = seq;
1561 }
1562
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001563 fio.old_blkaddr = ni.blk_addr;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001564 f2fs_do_write_node_page(nid, &fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001565 set_node_addr(sbi, &ni, fio.new_blkaddr, is_fsync_dnode(page));
1566 dec_page_count(sbi, F2FS_DIRTY_NODES);
1567 up_read(&sbi->node_write);
1568
1569 if (wbc->for_reclaim) {
Chao Yu39373df2018-09-27 23:41:16 +08001570 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, NODE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001571 submitted = NULL;
1572 }
1573
1574 unlock_page(page);
1575
1576 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001577 f2fs_submit_merged_write(sbi, NODE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001578 submitted = NULL;
1579 }
1580 if (submitted)
1581 *submitted = fio.submitted;
1582
Yunlong Song0c535c52017-07-27 20:11:00 +08001583 if (do_balance)
1584 f2fs_balance_fs(sbi, false);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001585 return 0;
1586
1587redirty_out:
1588 redirty_page_for_writepage(wbc, page);
1589 return AOP_WRITEPAGE_ACTIVATE;
1590}
1591
Chao Yu44344f42018-09-13 07:40:53 +08001592int f2fs_move_node_page(struct page *node_page, int gc_type)
Yunlei He247e8952017-10-30 14:18:55 +08001593{
Chao Yu44344f42018-09-13 07:40:53 +08001594 int err = 0;
1595
Yunlei He247e8952017-10-30 14:18:55 +08001596 if (gc_type == FG_GC) {
1597 struct writeback_control wbc = {
1598 .sync_mode = WB_SYNC_ALL,
1599 .nr_to_write = 1,
1600 .for_reclaim = 0,
1601 };
1602
Chao Yud9b3df52018-12-25 17:43:42 +08001603 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
Chao Yua8c3e532018-12-12 18:12:30 +08001604
1605 set_page_dirty(node_page);
1606
Chao Yu44344f42018-09-13 07:40:53 +08001607 if (!clear_page_dirty_for_io(node_page)) {
1608 err = -EAGAIN;
Yunlei He247e8952017-10-30 14:18:55 +08001609 goto out_page;
Chao Yu44344f42018-09-13 07:40:53 +08001610 }
Yunlei He247e8952017-10-30 14:18:55 +08001611
1612 if (__write_node_page(node_page, false, NULL,
Chao Yu44344f42018-09-13 07:40:53 +08001613 &wbc, false, FS_GC_NODE_IO, NULL)) {
1614 err = -EAGAIN;
Yunlei He247e8952017-10-30 14:18:55 +08001615 unlock_page(node_page);
Chao Yu44344f42018-09-13 07:40:53 +08001616 }
Yunlei He247e8952017-10-30 14:18:55 +08001617 goto release_page;
1618 } else {
1619 /* set page dirty and write it */
1620 if (!PageWriteback(node_page))
1621 set_page_dirty(node_page);
1622 }
1623out_page:
1624 unlock_page(node_page);
1625release_page:
1626 f2fs_put_page(node_page, 0);
Chao Yu44344f42018-09-13 07:40:53 +08001627 return err;
Yunlei He247e8952017-10-30 14:18:55 +08001628}
1629
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001630static int f2fs_write_node_page(struct page *page,
1631 struct writeback_control *wbc)
1632{
Chao Yu3d5c54a2018-08-02 23:03:19 +08001633 return __write_node_page(page, false, NULL, wbc, false,
1634 FS_NODE_IO, NULL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001635}
1636
Chao Yu6b4d6a82018-05-30 00:20:41 +08001637int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
Chao Yu3d5c54a2018-08-02 23:03:19 +08001638 struct writeback_control *wbc, bool atomic,
1639 unsigned int *seq_id)
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001640{
Jan Kara45ec63b2017-11-15 17:34:51 -08001641 pgoff_t index;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001642 struct pagevec pvec;
1643 int ret = 0;
1644 struct page *last_page = NULL;
1645 bool marked = false;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001646 nid_t ino = inode->i_ino;
Jan Kara45ec63b2017-11-15 17:34:51 -08001647 int nr_pages;
Chao Yu39373df2018-09-27 23:41:16 +08001648 int nwritten = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001649
1650 if (atomic) {
1651 last_page = last_fsync_dnode(sbi, ino);
1652 if (IS_ERR_OR_NULL(last_page))
1653 return PTR_ERR_OR_ZERO(last_page);
1654 }
1655retry:
1656 pagevec_init(&pvec, 0);
1657 index = 0;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001658
Jan Kara45ec63b2017-11-15 17:34:51 -08001659 while ((nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jan Kara4d93a432017-11-15 17:35:19 -08001660 PAGECACHE_TAG_DIRTY))) {
Jan Kara45ec63b2017-11-15 17:34:51 -08001661 int i;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001662
1663 for (i = 0; i < nr_pages; i++) {
1664 struct page *page = pvec.pages[i];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001665 bool submitted = false;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001666
1667 if (unlikely(f2fs_cp_error(sbi))) {
1668 f2fs_put_page(last_page, 0);
1669 pagevec_release(&pvec);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001670 ret = -EIO;
1671 goto out;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001672 }
1673
1674 if (!IS_DNODE(page) || !is_cold_node(page))
1675 continue;
1676 if (ino_of_node(page) != ino)
1677 continue;
1678
1679 lock_page(page);
1680
1681 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
1682continue_unlock:
1683 unlock_page(page);
1684 continue;
1685 }
1686 if (ino_of_node(page) != ino)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001687 goto continue_unlock;
1688
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001689 if (!PageDirty(page) && page != last_page) {
1690 /* someone wrote it for us */
1691 goto continue_unlock;
1692 }
1693
Chao Yud9b3df52018-12-25 17:43:42 +08001694 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001695
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001696 set_fsync_mark(page, 0);
1697 set_dentry_mark(page, 0);
1698
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001699 if (!atomic || page == last_page) {
1700 set_fsync_mark(page, 1);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001701 if (IS_INODE(page)) {
1702 if (is_inode_flag_set(inode,
1703 FI_DIRTY_INODE))
Chao Yu6b4d6a82018-05-30 00:20:41 +08001704 f2fs_update_inode(inode, page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001705 set_dentry_mark(page,
Chao Yu6b4d6a82018-05-30 00:20:41 +08001706 f2fs_need_dentry_mark(sbi, ino));
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001707 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001708 /* may be written by other thread */
1709 if (!PageDirty(page))
1710 set_page_dirty(page);
1711 }
1712
1713 if (!clear_page_dirty_for_io(page))
1714 goto continue_unlock;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001715
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001716 ret = __write_node_page(page, atomic &&
1717 page == last_page,
Chao Yuc0fe4882017-08-02 23:21:48 +08001718 &submitted, wbc, true,
Chao Yu3d5c54a2018-08-02 23:03:19 +08001719 FS_NODE_IO, seq_id);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001720 if (ret) {
Jaegeuk Kim52681372016-04-13 16:24:44 -07001721 unlock_page(page);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001722 f2fs_put_page(last_page, 0);
1723 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001724 } else if (submitted) {
Chao Yu39373df2018-09-27 23:41:16 +08001725 nwritten++;
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001726 }
Chao Yu3f5f4952016-09-29 18:50:10 +08001727
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001728 if (page == last_page) {
1729 f2fs_put_page(page, 0);
1730 marked = true;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001731 break;
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001732 }
Jaegeuk Kim52681372016-04-13 16:24:44 -07001733 }
1734 pagevec_release(&pvec);
1735 cond_resched();
1736
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001737 if (ret || marked)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001738 break;
1739 }
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001740 if (!ret && atomic && !marked) {
1741 f2fs_msg(sbi->sb, KERN_DEBUG,
1742 "Retry to write fsync mark: ino=%u, idx=%lx",
1743 ino, last_page->index);
1744 lock_page(last_page);
Chao Yud9b3df52018-12-25 17:43:42 +08001745 f2fs_wait_on_page_writeback(last_page, NODE, true, true);
Jaegeuk Kim608514d2016-04-15 09:43:17 -07001746 set_page_dirty(last_page);
1747 unlock_page(last_page);
1748 goto retry;
1749 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001750out:
Chao Yu39373df2018-09-27 23:41:16 +08001751 if (nwritten)
1752 f2fs_submit_merged_write_cond(sbi, NULL, NULL, ino, NODE);
Jaegeuk Kimc267ec12016-04-15 09:25:04 -07001753 return ret ? -EIO: 0;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001754}
1755
Chao Yu6b4d6a82018-05-30 00:20:41 +08001756int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
1757 struct writeback_control *wbc,
Chao Yuc0fe4882017-08-02 23:21:48 +08001758 bool do_balance, enum iostat_type io_type)
Jaegeuk Kim52681372016-04-13 16:24:44 -07001759{
Jan Kara45ec63b2017-11-15 17:34:51 -08001760 pgoff_t index;
Jaegeuk Kim52681372016-04-13 16:24:44 -07001761 struct pagevec pvec;
1762 int step = 0;
Jaegeuk Kim12bb0a82016-03-11 15:33:22 -08001763 int nwritten = 0;
Chao Yu3f5f4952016-09-29 18:50:10 +08001764 int ret = 0;
Chao Yudeb78d42018-06-04 23:20:36 +08001765 int nr_pages, done = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001766
1767 pagevec_init(&pvec, 0);
1768
1769next_step:
1770 index = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001771
Chao Yudeb78d42018-06-04 23:20:36 +08001772 while (!done && (nr_pages = pagevec_lookup_tag(&pvec,
1773 NODE_MAPPING(sbi), &index, PAGECACHE_TAG_DIRTY))) {
Jan Kara45ec63b2017-11-15 17:34:51 -08001774 int i;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001775
1776 for (i = 0; i < nr_pages; i++) {
1777 struct page *page = pvec.pages[i];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001778 bool submitted = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001779
Chao Yudeb78d42018-06-04 23:20:36 +08001780 /* give a priority to WB_SYNC threads */
1781 if (atomic_read(&sbi->wb_sync_req[NODE]) &&
1782 wbc->sync_mode == WB_SYNC_NONE) {
1783 done = 1;
1784 break;
1785 }
1786
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001787 /*
1788 * flushing sequence with step:
1789 * 0. indirect nodes
1790 * 1. dentry dnodes
1791 * 2. file dnodes
1792 */
1793 if (step == 0 && IS_DNODE(page))
1794 continue;
1795 if (step == 1 && (!IS_DNODE(page) ||
1796 is_cold_node(page)))
1797 continue;
1798 if (step == 2 && (!IS_DNODE(page) ||
1799 !is_cold_node(page)))
1800 continue;
Chao Yu9a4cbc92016-02-22 18:35:46 +08001801lock_node:
Chao Yu90286462018-07-04 18:04:10 +08001802 if (wbc->sync_mode == WB_SYNC_ALL)
1803 lock_page(page);
1804 else if (!trylock_page(page))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001805 continue;
1806
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001807 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001808continue_unlock:
1809 unlock_page(page);
1810 continue;
1811 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001812
1813 if (!PageDirty(page)) {
1814 /* someone wrote it for us */
1815 goto continue_unlock;
1816 }
1817
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001818 /* flush inline_data */
Jaegeuk Kim52681372016-04-13 16:24:44 -07001819 if (is_inline_node(page)) {
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001820 clear_inline_node(page);
1821 unlock_page(page);
1822 flush_inline_data(sbi, ino_of_node(page));
Chao Yu9a4cbc92016-02-22 18:35:46 +08001823 goto lock_node;
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001824 }
1825
Chao Yud9b3df52018-12-25 17:43:42 +08001826 f2fs_wait_on_page_writeback(page, NODE, true, true);
Jaegeuk Kimfa3d2bd2016-01-28 11:48:52 -08001827
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001828 if (!clear_page_dirty_for_io(page))
1829 goto continue_unlock;
1830
Jaegeuk Kim52681372016-04-13 16:24:44 -07001831 set_fsync_mark(page, 0);
1832 set_dentry_mark(page, 0);
Jaegeuk Kim52746512014-08-11 18:18:36 -07001833
Yunlong Song0c535c52017-07-27 20:11:00 +08001834 ret = __write_node_page(page, false, &submitted,
Chao Yu3d5c54a2018-08-02 23:03:19 +08001835 wbc, do_balance, io_type, NULL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001836 if (ret)
Jaegeuk Kim52746512014-08-11 18:18:36 -07001837 unlock_page(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001838 else if (submitted)
Chao Yu3f5f4952016-09-29 18:50:10 +08001839 nwritten++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001840
1841 if (--wbc->nr_to_write == 0)
1842 break;
1843 }
1844 pagevec_release(&pvec);
1845 cond_resched();
1846
1847 if (wbc->nr_to_write == 0) {
1848 step = 2;
1849 break;
1850 }
1851 }
1852
1853 if (step < 2) {
Chao Yu5cf8d7b2018-07-25 19:16:21 +08001854 if (wbc->sync_mode == WB_SYNC_NONE && step == 1)
1855 goto out;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001856 step++;
1857 goto next_step;
1858 }
Chao Yu5cf8d7b2018-07-25 19:16:21 +08001859out:
Chao Yu3f5f4952016-09-29 18:50:10 +08001860 if (nwritten)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001861 f2fs_submit_merged_write(sbi, NODE);
Chao Yu86f78c12018-01-18 17:29:10 +08001862
1863 if (unlikely(f2fs_cp_error(sbi)))
1864 return -EIO;
Chao Yu3f5f4952016-09-29 18:50:10 +08001865 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001866}
1867
Chao Yu3d5c54a2018-08-02 23:03:19 +08001868int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
1869 unsigned int seq_id)
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001870{
Chao Yu3d5c54a2018-08-02 23:03:19 +08001871 struct fsync_node_entry *fn;
1872 struct page *page;
1873 struct list_head *head = &sbi->fsync_node_list;
1874 unsigned long flags;
1875 unsigned int cur_seq_id = 0;
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001876 int ret2, ret = 0;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001877
Chao Yu3d5c54a2018-08-02 23:03:19 +08001878 while (seq_id && cur_seq_id < seq_id) {
1879 spin_lock_irqsave(&sbi->fsync_node_lock, flags);
1880 if (list_empty(head)) {
1881 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1882 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001883 }
Chao Yu3d5c54a2018-08-02 23:03:19 +08001884 fn = list_first_entry(head, struct fsync_node_entry, list);
1885 if (fn->seq_id > seq_id) {
1886 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1887 break;
1888 }
1889 cur_seq_id = fn->seq_id;
1890 page = fn->page;
1891 get_page(page);
1892 spin_unlock_irqrestore(&sbi->fsync_node_lock, flags);
1893
Chao Yud9b3df52018-12-25 17:43:42 +08001894 f2fs_wait_on_page_writeback(page, NODE, true, false);
Chao Yu3d5c54a2018-08-02 23:03:19 +08001895 if (TestClearPageError(page))
1896 ret = -EIO;
1897
1898 put_page(page);
1899
1900 if (ret)
1901 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001902 }
1903
Miklos Szeredi280db3c2016-09-16 12:44:21 +02001904 ret2 = filemap_check_errors(NODE_MAPPING(sbi));
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001905 if (!ret)
1906 ret = ret2;
Chao Yu3d5c54a2018-08-02 23:03:19 +08001907
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001908 return ret;
1909}
1910
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001911static int f2fs_write_node_pages(struct address_space *mapping,
1912 struct writeback_control *wbc)
1913{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001914 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001915 struct blk_plug plug;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001916 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001917
Chao Yu00cc7ed2017-06-29 23:20:45 +08001918 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1919 goto skip_write;
1920
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001921 /* balancing f2fs's metadata in background */
1922 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001923
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001924 /* collect a number of dirty node pages and write together */
Jaegeuk Kimeeec9e72019-01-22 14:04:33 -08001925 if (wbc->sync_mode != WB_SYNC_ALL &&
1926 get_pages(sbi, F2FS_DIRTY_NODES) <
1927 nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001928 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001929
Chao Yudeb78d42018-06-04 23:20:36 +08001930 if (wbc->sync_mode == WB_SYNC_ALL)
1931 atomic_inc(&sbi->wb_sync_req[NODE]);
1932 else if (atomic_read(&sbi->wb_sync_req[NODE]))
1933 goto skip_write;
1934
Yunlei Hed31c7c32016-02-04 16:14:00 +08001935 trace_f2fs_writepages(mapping->host, wbc, NODE);
1936
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001937 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001938 blk_start_plug(&plug);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001939 f2fs_sync_node_pages(sbi, wbc, true, FS_NODE_IO);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001940 blk_finish_plug(&plug);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001941 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Chao Yudeb78d42018-06-04 23:20:36 +08001942
1943 if (wbc->sync_mode == WB_SYNC_ALL)
1944 atomic_dec(&sbi->wb_sync_req[NODE]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001945 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001946
1947skip_write:
1948 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001949 trace_f2fs_writepages(mapping->host, wbc, NODE);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001950 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001951}
1952
1953static int f2fs_set_node_page_dirty(struct page *page)
1954{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001955 trace_f2fs_set_page_dirty(page, NODE);
1956
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001957 if (!PageUptodate(page))
1958 SetPageUptodate(page);
Weichao Guof7467672018-03-09 23:10:21 +08001959#ifdef CONFIG_F2FS_CHECK_FS
1960 if (IS_INODE(page))
1961 f2fs_inode_chksum_set(F2FS_P_SB(page), page);
1962#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001963 if (!PageDirty(page)) {
Jaegeuk Kim95459e92018-04-20 19:29:52 -07001964 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001965 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Chao Yuc4e07602019-03-06 17:30:59 +08001966 f2fs_set_page_private(page, 0);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001967 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001968 return 1;
1969 }
1970 return 0;
1971}
1972
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001973/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001974 * Structure of the f2fs node operations
1975 */
1976const struct address_space_operations f2fs_node_aops = {
1977 .writepage = f2fs_write_node_page,
1978 .writepages = f2fs_write_node_pages,
1979 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001980 .invalidatepage = f2fs_invalidate_page,
1981 .releasepage = f2fs_release_page,
Weichao Guo5b7a4872016-09-20 05:03:27 +08001982#ifdef CONFIG_MIGRATION
1983 .migratepage = f2fs_migrate_page,
1984#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001985};
1986
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001987static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1988 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001989{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001990 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001991}
1992
Chao Yu84af6ae2017-09-29 13:59:35 +08001993static int __insert_free_nid(struct f2fs_sb_info *sbi,
Fan Li068868f2017-10-28 19:03:37 +08001994 struct free_nid *i, enum nid_state state)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001995{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001996 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001997
Fan Li068868f2017-10-28 19:03:37 +08001998 int err = radix_tree_insert(&nm_i->free_nid_root, i->nid, i);
1999 if (err)
2000 return err;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002001
Chao Yu84af6ae2017-09-29 13:59:35 +08002002 f2fs_bug_on(sbi, state != i->state);
2003 nm_i->nid_cnt[state]++;
2004 if (state == FREE_NID)
2005 list_add_tail(&i->list, &nm_i->free_nid_list);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002006 return 0;
2007}
2008
Chao Yu84af6ae2017-09-29 13:59:35 +08002009static void __remove_free_nid(struct f2fs_sb_info *sbi,
Fan Li068868f2017-10-28 19:03:37 +08002010 struct free_nid *i, enum nid_state state)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002011{
2012 struct f2fs_nm_info *nm_i = NM_I(sbi);
2013
Chao Yu84af6ae2017-09-29 13:59:35 +08002014 f2fs_bug_on(sbi, state != i->state);
2015 nm_i->nid_cnt[state]--;
2016 if (state == FREE_NID)
2017 list_del(&i->list);
Fan Li068868f2017-10-28 19:03:37 +08002018 radix_tree_delete(&nm_i->free_nid_root, i->nid);
2019}
2020
2021static void __move_free_nid(struct f2fs_sb_info *sbi, struct free_nid *i,
2022 enum nid_state org_state, enum nid_state dst_state)
2023{
2024 struct f2fs_nm_info *nm_i = NM_I(sbi);
2025
2026 f2fs_bug_on(sbi, org_state != i->state);
2027 i->state = dst_state;
2028 nm_i->nid_cnt[org_state]--;
2029 nm_i->nid_cnt[dst_state]++;
2030
2031 switch (dst_state) {
2032 case PREALLOC_NID:
2033 list_del(&i->list);
2034 break;
2035 case FREE_NID:
2036 list_add_tail(&i->list, &nm_i->free_nid_list);
2037 break;
2038 default:
2039 BUG_ON(1);
2040 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002041}
2042
LiFan4e68a152017-11-22 16:07:23 +08002043static void update_free_nid_bitmap(struct f2fs_sb_info *sbi, nid_t nid,
2044 bool set, bool build)
2045{
2046 struct f2fs_nm_info *nm_i = NM_I(sbi);
2047 unsigned int nat_ofs = NAT_BLOCK_OFFSET(nid);
2048 unsigned int nid_ofs = nid - START_NID(nid);
2049
2050 if (!test_bit_le(nat_ofs, nm_i->nat_block_bitmap))
2051 return;
2052
2053 if (set) {
2054 if (test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2055 return;
2056 __set_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2057 nm_i->free_nid_count[nat_ofs]++;
2058 } else {
2059 if (!test_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]))
2060 return;
2061 __clear_bit_le(nid_ofs, nm_i->free_nid_bitmap[nat_ofs]);
2062 if (!build)
2063 nm_i->free_nid_count[nat_ofs]--;
2064 }
2065}
2066
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002067/* return if the nid is recognized as free */
LiFan4e68a152017-11-22 16:07:23 +08002068static bool add_free_nid(struct f2fs_sb_info *sbi,
2069 nid_t nid, bool build, bool update)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002070{
2071 struct f2fs_nm_info *nm_i = NM_I(sbi);
2072 struct free_nid *i, *e;
2073 struct nat_entry *ne;
2074 int err = -EINVAL;
2075 bool ret = false;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09002076
2077 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08002078 if (unlikely(nid == 0))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002079 return false;
Gu Zheng7bd59382013-10-22 14:52:26 +08002080
2081 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002082 i->nid = nid;
Chao Yu84af6ae2017-09-29 13:59:35 +08002083 i->state = FREE_NID;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002084
LiFan4e68a152017-11-22 16:07:23 +08002085 radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002086
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002087 spin_lock(&nm_i->nid_list_lock);
2088
2089 if (build) {
2090 /*
2091 * Thread A Thread B
2092 * - f2fs_create
2093 * - f2fs_new_inode
Chao Yu6b4d6a82018-05-30 00:20:41 +08002094 * - f2fs_alloc_nid
Chao Yu84af6ae2017-09-29 13:59:35 +08002095 * - __insert_nid_to_list(PREALLOC_NID)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002096 * - f2fs_balance_fs_bg
Chao Yu6b4d6a82018-05-30 00:20:41 +08002097 * - f2fs_build_free_nids
2098 * - __f2fs_build_free_nids
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002099 * - scan_nat_page
2100 * - add_free_nid
2101 * - __lookup_nat_cache
2102 * - f2fs_add_link
Chao Yu6b4d6a82018-05-30 00:20:41 +08002103 * - f2fs_init_inode_metadata
2104 * - f2fs_new_inode_page
2105 * - f2fs_new_node_page
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002106 * - set_node_addr
Chao Yu6b4d6a82018-05-30 00:20:41 +08002107 * - f2fs_alloc_nid_done
Chao Yu84af6ae2017-09-29 13:59:35 +08002108 * - __remove_nid_from_list(PREALLOC_NID)
2109 * - __insert_nid_to_list(FREE_NID)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002110 */
2111 ne = __lookup_nat_cache(nm_i, nid);
2112 if (ne && (!get_nat_flag(ne, IS_CHECKPOINTED) ||
2113 nat_get_blkaddr(ne) != NULL_ADDR))
2114 goto err_out;
2115
2116 e = __lookup_free_nid_list(nm_i, nid);
2117 if (e) {
Chao Yu84af6ae2017-09-29 13:59:35 +08002118 if (e->state == FREE_NID)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002119 ret = true;
2120 goto err_out;
2121 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002122 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002123 ret = true;
Fan Li068868f2017-10-28 19:03:37 +08002124 err = __insert_free_nid(sbi, i, FREE_NID);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002125err_out:
LiFan4e68a152017-11-22 16:07:23 +08002126 if (update) {
2127 update_free_nid_bitmap(sbi, nid, ret, build);
2128 if (!build)
2129 nm_i->available_nids++;
2130 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002131 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002132 radix_tree_preload_end();
LiFan4e68a152017-11-22 16:07:23 +08002133
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002134 if (err)
2135 kmem_cache_free(free_nid_slab, i);
2136 return ret;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002137}
2138
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002139static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002140{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002141 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002142 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002143 bool need_free = false;
2144
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002145 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002146 i = __lookup_free_nid_list(nm_i, nid);
Chao Yu84af6ae2017-09-29 13:59:35 +08002147 if (i && i->state == FREE_NID) {
Fan Li068868f2017-10-28 19:03:37 +08002148 __remove_free_nid(sbi, i, FREE_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002149 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002150 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002151 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002152
2153 if (need_free)
2154 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002155}
2156
Chao Yu79939f62018-06-15 14:45:57 +08002157static int scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002158 struct page *nat_page, nid_t start_nid)
2159{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09002160 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002161 struct f2fs_nat_block *nat_blk = page_address(nat_page);
2162 block_t blk_addr;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002163 unsigned int nat_ofs = NAT_BLOCK_OFFSET(start_nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002164 int i;
2165
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002166 __set_bit_le(nat_ofs, nm_i->nat_block_bitmap);
2167
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002168 i = start_nid % NAT_ENTRY_PER_BLOCK;
2169
2170 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Chao Yucfb271d2013-12-05 17:15:22 +08002171 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09002172 break;
Haicheng Li23d38842013-05-06 23:15:43 +08002173
2174 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Chao Yu79939f62018-06-15 14:45:57 +08002175
2176 if (blk_addr == NEW_ADDR)
2177 return -EINVAL;
2178
LiFan4e68a152017-11-22 16:07:23 +08002179 if (blk_addr == NULL_ADDR) {
2180 add_free_nid(sbi, start_nid, true, true);
2181 } else {
2182 spin_lock(&NM_I(sbi)->nid_list_lock);
2183 update_free_nid_bitmap(sbi, start_nid, false, true);
2184 spin_unlock(&NM_I(sbi)->nid_list_lock);
2185 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002186 }
Chao Yu79939f62018-06-15 14:45:57 +08002187
2188 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002189}
2190
Chao Yub5610612017-11-08 17:47:36 +08002191static void scan_curseg_cache(struct f2fs_sb_info *sbi)
2192{
2193 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
2194 struct f2fs_journal *journal = curseg->journal;
2195 int i;
2196
2197 down_read(&curseg->journal_rwsem);
2198 for (i = 0; i < nats_in_cursum(journal); i++) {
2199 block_t addr;
2200 nid_t nid;
2201
2202 addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
2203 nid = le32_to_cpu(nid_in_journal(journal, i));
2204 if (addr == NULL_ADDR)
LiFan4e68a152017-11-22 16:07:23 +08002205 add_free_nid(sbi, nid, true, false);
Chao Yub5610612017-11-08 17:47:36 +08002206 else
2207 remove_free_nid(sbi, nid);
2208 }
2209 up_read(&curseg->journal_rwsem);
2210}
2211
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002212static void scan_free_nid_bits(struct f2fs_sb_info *sbi)
2213{
2214 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002215 unsigned int i, idx;
Fan Li5772e0c2017-11-07 19:14:24 +08002216 nid_t nid;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002217
2218 down_read(&nm_i->nat_tree_lock);
2219
2220 for (i = 0; i < nm_i->nat_blocks; i++) {
2221 if (!test_bit_le(i, nm_i->nat_block_bitmap))
2222 continue;
2223 if (!nm_i->free_nid_count[i])
2224 continue;
2225 for (idx = 0; idx < NAT_ENTRY_PER_BLOCK; idx++) {
Fan Li5772e0c2017-11-07 19:14:24 +08002226 idx = find_next_bit_le(nm_i->free_nid_bitmap[i],
2227 NAT_ENTRY_PER_BLOCK, idx);
2228 if (idx >= NAT_ENTRY_PER_BLOCK)
2229 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002230
2231 nid = i * NAT_ENTRY_PER_BLOCK + idx;
LiFan4e68a152017-11-22 16:07:23 +08002232 add_free_nid(sbi, nid, true, false);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002233
Chao Yu84af6ae2017-09-29 13:59:35 +08002234 if (nm_i->nid_cnt[FREE_NID] >= MAX_FREE_NIDS)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002235 goto out;
2236 }
2237 }
2238out:
Chao Yub5610612017-11-08 17:47:36 +08002239 scan_curseg_cache(sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002240
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002241 up_read(&nm_i->nat_tree_lock);
2242}
2243
Chao Yu79939f62018-06-15 14:45:57 +08002244static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
Chao Yu6b4d6a82018-05-30 00:20:41 +08002245 bool sync, bool mount)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002246{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002247 struct f2fs_nm_info *nm_i = NM_I(sbi);
Chao Yu79939f62018-06-15 14:45:57 +08002248 int i = 0, ret;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002249 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002250
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002251 if (unlikely(nid >= nm_i->max_nid))
2252 nid = 0;
2253
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002254 /* Enough entries */
Chao Yu84af6ae2017-09-29 13:59:35 +08002255 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yu79939f62018-06-15 14:45:57 +08002256 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002257
Chao Yu6b4d6a82018-05-30 00:20:41 +08002258 if (!sync && !f2fs_available_free_memory(sbi, FREE_NIDS))
Chao Yu79939f62018-06-15 14:45:57 +08002259 return 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002260
2261 if (!mount) {
2262 /* try to find free nids in free_nid_bitmap */
2263 scan_free_nid_bits(sbi);
2264
Fan Lia51e85e2017-11-07 11:04:33 +08002265 if (nm_i->nid_cnt[FREE_NID] >= NAT_ENTRY_PER_BLOCK)
Chao Yu79939f62018-06-15 14:45:57 +08002266 return 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002267 }
2268
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002269 /* readahead nat pages to be scanned */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002270 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES,
Chao Yu26879fb2015-10-12 17:05:59 +08002271 META_NAT, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002272
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002273 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002274
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002275 while (1) {
Yunlei He923df752017-11-17 16:13:38 +08002276 if (!test_bit_le(NAT_BLOCK_OFFSET(nid),
2277 nm_i->nat_block_bitmap)) {
2278 struct page *page = get_current_nat_page(sbi, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002279
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002280 if (IS_ERR(page)) {
2281 ret = PTR_ERR(page);
2282 } else {
2283 ret = scan_nat_page(sbi, page, nid);
2284 f2fs_put_page(page, 1);
2285 }
Chao Yu79939f62018-06-15 14:45:57 +08002286
2287 if (ret) {
2288 up_read(&nm_i->nat_tree_lock);
2289 f2fs_bug_on(sbi, !mount);
2290 f2fs_msg(sbi->sb, KERN_ERR,
2291 "NAT is corrupt, run fsck to fix it");
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002292 return ret;
Chao Yu79939f62018-06-15 14:45:57 +08002293 }
Yunlei He923df752017-11-17 16:13:38 +08002294 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002295
2296 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08002297 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002298 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002299
Chao Yua6d494b2015-07-24 18:26:26 +08002300 if (++i >= FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002301 break;
2302 }
2303
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002304 /* go to the next free nat pages to find free nids abundantly */
2305 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002306
2307 /* find free nids from current sum_pages */
Chao Yub5610612017-11-08 17:47:36 +08002308 scan_curseg_cache(sbi);
Chao Yudfc08a12016-02-14 18:50:40 +08002309
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002310 up_read(&nm_i->nat_tree_lock);
Chao Yu2db23882015-10-12 17:07:33 +08002311
Chao Yu6b4d6a82018-05-30 00:20:41 +08002312 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nm_i->next_scan_nid),
Chao Yuea1a29a02015-10-12 17:08:48 +08002313 nm_i->ra_nid_pages, META_NAT, false);
Chao Yu79939f62018-06-15 14:45:57 +08002314
2315 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002316}
2317
Chao Yu79939f62018-06-15 14:45:57 +08002318int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002319{
Chao Yu79939f62018-06-15 14:45:57 +08002320 int ret;
2321
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002322 mutex_lock(&NM_I(sbi)->build_lock);
Chao Yu79939f62018-06-15 14:45:57 +08002323 ret = __f2fs_build_free_nids(sbi, sync, mount);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002324 mutex_unlock(&NM_I(sbi)->build_lock);
Chao Yu79939f62018-06-15 14:45:57 +08002325
2326 return ret;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002327}
2328
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002329/*
2330 * If this function returns success, caller can obtain a new nid
2331 * from second parameter of this function.
2332 * The returned nid could be used ino as well as nid when inode is created.
2333 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002334bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002335{
2336 struct f2fs_nm_info *nm_i = NM_I(sbi);
2337 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002338retry:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002339 if (time_to_inject(sbi, FAULT_ALLOC_NID)) {
2340 f2fs_show_injection_info(FAULT_ALLOC_NID);
Jaegeuk Kimcb789422016-04-29 16:29:22 -07002341 return false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002342 }
Arnd Bergmann33c54d52018-08-13 23:38:06 +02002343
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002344 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002345
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002346 if (unlikely(nm_i->available_nids == 0)) {
2347 spin_unlock(&nm_i->nid_list_lock);
2348 return false;
2349 }
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002350
Chao Yu6b4d6a82018-05-30 00:20:41 +08002351 /* We should not use stale free nids created by f2fs_build_free_nids */
2352 if (nm_i->nid_cnt[FREE_NID] && !on_f2fs_build_free_nids(nm_i)) {
Chao Yu84af6ae2017-09-29 13:59:35 +08002353 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
2354 i = list_first_entry(&nm_i->free_nid_list,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002355 struct free_nid, list);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002356 *nid = i->nid;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002357
Fan Li068868f2017-10-28 19:03:37 +08002358 __move_free_nid(sbi, i, FREE_NID, PREALLOC_NID);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002359 nm_i->available_nids--;
2360
2361 update_free_nid_bitmap(sbi, *nid, false, false);
2362
2363 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002364 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002365 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002366 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09002367
2368 /* Let's scan nat pages and its caches to get free nids */
Jaegeuk Kim8c2bb1b2018-09-19 15:45:19 -07002369 if (!f2fs_build_free_nids(sbi, true, false))
2370 goto retry;
2371 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002372}
2373
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002374/*
Chao Yu6b4d6a82018-05-30 00:20:41 +08002375 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002376 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002377void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002378{
2379 struct f2fs_nm_info *nm_i = NM_I(sbi);
2380 struct free_nid *i;
2381
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002382 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002383 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002384 f2fs_bug_on(sbi, !i);
Fan Li068868f2017-10-28 19:03:37 +08002385 __remove_free_nid(sbi, i, PREALLOC_NID);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002386 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002387
2388 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002389}
2390
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002391/*
Chao Yu6b4d6a82018-05-30 00:20:41 +08002392 * f2fs_alloc_nid() should be called prior to this function.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002393 */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002394void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002395{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002396 struct f2fs_nm_info *nm_i = NM_I(sbi);
2397 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002398 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09002399
Jaegeuk Kim65985d92013-08-14 21:57:27 +09002400 if (!nid)
2401 return;
2402
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002403 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002404 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002405 f2fs_bug_on(sbi, !i);
2406
Chao Yu6b4d6a82018-05-30 00:20:41 +08002407 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) {
Fan Li068868f2017-10-28 19:03:37 +08002408 __remove_free_nid(sbi, i, PREALLOC_NID);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002409 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08002410 } else {
Fan Li068868f2017-10-28 19:03:37 +08002411 __move_free_nid(sbi, i, PREALLOC_NID, FREE_NID);
Haicheng Li95630cb2013-05-06 23:15:41 +08002412 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002413
2414 nm_i->available_nids++;
2415
2416 update_free_nid_bitmap(sbi, nid, true, false);
2417
2418 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08002419
2420 if (need_free)
2421 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002422}
2423
Chao Yu6b4d6a82018-05-30 00:20:41 +08002424int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
Chao Yu31696582015-07-28 18:33:46 +08002425{
2426 struct f2fs_nm_info *nm_i = NM_I(sbi);
2427 struct free_nid *i, *next;
2428 int nr = nr_shrink;
2429
Chao Yu84af6ae2017-09-29 13:59:35 +08002430 if (nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Jaegeuk Kimad4edb82016-06-16 16:41:49 -07002431 return 0;
2432
Chao Yu31696582015-07-28 18:33:46 +08002433 if (!mutex_trylock(&nm_i->build_lock))
2434 return 0;
2435
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002436 spin_lock(&nm_i->nid_list_lock);
Chao Yu84af6ae2017-09-29 13:59:35 +08002437 list_for_each_entry_safe(i, next, &nm_i->free_nid_list, list) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002438 if (nr_shrink <= 0 ||
Chao Yu84af6ae2017-09-29 13:59:35 +08002439 nm_i->nid_cnt[FREE_NID] <= MAX_FREE_NIDS)
Chao Yu31696582015-07-28 18:33:46 +08002440 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002441
Fan Li068868f2017-10-28 19:03:37 +08002442 __remove_free_nid(sbi, i, FREE_NID);
Chao Yu31696582015-07-28 18:33:46 +08002443 kmem_cache_free(free_nid_slab, i);
2444 nr_shrink--;
Chao Yu31696582015-07-28 18:33:46 +08002445 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002446 spin_unlock(&nm_i->nid_list_lock);
Chao Yu31696582015-07-28 18:33:46 +08002447 mutex_unlock(&nm_i->build_lock);
2448
2449 return nr - nr_shrink;
2450}
2451
Chao Yu6b4d6a82018-05-30 00:20:41 +08002452void f2fs_recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08002453{
Chao Yu28cdce02014-03-11 13:37:38 +08002454 void *src_addr, *dst_addr;
2455 size_t inline_size;
2456 struct page *ipage;
2457 struct f2fs_inode *ri;
2458
Chao Yu6b4d6a82018-05-30 00:20:41 +08002459 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002460 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08002461
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002462 ri = F2FS_INODE(page);
Yunlei He4f76d6a2018-01-03 18:03:04 +08002463 if (ri->i_inline & F2FS_INLINE_XATTR) {
2464 set_inode_flag(inode, FI_INLINE_XATTR);
2465 } else {
Jaegeuk Kim91942322016-05-20 10:13:22 -07002466 clear_inode_flag(inode, FI_INLINE_XATTR);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002467 goto update_inode;
2468 }
2469
Chao Yu50ffaa92017-09-06 21:59:50 +08002470 dst_addr = inline_xattr_addr(inode, ipage);
2471 src_addr = inline_xattr_addr(inode, page);
Chao Yu28cdce02014-03-11 13:37:38 +08002472 inline_size = inline_xattr_size(inode);
2473
Chao Yud9b3df52018-12-25 17:43:42 +08002474 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
Chao Yu28cdce02014-03-11 13:37:38 +08002475 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07002476update_inode:
Chao Yu6b4d6a82018-05-30 00:20:41 +08002477 f2fs_update_inode(inode, ipage);
Chao Yu28cdce02014-03-11 13:37:38 +08002478 f2fs_put_page(ipage, 1);
2479}
2480
Chao Yu6b4d6a82018-05-30 00:20:41 +08002481int f2fs_recover_xattr_data(struct inode *inode, struct page *page)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002482{
Jaegeuk Kim40813632014-09-02 15:31:18 -07002483 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002484 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
Yunlei Hee21d43c2017-07-18 09:48:12 +08002485 nid_t new_xnid;
2486 struct dnode_of_data dn;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002487 struct node_info ni;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002488 struct page *xpage;
Chao Yu3c743262018-07-17 00:02:17 +08002489 int err;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002490
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002491 if (!prev_xnid)
2492 goto recover_xnid;
2493
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002494 /* 1: invalidate the previous xattr nid */
Chao Yu3c743262018-07-17 00:02:17 +08002495 err = f2fs_get_node_info(sbi, prev_xnid, &ni);
2496 if (err)
2497 return err;
2498
Chao Yu6b4d6a82018-05-30 00:20:41 +08002499 f2fs_invalidate_blocks(sbi, ni.blk_addr);
Chao Yud9bfbbb2017-07-06 01:11:31 +08002500 dec_valid_node_count(sbi, inode, false);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002501 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002502
2503recover_xnid:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002504 /* 2: update xattr nid in inode */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002505 if (!f2fs_alloc_nid(sbi, &new_xnid))
Yunlei Hee21d43c2017-07-18 09:48:12 +08002506 return -ENOSPC;
2507
2508 set_new_dnode(&dn, inode, NULL, NULL, new_xnid);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002509 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
Yunlei Hee21d43c2017-07-18 09:48:12 +08002510 if (IS_ERR(xpage)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002511 f2fs_alloc_nid_failed(sbi, new_xnid);
Yunlei Hee21d43c2017-07-18 09:48:12 +08002512 return PTR_ERR(xpage);
2513 }
2514
Chao Yu6b4d6a82018-05-30 00:20:41 +08002515 f2fs_alloc_nid_done(sbi, new_xnid);
2516 f2fs_update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002517
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002518 /* 3: update and set xattr node page dirty */
Yunlei Hee21d43c2017-07-18 09:48:12 +08002519 memcpy(F2FS_NODE(xpage), F2FS_NODE(page), VALID_XATTR_BLOCK_SIZE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002520
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002521 set_page_dirty(xpage);
2522 f2fs_put_page(xpage, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002523
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002524 return 0;
Jaegeuk Kimabb23662014-01-28 12:25:06 +09002525}
2526
Chao Yu6b4d6a82018-05-30 00:20:41 +08002527int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002528{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002529 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002530 nid_t ino = ino_of_node(page);
2531 struct node_info old_ni, new_ni;
2532 struct page *ipage;
Chao Yu3c743262018-07-17 00:02:17 +08002533 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002534
Chao Yu3c743262018-07-17 00:02:17 +08002535 err = f2fs_get_node_info(sbi, ino, &old_ni);
2536 if (err)
2537 return err;
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09002538
2539 if (unlikely(old_ni.blk_addr != NULL_ADDR))
2540 return -EINVAL;
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002541retry:
Jaegeuk Kim300e1292016-04-29 16:11:53 -07002542 ipage = f2fs_grab_cache_page(NODE_MAPPING(sbi), ino, false);
Jaegeuk Kime8ea9b32016-09-09 16:59:39 -07002543 if (!ipage) {
2544 congestion_wait(BLK_RW_ASYNC, HZ/50);
2545 goto retry;
2546 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002547
arter97e1c42042014-08-06 23:22:50 +09002548 /* Should not use this inode from free nid list */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002549 remove_free_nid(sbi, ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002550
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002551 if (!PageUptodate(ipage))
2552 SetPageUptodate(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002553 fill_node_footer(ipage, ino, ino, 0, true);
Chao Yu343c8062018-10-03 22:32:44 +08002554 set_cold_node(ipage, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002555
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002556 src = F2FS_INODE(page);
2557 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002558
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09002559 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
2560 dst->i_size = 0;
2561 dst->i_blocks = cpu_to_le64(1);
2562 dst->i_links = cpu_to_le32(1);
2563 dst->i_xattr_nid = 0;
Chao Yufbcf9312017-07-19 00:19:06 +08002564 dst->i_inline = src->i_inline & (F2FS_INLINE_XATTR | F2FS_EXTRA_ATTR);
Chao Yu5647b302017-07-26 00:01:41 +08002565 if (dst->i_inline & F2FS_EXTRA_ATTR) {
Chao Yufbcf9312017-07-19 00:19:06 +08002566 dst->i_extra_isize = src->i_extra_isize;
Chao Yu50ffaa92017-09-06 21:59:50 +08002567
Chao Yu5b11d2b2018-10-24 18:34:26 +08002568 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
Chao Yu50ffaa92017-09-06 21:59:50 +08002569 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2570 i_inline_xattr_size))
2571 dst->i_inline_xattr_size = src->i_inline_xattr_size;
2572
Chao Yu5b11d2b2018-10-24 18:34:26 +08002573 if (f2fs_sb_has_project_quota(sbi) &&
Chao Yu5647b302017-07-26 00:01:41 +08002574 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2575 i_projid))
2576 dst->i_projid = src->i_projid;
Chao Yuada3b8e2018-09-25 15:36:01 +08002577
Chao Yu5b11d2b2018-10-24 18:34:26 +08002578 if (f2fs_sb_has_inode_crtime(sbi) &&
Chao Yuada3b8e2018-09-25 15:36:01 +08002579 F2FS_FITS_IN_INODE(src, le16_to_cpu(src->i_extra_isize),
2580 i_crtime_nsec)) {
2581 dst->i_crtime = src->i_crtime;
2582 dst->i_crtime_nsec = src->i_crtime_nsec;
2583 }
Chao Yu5647b302017-07-26 00:01:41 +08002584 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002585
2586 new_ni = old_ni;
2587 new_ni.ino = ino;
2588
Chao Yu09c3a722017-07-09 00:13:07 +08002589 if (unlikely(inc_valid_node_count(sbi, NULL, true)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09002590 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09002591 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002592 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07002593 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002594 f2fs_put_page(ipage, 1);
2595 return 0;
2596}
2597
Chao Yu3c743262018-07-17 00:02:17 +08002598int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002599 unsigned int segno, struct f2fs_summary_block *sum)
2600{
2601 struct f2fs_node *rn;
2602 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002603 block_t addr;
Chao Yu9ecf4b82014-12-18 18:29:05 +08002604 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002605
2606 /* scan the node segment */
2607 last_offset = sbi->blocks_per_seg;
2608 addr = START_BLOCK(sbi, segno);
2609 sum_entry = &sum->entries[0];
2610
Chao Yu9ecf4b82014-12-18 18:29:05 +08002611 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002612 nrpages = min(last_offset - i, BIO_MAX_PAGES);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002613
arter97e1c42042014-08-06 23:22:50 +09002614 /* readahead node pages */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002615 f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002616
Chao Yu9ecf4b82014-12-18 18:29:05 +08002617 for (idx = addr; idx < addr + nrpages; idx++) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002618 struct page *page = f2fs_get_tmp_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08002619
Chao Yu3c743262018-07-17 00:02:17 +08002620 if (IS_ERR(page))
2621 return PTR_ERR(page);
2622
Chao Yu9ecf4b82014-12-18 18:29:05 +08002623 rn = F2FS_NODE(page);
2624 sum_entry->nid = rn->footer.nid;
2625 sum_entry->version = 0;
2626 sum_entry->ofs_in_node = 0;
2627 sum_entry++;
2628 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08002629 }
Chao Yubac4eef2014-05-27 08:41:07 +08002630
Chao Yu9ecf4b82014-12-18 18:29:05 +08002631 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08002632 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002633 }
Chao Yu3c743262018-07-17 00:02:17 +08002634 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002635}
2636
Chao Yuaec71382014-06-24 09:18:20 +08002637static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002638{
2639 struct f2fs_nm_info *nm_i = NM_I(sbi);
2640 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002641 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002642 int i;
2643
Chao Yub7ad7512016-02-19 18:08:46 +08002644 down_write(&curseg->journal_rwsem);
Chao Yudfc08a12016-02-14 18:50:40 +08002645 for (i = 0; i < nats_in_cursum(journal); i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002646 struct nat_entry *ne;
2647 struct f2fs_nat_entry raw_ne;
Chao Yudfc08a12016-02-14 18:50:40 +08002648 nid_t nid = le32_to_cpu(nid_in_journal(journal, i));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002649
Chao Yudfc08a12016-02-14 18:50:40 +08002650 raw_ne = nat_in_journal(journal, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08002651
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002652 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002653 if (!ne) {
Yunlei He45d6e702017-11-10 13:36:51 -08002654 ne = __alloc_nat_entry(nid, true);
2655 __init_nat_entry(nm_i, ne, &raw_ne, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002656 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002657
2658 /*
2659 * if a free nat in journal has not been used after last
2660 * checkpoint, we should remove it from available nids,
2661 * since later we will add it again.
2662 */
2663 if (!get_nat_flag(ne, IS_DIRTY) &&
2664 le32_to_cpu(raw_ne.block_addr) == NULL_ADDR) {
2665 spin_lock(&nm_i->nid_list_lock);
2666 nm_i->available_nids--;
2667 spin_unlock(&nm_i->nid_list_lock);
2668 }
2669
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002670 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002671 }
Chao Yudfc08a12016-02-14 18:50:40 +08002672 update_nats_in_cursum(journal, -i);
Chao Yub7ad7512016-02-19 18:08:46 +08002673 up_write(&curseg->journal_rwsem);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002674}
2675
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002676static void __adjust_nat_entry_set(struct nat_entry_set *nes,
2677 struct list_head *head, int max)
2678{
2679 struct nat_entry_set *cur;
2680
2681 if (nes->entry_cnt >= max)
2682 goto add_out;
2683
2684 list_for_each_entry(cur, head, set_list) {
2685 if (cur->entry_cnt >= nes->entry_cnt) {
2686 list_add(&nes->set_list, cur->set_list.prev);
2687 return;
2688 }
2689 }
2690add_out:
2691 list_add_tail(&nes->set_list, head);
2692}
2693
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002694static void __update_nat_bits(struct f2fs_sb_info *sbi, nid_t start_nid,
2695 struct page *page)
2696{
2697 struct f2fs_nm_info *nm_i = NM_I(sbi);
2698 unsigned int nat_index = start_nid / NAT_ENTRY_PER_BLOCK;
2699 struct f2fs_nat_block *nat_blk = page_address(page);
2700 int valid = 0;
Fan Li1033eee2017-10-30 15:19:48 +08002701 int i = 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002702
2703 if (!enabled_nat_bits(sbi, NULL))
2704 return;
2705
Fan Li1033eee2017-10-30 15:19:48 +08002706 if (nat_index == 0) {
2707 valid = 1;
2708 i = 1;
2709 }
2710 for (; i < NAT_ENTRY_PER_BLOCK; i++) {
2711 if (nat_blk->entries[i].block_addr != NULL_ADDR)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002712 valid++;
2713 }
2714 if (valid == 0) {
2715 __set_bit_le(nat_index, nm_i->empty_nat_bits);
2716 __clear_bit_le(nat_index, nm_i->full_nat_bits);
2717 return;
2718 }
2719
2720 __clear_bit_le(nat_index, nm_i->empty_nat_bits);
2721 if (valid == NAT_ENTRY_PER_BLOCK)
2722 __set_bit_le(nat_index, nm_i->full_nat_bits);
2723 else
2724 __clear_bit_le(nat_index, nm_i->full_nat_bits);
2725}
2726
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002727static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002728 struct nat_entry_set *set, struct cp_control *cpc)
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002729{
2730 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002731 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002732 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
2733 bool to_journal = true;
2734 struct f2fs_nat_block *nat_blk;
2735 struct nat_entry *ne, *cur;
2736 struct page *page = NULL;
2737
2738 /*
2739 * there are two steps to flush nat entries:
2740 * #1, flush nat entries to journal in current hot data summary block.
2741 * #2, flush nat entries to nat page.
2742 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002743 if (enabled_nat_bits(sbi, cpc) ||
2744 !__has_cursum_space(journal, set->entry_cnt, NAT_JOURNAL))
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002745 to_journal = false;
2746
2747 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002748 down_write(&curseg->journal_rwsem);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002749 } else {
2750 page = get_next_nat_page(sbi, start_nid);
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002751 if (IS_ERR(page))
2752 return PTR_ERR(page);
2753
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002754 nat_blk = page_address(page);
2755 f2fs_bug_on(sbi, !nat_blk);
2756 }
2757
2758 /* flush dirty nats in nat entry set */
2759 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
2760 struct f2fs_nat_entry *raw_ne;
2761 nid_t nid = nat_get_nid(ne);
2762 int offset;
2763
Chao Yu86aadea2017-06-05 18:29:08 +08002764 f2fs_bug_on(sbi, nat_get_blkaddr(ne) == NEW_ADDR);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002765
2766 if (to_journal) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002767 offset = f2fs_lookup_journal_in_cursum(journal,
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002768 NAT_JOURNAL, nid, 1);
2769 f2fs_bug_on(sbi, offset < 0);
Chao Yudfc08a12016-02-14 18:50:40 +08002770 raw_ne = &nat_in_journal(journal, offset);
2771 nid_in_journal(journal, offset) = cpu_to_le32(nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002772 } else {
2773 raw_ne = &nat_blk->entries[nid - start_nid];
2774 }
2775 raw_nat_from_node_info(raw_ne, &ne->ni);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002776 nat_reset_flag(ne);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002777 __clear_nat_cache_dirty(NM_I(sbi), set, ne);
2778 if (nat_get_blkaddr(ne) == NULL_ADDR) {
LiFan4e68a152017-11-22 16:07:23 +08002779 add_free_nid(sbi, nid, false, true);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002780 } else {
2781 spin_lock(&NM_I(sbi)->nid_list_lock);
2782 update_free_nid_bitmap(sbi, nid, false, false);
2783 spin_unlock(&NM_I(sbi)->nid_list_lock);
2784 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002785 }
2786
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002787 if (to_journal) {
Chao Yub7ad7512016-02-19 18:08:46 +08002788 up_write(&curseg->journal_rwsem);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002789 } else {
2790 __update_nat_bits(sbi, start_nid, page);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002791 f2fs_put_page(page, 1);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002792 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002793
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002794 /* Allow dirty nats by node block allocation in write_begin */
2795 if (!set->entry_cnt) {
2796 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
2797 kmem_cache_free(nat_entry_set_slab, set);
2798 }
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002799 return 0;
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002800}
2801
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002802/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002803 * This function is called during the checkpointing process.
2804 */
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002805int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002806{
2807 struct f2fs_nm_info *nm_i = NM_I(sbi);
2808 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
Chao Yub7ad7512016-02-19 18:08:46 +08002809 struct f2fs_journal *journal = curseg->journal;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002810 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002811 struct nat_entry_set *set, *tmp;
2812 unsigned int found;
2813 nid_t set_idx = 0;
2814 LIST_HEAD(sets);
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002815 int err = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002816
Jaegeuk Kim882c7da2018-06-28 19:34:40 -07002817 /* during unmount, let's flush nat_bits before checking dirty_nat_cnt */
2818 if (enabled_nat_bits(sbi, cpc)) {
2819 down_write(&nm_i->nat_tree_lock);
2820 remove_nats_in_journal(sbi);
2821 up_write(&nm_i->nat_tree_lock);
2822 }
2823
Changman Lee20d047c2014-11-25 12:44:23 +09002824 if (!nm_i->dirty_nat_cnt)
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002825 return 0;
Jaegeuk Kima5131192016-01-02 09:19:41 -08002826
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002827 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kima5131192016-01-02 09:19:41 -08002828
Chao Yuaec71382014-06-24 09:18:20 +08002829 /*
2830 * if there are no enough space in journal to store dirty nat
2831 * entries, remove all entries from journal and merge them
2832 * into nat entry set.
2833 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002834 if (enabled_nat_bits(sbi, cpc) ||
2835 !__has_cursum_space(journal, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08002836 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002837
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002838 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002839 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002840 unsigned idx;
2841 set_idx = setvec[found - 1]->set + 1;
2842 for (idx = 0; idx < found; idx++)
2843 __adjust_nat_entry_set(setvec[idx], &sets,
Chao Yudfc08a12016-02-14 18:50:40 +08002844 MAX_NAT_JENTRIES(journal));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002845 }
Chao Yuaec71382014-06-24 09:18:20 +08002846
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002847 /* flush dirty nats in nat entry set */
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002848 list_for_each_entry_safe(set, tmp, &sets, set_list) {
2849 err = __flush_nat_entry_set(sbi, set, cpc);
2850 if (err)
2851 break;
2852 }
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07002853
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002854 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002855 /* Allow dirty nats by node block allocation in write_begin */
Jaegeuk Kim4474e2f2018-09-17 17:36:06 -07002856
2857 return err;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002858}
Jaegeuk Kima5131192016-01-02 09:19:41 -08002859
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002860static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
2861{
2862 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
2863 struct f2fs_nm_info *nm_i = NM_I(sbi);
2864 unsigned int nat_bits_bytes = nm_i->nat_blocks / BITS_PER_BYTE;
2865 unsigned int i;
2866 __u64 cp_ver = cur_cp_version(ckpt);
2867 block_t nat_bits_addr;
2868
2869 if (!enabled_nat_bits(sbi, NULL))
2870 return 0;
2871
Chao Yua33ce032018-03-20 23:08:29 +08002872 nm_i->nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
Chao Yue585ca22017-11-30 19:28:17 +08002873 nm_i->nat_bits = f2fs_kzalloc(sbi,
2874 nm_i->nat_bits_blocks << F2FS_BLKSIZE_BITS, GFP_KERNEL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002875 if (!nm_i->nat_bits)
2876 return -ENOMEM;
2877
2878 nat_bits_addr = __start_cp_addr(sbi) + sbi->blocks_per_seg -
2879 nm_i->nat_bits_blocks;
2880 for (i = 0; i < nm_i->nat_bits_blocks; i++) {
Chao Yu3c743262018-07-17 00:02:17 +08002881 struct page *page;
2882
2883 page = f2fs_get_meta_page(sbi, nat_bits_addr++);
Chao Yuca52deb2018-10-04 11:15:20 +08002884 if (IS_ERR(page))
Chao Yu3c743262018-07-17 00:02:17 +08002885 return PTR_ERR(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002886
2887 memcpy(nm_i->nat_bits + (i << F2FS_BLKSIZE_BITS),
2888 page_address(page), F2FS_BLKSIZE);
2889 f2fs_put_page(page, 1);
2890 }
2891
2892 cp_ver |= (cur_cp_crc(ckpt) << 32);
2893 if (cpu_to_le64(cp_ver) != *(__le64 *)nm_i->nat_bits) {
2894 disable_nat_bits(sbi, true);
2895 return 0;
2896 }
2897
2898 nm_i->full_nat_bits = nm_i->nat_bits + 8;
2899 nm_i->empty_nat_bits = nm_i->full_nat_bits + nat_bits_bytes;
2900
2901 f2fs_msg(sbi->sb, KERN_NOTICE, "Found nat_bits in checkpoint");
2902 return 0;
2903}
2904
Hou Pengyang61c356c2017-05-17 02:48:48 +00002905static inline void load_free_nid_bitmap(struct f2fs_sb_info *sbi)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002906{
2907 struct f2fs_nm_info *nm_i = NM_I(sbi);
2908 unsigned int i = 0;
2909 nid_t nid, last_nid;
2910
2911 if (!enabled_nat_bits(sbi, NULL))
2912 return;
2913
2914 for (i = 0; i < nm_i->nat_blocks; i++) {
2915 i = find_next_bit_le(nm_i->empty_nat_bits, nm_i->nat_blocks, i);
2916 if (i >= nm_i->nat_blocks)
2917 break;
2918
2919 __set_bit_le(i, nm_i->nat_block_bitmap);
2920
2921 nid = i * NAT_ENTRY_PER_BLOCK;
Fan Li51cb3992017-11-02 11:02:52 +08002922 last_nid = nid + NAT_ENTRY_PER_BLOCK;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002923
2924 spin_lock(&NM_I(sbi)->nid_list_lock);
2925 for (; nid < last_nid; nid++)
2926 update_free_nid_bitmap(sbi, nid, true, true);
2927 spin_unlock(&NM_I(sbi)->nid_list_lock);
2928 }
2929
2930 for (i = 0; i < nm_i->nat_blocks; i++) {
2931 i = find_next_bit_le(nm_i->full_nat_bits, nm_i->nat_blocks, i);
2932 if (i >= nm_i->nat_blocks)
2933 break;
2934
2935 __set_bit_le(i, nm_i->nat_block_bitmap);
2936 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002937}
2938
2939static int init_node_manager(struct f2fs_sb_info *sbi)
2940{
2941 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
2942 struct f2fs_nm_info *nm_i = NM_I(sbi);
2943 unsigned char *version_bitmap;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002944 unsigned int nat_segs;
2945 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002946
2947 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
2948
2949 /* segment_count_nat includes pair segment so divide to 2. */
2950 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002951 nm_i->nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
2952 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nm_i->nat_blocks;
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09002953
Jaegeuk Kimb63da152014-02-17 12:44:20 +09002954 /* not used nids: 0, node, meta, (and root counted as valid node) */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002955 nm_i->available_nids = nm_i->max_nid - sbi->total_valid_node_count -
Chao Yu09234be2017-11-16 16:59:14 +08002956 sbi->nquota_files - F2FS_RESERVED_NODE_NUM;
Chao Yu84af6ae2017-09-29 13:59:35 +08002957 nm_i->nid_cnt[FREE_NID] = 0;
2958 nm_i->nid_cnt[PREALLOC_NID] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002959 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09002960 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Chao Yuea1a29a02015-10-12 17:08:48 +08002961 nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
Chao Yu2304cb02016-01-18 18:32:58 +08002962 nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002963
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002964 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Chao Yu84af6ae2017-09-29 13:59:35 +08002965 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08002966 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
2967 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002968 INIT_LIST_HEAD(&nm_i->nat_entries);
Chao Yu7309a5f2018-08-05 23:08:59 +08002969 spin_lock_init(&nm_i->nat_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002970
2971 mutex_init(&nm_i->build_lock);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002972 spin_lock_init(&nm_i->nid_list_lock);
Jaegeuk Kimb873b792016-08-04 11:38:25 -07002973 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002974
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002975 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002976 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002977 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
2978 if (!version_bitmap)
2979 return -EFAULT;
2980
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02002981 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
2982 GFP_KERNEL);
2983 if (!nm_i->nat_bitmap)
2984 return -ENOMEM;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002985
2986 err = __get_nat_bitmaps(sbi);
2987 if (err)
2988 return err;
2989
2990#ifdef CONFIG_F2FS_CHECK_FS
2991 nm_i->nat_bitmap_mir = kmemdup(version_bitmap, nm_i->bitmap_size,
2992 GFP_KERNEL);
2993 if (!nm_i->nat_bitmap_mir)
2994 return -ENOMEM;
2995#endif
2996
2997 return 0;
2998}
2999
3000static int init_free_nid_cache(struct f2fs_sb_info *sbi)
3001{
3002 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim9f1896c2018-03-09 17:42:28 -08003003 int i;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003004
Kees Cook61f48642018-06-12 14:28:23 -07003005 nm_i->free_nid_bitmap =
3006 f2fs_kzalloc(sbi, array_size(sizeof(unsigned char *),
3007 nm_i->nat_blocks),
3008 GFP_KERNEL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003009 if (!nm_i->free_nid_bitmap)
3010 return -ENOMEM;
3011
Jaegeuk Kim9f1896c2018-03-09 17:42:28 -08003012 for (i = 0; i < nm_i->nat_blocks; i++) {
3013 nm_i->free_nid_bitmap[i] = f2fs_kvzalloc(sbi,
Yunlei He4679c2e2018-06-27 14:46:21 +08003014 f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK), GFP_KERNEL);
Yunlei Heb22d0c62018-07-02 10:40:19 +08003015 if (!nm_i->free_nid_bitmap[i])
Jaegeuk Kim9f1896c2018-03-09 17:42:28 -08003016 return -ENOMEM;
3017 }
3018
Chao Yuead52592017-11-30 19:28:18 +08003019 nm_i->nat_block_bitmap = f2fs_kvzalloc(sbi, nm_i->nat_blocks / 8,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003020 GFP_KERNEL);
3021 if (!nm_i->nat_block_bitmap)
3022 return -ENOMEM;
3023
Kees Cookf950fa42018-06-12 14:28:35 -07003024 nm_i->free_nid_count =
3025 f2fs_kvzalloc(sbi, array_size(sizeof(unsigned short),
3026 nm_i->nat_blocks),
3027 GFP_KERNEL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003028 if (!nm_i->free_nid_count)
3029 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003030 return 0;
3031}
3032
Chao Yu6b4d6a82018-05-30 00:20:41 +08003033int f2fs_build_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003034{
3035 int err;
3036
Chao Yue585ca22017-11-30 19:28:17 +08003037 sbi->nm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_nm_info),
3038 GFP_KERNEL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003039 if (!sbi->nm_info)
3040 return -ENOMEM;
3041
3042 err = init_node_manager(sbi);
3043 if (err)
3044 return err;
3045
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003046 err = init_free_nid_cache(sbi);
3047 if (err)
3048 return err;
3049
3050 /* load free nid status from nat_bits table */
3051 load_free_nid_bitmap(sbi);
3052
Chao Yu79939f62018-06-15 14:45:57 +08003053 return f2fs_build_free_nids(sbi, true, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003054}
3055
Chao Yu6b4d6a82018-05-30 00:20:41 +08003056void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003057{
3058 struct f2fs_nm_info *nm_i = NM_I(sbi);
3059 struct free_nid *i, *next_i;
3060 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003061 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003062 nid_t nid = 0;
3063 unsigned int found;
3064
3065 if (!nm_i)
3066 return;
3067
3068 /* destroy free nid list */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003069 spin_lock(&nm_i->nid_list_lock);
Chao Yu84af6ae2017-09-29 13:59:35 +08003070 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Fan Li068868f2017-10-28 19:03:37 +08003071 __remove_free_nid(sbi, i, FREE_NID);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003072 spin_unlock(&nm_i->nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08003073 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003074 spin_lock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003075 }
Chao Yu84af6ae2017-09-29 13:59:35 +08003076 f2fs_bug_on(sbi, nm_i->nid_cnt[FREE_NID]);
3077 f2fs_bug_on(sbi, nm_i->nid_cnt[PREALLOC_NID]);
3078 f2fs_bug_on(sbi, !list_empty(&nm_i->free_nid_list));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003079 spin_unlock(&nm_i->nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003080
3081 /* destroy nat cache */
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003082 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003083 while ((found = __gang_lookup_nat_cache(nm_i,
3084 nid, NATVEC_SIZE, natvec))) {
3085 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003086
Gu Zhengb6ce3912014-03-07 18:43:24 +08003087 nid = nat_get_nid(natvec[found - 1]) + 1;
Chao Yu7309a5f2018-08-05 23:08:59 +08003088 for (idx = 0; idx < found; idx++) {
3089 spin_lock(&nm_i->nat_list_lock);
3090 list_del(&natvec[idx]->list);
3091 spin_unlock(&nm_i->nat_list_lock);
3092
Gu Zhengb6ce3912014-03-07 18:43:24 +08003093 __del_from_nat_cache(nm_i, natvec[idx]);
Chao Yu7309a5f2018-08-05 23:08:59 +08003094 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003095 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07003096 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08003097
3098 /* destroy nat set cache */
3099 nid = 0;
3100 while ((found = __gang_lookup_nat_set(nm_i,
3101 nid, SETVEC_SIZE, setvec))) {
3102 unsigned idx;
3103
3104 nid = setvec[found - 1]->set + 1;
3105 for (idx = 0; idx < found; idx++) {
3106 /* entry_cnt is not zero, when cp_error was occurred */
3107 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
3108 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
3109 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
3110 }
3111 }
Jaegeuk Kimb873b792016-08-04 11:38:25 -07003112 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003113
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003114 kvfree(nm_i->nat_block_bitmap);
Jaegeuk Kim9f1896c2018-03-09 17:42:28 -08003115 if (nm_i->free_nid_bitmap) {
3116 int i;
3117
3118 for (i = 0; i < nm_i->nat_blocks; i++)
3119 kvfree(nm_i->free_nid_bitmap[i]);
Jaegeuk Kim9248dba2018-12-13 18:38:33 -08003120 kvfree(nm_i->free_nid_bitmap);
Jaegeuk Kim9f1896c2018-03-09 17:42:28 -08003121 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003122 kvfree(nm_i->free_nid_count);
3123
Jaegeuk Kim9248dba2018-12-13 18:38:33 -08003124 kvfree(nm_i->nat_bitmap);
3125 kvfree(nm_i->nat_bits);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003126#ifdef CONFIG_F2FS_CHECK_FS
Jaegeuk Kim9248dba2018-12-13 18:38:33 -08003127 kvfree(nm_i->nat_bitmap_mir);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07003128#endif
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003129 sbi->nm_info = NULL;
Jaegeuk Kim9248dba2018-12-13 18:38:33 -08003130 kvfree(nm_i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003131}
3132
Chao Yu6b4d6a82018-05-30 00:20:41 +08003133int __init f2fs_create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003134{
3135 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08003136 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003137 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08003138 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003139
3140 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08003141 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08003142 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003143 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08003144
3145 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
3146 sizeof(struct nat_entry_set));
3147 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01003148 goto destroy_free_nid;
Chao Yu3d5c54a2018-08-02 23:03:19 +08003149
3150 fsync_node_entry_slab = f2fs_kmem_cache_create("fsync_node_entry",
3151 sizeof(struct fsync_node_entry));
3152 if (!fsync_node_entry_slab)
3153 goto destroy_nat_entry_set;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003154 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08003155
Chao Yu3d5c54a2018-08-02 23:03:19 +08003156destroy_nat_entry_set:
3157 kmem_cache_destroy(nat_entry_set_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003158destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08003159 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01003160destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08003161 kmem_cache_destroy(nat_entry_slab);
3162fail:
3163 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003164}
3165
Chao Yu6b4d6a82018-05-30 00:20:41 +08003166void f2fs_destroy_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003167{
Chao Yu3d5c54a2018-08-02 23:03:19 +08003168 kmem_cache_destroy(fsync_node_entry_slab);
Chao Yuaec71382014-06-24 09:18:20 +08003169 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09003170 kmem_cache_destroy(free_nid_slab);
3171 kmem_cache_destroy(nat_entry_slab);
3172}