blob: d9c52424bac21555f5a84a2f8ef9f7f2dc7751c5 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002 * fs/f2fs/node.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/mpage.h>
14#include <linux/backing-dev.h>
15#include <linux/blkdev.h>
16#include <linux/pagevec.h>
17#include <linux/swap.h>
18
19#include "f2fs.h"
20#include "node.h"
21#include "segment.h"
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -080022#include "trace.h"
Namjae Jeon51dd6242013-04-20 01:28:52 +090023#include <trace/events/f2fs.h>
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090024
Gu Zhengf978f5a2014-02-21 18:08:29 +080025#define on_build_free_nids(nmi) mutex_is_locked(&nm_i->build_lock)
26
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090027static struct kmem_cache *nat_entry_slab;
28static struct kmem_cache *free_nid_slab;
Chao Yuaec71382014-06-24 09:18:20 +080029static struct kmem_cache *nat_entry_set_slab;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090030
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090031bool available_free_memory(struct f2fs_sb_info *sbi, int type)
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090032{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090033 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090034 struct sysinfo val;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080035 unsigned long avail_ram;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090036 unsigned long mem_size = 0;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090037 bool res = false;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090038
39 si_meminfo(&val);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080040
41 /* only uses low memory */
42 avail_ram = val.totalram - val.totalhigh;
43
Chao Yu429511c2015-02-05 17:54:31 +080044 /*
45 * give 25%, 25%, 50%, 50%, 50% memory for each components respectively
46 */
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090047 if (type == FREE_NIDS) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080048 mem_size = (nm_i->fcnt * sizeof(struct free_nid)) >>
49 PAGE_CACHE_SHIFT;
50 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090051 } else if (type == NAT_ENTRIES) {
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080052 mem_size = (nm_i->nat_cnt * sizeof(struct nat_entry)) >>
53 PAGE_CACHE_SHIFT;
54 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 2);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090055 } else if (type == DIRTY_DENTS) {
Jaegeuk Kim2743f862014-06-28 01:00:41 +090056 if (sbi->sb->s_bdi->dirty_exceeded)
57 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090058 mem_size = get_pages(sbi, F2FS_DIRTY_DENTS);
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080059 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
60 } else if (type == INO_ENTRIES) {
61 int i;
62
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080063 for (i = 0; i <= UPDATE_INO; i++)
Chao Yu67298802014-11-18 11:18:36 +080064 mem_size += (sbi->im[i].ino_num *
65 sizeof(struct ino_entry)) >> PAGE_CACHE_SHIFT;
Jaegeuk Kime5e7ea32014-11-06 15:24:46 -080066 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Chao Yu429511c2015-02-05 17:54:31 +080067 } else if (type == EXTENT_CACHE) {
68 mem_size = (sbi->total_ext_tree * sizeof(struct extent_tree) +
69 atomic_read(&sbi->total_ext_node) *
70 sizeof(struct extent_node)) >> PAGE_CACHE_SHIFT;
71 res = mem_size < ((avail_ram * nm_i->ram_thresh / 100) >> 1);
Jaegeuk Kim1e843712014-12-09 06:08:59 -080072 } else {
73 if (sbi->sb->s_bdi->dirty_exceeded)
74 return false;
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +090075 }
76 return res;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +090077}
78
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090079static void clear_node_page_dirty(struct page *page)
80{
81 struct address_space *mapping = page->mapping;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090082 unsigned int long flags;
83
84 if (PageDirty(page)) {
85 spin_lock_irqsave(&mapping->tree_lock, flags);
86 radix_tree_tag_clear(&mapping->page_tree,
87 page_index(page),
88 PAGECACHE_TAG_DIRTY);
89 spin_unlock_irqrestore(&mapping->tree_lock, flags);
90
91 clear_page_dirty_for_io(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -070092 dec_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +090093 }
94 ClearPageUptodate(page);
95}
96
97static struct page *get_current_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
98{
99 pgoff_t index = current_nat_addr(sbi, nid);
100 return get_meta_page(sbi, index);
101}
102
103static struct page *get_next_nat_page(struct f2fs_sb_info *sbi, nid_t nid)
104{
105 struct page *src_page;
106 struct page *dst_page;
107 pgoff_t src_off;
108 pgoff_t dst_off;
109 void *src_addr;
110 void *dst_addr;
111 struct f2fs_nm_info *nm_i = NM_I(sbi);
112
113 src_off = current_nat_addr(sbi, nid);
114 dst_off = next_nat_addr(sbi, src_off);
115
116 /* get current nat block page with lock */
117 src_page = get_meta_page(sbi, src_off);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900118 dst_page = grab_meta_page(sbi, dst_off);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700119 f2fs_bug_on(sbi, PageDirty(src_page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900120
121 src_addr = page_address(src_page);
122 dst_addr = page_address(dst_page);
123 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
124 set_page_dirty(dst_page);
125 f2fs_put_page(src_page, 1);
126
127 set_to_next_nat(nm_i, nid);
128
129 return dst_page;
130}
131
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900132static struct nat_entry *__lookup_nat_cache(struct f2fs_nm_info *nm_i, nid_t n)
133{
134 return radix_tree_lookup(&nm_i->nat_root, n);
135}
136
137static unsigned int __gang_lookup_nat_cache(struct f2fs_nm_info *nm_i,
138 nid_t start, unsigned int nr, struct nat_entry **ep)
139{
140 return radix_tree_gang_lookup(&nm_i->nat_root, (void **)ep, start, nr);
141}
142
143static void __del_from_nat_cache(struct f2fs_nm_info *nm_i, struct nat_entry *e)
144{
145 list_del(&e->list);
146 radix_tree_delete(&nm_i->nat_root, nat_get_nid(e));
147 nm_i->nat_cnt--;
148 kmem_cache_free(nat_entry_slab, e);
149}
150
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700151static void __set_nat_cache_dirty(struct f2fs_nm_info *nm_i,
152 struct nat_entry *ne)
153{
154 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
155 struct nat_entry_set *head;
156
157 if (get_nat_flag(ne, IS_DIRTY))
158 return;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800159
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700160 head = radix_tree_lookup(&nm_i->nat_set_root, set);
161 if (!head) {
162 head = f2fs_kmem_cache_alloc(nat_entry_set_slab, GFP_ATOMIC);
163
164 INIT_LIST_HEAD(&head->entry_list);
165 INIT_LIST_HEAD(&head->set_list);
166 head->set = set;
167 head->entry_cnt = 0;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800168 f2fs_radix_tree_insert(&nm_i->nat_set_root, set, head);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700169 }
170 list_move_tail(&ne->list, &head->entry_list);
171 nm_i->dirty_nat_cnt++;
172 head->entry_cnt++;
173 set_nat_flag(ne, IS_DIRTY, true);
174}
175
176static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i,
177 struct nat_entry *ne)
178{
Changman Lee20d047c2014-11-25 12:44:23 +0900179 nid_t set = NAT_BLOCK_OFFSET(ne->ni.nid);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -0700180 struct nat_entry_set *head;
181
182 head = radix_tree_lookup(&nm_i->nat_set_root, set);
183 if (head) {
184 list_move_tail(&ne->list, &nm_i->nat_entries);
185 set_nat_flag(ne, IS_DIRTY, false);
186 head->entry_cnt--;
187 nm_i->dirty_nat_cnt--;
188 }
189}
190
191static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i,
192 nid_t start, unsigned int nr, struct nat_entry_set **ep)
193{
194 return radix_tree_gang_lookup(&nm_i->nat_set_root, (void **)ep,
195 start, nr);
196}
197
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -0700198int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid)
199{
200 struct f2fs_nm_info *nm_i = NM_I(sbi);
201 struct nat_entry *e;
202 bool need = false;
203
204 down_read(&nm_i->nat_tree_lock);
205 e = __lookup_nat_cache(nm_i, nid);
206 if (e) {
207 if (!get_nat_flag(e, IS_CHECKPOINTED) &&
208 !get_nat_flag(e, HAS_FSYNCED_INODE))
209 need = true;
210 }
211 up_read(&nm_i->nat_tree_lock);
212 return need;
213}
214
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700215bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900216{
217 struct f2fs_nm_info *nm_i = NM_I(sbi);
218 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700219 bool is_cp = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900220
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800221 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900222 e = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -0700223 if (e && !get_nat_flag(e, IS_CHECKPOINTED))
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700224 is_cp = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800225 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900226 return is_cp;
227}
228
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700229bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino)
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900230{
231 struct f2fs_nm_info *nm_i = NM_I(sbi);
232 struct nat_entry *e;
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700233 bool need_update = true;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900234
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800235 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700236 e = __lookup_nat_cache(nm_i, ino);
237 if (e && get_nat_flag(e, HAS_LAST_FSYNC) &&
238 (get_nat_flag(e, IS_CHECKPOINTED) ||
239 get_nat_flag(e, HAS_FSYNCED_INODE)))
240 need_update = false;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800241 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700242 return need_update;
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +0900243}
244
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900245static struct nat_entry *grab_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid)
246{
247 struct nat_entry *new;
248
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800249 new = f2fs_kmem_cache_alloc(nat_entry_slab, GFP_ATOMIC);
250 f2fs_radix_tree_insert(&nm_i->nat_root, nid, new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900251 memset(new, 0, sizeof(struct nat_entry));
252 nat_set_nid(new, nid);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700253 nat_reset_flag(new);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900254 list_add_tail(&new->list, &nm_i->nat_entries);
255 nm_i->nat_cnt++;
256 return new;
257}
258
259static void cache_nat_entry(struct f2fs_nm_info *nm_i, nid_t nid,
260 struct f2fs_nat_entry *ne)
261{
262 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800263
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800264 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900265 e = __lookup_nat_cache(nm_i, nid);
266 if (!e) {
267 e = grab_nat_entry(nm_i, nid);
Chao Yu94dac222014-04-17 10:51:05 +0800268 node_info_from_raw_nat(&e->ni, ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900269 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800270 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900271}
272
273static void set_node_addr(struct f2fs_sb_info *sbi, struct node_info *ni,
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900274 block_t new_blkaddr, bool fsync_done)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900275{
276 struct f2fs_nm_info *nm_i = NM_I(sbi);
277 struct nat_entry *e;
Jaegeuk Kim9be32d72014-12-05 10:39:49 -0800278
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800279 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900280 e = __lookup_nat_cache(nm_i, ni->nid);
281 if (!e) {
282 e = grab_nat_entry(nm_i, ni->nid);
Chao Yu5c27f4e2014-12-18 17:37:21 +0800283 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700284 f2fs_bug_on(sbi, ni->blk_addr == NEW_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900285 } else if (new_blkaddr == NEW_ADDR) {
286 /*
287 * when nid is reallocated,
288 * previous nat entry can be remained in nat cache.
289 * So, reinitialize it with new information.
290 */
Chao Yu5c27f4e2014-12-18 17:37:21 +0800291 copy_node_info(&e->ni, ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700292 f2fs_bug_on(sbi, ni->blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900293 }
294
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900295 /* sanity check */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700296 f2fs_bug_on(sbi, nat_get_blkaddr(e) != ni->blk_addr);
297 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NULL_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900298 new_blkaddr == NULL_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700299 f2fs_bug_on(sbi, nat_get_blkaddr(e) == NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900300 new_blkaddr == NEW_ADDR);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700301 f2fs_bug_on(sbi, nat_get_blkaddr(e) != NEW_ADDR &&
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900302 nat_get_blkaddr(e) != NULL_ADDR &&
303 new_blkaddr == NEW_ADDR);
304
arter97e1c42042014-08-06 23:22:50 +0900305 /* increment version no as node is removed */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900306 if (nat_get_blkaddr(e) != NEW_ADDR && new_blkaddr == NULL_ADDR) {
307 unsigned char version = nat_get_version(e);
308 nat_set_version(e, inc_node_version(version));
309 }
310
311 /* change address */
312 nat_set_blkaddr(e, new_blkaddr);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700313 if (new_blkaddr == NEW_ADDR || new_blkaddr == NULL_ADDR)
314 set_nat_flag(e, IS_CHECKPOINTED, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900315 __set_nat_cache_dirty(nm_i, e);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900316
317 /* update fsync_mark if its inode nat entry is still alive */
Chao Yud5b692b2015-04-30 18:35:50 +0800318 if (ni->nid != ni->ino)
319 e = __lookup_nat_cache(nm_i, ni->ino);
Jaegeuk Kim88bd02c2014-09-15 14:50:48 -0700320 if (e) {
321 if (fsync_done && ni->nid == ni->ino)
322 set_nat_flag(e, HAS_FSYNCED_INODE, true);
323 set_nat_flag(e, HAS_LAST_FSYNC, fsync_done);
324 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800325 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900326}
327
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +0900328int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900329{
330 struct f2fs_nm_info *nm_i = NM_I(sbi);
331
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900332 if (available_free_memory(sbi, NAT_ENTRIES))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900333 return 0;
334
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800335 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900336 while (nr_shrink && !list_empty(&nm_i->nat_entries)) {
337 struct nat_entry *ne;
338 ne = list_first_entry(&nm_i->nat_entries,
339 struct nat_entry, list);
340 __del_from_nat_cache(nm_i, ne);
341 nr_shrink--;
342 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800343 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900344 return nr_shrink;
345}
346
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900347/*
arter97e1c42042014-08-06 23:22:50 +0900348 * This function always returns success
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900349 */
350void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni)
351{
352 struct f2fs_nm_info *nm_i = NM_I(sbi);
353 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
354 struct f2fs_summary_block *sum = curseg->sum_blk;
355 nid_t start_nid = START_NID(nid);
356 struct f2fs_nat_block *nat_blk;
357 struct page *page = NULL;
358 struct f2fs_nat_entry ne;
359 struct nat_entry *e;
360 int i;
361
362 ni->nid = nid;
363
364 /* Check nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800365 down_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900366 e = __lookup_nat_cache(nm_i, nid);
367 if (e) {
368 ni->ino = nat_get_ino(e);
369 ni->blk_addr = nat_get_blkaddr(e);
370 ni->version = nat_get_version(e);
371 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -0800372 up_read(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900373 if (e)
374 return;
375
Jaegeuk Kim3547ea92014-12-30 23:08:26 -0800376 memset(&ne, 0, sizeof(struct f2fs_nat_entry));
377
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900378 /* Check current segment summary */
379 mutex_lock(&curseg->curseg_mutex);
380 i = lookup_journal_in_cursum(sum, NAT_JOURNAL, nid, 0);
381 if (i >= 0) {
382 ne = nat_in_journal(sum, i);
383 node_info_from_raw_nat(ni, &ne);
384 }
385 mutex_unlock(&curseg->curseg_mutex);
386 if (i >= 0)
387 goto cache;
388
389 /* Fill node_info from nat page */
390 page = get_current_nat_page(sbi, start_nid);
391 nat_blk = (struct f2fs_nat_block *)page_address(page);
392 ne = nat_blk->entries[nid - start_nid];
393 node_info_from_raw_nat(ni, &ne);
394 f2fs_put_page(page, 1);
395cache:
396 /* cache nat entry */
397 cache_nat_entry(NM_I(sbi), nid, &ne);
398}
399
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900400/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900401 * The maximum depth is four.
402 * Offset[0] will have raw inode offset.
403 */
Jaegeuk Kimde936532013-08-12 21:08:03 +0900404static int get_node_path(struct f2fs_inode_info *fi, long block,
405 int offset[4], unsigned int noffset[4])
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900406{
Jaegeuk Kimde936532013-08-12 21:08:03 +0900407 const long direct_index = ADDRS_PER_INODE(fi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900408 const long direct_blks = ADDRS_PER_BLOCK;
409 const long dptrs_per_blk = NIDS_PER_BLOCK;
410 const long indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
411 const long dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
412 int n = 0;
413 int level = 0;
414
415 noffset[0] = 0;
416
417 if (block < direct_index) {
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900418 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900419 goto got;
420 }
421 block -= direct_index;
422 if (block < direct_blks) {
423 offset[n++] = NODE_DIR1_BLOCK;
424 noffset[n] = 1;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900425 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900426 level = 1;
427 goto got;
428 }
429 block -= direct_blks;
430 if (block < direct_blks) {
431 offset[n++] = NODE_DIR2_BLOCK;
432 noffset[n] = 2;
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900433 offset[n] = block;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900434 level = 1;
435 goto got;
436 }
437 block -= direct_blks;
438 if (block < indirect_blks) {
439 offset[n++] = NODE_IND1_BLOCK;
440 noffset[n] = 3;
441 offset[n++] = block / direct_blks;
442 noffset[n] = 4 + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900443 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900444 level = 2;
445 goto got;
446 }
447 block -= indirect_blks;
448 if (block < indirect_blks) {
449 offset[n++] = NODE_IND2_BLOCK;
450 noffset[n] = 4 + dptrs_per_blk;
451 offset[n++] = block / direct_blks;
452 noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900453 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900454 level = 2;
455 goto got;
456 }
457 block -= indirect_blks;
458 if (block < dindirect_blks) {
459 offset[n++] = NODE_DIND_BLOCK;
460 noffset[n] = 5 + (dptrs_per_blk * 2);
461 offset[n++] = block / indirect_blks;
462 noffset[n] = 6 + (dptrs_per_blk * 2) +
463 offset[n - 1] * (dptrs_per_blk + 1);
464 offset[n++] = (block / direct_blks) % dptrs_per_blk;
465 noffset[n] = 7 + (dptrs_per_blk * 2) +
466 offset[n - 2] * (dptrs_per_blk + 1) +
467 offset[n - 1];
Namjae Jeon25c0a6e2013-03-02 12:41:31 +0900468 offset[n] = block % direct_blks;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900469 level = 3;
470 goto got;
471 } else {
472 BUG();
473 }
474got:
475 return level;
476}
477
478/*
479 * Caller should call f2fs_put_dnode(dn).
Chao Yu4f4124d2013-12-21 18:02:14 +0800480 * Also, it should grab and release a rwsem by calling f2fs_lock_op() and
481 * f2fs_unlock_op() only if ro is not set RDONLY_NODE.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900482 * In the case of RDONLY_NODE, we don't need to care about mutex.
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900483 */
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900484int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900485{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700486 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900487 struct page *npage[4];
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800488 struct page *parent = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900489 int offset[4];
490 unsigned int noffset[4];
491 nid_t nids[4];
492 int level, i;
493 int err = 0;
494
Jaegeuk Kimde936532013-08-12 21:08:03 +0900495 level = get_node_path(F2FS_I(dn->inode), index, offset, noffset);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900496
497 nids[0] = dn->inode->i_ino;
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900498 npage[0] = dn->inode_page;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900499
Jaegeuk Kim1646cfa2013-05-20 09:42:28 +0900500 if (!npage[0]) {
501 npage[0] = get_node_page(sbi, nids[0]);
502 if (IS_ERR(npage[0]))
503 return PTR_ERR(npage[0]);
504 }
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800505
506 /* if inline_data is set, should not report any block indices */
507 if (f2fs_has_inline_data(dn->inode) && index) {
Jaegeuk Kim76629162015-03-02 16:28:16 -0800508 err = -ENOENT;
Jaegeuk Kimf1a3b982015-02-11 11:25:11 -0800509 f2fs_put_page(npage[0], 1);
510 goto release_out;
511 }
512
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900513 parent = npage[0];
Changman Lee52c2db32013-02-20 07:47:06 +0900514 if (level != 0)
515 nids[1] = get_nid(parent, offset[0], true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900516 dn->inode_page = npage[0];
517 dn->inode_page_locked = true;
518
519 /* get indirect or direct nodes */
520 for (i = 1; i <= level; i++) {
521 bool done = false;
522
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900523 if (!nids[i] && mode == ALLOC_NODE) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900524 /* alloc new node */
525 if (!alloc_nid(sbi, &(nids[i]))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900526 err = -ENOSPC;
527 goto release_pages;
528 }
529
530 dn->nid = nids[i];
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900531 npage[i] = new_node_page(dn, noffset[i], NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900532 if (IS_ERR(npage[i])) {
533 alloc_nid_failed(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900534 err = PTR_ERR(npage[i]);
535 goto release_pages;
536 }
537
538 set_nid(parent, offset[i - 1], nids[i], i == 1);
539 alloc_nid_done(sbi, nids[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900540 done = true;
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900541 } else if (mode == LOOKUP_NODE_RA && i == level && level > 1) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900542 npage[i] = get_node_page_ra(parent, offset[i - 1]);
543 if (IS_ERR(npage[i])) {
544 err = PTR_ERR(npage[i]);
545 goto release_pages;
546 }
547 done = true;
548 }
549 if (i == 1) {
550 dn->inode_page_locked = false;
551 unlock_page(parent);
552 } else {
553 f2fs_put_page(parent, 1);
554 }
555
556 if (!done) {
557 npage[i] = get_node_page(sbi, nids[i]);
558 if (IS_ERR(npage[i])) {
559 err = PTR_ERR(npage[i]);
560 f2fs_put_page(npage[0], 0);
561 goto release_out;
562 }
563 }
564 if (i < level) {
565 parent = npage[i];
566 nids[i + 1] = get_nid(parent, offset[i], false);
567 }
568 }
569 dn->nid = nids[level];
570 dn->ofs_in_node = offset[level];
571 dn->node_page = npage[level];
572 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
573 return 0;
574
575release_pages:
576 f2fs_put_page(parent, 1);
577 if (i > 1)
578 f2fs_put_page(npage[0], 0);
579release_out:
580 dn->inode_page = NULL;
581 dn->node_page = NULL;
582 return err;
583}
584
585static void truncate_node(struct dnode_of_data *dn)
586{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700587 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900588 struct node_info ni;
589
590 get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900591 if (dn->inode->i_blocks == 0) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700592 f2fs_bug_on(sbi, ni.blk_addr != NULL_ADDR);
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900593 goto invalidate;
594 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700595 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900596
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900597 /* Deallocate node address */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900598 invalidate_blocks(sbi, ni.blk_addr);
Gu Zhengef86d702013-11-19 18:03:38 +0800599 dec_valid_node_count(sbi, dn->inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900600 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900601
602 if (dn->nid == dn->inode->i_ino) {
603 remove_orphan_inode(sbi, dn->nid);
604 dec_valid_inode_count(sbi);
605 } else {
606 sync_inode_page(dn);
607 }
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900608invalidate:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900609 clear_node_page_dirty(dn->node_page);
Chao Yucaf00472015-01-28 17:48:42 +0800610 set_sbi_flag(sbi, SBI_IS_DIRTY);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900611
612 f2fs_put_page(dn->node_page, 1);
Jaegeuk Kimbf39c002014-01-22 20:41:57 +0900613
614 invalidate_mapping_pages(NODE_MAPPING(sbi),
615 dn->node_page->index, dn->node_page->index);
616
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900617 dn->node_page = NULL;
Namjae Jeon51dd6242013-04-20 01:28:52 +0900618 trace_f2fs_truncate_node(dn->inode, dn->nid, ni.blk_addr);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900619}
620
621static int truncate_dnode(struct dnode_of_data *dn)
622{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900623 struct page *page;
624
625 if (dn->nid == 0)
626 return 1;
627
628 /* get direct node */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700629 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900630 if (IS_ERR(page) && PTR_ERR(page) == -ENOENT)
631 return 1;
632 else if (IS_ERR(page))
633 return PTR_ERR(page);
634
635 /* Make dnode_of_data for parameter */
636 dn->node_page = page;
637 dn->ofs_in_node = 0;
638 truncate_data_blocks(dn);
639 truncate_node(dn);
640 return 1;
641}
642
643static int truncate_nodes(struct dnode_of_data *dn, unsigned int nofs,
644 int ofs, int depth)
645{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900646 struct dnode_of_data rdn = *dn;
647 struct page *page;
648 struct f2fs_node *rn;
649 nid_t child_nid;
650 unsigned int child_nofs;
651 int freed = 0;
652 int i, ret;
653
654 if (dn->nid == 0)
655 return NIDS_PER_BLOCK + 1;
656
Namjae Jeon51dd6242013-04-20 01:28:52 +0900657 trace_f2fs_truncate_nodes_enter(dn->inode, dn->nid, dn->data_blkaddr);
658
Jaegeuk Kim40813632014-09-02 15:31:18 -0700659 page = get_node_page(F2FS_I_SB(dn->inode), dn->nid);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900660 if (IS_ERR(page)) {
661 trace_f2fs_truncate_nodes_exit(dn->inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900662 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900663 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900664
Gu Zheng45590712013-07-15 17:57:38 +0800665 rn = F2FS_NODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900666 if (depth < 3) {
667 for (i = ofs; i < NIDS_PER_BLOCK; i++, freed++) {
668 child_nid = le32_to_cpu(rn->in.nid[i]);
669 if (child_nid == 0)
670 continue;
671 rdn.nid = child_nid;
672 ret = truncate_dnode(&rdn);
673 if (ret < 0)
674 goto out_err;
675 set_nid(page, i, 0, false);
676 }
677 } else {
678 child_nofs = nofs + ofs * (NIDS_PER_BLOCK + 1) + 1;
679 for (i = ofs; i < NIDS_PER_BLOCK; i++) {
680 child_nid = le32_to_cpu(rn->in.nid[i]);
681 if (child_nid == 0) {
682 child_nofs += NIDS_PER_BLOCK + 1;
683 continue;
684 }
685 rdn.nid = child_nid;
686 ret = truncate_nodes(&rdn, child_nofs, 0, depth - 1);
687 if (ret == (NIDS_PER_BLOCK + 1)) {
688 set_nid(page, i, 0, false);
689 child_nofs += ret;
690 } else if (ret < 0 && ret != -ENOENT) {
691 goto out_err;
692 }
693 }
694 freed = child_nofs;
695 }
696
697 if (!ofs) {
698 /* remove current indirect node */
699 dn->node_page = page;
700 truncate_node(dn);
701 freed++;
702 } else {
703 f2fs_put_page(page, 1);
704 }
Namjae Jeon51dd6242013-04-20 01:28:52 +0900705 trace_f2fs_truncate_nodes_exit(dn->inode, freed);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900706 return freed;
707
708out_err:
709 f2fs_put_page(page, 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900710 trace_f2fs_truncate_nodes_exit(dn->inode, ret);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900711 return ret;
712}
713
714static int truncate_partial_nodes(struct dnode_of_data *dn,
715 struct f2fs_inode *ri, int *offset, int depth)
716{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900717 struct page *pages[2];
718 nid_t nid[3];
719 nid_t child_nid;
720 int err = 0;
721 int i;
722 int idx = depth - 2;
723
724 nid[0] = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
725 if (!nid[0])
726 return 0;
727
728 /* get indirect nodes in the path */
shifei10.gea225dca2013-10-29 15:32:34 +0800729 for (i = 0; i < idx + 1; i++) {
arter97e1c42042014-08-06 23:22:50 +0900730 /* reference count'll be increased */
Jaegeuk Kim40813632014-09-02 15:31:18 -0700731 pages[i] = get_node_page(F2FS_I_SB(dn->inode), nid[i]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900732 if (IS_ERR(pages[i])) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900733 err = PTR_ERR(pages[i]);
shifei10.gea225dca2013-10-29 15:32:34 +0800734 idx = i - 1;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900735 goto fail;
736 }
737 nid[i + 1] = get_nid(pages[i], offset[i + 1], false);
738 }
739
740 /* free direct nodes linked to a partial indirect node */
shifei10.gea225dca2013-10-29 15:32:34 +0800741 for (i = offset[idx + 1]; i < NIDS_PER_BLOCK; i++) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900742 child_nid = get_nid(pages[idx], i, false);
743 if (!child_nid)
744 continue;
745 dn->nid = child_nid;
746 err = truncate_dnode(dn);
747 if (err < 0)
748 goto fail;
749 set_nid(pages[idx], i, 0, false);
750 }
751
shifei10.gea225dca2013-10-29 15:32:34 +0800752 if (offset[idx + 1] == 0) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900753 dn->node_page = pages[idx];
754 dn->nid = nid[idx];
755 truncate_node(dn);
756 } else {
757 f2fs_put_page(pages[idx], 1);
758 }
759 offset[idx]++;
shifei10.gea225dca2013-10-29 15:32:34 +0800760 offset[idx + 1] = 0;
761 idx--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900762fail:
shifei10.gea225dca2013-10-29 15:32:34 +0800763 for (i = idx; i >= 0; i--)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900764 f2fs_put_page(pages[i], 1);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900765
766 trace_f2fs_truncate_partial_nodes(dn->inode, nid, depth, err);
767
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900768 return err;
769}
770
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900771/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900772 * All the block addresses of data and nodes should be nullified.
773 */
774int truncate_inode_blocks(struct inode *inode, pgoff_t from)
775{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700776 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900777 int err = 0, cont = 1;
778 int level, offset[4], noffset[4];
Jaegeuk Kim7dd690c2013-02-12 07:28:55 +0900779 unsigned int nofs = 0;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900780 struct f2fs_inode *ri;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900781 struct dnode_of_data dn;
782 struct page *page;
783
Namjae Jeon51dd6242013-04-20 01:28:52 +0900784 trace_f2fs_truncate_inode_blocks_enter(inode, from);
785
Jaegeuk Kimde936532013-08-12 21:08:03 +0900786 level = get_node_path(F2FS_I(inode), from, offset, noffset);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900787restart:
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900788 page = get_node_page(sbi, inode->i_ino);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900789 if (IS_ERR(page)) {
790 trace_f2fs_truncate_inode_blocks_exit(inode, PTR_ERR(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900791 return PTR_ERR(page);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900792 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900793
794 set_new_dnode(&dn, inode, page, NULL, 0);
795 unlock_page(page);
796
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900797 ri = F2FS_INODE(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900798 switch (level) {
799 case 0:
800 case 1:
801 nofs = noffset[1];
802 break;
803 case 2:
804 nofs = noffset[1];
805 if (!offset[level - 1])
806 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900807 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900808 if (err < 0 && err != -ENOENT)
809 goto fail;
810 nofs += 1 + NIDS_PER_BLOCK;
811 break;
812 case 3:
813 nofs = 5 + 2 * NIDS_PER_BLOCK;
814 if (!offset[level - 1])
815 goto skip_partial;
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900816 err = truncate_partial_nodes(&dn, ri, offset, level);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900817 if (err < 0 && err != -ENOENT)
818 goto fail;
819 break;
820 default:
821 BUG();
822 }
823
824skip_partial:
825 while (cont) {
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900826 dn.nid = le32_to_cpu(ri->i_nid[offset[0] - NODE_DIR1_BLOCK]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900827 switch (offset[0]) {
828 case NODE_DIR1_BLOCK:
829 case NODE_DIR2_BLOCK:
830 err = truncate_dnode(&dn);
831 break;
832
833 case NODE_IND1_BLOCK:
834 case NODE_IND2_BLOCK:
835 err = truncate_nodes(&dn, nofs, offset[1], 2);
836 break;
837
838 case NODE_DIND_BLOCK:
839 err = truncate_nodes(&dn, nofs, offset[1], 3);
840 cont = 0;
841 break;
842
843 default:
844 BUG();
845 }
846 if (err < 0 && err != -ENOENT)
847 goto fail;
848 if (offset[1] == 0 &&
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900849 ri->i_nid[offset[0] - NODE_DIR1_BLOCK]) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900850 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +0900851 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900852 f2fs_put_page(page, 1);
853 goto restart;
854 }
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +0900855 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +0900856 ri->i_nid[offset[0] - NODE_DIR1_BLOCK] = 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900857 set_page_dirty(page);
858 unlock_page(page);
859 }
860 offset[1] = 0;
861 offset[0]++;
862 nofs += err;
863 }
864fail:
865 f2fs_put_page(page, 0);
Namjae Jeon51dd6242013-04-20 01:28:52 +0900866 trace_f2fs_truncate_inode_blocks_exit(inode, err);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900867 return err > 0 ? 0 : err;
868}
869
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900870int truncate_xattr_node(struct inode *inode, struct page *page)
871{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700872 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900873 nid_t nid = F2FS_I(inode)->i_xattr_nid;
874 struct dnode_of_data dn;
875 struct page *npage;
876
877 if (!nid)
878 return 0;
879
880 npage = get_node_page(sbi, nid);
881 if (IS_ERR(npage))
882 return PTR_ERR(npage);
883
884 F2FS_I(inode)->i_xattr_nid = 0;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900885
886 /* need to do checkpoint during fsync */
887 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
888
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900889 set_new_dnode(&dn, inode, page, npage, nid);
890
891 if (page)
Chao Yu01d2d1a2013-11-28 15:43:07 +0800892 dn.inode_page_locked = true;
Jaegeuk Kim4f16fb02013-08-14 20:40:06 +0900893 truncate_node(&dn);
894 return 0;
895}
896
Jaegeuk Kim39936832012-11-22 16:21:29 +0900897/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800898 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
899 * f2fs_unlock_op().
Jaegeuk Kim39936832012-11-22 16:21:29 +0900900 */
Gu Zheng58e674d2013-11-19 18:03:18 +0800901void remove_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900902{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900903 struct dnode_of_data dn;
904
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700905 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
906 if (get_dnode_of_data(&dn, 0, LOOKUP_NODE))
Gu Zheng58e674d2013-11-19 18:03:18 +0800907 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900908
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700909 if (truncate_xattr_node(inode, dn.inode_page)) {
910 f2fs_put_dnode(&dn);
Gu Zheng58e674d2013-11-19 18:03:18 +0800911 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900912 }
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700913
914 /* remove potential inline_data blocks */
915 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
916 S_ISLNK(inode->i_mode))
917 truncate_data_blocks_range(&dn, 1);
918
arter97e1c42042014-08-06 23:22:50 +0900919 /* 0 is possible, after f2fs_new_inode() has failed */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700920 f2fs_bug_on(F2FS_I_SB(inode),
921 inode->i_blocks != 0 && inode->i_blocks != 1);
Jaegeuk Kimc2e69582014-08-25 14:45:59 -0700922
923 /* will put inode & node pages */
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900924 truncate_node(&dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900925}
926
Jaegeuk Kima014e032014-06-20 21:44:02 -0700927struct page *new_inode_page(struct inode *inode)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900928{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900929 struct dnode_of_data dn;
930
931 /* allocate inode page for new inode */
932 set_new_dnode(&dn, inode, NULL, NULL, inode->i_ino);
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900933
934 /* caller should f2fs_put_page(page, 1); */
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900935 return new_node_page(&dn, 0, NULL);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900936}
937
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900938struct page *new_node_page(struct dnode_of_data *dn,
939 unsigned int ofs, struct page *ipage)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900940{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700941 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900942 struct node_info old_ni, new_ni;
943 struct page *page;
944 int err;
945
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900946 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900947 return ERR_PTR(-EPERM);
948
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900949 page = grab_cache_page(NODE_MAPPING(sbi), dn->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900950 if (!page)
951 return ERR_PTR(-ENOMEM);
952
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900953 if (unlikely(!inc_valid_node_count(sbi, dn->inode))) {
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900954 err = -ENOSPC;
955 goto fail;
956 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900957
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900958 get_node_info(sbi, dn->nid, &old_ni);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900959
960 /* Reinitialize old_ni with new node page */
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700961 f2fs_bug_on(sbi, old_ni.blk_addr != NULL_ADDR);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900962 new_ni = old_ni;
963 new_ni.ino = dn->inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +0900964 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900965
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900966 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900967 fill_node_footer(page, dn->nid, dn->inode->i_ino, ofs, true);
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900968 set_cold_node(dn->inode, page);
Jaegeuk Kim9c027402013-08-12 16:00:46 +0900969 SetPageUptodate(page);
970 set_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900971
Chao Yu4bc8e9b2014-03-17 16:35:06 +0800972 if (f2fs_has_xattr_block(ofs))
Jaegeuk Kim479bd732013-08-12 16:04:53 +0900973 F2FS_I(dn->inode)->i_xattr_nid = dn->nid;
974
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900975 dn->node_page = page;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900976 if (ipage)
977 update_inode(dn->inode, ipage);
978 else
979 sync_inode_page(dn);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900980 if (ofs == 0)
981 inc_valid_inode_count(sbi);
982
983 return page;
984
985fail:
Jaegeuk Kim71e9fec2012-12-20 15:10:06 +0900986 clear_node_page_dirty(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900987 f2fs_put_page(page, 1);
988 return ERR_PTR(err);
989}
990
Jaegeuk Kim56ae6742013-03-31 12:47:20 +0900991/*
992 * Caller should do after getting the following values.
993 * 0: f2fs_put_page(page, 0)
994 * LOCKED_PAGE: f2fs_put_page(page, 1)
995 * error: nothing
996 */
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900997static int read_node_page(struct page *page, int rw)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +0900998{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700999 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001000 struct node_info ni;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001001 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001002 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001003 .type = NODE,
1004 .rw = rw,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001005 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001006 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001007 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001008
1009 get_node_info(sbi, page->index, &ni);
1010
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001011 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001012 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001013 f2fs_put_page(page, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001014 return -ENOENT;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001015 }
1016
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001017 if (PageUptodate(page))
1018 return LOCKED_PAGE;
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001019
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001020 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001021 return f2fs_submit_page_bio(&fio);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001022}
1023
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001024/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001025 * Readahead a node page
1026 */
1027void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)
1028{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001029 struct page *apage;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001030 int err;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001031
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001032 apage = find_get_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001033 if (apage && PageUptodate(apage)) {
1034 f2fs_put_page(apage, 0);
1035 return;
1036 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001037 f2fs_put_page(apage, 0);
1038
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001039 apage = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001040 if (!apage)
1041 return;
1042
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001043 err = read_node_page(apage, READA);
1044 if (err == 0)
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001045 f2fs_put_page(apage, 0);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001046 else if (err == LOCKED_PAGE)
1047 f2fs_put_page(apage, 1);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001048}
1049
1050struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid)
1051{
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001052 struct page *page;
1053 int err;
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001054repeat:
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001055 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001056 if (!page)
1057 return ERR_PTR(-ENOMEM);
1058
1059 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001060 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001061 return ERR_PTR(err);
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001062 else if (err != LOCKED_PAGE)
1063 lock_page(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001064
Jaegeuk Kim3bb5e2c2014-04-01 17:38:26 +09001065 if (unlikely(!PageUptodate(page) || nid != nid_of_node(page))) {
Jaegeuk Kimaaf96072015-02-06 18:53:45 -08001066 ClearPageUptodate(page);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001067 f2fs_put_page(page, 1);
1068 return ERR_PTR(-EIO);
1069 }
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001070 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001071 f2fs_put_page(page, 1);
1072 goto repeat;
1073 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001074 return page;
1075}
1076
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001077/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001078 * Return a locked page for the desired node page.
1079 * And, readahead MAX_RA_NODE number of node pages.
1080 */
1081struct page *get_node_page_ra(struct page *parent, int start)
1082{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001083 struct f2fs_sb_info *sbi = F2FS_P_SB(parent);
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001084 struct blk_plug plug;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001085 struct page *page;
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001086 int err, i, end;
1087 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001088
1089 /* First, try getting the desired direct node. */
1090 nid = get_nid(parent, start, false);
1091 if (!nid)
1092 return ERR_PTR(-ENOENT);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001093repeat:
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001094 page = grab_cache_page(NODE_MAPPING(sbi), nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001095 if (!page)
1096 return ERR_PTR(-ENOMEM);
1097
Jaegeuk Kim66d36a22013-02-26 12:43:46 +09001098 err = read_node_page(page, READ_SYNC);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001099 if (err < 0)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001100 return ERR_PTR(err);
Jaegeuk Kim56ae6742013-03-31 12:47:20 +09001101 else if (err == LOCKED_PAGE)
1102 goto page_hit;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001103
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001104 blk_start_plug(&plug);
1105
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001106 /* Then, try readahead for siblings of the desired node */
1107 end = start + MAX_RA_NODE;
1108 end = min(end, NIDS_PER_BLOCK);
1109 for (i = start + 1; i < end; i++) {
1110 nid = get_nid(parent, i, false);
1111 if (!nid)
1112 continue;
1113 ra_node_page(sbi, nid);
1114 }
1115
Jaegeuk Kimc718379b2013-04-24 13:19:56 +09001116 blk_finish_plug(&plug);
1117
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001118 lock_page(page);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001119 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001120 f2fs_put_page(page, 1);
1121 goto repeat;
1122 }
Namjae Jeone0f56cb2013-02-02 23:51:51 +09001123page_hit:
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001124 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001125 f2fs_put_page(page, 1);
1126 return ERR_PTR(-EIO);
1127 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001128 return page;
1129}
1130
1131void sync_inode_page(struct dnode_of_data *dn)
1132{
1133 if (IS_INODE(dn->node_page) || dn->inode_page == dn->node_page) {
1134 update_inode(dn->inode, dn->node_page);
1135 } else if (dn->inode_page) {
1136 if (!dn->inode_page_locked)
1137 lock_page(dn->inode_page);
1138 update_inode(dn->inode, dn->inode_page);
1139 if (!dn->inode_page_locked)
1140 unlock_page(dn->inode_page);
1141 } else {
Jaegeuk Kim39936832012-11-22 16:21:29 +09001142 update_inode_page(dn->inode);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001143 }
1144}
1145
1146int sync_node_pages(struct f2fs_sb_info *sbi, nid_t ino,
1147 struct writeback_control *wbc)
1148{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001149 pgoff_t index, end;
1150 struct pagevec pvec;
1151 int step = ino ? 2 : 0;
1152 int nwritten = 0, wrote = 0;
1153
1154 pagevec_init(&pvec, 0);
1155
1156next_step:
1157 index = 0;
1158 end = LONG_MAX;
1159
1160 while (index <= end) {
1161 int i, nr_pages;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001162 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001163 PAGECACHE_TAG_DIRTY,
1164 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1165 if (nr_pages == 0)
1166 break;
1167
1168 for (i = 0; i < nr_pages; i++) {
1169 struct page *page = pvec.pages[i];
1170
1171 /*
1172 * flushing sequence with step:
1173 * 0. indirect nodes
1174 * 1. dentry dnodes
1175 * 2. file dnodes
1176 */
1177 if (step == 0 && IS_DNODE(page))
1178 continue;
1179 if (step == 1 && (!IS_DNODE(page) ||
1180 is_cold_node(page)))
1181 continue;
1182 if (step == 2 && (!IS_DNODE(page) ||
1183 !is_cold_node(page)))
1184 continue;
1185
1186 /*
1187 * If an fsync mode,
1188 * we should not skip writing node pages.
1189 */
1190 if (ino && ino_of_node(page) == ino)
1191 lock_page(page);
1192 else if (!trylock_page(page))
1193 continue;
1194
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001195 if (unlikely(page->mapping != NODE_MAPPING(sbi))) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001196continue_unlock:
1197 unlock_page(page);
1198 continue;
1199 }
1200 if (ino && ino_of_node(page) != ino)
1201 goto continue_unlock;
1202
1203 if (!PageDirty(page)) {
1204 /* someone wrote it for us */
1205 goto continue_unlock;
1206 }
1207
1208 if (!clear_page_dirty_for_io(page))
1209 goto continue_unlock;
1210
1211 /* called by fsync() */
1212 if (ino && IS_DNODE(page)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001213 set_fsync_mark(page, 1);
Jaegeuk Kim2dcf51a2015-04-29 18:31:19 -07001214 if (IS_INODE(page))
1215 set_dentry_mark(page,
1216 need_dentry_mark(sbi, ino));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001217 nwritten++;
1218 } else {
1219 set_fsync_mark(page, 0);
1220 set_dentry_mark(page, 0);
1221 }
Jaegeuk Kim52746512014-08-11 18:18:36 -07001222
1223 if (NODE_MAPPING(sbi)->a_ops->writepage(page, wbc))
1224 unlock_page(page);
1225 else
1226 wrote++;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001227
1228 if (--wbc->nr_to_write == 0)
1229 break;
1230 }
1231 pagevec_release(&pvec);
1232 cond_resched();
1233
1234 if (wbc->nr_to_write == 0) {
1235 step = 2;
1236 break;
1237 }
1238 }
1239
1240 if (step < 2) {
1241 step++;
1242 goto next_step;
1243 }
1244
1245 if (wrote)
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001246 f2fs_submit_merged_bio(sbi, NODE, WRITE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001247 return nwritten;
1248}
1249
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001250int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
1251{
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001252 pgoff_t index = 0, end = LONG_MAX;
1253 struct pagevec pvec;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001254 int ret2 = 0, ret = 0;
1255
1256 pagevec_init(&pvec, 0);
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001257
1258 while (index <= end) {
1259 int i, nr_pages;
1260 nr_pages = pagevec_lookup_tag(&pvec, NODE_MAPPING(sbi), &index,
1261 PAGECACHE_TAG_WRITEBACK,
1262 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1263 if (nr_pages == 0)
1264 break;
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001265
1266 for (i = 0; i < nr_pages; i++) {
1267 struct page *page = pvec.pages[i];
1268
1269 /* until radix tree lookup accepts end_index */
Chao Yucfb271d2013-12-05 17:15:22 +08001270 if (unlikely(page->index > end))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001271 continue;
1272
Chao Yu4bf08ff2013-11-04 10:28:33 +08001273 if (ino && ino_of_node(page) == ino) {
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001274 f2fs_wait_on_page_writeback(page, NODE);
Chao Yu4bf08ff2013-11-04 10:28:33 +08001275 if (TestClearPageError(page))
1276 ret = -EIO;
1277 }
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001278 }
1279 pagevec_release(&pvec);
1280 cond_resched();
1281 }
1282
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001283 if (unlikely(test_and_clear_bit(AS_ENOSPC, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001284 ret2 = -ENOSPC;
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001285 if (unlikely(test_and_clear_bit(AS_EIO, &NODE_MAPPING(sbi)->flags)))
Jaegeuk Kimcfe58f92013-10-31 14:57:01 +09001286 ret2 = -EIO;
1287 if (!ret)
1288 ret = ret2;
1289 return ret;
1290}
1291
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001292static int f2fs_write_node_page(struct page *page,
1293 struct writeback_control *wbc)
1294{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001295 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001296 nid_t nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001297 struct node_info ni;
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001298 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001299 .sbi = sbi,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001300 .type = NODE,
Chris Fries6c311ec2014-01-17 14:44:39 -06001301 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001302 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001303 .encrypted_page = NULL,
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001304 };
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001305
Chao Yuecda0de2014-05-06 16:48:26 +08001306 trace_f2fs_writepage(page, NODE);
1307
Chao Yucaf00472015-01-28 17:48:42 +08001308 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001309 goto redirty_out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001310 if (unlikely(f2fs_cp_error(sbi)))
1311 goto redirty_out;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001312
Jaegeuk Kim3cb5ad12014-03-18 13:29:07 +09001313 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001314
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001315 /* get old block addr of this node page */
1316 nid = nid_of_node(page);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001317 f2fs_bug_on(sbi, page->index != nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001318
1319 get_node_info(sbi, nid, &ni);
1320
1321 /* This page is already truncated */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001322 if (unlikely(ni.blk_addr == NULL_ADDR)) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001323 ClearPageUptodate(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001324 dec_page_count(sbi, F2FS_DIRTY_NODES);
1325 unlock_page(page);
1326 return 0;
1327 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001328
Jaegeuk Kim2f97c322014-11-06 17:21:24 -08001329 if (wbc->for_reclaim) {
1330 if (!down_read_trylock(&sbi->node_write))
1331 goto redirty_out;
1332 } else {
1333 down_read(&sbi->node_write);
1334 }
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001335
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001336 set_page_writeback(page);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001337 fio.blk_addr = ni.blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001338 write_node_page(nid, &fio);
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001339 set_node_addr(sbi, &ni, fio.blk_addr, is_fsync_dnode(page));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001340 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yub3582c62014-07-03 18:58:39 +08001341 up_read(&sbi->node_write);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001342 unlock_page(page);
Jaegeuk Kim27c6bd62014-11-19 10:54:48 -08001343
1344 if (wbc->for_reclaim)
1345 f2fs_submit_merged_bio(sbi, NODE, WRITE);
1346
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001347 return 0;
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001348
1349redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001350 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim87a9bd22013-10-16 15:09:26 +09001351 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001352}
1353
1354static int f2fs_write_node_pages(struct address_space *mapping,
1355 struct writeback_control *wbc)
1356{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001357 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001358 long diff;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001359
Chao Yue5748432014-05-06 16:51:24 +08001360 trace_f2fs_writepages(mapping->host, wbc, NODE);
1361
Jaegeuk Kim4660f9c2013-10-24 14:19:18 +09001362 /* balancing f2fs's metadata in background */
1363 f2fs_balance_fs_bg(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001364
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001365 /* collect a number of dirty node pages and write together */
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001366 if (get_pages(sbi, F2FS_DIRTY_NODES) < nr_pages_to_skip(sbi, NODE))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001367 goto skip_write;
Jaegeuk Kima7fdffb2013-01-18 14:54:13 +09001368
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001369 diff = nr_pages_to_write(sbi, NODE, wbc);
Jaegeuk Kimfb5566d2014-01-08 10:09:51 +09001370 wbc->sync_mode = WB_SYNC_NONE;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001371 sync_node_pages(sbi, 0, wbc);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001372 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001373 return 0;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001374
1375skip_write:
1376 wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_NODES);
1377 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001378}
1379
1380static int f2fs_set_node_page_dirty(struct page *page)
1381{
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001382 trace_f2fs_set_page_dirty(page, NODE);
1383
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001384 SetPageUptodate(page);
1385 if (!PageDirty(page)) {
1386 __set_page_dirty_nobuffers(page);
Jaegeuk Kim40813632014-09-02 15:31:18 -07001387 inc_page_count(F2FS_P_SB(page), F2FS_DIRTY_NODES);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001388 SetPagePrivate(page);
Jaegeuk Kim9e4ded32014-12-17 19:58:58 -08001389 f2fs_trace_pid(page);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001390 return 1;
1391 }
1392 return 0;
1393}
1394
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001395/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001396 * Structure of the f2fs node operations
1397 */
1398const struct address_space_operations f2fs_node_aops = {
1399 .writepage = f2fs_write_node_page,
1400 .writepages = f2fs_write_node_pages,
1401 .set_page_dirty = f2fs_set_node_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001402 .invalidatepage = f2fs_invalidate_page,
1403 .releasepage = f2fs_release_page,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001404};
1405
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001406static struct free_nid *__lookup_free_nid_list(struct f2fs_nm_info *nm_i,
1407 nid_t n)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001408{
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001409 return radix_tree_lookup(&nm_i->free_nid_root, n);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001410}
1411
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001412static void __del_from_free_nid_list(struct f2fs_nm_info *nm_i,
1413 struct free_nid *i)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001414{
1415 list_del(&i->list);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001416 radix_tree_delete(&nm_i->free_nid_root, i->nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001417}
1418
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001419static int add_free_nid(struct f2fs_sb_info *sbi, nid_t nid, bool build)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001420{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001421 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001422 struct free_nid *i;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001423 struct nat_entry *ne;
1424 bool allocated = false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001425
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001426 if (!available_free_memory(sbi, FREE_NIDS))
Haicheng Li23d38842013-05-06 23:15:43 +08001427 return -1;
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001428
1429 /* 0 nid should not be used */
Chao Yucfb271d2013-12-05 17:15:22 +08001430 if (unlikely(nid == 0))
Jaegeuk Kim9198ace2013-04-25 13:21:12 +09001431 return 0;
Jaegeuk Kim59bbd472013-05-07 20:47:40 +09001432
Gu Zheng7bd59382013-10-22 14:52:26 +08001433 if (build) {
1434 /* do not add allocated nids */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001435 down_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001436 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001437 if (ne &&
Jaegeuk Kim7ef35e32014-09-15 12:07:13 -07001438 (!get_nat_flag(ne, IS_CHECKPOINTED) ||
1439 nat_get_blkaddr(ne) != NULL_ADDR))
Gu Zheng7bd59382013-10-22 14:52:26 +08001440 allocated = true;
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001441 up_read(&nm_i->nat_tree_lock);
Gu Zheng7bd59382013-10-22 14:52:26 +08001442 if (allocated)
1443 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001444 }
Gu Zheng7bd59382013-10-22 14:52:26 +08001445
1446 i = f2fs_kmem_cache_alloc(free_nid_slab, GFP_NOFS);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001447 i->nid = nid;
1448 i->state = NID_NEW;
1449
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001450 if (radix_tree_preload(GFP_NOFS)) {
1451 kmem_cache_free(free_nid_slab, i);
1452 return 0;
1453 }
1454
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001455 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001456 if (radix_tree_insert(&nm_i->free_nid_root, i->nid, i)) {
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001457 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 kmem_cache_free(free_nid_slab, i);
1460 return 0;
1461 }
1462 list_add_tail(&i->list, &nm_i->free_nid_list);
1463 nm_i->fcnt++;
1464 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001465 radix_tree_preload_end();
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001466 return 1;
1467}
1468
1469static void remove_free_nid(struct f2fs_nm_info *nm_i, nid_t nid)
1470{
1471 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001472 bool need_free = false;
1473
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001474 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001475 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001476 if (i && i->state == NID_NEW) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001477 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001478 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001479 need_free = true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001480 }
1481 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001482
1483 if (need_free)
1484 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001485}
1486
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001487static void scan_nat_page(struct f2fs_sb_info *sbi,
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001488 struct page *nat_page, nid_t start_nid)
1489{
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001490 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001491 struct f2fs_nat_block *nat_blk = page_address(nat_page);
1492 block_t blk_addr;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001493 int i;
1494
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001495 i = start_nid % NAT_ENTRY_PER_BLOCK;
1496
1497 for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
Haicheng Li23d38842013-05-06 23:15:43 +08001498
Chao Yucfb271d2013-12-05 17:15:22 +08001499 if (unlikely(start_nid >= nm_i->max_nid))
Jaegeuk Kim04431c42013-03-16 08:34:37 +09001500 break;
Haicheng Li23d38842013-05-06 23:15:43 +08001501
1502 blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001503 f2fs_bug_on(sbi, blk_addr == NEW_ADDR);
Haicheng Li23d38842013-05-06 23:15:43 +08001504 if (blk_addr == NULL_ADDR) {
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001505 if (add_free_nid(sbi, start_nid, true) < 0)
Haicheng Li23d38842013-05-06 23:15:43 +08001506 break;
1507 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001508 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001509}
1510
1511static void build_free_nids(struct f2fs_sb_info *sbi)
1512{
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001513 struct f2fs_nm_info *nm_i = NM_I(sbi);
1514 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1515 struct f2fs_summary_block *sum = curseg->sum_blk;
Haicheng Li87609522013-05-06 23:15:42 +08001516 int i = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001517 nid_t nid = nm_i->next_scan_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001518
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001519 /* Enough entries */
1520 if (nm_i->fcnt > NAT_ENTRY_PER_BLOCK)
1521 return;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001522
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001523 /* readahead nat pages to be scanned */
Chao Yu662befd2014-02-07 16:11:53 +08001524 ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), FREE_NID_PAGES, META_NAT);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001525
1526 while (1) {
1527 struct page *page = get_current_nat_page(sbi, nid);
1528
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001529 scan_nat_page(sbi, page, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001530 f2fs_put_page(page, 1);
1531
1532 nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
Chao Yucfb271d2013-12-05 17:15:22 +08001533 if (unlikely(nid >= nm_i->max_nid))
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001534 nid = 0;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001535
1536 if (i++ == FREE_NID_PAGES)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001537 break;
1538 }
1539
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001540 /* go to the next free nat pages to find free nids abundantly */
1541 nm_i->next_scan_nid = nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001542
1543 /* find free nids from current sum_pages */
1544 mutex_lock(&curseg->curseg_mutex);
1545 for (i = 0; i < nats_in_cursum(sum); i++) {
1546 block_t addr = le32_to_cpu(nat_in_journal(sum, i).block_addr);
1547 nid = le32_to_cpu(nid_in_journal(sum, i));
1548 if (addr == NULL_ADDR)
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001549 add_free_nid(sbi, nid, true);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001550 else
1551 remove_free_nid(nm_i, nid);
1552 }
1553 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001554}
1555
1556/*
1557 * If this function returns success, caller can obtain a new nid
1558 * from second parameter of this function.
1559 * The returned nid could be used ino as well as nid when inode is created.
1560 */
1561bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
1562{
1563 struct f2fs_nm_info *nm_i = NM_I(sbi);
1564 struct free_nid *i = NULL;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001565retry:
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001566 if (unlikely(sbi->total_valid_node_count + 1 > nm_i->available_nids))
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001567 return false;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001568
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001569 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001570
1571 /* We should not use stale free nids created by build_free_nids */
Gu Zhengf978f5a2014-02-21 18:08:29 +08001572 if (nm_i->fcnt && !on_build_free_nids(nm_i)) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001573 f2fs_bug_on(sbi, list_empty(&nm_i->free_nid_list));
Chao Yu2d7b8222014-03-29 11:33:17 +08001574 list_for_each_entry(i, &nm_i->free_nid_list, list)
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001575 if (i->state == NID_NEW)
1576 break;
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001577
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001578 f2fs_bug_on(sbi, i->state != NID_NEW);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001579 *nid = i->nid;
1580 i->state = NID_ALLOC;
1581 nm_i->fcnt--;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001582 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001583 return true;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001584 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001585 spin_unlock(&nm_i->free_nid_list_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001586
1587 /* Let's scan nat pages and its caches to get free nids */
1588 mutex_lock(&nm_i->build_lock);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001589 build_free_nids(sbi);
Jaegeuk Kim55008d82013-04-25 16:05:51 +09001590 mutex_unlock(&nm_i->build_lock);
1591 goto retry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001592}
1593
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001594/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001595 * alloc_nid() should be called prior to this function.
1596 */
1597void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid)
1598{
1599 struct f2fs_nm_info *nm_i = NM_I(sbi);
1600 struct free_nid *i;
1601
1602 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001603 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001604 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001605 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001606 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001607
1608 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001609}
1610
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001611/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001612 * alloc_nid() should be called prior to this function.
1613 */
1614void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid)
1615{
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001616 struct f2fs_nm_info *nm_i = NM_I(sbi);
1617 struct free_nid *i;
Chao Yucf0ee0f2014-04-02 08:55:00 +08001618 bool need_free = false;
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001619
Jaegeuk Kim65985d92013-08-14 21:57:27 +09001620 if (!nid)
1621 return;
1622
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001623 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001624 i = __lookup_free_nid_list(nm_i, nid);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001625 f2fs_bug_on(sbi, !i || i->state != NID_ALLOC);
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001626 if (!available_free_memory(sbi, FREE_NIDS)) {
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001627 __del_from_free_nid_list(nm_i, i);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001628 need_free = true;
Haicheng Li95630cb2013-05-06 23:15:41 +08001629 } else {
1630 i->state = NID_NEW;
1631 nm_i->fcnt++;
1632 }
Jaegeuk Kim49952fa2013-04-03 22:19:03 +09001633 spin_unlock(&nm_i->free_nid_list_lock);
Chao Yucf0ee0f2014-04-02 08:55:00 +08001634
1635 if (need_free)
1636 kmem_cache_free(free_nid_slab, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001637}
1638
Chao Yu70cfed82014-08-02 15:26:04 +08001639void recover_inline_xattr(struct inode *inode, struct page *page)
Chao Yu28cdce02014-03-11 13:37:38 +08001640{
Chao Yu28cdce02014-03-11 13:37:38 +08001641 void *src_addr, *dst_addr;
1642 size_t inline_size;
1643 struct page *ipage;
1644 struct f2fs_inode *ri;
1645
Jaegeuk Kim40813632014-09-02 15:31:18 -07001646 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001647 f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(ipage));
Chao Yu28cdce02014-03-11 13:37:38 +08001648
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001649 ri = F2FS_INODE(page);
1650 if (!(ri->i_inline & F2FS_INLINE_XATTR)) {
1651 clear_inode_flag(F2FS_I(inode), FI_INLINE_XATTR);
1652 goto update_inode;
1653 }
1654
Chao Yu28cdce02014-03-11 13:37:38 +08001655 dst_addr = inline_xattr_addr(ipage);
1656 src_addr = inline_xattr_addr(page);
1657 inline_size = inline_xattr_size(inode);
1658
Jaegeuk Kim54b591d2014-04-29 17:28:32 +09001659 f2fs_wait_on_page_writeback(ipage, NODE);
Chao Yu28cdce02014-03-11 13:37:38 +08001660 memcpy(dst_addr, src_addr, inline_size);
Jaegeuk Kime3b4d432014-08-07 23:45:42 -07001661update_inode:
Chao Yu28cdce02014-03-11 13:37:38 +08001662 update_inode(inode, ipage);
1663 f2fs_put_page(ipage, 1);
1664}
1665
Jaegeuk Kim1c35a902014-08-07 23:49:17 -07001666void recover_xattr_data(struct inode *inode, struct page *page, block_t blkaddr)
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001667{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001668 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001669 nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
1670 nid_t new_xnid = nid_of_node(page);
1671 struct node_info ni;
1672
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001673 /* 1: invalidate the previous xattr nid */
1674 if (!prev_xnid)
1675 goto recover_xnid;
1676
1677 /* Deallocate node address */
1678 get_node_info(sbi, prev_xnid, &ni);
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001679 f2fs_bug_on(sbi, ni.blk_addr == NULL_ADDR);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001680 invalidate_blocks(sbi, ni.blk_addr);
1681 dec_valid_node_count(sbi, inode);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001682 set_node_addr(sbi, &ni, NULL_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001683
1684recover_xnid:
1685 /* 2: allocate new xattr nid */
1686 if (unlikely(!inc_valid_node_count(sbi, inode)))
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001687 f2fs_bug_on(sbi, 1);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001688
1689 remove_free_nid(NM_I(sbi), new_xnid);
1690 get_node_info(sbi, new_xnid, &ni);
1691 ni.ino = inode->i_ino;
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001692 set_node_addr(sbi, &ni, NEW_ADDR, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001693 F2FS_I(inode)->i_xattr_nid = new_xnid;
1694
1695 /* 3: update xattr blkaddr */
1696 refresh_sit_entry(sbi, NEW_ADDR, blkaddr);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001697 set_node_addr(sbi, &ni, blkaddr, false);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001698
1699 update_inode_page(inode);
Jaegeuk Kimabb23662014-01-28 12:25:06 +09001700}
1701
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001702int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
1703{
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001704 struct f2fs_inode *src, *dst;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001705 nid_t ino = ino_of_node(page);
1706 struct node_info old_ni, new_ni;
1707 struct page *ipage;
1708
Jaegeuk Kime8271fa2014-04-18 15:21:04 +09001709 get_node_info(sbi, ino, &old_ni);
1710
1711 if (unlikely(old_ni.blk_addr != NULL_ADDR))
1712 return -EINVAL;
1713
Jaegeuk Kim4ef51a82014-01-21 18:51:16 +09001714 ipage = grab_cache_page(NODE_MAPPING(sbi), ino);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001715 if (!ipage)
1716 return -ENOMEM;
1717
arter97e1c42042014-08-06 23:22:50 +09001718 /* Should not use this inode from free nid list */
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001719 remove_free_nid(NM_I(sbi), ino);
1720
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001721 SetPageUptodate(ipage);
1722 fill_node_footer(ipage, ino, ino, 0, true);
1723
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001724 src = F2FS_INODE(page);
1725 dst = F2FS_INODE(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001726
Jaegeuk Kim58bfaf42013-12-26 16:30:41 +09001727 memcpy(dst, src, (unsigned long)&src->i_ext - (unsigned long)src);
1728 dst->i_size = 0;
1729 dst->i_blocks = cpu_to_le64(1);
1730 dst->i_links = cpu_to_le32(1);
1731 dst->i_xattr_nid = 0;
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001732 dst->i_inline = src->i_inline & F2FS_INLINE_XATTR;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001733
1734 new_ni = old_ni;
1735 new_ni.ino = ino;
1736
Chao Yucfb271d2013-12-05 17:15:22 +08001737 if (unlikely(!inc_valid_node_count(sbi, NULL)))
Jaegeuk Kim65e5cd02013-05-14 15:47:43 +09001738 WARN_ON(1);
Jaegeuk Kim479f40c2014-03-20 21:52:53 +09001739 set_node_addr(sbi, &new_ni, NEW_ADDR, false);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001740 inc_valid_inode_count(sbi);
Jaegeuk Kim617deb82014-08-07 17:04:24 -07001741 set_page_dirty(ipage);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001742 f2fs_put_page(ipage, 1);
1743 return 0;
1744}
1745
1746int restore_node_summary(struct f2fs_sb_info *sbi,
1747 unsigned int segno, struct f2fs_summary_block *sum)
1748{
1749 struct f2fs_node *rn;
1750 struct f2fs_summary *sum_entry;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001751 block_t addr;
Jaegeuk Kim90a893c2014-09-22 16:21:07 -07001752 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Chao Yu9ecf4b82014-12-18 18:29:05 +08001753 int i, idx, last_offset, nrpages;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001754
1755 /* scan the node segment */
1756 last_offset = sbi->blocks_per_seg;
1757 addr = START_BLOCK(sbi, segno);
1758 sum_entry = &sum->entries[0];
1759
Chao Yu9ecf4b82014-12-18 18:29:05 +08001760 for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
Chao Yu9af0ff12013-11-22 15:48:54 +08001761 nrpages = min(last_offset - i, bio_blocks);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001762
arter97e1c42042014-08-06 23:22:50 +09001763 /* readahead node pages */
Chao Yu9ecf4b82014-12-18 18:29:05 +08001764 ra_meta_pages(sbi, addr, nrpages, META_POR);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001765
Chao Yu9ecf4b82014-12-18 18:29:05 +08001766 for (idx = addr; idx < addr + nrpages; idx++) {
1767 struct page *page = get_meta_page(sbi, idx);
Chao Yu9af0ff12013-11-22 15:48:54 +08001768
Chao Yu9ecf4b82014-12-18 18:29:05 +08001769 rn = F2FS_NODE(page);
1770 sum_entry->nid = rn->footer.nid;
1771 sum_entry->version = 0;
1772 sum_entry->ofs_in_node = 0;
1773 sum_entry++;
1774 f2fs_put_page(page, 1);
Chao Yu9af0ff12013-11-22 15:48:54 +08001775 }
Chao Yubac4eef2014-05-27 08:41:07 +08001776
Chao Yu9ecf4b82014-12-18 18:29:05 +08001777 invalidate_mapping_pages(META_MAPPING(sbi), addr,
Chao Yubac4eef2014-05-27 08:41:07 +08001778 addr + nrpages);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001779 }
Chao Yu9ecf4b82014-12-18 18:29:05 +08001780 return 0;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001781}
1782
Chao Yuaec71382014-06-24 09:18:20 +08001783static void remove_nats_in_journal(struct f2fs_sb_info *sbi)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001784{
1785 struct f2fs_nm_info *nm_i = NM_I(sbi);
1786 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1787 struct f2fs_summary_block *sum = curseg->sum_blk;
1788 int i;
1789
1790 mutex_lock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001791 for (i = 0; i < nats_in_cursum(sum); i++) {
1792 struct nat_entry *ne;
1793 struct f2fs_nat_entry raw_ne;
1794 nid_t nid = le32_to_cpu(nid_in_journal(sum, i));
1795
1796 raw_ne = nat_in_journal(sum, i);
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001797
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001798 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001799 ne = __lookup_nat_cache(nm_i, nid);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001800 if (!ne) {
Jaegeuk Kim9be32d72014-12-05 10:39:49 -08001801 ne = grab_nat_entry(nm_i, nid);
1802 node_info_from_raw_nat(&ne->ni, &raw_ne);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001803 }
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001804 __set_nat_cache_dirty(nm_i, ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001805 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001806 }
1807 update_nats_in_cursum(sum, -i);
1808 mutex_unlock(&curseg->curseg_mutex);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001809}
1810
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001811static void __adjust_nat_entry_set(struct nat_entry_set *nes,
1812 struct list_head *head, int max)
1813{
1814 struct nat_entry_set *cur;
1815
1816 if (nes->entry_cnt >= max)
1817 goto add_out;
1818
1819 list_for_each_entry(cur, head, set_list) {
1820 if (cur->entry_cnt >= nes->entry_cnt) {
1821 list_add(&nes->set_list, cur->set_list.prev);
1822 return;
1823 }
1824 }
1825add_out:
1826 list_add_tail(&nes->set_list, head);
1827}
1828
1829static void __flush_nat_entry_set(struct f2fs_sb_info *sbi,
1830 struct nat_entry_set *set)
1831{
1832 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1833 struct f2fs_summary_block *sum = curseg->sum_blk;
1834 nid_t start_nid = set->set * NAT_ENTRY_PER_BLOCK;
1835 bool to_journal = true;
1836 struct f2fs_nat_block *nat_blk;
1837 struct nat_entry *ne, *cur;
1838 struct page *page = NULL;
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001839 struct f2fs_nm_info *nm_i = NM_I(sbi);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001840
1841 /*
1842 * there are two steps to flush nat entries:
1843 * #1, flush nat entries to journal in current hot data summary block.
1844 * #2, flush nat entries to nat page.
1845 */
1846 if (!__has_cursum_space(sum, set->entry_cnt, NAT_JOURNAL))
1847 to_journal = false;
1848
1849 if (to_journal) {
1850 mutex_lock(&curseg->curseg_mutex);
1851 } else {
1852 page = get_next_nat_page(sbi, start_nid);
1853 nat_blk = page_address(page);
1854 f2fs_bug_on(sbi, !nat_blk);
1855 }
1856
1857 /* flush dirty nats in nat entry set */
1858 list_for_each_entry_safe(ne, cur, &set->entry_list, list) {
1859 struct f2fs_nat_entry *raw_ne;
1860 nid_t nid = nat_get_nid(ne);
1861 int offset;
1862
1863 if (nat_get_blkaddr(ne) == NEW_ADDR)
1864 continue;
1865
1866 if (to_journal) {
1867 offset = lookup_journal_in_cursum(sum,
1868 NAT_JOURNAL, nid, 1);
1869 f2fs_bug_on(sbi, offset < 0);
1870 raw_ne = &nat_in_journal(sum, offset);
1871 nid_in_journal(sum, offset) = cpu_to_le32(nid);
1872 } else {
1873 raw_ne = &nat_blk->entries[nid - start_nid];
1874 }
1875 raw_nat_from_node_info(raw_ne, &ne->ni);
1876
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001877 down_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001878 nat_reset_flag(ne);
1879 __clear_nat_cache_dirty(NM_I(sbi), ne);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001880 up_write(&NM_I(sbi)->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001881
1882 if (nat_get_blkaddr(ne) == NULL_ADDR)
1883 add_free_nid(sbi, nid, false);
1884 }
1885
1886 if (to_journal)
1887 mutex_unlock(&curseg->curseg_mutex);
1888 else
1889 f2fs_put_page(page, 1);
1890
Changman Lee80ec2e92014-11-25 12:44:24 +09001891 f2fs_bug_on(sbi, set->entry_cnt);
1892
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001893 down_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001894 radix_tree_delete(&NM_I(sbi)->nat_set_root, set->set);
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001895 up_write(&nm_i->nat_tree_lock);
Changman Lee80ec2e92014-11-25 12:44:24 +09001896 kmem_cache_free(nat_entry_set_slab, set);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001897}
1898
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001899/*
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001900 * This function is called during the checkpointing process.
1901 */
1902void flush_nat_entries(struct f2fs_sb_info *sbi)
1903{
1904 struct f2fs_nm_info *nm_i = NM_I(sbi);
1905 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_HOT_DATA);
1906 struct f2fs_summary_block *sum = curseg->sum_blk;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001907 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001908 struct nat_entry_set *set, *tmp;
1909 unsigned int found;
1910 nid_t set_idx = 0;
1911 LIST_HEAD(sets);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001912
Changman Lee20d047c2014-11-25 12:44:23 +09001913 if (!nm_i->dirty_nat_cnt)
1914 return;
Chao Yuaec71382014-06-24 09:18:20 +08001915 /*
1916 * if there are no enough space in journal to store dirty nat
1917 * entries, remove all entries from journal and merge them
1918 * into nat entry set.
1919 */
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001920 if (!__has_cursum_space(sum, nm_i->dirty_nat_cnt, NAT_JOURNAL))
Chao Yuaec71382014-06-24 09:18:20 +08001921 remove_nats_in_journal(sbi);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001922
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001923 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001924 while ((found = __gang_lookup_nat_set(nm_i,
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08001925 set_idx, SETVEC_SIZE, setvec))) {
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001926 unsigned idx;
1927 set_idx = setvec[found - 1]->set + 1;
1928 for (idx = 0; idx < found; idx++)
1929 __adjust_nat_entry_set(setvec[idx], &sets,
1930 MAX_NAT_JENTRIES(sum));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001931 }
Wanpeng Li57ed1e92015-03-09 11:00:55 +08001932 up_write(&nm_i->nat_tree_lock);
Chao Yuaec71382014-06-24 09:18:20 +08001933
Jaegeuk Kim309cc2b2014-09-22 11:40:48 -07001934 /* flush dirty nats in nat entry set */
1935 list_for_each_entry_safe(set, tmp, &sets, set_list)
1936 __flush_nat_entry_set(sbi, set);
1937
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07001938 f2fs_bug_on(sbi, nm_i->dirty_nat_cnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001939}
1940
1941static int init_node_manager(struct f2fs_sb_info *sbi)
1942{
1943 struct f2fs_super_block *sb_raw = F2FS_RAW_SUPER(sbi);
1944 struct f2fs_nm_info *nm_i = NM_I(sbi);
1945 unsigned char *version_bitmap;
1946 unsigned int nat_segs, nat_blocks;
1947
1948 nm_i->nat_blkaddr = le32_to_cpu(sb_raw->nat_blkaddr);
1949
1950 /* segment_count_nat includes pair segment so divide to 2. */
1951 nat_segs = le32_to_cpu(sb_raw->segment_count_nat) >> 1;
1952 nat_blocks = nat_segs << le32_to_cpu(sb_raw->log_blocks_per_seg);
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001953
Jaegeuk Kim7ee0eea2014-04-18 11:14:37 +09001954 nm_i->max_nid = NAT_ENTRY_PER_BLOCK * nat_blocks;
1955
Jaegeuk Kimb63da152014-02-17 12:44:20 +09001956 /* not used nids: 0, node, meta, (and root counted as valid node) */
Chao Yuc200b1a2014-08-20 18:36:46 +08001957 nm_i->available_nids = nm_i->max_nid - F2FS_RESERVED_NODE_NUM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001958 nm_i->fcnt = 0;
1959 nm_i->nat_cnt = 0;
Jaegeuk Kimcdfc41c2014-03-19 13:31:37 +09001960 nm_i->ram_thresh = DEF_RAM_THRESHOLD;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001961
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09001962 INIT_RADIX_TREE(&nm_i->free_nid_root, GFP_ATOMIC);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001963 INIT_LIST_HEAD(&nm_i->free_nid_list);
Jaegeuk Kim769ec6e2014-12-03 20:47:26 -08001964 INIT_RADIX_TREE(&nm_i->nat_root, GFP_NOIO);
1965 INIT_RADIX_TREE(&nm_i->nat_set_root, GFP_NOIO);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001966 INIT_LIST_HEAD(&nm_i->nat_entries);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001967
1968 mutex_init(&nm_i->build_lock);
1969 spin_lock_init(&nm_i->free_nid_list_lock);
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08001970 init_rwsem(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001971
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001972 nm_i->next_scan_nid = le32_to_cpu(sbi->ckpt->next_free_nid);
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001973 nm_i->bitmap_size = __bitmap_size(sbi, NAT_BITMAP);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001974 version_bitmap = __bitmap_ptr(sbi, NAT_BITMAP);
1975 if (!version_bitmap)
1976 return -EFAULT;
1977
Alexandru Gheorghiu79b57932013-03-28 02:24:53 +02001978 nm_i->nat_bitmap = kmemdup(version_bitmap, nm_i->bitmap_size,
1979 GFP_KERNEL);
1980 if (!nm_i->nat_bitmap)
1981 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09001982 return 0;
1983}
1984
1985int build_node_manager(struct f2fs_sb_info *sbi)
1986{
1987 int err;
1988
1989 sbi->nm_info = kzalloc(sizeof(struct f2fs_nm_info), GFP_KERNEL);
1990 if (!sbi->nm_info)
1991 return -ENOMEM;
1992
1993 err = init_node_manager(sbi);
1994 if (err)
1995 return err;
1996
1997 build_free_nids(sbi);
1998 return 0;
1999}
2000
2001void destroy_node_manager(struct f2fs_sb_info *sbi)
2002{
2003 struct f2fs_nm_info *nm_i = NM_I(sbi);
2004 struct free_nid *i, *next_i;
2005 struct nat_entry *natvec[NATVEC_SIZE];
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002006 struct nat_entry_set *setvec[SETVEC_SIZE];
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002007 nid_t nid = 0;
2008 unsigned int found;
2009
2010 if (!nm_i)
2011 return;
2012
2013 /* destroy free nid list */
2014 spin_lock(&nm_i->free_nid_list_lock);
2015 list_for_each_entry_safe(i, next_i, &nm_i->free_nid_list, list) {
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002016 f2fs_bug_on(sbi, i->state == NID_ALLOC);
Jaegeuk Kim8a7ed662014-02-21 14:29:35 +09002017 __del_from_free_nid_list(nm_i, i);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002018 nm_i->fcnt--;
Chao Yucf0ee0f2014-04-02 08:55:00 +08002019 spin_unlock(&nm_i->free_nid_list_lock);
2020 kmem_cache_free(free_nid_slab, i);
2021 spin_lock(&nm_i->free_nid_list_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002022 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002023 f2fs_bug_on(sbi, nm_i->fcnt);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002024 spin_unlock(&nm_i->free_nid_list_lock);
2025
2026 /* destroy nat cache */
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002027 down_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002028 while ((found = __gang_lookup_nat_cache(nm_i,
2029 nid, NATVEC_SIZE, natvec))) {
2030 unsigned idx;
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002031
Gu Zhengb6ce3912014-03-07 18:43:24 +08002032 nid = nat_get_nid(natvec[found - 1]) + 1;
2033 for (idx = 0; idx < found; idx++)
2034 __del_from_nat_cache(nm_i, natvec[idx]);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002035 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -07002036 f2fs_bug_on(sbi, nm_i->nat_cnt);
Jaegeuk Kim7aed0d42015-01-07 10:47:57 -08002037
2038 /* destroy nat set cache */
2039 nid = 0;
2040 while ((found = __gang_lookup_nat_set(nm_i,
2041 nid, SETVEC_SIZE, setvec))) {
2042 unsigned idx;
2043
2044 nid = setvec[found - 1]->set + 1;
2045 for (idx = 0; idx < found; idx++) {
2046 /* entry_cnt is not zero, when cp_error was occurred */
2047 f2fs_bug_on(sbi, !list_empty(&setvec[idx]->entry_list));
2048 radix_tree_delete(&nm_i->nat_set_root, setvec[idx]->set);
2049 kmem_cache_free(nat_entry_set_slab, setvec[idx]);
2050 }
2051 }
Jaegeuk Kim8b26ef92014-12-03 21:15:10 -08002052 up_write(&nm_i->nat_tree_lock);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002053
2054 kfree(nm_i->nat_bitmap);
2055 sbi->nm_info = NULL;
2056 kfree(nm_i);
2057}
2058
Namjae Jeon6e6093a2013-01-17 00:08:30 +09002059int __init create_node_manager_caches(void)
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002060{
2061 nat_entry_slab = f2fs_kmem_cache_create("nat_entry",
Gu Zhenge8512d22014-03-07 18:43:28 +08002062 sizeof(struct nat_entry));
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002063 if (!nat_entry_slab)
Chao Yuaec71382014-06-24 09:18:20 +08002064 goto fail;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002065
2066 free_nid_slab = f2fs_kmem_cache_create("free_nid",
Gu Zhenge8512d22014-03-07 18:43:28 +08002067 sizeof(struct free_nid));
Chao Yuaec71382014-06-24 09:18:20 +08002068 if (!free_nid_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002069 goto destroy_nat_entry;
Chao Yuaec71382014-06-24 09:18:20 +08002070
2071 nat_entry_set_slab = f2fs_kmem_cache_create("nat_entry_set",
2072 sizeof(struct nat_entry_set));
2073 if (!nat_entry_set_slab)
Markus Elfringce3e6d22014-11-24 15:52:00 +01002074 goto destroy_free_nid;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002075 return 0;
Chao Yuaec71382014-06-24 09:18:20 +08002076
Markus Elfringce3e6d22014-11-24 15:52:00 +01002077destroy_free_nid:
Chao Yuaec71382014-06-24 09:18:20 +08002078 kmem_cache_destroy(free_nid_slab);
Markus Elfringce3e6d22014-11-24 15:52:00 +01002079destroy_nat_entry:
Chao Yuaec71382014-06-24 09:18:20 +08002080 kmem_cache_destroy(nat_entry_slab);
2081fail:
2082 return -ENOMEM;
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002083}
2084
2085void destroy_node_manager_caches(void)
2086{
Chao Yuaec71382014-06-24 09:18:20 +08002087 kmem_cache_destroy(nat_entry_set_slab);
Jaegeuk Kime05df3b2012-11-02 17:08:50 +09002088 kmem_cache_destroy(free_nid_slab);
2089 kmem_cache_destroy(nat_entry_slab);
2090}