blob: 97bd9d3db88285e36a24c9d05b73c3a5c5e269f1 [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];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800477 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900478 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 Kimf1a3b982015-02-11 11:25:11 -0800494
495 /* if inline_data is set, should not report any block indices */
496 if (f2fs_has_inline_data(dn->inode) && index) {
497 err = -EINVAL;
498 f2fs_put_page(npage[0], 1);
499 goto release_out;
500 }
501
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900502 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900503 if (level != 0)
504 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900505 dn->inode_page = npage[0];
506 dn->inode_page_locked = true;
507
508 /* get indirect or direct nodes */
509 for (i = 1; i <= level; i++) {
510 bool done = false;
511
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900512 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900513 /* alloc new node */
514 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900515 err = -ENOSPC;
516 goto release_pages;
517 }
518
519 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900520 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900521 if (IS_ERR(npage[i])) {
522 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900523 err = PTR_ERR(npage[i]);
524 goto release_pages;
525 }
526
527 set_nid(parent, offset[i - 1], nids[i], i == 1);
528 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900529 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900530 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900531 npage[i] = get_node_page_ra(parent, offset[i - 1]);
532 if (IS_ERR(npage[i])) {
533 err = PTR_ERR(npage[i]);
534 goto release_pages;
535 }
536 done = true;
537 }
538 if (i == 1) {
539 dn->inode_page_locked = false;
540 unlock_page(parent);
541 } else {
542 f2fs_put_page(parent, 1);
543 }
544
545 if (!done) {
546 npage[i] = get_node_page(sbi, nids[i]);
547 if (IS_ERR(npage[i])) {
548 err = PTR_ERR(npage[i]);
549 f2fs_put_page(npage[0], 0);
550 goto release_out;
551 }
552 }
553 if (i < level) {
554 parent = npage[i];
555 nids[i + 1] = get_nid(parent, offset[i], false);
556 }
557 }
558 dn->nid = nids[level];
559 dn->ofs_in_node = offset[level];
560 dn->node_page = npage[level];
561 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
562 return 0;
563
564release_pages:
565 f2fs_put_page(parent, 1);
566 if (i > 1)
567 f2fs_put_page(npage[0], 0);
568release_out:
569 dn->inode_page = NULL;
570 dn->node_page = NULL;
571 return err;
572}
573
574static void truncate_node(struct dnode_of_data *dn)
575{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700576 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900577 struct node_info ni;
578
579 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900580 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700581 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900582 goto invalidate;
583 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700584 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900585
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900586 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900587 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800588 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900589 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900590
591 if (dn->nid == dn->inode->i_ino) {
592 remove_orphan_inode(sbi, dn->nid);
593 dec_valid_inode_count(sbi);
594 } else {
595 sync_inode_page(dn);
596 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900597invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900598 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800599 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900600
601 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900602
603 invalidate_mapping_pages(NODE_MAPPING(sbi),
604 dn->node_page->index, dn->node_page->index);
605
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900606 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900607 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900608}
609
610static int truncate_dnode(struct dnode_of_data *dn)
611{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900612 struct page *page;
613
614 if (dn->nid == 0)
615 return 1;
616
617 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700618 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900619 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
620 return 1;
621 else if (IS_ERR(page))
622 return PTR_ERR(page);
623
624 /* Make dnode_of_data for parameter */
625 dn->node_page = page;
626 dn->ofs_in_node = 0;
627 truncate_data_blocks(dn);
628 truncate_node(dn);
629 return 1;
630}
631
632static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
633 int ofs, int depth)
634{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900635 struct dnode_of_data rdn = *dn;
636 struct page *page;
637 struct f2fs_node *rn;
638 nid_t child_nid;
639 unsigned int child_nofs;
640 int freed = 0;
641 int i, ret;
642
643 if (dn->nid == 0)
644 return NIDS_PER_BLOCK + 1;
645
Namjae Jeon51dd6242013-04-20 01:28:52 +0900646 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
647
Jaegeuk Kim40813632014-09-02 15:31:18 -0700648 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900649 if (IS_ERR(page)) {
650 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900651 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900652 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900653
Gu Zheng45590712013-07-15 17:57:38 +0800654 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900655 if (depth < 3) {
656 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
657 child_nid = le32_to_cpu(rn->in.nid[i]);
658 if (child_nid == 0)
659 continue;
660 rdn.nid = child_nid;
661 ret = truncate_dnode(&rdn);
662 if (ret < 0)
663 goto out_err;
664 set_nid(page, i, 0, false);
665 }
666 } else {
667 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
668 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
669 child_nid = le32_to_cpu(rn->in.nid[i]);
670 if (child_nid == 0) {
671 child_nofs += NIDS_PER_BLOCK + 1;
672 continue;
673 }
674 rdn.nid = child_nid;
675 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
676 if (ret == (NIDS_PER_BLOCK + 1)) {
677 set_nid(page, i, 0, false);
678 child_nofs += ret;
679 } else if (ret < 0 && ret != -ENOENT) {
680 goto out_err;
681 }
682 }
683 freed = child_nofs;
684 }
685
686 if (!ofs) {
687 /* remove current indirect node */
688 dn->node_page = page;
689 truncate_node(dn);
690 freed++;
691 } else {
692 f2fs_put_page(page, 1);
693 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900694 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900695 return freed;
696
697out_err:
698 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900699 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900700 return ret;
701}
702
703static int truncate_partial_nodes(struct dnode_of_data *dn,
704 struct f2fs_inode *ri, int *offset, int depth)
705{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900706 struct page *pages[2];
707 nid_t nid[3];
708 nid_t child_nid;
709 int err = 0;
710 int i;
711 int idx = depth - 2;
712
713 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
714 if (!nid[0])
715 return 0;
716
717 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800718 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900719 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700720 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900721 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900722 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800723 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900724 goto fail;
725 }
726 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
727 }
728
729 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800730 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900731 child_nid = get_nid(pages[idx], i, false);
732 if (!child_nid)
733 continue;
734 dn->nid = child_nid;
735 err = truncate_dnode(dn);
736 if (err < 0)
737 goto fail;
738 set_nid(pages[idx], i, 0, false);
739 }
740
shifei10.gea225dca2013-10-29 15:32:34 +0800741 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900742 dn->node_page = pages[idx];
743 dn->nid = nid[idx];
744 truncate_node(dn);
745 } else {
746 f2fs_put_page(pages[idx], 1);
747 }
748 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800749 offset[idx + 1] = 0;
750 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900751fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800752 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900753 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900754
755 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
756
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900757 return err;
758}
759
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900760/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900761 * All the block addresses of data and nodes should be nullified.
762 */
763int truncate_inode_blocks(struct inode *inode, pgoff_t from)
764{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700765 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900766 int err = 0, cont = 1;
767 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900768 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900769 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900770 struct dnode_of_data dn;
771 struct page *page;
772
Namjae Jeon51dd6242013-04-20 01:28:52 +0900773 trace_f2fs_truncate_inode_blocks_enter(inode, from);
774
Jaegeuk Kimde936532013-08-12 21:08:03 +0900775 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900776restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900777 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900778 if (IS_ERR(page)) {
779 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900780 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900781 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900782
783 set_new_dnode(&dn, inode, page, NULL, 0);
784 unlock_page(page);
785
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900786 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900787 switch (level) {
788 case 0:
789 case 1:
790 nofs = noffset[1];
791 break;
792 case 2:
793 nofs = noffset[1];
794 if (!offset[level - 1])
795 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900796 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900797 if (err < 0 && err != -ENOENT)
798 goto fail;
799 nofs += 1 + NIDS_PER_BLOCK;
800 break;
801 case 3:
802 nofs = 5 + 2 * NIDS_PER_BLOCK;
803 if (!offset[level - 1])
804 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900805 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900806 if (err < 0 && err != -ENOENT)
807 goto fail;
808 break;
809 default:
810 BUG();
811 }
812
813skip_partial:
814 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900815 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900816 switch (offset[0]) {
817 case NODE_DIR1_BLOCK:
818 case NODE_DIR2_BLOCK:
819 err = truncate_dnode(&dn);
820 break;
821
822 case NODE_IND1_BLOCK:
823 case NODE_IND2_BLOCK:
824 err = truncate_nodes(&dn, nofs, offset[1], 2);
825 break;
826
827 case NODE_DIND_BLOCK:
828 err = truncate_nodes(&dn, nofs, offset[1], 3);
829 cont = 0;
830 break;
831
832 default:
833 BUG();
834 }
835 if (err < 0 && err != -ENOENT)
836 goto fail;
837 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900838 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900839 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900840 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900841 f2fs_put_page(page, 1);
842 goto restart;
843 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900844 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900845 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900846 set_page_dirty(page);
847 unlock_page(page);
848 }
849 offset[1] = 0;
850 offset[0]++;
851 nofs += err;
852 }
853fail:
854 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900855 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900856 return err > 0 ? 0 : err;
857}
858
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900859int truncate_xattr_node(struct inode *inode, struct page *page)
860{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700861 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900862 nid_t nid = F2FS_I(inode)->i_xattr_nid;
863 struct dnode_of_data dn;
864 struct page *npage;
865
866 if (!nid)
867 return 0;
868
869 npage = get_node_page(sbi, nid);
870 if (IS_ERR(npage))
871 return PTR_ERR(npage);
872
873 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900874
875 /* need to do checkpoint during fsync */
876 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
877
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900878 set_new_dnode(&dn, inode, page, npage, nid);
879
880 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800881 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900882 truncate_node(&dn);
883 return 0;
884}
885
Jaegeuk Kim39936832012-11-22 16:21:29 +0900886/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800887 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
888 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900889 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800890void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900891{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900892 struct dnode_of_data dn;
893
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700894 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
895 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800896 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900897
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700898 if (truncate_xattr_node(inode, dn.inode_page)) {
899 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800900 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900901 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700902
903 /* remove potential inline_data blocks */
904 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
905 S_ISLNK(inode->i_mode))
906 truncate_data_blocks_range(&dn, 1);
907
arter97e1c42042014-08-06 23:22:50 +0900908 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700909 f2fs_bug_on(F2FS_I_SB(inode),
910 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700911
912 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900913 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900914}
915
Jaegeuk Kima014e032014-06-20 21:44:02 -0700916struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900917{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900918 struct dnode_of_data dn;
919
920 /* allocate inode page for new inode */
921 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900922
923 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900924 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900925}
926
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900927struct page *new_node_page(struct dnode_of_data *dn,
928 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900929{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700930 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900931 struct node_info old_ni, new_ni;
932 struct page *page;
933 int err;
934
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900935 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900936 return ERR_PTR(-EPERM);
937
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900938 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900939 if (!page)
940 return ERR_PTR(-ENOMEM);
941
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900942 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900943 err = -ENOSPC;
944 goto fail;
945 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900946
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900947 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900948
949 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700950 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900951 new_ni = old_ni;
952 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900953 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900954
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900955 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900956 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900957 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900958 SetPageUptodate(page);
959 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900960
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800961 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900962 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
963
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900964 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900965 if (ipage)
966 update_inode(dn->inode, ipage);
967 else
968 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900969 if (ofs == 0)
970 inc_valid_inode_count(sbi);
971
972 return page;
973
974fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900975 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900976 f2fs_put_page(page, 1);
977 return ERR_PTR(err);
978}
979
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900980/*
981 * Caller should do after getting the following values.
982 * 0: f2fs_put_page(page, 0)
983 * LOCKED_PAGE: f2fs_put_page(page, 1)
984 * error: nothing
985 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900986static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900987{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700988 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900989 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800990 struct f2fs_io_info fio = {
991 .type = NODE,
992 .rw = rw,
993 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900994
995 get_node_info(sbi, page->index, &ni);
996
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900997 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900998 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900999 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001000 }
1001
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001002 if (PageUptodate(page))
1003 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001004
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001005 fio.blk_addr = ni.blk_addr;
1006 return f2fs_submit_page_bio(sbi, page, &fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001007}
1008
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001009/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001010 * Readahead a node page
1011 */
1012void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1013{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001014 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001015 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001016
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001017 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001018 if (apage && PageUptodate(apage)) {
1019 f2fs_put_page(apage, 0);
1020 return;
1021 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001022 f2fs_put_page(apage, 0);
1023
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001024 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001025 if (!apage)
1026 return;
1027
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001028 err = read_node_page(apage, READA);
1029 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001030 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001031 else if (err == LOCKED_PAGE)
1032 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001033}
1034
1035struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1036{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001037 struct page *page;
1038 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001039repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001040 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001041 if (!page)
1042 return ERR_PTR(-ENOMEM);
1043
1044 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001045 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001046 return ERR_PTR(err);
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001047 else if (err != LOCKED_PAGE)
1048 lock_page(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001049
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001050 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001051 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001052 f2fs_put_page(page, 1);
1053 return ERR_PTR(-EIO);
1054 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001055 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001056 f2fs_put_page(page, 1);
1057 goto repeat;
1058 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001059 return page;
1060}
1061
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001062/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001063 * Return a locked page for the desired node page.
1064 * And, readahead MAX_RA_NODE number of node pages.
1065 */
1066struct page *get_node_page_ra(struct page *parent, int start)
1067{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001068 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001069 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001070 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001071 int err, i, end;
1072 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001073
1074 /* First, try getting the desired direct node. */
1075 nid = get_nid(parent, start, false);
1076 if (!nid)
1077 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001078repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001079 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001080 if (!page)
1081 return ERR_PTR(-ENOMEM);
1082
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001083 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001084 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001085 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001086 else if (err == LOCKED_PAGE)
1087 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001088
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001089 blk_start_plug(&plug);
1090
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001091 /* Then, try readahead for siblings of the desired node */
1092 end = start + MAX_RA_NODE;
1093 end = min(end, NIDS_PER_BLOCK);
1094 for (i = start + 1; i < end; i++) {
1095 nid = get_nid(parent, i, false);
1096 if (!nid)
1097 continue;
1098 ra_node_page(sbi, nid);
1099 }
1100
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001101 blk_finish_plug(&plug);
1102
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001103 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001104 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001105 f2fs_put_page(page, 1);
1106 goto repeat;
1107 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001108page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001109 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001110 f2fs_put_page(page, 1);
1111 return ERR_PTR(-EIO);
1112 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001113 return page;
1114}
1115
1116void sync_inode_page(struct dnode_of_data *dn)
1117{
1118 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1119 update_inode(dn->inode, dn->node_page);
1120 } else if (dn->inode_page) {
1121 if (!dn->inode_page_locked)
1122 lock_page(dn->inode_page);
1123 update_inode(dn->inode, dn->inode_page);
1124 if (!dn->inode_page_locked)
1125 unlock_page(dn->inode_page);
1126 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001127 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001128 }
1129}
1130
1131int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1132 struct writeback_control *wbc)
1133{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001134 pgoff_t index, end;
1135 struct pagevec pvec;
1136 int step = ino ? 2 : 0;
1137 int nwritten = 0, wrote = 0;
1138
1139 pagevec_init(&pvec, 0);
1140
1141next_step:
1142 index = 0;
1143 end = LONG_MAX;
1144
1145 while (index <= end) {
1146 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001147 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001148 PAGECACHE_TAG_DIRTY,
1149 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1150 if (nr_pages == 0)
1151 break;
1152
1153 for (i = 0; i < nr_pages; i++) {
1154 struct page *page = pvec.pages[i];
1155
1156 /*
1157 * flushing sequence with step:
1158 * 0. indirect nodes
1159 * 1. dentry dnodes
1160 * 2. file dnodes
1161 */
1162 if (step == 0 && IS_DNODE(page))
1163 continue;
1164 if (step == 1 && (!IS_DNODE(page) ||
1165 is_cold_node(page)))
1166 continue;
1167 if (step == 2 && (!IS_DNODE(page) ||
1168 !is_cold_node(page)))
1169 continue;
1170
1171 /*
1172 * If an fsync mode,
1173 * we should not skip writing node pages.
1174 */
1175 if (ino && ino_of_node(page) == ino)
1176 lock_page(page);
1177 else if (!trylock_page(page))
1178 continue;
1179
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001180 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001181continue_unlock:
1182 unlock_page(page);
1183 continue;
1184 }
1185 if (ino && ino_of_node(page) != ino)
1186 goto continue_unlock;
1187
1188 if (!PageDirty(page)) {
1189 /* someone wrote it for us */
1190 goto continue_unlock;
1191 }
1192
1193 if (!clear_page_dirty_for_io(page))
1194 goto continue_unlock;
1195
1196 /* called by fsync() */
1197 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001198 set_fsync_mark(page, 1);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -07001199 if (IS_INODE(page)) {
1200 if (!is_checkpointed_node(sbi, ino) &&
1201 !has_fsynced_inode(sbi, ino))
1202 set_dentry_mark(page, 1);
1203 else
1204 set_dentry_mark(page, 0);
1205 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001206 nwritten++;
1207 } else {
1208 set_fsync_mark(page, 0);
1209 set_dentry_mark(page, 0);
1210 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001211
1212 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1213 unlock_page(page);
1214 else
1215 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001216
1217 if (--wbc->nr_to_write == 0)
1218 break;
1219 }
1220 pagevec_release(&pvec);
1221 cond_resched();
1222
1223 if (wbc->nr_to_write == 0) {
1224 step = 2;
1225 break;
1226 }
1227 }
1228
1229 if (step < 2) {
1230 step++;
1231 goto next_step;
1232 }
1233
1234 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001235 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001236 return nwritten;
1237}
1238
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001239int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1240{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001241 pgoff_t index = 0, end = LONG_MAX;
1242 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001243 int ret2 = 0, ret = 0;
1244
1245 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001246
1247 while (index <= end) {
1248 int i, nr_pages;
1249 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1250 PAGECACHE_TAG_WRITEBACK,
1251 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1252 if (nr_pages == 0)
1253 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001254
1255 for (i = 0; i < nr_pages; i++) {
1256 struct page *page = pvec.pages[i];
1257
1258 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001259 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001260 continue;
1261
Chao Yu4bf08ff2013-11-04 10:28:33 +08001262 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001263 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001264 if (TestClearPageError(page))
1265 ret = -EIO;
1266 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001267 }
1268 pagevec_release(&pvec);
1269 cond_resched();
1270 }
1271
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001272 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001273 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001274 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001275 ret2 = -EIO;
1276 if (!ret)
1277 ret = ret2;
1278 return ret;
1279}
1280
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001281static int f2fs_write_node_page(struct page *page,
1282 struct writeback_control *wbc)
1283{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001284 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001285 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001286 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001287 struct f2fs_io_info fio = {
1288 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001289 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001290 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001291
Chao Yuecda0de2014-05-06 16:48:26 +08001292 trace_f2fs_writepage(page, NODE);
1293
Chao Yucaf00472015-01-28 17:48:42 +08001294 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001295 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001296 if (unlikely(f2fs_cp_error(sbi)))
1297 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001298
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001299 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001300
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001301 /* get old block addr of this node page */
1302 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001303 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001304
1305 get_node_info(sbi, nid, &ni);
1306
1307 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001308 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001309 dec_page_count(sbi, F2FS_DIRTY_NODES);
1310 unlock_page(page);
1311 return 0;
1312 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001313
Jaegeuk Kim2f97c322014-11-06 17:21:24 -08001314 if (wbc->for_reclaim) {
1315 if (!down_read_trylock(&sbi->node_write))
1316 goto redirty_out;
1317 } else {
1318 down_read(&sbi->node_write);
1319 }
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001320
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001321 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001322 fio.blk_addr = ni.blk_addr;
1323 write_node_page(sbi, page, nid, &fio);
1324 set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001325 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001326 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001327 unlock_page(page);
Jaegeuk Kim27c6bd62014-11-19 10:54:48 -08001328
1329 if (wbc->for_reclaim)
1330 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1331
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001332 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001333
1334redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001335 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001336 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001337}
1338
1339static int f2fs_write_node_pages(struct address_space *mapping,
1340 struct writeback_control *wbc)
1341{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001342 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001343 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001344
Chao Yue5748432014-05-06 16:51:24 +08001345 trace_f2fs_writepages(mapping->host, wbc, NODE);
1346
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001347 /* balancing f2fs's metadata in background */
1348 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001349
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001350 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001351 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001352 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001353
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001354 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001355 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001356 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001357 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001358 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001359
1360skip_write:
1361 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1362 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001363}
1364
1365static int f2fs_set_node_page_dirty(struct page *page)
1366{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001367 trace_f2fs_set_page_dirty(page, NODE);
1368
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001369 SetPageUptodate(page);
1370 if (!PageDirty(page)) {
1371 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001372 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001373 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001374 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001375 return 1;
1376 }
1377 return 0;
1378}
1379
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001380/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001381 * Structure of the f2fs node operations
1382 */
1383const struct address_space_operations f2fs_node_aops = {
1384 .writepage = f2fs_write_node_page,
1385 .writepages = f2fs_write_node_pages,
1386 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001387 .invalidatepage = f2fs_invalidate_page,
1388 .releasepage = f2fs_release_page,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001389};
1390
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001391static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1392 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001393{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001394 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001395}
1396
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001397static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1398 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001399{
1400 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001401 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001402}
1403
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001404static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001405{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001406 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001407 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001408 struct nat_entry *ne;
1409 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001410
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001411 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001412 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001413
1414 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001415 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001416 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001417
Gu Zheng7bd59382013-10-22 14:52:26 +08001418 if (build) {
1419 /* do not add allocated nids */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001420 down_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001421 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001422 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001423 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1424 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001425 allocated = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001426 up_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001427 if (allocated)
1428 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001429 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001430
1431 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001432 i->nid = nid;
1433 i->state = NID_NEW;
1434
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001435 if (radix_tree_preload(GFP_NOFS)) {
1436 kmem_cache_free(free_nid_slab, i);
1437 return 0;
1438 }
1439
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001440 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001441 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001442 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001443 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001444 kmem_cache_free(free_nid_slab, i);
1445 return 0;
1446 }
1447 list_add_tail(&i->list, &nm_i->free_nid_list);
1448 nm_i->fcnt++;
1449 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001450 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001451 return 1;
1452}
1453
1454static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1455{
1456 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001457 bool need_free = false;
1458
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001459 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001460 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001461 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001462 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001463 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001464 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001465 }
1466 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001467
1468 if (need_free)
1469 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001470}
1471
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001472static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001473 struct page *nat_page, nid_t start_nid)
1474{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001475 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001476 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1477 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001478 int i;
1479
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001480 i = start_nid % NAT_ENTRY_PER_BLOCK;
1481
1482 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001483
Chao Yucfb271d2013-12-05 17:15:22 +08001484 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001485 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001486
1487 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001488 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001489 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001490 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001491 break;
1492 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001493 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001494}
1495
1496static void build_free_nids(struct f2fs_sb_info *sbi)
1497{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001498 struct f2fs_nm_info *nm_i = NM_I(sbi);
1499 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1500 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001501 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001502 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001503
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001504 /* Enough entries */
1505 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1506 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001507
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001508 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001509 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001510
1511 while (1) {
1512 struct page *page = get_current_nat_page(sbi, nid);
1513
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001514 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001515 f2fs_put_page(page, 1);
1516
1517 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001518 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001519 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001520
1521 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001522 break;
1523 }
1524
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001525 /* go to the next free nat pages to find free nids abundantly */
1526 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001527
1528 /* find free nids from current sum_pages */
1529 mutex_lock(&curseg->curseg_mutex);
1530 for (i = 0; i < nats_in_cursum(sum); i++) {
1531 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1532 nid = le32_to_cpu(nid_in_journal(sum, i));
1533 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001534 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001535 else
1536 remove_free_nid(nm_i, nid);
1537 }
1538 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001539}
1540
1541/*
1542 * If this function returns success, caller can obtain a new nid
1543 * from second parameter of this function.
1544 * The returned nid could be used ino as well as nid when inode is created.
1545 */
1546bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1547{
1548 struct f2fs_nm_info *nm_i = NM_I(sbi);
1549 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001550retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001551 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001552 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001553
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001554 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001555
1556 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001557 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001558 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001559 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001560 if (i->state == NID_NEW)
1561 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001562
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001563 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001564 *nid = i->nid;
1565 i->state = NID_ALLOC;
1566 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001567 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001568 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001569 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001570 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001571
1572 /* Let's scan nat pages and its caches to get free nids */
1573 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001574 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001575 mutex_unlock(&nm_i->build_lock);
1576 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001577}
1578
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001579/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001580 * alloc_nid() should be called prior to this function.
1581 */
1582void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1583{
1584 struct f2fs_nm_info *nm_i = NM_I(sbi);
1585 struct free_nid *i;
1586
1587 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001588 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001589 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001590 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001591 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001592
1593 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001594}
1595
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001596/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001597 * alloc_nid() should be called prior to this function.
1598 */
1599void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1600{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001601 struct f2fs_nm_info *nm_i = NM_I(sbi);
1602 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001603 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001604
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001605 if (!nid)
1606 return;
1607
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001608 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001609 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001610 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001611 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001612 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001613 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001614 } else {
1615 i->state = NID_NEW;
1616 nm_i->fcnt++;
1617 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001618 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001619
1620 if (need_free)
1621 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001622}
1623
Chao Yu70cfed82014-08-02 15:26:04 +08001624void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001625{
Chao Yu28cdce02014-03-11 13:37:38 +08001626 void *src_addr, *dst_addr;
1627 size_t inline_size;
1628 struct page *ipage;
1629 struct f2fs_inode *ri;
1630
Jaegeuk Kim40813632014-09-02 15:31:18 -07001631 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001632 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001633
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001634 ri = F2FS_INODE(page);
1635 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1636 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1637 goto update_inode;
1638 }
1639
Chao Yu28cdce02014-03-11 13:37:38 +08001640 dst_addr = inline_xattr_addr(ipage);
1641 src_addr = inline_xattr_addr(page);
1642 inline_size = inline_xattr_size(inode);
1643
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001644 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001645 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001646update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001647 update_inode(inode, ipage);
1648 f2fs_put_page(ipage, 1);
1649}
1650
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001651void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001652{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001653 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001654 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1655 nid_t new_xnid = nid_of_node(page);
1656 struct node_info ni;
1657
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001658 /* 1: invalidate the previous xattr nid */
1659 if (!prev_xnid)
1660 goto recover_xnid;
1661
1662 /* Deallocate node address */
1663 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001664 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001665 invalidate_blocks(sbi, ni.blk_addr);
1666 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001667 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001668
1669recover_xnid:
1670 /* 2: allocate new xattr nid */
1671 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001672 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001673
1674 remove_free_nid(NM_I(sbi), new_xnid);
1675 get_node_info(sbi, new_xnid, &ni);
1676 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001677 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001678 F2FS_I(inode)->i_xattr_nid = new_xnid;
1679
1680 /* 3: update xattr blkaddr */
1681 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001682 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001683
1684 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001685}
1686
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001687int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1688{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001689 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001690 nid_t ino = ino_of_node(page);
1691 struct node_info old_ni, new_ni;
1692 struct page *ipage;
1693
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001694 get_node_info(sbi, ino, &old_ni);
1695
1696 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1697 return -EINVAL;
1698
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001699 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001700 if (!ipage)
1701 return -ENOMEM;
1702
arter97e1c42042014-08-06 23:22:50 +09001703 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001704 remove_free_nid(NM_I(sbi), ino);
1705
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001706 SetPageUptodate(ipage);
1707 fill_node_footer(ipage, ino, ino, 0, true);
1708
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001709 src = F2FS_INODE(page);
1710 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001711
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001712 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1713 dst->i_size = 0;
1714 dst->i_blocks = cpu_to_le64(1);
1715 dst->i_links = cpu_to_le32(1);
1716 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001717 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001718
1719 new_ni = old_ni;
1720 new_ni.ino = ino;
1721
Chao Yucfb271d2013-12-05 17:15:22 +08001722 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001723 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001724 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001725 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001726 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001727 f2fs_put_page(ipage, 1);
1728 return 0;
1729}
1730
1731int restore_node_summary(struct f2fs_sb_info *sbi,
1732 unsigned int segno, struct f2fs_summary_block *sum)
1733{
1734 struct f2fs_node *rn;
1735 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001736 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001737 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yu9ecf4b82014-12-18 18:29:05 +08001738 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001739
1740 /* scan the node segment */
1741 last_offset = sbi->blocks_per_seg;
1742 addr = START_BLOCK(sbi, segno);
1743 sum_entry = &sum->entries[0];
1744
Chao Yu9ecf4b82014-12-18 18:29:05 +08001745 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001746 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001747
arter97e1c42042014-08-06 23:22:50 +09001748 /* readahead node pages */
Chao Yu9ecf4b82014-12-18 18:29:05 +08001749 ra_meta_pages(sbi, addr, nrpages, META_POR);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001750
Chao Yu9ecf4b82014-12-18 18:29:05 +08001751 for (idx = addr; idx < addr + nrpages; idx++) {
1752 struct page *page = get_meta_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08001753
Chao Yu9ecf4b82014-12-18 18:29:05 +08001754 rn = F2FS_NODE(page);
1755 sum_entry->nid = rn->footer.nid;
1756 sum_entry->version = 0;
1757 sum_entry->ofs_in_node = 0;
1758 sum_entry++;
1759 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08001760 }
Chao Yubac4eef2014-05-27 08:41:07 +08001761
Chao Yu9ecf4b82014-12-18 18:29:05 +08001762 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08001763 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001764 }
Chao Yu9ecf4b82014-12-18 18:29:05 +08001765 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001766}
1767
Chao Yuaec71382014-06-24 09:18:20 +08001768static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001769{
1770 struct f2fs_nm_info *nm_i = NM_I(sbi);
1771 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1772 struct f2fs_summary_block *sum = curseg->sum_blk;
1773 int i;
1774
1775 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001776 for (i = 0; i < nats_in_cursum(sum); i++) {
1777 struct nat_entry *ne;
1778 struct f2fs_nat_entry raw_ne;
1779 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1780
1781 raw_ne = nat_in_journal(sum, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001782
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001783 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001784 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001785 if (!ne) {
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001786 ne = grab_nat_entry(nm_i, nid);
1787 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001788 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001789 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001790 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001791 }
1792 update_nats_in_cursum(sum, -i);
1793 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001794}
1795
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001796static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1797 struct list_head *head, int max)
1798{
1799 struct nat_entry_set *cur;
1800
1801 if (nes->entry_cnt >= max)
1802 goto add_out;
1803
1804 list_for_each_entry(cur, head, set_list) {
1805 if (cur->entry_cnt >= nes->entry_cnt) {
1806 list_add(&nes->set_list, cur->set_list.prev);
1807 return;
1808 }
1809 }
1810add_out:
1811 list_add_tail(&nes->set_list, head);
1812}
1813
1814static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1815 struct nat_entry_set *set)
1816{
1817 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1818 struct f2fs_summary_block *sum = curseg->sum_blk;
1819 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1820 bool to_journal = true;
1821 struct f2fs_nat_block *nat_blk;
1822 struct nat_entry *ne, *cur;
1823 struct page *page = NULL;
1824
1825 /*
1826 * there are two steps to flush nat entries:
1827 * #1, flush nat entries to journal in current hot data summary block.
1828 * #2, flush nat entries to nat page.
1829 */
1830 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1831 to_journal = false;
1832
1833 if (to_journal) {
1834 mutex_lock(&curseg->curseg_mutex);
1835 } else {
1836 page = get_next_nat_page(sbi, start_nid);
1837 nat_blk = page_address(page);
1838 f2fs_bug_on(sbi, !nat_blk);
1839 }
1840
1841 /* flush dirty nats in nat entry set */
1842 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1843 struct f2fs_nat_entry *raw_ne;
1844 nid_t nid = nat_get_nid(ne);
1845 int offset;
1846
1847 if (nat_get_blkaddr(ne) == NEW_ADDR)
1848 continue;
1849
1850 if (to_journal) {
1851 offset = lookup_journal_in_cursum(sum,
1852 NAT_JOURNAL, nid, 1);
1853 f2fs_bug_on(sbi, offset < 0);
1854 raw_ne = &nat_in_journal(sum, offset);
1855 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1856 } else {
1857 raw_ne = &nat_blk->entries[nid - start_nid];
1858 }
1859 raw_nat_from_node_info(raw_ne, &ne->ni);
1860
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001861 down_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001862 nat_reset_flag(ne);
1863 __clear_nat_cache_dirty(NM_I(sbi), ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001864 up_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001865
1866 if (nat_get_blkaddr(ne) == NULL_ADDR)
1867 add_free_nid(sbi, nid, false);
1868 }
1869
1870 if (to_journal)
1871 mutex_unlock(&curseg->curseg_mutex);
1872 else
1873 f2fs_put_page(page, 1);
1874
Changman Lee80ec2e92014-11-25 12:44:24 +09001875 f2fs_bug_on(sbi, set->entry_cnt);
1876
1877 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
1878 kmem_cache_free(nat_entry_set_slab, set);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001879}
1880
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001881/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001882 * This function is called during the checkpointing process.
1883 */
1884void flush_nat_entries(struct f2fs_sb_info *sbi)
1885{
1886 struct f2fs_nm_info *nm_i = NM_I(sbi);
1887 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1888 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001889 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001890 struct nat_entry_set *set, *tmp;
1891 unsigned int found;
1892 nid_t set_idx = 0;
1893 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001894
Changman Lee20d047c2014-11-25 12:44:23 +09001895 if (!nm_i->dirty_nat_cnt)
1896 return;
Chao Yuaec71382014-06-24 09:18:20 +08001897 /*
1898 * if there are no enough space in journal to store dirty nat
1899 * entries, remove all entries from journal and merge them
1900 * into nat entry set.
1901 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001902 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001903 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001904
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001905 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001906 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001907 unsigned idx;
1908 set_idx = setvec[found - 1]->set + 1;
1909 for (idx = 0; idx < found; idx++)
1910 __adjust_nat_entry_set(setvec[idx], &sets,
1911 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001912 }
Chao Yuaec71382014-06-24 09:18:20 +08001913
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001914 /* flush dirty nats in nat entry set */
1915 list_for_each_entry_safe(set, tmp, &sets, set_list)
1916 __flush_nat_entry_set(sbi, set);
1917
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001918 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001919}
1920
1921static int init_node_manager(struct f2fs_sb_info *sbi)
1922{
1923 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1924 struct f2fs_nm_info *nm_i = NM_I(sbi);
1925 unsigned char *version_bitmap;
1926 unsigned int nat_segs, nat_blocks;
1927
1928 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1929
1930 /* segment_count_nat includes pair segment so divide to 2. */
1931 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1932 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001933
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001934 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1935
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001936 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001937 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001938 nm_i->fcnt = 0;
1939 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001940 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001941
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001942 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001943 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001944 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
1945 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001946 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001947
1948 mutex_init(&nm_i->build_lock);
1949 spin_lock_init(&nm_i->free_nid_list_lock);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001950 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001951
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001952 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001953 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001954 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1955 if (!version_bitmap)
1956 return -EFAULT;
1957
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001958 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1959 GFP_KERNEL);
1960 if (!nm_i->nat_bitmap)
1961 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001962 return 0;
1963}
1964
1965int build_node_manager(struct f2fs_sb_info *sbi)
1966{
1967 int err;
1968
1969 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1970 if (!sbi->nm_info)
1971 return -ENOMEM;
1972
1973 err = init_node_manager(sbi);
1974 if (err)
1975 return err;
1976
1977 build_free_nids(sbi);
1978 return 0;
1979}
1980
1981void destroy_node_manager(struct f2fs_sb_info *sbi)
1982{
1983 struct f2fs_nm_info *nm_i = NM_I(sbi);
1984 struct free_nid *i, *next_i;
1985 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001986 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001987 nid_t nid = 0;
1988 unsigned int found;
1989
1990 if (!nm_i)
1991 return;
1992
1993 /* destroy free nid list */
1994 spin_lock(&nm_i->free_nid_list_lock);
1995 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001996 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001997 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001998 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001999 spin_unlock(&nm_i->free_nid_list_lock);
2000 kmem_cache_free(free_nid_slab, i);
2001 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002002 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002003 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002004 spin_unlock(&nm_i->free_nid_list_lock);
2005
2006 /* destroy nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002007 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002008 while ((found = __gang_lookup_nat_cache(nm_i,
2009 nid, NATVEC_SIZE, natvec))) {
2010 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002011
Gu Zhengb6ce3912014-03-07 18:43:24 +08002012 nid = nat_get_nid(natvec[found - 1]) + 1;
2013 for (idx = 0; idx < found; idx++)
2014 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002015 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002016 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002017
2018 /* destroy nat set cache */
2019 nid = 0;
2020 while ((found = __gang_lookup_nat_set(nm_i,
2021 nid, SETVEC_SIZE, setvec))) {
2022 unsigned idx;
2023
2024 nid = setvec[found - 1]->set + 1;
2025 for (idx = 0; idx < found; idx++) {
2026 /* entry_cnt is not zero, when cp_error was occurred */
2027 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2028 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2029 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2030 }
2031 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002032 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002033
2034 kfree(nm_i->nat_bitmap);
2035 sbi->nm_info = NULL;
2036 kfree(nm_i);
2037}
2038
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002039int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002040{
2041 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002042 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002043 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002044 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002045
2046 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002047 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002048 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002049 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002050
2051 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2052 sizeof(struct nat_entry_set));
2053 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002054 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002055 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002056
Markus Elfringce3e6d22014-11-24 15:52:00 +01002057destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002058 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002059destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002060 kmem_cache_destroy(nat_entry_slab);
2061fail:
2062 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002063}
2064
2065void destroy_node_manager_caches(void)
2066{
Chao Yuaec71382014-06-24 09:18:20 +08002067 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002068 kmem_cache_destroy(free_nid_slab);
2069 kmem_cache_destroy(nat_entry_slab);
2070}