blob: 880d5781b1b39a20eb3e7cd5282913f609e75509 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002 * fs/f2fs/node.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/mpage.h>
14#include <linux/backing-dev.h>
15#include <linux/blkdev.h>
16#include <linux/pagevec.h>
17#include <linux/swap.h>
18
19#include "f2fs.h"
20#include "node.h"
21#include "segment.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080022#include "trace.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090023#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090024
Gu Zhengf978f5a2014-02-21 18:08:29 +080025#define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
26
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090027static struct kmem_cache *nat_entry_slab;
28static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080029static struct kmem_cache *nat_entry_set_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090030
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090031bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090032{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090033 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090034 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080035 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090036 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090037 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090038
39 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080040
41 /* only uses low memory */
42 avail_ram = val.totalram - val.totalhigh;
43
Chao Yu429511c2015-02-05 17:54:31 +080044 /*
45 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
46 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090047 if (type == FREE_NIDS) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080048 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
49 PAGE_CACHE_SHIFT;
50 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090051 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080052 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
53 PAGE_CACHE_SHIFT;
54 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090055 } else if (type == DIRTY_DENTS) {
Jaegeuk Kim2743f862014-06-28 01:00:41 +090056 if (sbi->sb->s_bdi->dirty_exceeded)
57 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090058 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080059 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
60 } else if (type == INO_ENTRIES) {
61 int i;
62
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080063 for (i = 0; i <= UPDATE_INO; i++)
Chao Yu67298802014-11-18 11:18:36 +080064 mem_size += (sbi->im[i].ino_num *
65 sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080066 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080067 } else if (type == EXTENT_CACHE) {
68 mem_size = (sbi->total_ext_tree * sizeof(struct extent_tree) +
69 atomic_read(&sbi->total_ext_node) *
70 sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT;
71 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080072 } else {
73 if (sbi->sb->s_bdi->dirty_exceeded)
74 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090075 }
76 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090077}
78
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090079static void clear_node_page_dirty(struct page *page)
80{
81 struct address_space *mapping = page->mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090082 unsigned int long flags;
83
84 if (PageDirty(page)) {
85 spin_lock_irqsave(&mapping->tree_lock, flags);
86 radix_tree_tag_clear(&mapping->page_tree,
87 page_index(page),
88 PAGECACHE_TAG_DIRTY);
89 spin_unlock_irqrestore(&mapping->tree_lock, flags);
90
91 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -070092 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090093 }
94 ClearPageUptodate(page);
95}
96
97static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
98{
99 pgoff_t index = current_nat_addr(sbi, nid);
100 return get_meta_page(sbi, index);
101}
102
103static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
104{
105 struct page *src_page;
106 struct page *dst_page;
107 pgoff_t src_off;
108 pgoff_t dst_off;
109 void *src_addr;
110 void *dst_addr;
111 struct f2fs_nm_info *nm_i = NM_I(sbi);
112
113 src_off = current_nat_addr(sbi, nid);
114 dst_off = next_nat_addr(sbi, src_off);
115
116 /* get current nat block page with lock */
117 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900118 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700119 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900120
121 src_addr = page_address(src_page);
122 dst_addr = page_address(dst_page);
123 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
124 set_page_dirty(dst_page);
125 f2fs_put_page(src_page, 1);
126
127 set_to_next_nat(nm_i, nid);
128
129 return dst_page;
130}
131
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900132static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
133{
134 return radix_tree_lookup(&nm_i->nat_root, n);
135}
136
137static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
138 nid_t start, unsigned int nr, struct nat_entry **ep)
139{
140 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
141}
142
143static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
144{
145 list_del(&e->list);
146 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
147 nm_i->nat_cnt--;
148 kmem_cache_free(nat_entry_slab, e);
149}
150
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700151static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
152 struct nat_entry *ne)
153{
154 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
155 struct nat_entry_set *head;
156
157 if (get_nat_flag(ne, IS_DIRTY))
158 return;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800159
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700160 head = radix_tree_lookup(&nm_i->nat_set_root, set);
161 if (!head) {
162 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
163
164 INIT_LIST_HEAD(&head->entry_list);
165 INIT_LIST_HEAD(&head->set_list);
166 head->set = set;
167 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800168 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700169 }
170 list_move_tail(&ne->list, &head->entry_list);
171 nm_i->dirty_nat_cnt++;
172 head->entry_cnt++;
173 set_nat_flag(ne, IS_DIRTY, true);
174}
175
176static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
177 struct nat_entry *ne)
178{
Changman Lee20d047c2014-11-25 12:44:23 +0900179 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700180 struct nat_entry_set *head;
181
182 head = radix_tree_lookup(&nm_i->nat_set_root, set);
183 if (head) {
184 list_move_tail(&ne->list, &nm_i->nat_entries);
185 set_nat_flag(ne, IS_DIRTY, false);
186 head->entry_cnt--;
187 nm_i->dirty_nat_cnt--;
188 }
189}
190
191static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
192 nid_t start, unsigned int nr, struct nat_entry_set **ep)
193{
194 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
195 start, nr);
196}
197
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700198bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900199{
200 struct f2fs_nm_info *nm_i = NM_I(sbi);
201 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700202 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900203
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800204 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900205 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700206 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700207 is_cp = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800208 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900209 return is_cp;
210}
211
Chao Yu2aa7c512015-04-18 18:06:49 +0800212static bool has_fsynced_inode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900213{
214 struct f2fs_nm_info *nm_i = NM_I(sbi);
215 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700216 bool fsynced = false;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900217
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800218 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700219 e = __lookup_nat_cache(nm_i, ino);
220 if (e && get_nat_flag(e, HAS_FSYNCED_INODE))
221 fsynced = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800222 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700223 return fsynced;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900224}
225
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700226bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900227{
228 struct f2fs_nm_info *nm_i = NM_I(sbi);
229 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700230 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900231
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800232 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700233 e = __lookup_nat_cache(nm_i, ino);
234 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
235 (get_nat_flag(e, IS_CHECKPOINTED) ||
236 get_nat_flag(e, HAS_FSYNCED_INODE)))
237 need_update = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800238 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700239 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900240}
241
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900242static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
243{
244 struct nat_entry *new;
245
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800246 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
247 f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900248 memset(new, 0, sizeof(struct nat_entry));
249 nat_set_nid(new, nid);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700250 nat_reset_flag(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900251 list_add_tail(&new->list, &nm_i->nat_entries);
252 nm_i->nat_cnt++;
253 return new;
254}
255
256static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
257 struct f2fs_nat_entry *ne)
258{
259 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800260
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800261 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900262 e = __lookup_nat_cache(nm_i, nid);
263 if (!e) {
264 e = grab_nat_entry(nm_i, nid);
Chao Yu94dac222014-04-17 10:51:05 +0800265 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900266 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800267 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900268}
269
270static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900271 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900272{
273 struct f2fs_nm_info *nm_i = NM_I(sbi);
274 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800275
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800276 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900277 e = __lookup_nat_cache(nm_i, ni->nid);
278 if (!e) {
279 e = grab_nat_entry(nm_i, ni->nid);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800280 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700281 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900282 } else if (new_blkaddr == NEW_ADDR) {
283 /*
284 * when nid is reallocated,
285 * previous nat entry can be remained in nat cache.
286 * So, reinitialize it with new information.
287 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800288 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700289 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900290 }
291
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900292 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700293 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
294 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900295 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700296 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900297 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700298 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900299 nat_get_blkaddr(e) != NULL_ADDR &&
300 new_blkaddr == NEW_ADDR);
301
arter97e1c42042014-08-06 23:22:50 +0900302 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900303 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
304 unsigned char version = nat_get_version(e);
305 nat_set_version(e, inc_node_version(version));
306 }
307
308 /* change address */
309 nat_set_blkaddr(e, new_blkaddr);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700310 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
311 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900312 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900313
314 /* update fsync_mark if its inode nat entry is still alive */
315 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700316 if (e) {
317 if (fsync_done && ni->nid == ni->ino)
318 set_nat_flag(e, HAS_FSYNCED_INODE, true);
319 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
320 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800321 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900322}
323
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900324int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900325{
326 struct f2fs_nm_info *nm_i = NM_I(sbi);
327
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900328 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900329 return 0;
330
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800331 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900332 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
333 struct nat_entry *ne;
334 ne = list_first_entry(&nm_i->nat_entries,
335 struct nat_entry, list);
336 __del_from_nat_cache(nm_i, ne);
337 nr_shrink--;
338 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800339 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900340 return nr_shrink;
341}
342
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900343/*
arter97e1c42042014-08-06 23:22:50 +0900344 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900345 */
346void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
347{
348 struct f2fs_nm_info *nm_i = NM_I(sbi);
349 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
350 struct f2fs_summary_block *sum = curseg->sum_blk;
351 nid_t start_nid = START_NID(nid);
352 struct f2fs_nat_block *nat_blk;
353 struct page *page = NULL;
354 struct f2fs_nat_entry ne;
355 struct nat_entry *e;
356 int i;
357
358 ni->nid = nid;
359
360 /* Check nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800361 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900362 e = __lookup_nat_cache(nm_i, nid);
363 if (e) {
364 ni->ino = nat_get_ino(e);
365 ni->blk_addr = nat_get_blkaddr(e);
366 ni->version = nat_get_version(e);
367 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800368 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900369 if (e)
370 return;
371
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800372 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
373
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900374 /* Check current segment summary */
375 mutex_lock(&curseg->curseg_mutex);
376 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
377 if (i >= 0) {
378 ne = nat_in_journal(sum, i);
379 node_info_from_raw_nat(ni, &ne);
380 }
381 mutex_unlock(&curseg->curseg_mutex);
382 if (i >= 0)
383 goto cache;
384
385 /* Fill node_info from nat page */
386 page = get_current_nat_page(sbi, start_nid);
387 nat_blk = (struct f2fs_nat_block *)page_address(page);
388 ne = nat_blk->entries[nid - start_nid];
389 node_info_from_raw_nat(ni, &ne);
390 f2fs_put_page(page, 1);
391cache:
392 /* cache nat entry */
393 cache_nat_entry(NM_I(sbi), nid, &ne);
394}
395
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900396/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900397 * The maximum depth is four.
398 * Offset[0] will have raw inode offset.
399 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900400static int get_node_path(struct f2fs_inode_info *fi, long block,
401 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900402{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900403 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900404 const long direct_blks = ADDRS_PER_BLOCK;
405 const long dptrs_per_blk = NIDS_PER_BLOCK;
406 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
407 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
408 int n = 0;
409 int level = 0;
410
411 noffset[0] = 0;
412
413 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900414 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900415 goto got;
416 }
417 block -= direct_index;
418 if (block < direct_blks) {
419 offset[n++] = NODE_DIR1_BLOCK;
420 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900421 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900422 level = 1;
423 goto got;
424 }
425 block -= direct_blks;
426 if (block < direct_blks) {
427 offset[n++] = NODE_DIR2_BLOCK;
428 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900429 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900430 level = 1;
431 goto got;
432 }
433 block -= direct_blks;
434 if (block < indirect_blks) {
435 offset[n++] = NODE_IND1_BLOCK;
436 noffset[n] = 3;
437 offset[n++] = block / direct_blks;
438 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900439 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900440 level = 2;
441 goto got;
442 }
443 block -= indirect_blks;
444 if (block < indirect_blks) {
445 offset[n++] = NODE_IND2_BLOCK;
446 noffset[n] = 4 + dptrs_per_blk;
447 offset[n++] = block / direct_blks;
448 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900449 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900450 level = 2;
451 goto got;
452 }
453 block -= indirect_blks;
454 if (block < dindirect_blks) {
455 offset[n++] = NODE_DIND_BLOCK;
456 noffset[n] = 5 + (dptrs_per_blk * 2);
457 offset[n++] = block / indirect_blks;
458 noffset[n] = 6 + (dptrs_per_blk * 2) +
459 offset[n - 1] * (dptrs_per_blk + 1);
460 offset[n++] = (block / direct_blks) % dptrs_per_blk;
461 noffset[n] = 7 + (dptrs_per_blk * 2) +
462 offset[n - 2] * (dptrs_per_blk + 1) +
463 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900464 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900465 level = 3;
466 goto got;
467 } else {
468 BUG();
469 }
470got:
471 return level;
472}
473
474/*
475 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800476 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
477 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900478 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900479 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900480int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900481{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700482 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900483 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800484 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900485 int offset[4];
486 unsigned int noffset[4];
487 nid_t nids[4];
488 int level, i;
489 int err = 0;
490
Jaegeuk Kimde936532013-08-12 21:08:03 +0900491 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900492
493 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900494 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900495
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900496 if (!npage[0]) {
497 npage[0] = get_node_page(sbi, nids[0]);
498 if (IS_ERR(npage[0]))
499 return PTR_ERR(npage[0]);
500 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800501
502 /* if inline_data is set, should not report any block indices */
503 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800504 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800505 f2fs_put_page(npage[0], 1);
506 goto release_out;
507 }
508
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900509 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900510 if (level != 0)
511 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900512 dn->inode_page = npage[0];
513 dn->inode_page_locked = true;
514
515 /* get indirect or direct nodes */
516 for (i = 1; i <= level; i++) {
517 bool done = false;
518
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900519 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900520 /* alloc new node */
521 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900522 err = -ENOSPC;
523 goto release_pages;
524 }
525
526 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900527 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900528 if (IS_ERR(npage[i])) {
529 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900530 err = PTR_ERR(npage[i]);
531 goto release_pages;
532 }
533
534 set_nid(parent, offset[i - 1], nids[i], i == 1);
535 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900536 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900537 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900538 npage[i] = get_node_page_ra(parent, offset[i - 1]);
539 if (IS_ERR(npage[i])) {
540 err = PTR_ERR(npage[i]);
541 goto release_pages;
542 }
543 done = true;
544 }
545 if (i == 1) {
546 dn->inode_page_locked = false;
547 unlock_page(parent);
548 } else {
549 f2fs_put_page(parent, 1);
550 }
551
552 if (!done) {
553 npage[i] = get_node_page(sbi, nids[i]);
554 if (IS_ERR(npage[i])) {
555 err = PTR_ERR(npage[i]);
556 f2fs_put_page(npage[0], 0);
557 goto release_out;
558 }
559 }
560 if (i < level) {
561 parent = npage[i];
562 nids[i + 1] = get_nid(parent, offset[i], false);
563 }
564 }
565 dn->nid = nids[level];
566 dn->ofs_in_node = offset[level];
567 dn->node_page = npage[level];
568 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
569 return 0;
570
571release_pages:
572 f2fs_put_page(parent, 1);
573 if (i > 1)
574 f2fs_put_page(npage[0], 0);
575release_out:
576 dn->inode_page = NULL;
577 dn->node_page = NULL;
578 return err;
579}
580
581static void truncate_node(struct dnode_of_data *dn)
582{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700583 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900584 struct node_info ni;
585
586 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900587 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700588 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900589 goto invalidate;
590 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700591 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900592
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900593 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900594 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800595 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900596 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900597
598 if (dn->nid == dn->inode->i_ino) {
599 remove_orphan_inode(sbi, dn->nid);
600 dec_valid_inode_count(sbi);
601 } else {
602 sync_inode_page(dn);
603 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900604invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900605 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800606 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900607
608 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900609
610 invalidate_mapping_pages(NODE_MAPPING(sbi),
611 dn->node_page->index, dn->node_page->index);
612
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900613 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900614 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900615}
616
617static int truncate_dnode(struct dnode_of_data *dn)
618{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900619 struct page *page;
620
621 if (dn->nid == 0)
622 return 1;
623
624 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700625 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900626 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
627 return 1;
628 else if (IS_ERR(page))
629 return PTR_ERR(page);
630
631 /* Make dnode_of_data for parameter */
632 dn->node_page = page;
633 dn->ofs_in_node = 0;
634 truncate_data_blocks(dn);
635 truncate_node(dn);
636 return 1;
637}
638
639static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
640 int ofs, int depth)
641{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900642 struct dnode_of_data rdn = *dn;
643 struct page *page;
644 struct f2fs_node *rn;
645 nid_t child_nid;
646 unsigned int child_nofs;
647 int freed = 0;
648 int i, ret;
649
650 if (dn->nid == 0)
651 return NIDS_PER_BLOCK + 1;
652
Namjae Jeon51dd6242013-04-20 01:28:52 +0900653 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
654
Jaegeuk Kim40813632014-09-02 15:31:18 -0700655 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900656 if (IS_ERR(page)) {
657 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900658 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900659 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900660
Gu Zheng45590712013-07-15 17:57:38 +0800661 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900662 if (depth < 3) {
663 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
664 child_nid = le32_to_cpu(rn->in.nid[i]);
665 if (child_nid == 0)
666 continue;
667 rdn.nid = child_nid;
668 ret = truncate_dnode(&rdn);
669 if (ret < 0)
670 goto out_err;
671 set_nid(page, i, 0, false);
672 }
673 } else {
674 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
675 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
676 child_nid = le32_to_cpu(rn->in.nid[i]);
677 if (child_nid == 0) {
678 child_nofs += NIDS_PER_BLOCK + 1;
679 continue;
680 }
681 rdn.nid = child_nid;
682 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
683 if (ret == (NIDS_PER_BLOCK + 1)) {
684 set_nid(page, i, 0, false);
685 child_nofs += ret;
686 } else if (ret < 0 && ret != -ENOENT) {
687 goto out_err;
688 }
689 }
690 freed = child_nofs;
691 }
692
693 if (!ofs) {
694 /* remove current indirect node */
695 dn->node_page = page;
696 truncate_node(dn);
697 freed++;
698 } else {
699 f2fs_put_page(page, 1);
700 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900701 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900702 return freed;
703
704out_err:
705 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900706 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900707 return ret;
708}
709
710static int truncate_partial_nodes(struct dnode_of_data *dn,
711 struct f2fs_inode *ri, int *offset, int depth)
712{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900713 struct page *pages[2];
714 nid_t nid[3];
715 nid_t child_nid;
716 int err = 0;
717 int i;
718 int idx = depth - 2;
719
720 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
721 if (!nid[0])
722 return 0;
723
724 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800725 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900726 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700727 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900728 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900729 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800730 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900731 goto fail;
732 }
733 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
734 }
735
736 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800737 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900738 child_nid = get_nid(pages[idx], i, false);
739 if (!child_nid)
740 continue;
741 dn->nid = child_nid;
742 err = truncate_dnode(dn);
743 if (err < 0)
744 goto fail;
745 set_nid(pages[idx], i, 0, false);
746 }
747
shifei10.gea225dca2013-10-29 15:32:34 +0800748 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900749 dn->node_page = pages[idx];
750 dn->nid = nid[idx];
751 truncate_node(dn);
752 } else {
753 f2fs_put_page(pages[idx], 1);
754 }
755 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800756 offset[idx + 1] = 0;
757 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900758fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800759 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900760 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900761
762 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
763
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900764 return err;
765}
766
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900767/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900768 * All the block addresses of data and nodes should be nullified.
769 */
770int truncate_inode_blocks(struct inode *inode, pgoff_t from)
771{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700772 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900773 int err = 0, cont = 1;
774 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900775 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900776 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900777 struct dnode_of_data dn;
778 struct page *page;
779
Namjae Jeon51dd6242013-04-20 01:28:52 +0900780 trace_f2fs_truncate_inode_blocks_enter(inode, from);
781
Jaegeuk Kimde936532013-08-12 21:08:03 +0900782 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900783restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900784 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900785 if (IS_ERR(page)) {
786 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900787 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900788 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900789
790 set_new_dnode(&dn, inode, page, NULL, 0);
791 unlock_page(page);
792
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900793 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900794 switch (level) {
795 case 0:
796 case 1:
797 nofs = noffset[1];
798 break;
799 case 2:
800 nofs = noffset[1];
801 if (!offset[level - 1])
802 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900803 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900804 if (err < 0 && err != -ENOENT)
805 goto fail;
806 nofs += 1 + NIDS_PER_BLOCK;
807 break;
808 case 3:
809 nofs = 5 + 2 * NIDS_PER_BLOCK;
810 if (!offset[level - 1])
811 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900812 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900813 if (err < 0 && err != -ENOENT)
814 goto fail;
815 break;
816 default:
817 BUG();
818 }
819
820skip_partial:
821 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900822 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900823 switch (offset[0]) {
824 case NODE_DIR1_BLOCK:
825 case NODE_DIR2_BLOCK:
826 err = truncate_dnode(&dn);
827 break;
828
829 case NODE_IND1_BLOCK:
830 case NODE_IND2_BLOCK:
831 err = truncate_nodes(&dn, nofs, offset[1], 2);
832 break;
833
834 case NODE_DIND_BLOCK:
835 err = truncate_nodes(&dn, nofs, offset[1], 3);
836 cont = 0;
837 break;
838
839 default:
840 BUG();
841 }
842 if (err < 0 && err != -ENOENT)
843 goto fail;
844 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900845 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900846 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900847 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900848 f2fs_put_page(page, 1);
849 goto restart;
850 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900851 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900852 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900853 set_page_dirty(page);
854 unlock_page(page);
855 }
856 offset[1] = 0;
857 offset[0]++;
858 nofs += err;
859 }
860fail:
861 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900862 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900863 return err > 0 ? 0 : err;
864}
865
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900866int truncate_xattr_node(struct inode *inode, struct page *page)
867{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700868 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900869 nid_t nid = F2FS_I(inode)->i_xattr_nid;
870 struct dnode_of_data dn;
871 struct page *npage;
872
873 if (!nid)
874 return 0;
875
876 npage = get_node_page(sbi, nid);
877 if (IS_ERR(npage))
878 return PTR_ERR(npage);
879
880 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900881
882 /* need to do checkpoint during fsync */
883 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
884
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900885 set_new_dnode(&dn, inode, page, npage, nid);
886
887 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800888 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900889 truncate_node(&dn);
890 return 0;
891}
892
Jaegeuk Kim39936832012-11-22 16:21:29 +0900893/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800894 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
895 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900896 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800897void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900898{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900899 struct dnode_of_data dn;
900
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700901 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
902 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800903 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900904
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700905 if (truncate_xattr_node(inode, dn.inode_page)) {
906 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800907 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900908 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700909
910 /* remove potential inline_data blocks */
911 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
912 S_ISLNK(inode->i_mode))
913 truncate_data_blocks_range(&dn, 1);
914
arter97e1c42042014-08-06 23:22:50 +0900915 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700916 f2fs_bug_on(F2FS_I_SB(inode),
917 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700918
919 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900920 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900921}
922
Jaegeuk Kima014e032014-06-20 21:44:02 -0700923struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900924{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900925 struct dnode_of_data dn;
926
927 /* allocate inode page for new inode */
928 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900929
930 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900931 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900932}
933
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900934struct page *new_node_page(struct dnode_of_data *dn,
935 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900936{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700937 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900938 struct node_info old_ni, new_ni;
939 struct page *page;
940 int err;
941
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900942 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900943 return ERR_PTR(-EPERM);
944
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900945 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900946 if (!page)
947 return ERR_PTR(-ENOMEM);
948
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900949 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900950 err = -ENOSPC;
951 goto fail;
952 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900953
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900954 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900955
956 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700957 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900958 new_ni = old_ni;
959 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900960 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900961
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900962 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900963 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900964 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900965 SetPageUptodate(page);
966 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900967
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800968 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900969 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
970
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900971 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900972 if (ipage)
973 update_inode(dn->inode, ipage);
974 else
975 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900976 if (ofs == 0)
977 inc_valid_inode_count(sbi);
978
979 return page;
980
981fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900982 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900983 f2fs_put_page(page, 1);
984 return ERR_PTR(err);
985}
986
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900987/*
988 * Caller should do after getting the following values.
989 * 0: f2fs_put_page(page, 0)
990 * LOCKED_PAGE: f2fs_put_page(page, 1)
991 * error: nothing
992 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900993static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900994{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700995 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900996 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800997 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700998 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800999 .type = NODE,
1000 .rw = rw,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001001 .page = page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001002 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001003
1004 get_node_info(sbi, page->index, &ni);
1005
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001006 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001007 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001008 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001009 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001010 }
1011
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001012 if (PageUptodate(page))
1013 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001014
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001015 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001016 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017}
1018
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001019/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001020 * Readahead a node page
1021 */
1022void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1023{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001024 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001025 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001026
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001027 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001028 if (apage && PageUptodate(apage)) {
1029 f2fs_put_page(apage, 0);
1030 return;
1031 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001032 f2fs_put_page(apage, 0);
1033
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001034 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001035 if (!apage)
1036 return;
1037
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001038 err = read_node_page(apage, READA);
1039 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001040 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001041 else if (err == LOCKED_PAGE)
1042 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001043}
1044
1045struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1046{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001047 struct page *page;
1048 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001049repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001050 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001051 if (!page)
1052 return ERR_PTR(-ENOMEM);
1053
1054 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001055 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001056 return ERR_PTR(err);
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001057 else if (err != LOCKED_PAGE)
1058 lock_page(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001059
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001060 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001061 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001062 f2fs_put_page(page, 1);
1063 return ERR_PTR(-EIO);
1064 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001065 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001066 f2fs_put_page(page, 1);
1067 goto repeat;
1068 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001069 return page;
1070}
1071
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001072/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001073 * Return a locked page for the desired node page.
1074 * And, readahead MAX_RA_NODE number of node pages.
1075 */
1076struct page *get_node_page_ra(struct page *parent, int start)
1077{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001078 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001079 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001080 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001081 int err, i, end;
1082 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001083
1084 /* First, try getting the desired direct node. */
1085 nid = get_nid(parent, start, false);
1086 if (!nid)
1087 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001088repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001089 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001090 if (!page)
1091 return ERR_PTR(-ENOMEM);
1092
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001093 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001094 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001095 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001096 else if (err == LOCKED_PAGE)
1097 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001098
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001099 blk_start_plug(&plug);
1100
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001101 /* Then, try readahead for siblings of the desired node */
1102 end = start + MAX_RA_NODE;
1103 end = min(end, NIDS_PER_BLOCK);
1104 for (i = start + 1; i < end; i++) {
1105 nid = get_nid(parent, i, false);
1106 if (!nid)
1107 continue;
1108 ra_node_page(sbi, nid);
1109 }
1110
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001111 blk_finish_plug(&plug);
1112
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001113 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001114 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001115 f2fs_put_page(page, 1);
1116 goto repeat;
1117 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001118page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001119 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001120 f2fs_put_page(page, 1);
1121 return ERR_PTR(-EIO);
1122 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001123 return page;
1124}
1125
1126void sync_inode_page(struct dnode_of_data *dn)
1127{
1128 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1129 update_inode(dn->inode, dn->node_page);
1130 } else if (dn->inode_page) {
1131 if (!dn->inode_page_locked)
1132 lock_page(dn->inode_page);
1133 update_inode(dn->inode, dn->inode_page);
1134 if (!dn->inode_page_locked)
1135 unlock_page(dn->inode_page);
1136 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001137 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001138 }
1139}
1140
1141int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1142 struct writeback_control *wbc)
1143{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001144 pgoff_t index, end;
1145 struct pagevec pvec;
1146 int step = ino ? 2 : 0;
1147 int nwritten = 0, wrote = 0;
1148
1149 pagevec_init(&pvec, 0);
1150
1151next_step:
1152 index = 0;
1153 end = LONG_MAX;
1154
1155 while (index <= end) {
1156 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001157 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001158 PAGECACHE_TAG_DIRTY,
1159 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1160 if (nr_pages == 0)
1161 break;
1162
1163 for (i = 0; i < nr_pages; i++) {
1164 struct page *page = pvec.pages[i];
1165
1166 /*
1167 * flushing sequence with step:
1168 * 0. indirect nodes
1169 * 1. dentry dnodes
1170 * 2. file dnodes
1171 */
1172 if (step == 0 && IS_DNODE(page))
1173 continue;
1174 if (step == 1 && (!IS_DNODE(page) ||
1175 is_cold_node(page)))
1176 continue;
1177 if (step == 2 && (!IS_DNODE(page) ||
1178 !is_cold_node(page)))
1179 continue;
1180
1181 /*
1182 * If an fsync mode,
1183 * we should not skip writing node pages.
1184 */
1185 if (ino && ino_of_node(page) == ino)
1186 lock_page(page);
1187 else if (!trylock_page(page))
1188 continue;
1189
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001190 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001191continue_unlock:
1192 unlock_page(page);
1193 continue;
1194 }
1195 if (ino && ino_of_node(page) != ino)
1196 goto continue_unlock;
1197
1198 if (!PageDirty(page)) {
1199 /* someone wrote it for us */
1200 goto continue_unlock;
1201 }
1202
1203 if (!clear_page_dirty_for_io(page))
1204 goto continue_unlock;
1205
1206 /* called by fsync() */
1207 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001208 set_fsync_mark(page, 1);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -07001209 if (IS_INODE(page)) {
1210 if (!is_checkpointed_node(sbi, ino) &&
1211 !has_fsynced_inode(sbi, ino))
1212 set_dentry_mark(page, 1);
1213 else
1214 set_dentry_mark(page, 0);
1215 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001216 nwritten++;
1217 } else {
1218 set_fsync_mark(page, 0);
1219 set_dentry_mark(page, 0);
1220 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001221
1222 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1223 unlock_page(page);
1224 else
1225 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001226
1227 if (--wbc->nr_to_write == 0)
1228 break;
1229 }
1230 pagevec_release(&pvec);
1231 cond_resched();
1232
1233 if (wbc->nr_to_write == 0) {
1234 step = 2;
1235 break;
1236 }
1237 }
1238
1239 if (step < 2) {
1240 step++;
1241 goto next_step;
1242 }
1243
1244 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001245 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001246 return nwritten;
1247}
1248
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001249int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1250{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001251 pgoff_t index = 0, end = LONG_MAX;
1252 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001253 int ret2 = 0, ret = 0;
1254
1255 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001256
1257 while (index <= end) {
1258 int i, nr_pages;
1259 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1260 PAGECACHE_TAG_WRITEBACK,
1261 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1262 if (nr_pages == 0)
1263 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001264
1265 for (i = 0; i < nr_pages; i++) {
1266 struct page *page = pvec.pages[i];
1267
1268 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001269 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001270 continue;
1271
Chao Yu4bf08ff2013-11-04 10:28:33 +08001272 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001273 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001274 if (TestClearPageError(page))
1275 ret = -EIO;
1276 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001277 }
1278 pagevec_release(&pvec);
1279 cond_resched();
1280 }
1281
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001282 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001283 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001284 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001285 ret2 = -EIO;
1286 if (!ret)
1287 ret = ret2;
1288 return ret;
1289}
1290
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291static int f2fs_write_node_page(struct page *page,
1292 struct writeback_control *wbc)
1293{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001294 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001295 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001296 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001297 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001298 .sbi = sbi,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001299 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001300 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001301 .page = page,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001302 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001303
Chao Yuecda0de2014-05-06 16:48:26 +08001304 trace_f2fs_writepage(page, NODE);
1305
Chao Yucaf00472015-01-28 17:48:42 +08001306 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001307 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001308 if (unlikely(f2fs_cp_error(sbi)))
1309 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001310
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001311 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001312
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001313 /* get old block addr of this node page */
1314 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001315 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001316
1317 get_node_info(sbi, nid, &ni);
1318
1319 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001320 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001321 ClearPageUptodate(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001322 dec_page_count(sbi, F2FS_DIRTY_NODES);
1323 unlock_page(page);
1324 return 0;
1325 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001326
Jaegeuk Kim2f97c322014-11-06 17:21:24 -08001327 if (wbc->for_reclaim) {
1328 if (!down_read_trylock(&sbi->node_write))
1329 goto redirty_out;
1330 } else {
1331 down_read(&sbi->node_write);
1332 }
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001333
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001334 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001335 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001336 write_node_page(nid, &fio);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001337 set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001338 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001339 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001340 unlock_page(page);
Jaegeuk Kim27c6bd62014-11-19 10:54:48 -08001341
1342 if (wbc->for_reclaim)
1343 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1344
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001345 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001346
1347redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001348 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001349 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001350}
1351
1352static int f2fs_write_node_pages(struct address_space *mapping,
1353 struct writeback_control *wbc)
1354{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001355 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001356 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001357
Chao Yue5748432014-05-06 16:51:24 +08001358 trace_f2fs_writepages(mapping->host, wbc, NODE);
1359
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001360 /* balancing f2fs's metadata in background */
1361 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001362
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001363 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001364 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001365 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001366
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001367 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001368 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001369 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001370 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001371 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001372
1373skip_write:
1374 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1375 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001376}
1377
1378static int f2fs_set_node_page_dirty(struct page *page)
1379{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001380 trace_f2fs_set_page_dirty(page, NODE);
1381
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001382 SetPageUptodate(page);
1383 if (!PageDirty(page)) {
1384 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001385 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001387 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001388 return 1;
1389 }
1390 return 0;
1391}
1392
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001393/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001394 * Structure of the f2fs node operations
1395 */
1396const struct address_space_operations f2fs_node_aops = {
1397 .writepage = f2fs_write_node_page,
1398 .writepages = f2fs_write_node_pages,
1399 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001400 .invalidatepage = f2fs_invalidate_page,
1401 .releasepage = f2fs_release_page,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001402};
1403
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001404static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1405 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001406{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001407 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001408}
1409
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001410static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1411 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001412{
1413 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001414 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001415}
1416
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001417static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001418{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001419 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001421 struct nat_entry *ne;
1422 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001423
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001424 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001425 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001426
1427 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001428 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001429 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001430
Gu Zheng7bd59382013-10-22 14:52:26 +08001431 if (build) {
1432 /* do not add allocated nids */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001433 down_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001434 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001435 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001436 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1437 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001438 allocated = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001439 up_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001440 if (allocated)
1441 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001442 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001443
1444 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001445 i->nid = nid;
1446 i->state = NID_NEW;
1447
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001448 if (radix_tree_preload(GFP_NOFS)) {
1449 kmem_cache_free(free_nid_slab, i);
1450 return 0;
1451 }
1452
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001453 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001454 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001455 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001456 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001457 kmem_cache_free(free_nid_slab, i);
1458 return 0;
1459 }
1460 list_add_tail(&i->list, &nm_i->free_nid_list);
1461 nm_i->fcnt++;
1462 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001463 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001464 return 1;
1465}
1466
1467static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1468{
1469 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001470 bool need_free = false;
1471
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001472 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001473 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001474 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001475 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001476 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001477 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001478 }
1479 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001480
1481 if (need_free)
1482 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001483}
1484
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001485static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001486 struct page *nat_page, nid_t start_nid)
1487{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001488 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001489 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1490 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001491 int i;
1492
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001493 i = start_nid % NAT_ENTRY_PER_BLOCK;
1494
1495 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001496
Chao Yucfb271d2013-12-05 17:15:22 +08001497 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001498 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001499
1500 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001501 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001502 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001503 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001504 break;
1505 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001506 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001507}
1508
1509static void build_free_nids(struct f2fs_sb_info *sbi)
1510{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001511 struct f2fs_nm_info *nm_i = NM_I(sbi);
1512 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1513 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001514 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001515 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001516
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001517 /* Enough entries */
1518 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1519 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001520
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001521 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001522 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001523
1524 while (1) {
1525 struct page *page = get_current_nat_page(sbi, nid);
1526
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001527 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001528 f2fs_put_page(page, 1);
1529
1530 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001531 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001532 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001533
1534 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001535 break;
1536 }
1537
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001538 /* go to the next free nat pages to find free nids abundantly */
1539 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001540
1541 /* find free nids from current sum_pages */
1542 mutex_lock(&curseg->curseg_mutex);
1543 for (i = 0; i < nats_in_cursum(sum); i++) {
1544 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1545 nid = le32_to_cpu(nid_in_journal(sum, i));
1546 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001547 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001548 else
1549 remove_free_nid(nm_i, nid);
1550 }
1551 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001552}
1553
1554/*
1555 * If this function returns success, caller can obtain a new nid
1556 * from second parameter of this function.
1557 * The returned nid could be used ino as well as nid when inode is created.
1558 */
1559bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1560{
1561 struct f2fs_nm_info *nm_i = NM_I(sbi);
1562 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001563retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001564 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001565 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001566
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001567 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001568
1569 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001570 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001571 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001572 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001573 if (i->state == NID_NEW)
1574 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001575
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001576 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001577 *nid = i->nid;
1578 i->state = NID_ALLOC;
1579 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001580 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001581 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001582 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001583 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001584
1585 /* Let's scan nat pages and its caches to get free nids */
1586 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001587 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001588 mutex_unlock(&nm_i->build_lock);
1589 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001590}
1591
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001592/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001593 * alloc_nid() should be called prior to this function.
1594 */
1595void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1596{
1597 struct f2fs_nm_info *nm_i = NM_I(sbi);
1598 struct free_nid *i;
1599
1600 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001601 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001602 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001603 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001604 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001605
1606 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001607}
1608
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001609/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001610 * alloc_nid() should be called prior to this function.
1611 */
1612void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1613{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001614 struct f2fs_nm_info *nm_i = NM_I(sbi);
1615 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001616 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001617
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001618 if (!nid)
1619 return;
1620
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001621 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001622 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001623 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001624 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001625 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001626 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001627 } else {
1628 i->state = NID_NEW;
1629 nm_i->fcnt++;
1630 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001631 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001632
1633 if (need_free)
1634 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001635}
1636
Chao Yu70cfed82014-08-02 15:26:04 +08001637void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001638{
Chao Yu28cdce02014-03-11 13:37:38 +08001639 void *src_addr, *dst_addr;
1640 size_t inline_size;
1641 struct page *ipage;
1642 struct f2fs_inode *ri;
1643
Jaegeuk Kim40813632014-09-02 15:31:18 -07001644 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001645 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001646
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001647 ri = F2FS_INODE(page);
1648 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1649 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1650 goto update_inode;
1651 }
1652
Chao Yu28cdce02014-03-11 13:37:38 +08001653 dst_addr = inline_xattr_addr(ipage);
1654 src_addr = inline_xattr_addr(page);
1655 inline_size = inline_xattr_size(inode);
1656
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001657 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001658 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001659update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001660 update_inode(inode, ipage);
1661 f2fs_put_page(ipage, 1);
1662}
1663
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001664void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001665{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001666 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001667 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1668 nid_t new_xnid = nid_of_node(page);
1669 struct node_info ni;
1670
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001671 /* 1: invalidate the previous xattr nid */
1672 if (!prev_xnid)
1673 goto recover_xnid;
1674
1675 /* Deallocate node address */
1676 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001677 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001678 invalidate_blocks(sbi, ni.blk_addr);
1679 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001680 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001681
1682recover_xnid:
1683 /* 2: allocate new xattr nid */
1684 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001685 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001686
1687 remove_free_nid(NM_I(sbi), new_xnid);
1688 get_node_info(sbi, new_xnid, &ni);
1689 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001690 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001691 F2FS_I(inode)->i_xattr_nid = new_xnid;
1692
1693 /* 3: update xattr blkaddr */
1694 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001695 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001696
1697 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001698}
1699
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001700int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1701{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001702 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001703 nid_t ino = ino_of_node(page);
1704 struct node_info old_ni, new_ni;
1705 struct page *ipage;
1706
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001707 get_node_info(sbi, ino, &old_ni);
1708
1709 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1710 return -EINVAL;
1711
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001712 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001713 if (!ipage)
1714 return -ENOMEM;
1715
arter97e1c42042014-08-06 23:22:50 +09001716 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001717 remove_free_nid(NM_I(sbi), ino);
1718
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001719 SetPageUptodate(ipage);
1720 fill_node_footer(ipage, ino, ino, 0, true);
1721
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001722 src = F2FS_INODE(page);
1723 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001724
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001725 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1726 dst->i_size = 0;
1727 dst->i_blocks = cpu_to_le64(1);
1728 dst->i_links = cpu_to_le32(1);
1729 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001730 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001731
1732 new_ni = old_ni;
1733 new_ni.ino = ino;
1734
Chao Yucfb271d2013-12-05 17:15:22 +08001735 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001736 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001737 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001738 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001739 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001740 f2fs_put_page(ipage, 1);
1741 return 0;
1742}
1743
1744int restore_node_summary(struct f2fs_sb_info *sbi,
1745 unsigned int segno, struct f2fs_summary_block *sum)
1746{
1747 struct f2fs_node *rn;
1748 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001749 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001750 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yu9ecf4b82014-12-18 18:29:05 +08001751 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001752
1753 /* scan the node segment */
1754 last_offset = sbi->blocks_per_seg;
1755 addr = START_BLOCK(sbi, segno);
1756 sum_entry = &sum->entries[0];
1757
Chao Yu9ecf4b82014-12-18 18:29:05 +08001758 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001759 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001760
arter97e1c42042014-08-06 23:22:50 +09001761 /* readahead node pages */
Chao Yu9ecf4b82014-12-18 18:29:05 +08001762 ra_meta_pages(sbi, addr, nrpages, META_POR);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001763
Chao Yu9ecf4b82014-12-18 18:29:05 +08001764 for (idx = addr; idx < addr + nrpages; idx++) {
1765 struct page *page = get_meta_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08001766
Chao Yu9ecf4b82014-12-18 18:29:05 +08001767 rn = F2FS_NODE(page);
1768 sum_entry->nid = rn->footer.nid;
1769 sum_entry->version = 0;
1770 sum_entry->ofs_in_node = 0;
1771 sum_entry++;
1772 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08001773 }
Chao Yubac4eef2014-05-27 08:41:07 +08001774
Chao Yu9ecf4b82014-12-18 18:29:05 +08001775 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08001776 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001777 }
Chao Yu9ecf4b82014-12-18 18:29:05 +08001778 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001779}
1780
Chao Yuaec71382014-06-24 09:18:20 +08001781static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001782{
1783 struct f2fs_nm_info *nm_i = NM_I(sbi);
1784 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1785 struct f2fs_summary_block *sum = curseg->sum_blk;
1786 int i;
1787
1788 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001789 for (i = 0; i < nats_in_cursum(sum); i++) {
1790 struct nat_entry *ne;
1791 struct f2fs_nat_entry raw_ne;
1792 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1793
1794 raw_ne = nat_in_journal(sum, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001795
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001796 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001797 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001798 if (!ne) {
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001799 ne = grab_nat_entry(nm_i, nid);
1800 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001801 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001802 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001803 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001804 }
1805 update_nats_in_cursum(sum, -i);
1806 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001807}
1808
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001809static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1810 struct list_head *head, int max)
1811{
1812 struct nat_entry_set *cur;
1813
1814 if (nes->entry_cnt >= max)
1815 goto add_out;
1816
1817 list_for_each_entry(cur, head, set_list) {
1818 if (cur->entry_cnt >= nes->entry_cnt) {
1819 list_add(&nes->set_list, cur->set_list.prev);
1820 return;
1821 }
1822 }
1823add_out:
1824 list_add_tail(&nes->set_list, head);
1825}
1826
1827static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1828 struct nat_entry_set *set)
1829{
1830 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1831 struct f2fs_summary_block *sum = curseg->sum_blk;
1832 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1833 bool to_journal = true;
1834 struct f2fs_nat_block *nat_blk;
1835 struct nat_entry *ne, *cur;
1836 struct page *page = NULL;
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001837 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001838
1839 /*
1840 * there are two steps to flush nat entries:
1841 * #1, flush nat entries to journal in current hot data summary block.
1842 * #2, flush nat entries to nat page.
1843 */
1844 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1845 to_journal = false;
1846
1847 if (to_journal) {
1848 mutex_lock(&curseg->curseg_mutex);
1849 } else {
1850 page = get_next_nat_page(sbi, start_nid);
1851 nat_blk = page_address(page);
1852 f2fs_bug_on(sbi, !nat_blk);
1853 }
1854
1855 /* flush dirty nats in nat entry set */
1856 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1857 struct f2fs_nat_entry *raw_ne;
1858 nid_t nid = nat_get_nid(ne);
1859 int offset;
1860
1861 if (nat_get_blkaddr(ne) == NEW_ADDR)
1862 continue;
1863
1864 if (to_journal) {
1865 offset = lookup_journal_in_cursum(sum,
1866 NAT_JOURNAL, nid, 1);
1867 f2fs_bug_on(sbi, offset < 0);
1868 raw_ne = &nat_in_journal(sum, offset);
1869 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1870 } else {
1871 raw_ne = &nat_blk->entries[nid - start_nid];
1872 }
1873 raw_nat_from_node_info(raw_ne, &ne->ni);
1874
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001875 down_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001876 nat_reset_flag(ne);
1877 __clear_nat_cache_dirty(NM_I(sbi), ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001878 up_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001879
1880 if (nat_get_blkaddr(ne) == NULL_ADDR)
1881 add_free_nid(sbi, nid, false);
1882 }
1883
1884 if (to_journal)
1885 mutex_unlock(&curseg->curseg_mutex);
1886 else
1887 f2fs_put_page(page, 1);
1888
Changman Lee80ec2e92014-11-25 12:44:24 +09001889 f2fs_bug_on(sbi, set->entry_cnt);
1890
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001891 down_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001892 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001893 up_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001894 kmem_cache_free(nat_entry_set_slab, set);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001895}
1896
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001897/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001898 * This function is called during the checkpointing process.
1899 */
1900void flush_nat_entries(struct f2fs_sb_info *sbi)
1901{
1902 struct f2fs_nm_info *nm_i = NM_I(sbi);
1903 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1904 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001905 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001906 struct nat_entry_set *set, *tmp;
1907 unsigned int found;
1908 nid_t set_idx = 0;
1909 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001910
Changman Lee20d047c2014-11-25 12:44:23 +09001911 if (!nm_i->dirty_nat_cnt)
1912 return;
Chao Yuaec71382014-06-24 09:18:20 +08001913 /*
1914 * if there are no enough space in journal to store dirty nat
1915 * entries, remove all entries from journal and merge them
1916 * into nat entry set.
1917 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001918 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001919 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001920
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001921 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001922 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001923 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001924 unsigned idx;
1925 set_idx = setvec[found - 1]->set + 1;
1926 for (idx = 0; idx < found; idx++)
1927 __adjust_nat_entry_set(setvec[idx], &sets,
1928 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001929 }
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001930 up_write(&nm_i->nat_tree_lock);
Chao Yuaec71382014-06-24 09:18:20 +08001931
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001932 /* flush dirty nats in nat entry set */
1933 list_for_each_entry_safe(set, tmp, &sets, set_list)
1934 __flush_nat_entry_set(sbi, set);
1935
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001936 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001937}
1938
1939static int init_node_manager(struct f2fs_sb_info *sbi)
1940{
1941 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1942 struct f2fs_nm_info *nm_i = NM_I(sbi);
1943 unsigned char *version_bitmap;
1944 unsigned int nat_segs, nat_blocks;
1945
1946 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1947
1948 /* segment_count_nat includes pair segment so divide to 2. */
1949 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1950 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001951
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001952 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1953
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001954 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001955 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001956 nm_i->fcnt = 0;
1957 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001958 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001959
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001960 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001961 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001962 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
1963 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001964 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001965
1966 mutex_init(&nm_i->build_lock);
1967 spin_lock_init(&nm_i->free_nid_list_lock);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001968 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001969
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001970 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001971 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001972 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1973 if (!version_bitmap)
1974 return -EFAULT;
1975
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001976 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1977 GFP_KERNEL);
1978 if (!nm_i->nat_bitmap)
1979 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001980 return 0;
1981}
1982
1983int build_node_manager(struct f2fs_sb_info *sbi)
1984{
1985 int err;
1986
1987 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1988 if (!sbi->nm_info)
1989 return -ENOMEM;
1990
1991 err = init_node_manager(sbi);
1992 if (err)
1993 return err;
1994
1995 build_free_nids(sbi);
1996 return 0;
1997}
1998
1999void destroy_node_manager(struct f2fs_sb_info *sbi)
2000{
2001 struct f2fs_nm_info *nm_i = NM_I(sbi);
2002 struct free_nid *i, *next_i;
2003 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002004 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002005 nid_t nid = 0;
2006 unsigned int found;
2007
2008 if (!nm_i)
2009 return;
2010
2011 /* destroy free nid list */
2012 spin_lock(&nm_i->free_nid_list_lock);
2013 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002014 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002015 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002016 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002017 spin_unlock(&nm_i->free_nid_list_lock);
2018 kmem_cache_free(free_nid_slab, i);
2019 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002020 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002021 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002022 spin_unlock(&nm_i->free_nid_list_lock);
2023
2024 /* destroy nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002025 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002026 while ((found = __gang_lookup_nat_cache(nm_i,
2027 nid, NATVEC_SIZE, natvec))) {
2028 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002029
Gu Zhengb6ce3912014-03-07 18:43:24 +08002030 nid = nat_get_nid(natvec[found - 1]) + 1;
2031 for (idx = 0; idx < found; idx++)
2032 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002033 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002034 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002035
2036 /* destroy nat set cache */
2037 nid = 0;
2038 while ((found = __gang_lookup_nat_set(nm_i,
2039 nid, SETVEC_SIZE, setvec))) {
2040 unsigned idx;
2041
2042 nid = setvec[found - 1]->set + 1;
2043 for (idx = 0; idx < found; idx++) {
2044 /* entry_cnt is not zero, when cp_error was occurred */
2045 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2046 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2047 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2048 }
2049 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002050 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002051
2052 kfree(nm_i->nat_bitmap);
2053 sbi->nm_info = NULL;
2054 kfree(nm_i);
2055}
2056
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002057int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002058{
2059 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002060 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002061 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002062 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002063
2064 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002065 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002066 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002067 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002068
2069 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2070 sizeof(struct nat_entry_set));
2071 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002072 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002073 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002074
Markus Elfringce3e6d22014-11-24 15:52:00 +01002075destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002076 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002077destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002078 kmem_cache_destroy(nat_entry_slab);
2079fail:
2080 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002081}
2082
2083void destroy_node_manager_caches(void)
2084{
Chao Yuaec71382014-06-24 09:18:20 +08002085 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002086 kmem_cache_destroy(free_nid_slab);
2087 kmem_cache_destroy(nat_entry_slab);
2088}