blob: 05e6faa71cfffd505be7c85e76b89322672ab004 [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
44 /* give 25%, 25%, 50%, 50% memory for each components respectively */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090045 if (type == FREE_NIDS) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080046 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
47 PAGE_CACHE_SHIFT;
48 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090049 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080050 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
51 PAGE_CACHE_SHIFT;
52 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090053 } else if (type == DIRTY_DENTS) {
Jaegeuk Kim2743f862014-06-28 01:00:41 +090054 if (sbi->sb->s_bdi->dirty_exceeded)
55 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090056 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080057 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
58 } else if (type == INO_ENTRIES) {
59 int i;
60
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080061 for (i = 0; i <= UPDATE_INO; i++)
Chao Yu67298802014-11-18 11:18:36 +080062 mem_size += (sbi->im[i].ino_num *
63 sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080064 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080065 } else {
66 if (sbi->sb->s_bdi->dirty_exceeded)
67 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090068 }
69 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090070}
71
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090072static void clear_node_page_dirty(struct page *page)
73{
74 struct address_space *mapping = page->mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090075 unsigned int long flags;
76
77 if (PageDirty(page)) {
78 spin_lock_irqsave(&mapping->tree_lock, flags);
79 radix_tree_tag_clear(&mapping->page_tree,
80 page_index(page),
81 PAGECACHE_TAG_DIRTY);
82 spin_unlock_irqrestore(&mapping->tree_lock, flags);
83
84 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -070085 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090086 }
87 ClearPageUptodate(page);
88}
89
90static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
91{
92 pgoff_t index = current_nat_addr(sbi, nid);
93 return get_meta_page(sbi, index);
94}
95
96static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
97{
98 struct page *src_page;
99 struct page *dst_page;
100 pgoff_t src_off;
101 pgoff_t dst_off;
102 void *src_addr;
103 void *dst_addr;
104 struct f2fs_nm_info *nm_i = NM_I(sbi);
105
106 src_off = current_nat_addr(sbi, nid);
107 dst_off = next_nat_addr(sbi, src_off);
108
109 /* get current nat block page with lock */
110 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900111 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700112 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900113
114 src_addr = page_address(src_page);
115 dst_addr = page_address(dst_page);
116 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
117 set_page_dirty(dst_page);
118 f2fs_put_page(src_page, 1);
119
120 set_to_next_nat(nm_i, nid);
121
122 return dst_page;
123}
124
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900125static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
126{
127 return radix_tree_lookup(&nm_i->nat_root, n);
128}
129
130static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
131 nid_t start, unsigned int nr, struct nat_entry **ep)
132{
133 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
134}
135
136static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
137{
138 list_del(&e->list);
139 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
140 nm_i->nat_cnt--;
141 kmem_cache_free(nat_entry_slab, e);
142}
143
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700144static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
145 struct nat_entry *ne)
146{
147 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
148 struct nat_entry_set *head;
149
150 if (get_nat_flag(ne, IS_DIRTY))
151 return;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800152
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700153 head = radix_tree_lookup(&nm_i->nat_set_root, set);
154 if (!head) {
155 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
156
157 INIT_LIST_HEAD(&head->entry_list);
158 INIT_LIST_HEAD(&head->set_list);
159 head->set = set;
160 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800161 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700162 }
163 list_move_tail(&ne->list, &head->entry_list);
164 nm_i->dirty_nat_cnt++;
165 head->entry_cnt++;
166 set_nat_flag(ne, IS_DIRTY, true);
167}
168
169static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
170 struct nat_entry *ne)
171{
Changman Lee20d047c2014-11-25 12:44:23 +0900172 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700173 struct nat_entry_set *head;
174
175 head = radix_tree_lookup(&nm_i->nat_set_root, set);
176 if (head) {
177 list_move_tail(&ne->list, &nm_i->nat_entries);
178 set_nat_flag(ne, IS_DIRTY, false);
179 head->entry_cnt--;
180 nm_i->dirty_nat_cnt--;
181 }
182}
183
184static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
185 nid_t start, unsigned int nr, struct nat_entry_set **ep)
186{
187 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
188 start, nr);
189}
190
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700191bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900192{
193 struct f2fs_nm_info *nm_i = NM_I(sbi);
194 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700195 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900196
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800197 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900198 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700199 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700200 is_cp = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800201 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900202 return is_cp;
203}
204
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700205bool has_fsynced_inode(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900206{
207 struct f2fs_nm_info *nm_i = NM_I(sbi);
208 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700209 bool fsynced = false;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900210
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800211 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700212 e = __lookup_nat_cache(nm_i, ino);
213 if (e && get_nat_flag(e, HAS_FSYNCED_INODE))
214 fsynced = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800215 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700216 return fsynced;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900217}
218
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700219bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900220{
221 struct f2fs_nm_info *nm_i = NM_I(sbi);
222 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700223 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900224
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800225 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700226 e = __lookup_nat_cache(nm_i, ino);
227 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
228 (get_nat_flag(e, IS_CHECKPOINTED) ||
229 get_nat_flag(e, HAS_FSYNCED_INODE)))
230 need_update = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800231 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700232 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900233}
234
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900235static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
236{
237 struct nat_entry *new;
238
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800239 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
240 f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900241 memset(new, 0, sizeof(struct nat_entry));
242 nat_set_nid(new, nid);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700243 nat_reset_flag(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900244 list_add_tail(&new->list, &nm_i->nat_entries);
245 nm_i->nat_cnt++;
246 return new;
247}
248
249static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
250 struct f2fs_nat_entry *ne)
251{
252 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800253
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800254 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900255 e = __lookup_nat_cache(nm_i, nid);
256 if (!e) {
257 e = grab_nat_entry(nm_i, nid);
Chao Yu94dac222014-04-17 10:51:05 +0800258 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900259 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800260 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900261}
262
263static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900264 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900265{
266 struct f2fs_nm_info *nm_i = NM_I(sbi);
267 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800268
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800269 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900270 e = __lookup_nat_cache(nm_i, ni->nid);
271 if (!e) {
272 e = grab_nat_entry(nm_i, ni->nid);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800273 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700274 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900275 } else if (new_blkaddr == NEW_ADDR) {
276 /*
277 * when nid is reallocated,
278 * previous nat entry can be remained in nat cache.
279 * So, reinitialize it with new information.
280 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800281 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700282 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900283 }
284
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900285 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700286 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
287 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900288 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700289 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900290 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700291 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900292 nat_get_blkaddr(e) != NULL_ADDR &&
293 new_blkaddr == NEW_ADDR);
294
arter97e1c42042014-08-06 23:22:50 +0900295 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900296 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
297 unsigned char version = nat_get_version(e);
298 nat_set_version(e, inc_node_version(version));
299 }
300
301 /* change address */
302 nat_set_blkaddr(e, new_blkaddr);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700303 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
304 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900305 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900306
307 /* update fsync_mark if its inode nat entry is still alive */
308 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700309 if (e) {
310 if (fsync_done && ni->nid == ni->ino)
311 set_nat_flag(e, HAS_FSYNCED_INODE, true);
312 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
313 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800314 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900315}
316
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900317int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900318{
319 struct f2fs_nm_info *nm_i = NM_I(sbi);
320
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900321 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900322 return 0;
323
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800324 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900325 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
326 struct nat_entry *ne;
327 ne = list_first_entry(&nm_i->nat_entries,
328 struct nat_entry, list);
329 __del_from_nat_cache(nm_i, ne);
330 nr_shrink--;
331 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800332 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900333 return nr_shrink;
334}
335
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900336/*
arter97e1c42042014-08-06 23:22:50 +0900337 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900338 */
339void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
340{
341 struct f2fs_nm_info *nm_i = NM_I(sbi);
342 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
343 struct f2fs_summary_block *sum = curseg->sum_blk;
344 nid_t start_nid = START_NID(nid);
345 struct f2fs_nat_block *nat_blk;
346 struct page *page = NULL;
347 struct f2fs_nat_entry ne;
348 struct nat_entry *e;
349 int i;
350
351 ni->nid = nid;
352
353 /* Check nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800354 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900355 e = __lookup_nat_cache(nm_i, nid);
356 if (e) {
357 ni->ino = nat_get_ino(e);
358 ni->blk_addr = nat_get_blkaddr(e);
359 ni->version = nat_get_version(e);
360 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800361 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900362 if (e)
363 return;
364
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800365 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
366
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900367 /* Check current segment summary */
368 mutex_lock(&curseg->curseg_mutex);
369 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
370 if (i >= 0) {
371 ne = nat_in_journal(sum, i);
372 node_info_from_raw_nat(ni, &ne);
373 }
374 mutex_unlock(&curseg->curseg_mutex);
375 if (i >= 0)
376 goto cache;
377
378 /* Fill node_info from nat page */
379 page = get_current_nat_page(sbi, start_nid);
380 nat_blk = (struct f2fs_nat_block *)page_address(page);
381 ne = nat_blk->entries[nid - start_nid];
382 node_info_from_raw_nat(ni, &ne);
383 f2fs_put_page(page, 1);
384cache:
385 /* cache nat entry */
386 cache_nat_entry(NM_I(sbi), nid, &ne);
387}
388
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900389/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900390 * The maximum depth is four.
391 * Offset[0] will have raw inode offset.
392 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900393static int get_node_path(struct f2fs_inode_info *fi, long block,
394 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900395{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900396 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900397 const long direct_blks = ADDRS_PER_BLOCK;
398 const long dptrs_per_blk = NIDS_PER_BLOCK;
399 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
400 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
401 int n = 0;
402 int level = 0;
403
404 noffset[0] = 0;
405
406 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900407 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900408 goto got;
409 }
410 block -= direct_index;
411 if (block < direct_blks) {
412 offset[n++] = NODE_DIR1_BLOCK;
413 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900414 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900415 level = 1;
416 goto got;
417 }
418 block -= direct_blks;
419 if (block < direct_blks) {
420 offset[n++] = NODE_DIR2_BLOCK;
421 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900422 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900423 level = 1;
424 goto got;
425 }
426 block -= direct_blks;
427 if (block < indirect_blks) {
428 offset[n++] = NODE_IND1_BLOCK;
429 noffset[n] = 3;
430 offset[n++] = block / direct_blks;
431 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900432 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900433 level = 2;
434 goto got;
435 }
436 block -= indirect_blks;
437 if (block < indirect_blks) {
438 offset[n++] = NODE_IND2_BLOCK;
439 noffset[n] = 4 + dptrs_per_blk;
440 offset[n++] = block / direct_blks;
441 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900442 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900443 level = 2;
444 goto got;
445 }
446 block -= indirect_blks;
447 if (block < dindirect_blks) {
448 offset[n++] = NODE_DIND_BLOCK;
449 noffset[n] = 5 + (dptrs_per_blk * 2);
450 offset[n++] = block / indirect_blks;
451 noffset[n] = 6 + (dptrs_per_blk * 2) +
452 offset[n - 1] * (dptrs_per_blk + 1);
453 offset[n++] = (block / direct_blks) % dptrs_per_blk;
454 noffset[n] = 7 + (dptrs_per_blk * 2) +
455 offset[n - 2] * (dptrs_per_blk + 1) +
456 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900457 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900458 level = 3;
459 goto got;
460 } else {
461 BUG();
462 }
463got:
464 return level;
465}
466
467/*
468 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800469 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
470 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900471 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900472 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900473int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900474{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700475 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900476 struct page *npage[4];
477 struct page *parent;
478 int offset[4];
479 unsigned int noffset[4];
480 nid_t nids[4];
481 int level, i;
482 int err = 0;
483
Jaegeuk Kimde936532013-08-12 21:08:03 +0900484 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900485
486 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900487 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900488
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900489 if (!npage[0]) {
490 npage[0] = get_node_page(sbi, nids[0]);
491 if (IS_ERR(npage[0]))
492 return PTR_ERR(npage[0]);
493 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900494 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900495 if (level != 0)
496 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900497 dn->inode_page = npage[0];
498 dn->inode_page_locked = true;
499
500 /* get indirect or direct nodes */
501 for (i = 1; i <= level; i++) {
502 bool done = false;
503
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900504 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900505 /* alloc new node */
506 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900507 err = -ENOSPC;
508 goto release_pages;
509 }
510
511 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900512 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900513 if (IS_ERR(npage[i])) {
514 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900515 err = PTR_ERR(npage[i]);
516 goto release_pages;
517 }
518
519 set_nid(parent, offset[i - 1], nids[i], i == 1);
520 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900521 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900522 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900523 npage[i] = get_node_page_ra(parent, offset[i - 1]);
524 if (IS_ERR(npage[i])) {
525 err = PTR_ERR(npage[i]);
526 goto release_pages;
527 }
528 done = true;
529 }
530 if (i == 1) {
531 dn->inode_page_locked = false;
532 unlock_page(parent);
533 } else {
534 f2fs_put_page(parent, 1);
535 }
536
537 if (!done) {
538 npage[i] = get_node_page(sbi, nids[i]);
539 if (IS_ERR(npage[i])) {
540 err = PTR_ERR(npage[i]);
541 f2fs_put_page(npage[0], 0);
542 goto release_out;
543 }
544 }
545 if (i < level) {
546 parent = npage[i];
547 nids[i + 1] = get_nid(parent, offset[i], false);
548 }
549 }
550 dn->nid = nids[level];
551 dn->ofs_in_node = offset[level];
552 dn->node_page = npage[level];
553 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
554 return 0;
555
556release_pages:
557 f2fs_put_page(parent, 1);
558 if (i > 1)
559 f2fs_put_page(npage[0], 0);
560release_out:
561 dn->inode_page = NULL;
562 dn->node_page = NULL;
563 return err;
564}
565
566static void truncate_node(struct dnode_of_data *dn)
567{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700568 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900569 struct node_info ni;
570
571 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900572 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700573 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900574 goto invalidate;
575 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700576 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900577
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900578 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900579 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800580 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900581 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900582
583 if (dn->nid == dn->inode->i_ino) {
584 remove_orphan_inode(sbi, dn->nid);
585 dec_valid_inode_count(sbi);
586 } else {
587 sync_inode_page(dn);
588 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900589invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900590 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800591 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900592
593 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900594
595 invalidate_mapping_pages(NODE_MAPPING(sbi),
596 dn->node_page->index, dn->node_page->index);
597
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900598 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900599 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900600}
601
602static int truncate_dnode(struct dnode_of_data *dn)
603{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900604 struct page *page;
605
606 if (dn->nid == 0)
607 return 1;
608
609 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700610 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900611 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
612 return 1;
613 else if (IS_ERR(page))
614 return PTR_ERR(page);
615
616 /* Make dnode_of_data for parameter */
617 dn->node_page = page;
618 dn->ofs_in_node = 0;
619 truncate_data_blocks(dn);
620 truncate_node(dn);
621 return 1;
622}
623
624static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
625 int ofs, int depth)
626{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900627 struct dnode_of_data rdn = *dn;
628 struct page *page;
629 struct f2fs_node *rn;
630 nid_t child_nid;
631 unsigned int child_nofs;
632 int freed = 0;
633 int i, ret;
634
635 if (dn->nid == 0)
636 return NIDS_PER_BLOCK + 1;
637
Namjae Jeon51dd6242013-04-20 01:28:52 +0900638 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
639
Jaegeuk Kim40813632014-09-02 15:31:18 -0700640 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900641 if (IS_ERR(page)) {
642 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900643 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900644 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900645
Gu Zheng45590712013-07-15 17:57:38 +0800646 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900647 if (depth < 3) {
648 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
649 child_nid = le32_to_cpu(rn->in.nid[i]);
650 if (child_nid == 0)
651 continue;
652 rdn.nid = child_nid;
653 ret = truncate_dnode(&rdn);
654 if (ret < 0)
655 goto out_err;
656 set_nid(page, i, 0, false);
657 }
658 } else {
659 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
660 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
661 child_nid = le32_to_cpu(rn->in.nid[i]);
662 if (child_nid == 0) {
663 child_nofs += NIDS_PER_BLOCK + 1;
664 continue;
665 }
666 rdn.nid = child_nid;
667 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
668 if (ret == (NIDS_PER_BLOCK + 1)) {
669 set_nid(page, i, 0, false);
670 child_nofs += ret;
671 } else if (ret < 0 && ret != -ENOENT) {
672 goto out_err;
673 }
674 }
675 freed = child_nofs;
676 }
677
678 if (!ofs) {
679 /* remove current indirect node */
680 dn->node_page = page;
681 truncate_node(dn);
682 freed++;
683 } else {
684 f2fs_put_page(page, 1);
685 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900686 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900687 return freed;
688
689out_err:
690 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900691 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900692 return ret;
693}
694
695static int truncate_partial_nodes(struct dnode_of_data *dn,
696 struct f2fs_inode *ri, int *offset, int depth)
697{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900698 struct page *pages[2];
699 nid_t nid[3];
700 nid_t child_nid;
701 int err = 0;
702 int i;
703 int idx = depth - 2;
704
705 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
706 if (!nid[0])
707 return 0;
708
709 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800710 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900711 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700712 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900713 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900714 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800715 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900716 goto fail;
717 }
718 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
719 }
720
721 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800722 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900723 child_nid = get_nid(pages[idx], i, false);
724 if (!child_nid)
725 continue;
726 dn->nid = child_nid;
727 err = truncate_dnode(dn);
728 if (err < 0)
729 goto fail;
730 set_nid(pages[idx], i, 0, false);
731 }
732
shifei10.gea225dca2013-10-29 15:32:34 +0800733 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900734 dn->node_page = pages[idx];
735 dn->nid = nid[idx];
736 truncate_node(dn);
737 } else {
738 f2fs_put_page(pages[idx], 1);
739 }
740 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800741 offset[idx + 1] = 0;
742 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900743fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800744 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900745 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900746
747 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
748
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900749 return err;
750}
751
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900752/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900753 * All the block addresses of data and nodes should be nullified.
754 */
755int truncate_inode_blocks(struct inode *inode, pgoff_t from)
756{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700757 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900758 int err = 0, cont = 1;
759 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900760 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900761 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900762 struct dnode_of_data dn;
763 struct page *page;
764
Namjae Jeon51dd6242013-04-20 01:28:52 +0900765 trace_f2fs_truncate_inode_blocks_enter(inode, from);
766
Jaegeuk Kimde936532013-08-12 21:08:03 +0900767 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900768restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900769 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900770 if (IS_ERR(page)) {
771 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900772 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900773 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900774
775 set_new_dnode(&dn, inode, page, NULL, 0);
776 unlock_page(page);
777
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900778 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900779 switch (level) {
780 case 0:
781 case 1:
782 nofs = noffset[1];
783 break;
784 case 2:
785 nofs = noffset[1];
786 if (!offset[level - 1])
787 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900788 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900789 if (err < 0 && err != -ENOENT)
790 goto fail;
791 nofs += 1 + NIDS_PER_BLOCK;
792 break;
793 case 3:
794 nofs = 5 + 2 * NIDS_PER_BLOCK;
795 if (!offset[level - 1])
796 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900797 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900798 if (err < 0 && err != -ENOENT)
799 goto fail;
800 break;
801 default:
802 BUG();
803 }
804
805skip_partial:
806 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900807 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900808 switch (offset[0]) {
809 case NODE_DIR1_BLOCK:
810 case NODE_DIR2_BLOCK:
811 err = truncate_dnode(&dn);
812 break;
813
814 case NODE_IND1_BLOCK:
815 case NODE_IND2_BLOCK:
816 err = truncate_nodes(&dn, nofs, offset[1], 2);
817 break;
818
819 case NODE_DIND_BLOCK:
820 err = truncate_nodes(&dn, nofs, offset[1], 3);
821 cont = 0;
822 break;
823
824 default:
825 BUG();
826 }
827 if (err < 0 && err != -ENOENT)
828 goto fail;
829 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900830 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900831 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900832 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900833 f2fs_put_page(page, 1);
834 goto restart;
835 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900836 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900837 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900838 set_page_dirty(page);
839 unlock_page(page);
840 }
841 offset[1] = 0;
842 offset[0]++;
843 nofs += err;
844 }
845fail:
846 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900847 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900848 return err > 0 ? 0 : err;
849}
850
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900851int truncate_xattr_node(struct inode *inode, struct page *page)
852{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700853 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900854 nid_t nid = F2FS_I(inode)->i_xattr_nid;
855 struct dnode_of_data dn;
856 struct page *npage;
857
858 if (!nid)
859 return 0;
860
861 npage = get_node_page(sbi, nid);
862 if (IS_ERR(npage))
863 return PTR_ERR(npage);
864
865 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900866
867 /* need to do checkpoint during fsync */
868 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
869
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900870 set_new_dnode(&dn, inode, page, npage, nid);
871
872 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800873 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900874 truncate_node(&dn);
875 return 0;
876}
877
Jaegeuk Kim39936832012-11-22 16:21:29 +0900878/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800879 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
880 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900881 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800882void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900883{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900884 struct dnode_of_data dn;
885
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700886 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
887 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800888 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900889
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700890 if (truncate_xattr_node(inode, dn.inode_page)) {
891 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800892 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900893 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700894
895 /* remove potential inline_data blocks */
896 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
897 S_ISLNK(inode->i_mode))
898 truncate_data_blocks_range(&dn, 1);
899
arter97e1c42042014-08-06 23:22:50 +0900900 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700901 f2fs_bug_on(F2FS_I_SB(inode),
902 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700903
904 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900905 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900906}
907
Jaegeuk Kima014e032014-06-20 21:44:02 -0700908struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900909{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900910 struct dnode_of_data dn;
911
912 /* allocate inode page for new inode */
913 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900914
915 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900916 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900917}
918
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900919struct page *new_node_page(struct dnode_of_data *dn,
920 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900921{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700922 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900923 struct node_info old_ni, new_ni;
924 struct page *page;
925 int err;
926
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900927 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900928 return ERR_PTR(-EPERM);
929
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900930 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900931 if (!page)
932 return ERR_PTR(-ENOMEM);
933
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900934 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900935 err = -ENOSPC;
936 goto fail;
937 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900938
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900939 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900940
941 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700942 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900943 new_ni = old_ni;
944 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900945 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900946
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900947 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900948 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900949 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900950 SetPageUptodate(page);
951 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900952
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800953 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900954 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
955
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900956 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900957 if (ipage)
958 update_inode(dn->inode, ipage);
959 else
960 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900961 if (ofs == 0)
962 inc_valid_inode_count(sbi);
963
964 return page;
965
966fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900967 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900968 f2fs_put_page(page, 1);
969 return ERR_PTR(err);
970}
971
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900972/*
973 * Caller should do after getting the following values.
974 * 0: f2fs_put_page(page, 0)
975 * LOCKED_PAGE: f2fs_put_page(page, 1)
976 * error: nothing
977 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900978static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900979{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700980 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900981 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800982 struct f2fs_io_info fio = {
983 .type = NODE,
984 .rw = rw,
985 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900986
987 get_node_info(sbi, page->index, &ni);
988
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900989 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900990 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900991 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900992 }
993
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900994 if (PageUptodate(page))
995 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900996
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800997 fio.blk_addr = ni.blk_addr;
998 return f2fs_submit_page_bio(sbi, page, &fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999}
1000
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001001/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001002 * Readahead a node page
1003 */
1004void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1005{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001006 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001007 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001008
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001009 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001010 if (apage && PageUptodate(apage)) {
1011 f2fs_put_page(apage, 0);
1012 return;
1013 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001014 f2fs_put_page(apage, 0);
1015
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001016 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001017 if (!apage)
1018 return;
1019
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001020 err = read_node_page(apage, READA);
1021 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001022 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001023 else if (err == LOCKED_PAGE)
1024 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001025}
1026
1027struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1028{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001029 struct page *page;
1030 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001031repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001032 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001033 if (!page)
1034 return ERR_PTR(-ENOMEM);
1035
1036 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001037 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001038 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001039 else if (err == LOCKED_PAGE)
1040 goto got_it;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001041
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001042 lock_page(page);
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001043 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001044 f2fs_put_page(page, 1);
1045 return ERR_PTR(-EIO);
1046 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001047 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001048 f2fs_put_page(page, 1);
1049 goto repeat;
1050 }
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001051got_it:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001052 return page;
1053}
1054
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001055/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001056 * Return a locked page for the desired node page.
1057 * And, readahead MAX_RA_NODE number of node pages.
1058 */
1059struct page *get_node_page_ra(struct page *parent, int start)
1060{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001061 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001062 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001063 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001064 int err, i, end;
1065 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001066
1067 /* First, try getting the desired direct node. */
1068 nid = get_nid(parent, start, false);
1069 if (!nid)
1070 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001071repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001072 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001073 if (!page)
1074 return ERR_PTR(-ENOMEM);
1075
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001076 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001077 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001078 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001079 else if (err == LOCKED_PAGE)
1080 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001081
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001082 blk_start_plug(&plug);
1083
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001084 /* Then, try readahead for siblings of the desired node */
1085 end = start + MAX_RA_NODE;
1086 end = min(end, NIDS_PER_BLOCK);
1087 for (i = start + 1; i < end; i++) {
1088 nid = get_nid(parent, i, false);
1089 if (!nid)
1090 continue;
1091 ra_node_page(sbi, nid);
1092 }
1093
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001094 blk_finish_plug(&plug);
1095
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001096 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001097 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001098 f2fs_put_page(page, 1);
1099 goto repeat;
1100 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001101page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001102 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001103 f2fs_put_page(page, 1);
1104 return ERR_PTR(-EIO);
1105 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001106 return page;
1107}
1108
1109void sync_inode_page(struct dnode_of_data *dn)
1110{
1111 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1112 update_inode(dn->inode, dn->node_page);
1113 } else if (dn->inode_page) {
1114 if (!dn->inode_page_locked)
1115 lock_page(dn->inode_page);
1116 update_inode(dn->inode, dn->inode_page);
1117 if (!dn->inode_page_locked)
1118 unlock_page(dn->inode_page);
1119 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001120 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001121 }
1122}
1123
1124int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1125 struct writeback_control *wbc)
1126{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001127 pgoff_t index, end;
1128 struct pagevec pvec;
1129 int step = ino ? 2 : 0;
1130 int nwritten = 0, wrote = 0;
1131
1132 pagevec_init(&pvec, 0);
1133
1134next_step:
1135 index = 0;
1136 end = LONG_MAX;
1137
1138 while (index <= end) {
1139 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001140 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001141 PAGECACHE_TAG_DIRTY,
1142 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1143 if (nr_pages == 0)
1144 break;
1145
1146 for (i = 0; i < nr_pages; i++) {
1147 struct page *page = pvec.pages[i];
1148
1149 /*
1150 * flushing sequence with step:
1151 * 0. indirect nodes
1152 * 1. dentry dnodes
1153 * 2. file dnodes
1154 */
1155 if (step == 0 && IS_DNODE(page))
1156 continue;
1157 if (step == 1 && (!IS_DNODE(page) ||
1158 is_cold_node(page)))
1159 continue;
1160 if (step == 2 && (!IS_DNODE(page) ||
1161 !is_cold_node(page)))
1162 continue;
1163
1164 /*
1165 * If an fsync mode,
1166 * we should not skip writing node pages.
1167 */
1168 if (ino && ino_of_node(page) == ino)
1169 lock_page(page);
1170 else if (!trylock_page(page))
1171 continue;
1172
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001173 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001174continue_unlock:
1175 unlock_page(page);
1176 continue;
1177 }
1178 if (ino && ino_of_node(page) != ino)
1179 goto continue_unlock;
1180
1181 if (!PageDirty(page)) {
1182 /* someone wrote it for us */
1183 goto continue_unlock;
1184 }
1185
1186 if (!clear_page_dirty_for_io(page))
1187 goto continue_unlock;
1188
1189 /* called by fsync() */
1190 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001191 set_fsync_mark(page, 1);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -07001192 if (IS_INODE(page)) {
1193 if (!is_checkpointed_node(sbi, ino) &&
1194 !has_fsynced_inode(sbi, ino))
1195 set_dentry_mark(page, 1);
1196 else
1197 set_dentry_mark(page, 0);
1198 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001199 nwritten++;
1200 } else {
1201 set_fsync_mark(page, 0);
1202 set_dentry_mark(page, 0);
1203 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001204
1205 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1206 unlock_page(page);
1207 else
1208 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001209
1210 if (--wbc->nr_to_write == 0)
1211 break;
1212 }
1213 pagevec_release(&pvec);
1214 cond_resched();
1215
1216 if (wbc->nr_to_write == 0) {
1217 step = 2;
1218 break;
1219 }
1220 }
1221
1222 if (step < 2) {
1223 step++;
1224 goto next_step;
1225 }
1226
1227 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001228 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001229 return nwritten;
1230}
1231
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001232int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1233{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001234 pgoff_t index = 0, end = LONG_MAX;
1235 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001236 int ret2 = 0, ret = 0;
1237
1238 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001239
1240 while (index <= end) {
1241 int i, nr_pages;
1242 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1243 PAGECACHE_TAG_WRITEBACK,
1244 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1245 if (nr_pages == 0)
1246 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001247
1248 for (i = 0; i < nr_pages; i++) {
1249 struct page *page = pvec.pages[i];
1250
1251 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001252 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001253 continue;
1254
Chao Yu4bf08ff2013-11-04 10:28:33 +08001255 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001256 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001257 if (TestClearPageError(page))
1258 ret = -EIO;
1259 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001260 }
1261 pagevec_release(&pvec);
1262 cond_resched();
1263 }
1264
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001265 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001266 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001267 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001268 ret2 = -EIO;
1269 if (!ret)
1270 ret = ret2;
1271 return ret;
1272}
1273
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001274static int f2fs_write_node_page(struct page *page,
1275 struct writeback_control *wbc)
1276{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001277 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001278 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001279 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001280 struct f2fs_io_info fio = {
1281 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001282 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001283 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001284
Chao Yuecda0de2014-05-06 16:48:26 +08001285 trace_f2fs_writepage(page, NODE);
1286
Chao Yucaf00472015-01-28 17:48:42 +08001287 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001288 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001289 if (unlikely(f2fs_cp_error(sbi)))
1290 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001291
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001292 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001293
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001294 /* get old block addr of this node page */
1295 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001296 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001297
1298 get_node_info(sbi, nid, &ni);
1299
1300 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001301 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001302 dec_page_count(sbi, F2FS_DIRTY_NODES);
1303 unlock_page(page);
1304 return 0;
1305 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001306
Jaegeuk Kim2f97c322014-11-06 17:21:24 -08001307 if (wbc->for_reclaim) {
1308 if (!down_read_trylock(&sbi->node_write))
1309 goto redirty_out;
1310 } else {
1311 down_read(&sbi->node_write);
1312 }
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001313
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001314 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001315 fio.blk_addr = ni.blk_addr;
1316 write_node_page(sbi, page, nid, &fio);
1317 set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001318 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001319 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001320 unlock_page(page);
Jaegeuk Kim27c6bd62014-11-19 10:54:48 -08001321
1322 if (wbc->for_reclaim)
1323 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1324
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001325 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001326
1327redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001328 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001329 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001330}
1331
1332static int f2fs_write_node_pages(struct address_space *mapping,
1333 struct writeback_control *wbc)
1334{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001335 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001336 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001337
Chao Yue5748432014-05-06 16:51:24 +08001338 trace_f2fs_writepages(mapping->host, wbc, NODE);
1339
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001340 /* balancing f2fs's metadata in background */
1341 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001342
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001343 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001344 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001345 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001346
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001347 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001348 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001349 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001350 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001351 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001352
1353skip_write:
1354 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1355 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001356}
1357
1358static int f2fs_set_node_page_dirty(struct page *page)
1359{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001360 trace_f2fs_set_page_dirty(page, NODE);
1361
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001362 SetPageUptodate(page);
1363 if (!PageDirty(page)) {
1364 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001365 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001366 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001367 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001368 return 1;
1369 }
1370 return 0;
1371}
1372
Lukas Czernerd47992f2013-05-21 23:17:23 -04001373static void f2fs_invalidate_node_page(struct page *page, unsigned int offset,
1374 unsigned int length)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001375{
1376 struct inode *inode = page->mapping->host;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001377 if (PageDirty(page))
Jaegeuk Kim40813632014-09-02 15:31:18 -07001378 dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001379 ClearPagePrivate(page);
1380}
1381
1382static int f2fs_release_node_page(struct page *page, gfp_t wait)
1383{
1384 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001385 return 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001386}
1387
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001388/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001389 * Structure of the f2fs node operations
1390 */
1391const struct address_space_operations f2fs_node_aops = {
1392 .writepage = f2fs_write_node_page,
1393 .writepages = f2fs_write_node_pages,
1394 .set_page_dirty = f2fs_set_node_page_dirty,
1395 .invalidatepage = f2fs_invalidate_node_page,
1396 .releasepage = f2fs_release_node_page,
1397};
1398
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001399static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1400 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001401{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001402 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001403}
1404
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001405static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1406 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001407{
1408 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001409 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001410}
1411
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001412static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001413{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001414 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001415 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001416 struct nat_entry *ne;
1417 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001418
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001419 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001420 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001421
1422 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001423 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001424 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001425
Gu Zheng7bd59382013-10-22 14:52:26 +08001426 if (build) {
1427 /* do not add allocated nids */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001428 down_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001429 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001430 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001431 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1432 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001433 allocated = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001434 up_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001435 if (allocated)
1436 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001437 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001438
1439 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001440 i->nid = nid;
1441 i->state = NID_NEW;
1442
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001443 if (radix_tree_preload(GFP_NOFS)) {
1444 kmem_cache_free(free_nid_slab, i);
1445 return 0;
1446 }
1447
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001448 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001449 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001450 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001451 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001452 kmem_cache_free(free_nid_slab, i);
1453 return 0;
1454 }
1455 list_add_tail(&i->list, &nm_i->free_nid_list);
1456 nm_i->fcnt++;
1457 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001458 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001459 return 1;
1460}
1461
1462static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1463{
1464 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001465 bool need_free = false;
1466
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001467 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001468 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001469 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001470 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001471 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001472 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001473 }
1474 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001475
1476 if (need_free)
1477 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001478}
1479
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001480static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001481 struct page *nat_page, nid_t start_nid)
1482{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001483 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001484 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1485 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001486 int i;
1487
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001488 i = start_nid % NAT_ENTRY_PER_BLOCK;
1489
1490 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001491
Chao Yucfb271d2013-12-05 17:15:22 +08001492 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001493 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001494
1495 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001496 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001497 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001498 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001499 break;
1500 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001501 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001502}
1503
1504static void build_free_nids(struct f2fs_sb_info *sbi)
1505{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001506 struct f2fs_nm_info *nm_i = NM_I(sbi);
1507 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1508 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001509 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001510 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001511
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001512 /* Enough entries */
1513 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1514 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001515
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001516 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001517 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001518
1519 while (1) {
1520 struct page *page = get_current_nat_page(sbi, nid);
1521
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001522 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001523 f2fs_put_page(page, 1);
1524
1525 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001526 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001527 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001528
1529 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001530 break;
1531 }
1532
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001533 /* go to the next free nat pages to find free nids abundantly */
1534 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001535
1536 /* find free nids from current sum_pages */
1537 mutex_lock(&curseg->curseg_mutex);
1538 for (i = 0; i < nats_in_cursum(sum); i++) {
1539 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1540 nid = le32_to_cpu(nid_in_journal(sum, i));
1541 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001542 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001543 else
1544 remove_free_nid(nm_i, nid);
1545 }
1546 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001547}
1548
1549/*
1550 * If this function returns success, caller can obtain a new nid
1551 * from second parameter of this function.
1552 * The returned nid could be used ino as well as nid when inode is created.
1553 */
1554bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1555{
1556 struct f2fs_nm_info *nm_i = NM_I(sbi);
1557 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001558retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001559 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001560 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001561
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001562 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001563
1564 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001565 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001566 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001567 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001568 if (i->state == NID_NEW)
1569 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001570
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001571 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001572 *nid = i->nid;
1573 i->state = NID_ALLOC;
1574 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001575 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001576 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001577 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001578 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001579
1580 /* Let's scan nat pages and its caches to get free nids */
1581 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001582 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001583 mutex_unlock(&nm_i->build_lock);
1584 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001585}
1586
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001587/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001588 * alloc_nid() should be called prior to this function.
1589 */
1590void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1591{
1592 struct f2fs_nm_info *nm_i = NM_I(sbi);
1593 struct free_nid *i;
1594
1595 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001596 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001597 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001598 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001599 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001600
1601 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001602}
1603
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001604/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001605 * alloc_nid() should be called prior to this function.
1606 */
1607void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1608{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001609 struct f2fs_nm_info *nm_i = NM_I(sbi);
1610 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001611 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001612
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001613 if (!nid)
1614 return;
1615
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001616 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001617 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001618 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001619 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001620 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001621 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001622 } else {
1623 i->state = NID_NEW;
1624 nm_i->fcnt++;
1625 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001626 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001627
1628 if (need_free)
1629 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001630}
1631
Chao Yu70cfed82014-08-02 15:26:04 +08001632void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001633{
Chao Yu28cdce02014-03-11 13:37:38 +08001634 void *src_addr, *dst_addr;
1635 size_t inline_size;
1636 struct page *ipage;
1637 struct f2fs_inode *ri;
1638
Jaegeuk Kim40813632014-09-02 15:31:18 -07001639 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001640 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001641
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001642 ri = F2FS_INODE(page);
1643 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1644 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1645 goto update_inode;
1646 }
1647
Chao Yu28cdce02014-03-11 13:37:38 +08001648 dst_addr = inline_xattr_addr(ipage);
1649 src_addr = inline_xattr_addr(page);
1650 inline_size = inline_xattr_size(inode);
1651
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001652 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001653 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001654update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001655 update_inode(inode, ipage);
1656 f2fs_put_page(ipage, 1);
1657}
1658
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001659void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001660{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001661 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001662 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1663 nid_t new_xnid = nid_of_node(page);
1664 struct node_info ni;
1665
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001666 /* 1: invalidate the previous xattr nid */
1667 if (!prev_xnid)
1668 goto recover_xnid;
1669
1670 /* Deallocate node address */
1671 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001672 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001673 invalidate_blocks(sbi, ni.blk_addr);
1674 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001675 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001676
1677recover_xnid:
1678 /* 2: allocate new xattr nid */
1679 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001680 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001681
1682 remove_free_nid(NM_I(sbi), new_xnid);
1683 get_node_info(sbi, new_xnid, &ni);
1684 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001685 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001686 F2FS_I(inode)->i_xattr_nid = new_xnid;
1687
1688 /* 3: update xattr blkaddr */
1689 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001690 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001691
1692 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001693}
1694
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001695int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1696{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001697 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001698 nid_t ino = ino_of_node(page);
1699 struct node_info old_ni, new_ni;
1700 struct page *ipage;
1701
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001702 get_node_info(sbi, ino, &old_ni);
1703
1704 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1705 return -EINVAL;
1706
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001707 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001708 if (!ipage)
1709 return -ENOMEM;
1710
arter97e1c42042014-08-06 23:22:50 +09001711 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001712 remove_free_nid(NM_I(sbi), ino);
1713
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001714 SetPageUptodate(ipage);
1715 fill_node_footer(ipage, ino, ino, 0, true);
1716
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001717 src = F2FS_INODE(page);
1718 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001719
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001720 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1721 dst->i_size = 0;
1722 dst->i_blocks = cpu_to_le64(1);
1723 dst->i_links = cpu_to_le32(1);
1724 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001725 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001726
1727 new_ni = old_ni;
1728 new_ni.ino = ino;
1729
Chao Yucfb271d2013-12-05 17:15:22 +08001730 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001731 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001732 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001733 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001734 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001735 f2fs_put_page(ipage, 1);
1736 return 0;
1737}
1738
1739int restore_node_summary(struct f2fs_sb_info *sbi,
1740 unsigned int segno, struct f2fs_summary_block *sum)
1741{
1742 struct f2fs_node *rn;
1743 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001744 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001745 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yu9ecf4b82014-12-18 18:29:05 +08001746 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001747
1748 /* scan the node segment */
1749 last_offset = sbi->blocks_per_seg;
1750 addr = START_BLOCK(sbi, segno);
1751 sum_entry = &sum->entries[0];
1752
Chao Yu9ecf4b82014-12-18 18:29:05 +08001753 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001754 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001755
arter97e1c42042014-08-06 23:22:50 +09001756 /* readahead node pages */
Chao Yu9ecf4b82014-12-18 18:29:05 +08001757 ra_meta_pages(sbi, addr, nrpages, META_POR);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001758
Chao Yu9ecf4b82014-12-18 18:29:05 +08001759 for (idx = addr; idx < addr + nrpages; idx++) {
1760 struct page *page = get_meta_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08001761
Chao Yu9ecf4b82014-12-18 18:29:05 +08001762 rn = F2FS_NODE(page);
1763 sum_entry->nid = rn->footer.nid;
1764 sum_entry->version = 0;
1765 sum_entry->ofs_in_node = 0;
1766 sum_entry++;
1767 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08001768 }
Chao Yubac4eef2014-05-27 08:41:07 +08001769
Chao Yu9ecf4b82014-12-18 18:29:05 +08001770 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08001771 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001772 }
Chao Yu9ecf4b82014-12-18 18:29:05 +08001773 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001774}
1775
Chao Yuaec71382014-06-24 09:18:20 +08001776static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001777{
1778 struct f2fs_nm_info *nm_i = NM_I(sbi);
1779 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1780 struct f2fs_summary_block *sum = curseg->sum_blk;
1781 int i;
1782
1783 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001784 for (i = 0; i < nats_in_cursum(sum); i++) {
1785 struct nat_entry *ne;
1786 struct f2fs_nat_entry raw_ne;
1787 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1788
1789 raw_ne = nat_in_journal(sum, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001790
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001791 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001792 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001793 if (!ne) {
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001794 ne = grab_nat_entry(nm_i, nid);
1795 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001796 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001797 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001798 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001799 }
1800 update_nats_in_cursum(sum, -i);
1801 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001802}
1803
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001804static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1805 struct list_head *head, int max)
1806{
1807 struct nat_entry_set *cur;
1808
1809 if (nes->entry_cnt >= max)
1810 goto add_out;
1811
1812 list_for_each_entry(cur, head, set_list) {
1813 if (cur->entry_cnt >= nes->entry_cnt) {
1814 list_add(&nes->set_list, cur->set_list.prev);
1815 return;
1816 }
1817 }
1818add_out:
1819 list_add_tail(&nes->set_list, head);
1820}
1821
1822static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1823 struct nat_entry_set *set)
1824{
1825 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1826 struct f2fs_summary_block *sum = curseg->sum_blk;
1827 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1828 bool to_journal = true;
1829 struct f2fs_nat_block *nat_blk;
1830 struct nat_entry *ne, *cur;
1831 struct page *page = NULL;
1832
1833 /*
1834 * there are two steps to flush nat entries:
1835 * #1, flush nat entries to journal in current hot data summary block.
1836 * #2, flush nat entries to nat page.
1837 */
1838 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1839 to_journal = false;
1840
1841 if (to_journal) {
1842 mutex_lock(&curseg->curseg_mutex);
1843 } else {
1844 page = get_next_nat_page(sbi, start_nid);
1845 nat_blk = page_address(page);
1846 f2fs_bug_on(sbi, !nat_blk);
1847 }
1848
1849 /* flush dirty nats in nat entry set */
1850 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1851 struct f2fs_nat_entry *raw_ne;
1852 nid_t nid = nat_get_nid(ne);
1853 int offset;
1854
1855 if (nat_get_blkaddr(ne) == NEW_ADDR)
1856 continue;
1857
1858 if (to_journal) {
1859 offset = lookup_journal_in_cursum(sum,
1860 NAT_JOURNAL, nid, 1);
1861 f2fs_bug_on(sbi, offset < 0);
1862 raw_ne = &nat_in_journal(sum, offset);
1863 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1864 } else {
1865 raw_ne = &nat_blk->entries[nid - start_nid];
1866 }
1867 raw_nat_from_node_info(raw_ne, &ne->ni);
1868
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001869 down_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001870 nat_reset_flag(ne);
1871 __clear_nat_cache_dirty(NM_I(sbi), ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001872 up_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001873
1874 if (nat_get_blkaddr(ne) == NULL_ADDR)
1875 add_free_nid(sbi, nid, false);
1876 }
1877
1878 if (to_journal)
1879 mutex_unlock(&curseg->curseg_mutex);
1880 else
1881 f2fs_put_page(page, 1);
1882
Changman Lee80ec2e92014-11-25 12:44:24 +09001883 f2fs_bug_on(sbi, set->entry_cnt);
1884
1885 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
1886 kmem_cache_free(nat_entry_set_slab, set);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001887}
1888
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001889/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001890 * This function is called during the checkpointing process.
1891 */
1892void flush_nat_entries(struct f2fs_sb_info *sbi)
1893{
1894 struct f2fs_nm_info *nm_i = NM_I(sbi);
1895 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1896 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001897 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001898 struct nat_entry_set *set, *tmp;
1899 unsigned int found;
1900 nid_t set_idx = 0;
1901 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001902
Changman Lee20d047c2014-11-25 12:44:23 +09001903 if (!nm_i->dirty_nat_cnt)
1904 return;
Chao Yuaec71382014-06-24 09:18:20 +08001905 /*
1906 * if there are no enough space in journal to store dirty nat
1907 * entries, remove all entries from journal and merge them
1908 * into nat entry set.
1909 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001910 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001911 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001912
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001913 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001914 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001915 unsigned idx;
1916 set_idx = setvec[found - 1]->set + 1;
1917 for (idx = 0; idx < found; idx++)
1918 __adjust_nat_entry_set(setvec[idx], &sets,
1919 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001920 }
Chao Yuaec71382014-06-24 09:18:20 +08001921
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001922 /* flush dirty nats in nat entry set */
1923 list_for_each_entry_safe(set, tmp, &sets, set_list)
1924 __flush_nat_entry_set(sbi, set);
1925
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001926 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001927}
1928
1929static int init_node_manager(struct f2fs_sb_info *sbi)
1930{
1931 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1932 struct f2fs_nm_info *nm_i = NM_I(sbi);
1933 unsigned char *version_bitmap;
1934 unsigned int nat_segs, nat_blocks;
1935
1936 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1937
1938 /* segment_count_nat includes pair segment so divide to 2. */
1939 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1940 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001941
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001942 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1943
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001944 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001945 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001946 nm_i->fcnt = 0;
1947 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001948 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001949
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001950 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001951 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001952 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
1953 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001954 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001955
1956 mutex_init(&nm_i->build_lock);
1957 spin_lock_init(&nm_i->free_nid_list_lock);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001958 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001959
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001960 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001961 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001962 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1963 if (!version_bitmap)
1964 return -EFAULT;
1965
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001966 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1967 GFP_KERNEL);
1968 if (!nm_i->nat_bitmap)
1969 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001970 return 0;
1971}
1972
1973int build_node_manager(struct f2fs_sb_info *sbi)
1974{
1975 int err;
1976
1977 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1978 if (!sbi->nm_info)
1979 return -ENOMEM;
1980
1981 err = init_node_manager(sbi);
1982 if (err)
1983 return err;
1984
1985 build_free_nids(sbi);
1986 return 0;
1987}
1988
1989void destroy_node_manager(struct f2fs_sb_info *sbi)
1990{
1991 struct f2fs_nm_info *nm_i = NM_I(sbi);
1992 struct free_nid *i, *next_i;
1993 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001994 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001995 nid_t nid = 0;
1996 unsigned int found;
1997
1998 if (!nm_i)
1999 return;
2000
2001 /* destroy free nid list */
2002 spin_lock(&nm_i->free_nid_list_lock);
2003 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002004 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002005 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002006 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002007 spin_unlock(&nm_i->free_nid_list_lock);
2008 kmem_cache_free(free_nid_slab, i);
2009 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002010 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002011 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002012 spin_unlock(&nm_i->free_nid_list_lock);
2013
2014 /* destroy nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002015 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002016 while ((found = __gang_lookup_nat_cache(nm_i,
2017 nid, NATVEC_SIZE, natvec))) {
2018 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002019
Gu Zhengb6ce3912014-03-07 18:43:24 +08002020 nid = nat_get_nid(natvec[found - 1]) + 1;
2021 for (idx = 0; idx < found; idx++)
2022 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002023 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002024 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002025
2026 /* destroy nat set cache */
2027 nid = 0;
2028 while ((found = __gang_lookup_nat_set(nm_i,
2029 nid, SETVEC_SIZE, setvec))) {
2030 unsigned idx;
2031
2032 nid = setvec[found - 1]->set + 1;
2033 for (idx = 0; idx < found; idx++) {
2034 /* entry_cnt is not zero, when cp_error was occurred */
2035 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2036 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2037 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2038 }
2039 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002040 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002041
2042 kfree(nm_i->nat_bitmap);
2043 sbi->nm_info = NULL;
2044 kfree(nm_i);
2045}
2046
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002047int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002048{
2049 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002050 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002051 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002052 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002053
2054 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002055 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002056 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002057 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002058
2059 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2060 sizeof(struct nat_entry_set));
2061 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002062 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002063 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002064
Markus Elfringce3e6d22014-11-24 15:52:00 +01002065destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002066 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002067destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002068 kmem_cache_destroy(nat_entry_slab);
2069fail:
2070 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002071}
2072
2073void destroy_node_manager_caches(void)
2074{
Chao Yuaec71382014-06-24 09:18:20 +08002075 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002076 kmem_cache_destroy(free_nid_slab);
2077 kmem_cache_destroy(nat_entry_slab);
2078}